blob: 7ef1c95341d60f3141e428c4d9407d9b72c2b9a8 [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);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001121 if (shape->blinkwait == 0 || shape->blinkon == 0
1122 || shape->blinkoff == 0)
Bram Moolenaar2f27aab2017-11-12 18:32:00 +01001123 gui_mch_stop_blink();
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001124#ifdef FEAT_TERMINAL
1125 if (shape_bg != INVALCOLOR)
1126 {
1127 cattr = 0;
1128 cfg = shape_fg;
1129 cbg = shape_bg;
1130 }
1131 else
1132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (id > 0)
1134 {
1135 cattr = syn_id2colors(id, &cfg, &cbg);
1136#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1137 {
1138 static int iid;
1139 guicolor_T fg, bg;
1140
Bram Moolenaarc236c162008-07-13 17:41:49 +00001141 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001142# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001143 preedit_get_status()
1144# else
1145 im_get_status()
1146# endif
1147 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 iid = syn_name2id((char_u *)"CursorIM");
1150 if (iid > 0)
1151 {
1152 syn_id2colors(iid, &fg, &bg);
1153 if (bg != INVALCOLOR)
1154 cbg = bg;
1155 if (fg != INVALCOLOR)
1156 cfg = fg;
1157 }
1158 }
1159 }
1160#endif
1161 }
1162
1163 /*
1164 * Get the attributes for the character under the cursor.
1165 * When no cursor color was given, use the character color.
1166 */
1167 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1168 if (attr > HL_ALL)
1169 aep = syn_gui_attr2entry(attr);
1170 if (aep != NULL)
1171 {
1172 attr = aep->ae_attr;
1173 if (cfg == INVALCOLOR)
1174 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1175 : aep->ae_u.gui.fg_color);
1176 if (cbg == INVALCOLOR)
1177 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1178 : aep->ae_u.gui.bg_color);
1179 }
1180 if (cfg == INVALCOLOR)
1181 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1182 if (cbg == INVALCOLOR)
1183 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1184
1185#ifdef FEAT_XIM
1186 if (aep != NULL)
1187 {
1188 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1189 : aep->ae_u.gui.bg_color);
1190 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1191 : aep->ae_u.gui.fg_color);
1192 if (xim_bg_color == INVALCOLOR)
1193 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1194 : gui.back_pixel;
1195 if (xim_fg_color == INVALCOLOR)
1196 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1197 : gui.norm_pixel;
1198 }
1199 else
1200 {
1201 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1202 : gui.back_pixel;
1203 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1204 : gui.norm_pixel;
1205 }
1206#endif
1207
1208 attr &= ~HL_INVERSE;
1209 if (cattr & HL_INVERSE)
1210 {
1211 cc = cbg;
1212 cbg = cfg;
1213 cfg = cc;
1214 }
1215 cattr &= ~HL_INVERSE;
1216
1217 /*
1218 * When we don't have window focus, draw a hollow cursor.
1219 */
1220 if (!gui.in_focus)
1221 {
1222 gui_mch_draw_hollow_cursor(cbg);
1223 return;
1224 }
1225
1226 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001227 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228#ifdef FEAT_HANGULIN
1229 || composing_hangul
1230#endif
1231 )
1232 {
1233 /*
1234 * Draw the text character with the cursor colors. Use the
1235 * character attributes plus the cursor attributes.
1236 */
1237 gui.highlight_mask = (cattr | attr);
1238#ifdef FEAT_HANGULIN
1239 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001240 {
1241 char_u *comp_buf;
1242 int comp_len;
1243
1244 comp_buf = hangul_composing_buffer_get(&comp_len);
1245 if (comp_buf)
1246 {
1247 (void)gui_outstr_nowrap(comp_buf, comp_len,
1248 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1249 cfg, cbg, 0);
1250 vim_free(comp_buf);
1251 }
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 else
1254#endif
1255 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1256 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1257 }
1258 else
1259 {
1260#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1261 int col_off = FALSE;
1262#endif
1263 /*
1264 * First draw the partial cursor, then overwrite with the text
1265 * character, using a transparent background.
1266 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001267 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001270 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 else
1273 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001274 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 cur_width = gui.char_width;
1276 }
1277#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001278 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1279 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
1281 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001282 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 cur_width += gui.char_width;
1284# ifdef FEAT_RIGHTLEFT
1285 if (CURSOR_BAR_RIGHT)
1286 {
1287 /* gui.col points to the left halve of the character but
1288 * the vertical line needs to be on the right halve.
1289 * A double-wide horizontal line is also drawn from the
1290 * right halve in gui_mch_draw_part_cursor(). */
1291 col_off = TRUE;
1292 ++gui.col;
1293 }
1294# endif
1295 }
1296#endif
1297 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1298#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1299 if (col_off)
1300 --gui.col;
1301#endif
1302
1303#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1304 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1305 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1306 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1307 (guicolor_T)0, (guicolor_T)0, 0);
1308#endif
1309 }
1310 gui.highlight_mask = old_hl_mask;
1311 }
1312}
1313
1314#if defined(FEAT_MENU) || defined(PROTO)
1315 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001316gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001318# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (gui.menu_is_active && gui.in_use)
1320 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1321# endif
1322}
1323#endif
1324
1325/*
1326 * Position the various GUI components (text area, menu). The vertical
1327 * scrollbars are NOT handled here. See gui_update_scrollbars().
1328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001330gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int text_area_x;
1333 int text_area_y;
1334 int text_area_width;
1335 int text_area_height;
1336
1337 /* avoid that moving components around generates events */
1338 ++hold_gui_events;
1339
1340 text_area_x = 0;
1341 if (gui.which_scrollbars[SBAR_LEFT])
1342 text_area_x += gui.scrollbar_width;
1343
1344 text_area_y = 0;
1345#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1346 gui.menu_width = total_width;
1347 if (gui.menu_is_active)
1348 text_area_y += gui.menu_height;
1349#endif
1350#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1351 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1352 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1353#endif
1354
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001355# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001356 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001357 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001358 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001359#endif
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1362 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1363 {
1364# ifdef FEAT_GUI_ATHENA
1365 gui_mch_set_toolbar_pos(0, text_area_y,
1366 gui.menu_width, gui.toolbar_height);
1367# endif
1368 text_area_y += gui.toolbar_height;
1369 }
1370#endif
1371
1372 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1373 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1374
1375 gui_mch_set_text_area_pos(text_area_x,
1376 text_area_y,
1377 text_area_width,
1378 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001379#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 + xim_get_status_area_height()
1381#endif
1382 );
1383#ifdef FEAT_MENU
1384 gui_position_menu();
1385#endif
1386 if (gui.which_scrollbars[SBAR_BOTTOM])
1387 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1388 text_area_x,
1389 text_area_y + text_area_height,
1390 text_area_width,
1391 gui.scrollbar_height);
1392 gui.left_sbar_x = 0;
1393 gui.right_sbar_x = text_area_x + text_area_width;
1394
1395 --hold_gui_events;
1396}
1397
Bram Moolenaar02743632005-07-25 20:42:36 +00001398/*
1399 * Get the width of the widgets and decorations to the side of the text area.
1400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001402gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 int base_width;
1405
1406 base_width = 2 * gui.border_offset;
1407 if (gui.which_scrollbars[SBAR_LEFT])
1408 base_width += gui.scrollbar_width;
1409 if (gui.which_scrollbars[SBAR_RIGHT])
1410 base_width += gui.scrollbar_width;
1411 return base_width;
1412}
1413
Bram Moolenaar02743632005-07-25 20:42:36 +00001414/*
1415 * Get the height of the widgets and decorations above and below the text area.
1416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001418gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 int base_height;
1421
1422 base_height = 2 * gui.border_offset;
1423 if (gui.which_scrollbars[SBAR_BOTTOM])
1424 base_height += gui.scrollbar_height;
1425#ifdef FEAT_GUI_GTK
1426 /* We can't take the sizes properly into account until anything is
1427 * realized. Therefore we recalculate all the values here just before
1428 * setting the size. (--mdcki) */
1429#else
1430# ifdef FEAT_MENU
1431 if (gui.menu_is_active)
1432 base_height += gui.menu_height;
1433# endif
1434# ifdef FEAT_TOOLBAR
1435 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1436# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1437 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1438# else
1439 base_height += gui.toolbar_height;
1440# endif
1441# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001442# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1443 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001444 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001445 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447# ifdef FEAT_FOOTER
1448 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1449 base_height += gui.footer_height;
1450# endif
1451# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1452 base_height += gui_mch_text_area_extra_height();
1453# endif
1454#endif
1455 return base_height;
1456}
1457
1458/*
1459 * Should be called after the GUI shell has been resized. Its arguments are
1460 * the new width and height of the shell in pixels.
1461 */
1462 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001463gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 static int busy = FALSE;
1466
1467 if (!gui.shell_created) /* ignore when still initializing */
1468 return;
1469
1470 /*
1471 * Can't resize the screen while it is being redrawn. Remember the new
1472 * size and handle it later.
1473 */
1474 if (updating_screen || busy)
1475 {
1476 new_pixel_width = pixel_width;
1477 new_pixel_height = pixel_height;
1478 return;
1479 }
1480
1481again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001482 new_pixel_width = 0;
1483 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 busy = TRUE;
1485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 /* Flush pending output before redrawing */
1487 out_flush();
1488
1489 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001490 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 /*
1496 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1497 * at the last line here (why does it have to be one row too low?).
1498 */
1499 if (State == ASKMORE || State == CONFIRM)
1500 gui.row = gui.num_rows;
1501
1502 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1503 * the safe side. */
1504 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1505 || gui.num_rows != Rows || gui.num_cols != Columns)
1506 shell_resized();
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 gui_update_scrollbars(TRUE);
1509 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001510#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 xim_set_status_area();
1512#endif
1513
1514 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001515
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001516 /* We may have been called again while redrawing the screen.
1517 * Need to do it all again with the latest size then. But only if the size
1518 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (new_pixel_height)
1520 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001521 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1522 {
1523 new_pixel_width = 0;
1524 new_pixel_height = 0;
1525 }
1526 else
1527 {
1528 pixel_width = new_pixel_width;
1529 pixel_height = new_pixel_height;
1530 goto again;
1531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533}
1534
1535/*
1536 * Check if gui_resize_shell() must be called.
1537 */
1538 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001539gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 /* careful: gui_resize_shell() may postpone the resize again if we
1543 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001544 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545}
1546
1547 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001548gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 Rows = gui.num_rows;
1551 Columns = gui.num_cols;
1552 return OK;
1553}
1554
1555/*
1556 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001557 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1558 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001559 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1560 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001563gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001564 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001565 int fit_to_display,
1566 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
1568 int base_width;
1569 int base_height;
1570 int width;
1571 int height;
1572 int min_width;
1573 int min_height;
1574 int screen_w;
1575 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001576#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001577 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001578 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001579#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001580 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 if (!gui.shell_created)
1583 return;
1584
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001585#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 /* If not setting to a user specified size and maximized, calculate the
1587 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001588 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1589 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 gui_mch_newfont();
1592 return;
1593 }
1594#endif
1595
1596 base_width = gui_get_base_width();
1597 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001598 if (fit_to_display)
1599 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001600 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602#ifdef USE_SUN_WORKSHOP
1603 if (!mustset && usingSunWorkShop
1604 && workshop_get_width_height(&width, &height))
1605 {
1606 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1607 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1608 }
1609 else
1610#endif
1611 {
1612 width = Columns * gui.char_width + base_width;
1613 height = Rows * gui.char_height + base_height;
1614 }
1615
1616 if (fit_to_display)
1617 {
1618 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001619 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 Columns = (screen_w - base_width) / gui.char_width;
1622 if (Columns < MIN_COLUMNS)
1623 Columns = MIN_COLUMNS;
1624 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001625#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001626 ++did_adjust;
1627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001629 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631 Rows = (screen_h - base_height) / gui.char_height;
1632 check_shellsize();
1633 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001634#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001635 ++did_adjust;
1636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001638#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001639 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1640 && height + gui.char_height >= screen_h))
1641 /* don't unmaximize if at maximum size */
1642 un_maximize = FALSE;
1643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001645 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 gui.num_cols = Columns;
1647 gui.num_rows = Rows;
1648
1649 min_width = base_width + MIN_COLUMNS * gui.char_width;
1650 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001651 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001652
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001653#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001654 if (un_maximize)
1655 {
1656 /* If the window size is smaller than the screen unmaximize the
1657 * window, otherwise resizing won't work. */
1658 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1659 if ((width + gui.char_width < screen_w
1660 || height + gui.char_height * 2 < screen_h)
1661 && gui_mch_maximized())
1662 gui_mch_unmaximize();
1663 }
1664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001667 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001669 if (fit_to_display && x >= 0 && y >= 0)
1670 {
1671 /* Some window managers put the Vim window left of/above the screen.
1672 * Only change the position if it wasn't already negative before
1673 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 gui_mch_update();
1675 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1676 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1677 }
1678
1679 gui_position_components(width);
1680 gui_update_scrollbars(TRUE);
1681 gui_reset_scroll_region();
1682}
1683
1684/*
1685 * Called when Rows and/or Columns has changed.
1686 */
1687 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001688gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 gui_reset_scroll_region();
1691}
1692
1693/*
1694 * Make scroll region cover whole screen.
1695 */
1696 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001697gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 gui.scroll_region_top = 0;
1700 gui.scroll_region_bot = gui.num_rows - 1;
1701 gui.scroll_region_left = 0;
1702 gui.scroll_region_right = gui.num_cols - 1;
1703}
1704
1705 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001706gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 if (mask > HL_ALL) /* highlight code */
1709 gui.highlight_mask = mask;
1710 else /* mask */
1711 gui.highlight_mask |= mask;
1712}
1713
1714 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001715gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 if (mask > HL_ALL) /* highlight code */
1718 gui.highlight_mask = HL_NORMAL;
1719 else /* mask */
1720 gui.highlight_mask &= ~mask;
1721}
1722
1723/*
1724 * Clear a rectangular region of the screen from text pos (row1, col1) to
1725 * (row2, col2) inclusive.
1726 */
1727 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001728gui_clear_block(
1729 int row1,
1730 int col1,
1731 int row2,
1732 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 /* Clear the selection if we are about to write over it */
1735 clip_may_clear_selection(row1, row2);
1736
1737 gui_mch_clear_block(row1, col1, row2, col2);
1738
1739 /* Invalidate cursor if it was in this block */
1740 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1741 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1742 gui.cursor_is_valid = FALSE;
1743}
1744
1745/*
1746 * Write code to update the cursor later. This avoids the need to flush the
1747 * output buffer before calling gui_update_cursor().
1748 */
1749 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001750gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001752 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753}
1754
1755 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001756gui_write(
1757 char_u *s,
1758 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 char_u *p;
1761 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int force_scrollbar = FALSE;
1764 static win_T *old_curwin = NULL;
1765
1766/* #define DEBUG_GUI_WRITE */
1767#ifdef DEBUG_GUI_WRITE
1768 {
1769 int i;
1770 char_u *str;
1771
1772 printf("gui_write(%d):\n ", len);
1773 for (i = 0; i < len; i++)
1774 if (s[i] == ESC)
1775 {
1776 if (i != 0)
1777 printf("\n ");
1778 printf("<ESC>");
1779 }
1780 else
1781 {
1782 str = transchar_byte(s[i]);
1783 if (str[0] && str[1])
1784 printf("<%s>", (char *)str);
1785 else
1786 printf("%s", (char *)str);
1787 }
1788 printf("\n");
1789 }
1790#endif
1791 while (len)
1792 {
1793 if (s[0] == ESC && s[1] == '|')
1794 {
1795 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001796 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 arg1 = getdigits(&p);
1799 if (p > s + len)
1800 break;
1801 if (*p == ';')
1802 {
1803 ++p;
1804 arg2 = getdigits(&p);
1805 if (p > s + len)
1806 break;
1807 }
1808 }
1809 switch (*p)
1810 {
1811 case 'C': /* Clear screen */
1812 clip_scroll_selection(9999);
1813 gui_mch_clear_all();
1814 gui.cursor_is_valid = FALSE;
1815 force_scrollbar = TRUE;
1816 break;
1817 case 'M': /* Move cursor */
1818 gui_set_cursor(arg1, arg2);
1819 break;
1820 case 's': /* force cursor (shape) update */
1821 force_cursor = TRUE;
1822 break;
1823 case 'R': /* Set scroll region */
1824 if (arg1 < arg2)
1825 {
1826 gui.scroll_region_top = arg1;
1827 gui.scroll_region_bot = arg2;
1828 }
1829 else
1830 {
1831 gui.scroll_region_top = arg2;
1832 gui.scroll_region_bot = arg1;
1833 }
1834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 case 'V': /* Set vertical scroll region */
1836 if (arg1 < arg2)
1837 {
1838 gui.scroll_region_left = arg1;
1839 gui.scroll_region_right = arg2;
1840 }
1841 else
1842 {
1843 gui.scroll_region_left = arg2;
1844 gui.scroll_region_right = arg1;
1845 }
1846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 case 'd': /* Delete line */
1848 gui_delete_lines(gui.row, 1);
1849 break;
1850 case 'D': /* Delete lines */
1851 gui_delete_lines(gui.row, arg1);
1852 break;
1853 case 'i': /* Insert line */
1854 gui_insert_lines(gui.row, 1);
1855 break;
1856 case 'I': /* Insert lines */
1857 gui_insert_lines(gui.row, arg1);
1858 break;
1859 case '$': /* Clear to end-of-line */
1860 gui_clear_block(gui.row, gui.col, gui.row,
1861 (int)Columns - 1);
1862 break;
1863 case 'h': /* Turn on highlighting */
1864 gui_start_highlight(arg1);
1865 break;
1866 case 'H': /* Turn off highlighting */
1867 gui_stop_highlight(arg1);
1868 break;
1869 case 'f': /* flash the window (visual bell) */
1870 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1871 break;
1872 default:
1873 p = s + 1; /* Skip the ESC */
1874 break;
1875 }
1876 len -= (int)(++p - s);
1877 s = p;
1878 }
1879 else if (
1880#ifdef EBCDIC
1881 CtrlChar(s[0]) != 0 /* Ctrl character */
1882#else
1883 s[0] < 0x20 /* Ctrl character */
1884#endif
1885#ifdef FEAT_SIGN_ICONS
1886 && s[0] != SIGN_BYTE
1887# ifdef FEAT_NETBEANS_INTG
1888 && s[0] != MULTISIGN_BYTE
1889# endif
1890#endif
1891 )
1892 {
1893 if (s[0] == '\n') /* NL */
1894 {
1895 gui.col = 0;
1896 if (gui.row < gui.scroll_region_bot)
1897 gui.row++;
1898 else
1899 gui_delete_lines(gui.scroll_region_top, 1);
1900 }
1901 else if (s[0] == '\r') /* CR */
1902 {
1903 gui.col = 0;
1904 }
1905 else if (s[0] == '\b') /* Backspace */
1906 {
1907 if (gui.col)
1908 --gui.col;
1909 }
1910 else if (s[0] == Ctrl_L) /* cursor-right */
1911 {
1912 ++gui.col;
1913 }
1914 else if (s[0] == Ctrl_G) /* Beep */
1915 {
1916 gui_mch_beep();
1917 }
1918 /* Other Ctrl character: shouldn't happen! */
1919
1920 --len; /* Skip this char */
1921 ++s;
1922 }
1923 else
1924 {
1925 p = s;
1926 while (len > 0 && (
1927#ifdef EBCDIC
1928 CtrlChar(*p) == 0
1929#else
1930 *p >= 0x20
1931#endif
1932#ifdef FEAT_SIGN_ICONS
1933 || *p == SIGN_BYTE
1934# ifdef FEAT_NETBEANS_INTG
1935 || *p == MULTISIGN_BYTE
1936# endif
1937#endif
1938 ))
1939 {
1940 len--;
1941 p++;
1942 }
1943 gui_outstr(s, (int)(p - s));
1944 s = p;
1945 }
1946 }
1947
1948 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1949 * set). */
1950 if (force_cursor)
1951 gui_update_cursor(TRUE, TRUE);
1952
1953 /* When switching to another window the dragging must have stopped.
1954 * Required for GTK, dragged_sb isn't reset. */
1955 if (old_curwin != curwin)
1956 gui.dragged_sb = SBAR_NONE;
1957
1958 /* Update the scrollbars after clearing the screen or when switched
1959 * to another window.
1960 * Update the horizontal scrollbar always, it's difficult to check all
1961 * situations where it might change. */
1962 if (force_scrollbar || old_curwin != curwin)
1963 gui_update_scrollbars(force_scrollbar);
1964 else
1965 gui_update_horiz_scrollbar(FALSE);
1966 old_curwin = curwin;
1967
1968 /*
1969 * We need to make sure this is cleared since Athena doesn't tell us when
1970 * he is done dragging. Do the same for GTK.
1971 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001972#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 gui.dragged_sb = SBAR_NONE;
1974#endif
1975
1976 gui_mch_flush(); /* In case vim decides to take a nap */
1977}
1978
1979/*
1980 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1981 * produces wrong results. Call gui_dont_update_cursor() before that code and
1982 * gui_can_update_cursor() afterwards.
1983 */
1984 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001985gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 if (gui.in_use)
1988 {
1989 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001990 if (undraw)
1991 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 can_update_cursor = FALSE;
1993 }
1994}
1995
1996 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001997gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 can_update_cursor = TRUE;
2000 /* No need to update the cursor right now, there is always more output
2001 * after scrolling. */
2002}
2003
2004 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002005gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 int this_len;
2008#ifdef FEAT_MBYTE
2009 int cells;
2010#endif
2011
2012 if (len == 0)
2013 return;
2014
2015 if (len < 0)
2016 len = (int)STRLEN(s);
2017
2018 while (len > 0)
2019 {
2020#ifdef FEAT_MBYTE
2021 if (has_mbyte)
2022 {
2023 /* Find out how many chars fit in the current line. */
2024 cells = 0;
2025 for (this_len = 0; this_len < len; )
2026 {
2027 cells += (*mb_ptr2cells)(s + this_len);
2028 if (gui.col + cells > Columns)
2029 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002030 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 if (this_len > len)
2033 this_len = len; /* don't include following composing char */
2034 }
2035 else
2036#endif
2037 if (gui.col + len > Columns)
2038 this_len = Columns - gui.col;
2039 else
2040 this_len = len;
2041
2042 (void)gui_outstr_nowrap(s, this_len,
2043 0, (guicolor_T)0, (guicolor_T)0, 0);
2044 s += this_len;
2045 len -= this_len;
2046#ifdef FEAT_MBYTE
2047 /* fill up for a double-width char that doesn't fit. */
2048 if (len > 0 && gui.col < Columns)
2049 (void)gui_outstr_nowrap((char_u *)" ", 1,
2050 0, (guicolor_T)0, (guicolor_T)0, 0);
2051#endif
2052 /* The cursor may wrap to the next line. */
2053 if (gui.col >= Columns)
2054 {
2055 gui.col = 0;
2056 gui.row++;
2057 }
2058 }
2059}
2060
2061/*
2062 * Output one character (may be one or two display cells).
2063 * Caller must check for valid "off".
2064 * Returns FAIL or OK, just like gui_outstr_nowrap().
2065 */
2066 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002067gui_screenchar(
2068 int off, /* Offset from start of screen */
2069 int flags,
2070 guicolor_T fg, /* colors for cursor */
2071 guicolor_T bg, /* colors for cursor */
2072 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074#ifdef FEAT_MBYTE
2075 char_u buf[MB_MAXBYTES + 1];
2076
2077 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2078 if (enc_utf8 && ScreenLines[off] == 0)
2079 return OK;
2080
2081 if (enc_utf8 && ScreenLinesUC[off] != 0)
2082 /* Draw UTF-8 multi-byte character. */
2083 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2084 flags, fg, bg, back);
2085
2086 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2087 {
2088 buf[0] = ScreenLines[off];
2089 buf[1] = ScreenLines2[off];
2090 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2091 }
2092
2093 /* Draw non-multi-byte character or DBCS character. */
2094 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002095 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 flags, fg, bg, back);
2097#else
2098 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2099#endif
2100}
2101
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002102#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103/*
2104 * Output the string at the given screen position. This is used in place
2105 * of gui_screenchar() where possible because Pango needs as much context
2106 * as possible to work nicely. It's a lot faster as well.
2107 */
2108 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002109gui_screenstr(
2110 int off, /* Offset from start of screen */
2111 int len, /* string length in screen cells */
2112 int flags,
2113 guicolor_T fg, /* colors for cursor */
2114 guicolor_T bg, /* colors for cursor */
2115 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 char_u *buf;
2118 int outlen = 0;
2119 int i;
2120 int retval;
2121
2122 if (len <= 0) /* "cannot happen"? */
2123 return OK;
2124
2125 if (enc_utf8)
2126 {
2127 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2128 if (buf == NULL)
2129 return OK; /* not much we could do here... */
2130
2131 for (i = off; i < off + len; ++i)
2132 {
2133 if (ScreenLines[i] == 0)
2134 continue; /* skip second half of double-width char */
2135
2136 if (ScreenLinesUC[i] == 0)
2137 buf[outlen++] = ScreenLines[i];
2138 else
2139 outlen += utfc_char2bytes(i, buf + outlen);
2140 }
2141
2142 buf[outlen] = NUL; /* only to aid debugging */
2143 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2144 vim_free(buf);
2145
2146 return retval;
2147 }
2148 else if (enc_dbcs == DBCS_JPNU)
2149 {
2150 buf = alloc((unsigned)(len * 2 + 1));
2151 if (buf == NULL)
2152 return OK; /* not much we could do here... */
2153
2154 for (i = off; i < off + len; ++i)
2155 {
2156 buf[outlen++] = ScreenLines[i];
2157
2158 /* handle double-byte single-width char */
2159 if (ScreenLines[i] == 0x8e)
2160 buf[outlen++] = ScreenLines2[i];
2161 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2162 buf[outlen++] = ScreenLines[++i];
2163 }
2164
2165 buf[outlen] = NUL; /* only to aid debugging */
2166 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2167 vim_free(buf);
2168
2169 return retval;
2170 }
2171 else
2172 {
2173 return gui_outstr_nowrap(&ScreenLines[off], len,
2174 flags, fg, bg, back);
2175 }
2176}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002177#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179/*
2180 * Output the given string at the current cursor position. If the string is
2181 * too long to fit on the line, then it is truncated.
2182 * "flags":
2183 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2184 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002185 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 * background.
2187 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2188 * it.
2189 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2190 * FAIL (the caller should start drawing "back" chars back).
2191 */
2192 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002193gui_outstr_nowrap(
2194 char_u *s,
2195 int len,
2196 int flags,
2197 guicolor_T fg, /* colors for cursor */
2198 guicolor_T bg, /* colors for cursor */
2199 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 long_u highlight_mask;
2202 long_u hl_mask_todo;
2203 guicolor_T fg_color;
2204 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002205 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002206#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002208# ifdef FEAT_MBYTE
2209 GuiFont wide_font = NOFONT;
2210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211# ifdef FEAT_XFONTSET
2212 GuiFontset fontset = NOFONTSET;
2213# endif
2214#endif
2215 attrentry_T *aep = NULL;
2216 int draw_flags;
2217 int col = gui.col;
2218#ifdef FEAT_SIGN_ICONS
2219 int draw_sign = FALSE;
2220# ifdef FEAT_NETBEANS_INTG
2221 int multi_sign = FALSE;
2222# endif
2223#endif
2224
2225 if (len < 0)
2226 len = (int)STRLEN(s);
2227 if (len == 0)
2228 return OK;
2229
2230#ifdef FEAT_SIGN_ICONS
2231 if (*s == SIGN_BYTE
2232# ifdef FEAT_NETBEANS_INTG
2233 || *s == MULTISIGN_BYTE
2234# endif
2235 )
2236 {
2237# ifdef FEAT_NETBEANS_INTG
2238 if (*s == MULTISIGN_BYTE)
2239 multi_sign = TRUE;
2240# endif
2241 /* draw spaces instead */
2242 s = (char_u *)" ";
2243 if (len == 1 && col > 0)
2244 --col;
2245 len = 2;
2246 draw_sign = TRUE;
2247 highlight_mask = 0;
2248 }
2249 else
2250#endif
2251 if (gui.highlight_mask > HL_ALL)
2252 {
2253 aep = syn_gui_attr2entry(gui.highlight_mask);
2254 if (aep == NULL) /* highlighting not set */
2255 highlight_mask = 0;
2256 else
2257 highlight_mask = aep->ae_attr;
2258 }
2259 else
2260 highlight_mask = gui.highlight_mask;
2261 hl_mask_todo = highlight_mask;
2262
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 /* Set the font */
2265 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2266 font = aep->ae_u.gui.font;
2267# ifdef FEAT_XFONTSET
2268 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2269 fontset = aep->ae_u.gui.fontset;
2270# endif
2271 else
2272 {
2273# ifdef FEAT_XFONTSET
2274 if (gui.fontset != NOFONTSET)
2275 fontset = gui.fontset;
2276 else
2277# endif
2278 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2279 {
2280 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2281 {
2282 font = gui.boldital_font;
2283 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2284 }
2285 else if (gui.bold_font != NOFONT)
2286 {
2287 font = gui.bold_font;
2288 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2289 }
2290 else
2291 font = gui.norm_font;
2292 }
2293 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2294 {
2295 font = gui.ital_font;
2296 hl_mask_todo &= ~HL_ITALIC;
2297 }
2298 else
2299 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002300
2301# ifdef FEAT_MBYTE
2302 /*
2303 * Choose correct wide_font by font. wide_font should be set with font
2304 * at same time in above block. But it will make many "ifdef" nasty
2305 * blocks. So we do it here.
2306 */
2307 if (font == gui.boldital_font && gui.wide_boldital_font)
2308 wide_font = gui.wide_boldital_font;
2309 else if (font == gui.bold_font && gui.wide_bold_font)
2310 wide_font = gui.wide_bold_font;
2311 else if (font == gui.ital_font && gui.wide_ital_font)
2312 wide_font = gui.wide_ital_font;
2313 else if (font == gui.norm_font && gui.wide_font)
2314 wide_font = gui.wide_font;
2315# endif
2316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318# ifdef FEAT_XFONTSET
2319 if (fontset != NOFONTSET)
2320 gui_mch_set_fontset(fontset);
2321 else
2322# endif
2323 gui_mch_set_font(font);
2324#endif
2325
2326 draw_flags = 0;
2327
2328 /* Set the color */
2329 bg_color = gui.back_pixel;
2330 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2331 {
2332 draw_flags |= DRAW_CURSOR;
2333 fg_color = fg;
2334 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002335 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337 else if (aep != NULL)
2338 {
2339 fg_color = aep->ae_u.gui.fg_color;
2340 if (fg_color == INVALCOLOR)
2341 fg_color = gui.norm_pixel;
2342 bg_color = aep->ae_u.gui.bg_color;
2343 if (bg_color == INVALCOLOR)
2344 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002345 sp_color = aep->ae_u.gui.sp_color;
2346 if (sp_color == INVALCOLOR)
2347 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002352 sp_color = fg_color;
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2356 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002357#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 gui_mch_set_colors(bg_color, fg_color);
2359#else
2360 gui_mch_set_fg_color(bg_color);
2361 gui_mch_set_bg_color(fg_color);
2362#endif
2363 }
2364 else
2365 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002366#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 gui_mch_set_colors(fg_color, bg_color);
2368#else
2369 gui_mch_set_fg_color(fg_color);
2370 gui_mch_set_bg_color(bg_color);
2371#endif
2372 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002373 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /* Clear the selection if we are about to write over it */
2376 if (!(flags & GUI_MON_NOCLEAR))
2377 clip_may_clear_selection(gui.row, gui.row);
2378
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 /* If there's no bold font, then fake it */
2381 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2382 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 /*
2385 * When drawing bold or italic characters the spill-over from the left
2386 * neighbor may be destroyed. Let the caller backup to start redrawing
2387 * just after a blank.
2388 */
2389 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2390 return FAIL;
2391
Bram Moolenaare60acc12011-05-10 16:41:25 +02002392#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 /* If there's no italic font, then fake it.
2394 * For GTK2, we don't need a different font for italic style. */
2395 if (hl_mask_todo & HL_ITALIC)
2396 draw_flags |= DRAW_ITALIC;
2397
2398 /* Do we underline the text? */
2399 if (hl_mask_todo & HL_UNDERLINE)
2400 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402#else
2403 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 draw_flags |= DRAW_UNDERL;
2406#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002407 /* Do we undercurl the text? */
2408 if (hl_mask_todo & HL_UNDERCURL)
2409 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002411 /* Do we strikethrough the text? */
2412 if (hl_mask_todo & HL_STRIKETHROUGH)
2413 draw_flags |= DRAW_STRIKE;
2414
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002415 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (flags & GUI_MON_TRS_CURSOR)
2417 draw_flags |= DRAW_TRANSP;
2418
2419 /*
2420 * Draw the text.
2421 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002422#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 /* The value returned is the length in display cells */
2424 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2425#else
2426# ifdef FEAT_MBYTE
2427 if (enc_utf8)
2428 {
2429 int start; /* index of bytes to be drawn */
2430 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002431 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 int cn; /* cellwidth of current char */
2433 int i; /* index of current char */
2434 int c; /* current char value */
2435 int cl; /* byte length of current char */
2436 int comping; /* current char is composing */
2437 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002438 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002439 int prev_wide = FALSE;
2440 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002441# ifdef WIN3264
2442 int sep_comp = FALSE; /* Don't separate composing chars. */
2443# else
2444 int sep_comp = TRUE; /* Separate composing chars. */
2445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
2447 /* Break the string at a composing character, it has to be drawn on
2448 * top of the previous character. */
2449 start = 0;
2450 cells = 0;
2451 for (i = 0; i < len; i += cl)
2452 {
2453 c = utf_ptr2char(s + i);
2454 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 comping = utf_iscomposing(c);
2456 if (!comping) /* count cells from non-composing chars */
2457 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002458 if (!comping || sep_comp)
2459 {
2460 if (cn > 1
2461# ifdef FEAT_XFONTSET
2462 && fontset == NOFONTSET
2463# endif
2464 && wide_font != NOFONT)
2465 curr_wide = TRUE;
2466 else
2467 curr_wide = FALSE;
2468 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002469 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (cl == 0) /* hit end of string */
2471 len = i + cl; /* len must be wrong "cannot happen" */
2472
Bram Moolenaar45933962013-01-23 17:43:57 +01002473 wide_changed = curr_wide != prev_wide;
2474
2475 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002477 if (i + cl >= len || (comping && sep_comp && i > start)
2478 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002479# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 || (cn > 1
2481# ifdef FEAT_XFONTSET
2482 /* No fontset: At least draw char after wide char at
2483 * right position. */
2484 && fontset == NOFONTSET
2485# endif
2486 )
2487# endif
2488 )
2489 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002490 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 thislen = i - start;
2492 else
2493 thislen = i - start + cl;
2494 if (thislen > 0)
2495 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002496 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002497 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2499 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002500 if (prev_wide)
2501 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 start += thislen;
2503 }
2504 scol += cells;
2505 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002506 /* Adjust to not draw a character which width is changed
2507 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002508 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002510 scol -= cn;
2511 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002514# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002515 /* No fontset: draw a space to fill the gap after a wide char
2516 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2518# ifdef FEAT_XFONTSET
2519 && fontset == NOFONTSET
2520# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002521 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2523 1, draw_flags);
2524# endif
2525 }
2526 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002527 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002529# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002530 /* Carbon ATSUI autodraws composing char over previous char */
2531 gui_mch_draw_string(gui.row, scol, s + i, cl,
2532 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002533# else
2534 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2535 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 start = i + cl;
2538 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002539 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 /* The stuff below assumes "len" is the length in screen columns. */
2542 len = scol - col;
2543 }
2544 else
2545# endif
2546 {
2547 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2548# ifdef FEAT_MBYTE
2549 if (enc_dbcs == DBCS_JPNU)
2550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 /* Get the length in display cells, this can be different from the
2552 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002553 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555# endif
2556 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002557#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2560 gui.col = col + len;
2561
2562 /* May need to invert it when it's part of the selection. */
2563 if (flags & GUI_MON_NOCLEAR)
2564 clip_may_redraw_selection(gui.row, col, len);
2565
2566 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2567 {
2568 /* Invalidate the old physical cursor position if we wrote over it */
2569 if (gui.cursor_row == gui.row
2570 && gui.cursor_col >= col
2571 && gui.cursor_col < col + len)
2572 gui.cursor_is_valid = FALSE;
2573 }
2574
2575#ifdef FEAT_SIGN_ICONS
2576 if (draw_sign)
2577 /* Draw the sign on top of the spaces. */
2578 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002579# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002580 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (multi_sign)
2582 netbeans_draw_multisign_indicator(gui.row);
2583# endif
2584#endif
2585
2586 return OK;
2587}
2588
2589/*
2590 * Un-draw the cursor. Actually this just redraws the character at the given
2591 * position. The character just before it too, for when it was in bold.
2592 */
2593 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002594gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 if (gui.cursor_is_valid)
2597 {
2598#ifdef FEAT_HANGULIN
2599 if (composing_hangul
2600 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002601 {
2602 char_u *comp_buf;
2603 int comp_len;
2604
2605 comp_buf = hangul_composing_buffer_get(&comp_len);
2606 if (comp_buf)
2607 {
2608 (void)gui_outstr_nowrap(comp_buf, comp_len,
2609 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2610 gui.norm_pixel, gui.back_pixel, 0);
2611 vim_free(comp_buf);
2612 }
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 else
2615 {
2616#endif
2617 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2618 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2619 && gui.cursor_col > 0)
2620 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2621 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2622#ifdef FEAT_HANGULIN
2623 if (composing_hangul)
2624 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2625 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2626 }
2627#endif
2628 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2629 * here in case it wasn't needed to undraw it. */
2630 gui.cursor_is_valid = FALSE;
2631 }
2632}
2633
2634 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002635gui_redraw(
2636 int x,
2637 int y,
2638 int w,
2639 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 int row1, col1, row2, col2;
2642
2643 row1 = Y_2_ROW(y);
2644 col1 = X_2_COL(x);
2645 row2 = Y_2_ROW(y + h - 1);
2646 col2 = X_2_COL(x + w - 1);
2647
2648 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2649
2650 /*
2651 * We may need to redraw the cursor, but don't take it upon us to change
2652 * its location after a scroll.
2653 * (maybe be more strict even and test col too?)
2654 * These things may be outside the update/clipping region and reality may
2655 * not reflect Vims internal ideas if these operations are clipped away.
2656 */
2657 if (gui.row == gui.cursor_row)
2658 gui_update_cursor(TRUE, TRUE);
2659}
2660
2661/*
2662 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2663 * from col1 to col2 (inclusive).
2664 * Return TRUE when the character before the first drawn character has
2665 * different attributes (may have to be redrawn too).
2666 */
2667 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002668gui_redraw_block(
2669 int row1,
2670 int col1,
2671 int row2,
2672 int col2,
2673 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 int old_row, old_col;
2676 long_u old_hl_mask;
2677 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002678 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int idx, len;
2680 int back, nback;
2681 int retval = FALSE;
2682#ifdef FEAT_MBYTE
2683 int orig_col1, orig_col2;
2684#endif
2685
2686 /* Don't try to update when ScreenLines is not valid */
2687 if (!screen_cleared || ScreenLines == NULL)
2688 return retval;
2689
2690 /* Don't try to draw outside the shell! */
2691 /* Check everything, strange values may be caused by a big border width */
2692 col1 = check_col(col1);
2693 col2 = check_col(col2);
2694 row1 = check_row(row1);
2695 row2 = check_row(row2);
2696
2697 /* Remember where our cursor was */
2698 old_row = gui.row;
2699 old_col = gui.col;
2700 old_hl_mask = gui.highlight_mask;
2701#ifdef FEAT_MBYTE
2702 orig_col1 = col1;
2703 orig_col2 = col2;
2704#endif
2705
2706 for (gui.row = row1; gui.row <= row2; gui.row++)
2707 {
2708#ifdef FEAT_MBYTE
2709 /* When only half of a double-wide character is in the block, include
2710 * the other half. */
2711 col1 = orig_col1;
2712 col2 = orig_col2;
2713 off = LineOffset[gui.row];
2714 if (enc_dbcs != 0)
2715 {
2716 if (col1 > 0)
2717 col1 -= dbcs_screen_head_off(ScreenLines + off,
2718 ScreenLines + off + col1);
2719 col2 += dbcs_screen_tail_off(ScreenLines + off,
2720 ScreenLines + off + col2);
2721 }
2722 else if (enc_utf8)
2723 {
2724 if (ScreenLines[off + col1] == 0)
2725 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002726# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2728 ++col2;
2729# endif
2730 }
2731#endif
2732 gui.col = col1;
2733 off = LineOffset[gui.row] + gui.col;
2734 len = col2 - col1 + 1;
2735
2736 /* Find how many chars back this highlighting starts, or where a space
2737 * is. Needed for when the bold trick is used */
2738 for (back = 0; back < col1; ++back)
2739 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2740 || ScreenLines[off - 1 - back] == ' ')
2741 break;
2742 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2743 && ScreenLines[off - 1] != ' ');
2744
2745 /* Break it up in strings of characters with the same attributes. */
2746 /* Print UTF-8 characters individually. */
2747 while (len > 0)
2748 {
2749 first_attr = ScreenAttrs[off];
2750 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002751#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (enc_utf8 && ScreenLinesUC[off] != 0)
2753 {
2754 /* output multi-byte character separately */
2755 nback = gui_screenchar(off, flags,
2756 (guicolor_T)0, (guicolor_T)0, back);
2757 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2758 idx = 2;
2759 else
2760 idx = 1;
2761 }
2762 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2763 {
2764 /* output double-byte, single-width character separately */
2765 nback = gui_screenchar(off, flags,
2766 (guicolor_T)0, (guicolor_T)0, back);
2767 idx = 1;
2768 }
2769 else
2770#endif
2771 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002772#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 for (idx = 0; idx < len; ++idx)
2774 {
2775 if (enc_utf8 && ScreenLines[off + idx] == 0)
2776 continue; /* skip second half of double-width char */
2777 if (ScreenAttrs[off + idx] != first_attr)
2778 break;
2779 }
2780 /* gui_screenstr() takes care of multibyte chars */
2781 nback = gui_screenstr(off, idx, flags,
2782 (guicolor_T)0, (guicolor_T)0, back);
2783#else
2784 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2785 idx++)
2786 {
2787# ifdef FEAT_MBYTE
2788 /* Stop at a multi-byte Unicode character. */
2789 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2790 break;
2791 if (enc_dbcs == DBCS_JPNU)
2792 {
2793 /* Stop at a double-byte single-width char. */
2794 if (ScreenLines[off + idx] == 0x8e)
2795 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002796 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 + off + idx) == 2)
2798 ++idx; /* skip second byte of double-byte char */
2799 }
2800# endif
2801 }
2802 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2803 (guicolor_T)0, (guicolor_T)0, back);
2804#endif
2805 }
2806 if (nback == FAIL)
2807 {
2808 /* Must back up to start drawing where a bold or italic word
2809 * starts. */
2810 off -= back;
2811 len += back;
2812 gui.col -= back;
2813 }
2814 else
2815 {
2816 off += idx;
2817 len -= idx;
2818 }
2819 back = 0;
2820 }
2821 }
2822
2823 /* Put the cursor back where it was */
2824 gui.row = old_row;
2825 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002826 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 return retval;
2829}
2830
2831 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002832gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
2834 if (count <= 0)
2835 return;
2836
2837 if (row + count > gui.scroll_region_bot)
2838 /* Scrolled out of region, just blank the lines out */
2839 gui_clear_block(row, gui.scroll_region_left,
2840 gui.scroll_region_bot, gui.scroll_region_right);
2841 else
2842 {
2843 gui_mch_delete_lines(row, count);
2844
2845 /* If the cursor was in the deleted lines it's now gone. If the
2846 * cursor was in the scrolled lines adjust its position. */
2847 if (gui.cursor_row >= row
2848 && gui.cursor_col >= gui.scroll_region_left
2849 && gui.cursor_col <= gui.scroll_region_right)
2850 {
2851 if (gui.cursor_row < row + count)
2852 gui.cursor_is_valid = FALSE;
2853 else if (gui.cursor_row <= gui.scroll_region_bot)
2854 gui.cursor_row -= count;
2855 }
2856 }
2857}
2858
2859 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002860gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
2862 if (count <= 0)
2863 return;
2864
2865 if (row + count > gui.scroll_region_bot)
2866 /* Scrolled out of region, just blank the lines out */
2867 gui_clear_block(row, gui.scroll_region_left,
2868 gui.scroll_region_bot, gui.scroll_region_right);
2869 else
2870 {
2871 gui_mch_insert_lines(row, count);
2872
2873 if (gui.cursor_row >= gui.row
2874 && gui.cursor_col >= gui.scroll_region_left
2875 && gui.cursor_col <= gui.scroll_region_right)
2876 {
2877 if (gui.cursor_row <= gui.scroll_region_bot - count)
2878 gui.cursor_row += count;
2879 else if (gui.cursor_row <= gui.scroll_region_bot)
2880 gui.cursor_is_valid = FALSE;
2881 }
2882 }
2883}
2884
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002885/*
2886 * Returns OK if a character was found to be available within the given time,
2887 * or FAIL otherwise.
2888 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002889 static int
2890gui_wait_for_chars_or_timer(long wtime)
2891{
2892#ifdef FEAT_TIMERS
2893 int due_time;
2894 long remaining = wtime;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002895 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002896
2897 /* When waiting very briefly don't trigger timers. */
2898 if (wtime >= 0 && wtime < 10L)
2899 return gui_mch_wait_for_chars(wtime);
2900
2901 while (wtime < 0 || remaining > 0)
2902 {
2903 /* Trigger timers and then get the time in wtime until the next one is
2904 * due. Wait up to that time. */
2905 due_time = check_due_timer();
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002906 if (typebuf.tb_change_cnt != tb_change_cnt)
2907 {
2908 /* timer may have used feedkeys() */
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002909 return FAIL;
Bram Moolenaar40b1b542016-04-20 20:18:23 +02002910 }
Bram Moolenaar975b5272016-03-15 23:10:59 +01002911 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
2912 due_time = remaining;
2913 if (gui_mch_wait_for_chars(due_time))
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002914 return OK;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002915 if (wtime > 0)
2916 remaining -= due_time;
2917 }
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002918 return FAIL;
Bram Moolenaar975b5272016-03-15 23:10:59 +01002919#else
2920 return gui_mch_wait_for_chars(wtime);
2921#endif
2922}
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924/*
2925 * The main GUI input routine. Waits for a character from the keyboard.
2926 * wtime == -1 Wait forever.
2927 * wtime == 0 Don't wait.
2928 * wtime > 0 Wait wtime milliseconds for a character.
2929 * Returns OK if a character was found to be available within the given time,
2930 * or FAIL otherwise.
2931 */
2932 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002933gui_wait_for_chars(long wtime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 int retval;
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002936 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002938#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 /*
2940 * If we're going to wait a bit, update the menus and mouse shape for the
2941 * current State.
2942 */
2943 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 gui_update_menus(0);
2945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 gui_mch_update();
2948 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
2953 /* Before waiting, flush any output to the screen. */
2954 gui_mch_flush();
2955
2956 if (wtime > 0)
2957 {
2958 /* Blink when waiting for a character. Probably only does something
2959 * for showmatch() */
2960 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002961 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 return retval;
2964 }
2965
2966 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002967 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 */
2969 gui_mch_start_blink();
2970
Bram Moolenaar3918c952005-03-15 22:34:55 +00002971 retval = FAIL;
2972 /*
2973 * We may want to trigger the CursorHold event. First wait for
2974 * 'updatetime' and if nothing is typed within that time put the
2975 * K_CURSORHOLD key in the input buffer.
2976 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002977 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002978 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002980 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002982 char_u buf[3];
2983
2984 /* Put K_CURSORHOLD in the input buffer. */
2985 buf[0] = CSI;
2986 buf[1] = KS_EXTRA;
2987 buf[2] = (int)KE_CURSORHOLD;
2988 add_to_input_buf(buf, 3);
2989
2990 retval = OK;
2991 }
2992#endif
2993
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002994 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002995 {
2996 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002997 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002998 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 gui_mch_stop_blink();
3002 return retval;
3003}
3004
3005/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003006 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 */
3008 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003009fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 p[0] = (char_u)(col / 128 + ' ' + 1);
3012 p[1] = (char_u)(col % 128 + ' ' + 1);
3013 p[2] = (char_u)(row / 128 + ' ' + 1);
3014 p[3] = (char_u)(row % 128 + ' ' + 1);
3015}
3016
3017/*
3018 * Generic mouse support function. Add a mouse event to the input buffer with
3019 * the given properties.
3020 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3021 * MOUSE_X1, MOUSE_X2
3022 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003023 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3024 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 * x, y --- Coordinates of mouse in pixels.
3026 * repeated_click --- TRUE if this click comes only a short time after a
3027 * previous click.
3028 * modifiers --- Bit field which may be any of the following modifiers
3029 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3030 * This function will ignore drag events where the mouse has not moved to a new
3031 * character.
3032 */
3033 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003034gui_send_mouse_event(
3035 int button,
3036 int x,
3037 int y,
3038 int repeated_click,
3039 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 static int prev_row = 0, prev_col = 0;
3042 static int prev_button = -1;
3043 static int num_clicks = 1;
3044 char_u string[10];
3045 enum key_extra button_char;
3046 int row, col;
3047#ifdef FEAT_CLIPBOARD
3048 int checkfor;
3049 int did_clip = FALSE;
3050#endif
3051
3052 /*
3053 * Scrolling may happen at any time, also while a selection is present.
3054 */
3055 switch (button)
3056 {
3057 case MOUSE_X1:
3058 button_char = KE_X1MOUSE;
3059 goto button_set;
3060 case MOUSE_X2:
3061 button_char = KE_X2MOUSE;
3062 goto button_set;
3063 case MOUSE_4:
3064 button_char = KE_MOUSEDOWN;
3065 goto button_set;
3066 case MOUSE_5:
3067 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003068 goto button_set;
3069 case MOUSE_6:
3070 button_char = KE_MOUSELEFT;
3071 goto button_set;
3072 case MOUSE_7:
3073 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074button_set:
3075 {
3076 /* Don't put events in the input queue now. */
3077 if (hold_gui_events)
3078 return;
3079
3080 string[3] = CSI;
3081 string[4] = KS_EXTRA;
3082 string[5] = (int)button_char;
3083
3084 /* Pass the pointer coordinates of the scroll event so that we
3085 * know which window to scroll. */
3086 row = gui_xy2colrow(x, y, &col);
3087 string[6] = (char_u)(col / 128 + ' ' + 1);
3088 string[7] = (char_u)(col % 128 + ' ' + 1);
3089 string[8] = (char_u)(row / 128 + ' ' + 1);
3090 string[9] = (char_u)(row % 128 + ' ' + 1);
3091
3092 if (modifiers == 0)
3093 add_to_input_buf(string + 3, 7);
3094 else
3095 {
3096 string[0] = CSI;
3097 string[1] = KS_MODIFIER;
3098 string[2] = 0;
3099 if (modifiers & MOUSE_SHIFT)
3100 string[2] |= MOD_MASK_SHIFT;
3101 if (modifiers & MOUSE_CTRL)
3102 string[2] |= MOD_MASK_CTRL;
3103 if (modifiers & MOUSE_ALT)
3104 string[2] |= MOD_MASK_ALT;
3105 add_to_input_buf(string, 10);
3106 }
3107 return;
3108 }
3109 }
3110
3111#ifdef FEAT_CLIPBOARD
3112 /* If a clipboard selection is in progress, handle it */
3113 if (clip_star.state == SELECT_IN_PROGRESS)
3114 {
3115 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3116 return;
3117 }
3118
3119 /* Determine which mouse settings to look for based on the current mode */
3120 switch (get_real_state())
3121 {
3122 case NORMAL_BUSY:
3123 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003124# ifdef FEAT_TERMINAL
3125 case TERMINAL:
3126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 case NORMAL: checkfor = MOUSE_NORMAL; break;
3128 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003129 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 case REPLACE:
3131 case REPLACE+LANGMAP:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003132# ifdef FEAT_VREPLACE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 case VREPLACE:
3134 case VREPLACE+LANGMAP:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 case INSERT:
3137 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3138 case ASKMORE:
3139 case HITRETURN: /* At the more- and hit-enter prompt pass the
3140 mouse event for a click on or below the
3141 message line. */
3142 if (Y_2_ROW(y) >= msg_row)
3143 checkfor = MOUSE_NORMAL;
3144 else
3145 checkfor = MOUSE_RETURN;
3146 break;
3147
3148 /*
3149 * On the command line, use the clipboard selection on all lines
3150 * but the command line. But not when pasting.
3151 */
3152 case CMDLINE:
3153 case CMDLINE+LANGMAP:
3154 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3155 checkfor = MOUSE_NONE;
3156 else
3157 checkfor = MOUSE_COMMAND;
3158 break;
3159
3160 default:
3161 checkfor = MOUSE_NONE;
3162 break;
3163 };
3164
3165 /*
3166 * Allow clipboard selection of text on the command line in "normal"
3167 * modes. Don't do this when dragging the status line, or extending a
3168 * Visual selection.
3169 */
3170 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003171 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 && button != MOUSE_DRAG
3173# ifdef FEAT_MOUSESHAPE
3174 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176# endif
3177 )
3178 checkfor = MOUSE_NONE;
3179
3180 /*
3181 * Use modeless selection when holding CTRL and SHIFT pressed.
3182 */
3183 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3184 checkfor = MOUSE_NONEF;
3185
3186 /*
3187 * In Ex mode, always use modeless selection.
3188 */
3189 if (exmode_active)
3190 checkfor = MOUSE_NONE;
3191
3192 /*
3193 * If the mouse settings say to not use the mouse, use the modeless
3194 * selection. But if Visual is active, assume that only the Visual area
3195 * will be selected.
3196 * Exception: On the command line, both the selection is used and a mouse
3197 * key is send.
3198 */
3199 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /* Don't do modeless selection in Visual mode. */
3202 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3203 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /*
3206 * When 'mousemodel' is "popup", shift-left is translated to right.
3207 * But not when also using Ctrl.
3208 */
3209 if (mouse_model_popup() && button == MOUSE_LEFT
3210 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3211 {
3212 button = MOUSE_RIGHT;
3213 modifiers &= ~ MOUSE_SHIFT;
3214 }
3215
3216 /* If the selection is done, allow the right button to extend it.
3217 * If the selection is cleared, allow the right button to start it
3218 * from the cursor position. */
3219 if (button == MOUSE_RIGHT)
3220 {
3221 if (clip_star.state == SELECT_CLEARED)
3222 {
3223 if (State & CMDLINE)
3224 {
3225 col = msg_col;
3226 row = msg_row;
3227 }
3228 else
3229 {
3230 col = curwin->w_wcol;
3231 row = curwin->w_wrow + W_WINROW(curwin);
3232 }
3233 clip_start_selection(col, row, FALSE);
3234 }
3235 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3236 repeated_click);
3237 did_clip = TRUE;
3238 }
3239 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003240 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3243 did_clip = TRUE;
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 /* Always allow pasting */
3247 if (button != MOUSE_MIDDLE)
3248 {
3249 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3250 return;
3251 if (checkfor != MOUSE_COMMAND)
3252 button = MOUSE_LEFT;
3253 }
3254 repeated_click = FALSE;
3255 }
3256
3257 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003258 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#endif
3260
3261 /* Don't put events in the input queue now. */
3262 if (hold_gui_events)
3263 return;
3264
3265 row = gui_xy2colrow(x, y, &col);
3266
3267 /*
3268 * If we are dragging and the mouse hasn't moved far enough to be on a
3269 * different character, then don't send an event to vim.
3270 */
3271 if (button == MOUSE_DRAG)
3272 {
3273 if (row == prev_row && col == prev_col)
3274 return;
3275 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3276 if (y < 0)
3277 row = -1;
3278 }
3279
3280 /*
3281 * If topline has changed (window scrolled) since the last click, reset
3282 * repeated_click, because we don't want starting Visual mode when
3283 * clicking on a different character in the text.
3284 */
3285 if (curwin->w_topline != gui_prev_topline
3286#ifdef FEAT_DIFF
3287 || curwin->w_topfill != gui_prev_topfill
3288#endif
3289 )
3290 repeated_click = FALSE;
3291
3292 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3293 string[1] = KS_MOUSE;
3294 string[2] = KE_FILLER;
3295 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3296 {
3297 if (repeated_click)
3298 {
3299 /*
3300 * Handle multiple clicks. They only count if the mouse is still
3301 * pointing at the same character.
3302 */
3303 if (button != prev_button || row != prev_row || col != prev_col)
3304 num_clicks = 1;
3305 else if (++num_clicks > 4)
3306 num_clicks = 1;
3307 }
3308 else
3309 num_clicks = 1;
3310 prev_button = button;
3311 gui_prev_topline = curwin->w_topline;
3312#ifdef FEAT_DIFF
3313 gui_prev_topfill = curwin->w_topfill;
3314#endif
3315
3316 string[3] = (char_u)(button | 0x20);
3317 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3318 }
3319 else
3320 string[3] = (char_u)button;
3321
3322 string[3] |= modifiers;
3323 fill_mouse_coord(string + 4, col, row);
3324 add_to_input_buf(string, 8);
3325
3326 if (row < 0)
3327 prev_row = 0;
3328 else
3329 prev_row = row;
3330 prev_col = col;
3331
3332 /*
3333 * We need to make sure this is cleared since Athena doesn't tell us when
3334 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3335 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003336#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 gui.dragged_sb = SBAR_NONE;
3338#endif
3339}
3340
3341/*
3342 * Convert x and y coordinate to column and row in text window.
3343 * Corrects for multi-byte character.
3344 * returns column in "*colp" and row as return value;
3345 */
3346 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003347gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 int col = check_col(X_2_COL(x));
3350 int row = check_row(Y_2_ROW(y));
3351
3352#ifdef FEAT_MBYTE
3353 *colp = mb_fix_col(col, row);
3354#else
3355 *colp = col;
3356#endif
3357 return row;
3358}
3359
3360#if defined(FEAT_MENU) || defined(PROTO)
3361/*
3362 * Callback function for when a menu entry has been selected.
3363 */
3364 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003365gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003367 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 /* Don't put events in the input queue now. */
3370 if (hold_gui_events)
3371 return;
3372
3373 bytes[0] = CSI;
3374 bytes[1] = KS_MENU;
3375 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003376 add_to_input_buf(bytes, 3);
3377 add_long_to_buf((long_u)menu, bytes);
3378 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380#endif
3381
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003382static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384/*
3385 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003386 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 * in p_go.
3388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003390gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392#ifdef FEAT_MENU
3393 static int prev_menu_is_active = -1;
3394#endif
3395#ifdef FEAT_TOOLBAR
3396 static int prev_toolbar = -1;
3397 int using_toolbar = FALSE;
3398#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003399#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003400 int using_tabline;
3401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#ifdef FEAT_FOOTER
3403 static int prev_footer = -1;
3404 int using_footer = FALSE;
3405#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 static int prev_tearoff = -1;
3408 int using_tearoff = FALSE;
3409#endif
3410
3411 char_u *p;
3412 int i;
3413#ifdef FEAT_MENU
3414 int grey_old, grey_new;
3415 char_u *temp;
3416#endif
3417 win_T *wp;
3418 int need_set_size;
3419 int fix_size;
3420
3421#ifdef FEAT_MENU
3422 if (oldval != NULL && gui.in_use)
3423 {
3424 /*
3425 * Check if the menu's go from grey to non-grey or vise versa.
3426 */
3427 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3428 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3429 if (grey_old != grey_new)
3430 {
3431 temp = p_go;
3432 p_go = oldval;
3433 gui_update_menus(MENU_ALL_MODES);
3434 p_go = temp;
3435 }
3436 }
3437 gui.menu_is_active = FALSE;
3438#endif
3439
3440 for (i = 0; i < 3; i++)
3441 gui.which_scrollbars[i] = FALSE;
3442 for (p = p_go; *p; p++)
3443 switch (*p)
3444 {
3445 case GO_LEFT:
3446 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3447 break;
3448 case GO_RIGHT:
3449 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 case GO_VLEFT:
3452 if (win_hasvertsplit())
3453 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3454 break;
3455 case GO_VRIGHT:
3456 if (win_hasvertsplit())
3457 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 case GO_BOT:
3460 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3461 break;
3462#ifdef FEAT_MENU
3463 case GO_MENUS:
3464 gui.menu_is_active = TRUE;
3465 break;
3466#endif
3467 case GO_GREY:
3468 /* make menu's have grey items, ignored here */
3469 break;
3470#ifdef FEAT_TOOLBAR
3471 case GO_TOOLBAR:
3472 using_toolbar = TRUE;
3473 break;
3474#endif
3475#ifdef FEAT_FOOTER
3476 case GO_FOOTER:
3477 using_footer = TRUE;
3478 break;
3479#endif
3480 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 using_tearoff = TRUE;
3483#endif
3484 break;
3485 default:
3486 /* Ignore options that are not supported */
3487 break;
3488 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (gui.in_use)
3491 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003492 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003494
3495#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003496 /* Update the GUI tab line, it may appear or disappear. This may
3497 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003498 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003499 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003500 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003501 /* We don't want a resize event change "Rows" here, save and
3502 * restore it. Resizing is handled below. */
3503 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003504 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003505 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003506 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003507 if (using_tabline)
3508 fix_size = TRUE;
3509 if (!gui_use_tabline())
3510 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3511 }
3512#endif
3513
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 for (i = 0; i < 3; i++)
3515 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003516 /* The scrollbar needs to be updated when it is shown/unshown and
3517 * when switching tab pages. But the size only changes when it's
3518 * shown/unshown. Thus we need two places to remember whether a
3519 * scrollbar is there or not. */
3520 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003521 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003522 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 if (i == SBAR_BOTTOM)
3525 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3526 gui.which_scrollbars[i]);
3527 else
3528 {
3529 FOR_ALL_WINDOWS(wp)
3530 {
3531 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3532 }
3533 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003534 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3535 {
3536 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003537 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003538 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003539 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003540 if (gui.which_scrollbars[i])
3541 fix_size = TRUE;
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003544 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003545 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548#ifdef FEAT_MENU
3549 if (gui.menu_is_active != prev_menu_is_active)
3550 {
3551 /* We don't want a resize event change "Rows" here, save and
3552 * restore it. Resizing is handled below. */
3553 i = Rows;
3554 gui_mch_enable_menu(gui.menu_is_active);
3555 Rows = i;
3556 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003557 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (gui.menu_is_active)
3559 fix_size = TRUE;
3560 }
3561#endif
3562
3563#ifdef FEAT_TOOLBAR
3564 if (using_toolbar != prev_toolbar)
3565 {
3566 gui_mch_show_toolbar(using_toolbar);
3567 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003568 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (using_toolbar)
3570 fix_size = TRUE;
3571 }
3572#endif
3573#ifdef FEAT_FOOTER
3574 if (using_footer != prev_footer)
3575 {
3576 gui_mch_enable_footer(using_footer);
3577 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003578 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (using_footer)
3580 fix_size = TRUE;
3581 }
3582#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003583#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 if (using_tearoff != prev_tearoff)
3585 {
3586 gui_mch_toggle_tearoffs(using_tearoff);
3587 prev_tearoff = using_tearoff;
3588 }
3589#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003590 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003591 {
3592#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003593 long prev_Columns = Columns;
3594 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /* Adjust the size of the window to make the text area keep the
3597 * same size and to avoid that part of our window is off-screen
3598 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003599 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003600
3601#ifdef FEAT_GUI_GTK
3602 /* GTK has the annoying habit of sending us resize events when
3603 * changing the window size ourselves. This mostly happens when
3604 * waiting for a character to arrive, quite unpredictably, and may
3605 * change Columns and Rows when we don't want it. Wait for a
3606 * character here to avoid this effect.
3607 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003608 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003609 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003610 * Don't change Rows when adding menu/toolbar/tabline.
3611 * Don't change Columns when adding vertical toolbar. */
3612 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003613 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003614 if ((need_set_size & RESIZE_VERT) == 0)
3615 Rows = prev_Rows;
3616 if ((need_set_size & RESIZE_HOR) == 0)
3617 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003618#endif
3619 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003620 /* When the console tabline appears or disappears the window positions
3621 * change. */
3622 if (firstwin->w_winrow != tabline_height())
3623 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625}
3626
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003627#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3628/*
3629 * Return TRUE if the GUI is taking care of the tabline.
3630 * It may still be hidden if 'showtabline' is zero.
3631 */
3632 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003633gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003634{
3635 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3636}
3637
3638/*
3639 * Return TRUE if the GUI is showing the tabline.
3640 * This uses 'showtabline'.
3641 */
3642 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003643gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003644{
3645 if (!gui_use_tabline()
3646 || p_stal == 0
3647 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3648 return FALSE;
3649 return TRUE;
3650}
3651
3652/*
3653 * Update the tabline.
3654 * This may display/undisplay the tabline and update the labels.
3655 */
3656 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003657gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003658{
3659 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003660 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003661
3662 if (!gui.starting && starting == 0)
3663 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003664 /* Updating the tabline uses direct GUI commands, flush
3665 * outstanding instructions first. (esp. clear screen) */
3666 out_flush();
3667 gui_mch_flush();
3668
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003669 if (!showit != !shown)
3670 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003671 if (showit != 0)
3672 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003673
3674 /* When the tabs change from hidden to shown or from shown to
3675 * hidden the size of the text area should remain the same. */
3676 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003677 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678 }
3679}
3680
3681/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003682 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003683 */
3684 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003685get_tabline_label(
3686 tabpage_T *tp,
3687 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688{
3689 int modified = FALSE;
3690 char_u buf[40];
3691 int wincount;
3692 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003693 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003694
Bram Moolenaar57657d82006-04-21 22:12:41 +00003695 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003696 opt = (tooltip ? &p_gtt : &p_gtl);
3697 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003698 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003699 int use_sandbox = FALSE;
3700 int save_called_emsg = called_emsg;
3701 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003702 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003703 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3704 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003705
3706 called_emsg = FALSE;
3707
3708 printer_page_num = tabpage_index(tp);
3709# ifdef FEAT_EVAL
3710 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003711 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003712# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003713 /* It's almost as going to the tabpage, but without autocommands. */
3714 curtab->tp_firstwin = firstwin;
3715 curtab->tp_lastwin = lastwin;
3716 curtab->tp_curwin = curwin;
3717 save_curtab = curtab;
3718 curtab = tp;
3719 topframe = curtab->tp_topframe;
3720 firstwin = curtab->tp_firstwin;
3721 lastwin = curtab->tp_lastwin;
3722 curwin = curtab->tp_curwin;
3723 curbuf = curwin->w_buffer;
3724
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003725 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003726 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003727 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003728 STRCPY(NameBuff, res);
3729
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003730 /* Back to the original curtab. */
3731 curtab = save_curtab;
3732 topframe = curtab->tp_topframe;
3733 firstwin = curtab->tp_firstwin;
3734 lastwin = curtab->tp_lastwin;
3735 curwin = curtab->tp_curwin;
3736 curbuf = curwin->w_buffer;
3737
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003738 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003739 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003740 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003741 called_emsg |= save_called_emsg;
3742 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003743
3744 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3745 * use a default label. */
3746 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003747 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003748 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003749 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003750 if (!tooltip)
3751 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003752
3753 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3754 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3755 if (bufIsChanged(wp->w_buffer))
3756 modified = TRUE;
3757 if (modified || wincount > 1)
3758 {
3759 if (wincount > 1)
3760 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3761 else
3762 buf[0] = NUL;
3763 if (modified)
3764 STRCAT(buf, "+");
3765 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003766 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003767 mch_memmove(NameBuff, buf, STRLEN(buf));
3768 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003769 }
3770}
3771
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003772/*
3773 * Send the event for clicking to select tab page "nr".
3774 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003775 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003776 */
3777 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003778send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003779{
3780 char_u string[3];
3781
3782 if (nr == tabpage_index(curtab))
3783 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003784
3785 /* Don't put events in the input queue now. */
3786 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003787# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003788 || cmdwin_type != 0
3789# endif
3790 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003791 {
3792 /* Set it back to the current tab page. */
3793 gui_mch_set_curtab(tabpage_index(curtab));
3794 return FALSE;
3795 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003796
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003797 string[0] = CSI;
3798 string[1] = KS_TABLINE;
3799 string[2] = KE_FILLER;
3800 add_to_input_buf(string, 3);
3801 string[0] = nr;
3802 add_to_input_buf_csi(string, 1);
3803 return TRUE;
3804}
3805
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003806/*
3807 * Send a tabline menu event
3808 */
3809 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003810send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003811{
3812 char_u string[3];
3813
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003814 /* Don't put events in the input queue now. */
3815 if (hold_gui_events)
3816 return;
3817
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003818 string[0] = CSI;
3819 string[1] = KS_TABMENU;
3820 string[2] = KE_FILLER;
3821 add_to_input_buf(string, 3);
3822 string[0] = tabidx;
3823 string[1] = (char_u)(long)event;
3824 add_to_input_buf_csi(string, 2);
3825}
3826
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829/*
3830 * Scrollbar stuff:
3831 */
3832
Bram Moolenaare45828b2006-02-15 22:12:56 +00003833/*
3834 * Remove all scrollbars. Used before switching to another tab page.
3835 */
3836 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003837gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003838{
3839 int i;
3840 win_T *wp;
3841
3842 for (i = 0; i < 3; i++)
3843 {
3844 if (i == SBAR_BOTTOM)
3845 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3846 else
3847 {
3848 FOR_ALL_WINDOWS(wp)
3849 {
3850 gui_do_scrollbar(wp, i, FALSE);
3851 }
3852 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003853 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003854 }
3855}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003858gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 static int sbar_ident = 0;
3861
3862 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3863 sb->wp = wp;
3864 sb->type = type;
3865 sb->value = 0;
3866#ifdef FEAT_GUI_ATHENA
3867 sb->pixval = 0;
3868#endif
3869 sb->size = 1;
3870 sb->max = 1;
3871 sb->top = 0;
3872 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 sb->status_height = 0;
3875 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3876}
3877
3878/*
3879 * Find the scrollbar with the given index.
3880 */
3881 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003882gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
3884 win_T *wp;
3885
3886 if (gui.bottom_sbar.ident == ident)
3887 return &gui.bottom_sbar;
3888 FOR_ALL_WINDOWS(wp)
3889 {
3890 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3891 return &wp->w_scrollbars[SBAR_LEFT];
3892 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3893 return &wp->w_scrollbars[SBAR_RIGHT];
3894 }
3895 return NULL;
3896}
3897
3898/*
3899 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3900 *
3901 * For Win32, Macintosh and GTK+ 2:
3902 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3903 * you stop dragging the scrollbar. We get here each time the scrollbar is
3904 * dragged another pixel, but as far as the rest of vim goes, it thinks
3905 * we're just hanging in the call to DispatchMessage() in
3906 * process_message(). The DispatchMessage() call that hangs was passed a
3907 * mouse button click event in the scrollbar window. -- webb.
3908 *
3909 * Solution: Do the scrolling right here. But only when allowed.
3910 * Ignore the scrollbars while executing an external command or when there
3911 * are still characters to be processed.
3912 */
3913 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003914gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 int sb_num;
3918#ifdef USE_ON_FLY_SCROLL
3919 colnr_T old_leftcol = curwin->w_leftcol;
3920# ifdef FEAT_SCROLLBIND
3921 linenr_T old_topline = curwin->w_topline;
3922# endif
3923# ifdef FEAT_DIFF
3924 int old_topfill = curwin->w_topfill;
3925# endif
3926#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003927 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int byte_count;
3929#endif
3930
3931 if (sb == NULL)
3932 return;
3933
3934 /* Don't put events in the input queue now. */
3935 if (hold_gui_events)
3936 return;
3937
3938#ifdef FEAT_CMDWIN
3939 if (cmdwin_type != 0 && sb->wp != curwin)
3940 return;
3941#endif
3942
3943 if (still_dragging)
3944 {
3945 if (sb->wp == NULL)
3946 gui.dragged_sb = SBAR_BOTTOM;
3947 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3948 gui.dragged_sb = SBAR_LEFT;
3949 else
3950 gui.dragged_sb = SBAR_RIGHT;
3951 gui.dragged_wp = sb->wp;
3952 }
3953 else
3954 {
3955 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003956#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003958 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 gui.dragged_wp = NULL;
3960#endif
3961 }
3962
3963 /* Vertical sbar info is kept in the first sbar (the left one) */
3964 if (sb->wp != NULL)
3965 sb = &sb->wp->w_scrollbars[0];
3966
3967 /*
3968 * Check validity of value
3969 */
3970 if (value < 0)
3971 value = 0;
3972#ifdef SCROLL_PAST_END
3973 else if (value > sb->max)
3974 value = sb->max;
3975#else
3976 if (value > sb->max - sb->size + 1)
3977 value = sb->max - sb->size + 1;
3978#endif
3979
3980 sb->value = value;
3981
3982#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003983 /* When not allowed to do the scrolling right now, return.
3984 * This also checked input_available(), but that causes the first click in
3985 * a scrollbar to be ignored when Vim doesn't have focus. */
3986 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return;
3988#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003989#ifdef FEAT_INS_EXPAND
3990 /* Disallow scrolling the current window when the completion popup menu is
3991 * visible. */
3992 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3993 return;
3994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996#ifdef FEAT_RIGHTLEFT
3997 if (sb->wp == NULL && curwin->w_p_rl)
3998 {
3999 value = sb->max + 1 - sb->size - value;
4000 if (value < 0)
4001 value = 0;
4002 }
4003#endif
4004
4005 if (sb->wp != NULL) /* vertical scrollbar */
4006 {
4007 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4009 sb_num++;
4010 if (wp == NULL)
4011 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013#ifdef USE_ON_FLY_SCROLL
4014 current_scrollbar = sb_num;
4015 scrollbar_value = value;
4016 if (State & NORMAL)
4017 {
4018 gui_do_scroll();
4019 setcursor();
4020 }
4021 else if (State & INSERT)
4022 {
4023 ins_scroll();
4024 setcursor();
4025 }
4026 else if (State & CMDLINE)
4027 {
4028 if (msg_scrolled == 0)
4029 {
4030 gui_do_scroll();
4031 redrawcmdline();
4032 }
4033 }
4034# ifdef FEAT_FOLDING
4035 /* Value may have been changed for closed fold. */
4036 sb->value = sb->wp->w_topline - 1;
4037# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004038
4039 /* When dragging one scrollbar and there is another one at the other
4040 * side move the thumb of that one too. */
4041 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4042 gui_mch_set_scrollbar_thumb(
4043 &sb->wp->w_scrollbars[
4044 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4045 ? SBAR_LEFT : SBAR_RIGHT],
4046 sb->value, sb->size, sb->max);
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048#else
4049 bytes[0] = CSI;
4050 bytes[1] = KS_VER_SCROLLBAR;
4051 bytes[2] = KE_FILLER;
4052 bytes[3] = (char_u)sb_num;
4053 byte_count = 4;
4054#endif
4055 }
4056 else
4057 {
4058#ifdef USE_ON_FLY_SCROLL
4059 scrollbar_value = value;
4060
4061 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004062 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else if (State & INSERT)
4064 ins_horscroll();
4065 else if (State & CMDLINE)
4066 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004067 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004069 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 redrawcmdline();
4071 }
4072 }
4073 if (old_leftcol != curwin->w_leftcol)
4074 {
4075 updateWindow(curwin); /* update window, status and cmdline */
4076 setcursor();
4077 }
4078#else
4079 bytes[0] = CSI;
4080 bytes[1] = KS_HOR_SCROLLBAR;
4081 bytes[2] = KE_FILLER;
4082 byte_count = 3;
4083#endif
4084 }
4085
4086#ifdef USE_ON_FLY_SCROLL
4087# ifdef FEAT_SCROLLBIND
4088 /*
4089 * synchronize other windows, as necessary according to 'scrollbind'
4090 */
4091 if (curwin->w_p_scb
4092 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4093 || (sb->wp == curwin && (curwin->w_topline != old_topline
4094# ifdef FEAT_DIFF
4095 || curwin->w_topfill != old_topfill
4096# endif
4097 ))))
4098 {
4099 do_check_scrollbind(TRUE);
4100 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004101 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (wp->w_redr_type > 0)
4103 updateWindow(wp);
4104 setcursor();
4105 }
4106# endif
4107 out_flush();
4108 gui_update_cursor(FALSE, TRUE);
4109#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004110 add_to_input_buf(bytes, byte_count);
4111 add_long_to_buf((long_u)value, bytes);
4112 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#endif
4114}
4115
4116/*
4117 * Scrollbar stuff:
4118 */
4119
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004120/*
4121 * Called when something in the window layout has changed.
4122 */
4123 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004124gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004125{
4126 if (gui.in_use && starting == 0)
4127 {
4128 out_flush();
4129 gui_init_which_components(NULL);
4130 gui_update_scrollbars(TRUE);
4131 }
4132 need_mouse_correct = TRUE;
4133}
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004136gui_update_scrollbars(
4137 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 win_T *wp;
4140 scrollbar_T *sb;
4141 long val, size, max; /* need 32 bits here */
4142 int which_sb;
4143 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 /* Update the horizontal scrollbar */
4147 gui_update_horiz_scrollbar(force);
4148
4149#ifndef WIN3264
4150 /* Return straight away if there is neither a left nor right scrollbar.
4151 * On MS-Windows this is required anyway for scrollwheel messages. */
4152 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4153 return;
4154#endif
4155
4156 /*
4157 * Don't want to update a scrollbar while we're dragging it. But if we
4158 * have both a left and right scrollbar, and we drag one of them, we still
4159 * need to update the other one.
4160 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004161 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4162 && gui.which_scrollbars[SBAR_LEFT]
4163 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 /*
4166 * If we have two scrollbars and one of them is being dragged, just
4167 * copy the scrollbar position from the dragged one to the other one.
4168 */
4169 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4170 if (gui.dragged_wp != NULL)
4171 gui_mch_set_scrollbar_thumb(
4172 &gui.dragged_wp->w_scrollbars[which_sb],
4173 gui.dragged_wp->w_scrollbars[0].value,
4174 gui.dragged_wp->w_scrollbars[0].size,
4175 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177
4178 /* avoid that moving components around generates events */
4179 ++hold_gui_events;
4180
Bram Moolenaar45a24952016-07-24 22:25:15 +02004181 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 if (wp->w_buffer == NULL) /* just in case */
4184 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185 /* Skip a scrollbar that is being dragged. */
4186 if (!force && (gui.dragged_sb == SBAR_LEFT
4187 || gui.dragged_sb == SBAR_RIGHT)
4188 && gui.dragged_wp == wp)
4189 continue;
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#ifdef SCROLL_PAST_END
4192 max = wp->w_buffer->b_ml.ml_line_count - 1;
4193#else
4194 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4195#endif
4196 if (max < 0) /* empty buffer */
4197 max = 0;
4198 val = wp->w_topline - 1;
4199 size = wp->w_height;
4200#ifdef SCROLL_PAST_END
4201 if (val > max) /* just in case */
4202 val = max;
4203#else
4204 if (size > max + 1) /* just in case */
4205 size = max + 1;
4206 if (val > max - size + 1)
4207 val = max - size + 1;
4208#endif
4209 if (val < 0) /* minimal value is 0 */
4210 val = 0;
4211
4212 /*
4213 * Scrollbar at index 0 (the left one) contains all the information.
4214 * It would be the same info for left and right so we just store it for
4215 * one of them.
4216 */
4217 sb = &wp->w_scrollbars[0];
4218
4219 /*
4220 * Note: no check for valid w_botline. If it's not valid the
4221 * scrollbars will be updated later anyway.
4222 */
4223 if (size < 1 || wp->w_botline - 2 > max)
4224 {
4225 /*
4226 * This can happen during changing files. Just don't update the
4227 * scrollbar for now.
4228 */
4229 sb->height = 0; /* Force update next time */
4230 if (gui.which_scrollbars[SBAR_LEFT])
4231 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4232 if (gui.which_scrollbars[SBAR_RIGHT])
4233 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4234 continue;
4235 }
4236 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 || sb->top != wp->w_winrow
4238 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004240 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
4242 /* Height, width or position of scrollbar has changed. For
4243 * vertical split: curwin changed. */
4244 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 sb->top = wp->w_winrow;
4246 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /* Calculate height and position in pixels */
4250 h = (sb->height + sb->status_height) * gui.char_height;
4251 y = sb->top * gui.char_height + gui.border_offset;
4252#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4253 if (gui.menu_is_active)
4254 y += gui.menu_height;
4255#endif
4256
4257#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4258 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4259# ifdef FEAT_GUI_ATHENA
4260 y += gui.toolbar_height;
4261# else
4262# ifdef FEAT_GUI_MSWIN
4263 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4264# endif
4265# endif
4266#endif
4267
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004268#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4269 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004270 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004271#endif
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 /* Height of top scrollbar includes width of top border */
4276 h += gui.border_offset;
4277 y -= gui.border_offset;
4278 }
4279 if (gui.which_scrollbars[SBAR_LEFT])
4280 {
4281 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4282 gui.left_sbar_x, y,
4283 gui.scrollbar_width, h);
4284 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4285 }
4286 if (gui.which_scrollbars[SBAR_RIGHT])
4287 {
4288 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4289 gui.right_sbar_x, y,
4290 gui.scrollbar_width, h);
4291 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4292 }
4293 }
4294
4295 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4296 * checking if the thumb moved at least a pixel. Only do this for
4297 * Athena, most other GUIs require the update anyway to make the
4298 * arrows work. */
4299#ifdef FEAT_GUI_ATHENA
4300 if (max == 0)
4301 y = 0;
4302 else
4303 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4304 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4305#else
4306 if (force || sb->value != val || sb->size != size || sb->max != max)
4307#endif
4308 {
4309 /* Thumb of scrollbar has moved */
4310 sb->value = val;
4311#ifdef FEAT_GUI_ATHENA
4312 sb->pixval = y;
4313#endif
4314 sb->size = size;
4315 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004316 if (gui.which_scrollbars[SBAR_LEFT]
4317 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4319 val, size, max);
4320 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004321 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4323 val, size, max);
4324 }
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 --hold_gui_events;
4328}
4329
4330/*
4331 * Enable or disable a scrollbar.
4332 * Check for scrollbars for vertically split windows which are not enabled
4333 * sometimes.
4334 */
4335 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004336gui_do_scrollbar(
4337 win_T *wp,
4338 int which, /* SBAR_LEFT or SBAR_RIGHT */
4339 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 int midcol = curwin->w_wincol + curwin->w_width / 2;
4342 int has_midcol = (wp->w_wincol <= midcol
4343 && wp->w_wincol + wp->w_width >= midcol);
4344
4345 /* Only enable scrollbars that contain the middle column of the current
4346 * window. */
4347 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4348 {
4349 /* Scrollbars only on one side. Don't enable scrollbars that don't
4350 * contain the middle column of the current window. */
4351 if (!has_midcol)
4352 enable = FALSE;
4353 }
4354 else
4355 {
4356 /* Scrollbars on both sides. Don't enable scrollbars that neither
4357 * contain the middle column of the current window nor are on the far
4358 * side. */
4359 if (midcol > Columns / 2)
4360 {
4361 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4362 enable = FALSE;
4363 }
4364 else
4365 {
4366 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4367 : !has_midcol)
4368 enable = FALSE;
4369 }
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4372}
4373
4374/*
4375 * Scroll a window according to the values set in the globals current_scrollbar
4376 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4377 * or FALSE otherwise.
4378 */
4379 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004380gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 win_T *wp, *save_wp;
4383 int i;
4384 long nlines;
4385 pos_T old_cursor;
4386 linenr_T old_topline;
4387#ifdef FEAT_DIFF
4388 int old_topfill;
4389#endif
4390
4391 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4392 if (wp == NULL)
4393 break;
4394 if (wp == NULL)
4395 /* Couldn't find window */
4396 return FALSE;
4397
4398 /*
4399 * Compute number of lines to scroll. If zero, nothing to do.
4400 */
4401 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4402 if (nlines == 0)
4403 return FALSE;
4404
4405 save_wp = curwin;
4406 old_topline = wp->w_topline;
4407#ifdef FEAT_DIFF
4408 old_topfill = wp->w_topfill;
4409#endif
4410 old_cursor = wp->w_cursor;
4411 curwin = wp;
4412 curbuf = wp->w_buffer;
4413 if (nlines < 0)
4414 scrolldown(-nlines, gui.dragged_wp == NULL);
4415 else
4416 scrollup(nlines, gui.dragged_wp == NULL);
4417 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4418 * the mouse-up event already, but we still want it to behave like when
4419 * dragging. But not the next click in an arrow. */
4420 if (gui.dragged_sb == SBAR_NONE)
4421 gui.dragged_wp = NULL;
4422
4423 if (old_topline != wp->w_topline
4424#ifdef FEAT_DIFF
4425 || old_topfill != wp->w_topfill
4426#endif
4427 )
4428 {
4429 if (p_so != 0)
4430 {
4431 cursor_correct(); /* fix window for 'so' */
4432 update_topline(); /* avoid up/down jump */
4433 }
4434 if (old_cursor.lnum != wp->w_cursor.lnum)
4435 coladvance(wp->w_curswant);
4436#ifdef FEAT_SCROLLBIND
4437 wp->w_scbind_pos = wp->w_topline;
4438#endif
4439 }
4440
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004441 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4442 validate_cursor();
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 curwin = save_wp;
4445 curbuf = save_wp->w_buffer;
4446
4447 /*
4448 * Don't call updateWindow() when nothing has changed (it will overwrite
4449 * the status line!).
4450 */
4451 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004452 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453#ifdef FEAT_DIFF
4454 || old_topfill != wp->w_topfill
4455#endif
4456 )
4457 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004458 int type = VALID;
4459
4460#ifdef FEAT_INS_EXPAND
4461 if (pum_visible())
4462 {
4463 type = NOT_VALID;
4464 wp->w_lines_valid = 0;
4465 }
4466#endif
4467 /* Don't set must_redraw here, it may cause the popup menu to
4468 * disappear when losing focus after a scrollbar drag. */
4469 if (wp->w_redr_type < type)
4470 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 updateWindow(wp); /* update window, status line, and cmdline */
4472 }
4473
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004474#ifdef FEAT_INS_EXPAND
4475 /* May need to redraw the popup menu. */
4476 if (pum_visible())
4477 pum_redraw();
4478#endif
4479
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004480 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481}
4482
4483
4484/*
4485 * Horizontal scrollbar stuff:
4486 */
4487
4488/*
4489 * Return length of line "lnum" for horizontal scrolling.
4490 */
4491 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004492scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 char_u *p;
4495 colnr_T col;
4496 int w;
4497
4498 p = ml_get(lnum);
4499 col = 0;
4500 if (*p != NUL)
4501 for (;;)
4502 {
4503 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004504 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (*p == NUL) /* don't count the last character */
4506 break;
4507 col += w;
4508 }
4509 return col;
4510}
4511
4512/* Remember which line is currently the longest, so that we don't have to
4513 * search for it when scrolling horizontally. */
4514static linenr_T longest_lnum = 0;
4515
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004516/*
4517 * Find longest visible line number. If this is not possible (or not desired,
4518 * by setting 'h' in "guioptions") then the current line number is returned.
4519 */
4520 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004521gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004522{
4523 linenr_T ret = 0;
4524
4525 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4526 * line numbers, topline and botline can be invalid when displaying is
4527 * postponed. */
4528 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4529 && curwin->w_topline <= curwin->w_cursor.lnum
4530 && curwin->w_botline > curwin->w_cursor.lnum
4531 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4532 {
4533 linenr_T lnum;
4534 colnr_T n;
4535 long max = 0;
4536
4537 /* Use maximum of all visible lines. Remember the lnum of the
4538 * longest line, closest to the cursor line. Used when scrolling
4539 * below. */
4540 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4541 {
4542 n = scroll_line_len(lnum);
4543 if (n > (colnr_T)max)
4544 {
4545 max = n;
4546 ret = lnum;
4547 }
4548 else if (n == (colnr_T)max
4549 && abs((int)(lnum - curwin->w_cursor.lnum))
4550 < abs((int)(ret - curwin->w_cursor.lnum)))
4551 ret = lnum;
4552 }
4553 }
4554 else
4555 /* Use cursor line only. */
4556 ret = curwin->w_cursor.lnum;
4557
4558 return ret;
4559}
4560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004562gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563{
4564 long value, size, max; /* need 32 bit ints here */
4565
4566 if (!gui.which_scrollbars[SBAR_BOTTOM])
4567 return;
4568
4569 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4570 return;
4571
4572 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4573 return;
4574
4575 /*
4576 * It is possible for the cursor to be invalid if we're in the middle of
4577 * something (like changing files). If so, don't do anything for now.
4578 */
4579 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4580 {
4581 gui.bottom_sbar.value = -1;
4582 return;
4583 }
4584
Bram Moolenaar02631462017-09-22 15:20:32 +02004585 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (curwin->w_p_wrap)
4587 {
4588 value = 0;
4589#ifdef SCROLL_PAST_END
4590 max = 0;
4591#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004592 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593#endif
4594 }
4595 else
4596 {
4597 value = curwin->w_leftcol;
4598
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004599 longest_lnum = gui_find_longest_lnum();
4600 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_VIRTUALEDIT
4603 if (virtual_active())
4604 {
4605 /* May move the cursor even further to the right. */
4606 if (curwin->w_virtcol >= (colnr_T)max)
4607 max = curwin->w_virtcol;
4608 }
4609#endif
4610
4611#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004612 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
4614 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004615 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 size -= curwin_col_off();
4617#ifndef SCROLL_PAST_END
4618 max -= curwin_col_off();
4619#endif
4620 }
4621
4622#ifndef SCROLL_PAST_END
4623 if (value > max - size + 1)
4624 value = max - size + 1; /* limit the value to allowable range */
4625#endif
4626
4627#ifdef FEAT_RIGHTLEFT
4628 if (curwin->w_p_rl)
4629 {
4630 value = max + 1 - size - value;
4631 if (value < 0)
4632 {
4633 size += value;
4634 value = 0;
4635 }
4636 }
4637#endif
4638 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4639 && max == gui.bottom_sbar.max)
4640 return;
4641
4642 gui.bottom_sbar.value = value;
4643 gui.bottom_sbar.size = size;
4644 gui.bottom_sbar.max = max;
4645 gui.prev_wrap = curwin->w_p_wrap;
4646
4647 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4648}
4649
4650/*
4651 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4652 */
4653 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004654gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 /* no wrapping, no scrolling */
4657 if (curwin->w_p_wrap)
4658 return FALSE;
4659
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004660 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return FALSE;
4662
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004663 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004666 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004668 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004669 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004671 if (compute_longest_lnum)
4672 {
4673 curwin->w_cursor.lnum = gui_find_longest_lnum();
4674 curwin->w_cursor.col = 0;
4675 }
4676 /* Do a sanity check on "longest_lnum", just in case. */
4677 else if (longest_lnum >= curwin->w_topline
4678 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
4680 curwin->w_cursor.lnum = longest_lnum;
4681 curwin->w_cursor.col = 0;
4682 }
4683 }
4684
4685 return leftcol_changed();
4686}
4687
4688/*
4689 * Check that none of the colors are the same as the background color
4690 */
4691 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004692gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4695 {
4696 gui_set_bg_color((char_u *)"White");
4697 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4698 gui_set_fg_color((char_u *)"Black");
4699 }
4700}
4701
Bram Moolenaar3918c952005-03-15 22:34:55 +00004702 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004703gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 gui.norm_pixel = gui_get_color(name);
4706 hl_set_fg_color_name(vim_strsave(name));
4707}
4708
Bram Moolenaar3918c952005-03-15 22:34:55 +00004709 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004710gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 gui.back_pixel = gui_get_color(name);
4713 hl_set_bg_color_name(vim_strsave(name));
4714}
4715
4716/*
4717 * Allocate a color by name.
4718 * Returns INVALCOLOR and gives an error message when failed.
4719 */
4720 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004721gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 guicolor_T t;
4724
4725 if (*name == NUL)
4726 return INVALCOLOR;
4727 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004730#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 && gui.in_use
4732#endif
4733 )
4734 EMSG2(_("E254: Cannot allocate color %s"), name);
4735 return t;
4736}
4737
4738/*
4739 * Return the grey value of a color (range 0-255).
4740 */
4741 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004742gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004744 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004746 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004747 + (((rgb >> 8) & 0xff) * 587)
4748 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749}
4750
4751#if defined(FEAT_GUI_X11) || defined(PROTO)
4752 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004753gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 win_T *wp;
4756
4757 /* Nothing to do if GUI hasn't started yet. */
4758 if (!gui.in_use)
4759 return;
4760
4761 FOR_ALL_WINDOWS(wp)
4762 {
4763 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4764 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4765 }
4766 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4767}
4768#endif
4769
4770/*
4771 * Call this when focus has changed.
4772 */
4773 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004774gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776/*
4777 * Skip this code to avoid drawing the cursor when debugging and switching
4778 * between the debugger window and gvim.
4779 */
4780#if 1
4781 gui.in_focus = in_focus;
4782 out_flush(); /* make sure output has been written */
4783 gui_update_cursor(TRUE, FALSE);
4784
4785# ifdef FEAT_XIM
4786 xim_set_focus(in_focus);
4787# endif
4788
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004789 /* Put events in the input queue only when allowed.
4790 * ui_focus_change() isn't called directly, because it invokes
4791 * autocommands and that must not happen asynchronously. */
4792 if (!hold_gui_events)
4793 {
4794 char_u bytes[3];
4795
4796 bytes[0] = CSI;
4797 bytes[1] = KS_EXTRA;
4798 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4799 add_to_input_buf(bytes, 3);
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801#endif
4802}
4803
4804/*
4805 * Called when the mouse moved (but not when dragging).
4806 */
4807 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004808gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004811 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaard3667a22006-03-16 21:35:52 +00004813 /* Ignore this while still starting up. */
4814 if (!gui.in_use || gui.starting)
4815 return;
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#ifdef FEAT_MOUSESHAPE
4818 /* Get window pointer, and update mouse shape as well. */
4819 wp = xy2win(x, y);
4820#endif
4821
4822 /* Only handle this when 'mousefocus' set and ... */
4823 if (p_mousef
4824 && !hold_gui_events /* not holding events */
4825 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4826 && State != HITRETURN /* but not hit-return prompt */
4827 && msg_scrolled == 0 /* no scrolled message */
4828 && !need_mouse_correct /* not moving the pointer */
4829 && gui.in_focus) /* gvim in focus */
4830 {
4831 /* Don't move the mouse when it's left or right of the Vim window */
4832 if (x < 0 || x > Columns * gui.char_width)
4833 return;
4834#ifndef FEAT_MOUSESHAPE
4835 wp = xy2win(x, y);
4836#endif
4837 if (wp == curwin || wp == NULL)
4838 return; /* still in the same old window, or none at all */
4839
Bram Moolenaar9c102382006-05-03 21:26:49 +00004840 /* Ignore position in the tab pages line. */
4841 if (Y_2_ROW(y) < tabline_height())
4842 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 /*
4845 * format a mouse click on status line input
4846 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004847 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4848 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 */
4850 if (finish_op)
4851 {
4852 /* abort the current operator first */
4853 st[0] = ESC;
4854 add_to_input_buf(st, 1);
4855 }
4856 st[0] = CSI;
4857 st[1] = KS_MOUSE;
4858 st[2] = KE_FILLER;
4859 st[3] = (char_u)MOUSE_LEFT;
4860 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004861 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 wp->w_height + W_WINROW(wp));
4863
4864 add_to_input_buf(st, 8);
4865 st[3] = (char_u)MOUSE_RELEASE;
4866 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867#ifdef FEAT_GUI_GTK
4868 /* Need to wake up the main loop */
4869 if (gtk_main_level() > 0)
4870 gtk_main_quit();
4871#endif
4872 }
4873}
4874
4875/*
4876 * Called when mouse should be moved to window with focus.
4877 */
4878 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004879gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 int x, y;
4882 win_T *wp = NULL;
4883
4884 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004885
4886 if (!(gui.in_use && p_mousef))
4887 return;
4888
4889 gui_mch_getmouse(&x, &y);
4890 /* Don't move the mouse when it's left or right of the Vim window */
4891 if (x < 0 || x > Columns * gui.char_width)
4892 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004893 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004894 wp = xy2win(x, y);
4895 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004897 validate_cline_row();
4898 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4899 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902}
4903
4904/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004905 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004908xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int row;
4911 int col;
4912 win_T *wp;
4913
4914 row = Y_2_ROW(y);
4915 col = X_2_COL(x);
4916 if (row < 0 || col < 0) /* before first window */
4917 return NULL;
4918 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004919 if (wp == NULL)
4920 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004921#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (State == HITRETURN || State == ASKMORE)
4923 {
4924 if (Y_2_ROW(y) >= msg_row)
4925 update_mouseshape(SHAPE_IDX_MOREL);
4926 else
4927 update_mouseshape(SHAPE_IDX_MORE);
4928 }
4929 else if (row > wp->w_height) /* below status line */
4930 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004931 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004932 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004934 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004935 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 update_mouseshape(SHAPE_IDX_STATUS);
4937 else
4938 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004940 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941}
4942
4943/*
4944 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4945 * File names may be given to redefine the args list.
4946 */
4947 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004948ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 char_u *arg = eap->arg;
4951
4952 /*
4953 * Check for "-f" argument: foreground, don't fork.
4954 * Also don't fork when started with "gvim -f".
4955 * Do fork when using "gui -b".
4956 */
4957 if (arg[0] == '-'
4958 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004959 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 gui.dofork = (arg[1] == 'b');
4962 eap->arg = skipwhite(eap->arg + 2);
4963 }
4964 if (!gui.in_use)
4965 {
4966 /* Clear the command. Needed for when forking+exiting, to avoid part
4967 * of the argument ending up after the shell prompt. */
4968 msg_clr_eos_force();
4969 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004970#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004971 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 if (!ends_excmd(*eap->arg))
4975 ex_next(eap);
4976}
4977
4978#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004979 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980/*
4981 * This is shared between Athena, Motif and GTK.
4982 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004983static void gfp_setname(char_u *fname, void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985/*
4986 * Callback function for do_in_runtimepath().
4987 */
4988 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004989gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004991 char_u *gfp_buffer = cookie;
4992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 if (STRLEN(fname) >= MAXPATHL)
4994 *gfp_buffer = NUL;
4995 else
4996 STRCPY(gfp_buffer, fname);
4997}
4998
4999/*
5000 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5001 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5002 */
5003 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005004gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006 if (STRLEN(name) > MAXPATHL - 14)
5007 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005008 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005009 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 return FAIL;
5012 return OK;
5013}
5014
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005015# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016/*
5017 * Given the name of the "icon=" argument, try finding the bitmap file for the
5018 * icon. If it is an absolute path name, use it as it is. Otherwise append
5019 * "ext" and search for it in 'runtimepath'.
5020 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5021 * contains "name".
5022 */
5023 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005024gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026 char_u buf[MAXPATHL + 1];
5027
5028 expand_env(name, buffer, MAXPATHL);
5029 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5030 STRCPY(buffer, buf);
5031}
5032# endif
5033#endif
5034
Bram Moolenaar9372a112005-12-06 19:59:18 +00005035#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005037display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 char_u *p;
5040
5041 if (isatty(2))
5042 fflush(stderr);
5043 else if (error_ga.ga_data != NULL)
5044 {
5045 /* avoid putting up a message box with blanks only */
5046 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5047 if (!isspace(*p))
5048 {
5049 /* Truncate a very long message, it will go off-screen. */
5050 if (STRLEN(p) > 2000)
5051 STRCPY(p + 2000 - 14, "...(truncated)");
5052 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005053 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055 }
5056 ga_clear(&error_ga);
5057 }
5058}
5059#endif
5060
5061#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5062/*
5063 * Return TRUE if still starting up and there is no place to enter text.
5064 * For GTK and X11 we check if stderr is not a tty, which means we were
5065 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5066 * allow typing on stdin.
5067 */
5068 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005069no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
5071 return ((!gui.in_use || gui.starting)
5072# ifndef NO_CONSOLE
5073 && !isatty(0) && !isatty(2)
5074# endif
5075 );
5076}
5077#endif
5078
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005079#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005080 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005081 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082/*
5083 * Update the current window and the screen.
5084 */
5085 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005086gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005088#ifdef FEAT_CONCEAL
5089 linenr_T conceal_old_cursor_line = 0;
5090 linenr_T conceal_new_cursor_line = 0;
5091 int conceal_update_lines = FALSE;
5092#endif
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 update_topline();
5095 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005096
5097#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005098 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005099 if (!finish_op && (
5100# ifdef FEAT_AUTOCMD
5101 has_cursormoved()
5102# endif
5103# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5104 ||
5105# endif
5106# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005107 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005108# endif
5109 )
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005110 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005111 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005112# ifdef FEAT_AUTOCMD
5113 if (has_cursormoved())
5114 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5115# endif
5116# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005117 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118 {
5119 conceal_old_cursor_line = last_cursormoved.lnum;
5120 conceal_new_cursor_line = curwin->w_cursor.lnum;
5121 conceal_update_lines = TRUE;
5122 }
5123# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005124 last_cursormoved = curwin->w_cursor;
5125 }
5126#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 update_screen(0); /* may need to update the screen */
5129 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005130# if defined(FEAT_CONCEAL)
5131 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005132 && (conceal_old_cursor_line != conceal_new_cursor_line
5133 || conceal_cursor_line(curwin)
5134 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005135 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005136 if (conceal_old_cursor_line != conceal_new_cursor_line)
5137 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005138 update_single_line(curwin, conceal_new_cursor_line);
5139 curwin->w_valid &= ~VALID_CROW;
5140 }
5141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 out_flush(); /* make sure output has been written */
5143 gui_update_cursor(TRUE, FALSE);
5144 gui_mch_flush();
5145}
5146#endif
5147
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005148#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005149static void concat_esc(garray_T *gap, char_u *text, int what);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
5151/*
5152 * Get the text to use in a find/replace dialog. Uses the last search pattern
5153 * if the argument is empty.
5154 * Returns an allocated string.
5155 */
5156 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005157get_find_dialog_text(
5158 char_u *arg,
5159 int *wwordp, /* return: TRUE if \< \> found */
5160 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 char_u *text;
5163
5164 if (*arg == NUL)
5165 text = last_search_pat();
5166 else
5167 text = arg;
5168 if (text != NULL)
5169 {
5170 text = vim_strsave(text);
5171 if (text != NULL)
5172 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005173 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 int i;
5175
5176 /* Remove "\V" */
5177 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5178 {
5179 mch_memmove(text, text + 2, (size_t)(len - 1));
5180 len -= 2;
5181 }
5182
5183 /* Recognize "\c" and "\C" and remove. */
5184 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5185 {
5186 *mcasep = (text[1] == 'C');
5187 mch_memmove(text, text + 2, (size_t)(len - 1));
5188 len -= 2;
5189 }
5190
5191 /* Recognize "\<text\>" and remove. */
5192 if (len >= 4
5193 && STRNCMP(text, "\\<", 2) == 0
5194 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5195 {
5196 *wwordp = TRUE;
5197 mch_memmove(text, text + 2, (size_t)(len - 4));
5198 text[len - 4] = NUL;
5199 }
5200
5201 /* Recognize "\/" or "\?" and remove. */
5202 for (i = 0; i + 1 < len; ++i)
5203 if (text[i] == '\\' && (text[i + 1] == '/'
5204 || text[i + 1] == '?'))
5205 {
5206 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5207 --len;
5208 }
5209 }
5210 }
5211 return text;
5212}
5213
5214/*
5215 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5216 */
5217 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005218concat_esc(garray_T *gap, char_u *text, int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219{
5220 while (*text != NUL)
5221 {
5222#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005223 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (l > 1)
5226 {
5227 while (--l >= 0)
5228 ga_append(gap, *text++);
5229 continue;
5230 }
5231#endif
5232 if (*text == what)
5233 ga_append(gap, '\\');
5234 ga_append(gap, *text);
5235 ++text;
5236 }
5237}
5238
5239/*
5240 * Handle the press of a button in the find-replace dialog.
5241 * Return TRUE when something was added to the input buffer.
5242 */
5243 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005244gui_do_findrepl(
5245 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5246 char_u *find_text,
5247 char_u *repl_text,
5248 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
5250 garray_T ga;
5251 int i;
5252 int type = (flags & FRD_TYPE_MASK);
5253 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005254 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005255 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005256 static int busy = FALSE;
5257
5258 /* When the screen is being updated we should not change buffers and
5259 * windows structures, it may cause freed memory to be used. Also don't
5260 * do this recursively (pressing "Find" quickly several times. */
5261 if (updating_screen || busy)
5262 return FALSE;
5263
5264 /* refuse replace when text cannot be changed */
5265 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5266 return FALSE;
5267
5268 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005271 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 ga_concat(&ga, (char_u *)"%s/");
5273
5274 ga_concat(&ga, (char_u *)"\\V");
5275 if (flags & FRD_MATCH_CASE)
5276 ga_concat(&ga, (char_u *)"\\C");
5277 else
5278 ga_concat(&ga, (char_u *)"\\c");
5279 if (flags & FRD_WHOLE_WORD)
5280 ga_concat(&ga, (char_u *)"\\<");
5281 if (type == FRD_REPLACEALL || down)
5282 concat_esc(&ga, find_text, '/'); /* escape slashes */
5283 else
5284 concat_esc(&ga, find_text, '?'); /* escape '?' */
5285 if (flags & FRD_WHOLE_WORD)
5286 ga_concat(&ga, (char_u *)"\\>");
5287
5288 if (type == FRD_REPLACEALL)
5289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005291 /* escape / and \ */
5292 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5293 if (p != NULL)
5294 ga_concat(&ga, p);
5295 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005297 }
5298 ga_append(&ga, NUL);
5299
5300 if (type == FRD_REPLACE)
5301 {
5302 /* Do the replacement when the text at the cursor matches. Thus no
5303 * replacement is done if the cursor was moved! */
5304 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5305 regmatch.rm_ic = 0;
5306 if (regmatch.regprog != NULL)
5307 {
5308 p = ml_get_cursor();
5309 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5310 && regmatch.startp[0] == p)
5311 {
5312 /* Clear the command line to remove any old "No match"
5313 * error. */
5314 msg_end_prompt();
5315
5316 if (u_save_cursor() == OK)
5317 {
5318 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005319 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005320
5321 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005322 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005323 ins_str(repl_text);
5324 }
5325 }
5326 else
5327 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005328 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005329 }
5330 }
5331
5332 if (type == FRD_REPLACEALL)
5333 {
5334 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005335 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 do_cmdline_cmd(ga.ga_data);
5337 }
5338 else
5339 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005340 int searchflags = SEARCH_MSG + SEARCH_MARK;
5341
5342 /* Search for the next match.
5343 * Don't skip text under cursor for single replace. */
5344 if (type == FRD_REPLACE)
5345 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 i = msg_scroll;
Bram Moolenaarcde88542015-08-11 19:14:00 +02005347 (void)do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005348 searchflags, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 msg_scroll = i; /* don't let an error message set msg_scroll */
5350 }
5351
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005352 /* Don't want to pass did_emsg to other code, it may cause disabling
5353 * syntax HL if we were busy redrawing. */
5354 did_emsg = save_did_emsg;
5355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (State & (NORMAL | INSERT))
5357 {
5358 gui_update_screen(); /* update the screen */
5359 msg_didout = 0; /* overwrite any message */
5360 need_wait_return = FALSE; /* don't wait for return */
5361 }
5362
5363 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005364 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 return (ga.ga_len > 0);
5366}
5367
5368#endif
5369
5370#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5371 || defined(FEAT_GUI_MSWIN) \
5372 || defined(FEAT_GUI_MAC) \
5373 || defined(PROTO)
5374
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005375static void gui_wingoto_xy(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377/*
5378 * Jump to the window at specified point (x, y).
5379 */
5380 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005381gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 int row = Y_2_ROW(y);
5384 int col = X_2_COL(x);
5385 win_T *wp;
5386
5387 if (row >= 0 && col >= 0)
5388 {
5389 wp = mouse_find_win(&row, &col);
5390 if (wp != NULL && wp != curwin)
5391 win_goto(wp);
5392 }
5393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395/*
5396 * Process file drop. Mouse cursor position, key modifiers, name of files
5397 * and count of files are given. Argument "fnames[count]" has full pathnames
5398 * of dropped files, they will be freed in this function, and caller can't use
5399 * fnames after call this function.
5400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005402gui_handle_drop(
5403 int x UNUSED,
5404 int y UNUSED,
5405 int_u modifiers,
5406 char_u **fnames,
5407 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 int i;
5410 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005411 static int entered = FALSE;
5412
5413 /*
5414 * This function is called by event handlers. Just in case we get a
5415 * second event before the first one is handled, ignore the second one.
5416 * Not sure if this can ever happen, just in case.
5417 */
5418 if (entered)
5419 return;
5420 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 /*
5423 * When the cursor is at the command line, add the file names to the
5424 * command line, don't edit the files.
5425 */
5426 if (State & CMDLINE)
5427 {
5428 shorten_filenames(fnames, count);
5429 for (i = 0; i < count; ++i)
5430 {
5431 if (fnames[i] != NULL)
5432 {
5433 if (i > 0)
5434 add_to_input_buf((char_u*)" ", 1);
5435
5436 /* We don't know what command is used thus we can't be sure
5437 * about which characters need to be escaped. Only escape the
5438 * most common ones. */
5439# ifdef BACKSLASH_IN_FILENAME
5440 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5441# else
5442 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5443# endif
5444 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005445 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 vim_free(p);
5447 vim_free(fnames[i]);
5448 }
5449 }
5450 vim_free(fnames);
5451 }
5452 else
5453 {
5454 /* Go to the window under mouse cursor, then shorten given "fnames" by
5455 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 shorten_filenames(fnames, count);
5458
5459 /* If Shift held down, remember the first item. */
5460 if ((modifiers & MOUSE_SHIFT) != 0)
5461 p = vim_strsave(fnames[0]);
5462 else
5463 p = NULL;
5464
5465 /* Handle the drop, :edit or :split to get to the file. This also
5466 * frees fnames[]. Skip this if there is only one item it's a
5467 * directory and Shift is held down. */
5468 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5469 && mch_isdir(fnames[0]))
5470 {
5471 vim_free(fnames[0]);
5472 vim_free(fnames);
5473 }
5474 else
5475 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5476
5477 /* If Shift held down, change to first file's directory. If the first
5478 * item is a directory, change to that directory (and let the explorer
5479 * plugin show the contents). */
5480 if (p != NULL)
5481 {
5482 if (mch_isdir(p))
5483 {
5484 if (mch_chdir((char *)p) == 0)
5485 shorten_fnames(TRUE);
5486 }
5487 else if (vim_chdirfile(p) == OK)
5488 shorten_fnames(TRUE);
5489 vim_free(p);
5490 }
5491
5492 /* Update the screen display */
5493 update_screen(NOT_VALID);
5494# ifdef FEAT_MENU
5495 gui_update_menus(0);
5496# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005497#ifdef FEAT_TITLE
5498 maketitle();
5499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 setcursor();
5501 out_flush();
5502 gui_update_cursor(FALSE, FALSE);
5503 gui_mch_flush();
5504 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005505
5506 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507}
5508#endif