blob: 2c2963d121626fb3a5fa2f78a063165095487f4f [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
Bram Moolenaar13505972019-01-24 15:04:48 +010016#if !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020020static void gui_reset_scroll_region(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010021static void gui_outstr(char_u *, int);
22static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020023static 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 +010024static void gui_delete_lines(int row, int count);
25static void gui_insert_lines(int row, int count);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020026static int gui_xy2colrow(int x, int y, int *colp);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000027#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010028static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010030static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010031static void gui_update_horiz_scrollbar(int);
32static void gui_set_fg_color(char_u *name);
33static void gui_set_bg_color(char_u *name);
Bram Moolenaar0630bb62019-11-04 22:52:12 +010034static win_T *xy2win(int x, int y, mouse_find_T popup);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020036#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010037static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020038
Bram Moolenaard25c16e2016-01-29 22:13:30 +010039static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020040
Bram Moolenaar30613902019-12-01 22:11:18 +010041// Return values for gui_read_child_pipe
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020042enum {
43 GUI_CHILD_IO_ERROR,
44 GUI_CHILD_OK,
45 GUI_CHILD_FAILED
46};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020047#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020048
Bram Moolenaard25c16e2016-01-29 22:13:30 +010049static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020050
Bram Moolenaar30613902019-12-01 22:11:18 +010051static int can_update_cursor = TRUE; // can display the cursor
52static int disable_flush = 0; // If > 0, gui_mch_flush() is disabled.
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54/*
55 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
56 * this makes the thumb indicate the part of the text that is shown. Motif
57 * can't do this.
58 */
Bram Moolenaar097148e2020-08-11 21:58:20 +020059#if defined(FEAT_GUI_ATHENA)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define SCROLL_PAST_END
61#endif
62
63/*
64 * gui_start -- Called when user wants to start the GUI.
65 *
66 * Careful: This function can be called recursively when there is a ":gui"
67 * command in the .gvimrc file. Only the first call should fork, not the
68 * recursive call.
69 */
70 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020071gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020075#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020076 char *msg = NULL;
77#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79 old_term = vim_strsave(T_NAME);
80
Bram Moolenaar30613902019-12-01 22:11:18 +010081 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010083 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 full_screen = FALSE;
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 ++recursive;
87
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020088#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020089 /*
90 * Quit the current process and continue in the child.
91 * Makes "gvim file" disconnect from the shell it was started in.
92 * Don't do this when Vim was started with "-f" or the 'f' flag is present
93 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020094 * Don't do this when there is a running job, we can only get the status
95 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020096 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020097 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
98# ifdef FEAT_JOB_CHANNEL
99 && !job_any_running()
100# endif
101 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200102 {
103 gui_do_fork();
104 }
105 else
106#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200107#ifdef GUI_MAY_SPAWN
108 if (gui.dospawn
109# ifdef EXPERIMENTAL_GUI_CMD
110 && gui.dofork
111# endif
112 && !vim_strchr(p_go, GO_FORG)
113 && !anyBufIsChanged()
114# ifdef FEAT_JOB_CHANNEL
115 && !job_any_running()
116# endif
117 )
118 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200119# ifdef EXPERIMENTAL_GUI_CMD
120 msg =
121# endif
122 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200123 }
124 else
125#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200126 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200127#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100128 // If there is 'f' in 'guioptions' and specify -g argument,
129 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200130 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100131 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200132#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200133 gui_attempt_start();
134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
Bram Moolenaar30613902019-12-01 22:11:18 +0100136 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100138 // Back to old term settings
139 //
140 // FIXME: If we got here because a child process failed and flagged to
Bram Moolenaar651fca82021-11-29 20:39:38 +0000141 // the parent to resume, and X11 is enabled, this will
Bram Moolenaar30613902019-12-01 22:11:18 +0100142 // hit an X11 I/O error and do a longjmp(), leaving recursive
143 // permanently set to 1. This is probably not as big a problem as it
144 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
145 // return "OK" unconditionally, so it would be very difficult to
146 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200147 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100148 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar30613902019-12-01 22:11:18 +0100149 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200150#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
151 if (msg)
152 emsg(msg);
153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 }
155
156 vim_free(old_term);
157
Bram Moolenaar30613902019-12-01 22:11:18 +0100158 // If the GUI started successfully, trigger the GUIEnter event, otherwise
159 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200160 gui_mch_update();
161 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
162 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200163 --recursive;
164}
165
166/*
167 * Set_termname() will call gui_init() to start the GUI.
168 * Set the "starting" flag, to indicate that the GUI will start.
169 *
170 * We don't want to open the GUI shell until after we've read .gvimrc,
171 * otherwise we don't know what font we will use, and hence we don't know
172 * what size the shell should be. So if there are errors in the .gvimrc
173 * file, they will have to go to the terminal: Set full_screen to FALSE.
174 * full_screen will be set to TRUE again by a successful termcapinit().
175 */
176 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100177gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200178{
179 static int recursive = 0;
180
181 ++recursive;
182 gui.starting = TRUE;
183
184#ifdef FEAT_GUI_GTK
185 gui.event_time = GDK_CURRENT_TIME;
186#endif
187
188 termcapinit((char_u *)"builtin_gui");
189 gui.starting = recursive - 1;
190
Bram Moolenaar9372a112005-12-06 19:59:18 +0000191#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200193 {
194# ifdef FEAT_EVAL
195 Window x11_window;
196 Display *x11_display;
197
198 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
199 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
200# endif
201
Bram Moolenaar30613902019-12-01 22:11:18 +0100202 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 --recursive;
207}
208
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200209#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200210
Bram Moolenaar30613902019-12-01 22:11:18 +0100211// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200212# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
213# include <sys/wait.h>
214# endif
215
216/*
217 * Create a new process, by forking. In the child, start the GUI, and in
218 * the parent, exit.
219 *
220 * If something goes wrong, this will return with gui.in_use still set
221 * to FALSE, in which case the caller should continue execution without
222 * the GUI.
223 *
224 * If the child fails to start the GUI, then the child will exit and the
225 * parent will return. If the child succeeds, then the parent will exit
226 * and the child will return.
227 */
228 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100229gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200230{
Bram Moolenaar30613902019-12-01 22:11:18 +0100231 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200232 int pipe_error;
233 int status;
234 int exit_status;
235 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200236
Bram Moolenaar30613902019-12-01 22:11:18 +0100237 // Setup a pipe between the child and the parent, so that the parent
238 // knows when the child has done the setsid() call and is allowed to
239 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200240 pipe_error = (pipe(pipefd) < 0);
241 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100242 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200243 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000244 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200245 return;
246 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100247 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200248 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100249 // Give the child some time to do the setsid(), otherwise the
250 // exit() may kill the child too (when starting gvim from inside a
251 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200252 if (!pipe_error)
253 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100254 // The read returns when the child closes the pipe (or when
255 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200256 close(pipefd[1]);
257 status = gui_read_child_pipe(pipefd[0]);
258 if (status == GUI_CHILD_FAILED)
259 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100260 // The child failed to start the GUI, so the caller must
261 // continue. There may be more error information written
262 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200263# ifdef __NeXT__
264 wait4(pid, &exit_status, 0, (struct rusage *)0);
265# else
266 waitpid(pid, &exit_status, 0);
267# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000268 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200269 return;
270 }
271 else if (status == GUI_CHILD_IO_ERROR)
272 {
273 pipe_error = TRUE;
274 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100275 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200276 }
277
278 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100279 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200280
Bram Moolenaar30613902019-12-01 22:11:18 +0100281 // When swapping screens we may need to go to the next line, e.g.,
282 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200283 if (newline_on_exit)
284 mch_errmsg("\r\n");
285
286 /*
287 * The parent must skip the normal exit() processing, the child
288 * will do it. For example, GTK messes up signals when exiting.
289 */
290 _exit(0);
291 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100292 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200293
Bram Moolenaar29695702012-05-18 17:03:18 +0200294#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100295 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200296 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100297 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200298#endif
299
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200300# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
301 /*
302 * Change our process group. On some systems/shells a CTRL-C in the
303 * shell where Vim was started would otherwise kill gvim!
304 */
305# if defined(HAVE_SETSID)
306 (void)setsid();
307# else
308 (void)setpgid(0, 0);
309# endif
310# endif
311 if (!pipe_error)
312 close(pipefd[0]);
313
314# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100315 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200316 gui_mch_forked();
317# endif
318
Bram Moolenaar30613902019-12-01 22:11:18 +0100319 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200320 gui_attempt_start();
321
Bram Moolenaar30613902019-12-01 22:11:18 +0100322 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200323 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200324 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200325 if (gui.in_use)
326 write_eintr(pipefd[1], "ok", 3);
327 else
328 write_eintr(pipefd[1], "fail", 5);
329 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200330 }
331
Bram Moolenaar30613902019-12-01 22:11:18 +0100332 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200333 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100334 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200335}
336
337/*
338 * Read from a pipe assumed to be connected to the child process (this
339 * function is called from the parent).
340 * Return GUI_CHILD_OK if the child successfully started the GUI,
341 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
342 * some other error.
343 *
344 * The file descriptor will be closed before the function returns.
345 */
346 static int
347gui_read_child_pipe(int fd)
348{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200349 long bytes_read;
350#define READ_BUFFER_SIZE 10
351 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200352
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200353 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
354#undef READ_BUFFER_SIZE
355 close(fd);
356 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200357 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200358 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200359 if (strcmp(buffer, "ok") == 0)
360 return GUI_CHILD_OK;
361 return GUI_CHILD_FAILED;
362}
363
Bram Moolenaar30613902019-12-01 22:11:18 +0100364#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
367 * Call this when vim starts up, whether or not the GUI is started
368 */
369 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100370gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
Bram Moolenaar30613902019-12-01 22:11:18 +0100372 gui.in_use = FALSE; // No GUI yet (maybe later)
373 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 gui_mch_prepare(argc, argv);
375}
376
377/*
378 * Try initializing the GUI and check if it can be started.
379 * Used from main() to check early if "vim -g" can start the GUI.
380 * Used from gui_init() to prepare for starting the GUI.
381 * Returns FAIL or OK.
382 */
383 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100384gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
386 static int result = MAYBE;
387
388 if (result != MAYBE)
389 {
390 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000391 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 return result;
393 }
394
395 gui.shell_created = FALSE;
396 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100397 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 gui.dragged_sb = SBAR_NONE;
399 gui.dragged_wp = NULL;
400 gui.pointer_hidden = FALSE;
401 gui.col = 0;
402 gui.row = 0;
403 gui.num_cols = Columns;
404 gui.num_rows = Rows;
405
406 gui.cursor_is_valid = FALSE;
407 gui.scroll_region_top = 0;
408 gui.scroll_region_bot = Rows - 1;
409 gui.scroll_region_left = 0;
410 gui.scroll_region_right = Columns - 1;
411 gui.highlight_mask = HL_NORMAL;
412 gui.char_width = 1;
413 gui.char_height = 1;
414 gui.char_ascent = 0;
415 gui.border_width = 0;
416
417 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200418#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 gui.bold_font = NOFONT;
420 gui.ital_font = NOFONT;
421 gui.boldital_font = NOFONT;
422# ifdef FEAT_XFONTSET
423 gui.fontset = NOFONTSET;
424# endif
425#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200426 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100427#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200428 gui.wide_bold_font = NOFONT;
429 gui.wide_ital_font = NOFONT;
430 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200434# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435# ifdef FONTSET_ALWAYS
436 gui.menu_fontset = NOFONTSET;
437# else
438 gui.menu_font = NOFONT;
439# endif
440# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100441 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# ifndef FEAT_GUI_GTK
443 gui.menu_height = MENU_DEFAULT_HEIGHT;
444 gui.menu_width = 0;
445# endif
446#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100447#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
448 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 gui.toolbar_height = 0;
450#endif
451#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
452 gui.footer_height = 0;
453#endif
454#ifdef FEAT_BEVAL_TIP
455 gui.tooltip_fontset = NOFONTSET;
456#endif
457
458 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
459 gui.prev_wrap = -1;
460
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100461# ifdef FEAT_GUI_GTK
462 CLEAR_FIELD(gui.ligatures_map);
463#endif
464
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200465#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 result = OK;
467#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200468# ifdef FEAT_GUI_GTK
469 /*
470 * Note: Don't call gtk_init_check() before fork, it will be called after
471 * the fork. When calling it before fork, it make vim hang for a while.
472 * See gui_do_fork().
473 * Use a simpler check if the GUI window can probably be opened.
474 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200475 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200476# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479#endif
480 return result;
481}
482
483/*
484 * This is the call which starts the GUI.
485 */
486 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100487gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
489 win_T *wp;
490 static int recursive = 0;
491
492 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000493 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 * function will then be executed at the first call, the rest by the
495 * recursive call. This allow the shell to be opened halfway reading a
496 * gvimrc file.
497 */
498 if (!recursive)
499 {
500 ++recursive;
501
502 clip_init(TRUE);
503
Bram Moolenaar30613902019-12-01 22:11:18 +0100504 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 if (gui_init_check() == FAIL)
506 {
507 --recursive;
508 clip_init(FALSE);
509 return;
510 }
511
512 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000513 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
514 * breaks the Paste toolbar button.
515 */
516 set_option_value((char_u *)"paste", 0L, NULL, 0);
517
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200518 // Set t_Co to the number of colors: RGB.
519 set_color_count(256 * 256 * 256);
520
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000521 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 * Set up system-wide default menus.
523 */
524#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
525 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
526 {
527 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 sys_menu = FALSE;
530 }
531#endif
532
533 /*
534 * Switch on the mouse by default, unless the user changed it already.
535 * This can then be changed in the .gvimrc.
536 */
537 if (!option_was_set((char_u *)"mouse"))
538 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000539 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
541 /*
542 * If -U option given, use only the initializations from that file and
543 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
544 */
545 if (use_gvimrc != NULL)
546 {
547 if (STRCMP(use_gvimrc, "NONE") != 0
548 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000550 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
552 else
553 {
554 /*
555 * Get system wide defaults for gvim, only when file name defined.
556 */
557#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560
561 /*
562 * Try to read GUI initialization commands from the following
563 * places:
564 * - environment variable GVIMINIT
565 * - the user gvimrc file (~/.gvimrc)
566 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
567 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
568 * The first that exists is used, the rest is ignored.
569 */
570 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000571 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000574 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200577#ifdef USR_GVIMRC_FILE3
578 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 )
582 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200583#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
585 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#endif
587 }
588
589 /*
590 * Read initialization commands from ".gvimrc" in current
591 * directory. This is only done if the 'exrc' option is set.
592 * Because of security reasons we disallow shell and write
593 * commands now, except for unix if the file is owned by the user
594 * or 'secure' option has been reset in environment of global
595 * ".gvimrc".
596 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
597 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
598 */
599 if (p_exrc)
600 {
601#ifdef UNIX
602 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200603 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaar30613902019-12-01 22:11:18 +0100605 // if ".gvimrc" file is not owned by user, set 'secure'
606 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
608 secure = p_secure;
609 }
610#else
611 secure = p_secure;
612#endif
613
614 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200615 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#ifdef SYS_GVIMRC_FILE
617 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200618 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#endif
620#ifdef USR_GVIMRC_FILE2
621 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200622 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#endif
624#ifdef USR_GVIMRC_FILE3
625 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200626 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200628#ifdef USR_GVIMRC_FILE4
629 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200630 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 if (secure == 2)
636 need_wait_return = TRUE;
637 secure = 0;
638 }
639 }
640
641 if (need_wait_return || msg_didany)
642 wait_return(TRUE);
643
644 --recursive;
645 }
646
Bram Moolenaar30613902019-12-01 22:11:18 +0100647 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (gui.in_use)
649 return;
650
651 /*
652 * Create the GUI shell.
653 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100654 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 if (gui_mch_init() == FAIL)
656 goto error;
657
Bram Moolenaar30613902019-12-01 22:11:18 +0100658 // Avoid a delay for an error message that was printed in the terminal
659 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 emsg_on_display = FALSE;
661 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100662 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 need_wait_return = FALSE;
664 msg_didany = FALSE;
665
666 /*
667 * Check validity of any generic resources that may have been loaded.
668 */
669 if (gui.border_width < 0)
670 gui.border_width = 0;
671
672 /*
673 * Set up the fonts. First use a font specified with "-fn" or "-font".
674 */
675 if (font_argument != NULL)
676 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
677 if (
678#ifdef FEAT_XFONTSET
679 (*p_guifontset == NUL
680 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
681#endif
682 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
683 : p_guifont, FALSE) == FAIL)
684 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000685 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 goto error2;
687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000689 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 gui.num_cols = Columns;
692 gui.num_rows = Rows;
693 gui_reset_scroll_region();
694
Bram Moolenaar30613902019-12-01 22:11:18 +0100695 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 FOR_ALL_WINDOWS(wp)
697 {
698 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
699 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
700 }
701 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
702
703#ifdef FEAT_MENU
704 gui_create_initial_menus(root_menu);
705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_SIGN_ICONS
707 sign_gui_started();
708#endif
709
Bram Moolenaar30613902019-12-01 22:11:18 +0100710 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 gui_init_which_components(NULL);
712
Bram Moolenaar30613902019-12-01 22:11:18 +0100713 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 gui.shell_created = TRUE;
715
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100716#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100717 // Set the shell size, adjusted for the screen size. For GTK this only
718 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100719 // If the window is already maximized (e.g. when --windowid is passed in),
720 // we want to use the system-provided dimensions by passing FALSE to
721 // mustset. Otherwise, we want to initialize with the default rows/columns.
722 if (gui_mch_maximized())
723 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
724 else
725 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100726#else
727# ifndef FEAT_GUI_GTK
728 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#endif
731#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100732 // Need to set the size of the menubar after all the menus have been
733 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 gui_mch_compute_menu_height((Widget)0);
735#endif
736
737 /*
738 * Actually open the GUI shell.
739 */
740 if (gui_mch_open() != FAIL)
741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 maketitle();
743 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 init_gui_options();
746#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100747 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 p_tbidi = FALSE;
749#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000750#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100751 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000753
754# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100755 // If there is no 'm' in 'guioptions' we need to remove the menu now.
756 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000757 if (vim_strchr(p_go, GO_MENUS) == NULL)
758 {
759 --gui.starting;
760 gui_mch_enable_menu(FALSE);
761 ++gui.starting;
762 gui_mch_update();
763 }
764# endif
765
Bram Moolenaar30613902019-12-01 22:11:18 +0100766 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100767 if (gui_mch_maximized())
768 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
769 else
770 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100772 // When 'lines' was set while starting up the topframe may have to be
773 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000774 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000775
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100776#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100777 // Always create the Balloon Evaluation area, but disable it when
778 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100779 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200780 {
781# ifdef FEAT_VARTABS
782 vim_free(balloonEval->vts);
783# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100784 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200785 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100786 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000787# ifdef FEAT_GUI_GTK
788 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
789 &general_beval_cb, NULL);
790# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000791# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000792 {
793 extern Widget textArea;
794 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000795 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000796 }
797# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100798# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000799 balloonEval = gui_mch_create_beval_area(NULL, NULL,
800 &general_beval_cb, NULL);
801# endif
802# endif
803# endif
804 if (!p_beval)
805 gui_mch_disable_beval_area(balloonEval);
806#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200807
808#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200809 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200810 // Don't do this for MS-Windows yet, it sends CTRL-K without the
811 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200812 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200813#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
816 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000817 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100819 // When 'cmdheight' was set during startup it may not have taken
820 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000821 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000822 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824 return;
825 }
826
827error2:
828#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100829 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 gui_mch_uninit();
831#endif
832
833error:
834 gui.in_use = FALSE;
835 clip_init(FALSE);
836}
837
838
839 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100840gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaar30613902019-12-01 22:11:18 +0100842 // don't free the fonts, it leads to a BUS error
843 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 gui.in_use = FALSE;
846 gui_mch_exit(rc);
847}
848
Bram Moolenaar9372a112005-12-06 19:59:18 +0000849#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200850 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000851# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852/*
853 * Called when the GUI shell is closed by the user. If there are no changed
854 * files Vim exits, otherwise there will be a dialog to ask the user what to
855 * do.
856 * When this function returns, Vim should NOT exit!
857 */
858 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100859gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200861 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
Bram Moolenaar3552e742021-05-29 12:21:58 +0200863 if (before_quit_autocmds(curwin, TRUE, FALSE))
864 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar30613902019-12-01 22:11:18 +0100866 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 exiting = TRUE;
868# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200869 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870# endif
871# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200872 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100874 // If there are changed buffers, present the user with a dialog if
875 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100876 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 getout(0);
878
879 exiting = FALSE;
880 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100881 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882}
883#endif
884
885/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200886 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 * first font name that works is used. If none is found, use the default
888 * font.
889 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
890 * Return OK when able to set the font. When it failed FAIL is returned and
891 * the fonts are unchanged.
892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100894gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896#define FONTLEN 320
897 char_u font_name[FONTLEN];
898 int font_list_empty = FALSE;
899 int ret = FAIL;
900
901 if (!gui.in_use)
902 return FAIL;
903
904 font_name[0] = NUL;
905 if (*font_list == NUL)
906 font_list_empty = TRUE;
907 else
908 {
909#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100910 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (fontset)
912 ret = gui_mch_init_font(font_list, TRUE);
913 else
914#endif
915 while (*font_list != NUL)
916 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100917 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
919
Bram Moolenaar30613902019-12-01 22:11:18 +0100920 // Careful!!! The Win32 version of gui_mch_init_font(), when
921 // called with "*" will change p_guifont to the selected font
922 // name, which frees the old value. This makes font_list
923 // invalid. Thus when OK is returned here, font_list must no
924 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (gui_mch_init_font(font_name, FALSE) == OK)
926 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100927#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100928 // If it's a Unicode font, try setting 'guifontwide' to a
929 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
931 && strstr((char *)font_name, "10646") != NULL)
932 set_guifontwide(font_name);
933#endif
934 ret = OK;
935 break;
936 }
937 }
938 }
939
940 if (ret != OK
941 && STRCMP(font_list, "*") != 0
942 && (font_list_empty || gui.norm_font == NOFONT))
943 {
944 /*
945 * Couldn't load any font in 'font_list', keep the current font if
946 * there is one. If 'font_list' is empty, or if there is no current
947 * font, tell gui_mch_init_font() to try to find a font we can load.
948 */
949 ret = gui_mch_init_font(NULL, FALSE);
950 }
951
952 if (ret == OK)
953 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200954#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100955 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956# ifdef FEAT_XFONTSET
957 if (gui.fontset != NOFONTSET)
958 gui_mch_set_fontset(gui.fontset);
959 else
960# endif
961 gui_mch_set_font(gui.norm_font);
962#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100963 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965
966 return ret;
967}
968
Bram Moolenaar13505972019-01-24 15:04:48 +0100969#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970/*
971 * Try setting 'guifontwide' to a font twice as wide as "name".
972 */
973 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100974set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100977 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 char_u *wp = NULL;
979 char_u *p;
980 GuiFont font;
981
982 wp = wide_name;
983 for (p = name; *p != NUL; ++p)
984 {
985 *wp++ = *p;
986 if (*p == '-')
987 {
988 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100989 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
991 if (p[1] == '-')
992 *wp++ = '*';
993 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100994 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
996 ++p;
997 i = getdigits(&p);
998 if (i != 0)
999 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001000 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 sprintf((char *)wp, "%d%s", i * 2, p);
1002 font = gui_mch_get_font(wide_name, FALSE);
1003 if (font != NOFONT)
1004 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001005 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 gui.wide_font = font;
1007 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001008 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
1010 }
1011 break;
1012 }
1013 }
1014 }
1015}
Bram Moolenaar30613902019-12-01 22:11:18 +01001016#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018/*
1019 * Get the font for 'guifontwide'.
1020 * Return FAIL for an invalid font name.
1021 */
1022 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001023gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 GuiFont font = NOFONT;
1026 char_u font_name[FONTLEN];
1027 char_u *p;
1028
Bram Moolenaar30613902019-12-01 22:11:18 +01001029 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1030 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1033 {
1034 for (p = p_guifontwide; *p != NUL; )
1035 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001036 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1038 font = gui_mch_get_font(font_name, FALSE);
1039 if (font != NOFONT)
1040 break;
1041 }
1042 if (font == NOFONT)
1043 return FAIL;
1044 }
1045
1046 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001047#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001048 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (font != NOFONT && gui.norm_font != NOFONT
1050 && pango_font_description_equal(font, gui.norm_font))
1051 {
1052 gui.wide_font = NOFONT;
1053 gui_mch_free_font(font);
1054 }
1055 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001058#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001059 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001060#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001061 /*
1062 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1063 * support those fonts for 'guifontwide'.
1064 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 return OK;
1067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001069#if defined(FEAT_GUI_GTK) || defined(PROTO)
1070/*
1071 * Set list of ascii characters that combined can create ligature.
1072 * Store them in char map for quick access from gui_gtk2_draw_string.
1073 */
1074 void
1075gui_set_ligatures(void)
1076{
1077 char_u *p;
1078
1079 if (*p_guiligatures != NUL)
1080 {
1081 // check for invalid characters
1082 for (p = p_guiligatures; *p != NUL; ++p)
1083 if (*p < 32 || *p > 127)
1084 {
1085 emsg(_(e_ascii_code_not_in_range));
1086 return;
1087 }
1088
1089 // store valid setting into ligatures_map
1090 CLEAR_FIELD(gui.ligatures_map);
1091 for (p = p_guiligatures; *p != NUL; ++p)
1092 gui.ligatures_map[*p] = 1;
1093 }
1094 else
1095 CLEAR_FIELD(gui.ligatures_map);
1096}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001097
1098/*
1099 * Adjust the columns to undraw for when the cursor is on ligatures.
1100 */
1101 static void
1102gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1103{
1104 int off;
1105
1106 if (ScreenLines == NULL || *p_guiligatures == NUL)
1107 return;
1108
1109 // expand before the cursor for all the chars in gui.ligatures_map
1110 off = LineOffset[gui.cursor_row] + *startcol;
1111 if (gui.ligatures_map[ScreenLines[off]])
1112 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1113 (*startcol)--;
1114
1115 // expand after the cursor for all the chars in gui.ligatures_map
1116 off = LineOffset[gui.cursor_row] + *endcol;
1117 if (gui.ligatures_map[ScreenLines[off]])
1118 while (*endcol < ((int)screen_Columns - 1)
1119 && gui.ligatures_map[ScreenLines[++off]])
1120 (*endcol)++;
1121}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001122#endif
1123
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001124 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001125gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127 gui.row = row;
1128 gui.col = col;
1129}
1130
1131/*
1132 * gui_check_pos - check if the cursor is on the screen.
1133 */
1134 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001135gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 if (gui.row >= screen_Rows)
1138 gui.row = screen_Rows - 1;
1139 if (gui.col >= screen_Columns)
1140 gui.col = screen_Columns - 1;
1141 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1142 gui.cursor_is_valid = FALSE;
1143}
1144
1145/*
1146 * Redraw the cursor if necessary or when forced.
1147 * Careful: The contents of ScreenLines[] must match what is on the screen,
1148 * otherwise this goes wrong. May need to call out_flush() first.
1149 */
1150 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001151gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001152 int force, // when TRUE, update even when not moved
1153 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155 int cur_width = 0;
1156 int cur_height = 0;
1157 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001158 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001160#ifdef FEAT_TERMINAL
1161 guicolor_T shape_fg = INVALCOLOR;
1162 guicolor_T shape_bg = INVALCOLOR;
1163#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001164 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1165 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 int attr;
1167 attrentry_T *aep = NULL;
1168
Bram Moolenaar30613902019-12-01 22:11:18 +01001169 // Don't update the cursor when halfway busy scrolling or the screen size
1170 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001171 if (!can_update_cursor || screen_Columns != gui.num_cols
1172 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 return;
1174
1175 gui_check_pos();
1176 if (!gui.cursor_is_valid || force
1177 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1178 {
1179 gui_undraw_cursor();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001180
1181 // If a cursor-less sleep is ongoing, leave the cursor invisible
1182 if (cursor_is_sleeping())
1183 return;
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (gui.row < 0)
1186 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001187#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1189 im_set_position(gui.row, gui.col);
1190#endif
1191 gui.cursor_row = gui.row;
1192 gui.cursor_col = gui.col;
1193
Bram Moolenaar30613902019-12-01 22:11:18 +01001194 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (!screen_cleared || ScreenLines == NULL)
1196 return;
1197
Bram Moolenaar30613902019-12-01 22:11:18 +01001198 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (clear_selection)
1200 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001201 // Check that the cursor is inside the shell (resizing may have made
1202 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1204 return;
1205
1206 gui.cursor_is_valid = TRUE;
1207
1208 /*
1209 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001210 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001212#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001213 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001214 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001216#endif
1217 shape = &shape_table[get_shape_idx(FALSE)];
1218 if (State & LANGMAP)
1219 id = shape->id_lm;
1220 else
1221 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar30613902019-12-01 22:11:18 +01001223 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 cfg = INVALCOLOR;
1225 cbg = INVALCOLOR;
1226 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001227 gui_mch_set_blinking(shape->blinkwait,
1228 shape->blinkon,
1229 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001230 if (shape->blinkwait == 0 || shape->blinkon == 0
1231 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001232 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001233#ifdef FEAT_TERMINAL
1234 if (shape_bg != INVALCOLOR)
1235 {
1236 cattr = 0;
1237 cfg = shape_fg;
1238 cbg = shape_bg;
1239 }
1240 else
1241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (id > 0)
1243 {
1244 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001245#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 static int iid;
1248 guicolor_T fg, bg;
1249
Bram Moolenaarc236c162008-07-13 17:41:49 +00001250 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001251# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001252 preedit_get_status()
1253# else
1254 im_get_status()
1255# endif
1256 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 iid = syn_name2id((char_u *)"CursorIM");
1259 if (iid > 0)
1260 {
1261 syn_id2colors(iid, &fg, &bg);
1262 if (bg != INVALCOLOR)
1263 cbg = bg;
1264 if (fg != INVALCOLOR)
1265 cfg = fg;
1266 }
1267 }
1268 }
1269#endif
1270 }
1271
1272 /*
1273 * Get the attributes for the character under the cursor.
1274 * When no cursor color was given, use the character color.
1275 */
1276 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1277 if (attr > HL_ALL)
1278 aep = syn_gui_attr2entry(attr);
1279 if (aep != NULL)
1280 {
1281 attr = aep->ae_attr;
1282 if (cfg == INVALCOLOR)
1283 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1284 : aep->ae_u.gui.fg_color);
1285 if (cbg == INVALCOLOR)
1286 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1287 : aep->ae_u.gui.bg_color);
1288 }
1289 if (cfg == INVALCOLOR)
1290 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1291 if (cbg == INVALCOLOR)
1292 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1293
1294#ifdef FEAT_XIM
1295 if (aep != NULL)
1296 {
1297 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1298 : aep->ae_u.gui.bg_color);
1299 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1300 : aep->ae_u.gui.fg_color);
1301 if (xim_bg_color == INVALCOLOR)
1302 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1303 : gui.back_pixel;
1304 if (xim_fg_color == INVALCOLOR)
1305 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1306 : gui.norm_pixel;
1307 }
1308 else
1309 {
1310 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1311 : gui.back_pixel;
1312 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1313 : gui.norm_pixel;
1314 }
1315#endif
1316
1317 attr &= ~HL_INVERSE;
1318 if (cattr & HL_INVERSE)
1319 {
1320 cc = cbg;
1321 cbg = cfg;
1322 cfg = cc;
1323 }
1324 cattr &= ~HL_INVERSE;
1325
1326 /*
1327 * When we don't have window focus, draw a hollow cursor.
1328 */
1329 if (!gui.in_focus)
1330 {
1331 gui_mch_draw_hollow_cursor(cbg);
1332 return;
1333 }
1334
1335 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001336 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 /*
1339 * Draw the text character with the cursor colors. Use the
1340 * character attributes plus the cursor attributes.
1341 */
1342 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001343 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1345 }
1346 else
1347 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001348#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 int col_off = FALSE;
1350#endif
1351 /*
1352 * First draw the partial cursor, then overwrite with the text
1353 * character, using a transparent background.
1354 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001355 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001358 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 else
1361 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001362 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 cur_width = gui.char_width;
1364 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001365 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1366 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001368 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001369 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001371#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (CURSOR_BAR_RIGHT)
1373 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001374 // gui.col points to the left half of the character but
1375 // the vertical line needs to be on the right half.
Bram Moolenaar30613902019-12-01 22:11:18 +01001376 // A double-wide horizontal line is also drawn from the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001377 // right half in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 col_off = TRUE;
1379 ++gui.col;
1380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001384#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (col_off)
1386 --gui.col;
1387#endif
1388
Bram Moolenaar30613902019-12-01 22:11:18 +01001389#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1391 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1392 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1393 (guicolor_T)0, (guicolor_T)0, 0);
1394#endif
1395 }
1396 gui.highlight_mask = old_hl_mask;
1397 }
1398}
1399
1400#if defined(FEAT_MENU) || defined(PROTO)
1401 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001402gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001404# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (gui.menu_is_active && gui.in_use)
1406 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1407# endif
1408}
1409#endif
1410
1411/*
1412 * Position the various GUI components (text area, menu). The vertical
1413 * scrollbars are NOT handled here. See gui_update_scrollbars().
1414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001416gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418 int text_area_x;
1419 int text_area_y;
1420 int text_area_width;
1421 int text_area_height;
1422
Bram Moolenaar30613902019-12-01 22:11:18 +01001423 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 ++hold_gui_events;
1425
1426 text_area_x = 0;
1427 if (gui.which_scrollbars[SBAR_LEFT])
1428 text_area_x += gui.scrollbar_width;
1429
1430 text_area_y = 0;
1431#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1432 gui.menu_width = total_width;
1433 if (gui.menu_is_active)
1434 text_area_y += gui.menu_height;
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001437# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001438 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001439 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001440 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001441#endif
1442
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001443#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001444 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1446 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001447# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 gui_mch_set_toolbar_pos(0, text_area_y,
1449 gui.menu_width, gui.toolbar_height);
1450# endif
1451 text_area_y += gui.toolbar_height;
1452 }
1453#endif
1454
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001455# if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
1456 gui_mch_set_tabline_pos(0, text_area_y,
1457 gui.menu_width, gui.tabline_height);
1458 if (gui_has_tabline())
1459 text_area_y += gui.tabline_height;
1460#endif
1461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1463 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1464
1465 gui_mch_set_text_area_pos(text_area_x,
1466 text_area_y,
1467 text_area_width,
1468 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001469#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 + xim_get_status_area_height()
1471#endif
1472 );
1473#ifdef FEAT_MENU
1474 gui_position_menu();
1475#endif
1476 if (gui.which_scrollbars[SBAR_BOTTOM])
1477 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1478 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001479 text_area_y + text_area_height
1480 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 text_area_width,
1482 gui.scrollbar_height);
1483 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001484 gui.right_sbar_x = text_area_x + text_area_width
1485 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
1487 --hold_gui_events;
1488}
1489
Bram Moolenaar02743632005-07-25 20:42:36 +00001490/*
1491 * Get the width of the widgets and decorations to the side of the text area.
1492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001494gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 int base_width;
1497
1498 base_width = 2 * gui.border_offset;
1499 if (gui.which_scrollbars[SBAR_LEFT])
1500 base_width += gui.scrollbar_width;
1501 if (gui.which_scrollbars[SBAR_RIGHT])
1502 base_width += gui.scrollbar_width;
1503 return base_width;
1504}
1505
Bram Moolenaar02743632005-07-25 20:42:36 +00001506/*
1507 * Get the height of the widgets and decorations above and below the text area.
1508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001510gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 int base_height;
1513
1514 base_height = 2 * gui.border_offset;
1515 if (gui.which_scrollbars[SBAR_BOTTOM])
1516 base_height += gui.scrollbar_height;
1517#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001518 // We can't take the sizes properly into account until anything is
1519 // realized. Therefore we recalculate all the values here just before
1520 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521#else
1522# ifdef FEAT_MENU
1523 if (gui.menu_is_active)
1524 base_height += gui.menu_height;
1525# endif
1526# ifdef FEAT_TOOLBAR
1527 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001530# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001531 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001532 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001533 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535# ifdef FEAT_FOOTER
1536 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1537 base_height += gui.footer_height;
1538# endif
1539# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1540 base_height += gui_mch_text_area_extra_height();
1541# endif
1542#endif
1543 return base_height;
1544}
1545
1546/*
1547 * Should be called after the GUI shell has been resized. Its arguments are
1548 * the new width and height of the shell in pixels.
1549 */
1550 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001551gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 static int busy = FALSE;
1554
Bram Moolenaar30613902019-12-01 22:11:18 +01001555 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return;
1557
1558 /*
1559 * Can't resize the screen while it is being redrawn. Remember the new
1560 * size and handle it later.
1561 */
1562 if (updating_screen || busy)
1563 {
1564 new_pixel_width = pixel_width;
1565 new_pixel_height = pixel_height;
1566 return;
1567 }
1568
1569again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001570 new_pixel_width = 0;
1571 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 busy = TRUE;
1573
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001574#ifdef FEAT_GUI_HAIKU
1575 vim_lock_screen();
1576#endif
1577
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 out_flush();
1580
1581 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001582 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 /*
1588 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1589 * at the last line here (why does it have to be one row too low?).
1590 */
1591 if (State == ASKMORE || State == CONFIRM)
1592 gui.row = gui.num_rows;
1593
Bram Moolenaar30613902019-12-01 22:11:18 +01001594 // Only comparing Rows and Columns may be sufficient, but let's stay on
1595 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1597 || gui.num_rows != Rows || gui.num_cols != Columns)
1598 shell_resized();
1599
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001600#ifdef FEAT_GUI_HAIKU
1601 vim_unlock_screen();
1602#endif
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 gui_update_scrollbars(TRUE);
1605 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001606#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 xim_set_status_area();
1608#endif
1609
1610 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001611
Bram Moolenaar30613902019-12-01 22:11:18 +01001612 // We may have been called again while redrawing the screen.
1613 // Need to do it all again with the latest size then. But only if the size
1614 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (new_pixel_height)
1616 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001617 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1618 {
1619 new_pixel_width = 0;
1620 new_pixel_height = 0;
1621 }
1622 else
1623 {
1624 pixel_width = new_pixel_width;
1625 pixel_height = new_pixel_height;
1626 goto again;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629}
1630
1631/*
1632 * Check if gui_resize_shell() must be called.
1633 */
1634 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001635gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001638 // careful: gui_resize_shell() may postpone the resize again if we
1639 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001640 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641}
1642
1643 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001644gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 Rows = gui.num_rows;
1647 Columns = gui.num_cols;
1648 return OK;
1649}
1650
1651/*
1652 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001653 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1654 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001655 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1656 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001659gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001660 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001661 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001662 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 int base_width;
1665 int base_height;
1666 int width;
1667 int height;
1668 int min_width;
1669 int min_height;
1670 int screen_w;
1671 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001672#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001673 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001674 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001675#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001676 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 if (!gui.shell_created)
1679 return;
1680
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001681#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001682 // If not setting to a user specified size and maximized, calculate the
1683 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001684 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1685 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 gui_mch_newfont();
1688 return;
1689 }
1690#endif
1691
1692 base_width = gui_get_base_width();
1693 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001694 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001695 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001696 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001697
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001698 width = Columns * gui.char_width + base_width;
1699 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701 if (fit_to_display)
1702 {
1703 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001704 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 {
1706 Columns = (screen_w - base_width) / gui.char_width;
1707 if (Columns < MIN_COLUMNS)
1708 Columns = MIN_COLUMNS;
1709 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001710#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001711 ++did_adjust;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001714 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
1716 Rows = (screen_h - base_height) / gui.char_height;
1717 check_shellsize();
1718 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001719#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001720 ++did_adjust;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001723#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001724 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1725 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001726 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001727 un_maximize = FALSE;
1728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001730 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 gui.num_cols = Columns;
1732 gui.num_rows = Rows;
1733
1734 min_width = base_width + MIN_COLUMNS * gui.char_width;
1735 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001736 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001737
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001738#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001739 if (un_maximize)
1740 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001741 // If the window size is smaller than the screen unmaximize the
1742 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001743 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1744 if ((width + gui.char_width < screen_w
1745 || height + gui.char_height * 2 < screen_h)
1746 && gui_mch_maximized())
1747 gui_mch_unmaximize();
1748 }
1749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001752 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001754 if (fit_to_display && x >= 0 && y >= 0)
1755 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001756 // Some window managers put the Vim window left of/above the screen.
1757 // Only change the position if it wasn't already negative before
1758 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 gui_mch_update();
1760 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1761 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1762 }
1763
1764 gui_position_components(width);
1765 gui_update_scrollbars(TRUE);
1766 gui_reset_scroll_region();
1767}
1768
1769/*
1770 * Called when Rows and/or Columns has changed.
1771 */
1772 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001773gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 gui_reset_scroll_region();
1776}
1777
1778/*
1779 * Make scroll region cover whole screen.
1780 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001781 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001782gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 gui.scroll_region_top = 0;
1785 gui.scroll_region_bot = gui.num_rows - 1;
1786 gui.scroll_region_left = 0;
1787 gui.scroll_region_right = gui.num_cols - 1;
1788}
1789
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001790 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001791gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
Bram Moolenaar30613902019-12-01 22:11:18 +01001793 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001795 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 gui.highlight_mask |= mask;
1797}
1798
1799 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001800gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
Bram Moolenaar30613902019-12-01 22:11:18 +01001802 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001804 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 gui.highlight_mask &= ~mask;
1806}
1807
1808/*
1809 * Clear a rectangular region of the screen from text pos (row1, col1) to
1810 * (row2, col2) inclusive.
1811 */
1812 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001813gui_clear_block(
1814 int row1,
1815 int col1,
1816 int row2,
1817 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
Bram Moolenaar30613902019-12-01 22:11:18 +01001819 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 clip_may_clear_selection(row1, row2);
1821
1822 gui_mch_clear_block(row1, col1, row2, col2);
1823
Bram Moolenaar30613902019-12-01 22:11:18 +01001824 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1826 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1827 gui.cursor_is_valid = FALSE;
1828}
1829
1830/*
1831 * Write code to update the cursor later. This avoids the need to flush the
1832 * output buffer before calling gui_update_cursor().
1833 */
1834 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001835gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001837 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838}
1839
1840 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001841gui_write(
1842 char_u *s,
1843 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845 char_u *p;
1846 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001847 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 int force_scrollbar = FALSE;
1849 static win_T *old_curwin = NULL;
1850
Bram Moolenaar30613902019-12-01 22:11:18 +01001851// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852#ifdef DEBUG_GUI_WRITE
1853 {
1854 int i;
1855 char_u *str;
1856
1857 printf("gui_write(%d):\n ", len);
1858 for (i = 0; i < len; i++)
1859 if (s[i] == ESC)
1860 {
1861 if (i != 0)
1862 printf("\n ");
1863 printf("<ESC>");
1864 }
1865 else
1866 {
1867 str = transchar_byte(s[i]);
1868 if (str[0] && str[1])
1869 printf("<%s>", (char *)str);
1870 else
1871 printf("%s", (char *)str);
1872 }
1873 printf("\n");
1874 }
1875#endif
1876 while (len)
1877 {
1878 if (s[0] == ESC && s[1] == '|')
1879 {
1880 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001881 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
1883 arg1 = getdigits(&p);
1884 if (p > s + len)
1885 break;
1886 if (*p == ';')
1887 {
1888 ++p;
1889 arg2 = getdigits(&p);
1890 if (p > s + len)
1891 break;
1892 }
1893 }
1894 switch (*p)
1895 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001896 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 clip_scroll_selection(9999);
1898 gui_mch_clear_all();
1899 gui.cursor_is_valid = FALSE;
1900 force_scrollbar = TRUE;
1901 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001902 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 gui_set_cursor(arg1, arg2);
1904 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001905 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 force_cursor = TRUE;
1907 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001908 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (arg1 < arg2)
1910 {
1911 gui.scroll_region_top = arg1;
1912 gui.scroll_region_bot = arg2;
1913 }
1914 else
1915 {
1916 gui.scroll_region_top = arg2;
1917 gui.scroll_region_bot = arg1;
1918 }
1919 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001920 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if (arg1 < arg2)
1922 {
1923 gui.scroll_region_left = arg1;
1924 gui.scroll_region_right = arg2;
1925 }
1926 else
1927 {
1928 gui.scroll_region_left = arg2;
1929 gui.scroll_region_right = arg1;
1930 }
1931 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001932 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 gui_delete_lines(gui.row, 1);
1934 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001935 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 gui_delete_lines(gui.row, arg1);
1937 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001938 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 gui_insert_lines(gui.row, 1);
1940 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001941 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 gui_insert_lines(gui.row, arg1);
1943 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001944 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 gui_clear_block(gui.row, gui.col, gui.row,
1946 (int)Columns - 1);
1947 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001948 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 gui_start_highlight(arg1);
1950 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001951 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 gui_stop_highlight(arg1);
1953 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001954 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1956 break;
1957 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001958 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 break;
1960 }
1961 len -= (int)(++p - s);
1962 s = p;
1963 }
1964 else if (
1965#ifdef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +01001966 CtrlChar(s[0]) != 0 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#else
Bram Moolenaar30613902019-12-01 22:11:18 +01001968 s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#endif
1970#ifdef FEAT_SIGN_ICONS
1971 && s[0] != SIGN_BYTE
1972# ifdef FEAT_NETBEANS_INTG
1973 && s[0] != MULTISIGN_BYTE
1974# endif
1975#endif
1976 )
1977 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001978 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 gui.col = 0;
1981 if (gui.row < gui.scroll_region_bot)
1982 gui.row++;
1983 else
1984 gui_delete_lines(gui.scroll_region_top, 1);
1985 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001986 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 gui.col = 0;
1989 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001990 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
1992 if (gui.col)
1993 --gui.col;
1994 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001995 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
1997 ++gui.col;
1998 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001999 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
2001 gui_mch_beep();
2002 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002003 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
Bram Moolenaar30613902019-12-01 22:11:18 +01002005 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 ++s;
2007 }
2008 else
2009 {
2010 p = s;
2011 while (len > 0 && (
2012#ifdef EBCDIC
2013 CtrlChar(*p) == 0
2014#else
2015 *p >= 0x20
2016#endif
2017#ifdef FEAT_SIGN_ICONS
2018 || *p == SIGN_BYTE
2019# ifdef FEAT_NETBEANS_INTG
2020 || *p == MULTISIGN_BYTE
2021# endif
2022#endif
2023 ))
2024 {
2025 len--;
2026 p++;
2027 }
2028 gui_outstr(s, (int)(p - s));
2029 s = p;
2030 }
2031 }
2032
Bram Moolenaar30613902019-12-01 22:11:18 +01002033 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2034 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (force_cursor)
2036 gui_update_cursor(TRUE, TRUE);
2037
Bram Moolenaar30613902019-12-01 22:11:18 +01002038 // When switching to another window the dragging must have stopped.
2039 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (old_curwin != curwin)
2041 gui.dragged_sb = SBAR_NONE;
2042
Bram Moolenaar30613902019-12-01 22:11:18 +01002043 // Update the scrollbars after clearing the screen or when switched
2044 // to another window.
2045 // Update the horizontal scrollbar always, it's difficult to check all
2046 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (force_scrollbar || old_curwin != curwin)
2048 gui_update_scrollbars(force_scrollbar);
2049 else
2050 gui_update_horiz_scrollbar(FALSE);
2051 old_curwin = curwin;
2052
2053 /*
2054 * We need to make sure this is cleared since Athena doesn't tell us when
2055 * he is done dragging. Do the same for GTK.
2056 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002057#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 gui.dragged_sb = SBAR_NONE;
2059#endif
2060
Bram Moolenaar30613902019-12-01 22:11:18 +01002061 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062}
2063
2064/*
2065 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2066 * produces wrong results. Call gui_dont_update_cursor() before that code and
2067 * gui_can_update_cursor() afterwards.
2068 */
2069 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002070gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071{
2072 if (gui.in_use)
2073 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002074 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02002075 if (undraw)
2076 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 can_update_cursor = FALSE;
2078 }
2079}
2080
2081 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002082gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002085 // No need to update the cursor right now, there is always more output
2086 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087}
2088
Bram Moolenaara338adc2018-01-31 20:51:47 +01002089/*
2090 * Disable issuing gui_mch_flush().
2091 */
2092 void
2093gui_disable_flush(void)
2094{
2095 ++disable_flush;
2096}
2097
2098/*
2099 * Enable issuing gui_mch_flush().
2100 */
2101 void
2102gui_enable_flush(void)
2103{
2104 --disable_flush;
2105}
2106
2107/*
2108 * Issue gui_mch_flush() if it is not disabled.
2109 */
2110 void
2111gui_may_flush(void)
2112{
2113 if (disable_flush == 0)
2114 gui_mch_flush();
2115}
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002118gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 if (len == 0)
2124 return;
2125
2126 if (len < 0)
2127 len = (int)STRLEN(s);
2128
2129 while (len > 0)
2130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (has_mbyte)
2132 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002133 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 cells = 0;
2135 for (this_len = 0; this_len < len; )
2136 {
2137 cells += (*mb_ptr2cells)(s + this_len);
2138 if (gui.col + cells > Columns)
2139 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002140 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002143 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (gui.col + len > Columns)
2147 this_len = Columns - gui.col;
2148 else
2149 this_len = len;
2150
2151 (void)gui_outstr_nowrap(s, this_len,
2152 0, (guicolor_T)0, (guicolor_T)0, 0);
2153 s += this_len;
2154 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002155 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (len > 0 && gui.col < Columns)
2157 (void)gui_outstr_nowrap((char_u *)" ", 1,
2158 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002159 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (gui.col >= Columns)
2161 {
2162 gui.col = 0;
2163 gui.row++;
2164 }
2165 }
2166}
2167
2168/*
2169 * Output one character (may be one or two display cells).
2170 * Caller must check for valid "off".
2171 * Returns FAIL or OK, just like gui_outstr_nowrap().
2172 */
2173 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002174gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002175 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002176 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002177 guicolor_T fg, // colors for cursor
2178 guicolor_T bg, // colors for cursor
2179 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 char_u buf[MB_MAXBYTES + 1];
2182
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002183 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (enc_utf8 && ScreenLines[off] == 0)
2185 return OK;
2186
2187 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002188 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2190 flags, fg, bg, back);
2191
2192 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2193 {
2194 buf[0] = ScreenLines[off];
2195 buf[1] = ScreenLines2[off];
2196 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2197 }
2198
Bram Moolenaar30613902019-12-01 22:11:18 +01002199 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002201 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203}
2204
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002205#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206/*
2207 * Output the string at the given screen position. This is used in place
2208 * of gui_screenchar() where possible because Pango needs as much context
2209 * as possible to work nicely. It's a lot faster as well.
2210 */
2211 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002212gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002213 int off, // Offset from start of screen
2214 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002215 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002216 guicolor_T fg, // colors for cursor
2217 guicolor_T bg, // colors for cursor
2218 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 char_u *buf;
2221 int outlen = 0;
2222 int i;
2223 int retval;
2224
Bram Moolenaar30613902019-12-01 22:11:18 +01002225 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 return OK;
2227
2228 if (enc_utf8)
2229 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002230 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002232 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234 for (i = off; i < off + len; ++i)
2235 {
2236 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002237 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239 if (ScreenLinesUC[i] == 0)
2240 buf[outlen++] = ScreenLines[i];
2241 else
2242 outlen += utfc_char2bytes(i, buf + outlen);
2243 }
2244
Bram Moolenaar30613902019-12-01 22:11:18 +01002245 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2247 vim_free(buf);
2248
2249 return retval;
2250 }
2251 else if (enc_dbcs == DBCS_JPNU)
2252 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002253 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002255 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257 for (i = off; i < off + len; ++i)
2258 {
2259 buf[outlen++] = ScreenLines[i];
2260
Bram Moolenaar30613902019-12-01 22:11:18 +01002261 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (ScreenLines[i] == 0x8e)
2263 buf[outlen++] = ScreenLines2[i];
2264 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2265 buf[outlen++] = ScreenLines[++i];
2266 }
2267
Bram Moolenaar30613902019-12-01 22:11:18 +01002268 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2270 vim_free(buf);
2271
2272 return retval;
2273 }
2274 else
2275 {
2276 return gui_outstr_nowrap(&ScreenLines[off], len,
2277 flags, fg, bg, back);
2278 }
2279}
Bram Moolenaar30613902019-12-01 22:11:18 +01002280#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282/*
2283 * Output the given string at the current cursor position. If the string is
2284 * too long to fit on the line, then it is truncated.
2285 * "flags":
2286 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2287 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002288 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * background.
2290 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2291 * it.
2292 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2293 * FAIL (the caller should start drawing "back" chars back).
2294 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002295 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002296gui_outstr_nowrap(
2297 char_u *s,
2298 int len,
2299 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002300 guicolor_T fg, // colors for cursor
2301 guicolor_T bg, // colors for cursor
2302 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303{
2304 long_u highlight_mask;
2305 long_u hl_mask_todo;
2306 guicolor_T fg_color;
2307 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002308 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002311 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312# ifdef FEAT_XFONTSET
2313 GuiFontset fontset = NOFONTSET;
2314# endif
2315#endif
2316 attrentry_T *aep = NULL;
2317 int draw_flags;
2318 int col = gui.col;
2319#ifdef FEAT_SIGN_ICONS
2320 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002321 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002322 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323# ifdef FEAT_NETBEANS_INTG
2324 int multi_sign = FALSE;
2325# endif
2326#endif
2327
2328 if (len < 0)
2329 len = (int)STRLEN(s);
2330 if (len == 0)
2331 return OK;
2332
2333#ifdef FEAT_SIGN_ICONS
2334 if (*s == SIGN_BYTE
2335# ifdef FEAT_NETBEANS_INTG
2336 || *s == MULTISIGN_BYTE
2337# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340# ifdef FEAT_NETBEANS_INTG
2341 if (*s == MULTISIGN_BYTE)
2342 multi_sign = TRUE;
2343# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002344 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002345 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2346 (curwin->w_p_nu || curwin->w_p_rnu))
2347 {
2348 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2349 s = extra;
2350 }
2351 else
2352 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (len == 1 && col > 0)
2354 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002355 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002356 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002357 // right align sign icon in the number column
2358 signcol = col + len - 3;
2359 else
2360 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 draw_sign = TRUE;
2362 highlight_mask = 0;
2363 }
2364 else
2365#endif
2366 if (gui.highlight_mask > HL_ALL)
2367 {
2368 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002369 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 highlight_mask = 0;
2371 else
2372 highlight_mask = aep->ae_attr;
2373 }
2374 else
2375 highlight_mask = gui.highlight_mask;
2376 hl_mask_todo = highlight_mask;
2377
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002379 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2381 font = aep->ae_u.gui.font;
2382# ifdef FEAT_XFONTSET
2383 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2384 fontset = aep->ae_u.gui.fontset;
2385# endif
2386 else
2387 {
2388# ifdef FEAT_XFONTSET
2389 if (gui.fontset != NOFONTSET)
2390 fontset = gui.fontset;
2391 else
2392# endif
2393 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2394 {
2395 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2396 {
2397 font = gui.boldital_font;
2398 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2399 }
2400 else if (gui.bold_font != NOFONT)
2401 {
2402 font = gui.bold_font;
2403 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2404 }
2405 else
2406 font = gui.norm_font;
2407 }
2408 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2409 {
2410 font = gui.ital_font;
2411 hl_mask_todo &= ~HL_ITALIC;
2412 }
2413 else
2414 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002415
Bram Moolenaardb250522013-06-17 22:43:25 +02002416 /*
2417 * Choose correct wide_font by font. wide_font should be set with font
2418 * at same time in above block. But it will make many "ifdef" nasty
2419 * blocks. So we do it here.
2420 */
2421 if (font == gui.boldital_font && gui.wide_boldital_font)
2422 wide_font = gui.wide_boldital_font;
2423 else if (font == gui.bold_font && gui.wide_bold_font)
2424 wide_font = gui.wide_bold_font;
2425 else if (font == gui.ital_font && gui.wide_ital_font)
2426 wide_font = gui.wide_ital_font;
2427 else if (font == gui.norm_font && gui.wide_font)
2428 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430# ifdef FEAT_XFONTSET
2431 if (fontset != NOFONTSET)
2432 gui_mch_set_fontset(fontset);
2433 else
2434# endif
2435 gui_mch_set_font(font);
2436#endif
2437
2438 draw_flags = 0;
2439
Bram Moolenaar30613902019-12-01 22:11:18 +01002440 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 bg_color = gui.back_pixel;
2442 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2443 {
2444 draw_flags |= DRAW_CURSOR;
2445 fg_color = fg;
2446 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002447 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449 else if (aep != NULL)
2450 {
2451 fg_color = aep->ae_u.gui.fg_color;
2452 if (fg_color == INVALCOLOR)
2453 fg_color = gui.norm_pixel;
2454 bg_color = aep->ae_u.gui.bg_color;
2455 if (bg_color == INVALCOLOR)
2456 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002457 sp_color = aep->ae_u.gui.sp_color;
2458 if (sp_color == INVALCOLOR)
2459 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002464 sp_color = fg_color;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 gui_mch_set_fg_color(bg_color);
2470 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472 else
2473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 gui_mch_set_fg_color(fg_color);
2475 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002477 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaar30613902019-12-01 22:11:18 +01002479 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (!(flags & GUI_MON_NOCLEAR))
2481 clip_may_clear_selection(gui.row, gui.row);
2482
2483
Bram Moolenaar30613902019-12-01 22:11:18 +01002484 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2486 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 /*
2489 * When drawing bold or italic characters the spill-over from the left
2490 * neighbor may be destroyed. Let the caller backup to start redrawing
2491 * just after a blank.
2492 */
2493 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2494 return FAIL;
2495
Bram Moolenaare60acc12011-05-10 16:41:25 +02002496#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002497 // If there's no italic font, then fake it.
2498 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (hl_mask_todo & HL_ITALIC)
2500 draw_flags |= DRAW_ITALIC;
2501
Bram Moolenaar30613902019-12-01 22:11:18 +01002502 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (hl_mask_todo & HL_UNDERLINE)
2504 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002507 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002508 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 draw_flags |= DRAW_UNDERL;
2510#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002511 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002512 if (hl_mask_todo & HL_UNDERCURL)
2513 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar30613902019-12-01 22:11:18 +01002515 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002516 if (hl_mask_todo & HL_STRIKETHROUGH)
2517 draw_flags |= DRAW_STRIKE;
2518
Bram Moolenaar30613902019-12-01 22:11:18 +01002519 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (flags & GUI_MON_TRS_CURSOR)
2521 draw_flags |= DRAW_TRANSP;
2522
2523 /*
2524 * Draw the text.
2525 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002526#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002527 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (enc_utf8)
2531 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002532 int start; // index of bytes to be drawn
2533 int cells; // cellwidth of bytes to be drawn
2534 int thislen; // length of bytes to be drawn
2535 int cn; // cellwidth of current char
2536 int i; // index of current char
2537 int c; // current char value
2538 int cl; // byte length of current char
2539 int comping; // current char is composing
2540 int scol = col; // screen column
2541 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002542 int prev_wide = FALSE;
2543 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002544# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002545 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002546# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002547 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar30613902019-12-01 22:11:18 +01002550 // Break the string at a composing character, it has to be drawn on
2551 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 start = 0;
2553 cells = 0;
2554 for (i = 0; i < len; i += cl)
2555 {
2556 c = utf_ptr2char(s + i);
2557 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002559 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002561 if (!comping || sep_comp)
2562 {
2563 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002564# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002565 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002566# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002567 && wide_font != NOFONT)
2568 curr_wide = TRUE;
2569 else
2570 curr_wide = FALSE;
2571 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002572 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002573 if (cl == 0) // hit end of string
2574 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar45933962013-01-23 17:43:57 +01002576 wide_changed = curr_wide != prev_wide;
2577
Bram Moolenaar30613902019-12-01 22:11:18 +01002578 // Print the string so far if it's the last character or there is
2579 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002580 if (i + cl >= len || (comping && sep_comp && i > start)
2581 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002582# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002584# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002585 // No fontset: At least draw char after wide char at
2586 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002589 )
2590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 )
2592 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002593 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 thislen = i - start;
2595 else
2596 thislen = i - start + cl;
2597 if (thislen > 0)
2598 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002599 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002600 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2602 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002603 if (prev_wide)
2604 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 start += thislen;
2606 }
2607 scol += cells;
2608 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002609 // Adjust to not draw a character which width is changed
2610 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002611 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002613 scol -= cn;
2614 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616
Bram Moolenaar13505972019-01-24 15:04:48 +01002617# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002618 // No fontset: draw a space to fill the gap after a wide char
2619 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002621# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002623# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002624 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2626 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002629 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002630 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002632# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002633 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002634 gui_mch_draw_string(gui.row, scol, s + i, cl,
2635 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002636# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002637 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2638 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 start = i + cl;
2641 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002642 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002644 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 len = scol - col;
2646 }
2647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 {
2649 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (enc_dbcs == DBCS_JPNU)
2651 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002652 // Get the length in display cells, this can be different from the
2653 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002654 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002657#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2660 gui.col = col + len;
2661
Bram Moolenaar30613902019-12-01 22:11:18 +01002662 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (flags & GUI_MON_NOCLEAR)
2664 clip_may_redraw_selection(gui.row, col, len);
2665
2666 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2667 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002668 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (gui.cursor_row == gui.row
2670 && gui.cursor_col >= col
2671 && gui.cursor_col < col + len)
2672 gui.cursor_is_valid = FALSE;
2673 }
2674
2675#ifdef FEAT_SIGN_ICONS
2676 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002677 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002678 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002679# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002680 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (multi_sign)
2682 netbeans_draw_multisign_indicator(gui.row);
2683# endif
2684#endif
2685
2686 return OK;
2687}
2688
2689/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002690 * Undraw the cursor. This actually redraws the character at the cursor
2691 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
2693 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002694gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 if (gui.cursor_is_valid)
2697 {
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002698 // Always redraw the character just before if there is one, because
2699 // with some fonts and characters there can be a one pixel overlap.
2700 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2701 int endcol = gui.cursor_col;
2702
2703#ifdef FEAT_GUI_GTK
2704 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
2705#endif
2706 gui_redraw_block(gui.cursor_row, startcol,
2707 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002708
Bram Moolenaar54612582019-11-21 17:13:31 +01002709 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2710 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 gui.cursor_is_valid = FALSE;
2712 }
2713}
2714
2715 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002716gui_redraw(
2717 int x,
2718 int y,
2719 int w,
2720 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 int row1, col1, row2, col2;
2723
2724 row1 = Y_2_ROW(y);
2725 col1 = X_2_COL(x);
2726 row2 = Y_2_ROW(y + h - 1);
2727 col2 = X_2_COL(x + w - 1);
2728
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002729 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * We may need to redraw the cursor, but don't take it upon us to change
2733 * its location after a scroll.
2734 * (maybe be more strict even and test col too?)
2735 * These things may be outside the update/clipping region and reality may
2736 * not reflect Vims internal ideas if these operations are clipped away.
2737 */
2738 if (gui.row == gui.cursor_row)
2739 gui_update_cursor(TRUE, TRUE);
2740}
2741
2742/*
2743 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2744 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002746 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002747gui_redraw_block(
2748 int row1,
2749 int col1,
2750 int row2,
2751 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002752 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753{
2754 int old_row, old_col;
2755 long_u old_hl_mask;
2756 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002757 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 int idx, len;
2759 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaar30613902019-12-01 22:11:18 +01002762 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002764 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar30613902019-12-01 22:11:18 +01002766 // Don't try to draw outside the shell!
2767 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 col1 = check_col(col1);
2769 col2 = check_col(col2);
2770 row1 = check_row(row1);
2771 row2 = check_row(row2);
2772
Bram Moolenaar30613902019-12-01 22:11:18 +01002773 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 old_row = gui.row;
2775 old_col = gui.col;
2776 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 orig_col1 = col1;
2778 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 for (gui.row = row1; gui.row <= row2; gui.row++)
2781 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002782 // When only half of a double-wide character is in the block, include
2783 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 col1 = orig_col1;
2785 col2 = orig_col2;
2786 off = LineOffset[gui.row];
2787 if (enc_dbcs != 0)
2788 {
2789 if (col1 > 0)
2790 col1 -= dbcs_screen_head_off(ScreenLines + off,
2791 ScreenLines + off + col1);
2792 col2 += dbcs_screen_tail_off(ScreenLines + off,
2793 ScreenLines + off + col2);
2794 }
2795 else if (enc_utf8)
2796 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002797 if (ScreenLines[off + col1] == 0)
2798 {
2799 if (col1 > 0)
2800 --col1;
2801 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002802 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002803 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002804 // Make this IEMSGN when it no longer breaks Travis CI.
2805 vim_snprintf((char *)IObuff, IOSIZE,
2806 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2807 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002808 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002809 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002810 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002811#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2813 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 gui.col = col1;
2817 off = LineOffset[gui.row] + gui.col;
2818 len = col2 - col1 + 1;
2819
Bram Moolenaar30613902019-12-01 22:11:18 +01002820 // Find how many chars back this highlighting starts, or where a space
2821 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 for (back = 0; back < col1; ++back)
2823 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2824 || ScreenLines[off - 1 - back] == ' ')
2825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
Bram Moolenaar30613902019-12-01 22:11:18 +01002827 // Break it up in strings of characters with the same attributes.
2828 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 while (len > 0)
2830 {
2831 first_attr = ScreenAttrs[off];
2832 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002833#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 if (enc_utf8 && ScreenLinesUC[off] != 0)
2835 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002836 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 nback = gui_screenchar(off, flags,
2838 (guicolor_T)0, (guicolor_T)0, back);
2839 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2840 idx = 2;
2841 else
2842 idx = 1;
2843 }
2844 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2845 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002846 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 nback = gui_screenchar(off, flags,
2848 (guicolor_T)0, (guicolor_T)0, back);
2849 idx = 1;
2850 }
2851 else
2852#endif
2853 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002854#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 for (idx = 0; idx < len; ++idx)
2856 {
2857 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002858 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (ScreenAttrs[off + idx] != first_attr)
2860 break;
2861 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002862 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 nback = gui_screenstr(off, idx, flags,
2864 (guicolor_T)0, (guicolor_T)0, back);
2865#else
2866 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2867 idx++)
2868 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002869 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2871 break;
2872 if (enc_dbcs == DBCS_JPNU)
2873 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002874 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (ScreenLines[off + idx] == 0x8e)
2876 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002877 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002879 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2883 (guicolor_T)0, (guicolor_T)0, back);
2884#endif
2885 }
2886 if (nback == FAIL)
2887 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002888 // Must back up to start drawing where a bold or italic word
2889 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 off -= back;
2891 len += back;
2892 gui.col -= back;
2893 }
2894 else
2895 {
2896 off += idx;
2897 len -= idx;
2898 }
2899 back = 0;
2900 }
2901 }
2902
Bram Moolenaar30613902019-12-01 22:11:18 +01002903 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 gui.row = old_row;
2905 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002906 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907}
2908
2909 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002910gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
2912 if (count <= 0)
2913 return;
2914
2915 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002916 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 gui_clear_block(row, gui.scroll_region_left,
2918 gui.scroll_region_bot, gui.scroll_region_right);
2919 else
2920 {
2921 gui_mch_delete_lines(row, count);
2922
Bram Moolenaar30613902019-12-01 22:11:18 +01002923 // If the cursor was in the deleted lines it's now gone. If the
2924 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 if (gui.cursor_row >= row
2926 && gui.cursor_col >= gui.scroll_region_left
2927 && gui.cursor_col <= gui.scroll_region_right)
2928 {
2929 if (gui.cursor_row < row + count)
2930 gui.cursor_is_valid = FALSE;
2931 else if (gui.cursor_row <= gui.scroll_region_bot)
2932 gui.cursor_row -= count;
2933 }
2934 }
2935}
2936
2937 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002938gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 if (count <= 0)
2941 return;
2942
2943 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002944 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 gui_clear_block(row, gui.scroll_region_left,
2946 gui.scroll_region_bot, gui.scroll_region_right);
2947 else
2948 {
2949 gui_mch_insert_lines(row, count);
2950
2951 if (gui.cursor_row >= gui.row
2952 && gui.cursor_col >= gui.scroll_region_left
2953 && gui.cursor_col <= gui.scroll_region_right)
2954 {
2955 if (gui.cursor_row <= gui.scroll_region_bot - count)
2956 gui.cursor_row += count;
2957 else if (gui.cursor_row <= gui.scroll_region_bot)
2958 gui.cursor_is_valid = FALSE;
2959 }
2960 }
2961}
2962
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002963#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002964/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002965 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2966 */
2967 static int
2968gui_wait_for_chars_3(
2969 long wtime,
2970 int *interrupted UNUSED,
2971 int ignore_input UNUSED)
2972{
2973 return gui_mch_wait_for_chars(wtime);
2974}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002975#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002976
2977/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002978 * Returns OK if a character was found to be available within the given time,
2979 * or FAIL otherwise.
2980 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002981 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002982gui_wait_for_chars_or_timer(
2983 long wtime,
2984 int *interrupted UNUSED,
2985 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002986{
2987#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002988 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2989 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002990#else
2991 return gui_mch_wait_for_chars(wtime);
2992#endif
2993}
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
2996 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002997 * "wtime" == -1 Wait forever.
2998 * "wtime" == 0 Don't wait.
2999 * "wtime" > 0 Wait wtime milliseconds for a character.
3000 *
3001 * Returns the number of characters read or zero when timed out or interrupted.
3002 * "buf" may be NULL, in which case a non-zero number is returned if characters
3003 * are available.
3004 */
3005 static int
3006gui_wait_for_chars_buf(
3007 char_u *buf,
3008 int maxlen,
3009 long wtime, // don't use "time", MIPS cannot handle it
3010 int tb_change_cnt)
3011{
3012 int retval;
3013
3014#ifdef FEAT_MENU
3015 // If we're going to wait a bit, update the menus and mouse shape for the
3016 // current State.
3017 if (wtime != 0)
3018 gui_update_menus(0);
3019#endif
3020
3021 gui_mch_update();
3022 if (input_available()) // Got char, return immediately
3023 {
3024 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3025 return read_from_input_buf(buf, (long)maxlen);
3026 return 0;
3027 }
3028 if (wtime == 0) // Don't wait for char
3029 return FAIL;
3030
3031 // Before waiting, flush any output to the screen.
3032 gui_mch_flush();
3033
3034 // Blink while waiting for a character.
3035 gui_mch_start_blink();
3036
3037 // Common function to loop until "wtime" is met, while handling timers and
3038 // other callbacks.
3039 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3040 gui_wait_for_chars_or_timer, NULL);
3041
3042 gui_mch_stop_blink(TRUE);
3043
3044 return retval;
3045}
3046
3047/*
3048 * Wait for a character from the keyboard without actually reading it.
3049 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 * wtime == -1 Wait forever.
3051 * wtime == 0 Don't wait.
3052 * wtime > 0 Wait wtime milliseconds for a character.
3053 * Returns OK if a character was found to be available within the given time,
3054 * or FAIL otherwise.
3055 */
3056 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003057gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003059 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060}
3061
3062/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003063 * Equivalent of mch_inchar() for the GUI.
3064 */
3065 int
3066gui_inchar(
3067 char_u *buf,
3068 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003069 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003070 int tb_change_cnt)
3071{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003072 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003073}
3074
3075/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003076 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 */
3078 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003079fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 p[0] = (char_u)(col / 128 + ' ' + 1);
3082 p[1] = (char_u)(col % 128 + ' ' + 1);
3083 p[2] = (char_u)(row / 128 + ' ' + 1);
3084 p[3] = (char_u)(row % 128 + ' ' + 1);
3085}
3086
3087/*
3088 * Generic mouse support function. Add a mouse event to the input buffer with
3089 * the given properties.
3090 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3091 * MOUSE_X1, MOUSE_X2
3092 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003093 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3094 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 * x, y --- Coordinates of mouse in pixels.
3096 * repeated_click --- TRUE if this click comes only a short time after a
3097 * previous click.
3098 * modifiers --- Bit field which may be any of the following modifiers
3099 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3100 * This function will ignore drag events where the mouse has not moved to a new
3101 * character.
3102 */
3103 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003104gui_send_mouse_event(
3105 int button,
3106 int x,
3107 int y,
3108 int repeated_click,
3109 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 static int prev_row = 0, prev_col = 0;
3112 static int prev_button = -1;
3113 static int num_clicks = 1;
3114 char_u string[10];
3115 enum key_extra button_char;
3116 int row, col;
3117#ifdef FEAT_CLIPBOARD
3118 int checkfor;
3119 int did_clip = FALSE;
3120#endif
3121
3122 /*
3123 * Scrolling may happen at any time, also while a selection is present.
3124 */
3125 switch (button)
3126 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003127 case MOUSE_MOVE:
3128 button_char = KE_MOUSEMOVE_XY;
3129 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 case MOUSE_X1:
3131 button_char = KE_X1MOUSE;
3132 goto button_set;
3133 case MOUSE_X2:
3134 button_char = KE_X2MOUSE;
3135 goto button_set;
3136 case MOUSE_4:
3137 button_char = KE_MOUSEDOWN;
3138 goto button_set;
3139 case MOUSE_5:
3140 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003141 goto button_set;
3142 case MOUSE_6:
3143 button_char = KE_MOUSELEFT;
3144 goto button_set;
3145 case MOUSE_7:
3146 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147button_set:
3148 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003149 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (hold_gui_events)
3151 return;
3152
3153 string[3] = CSI;
3154 string[4] = KS_EXTRA;
3155 string[5] = (int)button_char;
3156
Bram Moolenaar30613902019-12-01 22:11:18 +01003157 // Pass the pointer coordinates of the scroll event so that we
3158 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 row = gui_xy2colrow(x, y, &col);
3160 string[6] = (char_u)(col / 128 + ' ' + 1);
3161 string[7] = (char_u)(col % 128 + ' ' + 1);
3162 string[8] = (char_u)(row / 128 + ' ' + 1);
3163 string[9] = (char_u)(row % 128 + ' ' + 1);
3164
3165 if (modifiers == 0)
3166 add_to_input_buf(string + 3, 7);
3167 else
3168 {
3169 string[0] = CSI;
3170 string[1] = KS_MODIFIER;
3171 string[2] = 0;
3172 if (modifiers & MOUSE_SHIFT)
3173 string[2] |= MOD_MASK_SHIFT;
3174 if (modifiers & MOUSE_CTRL)
3175 string[2] |= MOD_MASK_CTRL;
3176 if (modifiers & MOUSE_ALT)
3177 string[2] |= MOD_MASK_ALT;
3178 add_to_input_buf(string, 10);
3179 }
3180 return;
3181 }
3182 }
3183
3184#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003185 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (clip_star.state == SELECT_IN_PROGRESS)
3187 {
3188 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003189
3190 // A release event may still need to be sent if the position is equal.
3191 row = gui_xy2colrow(x, y, &col);
3192 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3193 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195
Bram Moolenaar30613902019-12-01 22:11:18 +01003196 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 switch (get_real_state())
3198 {
3199 case NORMAL_BUSY:
3200 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003201# ifdef FEAT_TERMINAL
3202 case TERMINAL:
3203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 case NORMAL: checkfor = MOUSE_NORMAL; break;
3205 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003206 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 case REPLACE:
3208 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 case VREPLACE:
3210 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 case INSERT:
3212 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3213 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003214 case HITRETURN: // At the more- and hit-enter prompt pass the
3215 // mouse event for a click on or below the
3216 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (Y_2_ROW(y) >= msg_row)
3218 checkfor = MOUSE_NORMAL;
3219 else
3220 checkfor = MOUSE_RETURN;
3221 break;
3222
3223 /*
3224 * On the command line, use the clipboard selection on all lines
3225 * but the command line. But not when pasting.
3226 */
3227 case CMDLINE:
3228 case CMDLINE+LANGMAP:
3229 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3230 checkfor = MOUSE_NONE;
3231 else
3232 checkfor = MOUSE_COMMAND;
3233 break;
3234
3235 default:
3236 checkfor = MOUSE_NONE;
3237 break;
3238 };
3239
3240 /*
3241 * Allow clipboard selection of text on the command line in "normal"
3242 * modes. Don't do this when dragging the status line, or extending a
3243 * Visual selection.
3244 */
3245 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003246 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 && button != MOUSE_DRAG
3248# ifdef FEAT_MOUSESHAPE
3249 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251# endif
3252 )
3253 checkfor = MOUSE_NONE;
3254
3255 /*
3256 * Use modeless selection when holding CTRL and SHIFT pressed.
3257 */
3258 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3259 checkfor = MOUSE_NONEF;
3260
3261 /*
3262 * In Ex mode, always use modeless selection.
3263 */
3264 if (exmode_active)
3265 checkfor = MOUSE_NONE;
3266
3267 /*
3268 * If the mouse settings say to not use the mouse, use the modeless
3269 * selection. But if Visual is active, assume that only the Visual area
3270 * will be selected.
3271 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003272 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
3274 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3275 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003276 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3278 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
3280 /*
3281 * When 'mousemodel' is "popup", shift-left is translated to right.
3282 * But not when also using Ctrl.
3283 */
3284 if (mouse_model_popup() && button == MOUSE_LEFT
3285 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3286 {
3287 button = MOUSE_RIGHT;
3288 modifiers &= ~ MOUSE_SHIFT;
3289 }
3290
Bram Moolenaar30613902019-12-01 22:11:18 +01003291 // If the selection is done, allow the right button to extend it.
3292 // If the selection is cleared, allow the right button to start it
3293 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 if (button == MOUSE_RIGHT)
3295 {
3296 if (clip_star.state == SELECT_CLEARED)
3297 {
3298 if (State & CMDLINE)
3299 {
3300 col = msg_col;
3301 row = msg_row;
3302 }
3303 else
3304 {
3305 col = curwin->w_wcol;
3306 row = curwin->w_wrow + W_WINROW(curwin);
3307 }
3308 clip_start_selection(col, row, FALSE);
3309 }
3310 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3311 repeated_click);
3312 did_clip = TRUE;
3313 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003314 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003315 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3318 did_clip = TRUE;
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar30613902019-12-01 22:11:18 +01003321 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (button != MOUSE_MIDDLE)
3323 {
3324 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3325 return;
3326 if (checkfor != MOUSE_COMMAND)
3327 button = MOUSE_LEFT;
3328 }
3329 repeated_click = FALSE;
3330 }
3331
3332 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003333 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
3335
Bram Moolenaar30613902019-12-01 22:11:18 +01003336 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (hold_gui_events)
3338 return;
3339
3340 row = gui_xy2colrow(x, y, &col);
3341
3342 /*
3343 * If we are dragging and the mouse hasn't moved far enough to be on a
3344 * different character, then don't send an event to vim.
3345 */
3346 if (button == MOUSE_DRAG)
3347 {
3348 if (row == prev_row && col == prev_col)
3349 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003350 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 if (y < 0)
3352 row = -1;
3353 }
3354
3355 /*
3356 * If topline has changed (window scrolled) since the last click, reset
3357 * repeated_click, because we don't want starting Visual mode when
3358 * clicking on a different character in the text.
3359 */
3360 if (curwin->w_topline != gui_prev_topline
3361#ifdef FEAT_DIFF
3362 || curwin->w_topfill != gui_prev_topfill
3363#endif
3364 )
3365 repeated_click = FALSE;
3366
Bram Moolenaar30613902019-12-01 22:11:18 +01003367 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 string[1] = KS_MOUSE;
3369 string[2] = KE_FILLER;
3370 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3371 {
3372 if (repeated_click)
3373 {
3374 /*
3375 * Handle multiple clicks. They only count if the mouse is still
3376 * pointing at the same character.
3377 */
3378 if (button != prev_button || row != prev_row || col != prev_col)
3379 num_clicks = 1;
3380 else if (++num_clicks > 4)
3381 num_clicks = 1;
3382 }
3383 else
3384 num_clicks = 1;
3385 prev_button = button;
3386 gui_prev_topline = curwin->w_topline;
3387#ifdef FEAT_DIFF
3388 gui_prev_topfill = curwin->w_topfill;
3389#endif
3390
3391 string[3] = (char_u)(button | 0x20);
3392 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3393 }
3394 else
3395 string[3] = (char_u)button;
3396
3397 string[3] |= modifiers;
3398 fill_mouse_coord(string + 4, col, row);
3399 add_to_input_buf(string, 8);
3400
3401 if (row < 0)
3402 prev_row = 0;
3403 else
3404 prev_row = row;
3405 prev_col = col;
3406
3407 /*
3408 * We need to make sure this is cleared since Athena doesn't tell us when
3409 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3410 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003411#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 gui.dragged_sb = SBAR_NONE;
3413#endif
3414}
3415
3416/*
3417 * Convert x and y coordinate to column and row in text window.
3418 * Corrects for multi-byte character.
3419 * returns column in "*colp" and row as return value;
3420 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003421 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003422gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 int col = check_col(X_2_COL(x));
3425 int row = check_row(Y_2_ROW(y));
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 return row;
3429}
3430
3431#if defined(FEAT_MENU) || defined(PROTO)
3432/*
3433 * Callback function for when a menu entry has been selected.
3434 */
3435 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003436gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003438 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar30613902019-12-01 22:11:18 +01003440 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (hold_gui_events)
3442 return;
3443
3444 bytes[0] = CSI;
3445 bytes[1] = KS_MENU;
3446 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003447 add_to_input_buf(bytes, 3);
3448 add_long_to_buf((long_u)menu, bytes);
3449 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450}
3451#endif
3452
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003453static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455/*
3456 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003457 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 * in p_go.
3459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003461gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003463#ifdef FEAT_GUI_DARKTHEME
3464 static int prev_dark_theme = -1;
3465 int using_dark_theme = FALSE;
3466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#ifdef FEAT_MENU
3468 static int prev_menu_is_active = -1;
3469#endif
3470#ifdef FEAT_TOOLBAR
3471 static int prev_toolbar = -1;
3472 int using_toolbar = FALSE;
3473#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003474#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003475 int using_tabline;
3476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477#ifdef FEAT_FOOTER
3478 static int prev_footer = -1;
3479 int using_footer = FALSE;
3480#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 static int prev_tearoff = -1;
3483 int using_tearoff = FALSE;
3484#endif
3485
3486 char_u *p;
3487 int i;
3488#ifdef FEAT_MENU
3489 int grey_old, grey_new;
3490 char_u *temp;
3491#endif
3492 win_T *wp;
3493 int need_set_size;
3494 int fix_size;
3495
3496#ifdef FEAT_MENU
3497 if (oldval != NULL && gui.in_use)
3498 {
3499 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003500 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 */
3502 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3503 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3504 if (grey_old != grey_new)
3505 {
3506 temp = p_go;
3507 p_go = oldval;
3508 gui_update_menus(MENU_ALL_MODES);
3509 p_go = temp;
3510 }
3511 }
3512 gui.menu_is_active = FALSE;
3513#endif
3514
3515 for (i = 0; i < 3; i++)
3516 gui.which_scrollbars[i] = FALSE;
3517 for (p = p_go; *p; p++)
3518 switch (*p)
3519 {
3520 case GO_LEFT:
3521 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3522 break;
3523 case GO_RIGHT:
3524 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 case GO_VLEFT:
3527 if (win_hasvertsplit())
3528 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3529 break;
3530 case GO_VRIGHT:
3531 if (win_hasvertsplit())
3532 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3533 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 case GO_BOT:
3535 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3536 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003537#ifdef FEAT_GUI_DARKTHEME
3538 case GO_DARKTHEME:
3539 using_dark_theme = TRUE;
3540 break;
3541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#ifdef FEAT_MENU
3543 case GO_MENUS:
3544 gui.menu_is_active = TRUE;
3545 break;
3546#endif
3547 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003548 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550#ifdef FEAT_TOOLBAR
3551 case GO_TOOLBAR:
3552 using_toolbar = TRUE;
3553 break;
3554#endif
3555#ifdef FEAT_FOOTER
3556 case GO_FOOTER:
3557 using_footer = TRUE;
3558 break;
3559#endif
3560 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 using_tearoff = TRUE;
3563#endif
3564 break;
3565 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003566 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 break;
3568 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (gui.in_use)
3571 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003572 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003574
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003575#ifdef FEAT_GUI_DARKTHEME
3576 if (using_dark_theme != prev_dark_theme)
3577 {
3578 gui_mch_set_dark_theme(using_dark_theme);
3579 prev_dark_theme = using_dark_theme;
3580 }
3581#endif
3582
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003584 // Update the GUI tab line, it may appear or disappear. This may
3585 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003587 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003588 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003589 // We don't want a resize event change "Rows" here, save and
3590 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003591 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003592 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003593 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003594 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595 if (using_tabline)
3596 fix_size = TRUE;
3597 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003598 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003599 }
3600#endif
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 for (i = 0; i < 3; i++)
3603 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003604 // The scrollbar needs to be updated when it is shown/unshown and
3605 // when switching tab pages. But the size only changes when it's
3606 // shown/unshown. Thus we need two places to remember whether a
3607 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003608 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003609 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003610 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 if (i == SBAR_BOTTOM)
3613 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3614 gui.which_scrollbars[i]);
3615 else
3616 {
3617 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003620 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3621 {
3622 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003623 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003624 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003625 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003626 if (gui.which_scrollbars[i])
3627 fix_size = TRUE;
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003630 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003631 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633
3634#ifdef FEAT_MENU
3635 if (gui.menu_is_active != prev_menu_is_active)
3636 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003637 // We don't want a resize event change "Rows" here, save and
3638 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 i = Rows;
3640 gui_mch_enable_menu(gui.menu_is_active);
3641 Rows = i;
3642 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003643 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (gui.menu_is_active)
3645 fix_size = TRUE;
3646 }
3647#endif
3648
3649#ifdef FEAT_TOOLBAR
3650 if (using_toolbar != prev_toolbar)
3651 {
3652 gui_mch_show_toolbar(using_toolbar);
3653 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003654 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (using_toolbar)
3656 fix_size = TRUE;
3657 }
3658#endif
3659#ifdef FEAT_FOOTER
3660 if (using_footer != prev_footer)
3661 {
3662 gui_mch_enable_footer(using_footer);
3663 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003664 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (using_footer)
3666 fix_size = TRUE;
3667 }
3668#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003669#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (using_tearoff != prev_tearoff)
3671 {
3672 gui_mch_toggle_tearoffs(using_tearoff);
3673 prev_tearoff = using_tearoff;
3674 }
3675#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003676 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003677 {
3678#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003679 long prev_Columns = Columns;
3680 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003681#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003682 // Adjust the size of the window to make the text area keep the
3683 // same size and to avoid that part of our window is off-screen
3684 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003685 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003686
3687#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003688 // GTK has the annoying habit of sending us resize events when
3689 // changing the window size ourselves. This mostly happens when
3690 // waiting for a character to arrive, quite unpredictably, and may
3691 // change Columns and Rows when we don't want it. Wait for a
3692 // character here to avoid this effect.
3693 // If you remove this, please test this command for resizing
3694 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3695 // Don't do this while starting up though.
3696 // Don't change Rows when adding menu/toolbar/tabline.
3697 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003698 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003699 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003700 if ((need_set_size & RESIZE_VERT) == 0)
3701 Rows = prev_Rows;
3702 if ((need_set_size & RESIZE_HOR) == 0)
3703 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003704#endif
3705 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003706 // When the console tabline appears or disappears the window positions
3707 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003708 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003709 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711}
3712
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003713#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3714/*
3715 * Return TRUE if the GUI is taking care of the tabline.
3716 * It may still be hidden if 'showtabline' is zero.
3717 */
3718 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003719gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003720{
3721 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3722}
3723
3724/*
3725 * Return TRUE if the GUI is showing the tabline.
3726 * This uses 'showtabline'.
3727 */
3728 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003729gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003730{
3731 if (!gui_use_tabline()
3732 || p_stal == 0
3733 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3734 return FALSE;
3735 return TRUE;
3736}
3737
3738/*
3739 * Update the tabline.
3740 * This may display/undisplay the tabline and update the labels.
3741 */
3742 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003743gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003744{
3745 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003746 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003747
3748 if (!gui.starting && starting == 0)
3749 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003750 // Updating the tabline uses direct GUI commands, flush
3751 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003752 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003753
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003754 if (!showit != !shown)
3755 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003756 if (showit != 0)
3757 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003758
Bram Moolenaar30613902019-12-01 22:11:18 +01003759 // When the tabs change from hidden to shown or from shown to
3760 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003761 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003762 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003763 }
3764}
3765
3766/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003767 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 */
3769 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003770get_tabline_label(
3771 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003772 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003773{
3774 int modified = FALSE;
3775 char_u buf[40];
3776 int wincount;
3777 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003778 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003779
Bram Moolenaar30613902019-12-01 22:11:18 +01003780 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003781 opt = (tooltip ? &p_gtt : &p_gtl);
3782 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003783 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003784 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003785 int called_emsg_before = called_emsg;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003786 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003787 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003788 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3789 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003790
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003791 printer_page_num = tabpage_index(tp);
3792# ifdef FEAT_EVAL
3793 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003794 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003795# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003796 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003797 curtab->tp_firstwin = firstwin;
3798 curtab->tp_lastwin = lastwin;
3799 curtab->tp_curwin = curwin;
3800 save_curtab = curtab;
3801 curtab = tp;
3802 topframe = curtab->tp_topframe;
3803 firstwin = curtab->tp_firstwin;
3804 lastwin = curtab->tp_lastwin;
3805 curwin = curtab->tp_curwin;
3806 curbuf = curwin->w_buffer;
3807
Bram Moolenaar30613902019-12-01 22:11:18 +01003808 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003809 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003810 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003811 STRCPY(NameBuff, res);
3812
Bram Moolenaar30613902019-12-01 22:11:18 +01003813 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003814 curtab = save_curtab;
3815 topframe = curtab->tp_topframe;
3816 firstwin = curtab->tp_firstwin;
3817 lastwin = curtab->tp_lastwin;
3818 curwin = curtab->tp_curwin;
3819 curbuf = curwin->w_buffer;
3820
Bram Moolenaar53989552019-12-23 22:59:18 +01003821 if (called_emsg > called_emsg_before)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003822 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003823 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003824 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003825
Bram Moolenaar30613902019-12-01 22:11:18 +01003826 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3827 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003828 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003829 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003830 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003831 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003832 if (!tooltip)
3833 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003834
3835 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3836 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3837 if (bufIsChanged(wp->w_buffer))
3838 modified = TRUE;
3839 if (modified || wincount > 1)
3840 {
3841 if (wincount > 1)
3842 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3843 else
3844 buf[0] = NUL;
3845 if (modified)
3846 STRCAT(buf, "+");
3847 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003848 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003849 mch_memmove(NameBuff, buf, STRLEN(buf));
3850 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003851 }
3852}
3853
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003854/*
3855 * Send the event for clicking to select tab page "nr".
3856 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003857 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003858 */
3859 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003860send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003861{
3862 char_u string[3];
3863
3864 if (nr == tabpage_index(curtab))
3865 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003866
Bram Moolenaar30613902019-12-01 22:11:18 +01003867 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003868 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003869# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003870 || cmdwin_type != 0
3871# endif
3872 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003873 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003874 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003875 gui_mch_set_curtab(tabpage_index(curtab));
3876 return FALSE;
3877 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003878
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003879 string[0] = CSI;
3880 string[1] = KS_TABLINE;
3881 string[2] = KE_FILLER;
3882 add_to_input_buf(string, 3);
3883 string[0] = nr;
3884 add_to_input_buf_csi(string, 1);
3885 return TRUE;
3886}
3887
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003888/*
3889 * Send a tabline menu event
3890 */
3891 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003892send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003893{
3894 char_u string[3];
3895
Bram Moolenaar29547192018-12-11 20:39:19 +01003896 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003897 if (hold_gui_events)
3898 return;
3899
Bram Moolenaar29547192018-12-11 20:39:19 +01003900 // Cannot close the last tabpage.
3901 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3902 return;
3903
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003904 string[0] = CSI;
3905 string[1] = KS_TABMENU;
3906 string[2] = KE_FILLER;
3907 add_to_input_buf(string, 3);
3908 string[0] = tabidx;
3909 string[1] = (char_u)(long)event;
3910 add_to_input_buf_csi(string, 2);
3911}
3912
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915/*
3916 * Scrollbar stuff:
3917 */
3918
Bram Moolenaare45828b2006-02-15 22:12:56 +00003919/*
3920 * Remove all scrollbars. Used before switching to another tab page.
3921 */
3922 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003923gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003924{
3925 int i;
3926 win_T *wp;
3927
3928 for (i = 0; i < 3; i++)
3929 {
3930 if (i == SBAR_BOTTOM)
3931 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3932 else
3933 {
3934 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003935 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003936 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003937 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003938 }
3939}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003942gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 static int sbar_ident = 0;
3945
Bram Moolenaar30613902019-12-01 22:11:18 +01003946 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 sb->wp = wp;
3948 sb->type = type;
3949 sb->value = 0;
3950#ifdef FEAT_GUI_ATHENA
3951 sb->pixval = 0;
3952#endif
3953 sb->size = 1;
3954 sb->max = 1;
3955 sb->top = 0;
3956 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 sb->status_height = 0;
3959 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3960}
3961
3962/*
3963 * Find the scrollbar with the given index.
3964 */
3965 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003966gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
3968 win_T *wp;
3969
3970 if (gui.bottom_sbar.ident == ident)
3971 return &gui.bottom_sbar;
3972 FOR_ALL_WINDOWS(wp)
3973 {
3974 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3975 return &wp->w_scrollbars[SBAR_LEFT];
3976 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3977 return &wp->w_scrollbars[SBAR_RIGHT];
3978 }
3979 return NULL;
3980}
3981
3982/*
3983 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3984 *
3985 * For Win32, Macintosh and GTK+ 2:
3986 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3987 * you stop dragging the scrollbar. We get here each time the scrollbar is
3988 * dragged another pixel, but as far as the rest of vim goes, it thinks
3989 * we're just hanging in the call to DispatchMessage() in
3990 * process_message(). The DispatchMessage() call that hangs was passed a
3991 * mouse button click event in the scrollbar window. -- webb.
3992 *
3993 * Solution: Do the scrolling right here. But only when allowed.
3994 * Ignore the scrollbars while executing an external command or when there
3995 * are still characters to be processed.
3996 */
3997 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003998gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 int sb_num;
4002#ifdef USE_ON_FLY_SCROLL
4003 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005# ifdef FEAT_DIFF
4006 int old_topfill = curwin->w_topfill;
4007# endif
4008#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004009 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int byte_count;
4011#endif
4012
4013 if (sb == NULL)
4014 return;
4015
Bram Moolenaar30613902019-12-01 22:11:18 +01004016 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (hold_gui_events)
4018 return;
4019
4020#ifdef FEAT_CMDWIN
4021 if (cmdwin_type != 0 && sb->wp != curwin)
4022 return;
4023#endif
4024
4025 if (still_dragging)
4026 {
4027 if (sb->wp == NULL)
4028 gui.dragged_sb = SBAR_BOTTOM;
4029 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4030 gui.dragged_sb = SBAR_LEFT;
4031 else
4032 gui.dragged_sb = SBAR_RIGHT;
4033 gui.dragged_wp = sb->wp;
4034 }
4035 else
4036 {
4037 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004038#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004039 // Keep the "dragged_wp" value until after the scrolling, for when the
4040 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 gui.dragged_wp = NULL;
4042#endif
4043 }
4044
Bram Moolenaar30613902019-12-01 22:11:18 +01004045 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 if (sb->wp != NULL)
4047 sb = &sb->wp->w_scrollbars[0];
4048
4049 /*
4050 * Check validity of value
4051 */
4052 if (value < 0)
4053 value = 0;
4054#ifdef SCROLL_PAST_END
4055 else if (value > sb->max)
4056 value = sb->max;
4057#else
4058 if (value > sb->max - sb->size + 1)
4059 value = sb->max - sb->size + 1;
4060#endif
4061
4062 sb->value = value;
4063
4064#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004065 // When not allowed to do the scrolling right now, return.
4066 // This also checked input_available(), but that causes the first click in
4067 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004068 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 return;
4070#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004071 // Disallow scrolling the current window when the completion popup menu is
4072 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004073 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4074 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076#ifdef FEAT_RIGHTLEFT
4077 if (sb->wp == NULL && curwin->w_p_rl)
4078 {
4079 value = sb->max + 1 - sb->size - value;
4080 if (value < 0)
4081 value = 0;
4082 }
4083#endif
4084
Bram Moolenaar30613902019-12-01 22:11:18 +01004085 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
4087 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4089 sb_num++;
4090 if (wp == NULL)
4091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
4093#ifdef USE_ON_FLY_SCROLL
4094 current_scrollbar = sb_num;
4095 scrollbar_value = value;
4096 if (State & NORMAL)
4097 {
4098 gui_do_scroll();
4099 setcursor();
4100 }
4101 else if (State & INSERT)
4102 {
4103 ins_scroll();
4104 setcursor();
4105 }
4106 else if (State & CMDLINE)
4107 {
4108 if (msg_scrolled == 0)
4109 {
4110 gui_do_scroll();
4111 redrawcmdline();
4112 }
4113 }
4114# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004115 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 sb->value = sb->wp->w_topline - 1;
4117# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004118
Bram Moolenaar30613902019-12-01 22:11:18 +01004119 // When dragging one scrollbar and there is another one at the other
4120 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004121 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4122 gui_mch_set_scrollbar_thumb(
4123 &sb->wp->w_scrollbars[
4124 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4125 ? SBAR_LEFT : SBAR_RIGHT],
4126 sb->value, sb->size, sb->max);
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#else
4129 bytes[0] = CSI;
4130 bytes[1] = KS_VER_SCROLLBAR;
4131 bytes[2] = KE_FILLER;
4132 bytes[3] = (char_u)sb_num;
4133 byte_count = 4;
4134#endif
4135 }
4136 else
4137 {
4138#ifdef USE_ON_FLY_SCROLL
4139 scrollbar_value = value;
4140
4141 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004142 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 else if (State & INSERT)
4144 ins_horscroll();
4145 else if (State & CMDLINE)
4146 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004147 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004149 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 redrawcmdline();
4151 }
4152 }
4153 if (old_leftcol != curwin->w_leftcol)
4154 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004155 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 setcursor();
4157 }
4158#else
4159 bytes[0] = CSI;
4160 bytes[1] = KS_HOR_SCROLLBAR;
4161 bytes[2] = KE_FILLER;
4162 byte_count = 3;
4163#endif
4164 }
4165
4166#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /*
4168 * synchronize other windows, as necessary according to 'scrollbind'
4169 */
4170 if (curwin->w_p_scb
4171 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4172 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004173# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 ))))
4177 {
4178 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004179 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004180 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (wp->w_redr_type > 0)
4182 updateWindow(wp);
4183 setcursor();
4184 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004185 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004187 add_to_input_buf(bytes, byte_count);
4188 add_long_to_buf((long_u)value, bytes);
4189 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#endif
4191}
4192
4193/*
4194 * Scrollbar stuff:
4195 */
4196
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004197/*
4198 * Called when something in the window layout has changed.
4199 */
4200 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004201gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004202{
4203 if (gui.in_use && starting == 0)
4204 {
4205 out_flush();
4206 gui_init_which_components(NULL);
4207 gui_update_scrollbars(TRUE);
4208 }
4209 need_mouse_correct = TRUE;
4210}
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004213gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004214 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 win_T *wp;
4217 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004218 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 int which_sb;
4220 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar30613902019-12-01 22:11:18 +01004223 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 gui_update_horiz_scrollbar(force);
4225
Bram Moolenaar4f974752019-02-17 17:44:42 +01004226#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004227 // Return straight away if there is neither a left nor right scrollbar.
4228 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4230 return;
4231#endif
4232
4233 /*
4234 * Don't want to update a scrollbar while we're dragging it. But if we
4235 * have both a left and right scrollbar, and we drag one of them, we still
4236 * need to update the other one.
4237 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004238 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4239 && gui.which_scrollbars[SBAR_LEFT]
4240 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
4242 /*
4243 * If we have two scrollbars and one of them is being dragged, just
4244 * copy the scrollbar position from the dragged one to the other one.
4245 */
4246 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4247 if (gui.dragged_wp != NULL)
4248 gui_mch_set_scrollbar_thumb(
4249 &gui.dragged_wp->w_scrollbars[which_sb],
4250 gui.dragged_wp->w_scrollbars[0].value,
4251 gui.dragged_wp->w_scrollbars[0].size,
4252 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254
Bram Moolenaar30613902019-12-01 22:11:18 +01004255 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 ++hold_gui_events;
4257
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004258 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004260 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004262 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004263 if (!force && (gui.dragged_sb == SBAR_LEFT
4264 || gui.dragged_sb == SBAR_RIGHT)
4265 && gui.dragged_wp == wp)
4266 continue;
4267
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#ifdef SCROLL_PAST_END
4269 max = wp->w_buffer->b_ml.ml_line_count - 1;
4270#else
4271 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4272#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004273 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 max = 0;
4275 val = wp->w_topline - 1;
4276 size = wp->w_height;
4277#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004278 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 val = max;
4280#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004281 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 size = max + 1;
4283 if (val > max - size + 1)
4284 val = max - size + 1;
4285#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004286 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 val = 0;
4288
4289 /*
4290 * Scrollbar at index 0 (the left one) contains all the information.
4291 * It would be the same info for left and right so we just store it for
4292 * one of them.
4293 */
4294 sb = &wp->w_scrollbars[0];
4295
4296 /*
4297 * Note: no check for valid w_botline. If it's not valid the
4298 * scrollbars will be updated later anyway.
4299 */
4300 if (size < 1 || wp->w_botline - 2 > max)
4301 {
4302 /*
4303 * This can happen during changing files. Just don't update the
4304 * scrollbar for now.
4305 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004306 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 if (gui.which_scrollbars[SBAR_LEFT])
4308 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4309 if (gui.which_scrollbars[SBAR_RIGHT])
4310 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4311 continue;
4312 }
4313 if (force || 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 Moolenaar4033c552017-09-16 20:54:51 +02004317 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004319 // Height, width or position of scrollbar has changed. For
4320 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 sb->top = wp->w_winrow;
4323 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar30613902019-12-01 22:11:18 +01004326 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 h = (sb->height + sb->status_height) * gui.char_height;
4328 y = sb->top * gui.char_height + gui.border_offset;
4329#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4330 if (gui.menu_is_active)
4331 y += gui.menu_height;
4332#endif
4333
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004334#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA) \
4335 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#endif
4339
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004340#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004341 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004342 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004343#endif
4344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004347 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 h += gui.border_offset;
4349 y -= gui.border_offset;
4350 }
4351 if (gui.which_scrollbars[SBAR_LEFT])
4352 {
4353 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4354 gui.left_sbar_x, y,
4355 gui.scrollbar_width, h);
4356 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4357 }
4358 if (gui.which_scrollbars[SBAR_RIGHT])
4359 {
4360 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4361 gui.right_sbar_x, y,
4362 gui.scrollbar_width, h);
4363 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4364 }
4365 }
4366
Bram Moolenaar30613902019-12-01 22:11:18 +01004367 // Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4368 // checking if the thumb moved at least a pixel. Only do this for
4369 // Athena, most other GUIs require the update anyway to make the
4370 // arrows work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#ifdef FEAT_GUI_ATHENA
4372 if (max == 0)
4373 y = 0;
4374 else
4375 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4376 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4377#else
4378 if (force || sb->value != val || sb->size != size || sb->max != max)
4379#endif
4380 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004381 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 sb->value = val;
4383#ifdef FEAT_GUI_ATHENA
4384 sb->pixval = y;
4385#endif
4386 sb->size = size;
4387 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004388 if (gui.which_scrollbars[SBAR_LEFT]
4389 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4391 val, size, max);
4392 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004393 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4395 val, size, max);
4396 }
4397 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004398
4399 // update the title, it may show the scroll position
4400 maketitle();
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 --hold_gui_events;
4404}
4405
4406/*
4407 * Enable or disable a scrollbar.
4408 * Check for scrollbars for vertically split windows which are not enabled
4409 * sometimes.
4410 */
4411 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004412gui_do_scrollbar(
4413 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004414 int which, // SBAR_LEFT or SBAR_RIGHT
4415 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 int midcol = curwin->w_wincol + curwin->w_width / 2;
4418 int has_midcol = (wp->w_wincol <= midcol
4419 && wp->w_wincol + wp->w_width >= midcol);
4420
Bram Moolenaar30613902019-12-01 22:11:18 +01004421 // Only enable scrollbars that contain the middle column of the current
4422 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4424 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004425 // Scrollbars only on one side. Don't enable scrollbars that don't
4426 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 if (!has_midcol)
4428 enable = FALSE;
4429 }
4430 else
4431 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004432 // Scrollbars on both sides. Don't enable scrollbars that neither
4433 // contain the middle column of the current window nor are on the far
4434 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (midcol > Columns / 2)
4436 {
4437 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4438 enable = FALSE;
4439 }
4440 else
4441 {
4442 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4443 : !has_midcol)
4444 enable = FALSE;
4445 }
4446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4448}
4449
4450/*
4451 * Scroll a window according to the values set in the globals current_scrollbar
4452 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4453 * or FALSE otherwise.
4454 */
4455 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004456gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458 win_T *wp, *save_wp;
4459 int i;
4460 long nlines;
4461 pos_T old_cursor;
4462 linenr_T old_topline;
4463#ifdef FEAT_DIFF
4464 int old_topfill;
4465#endif
4466
4467 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4468 if (wp == NULL)
4469 break;
4470 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004471 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FALSE;
4473
4474 /*
4475 * Compute number of lines to scroll. If zero, nothing to do.
4476 */
4477 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4478 if (nlines == 0)
4479 return FALSE;
4480
4481 save_wp = curwin;
4482 old_topline = wp->w_topline;
4483#ifdef FEAT_DIFF
4484 old_topfill = wp->w_topfill;
4485#endif
4486 old_cursor = wp->w_cursor;
4487 curwin = wp;
4488 curbuf = wp->w_buffer;
4489 if (nlines < 0)
4490 scrolldown(-nlines, gui.dragged_wp == NULL);
4491 else
4492 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004493 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4494 // the mouse-up event already, but we still want it to behave like when
4495 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (gui.dragged_sb == SBAR_NONE)
4497 gui.dragged_wp = NULL;
4498
4499 if (old_topline != wp->w_topline
4500#ifdef FEAT_DIFF
4501 || old_topfill != wp->w_topfill
4502#endif
4503 )
4504 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004505 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004507 cursor_correct(); // fix window for 'so'
4508 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 if (old_cursor.lnum != wp->w_cursor.lnum)
4511 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514
Bram Moolenaar30613902019-12-01 22:11:18 +01004515 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004516 validate_cursor();
4517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 curwin = save_wp;
4519 curbuf = save_wp->w_buffer;
4520
4521 /*
4522 * Don't call updateWindow() when nothing has changed (it will overwrite
4523 * the status line!).
4524 */
4525 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004526 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#ifdef FEAT_DIFF
4528 || old_topfill != wp->w_topfill
4529#endif
4530 )
4531 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004532 int type = VALID;
4533
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004534 if (pum_visible())
4535 {
4536 type = NOT_VALID;
4537 wp->w_lines_valid = 0;
4538 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004539
Bram Moolenaar30613902019-12-01 22:11:18 +01004540 // Don't set must_redraw here, it may cause the popup menu to
4541 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004542 if (wp->w_redr_type < type)
4543 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004544 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004545 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004546 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548
Bram Moolenaar30613902019-12-01 22:11:18 +01004549 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004550 if (pum_visible())
4551 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004552
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004553 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554}
4555
4556
4557/*
4558 * Horizontal scrollbar stuff:
4559 */
4560
4561/*
4562 * Return length of line "lnum" for horizontal scrolling.
4563 */
4564 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004565scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 char_u *p;
4568 colnr_T col;
4569 int w;
4570
4571 p = ml_get(lnum);
4572 col = 0;
4573 if (*p != NUL)
4574 for (;;)
4575 {
4576 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004577 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004578 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 break;
4580 col += w;
4581 }
4582 return col;
4583}
4584
Bram Moolenaar30613902019-12-01 22:11:18 +01004585// Remember which line is currently the longest, so that we don't have to
4586// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587static linenr_T longest_lnum = 0;
4588
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589/*
4590 * Find longest visible line number. If this is not possible (or not desired,
4591 * by setting 'h' in "guioptions") then the current line number is returned.
4592 */
4593 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004594gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004595{
4596 linenr_T ret = 0;
4597
Bram Moolenaar30613902019-12-01 22:11:18 +01004598 // Calculate maximum for horizontal scrollbar. Check for reasonable
4599 // line numbers, topline and botline can be invalid when displaying is
4600 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004601 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4602 && curwin->w_topline <= curwin->w_cursor.lnum
4603 && curwin->w_botline > curwin->w_cursor.lnum
4604 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4605 {
4606 linenr_T lnum;
4607 colnr_T n;
4608 long max = 0;
4609
Bram Moolenaar30613902019-12-01 22:11:18 +01004610 // Use maximum of all visible lines. Remember the lnum of the
4611 // longest line, closest to the cursor line. Used when scrolling
4612 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004613 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4614 {
4615 n = scroll_line_len(lnum);
4616 if (n > (colnr_T)max)
4617 {
4618 max = n;
4619 ret = lnum;
4620 }
4621 else if (n == (colnr_T)max
4622 && abs((int)(lnum - curwin->w_cursor.lnum))
4623 < abs((int)(ret - curwin->w_cursor.lnum)))
4624 ret = lnum;
4625 }
4626 }
4627 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004628 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004629 ret = curwin->w_cursor.lnum;
4630
4631 return ret;
4632}
4633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004635gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
Bram Moolenaar30613902019-12-01 22:11:18 +01004637 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
4639 if (!gui.which_scrollbars[SBAR_BOTTOM])
4640 return;
4641
4642 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4643 return;
4644
4645 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4646 return;
4647
4648 /*
4649 * It is possible for the cursor to be invalid if we're in the middle of
4650 * something (like changing files). If so, don't do anything for now.
4651 */
4652 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4653 {
4654 gui.bottom_sbar.value = -1;
4655 return;
4656 }
4657
Bram Moolenaar02631462017-09-22 15:20:32 +02004658 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (curwin->w_p_wrap)
4660 {
4661 value = 0;
4662#ifdef SCROLL_PAST_END
4663 max = 0;
4664#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004665 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#endif
4667 }
4668 else
4669 {
4670 value = curwin->w_leftcol;
4671
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004672 longest_lnum = gui_find_longest_lnum();
4673 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (virtual_active())
4676 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004677 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (curwin->w_virtcol >= (colnr_T)max)
4679 max = curwin->w_virtcol;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
4682#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004683 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004685 // The line number isn't scrolled, thus there is less space when
4686 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 size -= curwin_col_off();
4688#ifndef SCROLL_PAST_END
4689 max -= curwin_col_off();
4690#endif
4691 }
4692
4693#ifndef SCROLL_PAST_END
4694 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004695 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696#endif
4697
4698#ifdef FEAT_RIGHTLEFT
4699 if (curwin->w_p_rl)
4700 {
4701 value = max + 1 - size - value;
4702 if (value < 0)
4703 {
4704 size += value;
4705 value = 0;
4706 }
4707 }
4708#endif
4709 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4710 && max == gui.bottom_sbar.max)
4711 return;
4712
4713 gui.bottom_sbar.value = value;
4714 gui.bottom_sbar.size = size;
4715 gui.bottom_sbar.max = max;
4716 gui.prev_wrap = curwin->w_p_wrap;
4717
4718 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4719}
4720
4721/*
4722 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4723 */
4724 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004725gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Bram Moolenaar30613902019-12-01 22:11:18 +01004727 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (curwin->w_p_wrap)
4729 return FALSE;
4730
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004731 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return FALSE;
4733
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004734 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
Bram Moolenaar30613902019-12-01 22:11:18 +01004736 // When the line of the cursor is too short, move the cursor to the
4737 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004739 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004740 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004742 if (compute_longest_lnum)
4743 {
4744 curwin->w_cursor.lnum = gui_find_longest_lnum();
4745 curwin->w_cursor.col = 0;
4746 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004747 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004748 else if (longest_lnum >= curwin->w_topline
4749 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 curwin->w_cursor.lnum = longest_lnum;
4752 curwin->w_cursor.col = 0;
4753 }
4754 }
4755
4756 return leftcol_changed();
4757}
4758
4759/*
4760 * Check that none of the colors are the same as the background color
4761 */
4762 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004763gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4766 {
4767 gui_set_bg_color((char_u *)"White");
4768 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4769 gui_set_fg_color((char_u *)"Black");
4770 }
4771}
4772
Bram Moolenaar3918c952005-03-15 22:34:55 +00004773 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004774gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 gui.norm_pixel = gui_get_color(name);
4777 hl_set_fg_color_name(vim_strsave(name));
4778}
4779
Bram Moolenaar3918c952005-03-15 22:34:55 +00004780 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004781gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 gui.back_pixel = gui_get_color(name);
4784 hl_set_bg_color_name(vim_strsave(name));
4785}
4786
4787/*
4788 * Allocate a color by name.
4789 * Returns INVALCOLOR and gives an error message when failed.
4790 */
4791 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004792gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 guicolor_T t;
4795
4796 if (*name == NUL)
4797 return INVALCOLOR;
4798 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004799
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004801#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 && gui.in_use
4803#endif
4804 )
Drew Vogele30d1022021-10-24 20:35:07 +01004805 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return t;
4807}
4808
4809/*
4810 * Return the grey value of a color (range 0-255).
4811 */
4812 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004813gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004815 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004818 + (((rgb >> 8) & 0xff) * 587)
4819 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820}
4821
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004822 char_u *
4823gui_bg_default(void)
4824{
4825 if (gui_get_lightness(gui.back_pixel) < 127)
4826 return (char_u *)"dark";
4827 return (char_u *)"light";
4828}
4829
4830/*
4831 * Option initializations that can only be done after opening the GUI window.
4832 */
4833 void
4834init_gui_options(void)
4835{
Bram Moolenaar30613902019-12-01 22:11:18 +01004836 // Set the 'background' option according to the lightness of the
4837 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004838 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4839 {
4840 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4841 highlight_changed();
4842 }
4843}
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#if defined(FEAT_GUI_X11) || defined(PROTO)
4846 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004847gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 win_T *wp;
4850
Bram Moolenaar30613902019-12-01 22:11:18 +01004851 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (!gui.in_use)
4853 return;
4854
4855 FOR_ALL_WINDOWS(wp)
4856 {
4857 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4858 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4859 }
4860 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4861}
4862#endif
4863
4864/*
4865 * Call this when focus has changed.
4866 */
4867 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004868gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
4870/*
4871 * Skip this code to avoid drawing the cursor when debugging and switching
4872 * between the debugger window and gvim.
4873 */
4874#if 1
4875 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004876 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878# ifdef FEAT_XIM
4879 xim_set_focus(in_focus);
4880# endif
4881
Bram Moolenaar30613902019-12-01 22:11:18 +01004882 // Put events in the input queue only when allowed.
4883 // ui_focus_change() isn't called directly, because it invokes
4884 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004885 if (!hold_gui_events)
4886 {
4887 char_u bytes[3];
4888
4889 bytes[0] = CSI;
4890 bytes[1] = KS_EXTRA;
4891 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4892 add_to_input_buf(bytes, 3);
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
4895}
4896
4897/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004898 * When mouse moved: apply 'mousefocus'.
4899 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004901 static void
4902gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
4904 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004905 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
4907#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004908 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004909 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#endif
4911
Bram Moolenaar30613902019-12-01 22:11:18 +01004912 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004914 && !hold_gui_events // not holding events
4915 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4916 && State != HITRETURN // but not hit-return prompt
4917 && msg_scrolled == 0 // no scrolled message
4918 && !need_mouse_correct // not moving the pointer
4919 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004921 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (x < 0 || x > Columns * gui.char_width)
4923 return;
4924#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004925 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#endif
4927 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004928 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaar30613902019-12-01 22:11:18 +01004930 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004931 if (Y_2_ROW(y) < tabline_height())
4932 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 /*
4935 * format a mouse click on status line input
4936 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004937 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4938 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
4940 if (finish_op)
4941 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004942 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 st[0] = ESC;
4944 add_to_input_buf(st, 1);
4945 }
4946 st[0] = CSI;
4947 st[1] = KS_MOUSE;
4948 st[2] = KE_FILLER;
4949 st[3] = (char_u)MOUSE_LEFT;
4950 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004951 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 wp->w_height + W_WINROW(wp));
4953
4954 add_to_input_buf(st, 8);
4955 st[3] = (char_u)MOUSE_RELEASE;
4956 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004958 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (gtk_main_level() > 0)
4960 gtk_main_quit();
4961#endif
4962 }
4963}
4964
4965/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004966 * Called when the mouse moved (but not when dragging).
4967 */
4968 void
4969gui_mouse_moved(int x, int y)
4970{
4971 // Ignore this while still starting up.
4972 if (!gui.in_use || gui.starting)
4973 return;
4974
4975 // apply 'mousefocus' and pointer shape
4976 gui_mouse_focus(x, y);
4977
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004978#ifdef FEAT_PROP_POPUP
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004979 if (popup_visible)
4980 // Generate a mouse-moved event, so that the popup can perhaps be
4981 // closed, just like in the terminal.
Bram Moolenaar445f11d2021-06-08 20:13:31 +02004982 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004983#endif
4984}
4985
4986/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004987 * Get the window where the mouse pointer is on.
4988 * Returns NULL if not found.
4989 */
4990 win_T *
4991gui_mouse_window(mouse_find_T popup)
4992{
4993 int x, y;
4994
4995 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4996 return NULL;
4997 gui_mch_getmouse(&x, &y);
4998
4999 // Only use the mouse when it's on the Vim window
5000 if (x >= 0 && x <= Columns * gui.char_width
5001 && y >= 0 && Y_2_ROW(y) >= tabline_height())
5002 return xy2win(x, y, popup);
5003 return NULL;
5004}
5005
5006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * Called when mouse should be moved to window with focus.
5008 */
5009 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005010gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 win_T *wp = NULL;
5013
5014 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005015
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005016 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01005017 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005019 validate_cline_row();
5020 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
5021 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024}
5025
5026/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005027 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01005028 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005031xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 int row;
5034 int col;
5035 win_T *wp;
5036
5037 row = Y_2_ROW(y);
5038 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01005039 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005041 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005042 if (wp == NULL)
5043 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005044#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (State == HITRETURN || State == ASKMORE)
5046 {
5047 if (Y_2_ROW(y) >= msg_row)
5048 update_mouseshape(SHAPE_IDX_MOREL);
5049 else
5050 update_mouseshape(SHAPE_IDX_MORE);
5051 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005052 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005054 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005055 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005057 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005058 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 update_mouseshape(SHAPE_IDX_STATUS);
5060 else
5061 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005063 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064}
5065
5066/*
5067 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
5068 * File names may be given to redefine the args list.
5069 */
5070 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005071ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073 char_u *arg = eap->arg;
5074
5075 /*
5076 * Check for "-f" argument: foreground, don't fork.
5077 * Also don't fork when started with "gvim -f".
5078 * Do fork when using "gui -b".
5079 */
5080 if (arg[0] == '-'
5081 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005082 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 gui.dofork = (arg[1] == 'b');
5085 eap->arg = skipwhite(eap->arg + 2);
5086 }
5087 if (!gui.in_use)
5088 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005089#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01005090 if (!gui.starting)
5091 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005092 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01005093 return;
5094 }
5095#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005096 // Clear the command. Needed for when forking+exiting, to avoid part
5097 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01005099#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005100 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005101 gui_start(eap->arg);
5102 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01005103#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005104 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01005105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005106 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005109 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 ex_next(eap);
5111}
5112
Bram Moolenaar4f974752019-02-17 17:44:42 +01005113#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005114 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
5115 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005116 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117/*
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005118 * This is shared between Athena, Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121/*
5122 * Callback function for do_in_runtimepath().
5123 */
5124 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005125gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005127 char_u *gfp_buffer = cookie;
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (STRLEN(fname) >= MAXPATHL)
5130 *gfp_buffer = NUL;
5131 else
5132 STRCPY(gfp_buffer, fname);
5133}
5134
5135/*
5136 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5137 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5138 */
5139 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005140gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 if (STRLEN(name) > MAXPATHL - 14)
5143 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005144 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005145 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005146 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 return FAIL;
5148 return OK;
5149}
5150
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005151# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152/*
5153 * Given the name of the "icon=" argument, try finding the bitmap file for the
5154 * icon. If it is an absolute path name, use it as it is. Otherwise append
5155 * "ext" and search for it in 'runtimepath'.
5156 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5157 * contains "name".
5158 */
5159 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005160gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 char_u buf[MAXPATHL + 1];
5163
5164 expand_env(name, buffer, MAXPATHL);
5165 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5166 STRCPY(buffer, buf);
5167}
5168# endif
5169#endif
5170
Bram Moolenaar9e175142020-04-30 22:51:01 +02005171#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5172 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005174display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 char_u *p;
5177
5178 if (isatty(2))
5179 fflush(stderr);
5180 else if (error_ga.ga_data != NULL)
5181 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005182 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5184 if (!isspace(*p))
5185 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005186 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (STRLEN(p) > 2000)
5188 STRCPY(p + 2000 - 14, "...(truncated)");
5189 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005190 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192 }
5193 ga_clear(&error_ga);
5194 }
5195}
5196#endif
5197
5198#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5199/*
5200 * Return TRUE if still starting up and there is no place to enter text.
5201 * For GTK and X11 we check if stderr is not a tty, which means we were
5202 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5203 * allow typing on stdin.
5204 */
5205 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005206no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 return ((!gui.in_use || gui.starting)
5209# ifndef NO_CONSOLE
5210 && !isatty(0) && !isatty(2)
5211# endif
5212 );
5213}
5214#endif
5215
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005216#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005217 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005218 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219/*
5220 * Update the current window and the screen.
5221 */
5222 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005223gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005225# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005226 linenr_T conceal_old_cursor_line = 0;
5227 linenr_T conceal_new_cursor_line = 0;
5228 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005229# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 update_topline();
5232 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005233
Bram Moolenaar30613902019-12-01 22:11:18 +01005234 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005235 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005236# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005237 || popup_visible
5238# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005239# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005240 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005241# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005242 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005243 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005244 if (has_cursormoved())
5245 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005246#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005247 if (popup_visible)
5248 popup_check_cursor_pos();
5249#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005250# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005251 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005252 {
5253 conceal_old_cursor_line = last_cursormoved.lnum;
5254 conceal_new_cursor_line = curwin->w_cursor.lnum;
5255 conceal_update_lines = TRUE;
5256 }
5257# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005258 last_cursormoved = curwin->w_cursor;
5259 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005260
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005261# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005262 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005263 && (conceal_old_cursor_line != conceal_new_cursor_line
5264 || conceal_cursor_line(curwin)
5265 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005266 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005267 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005268 redrawWinline(curwin, conceal_old_cursor_line);
5269 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005270 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005271 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005272 }
5273# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005274 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005275 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005276 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277}
5278#endif
5279
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005280#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281/*
5282 * Get the text to use in a find/replace dialog. Uses the last search pattern
5283 * if the argument is empty.
5284 * Returns an allocated string.
5285 */
5286 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005287get_find_dialog_text(
5288 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005289 int *wwordp, // return: TRUE if \< \> found
5290 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 char_u *text;
5293
5294 if (*arg == NUL)
5295 text = last_search_pat();
5296 else
5297 text = arg;
5298 if (text != NULL)
5299 {
5300 text = vim_strsave(text);
5301 if (text != NULL)
5302 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005303 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 int i;
5305
Bram Moolenaar30613902019-12-01 22:11:18 +01005306 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5308 {
5309 mch_memmove(text, text + 2, (size_t)(len - 1));
5310 len -= 2;
5311 }
5312
Bram Moolenaar30613902019-12-01 22:11:18 +01005313 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5315 {
5316 *mcasep = (text[1] == 'C');
5317 mch_memmove(text, text + 2, (size_t)(len - 1));
5318 len -= 2;
5319 }
5320
Bram Moolenaar30613902019-12-01 22:11:18 +01005321 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (len >= 4
5323 && STRNCMP(text, "\\<", 2) == 0
5324 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5325 {
5326 *wwordp = TRUE;
5327 mch_memmove(text, text + 2, (size_t)(len - 4));
5328 text[len - 4] = NUL;
5329 }
5330
Bram Moolenaar30613902019-12-01 22:11:18 +01005331 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 for (i = 0; i + 1 < len; ++i)
5333 if (text[i] == '\\' && (text[i + 1] == '/'
5334 || text[i + 1] == '?'))
5335 {
5336 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5337 --len;
5338 }
5339 }
5340 }
5341 return text;
5342}
5343
5344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 * Handle the press of a button in the find-replace dialog.
5346 * Return TRUE when something was added to the input buffer.
5347 */
5348 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005349gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005350 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005351 char_u *find_text,
5352 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005353 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 garray_T ga;
5356 int i;
5357 int type = (flags & FRD_TYPE_MASK);
5358 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005359 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005360 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005361 static int busy = FALSE;
5362
Bram Moolenaar30613902019-12-01 22:11:18 +01005363 // When the screen is being updated we should not change buffers and
5364 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005365 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005366 if (updating_screen || busy)
5367 return FALSE;
5368
Bram Moolenaar30613902019-12-01 22:11:18 +01005369 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005370 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5371 return FALSE;
5372
5373 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
5375 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005376 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 ga_concat(&ga, (char_u *)"%s/");
5378
5379 ga_concat(&ga, (char_u *)"\\V");
5380 if (flags & FRD_MATCH_CASE)
5381 ga_concat(&ga, (char_u *)"\\C");
5382 else
5383 ga_concat(&ga, (char_u *)"\\c");
5384 if (flags & FRD_WHOLE_WORD)
5385 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005386 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005387 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5388 if (p != NULL)
5389 ga_concat(&ga, p);
5390 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (flags & FRD_WHOLE_WORD)
5392 ga_concat(&ga, (char_u *)"\\>");
5393
5394 if (type == FRD_REPLACEALL)
5395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005397 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005398 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5399 if (p != NULL)
5400 ga_concat(&ga, p);
5401 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005403 }
5404 ga_append(&ga, NUL);
5405
5406 if (type == FRD_REPLACE)
5407 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005408 // Do the replacement when the text at the cursor matches. Thus no
5409 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005410 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5411 regmatch.rm_ic = 0;
5412 if (regmatch.regprog != NULL)
5413 {
5414 p = ml_get_cursor();
5415 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5416 && regmatch.startp[0] == p)
5417 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005418 // Clear the command line to remove any old "No match"
5419 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005420 msg_end_prompt();
5421
5422 if (u_save_cursor() == OK)
5423 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005424 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005425 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005426
5427 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005428 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005429 ins_str(repl_text);
5430 }
5431 }
5432 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005433 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005434 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005435 }
5436 }
5437
5438 if (type == FRD_REPLACEALL)
5439 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005440 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005441 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 do_cmdline_cmd(ga.ga_data);
5443 }
5444 else
5445 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005446 int searchflags = SEARCH_MSG + SEARCH_MARK;
5447
Bram Moolenaar30613902019-12-01 22:11:18 +01005448 // Search for the next match.
5449 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005450 if (type == FRD_REPLACE)
5451 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005453 if (down)
5454 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01005455 (void)do_search(NULL, '/', '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005456 }
5457 else
5458 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005459 // We need to escape '?' if and only if we are searching in the up
5460 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005461 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5462 if (p != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01005463 (void)do_search(NULL, '?', '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005464 vim_free(p);
5465 }
5466
Bram Moolenaar30613902019-12-01 22:11:18 +01005467 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469
Bram Moolenaar30613902019-12-01 22:11:18 +01005470 // Don't want to pass did_emsg to other code, it may cause disabling
5471 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005472 did_emsg = save_did_emsg;
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (State & (NORMAL | INSERT))
5475 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005476 gui_update_screen(); // update the screen
5477 msg_didout = 0; // overwrite any message
5478 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480
5481 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005482 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 return (ga.ga_len > 0);
5484}
5485
5486#endif
5487
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005488#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489/*
5490 * Jump to the window at specified point (x, y).
5491 */
5492 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005493gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 int row = Y_2_ROW(y);
5496 int col = X_2_COL(x);
5497 win_T *wp;
5498
5499 if (row >= 0 && col >= 0)
5500 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005501 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (wp != NULL && wp != curwin)
5503 win_goto(wp);
5504 }
5505}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005508 * Function passed to handle_drop() for the actions to be done after the
5509 * argument list has been updated.
5510 */
5511 static void
5512drop_callback(void *cookie)
5513{
5514 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005515 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005516
Bram Moolenaar30613902019-12-01 22:11:18 +01005517 // If Shift held down, change to first file's directory. If the first
5518 // item is a directory, change to that directory (and let the explorer
5519 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005520 if (p != NULL)
5521 {
5522 if (mch_isdir(p))
5523 {
5524 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005525 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005526 }
5527 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005528 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005529 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005530 if (do_shorten)
5531 {
5532 shorten_fnames(TRUE);
5533 last_chdir_reason = "drop";
5534 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005535 }
5536
Bram Moolenaar30613902019-12-01 22:11:18 +01005537 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005538 update_screen(NOT_VALID);
5539# ifdef FEAT_MENU
5540 gui_update_menus(0);
5541# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005542 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005543 setcursor();
5544 out_flush_cursor(FALSE, FALSE);
5545}
5546
5547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 * Process file drop. Mouse cursor position, key modifiers, name of files
5549 * and count of files are given. Argument "fnames[count]" has full pathnames
5550 * of dropped files, they will be freed in this function, and caller can't use
5551 * fnames after call this function.
5552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005554gui_handle_drop(
5555 int x UNUSED,
5556 int y UNUSED,
5557 int_u modifiers,
5558 char_u **fnames,
5559 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 int i;
5562 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005563 static int entered = FALSE;
5564
5565 /*
5566 * This function is called by event handlers. Just in case we get a
5567 * second event before the first one is handled, ignore the second one.
5568 * Not sure if this can ever happen, just in case.
5569 */
5570 if (entered)
5571 return;
5572 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 /*
5575 * When the cursor is at the command line, add the file names to the
5576 * command line, don't edit the files.
5577 */
5578 if (State & CMDLINE)
5579 {
5580 shorten_filenames(fnames, count);
5581 for (i = 0; i < count; ++i)
5582 {
5583 if (fnames[i] != NULL)
5584 {
5585 if (i > 0)
5586 add_to_input_buf((char_u*)" ", 1);
5587
Bram Moolenaar30613902019-12-01 22:11:18 +01005588 // We don't know what command is used thus we can't be sure
5589 // about which characters need to be escaped. Only escape the
5590 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591# ifdef BACKSLASH_IN_FILENAME
5592 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5593# else
5594 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5595# endif
5596 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005597 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 vim_free(p);
5599 vim_free(fnames[i]);
5600 }
5601 }
5602 vim_free(fnames);
5603 }
5604 else
5605 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005606 // Go to the window under mouse cursor, then shorten given "fnames" by
5607 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 shorten_filenames(fnames, count);
5610
Bram Moolenaar30613902019-12-01 22:11:18 +01005611 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if ((modifiers & MOUSE_SHIFT) != 0)
5613 p = vim_strsave(fnames[0]);
5614 else
5615 p = NULL;
5616
Bram Moolenaar30613902019-12-01 22:11:18 +01005617 // Handle the drop, :edit or :split to get to the file. This also
5618 // frees fnames[]. Skip this if there is only one item, it's a
5619 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5621 && mch_isdir(fnames[0]))
5622 {
5623 vim_free(fnames[0]);
5624 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005625 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005628 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005629 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005631
5632 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005635
5636/*
5637 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5638 * applied yet.
5639 * Return the key with modifiers applied if so, NUL if not.
5640 */
5641 int
5642check_for_interrupt(int key, int modifiers_arg)
5643{
5644 int modifiers = modifiers_arg;
5645 int c = merge_modifyOtherKeys(key, &modifiers);
5646
5647 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005648#ifdef UNIX
5649 || (intr_char != Ctrl_C && c == intr_char)
5650#endif
5651 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005652 {
5653 got_int = TRUE;
5654 return c;
5655 }
5656 return NUL;
5657}
5658