blob: 74e2c838c39fe7a17a5442065f2d3672fd3a9e18 [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 Moolenaard0573012017-10-28 21:11:06 +020040#if defined(UNIX) && !defined(FEAT_GUI_MAC)
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 Moolenaar8ac44152017-11-09 18:33:29 +0100696 gui_set_shellsize(TRUE, 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 Moolenaar8ac44152017-11-09 18:33:29 +0100735 gui_set_shellsize(TRUE, 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 Moolenaar8ac44152017-11-09 18:33:29 +0100912 gui_set_shellsize(TRUE, 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 Moolenaar8ac44152017-11-09 18:33:29 +01001556 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1557 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001560gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001561 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001562 int fit_to_display,
1563 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 int base_width;
1566 int base_height;
1567 int width;
1568 int height;
1569 int min_width;
1570 int min_height;
1571 int screen_w;
1572 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001573#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001574 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001575 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001576#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001577 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 if (!gui.shell_created)
1580 return;
1581
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001582#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 /* If not setting to a user specified size and maximized, calculate the
1584 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001585 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1586 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 gui_mch_newfont();
1589 return;
1590 }
1591#endif
1592
1593 base_width = gui_get_base_width();
1594 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001595 if (fit_to_display)
1596 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001597 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599#ifdef USE_SUN_WORKSHOP
1600 if (!mustset && usingSunWorkShop
1601 && workshop_get_width_height(&width, &height))
1602 {
1603 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1604 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1605 }
1606 else
1607#endif
1608 {
1609 width = Columns * gui.char_width + base_width;
1610 height = Rows * gui.char_height + base_height;
1611 }
1612
1613 if (fit_to_display)
1614 {
1615 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001616 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
1618 Columns = (screen_w - base_width) / gui.char_width;
1619 if (Columns < MIN_COLUMNS)
1620 Columns = MIN_COLUMNS;
1621 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001622#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001623 ++did_adjust;
1624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001626 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 Rows = (screen_h - base_height) / gui.char_height;
1629 check_shellsize();
1630 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001631#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001632 ++did_adjust;
1633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001635#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001636 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1637 && height + gui.char_height >= screen_h))
1638 /* don't unmaximize if at maximum size */
1639 un_maximize = FALSE;
1640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001642 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 gui.num_cols = Columns;
1644 gui.num_rows = Rows;
1645
1646 min_width = base_width + MIN_COLUMNS * gui.char_width;
1647 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001648 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001649
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001650#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001651 if (un_maximize)
1652 {
1653 /* If the window size is smaller than the screen unmaximize the
1654 * window, otherwise resizing won't work. */
1655 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1656 if ((width + gui.char_width < screen_w
1657 || height + gui.char_height * 2 < screen_h)
1658 && gui_mch_maximized())
1659 gui_mch_unmaximize();
1660 }
1661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001664 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001666 if (fit_to_display && x >= 0 && y >= 0)
1667 {
1668 /* Some window managers put the Vim window left of/above the screen.
1669 * Only change the position if it wasn't already negative before
1670 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 gui_mch_update();
1672 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1673 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1674 }
1675
1676 gui_position_components(width);
1677 gui_update_scrollbars(TRUE);
1678 gui_reset_scroll_region();
1679}
1680
1681/*
1682 * Called when Rows and/or Columns has changed.
1683 */
1684 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001685gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 gui_reset_scroll_region();
1688}
1689
1690/*
1691 * Make scroll region cover whole screen.
1692 */
1693 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001694gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 gui.scroll_region_top = 0;
1697 gui.scroll_region_bot = gui.num_rows - 1;
1698 gui.scroll_region_left = 0;
1699 gui.scroll_region_right = gui.num_cols - 1;
1700}
1701
1702 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001703gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 if (mask > HL_ALL) /* highlight code */
1706 gui.highlight_mask = mask;
1707 else /* mask */
1708 gui.highlight_mask |= mask;
1709}
1710
1711 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001712gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 if (mask > HL_ALL) /* highlight code */
1715 gui.highlight_mask = HL_NORMAL;
1716 else /* mask */
1717 gui.highlight_mask &= ~mask;
1718}
1719
1720/*
1721 * Clear a rectangular region of the screen from text pos (row1, col1) to
1722 * (row2, col2) inclusive.
1723 */
1724 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001725gui_clear_block(
1726 int row1,
1727 int col1,
1728 int row2,
1729 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
1731 /* Clear the selection if we are about to write over it */
1732 clip_may_clear_selection(row1, row2);
1733
1734 gui_mch_clear_block(row1, col1, row2, col2);
1735
1736 /* Invalidate cursor if it was in this block */
1737 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1738 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1739 gui.cursor_is_valid = FALSE;
1740}
1741
1742/*
1743 * Write code to update the cursor later. This avoids the need to flush the
1744 * output buffer before calling gui_update_cursor().
1745 */
1746 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001747gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001749 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751
1752 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001753gui_write(
1754 char_u *s,
1755 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 char_u *p;
1758 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 int force_scrollbar = FALSE;
1761 static win_T *old_curwin = NULL;
1762
1763/* #define DEBUG_GUI_WRITE */
1764#ifdef DEBUG_GUI_WRITE
1765 {
1766 int i;
1767 char_u *str;
1768
1769 printf("gui_write(%d):\n ", len);
1770 for (i = 0; i < len; i++)
1771 if (s[i] == ESC)
1772 {
1773 if (i != 0)
1774 printf("\n ");
1775 printf("<ESC>");
1776 }
1777 else
1778 {
1779 str = transchar_byte(s[i]);
1780 if (str[0] && str[1])
1781 printf("<%s>", (char *)str);
1782 else
1783 printf("%s", (char *)str);
1784 }
1785 printf("\n");
1786 }
1787#endif
1788 while (len)
1789 {
1790 if (s[0] == ESC && s[1] == '|')
1791 {
1792 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001793 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 {
1795 arg1 = getdigits(&p);
1796 if (p > s + len)
1797 break;
1798 if (*p == ';')
1799 {
1800 ++p;
1801 arg2 = getdigits(&p);
1802 if (p > s + len)
1803 break;
1804 }
1805 }
1806 switch (*p)
1807 {
1808 case 'C': /* Clear screen */
1809 clip_scroll_selection(9999);
1810 gui_mch_clear_all();
1811 gui.cursor_is_valid = FALSE;
1812 force_scrollbar = TRUE;
1813 break;
1814 case 'M': /* Move cursor */
1815 gui_set_cursor(arg1, arg2);
1816 break;
1817 case 's': /* force cursor (shape) update */
1818 force_cursor = TRUE;
1819 break;
1820 case 'R': /* Set scroll region */
1821 if (arg1 < arg2)
1822 {
1823 gui.scroll_region_top = arg1;
1824 gui.scroll_region_bot = arg2;
1825 }
1826 else
1827 {
1828 gui.scroll_region_top = arg2;
1829 gui.scroll_region_bot = arg1;
1830 }
1831 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 case 'V': /* Set vertical scroll region */
1833 if (arg1 < arg2)
1834 {
1835 gui.scroll_region_left = arg1;
1836 gui.scroll_region_right = arg2;
1837 }
1838 else
1839 {
1840 gui.scroll_region_left = arg2;
1841 gui.scroll_region_right = arg1;
1842 }
1843 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 case 'd': /* Delete line */
1845 gui_delete_lines(gui.row, 1);
1846 break;
1847 case 'D': /* Delete lines */
1848 gui_delete_lines(gui.row, arg1);
1849 break;
1850 case 'i': /* Insert line */
1851 gui_insert_lines(gui.row, 1);
1852 break;
1853 case 'I': /* Insert lines */
1854 gui_insert_lines(gui.row, arg1);
1855 break;
1856 case '$': /* Clear to end-of-line */
1857 gui_clear_block(gui.row, gui.col, gui.row,
1858 (int)Columns - 1);
1859 break;
1860 case 'h': /* Turn on highlighting */
1861 gui_start_highlight(arg1);
1862 break;
1863 case 'H': /* Turn off highlighting */
1864 gui_stop_highlight(arg1);
1865 break;
1866 case 'f': /* flash the window (visual bell) */
1867 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1868 break;
1869 default:
1870 p = s + 1; /* Skip the ESC */
1871 break;
1872 }
1873 len -= (int)(++p - s);
1874 s = p;
1875 }
1876 else if (
1877#ifdef EBCDIC
1878 CtrlChar(s[0]) != 0 /* Ctrl character */
1879#else
1880 s[0] < 0x20 /* Ctrl character */
1881#endif
1882#ifdef FEAT_SIGN_ICONS
1883 && s[0] != SIGN_BYTE
1884# ifdef FEAT_NETBEANS_INTG
1885 && s[0] != MULTISIGN_BYTE
1886# endif
1887#endif
1888 )
1889 {
1890 if (s[0] == '\n') /* NL */
1891 {
1892 gui.col = 0;
1893 if (gui.row < gui.scroll_region_bot)
1894 gui.row++;
1895 else
1896 gui_delete_lines(gui.scroll_region_top, 1);
1897 }
1898 else if (s[0] == '\r') /* CR */
1899 {
1900 gui.col = 0;
1901 }
1902 else if (s[0] == '\b') /* Backspace */
1903 {
1904 if (gui.col)
1905 --gui.col;
1906 }
1907 else if (s[0] == Ctrl_L) /* cursor-right */
1908 {
1909 ++gui.col;
1910 }
1911 else if (s[0] == Ctrl_G) /* Beep */
1912 {
1913 gui_mch_beep();
1914 }
1915 /* Other Ctrl character: shouldn't happen! */
1916
1917 --len; /* Skip this char */
1918 ++s;
1919 }
1920 else
1921 {
1922 p = s;
1923 while (len > 0 && (
1924#ifdef EBCDIC
1925 CtrlChar(*p) == 0
1926#else
1927 *p >= 0x20
1928#endif
1929#ifdef FEAT_SIGN_ICONS
1930 || *p == SIGN_BYTE
1931# ifdef FEAT_NETBEANS_INTG
1932 || *p == MULTISIGN_BYTE
1933# endif
1934#endif
1935 ))
1936 {
1937 len--;
1938 p++;
1939 }
1940 gui_outstr(s, (int)(p - s));
1941 s = p;
1942 }
1943 }
1944
1945 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1946 * set). */
1947 if (force_cursor)
1948 gui_update_cursor(TRUE, TRUE);
1949
1950 /* When switching to another window the dragging must have stopped.
1951 * Required for GTK, dragged_sb isn't reset. */
1952 if (old_curwin != curwin)
1953 gui.dragged_sb = SBAR_NONE;
1954
1955 /* Update the scrollbars after clearing the screen or when switched
1956 * to another window.
1957 * Update the horizontal scrollbar always, it's difficult to check all
1958 * situations where it might change. */
1959 if (force_scrollbar || old_curwin != curwin)
1960 gui_update_scrollbars(force_scrollbar);
1961 else
1962 gui_update_horiz_scrollbar(FALSE);
1963 old_curwin = curwin;
1964
1965 /*
1966 * We need to make sure this is cleared since Athena doesn't tell us when
1967 * he is done dragging. Do the same for GTK.
1968 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001969#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 gui.dragged_sb = SBAR_NONE;
1971#endif
1972
1973 gui_mch_flush(); /* In case vim decides to take a nap */
1974}
1975
1976/*
1977 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1978 * produces wrong results. Call gui_dont_update_cursor() before that code and
1979 * gui_can_update_cursor() afterwards.
1980 */
1981 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001982gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 if (gui.in_use)
1985 {
1986 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001987 if (undraw)
1988 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 can_update_cursor = FALSE;
1990 }
1991}
1992
1993 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001994gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 can_update_cursor = TRUE;
1997 /* No need to update the cursor right now, there is always more output
1998 * after scrolling. */
1999}
2000
2001 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002002gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 int this_len;
2005#ifdef FEAT_MBYTE
2006 int cells;
2007#endif
2008
2009 if (len == 0)
2010 return;
2011
2012 if (len < 0)
2013 len = (int)STRLEN(s);
2014
2015 while (len > 0)
2016 {
2017#ifdef FEAT_MBYTE
2018 if (has_mbyte)
2019 {
2020 /* Find out how many chars fit in the current line. */
2021 cells = 0;
2022 for (this_len = 0; this_len < len; )
2023 {
2024 cells += (*mb_ptr2cells)(s + this_len);
2025 if (gui.col + cells > Columns)
2026 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002027 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 if (this_len > len)
2030 this_len = len; /* don't include following composing char */
2031 }
2032 else
2033#endif
2034 if (gui.col + len > Columns)
2035 this_len = Columns - gui.col;
2036 else
2037 this_len = len;
2038
2039 (void)gui_outstr_nowrap(s, this_len,
2040 0, (guicolor_T)0, (guicolor_T)0, 0);
2041 s += this_len;
2042 len -= this_len;
2043#ifdef FEAT_MBYTE
2044 /* fill up for a double-width char that doesn't fit. */
2045 if (len > 0 && gui.col < Columns)
2046 (void)gui_outstr_nowrap((char_u *)" ", 1,
2047 0, (guicolor_T)0, (guicolor_T)0, 0);
2048#endif
2049 /* The cursor may wrap to the next line. */
2050 if (gui.col >= Columns)
2051 {
2052 gui.col = 0;
2053 gui.row++;
2054 }
2055 }
2056}
2057
2058/*
2059 * Output one character (may be one or two display cells).
2060 * Caller must check for valid "off".
2061 * Returns FAIL or OK, just like gui_outstr_nowrap().
2062 */
2063 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002064gui_screenchar(
2065 int off, /* Offset from start of screen */
2066 int flags,
2067 guicolor_T fg, /* colors for cursor */
2068 guicolor_T bg, /* colors for cursor */
2069 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
2071#ifdef FEAT_MBYTE
2072 char_u buf[MB_MAXBYTES + 1];
2073
2074 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2075 if (enc_utf8 && ScreenLines[off] == 0)
2076 return OK;
2077
2078 if (enc_utf8 && ScreenLinesUC[off] != 0)
2079 /* Draw UTF-8 multi-byte character. */
2080 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2081 flags, fg, bg, back);
2082
2083 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2084 {
2085 buf[0] = ScreenLines[off];
2086 buf[1] = ScreenLines2[off];
2087 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2088 }
2089
2090 /* Draw non-multi-byte character or DBCS character. */
2091 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002092 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 flags, fg, bg, back);
2094#else
2095 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2096#endif
2097}
2098
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002099#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100/*
2101 * Output the string at the given screen position. This is used in place
2102 * of gui_screenchar() where possible because Pango needs as much context
2103 * as possible to work nicely. It's a lot faster as well.
2104 */
2105 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002106gui_screenstr(
2107 int off, /* Offset from start of screen */
2108 int len, /* string length in screen cells */
2109 int flags,
2110 guicolor_T fg, /* colors for cursor */
2111 guicolor_T bg, /* colors for cursor */
2112 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 char_u *buf;
2115 int outlen = 0;
2116 int i;
2117 int retval;
2118
2119 if (len <= 0) /* "cannot happen"? */
2120 return OK;
2121
2122 if (enc_utf8)
2123 {
2124 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2125 if (buf == NULL)
2126 return OK; /* not much we could do here... */
2127
2128 for (i = off; i < off + len; ++i)
2129 {
2130 if (ScreenLines[i] == 0)
2131 continue; /* skip second half of double-width char */
2132
2133 if (ScreenLinesUC[i] == 0)
2134 buf[outlen++] = ScreenLines[i];
2135 else
2136 outlen += utfc_char2bytes(i, buf + outlen);
2137 }
2138
2139 buf[outlen] = NUL; /* only to aid debugging */
2140 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2141 vim_free(buf);
2142
2143 return retval;
2144 }
2145 else if (enc_dbcs == DBCS_JPNU)
2146 {
2147 buf = alloc((unsigned)(len * 2 + 1));
2148 if (buf == NULL)
2149 return OK; /* not much we could do here... */
2150
2151 for (i = off; i < off + len; ++i)
2152 {
2153 buf[outlen++] = ScreenLines[i];
2154
2155 /* handle double-byte single-width char */
2156 if (ScreenLines[i] == 0x8e)
2157 buf[outlen++] = ScreenLines2[i];
2158 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2159 buf[outlen++] = ScreenLines[++i];
2160 }
2161
2162 buf[outlen] = NUL; /* only to aid debugging */
2163 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2164 vim_free(buf);
2165
2166 return retval;
2167 }
2168 else
2169 {
2170 return gui_outstr_nowrap(&ScreenLines[off], len,
2171 flags, fg, bg, back);
2172 }
2173}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002174#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176/*
2177 * Output the given string at the current cursor position. If the string is
2178 * too long to fit on the line, then it is truncated.
2179 * "flags":
2180 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2181 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002182 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 * background.
2184 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2185 * it.
2186 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2187 * FAIL (the caller should start drawing "back" chars back).
2188 */
2189 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002190gui_outstr_nowrap(
2191 char_u *s,
2192 int len,
2193 int flags,
2194 guicolor_T fg, /* colors for cursor */
2195 guicolor_T bg, /* colors for cursor */
2196 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 long_u highlight_mask;
2199 long_u hl_mask_todo;
2200 guicolor_T fg_color;
2201 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002202 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002203#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002205# ifdef FEAT_MBYTE
2206 GuiFont wide_font = NOFONT;
2207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208# ifdef FEAT_XFONTSET
2209 GuiFontset fontset = NOFONTSET;
2210# endif
2211#endif
2212 attrentry_T *aep = NULL;
2213 int draw_flags;
2214 int col = gui.col;
2215#ifdef FEAT_SIGN_ICONS
2216 int draw_sign = FALSE;
2217# ifdef FEAT_NETBEANS_INTG
2218 int multi_sign = FALSE;
2219# endif
2220#endif
2221
2222 if (len < 0)
2223 len = (int)STRLEN(s);
2224 if (len == 0)
2225 return OK;
2226
2227#ifdef FEAT_SIGN_ICONS
2228 if (*s == SIGN_BYTE
2229# ifdef FEAT_NETBEANS_INTG
2230 || *s == MULTISIGN_BYTE
2231# endif
2232 )
2233 {
2234# ifdef FEAT_NETBEANS_INTG
2235 if (*s == MULTISIGN_BYTE)
2236 multi_sign = TRUE;
2237# endif
2238 /* draw spaces instead */
2239 s = (char_u *)" ";
2240 if (len == 1 && col > 0)
2241 --col;
2242 len = 2;
2243 draw_sign = TRUE;
2244 highlight_mask = 0;
2245 }
2246 else
2247#endif
2248 if (gui.highlight_mask > HL_ALL)
2249 {
2250 aep = syn_gui_attr2entry(gui.highlight_mask);
2251 if (aep == NULL) /* highlighting not set */
2252 highlight_mask = 0;
2253 else
2254 highlight_mask = aep->ae_attr;
2255 }
2256 else
2257 highlight_mask = gui.highlight_mask;
2258 hl_mask_todo = highlight_mask;
2259
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002260#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /* Set the font */
2262 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2263 font = aep->ae_u.gui.font;
2264# ifdef FEAT_XFONTSET
2265 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2266 fontset = aep->ae_u.gui.fontset;
2267# endif
2268 else
2269 {
2270# ifdef FEAT_XFONTSET
2271 if (gui.fontset != NOFONTSET)
2272 fontset = gui.fontset;
2273 else
2274# endif
2275 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2276 {
2277 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2278 {
2279 font = gui.boldital_font;
2280 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2281 }
2282 else if (gui.bold_font != NOFONT)
2283 {
2284 font = gui.bold_font;
2285 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2286 }
2287 else
2288 font = gui.norm_font;
2289 }
2290 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2291 {
2292 font = gui.ital_font;
2293 hl_mask_todo &= ~HL_ITALIC;
2294 }
2295 else
2296 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002297
2298# ifdef FEAT_MBYTE
2299 /*
2300 * Choose correct wide_font by font. wide_font should be set with font
2301 * at same time in above block. But it will make many "ifdef" nasty
2302 * blocks. So we do it here.
2303 */
2304 if (font == gui.boldital_font && gui.wide_boldital_font)
2305 wide_font = gui.wide_boldital_font;
2306 else if (font == gui.bold_font && gui.wide_bold_font)
2307 wide_font = gui.wide_bold_font;
2308 else if (font == gui.ital_font && gui.wide_ital_font)
2309 wide_font = gui.wide_ital_font;
2310 else if (font == gui.norm_font && gui.wide_font)
2311 wide_font = gui.wide_font;
2312# endif
2313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315# ifdef FEAT_XFONTSET
2316 if (fontset != NOFONTSET)
2317 gui_mch_set_fontset(fontset);
2318 else
2319# endif
2320 gui_mch_set_font(font);
2321#endif
2322
2323 draw_flags = 0;
2324
2325 /* Set the color */
2326 bg_color = gui.back_pixel;
2327 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2328 {
2329 draw_flags |= DRAW_CURSOR;
2330 fg_color = fg;
2331 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002332 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334 else if (aep != NULL)
2335 {
2336 fg_color = aep->ae_u.gui.fg_color;
2337 if (fg_color == INVALCOLOR)
2338 fg_color = gui.norm_pixel;
2339 bg_color = aep->ae_u.gui.bg_color;
2340 if (bg_color == INVALCOLOR)
2341 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002342 sp_color = aep->ae_u.gui.sp_color;
2343 if (sp_color == INVALCOLOR)
2344 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002349 sp_color = fg_color;
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2353 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002354#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 gui_mch_set_colors(bg_color, fg_color);
2356#else
2357 gui_mch_set_fg_color(bg_color);
2358 gui_mch_set_bg_color(fg_color);
2359#endif
2360 }
2361 else
2362 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002363#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 gui_mch_set_colors(fg_color, bg_color);
2365#else
2366 gui_mch_set_fg_color(fg_color);
2367 gui_mch_set_bg_color(bg_color);
2368#endif
2369 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002370 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 /* Clear the selection if we are about to write over it */
2373 if (!(flags & GUI_MON_NOCLEAR))
2374 clip_may_clear_selection(gui.row, gui.row);
2375
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /* If there's no bold font, then fake it */
2378 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2379 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 /*
2382 * When drawing bold or italic characters the spill-over from the left
2383 * neighbor may be destroyed. Let the caller backup to start redrawing
2384 * just after a blank.
2385 */
2386 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2387 return FAIL;
2388
Bram Moolenaare60acc12011-05-10 16:41:25 +02002389#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 /* If there's no italic font, then fake it.
2391 * For GTK2, we don't need a different font for italic style. */
2392 if (hl_mask_todo & HL_ITALIC)
2393 draw_flags |= DRAW_ITALIC;
2394
2395 /* Do we underline the text? */
2396 if (hl_mask_todo & HL_UNDERLINE)
2397 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#else
2400 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002401 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 draw_flags |= DRAW_UNDERL;
2403#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002404 /* Do we undercurl the text? */
2405 if (hl_mask_todo & HL_UNDERCURL)
2406 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002408 /* Do we strikethrough the text? */
2409 if (hl_mask_todo & HL_STRIKETHROUGH)
2410 draw_flags |= DRAW_STRIKE;
2411
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002412 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (flags & GUI_MON_TRS_CURSOR)
2414 draw_flags |= DRAW_TRANSP;
2415
2416 /*
2417 * Draw the text.
2418 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002419#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 /* The value returned is the length in display cells */
2421 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2422#else
2423# ifdef FEAT_MBYTE
2424 if (enc_utf8)
2425 {
2426 int start; /* index of bytes to be drawn */
2427 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002428 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 int cn; /* cellwidth of current char */
2430 int i; /* index of current char */
2431 int c; /* current char value */
2432 int cl; /* byte length of current char */
2433 int comping; /* current char is composing */
2434 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002435 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002436 int prev_wide = FALSE;
2437 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002438# ifdef WIN3264
2439 int sep_comp = FALSE; /* Don't separate composing chars. */
2440# else
2441 int sep_comp = TRUE; /* Separate composing chars. */
2442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 /* Break the string at a composing character, it has to be drawn on
2445 * top of the previous character. */
2446 start = 0;
2447 cells = 0;
2448 for (i = 0; i < len; i += cl)
2449 {
2450 c = utf_ptr2char(s + i);
2451 cn = utf_char2cells(c);
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 Moolenaara6ce1cc2017-10-28 19:23:11 +02002455 if (!comping || sep_comp)
2456 {
2457 if (cn > 1
2458# ifdef FEAT_XFONTSET
2459 && fontset == NOFONTSET
2460# endif
2461 && wide_font != NOFONT)
2462 curr_wide = TRUE;
2463 else
2464 curr_wide = FALSE;
2465 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002466 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (cl == 0) /* hit end of string */
2468 len = i + cl; /* len must be wrong "cannot happen" */
2469
Bram Moolenaar45933962013-01-23 17:43:57 +01002470 wide_changed = curr_wide != prev_wide;
2471
2472 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002474 if (i + cl >= len || (comping && sep_comp && i > start)
2475 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002476# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 || (cn > 1
2478# ifdef FEAT_XFONTSET
2479 /* No fontset: At least draw char after wide char at
2480 * right position. */
2481 && fontset == NOFONTSET
2482# endif
2483 )
2484# endif
2485 )
2486 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002487 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 thislen = i - start;
2489 else
2490 thislen = i - start + cl;
2491 if (thislen > 0)
2492 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002493 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002494 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2496 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002497 if (prev_wide)
2498 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 start += thislen;
2500 }
2501 scol += cells;
2502 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002503 /* Adjust to not draw a character which width is changed
2504 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002505 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002507 scol -= cn;
2508 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002511# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002512 /* No fontset: draw a space to fill the gap after a wide char
2513 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2515# ifdef FEAT_XFONTSET
2516 && fontset == NOFONTSET
2517# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002518 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2520 1, draw_flags);
2521# endif
2522 }
2523 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002524 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002526# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002527 /* Carbon ATSUI autodraws composing char over previous char */
2528 gui_mch_draw_string(gui.row, scol, s + i, cl,
2529 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002530# else
2531 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2532 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 start = i + cl;
2535 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002536 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538 /* The stuff below assumes "len" is the length in screen columns. */
2539 len = scol - col;
2540 }
2541 else
2542# endif
2543 {
2544 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2545# ifdef FEAT_MBYTE
2546 if (enc_dbcs == DBCS_JPNU)
2547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /* Get the length in display cells, this can be different from the
2549 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002550 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552# endif
2553 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002554#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2557 gui.col = col + len;
2558
2559 /* May need to invert it when it's part of the selection. */
2560 if (flags & GUI_MON_NOCLEAR)
2561 clip_may_redraw_selection(gui.row, col, len);
2562
2563 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2564 {
2565 /* Invalidate the old physical cursor position if we wrote over it */
2566 if (gui.cursor_row == gui.row
2567 && gui.cursor_col >= col
2568 && gui.cursor_col < col + len)
2569 gui.cursor_is_valid = FALSE;
2570 }
2571
2572#ifdef FEAT_SIGN_ICONS
2573 if (draw_sign)
2574 /* Draw the sign on top of the spaces. */
2575 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002576# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002577 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (multi_sign)
2579 netbeans_draw_multisign_indicator(gui.row);
2580# endif
2581#endif
2582
2583 return OK;
2584}
2585
2586/*
2587 * Un-draw the cursor. Actually this just redraws the character at the given
2588 * position. The character just before it too, for when it was in bold.
2589 */
2590 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002591gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 if (gui.cursor_is_valid)
2594 {
2595#ifdef FEAT_HANGULIN
2596 if (composing_hangul
2597 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002598 {
2599 char_u *comp_buf;
2600 int comp_len;
2601
2602 comp_buf = hangul_composing_buffer_get(&comp_len);
2603 if (comp_buf)
2604 {
2605 (void)gui_outstr_nowrap(comp_buf, comp_len,
2606 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2607 gui.norm_pixel, gui.back_pixel, 0);
2608 vim_free(comp_buf);
2609 }
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 else
2612 {
2613#endif
2614 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2615 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2616 && gui.cursor_col > 0)
2617 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2618 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2619#ifdef FEAT_HANGULIN
2620 if (composing_hangul)
2621 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2622 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2623 }
2624#endif
2625 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2626 * here in case it wasn't needed to undraw it. */
2627 gui.cursor_is_valid = FALSE;
2628 }
2629}
2630
2631 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002632gui_redraw(
2633 int x,
2634 int y,
2635 int w,
2636 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 int row1, col1, row2, col2;
2639
2640 row1 = Y_2_ROW(y);
2641 col1 = X_2_COL(x);
2642 row2 = Y_2_ROW(y + h - 1);
2643 col2 = X_2_COL(x + w - 1);
2644
2645 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2646
2647 /*
2648 * We may need to redraw the cursor, but don't take it upon us to change
2649 * its location after a scroll.
2650 * (maybe be more strict even and test col too?)
2651 * These things may be outside the update/clipping region and reality may
2652 * not reflect Vims internal ideas if these operations are clipped away.
2653 */
2654 if (gui.row == gui.cursor_row)
2655 gui_update_cursor(TRUE, TRUE);
2656}
2657
2658/*
2659 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2660 * from col1 to col2 (inclusive).
2661 * Return TRUE when the character before the first drawn character has
2662 * different attributes (may have to be redrawn too).
2663 */
2664 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002665gui_redraw_block(
2666 int row1,
2667 int col1,
2668 int row2,
2669 int col2,
2670 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
2672 int old_row, old_col;
2673 long_u old_hl_mask;
2674 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002675 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 int idx, len;
2677 int back, nback;
2678 int retval = FALSE;
2679#ifdef FEAT_MBYTE
2680 int orig_col1, orig_col2;
2681#endif
2682
2683 /* Don't try to update when ScreenLines is not valid */
2684 if (!screen_cleared || ScreenLines == NULL)
2685 return retval;
2686
2687 /* Don't try to draw outside the shell! */
2688 /* Check everything, strange values may be caused by a big border width */
2689 col1 = check_col(col1);
2690 col2 = check_col(col2);
2691 row1 = check_row(row1);
2692 row2 = check_row(row2);
2693
2694 /* Remember where our cursor was */
2695 old_row = gui.row;
2696 old_col = gui.col;
2697 old_hl_mask = gui.highlight_mask;
2698#ifdef FEAT_MBYTE
2699 orig_col1 = col1;
2700 orig_col2 = col2;
2701#endif
2702
2703 for (gui.row = row1; gui.row <= row2; gui.row++)
2704 {
2705#ifdef FEAT_MBYTE
2706 /* When only half of a double-wide character is in the block, include
2707 * the other half. */
2708 col1 = orig_col1;
2709 col2 = orig_col2;
2710 off = LineOffset[gui.row];
2711 if (enc_dbcs != 0)
2712 {
2713 if (col1 > 0)
2714 col1 -= dbcs_screen_head_off(ScreenLines + off,
2715 ScreenLines + off + col1);
2716 col2 += dbcs_screen_tail_off(ScreenLines + off,
2717 ScreenLines + off + col2);
2718 }
2719 else if (enc_utf8)
2720 {
2721 if (ScreenLines[off + col1] == 0)
2722 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002723# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2725 ++col2;
2726# endif
2727 }
2728#endif
2729 gui.col = col1;
2730 off = LineOffset[gui.row] + gui.col;
2731 len = col2 - col1 + 1;
2732
2733 /* Find how many chars back this highlighting starts, or where a space
2734 * is. Needed for when the bold trick is used */
2735 for (back = 0; back < col1; ++back)
2736 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2737 || ScreenLines[off - 1 - back] == ' ')
2738 break;
2739 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2740 && ScreenLines[off - 1] != ' ');
2741
2742 /* Break it up in strings of characters with the same attributes. */
2743 /* Print UTF-8 characters individually. */
2744 while (len > 0)
2745 {
2746 first_attr = ScreenAttrs[off];
2747 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002748#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (enc_utf8 && ScreenLinesUC[off] != 0)
2750 {
2751 /* output multi-byte character separately */
2752 nback = gui_screenchar(off, flags,
2753 (guicolor_T)0, (guicolor_T)0, back);
2754 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2755 idx = 2;
2756 else
2757 idx = 1;
2758 }
2759 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2760 {
2761 /* output double-byte, single-width character separately */
2762 nback = gui_screenchar(off, flags,
2763 (guicolor_T)0, (guicolor_T)0, back);
2764 idx = 1;
2765 }
2766 else
2767#endif
2768 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002769#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 for (idx = 0; idx < len; ++idx)
2771 {
2772 if (enc_utf8 && ScreenLines[off + idx] == 0)
2773 continue; /* skip second half of double-width char */
2774 if (ScreenAttrs[off + idx] != first_attr)
2775 break;
2776 }
2777 /* gui_screenstr() takes care of multibyte chars */
2778 nback = gui_screenstr(off, idx, flags,
2779 (guicolor_T)0, (guicolor_T)0, back);
2780#else
2781 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2782 idx++)
2783 {
2784# ifdef FEAT_MBYTE
2785 /* Stop at a multi-byte Unicode character. */
2786 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2787 break;
2788 if (enc_dbcs == DBCS_JPNU)
2789 {
2790 /* Stop at a double-byte single-width char. */
2791 if (ScreenLines[off + idx] == 0x8e)
2792 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002793 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 + off + idx) == 2)
2795 ++idx; /* skip second byte of double-byte char */
2796 }
2797# endif
2798 }
2799 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2800 (guicolor_T)0, (guicolor_T)0, back);
2801#endif
2802 }
2803 if (nback == FAIL)
2804 {
2805 /* Must back up to start drawing where a bold or italic word
2806 * starts. */
2807 off -= back;
2808 len += back;
2809 gui.col -= back;
2810 }
2811 else
2812 {
2813 off += idx;
2814 len -= idx;
2815 }
2816 back = 0;
2817 }
2818 }
2819
2820 /* Put the cursor back where it was */
2821 gui.row = old_row;
2822 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002823 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 return retval;
2826}
2827
2828 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002829gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 if (count <= 0)
2832 return;
2833
2834 if (row + count > gui.scroll_region_bot)
2835 /* Scrolled out of region, just blank the lines out */
2836 gui_clear_block(row, gui.scroll_region_left,
2837 gui.scroll_region_bot, gui.scroll_region_right);
2838 else
2839 {
2840 gui_mch_delete_lines(row, count);
2841
2842 /* If the cursor was in the deleted lines it's now gone. If the
2843 * cursor was in the scrolled lines adjust its position. */
2844 if (gui.cursor_row >= row
2845 && gui.cursor_col >= gui.scroll_region_left
2846 && gui.cursor_col <= gui.scroll_region_right)
2847 {
2848 if (gui.cursor_row < row + count)
2849 gui.cursor_is_valid = FALSE;
2850 else if (gui.cursor_row <= gui.scroll_region_bot)
2851 gui.cursor_row -= count;
2852 }
2853 }
2854}
2855
2856 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002857gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
2859 if (count <= 0)
2860 return;
2861
2862 if (row + count > gui.scroll_region_bot)
2863 /* Scrolled out of region, just blank the lines out */
2864 gui_clear_block(row, gui.scroll_region_left,
2865 gui.scroll_region_bot, gui.scroll_region_right);
2866 else
2867 {
2868 gui_mch_insert_lines(row, count);
2869
2870 if (gui.cursor_row >= gui.row
2871 && gui.cursor_col >= gui.scroll_region_left
2872 && gui.cursor_col <= gui.scroll_region_right)
2873 {
2874 if (gui.cursor_row <= gui.scroll_region_bot - count)
2875 gui.cursor_row += count;
2876 else if (gui.cursor_row <= gui.scroll_region_bot)
2877 gui.cursor_is_valid = FALSE;
2878 }
2879 }
2880}
2881
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002882/*
2883 * Returns OK if a character was found to be available within the given time,
2884 * or FAIL otherwise.
2885 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002886 static int
2887gui_wait_for_chars_or_timer(long wtime)
2888{
2889#ifdef FEAT_TIMERS
2890 int due_time;
2891 long remaining = wtime;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002892 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002893
2894 /* When waiting very briefly don't trigger timers. */
2895 if (wtime >= 0 && wtime < 10L)
2896 return gui_mch_wait_for_chars(wtime);
2897
2898 while (wtime < 0 || remaining > 0)
2899 {
2900 /* Trigger timers and then get the time in wtime until the next one is
2901 * due. Wait up to that time. */
2902 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002903 if (typebuf.tb_change_cnt != tb_change_cnt)
2904 {
2905 /* timer may have used feedkeys() */
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002906 return FAIL;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002907 }
Bram Moolenaar975b5272016-03-15 23:10:59 +01002908 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
2909 due_time = remaining;
2910 if (gui_mch_wait_for_chars(due_time))
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002911 return OK;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002912 if (wtime > 0)
2913 remaining -= due_time;
2914 }
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002915 return FAIL;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002916#else
2917 return gui_mch_wait_for_chars(wtime);
2918#endif
2919}
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921/*
2922 * The main GUI input routine. Waits for a character from the keyboard.
2923 * wtime == -1 Wait forever.
2924 * wtime == 0 Don't wait.
2925 * wtime > 0 Wait wtime milliseconds for a character.
2926 * Returns OK if a character was found to be available within the given time,
2927 * or FAIL otherwise.
2928 */
2929 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002930gui_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 int retval;
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002933 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002935#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 /*
2937 * If we're going to wait a bit, update the menus and mouse shape for the
2938 * current State.
2939 */
2940 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 gui_update_menus(0);
2942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 gui_mch_update();
2945 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /* Before waiting, flush any output to the screen. */
2951 gui_mch_flush();
2952
2953 if (wtime > 0)
2954 {
2955 /* Blink when waiting for a character. Probably only does something
2956 * for showmatch() */
2957 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002958 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 return retval;
2961 }
2962
2963 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002964 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 */
2966 gui_mch_start_blink();
2967
Bram Moolenaar3918c952005-03-15 22:34:55 +00002968 retval = FAIL;
2969 /*
2970 * We may want to trigger the CursorHold event. First wait for
2971 * 'updatetime' and if nothing is typed within that time put the
2972 * K_CURSORHOLD key in the input buffer.
2973 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002974 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002975 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002977 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002979 char_u buf[3];
2980
2981 /* Put K_CURSORHOLD in the input buffer. */
2982 buf[0] = CSI;
2983 buf[1] = KS_EXTRA;
2984 buf[2] = (int)KE_CURSORHOLD;
2985 add_to_input_buf(buf, 3);
2986
2987 retval = OK;
2988 }
2989#endif
2990
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002991 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002992 {
2993 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002994 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002995 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 gui_mch_stop_blink();
2999 return retval;
3000}
3001
3002/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003003 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 */
3005 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003006fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 p[0] = (char_u)(col / 128 + ' ' + 1);
3009 p[1] = (char_u)(col % 128 + ' ' + 1);
3010 p[2] = (char_u)(row / 128 + ' ' + 1);
3011 p[3] = (char_u)(row % 128 + ' ' + 1);
3012}
3013
3014/*
3015 * Generic mouse support function. Add a mouse event to the input buffer with
3016 * the given properties.
3017 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3018 * MOUSE_X1, MOUSE_X2
3019 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003020 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3021 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 * x, y --- Coordinates of mouse in pixels.
3023 * repeated_click --- TRUE if this click comes only a short time after a
3024 * previous click.
3025 * modifiers --- Bit field which may be any of the following modifiers
3026 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3027 * This function will ignore drag events where the mouse has not moved to a new
3028 * character.
3029 */
3030 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003031gui_send_mouse_event(
3032 int button,
3033 int x,
3034 int y,
3035 int repeated_click,
3036 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 static int prev_row = 0, prev_col = 0;
3039 static int prev_button = -1;
3040 static int num_clicks = 1;
3041 char_u string[10];
3042 enum key_extra button_char;
3043 int row, col;
3044#ifdef FEAT_CLIPBOARD
3045 int checkfor;
3046 int did_clip = FALSE;
3047#endif
3048
3049 /*
3050 * Scrolling may happen at any time, also while a selection is present.
3051 */
3052 switch (button)
3053 {
3054 case MOUSE_X1:
3055 button_char = KE_X1MOUSE;
3056 goto button_set;
3057 case MOUSE_X2:
3058 button_char = KE_X2MOUSE;
3059 goto button_set;
3060 case MOUSE_4:
3061 button_char = KE_MOUSEDOWN;
3062 goto button_set;
3063 case MOUSE_5:
3064 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003065 goto button_set;
3066 case MOUSE_6:
3067 button_char = KE_MOUSELEFT;
3068 goto button_set;
3069 case MOUSE_7:
3070 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071button_set:
3072 {
3073 /* Don't put events in the input queue now. */
3074 if (hold_gui_events)
3075 return;
3076
3077 string[3] = CSI;
3078 string[4] = KS_EXTRA;
3079 string[5] = (int)button_char;
3080
3081 /* Pass the pointer coordinates of the scroll event so that we
3082 * know which window to scroll. */
3083 row = gui_xy2colrow(x, y, &col);
3084 string[6] = (char_u)(col / 128 + ' ' + 1);
3085 string[7] = (char_u)(col % 128 + ' ' + 1);
3086 string[8] = (char_u)(row / 128 + ' ' + 1);
3087 string[9] = (char_u)(row % 128 + ' ' + 1);
3088
3089 if (modifiers == 0)
3090 add_to_input_buf(string + 3, 7);
3091 else
3092 {
3093 string[0] = CSI;
3094 string[1] = KS_MODIFIER;
3095 string[2] = 0;
3096 if (modifiers & MOUSE_SHIFT)
3097 string[2] |= MOD_MASK_SHIFT;
3098 if (modifiers & MOUSE_CTRL)
3099 string[2] |= MOD_MASK_CTRL;
3100 if (modifiers & MOUSE_ALT)
3101 string[2] |= MOD_MASK_ALT;
3102 add_to_input_buf(string, 10);
3103 }
3104 return;
3105 }
3106 }
3107
3108#ifdef FEAT_CLIPBOARD
3109 /* If a clipboard selection is in progress, handle it */
3110 if (clip_star.state == SELECT_IN_PROGRESS)
3111 {
3112 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3113 return;
3114 }
3115
3116 /* Determine which mouse settings to look for based on the current mode */
3117 switch (get_real_state())
3118 {
3119 case NORMAL_BUSY:
3120 case OP_PENDING:
3121 case NORMAL: checkfor = MOUSE_NORMAL; break;
3122 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003123 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 case REPLACE:
3125 case REPLACE+LANGMAP:
3126#ifdef FEAT_VREPLACE
3127 case VREPLACE:
3128 case VREPLACE+LANGMAP:
3129#endif
3130 case INSERT:
3131 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3132 case ASKMORE:
3133 case HITRETURN: /* At the more- and hit-enter prompt pass the
3134 mouse event for a click on or below the
3135 message line. */
3136 if (Y_2_ROW(y) >= msg_row)
3137 checkfor = MOUSE_NORMAL;
3138 else
3139 checkfor = MOUSE_RETURN;
3140 break;
3141
3142 /*
3143 * On the command line, use the clipboard selection on all lines
3144 * but the command line. But not when pasting.
3145 */
3146 case CMDLINE:
3147 case CMDLINE+LANGMAP:
3148 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3149 checkfor = MOUSE_NONE;
3150 else
3151 checkfor = MOUSE_COMMAND;
3152 break;
3153
3154 default:
3155 checkfor = MOUSE_NONE;
3156 break;
3157 };
3158
3159 /*
3160 * Allow clipboard selection of text on the command line in "normal"
3161 * modes. Don't do this when dragging the status line, or extending a
3162 * Visual selection.
3163 */
3164 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003165 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 && button != MOUSE_DRAG
3167# ifdef FEAT_MOUSESHAPE
3168 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170# endif
3171 )
3172 checkfor = MOUSE_NONE;
3173
3174 /*
3175 * Use modeless selection when holding CTRL and SHIFT pressed.
3176 */
3177 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3178 checkfor = MOUSE_NONEF;
3179
3180 /*
3181 * In Ex mode, always use modeless selection.
3182 */
3183 if (exmode_active)
3184 checkfor = MOUSE_NONE;
3185
3186 /*
3187 * If the mouse settings say to not use the mouse, use the modeless
3188 * selection. But if Visual is active, assume that only the Visual area
3189 * will be selected.
3190 * Exception: On the command line, both the selection is used and a mouse
3191 * key is send.
3192 */
3193 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 /* Don't do modeless selection in Visual mode. */
3196 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3197 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 /*
3200 * When 'mousemodel' is "popup", shift-left is translated to right.
3201 * But not when also using Ctrl.
3202 */
3203 if (mouse_model_popup() && button == MOUSE_LEFT
3204 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3205 {
3206 button = MOUSE_RIGHT;
3207 modifiers &= ~ MOUSE_SHIFT;
3208 }
3209
3210 /* If the selection is done, allow the right button to extend it.
3211 * If the selection is cleared, allow the right button to start it
3212 * from the cursor position. */
3213 if (button == MOUSE_RIGHT)
3214 {
3215 if (clip_star.state == SELECT_CLEARED)
3216 {
3217 if (State & CMDLINE)
3218 {
3219 col = msg_col;
3220 row = msg_row;
3221 }
3222 else
3223 {
3224 col = curwin->w_wcol;
3225 row = curwin->w_wrow + W_WINROW(curwin);
3226 }
3227 clip_start_selection(col, row, FALSE);
3228 }
3229 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3230 repeated_click);
3231 did_clip = TRUE;
3232 }
3233 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003234 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3237 did_clip = TRUE;
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 /* Always allow pasting */
3241 if (button != MOUSE_MIDDLE)
3242 {
3243 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3244 return;
3245 if (checkfor != MOUSE_COMMAND)
3246 button = MOUSE_LEFT;
3247 }
3248 repeated_click = FALSE;
3249 }
3250
3251 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003252 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253#endif
3254
3255 /* Don't put events in the input queue now. */
3256 if (hold_gui_events)
3257 return;
3258
3259 row = gui_xy2colrow(x, y, &col);
3260
3261 /*
3262 * If we are dragging and the mouse hasn't moved far enough to be on a
3263 * different character, then don't send an event to vim.
3264 */
3265 if (button == MOUSE_DRAG)
3266 {
3267 if (row == prev_row && col == prev_col)
3268 return;
3269 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3270 if (y < 0)
3271 row = -1;
3272 }
3273
3274 /*
3275 * If topline has changed (window scrolled) since the last click, reset
3276 * repeated_click, because we don't want starting Visual mode when
3277 * clicking on a different character in the text.
3278 */
3279 if (curwin->w_topline != gui_prev_topline
3280#ifdef FEAT_DIFF
3281 || curwin->w_topfill != gui_prev_topfill
3282#endif
3283 )
3284 repeated_click = FALSE;
3285
3286 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3287 string[1] = KS_MOUSE;
3288 string[2] = KE_FILLER;
3289 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3290 {
3291 if (repeated_click)
3292 {
3293 /*
3294 * Handle multiple clicks. They only count if the mouse is still
3295 * pointing at the same character.
3296 */
3297 if (button != prev_button || row != prev_row || col != prev_col)
3298 num_clicks = 1;
3299 else if (++num_clicks > 4)
3300 num_clicks = 1;
3301 }
3302 else
3303 num_clicks = 1;
3304 prev_button = button;
3305 gui_prev_topline = curwin->w_topline;
3306#ifdef FEAT_DIFF
3307 gui_prev_topfill = curwin->w_topfill;
3308#endif
3309
3310 string[3] = (char_u)(button | 0x20);
3311 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3312 }
3313 else
3314 string[3] = (char_u)button;
3315
3316 string[3] |= modifiers;
3317 fill_mouse_coord(string + 4, col, row);
3318 add_to_input_buf(string, 8);
3319
3320 if (row < 0)
3321 prev_row = 0;
3322 else
3323 prev_row = row;
3324 prev_col = col;
3325
3326 /*
3327 * We need to make sure this is cleared since Athena doesn't tell us when
3328 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3329 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003330#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 gui.dragged_sb = SBAR_NONE;
3332#endif
3333}
3334
3335/*
3336 * Convert x and y coordinate to column and row in text window.
3337 * Corrects for multi-byte character.
3338 * returns column in "*colp" and row as return value;
3339 */
3340 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003341gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 int col = check_col(X_2_COL(x));
3344 int row = check_row(Y_2_ROW(y));
3345
3346#ifdef FEAT_MBYTE
3347 *colp = mb_fix_col(col, row);
3348#else
3349 *colp = col;
3350#endif
3351 return row;
3352}
3353
3354#if defined(FEAT_MENU) || defined(PROTO)
3355/*
3356 * Callback function for when a menu entry has been selected.
3357 */
3358 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003359gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003361 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363 /* Don't put events in the input queue now. */
3364 if (hold_gui_events)
3365 return;
3366
3367 bytes[0] = CSI;
3368 bytes[1] = KS_MENU;
3369 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003370 add_to_input_buf(bytes, 3);
3371 add_long_to_buf((long_u)menu, bytes);
3372 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373}
3374#endif
3375
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003376static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378/*
3379 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003380 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * in p_go.
3382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003384gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#ifdef FEAT_MENU
3387 static int prev_menu_is_active = -1;
3388#endif
3389#ifdef FEAT_TOOLBAR
3390 static int prev_toolbar = -1;
3391 int using_toolbar = FALSE;
3392#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003393#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003394 int using_tabline;
3395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396#ifdef FEAT_FOOTER
3397 static int prev_footer = -1;
3398 int using_footer = FALSE;
3399#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 static int prev_tearoff = -1;
3402 int using_tearoff = FALSE;
3403#endif
3404
3405 char_u *p;
3406 int i;
3407#ifdef FEAT_MENU
3408 int grey_old, grey_new;
3409 char_u *temp;
3410#endif
3411 win_T *wp;
3412 int need_set_size;
3413 int fix_size;
3414
3415#ifdef FEAT_MENU
3416 if (oldval != NULL && gui.in_use)
3417 {
3418 /*
3419 * Check if the menu's go from grey to non-grey or vise versa.
3420 */
3421 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3422 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3423 if (grey_old != grey_new)
3424 {
3425 temp = p_go;
3426 p_go = oldval;
3427 gui_update_menus(MENU_ALL_MODES);
3428 p_go = temp;
3429 }
3430 }
3431 gui.menu_is_active = FALSE;
3432#endif
3433
3434 for (i = 0; i < 3; i++)
3435 gui.which_scrollbars[i] = FALSE;
3436 for (p = p_go; *p; p++)
3437 switch (*p)
3438 {
3439 case GO_LEFT:
3440 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3441 break;
3442 case GO_RIGHT:
3443 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 case GO_VLEFT:
3446 if (win_hasvertsplit())
3447 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3448 break;
3449 case GO_VRIGHT:
3450 if (win_hasvertsplit())
3451 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3452 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 case GO_BOT:
3454 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3455 break;
3456#ifdef FEAT_MENU
3457 case GO_MENUS:
3458 gui.menu_is_active = TRUE;
3459 break;
3460#endif
3461 case GO_GREY:
3462 /* make menu's have grey items, ignored here */
3463 break;
3464#ifdef FEAT_TOOLBAR
3465 case GO_TOOLBAR:
3466 using_toolbar = TRUE;
3467 break;
3468#endif
3469#ifdef FEAT_FOOTER
3470 case GO_FOOTER:
3471 using_footer = TRUE;
3472 break;
3473#endif
3474 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 using_tearoff = TRUE;
3477#endif
3478 break;
3479 default:
3480 /* Ignore options that are not supported */
3481 break;
3482 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (gui.in_use)
3485 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003486 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003488
3489#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003490 /* Update the GUI tab line, it may appear or disappear. This may
3491 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003492 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003493 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003494 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003495 /* We don't want a resize event change "Rows" here, save and
3496 * restore it. Resizing is handled below. */
3497 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003498 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003499 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003500 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003501 if (using_tabline)
3502 fix_size = TRUE;
3503 if (!gui_use_tabline())
3504 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3505 }
3506#endif
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 for (i = 0; i < 3; i++)
3509 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003510 /* The scrollbar needs to be updated when it is shown/unshown and
3511 * when switching tab pages. But the size only changes when it's
3512 * shown/unshown. Thus we need two places to remember whether a
3513 * scrollbar is there or not. */
3514 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003515 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003516 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 if (i == SBAR_BOTTOM)
3519 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3520 gui.which_scrollbars[i]);
3521 else
3522 {
3523 FOR_ALL_WINDOWS(wp)
3524 {
3525 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3526 }
3527 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003528 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3529 {
3530 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003531 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003532 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003533 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003534 if (gui.which_scrollbars[i])
3535 fix_size = TRUE;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003538 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003539 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541
3542#ifdef FEAT_MENU
3543 if (gui.menu_is_active != prev_menu_is_active)
3544 {
3545 /* We don't want a resize event change "Rows" here, save and
3546 * restore it. Resizing is handled below. */
3547 i = Rows;
3548 gui_mch_enable_menu(gui.menu_is_active);
3549 Rows = i;
3550 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003551 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (gui.menu_is_active)
3553 fix_size = TRUE;
3554 }
3555#endif
3556
3557#ifdef FEAT_TOOLBAR
3558 if (using_toolbar != prev_toolbar)
3559 {
3560 gui_mch_show_toolbar(using_toolbar);
3561 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003562 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 if (using_toolbar)
3564 fix_size = TRUE;
3565 }
3566#endif
3567#ifdef FEAT_FOOTER
3568 if (using_footer != prev_footer)
3569 {
3570 gui_mch_enable_footer(using_footer);
3571 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003572 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (using_footer)
3574 fix_size = TRUE;
3575 }
3576#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (using_tearoff != prev_tearoff)
3579 {
3580 gui_mch_toggle_tearoffs(using_tearoff);
3581 prev_tearoff = using_tearoff;
3582 }
3583#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003584 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003585 {
3586#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003587 long prev_Columns = Columns;
3588 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /* Adjust the size of the window to make the text area keep the
3591 * same size and to avoid that part of our window is off-screen
3592 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003593 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003594
3595#ifdef FEAT_GUI_GTK
3596 /* GTK has the annoying habit of sending us resize events when
3597 * changing the window size ourselves. This mostly happens when
3598 * waiting for a character to arrive, quite unpredictably, and may
3599 * change Columns and Rows when we don't want it. Wait for a
3600 * character here to avoid this effect.
3601 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003602 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003603 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003604 * Don't change Rows when adding menu/toolbar/tabline.
3605 * Don't change Columns when adding vertical toolbar. */
3606 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003607 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003608 if ((need_set_size & RESIZE_VERT) == 0)
3609 Rows = prev_Rows;
3610 if ((need_set_size & RESIZE_HOR) == 0)
3611 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003612#endif
3613 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003614 /* When the console tabline appears or disappears the window positions
3615 * change. */
3616 if (firstwin->w_winrow != tabline_height())
3617 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619}
3620
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003621#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3622/*
3623 * Return TRUE if the GUI is taking care of the tabline.
3624 * It may still be hidden if 'showtabline' is zero.
3625 */
3626 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003627gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003628{
3629 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3630}
3631
3632/*
3633 * Return TRUE if the GUI is showing the tabline.
3634 * This uses 'showtabline'.
3635 */
3636 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003637gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003638{
3639 if (!gui_use_tabline()
3640 || p_stal == 0
3641 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3642 return FALSE;
3643 return TRUE;
3644}
3645
3646/*
3647 * Update the tabline.
3648 * This may display/undisplay the tabline and update the labels.
3649 */
3650 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003651gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652{
3653 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003654 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003655
3656 if (!gui.starting && starting == 0)
3657 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003658 /* Updating the tabline uses direct GUI commands, flush
3659 * outstanding instructions first. (esp. clear screen) */
3660 out_flush();
3661 gui_mch_flush();
3662
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003663 if (!showit != !shown)
3664 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665 if (showit != 0)
3666 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003667
3668 /* When the tabs change from hidden to shown or from shown to
3669 * hidden the size of the text area should remain the same. */
3670 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003671 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003672 }
3673}
3674
3675/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003676 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003677 */
3678 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003679get_tabline_label(
3680 tabpage_T *tp,
3681 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003682{
3683 int modified = FALSE;
3684 char_u buf[40];
3685 int wincount;
3686 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003687 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688
Bram Moolenaar57657d82006-04-21 22:12:41 +00003689 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003690 opt = (tooltip ? &p_gtt : &p_gtl);
3691 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003692 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003693 int use_sandbox = FALSE;
3694 int save_called_emsg = called_emsg;
3695 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003696 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003697 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3698 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003699
3700 called_emsg = FALSE;
3701
3702 printer_page_num = tabpage_index(tp);
3703# ifdef FEAT_EVAL
3704 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003705 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003706# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003707 /* It's almost as going to the tabpage, but without autocommands. */
3708 curtab->tp_firstwin = firstwin;
3709 curtab->tp_lastwin = lastwin;
3710 curtab->tp_curwin = curwin;
3711 save_curtab = curtab;
3712 curtab = tp;
3713 topframe = curtab->tp_topframe;
3714 firstwin = curtab->tp_firstwin;
3715 lastwin = curtab->tp_lastwin;
3716 curwin = curtab->tp_curwin;
3717 curbuf = curwin->w_buffer;
3718
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003719 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003720 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003721 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003722 STRCPY(NameBuff, res);
3723
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003724 /* Back to the original curtab. */
3725 curtab = save_curtab;
3726 topframe = curtab->tp_topframe;
3727 firstwin = curtab->tp_firstwin;
3728 lastwin = curtab->tp_lastwin;
3729 curwin = curtab->tp_curwin;
3730 curbuf = curwin->w_buffer;
3731
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003732 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003733 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003734 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003735 called_emsg |= save_called_emsg;
3736 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003737
3738 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3739 * use a default label. */
3740 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003741 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003742 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003743 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003744 if (!tooltip)
3745 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003746
3747 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3748 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3749 if (bufIsChanged(wp->w_buffer))
3750 modified = TRUE;
3751 if (modified || wincount > 1)
3752 {
3753 if (wincount > 1)
3754 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3755 else
3756 buf[0] = NUL;
3757 if (modified)
3758 STRCAT(buf, "+");
3759 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003760 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003761 mch_memmove(NameBuff, buf, STRLEN(buf));
3762 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003763 }
3764}
3765
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003766/*
3767 * Send the event for clicking to select tab page "nr".
3768 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003769 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003770 */
3771 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003772send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003773{
3774 char_u string[3];
3775
3776 if (nr == tabpage_index(curtab))
3777 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003778
3779 /* Don't put events in the input queue now. */
3780 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003781# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003782 || cmdwin_type != 0
3783# endif
3784 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003785 {
3786 /* Set it back to the current tab page. */
3787 gui_mch_set_curtab(tabpage_index(curtab));
3788 return FALSE;
3789 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003790
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003791 string[0] = CSI;
3792 string[1] = KS_TABLINE;
3793 string[2] = KE_FILLER;
3794 add_to_input_buf(string, 3);
3795 string[0] = nr;
3796 add_to_input_buf_csi(string, 1);
3797 return TRUE;
3798}
3799
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003800/*
3801 * Send a tabline menu event
3802 */
3803 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003804send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003805{
3806 char_u string[3];
3807
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003808 /* Don't put events in the input queue now. */
3809 if (hold_gui_events)
3810 return;
3811
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003812 string[0] = CSI;
3813 string[1] = KS_TABMENU;
3814 string[2] = KE_FILLER;
3815 add_to_input_buf(string, 3);
3816 string[0] = tabidx;
3817 string[1] = (char_u)(long)event;
3818 add_to_input_buf_csi(string, 2);
3819}
3820
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823/*
3824 * Scrollbar stuff:
3825 */
3826
Bram Moolenaare45828b2006-02-15 22:12:56 +00003827/*
3828 * Remove all scrollbars. Used before switching to another tab page.
3829 */
3830 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003831gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003832{
3833 int i;
3834 win_T *wp;
3835
3836 for (i = 0; i < 3; i++)
3837 {
3838 if (i == SBAR_BOTTOM)
3839 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3840 else
3841 {
3842 FOR_ALL_WINDOWS(wp)
3843 {
3844 gui_do_scrollbar(wp, i, FALSE);
3845 }
3846 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003847 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003848 }
3849}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003852gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853{
3854 static int sbar_ident = 0;
3855
3856 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3857 sb->wp = wp;
3858 sb->type = type;
3859 sb->value = 0;
3860#ifdef FEAT_GUI_ATHENA
3861 sb->pixval = 0;
3862#endif
3863 sb->size = 1;
3864 sb->max = 1;
3865 sb->top = 0;
3866 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 sb->status_height = 0;
3869 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3870}
3871
3872/*
3873 * Find the scrollbar with the given index.
3874 */
3875 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003876gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 win_T *wp;
3879
3880 if (gui.bottom_sbar.ident == ident)
3881 return &gui.bottom_sbar;
3882 FOR_ALL_WINDOWS(wp)
3883 {
3884 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3885 return &wp->w_scrollbars[SBAR_LEFT];
3886 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3887 return &wp->w_scrollbars[SBAR_RIGHT];
3888 }
3889 return NULL;
3890}
3891
3892/*
3893 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3894 *
3895 * For Win32, Macintosh and GTK+ 2:
3896 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3897 * you stop dragging the scrollbar. We get here each time the scrollbar is
3898 * dragged another pixel, but as far as the rest of vim goes, it thinks
3899 * we're just hanging in the call to DispatchMessage() in
3900 * process_message(). The DispatchMessage() call that hangs was passed a
3901 * mouse button click event in the scrollbar window. -- webb.
3902 *
3903 * Solution: Do the scrolling right here. But only when allowed.
3904 * Ignore the scrollbars while executing an external command or when there
3905 * are still characters to be processed.
3906 */
3907 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003908gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 int sb_num;
3912#ifdef USE_ON_FLY_SCROLL
3913 colnr_T old_leftcol = curwin->w_leftcol;
3914# ifdef FEAT_SCROLLBIND
3915 linenr_T old_topline = curwin->w_topline;
3916# endif
3917# ifdef FEAT_DIFF
3918 int old_topfill = curwin->w_topfill;
3919# endif
3920#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003921 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 int byte_count;
3923#endif
3924
3925 if (sb == NULL)
3926 return;
3927
3928 /* Don't put events in the input queue now. */
3929 if (hold_gui_events)
3930 return;
3931
3932#ifdef FEAT_CMDWIN
3933 if (cmdwin_type != 0 && sb->wp != curwin)
3934 return;
3935#endif
3936
3937 if (still_dragging)
3938 {
3939 if (sb->wp == NULL)
3940 gui.dragged_sb = SBAR_BOTTOM;
3941 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3942 gui.dragged_sb = SBAR_LEFT;
3943 else
3944 gui.dragged_sb = SBAR_RIGHT;
3945 gui.dragged_wp = sb->wp;
3946 }
3947 else
3948 {
3949 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003950#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003952 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 gui.dragged_wp = NULL;
3954#endif
3955 }
3956
3957 /* Vertical sbar info is kept in the first sbar (the left one) */
3958 if (sb->wp != NULL)
3959 sb = &sb->wp->w_scrollbars[0];
3960
3961 /*
3962 * Check validity of value
3963 */
3964 if (value < 0)
3965 value = 0;
3966#ifdef SCROLL_PAST_END
3967 else if (value > sb->max)
3968 value = sb->max;
3969#else
3970 if (value > sb->max - sb->size + 1)
3971 value = sb->max - sb->size + 1;
3972#endif
3973
3974 sb->value = value;
3975
3976#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003977 /* When not allowed to do the scrolling right now, return.
3978 * This also checked input_available(), but that causes the first click in
3979 * a scrollbar to be ignored when Vim doesn't have focus. */
3980 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return;
3982#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003983#ifdef FEAT_INS_EXPAND
3984 /* Disallow scrolling the current window when the completion popup menu is
3985 * visible. */
3986 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3987 return;
3988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990#ifdef FEAT_RIGHTLEFT
3991 if (sb->wp == NULL && curwin->w_p_rl)
3992 {
3993 value = sb->max + 1 - sb->size - value;
3994 if (value < 0)
3995 value = 0;
3996 }
3997#endif
3998
3999 if (sb->wp != NULL) /* vertical scrollbar */
4000 {
4001 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4003 sb_num++;
4004 if (wp == NULL)
4005 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007#ifdef USE_ON_FLY_SCROLL
4008 current_scrollbar = sb_num;
4009 scrollbar_value = value;
4010 if (State & NORMAL)
4011 {
4012 gui_do_scroll();
4013 setcursor();
4014 }
4015 else if (State & INSERT)
4016 {
4017 ins_scroll();
4018 setcursor();
4019 }
4020 else if (State & CMDLINE)
4021 {
4022 if (msg_scrolled == 0)
4023 {
4024 gui_do_scroll();
4025 redrawcmdline();
4026 }
4027 }
4028# ifdef FEAT_FOLDING
4029 /* Value may have been changed for closed fold. */
4030 sb->value = sb->wp->w_topline - 1;
4031# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004032
4033 /* When dragging one scrollbar and there is another one at the other
4034 * side move the thumb of that one too. */
4035 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4036 gui_mch_set_scrollbar_thumb(
4037 &sb->wp->w_scrollbars[
4038 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4039 ? SBAR_LEFT : SBAR_RIGHT],
4040 sb->value, sb->size, sb->max);
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#else
4043 bytes[0] = CSI;
4044 bytes[1] = KS_VER_SCROLLBAR;
4045 bytes[2] = KE_FILLER;
4046 bytes[3] = (char_u)sb_num;
4047 byte_count = 4;
4048#endif
4049 }
4050 else
4051 {
4052#ifdef USE_ON_FLY_SCROLL
4053 scrollbar_value = value;
4054
4055 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004056 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else if (State & INSERT)
4058 ins_horscroll();
4059 else if (State & CMDLINE)
4060 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004061 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004063 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 redrawcmdline();
4065 }
4066 }
4067 if (old_leftcol != curwin->w_leftcol)
4068 {
4069 updateWindow(curwin); /* update window, status and cmdline */
4070 setcursor();
4071 }
4072#else
4073 bytes[0] = CSI;
4074 bytes[1] = KS_HOR_SCROLLBAR;
4075 bytes[2] = KE_FILLER;
4076 byte_count = 3;
4077#endif
4078 }
4079
4080#ifdef USE_ON_FLY_SCROLL
4081# ifdef FEAT_SCROLLBIND
4082 /*
4083 * synchronize other windows, as necessary according to 'scrollbind'
4084 */
4085 if (curwin->w_p_scb
4086 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4087 || (sb->wp == curwin && (curwin->w_topline != old_topline
4088# ifdef FEAT_DIFF
4089 || curwin->w_topfill != old_topfill
4090# endif
4091 ))))
4092 {
4093 do_check_scrollbind(TRUE);
4094 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004095 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (wp->w_redr_type > 0)
4097 updateWindow(wp);
4098 setcursor();
4099 }
4100# endif
4101 out_flush();
4102 gui_update_cursor(FALSE, TRUE);
4103#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004104 add_to_input_buf(bytes, byte_count);
4105 add_long_to_buf((long_u)value, bytes);
4106 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107#endif
4108}
4109
4110/*
4111 * Scrollbar stuff:
4112 */
4113
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004114/*
4115 * Called when something in the window layout has changed.
4116 */
4117 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004118gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004119{
4120 if (gui.in_use && starting == 0)
4121 {
4122 out_flush();
4123 gui_init_which_components(NULL);
4124 gui_update_scrollbars(TRUE);
4125 }
4126 need_mouse_correct = TRUE;
4127}
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004130gui_update_scrollbars(
4131 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 win_T *wp;
4134 scrollbar_T *sb;
4135 long val, size, max; /* need 32 bits here */
4136 int which_sb;
4137 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 /* Update the horizontal scrollbar */
4141 gui_update_horiz_scrollbar(force);
4142
4143#ifndef WIN3264
4144 /* Return straight away if there is neither a left nor right scrollbar.
4145 * On MS-Windows this is required anyway for scrollwheel messages. */
4146 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4147 return;
4148#endif
4149
4150 /*
4151 * Don't want to update a scrollbar while we're dragging it. But if we
4152 * have both a left and right scrollbar, and we drag one of them, we still
4153 * need to update the other one.
4154 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004155 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4156 && gui.which_scrollbars[SBAR_LEFT]
4157 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 /*
4160 * If we have two scrollbars and one of them is being dragged, just
4161 * copy the scrollbar position from the dragged one to the other one.
4162 */
4163 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4164 if (gui.dragged_wp != NULL)
4165 gui_mch_set_scrollbar_thumb(
4166 &gui.dragged_wp->w_scrollbars[which_sb],
4167 gui.dragged_wp->w_scrollbars[0].value,
4168 gui.dragged_wp->w_scrollbars[0].size,
4169 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171
4172 /* avoid that moving components around generates events */
4173 ++hold_gui_events;
4174
Bram Moolenaar45a24952016-07-24 22:25:15 +02004175 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 if (wp->w_buffer == NULL) /* just in case */
4178 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004179 /* Skip a scrollbar that is being dragged. */
4180 if (!force && (gui.dragged_sb == SBAR_LEFT
4181 || gui.dragged_sb == SBAR_RIGHT)
4182 && gui.dragged_wp == wp)
4183 continue;
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#ifdef SCROLL_PAST_END
4186 max = wp->w_buffer->b_ml.ml_line_count - 1;
4187#else
4188 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4189#endif
4190 if (max < 0) /* empty buffer */
4191 max = 0;
4192 val = wp->w_topline - 1;
4193 size = wp->w_height;
4194#ifdef SCROLL_PAST_END
4195 if (val > max) /* just in case */
4196 val = max;
4197#else
4198 if (size > max + 1) /* just in case */
4199 size = max + 1;
4200 if (val > max - size + 1)
4201 val = max - size + 1;
4202#endif
4203 if (val < 0) /* minimal value is 0 */
4204 val = 0;
4205
4206 /*
4207 * Scrollbar at index 0 (the left one) contains all the information.
4208 * It would be the same info for left and right so we just store it for
4209 * one of them.
4210 */
4211 sb = &wp->w_scrollbars[0];
4212
4213 /*
4214 * Note: no check for valid w_botline. If it's not valid the
4215 * scrollbars will be updated later anyway.
4216 */
4217 if (size < 1 || wp->w_botline - 2 > max)
4218 {
4219 /*
4220 * This can happen during changing files. Just don't update the
4221 * scrollbar for now.
4222 */
4223 sb->height = 0; /* Force update next time */
4224 if (gui.which_scrollbars[SBAR_LEFT])
4225 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4226 if (gui.which_scrollbars[SBAR_RIGHT])
4227 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4228 continue;
4229 }
4230 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 || sb->top != wp->w_winrow
4232 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004234 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
4236 /* Height, width or position of scrollbar has changed. For
4237 * vertical split: curwin changed. */
4238 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 sb->top = wp->w_winrow;
4240 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 /* Calculate height and position in pixels */
4244 h = (sb->height + sb->status_height) * gui.char_height;
4245 y = sb->top * gui.char_height + gui.border_offset;
4246#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4247 if (gui.menu_is_active)
4248 y += gui.menu_height;
4249#endif
4250
4251#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4252 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4253# ifdef FEAT_GUI_ATHENA
4254 y += gui.toolbar_height;
4255# else
4256# ifdef FEAT_GUI_MSWIN
4257 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4258# endif
4259# endif
4260#endif
4261
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004262#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4263 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004264 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004265#endif
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 /* Height of top scrollbar includes width of top border */
4270 h += gui.border_offset;
4271 y -= gui.border_offset;
4272 }
4273 if (gui.which_scrollbars[SBAR_LEFT])
4274 {
4275 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4276 gui.left_sbar_x, y,
4277 gui.scrollbar_width, h);
4278 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4279 }
4280 if (gui.which_scrollbars[SBAR_RIGHT])
4281 {
4282 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4283 gui.right_sbar_x, y,
4284 gui.scrollbar_width, h);
4285 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4286 }
4287 }
4288
4289 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4290 * checking if the thumb moved at least a pixel. Only do this for
4291 * Athena, most other GUIs require the update anyway to make the
4292 * arrows work. */
4293#ifdef FEAT_GUI_ATHENA
4294 if (max == 0)
4295 y = 0;
4296 else
4297 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4298 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4299#else
4300 if (force || sb->value != val || sb->size != size || sb->max != max)
4301#endif
4302 {
4303 /* Thumb of scrollbar has moved */
4304 sb->value = val;
4305#ifdef FEAT_GUI_ATHENA
4306 sb->pixval = y;
4307#endif
4308 sb->size = size;
4309 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004310 if (gui.which_scrollbars[SBAR_LEFT]
4311 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4313 val, size, max);
4314 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4317 val, size, max);
4318 }
4319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 --hold_gui_events;
4322}
4323
4324/*
4325 * Enable or disable a scrollbar.
4326 * Check for scrollbars for vertically split windows which are not enabled
4327 * sometimes.
4328 */
4329 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004330gui_do_scrollbar(
4331 win_T *wp,
4332 int which, /* SBAR_LEFT or SBAR_RIGHT */
4333 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 int midcol = curwin->w_wincol + curwin->w_width / 2;
4336 int has_midcol = (wp->w_wincol <= midcol
4337 && wp->w_wincol + wp->w_width >= midcol);
4338
4339 /* Only enable scrollbars that contain the middle column of the current
4340 * window. */
4341 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4342 {
4343 /* Scrollbars only on one side. Don't enable scrollbars that don't
4344 * contain the middle column of the current window. */
4345 if (!has_midcol)
4346 enable = FALSE;
4347 }
4348 else
4349 {
4350 /* Scrollbars on both sides. Don't enable scrollbars that neither
4351 * contain the middle column of the current window nor are on the far
4352 * side. */
4353 if (midcol > Columns / 2)
4354 {
4355 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4356 enable = FALSE;
4357 }
4358 else
4359 {
4360 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4361 : !has_midcol)
4362 enable = FALSE;
4363 }
4364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4366}
4367
4368/*
4369 * Scroll a window according to the values set in the globals current_scrollbar
4370 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4371 * or FALSE otherwise.
4372 */
4373 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004374gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 win_T *wp, *save_wp;
4377 int i;
4378 long nlines;
4379 pos_T old_cursor;
4380 linenr_T old_topline;
4381#ifdef FEAT_DIFF
4382 int old_topfill;
4383#endif
4384
4385 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4386 if (wp == NULL)
4387 break;
4388 if (wp == NULL)
4389 /* Couldn't find window */
4390 return FALSE;
4391
4392 /*
4393 * Compute number of lines to scroll. If zero, nothing to do.
4394 */
4395 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4396 if (nlines == 0)
4397 return FALSE;
4398
4399 save_wp = curwin;
4400 old_topline = wp->w_topline;
4401#ifdef FEAT_DIFF
4402 old_topfill = wp->w_topfill;
4403#endif
4404 old_cursor = wp->w_cursor;
4405 curwin = wp;
4406 curbuf = wp->w_buffer;
4407 if (nlines < 0)
4408 scrolldown(-nlines, gui.dragged_wp == NULL);
4409 else
4410 scrollup(nlines, gui.dragged_wp == NULL);
4411 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4412 * the mouse-up event already, but we still want it to behave like when
4413 * dragging. But not the next click in an arrow. */
4414 if (gui.dragged_sb == SBAR_NONE)
4415 gui.dragged_wp = NULL;
4416
4417 if (old_topline != wp->w_topline
4418#ifdef FEAT_DIFF
4419 || old_topfill != wp->w_topfill
4420#endif
4421 )
4422 {
4423 if (p_so != 0)
4424 {
4425 cursor_correct(); /* fix window for 'so' */
4426 update_topline(); /* avoid up/down jump */
4427 }
4428 if (old_cursor.lnum != wp->w_cursor.lnum)
4429 coladvance(wp->w_curswant);
4430#ifdef FEAT_SCROLLBIND
4431 wp->w_scbind_pos = wp->w_topline;
4432#endif
4433 }
4434
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004435 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4436 validate_cursor();
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 curwin = save_wp;
4439 curbuf = save_wp->w_buffer;
4440
4441 /*
4442 * Don't call updateWindow() when nothing has changed (it will overwrite
4443 * the status line!).
4444 */
4445 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004446 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_DIFF
4448 || old_topfill != wp->w_topfill
4449#endif
4450 )
4451 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004452 int type = VALID;
4453
4454#ifdef FEAT_INS_EXPAND
4455 if (pum_visible())
4456 {
4457 type = NOT_VALID;
4458 wp->w_lines_valid = 0;
4459 }
4460#endif
4461 /* Don't set must_redraw here, it may cause the popup menu to
4462 * disappear when losing focus after a scrollbar drag. */
4463 if (wp->w_redr_type < type)
4464 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 updateWindow(wp); /* update window, status line, and cmdline */
4466 }
4467
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004468#ifdef FEAT_INS_EXPAND
4469 /* May need to redraw the popup menu. */
4470 if (pum_visible())
4471 pum_redraw();
4472#endif
4473
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004474 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475}
4476
4477
4478/*
4479 * Horizontal scrollbar stuff:
4480 */
4481
4482/*
4483 * Return length of line "lnum" for horizontal scrolling.
4484 */
4485 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004486scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488 char_u *p;
4489 colnr_T col;
4490 int w;
4491
4492 p = ml_get(lnum);
4493 col = 0;
4494 if (*p != NUL)
4495 for (;;)
4496 {
4497 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004498 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (*p == NUL) /* don't count the last character */
4500 break;
4501 col += w;
4502 }
4503 return col;
4504}
4505
4506/* Remember which line is currently the longest, so that we don't have to
4507 * search for it when scrolling horizontally. */
4508static linenr_T longest_lnum = 0;
4509
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004510/*
4511 * Find longest visible line number. If this is not possible (or not desired,
4512 * by setting 'h' in "guioptions") then the current line number is returned.
4513 */
4514 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004515gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004516{
4517 linenr_T ret = 0;
4518
4519 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4520 * line numbers, topline and botline can be invalid when displaying is
4521 * postponed. */
4522 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4523 && curwin->w_topline <= curwin->w_cursor.lnum
4524 && curwin->w_botline > curwin->w_cursor.lnum
4525 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4526 {
4527 linenr_T lnum;
4528 colnr_T n;
4529 long max = 0;
4530
4531 /* Use maximum of all visible lines. Remember the lnum of the
4532 * longest line, closest to the cursor line. Used when scrolling
4533 * below. */
4534 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4535 {
4536 n = scroll_line_len(lnum);
4537 if (n > (colnr_T)max)
4538 {
4539 max = n;
4540 ret = lnum;
4541 }
4542 else if (n == (colnr_T)max
4543 && abs((int)(lnum - curwin->w_cursor.lnum))
4544 < abs((int)(ret - curwin->w_cursor.lnum)))
4545 ret = lnum;
4546 }
4547 }
4548 else
4549 /* Use cursor line only. */
4550 ret = curwin->w_cursor.lnum;
4551
4552 return ret;
4553}
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004556gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 long value, size, max; /* need 32 bit ints here */
4559
4560 if (!gui.which_scrollbars[SBAR_BOTTOM])
4561 return;
4562
4563 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4564 return;
4565
4566 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4567 return;
4568
4569 /*
4570 * It is possible for the cursor to be invalid if we're in the middle of
4571 * something (like changing files). If so, don't do anything for now.
4572 */
4573 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4574 {
4575 gui.bottom_sbar.value = -1;
4576 return;
4577 }
4578
Bram Moolenaar02631462017-09-22 15:20:32 +02004579 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (curwin->w_p_wrap)
4581 {
4582 value = 0;
4583#ifdef SCROLL_PAST_END
4584 max = 0;
4585#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004586 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
4588 }
4589 else
4590 {
4591 value = curwin->w_leftcol;
4592
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004593 longest_lnum = gui_find_longest_lnum();
4594 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#ifdef FEAT_VIRTUALEDIT
4597 if (virtual_active())
4598 {
4599 /* May move the cursor even further to the right. */
4600 if (curwin->w_virtcol >= (colnr_T)max)
4601 max = curwin->w_virtcol;
4602 }
4603#endif
4604
4605#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004606 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
4608 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004609 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 size -= curwin_col_off();
4611#ifndef SCROLL_PAST_END
4612 max -= curwin_col_off();
4613#endif
4614 }
4615
4616#ifndef SCROLL_PAST_END
4617 if (value > max - size + 1)
4618 value = max - size + 1; /* limit the value to allowable range */
4619#endif
4620
4621#ifdef FEAT_RIGHTLEFT
4622 if (curwin->w_p_rl)
4623 {
4624 value = max + 1 - size - value;
4625 if (value < 0)
4626 {
4627 size += value;
4628 value = 0;
4629 }
4630 }
4631#endif
4632 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4633 && max == gui.bottom_sbar.max)
4634 return;
4635
4636 gui.bottom_sbar.value = value;
4637 gui.bottom_sbar.size = size;
4638 gui.bottom_sbar.max = max;
4639 gui.prev_wrap = curwin->w_p_wrap;
4640
4641 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4642}
4643
4644/*
4645 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4646 */
4647 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004648gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
4650 /* no wrapping, no scrolling */
4651 if (curwin->w_p_wrap)
4652 return FALSE;
4653
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004654 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 return FALSE;
4656
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004657 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004662 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004663 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004665 if (compute_longest_lnum)
4666 {
4667 curwin->w_cursor.lnum = gui_find_longest_lnum();
4668 curwin->w_cursor.col = 0;
4669 }
4670 /* Do a sanity check on "longest_lnum", just in case. */
4671 else if (longest_lnum >= curwin->w_topline
4672 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
4674 curwin->w_cursor.lnum = longest_lnum;
4675 curwin->w_cursor.col = 0;
4676 }
4677 }
4678
4679 return leftcol_changed();
4680}
4681
4682/*
4683 * Check that none of the colors are the same as the background color
4684 */
4685 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004686gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4689 {
4690 gui_set_bg_color((char_u *)"White");
4691 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4692 gui_set_fg_color((char_u *)"Black");
4693 }
4694}
4695
Bram Moolenaar3918c952005-03-15 22:34:55 +00004696 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004697gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 gui.norm_pixel = gui_get_color(name);
4700 hl_set_fg_color_name(vim_strsave(name));
4701}
4702
Bram Moolenaar3918c952005-03-15 22:34:55 +00004703 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004704gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 gui.back_pixel = gui_get_color(name);
4707 hl_set_bg_color_name(vim_strsave(name));
4708}
4709
4710/*
4711 * Allocate a color by name.
4712 * Returns INVALCOLOR and gives an error message when failed.
4713 */
4714 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004715gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 guicolor_T t;
4718
4719 if (*name == NUL)
4720 return INVALCOLOR;
4721 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004724#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 && gui.in_use
4726#endif
4727 )
4728 EMSG2(_("E254: Cannot allocate color %s"), name);
4729 return t;
4730}
4731
4732/*
4733 * Return the grey value of a color (range 0-255).
4734 */
4735 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004736gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004738 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004740 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004741 + (((rgb >> 8) & 0xff) * 587)
4742 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
4745#if defined(FEAT_GUI_X11) || defined(PROTO)
4746 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004747gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 win_T *wp;
4750
4751 /* Nothing to do if GUI hasn't started yet. */
4752 if (!gui.in_use)
4753 return;
4754
4755 FOR_ALL_WINDOWS(wp)
4756 {
4757 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4758 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4759 }
4760 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4761}
4762#endif
4763
4764/*
4765 * Call this when focus has changed.
4766 */
4767 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004768gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770/*
4771 * Skip this code to avoid drawing the cursor when debugging and switching
4772 * between the debugger window and gvim.
4773 */
4774#if 1
4775 gui.in_focus = in_focus;
4776 out_flush(); /* make sure output has been written */
4777 gui_update_cursor(TRUE, FALSE);
4778
4779# ifdef FEAT_XIM
4780 xim_set_focus(in_focus);
4781# endif
4782
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004783 /* Put events in the input queue only when allowed.
4784 * ui_focus_change() isn't called directly, because it invokes
4785 * autocommands and that must not happen asynchronously. */
4786 if (!hold_gui_events)
4787 {
4788 char_u bytes[3];
4789
4790 bytes[0] = CSI;
4791 bytes[1] = KS_EXTRA;
4792 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4793 add_to_input_buf(bytes, 3);
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#endif
4796}
4797
4798/*
4799 * Called when the mouse moved (but not when dragging).
4800 */
4801 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004802gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004805 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
Bram Moolenaard3667a22006-03-16 21:35:52 +00004807 /* Ignore this while still starting up. */
4808 if (!gui.in_use || gui.starting)
4809 return;
4810
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#ifdef FEAT_MOUSESHAPE
4812 /* Get window pointer, and update mouse shape as well. */
4813 wp = xy2win(x, y);
4814#endif
4815
4816 /* Only handle this when 'mousefocus' set and ... */
4817 if (p_mousef
4818 && !hold_gui_events /* not holding events */
4819 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4820 && State != HITRETURN /* but not hit-return prompt */
4821 && msg_scrolled == 0 /* no scrolled message */
4822 && !need_mouse_correct /* not moving the pointer */
4823 && gui.in_focus) /* gvim in focus */
4824 {
4825 /* Don't move the mouse when it's left or right of the Vim window */
4826 if (x < 0 || x > Columns * gui.char_width)
4827 return;
4828#ifndef FEAT_MOUSESHAPE
4829 wp = xy2win(x, y);
4830#endif
4831 if (wp == curwin || wp == NULL)
4832 return; /* still in the same old window, or none at all */
4833
Bram Moolenaar9c102382006-05-03 21:26:49 +00004834 /* Ignore position in the tab pages line. */
4835 if (Y_2_ROW(y) < tabline_height())
4836 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 /*
4839 * format a mouse click on status line input
4840 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004841 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4842 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 */
4844 if (finish_op)
4845 {
4846 /* abort the current operator first */
4847 st[0] = ESC;
4848 add_to_input_buf(st, 1);
4849 }
4850 st[0] = CSI;
4851 st[1] = KS_MOUSE;
4852 st[2] = KE_FILLER;
4853 st[3] = (char_u)MOUSE_LEFT;
4854 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004855 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 wp->w_height + W_WINROW(wp));
4857
4858 add_to_input_buf(st, 8);
4859 st[3] = (char_u)MOUSE_RELEASE;
4860 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#ifdef FEAT_GUI_GTK
4862 /* Need to wake up the main loop */
4863 if (gtk_main_level() > 0)
4864 gtk_main_quit();
4865#endif
4866 }
4867}
4868
4869/*
4870 * Called when mouse should be moved to window with focus.
4871 */
4872 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004873gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
4875 int x, y;
4876 win_T *wp = NULL;
4877
4878 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004879
4880 if (!(gui.in_use && p_mousef))
4881 return;
4882
4883 gui_mch_getmouse(&x, &y);
4884 /* Don't move the mouse when it's left or right of the Vim window */
4885 if (x < 0 || x > Columns * gui.char_width)
4886 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004887 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004888 wp = xy2win(x, y);
4889 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004891 validate_cline_row();
4892 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4893 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896}
4897
4898/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004899 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004902xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 int row;
4905 int col;
4906 win_T *wp;
4907
4908 row = Y_2_ROW(y);
4909 col = X_2_COL(x);
4910 if (row < 0 || col < 0) /* before first window */
4911 return NULL;
4912 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004913 if (wp == NULL)
4914 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004915#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (State == HITRETURN || State == ASKMORE)
4917 {
4918 if (Y_2_ROW(y) >= msg_row)
4919 update_mouseshape(SHAPE_IDX_MOREL);
4920 else
4921 update_mouseshape(SHAPE_IDX_MORE);
4922 }
4923 else if (row > wp->w_height) /* below status line */
4924 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004925 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004926 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004928 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004929 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 update_mouseshape(SHAPE_IDX_STATUS);
4931 else
4932 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004934 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935}
4936
4937/*
4938 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4939 * File names may be given to redefine the args list.
4940 */
4941 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004942ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 char_u *arg = eap->arg;
4945
4946 /*
4947 * Check for "-f" argument: foreground, don't fork.
4948 * Also don't fork when started with "gvim -f".
4949 * Do fork when using "gui -b".
4950 */
4951 if (arg[0] == '-'
4952 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004953 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 gui.dofork = (arg[1] == 'b');
4956 eap->arg = skipwhite(eap->arg + 2);
4957 }
4958 if (!gui.in_use)
4959 {
4960 /* Clear the command. Needed for when forking+exiting, to avoid part
4961 * of the argument ending up after the shell prompt. */
4962 msg_clr_eos_force();
4963 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004964#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004965 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 if (!ends_excmd(*eap->arg))
4969 ex_next(eap);
4970}
4971
4972#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004973 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974/*
4975 * This is shared between Athena, Motif and GTK.
4976 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004977static void gfp_setname(char_u *fname, void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979/*
4980 * Callback function for do_in_runtimepath().
4981 */
4982 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004983gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004985 char_u *gfp_buffer = cookie;
4986
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (STRLEN(fname) >= MAXPATHL)
4988 *gfp_buffer = NUL;
4989 else
4990 STRCPY(gfp_buffer, fname);
4991}
4992
4993/*
4994 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4995 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4996 */
4997 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004998gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000 if (STRLEN(name) > MAXPATHL - 14)
5001 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005002 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005003 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005004 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 return FAIL;
5006 return OK;
5007}
5008
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005009# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * Given the name of the "icon=" argument, try finding the bitmap file for the
5012 * icon. If it is an absolute path name, use it as it is. Otherwise append
5013 * "ext" and search for it in 'runtimepath'.
5014 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5015 * contains "name".
5016 */
5017 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005018gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020 char_u buf[MAXPATHL + 1];
5021
5022 expand_env(name, buffer, MAXPATHL);
5023 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5024 STRCPY(buffer, buf);
5025}
5026# endif
5027#endif
5028
Bram Moolenaar9372a112005-12-06 19:59:18 +00005029#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005031display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 char_u *p;
5034
5035 if (isatty(2))
5036 fflush(stderr);
5037 else if (error_ga.ga_data != NULL)
5038 {
5039 /* avoid putting up a message box with blanks only */
5040 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5041 if (!isspace(*p))
5042 {
5043 /* Truncate a very long message, it will go off-screen. */
5044 if (STRLEN(p) > 2000)
5045 STRCPY(p + 2000 - 14, "...(truncated)");
5046 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005047 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 break;
5049 }
5050 ga_clear(&error_ga);
5051 }
5052}
5053#endif
5054
5055#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5056/*
5057 * Return TRUE if still starting up and there is no place to enter text.
5058 * For GTK and X11 we check if stderr is not a tty, which means we were
5059 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5060 * allow typing on stdin.
5061 */
5062 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005063no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 return ((!gui.in_use || gui.starting)
5066# ifndef NO_CONSOLE
5067 && !isatty(0) && !isatty(2)
5068# endif
5069 );
5070}
5071#endif
5072
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005073#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005074 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005075 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076/*
5077 * Update the current window and the screen.
5078 */
5079 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005080gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005082#ifdef FEAT_CONCEAL
5083 linenr_T conceal_old_cursor_line = 0;
5084 linenr_T conceal_new_cursor_line = 0;
5085 int conceal_update_lines = FALSE;
5086#endif
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 update_topline();
5089 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005090
5091#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005092 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005093 if (!finish_op && (
5094# ifdef FEAT_AUTOCMD
5095 has_cursormoved()
5096# endif
5097# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5098 ||
5099# endif
5100# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005101 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005102# endif
5103 )
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005104 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005105 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005106# ifdef FEAT_AUTOCMD
5107 if (has_cursormoved())
5108 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5109# endif
5110# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005111 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005112 {
5113 conceal_old_cursor_line = last_cursormoved.lnum;
5114 conceal_new_cursor_line = curwin->w_cursor.lnum;
5115 conceal_update_lines = TRUE;
5116 }
5117# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005118 last_cursormoved = curwin->w_cursor;
5119 }
5120#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005121
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 update_screen(0); /* may need to update the screen */
5123 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005124# if defined(FEAT_CONCEAL)
5125 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005126 && (conceal_old_cursor_line != conceal_new_cursor_line
5127 || conceal_cursor_line(curwin)
5128 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005129 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005130 if (conceal_old_cursor_line != conceal_new_cursor_line)
5131 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005132 update_single_line(curwin, conceal_new_cursor_line);
5133 curwin->w_valid &= ~VALID_CROW;
5134 }
5135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 out_flush(); /* make sure output has been written */
5137 gui_update_cursor(TRUE, FALSE);
5138 gui_mch_flush();
5139}
5140#endif
5141
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005142#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005143static void concat_esc(garray_T *gap, char_u *text, int what);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145/*
5146 * Get the text to use in a find/replace dialog. Uses the last search pattern
5147 * if the argument is empty.
5148 * Returns an allocated string.
5149 */
5150 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005151get_find_dialog_text(
5152 char_u *arg,
5153 int *wwordp, /* return: TRUE if \< \> found */
5154 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 char_u *text;
5157
5158 if (*arg == NUL)
5159 text = last_search_pat();
5160 else
5161 text = arg;
5162 if (text != NULL)
5163 {
5164 text = vim_strsave(text);
5165 if (text != NULL)
5166 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005167 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 int i;
5169
5170 /* Remove "\V" */
5171 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5172 {
5173 mch_memmove(text, text + 2, (size_t)(len - 1));
5174 len -= 2;
5175 }
5176
5177 /* Recognize "\c" and "\C" and remove. */
5178 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5179 {
5180 *mcasep = (text[1] == 'C');
5181 mch_memmove(text, text + 2, (size_t)(len - 1));
5182 len -= 2;
5183 }
5184
5185 /* Recognize "\<text\>" and remove. */
5186 if (len >= 4
5187 && STRNCMP(text, "\\<", 2) == 0
5188 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5189 {
5190 *wwordp = TRUE;
5191 mch_memmove(text, text + 2, (size_t)(len - 4));
5192 text[len - 4] = NUL;
5193 }
5194
5195 /* Recognize "\/" or "\?" and remove. */
5196 for (i = 0; i + 1 < len; ++i)
5197 if (text[i] == '\\' && (text[i + 1] == '/'
5198 || text[i + 1] == '?'))
5199 {
5200 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5201 --len;
5202 }
5203 }
5204 }
5205 return text;
5206}
5207
5208/*
5209 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5210 */
5211 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005212concat_esc(garray_T *gap, char_u *text, int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
5214 while (*text != NUL)
5215 {
5216#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005217 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (l > 1)
5220 {
5221 while (--l >= 0)
5222 ga_append(gap, *text++);
5223 continue;
5224 }
5225#endif
5226 if (*text == what)
5227 ga_append(gap, '\\');
5228 ga_append(gap, *text);
5229 ++text;
5230 }
5231}
5232
5233/*
5234 * Handle the press of a button in the find-replace dialog.
5235 * Return TRUE when something was added to the input buffer.
5236 */
5237 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005238gui_do_findrepl(
5239 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5240 char_u *find_text,
5241 char_u *repl_text,
5242 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 garray_T ga;
5245 int i;
5246 int type = (flags & FRD_TYPE_MASK);
5247 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005248 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005249 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005250 static int busy = FALSE;
5251
5252 /* When the screen is being updated we should not change buffers and
5253 * windows structures, it may cause freed memory to be used. Also don't
5254 * do this recursively (pressing "Find" quickly several times. */
5255 if (updating_screen || busy)
5256 return FALSE;
5257
5258 /* refuse replace when text cannot be changed */
5259 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5260 return FALSE;
5261
5262 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005265 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ga_concat(&ga, (char_u *)"%s/");
5267
5268 ga_concat(&ga, (char_u *)"\\V");
5269 if (flags & FRD_MATCH_CASE)
5270 ga_concat(&ga, (char_u *)"\\C");
5271 else
5272 ga_concat(&ga, (char_u *)"\\c");
5273 if (flags & FRD_WHOLE_WORD)
5274 ga_concat(&ga, (char_u *)"\\<");
5275 if (type == FRD_REPLACEALL || down)
5276 concat_esc(&ga, find_text, '/'); /* escape slashes */
5277 else
5278 concat_esc(&ga, find_text, '?'); /* escape '?' */
5279 if (flags & FRD_WHOLE_WORD)
5280 ga_concat(&ga, (char_u *)"\\>");
5281
5282 if (type == FRD_REPLACEALL)
5283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005285 /* escape / and \ */
5286 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5287 if (p != NULL)
5288 ga_concat(&ga, p);
5289 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005291 }
5292 ga_append(&ga, NUL);
5293
5294 if (type == FRD_REPLACE)
5295 {
5296 /* Do the replacement when the text at the cursor matches. Thus no
5297 * replacement is done if the cursor was moved! */
5298 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5299 regmatch.rm_ic = 0;
5300 if (regmatch.regprog != NULL)
5301 {
5302 p = ml_get_cursor();
5303 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5304 && regmatch.startp[0] == p)
5305 {
5306 /* Clear the command line to remove any old "No match"
5307 * error. */
5308 msg_end_prompt();
5309
5310 if (u_save_cursor() == OK)
5311 {
5312 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005313 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005314
5315 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005316 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005317 ins_str(repl_text);
5318 }
5319 }
5320 else
5321 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005322 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005323 }
5324 }
5325
5326 if (type == FRD_REPLACEALL)
5327 {
5328 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005329 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 do_cmdline_cmd(ga.ga_data);
5331 }
5332 else
5333 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005334 int searchflags = SEARCH_MSG + SEARCH_MARK;
5335
5336 /* Search for the next match.
5337 * Don't skip text under cursor for single replace. */
5338 if (type == FRD_REPLACE)
5339 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 i = msg_scroll;
Bram Moolenaarcde88542015-08-11 19:14:00 +02005341 (void)do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005342 searchflags, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 msg_scroll = i; /* don't let an error message set msg_scroll */
5344 }
5345
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005346 /* Don't want to pass did_emsg to other code, it may cause disabling
5347 * syntax HL if we were busy redrawing. */
5348 did_emsg = save_did_emsg;
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (State & (NORMAL | INSERT))
5351 {
5352 gui_update_screen(); /* update the screen */
5353 msg_didout = 0; /* overwrite any message */
5354 need_wait_return = FALSE; /* don't wait for return */
5355 }
5356
5357 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005358 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 return (ga.ga_len > 0);
5360}
5361
5362#endif
5363
5364#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5365 || defined(FEAT_GUI_MSWIN) \
5366 || defined(FEAT_GUI_MAC) \
5367 || defined(PROTO)
5368
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005369static void gui_wingoto_xy(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371/*
5372 * Jump to the window at specified point (x, y).
5373 */
5374 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005375gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 int row = Y_2_ROW(y);
5378 int col = X_2_COL(x);
5379 win_T *wp;
5380
5381 if (row >= 0 && col >= 0)
5382 {
5383 wp = mouse_find_win(&row, &col);
5384 if (wp != NULL && wp != curwin)
5385 win_goto(wp);
5386 }
5387}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389/*
5390 * Process file drop. Mouse cursor position, key modifiers, name of files
5391 * and count of files are given. Argument "fnames[count]" has full pathnames
5392 * of dropped files, they will be freed in this function, and caller can't use
5393 * fnames after call this function.
5394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005396gui_handle_drop(
5397 int x UNUSED,
5398 int y UNUSED,
5399 int_u modifiers,
5400 char_u **fnames,
5401 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 int i;
5404 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005405 static int entered = FALSE;
5406
5407 /*
5408 * This function is called by event handlers. Just in case we get a
5409 * second event before the first one is handled, ignore the second one.
5410 * Not sure if this can ever happen, just in case.
5411 */
5412 if (entered)
5413 return;
5414 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416 /*
5417 * When the cursor is at the command line, add the file names to the
5418 * command line, don't edit the files.
5419 */
5420 if (State & CMDLINE)
5421 {
5422 shorten_filenames(fnames, count);
5423 for (i = 0; i < count; ++i)
5424 {
5425 if (fnames[i] != NULL)
5426 {
5427 if (i > 0)
5428 add_to_input_buf((char_u*)" ", 1);
5429
5430 /* We don't know what command is used thus we can't be sure
5431 * about which characters need to be escaped. Only escape the
5432 * most common ones. */
5433# ifdef BACKSLASH_IN_FILENAME
5434 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5435# else
5436 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5437# endif
5438 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005439 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 vim_free(p);
5441 vim_free(fnames[i]);
5442 }
5443 }
5444 vim_free(fnames);
5445 }
5446 else
5447 {
5448 /* Go to the window under mouse cursor, then shorten given "fnames" by
5449 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 shorten_filenames(fnames, count);
5452
5453 /* If Shift held down, remember the first item. */
5454 if ((modifiers & MOUSE_SHIFT) != 0)
5455 p = vim_strsave(fnames[0]);
5456 else
5457 p = NULL;
5458
5459 /* Handle the drop, :edit or :split to get to the file. This also
5460 * frees fnames[]. Skip this if there is only one item it's a
5461 * directory and Shift is held down. */
5462 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5463 && mch_isdir(fnames[0]))
5464 {
5465 vim_free(fnames[0]);
5466 vim_free(fnames);
5467 }
5468 else
5469 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5470
5471 /* If Shift held down, change to first file's directory. If the first
5472 * item is a directory, change to that directory (and let the explorer
5473 * plugin show the contents). */
5474 if (p != NULL)
5475 {
5476 if (mch_isdir(p))
5477 {
5478 if (mch_chdir((char *)p) == 0)
5479 shorten_fnames(TRUE);
5480 }
5481 else if (vim_chdirfile(p) == OK)
5482 shorten_fnames(TRUE);
5483 vim_free(p);
5484 }
5485
5486 /* Update the screen display */
5487 update_screen(NOT_VALID);
5488# ifdef FEAT_MENU
5489 gui_update_menus(0);
5490# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005491#ifdef FEAT_TITLE
5492 maketitle();
5493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 setcursor();
5495 out_flush();
5496 gui_update_cursor(FALSE, FALSE);
5497 gui_mch_flush();
5498 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005499
5500 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501}
5502#endif