blob: 0cce1f111a248ed7e7ee279cc9831a585fa7eb76 [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/*
56 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
57 * this makes the thumb indicate the part of the text that is shown. Motif
58 * can't do this.
59 */
Bram Moolenaar097148e2020-08-11 21:58:20 +020060#if defined(FEAT_GUI_ATHENA)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define SCROLL_PAST_END
62#endif
63
64/*
65 * gui_start -- Called when user wants to start the GUI.
66 *
67 * Careful: This function can be called recursively when there is a ":gui"
68 * command in the .gvimrc file. Only the first call should fork, not the
69 * recursive call.
70 */
71 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020072gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
74 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020076#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020077 char *msg = NULL;
78#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80 old_term = vim_strsave(T_NAME);
81
Bram Moolenaar30613902019-12-01 22:11:18 +010082 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010084 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 full_screen = FALSE;
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 ++recursive;
88
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020089#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 /*
91 * Quit the current process and continue in the child.
92 * Makes "gvim file" disconnect from the shell it was started in.
93 * Don't do this when Vim was started with "-f" or the 'f' flag is present
94 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020095 * Don't do this when there is a running job, we can only get the status
96 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020097 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020098 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
99# ifdef FEAT_JOB_CHANNEL
100 && !job_any_running()
101# endif
102 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200103 {
104 gui_do_fork();
105 }
106 else
107#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200108#ifdef GUI_MAY_SPAWN
109 if (gui.dospawn
110# ifdef EXPERIMENTAL_GUI_CMD
111 && gui.dofork
112# endif
113 && !vim_strchr(p_go, GO_FORG)
114 && !anyBufIsChanged()
115# ifdef FEAT_JOB_CHANNEL
116 && !job_any_running()
117# endif
118 )
119 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200120# ifdef EXPERIMENTAL_GUI_CMD
121 msg =
122# endif
123 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200124 }
125 else
126#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200127 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200128#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100129 // If there is 'f' in 'guioptions' and specify -g argument,
130 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200131 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100132 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200133#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200134 gui_attempt_start();
135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar30613902019-12-01 22:11:18 +0100137 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100139 // Back to old term settings
140 //
141 // FIXME: If we got here because a child process failed and flagged to
Bram Moolenaar651fca82021-11-29 20:39:38 +0000142 // the parent to resume, and X11 is enabled, this will
Bram Moolenaar30613902019-12-01 22:11:18 +0100143 // hit an X11 I/O error and do a longjmp(), leaving recursive
144 // permanently set to 1. This is probably not as big a problem as it
145 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
146 // return "OK" unconditionally, so it would be very difficult to
147 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200148 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100149 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200151#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
152 if (msg)
153 emsg(msg);
154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 }
156
157 vim_free(old_term);
158
Bram Moolenaar30613902019-12-01 22:11:18 +0100159 // If the GUI started successfully, trigger the GUIEnter event, otherwise
160 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200161 gui_mch_update();
162 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
163 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200164 --recursive;
165}
166
167/*
168 * Set_termname() will call gui_init() to start the GUI.
169 * Set the "starting" flag, to indicate that the GUI will start.
170 *
171 * We don't want to open the GUI shell until after we've read .gvimrc,
172 * otherwise we don't know what font we will use, and hence we don't know
173 * what size the shell should be. So if there are errors in the .gvimrc
174 * file, they will have to go to the terminal: Set full_screen to FALSE.
175 * full_screen will be set to TRUE again by a successful termcapinit().
176 */
177 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100178gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200179{
180 static int recursive = 0;
181
182 ++recursive;
183 gui.starting = TRUE;
184
185#ifdef FEAT_GUI_GTK
186 gui.event_time = GDK_CURRENT_TIME;
187#endif
188
189 termcapinit((char_u *)"builtin_gui");
190 gui.starting = recursive - 1;
191
Bram Moolenaar9372a112005-12-06 19:59:18 +0000192#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200194 {
195# ifdef FEAT_EVAL
196 Window x11_window;
197 Display *x11_display;
198
199 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
200 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
201# endif
202
Bram Moolenaar30613902019-12-01 22:11:18 +0100203 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 --recursive;
208}
209
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200210#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200211
Bram Moolenaar30613902019-12-01 22:11:18 +0100212// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200213# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
214# include <sys/wait.h>
215# endif
216
217/*
218 * Create a new process, by forking. In the child, start the GUI, and in
219 * the parent, exit.
220 *
221 * If something goes wrong, this will return with gui.in_use still set
222 * to FALSE, in which case the caller should continue execution without
223 * the GUI.
224 *
225 * If the child fails to start the GUI, then the child will exit and the
226 * parent will return. If the child succeeds, then the parent will exit
227 * and the child will return.
228 */
229 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100230gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200231{
Bram Moolenaar30613902019-12-01 22:11:18 +0100232 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200233 int pipe_error;
234 int status;
235 int exit_status;
236 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200237
Bram Moolenaar30613902019-12-01 22:11:18 +0100238 // Setup a pipe between the child and the parent, so that the parent
239 // knows when the child has done the setsid() call and is allowed to
240 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200241 pipe_error = (pipe(pipefd) < 0);
242 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100243 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200244 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000245 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200246 return;
247 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100248 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200249 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100250 // Give the child some time to do the setsid(), otherwise the
251 // exit() may kill the child too (when starting gvim from inside a
252 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200253 if (!pipe_error)
254 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100255 // The read returns when the child closes the pipe (or when
256 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200257 close(pipefd[1]);
258 status = gui_read_child_pipe(pipefd[0]);
259 if (status == GUI_CHILD_FAILED)
260 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100261 // The child failed to start the GUI, so the caller must
262 // continue. There may be more error information written
263 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200264# ifdef __NeXT__
265 wait4(pid, &exit_status, 0, (struct rusage *)0);
266# else
267 waitpid(pid, &exit_status, 0);
268# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000269 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200270 return;
271 }
272 else if (status == GUI_CHILD_IO_ERROR)
273 {
274 pipe_error = TRUE;
275 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100276 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200277 }
278
279 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100280 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200281
Bram Moolenaar30613902019-12-01 22:11:18 +0100282 // When swapping screens we may need to go to the next line, e.g.,
283 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200284 if (newline_on_exit)
285 mch_errmsg("\r\n");
286
287 /*
288 * The parent must skip the normal exit() processing, the child
289 * will do it. For example, GTK messes up signals when exiting.
290 */
291 _exit(0);
292 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100293 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200294
Bram Moolenaar29695702012-05-18 17:03:18 +0200295#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100296 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200297 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100298 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200299#endif
300
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200301# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
302 /*
303 * Change our process group. On some systems/shells a CTRL-C in the
304 * shell where Vim was started would otherwise kill gvim!
305 */
306# if defined(HAVE_SETSID)
307 (void)setsid();
308# else
309 (void)setpgid(0, 0);
310# endif
311# endif
312 if (!pipe_error)
313 close(pipefd[0]);
314
315# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100316 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200317 gui_mch_forked();
318# endif
319
Bram Moolenaar30613902019-12-01 22:11:18 +0100320 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200321 gui_attempt_start();
322
Bram Moolenaar30613902019-12-01 22:11:18 +0100323 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200324 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200325 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200326 if (gui.in_use)
327 write_eintr(pipefd[1], "ok", 3);
328 else
329 write_eintr(pipefd[1], "fail", 5);
330 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 }
332
Bram Moolenaar30613902019-12-01 22:11:18 +0100333 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200334 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100335 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200336}
337
338/*
339 * Read from a pipe assumed to be connected to the child process (this
340 * function is called from the parent).
341 * Return GUI_CHILD_OK if the child successfully started the GUI,
342 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
343 * some other error.
344 *
345 * The file descriptor will be closed before the function returns.
346 */
347 static int
348gui_read_child_pipe(int fd)
349{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200350 long bytes_read;
351#define READ_BUFFER_SIZE 10
352 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200353
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200354 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
355#undef READ_BUFFER_SIZE
356 close(fd);
357 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200358 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200359 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200360 if (strcmp(buffer, "ok") == 0)
361 return GUI_CHILD_OK;
362 return GUI_CHILD_FAILED;
363}
364
Bram Moolenaar30613902019-12-01 22:11:18 +0100365#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/*
368 * Call this when vim starts up, whether or not the GUI is started
369 */
370 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100371gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
Bram Moolenaar30613902019-12-01 22:11:18 +0100373 gui.in_use = FALSE; // No GUI yet (maybe later)
374 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 gui_mch_prepare(argc, argv);
376}
377
378/*
379 * Try initializing the GUI and check if it can be started.
380 * Used from main() to check early if "vim -g" can start the GUI.
381 * Used from gui_init() to prepare for starting the GUI.
382 * Returns FAIL or OK.
383 */
384 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100385gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 static int result = MAYBE;
388
389 if (result != MAYBE)
390 {
391 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000392 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 return result;
394 }
395
396 gui.shell_created = FALSE;
397 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100398 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 gui.dragged_sb = SBAR_NONE;
400 gui.dragged_wp = NULL;
401 gui.pointer_hidden = FALSE;
402 gui.col = 0;
403 gui.row = 0;
404 gui.num_cols = Columns;
405 gui.num_rows = Rows;
406
407 gui.cursor_is_valid = FALSE;
408 gui.scroll_region_top = 0;
409 gui.scroll_region_bot = Rows - 1;
410 gui.scroll_region_left = 0;
411 gui.scroll_region_right = Columns - 1;
412 gui.highlight_mask = HL_NORMAL;
413 gui.char_width = 1;
414 gui.char_height = 1;
415 gui.char_ascent = 0;
416 gui.border_width = 0;
417
418 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200419#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 gui.bold_font = NOFONT;
421 gui.ital_font = NOFONT;
422 gui.boldital_font = NOFONT;
423# ifdef FEAT_XFONTSET
424 gui.fontset = NOFONTSET;
425# endif
426#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200427 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100428#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200429 gui.wide_bold_font = NOFONT;
430 gui.wide_ital_font = NOFONT;
431 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200435# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# ifdef FONTSET_ALWAYS
437 gui.menu_fontset = NOFONTSET;
438# else
439 gui.menu_font = NOFONT;
440# endif
441# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100442 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# ifndef FEAT_GUI_GTK
444 gui.menu_height = MENU_DEFAULT_HEIGHT;
445 gui.menu_width = 0;
446# endif
447#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100448#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
449 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 gui.toolbar_height = 0;
451#endif
452#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
453 gui.footer_height = 0;
454#endif
455#ifdef FEAT_BEVAL_TIP
456 gui.tooltip_fontset = NOFONTSET;
457#endif
458
459 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
460 gui.prev_wrap = -1;
461
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100462# ifdef FEAT_GUI_GTK
463 CLEAR_FIELD(gui.ligatures_map);
464#endif
465
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200466#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 result = OK;
468#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200469# ifdef FEAT_GUI_GTK
470 /*
471 * Note: Don't call gtk_init_check() before fork, it will be called after
472 * the fork. When calling it before fork, it make vim hang for a while.
473 * See gui_do_fork().
474 * Use a simpler check if the GUI window can probably be opened.
475 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200476 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200477# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#endif
481 return result;
482}
483
484/*
485 * This is the call which starts the GUI.
486 */
487 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100488gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 win_T *wp;
491 static int recursive = 0;
492
493 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000494 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * function will then be executed at the first call, the rest by the
496 * recursive call. This allow the shell to be opened halfway reading a
497 * gvimrc file.
498 */
499 if (!recursive)
500 {
501 ++recursive;
502
503 clip_init(TRUE);
504
Bram Moolenaar30613902019-12-01 22:11:18 +0100505 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (gui_init_check() == FAIL)
507 {
508 --recursive;
509 clip_init(FALSE);
510 return;
511 }
512
513 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000514 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
515 * breaks the Paste toolbar button.
516 */
517 set_option_value((char_u *)"paste", 0L, NULL, 0);
518
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200519 // Set t_Co to the number of colors: RGB.
520 set_color_count(256 * 256 * 256);
521
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000522 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 * Set up system-wide default menus.
524 */
525#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
526 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
527 {
528 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 sys_menu = FALSE;
531 }
532#endif
533
534 /*
535 * Switch on the mouse by default, unless the user changed it already.
536 * This can then be changed in the .gvimrc.
537 */
538 if (!option_was_set((char_u *)"mouse"))
539 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000540 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * If -U option given, use only the initializations from that file and
544 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
545 */
546 if (use_gvimrc != NULL)
547 {
548 if (STRCMP(use_gvimrc, "NONE") != 0
549 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000551 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553 else
554 {
555 /*
556 * Get system wide defaults for gvim, only when file name defined.
557 */
558#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561
562 /*
563 * Try to read GUI initialization commands from the following
564 * places:
565 * - environment variable GVIMINIT
566 * - the user gvimrc file (~/.gvimrc)
567 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
568 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
569 * The first that exists is used, the rest is ignored.
570 */
571 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000572 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000575 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200578#ifdef USR_GVIMRC_FILE3
579 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 )
583 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200584#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
586 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#endif
588 }
589
590 /*
591 * Read initialization commands from ".gvimrc" in current
592 * directory. This is only done if the 'exrc' option is set.
593 * Because of security reasons we disallow shell and write
594 * commands now, except for unix if the file is owned by the user
595 * or 'secure' option has been reset in environment of global
596 * ".gvimrc".
597 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
598 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
599 */
600 if (p_exrc)
601 {
602#ifdef UNIX
603 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200604 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar30613902019-12-01 22:11:18 +0100606 // if ".gvimrc" file is not owned by user, set 'secure'
607 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
609 secure = p_secure;
610 }
611#else
612 secure = p_secure;
613#endif
614
615 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200616 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef SYS_GVIMRC_FILE
618 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200619 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#endif
621#ifdef USR_GVIMRC_FILE2
622 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200623 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#endif
625#ifdef USR_GVIMRC_FILE3
626 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200627 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200629#ifdef USR_GVIMRC_FILE4
630 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200631 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 if (secure == 2)
637 need_wait_return = TRUE;
638 secure = 0;
639 }
640 }
641
642 if (need_wait_return || msg_didany)
643 wait_return(TRUE);
644
645 --recursive;
646 }
647
Bram Moolenaar30613902019-12-01 22:11:18 +0100648 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (gui.in_use)
650 return;
651
652 /*
653 * Create the GUI shell.
654 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100655 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (gui_mch_init() == FAIL)
657 goto error;
658
Bram Moolenaar30613902019-12-01 22:11:18 +0100659 // Avoid a delay for an error message that was printed in the terminal
660 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 emsg_on_display = FALSE;
662 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100663 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 need_wait_return = FALSE;
665 msg_didany = FALSE;
666
667 /*
668 * Check validity of any generic resources that may have been loaded.
669 */
670 if (gui.border_width < 0)
671 gui.border_width = 0;
672
673 /*
674 * Set up the fonts. First use a font specified with "-fn" or "-font".
675 */
676 if (font_argument != NULL)
677 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
678 if (
679#ifdef FEAT_XFONTSET
680 (*p_guifontset == NUL
681 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
682#endif
683 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
684 : p_guifont, FALSE) == FAIL)
685 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000686 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 goto error2;
688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000690 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 gui.num_cols = Columns;
693 gui.num_rows = Rows;
694 gui_reset_scroll_region();
695
Bram Moolenaar30613902019-12-01 22:11:18 +0100696 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 FOR_ALL_WINDOWS(wp)
698 {
699 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
700 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
701 }
702 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
703
704#ifdef FEAT_MENU
705 gui_create_initial_menus(root_menu);
706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef FEAT_SIGN_ICONS
708 sign_gui_started();
709#endif
710
Bram Moolenaar30613902019-12-01 22:11:18 +0100711 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 gui_init_which_components(NULL);
713
Bram Moolenaar30613902019-12-01 22:11:18 +0100714 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 gui.shell_created = TRUE;
716
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100717#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100718 // Set the shell size, adjusted for the screen size. For GTK this only
719 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100720 // If the window is already maximized (e.g. when --windowid is passed in),
721 // we want to use the system-provided dimensions by passing FALSE to
722 // mustset. Otherwise, we want to initialize with the default rows/columns.
723 if (gui_mch_maximized())
724 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
725 else
726 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100727#else
728# ifndef FEAT_GUI_GTK
729 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#endif
732#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100733 // Need to set the size of the menubar after all the menus have been
734 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 gui_mch_compute_menu_height((Widget)0);
736#endif
737
738 /*
739 * Actually open the GUI shell.
740 */
741 if (gui_mch_open() != FAIL)
742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 maketitle();
744 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 init_gui_options();
747#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100748 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 p_tbidi = FALSE;
750#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000751#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100752 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000754
755# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100756 // If there is no 'm' in 'guioptions' we need to remove the menu now.
757 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000758 if (vim_strchr(p_go, GO_MENUS) == NULL)
759 {
760 --gui.starting;
761 gui_mch_enable_menu(FALSE);
762 ++gui.starting;
763 gui_mch_update();
764 }
765# endif
766
Bram Moolenaar30613902019-12-01 22:11:18 +0100767 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100768 if (gui_mch_maximized())
769 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
770 else
771 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100773 // When 'lines' was set while starting up the topframe may have to be
774 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000775 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000776
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100777#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100778 // Always create the Balloon Evaluation area, but disable it when
779 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100780 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200781 {
782# ifdef FEAT_VARTABS
783 vim_free(balloonEval->vts);
784# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100785 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200786 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100787 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000788# ifdef FEAT_GUI_GTK
789 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
790 &general_beval_cb, NULL);
791# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000792# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000793 {
794 extern Widget textArea;
795 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000796 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000797 }
798# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100799# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000800 balloonEval = gui_mch_create_beval_area(NULL, NULL,
801 &general_beval_cb, NULL);
802# endif
803# endif
804# endif
805 if (!p_beval)
806 gui_mch_disable_beval_area(balloonEval);
807#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200808
809#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200810 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200811 // Don't do this for MS-Windows yet, it sends CTRL-K without the
812 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200813 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200814#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
817 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000818 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100820 // When 'cmdheight' was set during startup it may not have taken
821 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000822 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000823 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 return;
826 }
827
828error2:
829#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100830 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 gui_mch_uninit();
832#endif
833
834error:
835 gui.in_use = FALSE;
836 clip_init(FALSE);
837}
838
839
840 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100841gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar30613902019-12-01 22:11:18 +0100843 // don't free the fonts, it leads to a BUS error
844 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui.in_use = FALSE;
847 gui_mch_exit(rc);
848}
849
Bram Moolenaar9372a112005-12-06 19:59:18 +0000850#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200851 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000852# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853/*
854 * Called when the GUI shell is closed by the user. If there are no changed
855 * files Vim exits, otherwise there will be a dialog to ask the user what to
856 * do.
857 * When this function returns, Vim should NOT exit!
858 */
859 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100860gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200862 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
Bram Moolenaar3552e742021-05-29 12:21:58 +0200864 if (before_quit_autocmds(curwin, TRUE, FALSE))
865 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaar30613902019-12-01 22:11:18 +0100867 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 exiting = TRUE;
869# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200870 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# endif
872# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200873 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100875 // If there are changed buffers, present the user with a dialog if
876 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100877 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 getout(0);
879
880 exiting = FALSE;
881 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100882 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884#endif
885
886/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200887 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * first font name that works is used. If none is found, use the default
889 * font.
890 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
891 * Return OK when able to set the font. When it failed FAIL is returned and
892 * the fonts are unchanged.
893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100895gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897#define FONTLEN 320
898 char_u font_name[FONTLEN];
899 int font_list_empty = FALSE;
900 int ret = FAIL;
901
902 if (!gui.in_use)
903 return FAIL;
904
905 font_name[0] = NUL;
906 if (*font_list == NUL)
907 font_list_empty = TRUE;
908 else
909 {
910#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100911 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (fontset)
913 ret = gui_mch_init_font(font_list, TRUE);
914 else
915#endif
916 while (*font_list != NUL)
917 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100918 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
920
Bram Moolenaar30613902019-12-01 22:11:18 +0100921 // Careful!!! The Win32 version of gui_mch_init_font(), when
922 // called with "*" will change p_guifont to the selected font
923 // name, which frees the old value. This makes font_list
924 // invalid. Thus when OK is returned here, font_list must no
925 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (gui_mch_init_font(font_name, FALSE) == OK)
927 {
K.Takata94373c42022-01-27 15:04:22 +0000928#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100929 // If it's a Unicode font, try setting 'guifontwide' to a
930 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
932 && strstr((char *)font_name, "10646") != NULL)
933 set_guifontwide(font_name);
934#endif
935 ret = OK;
936 break;
937 }
938 }
939 }
940
941 if (ret != OK
942 && STRCMP(font_list, "*") != 0
943 && (font_list_empty || gui.norm_font == NOFONT))
944 {
945 /*
946 * Couldn't load any font in 'font_list', keep the current font if
947 * there is one. If 'font_list' is empty, or if there is no current
948 * font, tell gui_mch_init_font() to try to find a font we can load.
949 */
950 ret = gui_mch_init_font(NULL, FALSE);
951 }
952
953 if (ret == OK)
954 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200955#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100956 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957# ifdef FEAT_XFONTSET
958 if (gui.fontset != NOFONTSET)
959 gui_mch_set_fontset(gui.fontset);
960 else
961# endif
962 gui_mch_set_font(gui.norm_font);
963#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100964 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966
967 return ret;
968}
969
K.Takata94373c42022-01-27 15:04:22 +0000970#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Try setting 'guifontwide' to a font twice as wide as "name".
973 */
974 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100975set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100978 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 char_u *wp = NULL;
980 char_u *p;
981 GuiFont font;
982
983 wp = wide_name;
984 for (p = name; *p != NUL; ++p)
985 {
986 *wp++ = *p;
987 if (*p == '-')
988 {
989 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100990 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 if (p[1] == '-')
993 *wp++ = '*';
994 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100995 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
997 ++p;
998 i = getdigits(&p);
999 if (i != 0)
1000 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001001 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 sprintf((char *)wp, "%d%s", i * 2, p);
1003 font = gui_mch_get_font(wide_name, FALSE);
1004 if (font != NOFONT)
1005 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001006 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 gui.wide_font = font;
1008 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001009 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012 break;
1013 }
1014 }
1015 }
1016}
K.Takata94373c42022-01-27 15:04:22 +00001017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019/*
1020 * Get the font for 'guifontwide'.
1021 * Return FAIL for an invalid font name.
1022 */
1023 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001024gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026 GuiFont font = NOFONT;
1027 char_u font_name[FONTLEN];
1028 char_u *p;
1029
Bram Moolenaar30613902019-12-01 22:11:18 +01001030 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1031 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1034 {
1035 for (p = p_guifontwide; *p != NUL; )
1036 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001037 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1039 font = gui_mch_get_font(font_name, FALSE);
1040 if (font != NOFONT)
1041 break;
1042 }
1043 if (font == NOFONT)
1044 return FAIL;
1045 }
1046
1047 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001049 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (font != NOFONT && gui.norm_font != NOFONT
1051 && pango_font_description_equal(font, gui.norm_font))
1052 {
1053 gui.wide_font = NOFONT;
1054 gui_mch_free_font(font);
1055 }
1056 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001059#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001060 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001062 /*
1063 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1064 * support those fonts for 'guifontwide'.
1065 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return OK;
1068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001070#if defined(FEAT_GUI_GTK) || defined(PROTO)
1071/*
1072 * Set list of ascii characters that combined can create ligature.
1073 * Store them in char map for quick access from gui_gtk2_draw_string.
1074 */
1075 void
1076gui_set_ligatures(void)
1077{
1078 char_u *p;
1079
1080 if (*p_guiligatures != NUL)
1081 {
1082 // check for invalid characters
1083 for (p = p_guiligatures; *p != NUL; ++p)
1084 if (*p < 32 || *p > 127)
1085 {
1086 emsg(_(e_ascii_code_not_in_range));
1087 return;
1088 }
1089
1090 // store valid setting into ligatures_map
1091 CLEAR_FIELD(gui.ligatures_map);
1092 for (p = p_guiligatures; *p != NUL; ++p)
1093 gui.ligatures_map[*p] = 1;
1094 }
1095 else
1096 CLEAR_FIELD(gui.ligatures_map);
1097}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001098
1099/*
1100 * Adjust the columns to undraw for when the cursor is on ligatures.
1101 */
1102 static void
1103gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1104{
1105 int off;
1106
1107 if (ScreenLines == NULL || *p_guiligatures == NUL)
1108 return;
1109
1110 // expand before the cursor for all the chars in gui.ligatures_map
1111 off = LineOffset[gui.cursor_row] + *startcol;
1112 if (gui.ligatures_map[ScreenLines[off]])
1113 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1114 (*startcol)--;
1115
1116 // expand after the cursor for all the chars in gui.ligatures_map
1117 off = LineOffset[gui.cursor_row] + *endcol;
1118 if (gui.ligatures_map[ScreenLines[off]])
1119 while (*endcol < ((int)screen_Columns - 1)
1120 && gui.ligatures_map[ScreenLines[++off]])
1121 (*endcol)++;
1122}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001123#endif
1124
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001125 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001126gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 gui.row = row;
1129 gui.col = col;
1130}
1131
1132/*
1133 * gui_check_pos - check if the cursor is on the screen.
1134 */
1135 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001136gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 if (gui.row >= screen_Rows)
1139 gui.row = screen_Rows - 1;
1140 if (gui.col >= screen_Columns)
1141 gui.col = screen_Columns - 1;
1142 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1143 gui.cursor_is_valid = FALSE;
1144}
1145
1146/*
1147 * Redraw the cursor if necessary or when forced.
1148 * Careful: The contents of ScreenLines[] must match what is on the screen,
1149 * otherwise this goes wrong. May need to call out_flush() first.
1150 */
1151 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001152gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001153 int force, // when TRUE, update even when not moved
1154 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155{
1156 int cur_width = 0;
1157 int cur_height = 0;
1158 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001159 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001161#ifdef FEAT_TERMINAL
1162 guicolor_T shape_fg = INVALCOLOR;
1163 guicolor_T shape_bg = INVALCOLOR;
1164#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001165 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1166 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 int attr;
1168 attrentry_T *aep = NULL;
1169
Bram Moolenaar30613902019-12-01 22:11:18 +01001170 // Don't update the cursor when halfway busy scrolling or the screen size
1171 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001172 if (!can_update_cursor || screen_Columns != gui.num_cols
1173 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 return;
1175
1176 gui_check_pos();
1177 if (!gui.cursor_is_valid || force
1178 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1179 {
1180 gui_undraw_cursor();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001181
1182 // If a cursor-less sleep is ongoing, leave the cursor invisible
1183 if (cursor_is_sleeping())
1184 return;
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (gui.row < 0)
1187 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001188#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1190 im_set_position(gui.row, gui.col);
1191#endif
1192 gui.cursor_row = gui.row;
1193 gui.cursor_col = gui.col;
1194
Bram Moolenaar30613902019-12-01 22:11:18 +01001195 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (!screen_cleared || ScreenLines == NULL)
1197 return;
1198
Bram Moolenaar30613902019-12-01 22:11:18 +01001199 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (clear_selection)
1201 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001202 // Check that the cursor is inside the shell (resizing may have made
1203 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1205 return;
1206
1207 gui.cursor_is_valid = TRUE;
1208
1209 /*
1210 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001211 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001213#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001214 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001215 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001217#endif
1218 shape = &shape_table[get_shape_idx(FALSE)];
1219 if (State & LANGMAP)
1220 id = shape->id_lm;
1221 else
1222 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 cfg = INVALCOLOR;
1226 cbg = INVALCOLOR;
1227 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001228 gui_mch_set_blinking(shape->blinkwait,
1229 shape->blinkon,
1230 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001231 if (shape->blinkwait == 0 || shape->blinkon == 0
1232 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001233 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001234#ifdef FEAT_TERMINAL
1235 if (shape_bg != INVALCOLOR)
1236 {
1237 cattr = 0;
1238 cfg = shape_fg;
1239 cbg = shape_bg;
1240 }
1241 else
1242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (id > 0)
1244 {
1245 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001246#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 static int iid;
1249 guicolor_T fg, bg;
1250
Bram Moolenaarc236c162008-07-13 17:41:49 +00001251 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001252# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001253 preedit_get_status()
1254# else
1255 im_get_status()
1256# endif
1257 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 iid = syn_name2id((char_u *)"CursorIM");
1260 if (iid > 0)
1261 {
1262 syn_id2colors(iid, &fg, &bg);
1263 if (bg != INVALCOLOR)
1264 cbg = bg;
1265 if (fg != INVALCOLOR)
1266 cfg = fg;
1267 }
1268 }
1269 }
1270#endif
1271 }
1272
1273 /*
1274 * Get the attributes for the character under the cursor.
1275 * When no cursor color was given, use the character color.
1276 */
1277 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1278 if (attr > HL_ALL)
1279 aep = syn_gui_attr2entry(attr);
1280 if (aep != NULL)
1281 {
1282 attr = aep->ae_attr;
1283 if (cfg == INVALCOLOR)
1284 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1285 : aep->ae_u.gui.fg_color);
1286 if (cbg == INVALCOLOR)
1287 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1288 : aep->ae_u.gui.bg_color);
1289 }
1290 if (cfg == INVALCOLOR)
1291 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1292 if (cbg == INVALCOLOR)
1293 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1294
1295#ifdef FEAT_XIM
1296 if (aep != NULL)
1297 {
1298 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1299 : aep->ae_u.gui.bg_color);
1300 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1301 : aep->ae_u.gui.fg_color);
1302 if (xim_bg_color == INVALCOLOR)
1303 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1304 : gui.back_pixel;
1305 if (xim_fg_color == INVALCOLOR)
1306 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1307 : gui.norm_pixel;
1308 }
1309 else
1310 {
1311 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1312 : gui.back_pixel;
1313 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1314 : gui.norm_pixel;
1315 }
1316#endif
1317
1318 attr &= ~HL_INVERSE;
1319 if (cattr & HL_INVERSE)
1320 {
1321 cc = cbg;
1322 cbg = cfg;
1323 cfg = cc;
1324 }
1325 cattr &= ~HL_INVERSE;
1326
1327 /*
1328 * When we don't have window focus, draw a hollow cursor.
1329 */
1330 if (!gui.in_focus)
1331 {
1332 gui_mch_draw_hollow_cursor(cbg);
1333 return;
1334 }
1335
1336 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001337 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 /*
1340 * Draw the text character with the cursor colors. Use the
1341 * character attributes plus the cursor attributes.
1342 */
1343 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001344 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1346 }
1347 else
1348 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001349#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 int col_off = FALSE;
1351#endif
1352 /*
1353 * First draw the partial cursor, then overwrite with the text
1354 * character, using a transparent background.
1355 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001356 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001359 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else
1362 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001363 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 cur_width = gui.char_width;
1365 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001366 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1367 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001369 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001370 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001372#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (CURSOR_BAR_RIGHT)
1374 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001375 // gui.col points to the left half of the character but
1376 // the vertical line needs to be on the right half.
Bram Moolenaar30613902019-12-01 22:11:18 +01001377 // A double-wide horizontal line is also drawn from the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001378 // right half in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 col_off = TRUE;
1380 ++gui.col;
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001385#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (col_off)
1387 --gui.col;
1388#endif
1389
Bram Moolenaar30613902019-12-01 22:11:18 +01001390#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1392 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1393 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1394 (guicolor_T)0, (guicolor_T)0, 0);
1395#endif
1396 }
1397 gui.highlight_mask = old_hl_mask;
1398 }
1399}
1400
1401#if defined(FEAT_MENU) || defined(PROTO)
1402 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001403gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001405# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (gui.menu_is_active && gui.in_use)
1407 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1408# endif
1409}
1410#endif
1411
1412/*
1413 * Position the various GUI components (text area, menu). The vertical
1414 * scrollbars are NOT handled here. See gui_update_scrollbars().
1415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001417gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 int text_area_x;
1420 int text_area_y;
1421 int text_area_width;
1422 int text_area_height;
1423
Bram Moolenaar30613902019-12-01 22:11:18 +01001424 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 ++hold_gui_events;
1426
1427 text_area_x = 0;
1428 if (gui.which_scrollbars[SBAR_LEFT])
1429 text_area_x += gui.scrollbar_width;
1430
1431 text_area_y = 0;
1432#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1433 gui.menu_width = total_width;
1434 if (gui.menu_is_active)
1435 text_area_y += gui.menu_height;
1436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001438# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001439 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001440 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001441 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001442#endif
1443
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001444#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001445 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1447 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001448# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 gui_mch_set_toolbar_pos(0, text_area_y,
1450 gui.menu_width, gui.toolbar_height);
1451# endif
1452 text_area_y += gui.toolbar_height;
1453 }
1454#endif
1455
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001456# if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
1457 gui_mch_set_tabline_pos(0, text_area_y,
1458 gui.menu_width, gui.tabline_height);
1459 if (gui_has_tabline())
1460 text_area_y += gui.tabline_height;
1461#endif
1462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1464 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1465
1466 gui_mch_set_text_area_pos(text_area_x,
1467 text_area_y,
1468 text_area_width,
1469 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001470#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 + xim_get_status_area_height()
1472#endif
1473 );
1474#ifdef FEAT_MENU
1475 gui_position_menu();
1476#endif
1477 if (gui.which_scrollbars[SBAR_BOTTOM])
1478 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1479 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001480 text_area_y + text_area_height
1481 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 text_area_width,
1483 gui.scrollbar_height);
1484 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001485 gui.right_sbar_x = text_area_x + text_area_width
1486 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 --hold_gui_events;
1489}
1490
Bram Moolenaar02743632005-07-25 20:42:36 +00001491/*
1492 * Get the width of the widgets and decorations to the side of the text area.
1493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001495gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 int base_width;
1498
1499 base_width = 2 * gui.border_offset;
1500 if (gui.which_scrollbars[SBAR_LEFT])
1501 base_width += gui.scrollbar_width;
1502 if (gui.which_scrollbars[SBAR_RIGHT])
1503 base_width += gui.scrollbar_width;
1504 return base_width;
1505}
1506
Bram Moolenaar02743632005-07-25 20:42:36 +00001507/*
1508 * Get the height of the widgets and decorations above and below the text area.
1509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001511gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int base_height;
1514
1515 base_height = 2 * gui.border_offset;
1516 if (gui.which_scrollbars[SBAR_BOTTOM])
1517 base_height += gui.scrollbar_height;
1518#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001519 // We can't take the sizes properly into account until anything is
1520 // realized. Therefore we recalculate all the values here just before
1521 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522#else
1523# ifdef FEAT_MENU
1524 if (gui.menu_is_active)
1525 base_height += gui.menu_height;
1526# endif
1527# ifdef FEAT_TOOLBAR
1528 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001531# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001532 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001533 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001534 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536# ifdef FEAT_FOOTER
1537 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1538 base_height += gui.footer_height;
1539# endif
1540# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1541 base_height += gui_mch_text_area_extra_height();
1542# endif
1543#endif
1544 return base_height;
1545}
1546
1547/*
1548 * Should be called after the GUI shell has been resized. Its arguments are
1549 * the new width and height of the shell in pixels.
1550 */
1551 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001552gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 static int busy = FALSE;
1555
Bram Moolenaar30613902019-12-01 22:11:18 +01001556 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 return;
1558
1559 /*
1560 * Can't resize the screen while it is being redrawn. Remember the new
1561 * size and handle it later.
1562 */
1563 if (updating_screen || busy)
1564 {
1565 new_pixel_width = pixel_width;
1566 new_pixel_height = pixel_height;
1567 return;
1568 }
1569
1570again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001571 new_pixel_width = 0;
1572 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 busy = TRUE;
1574
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001575#ifdef FEAT_GUI_HAIKU
1576 vim_lock_screen();
1577#endif
1578
Bram Moolenaar30613902019-12-01 22:11:18 +01001579 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 out_flush();
1581
1582 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001583 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /*
1589 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1590 * at the last line here (why does it have to be one row too low?).
1591 */
1592 if (State == ASKMORE || State == CONFIRM)
1593 gui.row = gui.num_rows;
1594
Bram Moolenaar30613902019-12-01 22:11:18 +01001595 // Only comparing Rows and Columns may be sufficient, but let's stay on
1596 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1598 || gui.num_rows != Rows || gui.num_cols != Columns)
1599 shell_resized();
1600
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001601#ifdef FEAT_GUI_HAIKU
1602 vim_unlock_screen();
1603#endif
1604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 gui_update_scrollbars(TRUE);
1606 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001607#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 xim_set_status_area();
1609#endif
1610
1611 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001612
Bram Moolenaar30613902019-12-01 22:11:18 +01001613 // We may have been called again while redrawing the screen.
1614 // Need to do it all again with the latest size then. But only if the size
1615 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (new_pixel_height)
1617 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001618 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1619 {
1620 new_pixel_width = 0;
1621 new_pixel_height = 0;
1622 }
1623 else
1624 {
1625 pixel_width = new_pixel_width;
1626 pixel_height = new_pixel_height;
1627 goto again;
1628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630}
1631
1632/*
1633 * Check if gui_resize_shell() must be called.
1634 */
1635 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001636gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001639 // careful: gui_resize_shell() may postpone the resize again if we
1640 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001641 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642}
1643
1644 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001645gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 Rows = gui.num_rows;
1648 Columns = gui.num_cols;
1649 return OK;
1650}
1651
1652/*
1653 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001654 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1655 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001656 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1657 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001660gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001661 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001662 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001663 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 int base_width;
1666 int base_height;
1667 int width;
1668 int height;
1669 int min_width;
1670 int min_height;
1671 int screen_w;
1672 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001673#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001674 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001675 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001676#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001677 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 if (!gui.shell_created)
1680 return;
1681
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001682#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001683 // If not setting to a user specified size and maximized, calculate the
1684 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001685 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1686 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 gui_mch_newfont();
1689 return;
1690 }
1691#endif
1692
1693 base_width = gui_get_base_width();
1694 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001695 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001696 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001697 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001698
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001699 width = Columns * gui.char_width + base_width;
1700 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702 if (fit_to_display)
1703 {
1704 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001705 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 Columns = (screen_w - base_width) / gui.char_width;
1708 if (Columns < MIN_COLUMNS)
1709 Columns = MIN_COLUMNS;
1710 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001711#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001712 ++did_adjust;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001715 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
1717 Rows = (screen_h - base_height) / gui.char_height;
1718 check_shellsize();
1719 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001720#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001721 ++did_adjust;
1722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001724#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001725 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1726 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001727 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001728 un_maximize = FALSE;
1729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001731 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 gui.num_cols = Columns;
1733 gui.num_rows = Rows;
1734
1735 min_width = base_width + MIN_COLUMNS * gui.char_width;
1736 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001737 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001738
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001739#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001740 if (un_maximize)
1741 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001742 // If the window size is smaller than the screen unmaximize the
1743 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001744 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1745 if ((width + gui.char_width < screen_w
1746 || height + gui.char_height * 2 < screen_h)
1747 && gui_mch_maximized())
1748 gui_mch_unmaximize();
1749 }
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001753 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001755 if (fit_to_display && x >= 0 && y >= 0)
1756 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001757 // Some window managers put the Vim window left of/above the screen.
1758 // Only change the position if it wasn't already negative before
1759 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 gui_mch_update();
1761 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1762 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1763 }
1764
1765 gui_position_components(width);
1766 gui_update_scrollbars(TRUE);
1767 gui_reset_scroll_region();
1768}
1769
1770/*
1771 * Called when Rows and/or Columns has changed.
1772 */
1773 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001774gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 gui_reset_scroll_region();
1777}
1778
1779/*
1780 * Make scroll region cover whole screen.
1781 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001782 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001783gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 gui.scroll_region_top = 0;
1786 gui.scroll_region_bot = gui.num_rows - 1;
1787 gui.scroll_region_left = 0;
1788 gui.scroll_region_right = gui.num_cols - 1;
1789}
1790
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001791 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001792gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar30613902019-12-01 22:11:18 +01001794 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001796 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 gui.highlight_mask |= mask;
1798}
1799
1800 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001801gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
Bram Moolenaar30613902019-12-01 22:11:18 +01001803 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001805 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 gui.highlight_mask &= ~mask;
1807}
1808
1809/*
1810 * Clear a rectangular region of the screen from text pos (row1, col1) to
1811 * (row2, col2) inclusive.
1812 */
1813 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001814gui_clear_block(
1815 int row1,
1816 int col1,
1817 int row2,
1818 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
Bram Moolenaar30613902019-12-01 22:11:18 +01001820 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 clip_may_clear_selection(row1, row2);
1822
1823 gui_mch_clear_block(row1, col1, row2, col2);
1824
Bram Moolenaar30613902019-12-01 22:11:18 +01001825 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1827 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1828 gui.cursor_is_valid = FALSE;
1829}
1830
1831/*
1832 * Write code to update the cursor later. This avoids the need to flush the
1833 * output buffer before calling gui_update_cursor().
1834 */
1835 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001836gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001838 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839}
1840
1841 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001842gui_write(
1843 char_u *s,
1844 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846 char_u *p;
1847 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001848 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int force_scrollbar = FALSE;
1850 static win_T *old_curwin = NULL;
1851
Bram Moolenaar30613902019-12-01 22:11:18 +01001852// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853#ifdef DEBUG_GUI_WRITE
1854 {
1855 int i;
1856 char_u *str;
1857
1858 printf("gui_write(%d):\n ", len);
1859 for (i = 0; i < len; i++)
1860 if (s[i] == ESC)
1861 {
1862 if (i != 0)
1863 printf("\n ");
1864 printf("<ESC>");
1865 }
1866 else
1867 {
1868 str = transchar_byte(s[i]);
1869 if (str[0] && str[1])
1870 printf("<%s>", (char *)str);
1871 else
1872 printf("%s", (char *)str);
1873 }
1874 printf("\n");
1875 }
1876#endif
1877 while (len)
1878 {
1879 if (s[0] == ESC && s[1] == '|')
1880 {
1881 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001882 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 arg1 = getdigits(&p);
1885 if (p > s + len)
1886 break;
1887 if (*p == ';')
1888 {
1889 ++p;
1890 arg2 = getdigits(&p);
1891 if (p > s + len)
1892 break;
1893 }
1894 }
1895 switch (*p)
1896 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001897 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 clip_scroll_selection(9999);
1899 gui_mch_clear_all();
1900 gui.cursor_is_valid = FALSE;
1901 force_scrollbar = TRUE;
1902 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001903 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 gui_set_cursor(arg1, arg2);
1905 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001906 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 force_cursor = TRUE;
1908 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001909 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (arg1 < arg2)
1911 {
1912 gui.scroll_region_top = arg1;
1913 gui.scroll_region_bot = arg2;
1914 }
1915 else
1916 {
1917 gui.scroll_region_top = arg2;
1918 gui.scroll_region_bot = arg1;
1919 }
1920 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001921 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (arg1 < arg2)
1923 {
1924 gui.scroll_region_left = arg1;
1925 gui.scroll_region_right = arg2;
1926 }
1927 else
1928 {
1929 gui.scroll_region_left = arg2;
1930 gui.scroll_region_right = arg1;
1931 }
1932 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001933 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 gui_delete_lines(gui.row, 1);
1935 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001936 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 gui_delete_lines(gui.row, arg1);
1938 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001939 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 gui_insert_lines(gui.row, 1);
1941 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001942 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 gui_insert_lines(gui.row, arg1);
1944 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001945 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 gui_clear_block(gui.row, gui.col, gui.row,
1947 (int)Columns - 1);
1948 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001949 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 gui_start_highlight(arg1);
1951 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001952 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 gui_stop_highlight(arg1);
1954 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001955 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1957 break;
1958 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001959 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 break;
1961 }
1962 len -= (int)(++p - s);
1963 s = p;
1964 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001965 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#ifdef FEAT_SIGN_ICONS
1967 && s[0] != SIGN_BYTE
1968# ifdef FEAT_NETBEANS_INTG
1969 && s[0] != MULTISIGN_BYTE
1970# endif
1971#endif
1972 )
1973 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001974 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
1976 gui.col = 0;
1977 if (gui.row < gui.scroll_region_bot)
1978 gui.row++;
1979 else
1980 gui_delete_lines(gui.scroll_region_top, 1);
1981 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001982 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 gui.col = 0;
1985 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001986 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 if (gui.col)
1989 --gui.col;
1990 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001991 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 ++gui.col;
1994 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001995 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
1997 gui_mch_beep();
1998 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001999 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 ++s;
2003 }
2004 else
2005 {
2006 p = s;
2007 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#ifdef FEAT_SIGN_ICONS
2010 || *p == SIGN_BYTE
2011# ifdef FEAT_NETBEANS_INTG
2012 || *p == MULTISIGN_BYTE
2013# endif
2014#endif
2015 ))
2016 {
2017 len--;
2018 p++;
2019 }
2020 gui_outstr(s, (int)(p - s));
2021 s = p;
2022 }
2023 }
2024
Bram Moolenaar30613902019-12-01 22:11:18 +01002025 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2026 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (force_cursor)
2028 gui_update_cursor(TRUE, TRUE);
2029
Bram Moolenaar30613902019-12-01 22:11:18 +01002030 // When switching to another window the dragging must have stopped.
2031 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (old_curwin != curwin)
2033 gui.dragged_sb = SBAR_NONE;
2034
Bram Moolenaar30613902019-12-01 22:11:18 +01002035 // Update the scrollbars after clearing the screen or when switched
2036 // to another window.
2037 // Update the horizontal scrollbar always, it's difficult to check all
2038 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (force_scrollbar || old_curwin != curwin)
2040 gui_update_scrollbars(force_scrollbar);
2041 else
2042 gui_update_horiz_scrollbar(FALSE);
2043 old_curwin = curwin;
2044
2045 /*
2046 * We need to make sure this is cleared since Athena doesn't tell us when
2047 * he is done dragging. Do the same for GTK.
2048 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002049#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 gui.dragged_sb = SBAR_NONE;
2051#endif
2052
Bram Moolenaar30613902019-12-01 22:11:18 +01002053 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054}
2055
2056/*
2057 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2058 * produces wrong results. Call gui_dont_update_cursor() before that code and
2059 * gui_can_update_cursor() afterwards.
2060 */
2061 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002062gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
2064 if (gui.in_use)
2065 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002066 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02002067 if (undraw)
2068 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 can_update_cursor = FALSE;
2070 }
2071}
2072
2073 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002074gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075{
2076 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002077 // No need to update the cursor right now, there is always more output
2078 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079}
2080
Bram Moolenaara338adc2018-01-31 20:51:47 +01002081/*
2082 * Disable issuing gui_mch_flush().
2083 */
2084 void
2085gui_disable_flush(void)
2086{
2087 ++disable_flush;
2088}
2089
2090/*
2091 * Enable issuing gui_mch_flush().
2092 */
2093 void
2094gui_enable_flush(void)
2095{
2096 --disable_flush;
2097}
2098
2099/*
2100 * Issue gui_mch_flush() if it is not disabled.
2101 */
2102 void
2103gui_may_flush(void)
2104{
2105 if (disable_flush == 0)
2106 gui_mch_flush();
2107}
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002110gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115 if (len == 0)
2116 return;
2117
2118 if (len < 0)
2119 len = (int)STRLEN(s);
2120
2121 while (len > 0)
2122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (has_mbyte)
2124 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002125 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 cells = 0;
2127 for (this_len = 0; this_len < len; )
2128 {
2129 cells += (*mb_ptr2cells)(s + this_len);
2130 if (gui.col + cells > Columns)
2131 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002132 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002135 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
2137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (gui.col + len > Columns)
2139 this_len = Columns - gui.col;
2140 else
2141 this_len = len;
2142
2143 (void)gui_outstr_nowrap(s, this_len,
2144 0, (guicolor_T)0, (guicolor_T)0, 0);
2145 s += this_len;
2146 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002147 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (len > 0 && gui.col < Columns)
2149 (void)gui_outstr_nowrap((char_u *)" ", 1,
2150 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002151 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (gui.col >= Columns)
2153 {
2154 gui.col = 0;
2155 gui.row++;
2156 }
2157 }
2158}
2159
2160/*
2161 * Output one character (may be one or two display cells).
2162 * Caller must check for valid "off".
2163 * Returns FAIL or OK, just like gui_outstr_nowrap().
2164 */
2165 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002166gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002167 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002168 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002169 guicolor_T fg, // colors for cursor
2170 guicolor_T bg, // colors for cursor
2171 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 char_u buf[MB_MAXBYTES + 1];
2174
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002175 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (enc_utf8 && ScreenLines[off] == 0)
2177 return OK;
2178
2179 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002180 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2182 flags, fg, bg, back);
2183
2184 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2185 {
2186 buf[0] = ScreenLines[off];
2187 buf[1] = ScreenLines2[off];
2188 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2189 }
2190
Bram Moolenaar30613902019-12-01 22:11:18 +01002191 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002193 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195}
2196
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002197#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198/*
2199 * Output the string at the given screen position. This is used in place
2200 * of gui_screenchar() where possible because Pango needs as much context
2201 * as possible to work nicely. It's a lot faster as well.
2202 */
2203 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002204gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002205 int off, // Offset from start of screen
2206 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002207 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002208 guicolor_T fg, // colors for cursor
2209 guicolor_T bg, // colors for cursor
2210 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 char_u *buf;
2213 int outlen = 0;
2214 int i;
2215 int retval;
2216
Bram Moolenaar30613902019-12-01 22:11:18 +01002217 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 return OK;
2219
2220 if (enc_utf8)
2221 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002222 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002224 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 for (i = off; i < off + len; ++i)
2227 {
2228 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002229 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
2231 if (ScreenLinesUC[i] == 0)
2232 buf[outlen++] = ScreenLines[i];
2233 else
2234 outlen += utfc_char2bytes(i, buf + outlen);
2235 }
2236
Bram Moolenaar30613902019-12-01 22:11:18 +01002237 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2239 vim_free(buf);
2240
2241 return retval;
2242 }
2243 else if (enc_dbcs == DBCS_JPNU)
2244 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002245 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002247 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 for (i = off; i < off + len; ++i)
2250 {
2251 buf[outlen++] = ScreenLines[i];
2252
Bram Moolenaar30613902019-12-01 22:11:18 +01002253 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (ScreenLines[i] == 0x8e)
2255 buf[outlen++] = ScreenLines2[i];
2256 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2257 buf[outlen++] = ScreenLines[++i];
2258 }
2259
Bram Moolenaar30613902019-12-01 22:11:18 +01002260 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2262 vim_free(buf);
2263
2264 return retval;
2265 }
2266 else
2267 {
2268 return gui_outstr_nowrap(&ScreenLines[off], len,
2269 flags, fg, bg, back);
2270 }
2271}
Bram Moolenaar30613902019-12-01 22:11:18 +01002272#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274/*
2275 * Output the given string at the current cursor position. If the string is
2276 * too long to fit on the line, then it is truncated.
2277 * "flags":
2278 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2279 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002280 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * background.
2282 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2283 * it.
2284 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2285 * FAIL (the caller should start drawing "back" chars back).
2286 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002287 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002288gui_outstr_nowrap(
2289 char_u *s,
2290 int len,
2291 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002292 guicolor_T fg, // colors for cursor
2293 guicolor_T bg, // colors for cursor
2294 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 long_u highlight_mask;
2297 long_u hl_mask_todo;
2298 guicolor_T fg_color;
2299 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002300 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002303 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304# ifdef FEAT_XFONTSET
2305 GuiFontset fontset = NOFONTSET;
2306# endif
2307#endif
2308 attrentry_T *aep = NULL;
2309 int draw_flags;
2310 int col = gui.col;
2311#ifdef FEAT_SIGN_ICONS
2312 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002313 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002314 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315# ifdef FEAT_NETBEANS_INTG
2316 int multi_sign = FALSE;
2317# endif
2318#endif
2319
2320 if (len < 0)
2321 len = (int)STRLEN(s);
2322 if (len == 0)
2323 return OK;
2324
2325#ifdef FEAT_SIGN_ICONS
2326 if (*s == SIGN_BYTE
2327# ifdef FEAT_NETBEANS_INTG
2328 || *s == MULTISIGN_BYTE
2329# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002330 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332# ifdef FEAT_NETBEANS_INTG
2333 if (*s == MULTISIGN_BYTE)
2334 multi_sign = TRUE;
2335# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002336 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002337 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2338 (curwin->w_p_nu || curwin->w_p_rnu))
2339 {
2340 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2341 s = extra;
2342 }
2343 else
2344 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (len == 1 && col > 0)
2346 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002347 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002348 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002349 // right align sign icon in the number column
2350 signcol = col + len - 3;
2351 else
2352 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 draw_sign = TRUE;
2354 highlight_mask = 0;
2355 }
2356 else
2357#endif
2358 if (gui.highlight_mask > HL_ALL)
2359 {
2360 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002361 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 highlight_mask = 0;
2363 else
2364 highlight_mask = aep->ae_attr;
2365 }
2366 else
2367 highlight_mask = gui.highlight_mask;
2368 hl_mask_todo = highlight_mask;
2369
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002370#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002371 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2373 font = aep->ae_u.gui.font;
2374# ifdef FEAT_XFONTSET
2375 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2376 fontset = aep->ae_u.gui.fontset;
2377# endif
2378 else
2379 {
2380# ifdef FEAT_XFONTSET
2381 if (gui.fontset != NOFONTSET)
2382 fontset = gui.fontset;
2383 else
2384# endif
2385 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2386 {
2387 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2388 {
2389 font = gui.boldital_font;
2390 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2391 }
2392 else if (gui.bold_font != NOFONT)
2393 {
2394 font = gui.bold_font;
2395 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2396 }
2397 else
2398 font = gui.norm_font;
2399 }
2400 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2401 {
2402 font = gui.ital_font;
2403 hl_mask_todo &= ~HL_ITALIC;
2404 }
2405 else
2406 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002407
Bram Moolenaardb250522013-06-17 22:43:25 +02002408 /*
2409 * Choose correct wide_font by font. wide_font should be set with font
2410 * at same time in above block. But it will make many "ifdef" nasty
2411 * blocks. So we do it here.
2412 */
2413 if (font == gui.boldital_font && gui.wide_boldital_font)
2414 wide_font = gui.wide_boldital_font;
2415 else if (font == gui.bold_font && gui.wide_bold_font)
2416 wide_font = gui.wide_bold_font;
2417 else if (font == gui.ital_font && gui.wide_ital_font)
2418 wide_font = gui.wide_ital_font;
2419 else if (font == gui.norm_font && gui.wide_font)
2420 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422# ifdef FEAT_XFONTSET
2423 if (fontset != NOFONTSET)
2424 gui_mch_set_fontset(fontset);
2425 else
2426# endif
2427 gui_mch_set_font(font);
2428#endif
2429
2430 draw_flags = 0;
2431
Bram Moolenaar30613902019-12-01 22:11:18 +01002432 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 bg_color = gui.back_pixel;
2434 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2435 {
2436 draw_flags |= DRAW_CURSOR;
2437 fg_color = fg;
2438 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002439 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441 else if (aep != NULL)
2442 {
2443 fg_color = aep->ae_u.gui.fg_color;
2444 if (fg_color == INVALCOLOR)
2445 fg_color = gui.norm_pixel;
2446 bg_color = aep->ae_u.gui.bg_color;
2447 if (bg_color == INVALCOLOR)
2448 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002449 sp_color = aep->ae_u.gui.sp_color;
2450 if (sp_color == INVALCOLOR)
2451 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002456 sp_color = fg_color;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 gui_mch_set_fg_color(bg_color);
2462 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 else
2465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 gui_mch_set_fg_color(fg_color);
2467 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002469 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaar30613902019-12-01 22:11:18 +01002471 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (!(flags & GUI_MON_NOCLEAR))
2473 clip_may_clear_selection(gui.row, gui.row);
2474
2475
Bram Moolenaar30613902019-12-01 22:11:18 +01002476 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2478 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /*
2481 * When drawing bold or italic characters the spill-over from the left
2482 * neighbor may be destroyed. Let the caller backup to start redrawing
2483 * just after a blank.
2484 */
2485 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2486 return FAIL;
2487
Bram Moolenaare60acc12011-05-10 16:41:25 +02002488#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002489 // If there's no italic font, then fake it.
2490 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (hl_mask_todo & HL_ITALIC)
2492 draw_flags |= DRAW_ITALIC;
2493
Bram Moolenaar30613902019-12-01 22:11:18 +01002494 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (hl_mask_todo & HL_UNDERLINE)
2496 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002499 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002500 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 draw_flags |= DRAW_UNDERL;
2502#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002503 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002504 if (hl_mask_todo & HL_UNDERCURL)
2505 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaar30613902019-12-01 22:11:18 +01002507 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002508 if (hl_mask_todo & HL_STRIKETHROUGH)
2509 draw_flags |= DRAW_STRIKE;
2510
Bram Moolenaar30613902019-12-01 22:11:18 +01002511 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (flags & GUI_MON_TRS_CURSOR)
2513 draw_flags |= DRAW_TRANSP;
2514
2515 /*
2516 * Draw the text.
2517 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002518#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002519 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (enc_utf8)
2523 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002524 int start; // index of bytes to be drawn
2525 int cells; // cellwidth of bytes to be drawn
2526 int thislen; // length of bytes to be drawn
2527 int cn; // cellwidth of current char
2528 int i; // index of current char
2529 int c; // current char value
2530 int cl; // byte length of current char
2531 int comping; // current char is composing
2532 int scol = col; // screen column
2533 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002534 int prev_wide = FALSE;
2535 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002536# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002537 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002538# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002539 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaar30613902019-12-01 22:11:18 +01002542 // Break the string at a composing character, it has to be drawn on
2543 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 start = 0;
2545 cells = 0;
2546 for (i = 0; i < len; i += cl)
2547 {
2548 c = utf_ptr2char(s + i);
2549 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002551 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002553 if (!comping || sep_comp)
2554 {
2555 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002556# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002557 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002558# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002559 && wide_font != NOFONT)
2560 curr_wide = TRUE;
2561 else
2562 curr_wide = FALSE;
2563 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002564 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002565 if (cl == 0) // hit end of string
2566 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar45933962013-01-23 17:43:57 +01002568 wide_changed = curr_wide != prev_wide;
2569
Bram Moolenaar30613902019-12-01 22:11:18 +01002570 // Print the string so far if it's the last character or there is
2571 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002572 if (i + cl >= len || (comping && sep_comp && i > start)
2573 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002574# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002576# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002577 // No fontset: At least draw char after wide char at
2578 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002581 )
2582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 )
2584 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002585 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 thislen = i - start;
2587 else
2588 thislen = i - start + cl;
2589 if (thislen > 0)
2590 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002591 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002592 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2594 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002595 if (prev_wide)
2596 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 start += thislen;
2598 }
2599 scol += cells;
2600 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002601 // Adjust to not draw a character which width is changed
2602 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002603 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002605 scol -= cn;
2606 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608
Bram Moolenaar13505972019-01-24 15:04:48 +01002609# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002610 // No fontset: draw a space to fill the gap after a wide char
2611 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002613# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002615# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002616 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2618 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002621 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002622 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002624# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002625 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002626 gui_mch_draw_string(gui.row, scol, s + i, cl,
2627 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002628# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002629 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2630 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 start = i + cl;
2633 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002634 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002636 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 len = scol - col;
2638 }
2639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
2641 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (enc_dbcs == DBCS_JPNU)
2643 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002644 // Get the length in display cells, this can be different from the
2645 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002646 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002649#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2652 gui.col = col + len;
2653
Bram Moolenaar30613902019-12-01 22:11:18 +01002654 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (flags & GUI_MON_NOCLEAR)
2656 clip_may_redraw_selection(gui.row, col, len);
2657
2658 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2659 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002660 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (gui.cursor_row == gui.row
2662 && gui.cursor_col >= col
2663 && gui.cursor_col < col + len)
2664 gui.cursor_is_valid = FALSE;
2665 }
2666
2667#ifdef FEAT_SIGN_ICONS
2668 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002669 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002670 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002671# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002672 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (multi_sign)
2674 netbeans_draw_multisign_indicator(gui.row);
2675# endif
2676#endif
2677
2678 return OK;
2679}
2680
2681/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002682 * Undraw the cursor. This actually redraws the character at the cursor
2683 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
2685 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002686gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 if (gui.cursor_is_valid)
2689 {
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002690 // Always redraw the character just before if there is one, because
2691 // with some fonts and characters there can be a one pixel overlap.
2692 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2693 int endcol = gui.cursor_col;
2694
2695#ifdef FEAT_GUI_GTK
2696 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
2697#endif
2698 gui_redraw_block(gui.cursor_row, startcol,
2699 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002700
Bram Moolenaar54612582019-11-21 17:13:31 +01002701 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2702 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 gui.cursor_is_valid = FALSE;
2704 }
2705}
2706
2707 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002708gui_redraw(
2709 int x,
2710 int y,
2711 int w,
2712 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 int row1, col1, row2, col2;
2715
2716 row1 = Y_2_ROW(y);
2717 col1 = X_2_COL(x);
2718 row2 = Y_2_ROW(y + h - 1);
2719 col2 = X_2_COL(x + w - 1);
2720
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002721 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 /*
2724 * We may need to redraw the cursor, but don't take it upon us to change
2725 * its location after a scroll.
2726 * (maybe be more strict even and test col too?)
2727 * These things may be outside the update/clipping region and reality may
2728 * not reflect Vims internal ideas if these operations are clipped away.
2729 */
2730 if (gui.row == gui.cursor_row)
2731 gui_update_cursor(TRUE, TRUE);
2732}
2733
2734/*
2735 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2736 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002738 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002739gui_redraw_block(
2740 int row1,
2741 int col1,
2742 int row2,
2743 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002744 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 int old_row, old_col;
2747 long_u old_hl_mask;
2748 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002749 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 int idx, len;
2751 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaar30613902019-12-01 22:11:18 +01002754 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002756 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar30613902019-12-01 22:11:18 +01002758 // Don't try to draw outside the shell!
2759 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 col1 = check_col(col1);
2761 col2 = check_col(col2);
2762 row1 = check_row(row1);
2763 row2 = check_row(row2);
2764
Bram Moolenaar30613902019-12-01 22:11:18 +01002765 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 old_row = gui.row;
2767 old_col = gui.col;
2768 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 orig_col1 = col1;
2770 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 for (gui.row = row1; gui.row <= row2; gui.row++)
2773 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002774 // When only half of a double-wide character is in the block, include
2775 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 col1 = orig_col1;
2777 col2 = orig_col2;
2778 off = LineOffset[gui.row];
2779 if (enc_dbcs != 0)
2780 {
2781 if (col1 > 0)
2782 col1 -= dbcs_screen_head_off(ScreenLines + off,
2783 ScreenLines + off + col1);
2784 col2 += dbcs_screen_tail_off(ScreenLines + off,
2785 ScreenLines + off + col2);
2786 }
2787 else if (enc_utf8)
2788 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002789 if (ScreenLines[off + col1] == 0)
2790 {
2791 if (col1 > 0)
2792 --col1;
2793 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002794 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002795 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002796 // Make this IEMSGN when it no longer breaks Travis CI.
2797 vim_snprintf((char *)IObuff, IOSIZE,
2798 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2799 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002800 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002801 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002802 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002803#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2805 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 gui.col = col1;
2809 off = LineOffset[gui.row] + gui.col;
2810 len = col2 - col1 + 1;
2811
Bram Moolenaar30613902019-12-01 22:11:18 +01002812 // Find how many chars back this highlighting starts, or where a space
2813 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 for (back = 0; back < col1; ++back)
2815 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2816 || ScreenLines[off - 1 - back] == ' ')
2817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaar30613902019-12-01 22:11:18 +01002819 // Break it up in strings of characters with the same attributes.
2820 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 while (len > 0)
2822 {
2823 first_attr = ScreenAttrs[off];
2824 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002825#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (enc_utf8 && ScreenLinesUC[off] != 0)
2827 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002828 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 nback = gui_screenchar(off, flags,
2830 (guicolor_T)0, (guicolor_T)0, back);
2831 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2832 idx = 2;
2833 else
2834 idx = 1;
2835 }
2836 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2837 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002838 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 nback = gui_screenchar(off, flags,
2840 (guicolor_T)0, (guicolor_T)0, back);
2841 idx = 1;
2842 }
2843 else
2844#endif
2845 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002846#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 for (idx = 0; idx < len; ++idx)
2848 {
2849 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002850 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (ScreenAttrs[off + idx] != first_attr)
2852 break;
2853 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002854 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 nback = gui_screenstr(off, idx, flags,
2856 (guicolor_T)0, (guicolor_T)0, back);
2857#else
2858 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2859 idx++)
2860 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002861 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2863 break;
2864 if (enc_dbcs == DBCS_JPNU)
2865 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002866 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (ScreenLines[off + idx] == 0x8e)
2868 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002869 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002871 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2875 (guicolor_T)0, (guicolor_T)0, back);
2876#endif
2877 }
2878 if (nback == FAIL)
2879 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002880 // Must back up to start drawing where a bold or italic word
2881 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 off -= back;
2883 len += back;
2884 gui.col -= back;
2885 }
2886 else
2887 {
2888 off += idx;
2889 len -= idx;
2890 }
2891 back = 0;
2892 }
2893 }
2894
Bram Moolenaar30613902019-12-01 22:11:18 +01002895 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 gui.row = old_row;
2897 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002898 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899}
2900
2901 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002902gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 if (count <= 0)
2905 return;
2906
2907 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002908 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 gui_clear_block(row, gui.scroll_region_left,
2910 gui.scroll_region_bot, gui.scroll_region_right);
2911 else
2912 {
2913 gui_mch_delete_lines(row, count);
2914
Bram Moolenaar30613902019-12-01 22:11:18 +01002915 // If the cursor was in the deleted lines it's now gone. If the
2916 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 if (gui.cursor_row >= row
2918 && gui.cursor_col >= gui.scroll_region_left
2919 && gui.cursor_col <= gui.scroll_region_right)
2920 {
2921 if (gui.cursor_row < row + count)
2922 gui.cursor_is_valid = FALSE;
2923 else if (gui.cursor_row <= gui.scroll_region_bot)
2924 gui.cursor_row -= count;
2925 }
2926 }
2927}
2928
2929 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002930gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 if (count <= 0)
2933 return;
2934
2935 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002936 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 gui_clear_block(row, gui.scroll_region_left,
2938 gui.scroll_region_bot, gui.scroll_region_right);
2939 else
2940 {
2941 gui_mch_insert_lines(row, count);
2942
2943 if (gui.cursor_row >= gui.row
2944 && gui.cursor_col >= gui.scroll_region_left
2945 && gui.cursor_col <= gui.scroll_region_right)
2946 {
2947 if (gui.cursor_row <= gui.scroll_region_bot - count)
2948 gui.cursor_row += count;
2949 else if (gui.cursor_row <= gui.scroll_region_bot)
2950 gui.cursor_is_valid = FALSE;
2951 }
2952 }
2953}
2954
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002955#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002956/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002957 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2958 */
2959 static int
2960gui_wait_for_chars_3(
2961 long wtime,
2962 int *interrupted UNUSED,
2963 int ignore_input UNUSED)
2964{
2965 return gui_mch_wait_for_chars(wtime);
2966}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002967#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002968
2969/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002970 * Returns OK if a character was found to be available within the given time,
2971 * or FAIL otherwise.
2972 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002973 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002974gui_wait_for_chars_or_timer(
2975 long wtime,
2976 int *interrupted UNUSED,
2977 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002978{
2979#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002980 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2981 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002982#else
2983 return gui_mch_wait_for_chars(wtime);
2984#endif
2985}
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987/*
2988 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002989 * "wtime" == -1 Wait forever.
2990 * "wtime" == 0 Don't wait.
2991 * "wtime" > 0 Wait wtime milliseconds for a character.
2992 *
2993 * Returns the number of characters read or zero when timed out or interrupted.
2994 * "buf" may be NULL, in which case a non-zero number is returned if characters
2995 * are available.
2996 */
2997 static int
2998gui_wait_for_chars_buf(
2999 char_u *buf,
3000 int maxlen,
3001 long wtime, // don't use "time", MIPS cannot handle it
3002 int tb_change_cnt)
3003{
3004 int retval;
3005
3006#ifdef FEAT_MENU
3007 // If we're going to wait a bit, update the menus and mouse shape for the
3008 // current State.
3009 if (wtime != 0)
3010 gui_update_menus(0);
3011#endif
3012
3013 gui_mch_update();
3014 if (input_available()) // Got char, return immediately
3015 {
3016 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3017 return read_from_input_buf(buf, (long)maxlen);
3018 return 0;
3019 }
3020 if (wtime == 0) // Don't wait for char
3021 return FAIL;
3022
3023 // Before waiting, flush any output to the screen.
3024 gui_mch_flush();
3025
3026 // Blink while waiting for a character.
3027 gui_mch_start_blink();
3028
3029 // Common function to loop until "wtime" is met, while handling timers and
3030 // other callbacks.
3031 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3032 gui_wait_for_chars_or_timer, NULL);
3033
3034 gui_mch_stop_blink(TRUE);
3035
3036 return retval;
3037}
3038
3039/*
3040 * Wait for a character from the keyboard without actually reading it.
3041 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 * wtime == -1 Wait forever.
3043 * wtime == 0 Don't wait.
3044 * wtime > 0 Wait wtime milliseconds for a character.
3045 * Returns OK if a character was found to be available within the given time,
3046 * or FAIL otherwise.
3047 */
3048 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003049gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003051 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052}
3053
3054/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003055 * Equivalent of mch_inchar() for the GUI.
3056 */
3057 int
3058gui_inchar(
3059 char_u *buf,
3060 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003061 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003062 int tb_change_cnt)
3063{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003064 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003065}
3066
3067/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003068 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 */
3070 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003071fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 p[0] = (char_u)(col / 128 + ' ' + 1);
3074 p[1] = (char_u)(col % 128 + ' ' + 1);
3075 p[2] = (char_u)(row / 128 + ' ' + 1);
3076 p[3] = (char_u)(row % 128 + ' ' + 1);
3077}
3078
3079/*
3080 * Generic mouse support function. Add a mouse event to the input buffer with
3081 * the given properties.
3082 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3083 * MOUSE_X1, MOUSE_X2
3084 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003085 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3086 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * x, y --- Coordinates of mouse in pixels.
3088 * repeated_click --- TRUE if this click comes only a short time after a
3089 * previous click.
3090 * modifiers --- Bit field which may be any of the following modifiers
3091 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3092 * This function will ignore drag events where the mouse has not moved to a new
3093 * character.
3094 */
3095 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003096gui_send_mouse_event(
3097 int button,
3098 int x,
3099 int y,
3100 int repeated_click,
3101 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
3103 static int prev_row = 0, prev_col = 0;
3104 static int prev_button = -1;
3105 static int num_clicks = 1;
3106 char_u string[10];
3107 enum key_extra button_char;
3108 int row, col;
3109#ifdef FEAT_CLIPBOARD
3110 int checkfor;
3111 int did_clip = FALSE;
3112#endif
3113
3114 /*
3115 * Scrolling may happen at any time, also while a selection is present.
3116 */
3117 switch (button)
3118 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003119 case MOUSE_MOVE:
3120 button_char = KE_MOUSEMOVE_XY;
3121 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 case MOUSE_X1:
3123 button_char = KE_X1MOUSE;
3124 goto button_set;
3125 case MOUSE_X2:
3126 button_char = KE_X2MOUSE;
3127 goto button_set;
3128 case MOUSE_4:
3129 button_char = KE_MOUSEDOWN;
3130 goto button_set;
3131 case MOUSE_5:
3132 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003133 goto button_set;
3134 case MOUSE_6:
3135 button_char = KE_MOUSELEFT;
3136 goto button_set;
3137 case MOUSE_7:
3138 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139button_set:
3140 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003141 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 if (hold_gui_events)
3143 return;
3144
3145 string[3] = CSI;
3146 string[4] = KS_EXTRA;
3147 string[5] = (int)button_char;
3148
Bram Moolenaar30613902019-12-01 22:11:18 +01003149 // Pass the pointer coordinates of the scroll event so that we
3150 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 row = gui_xy2colrow(x, y, &col);
3152 string[6] = (char_u)(col / 128 + ' ' + 1);
3153 string[7] = (char_u)(col % 128 + ' ' + 1);
3154 string[8] = (char_u)(row / 128 + ' ' + 1);
3155 string[9] = (char_u)(row % 128 + ' ' + 1);
3156
3157 if (modifiers == 0)
3158 add_to_input_buf(string + 3, 7);
3159 else
3160 {
3161 string[0] = CSI;
3162 string[1] = KS_MODIFIER;
3163 string[2] = 0;
3164 if (modifiers & MOUSE_SHIFT)
3165 string[2] |= MOD_MASK_SHIFT;
3166 if (modifiers & MOUSE_CTRL)
3167 string[2] |= MOD_MASK_CTRL;
3168 if (modifiers & MOUSE_ALT)
3169 string[2] |= MOD_MASK_ALT;
3170 add_to_input_buf(string, 10);
3171 }
3172 return;
3173 }
3174 }
3175
3176#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003177 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (clip_star.state == SELECT_IN_PROGRESS)
3179 {
3180 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003181
3182 // A release event may still need to be sent if the position is equal.
3183 row = gui_xy2colrow(x, y, &col);
3184 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187
Bram Moolenaar30613902019-12-01 22:11:18 +01003188 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 switch (get_real_state())
3190 {
3191 case NORMAL_BUSY:
3192 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003193# ifdef FEAT_TERMINAL
3194 case TERMINAL:
3195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 case NORMAL: checkfor = MOUSE_NORMAL; break;
3197 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003198 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 case REPLACE:
3200 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 case VREPLACE:
3202 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 case INSERT:
3204 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3205 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003206 case HITRETURN: // At the more- and hit-enter prompt pass the
3207 // mouse event for a click on or below the
3208 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (Y_2_ROW(y) >= msg_row)
3210 checkfor = MOUSE_NORMAL;
3211 else
3212 checkfor = MOUSE_RETURN;
3213 break;
3214
3215 /*
3216 * On the command line, use the clipboard selection on all lines
3217 * but the command line. But not when pasting.
3218 */
3219 case CMDLINE:
3220 case CMDLINE+LANGMAP:
3221 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3222 checkfor = MOUSE_NONE;
3223 else
3224 checkfor = MOUSE_COMMAND;
3225 break;
3226
3227 default:
3228 checkfor = MOUSE_NONE;
3229 break;
3230 };
3231
3232 /*
3233 * Allow clipboard selection of text on the command line in "normal"
3234 * modes. Don't do this when dragging the status line, or extending a
3235 * Visual selection.
3236 */
3237 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003238 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 && button != MOUSE_DRAG
3240# ifdef FEAT_MOUSESHAPE
3241 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243# endif
3244 )
3245 checkfor = MOUSE_NONE;
3246
3247 /*
3248 * Use modeless selection when holding CTRL and SHIFT pressed.
3249 */
3250 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3251 checkfor = MOUSE_NONEF;
3252
3253 /*
3254 * In Ex mode, always use modeless selection.
3255 */
3256 if (exmode_active)
3257 checkfor = MOUSE_NONE;
3258
3259 /*
3260 * If the mouse settings say to not use the mouse, use the modeless
3261 * selection. But if Visual is active, assume that only the Visual area
3262 * will be selected.
3263 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003264 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
3266 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3267 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003268 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3270 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
3272 /*
3273 * When 'mousemodel' is "popup", shift-left is translated to right.
3274 * But not when also using Ctrl.
3275 */
3276 if (mouse_model_popup() && button == MOUSE_LEFT
3277 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3278 {
3279 button = MOUSE_RIGHT;
3280 modifiers &= ~ MOUSE_SHIFT;
3281 }
3282
Bram Moolenaar30613902019-12-01 22:11:18 +01003283 // If the selection is done, allow the right button to extend it.
3284 // If the selection is cleared, allow the right button to start it
3285 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (button == MOUSE_RIGHT)
3287 {
3288 if (clip_star.state == SELECT_CLEARED)
3289 {
3290 if (State & CMDLINE)
3291 {
3292 col = msg_col;
3293 row = msg_row;
3294 }
3295 else
3296 {
3297 col = curwin->w_wcol;
3298 row = curwin->w_wrow + W_WINROW(curwin);
3299 }
3300 clip_start_selection(col, row, FALSE);
3301 }
3302 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3303 repeated_click);
3304 did_clip = TRUE;
3305 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003306 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003307 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3310 did_clip = TRUE;
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar30613902019-12-01 22:11:18 +01003313 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (button != MOUSE_MIDDLE)
3315 {
3316 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3317 return;
3318 if (checkfor != MOUSE_COMMAND)
3319 button = MOUSE_LEFT;
3320 }
3321 repeated_click = FALSE;
3322 }
3323
3324 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003325 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#endif
3327
Bram Moolenaar30613902019-12-01 22:11:18 +01003328 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (hold_gui_events)
3330 return;
3331
3332 row = gui_xy2colrow(x, y, &col);
3333
3334 /*
3335 * If we are dragging and the mouse hasn't moved far enough to be on a
3336 * different character, then don't send an event to vim.
3337 */
3338 if (button == MOUSE_DRAG)
3339 {
3340 if (row == prev_row && col == prev_col)
3341 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003342 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (y < 0)
3344 row = -1;
3345 }
3346
3347 /*
3348 * If topline has changed (window scrolled) since the last click, reset
3349 * repeated_click, because we don't want starting Visual mode when
3350 * clicking on a different character in the text.
3351 */
3352 if (curwin->w_topline != gui_prev_topline
3353#ifdef FEAT_DIFF
3354 || curwin->w_topfill != gui_prev_topfill
3355#endif
3356 )
3357 repeated_click = FALSE;
3358
Bram Moolenaar30613902019-12-01 22:11:18 +01003359 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 string[1] = KS_MOUSE;
3361 string[2] = KE_FILLER;
3362 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3363 {
3364 if (repeated_click)
3365 {
3366 /*
3367 * Handle multiple clicks. They only count if the mouse is still
3368 * pointing at the same character.
3369 */
3370 if (button != prev_button || row != prev_row || col != prev_col)
3371 num_clicks = 1;
3372 else if (++num_clicks > 4)
3373 num_clicks = 1;
3374 }
3375 else
3376 num_clicks = 1;
3377 prev_button = button;
3378 gui_prev_topline = curwin->w_topline;
3379#ifdef FEAT_DIFF
3380 gui_prev_topfill = curwin->w_topfill;
3381#endif
3382
3383 string[3] = (char_u)(button | 0x20);
3384 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3385 }
3386 else
3387 string[3] = (char_u)button;
3388
3389 string[3] |= modifiers;
3390 fill_mouse_coord(string + 4, col, row);
3391 add_to_input_buf(string, 8);
3392
3393 if (row < 0)
3394 prev_row = 0;
3395 else
3396 prev_row = row;
3397 prev_col = col;
3398
3399 /*
3400 * We need to make sure this is cleared since Athena doesn't tell us when
3401 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3402 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003403#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 gui.dragged_sb = SBAR_NONE;
3405#endif
3406}
3407
3408/*
3409 * Convert x and y coordinate to column and row in text window.
3410 * Corrects for multi-byte character.
3411 * returns column in "*colp" and row as return value;
3412 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003413 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003414gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 int col = check_col(X_2_COL(x));
3417 int row = check_row(Y_2_ROW(y));
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return row;
3421}
3422
3423#if defined(FEAT_MENU) || defined(PROTO)
3424/*
3425 * Callback function for when a menu entry has been selected.
3426 */
3427 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003428gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003430 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar30613902019-12-01 22:11:18 +01003432 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (hold_gui_events)
3434 return;
3435
3436 bytes[0] = CSI;
3437 bytes[1] = KS_MENU;
3438 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003439 add_to_input_buf(bytes, 3);
3440 add_long_to_buf((long_u)menu, bytes);
3441 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443#endif
3444
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003445static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447/*
3448 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003449 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 * in p_go.
3451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003453gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003455#ifdef FEAT_GUI_DARKTHEME
3456 static int prev_dark_theme = -1;
3457 int using_dark_theme = FALSE;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#ifdef FEAT_MENU
3460 static int prev_menu_is_active = -1;
3461#endif
3462#ifdef FEAT_TOOLBAR
3463 static int prev_toolbar = -1;
3464 int using_toolbar = FALSE;
3465#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003466#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003467 int using_tabline;
3468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469#ifdef FEAT_FOOTER
3470 static int prev_footer = -1;
3471 int using_footer = FALSE;
3472#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 static int prev_tearoff = -1;
3475 int using_tearoff = FALSE;
3476#endif
3477
3478 char_u *p;
3479 int i;
3480#ifdef FEAT_MENU
3481 int grey_old, grey_new;
3482 char_u *temp;
3483#endif
3484 win_T *wp;
3485 int need_set_size;
3486 int fix_size;
3487
3488#ifdef FEAT_MENU
3489 if (oldval != NULL && gui.in_use)
3490 {
3491 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003492 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
3494 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3495 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3496 if (grey_old != grey_new)
3497 {
3498 temp = p_go;
3499 p_go = oldval;
3500 gui_update_menus(MENU_ALL_MODES);
3501 p_go = temp;
3502 }
3503 }
3504 gui.menu_is_active = FALSE;
3505#endif
3506
3507 for (i = 0; i < 3; i++)
3508 gui.which_scrollbars[i] = FALSE;
3509 for (p = p_go; *p; p++)
3510 switch (*p)
3511 {
3512 case GO_LEFT:
3513 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3514 break;
3515 case GO_RIGHT:
3516 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 case GO_VLEFT:
3519 if (win_hasvertsplit())
3520 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3521 break;
3522 case GO_VRIGHT:
3523 if (win_hasvertsplit())
3524 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 case GO_BOT:
3527 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3528 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003529#ifdef FEAT_GUI_DARKTHEME
3530 case GO_DARKTHEME:
3531 using_dark_theme = TRUE;
3532 break;
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef FEAT_MENU
3535 case GO_MENUS:
3536 gui.menu_is_active = TRUE;
3537 break;
3538#endif
3539 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003540 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 break;
3542#ifdef FEAT_TOOLBAR
3543 case GO_TOOLBAR:
3544 using_toolbar = TRUE;
3545 break;
3546#endif
3547#ifdef FEAT_FOOTER
3548 case GO_FOOTER:
3549 using_footer = TRUE;
3550 break;
3551#endif
3552 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003553#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 using_tearoff = TRUE;
3555#endif
3556 break;
3557 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003558 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 break;
3560 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (gui.in_use)
3563 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003564 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003566
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003567#ifdef FEAT_GUI_DARKTHEME
3568 if (using_dark_theme != prev_dark_theme)
3569 {
3570 gui_mch_set_dark_theme(using_dark_theme);
3571 prev_dark_theme = using_dark_theme;
3572 }
3573#endif
3574
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003575#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003576 // Update the GUI tab line, it may appear or disappear. This may
3577 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003578 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003579 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003580 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003581 // We don't want a resize event change "Rows" here, save and
3582 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003583 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003585 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003586 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003587 if (using_tabline)
3588 fix_size = TRUE;
3589 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003590 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003591 }
3592#endif
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 for (i = 0; i < 3; i++)
3595 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003596 // The scrollbar needs to be updated when it is shown/unshown and
3597 // when switching tab pages. But the size only changes when it's
3598 // shown/unshown. Thus we need two places to remember whether a
3599 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003600 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003601 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003602 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 if (i == SBAR_BOTTOM)
3605 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3606 gui.which_scrollbars[i]);
3607 else
3608 {
3609 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003612 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3613 {
3614 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003615 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003616 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003617 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003618 if (gui.which_scrollbars[i])
3619 fix_size = TRUE;
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003622 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003623 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625
3626#ifdef FEAT_MENU
3627 if (gui.menu_is_active != prev_menu_is_active)
3628 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003629 // We don't want a resize event change "Rows" here, save and
3630 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 i = Rows;
3632 gui_mch_enable_menu(gui.menu_is_active);
3633 Rows = i;
3634 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003635 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (gui.menu_is_active)
3637 fix_size = TRUE;
3638 }
3639#endif
3640
3641#ifdef FEAT_TOOLBAR
3642 if (using_toolbar != prev_toolbar)
3643 {
3644 gui_mch_show_toolbar(using_toolbar);
3645 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003646 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (using_toolbar)
3648 fix_size = TRUE;
3649 }
3650#endif
3651#ifdef FEAT_FOOTER
3652 if (using_footer != prev_footer)
3653 {
3654 gui_mch_enable_footer(using_footer);
3655 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003656 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (using_footer)
3658 fix_size = TRUE;
3659 }
3660#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003661#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (using_tearoff != prev_tearoff)
3663 {
3664 gui_mch_toggle_tearoffs(using_tearoff);
3665 prev_tearoff = using_tearoff;
3666 }
3667#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003668 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003669 {
3670#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003671 long prev_Columns = Columns;
3672 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003673#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003674 // Adjust the size of the window to make the text area keep the
3675 // same size and to avoid that part of our window is off-screen
3676 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003677 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003678
3679#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003680 // GTK has the annoying habit of sending us resize events when
3681 // changing the window size ourselves. This mostly happens when
3682 // waiting for a character to arrive, quite unpredictably, and may
3683 // change Columns and Rows when we don't want it. Wait for a
3684 // character here to avoid this effect.
3685 // If you remove this, please test this command for resizing
3686 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3687 // Don't do this while starting up though.
3688 // Don't change Rows when adding menu/toolbar/tabline.
3689 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003690 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003691 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003692 if ((need_set_size & RESIZE_VERT) == 0)
3693 Rows = prev_Rows;
3694 if ((need_set_size & RESIZE_HOR) == 0)
3695 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003696#endif
3697 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003698 // When the console tabline appears or disappears the window positions
3699 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003700 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003701 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703}
3704
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3706/*
3707 * Return TRUE if the GUI is taking care of the tabline.
3708 * It may still be hidden if 'showtabline' is zero.
3709 */
3710 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003711gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003712{
3713 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3714}
3715
3716/*
3717 * Return TRUE if the GUI is showing the tabline.
3718 * This uses 'showtabline'.
3719 */
3720 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003721gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003722{
3723 if (!gui_use_tabline()
3724 || p_stal == 0
3725 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3726 return FALSE;
3727 return TRUE;
3728}
3729
3730/*
3731 * Update the tabline.
3732 * This may display/undisplay the tabline and update the labels.
3733 */
3734 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003735gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003736{
3737 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003738 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003739
3740 if (!gui.starting && starting == 0)
3741 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003742 // Updating the tabline uses direct GUI commands, flush
3743 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003744 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003745
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003746 if (!showit != !shown)
3747 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003748 if (showit != 0)
3749 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003750
Bram Moolenaar30613902019-12-01 22:11:18 +01003751 // When the tabs change from hidden to shown or from shown to
3752 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003753 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003754 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003755 }
3756}
3757
3758/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003759 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003760 */
3761 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003762get_tabline_label(
3763 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003764 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003765{
3766 int modified = FALSE;
3767 char_u buf[40];
3768 int wincount;
3769 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003770 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003771
Bram Moolenaar30613902019-12-01 22:11:18 +01003772 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003773 opt = (tooltip ? &p_gtt : &p_gtl);
3774 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003775 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003776 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003777 int called_emsg_before = called_emsg;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003778 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003779 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003780 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3781 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003782
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003783 printer_page_num = tabpage_index(tp);
3784# ifdef FEAT_EVAL
3785 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003786 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003787# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003788 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003789 curtab->tp_firstwin = firstwin;
3790 curtab->tp_lastwin = lastwin;
3791 curtab->tp_curwin = curwin;
3792 save_curtab = curtab;
3793 curtab = tp;
3794 topframe = curtab->tp_topframe;
3795 firstwin = curtab->tp_firstwin;
3796 lastwin = curtab->tp_lastwin;
3797 curwin = curtab->tp_curwin;
3798 curbuf = curwin->w_buffer;
3799
Bram Moolenaar30613902019-12-01 22:11:18 +01003800 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003801 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003802 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003803 STRCPY(NameBuff, res);
3804
Bram Moolenaar30613902019-12-01 22:11:18 +01003805 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003806 curtab = save_curtab;
3807 topframe = curtab->tp_topframe;
3808 firstwin = curtab->tp_firstwin;
3809 lastwin = curtab->tp_lastwin;
3810 curwin = curtab->tp_curwin;
3811 curbuf = curwin->w_buffer;
3812
Bram Moolenaar53989552019-12-23 22:59:18 +01003813 if (called_emsg > called_emsg_before)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003814 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003815 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003816 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003817
Bram Moolenaar30613902019-12-01 22:11:18 +01003818 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3819 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003820 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003821 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003822 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003823 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003824 if (!tooltip)
3825 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003826
3827 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3828 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3829 if (bufIsChanged(wp->w_buffer))
3830 modified = TRUE;
3831 if (modified || wincount > 1)
3832 {
3833 if (wincount > 1)
3834 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3835 else
3836 buf[0] = NUL;
3837 if (modified)
3838 STRCAT(buf, "+");
3839 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003840 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003841 mch_memmove(NameBuff, buf, STRLEN(buf));
3842 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003843 }
3844}
3845
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003846/*
3847 * Send the event for clicking to select tab page "nr".
3848 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003849 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003850 */
3851 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003852send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003853{
3854 char_u string[3];
3855
3856 if (nr == tabpage_index(curtab))
3857 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003858
Bram Moolenaar30613902019-12-01 22:11:18 +01003859 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003860 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003861# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003862 || cmdwin_type != 0
3863# endif
3864 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003865 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003866 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003867 gui_mch_set_curtab(tabpage_index(curtab));
3868 return FALSE;
3869 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003870
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003871 string[0] = CSI;
3872 string[1] = KS_TABLINE;
3873 string[2] = KE_FILLER;
3874 add_to_input_buf(string, 3);
3875 string[0] = nr;
3876 add_to_input_buf_csi(string, 1);
3877 return TRUE;
3878}
3879
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003880/*
3881 * Send a tabline menu event
3882 */
3883 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003884send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003885{
3886 char_u string[3];
3887
Bram Moolenaar29547192018-12-11 20:39:19 +01003888 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003889 if (hold_gui_events)
3890 return;
3891
Bram Moolenaar29547192018-12-11 20:39:19 +01003892 // Cannot close the last tabpage.
3893 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3894 return;
3895
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003896 string[0] = CSI;
3897 string[1] = KS_TABMENU;
3898 string[2] = KE_FILLER;
3899 add_to_input_buf(string, 3);
3900 string[0] = tabidx;
3901 string[1] = (char_u)(long)event;
3902 add_to_input_buf_csi(string, 2);
3903}
3904
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907/*
3908 * Scrollbar stuff:
3909 */
3910
Bram Moolenaare45828b2006-02-15 22:12:56 +00003911/*
3912 * Remove all scrollbars. Used before switching to another tab page.
3913 */
3914 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003915gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003916{
3917 int i;
3918 win_T *wp;
3919
3920 for (i = 0; i < 3; i++)
3921 {
3922 if (i == SBAR_BOTTOM)
3923 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3924 else
3925 {
3926 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003927 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003928 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003929 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003930 }
3931}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003934gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936 static int sbar_ident = 0;
3937
Bram Moolenaar30613902019-12-01 22:11:18 +01003938 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 sb->wp = wp;
3940 sb->type = type;
3941 sb->value = 0;
3942#ifdef FEAT_GUI_ATHENA
3943 sb->pixval = 0;
3944#endif
3945 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 Moolenaarb3f74062020-02-26 16:16:53 +01004326#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA) \
4327 || 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 Moolenaar30613902019-12-01 22:11:18 +01004359 // Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4360 // checking if the thumb moved at least a pixel. Only do this for
4361 // Athena, most other GUIs require the update anyway to make the
4362 // arrows work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363#ifdef FEAT_GUI_ATHENA
4364 if (max == 0)
4365 y = 0;
4366 else
4367 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4368 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4369#else
4370 if (force || sb->value != val || sb->size != size || sb->max != max)
4371#endif
4372 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004373 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 sb->value = val;
4375#ifdef FEAT_GUI_ATHENA
4376 sb->pixval = y;
4377#endif
4378 sb->size = size;
4379 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004380 if (gui.which_scrollbars[SBAR_LEFT]
4381 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4383 val, size, max);
4384 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004385 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4387 val, size, max);
4388 }
4389 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004390
4391 // update the title, it may show the scroll position
4392 maketitle();
4393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 --hold_gui_events;
4396}
4397
4398/*
4399 * Enable or disable a scrollbar.
4400 * Check for scrollbars for vertically split windows which are not enabled
4401 * sometimes.
4402 */
4403 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004404gui_do_scrollbar(
4405 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004406 int which, // SBAR_LEFT or SBAR_RIGHT
4407 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 int midcol = curwin->w_wincol + curwin->w_width / 2;
4410 int has_midcol = (wp->w_wincol <= midcol
4411 && wp->w_wincol + wp->w_width >= midcol);
4412
Bram Moolenaar30613902019-12-01 22:11:18 +01004413 // Only enable scrollbars that contain the middle column of the current
4414 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4416 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004417 // Scrollbars only on one side. Don't enable scrollbars that don't
4418 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (!has_midcol)
4420 enable = FALSE;
4421 }
4422 else
4423 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004424 // Scrollbars on both sides. Don't enable scrollbars that neither
4425 // contain the middle column of the current window nor are on the far
4426 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 if (midcol > Columns / 2)
4428 {
4429 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4430 enable = FALSE;
4431 }
4432 else
4433 {
4434 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4435 : !has_midcol)
4436 enable = FALSE;
4437 }
4438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4440}
4441
4442/*
4443 * Scroll a window according to the values set in the globals current_scrollbar
4444 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4445 * or FALSE otherwise.
4446 */
4447 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004448gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 win_T *wp, *save_wp;
4451 int i;
4452 long nlines;
4453 pos_T old_cursor;
4454 linenr_T old_topline;
4455#ifdef FEAT_DIFF
4456 int old_topfill;
4457#endif
4458
4459 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4460 if (wp == NULL)
4461 break;
4462 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004463 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 return FALSE;
4465
4466 /*
4467 * Compute number of lines to scroll. If zero, nothing to do.
4468 */
4469 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4470 if (nlines == 0)
4471 return FALSE;
4472
4473 save_wp = curwin;
4474 old_topline = wp->w_topline;
4475#ifdef FEAT_DIFF
4476 old_topfill = wp->w_topfill;
4477#endif
4478 old_cursor = wp->w_cursor;
4479 curwin = wp;
4480 curbuf = wp->w_buffer;
4481 if (nlines < 0)
4482 scrolldown(-nlines, gui.dragged_wp == NULL);
4483 else
4484 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004485 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4486 // the mouse-up event already, but we still want it to behave like when
4487 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (gui.dragged_sb == SBAR_NONE)
4489 gui.dragged_wp = NULL;
4490
4491 if (old_topline != wp->w_topline
4492#ifdef FEAT_DIFF
4493 || old_topfill != wp->w_topfill
4494#endif
4495 )
4496 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004497 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004499 cursor_correct(); // fix window for 'so'
4500 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502 if (old_cursor.lnum != wp->w_cursor.lnum)
4503 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506
Bram Moolenaar30613902019-12-01 22:11:18 +01004507 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004508 validate_cursor();
4509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 curwin = save_wp;
4511 curbuf = save_wp->w_buffer;
4512
4513 /*
4514 * Don't call updateWindow() when nothing has changed (it will overwrite
4515 * the status line!).
4516 */
4517 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004518 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#ifdef FEAT_DIFF
4520 || old_topfill != wp->w_topfill
4521#endif
4522 )
4523 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004524 int type = VALID;
4525
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004526 if (pum_visible())
4527 {
4528 type = NOT_VALID;
4529 wp->w_lines_valid = 0;
4530 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004531
Bram Moolenaar30613902019-12-01 22:11:18 +01004532 // Don't set must_redraw here, it may cause the popup menu to
4533 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004534 if (wp->w_redr_type < type)
4535 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004536 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004537 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004538 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540
Bram Moolenaar30613902019-12-01 22:11:18 +01004541 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004542 if (pum_visible())
4543 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004544
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004545 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546}
4547
4548
4549/*
4550 * Horizontal scrollbar stuff:
4551 */
4552
4553/*
4554 * Return length of line "lnum" for horizontal scrolling.
4555 */
4556 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004557scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558{
4559 char_u *p;
4560 colnr_T col;
4561 int w;
4562
4563 p = ml_get(lnum);
4564 col = 0;
4565 if (*p != NUL)
4566 for (;;)
4567 {
4568 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004569 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004570 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 break;
4572 col += w;
4573 }
4574 return col;
4575}
4576
Bram Moolenaar30613902019-12-01 22:11:18 +01004577// Remember which line is currently the longest, so that we don't have to
4578// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579static linenr_T longest_lnum = 0;
4580
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004581/*
4582 * Find longest visible line number. If this is not possible (or not desired,
4583 * by setting 'h' in "guioptions") then the current line number is returned.
4584 */
4585 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004586gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004587{
4588 linenr_T ret = 0;
4589
Bram Moolenaar30613902019-12-01 22:11:18 +01004590 // Calculate maximum for horizontal scrollbar. Check for reasonable
4591 // line numbers, topline and botline can be invalid when displaying is
4592 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004593 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4594 && curwin->w_topline <= curwin->w_cursor.lnum
4595 && curwin->w_botline > curwin->w_cursor.lnum
4596 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4597 {
4598 linenr_T lnum;
4599 colnr_T n;
4600 long max = 0;
4601
Bram Moolenaar30613902019-12-01 22:11:18 +01004602 // Use maximum of all visible lines. Remember the lnum of the
4603 // longest line, closest to the cursor line. Used when scrolling
4604 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004605 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4606 {
4607 n = scroll_line_len(lnum);
4608 if (n > (colnr_T)max)
4609 {
4610 max = n;
4611 ret = lnum;
4612 }
4613 else if (n == (colnr_T)max
4614 && abs((int)(lnum - curwin->w_cursor.lnum))
4615 < abs((int)(ret - curwin->w_cursor.lnum)))
4616 ret = lnum;
4617 }
4618 }
4619 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004620 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004621 ret = curwin->w_cursor.lnum;
4622
4623 return ret;
4624}
4625
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004627gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
Bram Moolenaar30613902019-12-01 22:11:18 +01004629 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 if (!gui.which_scrollbars[SBAR_BOTTOM])
4632 return;
4633
4634 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4635 return;
4636
4637 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4638 return;
4639
4640 /*
4641 * It is possible for the cursor to be invalid if we're in the middle of
4642 * something (like changing files). If so, don't do anything for now.
4643 */
4644 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4645 {
4646 gui.bottom_sbar.value = -1;
4647 return;
4648 }
4649
Bram Moolenaar02631462017-09-22 15:20:32 +02004650 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (curwin->w_p_wrap)
4652 {
4653 value = 0;
4654#ifdef SCROLL_PAST_END
4655 max = 0;
4656#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004657 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659 }
4660 else
4661 {
4662 value = curwin->w_leftcol;
4663
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004664 longest_lnum = gui_find_longest_lnum();
4665 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (virtual_active())
4668 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004669 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (curwin->w_virtcol >= (colnr_T)max)
4671 max = curwin->w_virtcol;
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004675 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004677 // The line number isn't scrolled, thus there is less space when
4678 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 size -= curwin_col_off();
4680#ifndef SCROLL_PAST_END
4681 max -= curwin_col_off();
4682#endif
4683 }
4684
4685#ifndef SCROLL_PAST_END
4686 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004687 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#endif
4689
4690#ifdef FEAT_RIGHTLEFT
4691 if (curwin->w_p_rl)
4692 {
4693 value = max + 1 - size - value;
4694 if (value < 0)
4695 {
4696 size += value;
4697 value = 0;
4698 }
4699 }
4700#endif
4701 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4702 && max == gui.bottom_sbar.max)
4703 return;
4704
4705 gui.bottom_sbar.value = value;
4706 gui.bottom_sbar.size = size;
4707 gui.bottom_sbar.max = max;
4708 gui.prev_wrap = curwin->w_p_wrap;
4709
4710 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4711}
4712
4713/*
4714 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4715 */
4716 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004717gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
Bram Moolenaar30613902019-12-01 22:11:18 +01004719 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (curwin->w_p_wrap)
4721 return FALSE;
4722
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004723 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return FALSE;
4725
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004726 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
Bram Moolenaar30613902019-12-01 22:11:18 +01004728 // When the line of the cursor is too short, move the cursor to the
4729 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004731 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004732 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004734 if (compute_longest_lnum)
4735 {
4736 curwin->w_cursor.lnum = gui_find_longest_lnum();
4737 curwin->w_cursor.col = 0;
4738 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004739 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004740 else if (longest_lnum >= curwin->w_topline
4741 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 curwin->w_cursor.lnum = longest_lnum;
4744 curwin->w_cursor.col = 0;
4745 }
4746 }
4747
4748 return leftcol_changed();
4749}
4750
4751/*
4752 * Check that none of the colors are the same as the background color
4753 */
4754 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004755gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4758 {
4759 gui_set_bg_color((char_u *)"White");
4760 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4761 gui_set_fg_color((char_u *)"Black");
4762 }
4763}
4764
Bram Moolenaar3918c952005-03-15 22:34:55 +00004765 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004766gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
4768 gui.norm_pixel = gui_get_color(name);
4769 hl_set_fg_color_name(vim_strsave(name));
4770}
4771
Bram Moolenaar3918c952005-03-15 22:34:55 +00004772 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004773gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 gui.back_pixel = gui_get_color(name);
4776 hl_set_bg_color_name(vim_strsave(name));
4777}
4778
4779/*
4780 * Allocate a color by name.
4781 * Returns INVALCOLOR and gives an error message when failed.
4782 */
4783 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004784gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 guicolor_T t;
4787
4788 if (*name == NUL)
4789 return INVALCOLOR;
4790 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004793#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 && gui.in_use
4795#endif
4796 )
Drew Vogele30d1022021-10-24 20:35:07 +01004797 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 return t;
4799}
4800
4801/*
4802 * Return the grey value of a color (range 0-255).
4803 */
4804 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004805gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004807 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004809 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004810 + (((rgb >> 8) & 0xff) * 587)
4811 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812}
4813
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004814 char_u *
4815gui_bg_default(void)
4816{
4817 if (gui_get_lightness(gui.back_pixel) < 127)
4818 return (char_u *)"dark";
4819 return (char_u *)"light";
4820}
4821
4822/*
4823 * Option initializations that can only be done after opening the GUI window.
4824 */
4825 void
4826init_gui_options(void)
4827{
Bram Moolenaar30613902019-12-01 22:11:18 +01004828 // Set the 'background' option according to the lightness of the
4829 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004830 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4831 {
4832 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4833 highlight_changed();
4834 }
4835}
4836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837#if defined(FEAT_GUI_X11) || defined(PROTO)
4838 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004839gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
4841 win_T *wp;
4842
Bram Moolenaar30613902019-12-01 22:11:18 +01004843 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (!gui.in_use)
4845 return;
4846
4847 FOR_ALL_WINDOWS(wp)
4848 {
4849 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4850 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4851 }
4852 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4853}
4854#endif
4855
4856/*
4857 * Call this when focus has changed.
4858 */
4859 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004860gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862/*
4863 * Skip this code to avoid drawing the cursor when debugging and switching
4864 * between the debugger window and gvim.
4865 */
4866#if 1
4867 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004868 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870# ifdef FEAT_XIM
4871 xim_set_focus(in_focus);
4872# endif
4873
Bram Moolenaar30613902019-12-01 22:11:18 +01004874 // Put events in the input queue only when allowed.
4875 // ui_focus_change() isn't called directly, because it invokes
4876 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004877 if (!hold_gui_events)
4878 {
4879 char_u bytes[3];
4880
4881 bytes[0] = CSI;
4882 bytes[1] = KS_EXTRA;
4883 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4884 add_to_input_buf(bytes, 3);
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#endif
4887}
4888
4889/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004890 * When mouse moved: apply 'mousefocus'.
4891 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004893 static void
4894gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004897 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
4899#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004900 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004901 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903
Bram Moolenaar30613902019-12-01 22:11:18 +01004904 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004906 && !hold_gui_events // not holding events
4907 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4908 && State != HITRETURN // but not hit-return prompt
4909 && msg_scrolled == 0 // no scrolled message
4910 && !need_mouse_correct // not moving the pointer
4911 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004913 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (x < 0 || x > Columns * gui.char_width)
4915 return;
4916#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004917 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
4919 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004920 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar30613902019-12-01 22:11:18 +01004922 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004923 if (Y_2_ROW(y) < tabline_height())
4924 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 /*
4927 * format a mouse click on status line input
4928 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004929 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4930 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
4932 if (finish_op)
4933 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004934 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 st[0] = ESC;
4936 add_to_input_buf(st, 1);
4937 }
4938 st[0] = CSI;
4939 st[1] = KS_MOUSE;
4940 st[2] = KE_FILLER;
4941 st[3] = (char_u)MOUSE_LEFT;
4942 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004943 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 wp->w_height + W_WINROW(wp));
4945
4946 add_to_input_buf(st, 8);
4947 st[3] = (char_u)MOUSE_RELEASE;
4948 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004950 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 if (gtk_main_level() > 0)
4952 gtk_main_quit();
4953#endif
4954 }
4955}
4956
4957/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004958 * Called when the mouse moved (but not when dragging).
4959 */
4960 void
4961gui_mouse_moved(int x, int y)
4962{
4963 // Ignore this while still starting up.
4964 if (!gui.in_use || gui.starting)
4965 return;
4966
4967 // apply 'mousefocus' and pointer shape
4968 gui_mouse_focus(x, y);
4969
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004970#ifdef FEAT_PROP_POPUP
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004971 if (popup_visible)
4972 // Generate a mouse-moved event, so that the popup can perhaps be
4973 // closed, just like in the terminal.
Bram Moolenaar445f11d2021-06-08 20:13:31 +02004974 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004975#endif
4976}
4977
4978/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004979 * Get the window where the mouse pointer is on.
4980 * Returns NULL if not found.
4981 */
4982 win_T *
4983gui_mouse_window(mouse_find_T popup)
4984{
4985 int x, y;
4986
4987 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4988 return NULL;
4989 gui_mch_getmouse(&x, &y);
4990
4991 // Only use the mouse when it's on the Vim window
4992 if (x >= 0 && x <= Columns * gui.char_width
4993 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4994 return xy2win(x, y, popup);
4995 return NULL;
4996}
4997
4998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 * Called when mouse should be moved to window with focus.
5000 */
5001 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005002gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 win_T *wp = NULL;
5005
5006 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005007
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005008 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01005009 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005011 validate_cline_row();
5012 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
5013 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016}
5017
5018/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005019 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01005020 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005023xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 int row;
5026 int col;
5027 win_T *wp;
5028
5029 row = Y_2_ROW(y);
5030 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01005031 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005033 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005034 if (wp == NULL)
5035 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005036#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (State == HITRETURN || State == ASKMORE)
5038 {
5039 if (Y_2_ROW(y) >= msg_row)
5040 update_mouseshape(SHAPE_IDX_MOREL);
5041 else
5042 update_mouseshape(SHAPE_IDX_MORE);
5043 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005044 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005046 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005047 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005049 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005050 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 update_mouseshape(SHAPE_IDX_STATUS);
5052 else
5053 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005055 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056}
5057
5058/*
5059 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
5060 * File names may be given to redefine the args list.
5061 */
5062 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005063ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 char_u *arg = eap->arg;
5066
5067 /*
5068 * Check for "-f" argument: foreground, don't fork.
5069 * Also don't fork when started with "gvim -f".
5070 * Do fork when using "gui -b".
5071 */
5072 if (arg[0] == '-'
5073 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005074 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 gui.dofork = (arg[1] == 'b');
5077 eap->arg = skipwhite(eap->arg + 2);
5078 }
5079 if (!gui.in_use)
5080 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005081#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01005082 if (!gui.starting)
5083 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005084 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01005085 return;
5086 }
5087#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005088 // Clear the command. Needed for when forking+exiting, to avoid part
5089 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01005091#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005092 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005093 gui_start(eap->arg);
5094 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01005095#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005096 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01005097#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005098 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005101 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 ex_next(eap);
5103}
5104
Bram Moolenaar4f974752019-02-17 17:44:42 +01005105#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005106 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
5107 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005108 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109/*
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005110 * This is shared between Athena, Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113/*
5114 * Callback function for do_in_runtimepath().
5115 */
5116 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005117gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005119 char_u *gfp_buffer = cookie;
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (STRLEN(fname) >= MAXPATHL)
5122 *gfp_buffer = NUL;
5123 else
5124 STRCPY(gfp_buffer, fname);
5125}
5126
5127/*
5128 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5129 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5130 */
5131 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005132gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134 if (STRLEN(name) > MAXPATHL - 14)
5135 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005136 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005137 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005138 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 return FAIL;
5140 return OK;
5141}
5142
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005143# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144/*
5145 * Given the name of the "icon=" argument, try finding the bitmap file for the
5146 * icon. If it is an absolute path name, use it as it is. Otherwise append
5147 * "ext" and search for it in 'runtimepath'.
5148 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5149 * contains "name".
5150 */
5151 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005152gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 char_u buf[MAXPATHL + 1];
5155
5156 expand_env(name, buffer, MAXPATHL);
5157 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5158 STRCPY(buffer, buf);
5159}
5160# endif
5161#endif
5162
Bram Moolenaar9e175142020-04-30 22:51:01 +02005163#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5164 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005166display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 char_u *p;
5169
5170 if (isatty(2))
5171 fflush(stderr);
5172 else if (error_ga.ga_data != NULL)
5173 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005174 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5176 if (!isspace(*p))
5177 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005178 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (STRLEN(p) > 2000)
5180 STRCPY(p + 2000 - 14, "...(truncated)");
5181 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005182 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184 }
5185 ga_clear(&error_ga);
5186 }
5187}
5188#endif
5189
5190#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5191/*
5192 * Return TRUE if still starting up and there is no place to enter text.
5193 * For GTK and X11 we check if stderr is not a tty, which means we were
5194 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5195 * allow typing on stdin.
5196 */
5197 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005198no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 return ((!gui.in_use || gui.starting)
5201# ifndef NO_CONSOLE
5202 && !isatty(0) && !isatty(2)
5203# endif
5204 );
5205}
5206#endif
5207
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005208#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005209 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005210 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211/*
5212 * Update the current window and the screen.
5213 */
5214 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005215gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005217# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005218 linenr_T conceal_old_cursor_line = 0;
5219 linenr_T conceal_new_cursor_line = 0;
5220 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005221# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 update_topline();
5224 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005225
Bram Moolenaar30613902019-12-01 22:11:18 +01005226 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005227 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005228# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005229 || popup_visible
5230# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005231# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005232 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005233# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005234 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005235 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005236 if (has_cursormoved())
5237 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005238#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005239 if (popup_visible)
5240 popup_check_cursor_pos();
5241#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005242# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005243 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005244 {
5245 conceal_old_cursor_line = last_cursormoved.lnum;
5246 conceal_new_cursor_line = curwin->w_cursor.lnum;
5247 conceal_update_lines = TRUE;
5248 }
5249# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005250 last_cursormoved = curwin->w_cursor;
5251 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005252
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005253# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005254 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005255 && (conceal_old_cursor_line != conceal_new_cursor_line
5256 || conceal_cursor_line(curwin)
5257 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005258 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005259 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005260 redrawWinline(curwin, conceal_old_cursor_line);
5261 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005262 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005263 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005264 }
5265# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005266 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005267 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005268 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269}
5270#endif
5271
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005272#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273/*
5274 * Get the text to use in a find/replace dialog. Uses the last search pattern
5275 * if the argument is empty.
5276 * Returns an allocated string.
5277 */
5278 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005279get_find_dialog_text(
5280 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005281 int *wwordp, // return: TRUE if \< \> found
5282 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 char_u *text;
5285
5286 if (*arg == NUL)
5287 text = last_search_pat();
5288 else
5289 text = arg;
5290 if (text != NULL)
5291 {
5292 text = vim_strsave(text);
5293 if (text != NULL)
5294 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005295 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 int i;
5297
Bram Moolenaar30613902019-12-01 22:11:18 +01005298 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5300 {
5301 mch_memmove(text, text + 2, (size_t)(len - 1));
5302 len -= 2;
5303 }
5304
Bram Moolenaar30613902019-12-01 22:11:18 +01005305 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5307 {
5308 *mcasep = (text[1] == 'C');
5309 mch_memmove(text, text + 2, (size_t)(len - 1));
5310 len -= 2;
5311 }
5312
Bram Moolenaar30613902019-12-01 22:11:18 +01005313 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (len >= 4
5315 && STRNCMP(text, "\\<", 2) == 0
5316 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5317 {
5318 *wwordp = TRUE;
5319 mch_memmove(text, text + 2, (size_t)(len - 4));
5320 text[len - 4] = NUL;
5321 }
5322
Bram Moolenaar30613902019-12-01 22:11:18 +01005323 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 for (i = 0; i + 1 < len; ++i)
5325 if (text[i] == '\\' && (text[i + 1] == '/'
5326 || text[i + 1] == '?'))
5327 {
5328 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5329 --len;
5330 }
5331 }
5332 }
5333 return text;
5334}
5335
5336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 * Handle the press of a button in the find-replace dialog.
5338 * Return TRUE when something was added to the input buffer.
5339 */
5340 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005341gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005342 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005343 char_u *find_text,
5344 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005345 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 garray_T ga;
5348 int i;
5349 int type = (flags & FRD_TYPE_MASK);
5350 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005351 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005352 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005353 static int busy = FALSE;
5354
Bram Moolenaar30613902019-12-01 22:11:18 +01005355 // When the screen is being updated we should not change buffers and
5356 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005357 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005358 if (updating_screen || busy)
5359 return FALSE;
5360
Bram Moolenaar30613902019-12-01 22:11:18 +01005361 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005362 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5363 return FALSE;
5364
5365 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005368 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 ga_concat(&ga, (char_u *)"%s/");
5370
5371 ga_concat(&ga, (char_u *)"\\V");
5372 if (flags & FRD_MATCH_CASE)
5373 ga_concat(&ga, (char_u *)"\\C");
5374 else
5375 ga_concat(&ga, (char_u *)"\\c");
5376 if (flags & FRD_WHOLE_WORD)
5377 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005378 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005379 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5380 if (p != NULL)
5381 ga_concat(&ga, p);
5382 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (flags & FRD_WHOLE_WORD)
5384 ga_concat(&ga, (char_u *)"\\>");
5385
5386 if (type == FRD_REPLACEALL)
5387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005389 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005390 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5391 if (p != NULL)
5392 ga_concat(&ga, p);
5393 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005395 }
5396 ga_append(&ga, NUL);
5397
5398 if (type == FRD_REPLACE)
5399 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005400 // Do the replacement when the text at the cursor matches. Thus no
5401 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005402 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5403 regmatch.rm_ic = 0;
5404 if (regmatch.regprog != NULL)
5405 {
5406 p = ml_get_cursor();
5407 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5408 && regmatch.startp[0] == p)
5409 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005410 // Clear the command line to remove any old "No match"
5411 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005412 msg_end_prompt();
5413
5414 if (u_save_cursor() == OK)
5415 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005416 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005417 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005418
5419 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005420 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005421 ins_str(repl_text);
5422 }
5423 }
5424 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005425 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005426 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005427 }
5428 }
5429
5430 if (type == FRD_REPLACEALL)
5431 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005432 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005433 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 do_cmdline_cmd(ga.ga_data);
5435 }
5436 else
5437 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005438 int searchflags = SEARCH_MSG + SEARCH_MARK;
5439
Bram Moolenaar30613902019-12-01 22:11:18 +01005440 // Search for the next match.
5441 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005442 if (type == FRD_REPLACE)
5443 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005445 if (down)
5446 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01005447 (void)do_search(NULL, '/', '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005448 }
5449 else
5450 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005451 // We need to escape '?' if and only if we are searching in the up
5452 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005453 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5454 if (p != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01005455 (void)do_search(NULL, '?', '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005456 vim_free(p);
5457 }
5458
Bram Moolenaar30613902019-12-01 22:11:18 +01005459 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
5461
Bram Moolenaar30613902019-12-01 22:11:18 +01005462 // Don't want to pass did_emsg to other code, it may cause disabling
5463 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005464 did_emsg = save_did_emsg;
5465
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if (State & (NORMAL | INSERT))
5467 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005468 gui_update_screen(); // update the screen
5469 msg_didout = 0; // overwrite any message
5470 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472
5473 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005474 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 return (ga.ga_len > 0);
5476}
5477
5478#endif
5479
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005480#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481/*
5482 * Jump to the window at specified point (x, y).
5483 */
5484 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005485gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 int row = Y_2_ROW(y);
5488 int col = X_2_COL(x);
5489 win_T *wp;
5490
5491 if (row >= 0 && col >= 0)
5492 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005493 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (wp != NULL && wp != curwin)
5495 win_goto(wp);
5496 }
5497}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
5499/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005500 * Function passed to handle_drop() for the actions to be done after the
5501 * argument list has been updated.
5502 */
5503 static void
5504drop_callback(void *cookie)
5505{
5506 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005507 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005508
Bram Moolenaar30613902019-12-01 22:11:18 +01005509 // If Shift held down, change to first file's directory. If the first
5510 // item is a directory, change to that directory (and let the explorer
5511 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005512 if (p != NULL)
5513 {
5514 if (mch_isdir(p))
5515 {
5516 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005517 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005518 }
5519 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005520 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005521 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005522 if (do_shorten)
5523 {
5524 shorten_fnames(TRUE);
5525 last_chdir_reason = "drop";
5526 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005527 }
5528
Bram Moolenaar30613902019-12-01 22:11:18 +01005529 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005530 update_screen(NOT_VALID);
5531# ifdef FEAT_MENU
5532 gui_update_menus(0);
5533# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005534 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005535 setcursor();
5536 out_flush_cursor(FALSE, FALSE);
5537}
5538
5539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 * Process file drop. Mouse cursor position, key modifiers, name of files
5541 * and count of files are given. Argument "fnames[count]" has full pathnames
5542 * of dropped files, they will be freed in this function, and caller can't use
5543 * fnames after call this function.
5544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005546gui_handle_drop(
5547 int x UNUSED,
5548 int y UNUSED,
5549 int_u modifiers,
5550 char_u **fnames,
5551 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 int i;
5554 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005555 static int entered = FALSE;
5556
5557 /*
5558 * This function is called by event handlers. Just in case we get a
5559 * second event before the first one is handled, ignore the second one.
5560 * Not sure if this can ever happen, just in case.
5561 */
5562 if (entered)
5563 return;
5564 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 /*
5567 * When the cursor is at the command line, add the file names to the
5568 * command line, don't edit the files.
5569 */
5570 if (State & CMDLINE)
5571 {
5572 shorten_filenames(fnames, count);
5573 for (i = 0; i < count; ++i)
5574 {
5575 if (fnames[i] != NULL)
5576 {
5577 if (i > 0)
5578 add_to_input_buf((char_u*)" ", 1);
5579
Bram Moolenaar30613902019-12-01 22:11:18 +01005580 // We don't know what command is used thus we can't be sure
5581 // about which characters need to be escaped. Only escape the
5582 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583# ifdef BACKSLASH_IN_FILENAME
5584 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5585# else
5586 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5587# endif
5588 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005589 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 vim_free(p);
5591 vim_free(fnames[i]);
5592 }
5593 }
5594 vim_free(fnames);
5595 }
5596 else
5597 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005598 // Go to the window under mouse cursor, then shorten given "fnames" by
5599 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 shorten_filenames(fnames, count);
5602
Bram Moolenaar30613902019-12-01 22:11:18 +01005603 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if ((modifiers & MOUSE_SHIFT) != 0)
5605 p = vim_strsave(fnames[0]);
5606 else
5607 p = NULL;
5608
Bram Moolenaar30613902019-12-01 22:11:18 +01005609 // Handle the drop, :edit or :split to get to the file. This also
5610 // frees fnames[]. Skip this if there is only one item, it's a
5611 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5613 && mch_isdir(fnames[0]))
5614 {
5615 vim_free(fnames[0]);
5616 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005617 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005620 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005621 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005623
5624 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005627
5628/*
5629 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5630 * applied yet.
5631 * Return the key with modifiers applied if so, NUL if not.
5632 */
5633 int
5634check_for_interrupt(int key, int modifiers_arg)
5635{
5636 int modifiers = modifiers_arg;
5637 int c = merge_modifyOtherKeys(key, &modifiers);
5638
5639 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005640#ifdef UNIX
5641 || (intr_char != Ctrl_C && c == intr_char)
5642#endif
5643 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005644 {
5645 got_int = TRUE;
5646 return c;
5647 }
5648 return NUL;
5649}
5650