blob: 70170666db0f860404de7fb996b7dc17dbcd3d27 [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
13/* Structure containing all the GUI information */
14gui_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 Moolenaard25c16e2016-01-29 22:13:30 +010020static void gui_outstr(char_u *, int);
21static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010022static void gui_delete_lines(int row, int count);
23static void gui_insert_lines(int row, int count);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000024#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000026#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010027static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010028static void gui_update_horiz_scrollbar(int);
29static void gui_set_fg_color(char_u *name);
30static void gui_set_bg_color(char_u *name);
31static win_T *xy2win(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020033#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020035
Bram Moolenaard25c16e2016-01-29 22:13:30 +010036static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020037
38/* Return values for gui_read_child_pipe */
39enum {
40 GUI_CHILD_IO_ERROR,
41 GUI_CHILD_OK,
42 GUI_CHILD_FAILED
43};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020044#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020045
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020047
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int can_update_cursor = TRUE; /* can display the cursor */
Bram Moolenaara338adc2018-01-31 20:51:47 +010049static int disable_flush = 0; /* If > 0, gui_mch_flush() is disabled. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
51/*
52 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
53 * this makes the thumb indicate the part of the text that is shown. Motif
54 * can't do this.
55 */
56#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
57# define SCROLL_PAST_END
58#endif
59
60/*
61 * gui_start -- Called when user wants to start the GUI.
62 *
63 * Careful: This function can be called recursively when there is a ":gui"
64 * command in the .gvimrc file. Only the first call should fork, not the
65 * recursive call.
66 */
67 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +010068gui_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069{
70 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 static int recursive = 0;
72
73 old_term = vim_strsave(T_NAME);
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 settmode(TMODE_COOK); /* stop RAW mode */
76 if (full_screen)
77 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 full_screen = FALSE;
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 ++recursive;
81
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020082#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020083 /*
84 * Quit the current process and continue in the child.
85 * Makes "gvim file" disconnect from the shell it was started in.
86 * Don't do this when Vim was started with "-f" or the 'f' flag is present
87 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020088 * Don't do this when there is a running job, we can only get the status
89 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020091 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
92# ifdef FEAT_JOB_CHANNEL
93 && !job_any_running()
94# endif
95 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020096 {
97 gui_do_fork();
98 }
99 else
100#endif
101 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200102#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200103 /* If there is 'f' in 'guioptions' and specify -g argument,
104 * gui_mch_init_check() was not called yet. */
105 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100106 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200107#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200108 gui_attempt_start();
109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111 if (!gui.in_use) /* failed to start GUI */
112 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200113 /* Back to old term settings
114 *
115 * FIXME: If we got here because a child process failed and flagged to
116 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
117 * hit an X11 I/O error and do a longjmp(), leaving recursive
118 * permanently set to 1. This is probably not as big a problem as it
119 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
120 * return "OK" unconditionally, so it would be very difficult to
121 * actually hit this case.
122 */
123 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 settmode(TMODE_RAW); /* restart RAW mode */
125#ifdef FEAT_TITLE
126 set_title_defaults(); /* set 'title' and 'icon' again */
127#endif
128 }
129
130 vim_free(old_term);
131
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200132 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
133 * the GUIFailed event. */
134 gui_mch_update();
135 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
136 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200137 --recursive;
138}
139
140/*
141 * Set_termname() will call gui_init() to start the GUI.
142 * Set the "starting" flag, to indicate that the GUI will start.
143 *
144 * We don't want to open the GUI shell until after we've read .gvimrc,
145 * otherwise we don't know what font we will use, and hence we don't know
146 * what size the shell should be. So if there are errors in the .gvimrc
147 * file, they will have to go to the terminal: Set full_screen to FALSE.
148 * full_screen will be set to TRUE again by a successful termcapinit().
149 */
150 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100151gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200152{
153 static int recursive = 0;
154
155 ++recursive;
156 gui.starting = TRUE;
157
158#ifdef FEAT_GUI_GTK
159 gui.event_time = GDK_CURRENT_TIME;
160#endif
161
162 termcapinit((char_u *)"builtin_gui");
163 gui.starting = recursive - 1;
164
Bram Moolenaar9372a112005-12-06 19:59:18 +0000165#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {
168# ifdef FEAT_EVAL
169 Window x11_window;
170 Display *x11_display;
171
172 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
173 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
174# endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 /* Display error messages in a dialog now. */
177 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 --recursive;
181}
182
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200183#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200184
185/* for waitpid() */
186# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
187# include <sys/wait.h>
188# endif
189
190/*
191 * Create a new process, by forking. In the child, start the GUI, and in
192 * the parent, exit.
193 *
194 * If something goes wrong, this will return with gui.in_use still set
195 * to FALSE, in which case the caller should continue execution without
196 * the GUI.
197 *
198 * If the child fails to start the GUI, then the child will exit and the
199 * parent will return. If the child succeeds, then the parent will exit
200 * and the child will return.
201 */
202 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100203gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200204{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200205 int pipefd[2]; /* pipe between parent and child */
206 int pipe_error;
207 int status;
208 int exit_status;
209 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200210
211 /* Setup a pipe between the child and the parent, so that the parent
212 * knows when the child has done the setsid() call and is allowed to
213 * exit. */
214 pipe_error = (pipe(pipefd) < 0);
215 pid = fork();
216 if (pid < 0) /* Fork error */
217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200219 return;
220 }
221 else if (pid > 0) /* Parent */
222 {
223 /* Give the child some time to do the setsid(), otherwise the
224 * exit() may kill the child too (when starting gvim from inside a
225 * gvim). */
226 if (!pipe_error)
227 {
228 /* The read returns when the child closes the pipe (or when
229 * the child dies for some reason). */
230 close(pipefd[1]);
231 status = gui_read_child_pipe(pipefd[0]);
232 if (status == GUI_CHILD_FAILED)
233 {
234 /* The child failed to start the GUI, so the caller must
235 * continue. There may be more error information written
236 * to stderr by the child. */
237# ifdef __NeXT__
238 wait4(pid, &exit_status, 0, (struct rusage *)0);
239# else
240 waitpid(pid, &exit_status, 0);
241# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200243 return;
244 }
245 else if (status == GUI_CHILD_IO_ERROR)
246 {
247 pipe_error = TRUE;
248 }
249 /* else GUI_CHILD_OK: parent exit */
250 }
251
252 if (pipe_error)
253 ui_delay(300L, TRUE);
254
255 /* When swapping screens we may need to go to the next line, e.g.,
256 * after a hit-enter prompt and using ":gui". */
257 if (newline_on_exit)
258 mch_errmsg("\r\n");
259
260 /*
261 * The parent must skip the normal exit() processing, the child
262 * will do it. For example, GTK messes up signals when exiting.
263 */
264 _exit(0);
265 }
266 /* Child */
267
Bram Moolenaar29695702012-05-18 17:03:18 +0200268#ifdef FEAT_GUI_GTK
269 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
270 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100271 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200272#endif
273
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200274# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
275 /*
276 * Change our process group. On some systems/shells a CTRL-C in the
277 * shell where Vim was started would otherwise kill gvim!
278 */
279# if defined(HAVE_SETSID)
280 (void)setsid();
281# else
282 (void)setpgid(0, 0);
283# endif
284# endif
285 if (!pipe_error)
286 close(pipefd[0]);
287
288# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
289 /* Tell the session manager our new PID */
290 gui_mch_forked();
291# endif
292
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200293 /* Try to start the GUI */
294 gui_attempt_start();
295
296 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200297 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200298 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200299 if (gui.in_use)
300 write_eintr(pipefd[1], "ok", 3);
301 else
302 write_eintr(pipefd[1], "fail", 5);
303 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200304 }
305
306 /* If we failed to start the GUI, exit now. */
307 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100308 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200309}
310
311/*
312 * Read from a pipe assumed to be connected to the child process (this
313 * function is called from the parent).
314 * Return GUI_CHILD_OK if the child successfully started the GUI,
315 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
316 * some other error.
317 *
318 * The file descriptor will be closed before the function returns.
319 */
320 static int
321gui_read_child_pipe(int fd)
322{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200323 long bytes_read;
324#define READ_BUFFER_SIZE 10
325 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200326
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
328#undef READ_BUFFER_SIZE
329 close(fd);
330 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200332 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200333 if (strcmp(buffer, "ok") == 0)
334 return GUI_CHILD_OK;
335 return GUI_CHILD_FAILED;
336}
337
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200338#endif /* GUI_MAY_FORK */
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340/*
341 * Call this when vim starts up, whether or not the GUI is started
342 */
343 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100344gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346 gui.in_use = FALSE; /* No GUI yet (maybe later) */
347 gui.starting = FALSE; /* No GUI yet (maybe later) */
348 gui_mch_prepare(argc, argv);
349}
350
351/*
352 * Try initializing the GUI and check if it can be started.
353 * Used from main() to check early if "vim -g" can start the GUI.
354 * Used from gui_init() to prepare for starting the GUI.
355 * Returns FAIL or OK.
356 */
357 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100358gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360 static int result = MAYBE;
361
362 if (result != MAYBE)
363 {
364 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 return result;
367 }
368
369 gui.shell_created = FALSE;
370 gui.dying = FALSE;
371 gui.in_focus = TRUE; /* so the guicursor setting works */
372 gui.dragged_sb = SBAR_NONE;
373 gui.dragged_wp = NULL;
374 gui.pointer_hidden = FALSE;
375 gui.col = 0;
376 gui.row = 0;
377 gui.num_cols = Columns;
378 gui.num_rows = Rows;
379
380 gui.cursor_is_valid = FALSE;
381 gui.scroll_region_top = 0;
382 gui.scroll_region_bot = Rows - 1;
383 gui.scroll_region_left = 0;
384 gui.scroll_region_right = Columns - 1;
385 gui.highlight_mask = HL_NORMAL;
386 gui.char_width = 1;
387 gui.char_height = 1;
388 gui.char_ascent = 0;
389 gui.border_width = 0;
390
391 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200392#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 gui.bold_font = NOFONT;
394 gui.ital_font = NOFONT;
395 gui.boldital_font = NOFONT;
396# ifdef FEAT_XFONTSET
397 gui.fontset = NOFONTSET;
398# endif
399#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200400 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100401#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200402 gui.wide_bold_font = NOFONT;
403 gui.wide_ital_font = NOFONT;
404 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200408# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# ifdef FONTSET_ALWAYS
410 gui.menu_fontset = NOFONTSET;
411# else
412 gui.menu_font = NOFONT;
413# endif
414# endif
415 gui.menu_is_active = TRUE; /* default: include menu */
416# ifndef FEAT_GUI_GTK
417 gui.menu_height = MENU_DEFAULT_HEIGHT;
418 gui.menu_width = 0;
419# endif
420#endif
421#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
422 gui.toolbar_height = 0;
423#endif
424#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
425 gui.footer_height = 0;
426#endif
427#ifdef FEAT_BEVAL_TIP
428 gui.tooltip_fontset = NOFONTSET;
429#endif
430
431 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
432 gui.prev_wrap = -1;
433
434#ifdef ALWAYS_USE_GUI
435 result = OK;
436#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200437# ifdef FEAT_GUI_GTK
438 /*
439 * Note: Don't call gtk_init_check() before fork, it will be called after
440 * the fork. When calling it before fork, it make vim hang for a while.
441 * See gui_do_fork().
442 * Use a simpler check if the GUI window can probably be opened.
443 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200444 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200445# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448#endif
449 return result;
450}
451
452/*
453 * This is the call which starts the GUI.
454 */
455 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100456gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 win_T *wp;
459 static int recursive = 0;
460
461 /*
462 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
463 * function will then be executed at the first call, the rest by the
464 * recursive call. This allow the shell to be opened halfway reading a
465 * gvimrc file.
466 */
467 if (!recursive)
468 {
469 ++recursive;
470
471 clip_init(TRUE);
472
473 /* If can't initialize, don't try doing the rest */
474 if (gui_init_check() == FAIL)
475 {
476 --recursive;
477 clip_init(FALSE);
478 return;
479 }
480
481 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000482 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
483 * breaks the Paste toolbar button.
484 */
485 set_option_value((char_u *)"paste", 0L, NULL, 0);
486
487 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 * Set up system-wide default menus.
489 */
490#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
491 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
492 {
493 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000494 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 sys_menu = FALSE;
496 }
497#endif
498
499 /*
500 * Switch on the mouse by default, unless the user changed it already.
501 * This can then be changed in the .gvimrc.
502 */
503 if (!option_was_set((char_u *)"mouse"))
504 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000505 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
507 /*
508 * If -U option given, use only the initializations from that file and
509 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
510 */
511 if (use_gvimrc != NULL)
512 {
513 if (STRCMP(use_gvimrc, "NONE") != 0
514 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000515 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100516 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
520 /*
521 * Get system wide defaults for gvim, only when file name defined.
522 */
523#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000524 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526
527 /*
528 * Try to read GUI initialization commands from the following
529 * places:
530 * - environment variable GVIMINIT
531 * - the user gvimrc file (~/.gvimrc)
532 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
533 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
534 * The first that exists is used, the rest is ignored.
535 */
536 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000537 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
538 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000540 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
541 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200543#ifdef USR_GVIMRC_FILE3
544 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
545 DOSO_GVIMRC) == FAIL
546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 )
548 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200549#ifdef USR_GVIMRC_FILE4
550 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
552 }
553
554 /*
555 * Read initialization commands from ".gvimrc" in current
556 * directory. This is only done if the 'exrc' option is set.
557 * Because of security reasons we disallow shell and write
558 * commands now, except for unix if the file is owned by the user
559 * or 'secure' option has been reset in environment of global
560 * ".gvimrc".
561 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
562 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
563 */
564 if (p_exrc)
565 {
566#ifdef UNIX
567 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200568 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
570 /* if ".gvimrc" file is not owned by user, set 'secure'
571 * mode */
572 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
573 secure = p_secure;
574 }
575#else
576 secure = p_secure;
577#endif
578
579 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
580 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
581#ifdef SYS_GVIMRC_FILE
582 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
583 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
584#endif
585#ifdef USR_GVIMRC_FILE2
586 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
587 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
588#endif
589#ifdef USR_GVIMRC_FILE3
590 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
591 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
592#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200593#ifdef USR_GVIMRC_FILE4
594 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
595 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000598 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600 if (secure == 2)
601 need_wait_return = TRUE;
602 secure = 0;
603 }
604 }
605
606 if (need_wait_return || msg_didany)
607 wait_return(TRUE);
608
609 --recursive;
610 }
611
612 /* If recursive call opened the shell, return here from the first call */
613 if (gui.in_use)
614 return;
615
616 /*
617 * Create the GUI shell.
618 */
619 gui.in_use = TRUE; /* Must be set after menus have been set up */
620 if (gui_mch_init() == FAIL)
621 goto error;
622
623 /* Avoid a delay for an error message that was printed in the terminal
624 * where Vim was started. */
625 emsg_on_display = FALSE;
626 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100627 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 need_wait_return = FALSE;
629 msg_didany = FALSE;
630
631 /*
632 * Check validity of any generic resources that may have been loaded.
633 */
634 if (gui.border_width < 0)
635 gui.border_width = 0;
636
637 /*
638 * Set up the fonts. First use a font specified with "-fn" or "-font".
639 */
640 if (font_argument != NULL)
641 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
642 if (
643#ifdef FEAT_XFONTSET
644 (*p_guifontset == NUL
645 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
646#endif
647 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
648 : p_guifont, FALSE) == FAIL)
649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100650 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 goto error2;
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100654 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656 gui.num_cols = Columns;
657 gui.num_rows = Rows;
658 gui_reset_scroll_region();
659
660 /* Create initial scrollbars */
661 FOR_ALL_WINDOWS(wp)
662 {
663 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
664 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
665 }
666 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
667
668#ifdef FEAT_MENU
669 gui_create_initial_menus(root_menu);
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_SIGN_ICONS
672 sign_gui_started();
673#endif
674
675 /* Configure the desired menu and scrollbars */
676 gui_init_which_components(NULL);
677
678 /* All components of the GUI have been created now */
679 gui.shell_created = TRUE;
680
681#ifndef FEAT_GUI_GTK
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100682 // Set the shell size, adjusted for the screen size. For GTK this only
683 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100684 // If the window is already maximized (e.g. when --windowid is passed in),
685 // we want to use the system-provided dimensions by passing FALSE to
686 // mustset. Otherwise, we want to initialize with the default rows/columns.
687 if (gui_mch_maximized())
688 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
689 else
690 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#endif
692#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
693 /* Need to set the size of the menubar after all the menus have been
694 * created. */
695 gui_mch_compute_menu_height((Widget)0);
696#endif
697
698 /*
699 * Actually open the GUI shell.
700 */
701 if (gui_mch_open() != FAIL)
702 {
703#ifdef FEAT_TITLE
704 maketitle();
705 resettitle();
706#endif
707 init_gui_options();
708#ifdef FEAT_ARABIC
709 /* Our GUI can't do bidi. */
710 p_tbidi = FALSE;
711#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000712#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 /* Give GTK+ a chance to put all widget's into place. */
714 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000715
716# ifdef FEAT_MENU
717 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
718 * It was still there to make F10 work. */
719 if (vim_strchr(p_go, GO_MENUS) == NULL)
720 {
721 --gui.starting;
722 gui_mch_enable_menu(FALSE);
723 ++gui.starting;
724 gui_mch_update();
725 }
726# endif
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 /* Now make sure the shell fits on the screen. */
Bram Moolenaar372674f2019-03-30 20:31:22 +0100729 if (gui_mch_maximized())
730 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
731 else
732 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000734 /* When 'lines' was set while starting up the topframe may have to be
735 * resized. */
736 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000737
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100738#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000739 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100740 * 'ballooneval' is off. */
741 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200742 {
743# ifdef FEAT_VARTABS
744 vim_free(balloonEval->vts);
745# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100746 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200747 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100748 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000749# ifdef FEAT_GUI_GTK
750 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
751 &general_beval_cb, NULL);
752# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000753# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000754 {
755 extern Widget textArea;
756 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000757 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000758 }
759# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100760# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000761 balloonEval = gui_mch_create_beval_area(NULL, NULL,
762 &general_beval_cb, NULL);
763# endif
764# endif
765# endif
766 if (!p_beval)
767 gui_mch_disable_beval_area(balloonEval);
768#endif
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
771 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000774 /* When 'cmdheight' was set during startup it may not have taken
775 * effect yet. */
776 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000777 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 return;
780 }
781
782error2:
783#ifdef FEAT_GUI_X11
784 /* undo gui_mch_init() */
785 gui_mch_uninit();
786#endif
787
788error:
789 gui.in_use = FALSE;
790 clip_init(FALSE);
791}
792
793
794 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100795gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /* don't free the fonts, it leads to a BUS error
798 * richard@whitequeen.com Jul 99 */
799 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 gui.in_use = FALSE;
801 gui_mch_exit(rc);
802}
803
Bram Moolenaar9372a112005-12-06 19:59:18 +0000804#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000806# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807/*
808 * Called when the GUI shell is closed by the user. If there are no changed
809 * files Vim exits, otherwise there will be a dialog to ask the user what to
810 * do.
811 * When this function returns, Vim should NOT exit!
812 */
813 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100814gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 cmdmod_T save_cmdmod;
817
818 save_cmdmod = cmdmod;
819
820 /* Only exit when there are no changed files */
821 exiting = TRUE;
822# ifdef FEAT_BROWSE
823 cmdmod.browse = TRUE;
824# endif
825# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
826 cmdmod.confirm = TRUE;
827# endif
828 /* If there are changed buffers, present the user with a dialog if
829 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100830 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 getout(0);
832
833 exiting = FALSE;
834 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000835 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836}
837#endif
838
839/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200840 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 * first font name that works is used. If none is found, use the default
842 * font.
843 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
844 * Return OK when able to set the font. When it failed FAIL is returned and
845 * the fonts are unchanged.
846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100848gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850#define FONTLEN 320
851 char_u font_name[FONTLEN];
852 int font_list_empty = FALSE;
853 int ret = FAIL;
854
855 if (!gui.in_use)
856 return FAIL;
857
858 font_name[0] = NUL;
859 if (*font_list == NUL)
860 font_list_empty = TRUE;
861 else
862 {
863#ifdef FEAT_XFONTSET
864 /* When using a fontset, the whole list of fonts is one name. */
865 if (fontset)
866 ret = gui_mch_init_font(font_list, TRUE);
867 else
868#endif
869 while (*font_list != NUL)
870 {
871 /* Isolate one comma separated font name. */
872 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
873
874 /* Careful!!! The Win32 version of gui_mch_init_font(), when
875 * called with "*" will change p_guifont to the selected font
876 * name, which frees the old value. This makes font_list
877 * invalid. Thus when OK is returned here, font_list must no
878 * longer be used! */
879 if (gui_mch_init_font(font_name, FALSE) == OK)
880 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100881#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 /* If it's a Unicode font, try setting 'guifontwide' to a
883 * similar double-width font. */
884 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
885 && strstr((char *)font_name, "10646") != NULL)
886 set_guifontwide(font_name);
887#endif
888 ret = OK;
889 break;
890 }
891 }
892 }
893
894 if (ret != OK
895 && STRCMP(font_list, "*") != 0
896 && (font_list_empty || gui.norm_font == NOFONT))
897 {
898 /*
899 * Couldn't load any font in 'font_list', keep the current font if
900 * there is one. If 'font_list' is empty, or if there is no current
901 * font, tell gui_mch_init_font() to try to find a font we can load.
902 */
903 ret = gui_mch_init_font(NULL, FALSE);
904 }
905
906 if (ret == OK)
907 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200908#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 /* Set normal font as current font */
910# ifdef FEAT_XFONTSET
911 if (gui.fontset != NOFONTSET)
912 gui_mch_set_fontset(gui.fontset);
913 else
914# endif
915 gui_mch_set_font(gui.norm_font);
916#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100917 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 return ret;
921}
922
Bram Moolenaar13505972019-01-24 15:04:48 +0100923#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924/*
925 * Try setting 'guifontwide' to a font twice as wide as "name".
926 */
927 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100928set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 int i = 0;
931 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
932 char_u *wp = NULL;
933 char_u *p;
934 GuiFont font;
935
936 wp = wide_name;
937 for (p = name; *p != NUL; ++p)
938 {
939 *wp++ = *p;
940 if (*p == '-')
941 {
942 ++i;
943 if (i == 6) /* font type: change "--" to "-*-" */
944 {
945 if (p[1] == '-')
946 *wp++ = '*';
947 }
948 else if (i == 12) /* found the width */
949 {
950 ++p;
951 i = getdigits(&p);
952 if (i != 0)
953 {
954 /* Double the width specification. */
955 sprintf((char *)wp, "%d%s", i * 2, p);
956 font = gui_mch_get_font(wide_name, FALSE);
957 if (font != NOFONT)
958 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000959 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 gui.wide_font = font;
961 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000962 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
964 }
965 break;
966 }
967 }
968 }
969}
Bram Moolenaar13505972019-01-24 15:04:48 +0100970#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972/*
973 * Get the font for 'guifontwide'.
974 * Return FAIL for an invalid font name.
975 */
976 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100977gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 GuiFont font = NOFONT;
980 char_u font_name[FONTLEN];
981 char_u *p;
982
983 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
984 return OK; /* Will give an error message later. */
985
986 if (p_guifontwide != NULL && *p_guifontwide != NUL)
987 {
988 for (p = p_guifontwide; *p != NUL; )
989 {
990 /* Isolate one comma separated font name. */
991 (void)copy_option_part(&p, font_name, FONTLEN, ",");
992 font = gui_mch_get_font(font_name, FALSE);
993 if (font != NOFONT)
994 break;
995 }
996 if (font == NOFONT)
997 return FAIL;
998 }
999
1000 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001001#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1003 if (font != NOFONT && gui.norm_font != NOFONT
1004 && pango_font_description_equal(font, gui.norm_font))
1005 {
1006 gui.wide_font = NOFONT;
1007 gui_mch_free_font(font);
1008 }
1009 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001012#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001013 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001014#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001015 /*
1016 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1017 * support those fonts for 'guifontwide'.
1018 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 return OK;
1021}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001024gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026 gui.row = row;
1027 gui.col = col;
1028}
1029
1030/*
1031 * gui_check_pos - check if the cursor is on the screen.
1032 */
1033 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001034gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036 if (gui.row >= screen_Rows)
1037 gui.row = screen_Rows - 1;
1038 if (gui.col >= screen_Columns)
1039 gui.col = screen_Columns - 1;
1040 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1041 gui.cursor_is_valid = FALSE;
1042}
1043
1044/*
1045 * Redraw the cursor if necessary or when forced.
1046 * Careful: The contents of ScreenLines[] must match what is on the screen,
1047 * otherwise this goes wrong. May need to call out_flush() first.
1048 */
1049 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001050gui_update_cursor(
1051 int force, /* when TRUE, update even when not moved */
1052 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 int cur_width = 0;
1055 int cur_height = 0;
1056 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001057 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001059#ifdef FEAT_TERMINAL
1060 guicolor_T shape_fg = INVALCOLOR;
1061 guicolor_T shape_bg = INVALCOLOR;
1062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1064 int cattr; /* cursor attributes */
1065 int attr;
1066 attrentry_T *aep = NULL;
1067
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001068 /* Don't update the cursor when halfway busy scrolling or the screen size
1069 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1070 if (!can_update_cursor || screen_Columns != gui.num_cols
1071 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return;
1073
1074 gui_check_pos();
1075 if (!gui.cursor_is_valid || force
1076 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1077 {
1078 gui_undraw_cursor();
1079 if (gui.row < 0)
1080 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001081#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1083 im_set_position(gui.row, gui.col);
1084#endif
1085 gui.cursor_row = gui.row;
1086 gui.cursor_col = gui.col;
1087
1088 /* Only write to the screen after ScreenLines[] has been initialized */
1089 if (!screen_cleared || ScreenLines == NULL)
1090 return;
1091
1092 /* Clear the selection if we are about to write over it */
1093 if (clear_selection)
1094 clip_may_clear_selection(gui.row, gui.row);
1095 /* Check that the cursor is inside the shell (resizing may have made
1096 * it invalid) */
1097 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1098 return;
1099
1100 gui.cursor_is_valid = TRUE;
1101
1102 /*
1103 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001104 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001106#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001107 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001108 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001110#endif
1111 shape = &shape_table[get_shape_idx(FALSE)];
1112 if (State & LANGMAP)
1113 id = shape->id_lm;
1114 else
1115 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
1117 /* get the colors and attributes for the cursor. Default is inverted */
1118 cfg = INVALCOLOR;
1119 cbg = INVALCOLOR;
1120 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001121 gui_mch_set_blinking(shape->blinkwait,
1122 shape->blinkon,
1123 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001124 if (shape->blinkwait == 0 || shape->blinkon == 0
1125 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001126 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001127#ifdef FEAT_TERMINAL
1128 if (shape_bg != INVALCOLOR)
1129 {
1130 cattr = 0;
1131 cfg = shape_fg;
1132 cbg = shape_bg;
1133 }
1134 else
1135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (id > 0)
1137 {
1138 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001139#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
1141 static int iid;
1142 guicolor_T fg, bg;
1143
Bram Moolenaarc236c162008-07-13 17:41:49 +00001144 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001145# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001146 preedit_get_status()
1147# else
1148 im_get_status()
1149# endif
1150 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 iid = syn_name2id((char_u *)"CursorIM");
1153 if (iid > 0)
1154 {
1155 syn_id2colors(iid, &fg, &bg);
1156 if (bg != INVALCOLOR)
1157 cbg = bg;
1158 if (fg != INVALCOLOR)
1159 cfg = fg;
1160 }
1161 }
1162 }
1163#endif
1164 }
1165
1166 /*
1167 * Get the attributes for the character under the cursor.
1168 * When no cursor color was given, use the character color.
1169 */
1170 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1171 if (attr > HL_ALL)
1172 aep = syn_gui_attr2entry(attr);
1173 if (aep != NULL)
1174 {
1175 attr = aep->ae_attr;
1176 if (cfg == INVALCOLOR)
1177 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1178 : aep->ae_u.gui.fg_color);
1179 if (cbg == INVALCOLOR)
1180 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1181 : aep->ae_u.gui.bg_color);
1182 }
1183 if (cfg == INVALCOLOR)
1184 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1185 if (cbg == INVALCOLOR)
1186 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1187
1188#ifdef FEAT_XIM
1189 if (aep != NULL)
1190 {
1191 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1192 : aep->ae_u.gui.bg_color);
1193 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1194 : aep->ae_u.gui.fg_color);
1195 if (xim_bg_color == INVALCOLOR)
1196 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1197 : gui.back_pixel;
1198 if (xim_fg_color == INVALCOLOR)
1199 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1200 : gui.norm_pixel;
1201 }
1202 else
1203 {
1204 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1205 : gui.back_pixel;
1206 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1207 : gui.norm_pixel;
1208 }
1209#endif
1210
1211 attr &= ~HL_INVERSE;
1212 if (cattr & HL_INVERSE)
1213 {
1214 cc = cbg;
1215 cbg = cfg;
1216 cfg = cc;
1217 }
1218 cattr &= ~HL_INVERSE;
1219
1220 /*
1221 * When we don't have window focus, draw a hollow cursor.
1222 */
1223 if (!gui.in_focus)
1224 {
1225 gui_mch_draw_hollow_cursor(cbg);
1226 return;
1227 }
1228
1229 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001230 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_HANGULIN
1232 || composing_hangul
1233#endif
1234 )
1235 {
1236 /*
1237 * Draw the text character with the cursor colors. Use the
1238 * character attributes plus the cursor attributes.
1239 */
1240 gui.highlight_mask = (cattr | attr);
1241#ifdef FEAT_HANGULIN
1242 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001243 {
1244 char_u *comp_buf;
1245 int comp_len;
1246
1247 comp_buf = hangul_composing_buffer_get(&comp_len);
1248 if (comp_buf)
1249 {
1250 (void)gui_outstr_nowrap(comp_buf, comp_len,
1251 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1252 cfg, cbg, 0);
1253 vim_free(comp_buf);
1254 }
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else
1257#endif
1258 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1259 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1260 }
1261 else
1262 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001263#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 int col_off = FALSE;
1265#endif
1266 /*
1267 * First draw the partial cursor, then overwrite with the text
1268 * character, using a transparent background.
1269 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001270 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001273 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 else
1276 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001277 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 cur_width = gui.char_width;
1279 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001280 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1281 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
1283 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001284 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001286#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (CURSOR_BAR_RIGHT)
1288 {
1289 /* gui.col points to the left halve of the character but
1290 * the vertical line needs to be on the right halve.
1291 * A double-wide horizontal line is also drawn from the
1292 * right halve in gui_mch_draw_part_cursor(). */
1293 col_off = TRUE;
1294 ++gui.col;
1295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001299#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (col_off)
1301 --gui.col;
1302#endif
1303
1304#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1305 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1306 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1307 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1308 (guicolor_T)0, (guicolor_T)0, 0);
1309#endif
1310 }
1311 gui.highlight_mask = old_hl_mask;
1312 }
1313}
1314
1315#if defined(FEAT_MENU) || defined(PROTO)
1316 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001317gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001319# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (gui.menu_is_active && gui.in_use)
1321 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1322# endif
1323}
1324#endif
1325
1326/*
1327 * Position the various GUI components (text area, menu). The vertical
1328 * scrollbars are NOT handled here. See gui_update_scrollbars().
1329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001331gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 int text_area_x;
1334 int text_area_y;
1335 int text_area_width;
1336 int text_area_height;
1337
1338 /* avoid that moving components around generates events */
1339 ++hold_gui_events;
1340
1341 text_area_x = 0;
1342 if (gui.which_scrollbars[SBAR_LEFT])
1343 text_area_x += gui.scrollbar_width;
1344
1345 text_area_y = 0;
1346#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1347 gui.menu_width = total_width;
1348 if (gui.menu_is_active)
1349 text_area_y += gui.menu_height;
1350#endif
1351#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1352 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1353 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1354#endif
1355
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001356# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001357 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001358 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001359 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1363 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1364 {
1365# ifdef FEAT_GUI_ATHENA
1366 gui_mch_set_toolbar_pos(0, text_area_y,
1367 gui.menu_width, gui.toolbar_height);
1368# endif
1369 text_area_y += gui.toolbar_height;
1370 }
1371#endif
1372
1373 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1374 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1375
1376 gui_mch_set_text_area_pos(text_area_x,
1377 text_area_y,
1378 text_area_width,
1379 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001380#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 + xim_get_status_area_height()
1382#endif
1383 );
1384#ifdef FEAT_MENU
1385 gui_position_menu();
1386#endif
1387 if (gui.which_scrollbars[SBAR_BOTTOM])
1388 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1389 text_area_x,
1390 text_area_y + text_area_height,
1391 text_area_width,
1392 gui.scrollbar_height);
1393 gui.left_sbar_x = 0;
1394 gui.right_sbar_x = text_area_x + text_area_width;
1395
1396 --hold_gui_events;
1397}
1398
Bram Moolenaar02743632005-07-25 20:42:36 +00001399/*
1400 * Get the width of the widgets and decorations to the side of the text area.
1401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001403gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
1405 int base_width;
1406
1407 base_width = 2 * gui.border_offset;
1408 if (gui.which_scrollbars[SBAR_LEFT])
1409 base_width += gui.scrollbar_width;
1410 if (gui.which_scrollbars[SBAR_RIGHT])
1411 base_width += gui.scrollbar_width;
1412 return base_width;
1413}
1414
Bram Moolenaar02743632005-07-25 20:42:36 +00001415/*
1416 * Get the height of the widgets and decorations above and below the text area.
1417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001419gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 int base_height;
1422
1423 base_height = 2 * gui.border_offset;
1424 if (gui.which_scrollbars[SBAR_BOTTOM])
1425 base_height += gui.scrollbar_height;
1426#ifdef FEAT_GUI_GTK
1427 /* We can't take the sizes properly into account until anything is
1428 * realized. Therefore we recalculate all the values here just before
1429 * setting the size. (--mdcki) */
1430#else
1431# ifdef FEAT_MENU
1432 if (gui.menu_is_active)
1433 base_height += gui.menu_height;
1434# endif
1435# ifdef FEAT_TOOLBAR
1436 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1437# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1438 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1439# else
1440 base_height += gui.toolbar_height;
1441# endif
1442# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001443# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1444 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001445 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001446 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448# ifdef FEAT_FOOTER
1449 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1450 base_height += gui.footer_height;
1451# endif
1452# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1453 base_height += gui_mch_text_area_extra_height();
1454# endif
1455#endif
1456 return base_height;
1457}
1458
1459/*
1460 * Should be called after the GUI shell has been resized. Its arguments are
1461 * the new width and height of the shell in pixels.
1462 */
1463 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001464gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 static int busy = FALSE;
1467
1468 if (!gui.shell_created) /* ignore when still initializing */
1469 return;
1470
1471 /*
1472 * Can't resize the screen while it is being redrawn. Remember the new
1473 * size and handle it later.
1474 */
1475 if (updating_screen || busy)
1476 {
1477 new_pixel_width = pixel_width;
1478 new_pixel_height = pixel_height;
1479 return;
1480 }
1481
1482again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001483 new_pixel_width = 0;
1484 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 busy = TRUE;
1486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 /* Flush pending output before redrawing */
1488 out_flush();
1489
1490 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001491 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /*
1497 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1498 * at the last line here (why does it have to be one row too low?).
1499 */
1500 if (State == ASKMORE || State == CONFIRM)
1501 gui.row = gui.num_rows;
1502
1503 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1504 * the safe side. */
1505 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1506 || gui.num_rows != Rows || gui.num_cols != Columns)
1507 shell_resized();
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 gui_update_scrollbars(TRUE);
1510 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001511#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 xim_set_status_area();
1513#endif
1514
1515 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001516
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001517 /* We may have been called again while redrawing the screen.
1518 * Need to do it all again with the latest size then. But only if the size
1519 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (new_pixel_height)
1521 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001522 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1523 {
1524 new_pixel_width = 0;
1525 new_pixel_height = 0;
1526 }
1527 else
1528 {
1529 pixel_width = new_pixel_width;
1530 pixel_height = new_pixel_height;
1531 goto again;
1532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534}
1535
1536/*
1537 * Check if gui_resize_shell() must be called.
1538 */
1539 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001540gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /* careful: gui_resize_shell() may postpone the resize again if we
1544 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001545 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546}
1547
1548 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001549gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
1551 Rows = gui.num_rows;
1552 Columns = gui.num_cols;
1553 return OK;
1554}
1555
1556/*
1557 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001558 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1559 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001560 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1561 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001564gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001565 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001566 int fit_to_display,
1567 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 int base_width;
1570 int base_height;
1571 int width;
1572 int height;
1573 int min_width;
1574 int min_height;
1575 int screen_w;
1576 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001577#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001578 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001579 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001580#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001581 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 if (!gui.shell_created)
1584 return;
1585
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001586#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 /* If not setting to a user specified size and maximized, calculate the
1588 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001589 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1590 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 gui_mch_newfont();
1593 return;
1594 }
1595#endif
1596
1597 base_width = gui_get_base_width();
1598 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001599 if (fit_to_display)
1600 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001601 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001602
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001603 width = Columns * gui.char_width + base_width;
1604 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 if (fit_to_display)
1607 {
1608 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001609 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 Columns = (screen_w - base_width) / gui.char_width;
1612 if (Columns < MIN_COLUMNS)
1613 Columns = MIN_COLUMNS;
1614 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001615#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001616 ++did_adjust;
1617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001619 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 Rows = (screen_h - base_height) / gui.char_height;
1622 check_shellsize();
1623 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001624#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001625 ++did_adjust;
1626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001628#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001629 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1630 && height + gui.char_height >= screen_h))
1631 /* don't unmaximize if at maximum size */
1632 un_maximize = FALSE;
1633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001635 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 gui.num_cols = Columns;
1637 gui.num_rows = Rows;
1638
1639 min_width = base_width + MIN_COLUMNS * gui.char_width;
1640 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001641 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001642
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001643#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001644 if (un_maximize)
1645 {
1646 /* If the window size is smaller than the screen unmaximize the
1647 * window, otherwise resizing won't work. */
1648 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1649 if ((width + gui.char_width < screen_w
1650 || height + gui.char_height * 2 < screen_h)
1651 && gui_mch_maximized())
1652 gui_mch_unmaximize();
1653 }
1654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001657 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001659 if (fit_to_display && x >= 0 && y >= 0)
1660 {
1661 /* Some window managers put the Vim window left of/above the screen.
1662 * Only change the position if it wasn't already negative before
1663 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 gui_mch_update();
1665 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1666 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1667 }
1668
1669 gui_position_components(width);
1670 gui_update_scrollbars(TRUE);
1671 gui_reset_scroll_region();
1672}
1673
1674/*
1675 * Called when Rows and/or Columns has changed.
1676 */
1677 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001678gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 gui_reset_scroll_region();
1681}
1682
1683/*
1684 * Make scroll region cover whole screen.
1685 */
1686 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001687gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 gui.scroll_region_top = 0;
1690 gui.scroll_region_bot = gui.num_rows - 1;
1691 gui.scroll_region_left = 0;
1692 gui.scroll_region_right = gui.num_cols - 1;
1693}
1694
1695 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001696gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 if (mask > HL_ALL) /* highlight code */
1699 gui.highlight_mask = mask;
1700 else /* mask */
1701 gui.highlight_mask |= mask;
1702}
1703
1704 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001705gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 if (mask > HL_ALL) /* highlight code */
1708 gui.highlight_mask = HL_NORMAL;
1709 else /* mask */
1710 gui.highlight_mask &= ~mask;
1711}
1712
1713/*
1714 * Clear a rectangular region of the screen from text pos (row1, col1) to
1715 * (row2, col2) inclusive.
1716 */
1717 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001718gui_clear_block(
1719 int row1,
1720 int col1,
1721 int row2,
1722 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 /* Clear the selection if we are about to write over it */
1725 clip_may_clear_selection(row1, row2);
1726
1727 gui_mch_clear_block(row1, col1, row2, col2);
1728
1729 /* Invalidate cursor if it was in this block */
1730 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1731 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1732 gui.cursor_is_valid = FALSE;
1733}
1734
1735/*
1736 * Write code to update the cursor later. This avoids the need to flush the
1737 * output buffer before calling gui_update_cursor().
1738 */
1739 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001740gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001742 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743}
1744
1745 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001746gui_write(
1747 char_u *s,
1748 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 char_u *p;
1751 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 int force_scrollbar = FALSE;
1754 static win_T *old_curwin = NULL;
1755
1756/* #define DEBUG_GUI_WRITE */
1757#ifdef DEBUG_GUI_WRITE
1758 {
1759 int i;
1760 char_u *str;
1761
1762 printf("gui_write(%d):\n ", len);
1763 for (i = 0; i < len; i++)
1764 if (s[i] == ESC)
1765 {
1766 if (i != 0)
1767 printf("\n ");
1768 printf("<ESC>");
1769 }
1770 else
1771 {
1772 str = transchar_byte(s[i]);
1773 if (str[0] && str[1])
1774 printf("<%s>", (char *)str);
1775 else
1776 printf("%s", (char *)str);
1777 }
1778 printf("\n");
1779 }
1780#endif
1781 while (len)
1782 {
1783 if (s[0] == ESC && s[1] == '|')
1784 {
1785 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001786 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
1788 arg1 = getdigits(&p);
1789 if (p > s + len)
1790 break;
1791 if (*p == ';')
1792 {
1793 ++p;
1794 arg2 = getdigits(&p);
1795 if (p > s + len)
1796 break;
1797 }
1798 }
1799 switch (*p)
1800 {
1801 case 'C': /* Clear screen */
1802 clip_scroll_selection(9999);
1803 gui_mch_clear_all();
1804 gui.cursor_is_valid = FALSE;
1805 force_scrollbar = TRUE;
1806 break;
1807 case 'M': /* Move cursor */
1808 gui_set_cursor(arg1, arg2);
1809 break;
1810 case 's': /* force cursor (shape) update */
1811 force_cursor = TRUE;
1812 break;
1813 case 'R': /* Set scroll region */
1814 if (arg1 < arg2)
1815 {
1816 gui.scroll_region_top = arg1;
1817 gui.scroll_region_bot = arg2;
1818 }
1819 else
1820 {
1821 gui.scroll_region_top = arg2;
1822 gui.scroll_region_bot = arg1;
1823 }
1824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 case 'V': /* Set vertical scroll region */
1826 if (arg1 < arg2)
1827 {
1828 gui.scroll_region_left = arg1;
1829 gui.scroll_region_right = arg2;
1830 }
1831 else
1832 {
1833 gui.scroll_region_left = arg2;
1834 gui.scroll_region_right = arg1;
1835 }
1836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 case 'd': /* Delete line */
1838 gui_delete_lines(gui.row, 1);
1839 break;
1840 case 'D': /* Delete lines */
1841 gui_delete_lines(gui.row, arg1);
1842 break;
1843 case 'i': /* Insert line */
1844 gui_insert_lines(gui.row, 1);
1845 break;
1846 case 'I': /* Insert lines */
1847 gui_insert_lines(gui.row, arg1);
1848 break;
1849 case '$': /* Clear to end-of-line */
1850 gui_clear_block(gui.row, gui.col, gui.row,
1851 (int)Columns - 1);
1852 break;
1853 case 'h': /* Turn on highlighting */
1854 gui_start_highlight(arg1);
1855 break;
1856 case 'H': /* Turn off highlighting */
1857 gui_stop_highlight(arg1);
1858 break;
1859 case 'f': /* flash the window (visual bell) */
1860 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1861 break;
1862 default:
1863 p = s + 1; /* Skip the ESC */
1864 break;
1865 }
1866 len -= (int)(++p - s);
1867 s = p;
1868 }
1869 else if (
1870#ifdef EBCDIC
1871 CtrlChar(s[0]) != 0 /* Ctrl character */
1872#else
1873 s[0] < 0x20 /* Ctrl character */
1874#endif
1875#ifdef FEAT_SIGN_ICONS
1876 && s[0] != SIGN_BYTE
1877# ifdef FEAT_NETBEANS_INTG
1878 && s[0] != MULTISIGN_BYTE
1879# endif
1880#endif
1881 )
1882 {
1883 if (s[0] == '\n') /* NL */
1884 {
1885 gui.col = 0;
1886 if (gui.row < gui.scroll_region_bot)
1887 gui.row++;
1888 else
1889 gui_delete_lines(gui.scroll_region_top, 1);
1890 }
1891 else if (s[0] == '\r') /* CR */
1892 {
1893 gui.col = 0;
1894 }
1895 else if (s[0] == '\b') /* Backspace */
1896 {
1897 if (gui.col)
1898 --gui.col;
1899 }
1900 else if (s[0] == Ctrl_L) /* cursor-right */
1901 {
1902 ++gui.col;
1903 }
1904 else if (s[0] == Ctrl_G) /* Beep */
1905 {
1906 gui_mch_beep();
1907 }
1908 /* Other Ctrl character: shouldn't happen! */
1909
1910 --len; /* Skip this char */
1911 ++s;
1912 }
1913 else
1914 {
1915 p = s;
1916 while (len > 0 && (
1917#ifdef EBCDIC
1918 CtrlChar(*p) == 0
1919#else
1920 *p >= 0x20
1921#endif
1922#ifdef FEAT_SIGN_ICONS
1923 || *p == SIGN_BYTE
1924# ifdef FEAT_NETBEANS_INTG
1925 || *p == MULTISIGN_BYTE
1926# endif
1927#endif
1928 ))
1929 {
1930 len--;
1931 p++;
1932 }
1933 gui_outstr(s, (int)(p - s));
1934 s = p;
1935 }
1936 }
1937
1938 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1939 * set). */
1940 if (force_cursor)
1941 gui_update_cursor(TRUE, TRUE);
1942
1943 /* When switching to another window the dragging must have stopped.
1944 * Required for GTK, dragged_sb isn't reset. */
1945 if (old_curwin != curwin)
1946 gui.dragged_sb = SBAR_NONE;
1947
1948 /* Update the scrollbars after clearing the screen or when switched
1949 * to another window.
1950 * Update the horizontal scrollbar always, it's difficult to check all
1951 * situations where it might change. */
1952 if (force_scrollbar || old_curwin != curwin)
1953 gui_update_scrollbars(force_scrollbar);
1954 else
1955 gui_update_horiz_scrollbar(FALSE);
1956 old_curwin = curwin;
1957
1958 /*
1959 * We need to make sure this is cleared since Athena doesn't tell us when
1960 * he is done dragging. Do the same for GTK.
1961 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001962#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 gui.dragged_sb = SBAR_NONE;
1964#endif
1965
Bram Moolenaara338adc2018-01-31 20:51:47 +01001966 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967}
1968
1969/*
1970 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1971 * produces wrong results. Call gui_dont_update_cursor() before that code and
1972 * gui_can_update_cursor() afterwards.
1973 */
1974 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001975gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 if (gui.in_use)
1978 {
1979 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001980 if (undraw)
1981 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 can_update_cursor = FALSE;
1983 }
1984}
1985
1986 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001987gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 can_update_cursor = TRUE;
1990 /* No need to update the cursor right now, there is always more output
1991 * after scrolling. */
1992}
1993
Bram Moolenaara338adc2018-01-31 20:51:47 +01001994/*
1995 * Disable issuing gui_mch_flush().
1996 */
1997 void
1998gui_disable_flush(void)
1999{
2000 ++disable_flush;
2001}
2002
2003/*
2004 * Enable issuing gui_mch_flush().
2005 */
2006 void
2007gui_enable_flush(void)
2008{
2009 --disable_flush;
2010}
2011
2012/*
2013 * Issue gui_mch_flush() if it is not disabled.
2014 */
2015 void
2016gui_may_flush(void)
2017{
2018 if (disable_flush == 0)
2019 gui_mch_flush();
2020}
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002023gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 if (len == 0)
2029 return;
2030
2031 if (len < 0)
2032 len = (int)STRLEN(s);
2033
2034 while (len > 0)
2035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (has_mbyte)
2037 {
2038 /* Find out how many chars fit in the current line. */
2039 cells = 0;
2040 for (this_len = 0; this_len < len; )
2041 {
2042 cells += (*mb_ptr2cells)(s + this_len);
2043 if (gui.col + cells > Columns)
2044 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002045 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 if (this_len > len)
2048 this_len = len; /* don't include following composing char */
2049 }
2050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (gui.col + len > Columns)
2052 this_len = Columns - gui.col;
2053 else
2054 this_len = len;
2055
2056 (void)gui_outstr_nowrap(s, this_len,
2057 0, (guicolor_T)0, (guicolor_T)0, 0);
2058 s += this_len;
2059 len -= this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 /* fill up for a double-width char that doesn't fit. */
2061 if (len > 0 && gui.col < Columns)
2062 (void)gui_outstr_nowrap((char_u *)" ", 1,
2063 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 /* The cursor may wrap to the next line. */
2065 if (gui.col >= Columns)
2066 {
2067 gui.col = 0;
2068 gui.row++;
2069 }
2070 }
2071}
2072
2073/*
2074 * Output one character (may be one or two display cells).
2075 * Caller must check for valid "off".
2076 * Returns FAIL or OK, just like gui_outstr_nowrap().
2077 */
2078 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002079gui_screenchar(
2080 int off, /* Offset from start of screen */
2081 int flags,
2082 guicolor_T fg, /* colors for cursor */
2083 guicolor_T bg, /* colors for cursor */
2084 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 char_u buf[MB_MAXBYTES + 1];
2087
2088 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2089 if (enc_utf8 && ScreenLines[off] == 0)
2090 return OK;
2091
2092 if (enc_utf8 && ScreenLinesUC[off] != 0)
2093 /* Draw UTF-8 multi-byte character. */
2094 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2095 flags, fg, bg, back);
2096
2097 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2098 {
2099 buf[0] = ScreenLines[off];
2100 buf[1] = ScreenLines2[off];
2101 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2102 }
2103
2104 /* Draw non-multi-byte character or DBCS character. */
2105 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002106 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108}
2109
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002110#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111/*
2112 * Output the string at the given screen position. This is used in place
2113 * of gui_screenchar() where possible because Pango needs as much context
2114 * as possible to work nicely. It's a lot faster as well.
2115 */
2116 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002117gui_screenstr(
2118 int off, /* Offset from start of screen */
2119 int len, /* string length in screen cells */
2120 int flags,
2121 guicolor_T fg, /* colors for cursor */
2122 guicolor_T bg, /* colors for cursor */
2123 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 char_u *buf;
2126 int outlen = 0;
2127 int i;
2128 int retval;
2129
2130 if (len <= 0) /* "cannot happen"? */
2131 return OK;
2132
2133 if (enc_utf8)
2134 {
2135 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2136 if (buf == NULL)
2137 return OK; /* not much we could do here... */
2138
2139 for (i = off; i < off + len; ++i)
2140 {
2141 if (ScreenLines[i] == 0)
2142 continue; /* skip second half of double-width char */
2143
2144 if (ScreenLinesUC[i] == 0)
2145 buf[outlen++] = ScreenLines[i];
2146 else
2147 outlen += utfc_char2bytes(i, buf + outlen);
2148 }
2149
2150 buf[outlen] = NUL; /* only to aid debugging */
2151 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2152 vim_free(buf);
2153
2154 return retval;
2155 }
2156 else if (enc_dbcs == DBCS_JPNU)
2157 {
2158 buf = alloc((unsigned)(len * 2 + 1));
2159 if (buf == NULL)
2160 return OK; /* not much we could do here... */
2161
2162 for (i = off; i < off + len; ++i)
2163 {
2164 buf[outlen++] = ScreenLines[i];
2165
2166 /* handle double-byte single-width char */
2167 if (ScreenLines[i] == 0x8e)
2168 buf[outlen++] = ScreenLines2[i];
2169 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2170 buf[outlen++] = ScreenLines[++i];
2171 }
2172
2173 buf[outlen] = NUL; /* only to aid debugging */
2174 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2175 vim_free(buf);
2176
2177 return retval;
2178 }
2179 else
2180 {
2181 return gui_outstr_nowrap(&ScreenLines[off], len,
2182 flags, fg, bg, back);
2183 }
2184}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002185#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187/*
2188 * Output the given string at the current cursor position. If the string is
2189 * too long to fit on the line, then it is truncated.
2190 * "flags":
2191 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2192 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002193 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * background.
2195 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2196 * it.
2197 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2198 * FAIL (the caller should start drawing "back" chars back).
2199 */
2200 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002201gui_outstr_nowrap(
2202 char_u *s,
2203 int len,
2204 int flags,
2205 guicolor_T fg, /* colors for cursor */
2206 guicolor_T bg, /* colors for cursor */
2207 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 long_u highlight_mask;
2210 long_u hl_mask_todo;
2211 guicolor_T fg_color;
2212 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002213 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002214#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002216 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# ifdef FEAT_XFONTSET
2218 GuiFontset fontset = NOFONTSET;
2219# endif
2220#endif
2221 attrentry_T *aep = NULL;
2222 int draw_flags;
2223 int col = gui.col;
2224#ifdef FEAT_SIGN_ICONS
2225 int draw_sign = FALSE;
2226# ifdef FEAT_NETBEANS_INTG
2227 int multi_sign = FALSE;
2228# endif
2229#endif
2230
2231 if (len < 0)
2232 len = (int)STRLEN(s);
2233 if (len == 0)
2234 return OK;
2235
2236#ifdef FEAT_SIGN_ICONS
2237 if (*s == SIGN_BYTE
2238# ifdef FEAT_NETBEANS_INTG
2239 || *s == MULTISIGN_BYTE
2240# endif
2241 )
2242 {
2243# ifdef FEAT_NETBEANS_INTG
2244 if (*s == MULTISIGN_BYTE)
2245 multi_sign = TRUE;
2246# endif
2247 /* draw spaces instead */
2248 s = (char_u *)" ";
2249 if (len == 1 && col > 0)
2250 --col;
2251 len = 2;
2252 draw_sign = TRUE;
2253 highlight_mask = 0;
2254 }
2255 else
2256#endif
2257 if (gui.highlight_mask > HL_ALL)
2258 {
2259 aep = syn_gui_attr2entry(gui.highlight_mask);
2260 if (aep == NULL) /* highlighting not set */
2261 highlight_mask = 0;
2262 else
2263 highlight_mask = aep->ae_attr;
2264 }
2265 else
2266 highlight_mask = gui.highlight_mask;
2267 hl_mask_todo = highlight_mask;
2268
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /* Set the font */
2271 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2272 font = aep->ae_u.gui.font;
2273# ifdef FEAT_XFONTSET
2274 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2275 fontset = aep->ae_u.gui.fontset;
2276# endif
2277 else
2278 {
2279# ifdef FEAT_XFONTSET
2280 if (gui.fontset != NOFONTSET)
2281 fontset = gui.fontset;
2282 else
2283# endif
2284 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2285 {
2286 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2287 {
2288 font = gui.boldital_font;
2289 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2290 }
2291 else if (gui.bold_font != NOFONT)
2292 {
2293 font = gui.bold_font;
2294 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2295 }
2296 else
2297 font = gui.norm_font;
2298 }
2299 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2300 {
2301 font = gui.ital_font;
2302 hl_mask_todo &= ~HL_ITALIC;
2303 }
2304 else
2305 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002306
Bram Moolenaardb250522013-06-17 22:43:25 +02002307 /*
2308 * Choose correct wide_font by font. wide_font should be set with font
2309 * at same time in above block. But it will make many "ifdef" nasty
2310 * blocks. So we do it here.
2311 */
2312 if (font == gui.boldital_font && gui.wide_boldital_font)
2313 wide_font = gui.wide_boldital_font;
2314 else if (font == gui.bold_font && gui.wide_bold_font)
2315 wide_font = gui.wide_bold_font;
2316 else if (font == gui.ital_font && gui.wide_ital_font)
2317 wide_font = gui.wide_ital_font;
2318 else if (font == gui.norm_font && gui.wide_font)
2319 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321# ifdef FEAT_XFONTSET
2322 if (fontset != NOFONTSET)
2323 gui_mch_set_fontset(fontset);
2324 else
2325# endif
2326 gui_mch_set_font(font);
2327#endif
2328
2329 draw_flags = 0;
2330
2331 /* Set the color */
2332 bg_color = gui.back_pixel;
2333 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2334 {
2335 draw_flags |= DRAW_CURSOR;
2336 fg_color = fg;
2337 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002338 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340 else if (aep != NULL)
2341 {
2342 fg_color = aep->ae_u.gui.fg_color;
2343 if (fg_color == INVALCOLOR)
2344 fg_color = gui.norm_pixel;
2345 bg_color = aep->ae_u.gui.bg_color;
2346 if (bg_color == INVALCOLOR)
2347 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002348 sp_color = aep->ae_u.gui.sp_color;
2349 if (sp_color == INVALCOLOR)
2350 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002355 sp_color = fg_color;
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2359 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002360#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 gui_mch_set_colors(bg_color, fg_color);
2362#else
2363 gui_mch_set_fg_color(bg_color);
2364 gui_mch_set_bg_color(fg_color);
2365#endif
2366 }
2367 else
2368 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002369#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 gui_mch_set_colors(fg_color, bg_color);
2371#else
2372 gui_mch_set_fg_color(fg_color);
2373 gui_mch_set_bg_color(bg_color);
2374#endif
2375 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002376 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 /* Clear the selection if we are about to write over it */
2379 if (!(flags & GUI_MON_NOCLEAR))
2380 clip_may_clear_selection(gui.row, gui.row);
2381
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 /* If there's no bold font, then fake it */
2384 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2385 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 /*
2388 * When drawing bold or italic characters the spill-over from the left
2389 * neighbor may be destroyed. Let the caller backup to start redrawing
2390 * just after a blank.
2391 */
2392 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2393 return FAIL;
2394
Bram Moolenaare60acc12011-05-10 16:41:25 +02002395#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 /* If there's no italic font, then fake it.
2397 * For GTK2, we don't need a different font for italic style. */
2398 if (hl_mask_todo & HL_ITALIC)
2399 draw_flags |= DRAW_ITALIC;
2400
2401 /* Do we underline the text? */
2402 if (hl_mask_todo & HL_UNDERLINE)
2403 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405#else
2406 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 draw_flags |= DRAW_UNDERL;
2409#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002410 /* Do we undercurl the text? */
2411 if (hl_mask_todo & HL_UNDERCURL)
2412 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002414 /* Do we strikethrough the text? */
2415 if (hl_mask_todo & HL_STRIKETHROUGH)
2416 draw_flags |= DRAW_STRIKE;
2417
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002418 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (flags & GUI_MON_TRS_CURSOR)
2420 draw_flags |= DRAW_TRANSP;
2421
2422 /*
2423 * Draw the text.
2424 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002425#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /* The value returned is the length in display cells */
2427 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2428#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (enc_utf8)
2430 {
2431 int start; /* index of bytes to be drawn */
2432 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002433 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int cn; /* cellwidth of current char */
2435 int i; /* index of current char */
2436 int c; /* current char value */
2437 int cl; /* byte length of current char */
2438 int comping; /* current char is composing */
2439 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002440 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002441 int prev_wide = FALSE;
2442 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002443# ifdef MSWIN
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002444 int sep_comp = FALSE; /* Don't separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002445# else
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002446 int sep_comp = TRUE; /* Separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 /* Break the string at a composing character, it has to be drawn on
2450 * top of the previous character. */
2451 start = 0;
2452 cells = 0;
2453 for (i = 0; i < len; i += cl)
2454 {
2455 c = utf_ptr2char(s + i);
2456 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 comping = utf_iscomposing(c);
2458 if (!comping) /* count cells from non-composing chars */
2459 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002460 if (!comping || sep_comp)
2461 {
2462 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002463# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002464 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002465# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002466 && wide_font != NOFONT)
2467 curr_wide = TRUE;
2468 else
2469 curr_wide = FALSE;
2470 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002471 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (cl == 0) /* hit end of string */
2473 len = i + cl; /* len must be wrong "cannot happen" */
2474
Bram Moolenaar45933962013-01-23 17:43:57 +01002475 wide_changed = curr_wide != prev_wide;
2476
2477 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002479 if (i + cl >= len || (comping && sep_comp && i > start)
2480 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002481# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002483# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 /* No fontset: At least draw char after wide char at
2485 * right position. */
2486 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002488 )
2489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 )
2491 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002492 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 thislen = i - start;
2494 else
2495 thislen = i - start + cl;
2496 if (thislen > 0)
2497 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002498 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002499 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2501 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002502 if (prev_wide)
2503 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 start += thislen;
2505 }
2506 scol += cells;
2507 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002508 /* Adjust to not draw a character which width is changed
2509 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002510 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002512 scol -= cn;
2513 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515
Bram Moolenaar13505972019-01-24 15:04:48 +01002516# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002517 /* No fontset: draw a space to fill the gap after a wide char
2518 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002520# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002522# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002523 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2525 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002529 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002531# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002532 /* Carbon ATSUI autodraws composing char over previous char */
2533 gui_mch_draw_string(gui.row, scol, s + i, cl,
2534 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002535# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002536 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2537 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 start = i + cl;
2540 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002541 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 /* The stuff below assumes "len" is the length in screen columns. */
2544 len = scol - col;
2545 }
2546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (enc_dbcs == DBCS_JPNU)
2550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 /* Get the length in display cells, this can be different from the
2552 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002553 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002556#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2559 gui.col = col + len;
2560
2561 /* May need to invert it when it's part of the selection. */
2562 if (flags & GUI_MON_NOCLEAR)
2563 clip_may_redraw_selection(gui.row, col, len);
2564
2565 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2566 {
2567 /* Invalidate the old physical cursor position if we wrote over it */
2568 if (gui.cursor_row == gui.row
2569 && gui.cursor_col >= col
2570 && gui.cursor_col < col + len)
2571 gui.cursor_is_valid = FALSE;
2572 }
2573
2574#ifdef FEAT_SIGN_ICONS
2575 if (draw_sign)
2576 /* Draw the sign on top of the spaces. */
2577 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002578# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002579 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (multi_sign)
2581 netbeans_draw_multisign_indicator(gui.row);
2582# endif
2583#endif
2584
2585 return OK;
2586}
2587
2588/*
2589 * Un-draw the cursor. Actually this just redraws the character at the given
2590 * position. The character just before it too, for when it was in bold.
2591 */
2592 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002593gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 if (gui.cursor_is_valid)
2596 {
2597#ifdef FEAT_HANGULIN
2598 if (composing_hangul
2599 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002600 {
2601 char_u *comp_buf;
2602 int comp_len;
2603
2604 comp_buf = hangul_composing_buffer_get(&comp_len);
2605 if (comp_buf)
2606 {
2607 (void)gui_outstr_nowrap(comp_buf, comp_len,
2608 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2609 gui.norm_pixel, gui.back_pixel, 0);
2610 vim_free(comp_buf);
2611 }
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 else
2614 {
2615#endif
2616 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2617 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2618 && gui.cursor_col > 0)
2619 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2620 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2621#ifdef FEAT_HANGULIN
2622 if (composing_hangul)
2623 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2624 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2625 }
2626#endif
2627 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2628 * here in case it wasn't needed to undraw it. */
2629 gui.cursor_is_valid = FALSE;
2630 }
2631}
2632
2633 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002634gui_redraw(
2635 int x,
2636 int y,
2637 int w,
2638 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 int row1, col1, row2, col2;
2641
2642 row1 = Y_2_ROW(y);
2643 col1 = X_2_COL(x);
2644 row2 = Y_2_ROW(y + h - 1);
2645 col2 = X_2_COL(x + w - 1);
2646
2647 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2648
2649 /*
2650 * We may need to redraw the cursor, but don't take it upon us to change
2651 * its location after a scroll.
2652 * (maybe be more strict even and test col too?)
2653 * These things may be outside the update/clipping region and reality may
2654 * not reflect Vims internal ideas if these operations are clipped away.
2655 */
2656 if (gui.row == gui.cursor_row)
2657 gui_update_cursor(TRUE, TRUE);
2658}
2659
2660/*
2661 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2662 * from col1 to col2 (inclusive).
2663 * Return TRUE when the character before the first drawn character has
2664 * different attributes (may have to be redrawn too).
2665 */
2666 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002667gui_redraw_block(
2668 int row1,
2669 int col1,
2670 int row2,
2671 int col2,
2672 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 int old_row, old_col;
2675 long_u old_hl_mask;
2676 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002677 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int idx, len;
2679 int back, nback;
2680 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 /* Don't try to update when ScreenLines is not valid */
2684 if (!screen_cleared || ScreenLines == NULL)
2685 return retval;
2686
2687 /* Don't try to draw outside the shell! */
2688 /* Check everything, strange values may be caused by a big border width */
2689 col1 = check_col(col1);
2690 col2 = check_col(col2);
2691 row1 = check_row(row1);
2692 row2 = check_row(row2);
2693
2694 /* Remember where our cursor was */
2695 old_row = gui.row;
2696 old_col = gui.col;
2697 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 orig_col1 = col1;
2699 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 for (gui.row = row1; gui.row <= row2; gui.row++)
2702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 /* When only half of a double-wide character is in the block, include
2704 * the other half. */
2705 col1 = orig_col1;
2706 col2 = orig_col2;
2707 off = LineOffset[gui.row];
2708 if (enc_dbcs != 0)
2709 {
2710 if (col1 > 0)
2711 col1 -= dbcs_screen_head_off(ScreenLines + off,
2712 ScreenLines + off + col1);
2713 col2 += dbcs_screen_tail_off(ScreenLines + off,
2714 ScreenLines + off + col2);
2715 }
2716 else if (enc_utf8)
2717 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002718 if (ScreenLines[off + col1] == 0)
2719 {
2720 if (col1 > 0)
2721 --col1;
2722 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002723 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002724 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002725 // Make this IEMSGN when it no longer breaks Travis CI.
2726 vim_snprintf((char *)IObuff, IOSIZE,
2727 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2728 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002729 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002730 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002731 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002732#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2734 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 gui.col = col1;
2738 off = LineOffset[gui.row] + gui.col;
2739 len = col2 - col1 + 1;
2740
2741 /* Find how many chars back this highlighting starts, or where a space
2742 * is. Needed for when the bold trick is used */
2743 for (back = 0; back < col1; ++back)
2744 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2745 || ScreenLines[off - 1 - back] == ' ')
2746 break;
2747 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2748 && ScreenLines[off - 1] != ' ');
2749
2750 /* Break it up in strings of characters with the same attributes. */
2751 /* Print UTF-8 characters individually. */
2752 while (len > 0)
2753 {
2754 first_attr = ScreenAttrs[off];
2755 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002756#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (enc_utf8 && ScreenLinesUC[off] != 0)
2758 {
2759 /* output multi-byte character separately */
2760 nback = gui_screenchar(off, flags,
2761 (guicolor_T)0, (guicolor_T)0, back);
2762 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2763 idx = 2;
2764 else
2765 idx = 1;
2766 }
2767 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2768 {
2769 /* output double-byte, single-width character separately */
2770 nback = gui_screenchar(off, flags,
2771 (guicolor_T)0, (guicolor_T)0, back);
2772 idx = 1;
2773 }
2774 else
2775#endif
2776 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002777#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 for (idx = 0; idx < len; ++idx)
2779 {
2780 if (enc_utf8 && ScreenLines[off + idx] == 0)
2781 continue; /* skip second half of double-width char */
2782 if (ScreenAttrs[off + idx] != first_attr)
2783 break;
2784 }
2785 /* gui_screenstr() takes care of multibyte chars */
2786 nback = gui_screenstr(off, idx, flags,
2787 (guicolor_T)0, (guicolor_T)0, back);
2788#else
2789 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2790 idx++)
2791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 /* Stop at a multi-byte Unicode character. */
2793 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2794 break;
2795 if (enc_dbcs == DBCS_JPNU)
2796 {
2797 /* Stop at a double-byte single-width char. */
2798 if (ScreenLines[off + idx] == 0x8e)
2799 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002800 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 + off + idx) == 2)
2802 ++idx; /* skip second byte of double-byte char */
2803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2806 (guicolor_T)0, (guicolor_T)0, back);
2807#endif
2808 }
2809 if (nback == FAIL)
2810 {
2811 /* Must back up to start drawing where a bold or italic word
2812 * starts. */
2813 off -= back;
2814 len += back;
2815 gui.col -= back;
2816 }
2817 else
2818 {
2819 off += idx;
2820 len -= idx;
2821 }
2822 back = 0;
2823 }
2824 }
2825
2826 /* Put the cursor back where it was */
2827 gui.row = old_row;
2828 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002829 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 return retval;
2832}
2833
2834 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002835gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 if (count <= 0)
2838 return;
2839
2840 if (row + count > gui.scroll_region_bot)
2841 /* Scrolled out of region, just blank the lines out */
2842 gui_clear_block(row, gui.scroll_region_left,
2843 gui.scroll_region_bot, gui.scroll_region_right);
2844 else
2845 {
2846 gui_mch_delete_lines(row, count);
2847
2848 /* If the cursor was in the deleted lines it's now gone. If the
2849 * cursor was in the scrolled lines adjust its position. */
2850 if (gui.cursor_row >= row
2851 && gui.cursor_col >= gui.scroll_region_left
2852 && gui.cursor_col <= gui.scroll_region_right)
2853 {
2854 if (gui.cursor_row < row + count)
2855 gui.cursor_is_valid = FALSE;
2856 else if (gui.cursor_row <= gui.scroll_region_bot)
2857 gui.cursor_row -= count;
2858 }
2859 }
2860}
2861
2862 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002863gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 if (count <= 0)
2866 return;
2867
2868 if (row + count > gui.scroll_region_bot)
2869 /* Scrolled out of region, just blank the lines out */
2870 gui_clear_block(row, gui.scroll_region_left,
2871 gui.scroll_region_bot, gui.scroll_region_right);
2872 else
2873 {
2874 gui_mch_insert_lines(row, count);
2875
2876 if (gui.cursor_row >= gui.row
2877 && gui.cursor_col >= gui.scroll_region_left
2878 && gui.cursor_col <= gui.scroll_region_right)
2879 {
2880 if (gui.cursor_row <= gui.scroll_region_bot - count)
2881 gui.cursor_row += count;
2882 else if (gui.cursor_row <= gui.scroll_region_bot)
2883 gui.cursor_is_valid = FALSE;
2884 }
2885 }
2886}
2887
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002888#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002889/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002890 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2891 */
2892 static int
2893gui_wait_for_chars_3(
2894 long wtime,
2895 int *interrupted UNUSED,
2896 int ignore_input UNUSED)
2897{
2898 return gui_mch_wait_for_chars(wtime);
2899}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002900#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002901
2902/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002903 * Returns OK if a character was found to be available within the given time,
2904 * or FAIL otherwise.
2905 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002906 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002907gui_wait_for_chars_or_timer(
2908 long wtime,
2909 int *interrupted UNUSED,
2910 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002911{
2912#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002913 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2914 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002915#else
2916 return gui_mch_wait_for_chars(wtime);
2917#endif
2918}
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920/*
2921 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002922 * "wtime" == -1 Wait forever.
2923 * "wtime" == 0 Don't wait.
2924 * "wtime" > 0 Wait wtime milliseconds for a character.
2925 *
2926 * Returns the number of characters read or zero when timed out or interrupted.
2927 * "buf" may be NULL, in which case a non-zero number is returned if characters
2928 * are available.
2929 */
2930 static int
2931gui_wait_for_chars_buf(
2932 char_u *buf,
2933 int maxlen,
2934 long wtime, // don't use "time", MIPS cannot handle it
2935 int tb_change_cnt)
2936{
2937 int retval;
2938
2939#ifdef FEAT_MENU
2940 // If we're going to wait a bit, update the menus and mouse shape for the
2941 // current State.
2942 if (wtime != 0)
2943 gui_update_menus(0);
2944#endif
2945
2946 gui_mch_update();
2947 if (input_available()) // Got char, return immediately
2948 {
2949 if (buf != NULL && !typebuf_changed(tb_change_cnt))
2950 return read_from_input_buf(buf, (long)maxlen);
2951 return 0;
2952 }
2953 if (wtime == 0) // Don't wait for char
2954 return FAIL;
2955
2956 // Before waiting, flush any output to the screen.
2957 gui_mch_flush();
2958
2959 // Blink while waiting for a character.
2960 gui_mch_start_blink();
2961
2962 // Common function to loop until "wtime" is met, while handling timers and
2963 // other callbacks.
2964 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
2965 gui_wait_for_chars_or_timer, NULL);
2966
2967 gui_mch_stop_blink(TRUE);
2968
2969 return retval;
2970}
2971
2972/*
2973 * Wait for a character from the keyboard without actually reading it.
2974 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 * wtime == -1 Wait forever.
2976 * wtime == 0 Don't wait.
2977 * wtime > 0 Wait wtime milliseconds for a character.
2978 * Returns OK if a character was found to be available within the given time,
2979 * or FAIL otherwise.
2980 */
2981 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002982gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002984 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985}
2986
2987/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002988 * Equivalent of mch_inchar() for the GUI.
2989 */
2990 int
2991gui_inchar(
2992 char_u *buf,
2993 int maxlen,
2994 long wtime, /* milli seconds */
2995 int tb_change_cnt)
2996{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002997 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002998}
2999
3000/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003001 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
3003 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003004fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 p[0] = (char_u)(col / 128 + ' ' + 1);
3007 p[1] = (char_u)(col % 128 + ' ' + 1);
3008 p[2] = (char_u)(row / 128 + ' ' + 1);
3009 p[3] = (char_u)(row % 128 + ' ' + 1);
3010}
3011
3012/*
3013 * Generic mouse support function. Add a mouse event to the input buffer with
3014 * the given properties.
3015 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3016 * MOUSE_X1, MOUSE_X2
3017 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003018 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3019 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 * x, y --- Coordinates of mouse in pixels.
3021 * repeated_click --- TRUE if this click comes only a short time after a
3022 * previous click.
3023 * modifiers --- Bit field which may be any of the following modifiers
3024 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3025 * This function will ignore drag events where the mouse has not moved to a new
3026 * character.
3027 */
3028 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003029gui_send_mouse_event(
3030 int button,
3031 int x,
3032 int y,
3033 int repeated_click,
3034 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 static int prev_row = 0, prev_col = 0;
3037 static int prev_button = -1;
3038 static int num_clicks = 1;
3039 char_u string[10];
3040 enum key_extra button_char;
3041 int row, col;
3042#ifdef FEAT_CLIPBOARD
3043 int checkfor;
3044 int did_clip = FALSE;
3045#endif
3046
3047 /*
3048 * Scrolling may happen at any time, also while a selection is present.
3049 */
3050 switch (button)
3051 {
3052 case MOUSE_X1:
3053 button_char = KE_X1MOUSE;
3054 goto button_set;
3055 case MOUSE_X2:
3056 button_char = KE_X2MOUSE;
3057 goto button_set;
3058 case MOUSE_4:
3059 button_char = KE_MOUSEDOWN;
3060 goto button_set;
3061 case MOUSE_5:
3062 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003063 goto button_set;
3064 case MOUSE_6:
3065 button_char = KE_MOUSELEFT;
3066 goto button_set;
3067 case MOUSE_7:
3068 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069button_set:
3070 {
3071 /* Don't put events in the input queue now. */
3072 if (hold_gui_events)
3073 return;
3074
3075 string[3] = CSI;
3076 string[4] = KS_EXTRA;
3077 string[5] = (int)button_char;
3078
3079 /* Pass the pointer coordinates of the scroll event so that we
3080 * know which window to scroll. */
3081 row = gui_xy2colrow(x, y, &col);
3082 string[6] = (char_u)(col / 128 + ' ' + 1);
3083 string[7] = (char_u)(col % 128 + ' ' + 1);
3084 string[8] = (char_u)(row / 128 + ' ' + 1);
3085 string[9] = (char_u)(row % 128 + ' ' + 1);
3086
3087 if (modifiers == 0)
3088 add_to_input_buf(string + 3, 7);
3089 else
3090 {
3091 string[0] = CSI;
3092 string[1] = KS_MODIFIER;
3093 string[2] = 0;
3094 if (modifiers & MOUSE_SHIFT)
3095 string[2] |= MOD_MASK_SHIFT;
3096 if (modifiers & MOUSE_CTRL)
3097 string[2] |= MOD_MASK_CTRL;
3098 if (modifiers & MOUSE_ALT)
3099 string[2] |= MOD_MASK_ALT;
3100 add_to_input_buf(string, 10);
3101 }
3102 return;
3103 }
3104 }
3105
3106#ifdef FEAT_CLIPBOARD
3107 /* If a clipboard selection is in progress, handle it */
3108 if (clip_star.state == SELECT_IN_PROGRESS)
3109 {
3110 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3111 return;
3112 }
3113
3114 /* Determine which mouse settings to look for based on the current mode */
3115 switch (get_real_state())
3116 {
3117 case NORMAL_BUSY:
3118 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003119# ifdef FEAT_TERMINAL
3120 case TERMINAL:
3121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 case NORMAL: checkfor = MOUSE_NORMAL; break;
3123 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003124 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 case REPLACE:
3126 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 case VREPLACE:
3128 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 case INSERT:
3130 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3131 case ASKMORE:
3132 case HITRETURN: /* At the more- and hit-enter prompt pass the
3133 mouse event for a click on or below the
3134 message line. */
3135 if (Y_2_ROW(y) >= msg_row)
3136 checkfor = MOUSE_NORMAL;
3137 else
3138 checkfor = MOUSE_RETURN;
3139 break;
3140
3141 /*
3142 * On the command line, use the clipboard selection on all lines
3143 * but the command line. But not when pasting.
3144 */
3145 case CMDLINE:
3146 case CMDLINE+LANGMAP:
3147 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3148 checkfor = MOUSE_NONE;
3149 else
3150 checkfor = MOUSE_COMMAND;
3151 break;
3152
3153 default:
3154 checkfor = MOUSE_NONE;
3155 break;
3156 };
3157
3158 /*
3159 * Allow clipboard selection of text on the command line in "normal"
3160 * modes. Don't do this when dragging the status line, or extending a
3161 * Visual selection.
3162 */
3163 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003164 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 && button != MOUSE_DRAG
3166# ifdef FEAT_MOUSESHAPE
3167 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169# endif
3170 )
3171 checkfor = MOUSE_NONE;
3172
3173 /*
3174 * Use modeless selection when holding CTRL and SHIFT pressed.
3175 */
3176 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3177 checkfor = MOUSE_NONEF;
3178
3179 /*
3180 * In Ex mode, always use modeless selection.
3181 */
3182 if (exmode_active)
3183 checkfor = MOUSE_NONE;
3184
3185 /*
3186 * If the mouse settings say to not use the mouse, use the modeless
3187 * selection. But if Visual is active, assume that only the Visual area
3188 * will be selected.
3189 * Exception: On the command line, both the selection is used and a mouse
3190 * key is send.
3191 */
3192 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 /* Don't do modeless selection in Visual mode. */
3195 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3196 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 /*
3199 * When 'mousemodel' is "popup", shift-left is translated to right.
3200 * But not when also using Ctrl.
3201 */
3202 if (mouse_model_popup() && button == MOUSE_LEFT
3203 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3204 {
3205 button = MOUSE_RIGHT;
3206 modifiers &= ~ MOUSE_SHIFT;
3207 }
3208
3209 /* If the selection is done, allow the right button to extend it.
3210 * If the selection is cleared, allow the right button to start it
3211 * from the cursor position. */
3212 if (button == MOUSE_RIGHT)
3213 {
3214 if (clip_star.state == SELECT_CLEARED)
3215 {
3216 if (State & CMDLINE)
3217 {
3218 col = msg_col;
3219 row = msg_row;
3220 }
3221 else
3222 {
3223 col = curwin->w_wcol;
3224 row = curwin->w_wrow + W_WINROW(curwin);
3225 }
3226 clip_start_selection(col, row, FALSE);
3227 }
3228 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3229 repeated_click);
3230 did_clip = TRUE;
3231 }
3232 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003233 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3236 did_clip = TRUE;
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
3239 /* Always allow pasting */
3240 if (button != MOUSE_MIDDLE)
3241 {
3242 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3243 return;
3244 if (checkfor != MOUSE_COMMAND)
3245 button = MOUSE_LEFT;
3246 }
3247 repeated_click = FALSE;
3248 }
3249
3250 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003251 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif
3253
3254 /* Don't put events in the input queue now. */
3255 if (hold_gui_events)
3256 return;
3257
3258 row = gui_xy2colrow(x, y, &col);
3259
3260 /*
3261 * If we are dragging and the mouse hasn't moved far enough to be on a
3262 * different character, then don't send an event to vim.
3263 */
3264 if (button == MOUSE_DRAG)
3265 {
3266 if (row == prev_row && col == prev_col)
3267 return;
3268 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3269 if (y < 0)
3270 row = -1;
3271 }
3272
3273 /*
3274 * If topline has changed (window scrolled) since the last click, reset
3275 * repeated_click, because we don't want starting Visual mode when
3276 * clicking on a different character in the text.
3277 */
3278 if (curwin->w_topline != gui_prev_topline
3279#ifdef FEAT_DIFF
3280 || curwin->w_topfill != gui_prev_topfill
3281#endif
3282 )
3283 repeated_click = FALSE;
3284
3285 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3286 string[1] = KS_MOUSE;
3287 string[2] = KE_FILLER;
3288 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3289 {
3290 if (repeated_click)
3291 {
3292 /*
3293 * Handle multiple clicks. They only count if the mouse is still
3294 * pointing at the same character.
3295 */
3296 if (button != prev_button || row != prev_row || col != prev_col)
3297 num_clicks = 1;
3298 else if (++num_clicks > 4)
3299 num_clicks = 1;
3300 }
3301 else
3302 num_clicks = 1;
3303 prev_button = button;
3304 gui_prev_topline = curwin->w_topline;
3305#ifdef FEAT_DIFF
3306 gui_prev_topfill = curwin->w_topfill;
3307#endif
3308
3309 string[3] = (char_u)(button | 0x20);
3310 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3311 }
3312 else
3313 string[3] = (char_u)button;
3314
3315 string[3] |= modifiers;
3316 fill_mouse_coord(string + 4, col, row);
3317 add_to_input_buf(string, 8);
3318
3319 if (row < 0)
3320 prev_row = 0;
3321 else
3322 prev_row = row;
3323 prev_col = col;
3324
3325 /*
3326 * We need to make sure this is cleared since Athena doesn't tell us when
3327 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3328 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003329#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 gui.dragged_sb = SBAR_NONE;
3331#endif
3332}
3333
3334/*
3335 * Convert x and y coordinate to column and row in text window.
3336 * Corrects for multi-byte character.
3337 * returns column in "*colp" and row as return value;
3338 */
3339 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003340gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 int col = check_col(X_2_COL(x));
3343 int row = check_row(Y_2_ROW(y));
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 return row;
3347}
3348
3349#if defined(FEAT_MENU) || defined(PROTO)
3350/*
3351 * Callback function for when a menu entry has been selected.
3352 */
3353 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003354gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003356 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 /* Don't put events in the input queue now. */
3359 if (hold_gui_events)
3360 return;
3361
3362 bytes[0] = CSI;
3363 bytes[1] = KS_MENU;
3364 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003365 add_to_input_buf(bytes, 3);
3366 add_long_to_buf((long_u)menu, bytes);
3367 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368}
3369#endif
3370
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003371static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373/*
3374 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003375 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 * in p_go.
3377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003379gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#ifdef FEAT_MENU
3382 static int prev_menu_is_active = -1;
3383#endif
3384#ifdef FEAT_TOOLBAR
3385 static int prev_toolbar = -1;
3386 int using_toolbar = FALSE;
3387#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003388#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003389 int using_tabline;
3390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#ifdef FEAT_FOOTER
3392 static int prev_footer = -1;
3393 int using_footer = FALSE;
3394#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 static int prev_tearoff = -1;
3397 int using_tearoff = FALSE;
3398#endif
3399
3400 char_u *p;
3401 int i;
3402#ifdef FEAT_MENU
3403 int grey_old, grey_new;
3404 char_u *temp;
3405#endif
3406 win_T *wp;
3407 int need_set_size;
3408 int fix_size;
3409
3410#ifdef FEAT_MENU
3411 if (oldval != NULL && gui.in_use)
3412 {
3413 /*
3414 * Check if the menu's go from grey to non-grey or vise versa.
3415 */
3416 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3417 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3418 if (grey_old != grey_new)
3419 {
3420 temp = p_go;
3421 p_go = oldval;
3422 gui_update_menus(MENU_ALL_MODES);
3423 p_go = temp;
3424 }
3425 }
3426 gui.menu_is_active = FALSE;
3427#endif
3428
3429 for (i = 0; i < 3; i++)
3430 gui.which_scrollbars[i] = FALSE;
3431 for (p = p_go; *p; p++)
3432 switch (*p)
3433 {
3434 case GO_LEFT:
3435 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3436 break;
3437 case GO_RIGHT:
3438 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 case GO_VLEFT:
3441 if (win_hasvertsplit())
3442 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3443 break;
3444 case GO_VRIGHT:
3445 if (win_hasvertsplit())
3446 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 case GO_BOT:
3449 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3450 break;
3451#ifdef FEAT_MENU
3452 case GO_MENUS:
3453 gui.menu_is_active = TRUE;
3454 break;
3455#endif
3456 case GO_GREY:
3457 /* make menu's have grey items, ignored here */
3458 break;
3459#ifdef FEAT_TOOLBAR
3460 case GO_TOOLBAR:
3461 using_toolbar = TRUE;
3462 break;
3463#endif
3464#ifdef FEAT_FOOTER
3465 case GO_FOOTER:
3466 using_footer = TRUE;
3467 break;
3468#endif
3469 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 using_tearoff = TRUE;
3472#endif
3473 break;
3474 default:
3475 /* Ignore options that are not supported */
3476 break;
3477 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (gui.in_use)
3480 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003481 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003483
3484#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003485 /* Update the GUI tab line, it may appear or disappear. This may
3486 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003487 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003488 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003490 /* We don't want a resize event change "Rows" here, save and
3491 * restore it. Resizing is handled below. */
3492 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003493 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003494 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003495 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003496 if (using_tabline)
3497 fix_size = TRUE;
3498 if (!gui_use_tabline())
3499 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3500 }
3501#endif
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 for (i = 0; i < 3; i++)
3504 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003505 /* The scrollbar needs to be updated when it is shown/unshown and
3506 * when switching tab pages. But the size only changes when it's
3507 * shown/unshown. Thus we need two places to remember whether a
3508 * scrollbar is there or not. */
3509 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003510 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003511 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 if (i == SBAR_BOTTOM)
3514 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3515 gui.which_scrollbars[i]);
3516 else
3517 {
3518 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003521 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3522 {
3523 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003524 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003525 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003526 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003527 if (gui.which_scrollbars[i])
3528 fix_size = TRUE;
3529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003531 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003532 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534
3535#ifdef FEAT_MENU
3536 if (gui.menu_is_active != prev_menu_is_active)
3537 {
3538 /* We don't want a resize event change "Rows" here, save and
3539 * restore it. Resizing is handled below. */
3540 i = Rows;
3541 gui_mch_enable_menu(gui.menu_is_active);
3542 Rows = i;
3543 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003544 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (gui.menu_is_active)
3546 fix_size = TRUE;
3547 }
3548#endif
3549
3550#ifdef FEAT_TOOLBAR
3551 if (using_toolbar != prev_toolbar)
3552 {
3553 gui_mch_show_toolbar(using_toolbar);
3554 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003555 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (using_toolbar)
3557 fix_size = TRUE;
3558 }
3559#endif
3560#ifdef FEAT_FOOTER
3561 if (using_footer != prev_footer)
3562 {
3563 gui_mch_enable_footer(using_footer);
3564 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003565 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (using_footer)
3567 fix_size = TRUE;
3568 }
3569#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003570#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (using_tearoff != prev_tearoff)
3572 {
3573 gui_mch_toggle_tearoffs(using_tearoff);
3574 prev_tearoff = using_tearoff;
3575 }
3576#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003577 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003578 {
3579#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003580 long prev_Columns = Columns;
3581 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 /* Adjust the size of the window to make the text area keep the
3584 * same size and to avoid that part of our window is off-screen
3585 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003586 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003587
3588#ifdef FEAT_GUI_GTK
3589 /* GTK has the annoying habit of sending us resize events when
3590 * changing the window size ourselves. This mostly happens when
3591 * waiting for a character to arrive, quite unpredictably, and may
3592 * change Columns and Rows when we don't want it. Wait for a
3593 * character here to avoid this effect.
3594 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003595 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003596 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003597 * Don't change Rows when adding menu/toolbar/tabline.
3598 * Don't change Columns when adding vertical toolbar. */
3599 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003600 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003601 if ((need_set_size & RESIZE_VERT) == 0)
3602 Rows = prev_Rows;
3603 if ((need_set_size & RESIZE_HOR) == 0)
3604 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003605#endif
3606 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003607 /* When the console tabline appears or disappears the window positions
3608 * change. */
3609 if (firstwin->w_winrow != tabline_height())
3610 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612}
3613
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003614#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3615/*
3616 * Return TRUE if the GUI is taking care of the tabline.
3617 * It may still be hidden if 'showtabline' is zero.
3618 */
3619 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003620gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003621{
3622 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3623}
3624
3625/*
3626 * Return TRUE if the GUI is showing the tabline.
3627 * This uses 'showtabline'.
3628 */
3629 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003630gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003631{
3632 if (!gui_use_tabline()
3633 || p_stal == 0
3634 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3635 return FALSE;
3636 return TRUE;
3637}
3638
3639/*
3640 * Update the tabline.
3641 * This may display/undisplay the tabline and update the labels.
3642 */
3643 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003644gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003645{
3646 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003647 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648
3649 if (!gui.starting && starting == 0)
3650 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003651 /* Updating the tabline uses direct GUI commands, flush
3652 * outstanding instructions first. (esp. clear screen) */
3653 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003654
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003655 if (!showit != !shown)
3656 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003657 if (showit != 0)
3658 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003659
3660 /* When the tabs change from hidden to shown or from shown to
3661 * hidden the size of the text area should remain the same. */
3662 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003663 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003664 }
3665}
3666
3667/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003668 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003669 */
3670 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003671get_tabline_label(
3672 tabpage_T *tp,
3673 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674{
3675 int modified = FALSE;
3676 char_u buf[40];
3677 int wincount;
3678 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003679 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003680
Bram Moolenaar57657d82006-04-21 22:12:41 +00003681 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003682 opt = (tooltip ? &p_gtt : &p_gtl);
3683 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003685 int use_sandbox = FALSE;
3686 int save_called_emsg = called_emsg;
3687 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003688 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003689 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3690 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003691
3692 called_emsg = FALSE;
3693
3694 printer_page_num = tabpage_index(tp);
3695# ifdef FEAT_EVAL
3696 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003697 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003698# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003699 /* It's almost as going to the tabpage, but without autocommands. */
3700 curtab->tp_firstwin = firstwin;
3701 curtab->tp_lastwin = lastwin;
3702 curtab->tp_curwin = curwin;
3703 save_curtab = curtab;
3704 curtab = tp;
3705 topframe = curtab->tp_topframe;
3706 firstwin = curtab->tp_firstwin;
3707 lastwin = curtab->tp_lastwin;
3708 curwin = curtab->tp_curwin;
3709 curbuf = curwin->w_buffer;
3710
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003711 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003712 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003713 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003714 STRCPY(NameBuff, res);
3715
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003716 /* Back to the original curtab. */
3717 curtab = save_curtab;
3718 topframe = curtab->tp_topframe;
3719 firstwin = curtab->tp_firstwin;
3720 lastwin = curtab->tp_lastwin;
3721 curwin = curtab->tp_curwin;
3722 curbuf = curwin->w_buffer;
3723
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003724 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003725 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003726 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003727 called_emsg |= save_called_emsg;
3728 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003729
3730 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3731 * use a default label. */
3732 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003733 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003734 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003735 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003736 if (!tooltip)
3737 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003738
3739 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3740 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3741 if (bufIsChanged(wp->w_buffer))
3742 modified = TRUE;
3743 if (modified || wincount > 1)
3744 {
3745 if (wincount > 1)
3746 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3747 else
3748 buf[0] = NUL;
3749 if (modified)
3750 STRCAT(buf, "+");
3751 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003752 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003753 mch_memmove(NameBuff, buf, STRLEN(buf));
3754 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003755 }
3756}
3757
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003758/*
3759 * Send the event for clicking to select tab page "nr".
3760 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003761 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003762 */
3763 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003764send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003765{
3766 char_u string[3];
3767
3768 if (nr == tabpage_index(curtab))
3769 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003770
3771 /* Don't put events in the input queue now. */
3772 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003773# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003774 || cmdwin_type != 0
3775# endif
3776 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003777 {
3778 /* Set it back to the current tab page. */
3779 gui_mch_set_curtab(tabpage_index(curtab));
3780 return FALSE;
3781 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003782
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003783 string[0] = CSI;
3784 string[1] = KS_TABLINE;
3785 string[2] = KE_FILLER;
3786 add_to_input_buf(string, 3);
3787 string[0] = nr;
3788 add_to_input_buf_csi(string, 1);
3789 return TRUE;
3790}
3791
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003792/*
3793 * Send a tabline menu event
3794 */
3795 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003796send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003797{
3798 char_u string[3];
3799
Bram Moolenaar29547192018-12-11 20:39:19 +01003800 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003801 if (hold_gui_events)
3802 return;
3803
Bram Moolenaar29547192018-12-11 20:39:19 +01003804 // Cannot close the last tabpage.
3805 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3806 return;
3807
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003808 string[0] = CSI;
3809 string[1] = KS_TABMENU;
3810 string[2] = KE_FILLER;
3811 add_to_input_buf(string, 3);
3812 string[0] = tabidx;
3813 string[1] = (char_u)(long)event;
3814 add_to_input_buf_csi(string, 2);
3815}
3816
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819/*
3820 * Scrollbar stuff:
3821 */
3822
Bram Moolenaare45828b2006-02-15 22:12:56 +00003823/*
3824 * Remove all scrollbars. Used before switching to another tab page.
3825 */
3826 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003827gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003828{
3829 int i;
3830 win_T *wp;
3831
3832 for (i = 0; i < 3; i++)
3833 {
3834 if (i == SBAR_BOTTOM)
3835 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3836 else
3837 {
3838 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003839 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003840 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003841 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003842 }
3843}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003846gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848 static int sbar_ident = 0;
3849
3850 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3851 sb->wp = wp;
3852 sb->type = type;
3853 sb->value = 0;
3854#ifdef FEAT_GUI_ATHENA
3855 sb->pixval = 0;
3856#endif
3857 sb->size = 1;
3858 sb->max = 1;
3859 sb->top = 0;
3860 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 sb->status_height = 0;
3863 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3864}
3865
3866/*
3867 * Find the scrollbar with the given index.
3868 */
3869 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003870gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 win_T *wp;
3873
3874 if (gui.bottom_sbar.ident == ident)
3875 return &gui.bottom_sbar;
3876 FOR_ALL_WINDOWS(wp)
3877 {
3878 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3879 return &wp->w_scrollbars[SBAR_LEFT];
3880 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3881 return &wp->w_scrollbars[SBAR_RIGHT];
3882 }
3883 return NULL;
3884}
3885
3886/*
3887 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3888 *
3889 * For Win32, Macintosh and GTK+ 2:
3890 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3891 * you stop dragging the scrollbar. We get here each time the scrollbar is
3892 * dragged another pixel, but as far as the rest of vim goes, it thinks
3893 * we're just hanging in the call to DispatchMessage() in
3894 * process_message(). The DispatchMessage() call that hangs was passed a
3895 * mouse button click event in the scrollbar window. -- webb.
3896 *
3897 * Solution: Do the scrolling right here. But only when allowed.
3898 * Ignore the scrollbars while executing an external command or when there
3899 * are still characters to be processed.
3900 */
3901 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003902gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 int sb_num;
3906#ifdef USE_ON_FLY_SCROLL
3907 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909# ifdef FEAT_DIFF
3910 int old_topfill = curwin->w_topfill;
3911# endif
3912#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003913 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 int byte_count;
3915#endif
3916
3917 if (sb == NULL)
3918 return;
3919
3920 /* Don't put events in the input queue now. */
3921 if (hold_gui_events)
3922 return;
3923
3924#ifdef FEAT_CMDWIN
3925 if (cmdwin_type != 0 && sb->wp != curwin)
3926 return;
3927#endif
3928
3929 if (still_dragging)
3930 {
3931 if (sb->wp == NULL)
3932 gui.dragged_sb = SBAR_BOTTOM;
3933 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3934 gui.dragged_sb = SBAR_LEFT;
3935 else
3936 gui.dragged_sb = SBAR_RIGHT;
3937 gui.dragged_wp = sb->wp;
3938 }
3939 else
3940 {
3941 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003942#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003944 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 gui.dragged_wp = NULL;
3946#endif
3947 }
3948
3949 /* Vertical sbar info is kept in the first sbar (the left one) */
3950 if (sb->wp != NULL)
3951 sb = &sb->wp->w_scrollbars[0];
3952
3953 /*
3954 * Check validity of value
3955 */
3956 if (value < 0)
3957 value = 0;
3958#ifdef SCROLL_PAST_END
3959 else if (value > sb->max)
3960 value = sb->max;
3961#else
3962 if (value > sb->max - sb->size + 1)
3963 value = sb->max - sb->size + 1;
3964#endif
3965
3966 sb->value = value;
3967
3968#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003969 /* When not allowed to do the scrolling right now, return.
3970 * This also checked input_available(), but that causes the first click in
3971 * a scrollbar to be ignored when Vim doesn't have focus. */
3972 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 return;
3974#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003975#ifdef FEAT_INS_EXPAND
3976 /* Disallow scrolling the current window when the completion popup menu is
3977 * visible. */
3978 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3979 return;
3980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982#ifdef FEAT_RIGHTLEFT
3983 if (sb->wp == NULL && curwin->w_p_rl)
3984 {
3985 value = sb->max + 1 - sb->size - value;
3986 if (value < 0)
3987 value = 0;
3988 }
3989#endif
3990
3991 if (sb->wp != NULL) /* vertical scrollbar */
3992 {
3993 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3995 sb_num++;
3996 if (wp == NULL)
3997 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999#ifdef USE_ON_FLY_SCROLL
4000 current_scrollbar = sb_num;
4001 scrollbar_value = value;
4002 if (State & NORMAL)
4003 {
4004 gui_do_scroll();
4005 setcursor();
4006 }
4007 else if (State & INSERT)
4008 {
4009 ins_scroll();
4010 setcursor();
4011 }
4012 else if (State & CMDLINE)
4013 {
4014 if (msg_scrolled == 0)
4015 {
4016 gui_do_scroll();
4017 redrawcmdline();
4018 }
4019 }
4020# ifdef FEAT_FOLDING
4021 /* Value may have been changed for closed fold. */
4022 sb->value = sb->wp->w_topline - 1;
4023# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004024
4025 /* When dragging one scrollbar and there is another one at the other
4026 * side move the thumb of that one too. */
4027 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4028 gui_mch_set_scrollbar_thumb(
4029 &sb->wp->w_scrollbars[
4030 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4031 ? SBAR_LEFT : SBAR_RIGHT],
4032 sb->value, sb->size, sb->max);
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#else
4035 bytes[0] = CSI;
4036 bytes[1] = KS_VER_SCROLLBAR;
4037 bytes[2] = KE_FILLER;
4038 bytes[3] = (char_u)sb_num;
4039 byte_count = 4;
4040#endif
4041 }
4042 else
4043 {
4044#ifdef USE_ON_FLY_SCROLL
4045 scrollbar_value = value;
4046
4047 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004048 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 else if (State & INSERT)
4050 ins_horscroll();
4051 else if (State & CMDLINE)
4052 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004053 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004055 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 redrawcmdline();
4057 }
4058 }
4059 if (old_leftcol != curwin->w_leftcol)
4060 {
4061 updateWindow(curwin); /* update window, status and cmdline */
4062 setcursor();
4063 }
4064#else
4065 bytes[0] = CSI;
4066 bytes[1] = KS_HOR_SCROLLBAR;
4067 bytes[2] = KE_FILLER;
4068 byte_count = 3;
4069#endif
4070 }
4071
4072#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 /*
4074 * synchronize other windows, as necessary according to 'scrollbind'
4075 */
4076 if (curwin->w_p_scb
4077 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4078 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004079# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 ))))
4083 {
4084 do_check_scrollbind(TRUE);
4085 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004086 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (wp->w_redr_type > 0)
4088 updateWindow(wp);
4089 setcursor();
4090 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004091 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004093 add_to_input_buf(bytes, byte_count);
4094 add_long_to_buf((long_u)value, bytes);
4095 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#endif
4097}
4098
4099/*
4100 * Scrollbar stuff:
4101 */
4102
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004103/*
4104 * Called when something in the window layout has changed.
4105 */
4106 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004107gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004108{
4109 if (gui.in_use && starting == 0)
4110 {
4111 out_flush();
4112 gui_init_which_components(NULL);
4113 gui_update_scrollbars(TRUE);
4114 }
4115 need_mouse_correct = TRUE;
4116}
4117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004119gui_update_scrollbars(
4120 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121{
4122 win_T *wp;
4123 scrollbar_T *sb;
4124 long val, size, max; /* need 32 bits here */
4125 int which_sb;
4126 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 /* Update the horizontal scrollbar */
4130 gui_update_horiz_scrollbar(force);
4131
Bram Moolenaar4f974752019-02-17 17:44:42 +01004132#ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 /* Return straight away if there is neither a left nor right scrollbar.
4134 * On MS-Windows this is required anyway for scrollwheel messages. */
4135 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4136 return;
4137#endif
4138
4139 /*
4140 * Don't want to update a scrollbar while we're dragging it. But if we
4141 * have both a left and right scrollbar, and we drag one of them, we still
4142 * need to update the other one.
4143 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004144 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4145 && gui.which_scrollbars[SBAR_LEFT]
4146 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
4148 /*
4149 * If we have two scrollbars and one of them is being dragged, just
4150 * copy the scrollbar position from the dragged one to the other one.
4151 */
4152 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4153 if (gui.dragged_wp != NULL)
4154 gui_mch_set_scrollbar_thumb(
4155 &gui.dragged_wp->w_scrollbars[which_sb],
4156 gui.dragged_wp->w_scrollbars[0].value,
4157 gui.dragged_wp->w_scrollbars[0].size,
4158 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160
4161 /* avoid that moving components around generates events */
4162 ++hold_gui_events;
4163
Bram Moolenaar45a24952016-07-24 22:25:15 +02004164 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 if (wp->w_buffer == NULL) /* just in case */
4167 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004168 /* Skip a scrollbar that is being dragged. */
4169 if (!force && (gui.dragged_sb == SBAR_LEFT
4170 || gui.dragged_sb == SBAR_RIGHT)
4171 && gui.dragged_wp == wp)
4172 continue;
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#ifdef SCROLL_PAST_END
4175 max = wp->w_buffer->b_ml.ml_line_count - 1;
4176#else
4177 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4178#endif
4179 if (max < 0) /* empty buffer */
4180 max = 0;
4181 val = wp->w_topline - 1;
4182 size = wp->w_height;
4183#ifdef SCROLL_PAST_END
4184 if (val > max) /* just in case */
4185 val = max;
4186#else
4187 if (size > max + 1) /* just in case */
4188 size = max + 1;
4189 if (val > max - size + 1)
4190 val = max - size + 1;
4191#endif
4192 if (val < 0) /* minimal value is 0 */
4193 val = 0;
4194
4195 /*
4196 * Scrollbar at index 0 (the left one) contains all the information.
4197 * It would be the same info for left and right so we just store it for
4198 * one of them.
4199 */
4200 sb = &wp->w_scrollbars[0];
4201
4202 /*
4203 * Note: no check for valid w_botline. If it's not valid the
4204 * scrollbars will be updated later anyway.
4205 */
4206 if (size < 1 || wp->w_botline - 2 > max)
4207 {
4208 /*
4209 * This can happen during changing files. Just don't update the
4210 * scrollbar for now.
4211 */
4212 sb->height = 0; /* Force update next time */
4213 if (gui.which_scrollbars[SBAR_LEFT])
4214 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4215 if (gui.which_scrollbars[SBAR_RIGHT])
4216 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4217 continue;
4218 }
4219 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 || sb->top != wp->w_winrow
4221 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004223 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
4225 /* Height, width or position of scrollbar has changed. For
4226 * vertical split: curwin changed. */
4227 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 sb->top = wp->w_winrow;
4229 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
4232 /* Calculate height and position in pixels */
4233 h = (sb->height + sb->status_height) * gui.char_height;
4234 y = sb->top * gui.char_height + gui.border_offset;
4235#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4236 if (gui.menu_is_active)
4237 y += gui.menu_height;
4238#endif
4239
4240#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4241 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4242# ifdef FEAT_GUI_ATHENA
4243 y += gui.toolbar_height;
4244# else
4245# ifdef FEAT_GUI_MSWIN
4246 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4247# endif
4248# endif
4249#endif
4250
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004251#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4252 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004253 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004254#endif
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
4258 /* Height of top scrollbar includes width of top border */
4259 h += gui.border_offset;
4260 y -= gui.border_offset;
4261 }
4262 if (gui.which_scrollbars[SBAR_LEFT])
4263 {
4264 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4265 gui.left_sbar_x, y,
4266 gui.scrollbar_width, h);
4267 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4268 }
4269 if (gui.which_scrollbars[SBAR_RIGHT])
4270 {
4271 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4272 gui.right_sbar_x, y,
4273 gui.scrollbar_width, h);
4274 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4275 }
4276 }
4277
4278 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4279 * checking if the thumb moved at least a pixel. Only do this for
4280 * Athena, most other GUIs require the update anyway to make the
4281 * arrows work. */
4282#ifdef FEAT_GUI_ATHENA
4283 if (max == 0)
4284 y = 0;
4285 else
4286 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4287 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4288#else
4289 if (force || sb->value != val || sb->size != size || sb->max != max)
4290#endif
4291 {
4292 /* Thumb of scrollbar has moved */
4293 sb->value = val;
4294#ifdef FEAT_GUI_ATHENA
4295 sb->pixval = y;
4296#endif
4297 sb->size = size;
4298 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004299 if (gui.which_scrollbars[SBAR_LEFT]
4300 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4302 val, size, max);
4303 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004304 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4306 val, size, max);
4307 }
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 --hold_gui_events;
4311}
4312
4313/*
4314 * Enable or disable a scrollbar.
4315 * Check for scrollbars for vertically split windows which are not enabled
4316 * sometimes.
4317 */
4318 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004319gui_do_scrollbar(
4320 win_T *wp,
4321 int which, /* SBAR_LEFT or SBAR_RIGHT */
4322 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int midcol = curwin->w_wincol + curwin->w_width / 2;
4325 int has_midcol = (wp->w_wincol <= midcol
4326 && wp->w_wincol + wp->w_width >= midcol);
4327
4328 /* Only enable scrollbars that contain the middle column of the current
4329 * window. */
4330 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4331 {
4332 /* Scrollbars only on one side. Don't enable scrollbars that don't
4333 * contain the middle column of the current window. */
4334 if (!has_midcol)
4335 enable = FALSE;
4336 }
4337 else
4338 {
4339 /* Scrollbars on both sides. Don't enable scrollbars that neither
4340 * contain the middle column of the current window nor are on the far
4341 * side. */
4342 if (midcol > Columns / 2)
4343 {
4344 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4345 enable = FALSE;
4346 }
4347 else
4348 {
4349 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4350 : !has_midcol)
4351 enable = FALSE;
4352 }
4353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4355}
4356
4357/*
4358 * Scroll a window according to the values set in the globals current_scrollbar
4359 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4360 * or FALSE otherwise.
4361 */
4362 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004363gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 win_T *wp, *save_wp;
4366 int i;
4367 long nlines;
4368 pos_T old_cursor;
4369 linenr_T old_topline;
4370#ifdef FEAT_DIFF
4371 int old_topfill;
4372#endif
4373
4374 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4375 if (wp == NULL)
4376 break;
4377 if (wp == NULL)
4378 /* Couldn't find window */
4379 return FALSE;
4380
4381 /*
4382 * Compute number of lines to scroll. If zero, nothing to do.
4383 */
4384 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4385 if (nlines == 0)
4386 return FALSE;
4387
4388 save_wp = curwin;
4389 old_topline = wp->w_topline;
4390#ifdef FEAT_DIFF
4391 old_topfill = wp->w_topfill;
4392#endif
4393 old_cursor = wp->w_cursor;
4394 curwin = wp;
4395 curbuf = wp->w_buffer;
4396 if (nlines < 0)
4397 scrolldown(-nlines, gui.dragged_wp == NULL);
4398 else
4399 scrollup(nlines, gui.dragged_wp == NULL);
4400 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4401 * the mouse-up event already, but we still want it to behave like when
4402 * dragging. But not the next click in an arrow. */
4403 if (gui.dragged_sb == SBAR_NONE)
4404 gui.dragged_wp = NULL;
4405
4406 if (old_topline != wp->w_topline
4407#ifdef FEAT_DIFF
4408 || old_topfill != wp->w_topfill
4409#endif
4410 )
4411 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004412 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 cursor_correct(); /* fix window for 'so' */
4415 update_topline(); /* avoid up/down jump */
4416 }
4417 if (old_cursor.lnum != wp->w_cursor.lnum)
4418 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004422 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4423 validate_cursor();
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 curwin = save_wp;
4426 curbuf = save_wp->w_buffer;
4427
4428 /*
4429 * Don't call updateWindow() when nothing has changed (it will overwrite
4430 * the status line!).
4431 */
4432 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004433 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#ifdef FEAT_DIFF
4435 || old_topfill != wp->w_topfill
4436#endif
4437 )
4438 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004439 int type = VALID;
4440
4441#ifdef FEAT_INS_EXPAND
4442 if (pum_visible())
4443 {
4444 type = NOT_VALID;
4445 wp->w_lines_valid = 0;
4446 }
4447#endif
4448 /* Don't set must_redraw here, it may cause the popup menu to
4449 * disappear when losing focus after a scrollbar drag. */
4450 if (wp->w_redr_type < type)
4451 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004452 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004454 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004457#ifdef FEAT_INS_EXPAND
4458 /* May need to redraw the popup menu. */
4459 if (pum_visible())
4460 pum_redraw();
4461#endif
4462
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004463 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464}
4465
4466
4467/*
4468 * Horizontal scrollbar stuff:
4469 */
4470
4471/*
4472 * Return length of line "lnum" for horizontal scrolling.
4473 */
4474 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004475scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
4477 char_u *p;
4478 colnr_T col;
4479 int w;
4480
4481 p = ml_get(lnum);
4482 col = 0;
4483 if (*p != NUL)
4484 for (;;)
4485 {
4486 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004487 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (*p == NUL) /* don't count the last character */
4489 break;
4490 col += w;
4491 }
4492 return col;
4493}
4494
4495/* Remember which line is currently the longest, so that we don't have to
4496 * search for it when scrolling horizontally. */
4497static linenr_T longest_lnum = 0;
4498
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004499/*
4500 * Find longest visible line number. If this is not possible (or not desired,
4501 * by setting 'h' in "guioptions") then the current line number is returned.
4502 */
4503 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004504gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004505{
4506 linenr_T ret = 0;
4507
4508 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4509 * line numbers, topline and botline can be invalid when displaying is
4510 * postponed. */
4511 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4512 && curwin->w_topline <= curwin->w_cursor.lnum
4513 && curwin->w_botline > curwin->w_cursor.lnum
4514 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4515 {
4516 linenr_T lnum;
4517 colnr_T n;
4518 long max = 0;
4519
4520 /* Use maximum of all visible lines. Remember the lnum of the
4521 * longest line, closest to the cursor line. Used when scrolling
4522 * below. */
4523 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4524 {
4525 n = scroll_line_len(lnum);
4526 if (n > (colnr_T)max)
4527 {
4528 max = n;
4529 ret = lnum;
4530 }
4531 else if (n == (colnr_T)max
4532 && abs((int)(lnum - curwin->w_cursor.lnum))
4533 < abs((int)(ret - curwin->w_cursor.lnum)))
4534 ret = lnum;
4535 }
4536 }
4537 else
4538 /* Use cursor line only. */
4539 ret = curwin->w_cursor.lnum;
4540
4541 return ret;
4542}
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004545gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 long value, size, max; /* need 32 bit ints here */
4548
4549 if (!gui.which_scrollbars[SBAR_BOTTOM])
4550 return;
4551
4552 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4553 return;
4554
4555 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4556 return;
4557
4558 /*
4559 * It is possible for the cursor to be invalid if we're in the middle of
4560 * something (like changing files). If so, don't do anything for now.
4561 */
4562 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4563 {
4564 gui.bottom_sbar.value = -1;
4565 return;
4566 }
4567
Bram Moolenaar02631462017-09-22 15:20:32 +02004568 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (curwin->w_p_wrap)
4570 {
4571 value = 0;
4572#ifdef SCROLL_PAST_END
4573 max = 0;
4574#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004575 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#endif
4577 }
4578 else
4579 {
4580 value = curwin->w_leftcol;
4581
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004582 longest_lnum = gui_find_longest_lnum();
4583 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (virtual_active())
4586 {
4587 /* May move the cursor even further to the right. */
4588 if (curwin->w_virtcol >= (colnr_T)max)
4589 max = curwin->w_virtcol;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004593 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
4595 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004596 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 size -= curwin_col_off();
4598#ifndef SCROLL_PAST_END
4599 max -= curwin_col_off();
4600#endif
4601 }
4602
4603#ifndef SCROLL_PAST_END
4604 if (value > max - size + 1)
4605 value = max - size + 1; /* limit the value to allowable range */
4606#endif
4607
4608#ifdef FEAT_RIGHTLEFT
4609 if (curwin->w_p_rl)
4610 {
4611 value = max + 1 - size - value;
4612 if (value < 0)
4613 {
4614 size += value;
4615 value = 0;
4616 }
4617 }
4618#endif
4619 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4620 && max == gui.bottom_sbar.max)
4621 return;
4622
4623 gui.bottom_sbar.value = value;
4624 gui.bottom_sbar.size = size;
4625 gui.bottom_sbar.max = max;
4626 gui.prev_wrap = curwin->w_p_wrap;
4627
4628 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4629}
4630
4631/*
4632 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4633 */
4634 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004635gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 /* no wrapping, no scrolling */
4638 if (curwin->w_p_wrap)
4639 return FALSE;
4640
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004641 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return FALSE;
4643
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004644 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004647 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004649 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004650 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652 if (compute_longest_lnum)
4653 {
4654 curwin->w_cursor.lnum = gui_find_longest_lnum();
4655 curwin->w_cursor.col = 0;
4656 }
4657 /* Do a sanity check on "longest_lnum", just in case. */
4658 else if (longest_lnum >= curwin->w_topline
4659 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
4661 curwin->w_cursor.lnum = longest_lnum;
4662 curwin->w_cursor.col = 0;
4663 }
4664 }
4665
4666 return leftcol_changed();
4667}
4668
4669/*
4670 * Check that none of the colors are the same as the background color
4671 */
4672 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004673gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4676 {
4677 gui_set_bg_color((char_u *)"White");
4678 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4679 gui_set_fg_color((char_u *)"Black");
4680 }
4681}
4682
Bram Moolenaar3918c952005-03-15 22:34:55 +00004683 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004684gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 gui.norm_pixel = gui_get_color(name);
4687 hl_set_fg_color_name(vim_strsave(name));
4688}
4689
Bram Moolenaar3918c952005-03-15 22:34:55 +00004690 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004691gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 gui.back_pixel = gui_get_color(name);
4694 hl_set_bg_color_name(vim_strsave(name));
4695}
4696
4697/*
4698 * Allocate a color by name.
4699 * Returns INVALCOLOR and gives an error message when failed.
4700 */
4701 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004702gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 guicolor_T t;
4705
4706 if (*name == NUL)
4707 return INVALCOLOR;
4708 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004711#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 && gui.in_use
4713#endif
4714 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004715 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return t;
4717}
4718
4719/*
4720 * Return the grey value of a color (range 0-255).
4721 */
4722 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004723gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004725 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004727 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004728 + (((rgb >> 8) & 0xff) * 587)
4729 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
4732#if defined(FEAT_GUI_X11) || defined(PROTO)
4733 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004734gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 win_T *wp;
4737
4738 /* Nothing to do if GUI hasn't started yet. */
4739 if (!gui.in_use)
4740 return;
4741
4742 FOR_ALL_WINDOWS(wp)
4743 {
4744 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4745 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4746 }
4747 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4748}
4749#endif
4750
4751/*
4752 * Call this when focus has changed.
4753 */
4754 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004755gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757/*
4758 * Skip this code to avoid drawing the cursor when debugging and switching
4759 * between the debugger window and gvim.
4760 */
4761#if 1
4762 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004763 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765# ifdef FEAT_XIM
4766 xim_set_focus(in_focus);
4767# endif
4768
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004769 /* Put events in the input queue only when allowed.
4770 * ui_focus_change() isn't called directly, because it invokes
4771 * autocommands and that must not happen asynchronously. */
4772 if (!hold_gui_events)
4773 {
4774 char_u bytes[3];
4775
4776 bytes[0] = CSI;
4777 bytes[1] = KS_EXTRA;
4778 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4779 add_to_input_buf(bytes, 3);
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#endif
4782}
4783
4784/*
4785 * Called when the mouse moved (but not when dragging).
4786 */
4787 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004788gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004791 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
Bram Moolenaard3667a22006-03-16 21:35:52 +00004793 /* Ignore this while still starting up. */
4794 if (!gui.in_use || gui.starting)
4795 return;
4796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797#ifdef FEAT_MOUSESHAPE
4798 /* Get window pointer, and update mouse shape as well. */
4799 wp = xy2win(x, y);
4800#endif
4801
4802 /* Only handle this when 'mousefocus' set and ... */
4803 if (p_mousef
4804 && !hold_gui_events /* not holding events */
4805 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4806 && State != HITRETURN /* but not hit-return prompt */
4807 && msg_scrolled == 0 /* no scrolled message */
4808 && !need_mouse_correct /* not moving the pointer */
4809 && gui.in_focus) /* gvim in focus */
4810 {
4811 /* Don't move the mouse when it's left or right of the Vim window */
4812 if (x < 0 || x > Columns * gui.char_width)
4813 return;
4814#ifndef FEAT_MOUSESHAPE
4815 wp = xy2win(x, y);
4816#endif
4817 if (wp == curwin || wp == NULL)
4818 return; /* still in the same old window, or none at all */
4819
Bram Moolenaar9c102382006-05-03 21:26:49 +00004820 /* Ignore position in the tab pages line. */
4821 if (Y_2_ROW(y) < tabline_height())
4822 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 /*
4825 * format a mouse click on status line input
4826 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004827 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4828 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 */
4830 if (finish_op)
4831 {
4832 /* abort the current operator first */
4833 st[0] = ESC;
4834 add_to_input_buf(st, 1);
4835 }
4836 st[0] = CSI;
4837 st[1] = KS_MOUSE;
4838 st[2] = KE_FILLER;
4839 st[3] = (char_u)MOUSE_LEFT;
4840 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004841 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 wp->w_height + W_WINROW(wp));
4843
4844 add_to_input_buf(st, 8);
4845 st[3] = (char_u)MOUSE_RELEASE;
4846 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#ifdef FEAT_GUI_GTK
4848 /* Need to wake up the main loop */
4849 if (gtk_main_level() > 0)
4850 gtk_main_quit();
4851#endif
4852 }
4853}
4854
4855/*
4856 * Called when mouse should be moved to window with focus.
4857 */
4858 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004859gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
4861 int x, y;
4862 win_T *wp = NULL;
4863
4864 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004865
4866 if (!(gui.in_use && p_mousef))
4867 return;
4868
4869 gui_mch_getmouse(&x, &y);
4870 /* Don't move the mouse when it's left or right of the Vim window */
4871 if (x < 0 || x > Columns * gui.char_width)
4872 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004873 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004874 wp = xy2win(x, y);
4875 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004877 validate_cline_row();
4878 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4879 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882}
4883
4884/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004885 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004886 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 static win_T *
Bram Moolenaar4f974752019-02-17 17:44:42 +01004889xy2win(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int row;
4892 int col;
4893 win_T *wp;
4894
4895 row = Y_2_ROW(y);
4896 col = X_2_COL(x);
4897 if (row < 0 || col < 0) /* before first window */
4898 return NULL;
4899 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004900 if (wp == NULL)
4901 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004902#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (State == HITRETURN || State == ASKMORE)
4904 {
4905 if (Y_2_ROW(y) >= msg_row)
4906 update_mouseshape(SHAPE_IDX_MOREL);
4907 else
4908 update_mouseshape(SHAPE_IDX_MORE);
4909 }
4910 else if (row > wp->w_height) /* below status line */
4911 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004912 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004913 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004915 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004916 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 update_mouseshape(SHAPE_IDX_STATUS);
4918 else
4919 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004921 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922}
4923
4924/*
4925 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4926 * File names may be given to redefine the args list.
4927 */
4928 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004929ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 char_u *arg = eap->arg;
4932
4933 /*
4934 * Check for "-f" argument: foreground, don't fork.
4935 * Also don't fork when started with "gvim -f".
4936 * Do fork when using "gui -b".
4937 */
4938 if (arg[0] == '-'
4939 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004940 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 gui.dofork = (arg[1] == 'b');
4943 eap->arg = skipwhite(eap->arg + 2);
4944 }
4945 if (!gui.in_use)
4946 {
4947 /* Clear the command. Needed for when forking+exiting, to avoid part
4948 * of the argument ending up after the shell prompt. */
4949 msg_clr_eos_force();
4950 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004951#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004952 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 if (!ends_excmd(*eap->arg))
4956 ex_next(eap);
4957}
4958
Bram Moolenaar4f974752019-02-17 17:44:42 +01004959#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
4960 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)) \
4961 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
4963 * This is shared between Athena, Motif and GTK.
4964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966/*
4967 * Callback function for do_in_runtimepath().
4968 */
4969 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004970gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004972 char_u *gfp_buffer = cookie;
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (STRLEN(fname) >= MAXPATHL)
4975 *gfp_buffer = NUL;
4976 else
4977 STRCPY(gfp_buffer, fname);
4978}
4979
4980/*
4981 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4982 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4983 */
4984 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004985gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 if (STRLEN(name) > MAXPATHL - 14)
4988 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004989 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004990 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004991 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 return FAIL;
4993 return OK;
4994}
4995
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004996# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997/*
4998 * Given the name of the "icon=" argument, try finding the bitmap file for the
4999 * icon. If it is an absolute path name, use it as it is. Otherwise append
5000 * "ext" and search for it in 'runtimepath'.
5001 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5002 * contains "name".
5003 */
5004 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005005gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007 char_u buf[MAXPATHL + 1];
5008
5009 expand_env(name, buffer, MAXPATHL);
5010 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5011 STRCPY(buffer, buf);
5012}
5013# endif
5014#endif
5015
Bram Moolenaar9372a112005-12-06 19:59:18 +00005016#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005018display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020 char_u *p;
5021
5022 if (isatty(2))
5023 fflush(stderr);
5024 else if (error_ga.ga_data != NULL)
5025 {
5026 /* avoid putting up a message box with blanks only */
5027 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5028 if (!isspace(*p))
5029 {
5030 /* Truncate a very long message, it will go off-screen. */
5031 if (STRLEN(p) > 2000)
5032 STRCPY(p + 2000 - 14, "...(truncated)");
5033 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005034 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036 }
5037 ga_clear(&error_ga);
5038 }
5039}
5040#endif
5041
5042#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5043/*
5044 * Return TRUE if still starting up and there is no place to enter text.
5045 * For GTK and X11 we check if stderr is not a tty, which means we were
5046 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5047 * allow typing on stdin.
5048 */
5049 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005050no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 return ((!gui.in_use || gui.starting)
5053# ifndef NO_CONSOLE
5054 && !isatty(0) && !isatty(2)
5055# endif
5056 );
5057}
5058#endif
5059
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005060#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005061 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005062 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063/*
5064 * Update the current window and the screen.
5065 */
5066 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005067gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005069# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005070 linenr_T conceal_old_cursor_line = 0;
5071 linenr_T conceal_new_cursor_line = 0;
5072 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005073# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 update_topline();
5076 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005077
Bram Moolenaarec80df72008-05-07 19:46:51 +00005078 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005079 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005080# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005081 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005082# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005083 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005084 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005085 if (has_cursormoved())
5086 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005087# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005088 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005089 {
5090 conceal_old_cursor_line = last_cursormoved.lnum;
5091 conceal_new_cursor_line = curwin->w_cursor.lnum;
5092 conceal_update_lines = TRUE;
5093 }
5094# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005095 last_cursormoved = curwin->w_cursor;
5096 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005097
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005098# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005099 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005100 && (conceal_old_cursor_line != conceal_new_cursor_line
5101 || conceal_cursor_line(curwin)
5102 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005103 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005104 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005105 redrawWinline(curwin, conceal_old_cursor_line);
5106 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005107 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005108 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005109 }
5110# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005111 update_screen(0); /* may need to update the screen */
5112 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005113 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114}
5115#endif
5116
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005117#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118/*
5119 * Get the text to use in a find/replace dialog. Uses the last search pattern
5120 * if the argument is empty.
5121 * Returns an allocated string.
5122 */
5123 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005124get_find_dialog_text(
5125 char_u *arg,
5126 int *wwordp, /* return: TRUE if \< \> found */
5127 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128{
5129 char_u *text;
5130
5131 if (*arg == NUL)
5132 text = last_search_pat();
5133 else
5134 text = arg;
5135 if (text != NULL)
5136 {
5137 text = vim_strsave(text);
5138 if (text != NULL)
5139 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005140 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 int i;
5142
5143 /* Remove "\V" */
5144 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5145 {
5146 mch_memmove(text, text + 2, (size_t)(len - 1));
5147 len -= 2;
5148 }
5149
5150 /* Recognize "\c" and "\C" and remove. */
5151 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5152 {
5153 *mcasep = (text[1] == 'C');
5154 mch_memmove(text, text + 2, (size_t)(len - 1));
5155 len -= 2;
5156 }
5157
5158 /* Recognize "\<text\>" and remove. */
5159 if (len >= 4
5160 && STRNCMP(text, "\\<", 2) == 0
5161 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5162 {
5163 *wwordp = TRUE;
5164 mch_memmove(text, text + 2, (size_t)(len - 4));
5165 text[len - 4] = NUL;
5166 }
5167
5168 /* Recognize "\/" or "\?" and remove. */
5169 for (i = 0; i + 1 < len; ++i)
5170 if (text[i] == '\\' && (text[i + 1] == '/'
5171 || text[i + 1] == '?'))
5172 {
5173 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5174 --len;
5175 }
5176 }
5177 }
5178 return text;
5179}
5180
5181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 * Handle the press of a button in the find-replace dialog.
5183 * Return TRUE when something was added to the input buffer.
5184 */
5185 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005186gui_do_findrepl(
5187 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5188 char_u *find_text,
5189 char_u *repl_text,
5190 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
5192 garray_T ga;
5193 int i;
5194 int type = (flags & FRD_TYPE_MASK);
5195 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005196 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005197 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005198 static int busy = FALSE;
5199
5200 /* When the screen is being updated we should not change buffers and
5201 * windows structures, it may cause freed memory to be used. Also don't
5202 * do this recursively (pressing "Find" quickly several times. */
5203 if (updating_screen || busy)
5204 return FALSE;
5205
5206 /* refuse replace when text cannot be changed */
5207 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5208 return FALSE;
5209
5210 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
5212 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005213 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 ga_concat(&ga, (char_u *)"%s/");
5215
5216 ga_concat(&ga, (char_u *)"\\V");
5217 if (flags & FRD_MATCH_CASE)
5218 ga_concat(&ga, (char_u *)"\\C");
5219 else
5220 ga_concat(&ga, (char_u *)"\\c");
5221 if (flags & FRD_WHOLE_WORD)
5222 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005223 /* escape / and \ */
5224 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5225 if (p != NULL)
5226 ga_concat(&ga, p);
5227 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (flags & FRD_WHOLE_WORD)
5229 ga_concat(&ga, (char_u *)"\\>");
5230
5231 if (type == FRD_REPLACEALL)
5232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005234 /* escape / and \ */
5235 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5236 if (p != NULL)
5237 ga_concat(&ga, p);
5238 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005240 }
5241 ga_append(&ga, NUL);
5242
5243 if (type == FRD_REPLACE)
5244 {
5245 /* Do the replacement when the text at the cursor matches. Thus no
5246 * replacement is done if the cursor was moved! */
5247 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5248 regmatch.rm_ic = 0;
5249 if (regmatch.regprog != NULL)
5250 {
5251 p = ml_get_cursor();
5252 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5253 && regmatch.startp[0] == p)
5254 {
5255 /* Clear the command line to remove any old "No match"
5256 * error. */
5257 msg_end_prompt();
5258
5259 if (u_save_cursor() == OK)
5260 {
5261 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005262 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005263
5264 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005265 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005266 ins_str(repl_text);
5267 }
5268 }
5269 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005270 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005271 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005272 }
5273 }
5274
5275 if (type == FRD_REPLACEALL)
5276 {
5277 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005278 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 do_cmdline_cmd(ga.ga_data);
5280 }
5281 else
5282 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005283 int searchflags = SEARCH_MSG + SEARCH_MARK;
5284
5285 /* Search for the next match.
5286 * Don't skip text under cursor for single replace. */
5287 if (type == FRD_REPLACE)
5288 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005290 if (down)
5291 {
5292 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5293 }
5294 else
5295 {
5296 /* We need to escape '?' if and only if we are searching in the up
5297 * direction */
5298 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5299 if (p != NULL)
5300 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5301 vim_free(p);
5302 }
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 msg_scroll = i; /* don't let an error message set msg_scroll */
5305 }
5306
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005307 /* Don't want to pass did_emsg to other code, it may cause disabling
5308 * syntax HL if we were busy redrawing. */
5309 did_emsg = save_did_emsg;
5310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (State & (NORMAL | INSERT))
5312 {
5313 gui_update_screen(); /* update the screen */
5314 msg_didout = 0; /* overwrite any message */
5315 need_wait_return = FALSE; /* don't wait for return */
5316 }
5317
5318 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005319 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 return (ga.ga_len > 0);
5321}
5322
5323#endif
5324
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005325#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326/*
5327 * Jump to the window at specified point (x, y).
5328 */
5329 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005330gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
5332 int row = Y_2_ROW(y);
5333 int col = X_2_COL(x);
5334 win_T *wp;
5335
5336 if (row >= 0 && col >= 0)
5337 {
5338 wp = mouse_find_win(&row, &col);
5339 if (wp != NULL && wp != curwin)
5340 win_goto(wp);
5341 }
5342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005345 * Function passed to handle_drop() for the actions to be done after the
5346 * argument list has been updated.
5347 */
5348 static void
5349drop_callback(void *cookie)
5350{
5351 char_u *p = cookie;
5352
5353 /* If Shift held down, change to first file's directory. If the first
5354 * item is a directory, change to that directory (and let the explorer
5355 * plugin show the contents). */
5356 if (p != NULL)
5357 {
5358 if (mch_isdir(p))
5359 {
5360 if (mch_chdir((char *)p) == 0)
5361 shorten_fnames(TRUE);
5362 }
5363 else if (vim_chdirfile(p, "drop") == OK)
5364 shorten_fnames(TRUE);
5365 vim_free(p);
5366 }
5367
5368 /* Update the screen display */
5369 update_screen(NOT_VALID);
5370# ifdef FEAT_MENU
5371 gui_update_menus(0);
5372# endif
5373#ifdef FEAT_TITLE
5374 maketitle();
5375#endif
5376 setcursor();
5377 out_flush_cursor(FALSE, FALSE);
5378}
5379
5380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 * Process file drop. Mouse cursor position, key modifiers, name of files
5382 * and count of files are given. Argument "fnames[count]" has full pathnames
5383 * of dropped files, they will be freed in this function, and caller can't use
5384 * fnames after call this function.
5385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005387gui_handle_drop(
5388 int x UNUSED,
5389 int y UNUSED,
5390 int_u modifiers,
5391 char_u **fnames,
5392 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 int i;
5395 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005396 static int entered = FALSE;
5397
5398 /*
5399 * This function is called by event handlers. Just in case we get a
5400 * second event before the first one is handled, ignore the second one.
5401 * Not sure if this can ever happen, just in case.
5402 */
5403 if (entered)
5404 return;
5405 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
5407 /*
5408 * When the cursor is at the command line, add the file names to the
5409 * command line, don't edit the files.
5410 */
5411 if (State & CMDLINE)
5412 {
5413 shorten_filenames(fnames, count);
5414 for (i = 0; i < count; ++i)
5415 {
5416 if (fnames[i] != NULL)
5417 {
5418 if (i > 0)
5419 add_to_input_buf((char_u*)" ", 1);
5420
5421 /* We don't know what command is used thus we can't be sure
5422 * about which characters need to be escaped. Only escape the
5423 * most common ones. */
5424# ifdef BACKSLASH_IN_FILENAME
5425 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5426# else
5427 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5428# endif
5429 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005430 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 vim_free(p);
5432 vim_free(fnames[i]);
5433 }
5434 }
5435 vim_free(fnames);
5436 }
5437 else
5438 {
5439 /* Go to the window under mouse cursor, then shorten given "fnames" by
5440 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 shorten_filenames(fnames, count);
5443
5444 /* If Shift held down, remember the first item. */
5445 if ((modifiers & MOUSE_SHIFT) != 0)
5446 p = vim_strsave(fnames[0]);
5447 else
5448 p = NULL;
5449
5450 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005451 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 * directory and Shift is held down. */
5453 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5454 && mch_isdir(fnames[0]))
5455 {
5456 vim_free(fnames[0]);
5457 vim_free(fnames);
5458 }
5459 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005460 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5461 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005463
5464 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465}
5466#endif