blob: f2f541e79f34e770badbf5d2abdda3f5161f7da7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
Bram Moolenaar30613902019-12-01 22:11:18 +010013// Structure containing all the GUI information
Bram Moolenaar071d4272004-06-13 20:20:40 +000014gui_T gui;
15
K.Takata94373c42022-01-27 15:04:22 +000016#if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
17# define USE_SET_GUIFONTWIDE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010018static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010020static void gui_check_pos(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020021static void gui_reset_scroll_region(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010022static void gui_outstr(char_u *, int);
23static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020024static int gui_outstr_nowrap(char_u *s, int len, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static void gui_delete_lines(int row, int count);
26static void gui_insert_lines(int row, int count);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020027static int gui_xy2colrow(int x, int y, int *colp);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000028#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010029static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000030#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010031static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010032static void gui_update_horiz_scrollbar(int);
33static void gui_set_fg_color(char_u *name);
34static void gui_set_bg_color(char_u *name);
Bram Moolenaar0630bb62019-11-04 22:52:12 +010035static win_T *xy2win(int x, int y, mouse_find_T popup);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020037#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010038static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020039
Bram Moolenaard25c16e2016-01-29 22:13:30 +010040static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020041
Bram Moolenaar30613902019-12-01 22:11:18 +010042// Return values for gui_read_child_pipe
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020043enum {
44 GUI_CHILD_IO_ERROR,
45 GUI_CHILD_OK,
46 GUI_CHILD_FAILED
47};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020048#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020049
Bram Moolenaard25c16e2016-01-29 22:13:30 +010050static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020051
Bram Moolenaar30613902019-12-01 22:11:18 +010052static int can_update_cursor = TRUE; // can display the cursor
53static int disable_flush = 0; // If > 0, gui_mch_flush() is disabled.
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 * gui_start -- Called when user wants to start the GUI.
57 *
58 * Careful: This function can be called recursively when there is a ":gui"
59 * command in the .gvimrc file. Only the first call should fork, not the
60 * recursive call.
61 */
62 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020063gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
65 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020067#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020068 char *msg = NULL;
69#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71 old_term = vim_strsave(T_NAME);
72
Bram Moolenaar30613902019-12-01 22:11:18 +010073 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010075 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 full_screen = FALSE;
77
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 ++recursive;
79
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020080#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020081 /*
82 * Quit the current process and continue in the child.
83 * Makes "gvim file" disconnect from the shell it was started in.
84 * Don't do this when Vim was started with "-f" or the 'f' flag is present
85 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020086 * Don't do this when there is a running job, we can only get the status
87 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020088 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020089 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
90# ifdef FEAT_JOB_CHANNEL
91 && !job_any_running()
92# endif
93 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020094 {
95 gui_do_fork();
96 }
97 else
98#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +020099#ifdef GUI_MAY_SPAWN
100 if (gui.dospawn
101# ifdef EXPERIMENTAL_GUI_CMD
102 && gui.dofork
103# endif
104 && !vim_strchr(p_go, GO_FORG)
105 && !anyBufIsChanged()
106# ifdef FEAT_JOB_CHANNEL
107 && !job_any_running()
108# endif
109 )
110 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200111# ifdef EXPERIMENTAL_GUI_CMD
112 msg =
113# endif
114 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200115 }
116 else
117#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200118 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200119#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100120 // If there is 'f' in 'guioptions' and specify -g argument,
121 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200122 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100123 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200124#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200125 gui_attempt_start();
126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
Bram Moolenaar30613902019-12-01 22:11:18 +0100128 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100130 // Back to old term settings
131 //
132 // FIXME: If we got here because a child process failed and flagged to
Bram Moolenaar651fca82021-11-29 20:39:38 +0000133 // the parent to resume, and X11 is enabled, this will
Bram Moolenaar30613902019-12-01 22:11:18 +0100134 // hit an X11 I/O error and do a longjmp(), leaving recursive
135 // permanently set to 1. This is probably not as big a problem as it
136 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
137 // return "OK" unconditionally, so it would be very difficult to
138 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200139 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100140 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar30613902019-12-01 22:11:18 +0100141 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200142#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
143 if (msg)
144 emsg(msg);
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 }
147
148 vim_free(old_term);
149
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 // If the GUI started successfully, trigger the GUIEnter event, otherwise
151 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200152 gui_mch_update();
153 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
154 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200155 --recursive;
156}
157
158/*
159 * Set_termname() will call gui_init() to start the GUI.
160 * Set the "starting" flag, to indicate that the GUI will start.
161 *
162 * We don't want to open the GUI shell until after we've read .gvimrc,
163 * otherwise we don't know what font we will use, and hence we don't know
164 * what size the shell should be. So if there are errors in the .gvimrc
165 * file, they will have to go to the terminal: Set full_screen to FALSE.
166 * full_screen will be set to TRUE again by a successful termcapinit().
167 */
168 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100169gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200170{
171 static int recursive = 0;
172
173 ++recursive;
174 gui.starting = TRUE;
175
176#ifdef FEAT_GUI_GTK
177 gui.event_time = GDK_CURRENT_TIME;
178#endif
179
180 termcapinit((char_u *)"builtin_gui");
181 gui.starting = recursive - 1;
182
Bram Moolenaar9372a112005-12-06 19:59:18 +0000183#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200185 {
186# ifdef FEAT_EVAL
187 Window x11_window;
188 Display *x11_display;
189
190 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
191 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
192# endif
193
Bram Moolenaar30613902019-12-01 22:11:18 +0100194 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 --recursive;
199}
200
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200201#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200202
Bram Moolenaar30613902019-12-01 22:11:18 +0100203// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200204# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
205# include <sys/wait.h>
206# endif
207
208/*
209 * Create a new process, by forking. In the child, start the GUI, and in
210 * the parent, exit.
211 *
212 * If something goes wrong, this will return with gui.in_use still set
213 * to FALSE, in which case the caller should continue execution without
214 * the GUI.
215 *
216 * If the child fails to start the GUI, then the child will exit and the
217 * parent will return. If the child succeeds, then the parent will exit
218 * and the child will return.
219 */
220 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100221gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200222{
Bram Moolenaar30613902019-12-01 22:11:18 +0100223 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200224 int pipe_error;
225 int status;
226 int exit_status;
227 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200228
Bram Moolenaar30613902019-12-01 22:11:18 +0100229 // Setup a pipe between the child and the parent, so that the parent
230 // knows when the child has done the setsid() call and is allowed to
231 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200232 pipe_error = (pipe(pipefd) < 0);
233 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100234 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200235 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000236 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200237 return;
238 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100239 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200240 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100241 // Give the child some time to do the setsid(), otherwise the
242 // exit() may kill the child too (when starting gvim from inside a
243 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200244 if (!pipe_error)
245 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100246 // The read returns when the child closes the pipe (or when
247 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200248 close(pipefd[1]);
249 status = gui_read_child_pipe(pipefd[0]);
250 if (status == GUI_CHILD_FAILED)
251 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100252 // The child failed to start the GUI, so the caller must
253 // continue. There may be more error information written
254 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200255# ifdef __NeXT__
256 wait4(pid, &exit_status, 0, (struct rusage *)0);
257# else
258 waitpid(pid, &exit_status, 0);
259# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000260 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200261 return;
262 }
263 else if (status == GUI_CHILD_IO_ERROR)
264 {
265 pipe_error = TRUE;
266 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100267 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200268 }
269
270 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100271 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200272
Bram Moolenaar30613902019-12-01 22:11:18 +0100273 // When swapping screens we may need to go to the next line, e.g.,
274 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200275 if (newline_on_exit)
276 mch_errmsg("\r\n");
277
278 /*
279 * The parent must skip the normal exit() processing, the child
280 * will do it. For example, GTK messes up signals when exiting.
281 */
282 _exit(0);
283 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100284 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200285
K.Takata6e1d31e2022-02-03 13:05:32 +0000286# ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100287 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200288 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100289 getout_preserve_modified(1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000290# endif
Bram Moolenaar29695702012-05-18 17:03:18 +0200291
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200292# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
293 /*
294 * Change our process group. On some systems/shells a CTRL-C in the
295 * shell where Vim was started would otherwise kill gvim!
296 */
297# if defined(HAVE_SETSID)
298 (void)setsid();
299# else
300 (void)setpgid(0, 0);
301# endif
302# endif
303 if (!pipe_error)
304 close(pipefd[0]);
305
306# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100307 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200308 gui_mch_forked();
309# endif
310
Bram Moolenaar30613902019-12-01 22:11:18 +0100311 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200312 gui_attempt_start();
313
Bram Moolenaar30613902019-12-01 22:11:18 +0100314 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200315 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200316 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200317 if (gui.in_use)
318 write_eintr(pipefd[1], "ok", 3);
319 else
320 write_eintr(pipefd[1], "fail", 5);
321 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200322 }
323
Bram Moolenaar30613902019-12-01 22:11:18 +0100324 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200325 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100326 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200327}
328
329/*
330 * Read from a pipe assumed to be connected to the child process (this
331 * function is called from the parent).
332 * Return GUI_CHILD_OK if the child successfully started the GUI,
333 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
334 * some other error.
335 *
336 * The file descriptor will be closed before the function returns.
337 */
338 static int
339gui_read_child_pipe(int fd)
340{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200341 long bytes_read;
K.Takata6e1d31e2022-02-03 13:05:32 +0000342# define READ_BUFFER_SIZE 10
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200343 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200344
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200345 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000346# undef READ_BUFFER_SIZE
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200347 close(fd);
348 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200349 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200350 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200351 if (strcmp(buffer, "ok") == 0)
352 return GUI_CHILD_OK;
353 return GUI_CHILD_FAILED;
354}
355
Bram Moolenaar30613902019-12-01 22:11:18 +0100356#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358/*
359 * Call this when vim starts up, whether or not the GUI is started
360 */
361 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100362gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
Bram Moolenaar30613902019-12-01 22:11:18 +0100364 gui.in_use = FALSE; // No GUI yet (maybe later)
365 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 gui_mch_prepare(argc, argv);
367}
368
369/*
370 * Try initializing the GUI and check if it can be started.
371 * Used from main() to check early if "vim -g" can start the GUI.
372 * Used from gui_init() to prepare for starting the GUI.
373 * Returns FAIL or OK.
374 */
375 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100376gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377{
378 static int result = MAYBE;
379
380 if (result != MAYBE)
381 {
382 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000383 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 return result;
385 }
386
387 gui.shell_created = FALSE;
388 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100389 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 gui.dragged_sb = SBAR_NONE;
391 gui.dragged_wp = NULL;
392 gui.pointer_hidden = FALSE;
393 gui.col = 0;
394 gui.row = 0;
395 gui.num_cols = Columns;
396 gui.num_rows = Rows;
397
398 gui.cursor_is_valid = FALSE;
399 gui.scroll_region_top = 0;
400 gui.scroll_region_bot = Rows - 1;
401 gui.scroll_region_left = 0;
402 gui.scroll_region_right = Columns - 1;
403 gui.highlight_mask = HL_NORMAL;
404 gui.char_width = 1;
405 gui.char_height = 1;
406 gui.char_ascent = 0;
407 gui.border_width = 0;
408
409 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200410#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 gui.bold_font = NOFONT;
412 gui.ital_font = NOFONT;
413 gui.boldital_font = NOFONT;
414# ifdef FEAT_XFONTSET
415 gui.fontset = NOFONTSET;
416# endif
417#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200418 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100419#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200420 gui.wide_bold_font = NOFONT;
421 gui.wide_ital_font = NOFONT;
422 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
425#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200426# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427# ifdef FONTSET_ALWAYS
428 gui.menu_fontset = NOFONTSET;
429# else
430 gui.menu_font = NOFONT;
431# endif
432# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100433 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434# ifndef FEAT_GUI_GTK
435 gui.menu_height = MENU_DEFAULT_HEIGHT;
436 gui.menu_width = 0;
437# endif
438#endif
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100439#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 gui.toolbar_height = 0;
441#endif
442#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
443 gui.footer_height = 0;
444#endif
445#ifdef FEAT_BEVAL_TIP
446 gui.tooltip_fontset = NOFONTSET;
447#endif
448
449 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
450 gui.prev_wrap = -1;
451
K.Takata6e1d31e2022-02-03 13:05:32 +0000452#ifdef FEAT_GUI_GTK
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100453 CLEAR_FIELD(gui.ligatures_map);
454#endif
455
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200456#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 result = OK;
458#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200459# ifdef FEAT_GUI_GTK
460 /*
461 * Note: Don't call gtk_init_check() before fork, it will be called after
462 * the fork. When calling it before fork, it make vim hang for a while.
463 * See gui_do_fork().
464 * Use a simpler check if the GUI window can probably be opened.
465 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200466 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200467# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470#endif
471 return result;
472}
473
474/*
475 * This is the call which starts the GUI.
476 */
477 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100478gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 win_T *wp;
481 static int recursive = 0;
482
483 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000484 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * function will then be executed at the first call, the rest by the
486 * recursive call. This allow the shell to be opened halfway reading a
487 * gvimrc file.
488 */
489 if (!recursive)
490 {
491 ++recursive;
492
493 clip_init(TRUE);
494
Bram Moolenaar30613902019-12-01 22:11:18 +0100495 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 if (gui_init_check() == FAIL)
497 {
498 --recursive;
499 clip_init(FALSE);
500 return;
501 }
502
503 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000504 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
505 * breaks the Paste toolbar button.
506 */
507 set_option_value((char_u *)"paste", 0L, NULL, 0);
508
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200509 // Set t_Co to the number of colors: RGB.
510 set_color_count(256 * 256 * 256);
511
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000512 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 * Set up system-wide default menus.
514 */
515#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
516 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
517 {
518 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 sys_menu = FALSE;
521 }
522#endif
523
524 /*
525 * Switch on the mouse by default, unless the user changed it already.
526 * This can then be changed in the .gvimrc.
527 */
528 if (!option_was_set((char_u *)"mouse"))
529 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000530 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 /*
533 * If -U option given, use only the initializations from that file and
534 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
535 */
536 if (use_gvimrc != NULL)
537 {
538 if (STRCMP(use_gvimrc, "NONE") != 0
539 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000541 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
543 else
544 {
545 /*
546 * Get system wide defaults for gvim, only when file name defined.
547 */
548#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#endif
551
552 /*
553 * Try to read GUI initialization commands from the following
554 * places:
555 * - environment variable GVIMINIT
556 * - the user gvimrc file (~/.gvimrc)
557 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
558 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
559 * The first that exists is used, the rest is ignored.
560 */
561 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000562 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000565 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200568#ifdef USR_GVIMRC_FILE3
569 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 )
573 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200574#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
576 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
578 }
579
580 /*
581 * Read initialization commands from ".gvimrc" in current
582 * directory. This is only done if the 'exrc' option is set.
583 * Because of security reasons we disallow shell and write
584 * commands now, except for unix if the file is owned by the user
585 * or 'secure' option has been reset in environment of global
586 * ".gvimrc".
587 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
588 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
589 */
590 if (p_exrc)
591 {
592#ifdef UNIX
593 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200594 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
Bram Moolenaar30613902019-12-01 22:11:18 +0100596 // if ".gvimrc" file is not owned by user, set 'secure'
597 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
599 secure = p_secure;
600 }
601#else
602 secure = p_secure;
603#endif
604
605 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200606 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef SYS_GVIMRC_FILE
608 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200609 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611#ifdef USR_GVIMRC_FILE2
612 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200613 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615#ifdef USR_GVIMRC_FILE3
616 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200617 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200619#ifdef USR_GVIMRC_FILE4
620 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200621 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 if (secure == 2)
627 need_wait_return = TRUE;
628 secure = 0;
629 }
630 }
631
632 if (need_wait_return || msg_didany)
633 wait_return(TRUE);
634
635 --recursive;
636 }
637
Bram Moolenaar30613902019-12-01 22:11:18 +0100638 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (gui.in_use)
640 return;
641
642 /*
643 * Create the GUI shell.
644 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100645 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (gui_mch_init() == FAIL)
647 goto error;
648
Bram Moolenaar30613902019-12-01 22:11:18 +0100649 // Avoid a delay for an error message that was printed in the terminal
650 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 emsg_on_display = FALSE;
652 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100653 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 need_wait_return = FALSE;
655 msg_didany = FALSE;
656
657 /*
658 * Check validity of any generic resources that may have been loaded.
659 */
660 if (gui.border_width < 0)
661 gui.border_width = 0;
662
663 /*
664 * Set up the fonts. First use a font specified with "-fn" or "-font".
665 */
666 if (font_argument != NULL)
667 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
668 if (
669#ifdef FEAT_XFONTSET
670 (*p_guifontset == NUL
671 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
672#endif
673 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
674 : p_guifont, FALSE) == FAIL)
675 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000676 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 goto error2;
678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000680 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 gui.num_cols = Columns;
683 gui.num_rows = Rows;
684 gui_reset_scroll_region();
685
Bram Moolenaar30613902019-12-01 22:11:18 +0100686 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 FOR_ALL_WINDOWS(wp)
688 {
689 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
690 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
691 }
692 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
693
694#ifdef FEAT_MENU
695 gui_create_initial_menus(root_menu);
696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef FEAT_SIGN_ICONS
698 sign_gui_started();
699#endif
700
Bram Moolenaar30613902019-12-01 22:11:18 +0100701 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 gui_init_which_components(NULL);
703
Bram Moolenaar30613902019-12-01 22:11:18 +0100704 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 gui.shell_created = TRUE;
706
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100707#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100708 // Set the shell size, adjusted for the screen size. For GTK this only
709 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100710 // If the window is already maximized (e.g. when --windowid is passed in),
711 // we want to use the system-provided dimensions by passing FALSE to
712 // mustset. Otherwise, we want to initialize with the default rows/columns.
713 if (gui_mch_maximized())
714 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
715 else
716 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100717#else
718# ifndef FEAT_GUI_GTK
719 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100723 // Need to set the size of the menubar after all the menus have been
724 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 gui_mch_compute_menu_height((Widget)0);
726#endif
727
728 /*
729 * Actually open the GUI shell.
730 */
731 if (gui_mch_open() != FAIL)
732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 maketitle();
734 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 init_gui_options();
737#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100738 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 p_tbidi = FALSE;
740#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000741#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100742 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000744
745# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100746 // If there is no 'm' in 'guioptions' we need to remove the menu now.
747 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000748 if (vim_strchr(p_go, GO_MENUS) == NULL)
749 {
750 --gui.starting;
751 gui_mch_enable_menu(FALSE);
752 ++gui.starting;
753 gui_mch_update();
754 }
755# endif
756
Bram Moolenaar30613902019-12-01 22:11:18 +0100757 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100758 if (gui_mch_maximized())
759 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
760 else
761 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100763 // When 'lines' was set while starting up the topframe may have to be
764 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000765 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000766
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100767#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100768 // Always create the Balloon Evaluation area, but disable it when
769 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100770 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200771 {
772# ifdef FEAT_VARTABS
773 vim_free(balloonEval->vts);
774# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100775 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200776 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100777 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000778# ifdef FEAT_GUI_GTK
779 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
780 &general_beval_cb, NULL);
781# else
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100782# if defined(FEAT_GUI_MOTIF)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000783 {
784 extern Widget textArea;
785 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000786 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000787 }
788# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100789# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000790 balloonEval = gui_mch_create_beval_area(NULL, NULL,
791 &general_beval_cb, NULL);
792# endif
793# endif
794# endif
795 if (!p_beval)
796 gui_mch_disable_beval_area(balloonEval);
797#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200798
799#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200800 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200801 // Don't do this for MS-Windows yet, it sends CTRL-K without the
802 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200803 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200804#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
807 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000808 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100810 // When 'cmdheight' was set during startup it may not have taken
811 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000812 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000813 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 return;
816 }
817
818error2:
819#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100820 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 gui_mch_uninit();
822#endif
823
824error:
825 gui.in_use = FALSE;
826 clip_init(FALSE);
827}
828
829
830 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100831gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaar30613902019-12-01 22:11:18 +0100833 // don't free the fonts, it leads to a BUS error
834 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 gui.in_use = FALSE;
837 gui_mch_exit(rc);
838}
839
Bram Moolenaar9372a112005-12-06 19:59:18 +0000840#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200841 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000842# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843/*
844 * Called when the GUI shell is closed by the user. If there are no changed
845 * files Vim exits, otherwise there will be a dialog to ask the user what to
846 * do.
847 * When this function returns, Vim should NOT exit!
848 */
849 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100850gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200852 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaar3552e742021-05-29 12:21:58 +0200854 if (before_quit_autocmds(curwin, TRUE, FALSE))
855 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaar30613902019-12-01 22:11:18 +0100857 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 exiting = TRUE;
859# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200860 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861# endif
862# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200863 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100865 // If there are changed buffers, present the user with a dialog if
866 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100867 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 getout(0);
869
870 exiting = FALSE;
871 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100872 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874#endif
875
876/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200877 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * first font name that works is used. If none is found, use the default
879 * font.
880 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
881 * Return OK when able to set the font. When it failed FAIL is returned and
882 * the fonts are unchanged.
883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100885gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887#define FONTLEN 320
888 char_u font_name[FONTLEN];
889 int font_list_empty = FALSE;
890 int ret = FAIL;
891
892 if (!gui.in_use)
893 return FAIL;
894
895 font_name[0] = NUL;
896 if (*font_list == NUL)
897 font_list_empty = TRUE;
898 else
899 {
900#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100901 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (fontset)
903 ret = gui_mch_init_font(font_list, TRUE);
904 else
905#endif
906 while (*font_list != NUL)
907 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100908 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
910
Bram Moolenaar30613902019-12-01 22:11:18 +0100911 // Careful!!! The Win32 version of gui_mch_init_font(), when
912 // called with "*" will change p_guifont to the selected font
913 // name, which frees the old value. This makes font_list
914 // invalid. Thus when OK is returned here, font_list must no
915 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (gui_mch_init_font(font_name, FALSE) == OK)
917 {
K.Takata94373c42022-01-27 15:04:22 +0000918#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100919 // If it's a Unicode font, try setting 'guifontwide' to a
920 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
922 && strstr((char *)font_name, "10646") != NULL)
923 set_guifontwide(font_name);
924#endif
925 ret = OK;
926 break;
927 }
928 }
929 }
930
931 if (ret != OK
932 && STRCMP(font_list, "*") != 0
933 && (font_list_empty || gui.norm_font == NOFONT))
934 {
935 /*
936 * Couldn't load any font in 'font_list', keep the current font if
937 * there is one. If 'font_list' is empty, or if there is no current
938 * font, tell gui_mch_init_font() to try to find a font we can load.
939 */
940 ret = gui_mch_init_font(NULL, FALSE);
941 }
942
943 if (ret == OK)
944 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200945#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100946 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947# ifdef FEAT_XFONTSET
948 if (gui.fontset != NOFONTSET)
949 gui_mch_set_fontset(gui.fontset);
950 else
951# endif
952 gui_mch_set_font(gui.norm_font);
953#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100954 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956
957 return ret;
958}
959
K.Takata94373c42022-01-27 15:04:22 +0000960#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961/*
962 * Try setting 'guifontwide' to a font twice as wide as "name".
963 */
964 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100965set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100968 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 char_u *wp = NULL;
970 char_u *p;
971 GuiFont font;
972
973 wp = wide_name;
974 for (p = name; *p != NUL; ++p)
975 {
976 *wp++ = *p;
977 if (*p == '-')
978 {
979 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100980 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982 if (p[1] == '-')
983 *wp++ = '*';
984 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100985 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
987 ++p;
988 i = getdigits(&p);
989 if (i != 0)
990 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100991 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 sprintf((char *)wp, "%d%s", i * 2, p);
993 font = gui_mch_get_font(wide_name, FALSE);
994 if (font != NOFONT)
995 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000996 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 gui.wide_font = font;
998 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000999 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
1001 }
1002 break;
1003 }
1004 }
1005 }
1006}
K.Takata94373c42022-01-27 15:04:22 +00001007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009/*
1010 * Get the font for 'guifontwide'.
1011 * Return FAIL for an invalid font name.
1012 */
1013 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001014gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
1016 GuiFont font = NOFONT;
1017 char_u font_name[FONTLEN];
1018 char_u *p;
1019
Bram Moolenaar30613902019-12-01 22:11:18 +01001020 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1021 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1024 {
1025 for (p = p_guifontwide; *p != NUL; )
1026 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001027 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1029 font = gui_mch_get_font(font_name, FALSE);
1030 if (font != NOFONT)
1031 break;
1032 }
1033 if (font == NOFONT)
1034 return FAIL;
1035 }
1036
1037 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001038#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001039 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (font != NOFONT && gui.norm_font != NOFONT
1041 && pango_font_description_equal(font, gui.norm_font))
1042 {
1043 gui.wide_font = NOFONT;
1044 gui_mch_free_font(font);
1045 }
1046 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001049#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001050 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001051#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001052 /*
1053 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1054 * support those fonts for 'guifontwide'.
1055 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 return OK;
1058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001060#if defined(FEAT_GUI_GTK) || defined(PROTO)
1061/*
1062 * Set list of ascii characters that combined can create ligature.
1063 * Store them in char map for quick access from gui_gtk2_draw_string.
1064 */
1065 void
1066gui_set_ligatures(void)
1067{
1068 char_u *p;
1069
1070 if (*p_guiligatures != NUL)
1071 {
1072 // check for invalid characters
1073 for (p = p_guiligatures; *p != NUL; ++p)
1074 if (*p < 32 || *p > 127)
1075 {
1076 emsg(_(e_ascii_code_not_in_range));
1077 return;
1078 }
1079
1080 // store valid setting into ligatures_map
1081 CLEAR_FIELD(gui.ligatures_map);
1082 for (p = p_guiligatures; *p != NUL; ++p)
1083 gui.ligatures_map[*p] = 1;
1084 }
1085 else
1086 CLEAR_FIELD(gui.ligatures_map);
1087}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001088
1089/*
1090 * Adjust the columns to undraw for when the cursor is on ligatures.
1091 */
1092 static void
1093gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1094{
1095 int off;
1096
1097 if (ScreenLines == NULL || *p_guiligatures == NUL)
1098 return;
1099
1100 // expand before the cursor for all the chars in gui.ligatures_map
1101 off = LineOffset[gui.cursor_row] + *startcol;
1102 if (gui.ligatures_map[ScreenLines[off]])
1103 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1104 (*startcol)--;
1105
1106 // expand after the cursor for all the chars in gui.ligatures_map
1107 off = LineOffset[gui.cursor_row] + *endcol;
1108 if (gui.ligatures_map[ScreenLines[off]])
1109 while (*endcol < ((int)screen_Columns - 1)
1110 && gui.ligatures_map[ScreenLines[++off]])
1111 (*endcol)++;
1112}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001113#endif
1114
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001115 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001116gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 gui.row = row;
1119 gui.col = col;
1120}
1121
1122/*
1123 * gui_check_pos - check if the cursor is on the screen.
1124 */
1125 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001126gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 if (gui.row >= screen_Rows)
1129 gui.row = screen_Rows - 1;
1130 if (gui.col >= screen_Columns)
1131 gui.col = screen_Columns - 1;
1132 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1133 gui.cursor_is_valid = FALSE;
1134}
1135
1136/*
1137 * Redraw the cursor if necessary or when forced.
1138 * Careful: The contents of ScreenLines[] must match what is on the screen,
1139 * otherwise this goes wrong. May need to call out_flush() first.
1140 */
1141 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001142gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001143 int force, // when TRUE, update even when not moved
1144 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
1146 int cur_width = 0;
1147 int cur_height = 0;
1148 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001149 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001151#ifdef FEAT_TERMINAL
1152 guicolor_T shape_fg = INVALCOLOR;
1153 guicolor_T shape_bg = INVALCOLOR;
1154#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001155 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1156 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 int attr;
1158 attrentry_T *aep = NULL;
1159
Bram Moolenaar30613902019-12-01 22:11:18 +01001160 // Don't update the cursor when halfway busy scrolling or the screen size
1161 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001162 if (!can_update_cursor || screen_Columns != gui.num_cols
1163 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 return;
1165
1166 gui_check_pos();
1167 if (!gui.cursor_is_valid || force
1168 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1169 {
1170 gui_undraw_cursor();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001171
1172 // If a cursor-less sleep is ongoing, leave the cursor invisible
1173 if (cursor_is_sleeping())
1174 return;
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (gui.row < 0)
1177 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001178#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1180 im_set_position(gui.row, gui.col);
1181#endif
1182 gui.cursor_row = gui.row;
1183 gui.cursor_col = gui.col;
1184
Bram Moolenaar30613902019-12-01 22:11:18 +01001185 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (!screen_cleared || ScreenLines == NULL)
1187 return;
1188
Bram Moolenaar30613902019-12-01 22:11:18 +01001189 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (clear_selection)
1191 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001192 // Check that the cursor is inside the shell (resizing may have made
1193 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1195 return;
1196
1197 gui.cursor_is_valid = TRUE;
1198
1199 /*
1200 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001201 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001203#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001204 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001205 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001207#endif
1208 shape = &shape_table[get_shape_idx(FALSE)];
1209 if (State & LANGMAP)
1210 id = shape->id_lm;
1211 else
1212 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Bram Moolenaar30613902019-12-01 22:11:18 +01001214 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 cfg = INVALCOLOR;
1216 cbg = INVALCOLOR;
1217 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001218 gui_mch_set_blinking(shape->blinkwait,
1219 shape->blinkon,
1220 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001221 if (shape->blinkwait == 0 || shape->blinkon == 0
1222 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001223 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001224#ifdef FEAT_TERMINAL
1225 if (shape_bg != INVALCOLOR)
1226 {
1227 cattr = 0;
1228 cfg = shape_fg;
1229 cbg = shape_bg;
1230 }
1231 else
1232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (id > 0)
1234 {
1235 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001236#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
1238 static int iid;
1239 guicolor_T fg, bg;
1240
Bram Moolenaarc236c162008-07-13 17:41:49 +00001241 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001242# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001243 preedit_get_status()
1244# else
1245 im_get_status()
1246# endif
1247 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
1249 iid = syn_name2id((char_u *)"CursorIM");
1250 if (iid > 0)
1251 {
1252 syn_id2colors(iid, &fg, &bg);
1253 if (bg != INVALCOLOR)
1254 cbg = bg;
1255 if (fg != INVALCOLOR)
1256 cfg = fg;
1257 }
1258 }
1259 }
1260#endif
1261 }
1262
1263 /*
1264 * Get the attributes for the character under the cursor.
1265 * When no cursor color was given, use the character color.
1266 */
1267 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1268 if (attr > HL_ALL)
1269 aep = syn_gui_attr2entry(attr);
1270 if (aep != NULL)
1271 {
1272 attr = aep->ae_attr;
1273 if (cfg == INVALCOLOR)
1274 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1275 : aep->ae_u.gui.fg_color);
1276 if (cbg == INVALCOLOR)
1277 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1278 : aep->ae_u.gui.bg_color);
1279 }
1280 if (cfg == INVALCOLOR)
1281 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1282 if (cbg == INVALCOLOR)
1283 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1284
1285#ifdef FEAT_XIM
1286 if (aep != NULL)
1287 {
1288 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1289 : aep->ae_u.gui.bg_color);
1290 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1291 : aep->ae_u.gui.fg_color);
1292 if (xim_bg_color == INVALCOLOR)
1293 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1294 : gui.back_pixel;
1295 if (xim_fg_color == INVALCOLOR)
1296 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1297 : gui.norm_pixel;
1298 }
1299 else
1300 {
1301 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1302 : gui.back_pixel;
1303 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1304 : gui.norm_pixel;
1305 }
1306#endif
1307
1308 attr &= ~HL_INVERSE;
1309 if (cattr & HL_INVERSE)
1310 {
1311 cc = cbg;
1312 cbg = cfg;
1313 cfg = cc;
1314 }
1315 cattr &= ~HL_INVERSE;
1316
1317 /*
1318 * When we don't have window focus, draw a hollow cursor.
1319 */
1320 if (!gui.in_focus)
1321 {
1322 gui_mch_draw_hollow_cursor(cbg);
1323 return;
1324 }
1325
1326 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001327 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 {
1329 /*
1330 * Draw the text character with the cursor colors. Use the
1331 * character attributes plus the cursor attributes.
1332 */
1333 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001334 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1336 }
1337 else
1338 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001339#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 int col_off = FALSE;
1341#endif
1342 /*
1343 * First draw the partial cursor, then overwrite with the text
1344 * character, using a transparent background.
1345 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001346 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001349 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351 else
1352 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001353 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 cur_width = gui.char_width;
1355 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001356 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1357 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001359 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001360 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001362#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (CURSOR_BAR_RIGHT)
1364 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001365 // gui.col points to the left half of the character but
1366 // the vertical line needs to be on the right half.
Bram Moolenaar30613902019-12-01 22:11:18 +01001367 // A double-wide horizontal line is also drawn from the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001368 // right half in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 col_off = TRUE;
1370 ++gui.col;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001375#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (col_off)
1377 --gui.col;
1378#endif
1379
Bram Moolenaar30613902019-12-01 22:11:18 +01001380#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1382 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1383 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1384 (guicolor_T)0, (guicolor_T)0, 0);
1385#endif
1386 }
1387 gui.highlight_mask = old_hl_mask;
1388 }
1389}
1390
1391#if defined(FEAT_MENU) || defined(PROTO)
1392 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001393gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001395# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (gui.menu_is_active && gui.in_use)
1397 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1398# endif
1399}
1400#endif
1401
1402/*
1403 * Position the various GUI components (text area, menu). The vertical
1404 * scrollbars are NOT handled here. See gui_update_scrollbars().
1405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001407gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 int text_area_x;
1410 int text_area_y;
1411 int text_area_width;
1412 int text_area_height;
1413
Bram Moolenaar30613902019-12-01 22:11:18 +01001414 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 ++hold_gui_events;
1416
1417 text_area_x = 0;
1418 if (gui.which_scrollbars[SBAR_LEFT])
1419 text_area_x += gui.scrollbar_width;
1420
1421 text_area_y = 0;
1422#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1423 gui.menu_width = total_width;
1424 if (gui.menu_is_active)
1425 text_area_y += gui.menu_height;
1426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
K.Takata6e1d31e2022-02-03 13:05:32 +00001428#if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001429 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001430 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001431 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001432#endif
1433
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001434#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001435 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1437 {
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001438# if defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 gui_mch_set_toolbar_pos(0, text_area_y,
1440 gui.menu_width, gui.toolbar_height);
1441# endif
1442 text_area_y += gui.toolbar_height;
1443 }
1444#endif
1445
K.Takata6e1d31e2022-02-03 13:05:32 +00001446#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001447 gui_mch_set_tabline_pos(0, text_area_y,
1448 gui.menu_width, gui.tabline_height);
1449 if (gui_has_tabline())
1450 text_area_y += gui.tabline_height;
1451#endif
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1454 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1455
1456 gui_mch_set_text_area_pos(text_area_x,
1457 text_area_y,
1458 text_area_width,
1459 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001460#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 + xim_get_status_area_height()
1462#endif
1463 );
1464#ifdef FEAT_MENU
1465 gui_position_menu();
1466#endif
1467 if (gui.which_scrollbars[SBAR_BOTTOM])
1468 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1469 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001470 text_area_y + text_area_height
1471 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 text_area_width,
1473 gui.scrollbar_height);
1474 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001475 gui.right_sbar_x = text_area_x + text_area_width
1476 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 --hold_gui_events;
1479}
1480
Bram Moolenaar02743632005-07-25 20:42:36 +00001481/*
1482 * Get the width of the widgets and decorations to the side of the text area.
1483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001485gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 int base_width;
1488
1489 base_width = 2 * gui.border_offset;
1490 if (gui.which_scrollbars[SBAR_LEFT])
1491 base_width += gui.scrollbar_width;
1492 if (gui.which_scrollbars[SBAR_RIGHT])
1493 base_width += gui.scrollbar_width;
1494 return base_width;
1495}
1496
Bram Moolenaar02743632005-07-25 20:42:36 +00001497/*
1498 * Get the height of the widgets and decorations above and below the text area.
1499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001501gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 int base_height;
1504
1505 base_height = 2 * gui.border_offset;
1506 if (gui.which_scrollbars[SBAR_BOTTOM])
1507 base_height += gui.scrollbar_height;
1508#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001509 // We can't take the sizes properly into account until anything is
1510 // realized. Therefore we recalculate all the values here just before
1511 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512#else
1513# ifdef FEAT_MENU
1514 if (gui.menu_is_active)
1515 base_height += gui.menu_height;
1516# endif
1517# ifdef FEAT_TOOLBAR
1518 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001521# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001522 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001523 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001524 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526# ifdef FEAT_FOOTER
1527 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1528 base_height += gui.footer_height;
1529# endif
1530# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1531 base_height += gui_mch_text_area_extra_height();
1532# endif
1533#endif
1534 return base_height;
1535}
1536
1537/*
1538 * Should be called after the GUI shell has been resized. Its arguments are
1539 * the new width and height of the shell in pixels.
1540 */
1541 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001542gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 static int busy = FALSE;
1545
Bram Moolenaar30613902019-12-01 22:11:18 +01001546 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 return;
1548
1549 /*
1550 * Can't resize the screen while it is being redrawn. Remember the new
1551 * size and handle it later.
1552 */
1553 if (updating_screen || busy)
1554 {
1555 new_pixel_width = pixel_width;
1556 new_pixel_height = pixel_height;
1557 return;
1558 }
1559
1560again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001561 new_pixel_width = 0;
1562 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 busy = TRUE;
1564
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001565#ifdef FEAT_GUI_HAIKU
1566 vim_lock_screen();
1567#endif
1568
Bram Moolenaar30613902019-12-01 22:11:18 +01001569 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 out_flush();
1571
1572 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001573 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 /*
1579 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1580 * at the last line here (why does it have to be one row too low?).
1581 */
1582 if (State == ASKMORE || State == CONFIRM)
1583 gui.row = gui.num_rows;
1584
Bram Moolenaar30613902019-12-01 22:11:18 +01001585 // Only comparing Rows and Columns may be sufficient, but let's stay on
1586 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1588 || gui.num_rows != Rows || gui.num_cols != Columns)
1589 shell_resized();
1590
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001591#ifdef FEAT_GUI_HAIKU
1592 vim_unlock_screen();
1593#endif
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 gui_update_scrollbars(TRUE);
1596 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001597#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 xim_set_status_area();
1599#endif
1600
1601 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001602
Bram Moolenaar30613902019-12-01 22:11:18 +01001603 // We may have been called again while redrawing the screen.
1604 // Need to do it all again with the latest size then. But only if the size
1605 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (new_pixel_height)
1607 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001608 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1609 {
1610 new_pixel_width = 0;
1611 new_pixel_height = 0;
1612 }
1613 else
1614 {
1615 pixel_width = new_pixel_width;
1616 pixel_height = new_pixel_height;
1617 goto again;
1618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620}
1621
1622/*
1623 * Check if gui_resize_shell() must be called.
1624 */
1625 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001626gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001629 // careful: gui_resize_shell() may postpone the resize again if we
1630 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001631 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632}
1633
1634 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001635gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 Rows = gui.num_rows;
1638 Columns = gui.num_cols;
1639 return OK;
1640}
1641
1642/*
1643 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001644 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1645 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001646 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1647 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001650gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001651 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001652 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001653 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655 int base_width;
1656 int base_height;
1657 int width;
1658 int height;
1659 int min_width;
1660 int min_height;
1661 int screen_w;
1662 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001663#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001664 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001665 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001666#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001667 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 if (!gui.shell_created)
1670 return;
1671
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001672#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001673 // If not setting to a user specified size and maximized, calculate the
1674 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001675 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1676 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
1678 gui_mch_newfont();
1679 return;
1680 }
1681#endif
1682
1683 base_width = gui_get_base_width();
1684 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001685 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001686 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001687 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001688
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001689 width = Columns * gui.char_width + base_width;
1690 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 if (fit_to_display)
1693 {
1694 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001695 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 Columns = (screen_w - base_width) / gui.char_width;
1698 if (Columns < MIN_COLUMNS)
1699 Columns = MIN_COLUMNS;
1700 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001701#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001702 ++did_adjust;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001705 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 Rows = (screen_h - base_height) / gui.char_height;
1708 check_shellsize();
1709 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001710#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001711 ++did_adjust;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001714#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001715 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1716 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001717 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001718 un_maximize = FALSE;
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001721 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 gui.num_cols = Columns;
1723 gui.num_rows = Rows;
1724
1725 min_width = base_width + MIN_COLUMNS * gui.char_width;
1726 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001727 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001728
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001729#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001730 if (un_maximize)
1731 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001732 // If the window size is smaller than the screen unmaximize the
1733 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001734 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1735 if ((width + gui.char_width < screen_w
1736 || height + gui.char_height * 2 < screen_h)
1737 && gui_mch_maximized())
1738 gui_mch_unmaximize();
1739 }
1740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001743 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001745 if (fit_to_display && x >= 0 && y >= 0)
1746 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001747 // Some window managers put the Vim window left of/above the screen.
1748 // Only change the position if it wasn't already negative before
1749 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 gui_mch_update();
1751 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1752 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1753 }
1754
1755 gui_position_components(width);
1756 gui_update_scrollbars(TRUE);
1757 gui_reset_scroll_region();
1758}
1759
1760/*
1761 * Called when Rows and/or Columns has changed.
1762 */
1763 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001764gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 gui_reset_scroll_region();
1767}
1768
1769/*
1770 * Make scroll region cover whole screen.
1771 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001772 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001773gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 gui.scroll_region_top = 0;
1776 gui.scroll_region_bot = gui.num_rows - 1;
1777 gui.scroll_region_left = 0;
1778 gui.scroll_region_right = gui.num_cols - 1;
1779}
1780
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001781 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001782gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
Bram Moolenaar30613902019-12-01 22:11:18 +01001784 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001786 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 gui.highlight_mask |= mask;
1788}
1789
1790 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001791gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
Bram Moolenaar30613902019-12-01 22:11:18 +01001793 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001795 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 gui.highlight_mask &= ~mask;
1797}
1798
1799/*
1800 * Clear a rectangular region of the screen from text pos (row1, col1) to
1801 * (row2, col2) inclusive.
1802 */
1803 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001804gui_clear_block(
1805 int row1,
1806 int col1,
1807 int row2,
1808 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
Bram Moolenaar30613902019-12-01 22:11:18 +01001810 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 clip_may_clear_selection(row1, row2);
1812
1813 gui_mch_clear_block(row1, col1, row2, col2);
1814
Bram Moolenaar30613902019-12-01 22:11:18 +01001815 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1817 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1818 gui.cursor_is_valid = FALSE;
1819}
1820
1821/*
1822 * Write code to update the cursor later. This avoids the need to flush the
1823 * output buffer before calling gui_update_cursor().
1824 */
1825 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001826gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001828 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829}
1830
1831 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001832gui_write(
1833 char_u *s,
1834 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 char_u *p;
1837 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001838 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int force_scrollbar = FALSE;
1840 static win_T *old_curwin = NULL;
1841
Bram Moolenaar30613902019-12-01 22:11:18 +01001842// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843#ifdef DEBUG_GUI_WRITE
1844 {
1845 int i;
1846 char_u *str;
1847
1848 printf("gui_write(%d):\n ", len);
1849 for (i = 0; i < len; i++)
1850 if (s[i] == ESC)
1851 {
1852 if (i != 0)
1853 printf("\n ");
1854 printf("<ESC>");
1855 }
1856 else
1857 {
1858 str = transchar_byte(s[i]);
1859 if (str[0] && str[1])
1860 printf("<%s>", (char *)str);
1861 else
1862 printf("%s", (char *)str);
1863 }
1864 printf("\n");
1865 }
1866#endif
1867 while (len)
1868 {
1869 if (s[0] == ESC && s[1] == '|')
1870 {
1871 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001872 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874 arg1 = getdigits(&p);
1875 if (p > s + len)
1876 break;
1877 if (*p == ';')
1878 {
1879 ++p;
1880 arg2 = getdigits(&p);
1881 if (p > s + len)
1882 break;
1883 }
1884 }
1885 switch (*p)
1886 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001887 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 clip_scroll_selection(9999);
1889 gui_mch_clear_all();
1890 gui.cursor_is_valid = FALSE;
1891 force_scrollbar = TRUE;
1892 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001893 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 gui_set_cursor(arg1, arg2);
1895 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001896 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 force_cursor = TRUE;
1898 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001899 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (arg1 < arg2)
1901 {
1902 gui.scroll_region_top = arg1;
1903 gui.scroll_region_bot = arg2;
1904 }
1905 else
1906 {
1907 gui.scroll_region_top = arg2;
1908 gui.scroll_region_bot = arg1;
1909 }
1910 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001911 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (arg1 < arg2)
1913 {
1914 gui.scroll_region_left = arg1;
1915 gui.scroll_region_right = arg2;
1916 }
1917 else
1918 {
1919 gui.scroll_region_left = arg2;
1920 gui.scroll_region_right = arg1;
1921 }
1922 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001923 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 gui_delete_lines(gui.row, 1);
1925 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001926 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 gui_delete_lines(gui.row, arg1);
1928 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001929 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 gui_insert_lines(gui.row, 1);
1931 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001932 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 gui_insert_lines(gui.row, arg1);
1934 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001935 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 gui_clear_block(gui.row, gui.col, gui.row,
1937 (int)Columns - 1);
1938 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001939 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 gui_start_highlight(arg1);
1941 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001942 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 gui_stop_highlight(arg1);
1944 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001945 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1947 break;
1948 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001949 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 break;
1951 }
1952 len -= (int)(++p - s);
1953 s = p;
1954 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001955 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956#ifdef FEAT_SIGN_ICONS
1957 && s[0] != SIGN_BYTE
1958# ifdef FEAT_NETBEANS_INTG
1959 && s[0] != MULTISIGN_BYTE
1960# endif
1961#endif
1962 )
1963 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001964 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 gui.col = 0;
1967 if (gui.row < gui.scroll_region_bot)
1968 gui.row++;
1969 else
1970 gui_delete_lines(gui.scroll_region_top, 1);
1971 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001972 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 gui.col = 0;
1975 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001976 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 if (gui.col)
1979 --gui.col;
1980 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001981 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 ++gui.col;
1984 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001985 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 gui_mch_beep();
1988 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001989 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
Bram Moolenaar30613902019-12-01 22:11:18 +01001991 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 ++s;
1993 }
1994 else
1995 {
1996 p = s;
1997 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#ifdef FEAT_SIGN_ICONS
2000 || *p == SIGN_BYTE
2001# ifdef FEAT_NETBEANS_INTG
2002 || *p == MULTISIGN_BYTE
2003# endif
2004#endif
2005 ))
2006 {
2007 len--;
2008 p++;
2009 }
2010 gui_outstr(s, (int)(p - s));
2011 s = p;
2012 }
2013 }
2014
Bram Moolenaar30613902019-12-01 22:11:18 +01002015 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2016 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (force_cursor)
2018 gui_update_cursor(TRUE, TRUE);
2019
Bram Moolenaar30613902019-12-01 22:11:18 +01002020 // When switching to another window the dragging must have stopped.
2021 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (old_curwin != curwin)
2023 gui.dragged_sb = SBAR_NONE;
2024
Bram Moolenaar30613902019-12-01 22:11:18 +01002025 // Update the scrollbars after clearing the screen or when switched
2026 // to another window.
2027 // Update the horizontal scrollbar always, it's difficult to check all
2028 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (force_scrollbar || old_curwin != curwin)
2030 gui_update_scrollbars(force_scrollbar);
2031 else
2032 gui_update_horiz_scrollbar(FALSE);
2033 old_curwin = curwin;
2034
2035 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002036 * We need to make sure this is cleared since GTK doesn't tell us when
2037 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002039#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 gui.dragged_sb = SBAR_NONE;
2041#endif
2042
Bram Moolenaar30613902019-12-01 22:11:18 +01002043 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044}
2045
2046/*
2047 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2048 * produces wrong results. Call gui_dont_update_cursor() before that code and
2049 * gui_can_update_cursor() afterwards.
2050 */
2051 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002052gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 if (gui.in_use)
2055 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002056 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02002057 if (undraw)
2058 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 can_update_cursor = FALSE;
2060 }
2061}
2062
2063 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002064gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002067 // No need to update the cursor right now, there is always more output
2068 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
Bram Moolenaara338adc2018-01-31 20:51:47 +01002071/*
2072 * Disable issuing gui_mch_flush().
2073 */
2074 void
2075gui_disable_flush(void)
2076{
2077 ++disable_flush;
2078}
2079
2080/*
2081 * Enable issuing gui_mch_flush().
2082 */
2083 void
2084gui_enable_flush(void)
2085{
2086 --disable_flush;
2087}
2088
2089/*
2090 * Issue gui_mch_flush() if it is not disabled.
2091 */
2092 void
2093gui_may_flush(void)
2094{
2095 if (disable_flush == 0)
2096 gui_mch_flush();
2097}
2098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002100gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101{
2102 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 if (len == 0)
2106 return;
2107
2108 if (len < 0)
2109 len = (int)STRLEN(s);
2110
2111 while (len > 0)
2112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 if (has_mbyte)
2114 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002115 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 cells = 0;
2117 for (this_len = 0; this_len < len; )
2118 {
2119 cells += (*mb_ptr2cells)(s + this_len);
2120 if (gui.col + cells > Columns)
2121 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002122 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002125 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 }
2127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (gui.col + len > Columns)
2129 this_len = Columns - gui.col;
2130 else
2131 this_len = len;
2132
2133 (void)gui_outstr_nowrap(s, this_len,
2134 0, (guicolor_T)0, (guicolor_T)0, 0);
2135 s += this_len;
2136 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002137 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (len > 0 && gui.col < Columns)
2139 (void)gui_outstr_nowrap((char_u *)" ", 1,
2140 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002141 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (gui.col >= Columns)
2143 {
2144 gui.col = 0;
2145 gui.row++;
2146 }
2147 }
2148}
2149
2150/*
2151 * Output one character (may be one or two display cells).
2152 * Caller must check for valid "off".
2153 * Returns FAIL or OK, just like gui_outstr_nowrap().
2154 */
2155 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002156gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002157 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002158 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002159 guicolor_T fg, // colors for cursor
2160 guicolor_T bg, // colors for cursor
2161 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 char_u buf[MB_MAXBYTES + 1];
2164
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002165 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (enc_utf8 && ScreenLines[off] == 0)
2167 return OK;
2168
2169 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002170 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2172 flags, fg, bg, back);
2173
2174 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2175 {
2176 buf[0] = ScreenLines[off];
2177 buf[1] = ScreenLines2[off];
2178 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2179 }
2180
Bram Moolenaar30613902019-12-01 22:11:18 +01002181 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002183 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185}
2186
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002187#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188/*
2189 * Output the string at the given screen position. This is used in place
2190 * of gui_screenchar() where possible because Pango needs as much context
2191 * as possible to work nicely. It's a lot faster as well.
2192 */
2193 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002194gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002195 int off, // Offset from start of screen
2196 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002197 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002198 guicolor_T fg, // colors for cursor
2199 guicolor_T bg, // colors for cursor
2200 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
2202 char_u *buf;
2203 int outlen = 0;
2204 int i;
2205 int retval;
2206
Bram Moolenaar30613902019-12-01 22:11:18 +01002207 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 return OK;
2209
2210 if (enc_utf8)
2211 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002212 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002214 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216 for (i = off; i < off + len; ++i)
2217 {
2218 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002219 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 if (ScreenLinesUC[i] == 0)
2222 buf[outlen++] = ScreenLines[i];
2223 else
2224 outlen += utfc_char2bytes(i, buf + outlen);
2225 }
2226
Bram Moolenaar30613902019-12-01 22:11:18 +01002227 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2229 vim_free(buf);
2230
2231 return retval;
2232 }
2233 else if (enc_dbcs == DBCS_JPNU)
2234 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002235 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002237 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239 for (i = off; i < off + len; ++i)
2240 {
2241 buf[outlen++] = ScreenLines[i];
2242
Bram Moolenaar30613902019-12-01 22:11:18 +01002243 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (ScreenLines[i] == 0x8e)
2245 buf[outlen++] = ScreenLines2[i];
2246 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2247 buf[outlen++] = ScreenLines[++i];
2248 }
2249
Bram Moolenaar30613902019-12-01 22:11:18 +01002250 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2252 vim_free(buf);
2253
2254 return retval;
2255 }
2256 else
2257 {
2258 return gui_outstr_nowrap(&ScreenLines[off], len,
2259 flags, fg, bg, back);
2260 }
2261}
Bram Moolenaar30613902019-12-01 22:11:18 +01002262#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264/*
2265 * Output the given string at the current cursor position. If the string is
2266 * too long to fit on the line, then it is truncated.
2267 * "flags":
2268 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2269 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002270 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * background.
2272 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2273 * it.
2274 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2275 * FAIL (the caller should start drawing "back" chars back).
2276 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002277 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002278gui_outstr_nowrap(
2279 char_u *s,
2280 int len,
2281 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002282 guicolor_T fg, // colors for cursor
2283 guicolor_T bg, // colors for cursor
2284 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 long_u highlight_mask;
2287 long_u hl_mask_todo;
2288 guicolor_T fg_color;
2289 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002290 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002291#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002293 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294# ifdef FEAT_XFONTSET
2295 GuiFontset fontset = NOFONTSET;
2296# endif
2297#endif
2298 attrentry_T *aep = NULL;
2299 int draw_flags;
2300 int col = gui.col;
2301#ifdef FEAT_SIGN_ICONS
2302 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002303 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002304 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305# ifdef FEAT_NETBEANS_INTG
2306 int multi_sign = FALSE;
2307# endif
2308#endif
2309
2310 if (len < 0)
2311 len = (int)STRLEN(s);
2312 if (len == 0)
2313 return OK;
2314
2315#ifdef FEAT_SIGN_ICONS
2316 if (*s == SIGN_BYTE
2317# ifdef FEAT_NETBEANS_INTG
2318 || *s == MULTISIGN_BYTE
2319# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002320 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322# ifdef FEAT_NETBEANS_INTG
2323 if (*s == MULTISIGN_BYTE)
2324 multi_sign = TRUE;
2325# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002326 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002327 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2328 (curwin->w_p_nu || curwin->w_p_rnu))
2329 {
2330 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2331 s = extra;
2332 }
2333 else
2334 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (len == 1 && col > 0)
2336 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002337 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002338 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002339 // right align sign icon in the number column
2340 signcol = col + len - 3;
2341 else
2342 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 draw_sign = TRUE;
2344 highlight_mask = 0;
2345 }
2346 else
2347#endif
2348 if (gui.highlight_mask > HL_ALL)
2349 {
2350 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002351 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 highlight_mask = 0;
2353 else
2354 highlight_mask = aep->ae_attr;
2355 }
2356 else
2357 highlight_mask = gui.highlight_mask;
2358 hl_mask_todo = highlight_mask;
2359
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002360#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002361 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2363 font = aep->ae_u.gui.font;
2364# ifdef FEAT_XFONTSET
2365 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2366 fontset = aep->ae_u.gui.fontset;
2367# endif
2368 else
2369 {
2370# ifdef FEAT_XFONTSET
2371 if (gui.fontset != NOFONTSET)
2372 fontset = gui.fontset;
2373 else
2374# endif
2375 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2376 {
2377 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2378 {
2379 font = gui.boldital_font;
2380 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2381 }
2382 else if (gui.bold_font != NOFONT)
2383 {
2384 font = gui.bold_font;
2385 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2386 }
2387 else
2388 font = gui.norm_font;
2389 }
2390 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2391 {
2392 font = gui.ital_font;
2393 hl_mask_todo &= ~HL_ITALIC;
2394 }
2395 else
2396 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002397
Bram Moolenaardb250522013-06-17 22:43:25 +02002398 /*
2399 * Choose correct wide_font by font. wide_font should be set with font
2400 * at same time in above block. But it will make many "ifdef" nasty
2401 * blocks. So we do it here.
2402 */
2403 if (font == gui.boldital_font && gui.wide_boldital_font)
2404 wide_font = gui.wide_boldital_font;
2405 else if (font == gui.bold_font && gui.wide_bold_font)
2406 wide_font = gui.wide_bold_font;
2407 else if (font == gui.ital_font && gui.wide_ital_font)
2408 wide_font = gui.wide_ital_font;
2409 else if (font == gui.norm_font && gui.wide_font)
2410 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412# ifdef FEAT_XFONTSET
2413 if (fontset != NOFONTSET)
2414 gui_mch_set_fontset(fontset);
2415 else
2416# endif
2417 gui_mch_set_font(font);
2418#endif
2419
2420 draw_flags = 0;
2421
Bram Moolenaar30613902019-12-01 22:11:18 +01002422 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 bg_color = gui.back_pixel;
2424 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2425 {
2426 draw_flags |= DRAW_CURSOR;
2427 fg_color = fg;
2428 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002429 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 else if (aep != NULL)
2432 {
2433 fg_color = aep->ae_u.gui.fg_color;
2434 if (fg_color == INVALCOLOR)
2435 fg_color = gui.norm_pixel;
2436 bg_color = aep->ae_u.gui.bg_color;
2437 if (bg_color == INVALCOLOR)
2438 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002439 sp_color = aep->ae_u.gui.sp_color;
2440 if (sp_color == INVALCOLOR)
2441 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002446 sp_color = fg_color;
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 gui_mch_set_fg_color(bg_color);
2452 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
2454 else
2455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 gui_mch_set_fg_color(fg_color);
2457 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002459 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
Bram Moolenaar30613902019-12-01 22:11:18 +01002461 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (!(flags & GUI_MON_NOCLEAR))
2463 clip_may_clear_selection(gui.row, gui.row);
2464
2465
Bram Moolenaar30613902019-12-01 22:11:18 +01002466 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2468 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 /*
2471 * When drawing bold or italic characters the spill-over from the left
2472 * neighbor may be destroyed. Let the caller backup to start redrawing
2473 * just after a blank.
2474 */
2475 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2476 return FAIL;
2477
Bram Moolenaare60acc12011-05-10 16:41:25 +02002478#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002479 // If there's no italic font, then fake it.
2480 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (hl_mask_todo & HL_ITALIC)
2482 draw_flags |= DRAW_ITALIC;
2483
Bram Moolenaar30613902019-12-01 22:11:18 +01002484 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (hl_mask_todo & HL_UNDERLINE)
2486 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002489 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002490 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 draw_flags |= DRAW_UNDERL;
2492#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002493 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002494 if (hl_mask_todo & HL_UNDERCURL)
2495 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaar30613902019-12-01 22:11:18 +01002497 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002498 if (hl_mask_todo & HL_STRIKETHROUGH)
2499 draw_flags |= DRAW_STRIKE;
2500
Bram Moolenaar30613902019-12-01 22:11:18 +01002501 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (flags & GUI_MON_TRS_CURSOR)
2503 draw_flags |= DRAW_TRANSP;
2504
2505 /*
2506 * Draw the text.
2507 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002508#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002509 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2511#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (enc_utf8)
2513 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002514 int start; // index of bytes to be drawn
2515 int cells; // cellwidth of bytes to be drawn
2516 int thislen; // length of bytes to be drawn
2517 int cn; // cellwidth of current char
2518 int i; // index of current char
2519 int c; // current char value
2520 int cl; // byte length of current char
2521 int comping; // current char is composing
2522 int scol = col; // screen column
2523 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002524 int prev_wide = FALSE;
2525 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002526# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002527 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002528# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002529 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaar30613902019-12-01 22:11:18 +01002532 // Break the string at a composing character, it has to be drawn on
2533 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 start = 0;
2535 cells = 0;
2536 for (i = 0; i < len; i += cl)
2537 {
2538 c = utf_ptr2char(s + i);
2539 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002541 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002543 if (!comping || sep_comp)
2544 {
2545 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002546# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002547 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002548# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002549 && wide_font != NOFONT)
2550 curr_wide = TRUE;
2551 else
2552 curr_wide = FALSE;
2553 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002554 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002555 if (cl == 0) // hit end of string
2556 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar45933962013-01-23 17:43:57 +01002558 wide_changed = curr_wide != prev_wide;
2559
Bram Moolenaar30613902019-12-01 22:11:18 +01002560 // Print the string so far if it's the last character or there is
2561 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002562 if (i + cl >= len || (comping && sep_comp && i > start)
2563 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002564# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002566# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002567 // No fontset: At least draw char after wide char at
2568 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002571 )
2572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 )
2574 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002575 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 thislen = i - start;
2577 else
2578 thislen = i - start + cl;
2579 if (thislen > 0)
2580 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002581 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002582 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2584 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002585 if (prev_wide)
2586 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 start += thislen;
2588 }
2589 scol += cells;
2590 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002591 // Adjust to not draw a character which width is changed
2592 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002593 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002595 scol -= cn;
2596 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598
Bram Moolenaar13505972019-01-24 15:04:48 +01002599# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002600 // No fontset: draw a space to fill the gap after a wide char
2601 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002603# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002605# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002606 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2608 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002611 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002612 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002614# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002615 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002616 gui_mch_draw_string(gui.row, scol, s + i, cl,
2617 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002618# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002619 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2620 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 start = i + cl;
2623 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002624 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002626 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 len = scol - col;
2628 }
2629 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (enc_dbcs == DBCS_JPNU)
2633 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002634 // Get the length in display cells, this can be different from the
2635 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002636 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002639#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2642 gui.col = col + len;
2643
Bram Moolenaar30613902019-12-01 22:11:18 +01002644 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (flags & GUI_MON_NOCLEAR)
2646 clip_may_redraw_selection(gui.row, col, len);
2647
2648 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2649 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002650 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (gui.cursor_row == gui.row
2652 && gui.cursor_col >= col
2653 && gui.cursor_col < col + len)
2654 gui.cursor_is_valid = FALSE;
2655 }
2656
2657#ifdef FEAT_SIGN_ICONS
2658 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002659 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002660 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002661# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002662 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (multi_sign)
2664 netbeans_draw_multisign_indicator(gui.row);
2665# endif
2666#endif
2667
2668 return OK;
2669}
2670
2671/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002672 * Undraw the cursor. This actually redraws the character at the cursor
2673 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 */
2675 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002676gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 if (gui.cursor_is_valid)
2679 {
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002680 // Always redraw the character just before if there is one, because
2681 // with some fonts and characters there can be a one pixel overlap.
2682 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2683 int endcol = gui.cursor_col;
2684
2685#ifdef FEAT_GUI_GTK
2686 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
2687#endif
2688 gui_redraw_block(gui.cursor_row, startcol,
2689 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002690
Bram Moolenaar54612582019-11-21 17:13:31 +01002691 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2692 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 gui.cursor_is_valid = FALSE;
2694 }
2695}
2696
2697 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002698gui_redraw(
2699 int x,
2700 int y,
2701 int w,
2702 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 int row1, col1, row2, col2;
2705
2706 row1 = Y_2_ROW(y);
2707 col1 = X_2_COL(x);
2708 row2 = Y_2_ROW(y + h - 1);
2709 col2 = X_2_COL(x + w - 1);
2710
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002711 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 /*
2714 * We may need to redraw the cursor, but don't take it upon us to change
2715 * its location after a scroll.
2716 * (maybe be more strict even and test col too?)
2717 * These things may be outside the update/clipping region and reality may
2718 * not reflect Vims internal ideas if these operations are clipped away.
2719 */
2720 if (gui.row == gui.cursor_row)
2721 gui_update_cursor(TRUE, TRUE);
2722}
2723
2724/*
2725 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2726 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002728 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002729gui_redraw_block(
2730 int row1,
2731 int col1,
2732 int row2,
2733 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002734 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 int old_row, old_col;
2737 long_u old_hl_mask;
2738 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002739 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 int idx, len;
2741 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
Bram Moolenaar30613902019-12-01 22:11:18 +01002744 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002746 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar30613902019-12-01 22:11:18 +01002748 // Don't try to draw outside the shell!
2749 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 col1 = check_col(col1);
2751 col2 = check_col(col2);
2752 row1 = check_row(row1);
2753 row2 = check_row(row2);
2754
Bram Moolenaar30613902019-12-01 22:11:18 +01002755 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 old_row = gui.row;
2757 old_col = gui.col;
2758 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 orig_col1 = col1;
2760 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 for (gui.row = row1; gui.row <= row2; gui.row++)
2763 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002764 // When only half of a double-wide character is in the block, include
2765 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 col1 = orig_col1;
2767 col2 = orig_col2;
2768 off = LineOffset[gui.row];
2769 if (enc_dbcs != 0)
2770 {
2771 if (col1 > 0)
2772 col1 -= dbcs_screen_head_off(ScreenLines + off,
2773 ScreenLines + off + col1);
2774 col2 += dbcs_screen_tail_off(ScreenLines + off,
2775 ScreenLines + off + col2);
2776 }
2777 else if (enc_utf8)
2778 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002779 if (ScreenLines[off + col1] == 0)
2780 {
2781 if (col1 > 0)
2782 --col1;
2783 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002784 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002785 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002786 // Make this IEMSGN when it no longer breaks Travis CI.
2787 vim_snprintf((char *)IObuff, IOSIZE,
2788 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2789 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002790 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002791 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002792 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002793#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2795 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 gui.col = col1;
2799 off = LineOffset[gui.row] + gui.col;
2800 len = col2 - col1 + 1;
2801
Bram Moolenaar30613902019-12-01 22:11:18 +01002802 // Find how many chars back this highlighting starts, or where a space
2803 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 for (back = 0; back < col1; ++back)
2805 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2806 || ScreenLines[off - 1 - back] == ' ')
2807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
Bram Moolenaar30613902019-12-01 22:11:18 +01002809 // Break it up in strings of characters with the same attributes.
2810 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 while (len > 0)
2812 {
2813 first_attr = ScreenAttrs[off];
2814 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002815#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (enc_utf8 && ScreenLinesUC[off] != 0)
2817 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002818 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 nback = gui_screenchar(off, flags,
2820 (guicolor_T)0, (guicolor_T)0, back);
2821 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2822 idx = 2;
2823 else
2824 idx = 1;
2825 }
2826 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2827 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002828 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 nback = gui_screenchar(off, flags,
2830 (guicolor_T)0, (guicolor_T)0, back);
2831 idx = 1;
2832 }
2833 else
2834#endif
2835 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002836#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 for (idx = 0; idx < len; ++idx)
2838 {
2839 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002840 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (ScreenAttrs[off + idx] != first_attr)
2842 break;
2843 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002844 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 nback = gui_screenstr(off, idx, flags,
2846 (guicolor_T)0, (guicolor_T)0, back);
2847#else
2848 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2849 idx++)
2850 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002851 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2853 break;
2854 if (enc_dbcs == DBCS_JPNU)
2855 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002856 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (ScreenLines[off + idx] == 0x8e)
2858 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002859 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002861 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2865 (guicolor_T)0, (guicolor_T)0, back);
2866#endif
2867 }
2868 if (nback == FAIL)
2869 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002870 // Must back up to start drawing where a bold or italic word
2871 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 off -= back;
2873 len += back;
2874 gui.col -= back;
2875 }
2876 else
2877 {
2878 off += idx;
2879 len -= idx;
2880 }
2881 back = 0;
2882 }
2883 }
2884
Bram Moolenaar30613902019-12-01 22:11:18 +01002885 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 gui.row = old_row;
2887 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002888 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889}
2890
2891 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002892gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 if (count <= 0)
2895 return;
2896
2897 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002898 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 gui_clear_block(row, gui.scroll_region_left,
2900 gui.scroll_region_bot, gui.scroll_region_right);
2901 else
2902 {
2903 gui_mch_delete_lines(row, count);
2904
Bram Moolenaar30613902019-12-01 22:11:18 +01002905 // If the cursor was in the deleted lines it's now gone. If the
2906 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 if (gui.cursor_row >= row
2908 && gui.cursor_col >= gui.scroll_region_left
2909 && gui.cursor_col <= gui.scroll_region_right)
2910 {
2911 if (gui.cursor_row < row + count)
2912 gui.cursor_is_valid = FALSE;
2913 else if (gui.cursor_row <= gui.scroll_region_bot)
2914 gui.cursor_row -= count;
2915 }
2916 }
2917}
2918
2919 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002920gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 if (count <= 0)
2923 return;
2924
2925 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002926 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 gui_clear_block(row, gui.scroll_region_left,
2928 gui.scroll_region_bot, gui.scroll_region_right);
2929 else
2930 {
2931 gui_mch_insert_lines(row, count);
2932
2933 if (gui.cursor_row >= gui.row
2934 && gui.cursor_col >= gui.scroll_region_left
2935 && gui.cursor_col <= gui.scroll_region_right)
2936 {
2937 if (gui.cursor_row <= gui.scroll_region_bot - count)
2938 gui.cursor_row += count;
2939 else if (gui.cursor_row <= gui.scroll_region_bot)
2940 gui.cursor_is_valid = FALSE;
2941 }
2942 }
2943}
2944
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002945#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002946/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002947 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2948 */
2949 static int
2950gui_wait_for_chars_3(
2951 long wtime,
2952 int *interrupted UNUSED,
2953 int ignore_input UNUSED)
2954{
2955 return gui_mch_wait_for_chars(wtime);
2956}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002957#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002958
2959/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002960 * Returns OK if a character was found to be available within the given time,
2961 * or FAIL otherwise.
2962 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002963 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002964gui_wait_for_chars_or_timer(
2965 long wtime,
2966 int *interrupted UNUSED,
2967 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002968{
2969#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002970 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2971 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002972#else
2973 return gui_mch_wait_for_chars(wtime);
2974#endif
2975}
2976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977/*
2978 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002979 * "wtime" == -1 Wait forever.
2980 * "wtime" == 0 Don't wait.
2981 * "wtime" > 0 Wait wtime milliseconds for a character.
2982 *
2983 * Returns the number of characters read or zero when timed out or interrupted.
2984 * "buf" may be NULL, in which case a non-zero number is returned if characters
2985 * are available.
2986 */
2987 static int
2988gui_wait_for_chars_buf(
2989 char_u *buf,
2990 int maxlen,
2991 long wtime, // don't use "time", MIPS cannot handle it
2992 int tb_change_cnt)
2993{
2994 int retval;
2995
2996#ifdef FEAT_MENU
2997 // If we're going to wait a bit, update the menus and mouse shape for the
2998 // current State.
2999 if (wtime != 0)
3000 gui_update_menus(0);
3001#endif
3002
3003 gui_mch_update();
3004 if (input_available()) // Got char, return immediately
3005 {
3006 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3007 return read_from_input_buf(buf, (long)maxlen);
3008 return 0;
3009 }
3010 if (wtime == 0) // Don't wait for char
3011 return FAIL;
3012
3013 // Before waiting, flush any output to the screen.
3014 gui_mch_flush();
3015
3016 // Blink while waiting for a character.
3017 gui_mch_start_blink();
3018
3019 // Common function to loop until "wtime" is met, while handling timers and
3020 // other callbacks.
3021 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3022 gui_wait_for_chars_or_timer, NULL);
3023
3024 gui_mch_stop_blink(TRUE);
3025
3026 return retval;
3027}
3028
3029/*
3030 * Wait for a character from the keyboard without actually reading it.
3031 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 * wtime == -1 Wait forever.
3033 * wtime == 0 Don't wait.
3034 * wtime > 0 Wait wtime milliseconds for a character.
3035 * Returns OK if a character was found to be available within the given time,
3036 * or FAIL otherwise.
3037 */
3038 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003039gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003041 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043
3044/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003045 * Equivalent of mch_inchar() for the GUI.
3046 */
3047 int
3048gui_inchar(
3049 char_u *buf,
3050 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003051 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003052 int tb_change_cnt)
3053{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003054 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003055}
3056
3057/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003058 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
3060 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003061fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 p[0] = (char_u)(col / 128 + ' ' + 1);
3064 p[1] = (char_u)(col % 128 + ' ' + 1);
3065 p[2] = (char_u)(row / 128 + ' ' + 1);
3066 p[3] = (char_u)(row % 128 + ' ' + 1);
3067}
3068
3069/*
3070 * Generic mouse support function. Add a mouse event to the input buffer with
3071 * the given properties.
3072 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3073 * MOUSE_X1, MOUSE_X2
3074 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003075 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3076 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 * x, y --- Coordinates of mouse in pixels.
3078 * repeated_click --- TRUE if this click comes only a short time after a
3079 * previous click.
3080 * modifiers --- Bit field which may be any of the following modifiers
3081 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3082 * This function will ignore drag events where the mouse has not moved to a new
3083 * character.
3084 */
3085 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003086gui_send_mouse_event(
3087 int button,
3088 int x,
3089 int y,
3090 int repeated_click,
3091 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 static int prev_row = 0, prev_col = 0;
3094 static int prev_button = -1;
3095 static int num_clicks = 1;
3096 char_u string[10];
3097 enum key_extra button_char;
3098 int row, col;
3099#ifdef FEAT_CLIPBOARD
3100 int checkfor;
3101 int did_clip = FALSE;
3102#endif
3103
3104 /*
3105 * Scrolling may happen at any time, also while a selection is present.
3106 */
3107 switch (button)
3108 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003109 case MOUSE_MOVE:
3110 button_char = KE_MOUSEMOVE_XY;
3111 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 case MOUSE_X1:
3113 button_char = KE_X1MOUSE;
3114 goto button_set;
3115 case MOUSE_X2:
3116 button_char = KE_X2MOUSE;
3117 goto button_set;
3118 case MOUSE_4:
3119 button_char = KE_MOUSEDOWN;
3120 goto button_set;
3121 case MOUSE_5:
3122 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003123 goto button_set;
3124 case MOUSE_6:
3125 button_char = KE_MOUSELEFT;
3126 goto button_set;
3127 case MOUSE_7:
3128 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129button_set:
3130 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003131 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (hold_gui_events)
3133 return;
3134
Ernie Raelc4cb5442022-04-03 15:47:28 +01003135 row = gui_xy2colrow(x, y, &col);
3136 // Don't report a mouse move unless moved to a
3137 // different character position.
3138 if (button == MOUSE_MOVE)
3139 {
3140 if (row == prev_row && col == prev_col)
3141 return;
3142 else
3143 {
3144 prev_row = row >= 0 ? row : 0;
3145 prev_col = col;
3146 }
3147 }
3148
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 string[3] = CSI;
3150 string[4] = KS_EXTRA;
3151 string[5] = (int)button_char;
3152
Bram Moolenaar30613902019-12-01 22:11:18 +01003153 // Pass the pointer coordinates of the scroll event so that we
3154 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 string[6] = (char_u)(col / 128 + ' ' + 1);
3156 string[7] = (char_u)(col % 128 + ' ' + 1);
3157 string[8] = (char_u)(row / 128 + ' ' + 1);
3158 string[9] = (char_u)(row % 128 + ' ' + 1);
3159
3160 if (modifiers == 0)
3161 add_to_input_buf(string + 3, 7);
3162 else
3163 {
3164 string[0] = CSI;
3165 string[1] = KS_MODIFIER;
3166 string[2] = 0;
3167 if (modifiers & MOUSE_SHIFT)
3168 string[2] |= MOD_MASK_SHIFT;
3169 if (modifiers & MOUSE_CTRL)
3170 string[2] |= MOD_MASK_CTRL;
3171 if (modifiers & MOUSE_ALT)
3172 string[2] |= MOD_MASK_ALT;
3173 add_to_input_buf(string, 10);
3174 }
3175 return;
3176 }
3177 }
3178
3179#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003180 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (clip_star.state == SELECT_IN_PROGRESS)
3182 {
3183 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003184
3185 // A release event may still need to be sent if the position is equal.
3186 row = gui_xy2colrow(x, y, &col);
3187 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3188 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190
Bram Moolenaar30613902019-12-01 22:11:18 +01003191 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 switch (get_real_state())
3193 {
3194 case NORMAL_BUSY:
3195 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003196# ifdef FEAT_TERMINAL
3197 case TERMINAL:
3198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 case NORMAL: checkfor = MOUSE_NORMAL; break;
3200 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003201 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 case REPLACE:
3203 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 case VREPLACE:
3205 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 case INSERT:
3207 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3208 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003209 case HITRETURN: // At the more- and hit-enter prompt pass the
3210 // mouse event for a click on or below the
3211 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (Y_2_ROW(y) >= msg_row)
3213 checkfor = MOUSE_NORMAL;
3214 else
3215 checkfor = MOUSE_RETURN;
3216 break;
3217
3218 /*
3219 * On the command line, use the clipboard selection on all lines
3220 * but the command line. But not when pasting.
3221 */
3222 case CMDLINE:
3223 case CMDLINE+LANGMAP:
3224 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3225 checkfor = MOUSE_NONE;
3226 else
3227 checkfor = MOUSE_COMMAND;
3228 break;
3229
3230 default:
3231 checkfor = MOUSE_NONE;
3232 break;
3233 };
3234
3235 /*
3236 * Allow clipboard selection of text on the command line in "normal"
3237 * modes. Don't do this when dragging the status line, or extending a
3238 * Visual selection.
3239 */
3240 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003241 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 && button != MOUSE_DRAG
3243# ifdef FEAT_MOUSESHAPE
3244 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246# endif
3247 )
3248 checkfor = MOUSE_NONE;
3249
3250 /*
3251 * Use modeless selection when holding CTRL and SHIFT pressed.
3252 */
3253 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3254 checkfor = MOUSE_NONEF;
3255
3256 /*
3257 * In Ex mode, always use modeless selection.
3258 */
3259 if (exmode_active)
3260 checkfor = MOUSE_NONE;
3261
3262 /*
3263 * If the mouse settings say to not use the mouse, use the modeless
3264 * selection. But if Visual is active, assume that only the Visual area
3265 * will be selected.
3266 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003267 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3270 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003271 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3273 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 /*
3276 * When 'mousemodel' is "popup", shift-left is translated to right.
3277 * But not when also using Ctrl.
3278 */
3279 if (mouse_model_popup() && button == MOUSE_LEFT
3280 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3281 {
3282 button = MOUSE_RIGHT;
3283 modifiers &= ~ MOUSE_SHIFT;
3284 }
3285
Bram Moolenaar30613902019-12-01 22:11:18 +01003286 // If the selection is done, allow the right button to extend it.
3287 // If the selection is cleared, allow the right button to start it
3288 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (button == MOUSE_RIGHT)
3290 {
3291 if (clip_star.state == SELECT_CLEARED)
3292 {
3293 if (State & CMDLINE)
3294 {
3295 col = msg_col;
3296 row = msg_row;
3297 }
3298 else
3299 {
3300 col = curwin->w_wcol;
3301 row = curwin->w_wrow + W_WINROW(curwin);
3302 }
3303 clip_start_selection(col, row, FALSE);
3304 }
3305 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3306 repeated_click);
3307 did_clip = TRUE;
3308 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003309 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003310 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3313 did_clip = TRUE;
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar30613902019-12-01 22:11:18 +01003316 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (button != MOUSE_MIDDLE)
3318 {
3319 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3320 return;
3321 if (checkfor != MOUSE_COMMAND)
3322 button = MOUSE_LEFT;
3323 }
3324 repeated_click = FALSE;
3325 }
3326
3327 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003328 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#endif
3330
Bram Moolenaar30613902019-12-01 22:11:18 +01003331 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (hold_gui_events)
3333 return;
3334
3335 row = gui_xy2colrow(x, y, &col);
3336
3337 /*
3338 * If we are dragging and the mouse hasn't moved far enough to be on a
3339 * different character, then don't send an event to vim.
3340 */
3341 if (button == MOUSE_DRAG)
3342 {
3343 if (row == prev_row && col == prev_col)
3344 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003345 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 if (y < 0)
3347 row = -1;
3348 }
3349
3350 /*
3351 * If topline has changed (window scrolled) since the last click, reset
3352 * repeated_click, because we don't want starting Visual mode when
3353 * clicking on a different character in the text.
3354 */
3355 if (curwin->w_topline != gui_prev_topline
3356#ifdef FEAT_DIFF
3357 || curwin->w_topfill != gui_prev_topfill
3358#endif
3359 )
3360 repeated_click = FALSE;
3361
Bram Moolenaar30613902019-12-01 22:11:18 +01003362 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 string[1] = KS_MOUSE;
3364 string[2] = KE_FILLER;
3365 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3366 {
3367 if (repeated_click)
3368 {
3369 /*
3370 * Handle multiple clicks. They only count if the mouse is still
3371 * pointing at the same character.
3372 */
3373 if (button != prev_button || row != prev_row || col != prev_col)
3374 num_clicks = 1;
3375 else if (++num_clicks > 4)
3376 num_clicks = 1;
3377 }
3378 else
3379 num_clicks = 1;
3380 prev_button = button;
3381 gui_prev_topline = curwin->w_topline;
3382#ifdef FEAT_DIFF
3383 gui_prev_topfill = curwin->w_topfill;
3384#endif
3385
3386 string[3] = (char_u)(button | 0x20);
3387 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3388 }
3389 else
3390 string[3] = (char_u)button;
3391
3392 string[3] |= modifiers;
3393 fill_mouse_coord(string + 4, col, row);
3394 add_to_input_buf(string, 8);
3395
3396 if (row < 0)
3397 prev_row = 0;
3398 else
3399 prev_row = row;
3400 prev_col = col;
3401
3402 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003403 * We need to make sure this is cleared since GTK doesn't tell us when
3404 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003406#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 gui.dragged_sb = SBAR_NONE;
3408#endif
3409}
3410
3411/*
3412 * Convert x and y coordinate to column and row in text window.
3413 * Corrects for multi-byte character.
3414 * returns column in "*colp" and row as return value;
3415 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003416 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003417gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int col = check_col(X_2_COL(x));
3420 int row = check_row(Y_2_ROW(y));
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 return row;
3424}
3425
3426#if defined(FEAT_MENU) || defined(PROTO)
3427/*
3428 * Callback function for when a menu entry has been selected.
3429 */
3430 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003431gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003433 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaar30613902019-12-01 22:11:18 +01003435 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (hold_gui_events)
3437 return;
3438
3439 bytes[0] = CSI;
3440 bytes[1] = KS_MENU;
3441 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003442 add_to_input_buf(bytes, 3);
3443 add_long_to_buf((long_u)menu, bytes);
3444 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
3446#endif
3447
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003448static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450/*
3451 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003452 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 * in p_go.
3454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003456gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003458#ifdef FEAT_GUI_DARKTHEME
3459 static int prev_dark_theme = -1;
3460 int using_dark_theme = FALSE;
3461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462#ifdef FEAT_MENU
3463 static int prev_menu_is_active = -1;
3464#endif
3465#ifdef FEAT_TOOLBAR
3466 static int prev_toolbar = -1;
3467 int using_toolbar = FALSE;
3468#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003469#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003470 int using_tabline;
3471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#ifdef FEAT_FOOTER
3473 static int prev_footer = -1;
3474 int using_footer = FALSE;
3475#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003476#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 static int prev_tearoff = -1;
3478 int using_tearoff = FALSE;
3479#endif
3480
3481 char_u *p;
3482 int i;
3483#ifdef FEAT_MENU
3484 int grey_old, grey_new;
3485 char_u *temp;
3486#endif
3487 win_T *wp;
3488 int need_set_size;
3489 int fix_size;
3490
3491#ifdef FEAT_MENU
3492 if (oldval != NULL && gui.in_use)
3493 {
3494 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003495 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 */
3497 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3498 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3499 if (grey_old != grey_new)
3500 {
3501 temp = p_go;
3502 p_go = oldval;
3503 gui_update_menus(MENU_ALL_MODES);
3504 p_go = temp;
3505 }
3506 }
3507 gui.menu_is_active = FALSE;
3508#endif
3509
3510 for (i = 0; i < 3; i++)
3511 gui.which_scrollbars[i] = FALSE;
3512 for (p = p_go; *p; p++)
3513 switch (*p)
3514 {
3515 case GO_LEFT:
3516 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3517 break;
3518 case GO_RIGHT:
3519 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 case GO_VLEFT:
3522 if (win_hasvertsplit())
3523 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3524 break;
3525 case GO_VRIGHT:
3526 if (win_hasvertsplit())
3527 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 case GO_BOT:
3530 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3531 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003532#ifdef FEAT_GUI_DARKTHEME
3533 case GO_DARKTHEME:
3534 using_dark_theme = TRUE;
3535 break;
3536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537#ifdef FEAT_MENU
3538 case GO_MENUS:
3539 gui.menu_is_active = TRUE;
3540 break;
3541#endif
3542 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003543 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 break;
3545#ifdef FEAT_TOOLBAR
3546 case GO_TOOLBAR:
3547 using_toolbar = TRUE;
3548 break;
3549#endif
3550#ifdef FEAT_FOOTER
3551 case GO_FOOTER:
3552 using_footer = TRUE;
3553 break;
3554#endif
3555 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 using_tearoff = TRUE;
3558#endif
3559 break;
3560 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003561 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 break;
3563 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (gui.in_use)
3566 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003567 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003570#ifdef FEAT_GUI_DARKTHEME
3571 if (using_dark_theme != prev_dark_theme)
3572 {
3573 gui_mch_set_dark_theme(using_dark_theme);
3574 prev_dark_theme = using_dark_theme;
3575 }
3576#endif
3577
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003578#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003579 // Update the GUI tab line, it may appear or disappear. This may
3580 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003581 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003582 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003584 // We don't want a resize event change "Rows" here, save and
3585 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003586 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003587 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003588 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003589 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003590 if (using_tabline)
3591 fix_size = TRUE;
3592 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003593 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594 }
3595#endif
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 for (i = 0; i < 3; i++)
3598 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003599 // The scrollbar needs to be updated when it is shown/unshown and
3600 // when switching tab pages. But the size only changes when it's
3601 // shown/unshown. Thus we need two places to remember whether a
3602 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003603 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003604 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003605 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 if (i == SBAR_BOTTOM)
3608 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3609 gui.which_scrollbars[i]);
3610 else
3611 {
3612 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003615 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3616 {
3617 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003618 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003619 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003620 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003621 if (gui.which_scrollbars[i])
3622 fix_size = TRUE;
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003625 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003626 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628
3629#ifdef FEAT_MENU
3630 if (gui.menu_is_active != prev_menu_is_active)
3631 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003632 // We don't want a resize event change "Rows" here, save and
3633 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 i = Rows;
3635 gui_mch_enable_menu(gui.menu_is_active);
3636 Rows = i;
3637 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003638 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (gui.menu_is_active)
3640 fix_size = TRUE;
3641 }
3642#endif
3643
3644#ifdef FEAT_TOOLBAR
3645 if (using_toolbar != prev_toolbar)
3646 {
3647 gui_mch_show_toolbar(using_toolbar);
3648 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003649 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (using_toolbar)
3651 fix_size = TRUE;
3652 }
3653#endif
3654#ifdef FEAT_FOOTER
3655 if (using_footer != prev_footer)
3656 {
3657 gui_mch_enable_footer(using_footer);
3658 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003659 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (using_footer)
3661 fix_size = TRUE;
3662 }
3663#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003664#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (using_tearoff != prev_tearoff)
3666 {
3667 gui_mch_toggle_tearoffs(using_tearoff);
3668 prev_tearoff = using_tearoff;
3669 }
3670#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003671 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003672 {
3673#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003674 long prev_Columns = Columns;
3675 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003676#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003677 // Adjust the size of the window to make the text area keep the
3678 // same size and to avoid that part of our window is off-screen
3679 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003680 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003681
3682#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003683 // GTK has the annoying habit of sending us resize events when
3684 // changing the window size ourselves. This mostly happens when
3685 // waiting for a character to arrive, quite unpredictably, and may
3686 // change Columns and Rows when we don't want it. Wait for a
3687 // character here to avoid this effect.
3688 // If you remove this, please test this command for resizing
3689 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3690 // Don't do this while starting up though.
3691 // Don't change Rows when adding menu/toolbar/tabline.
3692 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003693 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003694 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003695 if ((need_set_size & RESIZE_VERT) == 0)
3696 Rows = prev_Rows;
3697 if ((need_set_size & RESIZE_HOR) == 0)
3698 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003699#endif
3700 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003701 // When the console tabline appears or disappears the window positions
3702 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003703 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003704 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706}
3707
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003708#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3709/*
3710 * Return TRUE if the GUI is taking care of the tabline.
3711 * It may still be hidden if 'showtabline' is zero.
3712 */
3713 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003714gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003715{
3716 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3717}
3718
3719/*
3720 * Return TRUE if the GUI is showing the tabline.
3721 * This uses 'showtabline'.
3722 */
3723 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003724gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003725{
3726 if (!gui_use_tabline()
3727 || p_stal == 0
3728 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3729 return FALSE;
3730 return TRUE;
3731}
3732
3733/*
3734 * Update the tabline.
3735 * This may display/undisplay the tabline and update the labels.
3736 */
3737 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003738gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003739{
3740 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003741 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003742
3743 if (!gui.starting && starting == 0)
3744 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003745 // Updating the tabline uses direct GUI commands, flush
3746 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003747 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003748
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003749 if (!showit != !shown)
3750 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003751 if (showit != 0)
3752 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003753
Bram Moolenaar30613902019-12-01 22:11:18 +01003754 // When the tabs change from hidden to shown or from shown to
3755 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003756 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003757 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003758 }
3759}
3760
3761/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003762 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003763 */
3764 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003765get_tabline_label(
3766 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003767 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768{
3769 int modified = FALSE;
3770 char_u buf[40];
3771 int wincount;
3772 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003773 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003774
Bram Moolenaar30613902019-12-01 22:11:18 +01003775 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003776 opt = (tooltip ? &p_gtt : &p_gtl);
3777 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003778 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003779 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003780 int called_emsg_before = called_emsg;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003781 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003782 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003783 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3784 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003785
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003786 printer_page_num = tabpage_index(tp);
3787# ifdef FEAT_EVAL
3788 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003789 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003790# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003791 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003792 curtab->tp_firstwin = firstwin;
3793 curtab->tp_lastwin = lastwin;
3794 curtab->tp_curwin = curwin;
3795 save_curtab = curtab;
3796 curtab = tp;
3797 topframe = curtab->tp_topframe;
3798 firstwin = curtab->tp_firstwin;
3799 lastwin = curtab->tp_lastwin;
3800 curwin = curtab->tp_curwin;
3801 curbuf = curwin->w_buffer;
3802
Bram Moolenaar30613902019-12-01 22:11:18 +01003803 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003804 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003805 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003806 STRCPY(NameBuff, res);
3807
Bram Moolenaar30613902019-12-01 22:11:18 +01003808 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003809 curtab = save_curtab;
3810 topframe = curtab->tp_topframe;
3811 firstwin = curtab->tp_firstwin;
3812 lastwin = curtab->tp_lastwin;
3813 curwin = curtab->tp_curwin;
3814 curbuf = curwin->w_buffer;
3815
Bram Moolenaar53989552019-12-23 22:59:18 +01003816 if (called_emsg > called_emsg_before)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003817 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003818 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003819 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003820
Bram Moolenaar30613902019-12-01 22:11:18 +01003821 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3822 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003823 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003824 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003825 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003826 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003827 if (!tooltip)
3828 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003829
3830 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3831 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3832 if (bufIsChanged(wp->w_buffer))
3833 modified = TRUE;
3834 if (modified || wincount > 1)
3835 {
3836 if (wincount > 1)
3837 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3838 else
3839 buf[0] = NUL;
3840 if (modified)
3841 STRCAT(buf, "+");
3842 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003843 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003844 mch_memmove(NameBuff, buf, STRLEN(buf));
3845 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003846 }
3847}
3848
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003849/*
3850 * Send the event for clicking to select tab page "nr".
3851 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003852 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003853 */
3854 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003855send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003856{
3857 char_u string[3];
3858
3859 if (nr == tabpage_index(curtab))
3860 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003861
Bram Moolenaar30613902019-12-01 22:11:18 +01003862 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003863 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003864# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003865 || cmdwin_type != 0
3866# endif
3867 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003868 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003869 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003870 gui_mch_set_curtab(tabpage_index(curtab));
3871 return FALSE;
3872 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003873
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003874 string[0] = CSI;
3875 string[1] = KS_TABLINE;
3876 string[2] = KE_FILLER;
3877 add_to_input_buf(string, 3);
3878 string[0] = nr;
3879 add_to_input_buf_csi(string, 1);
3880 return TRUE;
3881}
3882
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003883/*
3884 * Send a tabline menu event
3885 */
3886 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003887send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003888{
3889 char_u string[3];
3890
Bram Moolenaar29547192018-12-11 20:39:19 +01003891 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003892 if (hold_gui_events)
3893 return;
3894
Bram Moolenaar29547192018-12-11 20:39:19 +01003895 // Cannot close the last tabpage.
3896 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3897 return;
3898
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003899 string[0] = CSI;
3900 string[1] = KS_TABMENU;
3901 string[2] = KE_FILLER;
3902 add_to_input_buf(string, 3);
3903 string[0] = tabidx;
3904 string[1] = (char_u)(long)event;
3905 add_to_input_buf_csi(string, 2);
3906}
3907
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
3910/*
3911 * Scrollbar stuff:
3912 */
3913
Bram Moolenaare45828b2006-02-15 22:12:56 +00003914/*
3915 * Remove all scrollbars. Used before switching to another tab page.
3916 */
3917 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003918gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003919{
3920 int i;
3921 win_T *wp;
3922
3923 for (i = 0; i < 3; i++)
3924 {
3925 if (i == SBAR_BOTTOM)
3926 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3927 else
3928 {
3929 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003930 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003931 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003932 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003933 }
3934}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003937gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 static int sbar_ident = 0;
3940
Bram Moolenaar30613902019-12-01 22:11:18 +01003941 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 sb->wp = wp;
3943 sb->type = type;
3944 sb->value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 sb->size = 1;
3946 sb->max = 1;
3947 sb->top = 0;
3948 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 sb->status_height = 0;
3951 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3952}
3953
3954/*
3955 * Find the scrollbar with the given index.
3956 */
3957 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003958gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 win_T *wp;
3961
3962 if (gui.bottom_sbar.ident == ident)
3963 return &gui.bottom_sbar;
3964 FOR_ALL_WINDOWS(wp)
3965 {
3966 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3967 return &wp->w_scrollbars[SBAR_LEFT];
3968 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3969 return &wp->w_scrollbars[SBAR_RIGHT];
3970 }
3971 return NULL;
3972}
3973
3974/*
3975 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3976 *
3977 * For Win32, Macintosh and GTK+ 2:
3978 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3979 * you stop dragging the scrollbar. We get here each time the scrollbar is
3980 * dragged another pixel, but as far as the rest of vim goes, it thinks
3981 * we're just hanging in the call to DispatchMessage() in
3982 * process_message(). The DispatchMessage() call that hangs was passed a
3983 * mouse button click event in the scrollbar window. -- webb.
3984 *
3985 * Solution: Do the scrolling right here. But only when allowed.
3986 * Ignore the scrollbars while executing an external command or when there
3987 * are still characters to be processed.
3988 */
3989 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003990gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 int sb_num;
3994#ifdef USE_ON_FLY_SCROLL
3995 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997# ifdef FEAT_DIFF
3998 int old_topfill = curwin->w_topfill;
3999# endif
4000#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004001 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 int byte_count;
4003#endif
4004
4005 if (sb == NULL)
4006 return;
4007
Bram Moolenaar30613902019-12-01 22:11:18 +01004008 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (hold_gui_events)
4010 return;
4011
4012#ifdef FEAT_CMDWIN
4013 if (cmdwin_type != 0 && sb->wp != curwin)
4014 return;
4015#endif
4016
4017 if (still_dragging)
4018 {
4019 if (sb->wp == NULL)
4020 gui.dragged_sb = SBAR_BOTTOM;
4021 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4022 gui.dragged_sb = SBAR_LEFT;
4023 else
4024 gui.dragged_sb = SBAR_RIGHT;
4025 gui.dragged_wp = sb->wp;
4026 }
4027 else
4028 {
4029 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004030#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004031 // Keep the "dragged_wp" value until after the scrolling, for when the
4032 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 gui.dragged_wp = NULL;
4034#endif
4035 }
4036
Bram Moolenaar30613902019-12-01 22:11:18 +01004037 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (sb->wp != NULL)
4039 sb = &sb->wp->w_scrollbars[0];
4040
4041 /*
4042 * Check validity of value
4043 */
4044 if (value < 0)
4045 value = 0;
4046#ifdef SCROLL_PAST_END
4047 else if (value > sb->max)
4048 value = sb->max;
4049#else
4050 if (value > sb->max - sb->size + 1)
4051 value = sb->max - sb->size + 1;
4052#endif
4053
4054 sb->value = value;
4055
4056#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004057 // When not allowed to do the scrolling right now, return.
4058 // This also checked input_available(), but that causes the first click in
4059 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004060 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return;
4062#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004063 // Disallow scrolling the current window when the completion popup menu is
4064 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004065 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4066 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068#ifdef FEAT_RIGHTLEFT
4069 if (sb->wp == NULL && curwin->w_p_rl)
4070 {
4071 value = sb->max + 1 - sb->size - value;
4072 if (value < 0)
4073 value = 0;
4074 }
4075#endif
4076
Bram Moolenaar30613902019-12-01 22:11:18 +01004077 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
4079 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4081 sb_num++;
4082 if (wp == NULL)
4083 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085#ifdef USE_ON_FLY_SCROLL
4086 current_scrollbar = sb_num;
4087 scrollbar_value = value;
4088 if (State & NORMAL)
4089 {
4090 gui_do_scroll();
4091 setcursor();
4092 }
4093 else if (State & INSERT)
4094 {
4095 ins_scroll();
4096 setcursor();
4097 }
4098 else if (State & CMDLINE)
4099 {
4100 if (msg_scrolled == 0)
4101 {
4102 gui_do_scroll();
4103 redrawcmdline();
4104 }
4105 }
4106# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004107 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 sb->value = sb->wp->w_topline - 1;
4109# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004110
Bram Moolenaar30613902019-12-01 22:11:18 +01004111 // When dragging one scrollbar and there is another one at the other
4112 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004113 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4114 gui_mch_set_scrollbar_thumb(
4115 &sb->wp->w_scrollbars[
4116 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4117 ? SBAR_LEFT : SBAR_RIGHT],
4118 sb->value, sb->size, sb->max);
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120#else
4121 bytes[0] = CSI;
4122 bytes[1] = KS_VER_SCROLLBAR;
4123 bytes[2] = KE_FILLER;
4124 bytes[3] = (char_u)sb_num;
4125 byte_count = 4;
4126#endif
4127 }
4128 else
4129 {
4130#ifdef USE_ON_FLY_SCROLL
4131 scrollbar_value = value;
4132
4133 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004134 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 else if (State & INSERT)
4136 ins_horscroll();
4137 else if (State & CMDLINE)
4138 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004139 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004141 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 redrawcmdline();
4143 }
4144 }
4145 if (old_leftcol != curwin->w_leftcol)
4146 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004147 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 setcursor();
4149 }
4150#else
4151 bytes[0] = CSI;
4152 bytes[1] = KS_HOR_SCROLLBAR;
4153 bytes[2] = KE_FILLER;
4154 byte_count = 3;
4155#endif
4156 }
4157
4158#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /*
4160 * synchronize other windows, as necessary according to 'scrollbind'
4161 */
4162 if (curwin->w_p_scb
4163 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4164 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004165# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 ))))
4169 {
4170 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004171 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004172 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (wp->w_redr_type > 0)
4174 updateWindow(wp);
4175 setcursor();
4176 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004177 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004179 add_to_input_buf(bytes, byte_count);
4180 add_long_to_buf((long_u)value, bytes);
4181 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#endif
4183}
4184
4185/*
4186 * Scrollbar stuff:
4187 */
4188
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004189/*
4190 * Called when something in the window layout has changed.
4191 */
4192 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004193gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004194{
4195 if (gui.in_use && starting == 0)
4196 {
4197 out_flush();
4198 gui_init_which_components(NULL);
4199 gui_update_scrollbars(TRUE);
4200 }
4201 need_mouse_correct = TRUE;
4202}
4203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004205gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004206 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 win_T *wp;
4209 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004210 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 int which_sb;
4212 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
Bram Moolenaar30613902019-12-01 22:11:18 +01004215 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 gui_update_horiz_scrollbar(force);
4217
Bram Moolenaar4f974752019-02-17 17:44:42 +01004218#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004219 // Return straight away if there is neither a left nor right scrollbar.
4220 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4222 return;
4223#endif
4224
4225 /*
4226 * Don't want to update a scrollbar while we're dragging it. But if we
4227 * have both a left and right scrollbar, and we drag one of them, we still
4228 * need to update the other one.
4229 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004230 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4231 && gui.which_scrollbars[SBAR_LEFT]
4232 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
4234 /*
4235 * If we have two scrollbars and one of them is being dragged, just
4236 * copy the scrollbar position from the dragged one to the other one.
4237 */
4238 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4239 if (gui.dragged_wp != NULL)
4240 gui_mch_set_scrollbar_thumb(
4241 &gui.dragged_wp->w_scrollbars[which_sb],
4242 gui.dragged_wp->w_scrollbars[0].value,
4243 gui.dragged_wp->w_scrollbars[0].size,
4244 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246
Bram Moolenaar30613902019-12-01 22:11:18 +01004247 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 ++hold_gui_events;
4249
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004250 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004252 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004254 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004255 if (!force && (gui.dragged_sb == SBAR_LEFT
4256 || gui.dragged_sb == SBAR_RIGHT)
4257 && gui.dragged_wp == wp)
4258 continue;
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260#ifdef SCROLL_PAST_END
4261 max = wp->w_buffer->b_ml.ml_line_count - 1;
4262#else
4263 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4264#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004265 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 max = 0;
4267 val = wp->w_topline - 1;
4268 size = wp->w_height;
4269#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004270 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 val = max;
4272#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004273 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 size = max + 1;
4275 if (val > max - size + 1)
4276 val = max - size + 1;
4277#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004278 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 val = 0;
4280
4281 /*
4282 * Scrollbar at index 0 (the left one) contains all the information.
4283 * It would be the same info for left and right so we just store it for
4284 * one of them.
4285 */
4286 sb = &wp->w_scrollbars[0];
4287
4288 /*
4289 * Note: no check for valid w_botline. If it's not valid the
4290 * scrollbars will be updated later anyway.
4291 */
4292 if (size < 1 || wp->w_botline - 2 > max)
4293 {
4294 /*
4295 * This can happen during changing files. Just don't update the
4296 * scrollbar for now.
4297 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004298 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 if (gui.which_scrollbars[SBAR_LEFT])
4300 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4301 if (gui.which_scrollbars[SBAR_RIGHT])
4302 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4303 continue;
4304 }
4305 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 || sb->top != wp->w_winrow
4307 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004309 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004311 // Height, width or position of scrollbar has changed. For
4312 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 sb->top = wp->w_winrow;
4315 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
Bram Moolenaar30613902019-12-01 22:11:18 +01004318 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 h = (sb->height + sb->status_height) * gui.char_height;
4320 y = sb->top * gui.char_height + gui.border_offset;
4321#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4322 if (gui.menu_is_active)
4323 y += gui.menu_height;
4324#endif
4325
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004326#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004327 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330#endif
4331
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004332#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004333 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004334 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004335#endif
4336
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004339 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 h += gui.border_offset;
4341 y -= gui.border_offset;
4342 }
4343 if (gui.which_scrollbars[SBAR_LEFT])
4344 {
4345 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4346 gui.left_sbar_x, y,
4347 gui.scrollbar_width, h);
4348 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4349 }
4350 if (gui.which_scrollbars[SBAR_RIGHT])
4351 {
4352 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4353 gui.right_sbar_x, y,
4354 gui.scrollbar_width, h);
4355 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4356 }
4357 }
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (force || sb->value != val || sb->size != size || sb->max != max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004361 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 sb->value = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 sb->size = size;
4364 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004365 if (gui.which_scrollbars[SBAR_LEFT]
4366 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4368 val, size, max);
4369 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004370 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4372 val, size, max);
4373 }
4374 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004375
4376 // update the title, it may show the scroll position
4377 maketitle();
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 --hold_gui_events;
4381}
4382
4383/*
4384 * Enable or disable a scrollbar.
4385 * Check for scrollbars for vertically split windows which are not enabled
4386 * sometimes.
4387 */
4388 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004389gui_do_scrollbar(
4390 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004391 int which, // SBAR_LEFT or SBAR_RIGHT
4392 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 int midcol = curwin->w_wincol + curwin->w_width / 2;
4395 int has_midcol = (wp->w_wincol <= midcol
4396 && wp->w_wincol + wp->w_width >= midcol);
4397
Bram Moolenaar30613902019-12-01 22:11:18 +01004398 // Only enable scrollbars that contain the middle column of the current
4399 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4401 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004402 // Scrollbars only on one side. Don't enable scrollbars that don't
4403 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (!has_midcol)
4405 enable = FALSE;
4406 }
4407 else
4408 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004409 // Scrollbars on both sides. Don't enable scrollbars that neither
4410 // contain the middle column of the current window nor are on the far
4411 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (midcol > Columns / 2)
4413 {
4414 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4415 enable = FALSE;
4416 }
4417 else
4418 {
4419 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4420 : !has_midcol)
4421 enable = FALSE;
4422 }
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4425}
4426
4427/*
4428 * Scroll a window according to the values set in the globals current_scrollbar
4429 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4430 * or FALSE otherwise.
4431 */
4432 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004433gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
4435 win_T *wp, *save_wp;
4436 int i;
4437 long nlines;
4438 pos_T old_cursor;
4439 linenr_T old_topline;
4440#ifdef FEAT_DIFF
4441 int old_topfill;
4442#endif
4443
4444 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4445 if (wp == NULL)
4446 break;
4447 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004448 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 return FALSE;
4450
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!).
4501 */
4502 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004503 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#ifdef FEAT_DIFF
4505 || old_topfill != wp->w_topfill
4506#endif
4507 )
4508 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004509 int type = VALID;
4510
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004511 if (pum_visible())
4512 {
4513 type = NOT_VALID;
4514 wp->w_lines_valid = 0;
4515 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004516
Bram Moolenaar30613902019-12-01 22:11:18 +01004517 // Don't set must_redraw here, it may cause the popup menu to
4518 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004519 if (wp->w_redr_type < type)
4520 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004521 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004522 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004523 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525
Bram Moolenaar30613902019-12-01 22:11:18 +01004526 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004527 if (pum_visible())
4528 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004529
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004530 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531}
4532
4533
4534/*
4535 * Horizontal scrollbar stuff:
4536 */
4537
4538/*
4539 * Return length of line "lnum" for horizontal scrolling.
4540 */
4541 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004542scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544 char_u *p;
4545 colnr_T col;
4546 int w;
4547
4548 p = ml_get(lnum);
4549 col = 0;
4550 if (*p != NUL)
4551 for (;;)
4552 {
4553 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004554 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004555 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 break;
4557 col += w;
4558 }
4559 return col;
4560}
4561
Bram Moolenaar30613902019-12-01 22:11:18 +01004562// Remember which line is currently the longest, so that we don't have to
4563// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564static linenr_T longest_lnum = 0;
4565
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004566/*
4567 * Find longest visible line number. If this is not possible (or not desired,
4568 * by setting 'h' in "guioptions") then the current line number is returned.
4569 */
4570 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004571gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004572{
4573 linenr_T ret = 0;
4574
Bram Moolenaar30613902019-12-01 22:11:18 +01004575 // Calculate maximum for horizontal scrollbar. Check for reasonable
4576 // line numbers, topline and botline can be invalid when displaying is
4577 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004578 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4579 && curwin->w_topline <= curwin->w_cursor.lnum
4580 && curwin->w_botline > curwin->w_cursor.lnum
4581 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4582 {
4583 linenr_T lnum;
4584 colnr_T n;
4585 long max = 0;
4586
Bram Moolenaar30613902019-12-01 22:11:18 +01004587 // Use maximum of all visible lines. Remember the lnum of the
4588 // longest line, closest to the cursor line. Used when scrolling
4589 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004590 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4591 {
4592 n = scroll_line_len(lnum);
4593 if (n > (colnr_T)max)
4594 {
4595 max = n;
4596 ret = lnum;
4597 }
4598 else if (n == (colnr_T)max
4599 && abs((int)(lnum - curwin->w_cursor.lnum))
4600 < abs((int)(ret - curwin->w_cursor.lnum)))
4601 ret = lnum;
4602 }
4603 }
4604 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004605 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004606 ret = curwin->w_cursor.lnum;
4607
4608 return ret;
4609}
4610
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004612gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613{
Bram Moolenaar30613902019-12-01 22:11:18 +01004614 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 if (!gui.which_scrollbars[SBAR_BOTTOM])
4617 return;
4618
4619 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4620 return;
4621
4622 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4623 return;
4624
4625 /*
4626 * It is possible for the cursor to be invalid if we're in the middle of
4627 * something (like changing files). If so, don't do anything for now.
4628 */
4629 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4630 {
4631 gui.bottom_sbar.value = -1;
4632 return;
4633 }
4634
Bram Moolenaar02631462017-09-22 15:20:32 +02004635 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (curwin->w_p_wrap)
4637 {
4638 value = 0;
4639#ifdef SCROLL_PAST_END
4640 max = 0;
4641#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004642 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
4644 }
4645 else
4646 {
4647 value = curwin->w_leftcol;
4648
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004649 longest_lnum = gui_find_longest_lnum();
4650 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (virtual_active())
4653 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004654 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (curwin->w_virtcol >= (colnr_T)max)
4656 max = curwin->w_virtcol;
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004660 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004662 // The line number isn't scrolled, thus there is less space when
4663 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 size -= curwin_col_off();
4665#ifndef SCROLL_PAST_END
4666 max -= curwin_col_off();
4667#endif
4668 }
4669
4670#ifndef SCROLL_PAST_END
4671 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004672 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673#endif
4674
4675#ifdef FEAT_RIGHTLEFT
4676 if (curwin->w_p_rl)
4677 {
4678 value = max + 1 - size - value;
4679 if (value < 0)
4680 {
4681 size += value;
4682 value = 0;
4683 }
4684 }
4685#endif
4686 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4687 && max == gui.bottom_sbar.max)
4688 return;
4689
4690 gui.bottom_sbar.value = value;
4691 gui.bottom_sbar.size = size;
4692 gui.bottom_sbar.max = max;
4693 gui.prev_wrap = curwin->w_p_wrap;
4694
4695 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4696}
4697
4698/*
4699 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4700 */
4701 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004702gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
Bram Moolenaar30613902019-12-01 22:11:18 +01004704 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (curwin->w_p_wrap)
4706 return FALSE;
4707
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004708 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return FALSE;
4710
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004711 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaar30613902019-12-01 22:11:18 +01004713 // When the line of the cursor is too short, move the cursor to the
4714 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004716 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004717 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004719 if (compute_longest_lnum)
4720 {
4721 curwin->w_cursor.lnum = gui_find_longest_lnum();
4722 curwin->w_cursor.col = 0;
4723 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004724 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004725 else if (longest_lnum >= curwin->w_topline
4726 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
4728 curwin->w_cursor.lnum = longest_lnum;
4729 curwin->w_cursor.col = 0;
4730 }
4731 }
4732
4733 return leftcol_changed();
4734}
4735
4736/*
4737 * Check that none of the colors are the same as the background color
4738 */
4739 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004740gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4743 {
4744 gui_set_bg_color((char_u *)"White");
4745 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4746 gui_set_fg_color((char_u *)"Black");
4747 }
4748}
4749
Bram Moolenaar3918c952005-03-15 22:34:55 +00004750 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004751gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 gui.norm_pixel = gui_get_color(name);
4754 hl_set_fg_color_name(vim_strsave(name));
4755}
4756
Bram Moolenaar3918c952005-03-15 22:34:55 +00004757 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004758gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 gui.back_pixel = gui_get_color(name);
4761 hl_set_bg_color_name(vim_strsave(name));
4762}
4763
4764/*
4765 * Allocate a color by name.
4766 * Returns INVALCOLOR and gives an error message when failed.
4767 */
4768 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004769gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 guicolor_T t;
4772
4773 if (*name == NUL)
4774 return INVALCOLOR;
4775 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004778#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 && gui.in_use
4780#endif
4781 )
Drew Vogele30d1022021-10-24 20:35:07 +01004782 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 return t;
4784}
4785
4786/*
4787 * Return the grey value of a color (range 0-255).
4788 */
4789 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004790gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004792 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004794 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004795 + (((rgb >> 8) & 0xff) * 587)
4796 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797}
4798
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004799 char_u *
4800gui_bg_default(void)
4801{
4802 if (gui_get_lightness(gui.back_pixel) < 127)
4803 return (char_u *)"dark";
4804 return (char_u *)"light";
4805}
4806
4807/*
4808 * Option initializations that can only be done after opening the GUI window.
4809 */
4810 void
4811init_gui_options(void)
4812{
Bram Moolenaar30613902019-12-01 22:11:18 +01004813 // Set the 'background' option according to the lightness of the
4814 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004815 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4816 {
4817 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4818 highlight_changed();
4819 }
4820}
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#if defined(FEAT_GUI_X11) || defined(PROTO)
4823 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004824gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 win_T *wp;
4827
Bram Moolenaar30613902019-12-01 22:11:18 +01004828 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (!gui.in_use)
4830 return;
4831
4832 FOR_ALL_WINDOWS(wp)
4833 {
4834 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4835 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4836 }
4837 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4838}
4839#endif
4840
4841/*
4842 * Call this when focus has changed.
4843 */
4844 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004845gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
4847/*
4848 * Skip this code to avoid drawing the cursor when debugging and switching
4849 * between the debugger window and gvim.
4850 */
4851#if 1
4852 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004853 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855# ifdef FEAT_XIM
4856 xim_set_focus(in_focus);
4857# endif
4858
Bram Moolenaar30613902019-12-01 22:11:18 +01004859 // Put events in the input queue only when allowed.
4860 // ui_focus_change() isn't called directly, because it invokes
4861 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004862 if (!hold_gui_events)
4863 {
4864 char_u bytes[3];
4865
4866 bytes[0] = CSI;
4867 bytes[1] = KS_EXTRA;
4868 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4869 add_to_input_buf(bytes, 3);
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871#endif
4872}
4873
4874/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004875 * When mouse moved: apply 'mousefocus'.
4876 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004878 static void
4879gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004882 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004885 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004886 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
4888
Bram Moolenaar30613902019-12-01 22:11:18 +01004889 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004891 && !hold_gui_events // not holding events
4892 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4893 && State != HITRETURN // but not hit-return prompt
4894 && msg_scrolled == 0 // no scrolled message
4895 && !need_mouse_correct // not moving the pointer
4896 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004898 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (x < 0 || x > Columns * gui.char_width)
4900 return;
4901#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004902 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#endif
4904 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004905 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar30613902019-12-01 22:11:18 +01004907 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004908 if (Y_2_ROW(y) < tabline_height())
4909 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /*
4912 * format a mouse click on status line input
4913 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004914 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4915 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 */
4917 if (finish_op)
4918 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004919 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 st[0] = ESC;
4921 add_to_input_buf(st, 1);
4922 }
4923 st[0] = CSI;
4924 st[1] = KS_MOUSE;
4925 st[2] = KE_FILLER;
4926 st[3] = (char_u)MOUSE_LEFT;
4927 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004928 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 wp->w_height + W_WINROW(wp));
4930
4931 add_to_input_buf(st, 8);
4932 st[3] = (char_u)MOUSE_RELEASE;
4933 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004935 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (gtk_main_level() > 0)
4937 gtk_main_quit();
4938#endif
4939 }
4940}
4941
4942/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004943 * Called when the mouse moved (but not when dragging).
4944 */
4945 void
4946gui_mouse_moved(int x, int y)
4947{
4948 // Ignore this while still starting up.
4949 if (!gui.in_use || gui.starting)
4950 return;
4951
4952 // apply 'mousefocus' and pointer shape
4953 gui_mouse_focus(x, y);
4954
Ernie Raelc4cb5442022-04-03 15:47:28 +01004955 if (p_mousemev
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004956#ifdef FEAT_PROP_POPUP
Ernie Raelc4cb5442022-04-03 15:47:28 +01004957 || popup_uses_mouse_move
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004958#endif
Ernie Raelc4cb5442022-04-03 15:47:28 +01004959 )
4960 // Generate a mouse-moved event. For a <MouseMove> mapping. Or so the
4961 // popup can perhaps be closed, just like in the terminal.
4962 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004963}
4964
4965/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004966 * Get the window where the mouse pointer is on.
4967 * Returns NULL if not found.
4968 */
4969 win_T *
4970gui_mouse_window(mouse_find_T popup)
4971{
4972 int x, y;
4973
4974 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4975 return NULL;
4976 gui_mch_getmouse(&x, &y);
4977
4978 // Only use the mouse when it's on the Vim window
4979 if (x >= 0 && x <= Columns * gui.char_width
4980 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4981 return xy2win(x, y, popup);
4982 return NULL;
4983}
4984
4985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 * Called when mouse should be moved to window with focus.
4987 */
4988 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004989gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 win_T *wp = NULL;
4992
4993 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004994
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004995 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01004996 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004998 validate_cline_row();
4999 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
5000 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003}
5004
5005/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005006 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01005007 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005010xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 int row;
5013 int col;
5014 win_T *wp;
5015
5016 row = Y_2_ROW(y);
5017 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01005018 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005020 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005021 if (wp == NULL)
5022 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005023#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (State == HITRETURN || State == ASKMORE)
5025 {
5026 if (Y_2_ROW(y) >= msg_row)
5027 update_mouseshape(SHAPE_IDX_MOREL);
5028 else
5029 update_mouseshape(SHAPE_IDX_MORE);
5030 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005031 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005033 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005034 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005036 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005037 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 update_mouseshape(SHAPE_IDX_STATUS);
5039 else
5040 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005042 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043}
5044
5045/*
5046 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
5047 * File names may be given to redefine the args list.
5048 */
5049 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005050ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 char_u *arg = eap->arg;
5053
5054 /*
5055 * Check for "-f" argument: foreground, don't fork.
5056 * Also don't fork when started with "gvim -f".
5057 * Do fork when using "gui -b".
5058 */
5059 if (arg[0] == '-'
5060 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005061 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 gui.dofork = (arg[1] == 'b');
5064 eap->arg = skipwhite(eap->arg + 2);
5065 }
5066 if (!gui.in_use)
5067 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005068#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01005069 if (!gui.starting)
5070 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005071 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01005072 return;
5073 }
5074#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005075 // Clear the command. Needed for when forking+exiting, to avoid part
5076 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01005078#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005079 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005080 gui_start(eap->arg);
5081 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01005082#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005083 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01005084#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005085 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005088 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 ex_next(eap);
5090}
5091
Bram Moolenaar4f974752019-02-17 17:44:42 +01005092#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005093 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
5094 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005095 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01005097 * This is shared between Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100/*
5101 * Callback function for do_in_runtimepath().
5102 */
5103 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005104gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005106 char_u *gfp_buffer = cookie;
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (STRLEN(fname) >= MAXPATHL)
5109 *gfp_buffer = NUL;
5110 else
5111 STRCPY(gfp_buffer, fname);
5112}
5113
5114/*
5115 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5116 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5117 */
5118 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005119gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
5121 if (STRLEN(name) > MAXPATHL - 14)
5122 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005123 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005124 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005125 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 return FAIL;
5127 return OK;
5128}
5129
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005130# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131/*
5132 * Given the name of the "icon=" argument, try finding the bitmap file for the
5133 * icon. If it is an absolute path name, use it as it is. Otherwise append
5134 * "ext" and search for it in 'runtimepath'.
5135 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5136 * contains "name".
5137 */
5138 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005139gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 char_u buf[MAXPATHL + 1];
5142
5143 expand_env(name, buffer, MAXPATHL);
5144 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5145 STRCPY(buffer, buf);
5146}
5147# endif
5148#endif
5149
Bram Moolenaar9e175142020-04-30 22:51:01 +02005150#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5151 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005153display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154{
5155 char_u *p;
5156
5157 if (isatty(2))
5158 fflush(stderr);
5159 else if (error_ga.ga_data != NULL)
5160 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005161 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5163 if (!isspace(*p))
5164 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005165 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (STRLEN(p) > 2000)
5167 STRCPY(p + 2000 - 14, "...(truncated)");
5168 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005169 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171 }
5172 ga_clear(&error_ga);
5173 }
5174}
5175#endif
5176
5177#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5178/*
5179 * Return TRUE if still starting up and there is no place to enter text.
5180 * For GTK and X11 we check if stderr is not a tty, which means we were
5181 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5182 * allow typing on stdin.
5183 */
5184 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005185no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 return ((!gui.in_use || gui.starting)
5188# ifndef NO_CONSOLE
5189 && !isatty(0) && !isatty(2)
5190# endif
5191 );
5192}
5193#endif
5194
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005195#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005196 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005197 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198/*
5199 * Update the current window and the screen.
5200 */
5201 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005202gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005204# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005205 linenr_T conceal_old_cursor_line = 0;
5206 linenr_T conceal_new_cursor_line = 0;
5207 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005208# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 update_topline();
5211 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005212
Bram Moolenaar30613902019-12-01 22:11:18 +01005213 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005214 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005215# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005216 || popup_visible
5217# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005218# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005219 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005220# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005221 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005222 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005223 if (has_cursormoved())
5224 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
K.Takata6e1d31e2022-02-03 13:05:32 +00005225# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005226 if (popup_visible)
5227 popup_check_cursor_pos();
K.Takata6e1d31e2022-02-03 13:05:32 +00005228# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005229# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005230 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005231 {
5232 conceal_old_cursor_line = last_cursormoved.lnum;
5233 conceal_new_cursor_line = curwin->w_cursor.lnum;
5234 conceal_update_lines = TRUE;
5235 }
5236# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005237 last_cursormoved = curwin->w_cursor;
5238 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005239
LemonBoy09371822022-04-08 15:18:45 +01005240 if (!finish_op)
zeertzjqd58862d2022-04-12 11:32:48 +01005241 may_trigger_winscrolled();
LemonBoy09371822022-04-08 15:18:45 +01005242
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005243# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005244 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005245 && (conceal_old_cursor_line != conceal_new_cursor_line
5246 || conceal_cursor_line(curwin)
5247 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005248 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005249 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005250 redrawWinline(curwin, conceal_old_cursor_line);
5251 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005252 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005253 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005254 }
5255# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005256 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005257 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005258 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259}
5260#endif
5261
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005262#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263/*
5264 * Get the text to use in a find/replace dialog. Uses the last search pattern
5265 * if the argument is empty.
5266 * Returns an allocated string.
5267 */
5268 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005269get_find_dialog_text(
5270 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005271 int *wwordp, // return: TRUE if \< \> found
5272 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
5274 char_u *text;
5275
5276 if (*arg == NUL)
5277 text = last_search_pat();
5278 else
5279 text = arg;
5280 if (text != NULL)
5281 {
5282 text = vim_strsave(text);
5283 if (text != NULL)
5284 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005285 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 int i;
5287
Bram Moolenaar30613902019-12-01 22:11:18 +01005288 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5290 {
5291 mch_memmove(text, text + 2, (size_t)(len - 1));
5292 len -= 2;
5293 }
5294
Bram Moolenaar30613902019-12-01 22:11:18 +01005295 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5297 {
5298 *mcasep = (text[1] == 'C');
5299 mch_memmove(text, text + 2, (size_t)(len - 1));
5300 len -= 2;
5301 }
5302
Bram Moolenaar30613902019-12-01 22:11:18 +01005303 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (len >= 4
5305 && STRNCMP(text, "\\<", 2) == 0
5306 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5307 {
5308 *wwordp = TRUE;
5309 mch_memmove(text, text + 2, (size_t)(len - 4));
5310 text[len - 4] = NUL;
5311 }
5312
Bram Moolenaar30613902019-12-01 22:11:18 +01005313 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 for (i = 0; i + 1 < len; ++i)
5315 if (text[i] == '\\' && (text[i + 1] == '/'
5316 || text[i + 1] == '?'))
5317 {
5318 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5319 --len;
5320 }
5321 }
5322 }
5323 return text;
5324}
5325
5326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 * Handle the press of a button in the find-replace dialog.
5328 * Return TRUE when something was added to the input buffer.
5329 */
5330 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005331gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005332 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005333 char_u *find_text,
5334 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005335 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 garray_T ga;
5338 int i;
5339 int type = (flags & FRD_TYPE_MASK);
5340 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005341 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005342 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005343 static int busy = FALSE;
5344
Bram Moolenaar30613902019-12-01 22:11:18 +01005345 // When the screen is being updated we should not change buffers and
5346 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005347 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005348 if (updating_screen || busy)
5349 return FALSE;
5350
Bram Moolenaar30613902019-12-01 22:11:18 +01005351 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005352 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5353 return FALSE;
5354
5355 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005358 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 ga_concat(&ga, (char_u *)"%s/");
5360
5361 ga_concat(&ga, (char_u *)"\\V");
5362 if (flags & FRD_MATCH_CASE)
5363 ga_concat(&ga, (char_u *)"\\C");
5364 else
5365 ga_concat(&ga, (char_u *)"\\c");
5366 if (flags & FRD_WHOLE_WORD)
5367 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005368 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005369 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5370 if (p != NULL)
5371 ga_concat(&ga, p);
5372 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 if (flags & FRD_WHOLE_WORD)
5374 ga_concat(&ga, (char_u *)"\\>");
5375
5376 if (type == FRD_REPLACEALL)
5377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005379 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005380 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5381 if (p != NULL)
5382 ga_concat(&ga, p);
5383 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005385 }
5386 ga_append(&ga, NUL);
5387
5388 if (type == FRD_REPLACE)
5389 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005390 // Do the replacement when the text at the cursor matches. Thus no
5391 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005392 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5393 regmatch.rm_ic = 0;
5394 if (regmatch.regprog != NULL)
5395 {
5396 p = ml_get_cursor();
5397 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5398 && regmatch.startp[0] == p)
5399 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005400 // Clear the command line to remove any old "No match"
5401 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005402 msg_end_prompt();
5403
5404 if (u_save_cursor() == OK)
5405 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005406 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005407 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005408
5409 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005410 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005411 ins_str(repl_text);
5412 }
5413 }
5414 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005415 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005416 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005417 }
5418 }
5419
5420 if (type == FRD_REPLACEALL)
5421 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005422 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005423 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 do_cmdline_cmd(ga.ga_data);
5425 }
5426 else
5427 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005428 int searchflags = SEARCH_MSG + SEARCH_MARK;
5429
Bram Moolenaar30613902019-12-01 22:11:18 +01005430 // Search for the next match.
5431 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005432 if (type == FRD_REPLACE)
5433 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005435 if (down)
5436 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01005437 (void)do_search(NULL, '/', '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005438 }
5439 else
5440 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005441 // We need to escape '?' if and only if we are searching in the up
5442 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005443 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5444 if (p != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01005445 (void)do_search(NULL, '?', '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005446 vim_free(p);
5447 }
5448
Bram Moolenaar30613902019-12-01 22:11:18 +01005449 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451
Bram Moolenaar30613902019-12-01 22:11:18 +01005452 // Don't want to pass did_emsg to other code, it may cause disabling
5453 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005454 did_emsg = save_did_emsg;
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (State & (NORMAL | INSERT))
5457 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005458 gui_update_screen(); // update the screen
5459 msg_didout = 0; // overwrite any message
5460 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462
5463 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005464 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return (ga.ga_len > 0);
5466}
5467
5468#endif
5469
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005470#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471/*
5472 * Jump to the window at specified point (x, y).
5473 */
5474 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005475gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 int row = Y_2_ROW(y);
5478 int col = X_2_COL(x);
5479 win_T *wp;
5480
5481 if (row >= 0 && col >= 0)
5482 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005483 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if (wp != NULL && wp != curwin)
5485 win_goto(wp);
5486 }
5487}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005490 * Function passed to handle_drop() for the actions to be done after the
5491 * argument list has been updated.
5492 */
5493 static void
5494drop_callback(void *cookie)
5495{
5496 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005497 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005498
Bram Moolenaar30613902019-12-01 22:11:18 +01005499 // If Shift held down, change to first file's directory. If the first
5500 // item is a directory, change to that directory (and let the explorer
5501 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005502 if (p != NULL)
5503 {
5504 if (mch_isdir(p))
5505 {
5506 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005507 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005508 }
5509 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005510 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005511 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005512 if (do_shorten)
5513 {
5514 shorten_fnames(TRUE);
5515 last_chdir_reason = "drop";
5516 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005517 }
5518
Bram Moolenaar30613902019-12-01 22:11:18 +01005519 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005520 update_screen(NOT_VALID);
5521# ifdef FEAT_MENU
5522 gui_update_menus(0);
5523# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005524 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005525 setcursor();
5526 out_flush_cursor(FALSE, FALSE);
5527}
5528
5529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 * Process file drop. Mouse cursor position, key modifiers, name of files
5531 * and count of files are given. Argument "fnames[count]" has full pathnames
5532 * of dropped files, they will be freed in this function, and caller can't use
5533 * fnames after call this function.
5534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005536gui_handle_drop(
5537 int x UNUSED,
5538 int y UNUSED,
5539 int_u modifiers,
5540 char_u **fnames,
5541 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 int i;
5544 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005545 static int entered = FALSE;
5546
5547 /*
5548 * This function is called by event handlers. Just in case we get a
5549 * second event before the first one is handled, ignore the second one.
5550 * Not sure if this can ever happen, just in case.
5551 */
5552 if (entered)
5553 return;
5554 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
5556 /*
5557 * When the cursor is at the command line, add the file names to the
5558 * command line, don't edit the files.
5559 */
5560 if (State & CMDLINE)
5561 {
5562 shorten_filenames(fnames, count);
5563 for (i = 0; i < count; ++i)
5564 {
5565 if (fnames[i] != NULL)
5566 {
5567 if (i > 0)
5568 add_to_input_buf((char_u*)" ", 1);
5569
Bram Moolenaar30613902019-12-01 22:11:18 +01005570 // We don't know what command is used thus we can't be sure
5571 // about which characters need to be escaped. Only escape the
5572 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573# ifdef BACKSLASH_IN_FILENAME
5574 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5575# else
5576 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5577# endif
5578 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005579 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 vim_free(p);
5581 vim_free(fnames[i]);
5582 }
5583 }
5584 vim_free(fnames);
5585 }
5586 else
5587 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005588 // Go to the window under mouse cursor, then shorten given "fnames" by
5589 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 shorten_filenames(fnames, count);
5592
Bram Moolenaar30613902019-12-01 22:11:18 +01005593 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 if ((modifiers & MOUSE_SHIFT) != 0)
5595 p = vim_strsave(fnames[0]);
5596 else
5597 p = NULL;
5598
Bram Moolenaar30613902019-12-01 22:11:18 +01005599 // Handle the drop, :edit or :split to get to the file. This also
5600 // frees fnames[]. Skip this if there is only one item, it's a
5601 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5603 && mch_isdir(fnames[0]))
5604 {
5605 vim_free(fnames[0]);
5606 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005607 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005610 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005611 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005613
5614 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005617
5618/*
5619 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5620 * applied yet.
5621 * Return the key with modifiers applied if so, NUL if not.
5622 */
5623 int
5624check_for_interrupt(int key, int modifiers_arg)
5625{
5626 int modifiers = modifiers_arg;
5627 int c = merge_modifyOtherKeys(key, &modifiers);
5628
5629 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005630#ifdef UNIX
5631 || (intr_char != Ctrl_C && c == intr_char)
5632#endif
5633 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005634 {
5635 got_int = TRUE;
5636 return c;
5637 }
5638 return NUL;
5639}
5640