blob: 616f6022bd34b48f3634764372ad4684c2196889 [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
141 // the parent to resume, and X11 is enabled with FEAT_TITLE, this will
142 // 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 Moolenaar071d4272004-06-13 20:20:40 +0000149#ifdef FEAT_TITLE
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200152#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
153 if (msg)
154 emsg(msg);
155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 }
157
158 vim_free(old_term);
159
Bram Moolenaar30613902019-12-01 22:11:18 +0100160 // If the GUI started successfully, trigger the GUIEnter event, otherwise
161 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200162 gui_mch_update();
163 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
164 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200165 --recursive;
166}
167
168/*
169 * Set_termname() will call gui_init() to start the GUI.
170 * Set the "starting" flag, to indicate that the GUI will start.
171 *
172 * We don't want to open the GUI shell until after we've read .gvimrc,
173 * otherwise we don't know what font we will use, and hence we don't know
174 * what size the shell should be. So if there are errors in the .gvimrc
175 * file, they will have to go to the terminal: Set full_screen to FALSE.
176 * full_screen will be set to TRUE again by a successful termcapinit().
177 */
178 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100179gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200180{
181 static int recursive = 0;
182
183 ++recursive;
184 gui.starting = TRUE;
185
186#ifdef FEAT_GUI_GTK
187 gui.event_time = GDK_CURRENT_TIME;
188#endif
189
190 termcapinit((char_u *)"builtin_gui");
191 gui.starting = recursive - 1;
192
Bram Moolenaar9372a112005-12-06 19:59:18 +0000193#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200195 {
196# ifdef FEAT_EVAL
197 Window x11_window;
198 Display *x11_display;
199
200 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
201 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
202# endif
203
Bram Moolenaar30613902019-12-01 22:11:18 +0100204 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 --recursive;
209}
210
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200211#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200212
Bram Moolenaar30613902019-12-01 22:11:18 +0100213// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200214# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
215# include <sys/wait.h>
216# endif
217
218/*
219 * Create a new process, by forking. In the child, start the GUI, and in
220 * the parent, exit.
221 *
222 * If something goes wrong, this will return with gui.in_use still set
223 * to FALSE, in which case the caller should continue execution without
224 * the GUI.
225 *
226 * If the child fails to start the GUI, then the child will exit and the
227 * parent will return. If the child succeeds, then the parent will exit
228 * and the child will return.
229 */
230 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100231gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200232{
Bram Moolenaar30613902019-12-01 22:11:18 +0100233 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200234 int pipe_error;
235 int status;
236 int exit_status;
237 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200238
Bram Moolenaar30613902019-12-01 22:11:18 +0100239 // Setup a pipe between the child and the parent, so that the parent
240 // knows when the child has done the setsid() call and is allowed to
241 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200242 pipe_error = (pipe(pipefd) < 0);
243 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100244 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100246 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200247 return;
248 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100249 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200250 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100251 // Give the child some time to do the setsid(), otherwise the
252 // exit() may kill the child too (when starting gvim from inside a
253 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200254 if (!pipe_error)
255 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100256 // The read returns when the child closes the pipe (or when
257 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200258 close(pipefd[1]);
259 status = gui_read_child_pipe(pipefd[0]);
260 if (status == GUI_CHILD_FAILED)
261 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100262 // The child failed to start the GUI, so the caller must
263 // continue. There may be more error information written
264 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200265# ifdef __NeXT__
266 wait4(pid, &exit_status, 0, (struct rusage *)0);
267# else
268 waitpid(pid, &exit_status, 0);
269# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100270 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200271 return;
272 }
273 else if (status == GUI_CHILD_IO_ERROR)
274 {
275 pipe_error = TRUE;
276 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100277 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200278 }
279
280 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100281 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200282
Bram Moolenaar30613902019-12-01 22:11:18 +0100283 // When swapping screens we may need to go to the next line, e.g.,
284 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200285 if (newline_on_exit)
286 mch_errmsg("\r\n");
287
288 /*
289 * The parent must skip the normal exit() processing, the child
290 * will do it. For example, GTK messes up signals when exiting.
291 */
292 _exit(0);
293 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100294 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200295
Bram Moolenaar29695702012-05-18 17:03:18 +0200296#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100297 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200298 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100299 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200300#endif
301
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200302# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
303 /*
304 * Change our process group. On some systems/shells a CTRL-C in the
305 * shell where Vim was started would otherwise kill gvim!
306 */
307# if defined(HAVE_SETSID)
308 (void)setsid();
309# else
310 (void)setpgid(0, 0);
311# endif
312# endif
313 if (!pipe_error)
314 close(pipefd[0]);
315
316# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100317 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200318 gui_mch_forked();
319# endif
320
Bram Moolenaar30613902019-12-01 22:11:18 +0100321 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200322 gui_attempt_start();
323
Bram Moolenaar30613902019-12-01 22:11:18 +0100324 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200325 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200326 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 if (gui.in_use)
328 write_eintr(pipefd[1], "ok", 3);
329 else
330 write_eintr(pipefd[1], "fail", 5);
331 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200332 }
333
Bram Moolenaar30613902019-12-01 22:11:18 +0100334 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200335 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100336 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337}
338
339/*
340 * Read from a pipe assumed to be connected to the child process (this
341 * function is called from the parent).
342 * Return GUI_CHILD_OK if the child successfully started the GUI,
343 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
344 * some other error.
345 *
346 * The file descriptor will be closed before the function returns.
347 */
348 static int
349gui_read_child_pipe(int fd)
350{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200351 long bytes_read;
352#define READ_BUFFER_SIZE 10
353 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200354
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200355 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
356#undef READ_BUFFER_SIZE
357 close(fd);
358 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200359 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200360 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200361 if (strcmp(buffer, "ok") == 0)
362 return GUI_CHILD_OK;
363 return GUI_CHILD_FAILED;
364}
365
Bram Moolenaar30613902019-12-01 22:11:18 +0100366#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200367
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368/*
369 * Call this when vim starts up, whether or not the GUI is started
370 */
371 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100372gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar30613902019-12-01 22:11:18 +0100374 gui.in_use = FALSE; // No GUI yet (maybe later)
375 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 gui_mch_prepare(argc, argv);
377}
378
379/*
380 * Try initializing the GUI and check if it can be started.
381 * Used from main() to check early if "vim -g" can start the GUI.
382 * Used from gui_init() to prepare for starting the GUI.
383 * Returns FAIL or OK.
384 */
385 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100386gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 static int result = MAYBE;
389
390 if (result != MAYBE)
391 {
392 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100393 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 return result;
395 }
396
397 gui.shell_created = FALSE;
398 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100399 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 gui.dragged_sb = SBAR_NONE;
401 gui.dragged_wp = NULL;
402 gui.pointer_hidden = FALSE;
403 gui.col = 0;
404 gui.row = 0;
405 gui.num_cols = Columns;
406 gui.num_rows = Rows;
407
408 gui.cursor_is_valid = FALSE;
409 gui.scroll_region_top = 0;
410 gui.scroll_region_bot = Rows - 1;
411 gui.scroll_region_left = 0;
412 gui.scroll_region_right = Columns - 1;
413 gui.highlight_mask = HL_NORMAL;
414 gui.char_width = 1;
415 gui.char_height = 1;
416 gui.char_ascent = 0;
417 gui.border_width = 0;
418
419 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200420#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 gui.bold_font = NOFONT;
422 gui.ital_font = NOFONT;
423 gui.boldital_font = NOFONT;
424# ifdef FEAT_XFONTSET
425 gui.fontset = NOFONTSET;
426# endif
427#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200428 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100429#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200430 gui.wide_bold_font = NOFONT;
431 gui.wide_ital_font = NOFONT;
432 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200436# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437# ifdef FONTSET_ALWAYS
438 gui.menu_fontset = NOFONTSET;
439# else
440 gui.menu_font = NOFONT;
441# endif
442# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100443 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# ifndef FEAT_GUI_GTK
445 gui.menu_height = MENU_DEFAULT_HEIGHT;
446 gui.menu_width = 0;
447# endif
448#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100449#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
450 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 gui.toolbar_height = 0;
452#endif
453#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
454 gui.footer_height = 0;
455#endif
456#ifdef FEAT_BEVAL_TIP
457 gui.tooltip_fontset = NOFONTSET;
458#endif
459
460 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
461 gui.prev_wrap = -1;
462
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100463# ifdef FEAT_GUI_GTK
464 CLEAR_FIELD(gui.ligatures_map);
465#endif
466
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200467#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 result = OK;
469#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200470# ifdef FEAT_GUI_GTK
471 /*
472 * Note: Don't call gtk_init_check() before fork, it will be called after
473 * the fork. When calling it before fork, it make vim hang for a while.
474 * See gui_do_fork().
475 * Use a simpler check if the GUI window can probably be opened.
476 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200477 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200478# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481#endif
482 return result;
483}
484
485/*
486 * This is the call which starts the GUI.
487 */
488 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100489gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
491 win_T *wp;
492 static int recursive = 0;
493
494 /*
495 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
496 * function will then be executed at the first call, the rest by the
497 * recursive call. This allow the shell to be opened halfway reading a
498 * gvimrc file.
499 */
500 if (!recursive)
501 {
502 ++recursive;
503
504 clip_init(TRUE);
505
Bram Moolenaar30613902019-12-01 22:11:18 +0100506 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (gui_init_check() == FAIL)
508 {
509 --recursive;
510 clip_init(FALSE);
511 return;
512 }
513
514 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000515 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
516 * breaks the Paste toolbar button.
517 */
518 set_option_value((char_u *)"paste", 0L, NULL, 0);
519
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200520 // Set t_Co to the number of colors: RGB.
521 set_color_count(256 * 256 * 256);
522
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000523 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 * Set up system-wide default menus.
525 */
526#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
527 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
528 {
529 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 sys_menu = FALSE;
532 }
533#endif
534
535 /*
536 * Switch on the mouse by default, unless the user changed it already.
537 * This can then be changed in the .gvimrc.
538 */
539 if (!option_was_set((char_u *)"mouse"))
540 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000541 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543 /*
544 * If -U option given, use only the initializations from that file and
545 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
546 */
547 if (use_gvimrc != NULL)
548 {
549 if (STRCMP(use_gvimrc, "NONE") != 0
550 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +0100552 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554 else
555 {
556 /*
557 * Get system wide defaults for gvim, only when file name defined.
558 */
559#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561#endif
562
563 /*
564 * Try to read GUI initialization commands from the following
565 * places:
566 * - environment variable GVIMINIT
567 * - the user gvimrc file (~/.gvimrc)
568 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
569 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
570 * The first that exists is used, the rest is ignored.
571 */
572 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000573 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200579#ifdef USR_GVIMRC_FILE3
580 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 )
584 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200585#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
587 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
589 }
590
591 /*
592 * Read initialization commands from ".gvimrc" in current
593 * directory. This is only done if the 'exrc' option is set.
594 * Because of security reasons we disallow shell and write
595 * commands now, except for unix if the file is owned by the user
596 * or 'secure' option has been reset in environment of global
597 * ".gvimrc".
598 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
599 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
600 */
601 if (p_exrc)
602 {
603#ifdef UNIX
604 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200605 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar30613902019-12-01 22:11:18 +0100607 // if ".gvimrc" file is not owned by user, set 'secure'
608 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
610 secure = p_secure;
611 }
612#else
613 secure = p_secure;
614#endif
615
616 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200617 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef SYS_GVIMRC_FILE
619 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200620 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622#ifdef USR_GVIMRC_FILE2
623 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200624 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#endif
626#ifdef USR_GVIMRC_FILE3
627 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200628 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200630#ifdef USR_GVIMRC_FILE4
631 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200632 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 if (secure == 2)
638 need_wait_return = TRUE;
639 secure = 0;
640 }
641 }
642
643 if (need_wait_return || msg_didany)
644 wait_return(TRUE);
645
646 --recursive;
647 }
648
Bram Moolenaar30613902019-12-01 22:11:18 +0100649 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (gui.in_use)
651 return;
652
653 /*
654 * Create the GUI shell.
655 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100656 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (gui_mch_init() == FAIL)
658 goto error;
659
Bram Moolenaar30613902019-12-01 22:11:18 +0100660 // Avoid a delay for an error message that was printed in the terminal
661 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 emsg_on_display = FALSE;
663 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100664 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 need_wait_return = FALSE;
666 msg_didany = FALSE;
667
668 /*
669 * Check validity of any generic resources that may have been loaded.
670 */
671 if (gui.border_width < 0)
672 gui.border_width = 0;
673
674 /*
675 * Set up the fonts. First use a font specified with "-fn" or "-font".
676 */
677 if (font_argument != NULL)
678 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
679 if (
680#ifdef FEAT_XFONTSET
681 (*p_guifontset == NUL
682 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
683#endif
684 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
685 : p_guifont, FALSE) == FAIL)
686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 goto error2;
689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100691 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693 gui.num_cols = Columns;
694 gui.num_rows = Rows;
695 gui_reset_scroll_region();
696
Bram Moolenaar30613902019-12-01 22:11:18 +0100697 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 FOR_ALL_WINDOWS(wp)
699 {
700 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
701 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
702 }
703 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
704
705#ifdef FEAT_MENU
706 gui_create_initial_menus(root_menu);
707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#ifdef FEAT_SIGN_ICONS
709 sign_gui_started();
710#endif
711
Bram Moolenaar30613902019-12-01 22:11:18 +0100712 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 gui_init_which_components(NULL);
714
Bram Moolenaar30613902019-12-01 22:11:18 +0100715 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 gui.shell_created = TRUE;
717
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100718#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100719 // Set the shell size, adjusted for the screen size. For GTK this only
720 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100721 // If the window is already maximized (e.g. when --windowid is passed in),
722 // we want to use the system-provided dimensions by passing FALSE to
723 // mustset. Otherwise, we want to initialize with the default rows/columns.
724 if (gui_mch_maximized())
725 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
726 else
727 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100728#else
729# ifndef FEAT_GUI_GTK
730 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100734 // Need to set the size of the menubar after all the menus have been
735 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 gui_mch_compute_menu_height((Widget)0);
737#endif
738
739 /*
740 * Actually open the GUI shell.
741 */
742 if (gui_mch_open() != FAIL)
743 {
744#ifdef FEAT_TITLE
745 maketitle();
746 resettitle();
747#endif
748 init_gui_options();
749#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100750 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 p_tbidi = FALSE;
752#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000753#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100754 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000756
757# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100758 // If there is no 'm' in 'guioptions' we need to remove the menu now.
759 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000760 if (vim_strchr(p_go, GO_MENUS) == NULL)
761 {
762 --gui.starting;
763 gui_mch_enable_menu(FALSE);
764 ++gui.starting;
765 gui_mch_update();
766 }
767# endif
768
Bram Moolenaar30613902019-12-01 22:11:18 +0100769 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100770 if (gui_mch_maximized())
771 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
772 else
773 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100775 // When 'lines' was set while starting up the topframe may have to be
776 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000777 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000778
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100779#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100780 // Always create the Balloon Evaluation area, but disable it when
781 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100782 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200783 {
784# ifdef FEAT_VARTABS
785 vim_free(balloonEval->vts);
786# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100787 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200788 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100789 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000790# ifdef FEAT_GUI_GTK
791 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
792 &general_beval_cb, NULL);
793# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000794# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000795 {
796 extern Widget textArea;
797 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000798 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000799 }
800# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100801# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000802 balloonEval = gui_mch_create_beval_area(NULL, NULL,
803 &general_beval_cb, NULL);
804# endif
805# endif
806# endif
807 if (!p_beval)
808 gui_mch_disable_beval_area(balloonEval);
809#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200810
811#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200812 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200813 // Don't do this for MS-Windows yet, it sends CTRL-K without the
814 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200815 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200816#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
819 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100822 // When 'cmdheight' was set during startup it may not have taken
823 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000824 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000825 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 return;
828 }
829
830error2:
831#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100832 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 gui_mch_uninit();
834#endif
835
836error:
837 gui.in_use = FALSE;
838 clip_init(FALSE);
839}
840
841
842 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100843gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaar30613902019-12-01 22:11:18 +0100845 // don't free the fonts, it leads to a BUS error
846 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 gui.in_use = FALSE;
849 gui_mch_exit(rc);
850}
851
Bram Moolenaar9372a112005-12-06 19:59:18 +0000852#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200853 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000854# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Called when the GUI shell is closed by the user. If there are no changed
857 * files Vim exits, otherwise there will be a dialog to ask the user what to
858 * do.
859 * When this function returns, Vim should NOT exit!
860 */
861 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100862gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200864 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar3552e742021-05-29 12:21:58 +0200866 if (before_quit_autocmds(curwin, TRUE, FALSE))
867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar30613902019-12-01 22:11:18 +0100869 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 exiting = TRUE;
871# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200872 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# endif
874# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200875 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100877 // If there are changed buffers, present the user with a dialog if
878 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100879 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 getout(0);
881
882 exiting = FALSE;
883 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100884 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885}
886#endif
887
888/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200889 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * first font name that works is used. If none is found, use the default
891 * font.
892 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
893 * Return OK when able to set the font. When it failed FAIL is returned and
894 * the fonts are unchanged.
895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100897gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899#define FONTLEN 320
900 char_u font_name[FONTLEN];
901 int font_list_empty = FALSE;
902 int ret = FAIL;
903
904 if (!gui.in_use)
905 return FAIL;
906
907 font_name[0] = NUL;
908 if (*font_list == NUL)
909 font_list_empty = TRUE;
910 else
911 {
912#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100913 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (fontset)
915 ret = gui_mch_init_font(font_list, TRUE);
916 else
917#endif
918 while (*font_list != NUL)
919 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100920 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
922
Bram Moolenaar30613902019-12-01 22:11:18 +0100923 // Careful!!! The Win32 version of gui_mch_init_font(), when
924 // called with "*" will change p_guifont to the selected font
925 // name, which frees the old value. This makes font_list
926 // invalid. Thus when OK is returned here, font_list must no
927 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (gui_mch_init_font(font_name, FALSE) == OK)
929 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100930#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100931 // If it's a Unicode font, try setting 'guifontwide' to a
932 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
934 && strstr((char *)font_name, "10646") != NULL)
935 set_guifontwide(font_name);
936#endif
937 ret = OK;
938 break;
939 }
940 }
941 }
942
943 if (ret != OK
944 && STRCMP(font_list, "*") != 0
945 && (font_list_empty || gui.norm_font == NOFONT))
946 {
947 /*
948 * Couldn't load any font in 'font_list', keep the current font if
949 * there is one. If 'font_list' is empty, or if there is no current
950 * font, tell gui_mch_init_font() to try to find a font we can load.
951 */
952 ret = gui_mch_init_font(NULL, FALSE);
953 }
954
955 if (ret == OK)
956 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200957#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100958 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959# ifdef FEAT_XFONTSET
960 if (gui.fontset != NOFONTSET)
961 gui_mch_set_fontset(gui.fontset);
962 else
963# endif
964 gui_mch_set_font(gui.norm_font);
965#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100966 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968
969 return ret;
970}
971
Bram Moolenaar13505972019-01-24 15:04:48 +0100972#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973/*
974 * Try setting 'guifontwide' to a font twice as wide as "name".
975 */
976 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100977set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100980 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 char_u *wp = NULL;
982 char_u *p;
983 GuiFont font;
984
985 wp = wide_name;
986 for (p = name; *p != NUL; ++p)
987 {
988 *wp++ = *p;
989 if (*p == '-')
990 {
991 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100992 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994 if (p[1] == '-')
995 *wp++ = '*';
996 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100997 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 ++p;
1000 i = getdigits(&p);
1001 if (i != 0)
1002 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001003 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 sprintf((char *)wp, "%d%s", i * 2, p);
1005 font = gui_mch_get_font(wide_name, FALSE);
1006 if (font != NOFONT)
1007 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001008 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 gui.wide_font = font;
1010 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001011 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014 break;
1015 }
1016 }
1017 }
1018}
Bram Moolenaar30613902019-12-01 22:11:18 +01001019#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
1021/*
1022 * Get the font for 'guifontwide'.
1023 * Return FAIL for an invalid font name.
1024 */
1025 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001026gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 GuiFont font = NOFONT;
1029 char_u font_name[FONTLEN];
1030 char_u *p;
1031
Bram Moolenaar30613902019-12-01 22:11:18 +01001032 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1033 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1036 {
1037 for (p = p_guifontwide; *p != NUL; )
1038 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001039 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1041 font = gui_mch_get_font(font_name, FALSE);
1042 if (font != NOFONT)
1043 break;
1044 }
1045 if (font == NOFONT)
1046 return FAIL;
1047 }
1048
1049 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001050#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (font != NOFONT && gui.norm_font != NOFONT
1053 && pango_font_description_equal(font, gui.norm_font))
1054 {
1055 gui.wide_font = NOFONT;
1056 gui_mch_free_font(font);
1057 }
1058 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001062 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001063#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001064 /*
1065 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1066 * support those fonts for 'guifontwide'.
1067 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 return OK;
1070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001072#if defined(FEAT_GUI_GTK) || defined(PROTO)
1073/*
1074 * Set list of ascii characters that combined can create ligature.
1075 * Store them in char map for quick access from gui_gtk2_draw_string.
1076 */
1077 void
1078gui_set_ligatures(void)
1079{
1080 char_u *p;
1081
1082 if (*p_guiligatures != NUL)
1083 {
1084 // check for invalid characters
1085 for (p = p_guiligatures; *p != NUL; ++p)
1086 if (*p < 32 || *p > 127)
1087 {
1088 emsg(_(e_ascii_code_not_in_range));
1089 return;
1090 }
1091
1092 // store valid setting into ligatures_map
1093 CLEAR_FIELD(gui.ligatures_map);
1094 for (p = p_guiligatures; *p != NUL; ++p)
1095 gui.ligatures_map[*p] = 1;
1096 }
1097 else
1098 CLEAR_FIELD(gui.ligatures_map);
1099}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001100
1101/*
1102 * Adjust the columns to undraw for when the cursor is on ligatures.
1103 */
1104 static void
1105gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1106{
1107 int off;
1108
1109 if (ScreenLines == NULL || *p_guiligatures == NUL)
1110 return;
1111
1112 // expand before the cursor for all the chars in gui.ligatures_map
1113 off = LineOffset[gui.cursor_row] + *startcol;
1114 if (gui.ligatures_map[ScreenLines[off]])
1115 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1116 (*startcol)--;
1117
1118 // expand after the cursor for all the chars in gui.ligatures_map
1119 off = LineOffset[gui.cursor_row] + *endcol;
1120 if (gui.ligatures_map[ScreenLines[off]])
1121 while (*endcol < ((int)screen_Columns - 1)
1122 && gui.ligatures_map[ScreenLines[++off]])
1123 (*endcol)++;
1124}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001125#endif
1126
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001127 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001128gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
1130 gui.row = row;
1131 gui.col = col;
1132}
1133
1134/*
1135 * gui_check_pos - check if the cursor is on the screen.
1136 */
1137 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001138gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 if (gui.row >= screen_Rows)
1141 gui.row = screen_Rows - 1;
1142 if (gui.col >= screen_Columns)
1143 gui.col = screen_Columns - 1;
1144 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1145 gui.cursor_is_valid = FALSE;
1146}
1147
1148/*
1149 * Redraw the cursor if necessary or when forced.
1150 * Careful: The contents of ScreenLines[] must match what is on the screen,
1151 * otherwise this goes wrong. May need to call out_flush() first.
1152 */
1153 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001154gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001155 int force, // when TRUE, update even when not moved
1156 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 int cur_width = 0;
1159 int cur_height = 0;
1160 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001161 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001163#ifdef FEAT_TERMINAL
1164 guicolor_T shape_fg = INVALCOLOR;
1165 guicolor_T shape_bg = INVALCOLOR;
1166#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001167 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1168 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int attr;
1170 attrentry_T *aep = NULL;
1171
Bram Moolenaar30613902019-12-01 22:11:18 +01001172 // Don't update the cursor when halfway busy scrolling or the screen size
1173 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001174 if (!can_update_cursor || screen_Columns != gui.num_cols
1175 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 return;
1177
1178 gui_check_pos();
1179 if (!gui.cursor_is_valid || force
1180 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1181 {
1182 gui_undraw_cursor();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001183
1184 // If a cursor-less sleep is ongoing, leave the cursor invisible
1185 if (cursor_is_sleeping())
1186 return;
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (gui.row < 0)
1189 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001190#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1192 im_set_position(gui.row, gui.col);
1193#endif
1194 gui.cursor_row = gui.row;
1195 gui.cursor_col = gui.col;
1196
Bram Moolenaar30613902019-12-01 22:11:18 +01001197 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (!screen_cleared || ScreenLines == NULL)
1199 return;
1200
Bram Moolenaar30613902019-12-01 22:11:18 +01001201 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (clear_selection)
1203 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001204 // Check that the cursor is inside the shell (resizing may have made
1205 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1207 return;
1208
1209 gui.cursor_is_valid = TRUE;
1210
1211 /*
1212 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001213 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001215#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001216 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001217 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001219#endif
1220 shape = &shape_table[get_shape_idx(FALSE)];
1221 if (State & LANGMAP)
1222 id = shape->id_lm;
1223 else
1224 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar30613902019-12-01 22:11:18 +01001226 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 cfg = INVALCOLOR;
1228 cbg = INVALCOLOR;
1229 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001230 gui_mch_set_blinking(shape->blinkwait,
1231 shape->blinkon,
1232 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001233 if (shape->blinkwait == 0 || shape->blinkon == 0
1234 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001235 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001236#ifdef FEAT_TERMINAL
1237 if (shape_bg != INVALCOLOR)
1238 {
1239 cattr = 0;
1240 cfg = shape_fg;
1241 cbg = shape_bg;
1242 }
1243 else
1244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (id > 0)
1246 {
1247 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001248#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 static int iid;
1251 guicolor_T fg, bg;
1252
Bram Moolenaarc236c162008-07-13 17:41:49 +00001253 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001254# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001255 preedit_get_status()
1256# else
1257 im_get_status()
1258# endif
1259 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 iid = syn_name2id((char_u *)"CursorIM");
1262 if (iid > 0)
1263 {
1264 syn_id2colors(iid, &fg, &bg);
1265 if (bg != INVALCOLOR)
1266 cbg = bg;
1267 if (fg != INVALCOLOR)
1268 cfg = fg;
1269 }
1270 }
1271 }
1272#endif
1273 }
1274
1275 /*
1276 * Get the attributes for the character under the cursor.
1277 * When no cursor color was given, use the character color.
1278 */
1279 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1280 if (attr > HL_ALL)
1281 aep = syn_gui_attr2entry(attr);
1282 if (aep != NULL)
1283 {
1284 attr = aep->ae_attr;
1285 if (cfg == INVALCOLOR)
1286 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1287 : aep->ae_u.gui.fg_color);
1288 if (cbg == INVALCOLOR)
1289 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1290 : aep->ae_u.gui.bg_color);
1291 }
1292 if (cfg == INVALCOLOR)
1293 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1294 if (cbg == INVALCOLOR)
1295 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1296
1297#ifdef FEAT_XIM
1298 if (aep != NULL)
1299 {
1300 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1301 : aep->ae_u.gui.bg_color);
1302 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1303 : aep->ae_u.gui.fg_color);
1304 if (xim_bg_color == INVALCOLOR)
1305 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1306 : gui.back_pixel;
1307 if (xim_fg_color == INVALCOLOR)
1308 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1309 : gui.norm_pixel;
1310 }
1311 else
1312 {
1313 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1314 : gui.back_pixel;
1315 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1316 : gui.norm_pixel;
1317 }
1318#endif
1319
1320 attr &= ~HL_INVERSE;
1321 if (cattr & HL_INVERSE)
1322 {
1323 cc = cbg;
1324 cbg = cfg;
1325 cfg = cc;
1326 }
1327 cattr &= ~HL_INVERSE;
1328
1329 /*
1330 * When we don't have window focus, draw a hollow cursor.
1331 */
1332 if (!gui.in_focus)
1333 {
1334 gui_mch_draw_hollow_cursor(cbg);
1335 return;
1336 }
1337
1338 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001339 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 {
1341 /*
1342 * Draw the text character with the cursor colors. Use the
1343 * character attributes plus the cursor attributes.
1344 */
1345 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001346 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1348 }
1349 else
1350 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001351#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 int col_off = FALSE;
1353#endif
1354 /*
1355 * First draw the partial cursor, then overwrite with the text
1356 * character, using a transparent background.
1357 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001358 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001361 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else
1364 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001365 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 cur_width = gui.char_width;
1367 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001368 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1369 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001371 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001372 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001374#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (CURSOR_BAR_RIGHT)
1376 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001377 // gui.col points to the left halve of the character but
1378 // the vertical line needs to be on the right halve.
1379 // A double-wide horizontal line is also drawn from the
1380 // right halve in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 col_off = TRUE;
1382 ++gui.col;
1383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001387#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (col_off)
1389 --gui.col;
1390#endif
1391
Bram Moolenaar30613902019-12-01 22:11:18 +01001392#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1394 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1395 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1396 (guicolor_T)0, (guicolor_T)0, 0);
1397#endif
1398 }
1399 gui.highlight_mask = old_hl_mask;
1400 }
1401}
1402
1403#if defined(FEAT_MENU) || defined(PROTO)
1404 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001405gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001407# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (gui.menu_is_active && gui.in_use)
1409 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1410# endif
1411}
1412#endif
1413
1414/*
1415 * Position the various GUI components (text area, menu). The vertical
1416 * scrollbars are NOT handled here. See gui_update_scrollbars().
1417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001419gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 int text_area_x;
1422 int text_area_y;
1423 int text_area_width;
1424 int text_area_height;
1425
Bram Moolenaar30613902019-12-01 22:11:18 +01001426 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 ++hold_gui_events;
1428
1429 text_area_x = 0;
1430 if (gui.which_scrollbars[SBAR_LEFT])
1431 text_area_x += gui.scrollbar_width;
1432
1433 text_area_y = 0;
1434#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1435 gui.menu_width = total_width;
1436 if (gui.menu_is_active)
1437 text_area_y += gui.menu_height;
1438#endif
1439#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1440 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1441 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1442#endif
1443
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001444# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001445 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001446 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001447 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001448#endif
1449
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001450#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
1451 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1453 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001454# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 gui_mch_set_toolbar_pos(0, text_area_y,
1456 gui.menu_width, gui.toolbar_height);
1457# endif
1458 text_area_y += gui.toolbar_height;
1459 }
1460#endif
1461
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001462# if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
1463 gui_mch_set_tabline_pos(0, text_area_y,
1464 gui.menu_width, gui.tabline_height);
1465 if (gui_has_tabline())
1466 text_area_y += gui.tabline_height;
1467#endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1470 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1471
1472 gui_mch_set_text_area_pos(text_area_x,
1473 text_area_y,
1474 text_area_width,
1475 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001476#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 + xim_get_status_area_height()
1478#endif
1479 );
1480#ifdef FEAT_MENU
1481 gui_position_menu();
1482#endif
1483 if (gui.which_scrollbars[SBAR_BOTTOM])
1484 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1485 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001486 text_area_y + text_area_height
1487 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 text_area_width,
1489 gui.scrollbar_height);
1490 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001491 gui.right_sbar_x = text_area_x + text_area_width
1492 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 --hold_gui_events;
1495}
1496
Bram Moolenaar02743632005-07-25 20:42:36 +00001497/*
1498 * Get the width of the widgets and decorations to the side of the text area.
1499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001501gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 int base_width;
1504
1505 base_width = 2 * gui.border_offset;
1506 if (gui.which_scrollbars[SBAR_LEFT])
1507 base_width += gui.scrollbar_width;
1508 if (gui.which_scrollbars[SBAR_RIGHT])
1509 base_width += gui.scrollbar_width;
1510 return base_width;
1511}
1512
Bram Moolenaar02743632005-07-25 20:42:36 +00001513/*
1514 * Get the height of the widgets and decorations above and below the text area.
1515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001517gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 int base_height;
1520
1521 base_height = 2 * gui.border_offset;
1522 if (gui.which_scrollbars[SBAR_BOTTOM])
1523 base_height += gui.scrollbar_height;
1524#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001525 // We can't take the sizes properly into account until anything is
1526 // realized. Therefore we recalculate all the values here just before
1527 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#else
1529# ifdef FEAT_MENU
1530 if (gui.menu_is_active)
1531 base_height += gui.menu_height;
1532# endif
1533# ifdef FEAT_TOOLBAR
1534 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1535# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1536 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1537# else
1538 base_height += gui.toolbar_height;
1539# endif
1540# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001541# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001542 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001543 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001544 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546# ifdef FEAT_FOOTER
1547 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1548 base_height += gui.footer_height;
1549# endif
1550# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1551 base_height += gui_mch_text_area_extra_height();
1552# endif
1553#endif
1554 return base_height;
1555}
1556
1557/*
1558 * Should be called after the GUI shell has been resized. Its arguments are
1559 * the new width and height of the shell in pixels.
1560 */
1561 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001562gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 static int busy = FALSE;
1565
Bram Moolenaar30613902019-12-01 22:11:18 +01001566 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return;
1568
1569 /*
1570 * Can't resize the screen while it is being redrawn. Remember the new
1571 * size and handle it later.
1572 */
1573 if (updating_screen || busy)
1574 {
1575 new_pixel_width = pixel_width;
1576 new_pixel_height = pixel_height;
1577 return;
1578 }
1579
1580again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001581 new_pixel_width = 0;
1582 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 busy = TRUE;
1584
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001585#ifdef FEAT_GUI_HAIKU
1586 vim_lock_screen();
1587#endif
1588
Bram Moolenaar30613902019-12-01 22:11:18 +01001589 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 out_flush();
1591
1592 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001593 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /*
1599 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1600 * at the last line here (why does it have to be one row too low?).
1601 */
1602 if (State == ASKMORE || State == CONFIRM)
1603 gui.row = gui.num_rows;
1604
Bram Moolenaar30613902019-12-01 22:11:18 +01001605 // Only comparing Rows and Columns may be sufficient, but let's stay on
1606 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1608 || gui.num_rows != Rows || gui.num_cols != Columns)
1609 shell_resized();
1610
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001611#ifdef FEAT_GUI_HAIKU
1612 vim_unlock_screen();
1613#endif
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 gui_update_scrollbars(TRUE);
1616 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001617#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 xim_set_status_area();
1619#endif
1620
1621 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001622
Bram Moolenaar30613902019-12-01 22:11:18 +01001623 // We may have been called again while redrawing the screen.
1624 // Need to do it all again with the latest size then. But only if the size
1625 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (new_pixel_height)
1627 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001628 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1629 {
1630 new_pixel_width = 0;
1631 new_pixel_height = 0;
1632 }
1633 else
1634 {
1635 pixel_width = new_pixel_width;
1636 pixel_height = new_pixel_height;
1637 goto again;
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640}
1641
1642/*
1643 * Check if gui_resize_shell() must be called.
1644 */
1645 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001646gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001649 // careful: gui_resize_shell() may postpone the resize again if we
1650 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001651 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652}
1653
1654 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001655gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 Rows = gui.num_rows;
1658 Columns = gui.num_cols;
1659 return OK;
1660}
1661
1662/*
1663 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001664 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1665 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001666 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1667 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001670gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001671 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001672 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001673 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 int base_width;
1676 int base_height;
1677 int width;
1678 int height;
1679 int min_width;
1680 int min_height;
1681 int screen_w;
1682 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001683#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001684 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001685 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001686#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001687 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
1689 if (!gui.shell_created)
1690 return;
1691
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001692#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001693 // If not setting to a user specified size and maximized, calculate the
1694 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001695 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1696 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
1698 gui_mch_newfont();
1699 return;
1700 }
1701#endif
1702
1703 base_width = gui_get_base_width();
1704 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001705 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001706 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001707 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001708
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001709 width = Columns * gui.char_width + base_width;
1710 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712 if (fit_to_display)
1713 {
1714 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001715 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
1717 Columns = (screen_w - base_width) / gui.char_width;
1718 if (Columns < MIN_COLUMNS)
1719 Columns = MIN_COLUMNS;
1720 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001721#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001722 ++did_adjust;
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001725 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
1727 Rows = (screen_h - base_height) / gui.char_height;
1728 check_shellsize();
1729 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001730#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001731 ++did_adjust;
1732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001734#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001735 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1736 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001737 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001738 un_maximize = FALSE;
1739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001741 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 gui.num_cols = Columns;
1743 gui.num_rows = Rows;
1744
1745 min_width = base_width + MIN_COLUMNS * gui.char_width;
1746 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001747 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001748
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001749#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001750 if (un_maximize)
1751 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001752 // If the window size is smaller than the screen unmaximize the
1753 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001754 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1755 if ((width + gui.char_width < screen_w
1756 || height + gui.char_height * 2 < screen_h)
1757 && gui_mch_maximized())
1758 gui_mch_unmaximize();
1759 }
1760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001763 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001765 if (fit_to_display && x >= 0 && y >= 0)
1766 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001767 // Some window managers put the Vim window left of/above the screen.
1768 // Only change the position if it wasn't already negative before
1769 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 gui_mch_update();
1771 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1772 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1773 }
1774
1775 gui_position_components(width);
1776 gui_update_scrollbars(TRUE);
1777 gui_reset_scroll_region();
1778}
1779
1780/*
1781 * Called when Rows and/or Columns has changed.
1782 */
1783 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001784gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 gui_reset_scroll_region();
1787}
1788
1789/*
1790 * Make scroll region cover whole screen.
1791 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001792 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001793gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 gui.scroll_region_top = 0;
1796 gui.scroll_region_bot = gui.num_rows - 1;
1797 gui.scroll_region_left = 0;
1798 gui.scroll_region_right = gui.num_cols - 1;
1799}
1800
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001801 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001802gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
Bram Moolenaar30613902019-12-01 22:11:18 +01001804 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001806 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 gui.highlight_mask |= mask;
1808}
1809
1810 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001811gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
Bram Moolenaar30613902019-12-01 22:11:18 +01001813 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001815 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 gui.highlight_mask &= ~mask;
1817}
1818
1819/*
1820 * Clear a rectangular region of the screen from text pos (row1, col1) to
1821 * (row2, col2) inclusive.
1822 */
1823 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001824gui_clear_block(
1825 int row1,
1826 int col1,
1827 int row2,
1828 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
Bram Moolenaar30613902019-12-01 22:11:18 +01001830 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 clip_may_clear_selection(row1, row2);
1832
1833 gui_mch_clear_block(row1, col1, row2, col2);
1834
Bram Moolenaar30613902019-12-01 22:11:18 +01001835 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1837 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1838 gui.cursor_is_valid = FALSE;
1839}
1840
1841/*
1842 * Write code to update the cursor later. This avoids the need to flush the
1843 * output buffer before calling gui_update_cursor().
1844 */
1845 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001846gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001848 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849}
1850
1851 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001852gui_write(
1853 char_u *s,
1854 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 char_u *p;
1857 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001858 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int force_scrollbar = FALSE;
1860 static win_T *old_curwin = NULL;
1861
Bram Moolenaar30613902019-12-01 22:11:18 +01001862// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863#ifdef DEBUG_GUI_WRITE
1864 {
1865 int i;
1866 char_u *str;
1867
1868 printf("gui_write(%d):\n ", len);
1869 for (i = 0; i < len; i++)
1870 if (s[i] == ESC)
1871 {
1872 if (i != 0)
1873 printf("\n ");
1874 printf("<ESC>");
1875 }
1876 else
1877 {
1878 str = transchar_byte(s[i]);
1879 if (str[0] && str[1])
1880 printf("<%s>", (char *)str);
1881 else
1882 printf("%s", (char *)str);
1883 }
1884 printf("\n");
1885 }
1886#endif
1887 while (len)
1888 {
1889 if (s[0] == ESC && s[1] == '|')
1890 {
1891 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001892 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
1894 arg1 = getdigits(&p);
1895 if (p > s + len)
1896 break;
1897 if (*p == ';')
1898 {
1899 ++p;
1900 arg2 = getdigits(&p);
1901 if (p > s + len)
1902 break;
1903 }
1904 }
1905 switch (*p)
1906 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001907 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 clip_scroll_selection(9999);
1909 gui_mch_clear_all();
1910 gui.cursor_is_valid = FALSE;
1911 force_scrollbar = TRUE;
1912 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001913 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 gui_set_cursor(arg1, arg2);
1915 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001916 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 force_cursor = TRUE;
1918 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001919 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (arg1 < arg2)
1921 {
1922 gui.scroll_region_top = arg1;
1923 gui.scroll_region_bot = arg2;
1924 }
1925 else
1926 {
1927 gui.scroll_region_top = arg2;
1928 gui.scroll_region_bot = arg1;
1929 }
1930 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001931 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (arg1 < arg2)
1933 {
1934 gui.scroll_region_left = arg1;
1935 gui.scroll_region_right = arg2;
1936 }
1937 else
1938 {
1939 gui.scroll_region_left = arg2;
1940 gui.scroll_region_right = arg1;
1941 }
1942 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001943 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 gui_delete_lines(gui.row, 1);
1945 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001946 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 gui_delete_lines(gui.row, arg1);
1948 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001949 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 gui_insert_lines(gui.row, 1);
1951 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001952 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 gui_insert_lines(gui.row, arg1);
1954 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001955 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 gui_clear_block(gui.row, gui.col, gui.row,
1957 (int)Columns - 1);
1958 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001959 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 gui_start_highlight(arg1);
1961 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001962 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 gui_stop_highlight(arg1);
1964 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001965 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1967 break;
1968 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001969 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 break;
1971 }
1972 len -= (int)(++p - s);
1973 s = p;
1974 }
1975 else if (
1976#ifdef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +01001977 CtrlChar(s[0]) != 0 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#else
Bram Moolenaar30613902019-12-01 22:11:18 +01001979 s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980#endif
1981#ifdef FEAT_SIGN_ICONS
1982 && s[0] != SIGN_BYTE
1983# ifdef FEAT_NETBEANS_INTG
1984 && s[0] != MULTISIGN_BYTE
1985# endif
1986#endif
1987 )
1988 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001989 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 gui.col = 0;
1992 if (gui.row < gui.scroll_region_bot)
1993 gui.row++;
1994 else
1995 gui_delete_lines(gui.scroll_region_top, 1);
1996 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001997 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 gui.col = 0;
2000 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 if (gui.col)
2004 --gui.col;
2005 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002006 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 ++gui.col;
2009 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002010 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 gui_mch_beep();
2013 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002014 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar30613902019-12-01 22:11:18 +01002016 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 ++s;
2018 }
2019 else
2020 {
2021 p = s;
2022 while (len > 0 && (
2023#ifdef EBCDIC
2024 CtrlChar(*p) == 0
2025#else
2026 *p >= 0x20
2027#endif
2028#ifdef FEAT_SIGN_ICONS
2029 || *p == SIGN_BYTE
2030# ifdef FEAT_NETBEANS_INTG
2031 || *p == MULTISIGN_BYTE
2032# endif
2033#endif
2034 ))
2035 {
2036 len--;
2037 p++;
2038 }
2039 gui_outstr(s, (int)(p - s));
2040 s = p;
2041 }
2042 }
2043
Bram Moolenaar30613902019-12-01 22:11:18 +01002044 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2045 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (force_cursor)
2047 gui_update_cursor(TRUE, TRUE);
2048
Bram Moolenaar30613902019-12-01 22:11:18 +01002049 // When switching to another window the dragging must have stopped.
2050 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (old_curwin != curwin)
2052 gui.dragged_sb = SBAR_NONE;
2053
Bram Moolenaar30613902019-12-01 22:11:18 +01002054 // Update the scrollbars after clearing the screen or when switched
2055 // to another window.
2056 // Update the horizontal scrollbar always, it's difficult to check all
2057 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (force_scrollbar || old_curwin != curwin)
2059 gui_update_scrollbars(force_scrollbar);
2060 else
2061 gui_update_horiz_scrollbar(FALSE);
2062 old_curwin = curwin;
2063
2064 /*
2065 * We need to make sure this is cleared since Athena doesn't tell us when
2066 * he is done dragging. Do the same for GTK.
2067 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002068#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 gui.dragged_sb = SBAR_NONE;
2070#endif
2071
Bram Moolenaar30613902019-12-01 22:11:18 +01002072 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073}
2074
2075/*
2076 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2077 * produces wrong results. Call gui_dont_update_cursor() before that code and
2078 * gui_can_update_cursor() afterwards.
2079 */
2080 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002081gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 if (gui.in_use)
2084 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002085 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02002086 if (undraw)
2087 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 can_update_cursor = FALSE;
2089 }
2090}
2091
2092 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002093gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
2095 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002096 // No need to update the cursor right now, there is always more output
2097 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098}
2099
Bram Moolenaara338adc2018-01-31 20:51:47 +01002100/*
2101 * Disable issuing gui_mch_flush().
2102 */
2103 void
2104gui_disable_flush(void)
2105{
2106 ++disable_flush;
2107}
2108
2109/*
2110 * Enable issuing gui_mch_flush().
2111 */
2112 void
2113gui_enable_flush(void)
2114{
2115 --disable_flush;
2116}
2117
2118/*
2119 * Issue gui_mch_flush() if it is not disabled.
2120 */
2121 void
2122gui_may_flush(void)
2123{
2124 if (disable_flush == 0)
2125 gui_mch_flush();
2126}
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002129gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
2131 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 if (len == 0)
2135 return;
2136
2137 if (len < 0)
2138 len = (int)STRLEN(s);
2139
2140 while (len > 0)
2141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (has_mbyte)
2143 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002144 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 cells = 0;
2146 for (this_len = 0; this_len < len; )
2147 {
2148 cells += (*mb_ptr2cells)(s + this_len);
2149 if (gui.col + cells > Columns)
2150 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002151 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002154 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (gui.col + len > Columns)
2158 this_len = Columns - gui.col;
2159 else
2160 this_len = len;
2161
2162 (void)gui_outstr_nowrap(s, this_len,
2163 0, (guicolor_T)0, (guicolor_T)0, 0);
2164 s += this_len;
2165 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002166 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (len > 0 && gui.col < Columns)
2168 (void)gui_outstr_nowrap((char_u *)" ", 1,
2169 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002170 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (gui.col >= Columns)
2172 {
2173 gui.col = 0;
2174 gui.row++;
2175 }
2176 }
2177}
2178
2179/*
2180 * Output one character (may be one or two display cells).
2181 * Caller must check for valid "off".
2182 * Returns FAIL or OK, just like gui_outstr_nowrap().
2183 */
2184 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002185gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002186 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002187 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002188 guicolor_T fg, // colors for cursor
2189 guicolor_T bg, // colors for cursor
2190 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 char_u buf[MB_MAXBYTES + 1];
2193
Bram Moolenaar30613902019-12-01 22:11:18 +01002194 // Don't draw right halve of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (enc_utf8 && ScreenLines[off] == 0)
2196 return OK;
2197
2198 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002199 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2201 flags, fg, bg, back);
2202
2203 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2204 {
2205 buf[0] = ScreenLines[off];
2206 buf[1] = ScreenLines2[off];
2207 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2208 }
2209
Bram Moolenaar30613902019-12-01 22:11:18 +01002210 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002212 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214}
2215
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002216#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Output the string at the given screen position. This is used in place
2219 * of gui_screenchar() where possible because Pango needs as much context
2220 * as possible to work nicely. It's a lot faster as well.
2221 */
2222 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002223gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002224 int off, // Offset from start of screen
2225 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002226 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002227 guicolor_T fg, // colors for cursor
2228 guicolor_T bg, // colors for cursor
2229 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 char_u *buf;
2232 int outlen = 0;
2233 int i;
2234 int retval;
2235
Bram Moolenaar30613902019-12-01 22:11:18 +01002236 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 return OK;
2238
2239 if (enc_utf8)
2240 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002241 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002243 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
2245 for (i = off; i < off + len; ++i)
2246 {
2247 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002248 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 if (ScreenLinesUC[i] == 0)
2251 buf[outlen++] = ScreenLines[i];
2252 else
2253 outlen += utfc_char2bytes(i, buf + outlen);
2254 }
2255
Bram Moolenaar30613902019-12-01 22:11:18 +01002256 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2258 vim_free(buf);
2259
2260 return retval;
2261 }
2262 else if (enc_dbcs == DBCS_JPNU)
2263 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002264 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002266 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268 for (i = off; i < off + len; ++i)
2269 {
2270 buf[outlen++] = ScreenLines[i];
2271
Bram Moolenaar30613902019-12-01 22:11:18 +01002272 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (ScreenLines[i] == 0x8e)
2274 buf[outlen++] = ScreenLines2[i];
2275 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2276 buf[outlen++] = ScreenLines[++i];
2277 }
2278
Bram Moolenaar30613902019-12-01 22:11:18 +01002279 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2281 vim_free(buf);
2282
2283 return retval;
2284 }
2285 else
2286 {
2287 return gui_outstr_nowrap(&ScreenLines[off], len,
2288 flags, fg, bg, back);
2289 }
2290}
Bram Moolenaar30613902019-12-01 22:11:18 +01002291#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
2293/*
2294 * Output the given string at the current cursor position. If the string is
2295 * too long to fit on the line, then it is truncated.
2296 * "flags":
2297 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2298 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002299 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * background.
2301 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2302 * it.
2303 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2304 * FAIL (the caller should start drawing "back" chars back).
2305 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002306 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002307gui_outstr_nowrap(
2308 char_u *s,
2309 int len,
2310 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002311 guicolor_T fg, // colors for cursor
2312 guicolor_T bg, // colors for cursor
2313 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315 long_u highlight_mask;
2316 long_u hl_mask_todo;
2317 guicolor_T fg_color;
2318 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002319 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002322 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323# ifdef FEAT_XFONTSET
2324 GuiFontset fontset = NOFONTSET;
2325# endif
2326#endif
2327 attrentry_T *aep = NULL;
2328 int draw_flags;
2329 int col = gui.col;
2330#ifdef FEAT_SIGN_ICONS
2331 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002332 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002333 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334# ifdef FEAT_NETBEANS_INTG
2335 int multi_sign = FALSE;
2336# endif
2337#endif
2338
2339 if (len < 0)
2340 len = (int)STRLEN(s);
2341 if (len == 0)
2342 return OK;
2343
2344#ifdef FEAT_SIGN_ICONS
2345 if (*s == SIGN_BYTE
2346# ifdef FEAT_NETBEANS_INTG
2347 || *s == MULTISIGN_BYTE
2348# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002349 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351# ifdef FEAT_NETBEANS_INTG
2352 if (*s == MULTISIGN_BYTE)
2353 multi_sign = TRUE;
2354# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002355 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002356 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2357 (curwin->w_p_nu || curwin->w_p_rnu))
2358 {
2359 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2360 s = extra;
2361 }
2362 else
2363 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (len == 1 && col > 0)
2365 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002366 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002367 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002368 // right align sign icon in the number column
2369 signcol = col + len - 3;
2370 else
2371 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 draw_sign = TRUE;
2373 highlight_mask = 0;
2374 }
2375 else
2376#endif
2377 if (gui.highlight_mask > HL_ALL)
2378 {
2379 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002380 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 highlight_mask = 0;
2382 else
2383 highlight_mask = aep->ae_attr;
2384 }
2385 else
2386 highlight_mask = gui.highlight_mask;
2387 hl_mask_todo = highlight_mask;
2388
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002389#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002390 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2392 font = aep->ae_u.gui.font;
2393# ifdef FEAT_XFONTSET
2394 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2395 fontset = aep->ae_u.gui.fontset;
2396# endif
2397 else
2398 {
2399# ifdef FEAT_XFONTSET
2400 if (gui.fontset != NOFONTSET)
2401 fontset = gui.fontset;
2402 else
2403# endif
2404 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2405 {
2406 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2407 {
2408 font = gui.boldital_font;
2409 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2410 }
2411 else if (gui.bold_font != NOFONT)
2412 {
2413 font = gui.bold_font;
2414 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2415 }
2416 else
2417 font = gui.norm_font;
2418 }
2419 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2420 {
2421 font = gui.ital_font;
2422 hl_mask_todo &= ~HL_ITALIC;
2423 }
2424 else
2425 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002426
Bram Moolenaardb250522013-06-17 22:43:25 +02002427 /*
2428 * Choose correct wide_font by font. wide_font should be set with font
2429 * at same time in above block. But it will make many "ifdef" nasty
2430 * blocks. So we do it here.
2431 */
2432 if (font == gui.boldital_font && gui.wide_boldital_font)
2433 wide_font = gui.wide_boldital_font;
2434 else if (font == gui.bold_font && gui.wide_bold_font)
2435 wide_font = gui.wide_bold_font;
2436 else if (font == gui.ital_font && gui.wide_ital_font)
2437 wide_font = gui.wide_ital_font;
2438 else if (font == gui.norm_font && gui.wide_font)
2439 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441# ifdef FEAT_XFONTSET
2442 if (fontset != NOFONTSET)
2443 gui_mch_set_fontset(fontset);
2444 else
2445# endif
2446 gui_mch_set_font(font);
2447#endif
2448
2449 draw_flags = 0;
2450
Bram Moolenaar30613902019-12-01 22:11:18 +01002451 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 bg_color = gui.back_pixel;
2453 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2454 {
2455 draw_flags |= DRAW_CURSOR;
2456 fg_color = fg;
2457 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002458 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460 else if (aep != NULL)
2461 {
2462 fg_color = aep->ae_u.gui.fg_color;
2463 if (fg_color == INVALCOLOR)
2464 fg_color = gui.norm_pixel;
2465 bg_color = aep->ae_u.gui.bg_color;
2466 if (bg_color == INVALCOLOR)
2467 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002468 sp_color = aep->ae_u.gui.sp_color;
2469 if (sp_color == INVALCOLOR)
2470 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002475 sp_color = fg_color;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 gui_mch_set_fg_color(bg_color);
2481 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483 else
2484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 gui_mch_set_fg_color(fg_color);
2486 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002488 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
Bram Moolenaar30613902019-12-01 22:11:18 +01002490 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (!(flags & GUI_MON_NOCLEAR))
2492 clip_may_clear_selection(gui.row, gui.row);
2493
2494
Bram Moolenaar30613902019-12-01 22:11:18 +01002495 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2497 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499 /*
2500 * When drawing bold or italic characters the spill-over from the left
2501 * neighbor may be destroyed. Let the caller backup to start redrawing
2502 * just after a blank.
2503 */
2504 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2505 return FAIL;
2506
Bram Moolenaare60acc12011-05-10 16:41:25 +02002507#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002508 // If there's no italic font, then fake it.
2509 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (hl_mask_todo & HL_ITALIC)
2511 draw_flags |= DRAW_ITALIC;
2512
Bram Moolenaar30613902019-12-01 22:11:18 +01002513 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (hl_mask_todo & HL_UNDERLINE)
2515 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002518 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002519 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 draw_flags |= DRAW_UNDERL;
2521#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002522 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002523 if (hl_mask_todo & HL_UNDERCURL)
2524 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar30613902019-12-01 22:11:18 +01002526 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002527 if (hl_mask_todo & HL_STRIKETHROUGH)
2528 draw_flags |= DRAW_STRIKE;
2529
Bram Moolenaar30613902019-12-01 22:11:18 +01002530 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (flags & GUI_MON_TRS_CURSOR)
2532 draw_flags |= DRAW_TRANSP;
2533
2534 /*
2535 * Draw the text.
2536 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002537#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002538 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2540#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (enc_utf8)
2542 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002543 int start; // index of bytes to be drawn
2544 int cells; // cellwidth of bytes to be drawn
2545 int thislen; // length of bytes to be drawn
2546 int cn; // cellwidth of current char
2547 int i; // index of current char
2548 int c; // current char value
2549 int cl; // byte length of current char
2550 int comping; // current char is composing
2551 int scol = col; // screen column
2552 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002553 int prev_wide = FALSE;
2554 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002555# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002556 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002557# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002558 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar30613902019-12-01 22:11:18 +01002561 // Break the string at a composing character, it has to be drawn on
2562 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 start = 0;
2564 cells = 0;
2565 for (i = 0; i < len; i += cl)
2566 {
2567 c = utf_ptr2char(s + i);
2568 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002570 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002572 if (!comping || sep_comp)
2573 {
2574 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002575# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002576 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002577# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002578 && wide_font != NOFONT)
2579 curr_wide = TRUE;
2580 else
2581 curr_wide = FALSE;
2582 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002583 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002584 if (cl == 0) // hit end of string
2585 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar45933962013-01-23 17:43:57 +01002587 wide_changed = curr_wide != prev_wide;
2588
Bram Moolenaar30613902019-12-01 22:11:18 +01002589 // Print the string so far if it's the last character or there is
2590 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002591 if (i + cl >= len || (comping && sep_comp && i > start)
2592 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002593# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002595# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002596 // No fontset: At least draw char after wide char at
2597 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002600 )
2601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 )
2603 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002604 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 thislen = i - start;
2606 else
2607 thislen = i - start + cl;
2608 if (thislen > 0)
2609 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002610 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002611 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2613 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002614 if (prev_wide)
2615 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 start += thislen;
2617 }
2618 scol += cells;
2619 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002620 // Adjust to not draw a character which width is changed
2621 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002622 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002624 scol -= cn;
2625 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
Bram Moolenaar13505972019-01-24 15:04:48 +01002628# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002629 // No fontset: draw a space to fill the gap after a wide char
2630 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002632# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002634# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002635 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2637 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002640 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002641 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002643# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002644 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002645 gui_mch_draw_string(gui.row, scol, s + i, cl,
2646 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002647# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002648 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2649 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 start = i + cl;
2652 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002653 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002655 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 len = scol - col;
2657 }
2658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
2660 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (enc_dbcs == DBCS_JPNU)
2662 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002663 // Get the length in display cells, this can be different from the
2664 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002665 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002668#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2671 gui.col = col + len;
2672
Bram Moolenaar30613902019-12-01 22:11:18 +01002673 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (flags & GUI_MON_NOCLEAR)
2675 clip_may_redraw_selection(gui.row, col, len);
2676
2677 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2678 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002679 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (gui.cursor_row == gui.row
2681 && gui.cursor_col >= col
2682 && gui.cursor_col < col + len)
2683 gui.cursor_is_valid = FALSE;
2684 }
2685
2686#ifdef FEAT_SIGN_ICONS
2687 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002688 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002689 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002690# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002691 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (multi_sign)
2693 netbeans_draw_multisign_indicator(gui.row);
2694# endif
2695#endif
2696
2697 return OK;
2698}
2699
2700/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002701 * Undraw the cursor. This actually redraws the character at the cursor
2702 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 */
2704 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002705gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 if (gui.cursor_is_valid)
2708 {
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002709 // Always redraw the character just before if there is one, because
2710 // with some fonts and characters there can be a one pixel overlap.
2711 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2712 int endcol = gui.cursor_col;
2713
2714#ifdef FEAT_GUI_GTK
2715 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
2716#endif
2717 gui_redraw_block(gui.cursor_row, startcol,
2718 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002719
Bram Moolenaar54612582019-11-21 17:13:31 +01002720 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2721 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 gui.cursor_is_valid = FALSE;
2723 }
2724}
2725
2726 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002727gui_redraw(
2728 int x,
2729 int y,
2730 int w,
2731 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
2733 int row1, col1, row2, col2;
2734
2735 row1 = Y_2_ROW(y);
2736 col1 = X_2_COL(x);
2737 row2 = Y_2_ROW(y + h - 1);
2738 col2 = X_2_COL(x + w - 1);
2739
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002740 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 /*
2743 * We may need to redraw the cursor, but don't take it upon us to change
2744 * its location after a scroll.
2745 * (maybe be more strict even and test col too?)
2746 * These things may be outside the update/clipping region and reality may
2747 * not reflect Vims internal ideas if these operations are clipped away.
2748 */
2749 if (gui.row == gui.cursor_row)
2750 gui_update_cursor(TRUE, TRUE);
2751}
2752
2753/*
2754 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2755 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002757 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002758gui_redraw_block(
2759 int row1,
2760 int col1,
2761 int row2,
2762 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002763 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 int old_row, old_col;
2766 long_u old_hl_mask;
2767 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002768 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 int idx, len;
2770 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaar30613902019-12-01 22:11:18 +01002773 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002775 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
Bram Moolenaar30613902019-12-01 22:11:18 +01002777 // Don't try to draw outside the shell!
2778 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 col1 = check_col(col1);
2780 col2 = check_col(col2);
2781 row1 = check_row(row1);
2782 row2 = check_row(row2);
2783
Bram Moolenaar30613902019-12-01 22:11:18 +01002784 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 old_row = gui.row;
2786 old_col = gui.col;
2787 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 orig_col1 = col1;
2789 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 for (gui.row = row1; gui.row <= row2; gui.row++)
2792 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002793 // When only half of a double-wide character is in the block, include
2794 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 col1 = orig_col1;
2796 col2 = orig_col2;
2797 off = LineOffset[gui.row];
2798 if (enc_dbcs != 0)
2799 {
2800 if (col1 > 0)
2801 col1 -= dbcs_screen_head_off(ScreenLines + off,
2802 ScreenLines + off + col1);
2803 col2 += dbcs_screen_tail_off(ScreenLines + off,
2804 ScreenLines + off + col2);
2805 }
2806 else if (enc_utf8)
2807 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002808 if (ScreenLines[off + col1] == 0)
2809 {
2810 if (col1 > 0)
2811 --col1;
2812 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002813 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002814 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002815 // Make this IEMSGN when it no longer breaks Travis CI.
2816 vim_snprintf((char *)IObuff, IOSIZE,
2817 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2818 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002819 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002820 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002821 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002822#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2824 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 gui.col = col1;
2828 off = LineOffset[gui.row] + gui.col;
2829 len = col2 - col1 + 1;
2830
Bram Moolenaar30613902019-12-01 22:11:18 +01002831 // Find how many chars back this highlighting starts, or where a space
2832 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 for (back = 0; back < col1; ++back)
2834 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2835 || ScreenLines[off - 1 - back] == ' ')
2836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaar30613902019-12-01 22:11:18 +01002838 // Break it up in strings of characters with the same attributes.
2839 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 while (len > 0)
2841 {
2842 first_attr = ScreenAttrs[off];
2843 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002844#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (enc_utf8 && ScreenLinesUC[off] != 0)
2846 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002847 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 nback = gui_screenchar(off, flags,
2849 (guicolor_T)0, (guicolor_T)0, back);
2850 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2851 idx = 2;
2852 else
2853 idx = 1;
2854 }
2855 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2856 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002857 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 nback = gui_screenchar(off, flags,
2859 (guicolor_T)0, (guicolor_T)0, back);
2860 idx = 1;
2861 }
2862 else
2863#endif
2864 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002865#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 for (idx = 0; idx < len; ++idx)
2867 {
2868 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002869 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (ScreenAttrs[off + idx] != first_attr)
2871 break;
2872 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002873 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 nback = gui_screenstr(off, idx, flags,
2875 (guicolor_T)0, (guicolor_T)0, back);
2876#else
2877 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2878 idx++)
2879 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002880 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2882 break;
2883 if (enc_dbcs == DBCS_JPNU)
2884 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002885 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (ScreenLines[off + idx] == 0x8e)
2887 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002888 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002890 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2894 (guicolor_T)0, (guicolor_T)0, back);
2895#endif
2896 }
2897 if (nback == FAIL)
2898 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002899 // Must back up to start drawing where a bold or italic word
2900 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 off -= back;
2902 len += back;
2903 gui.col -= back;
2904 }
2905 else
2906 {
2907 off += idx;
2908 len -= idx;
2909 }
2910 back = 0;
2911 }
2912 }
2913
Bram Moolenaar30613902019-12-01 22:11:18 +01002914 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 gui.row = old_row;
2916 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002917 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
2920 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002921gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 if (count <= 0)
2924 return;
2925
2926 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002927 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 gui_clear_block(row, gui.scroll_region_left,
2929 gui.scroll_region_bot, gui.scroll_region_right);
2930 else
2931 {
2932 gui_mch_delete_lines(row, count);
2933
Bram Moolenaar30613902019-12-01 22:11:18 +01002934 // If the cursor was in the deleted lines it's now gone. If the
2935 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (gui.cursor_row >= row
2937 && gui.cursor_col >= gui.scroll_region_left
2938 && gui.cursor_col <= gui.scroll_region_right)
2939 {
2940 if (gui.cursor_row < row + count)
2941 gui.cursor_is_valid = FALSE;
2942 else if (gui.cursor_row <= gui.scroll_region_bot)
2943 gui.cursor_row -= count;
2944 }
2945 }
2946}
2947
2948 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002949gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 if (count <= 0)
2952 return;
2953
2954 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002955 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 gui_clear_block(row, gui.scroll_region_left,
2957 gui.scroll_region_bot, gui.scroll_region_right);
2958 else
2959 {
2960 gui_mch_insert_lines(row, count);
2961
2962 if (gui.cursor_row >= gui.row
2963 && gui.cursor_col >= gui.scroll_region_left
2964 && gui.cursor_col <= gui.scroll_region_right)
2965 {
2966 if (gui.cursor_row <= gui.scroll_region_bot - count)
2967 gui.cursor_row += count;
2968 else if (gui.cursor_row <= gui.scroll_region_bot)
2969 gui.cursor_is_valid = FALSE;
2970 }
2971 }
2972}
2973
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002974#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002975/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002976 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2977 */
2978 static int
2979gui_wait_for_chars_3(
2980 long wtime,
2981 int *interrupted UNUSED,
2982 int ignore_input UNUSED)
2983{
2984 return gui_mch_wait_for_chars(wtime);
2985}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002986#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002987
2988/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002989 * Returns OK if a character was found to be available within the given time,
2990 * or FAIL otherwise.
2991 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002992 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002993gui_wait_for_chars_or_timer(
2994 long wtime,
2995 int *interrupted UNUSED,
2996 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002997{
2998#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002999 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
3000 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01003001#else
3002 return gui_mch_wait_for_chars(wtime);
3003#endif
3004}
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006/*
3007 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003008 * "wtime" == -1 Wait forever.
3009 * "wtime" == 0 Don't wait.
3010 * "wtime" > 0 Wait wtime milliseconds for a character.
3011 *
3012 * Returns the number of characters read or zero when timed out or interrupted.
3013 * "buf" may be NULL, in which case a non-zero number is returned if characters
3014 * are available.
3015 */
3016 static int
3017gui_wait_for_chars_buf(
3018 char_u *buf,
3019 int maxlen,
3020 long wtime, // don't use "time", MIPS cannot handle it
3021 int tb_change_cnt)
3022{
3023 int retval;
3024
3025#ifdef FEAT_MENU
3026 // If we're going to wait a bit, update the menus and mouse shape for the
3027 // current State.
3028 if (wtime != 0)
3029 gui_update_menus(0);
3030#endif
3031
3032 gui_mch_update();
3033 if (input_available()) // Got char, return immediately
3034 {
3035 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3036 return read_from_input_buf(buf, (long)maxlen);
3037 return 0;
3038 }
3039 if (wtime == 0) // Don't wait for char
3040 return FAIL;
3041
3042 // Before waiting, flush any output to the screen.
3043 gui_mch_flush();
3044
3045 // Blink while waiting for a character.
3046 gui_mch_start_blink();
3047
3048 // Common function to loop until "wtime" is met, while handling timers and
3049 // other callbacks.
3050 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3051 gui_wait_for_chars_or_timer, NULL);
3052
3053 gui_mch_stop_blink(TRUE);
3054
3055 return retval;
3056}
3057
3058/*
3059 * Wait for a character from the keyboard without actually reading it.
3060 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 * wtime == -1 Wait forever.
3062 * wtime == 0 Don't wait.
3063 * wtime > 0 Wait wtime milliseconds for a character.
3064 * Returns OK if a character was found to be available within the given time,
3065 * or FAIL otherwise.
3066 */
3067 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003068gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003070 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
3073/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003074 * Equivalent of mch_inchar() for the GUI.
3075 */
3076 int
3077gui_inchar(
3078 char_u *buf,
3079 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003080 long wtime, // milli seconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003081 int tb_change_cnt)
3082{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003083 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003084}
3085
3086/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003087 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 */
3089 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003090fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 p[0] = (char_u)(col / 128 + ' ' + 1);
3093 p[1] = (char_u)(col % 128 + ' ' + 1);
3094 p[2] = (char_u)(row / 128 + ' ' + 1);
3095 p[3] = (char_u)(row % 128 + ' ' + 1);
3096}
3097
3098/*
3099 * Generic mouse support function. Add a mouse event to the input buffer with
3100 * the given properties.
3101 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3102 * MOUSE_X1, MOUSE_X2
3103 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003104 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3105 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * x, y --- Coordinates of mouse in pixels.
3107 * repeated_click --- TRUE if this click comes only a short time after a
3108 * previous click.
3109 * modifiers --- Bit field which may be any of the following modifiers
3110 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3111 * This function will ignore drag events where the mouse has not moved to a new
3112 * character.
3113 */
3114 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003115gui_send_mouse_event(
3116 int button,
3117 int x,
3118 int y,
3119 int repeated_click,
3120 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 static int prev_row = 0, prev_col = 0;
3123 static int prev_button = -1;
3124 static int num_clicks = 1;
3125 char_u string[10];
3126 enum key_extra button_char;
3127 int row, col;
3128#ifdef FEAT_CLIPBOARD
3129 int checkfor;
3130 int did_clip = FALSE;
3131#endif
3132
3133 /*
3134 * Scrolling may happen at any time, also while a selection is present.
3135 */
3136 switch (button)
3137 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003138 case MOUSE_MOVE:
3139 button_char = KE_MOUSEMOVE_XY;
3140 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 case MOUSE_X1:
3142 button_char = KE_X1MOUSE;
3143 goto button_set;
3144 case MOUSE_X2:
3145 button_char = KE_X2MOUSE;
3146 goto button_set;
3147 case MOUSE_4:
3148 button_char = KE_MOUSEDOWN;
3149 goto button_set;
3150 case MOUSE_5:
3151 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003152 goto button_set;
3153 case MOUSE_6:
3154 button_char = KE_MOUSELEFT;
3155 goto button_set;
3156 case MOUSE_7:
3157 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158button_set:
3159 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003160 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (hold_gui_events)
3162 return;
3163
3164 string[3] = CSI;
3165 string[4] = KS_EXTRA;
3166 string[5] = (int)button_char;
3167
Bram Moolenaar30613902019-12-01 22:11:18 +01003168 // Pass the pointer coordinates of the scroll event so that we
3169 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 row = gui_xy2colrow(x, y, &col);
3171 string[6] = (char_u)(col / 128 + ' ' + 1);
3172 string[7] = (char_u)(col % 128 + ' ' + 1);
3173 string[8] = (char_u)(row / 128 + ' ' + 1);
3174 string[9] = (char_u)(row % 128 + ' ' + 1);
3175
3176 if (modifiers == 0)
3177 add_to_input_buf(string + 3, 7);
3178 else
3179 {
3180 string[0] = CSI;
3181 string[1] = KS_MODIFIER;
3182 string[2] = 0;
3183 if (modifiers & MOUSE_SHIFT)
3184 string[2] |= MOD_MASK_SHIFT;
3185 if (modifiers & MOUSE_CTRL)
3186 string[2] |= MOD_MASK_CTRL;
3187 if (modifiers & MOUSE_ALT)
3188 string[2] |= MOD_MASK_ALT;
3189 add_to_input_buf(string, 10);
3190 }
3191 return;
3192 }
3193 }
3194
3195#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003196 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (clip_star.state == SELECT_IN_PROGRESS)
3198 {
3199 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003200
3201 // A release event may still need to be sent if the position is equal.
3202 row = gui_xy2colrow(x, y, &col);
3203 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3204 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206
Bram Moolenaar30613902019-12-01 22:11:18 +01003207 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 switch (get_real_state())
3209 {
3210 case NORMAL_BUSY:
3211 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003212# ifdef FEAT_TERMINAL
3213 case TERMINAL:
3214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 case NORMAL: checkfor = MOUSE_NORMAL; break;
3216 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003217 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 case REPLACE:
3219 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 case VREPLACE:
3221 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 case INSERT:
3223 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3224 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003225 case HITRETURN: // At the more- and hit-enter prompt pass the
3226 // mouse event for a click on or below the
3227 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (Y_2_ROW(y) >= msg_row)
3229 checkfor = MOUSE_NORMAL;
3230 else
3231 checkfor = MOUSE_RETURN;
3232 break;
3233
3234 /*
3235 * On the command line, use the clipboard selection on all lines
3236 * but the command line. But not when pasting.
3237 */
3238 case CMDLINE:
3239 case CMDLINE+LANGMAP:
3240 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3241 checkfor = MOUSE_NONE;
3242 else
3243 checkfor = MOUSE_COMMAND;
3244 break;
3245
3246 default:
3247 checkfor = MOUSE_NONE;
3248 break;
3249 };
3250
3251 /*
3252 * Allow clipboard selection of text on the command line in "normal"
3253 * modes. Don't do this when dragging the status line, or extending a
3254 * Visual selection.
3255 */
3256 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003257 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 && button != MOUSE_DRAG
3259# ifdef FEAT_MOUSESHAPE
3260 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262# endif
3263 )
3264 checkfor = MOUSE_NONE;
3265
3266 /*
3267 * Use modeless selection when holding CTRL and SHIFT pressed.
3268 */
3269 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3270 checkfor = MOUSE_NONEF;
3271
3272 /*
3273 * In Ex mode, always use modeless selection.
3274 */
3275 if (exmode_active)
3276 checkfor = MOUSE_NONE;
3277
3278 /*
3279 * If the mouse settings say to not use the mouse, use the modeless
3280 * selection. But if Visual is active, assume that only the Visual area
3281 * will be selected.
3282 * Exception: On the command line, both the selection is used and a mouse
3283 * key is send.
3284 */
3285 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3286 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003287 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3289 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 /*
3292 * When 'mousemodel' is "popup", shift-left is translated to right.
3293 * But not when also using Ctrl.
3294 */
3295 if (mouse_model_popup() && button == MOUSE_LEFT
3296 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3297 {
3298 button = MOUSE_RIGHT;
3299 modifiers &= ~ MOUSE_SHIFT;
3300 }
3301
Bram Moolenaar30613902019-12-01 22:11:18 +01003302 // If the selection is done, allow the right button to extend it.
3303 // If the selection is cleared, allow the right button to start it
3304 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (button == MOUSE_RIGHT)
3306 {
3307 if (clip_star.state == SELECT_CLEARED)
3308 {
3309 if (State & CMDLINE)
3310 {
3311 col = msg_col;
3312 row = msg_row;
3313 }
3314 else
3315 {
3316 col = curwin->w_wcol;
3317 row = curwin->w_wrow + W_WINROW(curwin);
3318 }
3319 clip_start_selection(col, row, FALSE);
3320 }
3321 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3322 repeated_click);
3323 did_clip = TRUE;
3324 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003325 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003326 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3329 did_clip = TRUE;
3330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar30613902019-12-01 22:11:18 +01003332 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (button != MOUSE_MIDDLE)
3334 {
3335 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3336 return;
3337 if (checkfor != MOUSE_COMMAND)
3338 button = MOUSE_LEFT;
3339 }
3340 repeated_click = FALSE;
3341 }
3342
3343 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003344 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#endif
3346
Bram Moolenaar30613902019-12-01 22:11:18 +01003347 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (hold_gui_events)
3349 return;
3350
3351 row = gui_xy2colrow(x, y, &col);
3352
3353 /*
3354 * If we are dragging and the mouse hasn't moved far enough to be on a
3355 * different character, then don't send an event to vim.
3356 */
3357 if (button == MOUSE_DRAG)
3358 {
3359 if (row == prev_row && col == prev_col)
3360 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003361 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (y < 0)
3363 row = -1;
3364 }
3365
3366 /*
3367 * If topline has changed (window scrolled) since the last click, reset
3368 * repeated_click, because we don't want starting Visual mode when
3369 * clicking on a different character in the text.
3370 */
3371 if (curwin->w_topline != gui_prev_topline
3372#ifdef FEAT_DIFF
3373 || curwin->w_topfill != gui_prev_topfill
3374#endif
3375 )
3376 repeated_click = FALSE;
3377
Bram Moolenaar30613902019-12-01 22:11:18 +01003378 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 string[1] = KS_MOUSE;
3380 string[2] = KE_FILLER;
3381 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3382 {
3383 if (repeated_click)
3384 {
3385 /*
3386 * Handle multiple clicks. They only count if the mouse is still
3387 * pointing at the same character.
3388 */
3389 if (button != prev_button || row != prev_row || col != prev_col)
3390 num_clicks = 1;
3391 else if (++num_clicks > 4)
3392 num_clicks = 1;
3393 }
3394 else
3395 num_clicks = 1;
3396 prev_button = button;
3397 gui_prev_topline = curwin->w_topline;
3398#ifdef FEAT_DIFF
3399 gui_prev_topfill = curwin->w_topfill;
3400#endif
3401
3402 string[3] = (char_u)(button | 0x20);
3403 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3404 }
3405 else
3406 string[3] = (char_u)button;
3407
3408 string[3] |= modifiers;
3409 fill_mouse_coord(string + 4, col, row);
3410 add_to_input_buf(string, 8);
3411
3412 if (row < 0)
3413 prev_row = 0;
3414 else
3415 prev_row = row;
3416 prev_col = col;
3417
3418 /*
3419 * We need to make sure this is cleared since Athena doesn't tell us when
3420 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3421 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003422#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 gui.dragged_sb = SBAR_NONE;
3424#endif
3425}
3426
3427/*
3428 * Convert x and y coordinate to column and row in text window.
3429 * Corrects for multi-byte character.
3430 * returns column in "*colp" and row as return value;
3431 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003432 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003433gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 int col = check_col(X_2_COL(x));
3436 int row = check_row(Y_2_ROW(y));
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 return row;
3440}
3441
3442#if defined(FEAT_MENU) || defined(PROTO)
3443/*
3444 * Callback function for when a menu entry has been selected.
3445 */
3446 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003447gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003449 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar30613902019-12-01 22:11:18 +01003451 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (hold_gui_events)
3453 return;
3454
3455 bytes[0] = CSI;
3456 bytes[1] = KS_MENU;
3457 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003458 add_to_input_buf(bytes, 3);
3459 add_long_to_buf((long_u)menu, bytes);
3460 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461}
3462#endif
3463
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003464static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466/*
3467 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003468 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 * in p_go.
3470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003472gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003474#ifdef FEAT_GUI_DARKTHEME
3475 static int prev_dark_theme = -1;
3476 int using_dark_theme = FALSE;
3477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#ifdef FEAT_MENU
3479 static int prev_menu_is_active = -1;
3480#endif
3481#ifdef FEAT_TOOLBAR
3482 static int prev_toolbar = -1;
3483 int using_toolbar = FALSE;
3484#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003485#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003486 int using_tabline;
3487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#ifdef FEAT_FOOTER
3489 static int prev_footer = -1;
3490 int using_footer = FALSE;
3491#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 static int prev_tearoff = -1;
3494 int using_tearoff = FALSE;
3495#endif
3496
3497 char_u *p;
3498 int i;
3499#ifdef FEAT_MENU
3500 int grey_old, grey_new;
3501 char_u *temp;
3502#endif
3503 win_T *wp;
3504 int need_set_size;
3505 int fix_size;
3506
3507#ifdef FEAT_MENU
3508 if (oldval != NULL && gui.in_use)
3509 {
3510 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003511 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 */
3513 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3514 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3515 if (grey_old != grey_new)
3516 {
3517 temp = p_go;
3518 p_go = oldval;
3519 gui_update_menus(MENU_ALL_MODES);
3520 p_go = temp;
3521 }
3522 }
3523 gui.menu_is_active = FALSE;
3524#endif
3525
3526 for (i = 0; i < 3; i++)
3527 gui.which_scrollbars[i] = FALSE;
3528 for (p = p_go; *p; p++)
3529 switch (*p)
3530 {
3531 case GO_LEFT:
3532 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3533 break;
3534 case GO_RIGHT:
3535 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 case GO_VLEFT:
3538 if (win_hasvertsplit())
3539 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3540 break;
3541 case GO_VRIGHT:
3542 if (win_hasvertsplit())
3543 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 case GO_BOT:
3546 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3547 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003548#ifdef FEAT_GUI_DARKTHEME
3549 case GO_DARKTHEME:
3550 using_dark_theme = TRUE;
3551 break;
3552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#ifdef FEAT_MENU
3554 case GO_MENUS:
3555 gui.menu_is_active = TRUE;
3556 break;
3557#endif
3558 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003559 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 break;
3561#ifdef FEAT_TOOLBAR
3562 case GO_TOOLBAR:
3563 using_toolbar = TRUE;
3564 break;
3565#endif
3566#ifdef FEAT_FOOTER
3567 case GO_FOOTER:
3568 using_footer = TRUE;
3569 break;
3570#endif
3571 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 using_tearoff = TRUE;
3574#endif
3575 break;
3576 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003577 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 break;
3579 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (gui.in_use)
3582 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003583 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003585
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003586#ifdef FEAT_GUI_DARKTHEME
3587 if (using_dark_theme != prev_dark_theme)
3588 {
3589 gui_mch_set_dark_theme(using_dark_theme);
3590 prev_dark_theme = using_dark_theme;
3591 }
3592#endif
3593
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003595 // Update the GUI tab line, it may appear or disappear. This may
3596 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003597 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003598 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003599 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003600 // We don't want a resize event change "Rows" here, save and
3601 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003602 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003604 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003605 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003606 if (using_tabline)
3607 fix_size = TRUE;
3608 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003609 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003610 }
3611#endif
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 for (i = 0; i < 3; i++)
3614 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003615 // The scrollbar needs to be updated when it is shown/unshown and
3616 // when switching tab pages. But the size only changes when it's
3617 // shown/unshown. Thus we need two places to remember whether a
3618 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003619 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003620 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003621 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 if (i == SBAR_BOTTOM)
3624 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3625 gui.which_scrollbars[i]);
3626 else
3627 {
3628 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003631 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3632 {
3633 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003634 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003635 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003636 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003637 if (gui.which_scrollbars[i])
3638 fix_size = TRUE;
3639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003641 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003642 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644
3645#ifdef FEAT_MENU
3646 if (gui.menu_is_active != prev_menu_is_active)
3647 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003648 // We don't want a resize event change "Rows" here, save and
3649 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 i = Rows;
3651 gui_mch_enable_menu(gui.menu_is_active);
3652 Rows = i;
3653 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003654 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (gui.menu_is_active)
3656 fix_size = TRUE;
3657 }
3658#endif
3659
3660#ifdef FEAT_TOOLBAR
3661 if (using_toolbar != prev_toolbar)
3662 {
3663 gui_mch_show_toolbar(using_toolbar);
3664 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003665 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (using_toolbar)
3667 fix_size = TRUE;
3668 }
3669#endif
3670#ifdef FEAT_FOOTER
3671 if (using_footer != prev_footer)
3672 {
3673 gui_mch_enable_footer(using_footer);
3674 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003675 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if (using_footer)
3677 fix_size = TRUE;
3678 }
3679#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003680#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (using_tearoff != prev_tearoff)
3682 {
3683 gui_mch_toggle_tearoffs(using_tearoff);
3684 prev_tearoff = using_tearoff;
3685 }
3686#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003687 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003688 {
3689#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003690 long prev_Columns = Columns;
3691 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003692#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003693 // Adjust the size of the window to make the text area keep the
3694 // same size and to avoid that part of our window is off-screen
3695 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003696 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003697
3698#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003699 // GTK has the annoying habit of sending us resize events when
3700 // changing the window size ourselves. This mostly happens when
3701 // waiting for a character to arrive, quite unpredictably, and may
3702 // change Columns and Rows when we don't want it. Wait for a
3703 // character here to avoid this effect.
3704 // If you remove this, please test this command for resizing
3705 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3706 // Don't do this while starting up though.
3707 // Don't change Rows when adding menu/toolbar/tabline.
3708 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003709 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003710 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003711 if ((need_set_size & RESIZE_VERT) == 0)
3712 Rows = prev_Rows;
3713 if ((need_set_size & RESIZE_HOR) == 0)
3714 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003715#endif
3716 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003717 // When the console tabline appears or disappears the window positions
3718 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003719 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003720 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722}
3723
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003724#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3725/*
3726 * Return TRUE if the GUI is taking care of the tabline.
3727 * It may still be hidden if 'showtabline' is zero.
3728 */
3729 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003730gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003731{
3732 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3733}
3734
3735/*
3736 * Return TRUE if the GUI is showing the tabline.
3737 * This uses 'showtabline'.
3738 */
3739 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003740gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741{
3742 if (!gui_use_tabline()
3743 || p_stal == 0
3744 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3745 return FALSE;
3746 return TRUE;
3747}
3748
3749/*
3750 * Update the tabline.
3751 * This may display/undisplay the tabline and update the labels.
3752 */
3753 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003754gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003755{
3756 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003757 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003758
3759 if (!gui.starting && starting == 0)
3760 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003761 // Updating the tabline uses direct GUI commands, flush
3762 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003763 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003764
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003765 if (!showit != !shown)
3766 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003767 if (showit != 0)
3768 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003769
Bram Moolenaar30613902019-12-01 22:11:18 +01003770 // When the tabs change from hidden to shown or from shown to
3771 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003772 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003773 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003774 }
3775}
3776
3777/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003778 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003779 */
3780 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003781get_tabline_label(
3782 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003783 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003784{
3785 int modified = FALSE;
3786 char_u buf[40];
3787 int wincount;
3788 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003789 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003790
Bram Moolenaar30613902019-12-01 22:11:18 +01003791 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003792 opt = (tooltip ? &p_gtt : &p_gtl);
3793 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003794 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003795 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003796 int called_emsg_before = called_emsg;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003797 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003798 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003799 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3800 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003801
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003802 printer_page_num = tabpage_index(tp);
3803# ifdef FEAT_EVAL
3804 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003805 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003806# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003807 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003808 curtab->tp_firstwin = firstwin;
3809 curtab->tp_lastwin = lastwin;
3810 curtab->tp_curwin = curwin;
3811 save_curtab = curtab;
3812 curtab = tp;
3813 topframe = curtab->tp_topframe;
3814 firstwin = curtab->tp_firstwin;
3815 lastwin = curtab->tp_lastwin;
3816 curwin = curtab->tp_curwin;
3817 curbuf = curwin->w_buffer;
3818
Bram Moolenaar30613902019-12-01 22:11:18 +01003819 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003820 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003821 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003822 STRCPY(NameBuff, res);
3823
Bram Moolenaar30613902019-12-01 22:11:18 +01003824 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003825 curtab = save_curtab;
3826 topframe = curtab->tp_topframe;
3827 firstwin = curtab->tp_firstwin;
3828 lastwin = curtab->tp_lastwin;
3829 curwin = curtab->tp_curwin;
3830 curbuf = curwin->w_buffer;
3831
Bram Moolenaar53989552019-12-23 22:59:18 +01003832 if (called_emsg > called_emsg_before)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003833 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003834 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003835 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003836
Bram Moolenaar30613902019-12-01 22:11:18 +01003837 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3838 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003839 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003840 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003841 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003842 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003843 if (!tooltip)
3844 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003845
3846 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3847 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3848 if (bufIsChanged(wp->w_buffer))
3849 modified = TRUE;
3850 if (modified || wincount > 1)
3851 {
3852 if (wincount > 1)
3853 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3854 else
3855 buf[0] = NUL;
3856 if (modified)
3857 STRCAT(buf, "+");
3858 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003859 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003860 mch_memmove(NameBuff, buf, STRLEN(buf));
3861 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003862 }
3863}
3864
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003865/*
3866 * Send the event for clicking to select tab page "nr".
3867 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003868 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003869 */
3870 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003871send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003872{
3873 char_u string[3];
3874
3875 if (nr == tabpage_index(curtab))
3876 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003877
Bram Moolenaar30613902019-12-01 22:11:18 +01003878 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003879 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003880# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003881 || cmdwin_type != 0
3882# endif
3883 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003884 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003885 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003886 gui_mch_set_curtab(tabpage_index(curtab));
3887 return FALSE;
3888 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003889
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003890 string[0] = CSI;
3891 string[1] = KS_TABLINE;
3892 string[2] = KE_FILLER;
3893 add_to_input_buf(string, 3);
3894 string[0] = nr;
3895 add_to_input_buf_csi(string, 1);
3896 return TRUE;
3897}
3898
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003899/*
3900 * Send a tabline menu event
3901 */
3902 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003903send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003904{
3905 char_u string[3];
3906
Bram Moolenaar29547192018-12-11 20:39:19 +01003907 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003908 if (hold_gui_events)
3909 return;
3910
Bram Moolenaar29547192018-12-11 20:39:19 +01003911 // Cannot close the last tabpage.
3912 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3913 return;
3914
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003915 string[0] = CSI;
3916 string[1] = KS_TABMENU;
3917 string[2] = KE_FILLER;
3918 add_to_input_buf(string, 3);
3919 string[0] = tabidx;
3920 string[1] = (char_u)(long)event;
3921 add_to_input_buf_csi(string, 2);
3922}
3923
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926/*
3927 * Scrollbar stuff:
3928 */
3929
Bram Moolenaare45828b2006-02-15 22:12:56 +00003930/*
3931 * Remove all scrollbars. Used before switching to another tab page.
3932 */
3933 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003934gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003935{
3936 int i;
3937 win_T *wp;
3938
3939 for (i = 0; i < 3; i++)
3940 {
3941 if (i == SBAR_BOTTOM)
3942 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3943 else
3944 {
3945 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003946 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003947 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003948 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003949 }
3950}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003953gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 static int sbar_ident = 0;
3956
Bram Moolenaar30613902019-12-01 22:11:18 +01003957 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 sb->wp = wp;
3959 sb->type = type;
3960 sb->value = 0;
3961#ifdef FEAT_GUI_ATHENA
3962 sb->pixval = 0;
3963#endif
3964 sb->size = 1;
3965 sb->max = 1;
3966 sb->top = 0;
3967 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 sb->status_height = 0;
3970 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3971}
3972
3973/*
3974 * Find the scrollbar with the given index.
3975 */
3976 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003977gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 win_T *wp;
3980
3981 if (gui.bottom_sbar.ident == ident)
3982 return &gui.bottom_sbar;
3983 FOR_ALL_WINDOWS(wp)
3984 {
3985 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3986 return &wp->w_scrollbars[SBAR_LEFT];
3987 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3988 return &wp->w_scrollbars[SBAR_RIGHT];
3989 }
3990 return NULL;
3991}
3992
3993/*
3994 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3995 *
3996 * For Win32, Macintosh and GTK+ 2:
3997 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3998 * you stop dragging the scrollbar. We get here each time the scrollbar is
3999 * dragged another pixel, but as far as the rest of vim goes, it thinks
4000 * we're just hanging in the call to DispatchMessage() in
4001 * process_message(). The DispatchMessage() call that hangs was passed a
4002 * mouse button click event in the scrollbar window. -- webb.
4003 *
4004 * Solution: Do the scrolling right here. But only when allowed.
4005 * Ignore the scrollbars while executing an external command or when there
4006 * are still characters to be processed.
4007 */
4008 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004009gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 int sb_num;
4013#ifdef USE_ON_FLY_SCROLL
4014 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016# ifdef FEAT_DIFF
4017 int old_topfill = curwin->w_topfill;
4018# endif
4019#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004020 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 int byte_count;
4022#endif
4023
4024 if (sb == NULL)
4025 return;
4026
Bram Moolenaar30613902019-12-01 22:11:18 +01004027 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (hold_gui_events)
4029 return;
4030
4031#ifdef FEAT_CMDWIN
4032 if (cmdwin_type != 0 && sb->wp != curwin)
4033 return;
4034#endif
4035
4036 if (still_dragging)
4037 {
4038 if (sb->wp == NULL)
4039 gui.dragged_sb = SBAR_BOTTOM;
4040 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4041 gui.dragged_sb = SBAR_LEFT;
4042 else
4043 gui.dragged_sb = SBAR_RIGHT;
4044 gui.dragged_wp = sb->wp;
4045 }
4046 else
4047 {
4048 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004049#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004050 // Keep the "dragged_wp" value until after the scrolling, for when the
4051 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 gui.dragged_wp = NULL;
4053#endif
4054 }
4055
Bram Moolenaar30613902019-12-01 22:11:18 +01004056 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (sb->wp != NULL)
4058 sb = &sb->wp->w_scrollbars[0];
4059
4060 /*
4061 * Check validity of value
4062 */
4063 if (value < 0)
4064 value = 0;
4065#ifdef SCROLL_PAST_END
4066 else if (value > sb->max)
4067 value = sb->max;
4068#else
4069 if (value > sb->max - sb->size + 1)
4070 value = sb->max - sb->size + 1;
4071#endif
4072
4073 sb->value = value;
4074
4075#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004076 // When not allowed to do the scrolling right now, return.
4077 // This also checked input_available(), but that causes the first click in
4078 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004079 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 return;
4081#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004082 // Disallow scrolling the current window when the completion popup menu is
4083 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004084 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4085 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087#ifdef FEAT_RIGHTLEFT
4088 if (sb->wp == NULL && curwin->w_p_rl)
4089 {
4090 value = sb->max + 1 - sb->size - value;
4091 if (value < 0)
4092 value = 0;
4093 }
4094#endif
4095
Bram Moolenaar30613902019-12-01 22:11:18 +01004096 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
4098 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4100 sb_num++;
4101 if (wp == NULL)
4102 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104#ifdef USE_ON_FLY_SCROLL
4105 current_scrollbar = sb_num;
4106 scrollbar_value = value;
4107 if (State & NORMAL)
4108 {
4109 gui_do_scroll();
4110 setcursor();
4111 }
4112 else if (State & INSERT)
4113 {
4114 ins_scroll();
4115 setcursor();
4116 }
4117 else if (State & CMDLINE)
4118 {
4119 if (msg_scrolled == 0)
4120 {
4121 gui_do_scroll();
4122 redrawcmdline();
4123 }
4124 }
4125# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004126 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 sb->value = sb->wp->w_topline - 1;
4128# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004129
Bram Moolenaar30613902019-12-01 22:11:18 +01004130 // When dragging one scrollbar and there is another one at the other
4131 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004132 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4133 gui_mch_set_scrollbar_thumb(
4134 &sb->wp->w_scrollbars[
4135 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4136 ? SBAR_LEFT : SBAR_RIGHT],
4137 sb->value, sb->size, sb->max);
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#else
4140 bytes[0] = CSI;
4141 bytes[1] = KS_VER_SCROLLBAR;
4142 bytes[2] = KE_FILLER;
4143 bytes[3] = (char_u)sb_num;
4144 byte_count = 4;
4145#endif
4146 }
4147 else
4148 {
4149#ifdef USE_ON_FLY_SCROLL
4150 scrollbar_value = value;
4151
4152 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004153 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 else if (State & INSERT)
4155 ins_horscroll();
4156 else if (State & CMDLINE)
4157 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004158 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004160 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 redrawcmdline();
4162 }
4163 }
4164 if (old_leftcol != curwin->w_leftcol)
4165 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004166 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 setcursor();
4168 }
4169#else
4170 bytes[0] = CSI;
4171 bytes[1] = KS_HOR_SCROLLBAR;
4172 bytes[2] = KE_FILLER;
4173 byte_count = 3;
4174#endif
4175 }
4176
4177#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /*
4179 * synchronize other windows, as necessary according to 'scrollbind'
4180 */
4181 if (curwin->w_p_scb
4182 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4183 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004184# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 ))))
4188 {
4189 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004190 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004191 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (wp->w_redr_type > 0)
4193 updateWindow(wp);
4194 setcursor();
4195 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004196 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004198 add_to_input_buf(bytes, byte_count);
4199 add_long_to_buf((long_u)value, bytes);
4200 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#endif
4202}
4203
4204/*
4205 * Scrollbar stuff:
4206 */
4207
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004208/*
4209 * Called when something in the window layout has changed.
4210 */
4211 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004212gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004213{
4214 if (gui.in_use && starting == 0)
4215 {
4216 out_flush();
4217 gui_init_which_components(NULL);
4218 gui_update_scrollbars(TRUE);
4219 }
4220 need_mouse_correct = TRUE;
4221}
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004224gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004225 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 win_T *wp;
4228 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004229 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 int which_sb;
4231 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
Bram Moolenaar30613902019-12-01 22:11:18 +01004234 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 gui_update_horiz_scrollbar(force);
4236
Bram Moolenaar4f974752019-02-17 17:44:42 +01004237#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004238 // Return straight away if there is neither a left nor right scrollbar.
4239 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4241 return;
4242#endif
4243
4244 /*
4245 * Don't want to update a scrollbar while we're dragging it. But if we
4246 * have both a left and right scrollbar, and we drag one of them, we still
4247 * need to update the other one.
4248 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004249 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4250 && gui.which_scrollbars[SBAR_LEFT]
4251 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 /*
4254 * If we have two scrollbars and one of them is being dragged, just
4255 * copy the scrollbar position from the dragged one to the other one.
4256 */
4257 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4258 if (gui.dragged_wp != NULL)
4259 gui_mch_set_scrollbar_thumb(
4260 &gui.dragged_wp->w_scrollbars[which_sb],
4261 gui.dragged_wp->w_scrollbars[0].value,
4262 gui.dragged_wp->w_scrollbars[0].size,
4263 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265
Bram Moolenaar30613902019-12-01 22:11:18 +01004266 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 ++hold_gui_events;
4268
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004269 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004271 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004273 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004274 if (!force && (gui.dragged_sb == SBAR_LEFT
4275 || gui.dragged_sb == SBAR_RIGHT)
4276 && gui.dragged_wp == wp)
4277 continue;
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279#ifdef SCROLL_PAST_END
4280 max = wp->w_buffer->b_ml.ml_line_count - 1;
4281#else
4282 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4283#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004284 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 max = 0;
4286 val = wp->w_topline - 1;
4287 size = wp->w_height;
4288#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004289 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 val = max;
4291#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004292 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 size = max + 1;
4294 if (val > max - size + 1)
4295 val = max - size + 1;
4296#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004297 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 val = 0;
4299
4300 /*
4301 * Scrollbar at index 0 (the left one) contains all the information.
4302 * It would be the same info for left and right so we just store it for
4303 * one of them.
4304 */
4305 sb = &wp->w_scrollbars[0];
4306
4307 /*
4308 * Note: no check for valid w_botline. If it's not valid the
4309 * scrollbars will be updated later anyway.
4310 */
4311 if (size < 1 || wp->w_botline - 2 > max)
4312 {
4313 /*
4314 * This can happen during changing files. Just don't update the
4315 * scrollbar for now.
4316 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004317 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (gui.which_scrollbars[SBAR_LEFT])
4319 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4320 if (gui.which_scrollbars[SBAR_RIGHT])
4321 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4322 continue;
4323 }
4324 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 || sb->top != wp->w_winrow
4326 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004328 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004330 // Height, width or position of scrollbar has changed. For
4331 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 sb->top = wp->w_winrow;
4334 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
Bram Moolenaar30613902019-12-01 22:11:18 +01004337 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 h = (sb->height + sb->status_height) * gui.char_height;
4339 y = sb->top * gui.char_height + gui.border_offset;
4340#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4341 if (gui.menu_is_active)
4342 y += gui.menu_height;
4343#endif
4344
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004345#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA) \
4346 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004348# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 y += gui.toolbar_height;
4350# else
4351# ifdef FEAT_GUI_MSWIN
4352 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4353# endif
4354# endif
4355#endif
4356
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004357#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004358 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004359 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004364 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 h += gui.border_offset;
4366 y -= gui.border_offset;
4367 }
4368 if (gui.which_scrollbars[SBAR_LEFT])
4369 {
4370 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4371 gui.left_sbar_x, y,
4372 gui.scrollbar_width, h);
4373 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4374 }
4375 if (gui.which_scrollbars[SBAR_RIGHT])
4376 {
4377 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4378 gui.right_sbar_x, y,
4379 gui.scrollbar_width, h);
4380 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4381 }
4382 }
4383
Bram Moolenaar30613902019-12-01 22:11:18 +01004384 // Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4385 // checking if the thumb moved at least a pixel. Only do this for
4386 // Athena, most other GUIs require the update anyway to make the
4387 // arrows work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388#ifdef FEAT_GUI_ATHENA
4389 if (max == 0)
4390 y = 0;
4391 else
4392 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4393 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4394#else
4395 if (force || sb->value != val || sb->size != size || sb->max != max)
4396#endif
4397 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004398 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 sb->value = val;
4400#ifdef FEAT_GUI_ATHENA
4401 sb->pixval = y;
4402#endif
4403 sb->size = size;
4404 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004405 if (gui.which_scrollbars[SBAR_LEFT]
4406 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4408 val, size, max);
4409 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004410 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4412 val, size, max);
4413 }
4414 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004415
Bram Moolenaarf5666662021-11-28 21:33:36 +00004416#ifdef FEAT_TITLE
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004417 // update the title, it may show the scroll position
4418 maketitle();
Bram Moolenaarf5666662021-11-28 21:33:36 +00004419#endif
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 --hold_gui_events;
4423}
4424
4425/*
4426 * Enable or disable a scrollbar.
4427 * Check for scrollbars for vertically split windows which are not enabled
4428 * sometimes.
4429 */
4430 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004431gui_do_scrollbar(
4432 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004433 int which, // SBAR_LEFT or SBAR_RIGHT
4434 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 int midcol = curwin->w_wincol + curwin->w_width / 2;
4437 int has_midcol = (wp->w_wincol <= midcol
4438 && wp->w_wincol + wp->w_width >= midcol);
4439
Bram Moolenaar30613902019-12-01 22:11:18 +01004440 // Only enable scrollbars that contain the middle column of the current
4441 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4443 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004444 // Scrollbars only on one side. Don't enable scrollbars that don't
4445 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (!has_midcol)
4447 enable = FALSE;
4448 }
4449 else
4450 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004451 // Scrollbars on both sides. Don't enable scrollbars that neither
4452 // contain the middle column of the current window nor are on the far
4453 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (midcol > Columns / 2)
4455 {
4456 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4457 enable = FALSE;
4458 }
4459 else
4460 {
4461 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4462 : !has_midcol)
4463 enable = FALSE;
4464 }
4465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4467}
4468
4469/*
4470 * Scroll a window according to the values set in the globals current_scrollbar
4471 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4472 * or FALSE otherwise.
4473 */
4474 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004475gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
4477 win_T *wp, *save_wp;
4478 int i;
4479 long nlines;
4480 pos_T old_cursor;
4481 linenr_T old_topline;
4482#ifdef FEAT_DIFF
4483 int old_topfill;
4484#endif
4485
4486 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4487 if (wp == NULL)
4488 break;
4489 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004490 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 return FALSE;
4492
4493 /*
4494 * Compute number of lines to scroll. If zero, nothing to do.
4495 */
4496 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4497 if (nlines == 0)
4498 return FALSE;
4499
4500 save_wp = curwin;
4501 old_topline = wp->w_topline;
4502#ifdef FEAT_DIFF
4503 old_topfill = wp->w_topfill;
4504#endif
4505 old_cursor = wp->w_cursor;
4506 curwin = wp;
4507 curbuf = wp->w_buffer;
4508 if (nlines < 0)
4509 scrolldown(-nlines, gui.dragged_wp == NULL);
4510 else
4511 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004512 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4513 // the mouse-up event already, but we still want it to behave like when
4514 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (gui.dragged_sb == SBAR_NONE)
4516 gui.dragged_wp = NULL;
4517
4518 if (old_topline != wp->w_topline
4519#ifdef FEAT_DIFF
4520 || old_topfill != wp->w_topfill
4521#endif
4522 )
4523 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004524 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004526 cursor_correct(); // fix window for 'so'
4527 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 if (old_cursor.lnum != wp->w_cursor.lnum)
4530 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533
Bram Moolenaar30613902019-12-01 22:11:18 +01004534 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004535 validate_cursor();
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 curwin = save_wp;
4538 curbuf = save_wp->w_buffer;
4539
4540 /*
4541 * Don't call updateWindow() when nothing has changed (it will overwrite
4542 * the status line!).
4543 */
4544 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004545 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546#ifdef FEAT_DIFF
4547 || old_topfill != wp->w_topfill
4548#endif
4549 )
4550 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004551 int type = VALID;
4552
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004553 if (pum_visible())
4554 {
4555 type = NOT_VALID;
4556 wp->w_lines_valid = 0;
4557 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004558
Bram Moolenaar30613902019-12-01 22:11:18 +01004559 // Don't set must_redraw here, it may cause the popup menu to
4560 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004561 if (wp->w_redr_type < type)
4562 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004563 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004564 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004565 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567
Bram Moolenaar30613902019-12-01 22:11:18 +01004568 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004569 if (pum_visible())
4570 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004571
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004572 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573}
4574
4575
4576/*
4577 * Horizontal scrollbar stuff:
4578 */
4579
4580/*
4581 * Return length of line "lnum" for horizontal scrolling.
4582 */
4583 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004584scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
4586 char_u *p;
4587 colnr_T col;
4588 int w;
4589
4590 p = ml_get(lnum);
4591 col = 0;
4592 if (*p != NUL)
4593 for (;;)
4594 {
4595 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004596 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004597 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 break;
4599 col += w;
4600 }
4601 return col;
4602}
4603
Bram Moolenaar30613902019-12-01 22:11:18 +01004604// Remember which line is currently the longest, so that we don't have to
4605// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606static linenr_T longest_lnum = 0;
4607
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004608/*
4609 * Find longest visible line number. If this is not possible (or not desired,
4610 * by setting 'h' in "guioptions") then the current line number is returned.
4611 */
4612 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004613gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004614{
4615 linenr_T ret = 0;
4616
Bram Moolenaar30613902019-12-01 22:11:18 +01004617 // Calculate maximum for horizontal scrollbar. Check for reasonable
4618 // line numbers, topline and botline can be invalid when displaying is
4619 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004620 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4621 && curwin->w_topline <= curwin->w_cursor.lnum
4622 && curwin->w_botline > curwin->w_cursor.lnum
4623 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4624 {
4625 linenr_T lnum;
4626 colnr_T n;
4627 long max = 0;
4628
Bram Moolenaar30613902019-12-01 22:11:18 +01004629 // Use maximum of all visible lines. Remember the lnum of the
4630 // longest line, closest to the cursor line. Used when scrolling
4631 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004632 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4633 {
4634 n = scroll_line_len(lnum);
4635 if (n > (colnr_T)max)
4636 {
4637 max = n;
4638 ret = lnum;
4639 }
4640 else if (n == (colnr_T)max
4641 && abs((int)(lnum - curwin->w_cursor.lnum))
4642 < abs((int)(ret - curwin->w_cursor.lnum)))
4643 ret = lnum;
4644 }
4645 }
4646 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004647 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004648 ret = curwin->w_cursor.lnum;
4649
4650 return ret;
4651}
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004654gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
Bram Moolenaar30613902019-12-01 22:11:18 +01004656 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 if (!gui.which_scrollbars[SBAR_BOTTOM])
4659 return;
4660
4661 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4662 return;
4663
4664 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4665 return;
4666
4667 /*
4668 * It is possible for the cursor to be invalid if we're in the middle of
4669 * something (like changing files). If so, don't do anything for now.
4670 */
4671 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4672 {
4673 gui.bottom_sbar.value = -1;
4674 return;
4675 }
4676
Bram Moolenaar02631462017-09-22 15:20:32 +02004677 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (curwin->w_p_wrap)
4679 {
4680 value = 0;
4681#ifdef SCROLL_PAST_END
4682 max = 0;
4683#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004684 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
4686 }
4687 else
4688 {
4689 value = curwin->w_leftcol;
4690
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004691 longest_lnum = gui_find_longest_lnum();
4692 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (virtual_active())
4695 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004696 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 if (curwin->w_virtcol >= (colnr_T)max)
4698 max = curwin->w_virtcol;
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004702 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004704 // The line number isn't scrolled, thus there is less space when
4705 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 size -= curwin_col_off();
4707#ifndef SCROLL_PAST_END
4708 max -= curwin_col_off();
4709#endif
4710 }
4711
4712#ifndef SCROLL_PAST_END
4713 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004714 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#endif
4716
4717#ifdef FEAT_RIGHTLEFT
4718 if (curwin->w_p_rl)
4719 {
4720 value = max + 1 - size - value;
4721 if (value < 0)
4722 {
4723 size += value;
4724 value = 0;
4725 }
4726 }
4727#endif
4728 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4729 && max == gui.bottom_sbar.max)
4730 return;
4731
4732 gui.bottom_sbar.value = value;
4733 gui.bottom_sbar.size = size;
4734 gui.bottom_sbar.max = max;
4735 gui.prev_wrap = curwin->w_p_wrap;
4736
4737 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4738}
4739
4740/*
4741 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4742 */
4743 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004744gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
Bram Moolenaar30613902019-12-01 22:11:18 +01004746 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (curwin->w_p_wrap)
4748 return FALSE;
4749
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004750 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return FALSE;
4752
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004753 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar30613902019-12-01 22:11:18 +01004755 // When the line of the cursor is too short, move the cursor to the
4756 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004758 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004759 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004761 if (compute_longest_lnum)
4762 {
4763 curwin->w_cursor.lnum = gui_find_longest_lnum();
4764 curwin->w_cursor.col = 0;
4765 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004766 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004767 else if (longest_lnum >= curwin->w_topline
4768 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 curwin->w_cursor.lnum = longest_lnum;
4771 curwin->w_cursor.col = 0;
4772 }
4773 }
4774
4775 return leftcol_changed();
4776}
4777
4778/*
4779 * Check that none of the colors are the same as the background color
4780 */
4781 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004782gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4785 {
4786 gui_set_bg_color((char_u *)"White");
4787 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4788 gui_set_fg_color((char_u *)"Black");
4789 }
4790}
4791
Bram Moolenaar3918c952005-03-15 22:34:55 +00004792 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004793gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 gui.norm_pixel = gui_get_color(name);
4796 hl_set_fg_color_name(vim_strsave(name));
4797}
4798
Bram Moolenaar3918c952005-03-15 22:34:55 +00004799 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004800gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802 gui.back_pixel = gui_get_color(name);
4803 hl_set_bg_color_name(vim_strsave(name));
4804}
4805
4806/*
4807 * Allocate a color by name.
4808 * Returns INVALCOLOR and gives an error message when failed.
4809 */
4810 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004811gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 guicolor_T t;
4814
4815 if (*name == NUL)
4816 return INVALCOLOR;
4817 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004820#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 && gui.in_use
4822#endif
4823 )
Drew Vogele30d1022021-10-24 20:35:07 +01004824 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 return t;
4826}
4827
4828/*
4829 * Return the grey value of a color (range 0-255).
4830 */
4831 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004832gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004834 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004836 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004837 + (((rgb >> 8) & 0xff) * 587)
4838 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
4840
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004841 char_u *
4842gui_bg_default(void)
4843{
4844 if (gui_get_lightness(gui.back_pixel) < 127)
4845 return (char_u *)"dark";
4846 return (char_u *)"light";
4847}
4848
4849/*
4850 * Option initializations that can only be done after opening the GUI window.
4851 */
4852 void
4853init_gui_options(void)
4854{
Bram Moolenaar30613902019-12-01 22:11:18 +01004855 // Set the 'background' option according to the lightness of the
4856 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004857 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4858 {
4859 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4860 highlight_changed();
4861 }
4862}
4863
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#if defined(FEAT_GUI_X11) || defined(PROTO)
4865 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004866gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 win_T *wp;
4869
Bram Moolenaar30613902019-12-01 22:11:18 +01004870 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (!gui.in_use)
4872 return;
4873
4874 FOR_ALL_WINDOWS(wp)
4875 {
4876 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4877 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4878 }
4879 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4880}
4881#endif
4882
4883/*
4884 * Call this when focus has changed.
4885 */
4886 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004887gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
4889/*
4890 * Skip this code to avoid drawing the cursor when debugging and switching
4891 * between the debugger window and gvim.
4892 */
4893#if 1
4894 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004895 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897# ifdef FEAT_XIM
4898 xim_set_focus(in_focus);
4899# endif
4900
Bram Moolenaar30613902019-12-01 22:11:18 +01004901 // Put events in the input queue only when allowed.
4902 // ui_focus_change() isn't called directly, because it invokes
4903 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004904 if (!hold_gui_events)
4905 {
4906 char_u bytes[3];
4907
4908 bytes[0] = CSI;
4909 bytes[1] = KS_EXTRA;
4910 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4911 add_to_input_buf(bytes, 3);
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
4914}
4915
4916/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004917 * When mouse moved: apply 'mousefocus'.
4918 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004920 static void
4921gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004924 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004927 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004928 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930
Bram Moolenaar30613902019-12-01 22:11:18 +01004931 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004933 && !hold_gui_events // not holding events
4934 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4935 && State != HITRETURN // but not hit-return prompt
4936 && msg_scrolled == 0 // no scrolled message
4937 && !need_mouse_correct // not moving the pointer
4938 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004940 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (x < 0 || x > Columns * gui.char_width)
4942 return;
4943#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004944 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945#endif
4946 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004947 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaar30613902019-12-01 22:11:18 +01004949 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004950 if (Y_2_ROW(y) < tabline_height())
4951 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 /*
4954 * format a mouse click on status line input
4955 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004956 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4957 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
4959 if (finish_op)
4960 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004961 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 st[0] = ESC;
4963 add_to_input_buf(st, 1);
4964 }
4965 st[0] = CSI;
4966 st[1] = KS_MOUSE;
4967 st[2] = KE_FILLER;
4968 st[3] = (char_u)MOUSE_LEFT;
4969 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004970 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 wp->w_height + W_WINROW(wp));
4972
4973 add_to_input_buf(st, 8);
4974 st[3] = (char_u)MOUSE_RELEASE;
4975 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004977 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (gtk_main_level() > 0)
4979 gtk_main_quit();
4980#endif
4981 }
4982}
4983
4984/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004985 * Called when the mouse moved (but not when dragging).
4986 */
4987 void
4988gui_mouse_moved(int x, int y)
4989{
4990 // Ignore this while still starting up.
4991 if (!gui.in_use || gui.starting)
4992 return;
4993
4994 // apply 'mousefocus' and pointer shape
4995 gui_mouse_focus(x, y);
4996
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004997#ifdef FEAT_PROP_POPUP
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004998 if (popup_visible)
4999 // Generate a mouse-moved event, so that the popup can perhaps be
5000 // closed, just like in the terminal.
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005001 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02005002#endif
5003}
5004
5005/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005006 * Get the window where the mouse pointer is on.
5007 * Returns NULL if not found.
5008 */
5009 win_T *
5010gui_mouse_window(mouse_find_T popup)
5011{
5012 int x, y;
5013
5014 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
5015 return NULL;
5016 gui_mch_getmouse(&x, &y);
5017
5018 // Only use the mouse when it's on the Vim window
5019 if (x >= 0 && x <= Columns * gui.char_width
5020 && y >= 0 && Y_2_ROW(y) >= tabline_height())
5021 return xy2win(x, y, popup);
5022 return NULL;
5023}
5024
5025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 * Called when mouse should be moved to window with focus.
5027 */
5028 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005029gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 win_T *wp = NULL;
5032
5033 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005034
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005035 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01005036 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005038 validate_cline_row();
5039 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
5040 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043}
5044
5045/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005046 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01005047 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005050xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 int row;
5053 int col;
5054 win_T *wp;
5055
5056 row = Y_2_ROW(y);
5057 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01005058 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005060 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005061 if (wp == NULL)
5062 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005063#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (State == HITRETURN || State == ASKMORE)
5065 {
5066 if (Y_2_ROW(y) >= msg_row)
5067 update_mouseshape(SHAPE_IDX_MOREL);
5068 else
5069 update_mouseshape(SHAPE_IDX_MORE);
5070 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005071 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005073 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005074 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005076 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005077 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 update_mouseshape(SHAPE_IDX_STATUS);
5079 else
5080 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005082 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083}
5084
5085/*
5086 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
5087 * File names may be given to redefine the args list.
5088 */
5089 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005090ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
5092 char_u *arg = eap->arg;
5093
5094 /*
5095 * Check for "-f" argument: foreground, don't fork.
5096 * Also don't fork when started with "gvim -f".
5097 * Do fork when using "gui -b".
5098 */
5099 if (arg[0] == '-'
5100 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005101 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
5103 gui.dofork = (arg[1] == 'b');
5104 eap->arg = skipwhite(eap->arg + 2);
5105 }
5106 if (!gui.in_use)
5107 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005108#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01005109 if (!gui.starting)
5110 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005111 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01005112 return;
5113 }
5114#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005115 // Clear the command. Needed for when forking+exiting, to avoid part
5116 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01005118#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005119 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005120 gui_start(eap->arg);
5121 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01005122#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005123 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01005124#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005125 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005128 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 ex_next(eap);
5130}
5131
Bram Moolenaar4f974752019-02-17 17:44:42 +01005132#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005133 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
5134 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005135 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136/*
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005137 * This is shared between Athena, Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140/*
5141 * Callback function for do_in_runtimepath().
5142 */
5143 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005144gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005146 char_u *gfp_buffer = cookie;
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (STRLEN(fname) >= MAXPATHL)
5149 *gfp_buffer = NUL;
5150 else
5151 STRCPY(gfp_buffer, fname);
5152}
5153
5154/*
5155 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5156 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5157 */
5158 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005159gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 if (STRLEN(name) > MAXPATHL - 14)
5162 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005163 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005164 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return FAIL;
5167 return OK;
5168}
5169
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005170# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171/*
5172 * Given the name of the "icon=" argument, try finding the bitmap file for the
5173 * icon. If it is an absolute path name, use it as it is. Otherwise append
5174 * "ext" and search for it in 'runtimepath'.
5175 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5176 * contains "name".
5177 */
5178 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005179gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
5181 char_u buf[MAXPATHL + 1];
5182
5183 expand_env(name, buffer, MAXPATHL);
5184 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5185 STRCPY(buffer, buf);
5186}
5187# endif
5188#endif
5189
Bram Moolenaar9e175142020-04-30 22:51:01 +02005190#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5191 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005193display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 char_u *p;
5196
5197 if (isatty(2))
5198 fflush(stderr);
5199 else if (error_ga.ga_data != NULL)
5200 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005201 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5203 if (!isspace(*p))
5204 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005205 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (STRLEN(p) > 2000)
5207 STRCPY(p + 2000 - 14, "...(truncated)");
5208 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005209 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 break;
5211 }
5212 ga_clear(&error_ga);
5213 }
5214}
5215#endif
5216
5217#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5218/*
5219 * Return TRUE if still starting up and there is no place to enter text.
5220 * For GTK and X11 we check if stderr is not a tty, which means we were
5221 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5222 * allow typing on stdin.
5223 */
5224 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005225no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
5227 return ((!gui.in_use || gui.starting)
5228# ifndef NO_CONSOLE
5229 && !isatty(0) && !isatty(2)
5230# endif
5231 );
5232}
5233#endif
5234
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005235#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005236 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005237 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238/*
5239 * Update the current window and the screen.
5240 */
5241 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005242gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005244# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005245 linenr_T conceal_old_cursor_line = 0;
5246 linenr_T conceal_new_cursor_line = 0;
5247 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005248# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005249
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 update_topline();
5251 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005252
Bram Moolenaar30613902019-12-01 22:11:18 +01005253 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005254 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005255# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005256 || popup_visible
5257# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005258# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005259 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005260# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005261 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005262 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005263 if (has_cursormoved())
5264 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005265#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005266 if (popup_visible)
5267 popup_check_cursor_pos();
5268#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005269# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005270 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005271 {
5272 conceal_old_cursor_line = last_cursormoved.lnum;
5273 conceal_new_cursor_line = curwin->w_cursor.lnum;
5274 conceal_update_lines = TRUE;
5275 }
5276# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005277 last_cursormoved = curwin->w_cursor;
5278 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005279
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005280# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005281 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005282 && (conceal_old_cursor_line != conceal_new_cursor_line
5283 || conceal_cursor_line(curwin)
5284 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005285 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005286 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005287 redrawWinline(curwin, conceal_old_cursor_line);
5288 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005289 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005290 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005291 }
5292# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005293 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005294 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005295 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296}
5297#endif
5298
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005299#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300/*
5301 * Get the text to use in a find/replace dialog. Uses the last search pattern
5302 * if the argument is empty.
5303 * Returns an allocated string.
5304 */
5305 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005306get_find_dialog_text(
5307 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005308 int *wwordp, // return: TRUE if \< \> found
5309 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
5311 char_u *text;
5312
5313 if (*arg == NUL)
5314 text = last_search_pat();
5315 else
5316 text = arg;
5317 if (text != NULL)
5318 {
5319 text = vim_strsave(text);
5320 if (text != NULL)
5321 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005322 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 int i;
5324
Bram Moolenaar30613902019-12-01 22:11:18 +01005325 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5327 {
5328 mch_memmove(text, text + 2, (size_t)(len - 1));
5329 len -= 2;
5330 }
5331
Bram Moolenaar30613902019-12-01 22:11:18 +01005332 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5334 {
5335 *mcasep = (text[1] == 'C');
5336 mch_memmove(text, text + 2, (size_t)(len - 1));
5337 len -= 2;
5338 }
5339
Bram Moolenaar30613902019-12-01 22:11:18 +01005340 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (len >= 4
5342 && STRNCMP(text, "\\<", 2) == 0
5343 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5344 {
5345 *wwordp = TRUE;
5346 mch_memmove(text, text + 2, (size_t)(len - 4));
5347 text[len - 4] = NUL;
5348 }
5349
Bram Moolenaar30613902019-12-01 22:11:18 +01005350 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 for (i = 0; i + 1 < len; ++i)
5352 if (text[i] == '\\' && (text[i + 1] == '/'
5353 || text[i + 1] == '?'))
5354 {
5355 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5356 --len;
5357 }
5358 }
5359 }
5360 return text;
5361}
5362
5363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 * Handle the press of a button in the find-replace dialog.
5365 * Return TRUE when something was added to the input buffer.
5366 */
5367 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005368gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005369 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005370 char_u *find_text,
5371 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005372 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 garray_T ga;
5375 int i;
5376 int type = (flags & FRD_TYPE_MASK);
5377 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005378 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005379 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005380 static int busy = FALSE;
5381
Bram Moolenaar30613902019-12-01 22:11:18 +01005382 // When the screen is being updated we should not change buffers and
5383 // windows structures, it may cause freed memory to be used. Also don't
5384 // do this recursively (pressing "Find" quickly several times.
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005385 if (updating_screen || busy)
5386 return FALSE;
5387
Bram Moolenaar30613902019-12-01 22:11:18 +01005388 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005389 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5390 return FALSE;
5391
5392 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
5394 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005395 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 ga_concat(&ga, (char_u *)"%s/");
5397
5398 ga_concat(&ga, (char_u *)"\\V");
5399 if (flags & FRD_MATCH_CASE)
5400 ga_concat(&ga, (char_u *)"\\C");
5401 else
5402 ga_concat(&ga, (char_u *)"\\c");
5403 if (flags & FRD_WHOLE_WORD)
5404 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005405 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005406 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5407 if (p != NULL)
5408 ga_concat(&ga, p);
5409 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (flags & FRD_WHOLE_WORD)
5411 ga_concat(&ga, (char_u *)"\\>");
5412
5413 if (type == FRD_REPLACEALL)
5414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005416 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005417 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5418 if (p != NULL)
5419 ga_concat(&ga, p);
5420 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005422 }
5423 ga_append(&ga, NUL);
5424
5425 if (type == FRD_REPLACE)
5426 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005427 // Do the replacement when the text at the cursor matches. Thus no
5428 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005429 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5430 regmatch.rm_ic = 0;
5431 if (regmatch.regprog != NULL)
5432 {
5433 p = ml_get_cursor();
5434 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5435 && regmatch.startp[0] == p)
5436 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005437 // Clear the command line to remove any old "No match"
5438 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005439 msg_end_prompt();
5440
5441 if (u_save_cursor() == OK)
5442 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005443 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005444 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005445
5446 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005447 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005448 ins_str(repl_text);
5449 }
5450 }
5451 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005452 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005453 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005454 }
5455 }
5456
5457 if (type == FRD_REPLACEALL)
5458 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005459 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005460 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 do_cmdline_cmd(ga.ga_data);
5462 }
5463 else
5464 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005465 int searchflags = SEARCH_MSG + SEARCH_MARK;
5466
Bram Moolenaar30613902019-12-01 22:11:18 +01005467 // Search for the next match.
5468 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005469 if (type == FRD_REPLACE)
5470 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005472 if (down)
5473 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01005474 (void)do_search(NULL, '/', '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005475 }
5476 else
5477 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005478 // We need to escape '?' if and only if we are searching in the up
5479 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005480 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5481 if (p != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01005482 (void)do_search(NULL, '?', '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005483 vim_free(p);
5484 }
5485
Bram Moolenaar30613902019-12-01 22:11:18 +01005486 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488
Bram Moolenaar30613902019-12-01 22:11:18 +01005489 // Don't want to pass did_emsg to other code, it may cause disabling
5490 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005491 did_emsg = save_did_emsg;
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (State & (NORMAL | INSERT))
5494 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005495 gui_update_screen(); // update the screen
5496 msg_didout = 0; // overwrite any message
5497 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499
5500 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005501 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 return (ga.ga_len > 0);
5503}
5504
5505#endif
5506
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005507#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508/*
5509 * Jump to the window at specified point (x, y).
5510 */
5511 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005512gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 int row = Y_2_ROW(y);
5515 int col = X_2_COL(x);
5516 win_T *wp;
5517
5518 if (row >= 0 && col >= 0)
5519 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005520 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (wp != NULL && wp != curwin)
5522 win_goto(wp);
5523 }
5524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005527 * Function passed to handle_drop() for the actions to be done after the
5528 * argument list has been updated.
5529 */
5530 static void
5531drop_callback(void *cookie)
5532{
5533 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005534 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005535
Bram Moolenaar30613902019-12-01 22:11:18 +01005536 // If Shift held down, change to first file's directory. If the first
5537 // item is a directory, change to that directory (and let the explorer
5538 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005539 if (p != NULL)
5540 {
5541 if (mch_isdir(p))
5542 {
5543 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005544 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005545 }
5546 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005547 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005548 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005549 if (do_shorten)
5550 {
5551 shorten_fnames(TRUE);
5552 last_chdir_reason = "drop";
5553 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005554 }
5555
Bram Moolenaar30613902019-12-01 22:11:18 +01005556 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005557 update_screen(NOT_VALID);
5558# ifdef FEAT_MENU
5559 gui_update_menus(0);
5560# endif
5561#ifdef FEAT_TITLE
5562 maketitle();
5563#endif
5564 setcursor();
5565 out_flush_cursor(FALSE, FALSE);
5566}
5567
5568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 * Process file drop. Mouse cursor position, key modifiers, name of files
5570 * and count of files are given. Argument "fnames[count]" has full pathnames
5571 * of dropped files, they will be freed in this function, and caller can't use
5572 * fnames after call this function.
5573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005575gui_handle_drop(
5576 int x UNUSED,
5577 int y UNUSED,
5578 int_u modifiers,
5579 char_u **fnames,
5580 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 int i;
5583 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005584 static int entered = FALSE;
5585
5586 /*
5587 * This function is called by event handlers. Just in case we get a
5588 * second event before the first one is handled, ignore the second one.
5589 * Not sure if this can ever happen, just in case.
5590 */
5591 if (entered)
5592 return;
5593 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 /*
5596 * When the cursor is at the command line, add the file names to the
5597 * command line, don't edit the files.
5598 */
5599 if (State & CMDLINE)
5600 {
5601 shorten_filenames(fnames, count);
5602 for (i = 0; i < count; ++i)
5603 {
5604 if (fnames[i] != NULL)
5605 {
5606 if (i > 0)
5607 add_to_input_buf((char_u*)" ", 1);
5608
Bram Moolenaar30613902019-12-01 22:11:18 +01005609 // We don't know what command is used thus we can't be sure
5610 // about which characters need to be escaped. Only escape the
5611 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612# ifdef BACKSLASH_IN_FILENAME
5613 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5614# else
5615 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5616# endif
5617 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005618 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 vim_free(p);
5620 vim_free(fnames[i]);
5621 }
5622 }
5623 vim_free(fnames);
5624 }
5625 else
5626 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005627 // Go to the window under mouse cursor, then shorten given "fnames" by
5628 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 shorten_filenames(fnames, count);
5631
Bram Moolenaar30613902019-12-01 22:11:18 +01005632 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if ((modifiers & MOUSE_SHIFT) != 0)
5634 p = vim_strsave(fnames[0]);
5635 else
5636 p = NULL;
5637
Bram Moolenaar30613902019-12-01 22:11:18 +01005638 // Handle the drop, :edit or :split to get to the file. This also
5639 // frees fnames[]. Skip this if there is only one item, it's a
5640 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5642 && mch_isdir(fnames[0]))
5643 {
5644 vim_free(fnames[0]);
5645 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005646 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005649 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005650 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005652
5653 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654}
5655#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005656
5657/*
5658 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5659 * applied yet.
5660 * Return the key with modifiers applied if so, NUL if not.
5661 */
5662 int
5663check_for_interrupt(int key, int modifiers_arg)
5664{
5665 int modifiers = modifiers_arg;
5666 int c = merge_modifyOtherKeys(key, &modifiers);
5667
5668 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005669#ifdef UNIX
5670 || (intr_char != Ctrl_C && c == intr_char)
5671#endif
5672 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005673 {
5674 got_int = TRUE;
5675 return c;
5676 }
5677 return NUL;
5678}
5679