blob: a17c57112deb2d94387a5d8b4463fffad0a5a15e [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);
20static void gui_position_components(int);
21static void gui_outstr(char_u *, int);
22static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020023#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010024static int gui_screenstr(int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010026static void gui_delete_lines(int row, int count);
27static void gui_insert_lines(int row, int count);
28static void fill_mouse_coord(char_u *p, int col, int row);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010030static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000031#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010032static void gui_do_scrollbar(win_T *wp, int which, int enable);
33static colnr_T scroll_line_len(linenr_T lnum);
34static linenr_T gui_find_longest_lnum(void);
35static void gui_update_horiz_scrollbar(int);
36static void gui_set_fg_color(char_u *name);
37static void gui_set_bg_color(char_u *name);
38static win_T *xy2win(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaar05514102012-08-29 16:34:27 +020040#if defined(UNIX) && !defined(MACOS_X) && !defined(__APPLE__)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020041# define MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020043
Bram Moolenaard25c16e2016-01-29 22:13:30 +010044static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020045
46/* Return values for gui_read_child_pipe */
47enum {
48 GUI_CHILD_IO_ERROR,
49 GUI_CHILD_OK,
50 GUI_CHILD_FAILED
51};
52
53#endif /* MAY_FORK */
54
Bram Moolenaard25c16e2016-01-29 22:13:30 +010055static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020056
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static int can_update_cursor = TRUE; /* can display the cursor */
58
59/*
60 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
61 * this makes the thumb indicate the part of the text that is shown. Motif
62 * can't do this.
63 */
64#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
65# define SCROLL_PAST_END
66#endif
67
68/*
69 * gui_start -- Called when user wants to start the GUI.
70 *
71 * Careful: This function can be called recursively when there is a ":gui"
72 * command in the .gvimrc file. Only the first call should fork, not the
73 * recursive call.
74 */
75 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +010076gui_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 static int recursive = 0;
80
81 old_term = vim_strsave(T_NAME);
82
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 settmode(TMODE_COOK); /* stop RAW mode */
84 if (full_screen)
85 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 full_screen = FALSE;
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 ++recursive;
89
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090#ifdef MAY_FORK
91 /*
92 * Quit the current process and continue in the child.
93 * Makes "gvim file" disconnect from the shell it was started in.
94 * Don't do this when Vim was started with "-f" or the 'f' flag is present
95 * in 'guioptions'.
96 */
97 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
98 {
99 gui_do_fork();
100 }
101 else
102#endif
103 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200104#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200105 /* If there is 'f' in 'guioptions' and specify -g argument,
106 * gui_mch_init_check() was not called yet. */
107 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100108 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200109#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200110 gui_attempt_start();
111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
113 if (!gui.in_use) /* failed to start GUI */
114 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200115 /* Back to old term settings
116 *
117 * FIXME: If we got here because a child process failed and flagged to
118 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
119 * hit an X11 I/O error and do a longjmp(), leaving recursive
120 * permanently set to 1. This is probably not as big a problem as it
121 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
122 * return "OK" unconditionally, so it would be very difficult to
123 * actually hit this case.
124 */
125 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 settmode(TMODE_RAW); /* restart RAW mode */
127#ifdef FEAT_TITLE
128 set_title_defaults(); /* set 'title' and 'icon' again */
129#endif
130 }
131
132 vim_free(old_term);
133
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200134#ifdef FEAT_AUTOCMD
135 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
136 * the GUIFailed event. */
137 gui_mch_update();
138 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
139 NULL, NULL, FALSE, curbuf);
140#endif
141 --recursive;
142}
143
144/*
145 * Set_termname() will call gui_init() to start the GUI.
146 * Set the "starting" flag, to indicate that the GUI will start.
147 *
148 * We don't want to open the GUI shell until after we've read .gvimrc,
149 * otherwise we don't know what font we will use, and hence we don't know
150 * what size the shell should be. So if there are errors in the .gvimrc
151 * file, they will have to go to the terminal: Set full_screen to FALSE.
152 * full_screen will be set to TRUE again by a successful termcapinit().
153 */
154 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100155gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200156{
157 static int recursive = 0;
158
159 ++recursive;
160 gui.starting = TRUE;
161
162#ifdef FEAT_GUI_GTK
163 gui.event_time = GDK_CURRENT_TIME;
164#endif
165
166 termcapinit((char_u *)"builtin_gui");
167 gui.starting = recursive - 1;
168
Bram Moolenaar9372a112005-12-06 19:59:18 +0000169#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200171 {
172# ifdef FEAT_EVAL
173 Window x11_window;
174 Display *x11_display;
175
176 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
177 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
178# endif
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 /* Display error messages in a dialog now. */
181 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 --recursive;
185}
186
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200187#ifdef MAY_FORK
188
189/* for waitpid() */
190# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
191# include <sys/wait.h>
192# endif
193
194/*
195 * Create a new process, by forking. In the child, start the GUI, and in
196 * the parent, exit.
197 *
198 * If something goes wrong, this will return with gui.in_use still set
199 * to FALSE, in which case the caller should continue execution without
200 * the GUI.
201 *
202 * If the child fails to start the GUI, then the child will exit and the
203 * parent will return. If the child succeeds, then the parent will exit
204 * and the child will return.
205 */
206 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100207gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200208{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200209 int pipefd[2]; /* pipe between parent and child */
210 int pipe_error;
211 int status;
212 int exit_status;
213 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200214
215 /* Setup a pipe between the child and the parent, so that the parent
216 * knows when the child has done the setsid() call and is allowed to
217 * exit. */
218 pipe_error = (pipe(pipefd) < 0);
219 pid = fork();
220 if (pid < 0) /* Fork error */
221 {
222 EMSG(_("E851: Failed to create a new process for the GUI"));
223 return;
224 }
225 else if (pid > 0) /* Parent */
226 {
227 /* Give the child some time to do the setsid(), otherwise the
228 * exit() may kill the child too (when starting gvim from inside a
229 * gvim). */
230 if (!pipe_error)
231 {
232 /* The read returns when the child closes the pipe (or when
233 * the child dies for some reason). */
234 close(pipefd[1]);
235 status = gui_read_child_pipe(pipefd[0]);
236 if (status == GUI_CHILD_FAILED)
237 {
238 /* The child failed to start the GUI, so the caller must
239 * continue. There may be more error information written
240 * to stderr by the child. */
241# ifdef __NeXT__
242 wait4(pid, &exit_status, 0, (struct rusage *)0);
243# else
244 waitpid(pid, &exit_status, 0);
245# endif
246 EMSG(_("E852: The child process failed to start the GUI"));
247 return;
248 }
249 else if (status == GUI_CHILD_IO_ERROR)
250 {
251 pipe_error = TRUE;
252 }
253 /* else GUI_CHILD_OK: parent exit */
254 }
255
256 if (pipe_error)
257 ui_delay(300L, TRUE);
258
259 /* When swapping screens we may need to go to the next line, e.g.,
260 * after a hit-enter prompt and using ":gui". */
261 if (newline_on_exit)
262 mch_errmsg("\r\n");
263
264 /*
265 * The parent must skip the normal exit() processing, the child
266 * will do it. For example, GTK messes up signals when exiting.
267 */
268 _exit(0);
269 }
270 /* Child */
271
Bram Moolenaar29695702012-05-18 17:03:18 +0200272#ifdef FEAT_GUI_GTK
273 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
274 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100275 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200276#endif
277
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200278# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
279 /*
280 * Change our process group. On some systems/shells a CTRL-C in the
281 * shell where Vim was started would otherwise kill gvim!
282 */
283# if defined(HAVE_SETSID)
284 (void)setsid();
285# else
286 (void)setpgid(0, 0);
287# endif
288# endif
289 if (!pipe_error)
290 close(pipefd[0]);
291
292# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
293 /* Tell the session manager our new PID */
294 gui_mch_forked();
295# endif
296
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200297 /* Try to start the GUI */
298 gui_attempt_start();
299
300 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200301 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200302 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200303 if (gui.in_use)
304 write_eintr(pipefd[1], "ok", 3);
305 else
306 write_eintr(pipefd[1], "fail", 5);
307 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200308 }
309
310 /* If we failed to start the GUI, exit now. */
311 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100312 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200313}
314
315/*
316 * Read from a pipe assumed to be connected to the child process (this
317 * function is called from the parent).
318 * Return GUI_CHILD_OK if the child successfully started the GUI,
319 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
320 * some other error.
321 *
322 * The file descriptor will be closed before the function returns.
323 */
324 static int
325gui_read_child_pipe(int fd)
326{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 long bytes_read;
328#define READ_BUFFER_SIZE 10
329 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200330
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200331 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
332#undef READ_BUFFER_SIZE
333 close(fd);
334 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200335 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200336 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337 if (strcmp(buffer, "ok") == 0)
338 return GUI_CHILD_OK;
339 return GUI_CHILD_FAILED;
340}
341
342#endif /* MAY_FORK */
343
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344/*
345 * Call this when vim starts up, whether or not the GUI is started
346 */
347 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100348gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349{
350 gui.in_use = FALSE; /* No GUI yet (maybe later) */
351 gui.starting = FALSE; /* No GUI yet (maybe later) */
352 gui_mch_prepare(argc, argv);
353}
354
355/*
356 * Try initializing the GUI and check if it can be started.
357 * Used from main() to check early if "vim -g" can start the GUI.
358 * Used from gui_init() to prepare for starting the GUI.
359 * Returns FAIL or OK.
360 */
361 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100362gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364 static int result = MAYBE;
365
366 if (result != MAYBE)
367 {
368 if (result == FAIL)
369 EMSG(_("E229: Cannot start the GUI"));
370 return result;
371 }
372
373 gui.shell_created = FALSE;
374 gui.dying = FALSE;
375 gui.in_focus = TRUE; /* so the guicursor setting works */
376 gui.dragged_sb = SBAR_NONE;
377 gui.dragged_wp = NULL;
378 gui.pointer_hidden = FALSE;
379 gui.col = 0;
380 gui.row = 0;
381 gui.num_cols = Columns;
382 gui.num_rows = Rows;
383
384 gui.cursor_is_valid = FALSE;
385 gui.scroll_region_top = 0;
386 gui.scroll_region_bot = Rows - 1;
387 gui.scroll_region_left = 0;
388 gui.scroll_region_right = Columns - 1;
389 gui.highlight_mask = HL_NORMAL;
390 gui.char_width = 1;
391 gui.char_height = 1;
392 gui.char_ascent = 0;
393 gui.border_width = 0;
394
395 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200396#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 gui.bold_font = NOFONT;
398 gui.ital_font = NOFONT;
399 gui.boldital_font = NOFONT;
400# ifdef FEAT_XFONTSET
401 gui.fontset = NOFONTSET;
402# endif
403#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200404#ifdef FEAT_MBYTE
405 gui.wide_font = NOFONT;
406# ifndef FEAT_GUI_GTK
407 gui.wide_bold_font = NOFONT;
408 gui.wide_ital_font = NOFONT;
409 gui.wide_boldital_font = NOFONT;
410# endif
411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412
413#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200414# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415# ifdef FONTSET_ALWAYS
416 gui.menu_fontset = NOFONTSET;
417# else
418 gui.menu_font = NOFONT;
419# endif
420# endif
421 gui.menu_is_active = TRUE; /* default: include menu */
422# ifndef FEAT_GUI_GTK
423 gui.menu_height = MENU_DEFAULT_HEIGHT;
424 gui.menu_width = 0;
425# endif
426#endif
427#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
428 gui.toolbar_height = 0;
429#endif
430#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
431 gui.footer_height = 0;
432#endif
433#ifdef FEAT_BEVAL_TIP
434 gui.tooltip_fontset = NOFONTSET;
435#endif
436
437 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
438 gui.prev_wrap = -1;
439
440#ifdef ALWAYS_USE_GUI
441 result = OK;
442#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200443# ifdef FEAT_GUI_GTK
444 /*
445 * Note: Don't call gtk_init_check() before fork, it will be called after
446 * the fork. When calling it before fork, it make vim hang for a while.
447 * See gui_do_fork().
448 * Use a simpler check if the GUI window can probably be opened.
449 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200450 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200451# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#endif
455 return result;
456}
457
458/*
459 * This is the call which starts the GUI.
460 */
461 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100462gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463{
464 win_T *wp;
465 static int recursive = 0;
466
467 /*
468 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
469 * function will then be executed at the first call, the rest by the
470 * recursive call. This allow the shell to be opened halfway reading a
471 * gvimrc file.
472 */
473 if (!recursive)
474 {
475 ++recursive;
476
477 clip_init(TRUE);
478
479 /* If can't initialize, don't try doing the rest */
480 if (gui_init_check() == FAIL)
481 {
482 --recursive;
483 clip_init(FALSE);
484 return;
485 }
486
487 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000488 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
489 * breaks the Paste toolbar button.
490 */
491 set_option_value((char_u *)"paste", 0L, NULL, 0);
492
493 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 * Set up system-wide default menus.
495 */
496#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
497 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
498 {
499 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000500 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 sys_menu = FALSE;
502 }
503#endif
504
505 /*
506 * Switch on the mouse by default, unless the user changed it already.
507 * This can then be changed in the .gvimrc.
508 */
509 if (!option_was_set((char_u *)"mouse"))
510 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000511 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
513 /*
514 * If -U option given, use only the initializations from that file and
515 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
516 */
517 if (use_gvimrc != NULL)
518 {
519 if (STRCMP(use_gvimrc, "NONE") != 0
520 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000521 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
523 }
524 else
525 {
526 /*
527 * Get system wide defaults for gvim, only when file name defined.
528 */
529#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000530 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531#endif
532
533 /*
534 * Try to read GUI initialization commands from the following
535 * places:
536 * - environment variable GVIMINIT
537 * - the user gvimrc file (~/.gvimrc)
538 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
539 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
540 * The first that exists is used, the rest is ignored.
541 */
542 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000543 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
544 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000546 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
547 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200549#ifdef USR_GVIMRC_FILE3
550 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
551 DOSO_GVIMRC) == FAIL
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 )
554 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200555#ifdef USR_GVIMRC_FILE4
556 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558 }
559
560 /*
561 * Read initialization commands from ".gvimrc" in current
562 * directory. This is only done if the 'exrc' option is set.
563 * Because of security reasons we disallow shell and write
564 * commands now, except for unix if the file is owned by the user
565 * or 'secure' option has been reset in environment of global
566 * ".gvimrc".
567 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
568 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
569 */
570 if (p_exrc)
571 {
572#ifdef UNIX
573 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200574 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 /* if ".gvimrc" file is not owned by user, set 'secure'
577 * mode */
578 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
579 secure = p_secure;
580 }
581#else
582 secure = p_secure;
583#endif
584
585 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
586 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
587#ifdef SYS_GVIMRC_FILE
588 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
589 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
590#endif
591#ifdef USR_GVIMRC_FILE2
592 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
593 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
594#endif
595#ifdef USR_GVIMRC_FILE3
596 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
597 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
598#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200599#ifdef USR_GVIMRC_FILE4
600 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
601 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000604 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 if (secure == 2)
607 need_wait_return = TRUE;
608 secure = 0;
609 }
610 }
611
612 if (need_wait_return || msg_didany)
613 wait_return(TRUE);
614
615 --recursive;
616 }
617
618 /* If recursive call opened the shell, return here from the first call */
619 if (gui.in_use)
620 return;
621
622 /*
623 * Create the GUI shell.
624 */
625 gui.in_use = TRUE; /* Must be set after menus have been set up */
626 if (gui_mch_init() == FAIL)
627 goto error;
628
629 /* Avoid a delay for an error message that was printed in the terminal
630 * where Vim was started. */
631 emsg_on_display = FALSE;
632 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100633 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 need_wait_return = FALSE;
635 msg_didany = FALSE;
636
637 /*
638 * Check validity of any generic resources that may have been loaded.
639 */
640 if (gui.border_width < 0)
641 gui.border_width = 0;
642
643 /*
644 * Set up the fonts. First use a font specified with "-fn" or "-font".
645 */
646 if (font_argument != NULL)
647 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
648 if (
649#ifdef FEAT_XFONTSET
650 (*p_guifontset == NUL
651 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
652#endif
653 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
654 : p_guifont, FALSE) == FAIL)
655 {
656 EMSG(_("E665: Cannot start GUI, no valid font found"));
657 goto error2;
658 }
659#ifdef FEAT_MBYTE
660 if (gui_get_wide_font() == FAIL)
661 EMSG(_("E231: 'guifontwide' invalid"));
662#endif
663
664 gui.num_cols = Columns;
665 gui.num_rows = Rows;
666 gui_reset_scroll_region();
667
668 /* Create initial scrollbars */
669 FOR_ALL_WINDOWS(wp)
670 {
671 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
672 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
673 }
674 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
675
676#ifdef FEAT_MENU
677 gui_create_initial_menus(root_menu);
678#endif
679#ifdef FEAT_SUN_WORKSHOP
680 if (usingSunWorkShop)
681 workshop_init();
682#endif
683#ifdef FEAT_SIGN_ICONS
684 sign_gui_started();
685#endif
686
687 /* Configure the desired menu and scrollbars */
688 gui_init_which_components(NULL);
689
690 /* All components of the GUI have been created now */
691 gui.shell_created = TRUE;
692
693#ifndef FEAT_GUI_GTK
694 /* Set the shell size, adjusted for the screen size. For GTK this only
695 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000696 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#endif
698#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
699 /* Need to set the size of the menubar after all the menus have been
700 * created. */
701 gui_mch_compute_menu_height((Widget)0);
702#endif
703
704 /*
705 * Actually open the GUI shell.
706 */
707 if (gui_mch_open() != FAIL)
708 {
709#ifdef FEAT_TITLE
710 maketitle();
711 resettitle();
712#endif
713 init_gui_options();
714#ifdef FEAT_ARABIC
715 /* Our GUI can't do bidi. */
716 p_tbidi = FALSE;
717#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000718#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 /* Give GTK+ a chance to put all widget's into place. */
720 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000721
722# ifdef FEAT_MENU
723 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
724 * It was still there to make F10 work. */
725 if (vim_strchr(p_go, GO_MENUS) == NULL)
726 {
727 --gui.starting;
728 gui_mch_enable_menu(FALSE);
729 ++gui.starting;
730 gui_mch_update();
731 }
732# endif
733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000735 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000737 /* When 'lines' was set while starting up the topframe may have to be
738 * resized. */
739 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000740
741#ifdef FEAT_BEVAL
742 /* Always create the Balloon Evaluation area, but disable it when
743 * 'ballooneval' is off */
744# ifdef FEAT_GUI_GTK
745 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
746 &general_beval_cb, NULL);
747# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000748# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000749 {
750 extern Widget textArea;
751 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000752 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000753 }
754# else
755# ifdef FEAT_GUI_W32
756 balloonEval = gui_mch_create_beval_area(NULL, NULL,
757 &general_beval_cb, NULL);
758# endif
759# endif
760# endif
761 if (!p_beval)
762 gui_mch_disable_beval_area(balloonEval);
763#endif
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
766 if (!im_xim_isvalid_imactivate())
767 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
768#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000769 /* When 'cmdheight' was set during startup it may not have taken
770 * effect yet. */
771 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000772 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 return;
775 }
776
777error2:
778#ifdef FEAT_GUI_X11
779 /* undo gui_mch_init() */
780 gui_mch_uninit();
781#endif
782
783error:
784 gui.in_use = FALSE;
785 clip_init(FALSE);
786}
787
788
789 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100790gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 /* don't free the fonts, it leads to a BUS error
793 * richard@whitequeen.com Jul 99 */
794 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 gui.in_use = FALSE;
796 gui_mch_exit(rc);
797}
798
Bram Moolenaar9372a112005-12-06 19:59:18 +0000799#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000801# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802/*
803 * Called when the GUI shell is closed by the user. If there are no changed
804 * files Vim exits, otherwise there will be a dialog to ask the user what to
805 * do.
806 * When this function returns, Vim should NOT exit!
807 */
808 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100809gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 cmdmod_T save_cmdmod;
812
813 save_cmdmod = cmdmod;
814
815 /* Only exit when there are no changed files */
816 exiting = TRUE;
817# ifdef FEAT_BROWSE
818 cmdmod.browse = TRUE;
819# endif
820# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
821 cmdmod.confirm = TRUE;
822# endif
823 /* If there are changed buffers, present the user with a dialog if
824 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100825 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 getout(0);
827
828 exiting = FALSE;
829 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000830 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832#endif
833
834/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200835 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * first font name that works is used. If none is found, use the default
837 * font.
838 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
839 * Return OK when able to set the font. When it failed FAIL is returned and
840 * the fonts are unchanged.
841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100843gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
845#define FONTLEN 320
846 char_u font_name[FONTLEN];
847 int font_list_empty = FALSE;
848 int ret = FAIL;
849
850 if (!gui.in_use)
851 return FAIL;
852
853 font_name[0] = NUL;
854 if (*font_list == NUL)
855 font_list_empty = TRUE;
856 else
857 {
858#ifdef FEAT_XFONTSET
859 /* When using a fontset, the whole list of fonts is one name. */
860 if (fontset)
861 ret = gui_mch_init_font(font_list, TRUE);
862 else
863#endif
864 while (*font_list != NUL)
865 {
866 /* Isolate one comma separated font name. */
867 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
868
869 /* Careful!!! The Win32 version of gui_mch_init_font(), when
870 * called with "*" will change p_guifont to the selected font
871 * name, which frees the old value. This makes font_list
872 * invalid. Thus when OK is returned here, font_list must no
873 * longer be used! */
874 if (gui_mch_init_font(font_name, FALSE) == OK)
875 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200876#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 /* If it's a Unicode font, try setting 'guifontwide' to a
878 * similar double-width font. */
879 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
880 && strstr((char *)font_name, "10646") != NULL)
881 set_guifontwide(font_name);
882#endif
883 ret = OK;
884 break;
885 }
886 }
887 }
888
889 if (ret != OK
890 && STRCMP(font_list, "*") != 0
891 && (font_list_empty || gui.norm_font == NOFONT))
892 {
893 /*
894 * Couldn't load any font in 'font_list', keep the current font if
895 * there is one. If 'font_list' is empty, or if there is no current
896 * font, tell gui_mch_init_font() to try to find a font we can load.
897 */
898 ret = gui_mch_init_font(NULL, FALSE);
899 }
900
901 if (ret == OK)
902 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200903#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 /* Set normal font as current font */
905# ifdef FEAT_XFONTSET
906 if (gui.fontset != NOFONTSET)
907 gui_mch_set_fontset(gui.fontset);
908 else
909# endif
910 gui_mch_set_font(gui.norm_font);
911#endif
Bram Moolenaarb0316262012-11-20 12:03:06 +0100912 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914
915 return ret;
916}
917
918#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200919# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920/*
921 * Try setting 'guifontwide' to a font twice as wide as "name".
922 */
923 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100924set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 int i = 0;
927 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
928 char_u *wp = NULL;
929 char_u *p;
930 GuiFont font;
931
932 wp = wide_name;
933 for (p = name; *p != NUL; ++p)
934 {
935 *wp++ = *p;
936 if (*p == '-')
937 {
938 ++i;
939 if (i == 6) /* font type: change "--" to "-*-" */
940 {
941 if (p[1] == '-')
942 *wp++ = '*';
943 }
944 else if (i == 12) /* found the width */
945 {
946 ++p;
947 i = getdigits(&p);
948 if (i != 0)
949 {
950 /* Double the width specification. */
951 sprintf((char *)wp, "%d%s", i * 2, p);
952 font = gui_mch_get_font(wide_name, FALSE);
953 if (font != NOFONT)
954 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000955 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 gui.wide_font = font;
957 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000958 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960 }
961 break;
962 }
963 }
964 }
965}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200966# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968/*
969 * Get the font for 'guifontwide'.
970 * Return FAIL for an invalid font name.
971 */
972 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100973gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
975 GuiFont font = NOFONT;
976 char_u font_name[FONTLEN];
977 char_u *p;
978
979 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
980 return OK; /* Will give an error message later. */
981
982 if (p_guifontwide != NULL && *p_guifontwide != NUL)
983 {
984 for (p = p_guifontwide; *p != NUL; )
985 {
986 /* Isolate one comma separated font name. */
987 (void)copy_option_part(&p, font_name, FONTLEN, ",");
988 font = gui_mch_get_font(font_name, FALSE);
989 if (font != NOFONT)
990 break;
991 }
992 if (font == NOFONT)
993 return FAIL;
994 }
995
996 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +0200997# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
999 if (font != NOFONT && gui.norm_font != NOFONT
1000 && pango_font_description_equal(font, gui.norm_font))
1001 {
1002 gui.wide_font = NOFONT;
1003 gui_mch_free_font(font);
1004 }
1005 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001008# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001009 gui_mch_wide_font_changed();
Bram Moolenaardb250522013-06-17 22:43:25 +02001010# else
1011 /*
1012 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1013 * support those fonts for 'guifontwide'.
1014 */
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return OK;
1017}
1018#endif
1019
1020 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001021gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 gui.row = row;
1024 gui.col = col;
1025}
1026
1027/*
1028 * gui_check_pos - check if the cursor is on the screen.
1029 */
1030 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001031gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
1033 if (gui.row >= screen_Rows)
1034 gui.row = screen_Rows - 1;
1035 if (gui.col >= screen_Columns)
1036 gui.col = screen_Columns - 1;
1037 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1038 gui.cursor_is_valid = FALSE;
1039}
1040
1041/*
1042 * Redraw the cursor if necessary or when forced.
1043 * Careful: The contents of ScreenLines[] must match what is on the screen,
1044 * otherwise this goes wrong. May need to call out_flush() first.
1045 */
1046 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001047gui_update_cursor(
1048 int force, /* when TRUE, update even when not moved */
1049 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051 int cur_width = 0;
1052 int cur_height = 0;
1053 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001054 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001056#ifdef FEAT_TERMINAL
1057 guicolor_T shape_fg = INVALCOLOR;
1058 guicolor_T shape_bg = INVALCOLOR;
1059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1061 int cattr; /* cursor attributes */
1062 int attr;
1063 attrentry_T *aep = NULL;
1064
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001065 /* Don't update the cursor when halfway busy scrolling or the screen size
1066 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1067 if (!can_update_cursor || screen_Columns != gui.num_cols
1068 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 return;
1070
1071 gui_check_pos();
1072 if (!gui.cursor_is_valid || force
1073 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1074 {
1075 gui_undraw_cursor();
1076 if (gui.row < 0)
1077 return;
1078#ifdef USE_IM_CONTROL
1079 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1080 im_set_position(gui.row, gui.col);
1081#endif
1082 gui.cursor_row = gui.row;
1083 gui.cursor_col = gui.col;
1084
1085 /* Only write to the screen after ScreenLines[] has been initialized */
1086 if (!screen_cleared || ScreenLines == NULL)
1087 return;
1088
1089 /* Clear the selection if we are about to write over it */
1090 if (clear_selection)
1091 clip_may_clear_selection(gui.row, gui.row);
1092 /* Check that the cursor is inside the shell (resizing may have made
1093 * it invalid) */
1094 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1095 return;
1096
1097 gui.cursor_is_valid = TRUE;
1098
1099 /*
1100 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001101 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001103#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001104 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001105 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001107#endif
1108 shape = &shape_table[get_shape_idx(FALSE)];
1109 if (State & LANGMAP)
1110 id = shape->id_lm;
1111 else
1112 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
1114 /* get the colors and attributes for the cursor. Default is inverted */
1115 cfg = INVALCOLOR;
1116 cbg = INVALCOLOR;
1117 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001118 gui_mch_set_blinking(shape->blinkwait,
1119 shape->blinkon,
1120 shape->blinkoff);
1121#ifdef FEAT_TERMINAL
1122 if (shape_bg != INVALCOLOR)
1123 {
1124 cattr = 0;
1125 cfg = shape_fg;
1126 cbg = shape_bg;
1127 }
1128 else
1129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (id > 0)
1131 {
1132 cattr = syn_id2colors(id, &cfg, &cbg);
1133#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1134 {
1135 static int iid;
1136 guicolor_T fg, bg;
1137
Bram Moolenaarc236c162008-07-13 17:41:49 +00001138 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001139# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001140 preedit_get_status()
1141# else
1142 im_get_status()
1143# endif
1144 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 iid = syn_name2id((char_u *)"CursorIM");
1147 if (iid > 0)
1148 {
1149 syn_id2colors(iid, &fg, &bg);
1150 if (bg != INVALCOLOR)
1151 cbg = bg;
1152 if (fg != INVALCOLOR)
1153 cfg = fg;
1154 }
1155 }
1156 }
1157#endif
1158 }
1159
1160 /*
1161 * Get the attributes for the character under the cursor.
1162 * When no cursor color was given, use the character color.
1163 */
1164 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1165 if (attr > HL_ALL)
1166 aep = syn_gui_attr2entry(attr);
1167 if (aep != NULL)
1168 {
1169 attr = aep->ae_attr;
1170 if (cfg == INVALCOLOR)
1171 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1172 : aep->ae_u.gui.fg_color);
1173 if (cbg == INVALCOLOR)
1174 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1175 : aep->ae_u.gui.bg_color);
1176 }
1177 if (cfg == INVALCOLOR)
1178 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1179 if (cbg == INVALCOLOR)
1180 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1181
1182#ifdef FEAT_XIM
1183 if (aep != NULL)
1184 {
1185 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1186 : aep->ae_u.gui.bg_color);
1187 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1188 : aep->ae_u.gui.fg_color);
1189 if (xim_bg_color == INVALCOLOR)
1190 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1191 : gui.back_pixel;
1192 if (xim_fg_color == INVALCOLOR)
1193 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1194 : gui.norm_pixel;
1195 }
1196 else
1197 {
1198 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1199 : gui.back_pixel;
1200 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1201 : gui.norm_pixel;
1202 }
1203#endif
1204
1205 attr &= ~HL_INVERSE;
1206 if (cattr & HL_INVERSE)
1207 {
1208 cc = cbg;
1209 cbg = cfg;
1210 cfg = cc;
1211 }
1212 cattr &= ~HL_INVERSE;
1213
1214 /*
1215 * When we don't have window focus, draw a hollow cursor.
1216 */
1217 if (!gui.in_focus)
1218 {
1219 gui_mch_draw_hollow_cursor(cbg);
1220 return;
1221 }
1222
1223 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001224 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#ifdef FEAT_HANGULIN
1226 || composing_hangul
1227#endif
1228 )
1229 {
1230 /*
1231 * Draw the text character with the cursor colors. Use the
1232 * character attributes plus the cursor attributes.
1233 */
1234 gui.highlight_mask = (cattr | attr);
1235#ifdef FEAT_HANGULIN
1236 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001237 {
1238 char_u *comp_buf;
1239 int comp_len;
1240
1241 comp_buf = hangul_composing_buffer_get(&comp_len);
1242 if (comp_buf)
1243 {
1244 (void)gui_outstr_nowrap(comp_buf, comp_len,
1245 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1246 cfg, cbg, 0);
1247 vim_free(comp_buf);
1248 }
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 else
1251#endif
1252 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1253 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1254 }
1255 else
1256 {
1257#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1258 int col_off = FALSE;
1259#endif
1260 /*
1261 * First draw the partial cursor, then overwrite with the text
1262 * character, using a transparent background.
1263 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001264 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
1266 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001267 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 else
1270 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001271 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 cur_width = gui.char_width;
1273 }
1274#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001275 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1276 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
1278 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001279 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 cur_width += gui.char_width;
1281# ifdef FEAT_RIGHTLEFT
1282 if (CURSOR_BAR_RIGHT)
1283 {
1284 /* gui.col points to the left halve of the character but
1285 * the vertical line needs to be on the right halve.
1286 * A double-wide horizontal line is also drawn from the
1287 * right halve in gui_mch_draw_part_cursor(). */
1288 col_off = TRUE;
1289 ++gui.col;
1290 }
1291# endif
1292 }
1293#endif
1294 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1295#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1296 if (col_off)
1297 --gui.col;
1298#endif
1299
1300#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1301 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1302 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1303 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1304 (guicolor_T)0, (guicolor_T)0, 0);
1305#endif
1306 }
1307 gui.highlight_mask = old_hl_mask;
1308 }
1309}
1310
1311#if defined(FEAT_MENU) || defined(PROTO)
1312 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001313gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001315# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (gui.menu_is_active && gui.in_use)
1317 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1318# endif
1319}
1320#endif
1321
1322/*
1323 * Position the various GUI components (text area, menu). The vertical
1324 * scrollbars are NOT handled here. See gui_update_scrollbars().
1325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001327gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 int text_area_x;
1330 int text_area_y;
1331 int text_area_width;
1332 int text_area_height;
1333
1334 /* avoid that moving components around generates events */
1335 ++hold_gui_events;
1336
1337 text_area_x = 0;
1338 if (gui.which_scrollbars[SBAR_LEFT])
1339 text_area_x += gui.scrollbar_width;
1340
1341 text_area_y = 0;
1342#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1343 gui.menu_width = total_width;
1344 if (gui.menu_is_active)
1345 text_area_y += gui.menu_height;
1346#endif
1347#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1348 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1349 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1350#endif
1351
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001352# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001353 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001354 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001355 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1359 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1360 {
1361# ifdef FEAT_GUI_ATHENA
1362 gui_mch_set_toolbar_pos(0, text_area_y,
1363 gui.menu_width, gui.toolbar_height);
1364# endif
1365 text_area_y += gui.toolbar_height;
1366 }
1367#endif
1368
1369 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1370 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1371
1372 gui_mch_set_text_area_pos(text_area_x,
1373 text_area_y,
1374 text_area_width,
1375 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001376#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 + xim_get_status_area_height()
1378#endif
1379 );
1380#ifdef FEAT_MENU
1381 gui_position_menu();
1382#endif
1383 if (gui.which_scrollbars[SBAR_BOTTOM])
1384 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1385 text_area_x,
1386 text_area_y + text_area_height,
1387 text_area_width,
1388 gui.scrollbar_height);
1389 gui.left_sbar_x = 0;
1390 gui.right_sbar_x = text_area_x + text_area_width;
1391
1392 --hold_gui_events;
1393}
1394
Bram Moolenaar02743632005-07-25 20:42:36 +00001395/*
1396 * Get the width of the widgets and decorations to the side of the text area.
1397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001399gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 int base_width;
1402
1403 base_width = 2 * gui.border_offset;
1404 if (gui.which_scrollbars[SBAR_LEFT])
1405 base_width += gui.scrollbar_width;
1406 if (gui.which_scrollbars[SBAR_RIGHT])
1407 base_width += gui.scrollbar_width;
1408 return base_width;
1409}
1410
Bram Moolenaar02743632005-07-25 20:42:36 +00001411/*
1412 * Get the height of the widgets and decorations above and below the text area.
1413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001415gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 int base_height;
1418
1419 base_height = 2 * gui.border_offset;
1420 if (gui.which_scrollbars[SBAR_BOTTOM])
1421 base_height += gui.scrollbar_height;
1422#ifdef FEAT_GUI_GTK
1423 /* We can't take the sizes properly into account until anything is
1424 * realized. Therefore we recalculate all the values here just before
1425 * setting the size. (--mdcki) */
1426#else
1427# ifdef FEAT_MENU
1428 if (gui.menu_is_active)
1429 base_height += gui.menu_height;
1430# endif
1431# ifdef FEAT_TOOLBAR
1432 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1433# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1434 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1435# else
1436 base_height += gui.toolbar_height;
1437# endif
1438# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001439# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1440 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001441 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001442 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444# ifdef FEAT_FOOTER
1445 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1446 base_height += gui.footer_height;
1447# endif
1448# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1449 base_height += gui_mch_text_area_extra_height();
1450# endif
1451#endif
1452 return base_height;
1453}
1454
1455/*
1456 * Should be called after the GUI shell has been resized. Its arguments are
1457 * the new width and height of the shell in pixels.
1458 */
1459 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001460gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 static int busy = FALSE;
1463
1464 if (!gui.shell_created) /* ignore when still initializing */
1465 return;
1466
1467 /*
1468 * Can't resize the screen while it is being redrawn. Remember the new
1469 * size and handle it later.
1470 */
1471 if (updating_screen || busy)
1472 {
1473 new_pixel_width = pixel_width;
1474 new_pixel_height = pixel_height;
1475 return;
1476 }
1477
1478again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001479 new_pixel_width = 0;
1480 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 busy = TRUE;
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 /* Flush pending output before redrawing */
1484 out_flush();
1485
1486 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001487 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 /*
1493 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1494 * at the last line here (why does it have to be one row too low?).
1495 */
1496 if (State == ASKMORE || State == CONFIRM)
1497 gui.row = gui.num_rows;
1498
1499 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1500 * the safe side. */
1501 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1502 || gui.num_rows != Rows || gui.num_cols != Columns)
1503 shell_resized();
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 gui_update_scrollbars(TRUE);
1506 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001507#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 xim_set_status_area();
1509#endif
1510
1511 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001512
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001513 /* We may have been called again while redrawing the screen.
1514 * Need to do it all again with the latest size then. But only if the size
1515 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (new_pixel_height)
1517 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001518 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1519 {
1520 new_pixel_width = 0;
1521 new_pixel_height = 0;
1522 }
1523 else
1524 {
1525 pixel_width = new_pixel_width;
1526 pixel_height = new_pixel_height;
1527 goto again;
1528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530}
1531
1532/*
1533 * Check if gui_resize_shell() must be called.
1534 */
1535 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001536gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 /* careful: gui_resize_shell() may postpone the resize again if we
1540 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001541 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542}
1543
1544 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001545gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 Rows = gui.num_rows;
1548 Columns = gui.num_cols;
1549 return OK;
1550}
1551
1552/*
1553 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001554 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1555 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001558gui_set_shellsize(
1559 int mustset UNUSED, /* set by the user */
1560 int fit_to_display,
1561 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 int base_width;
1564 int base_height;
1565 int width;
1566 int height;
1567 int min_width;
1568 int min_height;
1569 int screen_w;
1570 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001571#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001572 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001573 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001574#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001575 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 if (!gui.shell_created)
1578 return;
1579
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001580#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 /* If not setting to a user specified size and maximized, calculate the
1582 * number of characters that fit in the maximized window. */
1583 if (!mustset && gui_mch_maximized())
1584 {
1585 gui_mch_newfont();
1586 return;
1587 }
1588#endif
1589
1590 base_width = gui_get_base_width();
1591 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001592 if (fit_to_display)
1593 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001594 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596#ifdef USE_SUN_WORKSHOP
1597 if (!mustset && usingSunWorkShop
1598 && workshop_get_width_height(&width, &height))
1599 {
1600 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1601 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1602 }
1603 else
1604#endif
1605 {
1606 width = Columns * gui.char_width + base_width;
1607 height = Rows * gui.char_height + base_height;
1608 }
1609
1610 if (fit_to_display)
1611 {
1612 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001613 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 Columns = (screen_w - base_width) / gui.char_width;
1616 if (Columns < MIN_COLUMNS)
1617 Columns = MIN_COLUMNS;
1618 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001619#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001620 ++did_adjust;
1621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001623 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 Rows = (screen_h - base_height) / gui.char_height;
1626 check_shellsize();
1627 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001628#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001629 ++did_adjust;
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001632#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001633 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1634 && height + gui.char_height >= screen_h))
1635 /* don't unmaximize if at maximum size */
1636 un_maximize = FALSE;
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001639 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 gui.num_cols = Columns;
1641 gui.num_rows = Rows;
1642
1643 min_width = base_width + MIN_COLUMNS * gui.char_width;
1644 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001645 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001646
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001647#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001648 if (un_maximize)
1649 {
1650 /* If the window size is smaller than the screen unmaximize the
1651 * window, otherwise resizing won't work. */
1652 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1653 if ((width + gui.char_width < screen_w
1654 || height + gui.char_height * 2 < screen_h)
1655 && gui_mch_maximized())
1656 gui_mch_unmaximize();
1657 }
1658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001661 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001663 if (fit_to_display && x >= 0 && y >= 0)
1664 {
1665 /* Some window managers put the Vim window left of/above the screen.
1666 * Only change the position if it wasn't already negative before
1667 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 gui_mch_update();
1669 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1670 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1671 }
1672
1673 gui_position_components(width);
1674 gui_update_scrollbars(TRUE);
1675 gui_reset_scroll_region();
1676}
1677
1678/*
1679 * Called when Rows and/or Columns has changed.
1680 */
1681 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001682gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 gui_reset_scroll_region();
1685}
1686
1687/*
1688 * Make scroll region cover whole screen.
1689 */
1690 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001691gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 gui.scroll_region_top = 0;
1694 gui.scroll_region_bot = gui.num_rows - 1;
1695 gui.scroll_region_left = 0;
1696 gui.scroll_region_right = gui.num_cols - 1;
1697}
1698
1699 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001700gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 if (mask > HL_ALL) /* highlight code */
1703 gui.highlight_mask = mask;
1704 else /* mask */
1705 gui.highlight_mask |= mask;
1706}
1707
1708 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001709gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 if (mask > HL_ALL) /* highlight code */
1712 gui.highlight_mask = HL_NORMAL;
1713 else /* mask */
1714 gui.highlight_mask &= ~mask;
1715}
1716
1717/*
1718 * Clear a rectangular region of the screen from text pos (row1, col1) to
1719 * (row2, col2) inclusive.
1720 */
1721 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001722gui_clear_block(
1723 int row1,
1724 int col1,
1725 int row2,
1726 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 /* Clear the selection if we are about to write over it */
1729 clip_may_clear_selection(row1, row2);
1730
1731 gui_mch_clear_block(row1, col1, row2, col2);
1732
1733 /* Invalidate cursor if it was in this block */
1734 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1735 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1736 gui.cursor_is_valid = FALSE;
1737}
1738
1739/*
1740 * Write code to update the cursor later. This avoids the need to flush the
1741 * output buffer before calling gui_update_cursor().
1742 */
1743 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001744gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001746 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747}
1748
1749 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001750gui_write(
1751 char_u *s,
1752 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 char_u *p;
1755 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 int force_scrollbar = FALSE;
1758 static win_T *old_curwin = NULL;
1759
1760/* #define DEBUG_GUI_WRITE */
1761#ifdef DEBUG_GUI_WRITE
1762 {
1763 int i;
1764 char_u *str;
1765
1766 printf("gui_write(%d):\n ", len);
1767 for (i = 0; i < len; i++)
1768 if (s[i] == ESC)
1769 {
1770 if (i != 0)
1771 printf("\n ");
1772 printf("<ESC>");
1773 }
1774 else
1775 {
1776 str = transchar_byte(s[i]);
1777 if (str[0] && str[1])
1778 printf("<%s>", (char *)str);
1779 else
1780 printf("%s", (char *)str);
1781 }
1782 printf("\n");
1783 }
1784#endif
1785 while (len)
1786 {
1787 if (s[0] == ESC && s[1] == '|')
1788 {
1789 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001790 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 arg1 = getdigits(&p);
1793 if (p > s + len)
1794 break;
1795 if (*p == ';')
1796 {
1797 ++p;
1798 arg2 = getdigits(&p);
1799 if (p > s + len)
1800 break;
1801 }
1802 }
1803 switch (*p)
1804 {
1805 case 'C': /* Clear screen */
1806 clip_scroll_selection(9999);
1807 gui_mch_clear_all();
1808 gui.cursor_is_valid = FALSE;
1809 force_scrollbar = TRUE;
1810 break;
1811 case 'M': /* Move cursor */
1812 gui_set_cursor(arg1, arg2);
1813 break;
1814 case 's': /* force cursor (shape) update */
1815 force_cursor = TRUE;
1816 break;
1817 case 'R': /* Set scroll region */
1818 if (arg1 < arg2)
1819 {
1820 gui.scroll_region_top = arg1;
1821 gui.scroll_region_bot = arg2;
1822 }
1823 else
1824 {
1825 gui.scroll_region_top = arg2;
1826 gui.scroll_region_bot = arg1;
1827 }
1828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 case 'V': /* Set vertical scroll region */
1830 if (arg1 < arg2)
1831 {
1832 gui.scroll_region_left = arg1;
1833 gui.scroll_region_right = arg2;
1834 }
1835 else
1836 {
1837 gui.scroll_region_left = arg2;
1838 gui.scroll_region_right = arg1;
1839 }
1840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 case 'd': /* Delete line */
1842 gui_delete_lines(gui.row, 1);
1843 break;
1844 case 'D': /* Delete lines */
1845 gui_delete_lines(gui.row, arg1);
1846 break;
1847 case 'i': /* Insert line */
1848 gui_insert_lines(gui.row, 1);
1849 break;
1850 case 'I': /* Insert lines */
1851 gui_insert_lines(gui.row, arg1);
1852 break;
1853 case '$': /* Clear to end-of-line */
1854 gui_clear_block(gui.row, gui.col, gui.row,
1855 (int)Columns - 1);
1856 break;
1857 case 'h': /* Turn on highlighting */
1858 gui_start_highlight(arg1);
1859 break;
1860 case 'H': /* Turn off highlighting */
1861 gui_stop_highlight(arg1);
1862 break;
1863 case 'f': /* flash the window (visual bell) */
1864 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1865 break;
1866 default:
1867 p = s + 1; /* Skip the ESC */
1868 break;
1869 }
1870 len -= (int)(++p - s);
1871 s = p;
1872 }
1873 else if (
1874#ifdef EBCDIC
1875 CtrlChar(s[0]) != 0 /* Ctrl character */
1876#else
1877 s[0] < 0x20 /* Ctrl character */
1878#endif
1879#ifdef FEAT_SIGN_ICONS
1880 && s[0] != SIGN_BYTE
1881# ifdef FEAT_NETBEANS_INTG
1882 && s[0] != MULTISIGN_BYTE
1883# endif
1884#endif
1885 )
1886 {
1887 if (s[0] == '\n') /* NL */
1888 {
1889 gui.col = 0;
1890 if (gui.row < gui.scroll_region_bot)
1891 gui.row++;
1892 else
1893 gui_delete_lines(gui.scroll_region_top, 1);
1894 }
1895 else if (s[0] == '\r') /* CR */
1896 {
1897 gui.col = 0;
1898 }
1899 else if (s[0] == '\b') /* Backspace */
1900 {
1901 if (gui.col)
1902 --gui.col;
1903 }
1904 else if (s[0] == Ctrl_L) /* cursor-right */
1905 {
1906 ++gui.col;
1907 }
1908 else if (s[0] == Ctrl_G) /* Beep */
1909 {
1910 gui_mch_beep();
1911 }
1912 /* Other Ctrl character: shouldn't happen! */
1913
1914 --len; /* Skip this char */
1915 ++s;
1916 }
1917 else
1918 {
1919 p = s;
1920 while (len > 0 && (
1921#ifdef EBCDIC
1922 CtrlChar(*p) == 0
1923#else
1924 *p >= 0x20
1925#endif
1926#ifdef FEAT_SIGN_ICONS
1927 || *p == SIGN_BYTE
1928# ifdef FEAT_NETBEANS_INTG
1929 || *p == MULTISIGN_BYTE
1930# endif
1931#endif
1932 ))
1933 {
1934 len--;
1935 p++;
1936 }
1937 gui_outstr(s, (int)(p - s));
1938 s = p;
1939 }
1940 }
1941
1942 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1943 * set). */
1944 if (force_cursor)
1945 gui_update_cursor(TRUE, TRUE);
1946
1947 /* When switching to another window the dragging must have stopped.
1948 * Required for GTK, dragged_sb isn't reset. */
1949 if (old_curwin != curwin)
1950 gui.dragged_sb = SBAR_NONE;
1951
1952 /* Update the scrollbars after clearing the screen or when switched
1953 * to another window.
1954 * Update the horizontal scrollbar always, it's difficult to check all
1955 * situations where it might change. */
1956 if (force_scrollbar || old_curwin != curwin)
1957 gui_update_scrollbars(force_scrollbar);
1958 else
1959 gui_update_horiz_scrollbar(FALSE);
1960 old_curwin = curwin;
1961
1962 /*
1963 * We need to make sure this is cleared since Athena doesn't tell us when
1964 * he is done dragging. Do the same for GTK.
1965 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001966#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 gui.dragged_sb = SBAR_NONE;
1968#endif
1969
1970 gui_mch_flush(); /* In case vim decides to take a nap */
1971}
1972
1973/*
1974 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1975 * produces wrong results. Call gui_dont_update_cursor() before that code and
1976 * gui_can_update_cursor() afterwards.
1977 */
1978 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001979gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 if (gui.in_use)
1982 {
1983 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001984 if (undraw)
1985 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 can_update_cursor = FALSE;
1987 }
1988}
1989
1990 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001991gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 can_update_cursor = TRUE;
1994 /* No need to update the cursor right now, there is always more output
1995 * after scrolling. */
1996}
1997
1998 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001999gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 int this_len;
2002#ifdef FEAT_MBYTE
2003 int cells;
2004#endif
2005
2006 if (len == 0)
2007 return;
2008
2009 if (len < 0)
2010 len = (int)STRLEN(s);
2011
2012 while (len > 0)
2013 {
2014#ifdef FEAT_MBYTE
2015 if (has_mbyte)
2016 {
2017 /* Find out how many chars fit in the current line. */
2018 cells = 0;
2019 for (this_len = 0; this_len < len; )
2020 {
2021 cells += (*mb_ptr2cells)(s + this_len);
2022 if (gui.col + cells > Columns)
2023 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002024 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 if (this_len > len)
2027 this_len = len; /* don't include following composing char */
2028 }
2029 else
2030#endif
2031 if (gui.col + len > Columns)
2032 this_len = Columns - gui.col;
2033 else
2034 this_len = len;
2035
2036 (void)gui_outstr_nowrap(s, this_len,
2037 0, (guicolor_T)0, (guicolor_T)0, 0);
2038 s += this_len;
2039 len -= this_len;
2040#ifdef FEAT_MBYTE
2041 /* fill up for a double-width char that doesn't fit. */
2042 if (len > 0 && gui.col < Columns)
2043 (void)gui_outstr_nowrap((char_u *)" ", 1,
2044 0, (guicolor_T)0, (guicolor_T)0, 0);
2045#endif
2046 /* The cursor may wrap to the next line. */
2047 if (gui.col >= Columns)
2048 {
2049 gui.col = 0;
2050 gui.row++;
2051 }
2052 }
2053}
2054
2055/*
2056 * Output one character (may be one or two display cells).
2057 * Caller must check for valid "off".
2058 * Returns FAIL or OK, just like gui_outstr_nowrap().
2059 */
2060 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002061gui_screenchar(
2062 int off, /* Offset from start of screen */
2063 int flags,
2064 guicolor_T fg, /* colors for cursor */
2065 guicolor_T bg, /* colors for cursor */
2066 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068#ifdef FEAT_MBYTE
2069 char_u buf[MB_MAXBYTES + 1];
2070
2071 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2072 if (enc_utf8 && ScreenLines[off] == 0)
2073 return OK;
2074
2075 if (enc_utf8 && ScreenLinesUC[off] != 0)
2076 /* Draw UTF-8 multi-byte character. */
2077 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2078 flags, fg, bg, back);
2079
2080 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2081 {
2082 buf[0] = ScreenLines[off];
2083 buf[1] = ScreenLines2[off];
2084 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2085 }
2086
2087 /* Draw non-multi-byte character or DBCS character. */
2088 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002089 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 flags, fg, bg, back);
2091#else
2092 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2093#endif
2094}
2095
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002096#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097/*
2098 * Output the string at the given screen position. This is used in place
2099 * of gui_screenchar() where possible because Pango needs as much context
2100 * as possible to work nicely. It's a lot faster as well.
2101 */
2102 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002103gui_screenstr(
2104 int off, /* Offset from start of screen */
2105 int len, /* string length in screen cells */
2106 int flags,
2107 guicolor_T fg, /* colors for cursor */
2108 guicolor_T bg, /* colors for cursor */
2109 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 char_u *buf;
2112 int outlen = 0;
2113 int i;
2114 int retval;
2115
2116 if (len <= 0) /* "cannot happen"? */
2117 return OK;
2118
2119 if (enc_utf8)
2120 {
2121 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2122 if (buf == NULL)
2123 return OK; /* not much we could do here... */
2124
2125 for (i = off; i < off + len; ++i)
2126 {
2127 if (ScreenLines[i] == 0)
2128 continue; /* skip second half of double-width char */
2129
2130 if (ScreenLinesUC[i] == 0)
2131 buf[outlen++] = ScreenLines[i];
2132 else
2133 outlen += utfc_char2bytes(i, buf + outlen);
2134 }
2135
2136 buf[outlen] = NUL; /* only to aid debugging */
2137 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2138 vim_free(buf);
2139
2140 return retval;
2141 }
2142 else if (enc_dbcs == DBCS_JPNU)
2143 {
2144 buf = alloc((unsigned)(len * 2 + 1));
2145 if (buf == NULL)
2146 return OK; /* not much we could do here... */
2147
2148 for (i = off; i < off + len; ++i)
2149 {
2150 buf[outlen++] = ScreenLines[i];
2151
2152 /* handle double-byte single-width char */
2153 if (ScreenLines[i] == 0x8e)
2154 buf[outlen++] = ScreenLines2[i];
2155 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2156 buf[outlen++] = ScreenLines[++i];
2157 }
2158
2159 buf[outlen] = NUL; /* only to aid debugging */
2160 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2161 vim_free(buf);
2162
2163 return retval;
2164 }
2165 else
2166 {
2167 return gui_outstr_nowrap(&ScreenLines[off], len,
2168 flags, fg, bg, back);
2169 }
2170}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002171#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173/*
2174 * Output the given string at the current cursor position. If the string is
2175 * too long to fit on the line, then it is truncated.
2176 * "flags":
2177 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2178 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002179 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * background.
2181 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2182 * it.
2183 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2184 * FAIL (the caller should start drawing "back" chars back).
2185 */
2186 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002187gui_outstr_nowrap(
2188 char_u *s,
2189 int len,
2190 int flags,
2191 guicolor_T fg, /* colors for cursor */
2192 guicolor_T bg, /* colors for cursor */
2193 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 long_u highlight_mask;
2196 long_u hl_mask_todo;
2197 guicolor_T fg_color;
2198 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002199 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002200#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002202# ifdef FEAT_MBYTE
2203 GuiFont wide_font = NOFONT;
2204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205# ifdef FEAT_XFONTSET
2206 GuiFontset fontset = NOFONTSET;
2207# endif
2208#endif
2209 attrentry_T *aep = NULL;
2210 int draw_flags;
2211 int col = gui.col;
2212#ifdef FEAT_SIGN_ICONS
2213 int draw_sign = FALSE;
2214# ifdef FEAT_NETBEANS_INTG
2215 int multi_sign = FALSE;
2216# endif
2217#endif
2218
2219 if (len < 0)
2220 len = (int)STRLEN(s);
2221 if (len == 0)
2222 return OK;
2223
2224#ifdef FEAT_SIGN_ICONS
2225 if (*s == SIGN_BYTE
2226# ifdef FEAT_NETBEANS_INTG
2227 || *s == MULTISIGN_BYTE
2228# endif
2229 )
2230 {
2231# ifdef FEAT_NETBEANS_INTG
2232 if (*s == MULTISIGN_BYTE)
2233 multi_sign = TRUE;
2234# endif
2235 /* draw spaces instead */
2236 s = (char_u *)" ";
2237 if (len == 1 && col > 0)
2238 --col;
2239 len = 2;
2240 draw_sign = TRUE;
2241 highlight_mask = 0;
2242 }
2243 else
2244#endif
2245 if (gui.highlight_mask > HL_ALL)
2246 {
2247 aep = syn_gui_attr2entry(gui.highlight_mask);
2248 if (aep == NULL) /* highlighting not set */
2249 highlight_mask = 0;
2250 else
2251 highlight_mask = aep->ae_attr;
2252 }
2253 else
2254 highlight_mask = gui.highlight_mask;
2255 hl_mask_todo = highlight_mask;
2256
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002257#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 /* Set the font */
2259 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2260 font = aep->ae_u.gui.font;
2261# ifdef FEAT_XFONTSET
2262 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2263 fontset = aep->ae_u.gui.fontset;
2264# endif
2265 else
2266 {
2267# ifdef FEAT_XFONTSET
2268 if (gui.fontset != NOFONTSET)
2269 fontset = gui.fontset;
2270 else
2271# endif
2272 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2273 {
2274 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2275 {
2276 font = gui.boldital_font;
2277 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2278 }
2279 else if (gui.bold_font != NOFONT)
2280 {
2281 font = gui.bold_font;
2282 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2283 }
2284 else
2285 font = gui.norm_font;
2286 }
2287 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2288 {
2289 font = gui.ital_font;
2290 hl_mask_todo &= ~HL_ITALIC;
2291 }
2292 else
2293 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002294
2295# ifdef FEAT_MBYTE
2296 /*
2297 * Choose correct wide_font by font. wide_font should be set with font
2298 * at same time in above block. But it will make many "ifdef" nasty
2299 * blocks. So we do it here.
2300 */
2301 if (font == gui.boldital_font && gui.wide_boldital_font)
2302 wide_font = gui.wide_boldital_font;
2303 else if (font == gui.bold_font && gui.wide_bold_font)
2304 wide_font = gui.wide_bold_font;
2305 else if (font == gui.ital_font && gui.wide_ital_font)
2306 wide_font = gui.wide_ital_font;
2307 else if (font == gui.norm_font && gui.wide_font)
2308 wide_font = gui.wide_font;
2309# endif
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312# ifdef FEAT_XFONTSET
2313 if (fontset != NOFONTSET)
2314 gui_mch_set_fontset(fontset);
2315 else
2316# endif
2317 gui_mch_set_font(font);
2318#endif
2319
2320 draw_flags = 0;
2321
2322 /* Set the color */
2323 bg_color = gui.back_pixel;
2324 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2325 {
2326 draw_flags |= DRAW_CURSOR;
2327 fg_color = fg;
2328 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002329 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 else if (aep != NULL)
2332 {
2333 fg_color = aep->ae_u.gui.fg_color;
2334 if (fg_color == INVALCOLOR)
2335 fg_color = gui.norm_pixel;
2336 bg_color = aep->ae_u.gui.bg_color;
2337 if (bg_color == INVALCOLOR)
2338 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002339 sp_color = aep->ae_u.gui.sp_color;
2340 if (sp_color == INVALCOLOR)
2341 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002346 sp_color = fg_color;
2347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
2349 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2350 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002351#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 gui_mch_set_colors(bg_color, fg_color);
2353#else
2354 gui_mch_set_fg_color(bg_color);
2355 gui_mch_set_bg_color(fg_color);
2356#endif
2357 }
2358 else
2359 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002360#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 gui_mch_set_colors(fg_color, bg_color);
2362#else
2363 gui_mch_set_fg_color(fg_color);
2364 gui_mch_set_bg_color(bg_color);
2365#endif
2366 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002367 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 /* Clear the selection if we are about to write over it */
2370 if (!(flags & GUI_MON_NOCLEAR))
2371 clip_may_clear_selection(gui.row, gui.row);
2372
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 /* If there's no bold font, then fake it */
2375 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2376 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 /*
2379 * When drawing bold or italic characters the spill-over from the left
2380 * neighbor may be destroyed. Let the caller backup to start redrawing
2381 * just after a blank.
2382 */
2383 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2384 return FAIL;
2385
Bram Moolenaare60acc12011-05-10 16:41:25 +02002386#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 /* If there's no italic font, then fake it.
2388 * For GTK2, we don't need a different font for italic style. */
2389 if (hl_mask_todo & HL_ITALIC)
2390 draw_flags |= DRAW_ITALIC;
2391
2392 /* Do we underline the text? */
2393 if (hl_mask_todo & HL_UNDERLINE)
2394 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396#else
2397 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 draw_flags |= DRAW_UNDERL;
2400#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002401 /* Do we undercurl the text? */
2402 if (hl_mask_todo & HL_UNDERCURL)
2403 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002405 /* Do we strikethrough the text? */
2406 if (hl_mask_todo & HL_STRIKETHROUGH)
2407 draw_flags |= DRAW_STRIKE;
2408
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002409 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (flags & GUI_MON_TRS_CURSOR)
2411 draw_flags |= DRAW_TRANSP;
2412
2413 /*
2414 * Draw the text.
2415 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002416#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 /* The value returned is the length in display cells */
2418 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2419#else
2420# ifdef FEAT_MBYTE
2421 if (enc_utf8)
2422 {
2423 int start; /* index of bytes to be drawn */
2424 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002425 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 int cn; /* cellwidth of current char */
2427 int i; /* index of current char */
2428 int c; /* current char value */
2429 int cl; /* byte length of current char */
2430 int comping; /* current char is composing */
2431 int scol = col; /* screen column */
Bram Moolenaar45933962013-01-23 17:43:57 +01002432 int curr_wide; /* use 'guifontwide' */
2433 int prev_wide = FALSE;
2434 int wide_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 /* Break the string at a composing character, it has to be drawn on
2437 * top of the previous character. */
2438 start = 0;
2439 cells = 0;
2440 for (i = 0; i < len; i += cl)
2441 {
2442 c = utf_ptr2char(s + i);
2443 cn = utf_char2cells(c);
2444 if (cn > 1
2445# ifdef FEAT_XFONTSET
2446 && fontset == NOFONTSET
2447# endif
Bram Moolenaardb250522013-06-17 22:43:25 +02002448 && wide_font != NOFONT)
Bram Moolenaar45933962013-01-23 17:43:57 +01002449 curr_wide = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 else
Bram Moolenaar45933962013-01-23 17:43:57 +01002451 curr_wide = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 comping = utf_iscomposing(c);
2453 if (!comping) /* count cells from non-composing chars */
2454 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002455 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (cl == 0) /* hit end of string */
2457 len = i + cl; /* len must be wrong "cannot happen" */
2458
Bram Moolenaar45933962013-01-23 17:43:57 +01002459 wide_changed = curr_wide != prev_wide;
2460
2461 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 * a composing character. */
Bram Moolenaar45933962013-01-23 17:43:57 +01002463 if (i + cl >= len || (comping && i > start) || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002464# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 || (cn > 1
2466# ifdef FEAT_XFONTSET
2467 /* No fontset: At least draw char after wide char at
2468 * right position. */
2469 && fontset == NOFONTSET
2470# endif
2471 )
2472# endif
2473 )
2474 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002475 if (comping || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 thislen = i - start;
2477 else
2478 thislen = i - start + cl;
2479 if (thislen > 0)
2480 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002481 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002482 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2484 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002485 if (prev_wide)
2486 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 start += thislen;
2488 }
2489 scol += cells;
2490 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002491 /* Adjust to not draw a character which width is changed
2492 * against with last one. */
2493 if (wide_changed && !comping)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002495 scol -= cn;
2496 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002499# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002500 /* No fontset: draw a space to fill the gap after a wide char
2501 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2503# ifdef FEAT_XFONTSET
2504 && fontset == NOFONTSET
2505# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002506 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2508 1, draw_flags);
2509# endif
2510 }
2511 /* Draw a composing char on top of the previous char. */
2512 if (comping)
2513 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002514# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002515 /* Carbon ATSUI autodraws composing char over previous char */
2516 gui_mch_draw_string(gui.row, scol, s + i, cl,
2517 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002518# else
2519 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2520 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 start = i + cl;
2523 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002524 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526 /* The stuff below assumes "len" is the length in screen columns. */
2527 len = scol - col;
2528 }
2529 else
2530# endif
2531 {
2532 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2533# ifdef FEAT_MBYTE
2534 if (enc_dbcs == DBCS_JPNU)
2535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /* Get the length in display cells, this can be different from the
2537 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002538 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540# endif
2541 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002542#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
2544 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2545 gui.col = col + len;
2546
2547 /* May need to invert it when it's part of the selection. */
2548 if (flags & GUI_MON_NOCLEAR)
2549 clip_may_redraw_selection(gui.row, col, len);
2550
2551 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2552 {
2553 /* Invalidate the old physical cursor position if we wrote over it */
2554 if (gui.cursor_row == gui.row
2555 && gui.cursor_col >= col
2556 && gui.cursor_col < col + len)
2557 gui.cursor_is_valid = FALSE;
2558 }
2559
2560#ifdef FEAT_SIGN_ICONS
2561 if (draw_sign)
2562 /* Draw the sign on top of the spaces. */
2563 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002564# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002565 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (multi_sign)
2567 netbeans_draw_multisign_indicator(gui.row);
2568# endif
2569#endif
2570
2571 return OK;
2572}
2573
2574/*
2575 * Un-draw the cursor. Actually this just redraws the character at the given
2576 * position. The character just before it too, for when it was in bold.
2577 */
2578 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002579gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 if (gui.cursor_is_valid)
2582 {
2583#ifdef FEAT_HANGULIN
2584 if (composing_hangul
2585 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002586 {
2587 char_u *comp_buf;
2588 int comp_len;
2589
2590 comp_buf = hangul_composing_buffer_get(&comp_len);
2591 if (comp_buf)
2592 {
2593 (void)gui_outstr_nowrap(comp_buf, comp_len,
2594 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2595 gui.norm_pixel, gui.back_pixel, 0);
2596 vim_free(comp_buf);
2597 }
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 else
2600 {
2601#endif
2602 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2603 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2604 && gui.cursor_col > 0)
2605 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2606 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2607#ifdef FEAT_HANGULIN
2608 if (composing_hangul)
2609 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2610 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2611 }
2612#endif
2613 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2614 * here in case it wasn't needed to undraw it. */
2615 gui.cursor_is_valid = FALSE;
2616 }
2617}
2618
2619 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002620gui_redraw(
2621 int x,
2622 int y,
2623 int w,
2624 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 int row1, col1, row2, col2;
2627
2628 row1 = Y_2_ROW(y);
2629 col1 = X_2_COL(x);
2630 row2 = Y_2_ROW(y + h - 1);
2631 col2 = X_2_COL(x + w - 1);
2632
2633 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2634
2635 /*
2636 * We may need to redraw the cursor, but don't take it upon us to change
2637 * its location after a scroll.
2638 * (maybe be more strict even and test col too?)
2639 * These things may be outside the update/clipping region and reality may
2640 * not reflect Vims internal ideas if these operations are clipped away.
2641 */
2642 if (gui.row == gui.cursor_row)
2643 gui_update_cursor(TRUE, TRUE);
2644}
2645
2646/*
2647 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2648 * from col1 to col2 (inclusive).
2649 * Return TRUE when the character before the first drawn character has
2650 * different attributes (may have to be redrawn too).
2651 */
2652 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002653gui_redraw_block(
2654 int row1,
2655 int col1,
2656 int row2,
2657 int col2,
2658 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
2660 int old_row, old_col;
2661 long_u old_hl_mask;
2662 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002663 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 int idx, len;
2665 int back, nback;
2666 int retval = FALSE;
2667#ifdef FEAT_MBYTE
2668 int orig_col1, orig_col2;
2669#endif
2670
2671 /* Don't try to update when ScreenLines is not valid */
2672 if (!screen_cleared || ScreenLines == NULL)
2673 return retval;
2674
2675 /* Don't try to draw outside the shell! */
2676 /* Check everything, strange values may be caused by a big border width */
2677 col1 = check_col(col1);
2678 col2 = check_col(col2);
2679 row1 = check_row(row1);
2680 row2 = check_row(row2);
2681
2682 /* Remember where our cursor was */
2683 old_row = gui.row;
2684 old_col = gui.col;
2685 old_hl_mask = gui.highlight_mask;
2686#ifdef FEAT_MBYTE
2687 orig_col1 = col1;
2688 orig_col2 = col2;
2689#endif
2690
2691 for (gui.row = row1; gui.row <= row2; gui.row++)
2692 {
2693#ifdef FEAT_MBYTE
2694 /* When only half of a double-wide character is in the block, include
2695 * the other half. */
2696 col1 = orig_col1;
2697 col2 = orig_col2;
2698 off = LineOffset[gui.row];
2699 if (enc_dbcs != 0)
2700 {
2701 if (col1 > 0)
2702 col1 -= dbcs_screen_head_off(ScreenLines + off,
2703 ScreenLines + off + col1);
2704 col2 += dbcs_screen_tail_off(ScreenLines + off,
2705 ScreenLines + off + col2);
2706 }
2707 else if (enc_utf8)
2708 {
2709 if (ScreenLines[off + col1] == 0)
2710 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002711# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2713 ++col2;
2714# endif
2715 }
2716#endif
2717 gui.col = col1;
2718 off = LineOffset[gui.row] + gui.col;
2719 len = col2 - col1 + 1;
2720
2721 /* Find how many chars back this highlighting starts, or where a space
2722 * is. Needed for when the bold trick is used */
2723 for (back = 0; back < col1; ++back)
2724 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2725 || ScreenLines[off - 1 - back] == ' ')
2726 break;
2727 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2728 && ScreenLines[off - 1] != ' ');
2729
2730 /* Break it up in strings of characters with the same attributes. */
2731 /* Print UTF-8 characters individually. */
2732 while (len > 0)
2733 {
2734 first_attr = ScreenAttrs[off];
2735 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002736#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (enc_utf8 && ScreenLinesUC[off] != 0)
2738 {
2739 /* output multi-byte character separately */
2740 nback = gui_screenchar(off, flags,
2741 (guicolor_T)0, (guicolor_T)0, back);
2742 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2743 idx = 2;
2744 else
2745 idx = 1;
2746 }
2747 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2748 {
2749 /* output double-byte, single-width character separately */
2750 nback = gui_screenchar(off, flags,
2751 (guicolor_T)0, (guicolor_T)0, back);
2752 idx = 1;
2753 }
2754 else
2755#endif
2756 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002757#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 for (idx = 0; idx < len; ++idx)
2759 {
2760 if (enc_utf8 && ScreenLines[off + idx] == 0)
2761 continue; /* skip second half of double-width char */
2762 if (ScreenAttrs[off + idx] != first_attr)
2763 break;
2764 }
2765 /* gui_screenstr() takes care of multibyte chars */
2766 nback = gui_screenstr(off, idx, flags,
2767 (guicolor_T)0, (guicolor_T)0, back);
2768#else
2769 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2770 idx++)
2771 {
2772# ifdef FEAT_MBYTE
2773 /* Stop at a multi-byte Unicode character. */
2774 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2775 break;
2776 if (enc_dbcs == DBCS_JPNU)
2777 {
2778 /* Stop at a double-byte single-width char. */
2779 if (ScreenLines[off + idx] == 0x8e)
2780 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002781 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 + off + idx) == 2)
2783 ++idx; /* skip second byte of double-byte char */
2784 }
2785# endif
2786 }
2787 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2788 (guicolor_T)0, (guicolor_T)0, back);
2789#endif
2790 }
2791 if (nback == FAIL)
2792 {
2793 /* Must back up to start drawing where a bold or italic word
2794 * starts. */
2795 off -= back;
2796 len += back;
2797 gui.col -= back;
2798 }
2799 else
2800 {
2801 off += idx;
2802 len -= idx;
2803 }
2804 back = 0;
2805 }
2806 }
2807
2808 /* Put the cursor back where it was */
2809 gui.row = old_row;
2810 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002811 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 return retval;
2814}
2815
2816 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002817gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 if (count <= 0)
2820 return;
2821
2822 if (row + count > gui.scroll_region_bot)
2823 /* Scrolled out of region, just blank the lines out */
2824 gui_clear_block(row, gui.scroll_region_left,
2825 gui.scroll_region_bot, gui.scroll_region_right);
2826 else
2827 {
2828 gui_mch_delete_lines(row, count);
2829
2830 /* If the cursor was in the deleted lines it's now gone. If the
2831 * cursor was in the scrolled lines adjust its position. */
2832 if (gui.cursor_row >= row
2833 && gui.cursor_col >= gui.scroll_region_left
2834 && gui.cursor_col <= gui.scroll_region_right)
2835 {
2836 if (gui.cursor_row < row + count)
2837 gui.cursor_is_valid = FALSE;
2838 else if (gui.cursor_row <= gui.scroll_region_bot)
2839 gui.cursor_row -= count;
2840 }
2841 }
2842}
2843
2844 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002845gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
2847 if (count <= 0)
2848 return;
2849
2850 if (row + count > gui.scroll_region_bot)
2851 /* Scrolled out of region, just blank the lines out */
2852 gui_clear_block(row, gui.scroll_region_left,
2853 gui.scroll_region_bot, gui.scroll_region_right);
2854 else
2855 {
2856 gui_mch_insert_lines(row, count);
2857
2858 if (gui.cursor_row >= gui.row
2859 && gui.cursor_col >= gui.scroll_region_left
2860 && gui.cursor_col <= gui.scroll_region_right)
2861 {
2862 if (gui.cursor_row <= gui.scroll_region_bot - count)
2863 gui.cursor_row += count;
2864 else if (gui.cursor_row <= gui.scroll_region_bot)
2865 gui.cursor_is_valid = FALSE;
2866 }
2867 }
2868}
2869
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002870/*
2871 * Returns OK if a character was found to be available within the given time,
2872 * or FAIL otherwise.
2873 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002874 static int
2875gui_wait_for_chars_or_timer(long wtime)
2876{
2877#ifdef FEAT_TIMERS
2878 int due_time;
2879 long remaining = wtime;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002880 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002881
2882 /* When waiting very briefly don't trigger timers. */
2883 if (wtime >= 0 && wtime < 10L)
2884 return gui_mch_wait_for_chars(wtime);
2885
2886 while (wtime < 0 || remaining > 0)
2887 {
2888 /* Trigger timers and then get the time in wtime until the next one is
2889 * due. Wait up to that time. */
2890 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002891 if (typebuf.tb_change_cnt != tb_change_cnt)
2892 {
2893 /* timer may have used feedkeys() */
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002894 return FAIL;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002895 }
Bram Moolenaar975b5272016-03-15 23:10:59 +01002896 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
2897 due_time = remaining;
2898 if (gui_mch_wait_for_chars(due_time))
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002899 return OK;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002900 if (wtime > 0)
2901 remaining -= due_time;
2902 }
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002903 return FAIL;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002904#else
2905 return gui_mch_wait_for_chars(wtime);
2906#endif
2907}
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909/*
2910 * The main GUI input routine. Waits for a character from the keyboard.
2911 * wtime == -1 Wait forever.
2912 * wtime == 0 Don't wait.
2913 * wtime > 0 Wait wtime milliseconds for a character.
2914 * Returns OK if a character was found to be available within the given time,
2915 * or FAIL otherwise.
2916 */
2917 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002918gui_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
2920 int retval;
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002921 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002923#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 /*
2925 * If we're going to wait a bit, update the menus and mouse shape for the
2926 * current State.
2927 */
2928 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 gui_update_menus(0);
2930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 gui_mch_update();
2933 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 /* Before waiting, flush any output to the screen. */
2939 gui_mch_flush();
2940
2941 if (wtime > 0)
2942 {
2943 /* Blink when waiting for a character. Probably only does something
2944 * for showmatch() */
2945 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002946 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 return retval;
2949 }
2950
2951 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002952 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
2954 gui_mch_start_blink();
2955
Bram Moolenaar3918c952005-03-15 22:34:55 +00002956 retval = FAIL;
2957 /*
2958 * We may want to trigger the CursorHold event. First wait for
2959 * 'updatetime' and if nothing is typed within that time put the
2960 * K_CURSORHOLD key in the input buffer.
2961 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002962 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002963 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002965 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002967 char_u buf[3];
2968
2969 /* Put K_CURSORHOLD in the input buffer. */
2970 buf[0] = CSI;
2971 buf[1] = KS_EXTRA;
2972 buf[2] = (int)KE_CURSORHOLD;
2973 add_to_input_buf(buf, 3);
2974
2975 retval = OK;
2976 }
2977#endif
2978
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002979 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002980 {
2981 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002982 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002983 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 gui_mch_stop_blink();
2987 return retval;
2988}
2989
2990/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002991 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 */
2993 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002994fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 p[0] = (char_u)(col / 128 + ' ' + 1);
2997 p[1] = (char_u)(col % 128 + ' ' + 1);
2998 p[2] = (char_u)(row / 128 + ' ' + 1);
2999 p[3] = (char_u)(row % 128 + ' ' + 1);
3000}
3001
3002/*
3003 * Generic mouse support function. Add a mouse event to the input buffer with
3004 * the given properties.
3005 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3006 * MOUSE_X1, MOUSE_X2
3007 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003008 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3009 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 * x, y --- Coordinates of mouse in pixels.
3011 * repeated_click --- TRUE if this click comes only a short time after a
3012 * previous click.
3013 * modifiers --- Bit field which may be any of the following modifiers
3014 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3015 * This function will ignore drag events where the mouse has not moved to a new
3016 * character.
3017 */
3018 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003019gui_send_mouse_event(
3020 int button,
3021 int x,
3022 int y,
3023 int repeated_click,
3024 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 static int prev_row = 0, prev_col = 0;
3027 static int prev_button = -1;
3028 static int num_clicks = 1;
3029 char_u string[10];
3030 enum key_extra button_char;
3031 int row, col;
3032#ifdef FEAT_CLIPBOARD
3033 int checkfor;
3034 int did_clip = FALSE;
3035#endif
3036
3037 /*
3038 * Scrolling may happen at any time, also while a selection is present.
3039 */
3040 switch (button)
3041 {
3042 case MOUSE_X1:
3043 button_char = KE_X1MOUSE;
3044 goto button_set;
3045 case MOUSE_X2:
3046 button_char = KE_X2MOUSE;
3047 goto button_set;
3048 case MOUSE_4:
3049 button_char = KE_MOUSEDOWN;
3050 goto button_set;
3051 case MOUSE_5:
3052 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003053 goto button_set;
3054 case MOUSE_6:
3055 button_char = KE_MOUSELEFT;
3056 goto button_set;
3057 case MOUSE_7:
3058 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059button_set:
3060 {
3061 /* Don't put events in the input queue now. */
3062 if (hold_gui_events)
3063 return;
3064
3065 string[3] = CSI;
3066 string[4] = KS_EXTRA;
3067 string[5] = (int)button_char;
3068
3069 /* Pass the pointer coordinates of the scroll event so that we
3070 * know which window to scroll. */
3071 row = gui_xy2colrow(x, y, &col);
3072 string[6] = (char_u)(col / 128 + ' ' + 1);
3073 string[7] = (char_u)(col % 128 + ' ' + 1);
3074 string[8] = (char_u)(row / 128 + ' ' + 1);
3075 string[9] = (char_u)(row % 128 + ' ' + 1);
3076
3077 if (modifiers == 0)
3078 add_to_input_buf(string + 3, 7);
3079 else
3080 {
3081 string[0] = CSI;
3082 string[1] = KS_MODIFIER;
3083 string[2] = 0;
3084 if (modifiers & MOUSE_SHIFT)
3085 string[2] |= MOD_MASK_SHIFT;
3086 if (modifiers & MOUSE_CTRL)
3087 string[2] |= MOD_MASK_CTRL;
3088 if (modifiers & MOUSE_ALT)
3089 string[2] |= MOD_MASK_ALT;
3090 add_to_input_buf(string, 10);
3091 }
3092 return;
3093 }
3094 }
3095
3096#ifdef FEAT_CLIPBOARD
3097 /* If a clipboard selection is in progress, handle it */
3098 if (clip_star.state == SELECT_IN_PROGRESS)
3099 {
3100 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3101 return;
3102 }
3103
3104 /* Determine which mouse settings to look for based on the current mode */
3105 switch (get_real_state())
3106 {
3107 case NORMAL_BUSY:
3108 case OP_PENDING:
3109 case NORMAL: checkfor = MOUSE_NORMAL; break;
3110 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003111 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 case REPLACE:
3113 case REPLACE+LANGMAP:
3114#ifdef FEAT_VREPLACE
3115 case VREPLACE:
3116 case VREPLACE+LANGMAP:
3117#endif
3118 case INSERT:
3119 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3120 case ASKMORE:
3121 case HITRETURN: /* At the more- and hit-enter prompt pass the
3122 mouse event for a click on or below the
3123 message line. */
3124 if (Y_2_ROW(y) >= msg_row)
3125 checkfor = MOUSE_NORMAL;
3126 else
3127 checkfor = MOUSE_RETURN;
3128 break;
3129
3130 /*
3131 * On the command line, use the clipboard selection on all lines
3132 * but the command line. But not when pasting.
3133 */
3134 case CMDLINE:
3135 case CMDLINE+LANGMAP:
3136 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3137 checkfor = MOUSE_NONE;
3138 else
3139 checkfor = MOUSE_COMMAND;
3140 break;
3141
3142 default:
3143 checkfor = MOUSE_NONE;
3144 break;
3145 };
3146
3147 /*
3148 * Allow clipboard selection of text on the command line in "normal"
3149 * modes. Don't do this when dragging the status line, or extending a
3150 * Visual selection.
3151 */
3152 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003153 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 && button != MOUSE_DRAG
3155# ifdef FEAT_MOUSESHAPE
3156 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158# endif
3159 )
3160 checkfor = MOUSE_NONE;
3161
3162 /*
3163 * Use modeless selection when holding CTRL and SHIFT pressed.
3164 */
3165 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3166 checkfor = MOUSE_NONEF;
3167
3168 /*
3169 * In Ex mode, always use modeless selection.
3170 */
3171 if (exmode_active)
3172 checkfor = MOUSE_NONE;
3173
3174 /*
3175 * If the mouse settings say to not use the mouse, use the modeless
3176 * selection. But if Visual is active, assume that only the Visual area
3177 * will be selected.
3178 * Exception: On the command line, both the selection is used and a mouse
3179 * key is send.
3180 */
3181 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 /* Don't do modeless selection in Visual mode. */
3184 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 /*
3188 * When 'mousemodel' is "popup", shift-left is translated to right.
3189 * But not when also using Ctrl.
3190 */
3191 if (mouse_model_popup() && button == MOUSE_LEFT
3192 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3193 {
3194 button = MOUSE_RIGHT;
3195 modifiers &= ~ MOUSE_SHIFT;
3196 }
3197
3198 /* If the selection is done, allow the right button to extend it.
3199 * If the selection is cleared, allow the right button to start it
3200 * from the cursor position. */
3201 if (button == MOUSE_RIGHT)
3202 {
3203 if (clip_star.state == SELECT_CLEARED)
3204 {
3205 if (State & CMDLINE)
3206 {
3207 col = msg_col;
3208 row = msg_row;
3209 }
3210 else
3211 {
3212 col = curwin->w_wcol;
3213 row = curwin->w_wrow + W_WINROW(curwin);
3214 }
3215 clip_start_selection(col, row, FALSE);
3216 }
3217 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3218 repeated_click);
3219 did_clip = TRUE;
3220 }
3221 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003222 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3225 did_clip = TRUE;
3226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 /* Always allow pasting */
3229 if (button != MOUSE_MIDDLE)
3230 {
3231 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3232 return;
3233 if (checkfor != MOUSE_COMMAND)
3234 button = MOUSE_LEFT;
3235 }
3236 repeated_click = FALSE;
3237 }
3238
3239 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003240 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
3242
3243 /* Don't put events in the input queue now. */
3244 if (hold_gui_events)
3245 return;
3246
3247 row = gui_xy2colrow(x, y, &col);
3248
3249 /*
3250 * If we are dragging and the mouse hasn't moved far enough to be on a
3251 * different character, then don't send an event to vim.
3252 */
3253 if (button == MOUSE_DRAG)
3254 {
3255 if (row == prev_row && col == prev_col)
3256 return;
3257 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3258 if (y < 0)
3259 row = -1;
3260 }
3261
3262 /*
3263 * If topline has changed (window scrolled) since the last click, reset
3264 * repeated_click, because we don't want starting Visual mode when
3265 * clicking on a different character in the text.
3266 */
3267 if (curwin->w_topline != gui_prev_topline
3268#ifdef FEAT_DIFF
3269 || curwin->w_topfill != gui_prev_topfill
3270#endif
3271 )
3272 repeated_click = FALSE;
3273
3274 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3275 string[1] = KS_MOUSE;
3276 string[2] = KE_FILLER;
3277 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3278 {
3279 if (repeated_click)
3280 {
3281 /*
3282 * Handle multiple clicks. They only count if the mouse is still
3283 * pointing at the same character.
3284 */
3285 if (button != prev_button || row != prev_row || col != prev_col)
3286 num_clicks = 1;
3287 else if (++num_clicks > 4)
3288 num_clicks = 1;
3289 }
3290 else
3291 num_clicks = 1;
3292 prev_button = button;
3293 gui_prev_topline = curwin->w_topline;
3294#ifdef FEAT_DIFF
3295 gui_prev_topfill = curwin->w_topfill;
3296#endif
3297
3298 string[3] = (char_u)(button | 0x20);
3299 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3300 }
3301 else
3302 string[3] = (char_u)button;
3303
3304 string[3] |= modifiers;
3305 fill_mouse_coord(string + 4, col, row);
3306 add_to_input_buf(string, 8);
3307
3308 if (row < 0)
3309 prev_row = 0;
3310 else
3311 prev_row = row;
3312 prev_col = col;
3313
3314 /*
3315 * We need to make sure this is cleared since Athena doesn't tell us when
3316 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3317 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003318#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 gui.dragged_sb = SBAR_NONE;
3320#endif
3321}
3322
3323/*
3324 * Convert x and y coordinate to column and row in text window.
3325 * Corrects for multi-byte character.
3326 * returns column in "*colp" and row as return value;
3327 */
3328 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003329gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 int col = check_col(X_2_COL(x));
3332 int row = check_row(Y_2_ROW(y));
3333
3334#ifdef FEAT_MBYTE
3335 *colp = mb_fix_col(col, row);
3336#else
3337 *colp = col;
3338#endif
3339 return row;
3340}
3341
3342#if defined(FEAT_MENU) || defined(PROTO)
3343/*
3344 * Callback function for when a menu entry has been selected.
3345 */
3346 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003347gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003349 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 /* Don't put events in the input queue now. */
3352 if (hold_gui_events)
3353 return;
3354
3355 bytes[0] = CSI;
3356 bytes[1] = KS_MENU;
3357 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003358 add_to_input_buf(bytes, 3);
3359 add_long_to_buf((long_u)menu, bytes);
3360 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361}
3362#endif
3363
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003364static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003368 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 * in p_go.
3370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003372gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#ifdef FEAT_MENU
3375 static int prev_menu_is_active = -1;
3376#endif
3377#ifdef FEAT_TOOLBAR
3378 static int prev_toolbar = -1;
3379 int using_toolbar = FALSE;
3380#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003381#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003382 int using_tabline;
3383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#ifdef FEAT_FOOTER
3385 static int prev_footer = -1;
3386 int using_footer = FALSE;
3387#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 static int prev_tearoff = -1;
3390 int using_tearoff = FALSE;
3391#endif
3392
3393 char_u *p;
3394 int i;
3395#ifdef FEAT_MENU
3396 int grey_old, grey_new;
3397 char_u *temp;
3398#endif
3399 win_T *wp;
3400 int need_set_size;
3401 int fix_size;
3402
3403#ifdef FEAT_MENU
3404 if (oldval != NULL && gui.in_use)
3405 {
3406 /*
3407 * Check if the menu's go from grey to non-grey or vise versa.
3408 */
3409 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3410 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3411 if (grey_old != grey_new)
3412 {
3413 temp = p_go;
3414 p_go = oldval;
3415 gui_update_menus(MENU_ALL_MODES);
3416 p_go = temp;
3417 }
3418 }
3419 gui.menu_is_active = FALSE;
3420#endif
3421
3422 for (i = 0; i < 3; i++)
3423 gui.which_scrollbars[i] = FALSE;
3424 for (p = p_go; *p; p++)
3425 switch (*p)
3426 {
3427 case GO_LEFT:
3428 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3429 break;
3430 case GO_RIGHT:
3431 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 case GO_VLEFT:
3434 if (win_hasvertsplit())
3435 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3436 break;
3437 case GO_VRIGHT:
3438 if (win_hasvertsplit())
3439 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 case GO_BOT:
3442 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3443 break;
3444#ifdef FEAT_MENU
3445 case GO_MENUS:
3446 gui.menu_is_active = TRUE;
3447 break;
3448#endif
3449 case GO_GREY:
3450 /* make menu's have grey items, ignored here */
3451 break;
3452#ifdef FEAT_TOOLBAR
3453 case GO_TOOLBAR:
3454 using_toolbar = TRUE;
3455 break;
3456#endif
3457#ifdef FEAT_FOOTER
3458 case GO_FOOTER:
3459 using_footer = TRUE;
3460 break;
3461#endif
3462 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 using_tearoff = TRUE;
3465#endif
3466 break;
3467 default:
3468 /* Ignore options that are not supported */
3469 break;
3470 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (gui.in_use)
3473 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003474 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003476
3477#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003478 /* Update the GUI tab line, it may appear or disappear. This may
3479 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003480 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003481 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003483 /* We don't want a resize event change "Rows" here, save and
3484 * restore it. Resizing is handled below. */
3485 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003486 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003487 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003488 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489 if (using_tabline)
3490 fix_size = TRUE;
3491 if (!gui_use_tabline())
3492 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3493 }
3494#endif
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 for (i = 0; i < 3; i++)
3497 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003498 /* The scrollbar needs to be updated when it is shown/unshown and
3499 * when switching tab pages. But the size only changes when it's
3500 * shown/unshown. Thus we need two places to remember whether a
3501 * scrollbar is there or not. */
3502 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003503 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003504 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 if (i == SBAR_BOTTOM)
3507 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3508 gui.which_scrollbars[i]);
3509 else
3510 {
3511 FOR_ALL_WINDOWS(wp)
3512 {
3513 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3514 }
3515 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003516 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3517 {
3518 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003519 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003520 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003521 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003522 if (gui.which_scrollbars[i])
3523 fix_size = TRUE;
3524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003526 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003527 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529
3530#ifdef FEAT_MENU
3531 if (gui.menu_is_active != prev_menu_is_active)
3532 {
3533 /* We don't want a resize event change "Rows" here, save and
3534 * restore it. Resizing is handled below. */
3535 i = Rows;
3536 gui_mch_enable_menu(gui.menu_is_active);
3537 Rows = i;
3538 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003539 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (gui.menu_is_active)
3541 fix_size = TRUE;
3542 }
3543#endif
3544
3545#ifdef FEAT_TOOLBAR
3546 if (using_toolbar != prev_toolbar)
3547 {
3548 gui_mch_show_toolbar(using_toolbar);
3549 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003550 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (using_toolbar)
3552 fix_size = TRUE;
3553 }
3554#endif
3555#ifdef FEAT_FOOTER
3556 if (using_footer != prev_footer)
3557 {
3558 gui_mch_enable_footer(using_footer);
3559 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003560 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (using_footer)
3562 fix_size = TRUE;
3563 }
3564#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003565#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (using_tearoff != prev_tearoff)
3567 {
3568 gui_mch_toggle_tearoffs(using_tearoff);
3569 prev_tearoff = using_tearoff;
3570 }
3571#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003572 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003573 {
3574#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003575 long prev_Columns = Columns;
3576 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 /* Adjust the size of the window to make the text area keep the
3579 * same size and to avoid that part of our window is off-screen
3580 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003581 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003582
3583#ifdef FEAT_GUI_GTK
3584 /* GTK has the annoying habit of sending us resize events when
3585 * changing the window size ourselves. This mostly happens when
3586 * waiting for a character to arrive, quite unpredictably, and may
3587 * change Columns and Rows when we don't want it. Wait for a
3588 * character here to avoid this effect.
3589 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003590 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003591 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003592 * Don't change Rows when adding menu/toolbar/tabline.
3593 * Don't change Columns when adding vertical toolbar. */
3594 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003595 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003596 if ((need_set_size & RESIZE_VERT) == 0)
3597 Rows = prev_Rows;
3598 if ((need_set_size & RESIZE_HOR) == 0)
3599 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003600#endif
3601 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003602 /* When the console tabline appears or disappears the window positions
3603 * change. */
3604 if (firstwin->w_winrow != tabline_height())
3605 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607}
3608
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003609#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3610/*
3611 * Return TRUE if the GUI is taking care of the tabline.
3612 * It may still be hidden if 'showtabline' is zero.
3613 */
3614 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003615gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003616{
3617 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3618}
3619
3620/*
3621 * Return TRUE if the GUI is showing the tabline.
3622 * This uses 'showtabline'.
3623 */
3624 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003625gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003626{
3627 if (!gui_use_tabline()
3628 || p_stal == 0
3629 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3630 return FALSE;
3631 return TRUE;
3632}
3633
3634/*
3635 * Update the tabline.
3636 * This may display/undisplay the tabline and update the labels.
3637 */
3638 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003639gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003640{
3641 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003642 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003643
3644 if (!gui.starting && starting == 0)
3645 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003646 /* Updating the tabline uses direct GUI commands, flush
3647 * outstanding instructions first. (esp. clear screen) */
3648 out_flush();
3649 gui_mch_flush();
3650
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003651 if (!showit != !shown)
3652 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003653 if (showit != 0)
3654 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003655
3656 /* When the tabs change from hidden to shown or from shown to
3657 * hidden the size of the text area should remain the same. */
3658 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003659 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003660 }
3661}
3662
3663/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003664 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665 */
3666 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003667get_tabline_label(
3668 tabpage_T *tp,
3669 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003670{
3671 int modified = FALSE;
3672 char_u buf[40];
3673 int wincount;
3674 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003675 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676
Bram Moolenaar57657d82006-04-21 22:12:41 +00003677 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003678 opt = (tooltip ? &p_gtt : &p_gtl);
3679 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003680 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003681 int use_sandbox = FALSE;
3682 int save_called_emsg = called_emsg;
3683 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003684 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003685 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3686 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003687
3688 called_emsg = FALSE;
3689
3690 printer_page_num = tabpage_index(tp);
3691# ifdef FEAT_EVAL
3692 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003693 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003694# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003695 /* It's almost as going to the tabpage, but without autocommands. */
3696 curtab->tp_firstwin = firstwin;
3697 curtab->tp_lastwin = lastwin;
3698 curtab->tp_curwin = curwin;
3699 save_curtab = curtab;
3700 curtab = tp;
3701 topframe = curtab->tp_topframe;
3702 firstwin = curtab->tp_firstwin;
3703 lastwin = curtab->tp_lastwin;
3704 curwin = curtab->tp_curwin;
3705 curbuf = curwin->w_buffer;
3706
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003707 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003708 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003709 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003710 STRCPY(NameBuff, res);
3711
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003712 /* Back to the original curtab. */
3713 curtab = save_curtab;
3714 topframe = curtab->tp_topframe;
3715 firstwin = curtab->tp_firstwin;
3716 lastwin = curtab->tp_lastwin;
3717 curwin = curtab->tp_curwin;
3718 curbuf = curwin->w_buffer;
3719
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003720 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003721 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003722 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003723 called_emsg |= save_called_emsg;
3724 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003725
3726 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3727 * use a default label. */
3728 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003729 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003730 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003731 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003732 if (!tooltip)
3733 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003734
3735 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3736 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3737 if (bufIsChanged(wp->w_buffer))
3738 modified = TRUE;
3739 if (modified || wincount > 1)
3740 {
3741 if (wincount > 1)
3742 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3743 else
3744 buf[0] = NUL;
3745 if (modified)
3746 STRCAT(buf, "+");
3747 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003748 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003749 mch_memmove(NameBuff, buf, STRLEN(buf));
3750 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003751 }
3752}
3753
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003754/*
3755 * Send the event for clicking to select tab page "nr".
3756 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003757 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003758 */
3759 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003760send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003761{
3762 char_u string[3];
3763
3764 if (nr == tabpage_index(curtab))
3765 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003766
3767 /* Don't put events in the input queue now. */
3768 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003769# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003770 || cmdwin_type != 0
3771# endif
3772 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003773 {
3774 /* Set it back to the current tab page. */
3775 gui_mch_set_curtab(tabpage_index(curtab));
3776 return FALSE;
3777 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003778
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003779 string[0] = CSI;
3780 string[1] = KS_TABLINE;
3781 string[2] = KE_FILLER;
3782 add_to_input_buf(string, 3);
3783 string[0] = nr;
3784 add_to_input_buf_csi(string, 1);
3785 return TRUE;
3786}
3787
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003788/*
3789 * Send a tabline menu event
3790 */
3791 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003792send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003793{
3794 char_u string[3];
3795
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003796 /* Don't put events in the input queue now. */
3797 if (hold_gui_events)
3798 return;
3799
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003800 string[0] = CSI;
3801 string[1] = KS_TABMENU;
3802 string[2] = KE_FILLER;
3803 add_to_input_buf(string, 3);
3804 string[0] = tabidx;
3805 string[1] = (char_u)(long)event;
3806 add_to_input_buf_csi(string, 2);
3807}
3808
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811/*
3812 * Scrollbar stuff:
3813 */
3814
Bram Moolenaare45828b2006-02-15 22:12:56 +00003815/*
3816 * Remove all scrollbars. Used before switching to another tab page.
3817 */
3818 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003819gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003820{
3821 int i;
3822 win_T *wp;
3823
3824 for (i = 0; i < 3; i++)
3825 {
3826 if (i == SBAR_BOTTOM)
3827 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3828 else
3829 {
3830 FOR_ALL_WINDOWS(wp)
3831 {
3832 gui_do_scrollbar(wp, i, FALSE);
3833 }
3834 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003835 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003836 }
3837}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003838
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003840gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841{
3842 static int sbar_ident = 0;
3843
3844 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3845 sb->wp = wp;
3846 sb->type = type;
3847 sb->value = 0;
3848#ifdef FEAT_GUI_ATHENA
3849 sb->pixval = 0;
3850#endif
3851 sb->size = 1;
3852 sb->max = 1;
3853 sb->top = 0;
3854 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 sb->status_height = 0;
3857 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3858}
3859
3860/*
3861 * Find the scrollbar with the given index.
3862 */
3863 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003864gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 win_T *wp;
3867
3868 if (gui.bottom_sbar.ident == ident)
3869 return &gui.bottom_sbar;
3870 FOR_ALL_WINDOWS(wp)
3871 {
3872 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3873 return &wp->w_scrollbars[SBAR_LEFT];
3874 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3875 return &wp->w_scrollbars[SBAR_RIGHT];
3876 }
3877 return NULL;
3878}
3879
3880/*
3881 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3882 *
3883 * For Win32, Macintosh and GTK+ 2:
3884 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3885 * you stop dragging the scrollbar. We get here each time the scrollbar is
3886 * dragged another pixel, but as far as the rest of vim goes, it thinks
3887 * we're just hanging in the call to DispatchMessage() in
3888 * process_message(). The DispatchMessage() call that hangs was passed a
3889 * mouse button click event in the scrollbar window. -- webb.
3890 *
3891 * Solution: Do the scrolling right here. But only when allowed.
3892 * Ignore the scrollbars while executing an external command or when there
3893 * are still characters to be processed.
3894 */
3895 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003896gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 int sb_num;
3900#ifdef USE_ON_FLY_SCROLL
3901 colnr_T old_leftcol = curwin->w_leftcol;
3902# ifdef FEAT_SCROLLBIND
3903 linenr_T old_topline = curwin->w_topline;
3904# endif
3905# ifdef FEAT_DIFF
3906 int old_topfill = curwin->w_topfill;
3907# endif
3908#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003909 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 int byte_count;
3911#endif
3912
3913 if (sb == NULL)
3914 return;
3915
3916 /* Don't put events in the input queue now. */
3917 if (hold_gui_events)
3918 return;
3919
3920#ifdef FEAT_CMDWIN
3921 if (cmdwin_type != 0 && sb->wp != curwin)
3922 return;
3923#endif
3924
3925 if (still_dragging)
3926 {
3927 if (sb->wp == NULL)
3928 gui.dragged_sb = SBAR_BOTTOM;
3929 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3930 gui.dragged_sb = SBAR_LEFT;
3931 else
3932 gui.dragged_sb = SBAR_RIGHT;
3933 gui.dragged_wp = sb->wp;
3934 }
3935 else
3936 {
3937 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003938#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003940 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 gui.dragged_wp = NULL;
3942#endif
3943 }
3944
3945 /* Vertical sbar info is kept in the first sbar (the left one) */
3946 if (sb->wp != NULL)
3947 sb = &sb->wp->w_scrollbars[0];
3948
3949 /*
3950 * Check validity of value
3951 */
3952 if (value < 0)
3953 value = 0;
3954#ifdef SCROLL_PAST_END
3955 else if (value > sb->max)
3956 value = sb->max;
3957#else
3958 if (value > sb->max - sb->size + 1)
3959 value = sb->max - sb->size + 1;
3960#endif
3961
3962 sb->value = value;
3963
3964#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003965 /* When not allowed to do the scrolling right now, return.
3966 * This also checked input_available(), but that causes the first click in
3967 * a scrollbar to be ignored when Vim doesn't have focus. */
3968 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return;
3970#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003971#ifdef FEAT_INS_EXPAND
3972 /* Disallow scrolling the current window when the completion popup menu is
3973 * visible. */
3974 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3975 return;
3976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978#ifdef FEAT_RIGHTLEFT
3979 if (sb->wp == NULL && curwin->w_p_rl)
3980 {
3981 value = sb->max + 1 - sb->size - value;
3982 if (value < 0)
3983 value = 0;
3984 }
3985#endif
3986
3987 if (sb->wp != NULL) /* vertical scrollbar */
3988 {
3989 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3991 sb_num++;
3992 if (wp == NULL)
3993 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995#ifdef USE_ON_FLY_SCROLL
3996 current_scrollbar = sb_num;
3997 scrollbar_value = value;
3998 if (State & NORMAL)
3999 {
4000 gui_do_scroll();
4001 setcursor();
4002 }
4003 else if (State & INSERT)
4004 {
4005 ins_scroll();
4006 setcursor();
4007 }
4008 else if (State & CMDLINE)
4009 {
4010 if (msg_scrolled == 0)
4011 {
4012 gui_do_scroll();
4013 redrawcmdline();
4014 }
4015 }
4016# ifdef FEAT_FOLDING
4017 /* Value may have been changed for closed fold. */
4018 sb->value = sb->wp->w_topline - 1;
4019# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004020
4021 /* When dragging one scrollbar and there is another one at the other
4022 * side move the thumb of that one too. */
4023 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4024 gui_mch_set_scrollbar_thumb(
4025 &sb->wp->w_scrollbars[
4026 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4027 ? SBAR_LEFT : SBAR_RIGHT],
4028 sb->value, sb->size, sb->max);
4029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#else
4031 bytes[0] = CSI;
4032 bytes[1] = KS_VER_SCROLLBAR;
4033 bytes[2] = KE_FILLER;
4034 bytes[3] = (char_u)sb_num;
4035 byte_count = 4;
4036#endif
4037 }
4038 else
4039 {
4040#ifdef USE_ON_FLY_SCROLL
4041 scrollbar_value = value;
4042
4043 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004044 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 else if (State & INSERT)
4046 ins_horscroll();
4047 else if (State & CMDLINE)
4048 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004049 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004051 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 redrawcmdline();
4053 }
4054 }
4055 if (old_leftcol != curwin->w_leftcol)
4056 {
4057 updateWindow(curwin); /* update window, status and cmdline */
4058 setcursor();
4059 }
4060#else
4061 bytes[0] = CSI;
4062 bytes[1] = KS_HOR_SCROLLBAR;
4063 bytes[2] = KE_FILLER;
4064 byte_count = 3;
4065#endif
4066 }
4067
4068#ifdef USE_ON_FLY_SCROLL
4069# ifdef FEAT_SCROLLBIND
4070 /*
4071 * synchronize other windows, as necessary according to 'scrollbind'
4072 */
4073 if (curwin->w_p_scb
4074 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4075 || (sb->wp == curwin && (curwin->w_topline != old_topline
4076# ifdef FEAT_DIFF
4077 || curwin->w_topfill != old_topfill
4078# endif
4079 ))))
4080 {
4081 do_check_scrollbind(TRUE);
4082 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004083 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (wp->w_redr_type > 0)
4085 updateWindow(wp);
4086 setcursor();
4087 }
4088# endif
4089 out_flush();
4090 gui_update_cursor(FALSE, TRUE);
4091#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004092 add_to_input_buf(bytes, byte_count);
4093 add_long_to_buf((long_u)value, bytes);
4094 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#endif
4096}
4097
4098/*
4099 * Scrollbar stuff:
4100 */
4101
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004102/*
4103 * Called when something in the window layout has changed.
4104 */
4105 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004106gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004107{
4108 if (gui.in_use && starting == 0)
4109 {
4110 out_flush();
4111 gui_init_which_components(NULL);
4112 gui_update_scrollbars(TRUE);
4113 }
4114 need_mouse_correct = TRUE;
4115}
4116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004118gui_update_scrollbars(
4119 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 win_T *wp;
4122 scrollbar_T *sb;
4123 long val, size, max; /* need 32 bits here */
4124 int which_sb;
4125 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
4128 /* Update the horizontal scrollbar */
4129 gui_update_horiz_scrollbar(force);
4130
4131#ifndef WIN3264
4132 /* Return straight away if there is neither a left nor right scrollbar.
4133 * On MS-Windows this is required anyway for scrollwheel messages. */
4134 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4135 return;
4136#endif
4137
4138 /*
4139 * Don't want to update a scrollbar while we're dragging it. But if we
4140 * have both a left and right scrollbar, and we drag one of them, we still
4141 * need to update the other one.
4142 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004143 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4144 && gui.which_scrollbars[SBAR_LEFT]
4145 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
4147 /*
4148 * If we have two scrollbars and one of them is being dragged, just
4149 * copy the scrollbar position from the dragged one to the other one.
4150 */
4151 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4152 if (gui.dragged_wp != NULL)
4153 gui_mch_set_scrollbar_thumb(
4154 &gui.dragged_wp->w_scrollbars[which_sb],
4155 gui.dragged_wp->w_scrollbars[0].value,
4156 gui.dragged_wp->w_scrollbars[0].size,
4157 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159
4160 /* avoid that moving components around generates events */
4161 ++hold_gui_events;
4162
Bram Moolenaar45a24952016-07-24 22:25:15 +02004163 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 if (wp->w_buffer == NULL) /* just in case */
4166 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004167 /* Skip a scrollbar that is being dragged. */
4168 if (!force && (gui.dragged_sb == SBAR_LEFT
4169 || gui.dragged_sb == SBAR_RIGHT)
4170 && gui.dragged_wp == wp)
4171 continue;
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#ifdef SCROLL_PAST_END
4174 max = wp->w_buffer->b_ml.ml_line_count - 1;
4175#else
4176 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4177#endif
4178 if (max < 0) /* empty buffer */
4179 max = 0;
4180 val = wp->w_topline - 1;
4181 size = wp->w_height;
4182#ifdef SCROLL_PAST_END
4183 if (val > max) /* just in case */
4184 val = max;
4185#else
4186 if (size > max + 1) /* just in case */
4187 size = max + 1;
4188 if (val > max - size + 1)
4189 val = max - size + 1;
4190#endif
4191 if (val < 0) /* minimal value is 0 */
4192 val = 0;
4193
4194 /*
4195 * Scrollbar at index 0 (the left one) contains all the information.
4196 * It would be the same info for left and right so we just store it for
4197 * one of them.
4198 */
4199 sb = &wp->w_scrollbars[0];
4200
4201 /*
4202 * Note: no check for valid w_botline. If it's not valid the
4203 * scrollbars will be updated later anyway.
4204 */
4205 if (size < 1 || wp->w_botline - 2 > max)
4206 {
4207 /*
4208 * This can happen during changing files. Just don't update the
4209 * scrollbar for now.
4210 */
4211 sb->height = 0; /* Force update next time */
4212 if (gui.which_scrollbars[SBAR_LEFT])
4213 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4214 if (gui.which_scrollbars[SBAR_RIGHT])
4215 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4216 continue;
4217 }
4218 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 || sb->top != wp->w_winrow
4220 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004222 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 /* Height, width or position of scrollbar has changed. For
4225 * vertical split: curwin changed. */
4226 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 sb->top = wp->w_winrow;
4228 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /* Calculate height and position in pixels */
4232 h = (sb->height + sb->status_height) * gui.char_height;
4233 y = sb->top * gui.char_height + gui.border_offset;
4234#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4235 if (gui.menu_is_active)
4236 y += gui.menu_height;
4237#endif
4238
4239#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4240 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4241# ifdef FEAT_GUI_ATHENA
4242 y += gui.toolbar_height;
4243# else
4244# ifdef FEAT_GUI_MSWIN
4245 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4246# endif
4247# endif
4248#endif
4249
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004250#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4251 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004252 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004253#endif
4254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 /* Height of top scrollbar includes width of top border */
4258 h += gui.border_offset;
4259 y -= gui.border_offset;
4260 }
4261 if (gui.which_scrollbars[SBAR_LEFT])
4262 {
4263 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4264 gui.left_sbar_x, y,
4265 gui.scrollbar_width, h);
4266 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4267 }
4268 if (gui.which_scrollbars[SBAR_RIGHT])
4269 {
4270 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4271 gui.right_sbar_x, y,
4272 gui.scrollbar_width, h);
4273 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4274 }
4275 }
4276
4277 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4278 * checking if the thumb moved at least a pixel. Only do this for
4279 * Athena, most other GUIs require the update anyway to make the
4280 * arrows work. */
4281#ifdef FEAT_GUI_ATHENA
4282 if (max == 0)
4283 y = 0;
4284 else
4285 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4286 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4287#else
4288 if (force || sb->value != val || sb->size != size || sb->max != max)
4289#endif
4290 {
4291 /* Thumb of scrollbar has moved */
4292 sb->value = val;
4293#ifdef FEAT_GUI_ATHENA
4294 sb->pixval = y;
4295#endif
4296 sb->size = size;
4297 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004298 if (gui.which_scrollbars[SBAR_LEFT]
4299 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4301 val, size, max);
4302 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004303 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4305 val, size, max);
4306 }
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 --hold_gui_events;
4310}
4311
4312/*
4313 * Enable or disable a scrollbar.
4314 * Check for scrollbars for vertically split windows which are not enabled
4315 * sometimes.
4316 */
4317 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004318gui_do_scrollbar(
4319 win_T *wp,
4320 int which, /* SBAR_LEFT or SBAR_RIGHT */
4321 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 int midcol = curwin->w_wincol + curwin->w_width / 2;
4324 int has_midcol = (wp->w_wincol <= midcol
4325 && wp->w_wincol + wp->w_width >= midcol);
4326
4327 /* Only enable scrollbars that contain the middle column of the current
4328 * window. */
4329 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4330 {
4331 /* Scrollbars only on one side. Don't enable scrollbars that don't
4332 * contain the middle column of the current window. */
4333 if (!has_midcol)
4334 enable = FALSE;
4335 }
4336 else
4337 {
4338 /* Scrollbars on both sides. Don't enable scrollbars that neither
4339 * contain the middle column of the current window nor are on the far
4340 * side. */
4341 if (midcol > Columns / 2)
4342 {
4343 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4344 enable = FALSE;
4345 }
4346 else
4347 {
4348 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4349 : !has_midcol)
4350 enable = FALSE;
4351 }
4352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4354}
4355
4356/*
4357 * Scroll a window according to the values set in the globals current_scrollbar
4358 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4359 * or FALSE otherwise.
4360 */
4361 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004362gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
4364 win_T *wp, *save_wp;
4365 int i;
4366 long nlines;
4367 pos_T old_cursor;
4368 linenr_T old_topline;
4369#ifdef FEAT_DIFF
4370 int old_topfill;
4371#endif
4372
4373 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4374 if (wp == NULL)
4375 break;
4376 if (wp == NULL)
4377 /* Couldn't find window */
4378 return FALSE;
4379
4380 /*
4381 * Compute number of lines to scroll. If zero, nothing to do.
4382 */
4383 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4384 if (nlines == 0)
4385 return FALSE;
4386
4387 save_wp = curwin;
4388 old_topline = wp->w_topline;
4389#ifdef FEAT_DIFF
4390 old_topfill = wp->w_topfill;
4391#endif
4392 old_cursor = wp->w_cursor;
4393 curwin = wp;
4394 curbuf = wp->w_buffer;
4395 if (nlines < 0)
4396 scrolldown(-nlines, gui.dragged_wp == NULL);
4397 else
4398 scrollup(nlines, gui.dragged_wp == NULL);
4399 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4400 * the mouse-up event already, but we still want it to behave like when
4401 * dragging. But not the next click in an arrow. */
4402 if (gui.dragged_sb == SBAR_NONE)
4403 gui.dragged_wp = NULL;
4404
4405 if (old_topline != wp->w_topline
4406#ifdef FEAT_DIFF
4407 || old_topfill != wp->w_topfill
4408#endif
4409 )
4410 {
4411 if (p_so != 0)
4412 {
4413 cursor_correct(); /* fix window for 'so' */
4414 update_topline(); /* avoid up/down jump */
4415 }
4416 if (old_cursor.lnum != wp->w_cursor.lnum)
4417 coladvance(wp->w_curswant);
4418#ifdef FEAT_SCROLLBIND
4419 wp->w_scbind_pos = wp->w_topline;
4420#endif
4421 }
4422
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004423 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4424 validate_cursor();
4425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 curwin = save_wp;
4427 curbuf = save_wp->w_buffer;
4428
4429 /*
4430 * Don't call updateWindow() when nothing has changed (it will overwrite
4431 * the status line!).
4432 */
4433 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004434 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#ifdef FEAT_DIFF
4436 || old_topfill != wp->w_topfill
4437#endif
4438 )
4439 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004440 int type = VALID;
4441
4442#ifdef FEAT_INS_EXPAND
4443 if (pum_visible())
4444 {
4445 type = NOT_VALID;
4446 wp->w_lines_valid = 0;
4447 }
4448#endif
4449 /* Don't set must_redraw here, it may cause the popup menu to
4450 * disappear when losing focus after a scrollbar drag. */
4451 if (wp->w_redr_type < type)
4452 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 updateWindow(wp); /* update window, status line, and cmdline */
4454 }
4455
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004456#ifdef FEAT_INS_EXPAND
4457 /* May need to redraw the popup menu. */
4458 if (pum_visible())
4459 pum_redraw();
4460#endif
4461
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004462 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463}
4464
4465
4466/*
4467 * Horizontal scrollbar stuff:
4468 */
4469
4470/*
4471 * Return length of line "lnum" for horizontal scrolling.
4472 */
4473 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004474scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 char_u *p;
4477 colnr_T col;
4478 int w;
4479
4480 p = ml_get(lnum);
4481 col = 0;
4482 if (*p != NUL)
4483 for (;;)
4484 {
4485 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004486 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (*p == NUL) /* don't count the last character */
4488 break;
4489 col += w;
4490 }
4491 return col;
4492}
4493
4494/* Remember which line is currently the longest, so that we don't have to
4495 * search for it when scrolling horizontally. */
4496static linenr_T longest_lnum = 0;
4497
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004498/*
4499 * Find longest visible line number. If this is not possible (or not desired,
4500 * by setting 'h' in "guioptions") then the current line number is returned.
4501 */
4502 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004503gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004504{
4505 linenr_T ret = 0;
4506
4507 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4508 * line numbers, topline and botline can be invalid when displaying is
4509 * postponed. */
4510 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4511 && curwin->w_topline <= curwin->w_cursor.lnum
4512 && curwin->w_botline > curwin->w_cursor.lnum
4513 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4514 {
4515 linenr_T lnum;
4516 colnr_T n;
4517 long max = 0;
4518
4519 /* Use maximum of all visible lines. Remember the lnum of the
4520 * longest line, closest to the cursor line. Used when scrolling
4521 * below. */
4522 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4523 {
4524 n = scroll_line_len(lnum);
4525 if (n > (colnr_T)max)
4526 {
4527 max = n;
4528 ret = lnum;
4529 }
4530 else if (n == (colnr_T)max
4531 && abs((int)(lnum - curwin->w_cursor.lnum))
4532 < abs((int)(ret - curwin->w_cursor.lnum)))
4533 ret = lnum;
4534 }
4535 }
4536 else
4537 /* Use cursor line only. */
4538 ret = curwin->w_cursor.lnum;
4539
4540 return ret;
4541}
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004544gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 long value, size, max; /* need 32 bit ints here */
4547
4548 if (!gui.which_scrollbars[SBAR_BOTTOM])
4549 return;
4550
4551 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4552 return;
4553
4554 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4555 return;
4556
4557 /*
4558 * It is possible for the cursor to be invalid if we're in the middle of
4559 * something (like changing files). If so, don't do anything for now.
4560 */
4561 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4562 {
4563 gui.bottom_sbar.value = -1;
4564 return;
4565 }
4566
Bram Moolenaar02631462017-09-22 15:20:32 +02004567 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (curwin->w_p_wrap)
4569 {
4570 value = 0;
4571#ifdef SCROLL_PAST_END
4572 max = 0;
4573#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004574 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#endif
4576 }
4577 else
4578 {
4579 value = curwin->w_leftcol;
4580
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004581 longest_lnum = gui_find_longest_lnum();
4582 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_VIRTUALEDIT
4585 if (virtual_active())
4586 {
4587 /* May move the cursor even further to the right. */
4588 if (curwin->w_virtcol >= (colnr_T)max)
4589 max = curwin->w_virtcol;
4590 }
4591#endif
4592
4593#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004594 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#endif
4596 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004597 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 size -= curwin_col_off();
4599#ifndef SCROLL_PAST_END
4600 max -= curwin_col_off();
4601#endif
4602 }
4603
4604#ifndef SCROLL_PAST_END
4605 if (value > max - size + 1)
4606 value = max - size + 1; /* limit the value to allowable range */
4607#endif
4608
4609#ifdef FEAT_RIGHTLEFT
4610 if (curwin->w_p_rl)
4611 {
4612 value = max + 1 - size - value;
4613 if (value < 0)
4614 {
4615 size += value;
4616 value = 0;
4617 }
4618 }
4619#endif
4620 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4621 && max == gui.bottom_sbar.max)
4622 return;
4623
4624 gui.bottom_sbar.value = value;
4625 gui.bottom_sbar.size = size;
4626 gui.bottom_sbar.max = max;
4627 gui.prev_wrap = curwin->w_p_wrap;
4628
4629 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4630}
4631
4632/*
4633 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4634 */
4635 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004636gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
4638 /* no wrapping, no scrolling */
4639 if (curwin->w_p_wrap)
4640 return FALSE;
4641
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004642 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FALSE;
4644
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004645 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
4647 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004648 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004650 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004651 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004653 if (compute_longest_lnum)
4654 {
4655 curwin->w_cursor.lnum = gui_find_longest_lnum();
4656 curwin->w_cursor.col = 0;
4657 }
4658 /* Do a sanity check on "longest_lnum", just in case. */
4659 else if (longest_lnum >= curwin->w_topline
4660 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
4662 curwin->w_cursor.lnum = longest_lnum;
4663 curwin->w_cursor.col = 0;
4664 }
4665 }
4666
4667 return leftcol_changed();
4668}
4669
4670/*
4671 * Check that none of the colors are the same as the background color
4672 */
4673 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004674gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4677 {
4678 gui_set_bg_color((char_u *)"White");
4679 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4680 gui_set_fg_color((char_u *)"Black");
4681 }
4682}
4683
Bram Moolenaar3918c952005-03-15 22:34:55 +00004684 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004685gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 gui.norm_pixel = gui_get_color(name);
4688 hl_set_fg_color_name(vim_strsave(name));
4689}
4690
Bram Moolenaar3918c952005-03-15 22:34:55 +00004691 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004692gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 gui.back_pixel = gui_get_color(name);
4695 hl_set_bg_color_name(vim_strsave(name));
4696}
4697
4698/*
4699 * Allocate a color by name.
4700 * Returns INVALCOLOR and gives an error message when failed.
4701 */
4702 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004703gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 guicolor_T t;
4706
4707 if (*name == NUL)
4708 return INVALCOLOR;
4709 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004712#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 && gui.in_use
4714#endif
4715 )
4716 EMSG2(_("E254: Cannot allocate color %s"), name);
4717 return t;
4718}
4719
4720/*
4721 * Return the grey value of a color (range 0-255).
4722 */
4723 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004724gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004726 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004728 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004729 + (((rgb >> 8) & 0xff) * 587)
4730 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731}
4732
4733#if defined(FEAT_GUI_X11) || defined(PROTO)
4734 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004735gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 win_T *wp;
4738
4739 /* Nothing to do if GUI hasn't started yet. */
4740 if (!gui.in_use)
4741 return;
4742
4743 FOR_ALL_WINDOWS(wp)
4744 {
4745 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4746 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4747 }
4748 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4749}
4750#endif
4751
4752/*
4753 * Call this when focus has changed.
4754 */
4755 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004756gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758/*
4759 * Skip this code to avoid drawing the cursor when debugging and switching
4760 * between the debugger window and gvim.
4761 */
4762#if 1
4763 gui.in_focus = in_focus;
4764 out_flush(); /* make sure output has been written */
4765 gui_update_cursor(TRUE, FALSE);
4766
4767# ifdef FEAT_XIM
4768 xim_set_focus(in_focus);
4769# endif
4770
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004771 /* Put events in the input queue only when allowed.
4772 * ui_focus_change() isn't called directly, because it invokes
4773 * autocommands and that must not happen asynchronously. */
4774 if (!hold_gui_events)
4775 {
4776 char_u bytes[3];
4777
4778 bytes[0] = CSI;
4779 bytes[1] = KS_EXTRA;
4780 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4781 add_to_input_buf(bytes, 3);
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783#endif
4784}
4785
4786/*
4787 * Called when the mouse moved (but not when dragging).
4788 */
4789 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004790gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
4792 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004793 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaard3667a22006-03-16 21:35:52 +00004795 /* Ignore this while still starting up. */
4796 if (!gui.in_use || gui.starting)
4797 return;
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#ifdef FEAT_MOUSESHAPE
4800 /* Get window pointer, and update mouse shape as well. */
4801 wp = xy2win(x, y);
4802#endif
4803
4804 /* Only handle this when 'mousefocus' set and ... */
4805 if (p_mousef
4806 && !hold_gui_events /* not holding events */
4807 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4808 && State != HITRETURN /* but not hit-return prompt */
4809 && msg_scrolled == 0 /* no scrolled message */
4810 && !need_mouse_correct /* not moving the pointer */
4811 && gui.in_focus) /* gvim in focus */
4812 {
4813 /* Don't move the mouse when it's left or right of the Vim window */
4814 if (x < 0 || x > Columns * gui.char_width)
4815 return;
4816#ifndef FEAT_MOUSESHAPE
4817 wp = xy2win(x, y);
4818#endif
4819 if (wp == curwin || wp == NULL)
4820 return; /* still in the same old window, or none at all */
4821
Bram Moolenaar9c102382006-05-03 21:26:49 +00004822 /* Ignore position in the tab pages line. */
4823 if (Y_2_ROW(y) < tabline_height())
4824 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 /*
4827 * format a mouse click on status line input
4828 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004829 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4830 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 */
4832 if (finish_op)
4833 {
4834 /* abort the current operator first */
4835 st[0] = ESC;
4836 add_to_input_buf(st, 1);
4837 }
4838 st[0] = CSI;
4839 st[1] = KS_MOUSE;
4840 st[2] = KE_FILLER;
4841 st[3] = (char_u)MOUSE_LEFT;
4842 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004843 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 wp->w_height + W_WINROW(wp));
4845
4846 add_to_input_buf(st, 8);
4847 st[3] = (char_u)MOUSE_RELEASE;
4848 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849#ifdef FEAT_GUI_GTK
4850 /* Need to wake up the main loop */
4851 if (gtk_main_level() > 0)
4852 gtk_main_quit();
4853#endif
4854 }
4855}
4856
4857/*
4858 * Called when mouse should be moved to window with focus.
4859 */
4860 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004861gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 int x, y;
4864 win_T *wp = NULL;
4865
4866 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004867
4868 if (!(gui.in_use && p_mousef))
4869 return;
4870
4871 gui_mch_getmouse(&x, &y);
4872 /* Don't move the mouse when it's left or right of the Vim window */
4873 if (x < 0 || x > Columns * gui.char_width)
4874 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004875 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004876 wp = xy2win(x, y);
4877 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004879 validate_cline_row();
4880 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4881 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884}
4885
4886/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004887 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004890xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 int row;
4893 int col;
4894 win_T *wp;
4895
4896 row = Y_2_ROW(y);
4897 col = X_2_COL(x);
4898 if (row < 0 || col < 0) /* before first window */
4899 return NULL;
4900 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004901 if (wp == NULL)
4902 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004903#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (State == HITRETURN || State == ASKMORE)
4905 {
4906 if (Y_2_ROW(y) >= msg_row)
4907 update_mouseshape(SHAPE_IDX_MOREL);
4908 else
4909 update_mouseshape(SHAPE_IDX_MORE);
4910 }
4911 else if (row > wp->w_height) /* below status line */
4912 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004914 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004917 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 update_mouseshape(SHAPE_IDX_STATUS);
4919 else
4920 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004922 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923}
4924
4925/*
4926 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4927 * File names may be given to redefine the args list.
4928 */
4929 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004930ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
4932 char_u *arg = eap->arg;
4933
4934 /*
4935 * Check for "-f" argument: foreground, don't fork.
4936 * Also don't fork when started with "gvim -f".
4937 * Do fork when using "gui -b".
4938 */
4939 if (arg[0] == '-'
4940 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004941 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 gui.dofork = (arg[1] == 'b');
4944 eap->arg = skipwhite(eap->arg + 2);
4945 }
4946 if (!gui.in_use)
4947 {
4948 /* Clear the command. Needed for when forking+exiting, to avoid part
4949 * of the argument ending up after the shell prompt. */
4950 msg_clr_eos_force();
4951 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004952#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004953 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 if (!ends_excmd(*eap->arg))
4957 ex_next(eap);
4958}
4959
4960#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004961 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
4963 * This is shared between Athena, Motif and GTK.
4964 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004965static void gfp_setname(char_u *fname, void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967/*
4968 * Callback function for do_in_runtimepath().
4969 */
4970 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004971gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004973 char_u *gfp_buffer = cookie;
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (STRLEN(fname) >= MAXPATHL)
4976 *gfp_buffer = NUL;
4977 else
4978 STRCPY(gfp_buffer, fname);
4979}
4980
4981/*
4982 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4983 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4984 */
4985 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004986gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987{
4988 if (STRLEN(name) > MAXPATHL - 14)
4989 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004990 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004991 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004992 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 return FAIL;
4994 return OK;
4995}
4996
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004997# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998/*
4999 * Given the name of the "icon=" argument, try finding the bitmap file for the
5000 * icon. If it is an absolute path name, use it as it is. Otherwise append
5001 * "ext" and search for it in 'runtimepath'.
5002 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5003 * contains "name".
5004 */
5005 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005006gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
5008 char_u buf[MAXPATHL + 1];
5009
5010 expand_env(name, buffer, MAXPATHL);
5011 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5012 STRCPY(buffer, buf);
5013}
5014# endif
5015#endif
5016
Bram Moolenaar9372a112005-12-06 19:59:18 +00005017#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005019display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 char_u *p;
5022
5023 if (isatty(2))
5024 fflush(stderr);
5025 else if (error_ga.ga_data != NULL)
5026 {
5027 /* avoid putting up a message box with blanks only */
5028 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5029 if (!isspace(*p))
5030 {
5031 /* Truncate a very long message, it will go off-screen. */
5032 if (STRLEN(p) > 2000)
5033 STRCPY(p + 2000 - 14, "...(truncated)");
5034 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005035 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 break;
5037 }
5038 ga_clear(&error_ga);
5039 }
5040}
5041#endif
5042
5043#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5044/*
5045 * Return TRUE if still starting up and there is no place to enter text.
5046 * For GTK and X11 we check if stderr is not a tty, which means we were
5047 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5048 * allow typing on stdin.
5049 */
5050 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005051no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
5053 return ((!gui.in_use || gui.starting)
5054# ifndef NO_CONSOLE
5055 && !isatty(0) && !isatty(2)
5056# endif
5057 );
5058}
5059#endif
5060
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005061#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005062 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005063 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064/*
5065 * Update the current window and the screen.
5066 */
5067 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005068gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005070#ifdef FEAT_CONCEAL
5071 linenr_T conceal_old_cursor_line = 0;
5072 linenr_T conceal_new_cursor_line = 0;
5073 int conceal_update_lines = FALSE;
5074#endif
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 update_topline();
5077 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005078
5079#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005080 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005081 if (!finish_op && (
5082# ifdef FEAT_AUTOCMD
5083 has_cursormoved()
5084# endif
5085# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5086 ||
5087# endif
5088# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005089 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005090# endif
5091 )
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005092 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005093 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005094# ifdef FEAT_AUTOCMD
5095 if (has_cursormoved())
5096 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5097# endif
5098# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005099 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005100 {
5101 conceal_old_cursor_line = last_cursormoved.lnum;
5102 conceal_new_cursor_line = curwin->w_cursor.lnum;
5103 conceal_update_lines = TRUE;
5104 }
5105# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005106 last_cursormoved = curwin->w_cursor;
5107 }
5108#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 update_screen(0); /* may need to update the screen */
5111 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005112# if defined(FEAT_CONCEAL)
5113 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005114 && (conceal_old_cursor_line != conceal_new_cursor_line
5115 || conceal_cursor_line(curwin)
5116 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005117 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005118 if (conceal_old_cursor_line != conceal_new_cursor_line)
5119 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005120 update_single_line(curwin, conceal_new_cursor_line);
5121 curwin->w_valid &= ~VALID_CROW;
5122 }
5123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 out_flush(); /* make sure output has been written */
5125 gui_update_cursor(TRUE, FALSE);
5126 gui_mch_flush();
5127}
5128#endif
5129
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005130#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005131static void concat_esc(garray_T *gap, char_u *text, int what);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133/*
5134 * Get the text to use in a find/replace dialog. Uses the last search pattern
5135 * if the argument is empty.
5136 * Returns an allocated string.
5137 */
5138 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005139get_find_dialog_text(
5140 char_u *arg,
5141 int *wwordp, /* return: TRUE if \< \> found */
5142 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 char_u *text;
5145
5146 if (*arg == NUL)
5147 text = last_search_pat();
5148 else
5149 text = arg;
5150 if (text != NULL)
5151 {
5152 text = vim_strsave(text);
5153 if (text != NULL)
5154 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005155 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 int i;
5157
5158 /* Remove "\V" */
5159 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5160 {
5161 mch_memmove(text, text + 2, (size_t)(len - 1));
5162 len -= 2;
5163 }
5164
5165 /* Recognize "\c" and "\C" and remove. */
5166 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5167 {
5168 *mcasep = (text[1] == 'C');
5169 mch_memmove(text, text + 2, (size_t)(len - 1));
5170 len -= 2;
5171 }
5172
5173 /* Recognize "\<text\>" and remove. */
5174 if (len >= 4
5175 && STRNCMP(text, "\\<", 2) == 0
5176 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5177 {
5178 *wwordp = TRUE;
5179 mch_memmove(text, text + 2, (size_t)(len - 4));
5180 text[len - 4] = NUL;
5181 }
5182
5183 /* Recognize "\/" or "\?" and remove. */
5184 for (i = 0; i + 1 < len; ++i)
5185 if (text[i] == '\\' && (text[i + 1] == '/'
5186 || text[i + 1] == '?'))
5187 {
5188 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5189 --len;
5190 }
5191 }
5192 }
5193 return text;
5194}
5195
5196/*
5197 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5198 */
5199 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005200concat_esc(garray_T *gap, char_u *text, int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 while (*text != NUL)
5203 {
5204#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005205 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (l > 1)
5208 {
5209 while (--l >= 0)
5210 ga_append(gap, *text++);
5211 continue;
5212 }
5213#endif
5214 if (*text == what)
5215 ga_append(gap, '\\');
5216 ga_append(gap, *text);
5217 ++text;
5218 }
5219}
5220
5221/*
5222 * Handle the press of a button in the find-replace dialog.
5223 * Return TRUE when something was added to the input buffer.
5224 */
5225 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005226gui_do_findrepl(
5227 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5228 char_u *find_text,
5229 char_u *repl_text,
5230 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 garray_T ga;
5233 int i;
5234 int type = (flags & FRD_TYPE_MASK);
5235 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005236 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005237 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005238 static int busy = FALSE;
5239
5240 /* When the screen is being updated we should not change buffers and
5241 * windows structures, it may cause freed memory to be used. Also don't
5242 * do this recursively (pressing "Find" quickly several times. */
5243 if (updating_screen || busy)
5244 return FALSE;
5245
5246 /* refuse replace when text cannot be changed */
5247 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5248 return FALSE;
5249
5250 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
5252 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005253 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ga_concat(&ga, (char_u *)"%s/");
5255
5256 ga_concat(&ga, (char_u *)"\\V");
5257 if (flags & FRD_MATCH_CASE)
5258 ga_concat(&ga, (char_u *)"\\C");
5259 else
5260 ga_concat(&ga, (char_u *)"\\c");
5261 if (flags & FRD_WHOLE_WORD)
5262 ga_concat(&ga, (char_u *)"\\<");
5263 if (type == FRD_REPLACEALL || down)
5264 concat_esc(&ga, find_text, '/'); /* escape slashes */
5265 else
5266 concat_esc(&ga, find_text, '?'); /* escape '?' */
5267 if (flags & FRD_WHOLE_WORD)
5268 ga_concat(&ga, (char_u *)"\\>");
5269
5270 if (type == FRD_REPLACEALL)
5271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005273 /* escape / and \ */
5274 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5275 if (p != NULL)
5276 ga_concat(&ga, p);
5277 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005279 }
5280 ga_append(&ga, NUL);
5281
5282 if (type == FRD_REPLACE)
5283 {
5284 /* Do the replacement when the text at the cursor matches. Thus no
5285 * replacement is done if the cursor was moved! */
5286 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5287 regmatch.rm_ic = 0;
5288 if (regmatch.regprog != NULL)
5289 {
5290 p = ml_get_cursor();
5291 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5292 && regmatch.startp[0] == p)
5293 {
5294 /* Clear the command line to remove any old "No match"
5295 * error. */
5296 msg_end_prompt();
5297
5298 if (u_save_cursor() == OK)
5299 {
5300 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005301 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005302
5303 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005304 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005305 ins_str(repl_text);
5306 }
5307 }
5308 else
5309 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005310 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005311 }
5312 }
5313
5314 if (type == FRD_REPLACEALL)
5315 {
5316 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005317 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 do_cmdline_cmd(ga.ga_data);
5319 }
5320 else
5321 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005322 int searchflags = SEARCH_MSG + SEARCH_MARK;
5323
5324 /* Search for the next match.
5325 * Don't skip text under cursor for single replace. */
5326 if (type == FRD_REPLACE)
5327 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 i = msg_scroll;
Bram Moolenaarcde88542015-08-11 19:14:00 +02005329 (void)do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005330 searchflags, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 msg_scroll = i; /* don't let an error message set msg_scroll */
5332 }
5333
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005334 /* Don't want to pass did_emsg to other code, it may cause disabling
5335 * syntax HL if we were busy redrawing. */
5336 did_emsg = save_did_emsg;
5337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (State & (NORMAL | INSERT))
5339 {
5340 gui_update_screen(); /* update the screen */
5341 msg_didout = 0; /* overwrite any message */
5342 need_wait_return = FALSE; /* don't wait for return */
5343 }
5344
5345 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005346 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 return (ga.ga_len > 0);
5348}
5349
5350#endif
5351
5352#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5353 || defined(FEAT_GUI_MSWIN) \
5354 || defined(FEAT_GUI_MAC) \
5355 || defined(PROTO)
5356
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005357static void gui_wingoto_xy(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
5359/*
5360 * Jump to the window at specified point (x, y).
5361 */
5362 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005363gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 int row = Y_2_ROW(y);
5366 int col = X_2_COL(x);
5367 win_T *wp;
5368
5369 if (row >= 0 && col >= 0)
5370 {
5371 wp = mouse_find_win(&row, &col);
5372 if (wp != NULL && wp != curwin)
5373 win_goto(wp);
5374 }
5375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377/*
5378 * Process file drop. Mouse cursor position, key modifiers, name of files
5379 * and count of files are given. Argument "fnames[count]" has full pathnames
5380 * of dropped files, they will be freed in this function, and caller can't use
5381 * fnames after call this function.
5382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005384gui_handle_drop(
5385 int x UNUSED,
5386 int y UNUSED,
5387 int_u modifiers,
5388 char_u **fnames,
5389 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 int i;
5392 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005393 static int entered = FALSE;
5394
5395 /*
5396 * This function is called by event handlers. Just in case we get a
5397 * second event before the first one is handled, ignore the second one.
5398 * Not sure if this can ever happen, just in case.
5399 */
5400 if (entered)
5401 return;
5402 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
5404 /*
5405 * When the cursor is at the command line, add the file names to the
5406 * command line, don't edit the files.
5407 */
5408 if (State & CMDLINE)
5409 {
5410 shorten_filenames(fnames, count);
5411 for (i = 0; i < count; ++i)
5412 {
5413 if (fnames[i] != NULL)
5414 {
5415 if (i > 0)
5416 add_to_input_buf((char_u*)" ", 1);
5417
5418 /* We don't know what command is used thus we can't be sure
5419 * about which characters need to be escaped. Only escape the
5420 * most common ones. */
5421# ifdef BACKSLASH_IN_FILENAME
5422 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5423# else
5424 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5425# endif
5426 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005427 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 vim_free(p);
5429 vim_free(fnames[i]);
5430 }
5431 }
5432 vim_free(fnames);
5433 }
5434 else
5435 {
5436 /* Go to the window under mouse cursor, then shorten given "fnames" by
5437 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 shorten_filenames(fnames, count);
5440
5441 /* If Shift held down, remember the first item. */
5442 if ((modifiers & MOUSE_SHIFT) != 0)
5443 p = vim_strsave(fnames[0]);
5444 else
5445 p = NULL;
5446
5447 /* Handle the drop, :edit or :split to get to the file. This also
5448 * frees fnames[]. Skip this if there is only one item it's a
5449 * directory and Shift is held down. */
5450 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5451 && mch_isdir(fnames[0]))
5452 {
5453 vim_free(fnames[0]);
5454 vim_free(fnames);
5455 }
5456 else
5457 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5458
5459 /* If Shift held down, change to first file's directory. If the first
5460 * item is a directory, change to that directory (and let the explorer
5461 * plugin show the contents). */
5462 if (p != NULL)
5463 {
5464 if (mch_isdir(p))
5465 {
5466 if (mch_chdir((char *)p) == 0)
5467 shorten_fnames(TRUE);
5468 }
5469 else if (vim_chdirfile(p) == OK)
5470 shorten_fnames(TRUE);
5471 vim_free(p);
5472 }
5473
5474 /* Update the screen display */
5475 update_screen(NOT_VALID);
5476# ifdef FEAT_MENU
5477 gui_update_menus(0);
5478# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005479#ifdef FEAT_TITLE
5480 maketitle();
5481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 setcursor();
5483 out_flush();
5484 gui_update_cursor(FALSE, FALSE);
5485 gui_mch_flush();
5486 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005487
5488 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489}
5490#endif