blob: ed89985f0e0f4fc02ffdd99c34c80693dbdf4455 [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 Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !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#ifdef FEAT_MBYTE
401 gui.wide_font = NOFONT;
402# ifndef FEAT_GUI_GTK
403 gui.wide_bold_font = NOFONT;
404 gui.wide_ital_font = NOFONT;
405 gui.wide_boldital_font = NOFONT;
406# endif
407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200410# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411# ifdef FONTSET_ALWAYS
412 gui.menu_fontset = NOFONTSET;
413# else
414 gui.menu_font = NOFONT;
415# endif
416# endif
417 gui.menu_is_active = TRUE; /* default: include menu */
418# ifndef FEAT_GUI_GTK
419 gui.menu_height = MENU_DEFAULT_HEIGHT;
420 gui.menu_width = 0;
421# endif
422#endif
423#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
424 gui.toolbar_height = 0;
425#endif
426#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
427 gui.footer_height = 0;
428#endif
429#ifdef FEAT_BEVAL_TIP
430 gui.tooltip_fontset = NOFONTSET;
431#endif
432
433 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
434 gui.prev_wrap = -1;
435
436#ifdef ALWAYS_USE_GUI
437 result = OK;
438#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200439# ifdef FEAT_GUI_GTK
440 /*
441 * Note: Don't call gtk_init_check() before fork, it will be called after
442 * the fork. When calling it before fork, it make vim hang for a while.
443 * See gui_do_fork().
444 * Use a simpler check if the GUI window can probably be opened.
445 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200446 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#endif
451 return result;
452}
453
454/*
455 * This is the call which starts the GUI.
456 */
457 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100458gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 win_T *wp;
461 static int recursive = 0;
462
463 /*
464 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
465 * function will then be executed at the first call, the rest by the
466 * recursive call. This allow the shell to be opened halfway reading a
467 * gvimrc file.
468 */
469 if (!recursive)
470 {
471 ++recursive;
472
473 clip_init(TRUE);
474
475 /* If can't initialize, don't try doing the rest */
476 if (gui_init_check() == FAIL)
477 {
478 --recursive;
479 clip_init(FALSE);
480 return;
481 }
482
483 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000484 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
485 * breaks the Paste toolbar button.
486 */
487 set_option_value((char_u *)"paste", 0L, NULL, 0);
488
489 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Set up system-wide default menus.
491 */
492#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
493 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
494 {
495 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000496 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 sys_menu = FALSE;
498 }
499#endif
500
501 /*
502 * Switch on the mouse by default, unless the user changed it already.
503 * This can then be changed in the .gvimrc.
504 */
505 if (!option_was_set((char_u *)"mouse"))
506 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000507 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
509 /*
510 * If -U option given, use only the initializations from that file and
511 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
512 */
513 if (use_gvimrc != NULL)
514 {
515 if (STRCMP(use_gvimrc, "NONE") != 0
516 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000517 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100518 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 else
521 {
522 /*
523 * Get system wide defaults for gvim, only when file name defined.
524 */
525#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000526 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#endif
528
529 /*
530 * Try to read GUI initialization commands from the following
531 * places:
532 * - environment variable GVIMINIT
533 * - the user gvimrc file (~/.gvimrc)
534 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
535 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
536 * The first that exists is used, the rest is ignored.
537 */
538 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000539 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
540 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000542 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
543 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200545#ifdef USR_GVIMRC_FILE3
546 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
547 DOSO_GVIMRC) == FAIL
548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 )
550 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200551#ifdef USR_GVIMRC_FILE4
552 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#endif
554 }
555
556 /*
557 * Read initialization commands from ".gvimrc" in current
558 * directory. This is only done if the 'exrc' option is set.
559 * Because of security reasons we disallow shell and write
560 * commands now, except for unix if the file is owned by the user
561 * or 'secure' option has been reset in environment of global
562 * ".gvimrc".
563 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
564 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
565 */
566 if (p_exrc)
567 {
568#ifdef UNIX
569 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200570 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 /* if ".gvimrc" file is not owned by user, set 'secure'
573 * mode */
574 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
575 secure = p_secure;
576 }
577#else
578 secure = p_secure;
579#endif
580
581 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
582 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
583#ifdef SYS_GVIMRC_FILE
584 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
585 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
586#endif
587#ifdef USR_GVIMRC_FILE2
588 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
589 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
590#endif
591#ifdef USR_GVIMRC_FILE3
592 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
593 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
594#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200595#ifdef USR_GVIMRC_FILE4
596 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
597 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000600 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602 if (secure == 2)
603 need_wait_return = TRUE;
604 secure = 0;
605 }
606 }
607
608 if (need_wait_return || msg_didany)
609 wait_return(TRUE);
610
611 --recursive;
612 }
613
614 /* If recursive call opened the shell, return here from the first call */
615 if (gui.in_use)
616 return;
617
618 /*
619 * Create the GUI shell.
620 */
621 gui.in_use = TRUE; /* Must be set after menus have been set up */
622 if (gui_mch_init() == FAIL)
623 goto error;
624
625 /* Avoid a delay for an error message that was printed in the terminal
626 * where Vim was started. */
627 emsg_on_display = FALSE;
628 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100629 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 need_wait_return = FALSE;
631 msg_didany = FALSE;
632
633 /*
634 * Check validity of any generic resources that may have been loaded.
635 */
636 if (gui.border_width < 0)
637 gui.border_width = 0;
638
639 /*
640 * Set up the fonts. First use a font specified with "-fn" or "-font".
641 */
642 if (font_argument != NULL)
643 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
644 if (
645#ifdef FEAT_XFONTSET
646 (*p_guifontset == NUL
647 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
648#endif
649 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
650 : p_guifont, FALSE) == FAIL)
651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100652 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 goto error2;
654 }
655#ifdef FEAT_MBYTE
656 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100657 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#endif
659
660 gui.num_cols = Columns;
661 gui.num_rows = Rows;
662 gui_reset_scroll_region();
663
664 /* Create initial scrollbars */
665 FOR_ALL_WINDOWS(wp)
666 {
667 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
668 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
669 }
670 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
671
672#ifdef FEAT_MENU
673 gui_create_initial_menus(root_menu);
674#endif
675#ifdef FEAT_SUN_WORKSHOP
676 if (usingSunWorkShop)
677 workshop_init();
678#endif
679#ifdef FEAT_SIGN_ICONS
680 sign_gui_started();
681#endif
682
683 /* Configure the desired menu and scrollbars */
684 gui_init_which_components(NULL);
685
686 /* All components of the GUI have been created now */
687 gui.shell_created = TRUE;
688
689#ifndef FEAT_GUI_GTK
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100690 // Set the shell size, adjusted for the screen size. For GTK this only
691 // works after the shell has been opened, thus it is further down.
692 // For MS-Windows pass FALSE for "mustset" to make --windowid work.
693 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
695#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
696 /* Need to set the size of the menubar after all the menus have been
697 * created. */
698 gui_mch_compute_menu_height((Widget)0);
699#endif
700
701 /*
702 * Actually open the GUI shell.
703 */
704 if (gui_mch_open() != FAIL)
705 {
706#ifdef FEAT_TITLE
707 maketitle();
708 resettitle();
709#endif
710 init_gui_options();
711#ifdef FEAT_ARABIC
712 /* Our GUI can't do bidi. */
713 p_tbidi = FALSE;
714#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000715#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 /* Give GTK+ a chance to put all widget's into place. */
717 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000718
719# ifdef FEAT_MENU
720 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
721 * It was still there to make F10 work. */
722 if (vim_strchr(p_go, GO_MENUS) == NULL)
723 {
724 --gui.starting;
725 gui_mch_enable_menu(FALSE);
726 ++gui.starting;
727 gui_mch_update();
728 }
729# endif
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 /* Now make sure the shell fits on the screen. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100732 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
760# ifdef FEAT_GUI_W32
761 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 Moolenaar0eda7ac2010-06-26 05:38:18 +0200881#if defined(FEAT_MBYTE) && !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 Moolenaar8ac44152017-11-09 18:33:29 +0100917 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 return ret;
921}
922
923#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200924# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
926 * Try setting 'guifontwide' to a font twice as wide as "name".
927 */
928 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100929set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 int i = 0;
932 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
933 char_u *wp = NULL;
934 char_u *p;
935 GuiFont font;
936
937 wp = wide_name;
938 for (p = name; *p != NUL; ++p)
939 {
940 *wp++ = *p;
941 if (*p == '-')
942 {
943 ++i;
944 if (i == 6) /* font type: change "--" to "-*-" */
945 {
946 if (p[1] == '-')
947 *wp++ = '*';
948 }
949 else if (i == 12) /* found the width */
950 {
951 ++p;
952 i = getdigits(&p);
953 if (i != 0)
954 {
955 /* Double the width specification. */
956 sprintf((char *)wp, "%d%s", i * 2, p);
957 font = gui_mch_get_font(wide_name, FALSE);
958 if (font != NOFONT)
959 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000960 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 gui.wide_font = font;
962 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000963 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 }
966 break;
967 }
968 }
969 }
970}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200971# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973/*
974 * Get the font for 'guifontwide'.
975 * Return FAIL for an invalid font name.
976 */
977 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100978gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
980 GuiFont font = NOFONT;
981 char_u font_name[FONTLEN];
982 char_u *p;
983
984 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
985 return OK; /* Will give an error message later. */
986
987 if (p_guifontwide != NULL && *p_guifontwide != NUL)
988 {
989 for (p = p_guifontwide; *p != NUL; )
990 {
991 /* Isolate one comma separated font name. */
992 (void)copy_option_part(&p, font_name, FONTLEN, ",");
993 font = gui_mch_get_font(font_name, FALSE);
994 if (font != NOFONT)
995 break;
996 }
997 if (font == NOFONT)
998 return FAIL;
999 }
1000
1001 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001002# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1004 if (font != NOFONT && gui.norm_font != NOFONT
1005 && pango_font_description_equal(font, gui.norm_font))
1006 {
1007 gui.wide_font = NOFONT;
1008 gui_mch_free_font(font);
1009 }
1010 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001013# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001014 gui_mch_wide_font_changed();
Bram Moolenaardb250522013-06-17 22:43:25 +02001015# else
1016 /*
1017 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1018 * support those fonts for 'guifontwide'.
1019 */
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 return OK;
1022}
1023#endif
1024
1025 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001026gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 gui.row = row;
1029 gui.col = col;
1030}
1031
1032/*
1033 * gui_check_pos - check if the cursor is on the screen.
1034 */
1035 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001036gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
1038 if (gui.row >= screen_Rows)
1039 gui.row = screen_Rows - 1;
1040 if (gui.col >= screen_Columns)
1041 gui.col = screen_Columns - 1;
1042 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1043 gui.cursor_is_valid = FALSE;
1044}
1045
1046/*
1047 * Redraw the cursor if necessary or when forced.
1048 * Careful: The contents of ScreenLines[] must match what is on the screen,
1049 * otherwise this goes wrong. May need to call out_flush() first.
1050 */
1051 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001052gui_update_cursor(
1053 int force, /* when TRUE, update even when not moved */
1054 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 int cur_width = 0;
1057 int cur_height = 0;
1058 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001059 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001061#ifdef FEAT_TERMINAL
1062 guicolor_T shape_fg = INVALCOLOR;
1063 guicolor_T shape_bg = INVALCOLOR;
1064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1066 int cattr; /* cursor attributes */
1067 int attr;
1068 attrentry_T *aep = NULL;
1069
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001070 /* Don't update the cursor when halfway busy scrolling or the screen size
1071 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1072 if (!can_update_cursor || screen_Columns != gui.num_cols
1073 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 return;
1075
1076 gui_check_pos();
1077 if (!gui.cursor_is_valid || force
1078 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1079 {
1080 gui_undraw_cursor();
1081 if (gui.row < 0)
1082 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001083#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1085 im_set_position(gui.row, gui.col);
1086#endif
1087 gui.cursor_row = gui.row;
1088 gui.cursor_col = gui.col;
1089
1090 /* Only write to the screen after ScreenLines[] has been initialized */
1091 if (!screen_cleared || ScreenLines == NULL)
1092 return;
1093
1094 /* Clear the selection if we are about to write over it */
1095 if (clear_selection)
1096 clip_may_clear_selection(gui.row, gui.row);
1097 /* Check that the cursor is inside the shell (resizing may have made
1098 * it invalid) */
1099 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1100 return;
1101
1102 gui.cursor_is_valid = TRUE;
1103
1104 /*
1105 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001106 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001108#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001109 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001110 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001112#endif
1113 shape = &shape_table[get_shape_idx(FALSE)];
1114 if (State & LANGMAP)
1115 id = shape->id_lm;
1116 else
1117 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 /* get the colors and attributes for the cursor. Default is inverted */
1120 cfg = INVALCOLOR;
1121 cbg = INVALCOLOR;
1122 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001123 gui_mch_set_blinking(shape->blinkwait,
1124 shape->blinkon,
1125 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001126 if (shape->blinkwait == 0 || shape->blinkon == 0
1127 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001128 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001129#ifdef FEAT_TERMINAL
1130 if (shape_bg != INVALCOLOR)
1131 {
1132 cattr = 0;
1133 cfg = shape_fg;
1134 cbg = shape_bg;
1135 }
1136 else
1137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (id > 0)
1139 {
1140 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001141#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 static int iid;
1144 guicolor_T fg, bg;
1145
Bram Moolenaarc236c162008-07-13 17:41:49 +00001146 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001147# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001148 preedit_get_status()
1149# else
1150 im_get_status()
1151# endif
1152 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 iid = syn_name2id((char_u *)"CursorIM");
1155 if (iid > 0)
1156 {
1157 syn_id2colors(iid, &fg, &bg);
1158 if (bg != INVALCOLOR)
1159 cbg = bg;
1160 if (fg != INVALCOLOR)
1161 cfg = fg;
1162 }
1163 }
1164 }
1165#endif
1166 }
1167
1168 /*
1169 * Get the attributes for the character under the cursor.
1170 * When no cursor color was given, use the character color.
1171 */
1172 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1173 if (attr > HL_ALL)
1174 aep = syn_gui_attr2entry(attr);
1175 if (aep != NULL)
1176 {
1177 attr = aep->ae_attr;
1178 if (cfg == INVALCOLOR)
1179 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1180 : aep->ae_u.gui.fg_color);
1181 if (cbg == INVALCOLOR)
1182 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1183 : aep->ae_u.gui.bg_color);
1184 }
1185 if (cfg == INVALCOLOR)
1186 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1187 if (cbg == INVALCOLOR)
1188 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1189
1190#ifdef FEAT_XIM
1191 if (aep != NULL)
1192 {
1193 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1194 : aep->ae_u.gui.bg_color);
1195 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1196 : aep->ae_u.gui.fg_color);
1197 if (xim_bg_color == INVALCOLOR)
1198 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1199 : gui.back_pixel;
1200 if (xim_fg_color == INVALCOLOR)
1201 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1202 : gui.norm_pixel;
1203 }
1204 else
1205 {
1206 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1207 : gui.back_pixel;
1208 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1209 : gui.norm_pixel;
1210 }
1211#endif
1212
1213 attr &= ~HL_INVERSE;
1214 if (cattr & HL_INVERSE)
1215 {
1216 cc = cbg;
1217 cbg = cfg;
1218 cfg = cc;
1219 }
1220 cattr &= ~HL_INVERSE;
1221
1222 /*
1223 * When we don't have window focus, draw a hollow cursor.
1224 */
1225 if (!gui.in_focus)
1226 {
1227 gui_mch_draw_hollow_cursor(cbg);
1228 return;
1229 }
1230
1231 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001232 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef FEAT_HANGULIN
1234 || composing_hangul
1235#endif
1236 )
1237 {
1238 /*
1239 * Draw the text character with the cursor colors. Use the
1240 * character attributes plus the cursor attributes.
1241 */
1242 gui.highlight_mask = (cattr | attr);
1243#ifdef FEAT_HANGULIN
1244 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001245 {
1246 char_u *comp_buf;
1247 int comp_len;
1248
1249 comp_buf = hangul_composing_buffer_get(&comp_len);
1250 if (comp_buf)
1251 {
1252 (void)gui_outstr_nowrap(comp_buf, comp_len,
1253 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1254 cfg, cbg, 0);
1255 vim_free(comp_buf);
1256 }
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 else
1259#endif
1260 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1261 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1262 }
1263 else
1264 {
1265#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1266 int col_off = FALSE;
1267#endif
1268 /*
1269 * First draw the partial cursor, then overwrite with the text
1270 * character, using a transparent background.
1271 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001272 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
1274 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001275 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 else
1278 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001279 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 cur_width = gui.char_width;
1281 }
1282#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001283 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1284 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
1286 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001287 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 cur_width += gui.char_width;
1289# ifdef FEAT_RIGHTLEFT
1290 if (CURSOR_BAR_RIGHT)
1291 {
1292 /* gui.col points to the left halve of the character but
1293 * the vertical line needs to be on the right halve.
1294 * A double-wide horizontal line is also drawn from the
1295 * right halve in gui_mch_draw_part_cursor(). */
1296 col_off = TRUE;
1297 ++gui.col;
1298 }
1299# endif
1300 }
1301#endif
1302 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1303#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1304 if (col_off)
1305 --gui.col;
1306#endif
1307
1308#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1309 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1310 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1311 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1312 (guicolor_T)0, (guicolor_T)0, 0);
1313#endif
1314 }
1315 gui.highlight_mask = old_hl_mask;
1316 }
1317}
1318
1319#if defined(FEAT_MENU) || defined(PROTO)
1320 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001321gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001323# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (gui.menu_is_active && gui.in_use)
1325 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1326# endif
1327}
1328#endif
1329
1330/*
1331 * Position the various GUI components (text area, menu). The vertical
1332 * scrollbars are NOT handled here. See gui_update_scrollbars().
1333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001335gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 int text_area_x;
1338 int text_area_y;
1339 int text_area_width;
1340 int text_area_height;
1341
1342 /* avoid that moving components around generates events */
1343 ++hold_gui_events;
1344
1345 text_area_x = 0;
1346 if (gui.which_scrollbars[SBAR_LEFT])
1347 text_area_x += gui.scrollbar_width;
1348
1349 text_area_y = 0;
1350#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1351 gui.menu_width = total_width;
1352 if (gui.menu_is_active)
1353 text_area_y += gui.menu_height;
1354#endif
1355#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1356 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1357 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1358#endif
1359
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001360# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001361 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001362 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001363 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001364#endif
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1367 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1368 {
1369# ifdef FEAT_GUI_ATHENA
1370 gui_mch_set_toolbar_pos(0, text_area_y,
1371 gui.menu_width, gui.toolbar_height);
1372# endif
1373 text_area_y += gui.toolbar_height;
1374 }
1375#endif
1376
1377 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1378 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1379
1380 gui_mch_set_text_area_pos(text_area_x,
1381 text_area_y,
1382 text_area_width,
1383 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001384#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 + xim_get_status_area_height()
1386#endif
1387 );
1388#ifdef FEAT_MENU
1389 gui_position_menu();
1390#endif
1391 if (gui.which_scrollbars[SBAR_BOTTOM])
1392 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1393 text_area_x,
1394 text_area_y + text_area_height,
1395 text_area_width,
1396 gui.scrollbar_height);
1397 gui.left_sbar_x = 0;
1398 gui.right_sbar_x = text_area_x + text_area_width;
1399
1400 --hold_gui_events;
1401}
1402
Bram Moolenaar02743632005-07-25 20:42:36 +00001403/*
1404 * Get the width of the widgets and decorations to the side of the text area.
1405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001407gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 int base_width;
1410
1411 base_width = 2 * gui.border_offset;
1412 if (gui.which_scrollbars[SBAR_LEFT])
1413 base_width += gui.scrollbar_width;
1414 if (gui.which_scrollbars[SBAR_RIGHT])
1415 base_width += gui.scrollbar_width;
1416 return base_width;
1417}
1418
Bram Moolenaar02743632005-07-25 20:42:36 +00001419/*
1420 * Get the height of the widgets and decorations above and below the text area.
1421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001423gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 int base_height;
1426
1427 base_height = 2 * gui.border_offset;
1428 if (gui.which_scrollbars[SBAR_BOTTOM])
1429 base_height += gui.scrollbar_height;
1430#ifdef FEAT_GUI_GTK
1431 /* We can't take the sizes properly into account until anything is
1432 * realized. Therefore we recalculate all the values here just before
1433 * setting the size. (--mdcki) */
1434#else
1435# ifdef FEAT_MENU
1436 if (gui.menu_is_active)
1437 base_height += gui.menu_height;
1438# endif
1439# ifdef FEAT_TOOLBAR
1440 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1441# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1442 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1443# else
1444 base_height += gui.toolbar_height;
1445# endif
1446# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001447# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1448 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001449 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001450 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452# ifdef FEAT_FOOTER
1453 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1454 base_height += gui.footer_height;
1455# endif
1456# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1457 base_height += gui_mch_text_area_extra_height();
1458# endif
1459#endif
1460 return base_height;
1461}
1462
1463/*
1464 * Should be called after the GUI shell has been resized. Its arguments are
1465 * the new width and height of the shell in pixels.
1466 */
1467 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001468gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 static int busy = FALSE;
1471
1472 if (!gui.shell_created) /* ignore when still initializing */
1473 return;
1474
1475 /*
1476 * Can't resize the screen while it is being redrawn. Remember the new
1477 * size and handle it later.
1478 */
1479 if (updating_screen || busy)
1480 {
1481 new_pixel_width = pixel_width;
1482 new_pixel_height = pixel_height;
1483 return;
1484 }
1485
1486again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001487 new_pixel_width = 0;
1488 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 busy = TRUE;
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 /* Flush pending output before redrawing */
1492 out_flush();
1493
1494 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001495 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 /*
1501 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1502 * at the last line here (why does it have to be one row too low?).
1503 */
1504 if (State == ASKMORE || State == CONFIRM)
1505 gui.row = gui.num_rows;
1506
1507 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1508 * the safe side. */
1509 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1510 || gui.num_rows != Rows || gui.num_cols != Columns)
1511 shell_resized();
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 gui_update_scrollbars(TRUE);
1514 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001515#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 xim_set_status_area();
1517#endif
1518
1519 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001520
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001521 /* We may have been called again while redrawing the screen.
1522 * Need to do it all again with the latest size then. But only if the size
1523 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (new_pixel_height)
1525 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001526 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1527 {
1528 new_pixel_width = 0;
1529 new_pixel_height = 0;
1530 }
1531 else
1532 {
1533 pixel_width = new_pixel_width;
1534 pixel_height = new_pixel_height;
1535 goto again;
1536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538}
1539
1540/*
1541 * Check if gui_resize_shell() must be called.
1542 */
1543 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001544gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 /* careful: gui_resize_shell() may postpone the resize again if we
1548 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001549 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550}
1551
1552 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001553gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 Rows = gui.num_rows;
1556 Columns = gui.num_cols;
1557 return OK;
1558}
1559
1560/*
1561 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001562 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1563 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001564 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1565 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001568gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001569 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001570 int fit_to_display,
1571 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 int base_width;
1574 int base_height;
1575 int width;
1576 int height;
1577 int min_width;
1578 int min_height;
1579 int screen_w;
1580 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001581#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001582 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001583 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001584#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001585 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 if (!gui.shell_created)
1588 return;
1589
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001590#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* If not setting to a user specified size and maximized, calculate the
1592 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001593 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1594 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 gui_mch_newfont();
1597 return;
1598 }
1599#endif
1600
1601 base_width = gui_get_base_width();
1602 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001603 if (fit_to_display)
1604 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001605 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607#ifdef USE_SUN_WORKSHOP
1608 if (!mustset && usingSunWorkShop
1609 && workshop_get_width_height(&width, &height))
1610 {
1611 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1612 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1613 }
1614 else
1615#endif
1616 {
1617 width = Columns * gui.char_width + base_width;
1618 height = Rows * gui.char_height + base_height;
1619 }
1620
1621 if (fit_to_display)
1622 {
1623 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001624 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 Columns = (screen_w - base_width) / gui.char_width;
1627 if (Columns < MIN_COLUMNS)
1628 Columns = MIN_COLUMNS;
1629 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001630#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001631 ++did_adjust;
1632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001634 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 Rows = (screen_h - base_height) / gui.char_height;
1637 check_shellsize();
1638 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001639#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001640 ++did_adjust;
1641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001643#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001644 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1645 && height + gui.char_height >= screen_h))
1646 /* don't unmaximize if at maximum size */
1647 un_maximize = FALSE;
1648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001650 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 gui.num_cols = Columns;
1652 gui.num_rows = Rows;
1653
1654 min_width = base_width + MIN_COLUMNS * gui.char_width;
1655 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001656 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001657
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001658#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001659 if (un_maximize)
1660 {
1661 /* If the window size is smaller than the screen unmaximize the
1662 * window, otherwise resizing won't work. */
1663 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1664 if ((width + gui.char_width < screen_w
1665 || height + gui.char_height * 2 < screen_h)
1666 && gui_mch_maximized())
1667 gui_mch_unmaximize();
1668 }
1669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001672 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001674 if (fit_to_display && x >= 0 && y >= 0)
1675 {
1676 /* Some window managers put the Vim window left of/above the screen.
1677 * Only change the position if it wasn't already negative before
1678 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 gui_mch_update();
1680 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1681 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1682 }
1683
1684 gui_position_components(width);
1685 gui_update_scrollbars(TRUE);
1686 gui_reset_scroll_region();
1687}
1688
1689/*
1690 * Called when Rows and/or Columns has changed.
1691 */
1692 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001693gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 gui_reset_scroll_region();
1696}
1697
1698/*
1699 * Make scroll region cover whole screen.
1700 */
1701 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001702gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 gui.scroll_region_top = 0;
1705 gui.scroll_region_bot = gui.num_rows - 1;
1706 gui.scroll_region_left = 0;
1707 gui.scroll_region_right = gui.num_cols - 1;
1708}
1709
1710 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001711gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 if (mask > HL_ALL) /* highlight code */
1714 gui.highlight_mask = mask;
1715 else /* mask */
1716 gui.highlight_mask |= mask;
1717}
1718
1719 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001720gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 if (mask > HL_ALL) /* highlight code */
1723 gui.highlight_mask = HL_NORMAL;
1724 else /* mask */
1725 gui.highlight_mask &= ~mask;
1726}
1727
1728/*
1729 * Clear a rectangular region of the screen from text pos (row1, col1) to
1730 * (row2, col2) inclusive.
1731 */
1732 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001733gui_clear_block(
1734 int row1,
1735 int col1,
1736 int row2,
1737 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 /* Clear the selection if we are about to write over it */
1740 clip_may_clear_selection(row1, row2);
1741
1742 gui_mch_clear_block(row1, col1, row2, col2);
1743
1744 /* Invalidate cursor if it was in this block */
1745 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1746 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1747 gui.cursor_is_valid = FALSE;
1748}
1749
1750/*
1751 * Write code to update the cursor later. This avoids the need to flush the
1752 * output buffer before calling gui_update_cursor().
1753 */
1754 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001755gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001757 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
1760 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001761gui_write(
1762 char_u *s,
1763 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 char_u *p;
1766 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 int force_scrollbar = FALSE;
1769 static win_T *old_curwin = NULL;
1770
1771/* #define DEBUG_GUI_WRITE */
1772#ifdef DEBUG_GUI_WRITE
1773 {
1774 int i;
1775 char_u *str;
1776
1777 printf("gui_write(%d):\n ", len);
1778 for (i = 0; i < len; i++)
1779 if (s[i] == ESC)
1780 {
1781 if (i != 0)
1782 printf("\n ");
1783 printf("<ESC>");
1784 }
1785 else
1786 {
1787 str = transchar_byte(s[i]);
1788 if (str[0] && str[1])
1789 printf("<%s>", (char *)str);
1790 else
1791 printf("%s", (char *)str);
1792 }
1793 printf("\n");
1794 }
1795#endif
1796 while (len)
1797 {
1798 if (s[0] == ESC && s[1] == '|')
1799 {
1800 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001801 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 arg1 = getdigits(&p);
1804 if (p > s + len)
1805 break;
1806 if (*p == ';')
1807 {
1808 ++p;
1809 arg2 = getdigits(&p);
1810 if (p > s + len)
1811 break;
1812 }
1813 }
1814 switch (*p)
1815 {
1816 case 'C': /* Clear screen */
1817 clip_scroll_selection(9999);
1818 gui_mch_clear_all();
1819 gui.cursor_is_valid = FALSE;
1820 force_scrollbar = TRUE;
1821 break;
1822 case 'M': /* Move cursor */
1823 gui_set_cursor(arg1, arg2);
1824 break;
1825 case 's': /* force cursor (shape) update */
1826 force_cursor = TRUE;
1827 break;
1828 case 'R': /* Set scroll region */
1829 if (arg1 < arg2)
1830 {
1831 gui.scroll_region_top = arg1;
1832 gui.scroll_region_bot = arg2;
1833 }
1834 else
1835 {
1836 gui.scroll_region_top = arg2;
1837 gui.scroll_region_bot = arg1;
1838 }
1839 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 case 'V': /* Set vertical scroll region */
1841 if (arg1 < arg2)
1842 {
1843 gui.scroll_region_left = arg1;
1844 gui.scroll_region_right = arg2;
1845 }
1846 else
1847 {
1848 gui.scroll_region_left = arg2;
1849 gui.scroll_region_right = arg1;
1850 }
1851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 case 'd': /* Delete line */
1853 gui_delete_lines(gui.row, 1);
1854 break;
1855 case 'D': /* Delete lines */
1856 gui_delete_lines(gui.row, arg1);
1857 break;
1858 case 'i': /* Insert line */
1859 gui_insert_lines(gui.row, 1);
1860 break;
1861 case 'I': /* Insert lines */
1862 gui_insert_lines(gui.row, arg1);
1863 break;
1864 case '$': /* Clear to end-of-line */
1865 gui_clear_block(gui.row, gui.col, gui.row,
1866 (int)Columns - 1);
1867 break;
1868 case 'h': /* Turn on highlighting */
1869 gui_start_highlight(arg1);
1870 break;
1871 case 'H': /* Turn off highlighting */
1872 gui_stop_highlight(arg1);
1873 break;
1874 case 'f': /* flash the window (visual bell) */
1875 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1876 break;
1877 default:
1878 p = s + 1; /* Skip the ESC */
1879 break;
1880 }
1881 len -= (int)(++p - s);
1882 s = p;
1883 }
1884 else if (
1885#ifdef EBCDIC
1886 CtrlChar(s[0]) != 0 /* Ctrl character */
1887#else
1888 s[0] < 0x20 /* Ctrl character */
1889#endif
1890#ifdef FEAT_SIGN_ICONS
1891 && s[0] != SIGN_BYTE
1892# ifdef FEAT_NETBEANS_INTG
1893 && s[0] != MULTISIGN_BYTE
1894# endif
1895#endif
1896 )
1897 {
1898 if (s[0] == '\n') /* NL */
1899 {
1900 gui.col = 0;
1901 if (gui.row < gui.scroll_region_bot)
1902 gui.row++;
1903 else
1904 gui_delete_lines(gui.scroll_region_top, 1);
1905 }
1906 else if (s[0] == '\r') /* CR */
1907 {
1908 gui.col = 0;
1909 }
1910 else if (s[0] == '\b') /* Backspace */
1911 {
1912 if (gui.col)
1913 --gui.col;
1914 }
1915 else if (s[0] == Ctrl_L) /* cursor-right */
1916 {
1917 ++gui.col;
1918 }
1919 else if (s[0] == Ctrl_G) /* Beep */
1920 {
1921 gui_mch_beep();
1922 }
1923 /* Other Ctrl character: shouldn't happen! */
1924
1925 --len; /* Skip this char */
1926 ++s;
1927 }
1928 else
1929 {
1930 p = s;
1931 while (len > 0 && (
1932#ifdef EBCDIC
1933 CtrlChar(*p) == 0
1934#else
1935 *p >= 0x20
1936#endif
1937#ifdef FEAT_SIGN_ICONS
1938 || *p == SIGN_BYTE
1939# ifdef FEAT_NETBEANS_INTG
1940 || *p == MULTISIGN_BYTE
1941# endif
1942#endif
1943 ))
1944 {
1945 len--;
1946 p++;
1947 }
1948 gui_outstr(s, (int)(p - s));
1949 s = p;
1950 }
1951 }
1952
1953 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1954 * set). */
1955 if (force_cursor)
1956 gui_update_cursor(TRUE, TRUE);
1957
1958 /* When switching to another window the dragging must have stopped.
1959 * Required for GTK, dragged_sb isn't reset. */
1960 if (old_curwin != curwin)
1961 gui.dragged_sb = SBAR_NONE;
1962
1963 /* Update the scrollbars after clearing the screen or when switched
1964 * to another window.
1965 * Update the horizontal scrollbar always, it's difficult to check all
1966 * situations where it might change. */
1967 if (force_scrollbar || old_curwin != curwin)
1968 gui_update_scrollbars(force_scrollbar);
1969 else
1970 gui_update_horiz_scrollbar(FALSE);
1971 old_curwin = curwin;
1972
1973 /*
1974 * We need to make sure this is cleared since Athena doesn't tell us when
1975 * he is done dragging. Do the same for GTK.
1976 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001977#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 gui.dragged_sb = SBAR_NONE;
1979#endif
1980
Bram Moolenaara338adc2018-01-31 20:51:47 +01001981 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982}
1983
1984/*
1985 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1986 * produces wrong results. Call gui_dont_update_cursor() before that code and
1987 * gui_can_update_cursor() afterwards.
1988 */
1989 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001990gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
1992 if (gui.in_use)
1993 {
1994 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001995 if (undraw)
1996 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 can_update_cursor = FALSE;
1998 }
1999}
2000
2001 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002002gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 can_update_cursor = TRUE;
2005 /* No need to update the cursor right now, there is always more output
2006 * after scrolling. */
2007}
2008
Bram Moolenaara338adc2018-01-31 20:51:47 +01002009/*
2010 * Disable issuing gui_mch_flush().
2011 */
2012 void
2013gui_disable_flush(void)
2014{
2015 ++disable_flush;
2016}
2017
2018/*
2019 * Enable issuing gui_mch_flush().
2020 */
2021 void
2022gui_enable_flush(void)
2023{
2024 --disable_flush;
2025}
2026
2027/*
2028 * Issue gui_mch_flush() if it is not disabled.
2029 */
2030 void
2031gui_may_flush(void)
2032{
2033 if (disable_flush == 0)
2034 gui_mch_flush();
2035}
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002038gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 int this_len;
2041#ifdef FEAT_MBYTE
2042 int cells;
2043#endif
2044
2045 if (len == 0)
2046 return;
2047
2048 if (len < 0)
2049 len = (int)STRLEN(s);
2050
2051 while (len > 0)
2052 {
2053#ifdef FEAT_MBYTE
2054 if (has_mbyte)
2055 {
2056 /* Find out how many chars fit in the current line. */
2057 cells = 0;
2058 for (this_len = 0; this_len < len; )
2059 {
2060 cells += (*mb_ptr2cells)(s + this_len);
2061 if (gui.col + cells > Columns)
2062 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002063 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 if (this_len > len)
2066 this_len = len; /* don't include following composing char */
2067 }
2068 else
2069#endif
2070 if (gui.col + len > Columns)
2071 this_len = Columns - gui.col;
2072 else
2073 this_len = len;
2074
2075 (void)gui_outstr_nowrap(s, this_len,
2076 0, (guicolor_T)0, (guicolor_T)0, 0);
2077 s += this_len;
2078 len -= this_len;
2079#ifdef FEAT_MBYTE
2080 /* fill up for a double-width char that doesn't fit. */
2081 if (len > 0 && gui.col < Columns)
2082 (void)gui_outstr_nowrap((char_u *)" ", 1,
2083 0, (guicolor_T)0, (guicolor_T)0, 0);
2084#endif
2085 /* The cursor may wrap to the next line. */
2086 if (gui.col >= Columns)
2087 {
2088 gui.col = 0;
2089 gui.row++;
2090 }
2091 }
2092}
2093
2094/*
2095 * Output one character (may be one or two display cells).
2096 * Caller must check for valid "off".
2097 * Returns FAIL or OK, just like gui_outstr_nowrap().
2098 */
2099 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002100gui_screenchar(
2101 int off, /* Offset from start of screen */
2102 int flags,
2103 guicolor_T fg, /* colors for cursor */
2104 guicolor_T bg, /* colors for cursor */
2105 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
2107#ifdef FEAT_MBYTE
2108 char_u buf[MB_MAXBYTES + 1];
2109
2110 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2111 if (enc_utf8 && ScreenLines[off] == 0)
2112 return OK;
2113
2114 if (enc_utf8 && ScreenLinesUC[off] != 0)
2115 /* Draw UTF-8 multi-byte character. */
2116 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2117 flags, fg, bg, back);
2118
2119 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2120 {
2121 buf[0] = ScreenLines[off];
2122 buf[1] = ScreenLines2[off];
2123 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2124 }
2125
2126 /* Draw non-multi-byte character or DBCS character. */
2127 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002128 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 flags, fg, bg, back);
2130#else
2131 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2132#endif
2133}
2134
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002135#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136/*
2137 * Output the string at the given screen position. This is used in place
2138 * of gui_screenchar() where possible because Pango needs as much context
2139 * as possible to work nicely. It's a lot faster as well.
2140 */
2141 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002142gui_screenstr(
2143 int off, /* Offset from start of screen */
2144 int len, /* string length in screen cells */
2145 int flags,
2146 guicolor_T fg, /* colors for cursor */
2147 guicolor_T bg, /* colors for cursor */
2148 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 char_u *buf;
2151 int outlen = 0;
2152 int i;
2153 int retval;
2154
2155 if (len <= 0) /* "cannot happen"? */
2156 return OK;
2157
2158 if (enc_utf8)
2159 {
2160 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2161 if (buf == NULL)
2162 return OK; /* not much we could do here... */
2163
2164 for (i = off; i < off + len; ++i)
2165 {
2166 if (ScreenLines[i] == 0)
2167 continue; /* skip second half of double-width char */
2168
2169 if (ScreenLinesUC[i] == 0)
2170 buf[outlen++] = ScreenLines[i];
2171 else
2172 outlen += utfc_char2bytes(i, buf + outlen);
2173 }
2174
2175 buf[outlen] = NUL; /* only to aid debugging */
2176 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2177 vim_free(buf);
2178
2179 return retval;
2180 }
2181 else if (enc_dbcs == DBCS_JPNU)
2182 {
2183 buf = alloc((unsigned)(len * 2 + 1));
2184 if (buf == NULL)
2185 return OK; /* not much we could do here... */
2186
2187 for (i = off; i < off + len; ++i)
2188 {
2189 buf[outlen++] = ScreenLines[i];
2190
2191 /* handle double-byte single-width char */
2192 if (ScreenLines[i] == 0x8e)
2193 buf[outlen++] = ScreenLines2[i];
2194 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2195 buf[outlen++] = ScreenLines[++i];
2196 }
2197
2198 buf[outlen] = NUL; /* only to aid debugging */
2199 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2200 vim_free(buf);
2201
2202 return retval;
2203 }
2204 else
2205 {
2206 return gui_outstr_nowrap(&ScreenLines[off], len,
2207 flags, fg, bg, back);
2208 }
2209}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002210#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212/*
2213 * Output the given string at the current cursor position. If the string is
2214 * too long to fit on the line, then it is truncated.
2215 * "flags":
2216 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2217 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002218 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * background.
2220 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2221 * it.
2222 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2223 * FAIL (the caller should start drawing "back" chars back).
2224 */
2225 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002226gui_outstr_nowrap(
2227 char_u *s,
2228 int len,
2229 int flags,
2230 guicolor_T fg, /* colors for cursor */
2231 guicolor_T bg, /* colors for cursor */
2232 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 long_u highlight_mask;
2235 long_u hl_mask_todo;
2236 guicolor_T fg_color;
2237 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002238 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002239#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002241# ifdef FEAT_MBYTE
2242 GuiFont wide_font = NOFONT;
2243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_XFONTSET
2245 GuiFontset fontset = NOFONTSET;
2246# endif
2247#endif
2248 attrentry_T *aep = NULL;
2249 int draw_flags;
2250 int col = gui.col;
2251#ifdef FEAT_SIGN_ICONS
2252 int draw_sign = FALSE;
2253# ifdef FEAT_NETBEANS_INTG
2254 int multi_sign = FALSE;
2255# endif
2256#endif
2257
2258 if (len < 0)
2259 len = (int)STRLEN(s);
2260 if (len == 0)
2261 return OK;
2262
2263#ifdef FEAT_SIGN_ICONS
2264 if (*s == SIGN_BYTE
2265# ifdef FEAT_NETBEANS_INTG
2266 || *s == MULTISIGN_BYTE
2267# endif
2268 )
2269 {
2270# ifdef FEAT_NETBEANS_INTG
2271 if (*s == MULTISIGN_BYTE)
2272 multi_sign = TRUE;
2273# endif
2274 /* draw spaces instead */
2275 s = (char_u *)" ";
2276 if (len == 1 && col > 0)
2277 --col;
2278 len = 2;
2279 draw_sign = TRUE;
2280 highlight_mask = 0;
2281 }
2282 else
2283#endif
2284 if (gui.highlight_mask > HL_ALL)
2285 {
2286 aep = syn_gui_attr2entry(gui.highlight_mask);
2287 if (aep == NULL) /* highlighting not set */
2288 highlight_mask = 0;
2289 else
2290 highlight_mask = aep->ae_attr;
2291 }
2292 else
2293 highlight_mask = gui.highlight_mask;
2294 hl_mask_todo = highlight_mask;
2295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 /* Set the font */
2298 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2299 font = aep->ae_u.gui.font;
2300# ifdef FEAT_XFONTSET
2301 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2302 fontset = aep->ae_u.gui.fontset;
2303# endif
2304 else
2305 {
2306# ifdef FEAT_XFONTSET
2307 if (gui.fontset != NOFONTSET)
2308 fontset = gui.fontset;
2309 else
2310# endif
2311 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2312 {
2313 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2314 {
2315 font = gui.boldital_font;
2316 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2317 }
2318 else if (gui.bold_font != NOFONT)
2319 {
2320 font = gui.bold_font;
2321 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2322 }
2323 else
2324 font = gui.norm_font;
2325 }
2326 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2327 {
2328 font = gui.ital_font;
2329 hl_mask_todo &= ~HL_ITALIC;
2330 }
2331 else
2332 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002333
2334# ifdef FEAT_MBYTE
2335 /*
2336 * Choose correct wide_font by font. wide_font should be set with font
2337 * at same time in above block. But it will make many "ifdef" nasty
2338 * blocks. So we do it here.
2339 */
2340 if (font == gui.boldital_font && gui.wide_boldital_font)
2341 wide_font = gui.wide_boldital_font;
2342 else if (font == gui.bold_font && gui.wide_bold_font)
2343 wide_font = gui.wide_bold_font;
2344 else if (font == gui.ital_font && gui.wide_ital_font)
2345 wide_font = gui.wide_ital_font;
2346 else if (font == gui.norm_font && gui.wide_font)
2347 wide_font = gui.wide_font;
2348# endif
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351# ifdef FEAT_XFONTSET
2352 if (fontset != NOFONTSET)
2353 gui_mch_set_fontset(fontset);
2354 else
2355# endif
2356 gui_mch_set_font(font);
2357#endif
2358
2359 draw_flags = 0;
2360
2361 /* Set the color */
2362 bg_color = gui.back_pixel;
2363 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2364 {
2365 draw_flags |= DRAW_CURSOR;
2366 fg_color = fg;
2367 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002368 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 else if (aep != NULL)
2371 {
2372 fg_color = aep->ae_u.gui.fg_color;
2373 if (fg_color == INVALCOLOR)
2374 fg_color = gui.norm_pixel;
2375 bg_color = aep->ae_u.gui.bg_color;
2376 if (bg_color == INVALCOLOR)
2377 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002378 sp_color = aep->ae_u.gui.sp_color;
2379 if (sp_color == INVALCOLOR)
2380 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002385 sp_color = fg_color;
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2389 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002390#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 gui_mch_set_colors(bg_color, fg_color);
2392#else
2393 gui_mch_set_fg_color(bg_color);
2394 gui_mch_set_bg_color(fg_color);
2395#endif
2396 }
2397 else
2398 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002399#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 gui_mch_set_colors(fg_color, bg_color);
2401#else
2402 gui_mch_set_fg_color(fg_color);
2403 gui_mch_set_bg_color(bg_color);
2404#endif
2405 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002406 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 /* Clear the selection if we are about to write over it */
2409 if (!(flags & GUI_MON_NOCLEAR))
2410 clip_may_clear_selection(gui.row, gui.row);
2411
2412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 /* If there's no bold font, then fake it */
2414 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2415 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
2417 /*
2418 * When drawing bold or italic characters the spill-over from the left
2419 * neighbor may be destroyed. Let the caller backup to start redrawing
2420 * just after a blank.
2421 */
2422 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2423 return FAIL;
2424
Bram Moolenaare60acc12011-05-10 16:41:25 +02002425#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /* If there's no italic font, then fake it.
2427 * For GTK2, we don't need a different font for italic style. */
2428 if (hl_mask_todo & HL_ITALIC)
2429 draw_flags |= DRAW_ITALIC;
2430
2431 /* Do we underline the text? */
2432 if (hl_mask_todo & HL_UNDERLINE)
2433 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#else
2436 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002437 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 draw_flags |= DRAW_UNDERL;
2439#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002440 /* Do we undercurl the text? */
2441 if (hl_mask_todo & HL_UNDERCURL)
2442 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002444 /* Do we strikethrough the text? */
2445 if (hl_mask_todo & HL_STRIKETHROUGH)
2446 draw_flags |= DRAW_STRIKE;
2447
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002448 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (flags & GUI_MON_TRS_CURSOR)
2450 draw_flags |= DRAW_TRANSP;
2451
2452 /*
2453 * Draw the text.
2454 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002455#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /* The value returned is the length in display cells */
2457 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2458#else
2459# ifdef FEAT_MBYTE
2460 if (enc_utf8)
2461 {
2462 int start; /* index of bytes to be drawn */
2463 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002464 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 int cn; /* cellwidth of current char */
2466 int i; /* index of current char */
2467 int c; /* current char value */
2468 int cl; /* byte length of current char */
2469 int comping; /* current char is composing */
2470 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002471 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002472 int prev_wide = FALSE;
2473 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002474# ifdef WIN3264
2475 int sep_comp = FALSE; /* Don't separate composing chars. */
2476# else
2477 int sep_comp = TRUE; /* Separate composing chars. */
2478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /* Break the string at a composing character, it has to be drawn on
2481 * top of the previous character. */
2482 start = 0;
2483 cells = 0;
2484 for (i = 0; i < len; i += cl)
2485 {
2486 c = utf_ptr2char(s + i);
2487 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 comping = utf_iscomposing(c);
2489 if (!comping) /* count cells from non-composing chars */
2490 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002491 if (!comping || sep_comp)
2492 {
2493 if (cn > 1
2494# ifdef FEAT_XFONTSET
2495 && fontset == NOFONTSET
2496# endif
2497 && wide_font != NOFONT)
2498 curr_wide = TRUE;
2499 else
2500 curr_wide = FALSE;
2501 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002502 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (cl == 0) /* hit end of string */
2504 len = i + cl; /* len must be wrong "cannot happen" */
2505
Bram Moolenaar45933962013-01-23 17:43:57 +01002506 wide_changed = curr_wide != prev_wide;
2507
2508 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002510 if (i + cl >= len || (comping && sep_comp && i > start)
2511 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002512# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 || (cn > 1
2514# ifdef FEAT_XFONTSET
2515 /* No fontset: At least draw char after wide char at
2516 * right position. */
2517 && fontset == NOFONTSET
2518# endif
2519 )
2520# endif
2521 )
2522 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002523 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 thislen = i - start;
2525 else
2526 thislen = i - start + cl;
2527 if (thislen > 0)
2528 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002529 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002530 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2532 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002533 if (prev_wide)
2534 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 start += thislen;
2536 }
2537 scol += cells;
2538 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002539 /* Adjust to not draw a character which width is changed
2540 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002541 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002543 scol -= cn;
2544 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002547# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002548 /* No fontset: draw a space to fill the gap after a wide char
2549 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2551# ifdef FEAT_XFONTSET
2552 && fontset == NOFONTSET
2553# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002554 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2556 1, draw_flags);
2557# endif
2558 }
2559 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002560 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002562# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002563 /* Carbon ATSUI autodraws composing char over previous char */
2564 gui_mch_draw_string(gui.row, scol, s + i, cl,
2565 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002566# else
2567 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2568 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 start = i + cl;
2571 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002572 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 /* The stuff below assumes "len" is the length in screen columns. */
2575 len = scol - col;
2576 }
2577 else
2578# endif
2579 {
2580 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2581# ifdef FEAT_MBYTE
2582 if (enc_dbcs == DBCS_JPNU)
2583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 /* Get the length in display cells, this can be different from the
2585 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002586 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588# endif
2589 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002590#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2593 gui.col = col + len;
2594
2595 /* May need to invert it when it's part of the selection. */
2596 if (flags & GUI_MON_NOCLEAR)
2597 clip_may_redraw_selection(gui.row, col, len);
2598
2599 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2600 {
2601 /* Invalidate the old physical cursor position if we wrote over it */
2602 if (gui.cursor_row == gui.row
2603 && gui.cursor_col >= col
2604 && gui.cursor_col < col + len)
2605 gui.cursor_is_valid = FALSE;
2606 }
2607
2608#ifdef FEAT_SIGN_ICONS
2609 if (draw_sign)
2610 /* Draw the sign on top of the spaces. */
2611 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002612# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002613 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (multi_sign)
2615 netbeans_draw_multisign_indicator(gui.row);
2616# endif
2617#endif
2618
2619 return OK;
2620}
2621
2622/*
2623 * Un-draw the cursor. Actually this just redraws the character at the given
2624 * position. The character just before it too, for when it was in bold.
2625 */
2626 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002627gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 if (gui.cursor_is_valid)
2630 {
2631#ifdef FEAT_HANGULIN
2632 if (composing_hangul
2633 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002634 {
2635 char_u *comp_buf;
2636 int comp_len;
2637
2638 comp_buf = hangul_composing_buffer_get(&comp_len);
2639 if (comp_buf)
2640 {
2641 (void)gui_outstr_nowrap(comp_buf, comp_len,
2642 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2643 gui.norm_pixel, gui.back_pixel, 0);
2644 vim_free(comp_buf);
2645 }
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else
2648 {
2649#endif
2650 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2651 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2652 && gui.cursor_col > 0)
2653 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2654 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2655#ifdef FEAT_HANGULIN
2656 if (composing_hangul)
2657 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2658 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2659 }
2660#endif
2661 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2662 * here in case it wasn't needed to undraw it. */
2663 gui.cursor_is_valid = FALSE;
2664 }
2665}
2666
2667 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002668gui_redraw(
2669 int x,
2670 int y,
2671 int w,
2672 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 int row1, col1, row2, col2;
2675
2676 row1 = Y_2_ROW(y);
2677 col1 = X_2_COL(x);
2678 row2 = Y_2_ROW(y + h - 1);
2679 col2 = X_2_COL(x + w - 1);
2680
2681 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2682
2683 /*
2684 * We may need to redraw the cursor, but don't take it upon us to change
2685 * its location after a scroll.
2686 * (maybe be more strict even and test col too?)
2687 * These things may be outside the update/clipping region and reality may
2688 * not reflect Vims internal ideas if these operations are clipped away.
2689 */
2690 if (gui.row == gui.cursor_row)
2691 gui_update_cursor(TRUE, TRUE);
2692}
2693
2694/*
2695 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2696 * from col1 to col2 (inclusive).
2697 * Return TRUE when the character before the first drawn character has
2698 * different attributes (may have to be redrawn too).
2699 */
2700 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002701gui_redraw_block(
2702 int row1,
2703 int col1,
2704 int row2,
2705 int col2,
2706 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 int old_row, old_col;
2709 long_u old_hl_mask;
2710 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002711 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 int idx, len;
2713 int back, nback;
2714 int retval = FALSE;
2715#ifdef FEAT_MBYTE
2716 int orig_col1, orig_col2;
2717#endif
2718
2719 /* Don't try to update when ScreenLines is not valid */
2720 if (!screen_cleared || ScreenLines == NULL)
2721 return retval;
2722
2723 /* Don't try to draw outside the shell! */
2724 /* Check everything, strange values may be caused by a big border width */
2725 col1 = check_col(col1);
2726 col2 = check_col(col2);
2727 row1 = check_row(row1);
2728 row2 = check_row(row2);
2729
2730 /* Remember where our cursor was */
2731 old_row = gui.row;
2732 old_col = gui.col;
2733 old_hl_mask = gui.highlight_mask;
2734#ifdef FEAT_MBYTE
2735 orig_col1 = col1;
2736 orig_col2 = col2;
2737#endif
2738
2739 for (gui.row = row1; gui.row <= row2; gui.row++)
2740 {
2741#ifdef FEAT_MBYTE
2742 /* When only half of a double-wide character is in the block, include
2743 * the other half. */
2744 col1 = orig_col1;
2745 col2 = orig_col2;
2746 off = LineOffset[gui.row];
2747 if (enc_dbcs != 0)
2748 {
2749 if (col1 > 0)
2750 col1 -= dbcs_screen_head_off(ScreenLines + off,
2751 ScreenLines + off + col1);
2752 col2 += dbcs_screen_tail_off(ScreenLines + off,
2753 ScreenLines + off + col2);
2754 }
2755 else if (enc_utf8)
2756 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002757 if (ScreenLines[off + col1] == 0)
2758 {
2759 if (col1 > 0)
2760 --col1;
2761 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002762 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002763 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002764 // Make this IEMSGN when it no longer breaks Travis CI.
2765 vim_snprintf((char *)IObuff, IOSIZE,
2766 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2767 (long)gui.row);
2768 msg(IObuff);
2769 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002770 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002771# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2773 ++col2;
2774# endif
2775 }
2776#endif
2777 gui.col = col1;
2778 off = LineOffset[gui.row] + gui.col;
2779 len = col2 - col1 + 1;
2780
2781 /* Find how many chars back this highlighting starts, or where a space
2782 * is. Needed for when the bold trick is used */
2783 for (back = 0; back < col1; ++back)
2784 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2785 || ScreenLines[off - 1 - back] == ' ')
2786 break;
2787 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2788 && ScreenLines[off - 1] != ' ');
2789
2790 /* Break it up in strings of characters with the same attributes. */
2791 /* Print UTF-8 characters individually. */
2792 while (len > 0)
2793 {
2794 first_attr = ScreenAttrs[off];
2795 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002796#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (enc_utf8 && ScreenLinesUC[off] != 0)
2798 {
2799 /* output multi-byte character separately */
2800 nback = gui_screenchar(off, flags,
2801 (guicolor_T)0, (guicolor_T)0, back);
2802 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2803 idx = 2;
2804 else
2805 idx = 1;
2806 }
2807 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2808 {
2809 /* output double-byte, single-width character separately */
2810 nback = gui_screenchar(off, flags,
2811 (guicolor_T)0, (guicolor_T)0, back);
2812 idx = 1;
2813 }
2814 else
2815#endif
2816 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002817#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 for (idx = 0; idx < len; ++idx)
2819 {
2820 if (enc_utf8 && ScreenLines[off + idx] == 0)
2821 continue; /* skip second half of double-width char */
2822 if (ScreenAttrs[off + idx] != first_attr)
2823 break;
2824 }
2825 /* gui_screenstr() takes care of multibyte chars */
2826 nback = gui_screenstr(off, idx, flags,
2827 (guicolor_T)0, (guicolor_T)0, back);
2828#else
2829 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2830 idx++)
2831 {
2832# ifdef FEAT_MBYTE
2833 /* Stop at a multi-byte Unicode character. */
2834 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2835 break;
2836 if (enc_dbcs == DBCS_JPNU)
2837 {
2838 /* Stop at a double-byte single-width char. */
2839 if (ScreenLines[off + idx] == 0x8e)
2840 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002841 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 + off + idx) == 2)
2843 ++idx; /* skip second byte of double-byte char */
2844 }
2845# endif
2846 }
2847 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2848 (guicolor_T)0, (guicolor_T)0, back);
2849#endif
2850 }
2851 if (nback == FAIL)
2852 {
2853 /* Must back up to start drawing where a bold or italic word
2854 * starts. */
2855 off -= back;
2856 len += back;
2857 gui.col -= back;
2858 }
2859 else
2860 {
2861 off += idx;
2862 len -= idx;
2863 }
2864 back = 0;
2865 }
2866 }
2867
2868 /* Put the cursor back where it was */
2869 gui.row = old_row;
2870 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002871 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 return retval;
2874}
2875
2876 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002877gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 if (count <= 0)
2880 return;
2881
2882 if (row + count > gui.scroll_region_bot)
2883 /* Scrolled out of region, just blank the lines out */
2884 gui_clear_block(row, gui.scroll_region_left,
2885 gui.scroll_region_bot, gui.scroll_region_right);
2886 else
2887 {
2888 gui_mch_delete_lines(row, count);
2889
2890 /* If the cursor was in the deleted lines it's now gone. If the
2891 * cursor was in the scrolled lines adjust its position. */
2892 if (gui.cursor_row >= row
2893 && gui.cursor_col >= gui.scroll_region_left
2894 && gui.cursor_col <= gui.scroll_region_right)
2895 {
2896 if (gui.cursor_row < row + count)
2897 gui.cursor_is_valid = FALSE;
2898 else if (gui.cursor_row <= gui.scroll_region_bot)
2899 gui.cursor_row -= count;
2900 }
2901 }
2902}
2903
2904 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002905gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 if (count <= 0)
2908 return;
2909
2910 if (row + count > gui.scroll_region_bot)
2911 /* Scrolled out of region, just blank the lines out */
2912 gui_clear_block(row, gui.scroll_region_left,
2913 gui.scroll_region_bot, gui.scroll_region_right);
2914 else
2915 {
2916 gui_mch_insert_lines(row, count);
2917
2918 if (gui.cursor_row >= gui.row
2919 && gui.cursor_col >= gui.scroll_region_left
2920 && gui.cursor_col <= gui.scroll_region_right)
2921 {
2922 if (gui.cursor_row <= gui.scroll_region_bot - count)
2923 gui.cursor_row += count;
2924 else if (gui.cursor_row <= gui.scroll_region_bot)
2925 gui.cursor_is_valid = FALSE;
2926 }
2927 }
2928}
2929
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002930#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002931/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002932 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2933 */
2934 static int
2935gui_wait_for_chars_3(
2936 long wtime,
2937 int *interrupted UNUSED,
2938 int ignore_input UNUSED)
2939{
2940 return gui_mch_wait_for_chars(wtime);
2941}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002942#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002943
2944/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002945 * Returns OK if a character was found to be available within the given time,
2946 * or FAIL otherwise.
2947 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002948 static int
2949gui_wait_for_chars_or_timer(long wtime)
2950{
2951#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002952 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3, NULL, 0);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002953#else
2954 return gui_mch_wait_for_chars(wtime);
2955#endif
2956}
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958/*
2959 * The main GUI input routine. Waits for a character from the keyboard.
2960 * wtime == -1 Wait forever.
2961 * wtime == 0 Don't wait.
2962 * wtime > 0 Wait wtime milliseconds for a character.
2963 * Returns OK if a character was found to be available within the given time,
2964 * or FAIL otherwise.
2965 */
2966 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002967gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 int retval;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002970#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002971 ELAPSED_TYPE start_tv;
2972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002974#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 /*
2976 * If we're going to wait a bit, update the menus and mouse shape for the
2977 * current State.
2978 */
2979 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 gui_update_menus(0);
2981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 gui_mch_update();
2984 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989 /* Before waiting, flush any output to the screen. */
2990 gui_mch_flush();
2991
2992 if (wtime > 0)
2993 {
2994 /* Blink when waiting for a character. Probably only does something
2995 * for showmatch() */
2996 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002997 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002998 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 return retval;
3000 }
3001
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003002#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003003 ELAPSED_INIT(start_tv);
3004#endif
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00003007 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 gui_mch_start_blink();
3010
Bram Moolenaar3918c952005-03-15 22:34:55 +00003011 retval = FAIL;
3012 /*
3013 * We may want to trigger the CursorHold event. First wait for
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003014 * 'updatetime' and if nothing is typed within that time, and feedkeys()
3015 * wasn't used, put the K_CURSORHOLD key in the input buffer.
Bram Moolenaar3918c952005-03-15 22:34:55 +00003016 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01003017 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003018 retval = OK;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003019 else if (trigger_cursorhold()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003020#ifdef ELAPSED_FUNC
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003021 && ELAPSED_FUNC(start_tv) >= p_ut
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003022#endif
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003023 && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00003025 char_u buf[3];
3026
3027 /* Put K_CURSORHOLD in the input buffer. */
3028 buf[0] = CSI;
3029 buf[1] = KS_EXTRA;
3030 buf[2] = (int)KE_CURSORHOLD;
3031 add_to_input_buf(buf, 3);
3032
3033 retval = OK;
3034 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00003035
Bram Moolenaar9472eec2017-06-05 13:31:56 +02003036 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003037 {
3038 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00003039 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01003040 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003043 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 return retval;
3045}
3046
3047/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003048 * Equivalent of mch_inchar() for the GUI.
3049 */
3050 int
3051gui_inchar(
3052 char_u *buf,
3053 int maxlen,
3054 long wtime, /* milli seconds */
3055 int tb_change_cnt)
3056{
3057 if (gui_wait_for_chars(wtime, tb_change_cnt)
3058 && !typebuf_changed(tb_change_cnt))
3059 return read_from_input_buf(buf, (long)maxlen);
3060 return 0;
3061}
3062
3063/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003064 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
3066 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003067fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 p[0] = (char_u)(col / 128 + ' ' + 1);
3070 p[1] = (char_u)(col % 128 + ' ' + 1);
3071 p[2] = (char_u)(row / 128 + ' ' + 1);
3072 p[3] = (char_u)(row % 128 + ' ' + 1);
3073}
3074
3075/*
3076 * Generic mouse support function. Add a mouse event to the input buffer with
3077 * the given properties.
3078 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3079 * MOUSE_X1, MOUSE_X2
3080 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003081 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3082 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * x, y --- Coordinates of mouse in pixels.
3084 * repeated_click --- TRUE if this click comes only a short time after a
3085 * previous click.
3086 * modifiers --- Bit field which may be any of the following modifiers
3087 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3088 * This function will ignore drag events where the mouse has not moved to a new
3089 * character.
3090 */
3091 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003092gui_send_mouse_event(
3093 int button,
3094 int x,
3095 int y,
3096 int repeated_click,
3097 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 static int prev_row = 0, prev_col = 0;
3100 static int prev_button = -1;
3101 static int num_clicks = 1;
3102 char_u string[10];
3103 enum key_extra button_char;
3104 int row, col;
3105#ifdef FEAT_CLIPBOARD
3106 int checkfor;
3107 int did_clip = FALSE;
3108#endif
3109
3110 /*
3111 * Scrolling may happen at any time, also while a selection is present.
3112 */
3113 switch (button)
3114 {
3115 case MOUSE_X1:
3116 button_char = KE_X1MOUSE;
3117 goto button_set;
3118 case MOUSE_X2:
3119 button_char = KE_X2MOUSE;
3120 goto button_set;
3121 case MOUSE_4:
3122 button_char = KE_MOUSEDOWN;
3123 goto button_set;
3124 case MOUSE_5:
3125 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003126 goto button_set;
3127 case MOUSE_6:
3128 button_char = KE_MOUSELEFT;
3129 goto button_set;
3130 case MOUSE_7:
3131 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132button_set:
3133 {
3134 /* Don't put events in the input queue now. */
3135 if (hold_gui_events)
3136 return;
3137
3138 string[3] = CSI;
3139 string[4] = KS_EXTRA;
3140 string[5] = (int)button_char;
3141
3142 /* Pass the pointer coordinates of the scroll event so that we
3143 * know which window to scroll. */
3144 row = gui_xy2colrow(x, y, &col);
3145 string[6] = (char_u)(col / 128 + ' ' + 1);
3146 string[7] = (char_u)(col % 128 + ' ' + 1);
3147 string[8] = (char_u)(row / 128 + ' ' + 1);
3148 string[9] = (char_u)(row % 128 + ' ' + 1);
3149
3150 if (modifiers == 0)
3151 add_to_input_buf(string + 3, 7);
3152 else
3153 {
3154 string[0] = CSI;
3155 string[1] = KS_MODIFIER;
3156 string[2] = 0;
3157 if (modifiers & MOUSE_SHIFT)
3158 string[2] |= MOD_MASK_SHIFT;
3159 if (modifiers & MOUSE_CTRL)
3160 string[2] |= MOD_MASK_CTRL;
3161 if (modifiers & MOUSE_ALT)
3162 string[2] |= MOD_MASK_ALT;
3163 add_to_input_buf(string, 10);
3164 }
3165 return;
3166 }
3167 }
3168
3169#ifdef FEAT_CLIPBOARD
3170 /* If a clipboard selection is in progress, handle it */
3171 if (clip_star.state == SELECT_IN_PROGRESS)
3172 {
3173 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3174 return;
3175 }
3176
3177 /* Determine which mouse settings to look for based on the current mode */
3178 switch (get_real_state())
3179 {
3180 case NORMAL_BUSY:
3181 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003182# ifdef FEAT_TERMINAL
3183 case TERMINAL:
3184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 case NORMAL: checkfor = MOUSE_NORMAL; break;
3186 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003187 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 case REPLACE:
3189 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 case VREPLACE:
3191 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 case INSERT:
3193 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3194 case ASKMORE:
3195 case HITRETURN: /* At the more- and hit-enter prompt pass the
3196 mouse event for a click on or below the
3197 message line. */
3198 if (Y_2_ROW(y) >= msg_row)
3199 checkfor = MOUSE_NORMAL;
3200 else
3201 checkfor = MOUSE_RETURN;
3202 break;
3203
3204 /*
3205 * On the command line, use the clipboard selection on all lines
3206 * but the command line. But not when pasting.
3207 */
3208 case CMDLINE:
3209 case CMDLINE+LANGMAP:
3210 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3211 checkfor = MOUSE_NONE;
3212 else
3213 checkfor = MOUSE_COMMAND;
3214 break;
3215
3216 default:
3217 checkfor = MOUSE_NONE;
3218 break;
3219 };
3220
3221 /*
3222 * Allow clipboard selection of text on the command line in "normal"
3223 * modes. Don't do this when dragging the status line, or extending a
3224 * Visual selection.
3225 */
3226 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003227 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 && button != MOUSE_DRAG
3229# ifdef FEAT_MOUSESHAPE
3230 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232# endif
3233 )
3234 checkfor = MOUSE_NONE;
3235
3236 /*
3237 * Use modeless selection when holding CTRL and SHIFT pressed.
3238 */
3239 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3240 checkfor = MOUSE_NONEF;
3241
3242 /*
3243 * In Ex mode, always use modeless selection.
3244 */
3245 if (exmode_active)
3246 checkfor = MOUSE_NONE;
3247
3248 /*
3249 * If the mouse settings say to not use the mouse, use the modeless
3250 * selection. But if Visual is active, assume that only the Visual area
3251 * will be selected.
3252 * Exception: On the command line, both the selection is used and a mouse
3253 * key is send.
3254 */
3255 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 /* Don't do modeless selection in Visual mode. */
3258 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3259 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 /*
3262 * When 'mousemodel' is "popup", shift-left is translated to right.
3263 * But not when also using Ctrl.
3264 */
3265 if (mouse_model_popup() && button == MOUSE_LEFT
3266 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3267 {
3268 button = MOUSE_RIGHT;
3269 modifiers &= ~ MOUSE_SHIFT;
3270 }
3271
3272 /* If the selection is done, allow the right button to extend it.
3273 * If the selection is cleared, allow the right button to start it
3274 * from the cursor position. */
3275 if (button == MOUSE_RIGHT)
3276 {
3277 if (clip_star.state == SELECT_CLEARED)
3278 {
3279 if (State & CMDLINE)
3280 {
3281 col = msg_col;
3282 row = msg_row;
3283 }
3284 else
3285 {
3286 col = curwin->w_wcol;
3287 row = curwin->w_wrow + W_WINROW(curwin);
3288 }
3289 clip_start_selection(col, row, FALSE);
3290 }
3291 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3292 repeated_click);
3293 did_clip = TRUE;
3294 }
3295 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003296 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3299 did_clip = TRUE;
3300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
3302 /* Always allow pasting */
3303 if (button != MOUSE_MIDDLE)
3304 {
3305 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3306 return;
3307 if (checkfor != MOUSE_COMMAND)
3308 button = MOUSE_LEFT;
3309 }
3310 repeated_click = FALSE;
3311 }
3312
3313 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003314 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
3316
3317 /* Don't put events in the input queue now. */
3318 if (hold_gui_events)
3319 return;
3320
3321 row = gui_xy2colrow(x, y, &col);
3322
3323 /*
3324 * If we are dragging and the mouse hasn't moved far enough to be on a
3325 * different character, then don't send an event to vim.
3326 */
3327 if (button == MOUSE_DRAG)
3328 {
3329 if (row == prev_row && col == prev_col)
3330 return;
3331 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3332 if (y < 0)
3333 row = -1;
3334 }
3335
3336 /*
3337 * If topline has changed (window scrolled) since the last click, reset
3338 * repeated_click, because we don't want starting Visual mode when
3339 * clicking on a different character in the text.
3340 */
3341 if (curwin->w_topline != gui_prev_topline
3342#ifdef FEAT_DIFF
3343 || curwin->w_topfill != gui_prev_topfill
3344#endif
3345 )
3346 repeated_click = FALSE;
3347
3348 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3349 string[1] = KS_MOUSE;
3350 string[2] = KE_FILLER;
3351 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3352 {
3353 if (repeated_click)
3354 {
3355 /*
3356 * Handle multiple clicks. They only count if the mouse is still
3357 * pointing at the same character.
3358 */
3359 if (button != prev_button || row != prev_row || col != prev_col)
3360 num_clicks = 1;
3361 else if (++num_clicks > 4)
3362 num_clicks = 1;
3363 }
3364 else
3365 num_clicks = 1;
3366 prev_button = button;
3367 gui_prev_topline = curwin->w_topline;
3368#ifdef FEAT_DIFF
3369 gui_prev_topfill = curwin->w_topfill;
3370#endif
3371
3372 string[3] = (char_u)(button | 0x20);
3373 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3374 }
3375 else
3376 string[3] = (char_u)button;
3377
3378 string[3] |= modifiers;
3379 fill_mouse_coord(string + 4, col, row);
3380 add_to_input_buf(string, 8);
3381
3382 if (row < 0)
3383 prev_row = 0;
3384 else
3385 prev_row = row;
3386 prev_col = col;
3387
3388 /*
3389 * We need to make sure this is cleared since Athena doesn't tell us when
3390 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3391 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003392#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 gui.dragged_sb = SBAR_NONE;
3394#endif
3395}
3396
3397/*
3398 * Convert x and y coordinate to column and row in text window.
3399 * Corrects for multi-byte character.
3400 * returns column in "*colp" and row as return value;
3401 */
3402 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003403gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
3405 int col = check_col(X_2_COL(x));
3406 int row = check_row(Y_2_ROW(y));
3407
3408#ifdef FEAT_MBYTE
3409 *colp = mb_fix_col(col, row);
3410#else
3411 *colp = col;
3412#endif
3413 return row;
3414}
3415
3416#if defined(FEAT_MENU) || defined(PROTO)
3417/*
3418 * Callback function for when a menu entry has been selected.
3419 */
3420 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003421gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003423 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 /* Don't put events in the input queue now. */
3426 if (hold_gui_events)
3427 return;
3428
3429 bytes[0] = CSI;
3430 bytes[1] = KS_MENU;
3431 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003432 add_to_input_buf(bytes, 3);
3433 add_long_to_buf((long_u)menu, bytes);
3434 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435}
3436#endif
3437
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003438static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440/*
3441 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003442 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 * in p_go.
3444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003446gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#ifdef FEAT_MENU
3449 static int prev_menu_is_active = -1;
3450#endif
3451#ifdef FEAT_TOOLBAR
3452 static int prev_toolbar = -1;
3453 int using_toolbar = FALSE;
3454#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003455#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003456 int using_tabline;
3457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#ifdef FEAT_FOOTER
3459 static int prev_footer = -1;
3460 int using_footer = FALSE;
3461#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 static int prev_tearoff = -1;
3464 int using_tearoff = FALSE;
3465#endif
3466
3467 char_u *p;
3468 int i;
3469#ifdef FEAT_MENU
3470 int grey_old, grey_new;
3471 char_u *temp;
3472#endif
3473 win_T *wp;
3474 int need_set_size;
3475 int fix_size;
3476
3477#ifdef FEAT_MENU
3478 if (oldval != NULL && gui.in_use)
3479 {
3480 /*
3481 * Check if the menu's go from grey to non-grey or vise versa.
3482 */
3483 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3484 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3485 if (grey_old != grey_new)
3486 {
3487 temp = p_go;
3488 p_go = oldval;
3489 gui_update_menus(MENU_ALL_MODES);
3490 p_go = temp;
3491 }
3492 }
3493 gui.menu_is_active = FALSE;
3494#endif
3495
3496 for (i = 0; i < 3; i++)
3497 gui.which_scrollbars[i] = FALSE;
3498 for (p = p_go; *p; p++)
3499 switch (*p)
3500 {
3501 case GO_LEFT:
3502 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3503 break;
3504 case GO_RIGHT:
3505 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 case GO_VLEFT:
3508 if (win_hasvertsplit())
3509 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3510 break;
3511 case GO_VRIGHT:
3512 if (win_hasvertsplit())
3513 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 case GO_BOT:
3516 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3517 break;
3518#ifdef FEAT_MENU
3519 case GO_MENUS:
3520 gui.menu_is_active = TRUE;
3521 break;
3522#endif
3523 case GO_GREY:
3524 /* make menu's have grey items, ignored here */
3525 break;
3526#ifdef FEAT_TOOLBAR
3527 case GO_TOOLBAR:
3528 using_toolbar = TRUE;
3529 break;
3530#endif
3531#ifdef FEAT_FOOTER
3532 case GO_FOOTER:
3533 using_footer = TRUE;
3534 break;
3535#endif
3536 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003537#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 using_tearoff = TRUE;
3539#endif
3540 break;
3541 default:
3542 /* Ignore options that are not supported */
3543 break;
3544 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (gui.in_use)
3547 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003548 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003550
3551#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003552 /* Update the GUI tab line, it may appear or disappear. This may
3553 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003554 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003555 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003556 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003557 /* We don't want a resize event change "Rows" here, save and
3558 * restore it. Resizing is handled below. */
3559 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003560 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003561 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003562 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003563 if (using_tabline)
3564 fix_size = TRUE;
3565 if (!gui_use_tabline())
3566 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3567 }
3568#endif
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 for (i = 0; i < 3; i++)
3571 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003572 /* The scrollbar needs to be updated when it is shown/unshown and
3573 * when switching tab pages. But the size only changes when it's
3574 * shown/unshown. Thus we need two places to remember whether a
3575 * scrollbar is there or not. */
3576 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003577 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003578 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 if (i == SBAR_BOTTOM)
3581 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3582 gui.which_scrollbars[i]);
3583 else
3584 {
3585 FOR_ALL_WINDOWS(wp)
3586 {
3587 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3588 }
3589 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003590 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3591 {
3592 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003593 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003594 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003595 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003596 if (gui.which_scrollbars[i])
3597 fix_size = TRUE;
3598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003600 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003601 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
3604#ifdef FEAT_MENU
3605 if (gui.menu_is_active != prev_menu_is_active)
3606 {
3607 /* We don't want a resize event change "Rows" here, save and
3608 * restore it. Resizing is handled below. */
3609 i = Rows;
3610 gui_mch_enable_menu(gui.menu_is_active);
3611 Rows = i;
3612 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003613 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (gui.menu_is_active)
3615 fix_size = TRUE;
3616 }
3617#endif
3618
3619#ifdef FEAT_TOOLBAR
3620 if (using_toolbar != prev_toolbar)
3621 {
3622 gui_mch_show_toolbar(using_toolbar);
3623 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003624 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (using_toolbar)
3626 fix_size = TRUE;
3627 }
3628#endif
3629#ifdef FEAT_FOOTER
3630 if (using_footer != prev_footer)
3631 {
3632 gui_mch_enable_footer(using_footer);
3633 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003634 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (using_footer)
3636 fix_size = TRUE;
3637 }
3638#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003639#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (using_tearoff != prev_tearoff)
3641 {
3642 gui_mch_toggle_tearoffs(using_tearoff);
3643 prev_tearoff = using_tearoff;
3644 }
3645#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003646 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003647 {
3648#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003649 long prev_Columns = Columns;
3650 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 /* Adjust the size of the window to make the text area keep the
3653 * same size and to avoid that part of our window is off-screen
3654 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003655 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003656
3657#ifdef FEAT_GUI_GTK
3658 /* GTK has the annoying habit of sending us resize events when
3659 * changing the window size ourselves. This mostly happens when
3660 * waiting for a character to arrive, quite unpredictably, and may
3661 * change Columns and Rows when we don't want it. Wait for a
3662 * character here to avoid this effect.
3663 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003664 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003665 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003666 * Don't change Rows when adding menu/toolbar/tabline.
3667 * Don't change Columns when adding vertical toolbar. */
3668 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003669 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003670 if ((need_set_size & RESIZE_VERT) == 0)
3671 Rows = prev_Rows;
3672 if ((need_set_size & RESIZE_HOR) == 0)
3673 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003674#endif
3675 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003676 /* When the console tabline appears or disappears the window positions
3677 * change. */
3678 if (firstwin->w_winrow != tabline_height())
3679 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681}
3682
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003683#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3684/*
3685 * Return TRUE if the GUI is taking care of the tabline.
3686 * It may still be hidden if 'showtabline' is zero.
3687 */
3688 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003689gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003690{
3691 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3692}
3693
3694/*
3695 * Return TRUE if the GUI is showing the tabline.
3696 * This uses 'showtabline'.
3697 */
3698 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003699gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003700{
3701 if (!gui_use_tabline()
3702 || p_stal == 0
3703 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3704 return FALSE;
3705 return TRUE;
3706}
3707
3708/*
3709 * Update the tabline.
3710 * This may display/undisplay the tabline and update the labels.
3711 */
3712 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003713gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003714{
3715 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003716 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003717
3718 if (!gui.starting && starting == 0)
3719 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003720 /* Updating the tabline uses direct GUI commands, flush
3721 * outstanding instructions first. (esp. clear screen) */
3722 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003723
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003724 if (!showit != !shown)
3725 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003726 if (showit != 0)
3727 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003728
3729 /* When the tabs change from hidden to shown or from shown to
3730 * hidden the size of the text area should remain the same. */
3731 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003732 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003733 }
3734}
3735
3736/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003737 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003738 */
3739 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003740get_tabline_label(
3741 tabpage_T *tp,
3742 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003743{
3744 int modified = FALSE;
3745 char_u buf[40];
3746 int wincount;
3747 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003748 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003749
Bram Moolenaar57657d82006-04-21 22:12:41 +00003750 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003751 opt = (tooltip ? &p_gtt : &p_gtl);
3752 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003754 int use_sandbox = FALSE;
3755 int save_called_emsg = called_emsg;
3756 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003757 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003758 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3759 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003760
3761 called_emsg = FALSE;
3762
3763 printer_page_num = tabpage_index(tp);
3764# ifdef FEAT_EVAL
3765 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003766 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003767# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003768 /* It's almost as going to the tabpage, but without autocommands. */
3769 curtab->tp_firstwin = firstwin;
3770 curtab->tp_lastwin = lastwin;
3771 curtab->tp_curwin = curwin;
3772 save_curtab = curtab;
3773 curtab = tp;
3774 topframe = curtab->tp_topframe;
3775 firstwin = curtab->tp_firstwin;
3776 lastwin = curtab->tp_lastwin;
3777 curwin = curtab->tp_curwin;
3778 curbuf = curwin->w_buffer;
3779
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003780 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003781 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003782 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003783 STRCPY(NameBuff, res);
3784
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003785 /* Back to the original curtab. */
3786 curtab = save_curtab;
3787 topframe = curtab->tp_topframe;
3788 firstwin = curtab->tp_firstwin;
3789 lastwin = curtab->tp_lastwin;
3790 curwin = curtab->tp_curwin;
3791 curbuf = curwin->w_buffer;
3792
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003793 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003794 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003795 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003796 called_emsg |= save_called_emsg;
3797 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003798
3799 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3800 * use a default label. */
3801 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003802 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003803 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003804 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003805 if (!tooltip)
3806 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003807
3808 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3809 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3810 if (bufIsChanged(wp->w_buffer))
3811 modified = TRUE;
3812 if (modified || wincount > 1)
3813 {
3814 if (wincount > 1)
3815 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3816 else
3817 buf[0] = NUL;
3818 if (modified)
3819 STRCAT(buf, "+");
3820 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003821 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003822 mch_memmove(NameBuff, buf, STRLEN(buf));
3823 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003824 }
3825}
3826
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003827/*
3828 * Send the event for clicking to select tab page "nr".
3829 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003830 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003831 */
3832 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003833send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003834{
3835 char_u string[3];
3836
3837 if (nr == tabpage_index(curtab))
3838 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003839
3840 /* Don't put events in the input queue now. */
3841 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003842# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003843 || cmdwin_type != 0
3844# endif
3845 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003846 {
3847 /* Set it back to the current tab page. */
3848 gui_mch_set_curtab(tabpage_index(curtab));
3849 return FALSE;
3850 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003851
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003852 string[0] = CSI;
3853 string[1] = KS_TABLINE;
3854 string[2] = KE_FILLER;
3855 add_to_input_buf(string, 3);
3856 string[0] = nr;
3857 add_to_input_buf_csi(string, 1);
3858 return TRUE;
3859}
3860
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003861/*
3862 * Send a tabline menu event
3863 */
3864 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003865send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003866{
3867 char_u string[3];
3868
Bram Moolenaar29547192018-12-11 20:39:19 +01003869 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003870 if (hold_gui_events)
3871 return;
3872
Bram Moolenaar29547192018-12-11 20:39:19 +01003873 // Cannot close the last tabpage.
3874 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3875 return;
3876
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003877 string[0] = CSI;
3878 string[1] = KS_TABMENU;
3879 string[2] = KE_FILLER;
3880 add_to_input_buf(string, 3);
3881 string[0] = tabidx;
3882 string[1] = (char_u)(long)event;
3883 add_to_input_buf_csi(string, 2);
3884}
3885
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888/*
3889 * Scrollbar stuff:
3890 */
3891
Bram Moolenaare45828b2006-02-15 22:12:56 +00003892/*
3893 * Remove all scrollbars. Used before switching to another tab page.
3894 */
3895 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003896gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003897{
3898 int i;
3899 win_T *wp;
3900
3901 for (i = 0; i < 3; i++)
3902 {
3903 if (i == SBAR_BOTTOM)
3904 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3905 else
3906 {
3907 FOR_ALL_WINDOWS(wp)
3908 {
3909 gui_do_scrollbar(wp, i, FALSE);
3910 }
3911 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003912 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003913 }
3914}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003917gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 static int sbar_ident = 0;
3920
3921 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3922 sb->wp = wp;
3923 sb->type = type;
3924 sb->value = 0;
3925#ifdef FEAT_GUI_ATHENA
3926 sb->pixval = 0;
3927#endif
3928 sb->size = 1;
3929 sb->max = 1;
3930 sb->top = 0;
3931 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 sb->status_height = 0;
3934 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3935}
3936
3937/*
3938 * Find the scrollbar with the given index.
3939 */
3940 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003941gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 win_T *wp;
3944
3945 if (gui.bottom_sbar.ident == ident)
3946 return &gui.bottom_sbar;
3947 FOR_ALL_WINDOWS(wp)
3948 {
3949 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3950 return &wp->w_scrollbars[SBAR_LEFT];
3951 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3952 return &wp->w_scrollbars[SBAR_RIGHT];
3953 }
3954 return NULL;
3955}
3956
3957/*
3958 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3959 *
3960 * For Win32, Macintosh and GTK+ 2:
3961 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3962 * you stop dragging the scrollbar. We get here each time the scrollbar is
3963 * dragged another pixel, but as far as the rest of vim goes, it thinks
3964 * we're just hanging in the call to DispatchMessage() in
3965 * process_message(). The DispatchMessage() call that hangs was passed a
3966 * mouse button click event in the scrollbar window. -- webb.
3967 *
3968 * Solution: Do the scrolling right here. But only when allowed.
3969 * Ignore the scrollbars while executing an external command or when there
3970 * are still characters to be processed.
3971 */
3972 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003973gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 int sb_num;
3977#ifdef USE_ON_FLY_SCROLL
3978 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980# ifdef FEAT_DIFF
3981 int old_topfill = curwin->w_topfill;
3982# endif
3983#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003984 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 int byte_count;
3986#endif
3987
3988 if (sb == NULL)
3989 return;
3990
3991 /* Don't put events in the input queue now. */
3992 if (hold_gui_events)
3993 return;
3994
3995#ifdef FEAT_CMDWIN
3996 if (cmdwin_type != 0 && sb->wp != curwin)
3997 return;
3998#endif
3999
4000 if (still_dragging)
4001 {
4002 if (sb->wp == NULL)
4003 gui.dragged_sb = SBAR_BOTTOM;
4004 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4005 gui.dragged_sb = SBAR_LEFT;
4006 else
4007 gui.dragged_sb = SBAR_RIGHT;
4008 gui.dragged_wp = sb->wp;
4009 }
4010 else
4011 {
4012 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004013#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004015 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 gui.dragged_wp = NULL;
4017#endif
4018 }
4019
4020 /* Vertical sbar info is kept in the first sbar (the left one) */
4021 if (sb->wp != NULL)
4022 sb = &sb->wp->w_scrollbars[0];
4023
4024 /*
4025 * Check validity of value
4026 */
4027 if (value < 0)
4028 value = 0;
4029#ifdef SCROLL_PAST_END
4030 else if (value > sb->max)
4031 value = sb->max;
4032#else
4033 if (value > sb->max - sb->size + 1)
4034 value = sb->max - sb->size + 1;
4035#endif
4036
4037 sb->value = value;
4038
4039#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00004040 /* When not allowed to do the scrolling right now, return.
4041 * This also checked input_available(), but that causes the first click in
4042 * a scrollbar to be ignored when Vim doesn't have focus. */
4043 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return;
4045#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004046#ifdef FEAT_INS_EXPAND
4047 /* Disallow scrolling the current window when the completion popup menu is
4048 * visible. */
4049 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4050 return;
4051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053#ifdef FEAT_RIGHTLEFT
4054 if (sb->wp == NULL && curwin->w_p_rl)
4055 {
4056 value = sb->max + 1 - sb->size - value;
4057 if (value < 0)
4058 value = 0;
4059 }
4060#endif
4061
4062 if (sb->wp != NULL) /* vertical scrollbar */
4063 {
4064 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4066 sb_num++;
4067 if (wp == NULL)
4068 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070#ifdef USE_ON_FLY_SCROLL
4071 current_scrollbar = sb_num;
4072 scrollbar_value = value;
4073 if (State & NORMAL)
4074 {
4075 gui_do_scroll();
4076 setcursor();
4077 }
4078 else if (State & INSERT)
4079 {
4080 ins_scroll();
4081 setcursor();
4082 }
4083 else if (State & CMDLINE)
4084 {
4085 if (msg_scrolled == 0)
4086 {
4087 gui_do_scroll();
4088 redrawcmdline();
4089 }
4090 }
4091# ifdef FEAT_FOLDING
4092 /* Value may have been changed for closed fold. */
4093 sb->value = sb->wp->w_topline - 1;
4094# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004095
4096 /* When dragging one scrollbar and there is another one at the other
4097 * side move the thumb of that one too. */
4098 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4099 gui_mch_set_scrollbar_thumb(
4100 &sb->wp->w_scrollbars[
4101 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4102 ? SBAR_LEFT : SBAR_RIGHT],
4103 sb->value, sb->size, sb->max);
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#else
4106 bytes[0] = CSI;
4107 bytes[1] = KS_VER_SCROLLBAR;
4108 bytes[2] = KE_FILLER;
4109 bytes[3] = (char_u)sb_num;
4110 byte_count = 4;
4111#endif
4112 }
4113 else
4114 {
4115#ifdef USE_ON_FLY_SCROLL
4116 scrollbar_value = value;
4117
4118 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004119 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 else if (State & INSERT)
4121 ins_horscroll();
4122 else if (State & CMDLINE)
4123 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004124 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004126 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 redrawcmdline();
4128 }
4129 }
4130 if (old_leftcol != curwin->w_leftcol)
4131 {
4132 updateWindow(curwin); /* update window, status and cmdline */
4133 setcursor();
4134 }
4135#else
4136 bytes[0] = CSI;
4137 bytes[1] = KS_HOR_SCROLLBAR;
4138 bytes[2] = KE_FILLER;
4139 byte_count = 3;
4140#endif
4141 }
4142
4143#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 /*
4145 * synchronize other windows, as necessary according to 'scrollbind'
4146 */
4147 if (curwin->w_p_scb
4148 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4149 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004150# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 ))))
4154 {
4155 do_check_scrollbind(TRUE);
4156 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004157 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 if (wp->w_redr_type > 0)
4159 updateWindow(wp);
4160 setcursor();
4161 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004162 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004164 add_to_input_buf(bytes, byte_count);
4165 add_long_to_buf((long_u)value, bytes);
4166 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#endif
4168}
4169
4170/*
4171 * Scrollbar stuff:
4172 */
4173
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004174/*
4175 * Called when something in the window layout has changed.
4176 */
4177 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004178gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004179{
4180 if (gui.in_use && starting == 0)
4181 {
4182 out_flush();
4183 gui_init_which_components(NULL);
4184 gui_update_scrollbars(TRUE);
4185 }
4186 need_mouse_correct = TRUE;
4187}
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004190gui_update_scrollbars(
4191 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 win_T *wp;
4194 scrollbar_T *sb;
4195 long val, size, max; /* need 32 bits here */
4196 int which_sb;
4197 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
4200 /* Update the horizontal scrollbar */
4201 gui_update_horiz_scrollbar(force);
4202
4203#ifndef WIN3264
4204 /* Return straight away if there is neither a left nor right scrollbar.
4205 * On MS-Windows this is required anyway for scrollwheel messages. */
4206 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4207 return;
4208#endif
4209
4210 /*
4211 * Don't want to update a scrollbar while we're dragging it. But if we
4212 * have both a left and right scrollbar, and we drag one of them, we still
4213 * need to update the other one.
4214 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004215 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4216 && gui.which_scrollbars[SBAR_LEFT]
4217 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
4219 /*
4220 * If we have two scrollbars and one of them is being dragged, just
4221 * copy the scrollbar position from the dragged one to the other one.
4222 */
4223 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4224 if (gui.dragged_wp != NULL)
4225 gui_mch_set_scrollbar_thumb(
4226 &gui.dragged_wp->w_scrollbars[which_sb],
4227 gui.dragged_wp->w_scrollbars[0].value,
4228 gui.dragged_wp->w_scrollbars[0].size,
4229 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
4232 /* avoid that moving components around generates events */
4233 ++hold_gui_events;
4234
Bram Moolenaar45a24952016-07-24 22:25:15 +02004235 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 if (wp->w_buffer == NULL) /* just in case */
4238 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004239 /* Skip a scrollbar that is being dragged. */
4240 if (!force && (gui.dragged_sb == SBAR_LEFT
4241 || gui.dragged_sb == SBAR_RIGHT)
4242 && gui.dragged_wp == wp)
4243 continue;
4244
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#ifdef SCROLL_PAST_END
4246 max = wp->w_buffer->b_ml.ml_line_count - 1;
4247#else
4248 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4249#endif
4250 if (max < 0) /* empty buffer */
4251 max = 0;
4252 val = wp->w_topline - 1;
4253 size = wp->w_height;
4254#ifdef SCROLL_PAST_END
4255 if (val > max) /* just in case */
4256 val = max;
4257#else
4258 if (size > max + 1) /* just in case */
4259 size = max + 1;
4260 if (val > max - size + 1)
4261 val = max - size + 1;
4262#endif
4263 if (val < 0) /* minimal value is 0 */
4264 val = 0;
4265
4266 /*
4267 * Scrollbar at index 0 (the left one) contains all the information.
4268 * It would be the same info for left and right so we just store it for
4269 * one of them.
4270 */
4271 sb = &wp->w_scrollbars[0];
4272
4273 /*
4274 * Note: no check for valid w_botline. If it's not valid the
4275 * scrollbars will be updated later anyway.
4276 */
4277 if (size < 1 || wp->w_botline - 2 > max)
4278 {
4279 /*
4280 * This can happen during changing files. Just don't update the
4281 * scrollbar for now.
4282 */
4283 sb->height = 0; /* Force update next time */
4284 if (gui.which_scrollbars[SBAR_LEFT])
4285 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4286 if (gui.which_scrollbars[SBAR_RIGHT])
4287 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4288 continue;
4289 }
4290 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 || sb->top != wp->w_winrow
4292 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004294 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 /* Height, width or position of scrollbar has changed. For
4297 * vertical split: curwin changed. */
4298 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 sb->top = wp->w_winrow;
4300 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /* Calculate height and position in pixels */
4304 h = (sb->height + sb->status_height) * gui.char_height;
4305 y = sb->top * gui.char_height + gui.border_offset;
4306#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4307 if (gui.menu_is_active)
4308 y += gui.menu_height;
4309#endif
4310
4311#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4312 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4313# ifdef FEAT_GUI_ATHENA
4314 y += gui.toolbar_height;
4315# else
4316# ifdef FEAT_GUI_MSWIN
4317 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4318# endif
4319# endif
4320#endif
4321
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004322#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4323 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004324 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004325#endif
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 /* Height of top scrollbar includes width of top border */
4330 h += gui.border_offset;
4331 y -= gui.border_offset;
4332 }
4333 if (gui.which_scrollbars[SBAR_LEFT])
4334 {
4335 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4336 gui.left_sbar_x, y,
4337 gui.scrollbar_width, h);
4338 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4339 }
4340 if (gui.which_scrollbars[SBAR_RIGHT])
4341 {
4342 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4343 gui.right_sbar_x, y,
4344 gui.scrollbar_width, h);
4345 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4346 }
4347 }
4348
4349 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4350 * checking if the thumb moved at least a pixel. Only do this for
4351 * Athena, most other GUIs require the update anyway to make the
4352 * arrows work. */
4353#ifdef FEAT_GUI_ATHENA
4354 if (max == 0)
4355 y = 0;
4356 else
4357 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4358 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4359#else
4360 if (force || sb->value != val || sb->size != size || sb->max != max)
4361#endif
4362 {
4363 /* Thumb of scrollbar has moved */
4364 sb->value = val;
4365#ifdef FEAT_GUI_ATHENA
4366 sb->pixval = y;
4367#endif
4368 sb->size = size;
4369 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004370 if (gui.which_scrollbars[SBAR_LEFT]
4371 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4373 val, size, max);
4374 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004375 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4377 val, size, max);
4378 }
4379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 --hold_gui_events;
4382}
4383
4384/*
4385 * Enable or disable a scrollbar.
4386 * Check for scrollbars for vertically split windows which are not enabled
4387 * sometimes.
4388 */
4389 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004390gui_do_scrollbar(
4391 win_T *wp,
4392 int which, /* SBAR_LEFT or SBAR_RIGHT */
4393 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 int midcol = curwin->w_wincol + curwin->w_width / 2;
4396 int has_midcol = (wp->w_wincol <= midcol
4397 && wp->w_wincol + wp->w_width >= midcol);
4398
4399 /* Only enable scrollbars that contain the middle column of the current
4400 * window. */
4401 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4402 {
4403 /* Scrollbars only on one side. Don't enable scrollbars that don't
4404 * contain the middle column of the current window. */
4405 if (!has_midcol)
4406 enable = FALSE;
4407 }
4408 else
4409 {
4410 /* Scrollbars on both sides. Don't enable scrollbars that neither
4411 * contain the middle column of the current window nor are on the far
4412 * side. */
4413 if (midcol > Columns / 2)
4414 {
4415 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4416 enable = FALSE;
4417 }
4418 else
4419 {
4420 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4421 : !has_midcol)
4422 enable = FALSE;
4423 }
4424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4426}
4427
4428/*
4429 * Scroll a window according to the values set in the globals current_scrollbar
4430 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4431 * or FALSE otherwise.
4432 */
4433 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004434gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
4436 win_T *wp, *save_wp;
4437 int i;
4438 long nlines;
4439 pos_T old_cursor;
4440 linenr_T old_topline;
4441#ifdef FEAT_DIFF
4442 int old_topfill;
4443#endif
4444
4445 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4446 if (wp == NULL)
4447 break;
4448 if (wp == NULL)
4449 /* Couldn't find window */
4450 return FALSE;
4451
4452 /*
4453 * Compute number of lines to scroll. If zero, nothing to do.
4454 */
4455 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4456 if (nlines == 0)
4457 return FALSE;
4458
4459 save_wp = curwin;
4460 old_topline = wp->w_topline;
4461#ifdef FEAT_DIFF
4462 old_topfill = wp->w_topfill;
4463#endif
4464 old_cursor = wp->w_cursor;
4465 curwin = wp;
4466 curbuf = wp->w_buffer;
4467 if (nlines < 0)
4468 scrolldown(-nlines, gui.dragged_wp == NULL);
4469 else
4470 scrollup(nlines, gui.dragged_wp == NULL);
4471 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4472 * the mouse-up event already, but we still want it to behave like when
4473 * dragging. But not the next click in an arrow. */
4474 if (gui.dragged_sb == SBAR_NONE)
4475 gui.dragged_wp = NULL;
4476
4477 if (old_topline != wp->w_topline
4478#ifdef FEAT_DIFF
4479 || old_topfill != wp->w_topfill
4480#endif
4481 )
4482 {
4483 if (p_so != 0)
4484 {
4485 cursor_correct(); /* fix window for 'so' */
4486 update_topline(); /* avoid up/down jump */
4487 }
4488 if (old_cursor.lnum != wp->w_cursor.lnum)
4489 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004493 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4494 validate_cursor();
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 curwin = save_wp;
4497 curbuf = save_wp->w_buffer;
4498
4499 /*
4500 * Don't call updateWindow() when nothing has changed (it will overwrite
4501 * the status line!).
4502 */
4503 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004504 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#ifdef FEAT_DIFF
4506 || old_topfill != wp->w_topfill
4507#endif
4508 )
4509 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004510 int type = VALID;
4511
4512#ifdef FEAT_INS_EXPAND
4513 if (pum_visible())
4514 {
4515 type = NOT_VALID;
4516 wp->w_lines_valid = 0;
4517 }
4518#endif
4519 /* Don't set must_redraw here, it may cause the popup menu to
4520 * disappear when losing focus after a scrollbar drag. */
4521 if (wp->w_redr_type < type)
4522 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004523 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004525 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004528#ifdef FEAT_INS_EXPAND
4529 /* May need to redraw the popup menu. */
4530 if (pum_visible())
4531 pum_redraw();
4532#endif
4533
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004534 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535}
4536
4537
4538/*
4539 * Horizontal scrollbar stuff:
4540 */
4541
4542/*
4543 * Return length of line "lnum" for horizontal scrolling.
4544 */
4545 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004546scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547{
4548 char_u *p;
4549 colnr_T col;
4550 int w;
4551
4552 p = ml_get(lnum);
4553 col = 0;
4554 if (*p != NUL)
4555 for (;;)
4556 {
4557 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004558 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (*p == NUL) /* don't count the last character */
4560 break;
4561 col += w;
4562 }
4563 return col;
4564}
4565
4566/* Remember which line is currently the longest, so that we don't have to
4567 * search for it when scrolling horizontally. */
4568static linenr_T longest_lnum = 0;
4569
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004570/*
4571 * Find longest visible line number. If this is not possible (or not desired,
4572 * by setting 'h' in "guioptions") then the current line number is returned.
4573 */
4574 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004575gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004576{
4577 linenr_T ret = 0;
4578
4579 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4580 * line numbers, topline and botline can be invalid when displaying is
4581 * postponed. */
4582 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4583 && curwin->w_topline <= curwin->w_cursor.lnum
4584 && curwin->w_botline > curwin->w_cursor.lnum
4585 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4586 {
4587 linenr_T lnum;
4588 colnr_T n;
4589 long max = 0;
4590
4591 /* Use maximum of all visible lines. Remember the lnum of the
4592 * longest line, closest to the cursor line. Used when scrolling
4593 * below. */
4594 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4595 {
4596 n = scroll_line_len(lnum);
4597 if (n > (colnr_T)max)
4598 {
4599 max = n;
4600 ret = lnum;
4601 }
4602 else if (n == (colnr_T)max
4603 && abs((int)(lnum - curwin->w_cursor.lnum))
4604 < abs((int)(ret - curwin->w_cursor.lnum)))
4605 ret = lnum;
4606 }
4607 }
4608 else
4609 /* Use cursor line only. */
4610 ret = curwin->w_cursor.lnum;
4611
4612 return ret;
4613}
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004616gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 long value, size, max; /* need 32 bit ints here */
4619
4620 if (!gui.which_scrollbars[SBAR_BOTTOM])
4621 return;
4622
4623 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4624 return;
4625
4626 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4627 return;
4628
4629 /*
4630 * It is possible for the cursor to be invalid if we're in the middle of
4631 * something (like changing files). If so, don't do anything for now.
4632 */
4633 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4634 {
4635 gui.bottom_sbar.value = -1;
4636 return;
4637 }
4638
Bram Moolenaar02631462017-09-22 15:20:32 +02004639 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (curwin->w_p_wrap)
4641 {
4642 value = 0;
4643#ifdef SCROLL_PAST_END
4644 max = 0;
4645#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004646 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#endif
4648 }
4649 else
4650 {
4651 value = curwin->w_leftcol;
4652
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004653 longest_lnum = gui_find_longest_lnum();
4654 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#ifdef FEAT_VIRTUALEDIT
4657 if (virtual_active())
4658 {
4659 /* May move the cursor even further to the right. */
4660 if (curwin->w_virtcol >= (colnr_T)max)
4661 max = curwin->w_virtcol;
4662 }
4663#endif
4664
4665#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004666 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
4668 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004669 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 size -= curwin_col_off();
4671#ifndef SCROLL_PAST_END
4672 max -= curwin_col_off();
4673#endif
4674 }
4675
4676#ifndef SCROLL_PAST_END
4677 if (value > max - size + 1)
4678 value = max - size + 1; /* limit the value to allowable range */
4679#endif
4680
4681#ifdef FEAT_RIGHTLEFT
4682 if (curwin->w_p_rl)
4683 {
4684 value = max + 1 - size - value;
4685 if (value < 0)
4686 {
4687 size += value;
4688 value = 0;
4689 }
4690 }
4691#endif
4692 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4693 && max == gui.bottom_sbar.max)
4694 return;
4695
4696 gui.bottom_sbar.value = value;
4697 gui.bottom_sbar.size = size;
4698 gui.bottom_sbar.max = max;
4699 gui.prev_wrap = curwin->w_p_wrap;
4700
4701 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4702}
4703
4704/*
4705 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4706 */
4707 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004708gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 /* no wrapping, no scrolling */
4711 if (curwin->w_p_wrap)
4712 return FALSE;
4713
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004714 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 return FALSE;
4716
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004717 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
4719 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004720 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004722 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004723 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004725 if (compute_longest_lnum)
4726 {
4727 curwin->w_cursor.lnum = gui_find_longest_lnum();
4728 curwin->w_cursor.col = 0;
4729 }
4730 /* Do a sanity check on "longest_lnum", just in case. */
4731 else if (longest_lnum >= curwin->w_topline
4732 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
4734 curwin->w_cursor.lnum = longest_lnum;
4735 curwin->w_cursor.col = 0;
4736 }
4737 }
4738
4739 return leftcol_changed();
4740}
4741
4742/*
4743 * Check that none of the colors are the same as the background color
4744 */
4745 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004746gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4749 {
4750 gui_set_bg_color((char_u *)"White");
4751 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4752 gui_set_fg_color((char_u *)"Black");
4753 }
4754}
4755
Bram Moolenaar3918c952005-03-15 22:34:55 +00004756 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004757gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 gui.norm_pixel = gui_get_color(name);
4760 hl_set_fg_color_name(vim_strsave(name));
4761}
4762
Bram Moolenaar3918c952005-03-15 22:34:55 +00004763 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004764gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 gui.back_pixel = gui_get_color(name);
4767 hl_set_bg_color_name(vim_strsave(name));
4768}
4769
4770/*
4771 * Allocate a color by name.
4772 * Returns INVALCOLOR and gives an error message when failed.
4773 */
4774 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004775gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
4777 guicolor_T t;
4778
4779 if (*name == NUL)
4780 return INVALCOLOR;
4781 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004782
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004784#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 && gui.in_use
4786#endif
4787 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004788 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 return t;
4790}
4791
4792/*
4793 * Return the grey value of a color (range 0-255).
4794 */
4795 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004796gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004798 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004800 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004801 + (((rgb >> 8) & 0xff) * 587)
4802 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803}
4804
4805#if defined(FEAT_GUI_X11) || defined(PROTO)
4806 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004807gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 win_T *wp;
4810
4811 /* Nothing to do if GUI hasn't started yet. */
4812 if (!gui.in_use)
4813 return;
4814
4815 FOR_ALL_WINDOWS(wp)
4816 {
4817 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4818 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4819 }
4820 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4821}
4822#endif
4823
4824/*
4825 * Call this when focus has changed.
4826 */
4827 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004828gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
4830/*
4831 * Skip this code to avoid drawing the cursor when debugging and switching
4832 * between the debugger window and gvim.
4833 */
4834#if 1
4835 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004836 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838# ifdef FEAT_XIM
4839 xim_set_focus(in_focus);
4840# endif
4841
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004842 /* Put events in the input queue only when allowed.
4843 * ui_focus_change() isn't called directly, because it invokes
4844 * autocommands and that must not happen asynchronously. */
4845 if (!hold_gui_events)
4846 {
4847 char_u bytes[3];
4848
4849 bytes[0] = CSI;
4850 bytes[1] = KS_EXTRA;
4851 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4852 add_to_input_buf(bytes, 3);
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#endif
4855}
4856
4857/*
4858 * Called when the mouse moved (but not when dragging).
4859 */
4860 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004861gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004864 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaard3667a22006-03-16 21:35:52 +00004866 /* Ignore this while still starting up. */
4867 if (!gui.in_use || gui.starting)
4868 return;
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#ifdef FEAT_MOUSESHAPE
4871 /* Get window pointer, and update mouse shape as well. */
4872 wp = xy2win(x, y);
4873#endif
4874
4875 /* Only handle this when 'mousefocus' set and ... */
4876 if (p_mousef
4877 && !hold_gui_events /* not holding events */
4878 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4879 && State != HITRETURN /* but not hit-return prompt */
4880 && msg_scrolled == 0 /* no scrolled message */
4881 && !need_mouse_correct /* not moving the pointer */
4882 && gui.in_focus) /* gvim in focus */
4883 {
4884 /* Don't move the mouse when it's left or right of the Vim window */
4885 if (x < 0 || x > Columns * gui.char_width)
4886 return;
4887#ifndef FEAT_MOUSESHAPE
4888 wp = xy2win(x, y);
4889#endif
4890 if (wp == curwin || wp == NULL)
4891 return; /* still in the same old window, or none at all */
4892
Bram Moolenaar9c102382006-05-03 21:26:49 +00004893 /* Ignore position in the tab pages line. */
4894 if (Y_2_ROW(y) < tabline_height())
4895 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 /*
4898 * format a mouse click on status line input
4899 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004900 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4901 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 */
4903 if (finish_op)
4904 {
4905 /* abort the current operator first */
4906 st[0] = ESC;
4907 add_to_input_buf(st, 1);
4908 }
4909 st[0] = CSI;
4910 st[1] = KS_MOUSE;
4911 st[2] = KE_FILLER;
4912 st[3] = (char_u)MOUSE_LEFT;
4913 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004914 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 wp->w_height + W_WINROW(wp));
4916
4917 add_to_input_buf(st, 8);
4918 st[3] = (char_u)MOUSE_RELEASE;
4919 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#ifdef FEAT_GUI_GTK
4921 /* Need to wake up the main loop */
4922 if (gtk_main_level() > 0)
4923 gtk_main_quit();
4924#endif
4925 }
4926}
4927
4928/*
4929 * Called when mouse should be moved to window with focus.
4930 */
4931 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004932gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 int x, y;
4935 win_T *wp = NULL;
4936
4937 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004938
4939 if (!(gui.in_use && p_mousef))
4940 return;
4941
4942 gui_mch_getmouse(&x, &y);
4943 /* Don't move the mouse when it's left or right of the Vim window */
4944 if (x < 0 || x > Columns * gui.char_width)
4945 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004946 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004947 wp = xy2win(x, y);
4948 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004950 validate_cline_row();
4951 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4952 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955}
4956
4957/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004958 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004961xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 int row;
4964 int col;
4965 win_T *wp;
4966
4967 row = Y_2_ROW(y);
4968 col = X_2_COL(x);
4969 if (row < 0 || col < 0) /* before first window */
4970 return NULL;
4971 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004972 if (wp == NULL)
4973 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004974#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (State == HITRETURN || State == ASKMORE)
4976 {
4977 if (Y_2_ROW(y) >= msg_row)
4978 update_mouseshape(SHAPE_IDX_MOREL);
4979 else
4980 update_mouseshape(SHAPE_IDX_MORE);
4981 }
4982 else if (row > wp->w_height) /* below status line */
4983 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004984 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004985 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004987 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004988 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 update_mouseshape(SHAPE_IDX_STATUS);
4990 else
4991 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004993 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994}
4995
4996/*
4997 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4998 * File names may be given to redefine the args list.
4999 */
5000 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005001ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
5003 char_u *arg = eap->arg;
5004
5005 /*
5006 * Check for "-f" argument: foreground, don't fork.
5007 * Also don't fork when started with "gvim -f".
5008 * Do fork when using "gui -b".
5009 */
5010 if (arg[0] == '-'
5011 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005012 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 gui.dofork = (arg[1] == 'b');
5015 eap->arg = skipwhite(eap->arg + 2);
5016 }
5017 if (!gui.in_use)
5018 {
5019 /* Clear the command. Needed for when forking+exiting, to avoid part
5020 * of the argument ending up after the shell prompt. */
5021 msg_clr_eos_force();
5022 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005024 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 if (!ends_excmd(*eap->arg))
5028 ex_next(eap);
5029}
5030
5031#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00005032 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033/*
5034 * This is shared between Athena, Motif and GTK.
5035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037/*
5038 * Callback function for do_in_runtimepath().
5039 */
5040 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005041gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005043 char_u *gfp_buffer = cookie;
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (STRLEN(fname) >= MAXPATHL)
5046 *gfp_buffer = NUL;
5047 else
5048 STRCPY(gfp_buffer, fname);
5049}
5050
5051/*
5052 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5053 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5054 */
5055 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005056gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 if (STRLEN(name) > MAXPATHL - 14)
5059 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005060 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005061 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005062 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 return FAIL;
5064 return OK;
5065}
5066
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005067# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068/*
5069 * Given the name of the "icon=" argument, try finding the bitmap file for the
5070 * icon. If it is an absolute path name, use it as it is. Otherwise append
5071 * "ext" and search for it in 'runtimepath'.
5072 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5073 * contains "name".
5074 */
5075 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005076gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 char_u buf[MAXPATHL + 1];
5079
5080 expand_env(name, buffer, MAXPATHL);
5081 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5082 STRCPY(buffer, buf);
5083}
5084# endif
5085#endif
5086
Bram Moolenaar9372a112005-12-06 19:59:18 +00005087#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005089display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090{
5091 char_u *p;
5092
5093 if (isatty(2))
5094 fflush(stderr);
5095 else if (error_ga.ga_data != NULL)
5096 {
5097 /* avoid putting up a message box with blanks only */
5098 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5099 if (!isspace(*p))
5100 {
5101 /* Truncate a very long message, it will go off-screen. */
5102 if (STRLEN(p) > 2000)
5103 STRCPY(p + 2000 - 14, "...(truncated)");
5104 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005105 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 break;
5107 }
5108 ga_clear(&error_ga);
5109 }
5110}
5111#endif
5112
5113#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5114/*
5115 * Return TRUE if still starting up and there is no place to enter text.
5116 * For GTK and X11 we check if stderr is not a tty, which means we were
5117 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5118 * allow typing on stdin.
5119 */
5120 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005121no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 return ((!gui.in_use || gui.starting)
5124# ifndef NO_CONSOLE
5125 && !isatty(0) && !isatty(2)
5126# endif
5127 );
5128}
5129#endif
5130
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005131#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005132 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005133 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134/*
5135 * Update the current window and the screen.
5136 */
5137 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005138gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005140# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005141 linenr_T conceal_old_cursor_line = 0;
5142 linenr_T conceal_new_cursor_line = 0;
5143 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005144# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 update_topline();
5147 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005148
Bram Moolenaarec80df72008-05-07 19:46:51 +00005149 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005150 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005151# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005152 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005153# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005154 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005155 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005156 if (has_cursormoved())
5157 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005158# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005159 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005160 {
5161 conceal_old_cursor_line = last_cursormoved.lnum;
5162 conceal_new_cursor_line = curwin->w_cursor.lnum;
5163 conceal_update_lines = TRUE;
5164 }
5165# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005166 last_cursormoved = curwin->w_cursor;
5167 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005168
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005169# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005170 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005171 && (conceal_old_cursor_line != conceal_new_cursor_line
5172 || conceal_cursor_line(curwin)
5173 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005174 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005175 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005176 redrawWinline(curwin, conceal_old_cursor_line);
5177 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005178 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005179 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005180 }
5181# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005182 update_screen(0); /* may need to update the screen */
5183 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005184 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185}
5186#endif
5187
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005188#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189/*
5190 * Get the text to use in a find/replace dialog. Uses the last search pattern
5191 * if the argument is empty.
5192 * Returns an allocated string.
5193 */
5194 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005195get_find_dialog_text(
5196 char_u *arg,
5197 int *wwordp, /* return: TRUE if \< \> found */
5198 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 char_u *text;
5201
5202 if (*arg == NUL)
5203 text = last_search_pat();
5204 else
5205 text = arg;
5206 if (text != NULL)
5207 {
5208 text = vim_strsave(text);
5209 if (text != NULL)
5210 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005211 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 int i;
5213
5214 /* Remove "\V" */
5215 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5216 {
5217 mch_memmove(text, text + 2, (size_t)(len - 1));
5218 len -= 2;
5219 }
5220
5221 /* Recognize "\c" and "\C" and remove. */
5222 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5223 {
5224 *mcasep = (text[1] == 'C');
5225 mch_memmove(text, text + 2, (size_t)(len - 1));
5226 len -= 2;
5227 }
5228
5229 /* Recognize "\<text\>" and remove. */
5230 if (len >= 4
5231 && STRNCMP(text, "\\<", 2) == 0
5232 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5233 {
5234 *wwordp = TRUE;
5235 mch_memmove(text, text + 2, (size_t)(len - 4));
5236 text[len - 4] = NUL;
5237 }
5238
5239 /* Recognize "\/" or "\?" and remove. */
5240 for (i = 0; i + 1 < len; ++i)
5241 if (text[i] == '\\' && (text[i + 1] == '/'
5242 || text[i + 1] == '?'))
5243 {
5244 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5245 --len;
5246 }
5247 }
5248 }
5249 return text;
5250}
5251
5252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 * Handle the press of a button in the find-replace dialog.
5254 * Return TRUE when something was added to the input buffer.
5255 */
5256 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005257gui_do_findrepl(
5258 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5259 char_u *find_text,
5260 char_u *repl_text,
5261 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262{
5263 garray_T ga;
5264 int i;
5265 int type = (flags & FRD_TYPE_MASK);
5266 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005267 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005268 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005269 static int busy = FALSE;
5270
5271 /* When the screen is being updated we should not change buffers and
5272 * windows structures, it may cause freed memory to be used. Also don't
5273 * do this recursively (pressing "Find" quickly several times. */
5274 if (updating_screen || busy)
5275 return FALSE;
5276
5277 /* refuse replace when text cannot be changed */
5278 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5279 return FALSE;
5280
5281 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005284 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 ga_concat(&ga, (char_u *)"%s/");
5286
5287 ga_concat(&ga, (char_u *)"\\V");
5288 if (flags & FRD_MATCH_CASE)
5289 ga_concat(&ga, (char_u *)"\\C");
5290 else
5291 ga_concat(&ga, (char_u *)"\\c");
5292 if (flags & FRD_WHOLE_WORD)
5293 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005294 /* escape / and \ */
5295 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5296 if (p != NULL)
5297 ga_concat(&ga, p);
5298 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (flags & FRD_WHOLE_WORD)
5300 ga_concat(&ga, (char_u *)"\\>");
5301
5302 if (type == FRD_REPLACEALL)
5303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005305 /* escape / and \ */
5306 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5307 if (p != NULL)
5308 ga_concat(&ga, p);
5309 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005311 }
5312 ga_append(&ga, NUL);
5313
5314 if (type == FRD_REPLACE)
5315 {
5316 /* Do the replacement when the text at the cursor matches. Thus no
5317 * replacement is done if the cursor was moved! */
5318 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5319 regmatch.rm_ic = 0;
5320 if (regmatch.regprog != NULL)
5321 {
5322 p = ml_get_cursor();
5323 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5324 && regmatch.startp[0] == p)
5325 {
5326 /* Clear the command line to remove any old "No match"
5327 * error. */
5328 msg_end_prompt();
5329
5330 if (u_save_cursor() == OK)
5331 {
5332 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005333 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005334
5335 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005336 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005337 ins_str(repl_text);
5338 }
5339 }
5340 else
5341 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005342 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005343 }
5344 }
5345
5346 if (type == FRD_REPLACEALL)
5347 {
5348 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005349 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 do_cmdline_cmd(ga.ga_data);
5351 }
5352 else
5353 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005354 int searchflags = SEARCH_MSG + SEARCH_MARK;
5355
5356 /* Search for the next match.
5357 * Don't skip text under cursor for single replace. */
5358 if (type == FRD_REPLACE)
5359 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005361 if (down)
5362 {
5363 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5364 }
5365 else
5366 {
5367 /* We need to escape '?' if and only if we are searching in the up
5368 * direction */
5369 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5370 if (p != NULL)
5371 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5372 vim_free(p);
5373 }
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 msg_scroll = i; /* don't let an error message set msg_scroll */
5376 }
5377
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005378 /* Don't want to pass did_emsg to other code, it may cause disabling
5379 * syntax HL if we were busy redrawing. */
5380 did_emsg = save_did_emsg;
5381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 if (State & (NORMAL | INSERT))
5383 {
5384 gui_update_screen(); /* update the screen */
5385 msg_didout = 0; /* overwrite any message */
5386 need_wait_return = FALSE; /* don't wait for return */
5387 }
5388
5389 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005390 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 return (ga.ga_len > 0);
5392}
5393
5394#endif
5395
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005396#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397/*
5398 * Jump to the window at specified point (x, y).
5399 */
5400 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005401gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 int row = Y_2_ROW(y);
5404 int col = X_2_COL(x);
5405 win_T *wp;
5406
5407 if (row >= 0 && col >= 0)
5408 {
5409 wp = mouse_find_win(&row, &col);
5410 if (wp != NULL && wp != curwin)
5411 win_goto(wp);
5412 }
5413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005416 * Function passed to handle_drop() for the actions to be done after the
5417 * argument list has been updated.
5418 */
5419 static void
5420drop_callback(void *cookie)
5421{
5422 char_u *p = cookie;
5423
5424 /* If Shift held down, change to first file's directory. If the first
5425 * item is a directory, change to that directory (and let the explorer
5426 * plugin show the contents). */
5427 if (p != NULL)
5428 {
5429 if (mch_isdir(p))
5430 {
5431 if (mch_chdir((char *)p) == 0)
5432 shorten_fnames(TRUE);
5433 }
5434 else if (vim_chdirfile(p, "drop") == OK)
5435 shorten_fnames(TRUE);
5436 vim_free(p);
5437 }
5438
5439 /* Update the screen display */
5440 update_screen(NOT_VALID);
5441# ifdef FEAT_MENU
5442 gui_update_menus(0);
5443# endif
5444#ifdef FEAT_TITLE
5445 maketitle();
5446#endif
5447 setcursor();
5448 out_flush_cursor(FALSE, FALSE);
5449}
5450
5451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 * Process file drop. Mouse cursor position, key modifiers, name of files
5453 * and count of files are given. Argument "fnames[count]" has full pathnames
5454 * of dropped files, they will be freed in this function, and caller can't use
5455 * fnames after call this function.
5456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005458gui_handle_drop(
5459 int x UNUSED,
5460 int y UNUSED,
5461 int_u modifiers,
5462 char_u **fnames,
5463 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 int i;
5466 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005467 static int entered = FALSE;
5468
5469 /*
5470 * This function is called by event handlers. Just in case we get a
5471 * second event before the first one is handled, ignore the second one.
5472 * Not sure if this can ever happen, just in case.
5473 */
5474 if (entered)
5475 return;
5476 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478 /*
5479 * When the cursor is at the command line, add the file names to the
5480 * command line, don't edit the files.
5481 */
5482 if (State & CMDLINE)
5483 {
5484 shorten_filenames(fnames, count);
5485 for (i = 0; i < count; ++i)
5486 {
5487 if (fnames[i] != NULL)
5488 {
5489 if (i > 0)
5490 add_to_input_buf((char_u*)" ", 1);
5491
5492 /* We don't know what command is used thus we can't be sure
5493 * about which characters need to be escaped. Only escape the
5494 * most common ones. */
5495# ifdef BACKSLASH_IN_FILENAME
5496 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5497# else
5498 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5499# endif
5500 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005501 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 vim_free(p);
5503 vim_free(fnames[i]);
5504 }
5505 }
5506 vim_free(fnames);
5507 }
5508 else
5509 {
5510 /* Go to the window under mouse cursor, then shorten given "fnames" by
5511 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 shorten_filenames(fnames, count);
5514
5515 /* If Shift held down, remember the first item. */
5516 if ((modifiers & MOUSE_SHIFT) != 0)
5517 p = vim_strsave(fnames[0]);
5518 else
5519 p = NULL;
5520
5521 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005522 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 * directory and Shift is held down. */
5524 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5525 && mch_isdir(fnames[0]))
5526 {
5527 vim_free(fnames[0]);
5528 vim_free(fnames);
5529 }
5530 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005531 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5532 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005534
5535 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537#endif