blob: 3456f9246de43b6e58bac4969a8fe11c20baef10 [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
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100741#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000742 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100743 * 'ballooneval' is off. */
744 if (balloonEval != NULL)
745 vim_free(balloonEval);
746 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000747# ifdef FEAT_GUI_GTK
748 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
749 &general_beval_cb, NULL);
750# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000751# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000752 {
753 extern Widget textArea;
754 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000755 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000756 }
757# else
758# ifdef FEAT_GUI_W32
759 balloonEval = gui_mch_create_beval_area(NULL, NULL,
760 &general_beval_cb, NULL);
761# endif
762# endif
763# endif
764 if (!p_beval)
765 gui_mch_disable_beval_area(balloonEval);
766#endif
767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
769 if (!im_xim_isvalid_imactivate())
770 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
771#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000772 /* When 'cmdheight' was set during startup it may not have taken
773 * effect yet. */
774 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000775 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 return;
778 }
779
780error2:
781#ifdef FEAT_GUI_X11
782 /* undo gui_mch_init() */
783 gui_mch_uninit();
784#endif
785
786error:
787 gui.in_use = FALSE;
788 clip_init(FALSE);
789}
790
791
792 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100793gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /* don't free the fonts, it leads to a BUS error
796 * richard@whitequeen.com Jul 99 */
797 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 gui.in_use = FALSE;
799 gui_mch_exit(rc);
800}
801
Bram Moolenaar9372a112005-12-06 19:59:18 +0000802#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000804# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
806 * Called when the GUI shell is closed by the user. If there are no changed
807 * files Vim exits, otherwise there will be a dialog to ask the user what to
808 * do.
809 * When this function returns, Vim should NOT exit!
810 */
811 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100812gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 cmdmod_T save_cmdmod;
815
816 save_cmdmod = cmdmod;
817
818 /* Only exit when there are no changed files */
819 exiting = TRUE;
820# ifdef FEAT_BROWSE
821 cmdmod.browse = TRUE;
822# endif
823# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
824 cmdmod.confirm = TRUE;
825# endif
826 /* If there are changed buffers, present the user with a dialog if
827 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100828 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 getout(0);
830
831 exiting = FALSE;
832 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000833 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834}
835#endif
836
837/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200838 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * first font name that works is used. If none is found, use the default
840 * font.
841 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
842 * Return OK when able to set the font. When it failed FAIL is returned and
843 * the fonts are unchanged.
844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100846gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848#define FONTLEN 320
849 char_u font_name[FONTLEN];
850 int font_list_empty = FALSE;
851 int ret = FAIL;
852
853 if (!gui.in_use)
854 return FAIL;
855
856 font_name[0] = NUL;
857 if (*font_list == NUL)
858 font_list_empty = TRUE;
859 else
860 {
861#ifdef FEAT_XFONTSET
862 /* When using a fontset, the whole list of fonts is one name. */
863 if (fontset)
864 ret = gui_mch_init_font(font_list, TRUE);
865 else
866#endif
867 while (*font_list != NUL)
868 {
869 /* Isolate one comma separated font name. */
870 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
871
872 /* Careful!!! The Win32 version of gui_mch_init_font(), when
873 * called with "*" will change p_guifont to the selected font
874 * name, which frees the old value. This makes font_list
875 * invalid. Thus when OK is returned here, font_list must no
876 * longer be used! */
877 if (gui_mch_init_font(font_name, FALSE) == OK)
878 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200879#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 /* If it's a Unicode font, try setting 'guifontwide' to a
881 * similar double-width font. */
882 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
883 && strstr((char *)font_name, "10646") != NULL)
884 set_guifontwide(font_name);
885#endif
886 ret = OK;
887 break;
888 }
889 }
890 }
891
892 if (ret != OK
893 && STRCMP(font_list, "*") != 0
894 && (font_list_empty || gui.norm_font == NOFONT))
895 {
896 /*
897 * Couldn't load any font in 'font_list', keep the current font if
898 * there is one. If 'font_list' is empty, or if there is no current
899 * font, tell gui_mch_init_font() to try to find a font we can load.
900 */
901 ret = gui_mch_init_font(NULL, FALSE);
902 }
903
904 if (ret == OK)
905 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200906#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 /* Set normal font as current font */
908# ifdef FEAT_XFONTSET
909 if (gui.fontset != NOFONTSET)
910 gui_mch_set_fontset(gui.fontset);
911 else
912# endif
913 gui_mch_set_font(gui.norm_font);
914#endif
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100915 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917
918 return ret;
919}
920
921#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200922# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923/*
924 * Try setting 'guifontwide' to a font twice as wide as "name".
925 */
926 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100927set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 int i = 0;
930 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
931 char_u *wp = NULL;
932 char_u *p;
933 GuiFont font;
934
935 wp = wide_name;
936 for (p = name; *p != NUL; ++p)
937 {
938 *wp++ = *p;
939 if (*p == '-')
940 {
941 ++i;
942 if (i == 6) /* font type: change "--" to "-*-" */
943 {
944 if (p[1] == '-')
945 *wp++ = '*';
946 }
947 else if (i == 12) /* found the width */
948 {
949 ++p;
950 i = getdigits(&p);
951 if (i != 0)
952 {
953 /* Double the width specification. */
954 sprintf((char *)wp, "%d%s", i * 2, p);
955 font = gui_mch_get_font(wide_name, FALSE);
956 if (font != NOFONT)
957 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000958 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 gui.wide_font = font;
960 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000961 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963 }
964 break;
965 }
966 }
967 }
968}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200969# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971/*
972 * Get the font for 'guifontwide'.
973 * Return FAIL for an invalid font name.
974 */
975 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100976gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
978 GuiFont font = NOFONT;
979 char_u font_name[FONTLEN];
980 char_u *p;
981
982 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
983 return OK; /* Will give an error message later. */
984
985 if (p_guifontwide != NULL && *p_guifontwide != NUL)
986 {
987 for (p = p_guifontwide; *p != NUL; )
988 {
989 /* Isolate one comma separated font name. */
990 (void)copy_option_part(&p, font_name, FONTLEN, ",");
991 font = gui_mch_get_font(font_name, FALSE);
992 if (font != NOFONT)
993 break;
994 }
995 if (font == NOFONT)
996 return FAIL;
997 }
998
999 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001000# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1002 if (font != NOFONT && gui.norm_font != NOFONT
1003 && pango_font_description_equal(font, gui.norm_font))
1004 {
1005 gui.wide_font = NOFONT;
1006 gui_mch_free_font(font);
1007 }
1008 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001011# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001012 gui_mch_wide_font_changed();
Bram Moolenaardb250522013-06-17 22:43:25 +02001013# else
1014 /*
1015 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1016 * support those fonts for 'guifontwide'.
1017 */
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 return OK;
1020}
1021#endif
1022
1023 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001024gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026 gui.row = row;
1027 gui.col = col;
1028}
1029
1030/*
1031 * gui_check_pos - check if the cursor is on the screen.
1032 */
1033 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001034gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036 if (gui.row >= screen_Rows)
1037 gui.row = screen_Rows - 1;
1038 if (gui.col >= screen_Columns)
1039 gui.col = screen_Columns - 1;
1040 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1041 gui.cursor_is_valid = FALSE;
1042}
1043
1044/*
1045 * Redraw the cursor if necessary or when forced.
1046 * Careful: The contents of ScreenLines[] must match what is on the screen,
1047 * otherwise this goes wrong. May need to call out_flush() first.
1048 */
1049 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001050gui_update_cursor(
1051 int force, /* when TRUE, update even when not moved */
1052 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 int cur_width = 0;
1055 int cur_height = 0;
1056 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001057 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001059#ifdef FEAT_TERMINAL
1060 guicolor_T shape_fg = INVALCOLOR;
1061 guicolor_T shape_bg = INVALCOLOR;
1062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1064 int cattr; /* cursor attributes */
1065 int attr;
1066 attrentry_T *aep = NULL;
1067
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001068 /* Don't update the cursor when halfway busy scrolling or the screen size
1069 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1070 if (!can_update_cursor || screen_Columns != gui.num_cols
1071 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return;
1073
1074 gui_check_pos();
1075 if (!gui.cursor_is_valid || force
1076 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1077 {
1078 gui_undraw_cursor();
1079 if (gui.row < 0)
1080 return;
Bram Moolenaar819edbe2017-11-25 17:14:33 +01001081#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1083 im_set_position(gui.row, gui.col);
1084#endif
1085 gui.cursor_row = gui.row;
1086 gui.cursor_col = gui.col;
1087
1088 /* Only write to the screen after ScreenLines[] has been initialized */
1089 if (!screen_cleared || ScreenLines == NULL)
1090 return;
1091
1092 /* Clear the selection if we are about to write over it */
1093 if (clear_selection)
1094 clip_may_clear_selection(gui.row, gui.row);
1095 /* Check that the cursor is inside the shell (resizing may have made
1096 * it invalid) */
1097 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1098 return;
1099
1100 gui.cursor_is_valid = TRUE;
1101
1102 /*
1103 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001104 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001106#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001107 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001108 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001110#endif
1111 shape = &shape_table[get_shape_idx(FALSE)];
1112 if (State & LANGMAP)
1113 id = shape->id_lm;
1114 else
1115 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
1117 /* get the colors and attributes for the cursor. Default is inverted */
1118 cfg = INVALCOLOR;
1119 cbg = INVALCOLOR;
1120 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001121 gui_mch_set_blinking(shape->blinkwait,
1122 shape->blinkon,
1123 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001124 if (shape->blinkwait == 0 || shape->blinkon == 0
1125 || shape->blinkoff == 0)
Bram Moolenaar2f27aab2017-11-12 18:32:00 +01001126 gui_mch_stop_blink();
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001127#ifdef FEAT_TERMINAL
1128 if (shape_bg != INVALCOLOR)
1129 {
1130 cattr = 0;
1131 cfg = shape_fg;
1132 cbg = shape_bg;
1133 }
1134 else
1135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (id > 0)
1137 {
1138 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaarcb89c982017-12-17 21:54:55 +01001139#if defined(FEAT_XIM) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
1141 static int iid;
1142 guicolor_T fg, bg;
1143
Bram Moolenaarc236c162008-07-13 17:41:49 +00001144 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001145# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001146 preedit_get_status()
1147# else
1148 im_get_status()
1149# endif
1150 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 iid = syn_name2id((char_u *)"CursorIM");
1153 if (iid > 0)
1154 {
1155 syn_id2colors(iid, &fg, &bg);
1156 if (bg != INVALCOLOR)
1157 cbg = bg;
1158 if (fg != INVALCOLOR)
1159 cfg = fg;
1160 }
1161 }
1162 }
1163#endif
1164 }
1165
1166 /*
1167 * Get the attributes for the character under the cursor.
1168 * When no cursor color was given, use the character color.
1169 */
1170 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1171 if (attr > HL_ALL)
1172 aep = syn_gui_attr2entry(attr);
1173 if (aep != NULL)
1174 {
1175 attr = aep->ae_attr;
1176 if (cfg == INVALCOLOR)
1177 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1178 : aep->ae_u.gui.fg_color);
1179 if (cbg == INVALCOLOR)
1180 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1181 : aep->ae_u.gui.bg_color);
1182 }
1183 if (cfg == INVALCOLOR)
1184 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1185 if (cbg == INVALCOLOR)
1186 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1187
1188#ifdef FEAT_XIM
1189 if (aep != NULL)
1190 {
1191 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1192 : aep->ae_u.gui.bg_color);
1193 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1194 : aep->ae_u.gui.fg_color);
1195 if (xim_bg_color == INVALCOLOR)
1196 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1197 : gui.back_pixel;
1198 if (xim_fg_color == INVALCOLOR)
1199 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1200 : gui.norm_pixel;
1201 }
1202 else
1203 {
1204 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1205 : gui.back_pixel;
1206 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1207 : gui.norm_pixel;
1208 }
1209#endif
1210
1211 attr &= ~HL_INVERSE;
1212 if (cattr & HL_INVERSE)
1213 {
1214 cc = cbg;
1215 cbg = cfg;
1216 cfg = cc;
1217 }
1218 cattr &= ~HL_INVERSE;
1219
1220 /*
1221 * When we don't have window focus, draw a hollow cursor.
1222 */
1223 if (!gui.in_focus)
1224 {
1225 gui_mch_draw_hollow_cursor(cbg);
1226 return;
1227 }
1228
1229 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001230 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_HANGULIN
1232 || composing_hangul
1233#endif
1234 )
1235 {
1236 /*
1237 * Draw the text character with the cursor colors. Use the
1238 * character attributes plus the cursor attributes.
1239 */
1240 gui.highlight_mask = (cattr | attr);
1241#ifdef FEAT_HANGULIN
1242 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001243 {
1244 char_u *comp_buf;
1245 int comp_len;
1246
1247 comp_buf = hangul_composing_buffer_get(&comp_len);
1248 if (comp_buf)
1249 {
1250 (void)gui_outstr_nowrap(comp_buf, comp_len,
1251 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1252 cfg, cbg, 0);
1253 vim_free(comp_buf);
1254 }
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else
1257#endif
1258 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1259 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1260 }
1261 else
1262 {
1263#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1264 int col_off = FALSE;
1265#endif
1266 /*
1267 * First draw the partial cursor, then overwrite with the text
1268 * character, using a transparent background.
1269 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001270 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001273 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 else
1276 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001277 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 cur_width = gui.char_width;
1279 }
1280#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001281 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1282 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001285 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 cur_width += gui.char_width;
1287# ifdef FEAT_RIGHTLEFT
1288 if (CURSOR_BAR_RIGHT)
1289 {
1290 /* gui.col points to the left halve of the character but
1291 * the vertical line needs to be on the right halve.
1292 * A double-wide horizontal line is also drawn from the
1293 * right halve in gui_mch_draw_part_cursor(). */
1294 col_off = TRUE;
1295 ++gui.col;
1296 }
1297# endif
1298 }
1299#endif
1300 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1301#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1302 if (col_off)
1303 --gui.col;
1304#endif
1305
1306#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1307 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1308 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1309 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1310 (guicolor_T)0, (guicolor_T)0, 0);
1311#endif
1312 }
1313 gui.highlight_mask = old_hl_mask;
1314 }
1315}
1316
1317#if defined(FEAT_MENU) || defined(PROTO)
1318 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001319gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001321# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (gui.menu_is_active && gui.in_use)
1323 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1324# endif
1325}
1326#endif
1327
1328/*
1329 * Position the various GUI components (text area, menu). The vertical
1330 * scrollbars are NOT handled here. See gui_update_scrollbars().
1331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001333gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 int text_area_x;
1336 int text_area_y;
1337 int text_area_width;
1338 int text_area_height;
1339
1340 /* avoid that moving components around generates events */
1341 ++hold_gui_events;
1342
1343 text_area_x = 0;
1344 if (gui.which_scrollbars[SBAR_LEFT])
1345 text_area_x += gui.scrollbar_width;
1346
1347 text_area_y = 0;
1348#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1349 gui.menu_width = total_width;
1350 if (gui.menu_is_active)
1351 text_area_y += gui.menu_height;
1352#endif
1353#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1354 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1355 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1356#endif
1357
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001358# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001359 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001360 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001361 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1365 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1366 {
1367# ifdef FEAT_GUI_ATHENA
1368 gui_mch_set_toolbar_pos(0, text_area_y,
1369 gui.menu_width, gui.toolbar_height);
1370# endif
1371 text_area_y += gui.toolbar_height;
1372 }
1373#endif
1374
1375 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1376 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1377
1378 gui_mch_set_text_area_pos(text_area_x,
1379 text_area_y,
1380 text_area_width,
1381 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001382#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 + xim_get_status_area_height()
1384#endif
1385 );
1386#ifdef FEAT_MENU
1387 gui_position_menu();
1388#endif
1389 if (gui.which_scrollbars[SBAR_BOTTOM])
1390 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1391 text_area_x,
1392 text_area_y + text_area_height,
1393 text_area_width,
1394 gui.scrollbar_height);
1395 gui.left_sbar_x = 0;
1396 gui.right_sbar_x = text_area_x + text_area_width;
1397
1398 --hold_gui_events;
1399}
1400
Bram Moolenaar02743632005-07-25 20:42:36 +00001401/*
1402 * Get the width of the widgets and decorations to the side of the text area.
1403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001405gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 int base_width;
1408
1409 base_width = 2 * gui.border_offset;
1410 if (gui.which_scrollbars[SBAR_LEFT])
1411 base_width += gui.scrollbar_width;
1412 if (gui.which_scrollbars[SBAR_RIGHT])
1413 base_width += gui.scrollbar_width;
1414 return base_width;
1415}
1416
Bram Moolenaar02743632005-07-25 20:42:36 +00001417/*
1418 * Get the height of the widgets and decorations above and below the text area.
1419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001421gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 int base_height;
1424
1425 base_height = 2 * gui.border_offset;
1426 if (gui.which_scrollbars[SBAR_BOTTOM])
1427 base_height += gui.scrollbar_height;
1428#ifdef FEAT_GUI_GTK
1429 /* We can't take the sizes properly into account until anything is
1430 * realized. Therefore we recalculate all the values here just before
1431 * setting the size. (--mdcki) */
1432#else
1433# ifdef FEAT_MENU
1434 if (gui.menu_is_active)
1435 base_height += gui.menu_height;
1436# endif
1437# ifdef FEAT_TOOLBAR
1438 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1439# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1440 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1441# else
1442 base_height += gui.toolbar_height;
1443# endif
1444# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001445# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1446 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001447 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001448 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450# ifdef FEAT_FOOTER
1451 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1452 base_height += gui.footer_height;
1453# endif
1454# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1455 base_height += gui_mch_text_area_extra_height();
1456# endif
1457#endif
1458 return base_height;
1459}
1460
1461/*
1462 * Should be called after the GUI shell has been resized. Its arguments are
1463 * the new width and height of the shell in pixels.
1464 */
1465 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001466gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 static int busy = FALSE;
1469
1470 if (!gui.shell_created) /* ignore when still initializing */
1471 return;
1472
1473 /*
1474 * Can't resize the screen while it is being redrawn. Remember the new
1475 * size and handle it later.
1476 */
1477 if (updating_screen || busy)
1478 {
1479 new_pixel_width = pixel_width;
1480 new_pixel_height = pixel_height;
1481 return;
1482 }
1483
1484again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001485 new_pixel_width = 0;
1486 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 busy = TRUE;
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 /* Flush pending output before redrawing */
1490 out_flush();
1491
1492 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001493 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 /*
1499 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1500 * at the last line here (why does it have to be one row too low?).
1501 */
1502 if (State == ASKMORE || State == CONFIRM)
1503 gui.row = gui.num_rows;
1504
1505 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1506 * the safe side. */
1507 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1508 || gui.num_rows != Rows || gui.num_cols != Columns)
1509 shell_resized();
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 gui_update_scrollbars(TRUE);
1512 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001513#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 xim_set_status_area();
1515#endif
1516
1517 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001518
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001519 /* We may have been called again while redrawing the screen.
1520 * Need to do it all again with the latest size then. But only if the size
1521 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (new_pixel_height)
1523 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001524 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1525 {
1526 new_pixel_width = 0;
1527 new_pixel_height = 0;
1528 }
1529 else
1530 {
1531 pixel_width = new_pixel_width;
1532 pixel_height = new_pixel_height;
1533 goto again;
1534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536}
1537
1538/*
1539 * Check if gui_resize_shell() must be called.
1540 */
1541 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001542gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 /* careful: gui_resize_shell() may postpone the resize again if we
1546 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001547 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548}
1549
1550 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001551gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 Rows = gui.num_rows;
1554 Columns = gui.num_cols;
1555 return OK;
1556}
1557
1558/*
1559 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001560 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1561 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001562 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1563 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001566gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001567 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001568 int fit_to_display,
1569 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 int base_width;
1572 int base_height;
1573 int width;
1574 int height;
1575 int min_width;
1576 int min_height;
1577 int screen_w;
1578 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001579#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001580 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001581 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001582#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001583 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 if (!gui.shell_created)
1586 return;
1587
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001588#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 /* If not setting to a user specified size and maximized, calculate the
1590 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001591 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1592 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 gui_mch_newfont();
1595 return;
1596 }
1597#endif
1598
1599 base_width = gui_get_base_width();
1600 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001601 if (fit_to_display)
1602 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001603 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605#ifdef USE_SUN_WORKSHOP
1606 if (!mustset && usingSunWorkShop
1607 && workshop_get_width_height(&width, &height))
1608 {
1609 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1610 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1611 }
1612 else
1613#endif
1614 {
1615 width = Columns * gui.char_width + base_width;
1616 height = Rows * gui.char_height + base_height;
1617 }
1618
1619 if (fit_to_display)
1620 {
1621 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001622 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 Columns = (screen_w - base_width) / gui.char_width;
1625 if (Columns < MIN_COLUMNS)
1626 Columns = MIN_COLUMNS;
1627 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001628#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001629 ++did_adjust;
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001632 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 Rows = (screen_h - base_height) / gui.char_height;
1635 check_shellsize();
1636 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001637#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001638 ++did_adjust;
1639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001641#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001642 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1643 && height + gui.char_height >= screen_h))
1644 /* don't unmaximize if at maximum size */
1645 un_maximize = FALSE;
1646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001648 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 gui.num_cols = Columns;
1650 gui.num_rows = Rows;
1651
1652 min_width = base_width + MIN_COLUMNS * gui.char_width;
1653 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001654 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001655
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001656#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001657 if (un_maximize)
1658 {
1659 /* If the window size is smaller than the screen unmaximize the
1660 * window, otherwise resizing won't work. */
1661 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1662 if ((width + gui.char_width < screen_w
1663 || height + gui.char_height * 2 < screen_h)
1664 && gui_mch_maximized())
1665 gui_mch_unmaximize();
1666 }
1667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001670 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001672 if (fit_to_display && x >= 0 && y >= 0)
1673 {
1674 /* Some window managers put the Vim window left of/above the screen.
1675 * Only change the position if it wasn't already negative before
1676 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 gui_mch_update();
1678 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1679 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1680 }
1681
1682 gui_position_components(width);
1683 gui_update_scrollbars(TRUE);
1684 gui_reset_scroll_region();
1685}
1686
1687/*
1688 * Called when Rows and/or Columns has changed.
1689 */
1690 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001691gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 gui_reset_scroll_region();
1694}
1695
1696/*
1697 * Make scroll region cover whole screen.
1698 */
1699 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001700gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 gui.scroll_region_top = 0;
1703 gui.scroll_region_bot = gui.num_rows - 1;
1704 gui.scroll_region_left = 0;
1705 gui.scroll_region_right = gui.num_cols - 1;
1706}
1707
1708 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001709gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 if (mask > HL_ALL) /* highlight code */
1712 gui.highlight_mask = mask;
1713 else /* mask */
1714 gui.highlight_mask |= mask;
1715}
1716
1717 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001718gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 if (mask > HL_ALL) /* highlight code */
1721 gui.highlight_mask = HL_NORMAL;
1722 else /* mask */
1723 gui.highlight_mask &= ~mask;
1724}
1725
1726/*
1727 * Clear a rectangular region of the screen from text pos (row1, col1) to
1728 * (row2, col2) inclusive.
1729 */
1730 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001731gui_clear_block(
1732 int row1,
1733 int col1,
1734 int row2,
1735 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 /* Clear the selection if we are about to write over it */
1738 clip_may_clear_selection(row1, row2);
1739
1740 gui_mch_clear_block(row1, col1, row2, col2);
1741
1742 /* Invalidate cursor if it was in this block */
1743 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1744 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1745 gui.cursor_is_valid = FALSE;
1746}
1747
1748/*
1749 * Write code to update the cursor later. This avoids the need to flush the
1750 * output buffer before calling gui_update_cursor().
1751 */
1752 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001753gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001755 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756}
1757
1758 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001759gui_write(
1760 char_u *s,
1761 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 char_u *p;
1764 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 int force_scrollbar = FALSE;
1767 static win_T *old_curwin = NULL;
1768
1769/* #define DEBUG_GUI_WRITE */
1770#ifdef DEBUG_GUI_WRITE
1771 {
1772 int i;
1773 char_u *str;
1774
1775 printf("gui_write(%d):\n ", len);
1776 for (i = 0; i < len; i++)
1777 if (s[i] == ESC)
1778 {
1779 if (i != 0)
1780 printf("\n ");
1781 printf("<ESC>");
1782 }
1783 else
1784 {
1785 str = transchar_byte(s[i]);
1786 if (str[0] && str[1])
1787 printf("<%s>", (char *)str);
1788 else
1789 printf("%s", (char *)str);
1790 }
1791 printf("\n");
1792 }
1793#endif
1794 while (len)
1795 {
1796 if (s[0] == ESC && s[1] == '|')
1797 {
1798 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001799 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801 arg1 = getdigits(&p);
1802 if (p > s + len)
1803 break;
1804 if (*p == ';')
1805 {
1806 ++p;
1807 arg2 = getdigits(&p);
1808 if (p > s + len)
1809 break;
1810 }
1811 }
1812 switch (*p)
1813 {
1814 case 'C': /* Clear screen */
1815 clip_scroll_selection(9999);
1816 gui_mch_clear_all();
1817 gui.cursor_is_valid = FALSE;
1818 force_scrollbar = TRUE;
1819 break;
1820 case 'M': /* Move cursor */
1821 gui_set_cursor(arg1, arg2);
1822 break;
1823 case 's': /* force cursor (shape) update */
1824 force_cursor = TRUE;
1825 break;
1826 case 'R': /* Set scroll region */
1827 if (arg1 < arg2)
1828 {
1829 gui.scroll_region_top = arg1;
1830 gui.scroll_region_bot = arg2;
1831 }
1832 else
1833 {
1834 gui.scroll_region_top = arg2;
1835 gui.scroll_region_bot = arg1;
1836 }
1837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 case 'V': /* Set vertical scroll region */
1839 if (arg1 < arg2)
1840 {
1841 gui.scroll_region_left = arg1;
1842 gui.scroll_region_right = arg2;
1843 }
1844 else
1845 {
1846 gui.scroll_region_left = arg2;
1847 gui.scroll_region_right = arg1;
1848 }
1849 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 case 'd': /* Delete line */
1851 gui_delete_lines(gui.row, 1);
1852 break;
1853 case 'D': /* Delete lines */
1854 gui_delete_lines(gui.row, arg1);
1855 break;
1856 case 'i': /* Insert line */
1857 gui_insert_lines(gui.row, 1);
1858 break;
1859 case 'I': /* Insert lines */
1860 gui_insert_lines(gui.row, arg1);
1861 break;
1862 case '$': /* Clear to end-of-line */
1863 gui_clear_block(gui.row, gui.col, gui.row,
1864 (int)Columns - 1);
1865 break;
1866 case 'h': /* Turn on highlighting */
1867 gui_start_highlight(arg1);
1868 break;
1869 case 'H': /* Turn off highlighting */
1870 gui_stop_highlight(arg1);
1871 break;
1872 case 'f': /* flash the window (visual bell) */
1873 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1874 break;
1875 default:
1876 p = s + 1; /* Skip the ESC */
1877 break;
1878 }
1879 len -= (int)(++p - s);
1880 s = p;
1881 }
1882 else if (
1883#ifdef EBCDIC
1884 CtrlChar(s[0]) != 0 /* Ctrl character */
1885#else
1886 s[0] < 0x20 /* Ctrl character */
1887#endif
1888#ifdef FEAT_SIGN_ICONS
1889 && s[0] != SIGN_BYTE
1890# ifdef FEAT_NETBEANS_INTG
1891 && s[0] != MULTISIGN_BYTE
1892# endif
1893#endif
1894 )
1895 {
1896 if (s[0] == '\n') /* NL */
1897 {
1898 gui.col = 0;
1899 if (gui.row < gui.scroll_region_bot)
1900 gui.row++;
1901 else
1902 gui_delete_lines(gui.scroll_region_top, 1);
1903 }
1904 else if (s[0] == '\r') /* CR */
1905 {
1906 gui.col = 0;
1907 }
1908 else if (s[0] == '\b') /* Backspace */
1909 {
1910 if (gui.col)
1911 --gui.col;
1912 }
1913 else if (s[0] == Ctrl_L) /* cursor-right */
1914 {
1915 ++gui.col;
1916 }
1917 else if (s[0] == Ctrl_G) /* Beep */
1918 {
1919 gui_mch_beep();
1920 }
1921 /* Other Ctrl character: shouldn't happen! */
1922
1923 --len; /* Skip this char */
1924 ++s;
1925 }
1926 else
1927 {
1928 p = s;
1929 while (len > 0 && (
1930#ifdef EBCDIC
1931 CtrlChar(*p) == 0
1932#else
1933 *p >= 0x20
1934#endif
1935#ifdef FEAT_SIGN_ICONS
1936 || *p == SIGN_BYTE
1937# ifdef FEAT_NETBEANS_INTG
1938 || *p == MULTISIGN_BYTE
1939# endif
1940#endif
1941 ))
1942 {
1943 len--;
1944 p++;
1945 }
1946 gui_outstr(s, (int)(p - s));
1947 s = p;
1948 }
1949 }
1950
1951 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1952 * set). */
1953 if (force_cursor)
1954 gui_update_cursor(TRUE, TRUE);
1955
1956 /* When switching to another window the dragging must have stopped.
1957 * Required for GTK, dragged_sb isn't reset. */
1958 if (old_curwin != curwin)
1959 gui.dragged_sb = SBAR_NONE;
1960
1961 /* Update the scrollbars after clearing the screen or when switched
1962 * to another window.
1963 * Update the horizontal scrollbar always, it's difficult to check all
1964 * situations where it might change. */
1965 if (force_scrollbar || old_curwin != curwin)
1966 gui_update_scrollbars(force_scrollbar);
1967 else
1968 gui_update_horiz_scrollbar(FALSE);
1969 old_curwin = curwin;
1970
1971 /*
1972 * We need to make sure this is cleared since Athena doesn't tell us when
1973 * he is done dragging. Do the same for GTK.
1974 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001975#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 gui.dragged_sb = SBAR_NONE;
1977#endif
1978
1979 gui_mch_flush(); /* In case vim decides to take a nap */
1980}
1981
1982/*
1983 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1984 * produces wrong results. Call gui_dont_update_cursor() before that code and
1985 * gui_can_update_cursor() afterwards.
1986 */
1987 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001988gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 if (gui.in_use)
1991 {
1992 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001993 if (undraw)
1994 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 can_update_cursor = FALSE;
1996 }
1997}
1998
1999 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002000gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 can_update_cursor = TRUE;
2003 /* No need to update the cursor right now, there is always more output
2004 * after scrolling. */
2005}
2006
2007 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002008gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 int this_len;
2011#ifdef FEAT_MBYTE
2012 int cells;
2013#endif
2014
2015 if (len == 0)
2016 return;
2017
2018 if (len < 0)
2019 len = (int)STRLEN(s);
2020
2021 while (len > 0)
2022 {
2023#ifdef FEAT_MBYTE
2024 if (has_mbyte)
2025 {
2026 /* Find out how many chars fit in the current line. */
2027 cells = 0;
2028 for (this_len = 0; this_len < len; )
2029 {
2030 cells += (*mb_ptr2cells)(s + this_len);
2031 if (gui.col + cells > Columns)
2032 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002033 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 if (this_len > len)
2036 this_len = len; /* don't include following composing char */
2037 }
2038 else
2039#endif
2040 if (gui.col + len > Columns)
2041 this_len = Columns - gui.col;
2042 else
2043 this_len = len;
2044
2045 (void)gui_outstr_nowrap(s, this_len,
2046 0, (guicolor_T)0, (guicolor_T)0, 0);
2047 s += this_len;
2048 len -= this_len;
2049#ifdef FEAT_MBYTE
2050 /* fill up for a double-width char that doesn't fit. */
2051 if (len > 0 && gui.col < Columns)
2052 (void)gui_outstr_nowrap((char_u *)" ", 1,
2053 0, (guicolor_T)0, (guicolor_T)0, 0);
2054#endif
2055 /* The cursor may wrap to the next line. */
2056 if (gui.col >= Columns)
2057 {
2058 gui.col = 0;
2059 gui.row++;
2060 }
2061 }
2062}
2063
2064/*
2065 * Output one character (may be one or two display cells).
2066 * Caller must check for valid "off".
2067 * Returns FAIL or OK, just like gui_outstr_nowrap().
2068 */
2069 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002070gui_screenchar(
2071 int off, /* Offset from start of screen */
2072 int flags,
2073 guicolor_T fg, /* colors for cursor */
2074 guicolor_T bg, /* colors for cursor */
2075 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
2077#ifdef FEAT_MBYTE
2078 char_u buf[MB_MAXBYTES + 1];
2079
2080 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2081 if (enc_utf8 && ScreenLines[off] == 0)
2082 return OK;
2083
2084 if (enc_utf8 && ScreenLinesUC[off] != 0)
2085 /* Draw UTF-8 multi-byte character. */
2086 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2087 flags, fg, bg, back);
2088
2089 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2090 {
2091 buf[0] = ScreenLines[off];
2092 buf[1] = ScreenLines2[off];
2093 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2094 }
2095
2096 /* Draw non-multi-byte character or DBCS character. */
2097 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002098 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 flags, fg, bg, back);
2100#else
2101 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2102#endif
2103}
2104
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002105#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106/*
2107 * Output the string at the given screen position. This is used in place
2108 * of gui_screenchar() where possible because Pango needs as much context
2109 * as possible to work nicely. It's a lot faster as well.
2110 */
2111 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002112gui_screenstr(
2113 int off, /* Offset from start of screen */
2114 int len, /* string length in screen cells */
2115 int flags,
2116 guicolor_T fg, /* colors for cursor */
2117 guicolor_T bg, /* colors for cursor */
2118 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 char_u *buf;
2121 int outlen = 0;
2122 int i;
2123 int retval;
2124
2125 if (len <= 0) /* "cannot happen"? */
2126 return OK;
2127
2128 if (enc_utf8)
2129 {
2130 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2131 if (buf == NULL)
2132 return OK; /* not much we could do here... */
2133
2134 for (i = off; i < off + len; ++i)
2135 {
2136 if (ScreenLines[i] == 0)
2137 continue; /* skip second half of double-width char */
2138
2139 if (ScreenLinesUC[i] == 0)
2140 buf[outlen++] = ScreenLines[i];
2141 else
2142 outlen += utfc_char2bytes(i, buf + outlen);
2143 }
2144
2145 buf[outlen] = NUL; /* only to aid debugging */
2146 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2147 vim_free(buf);
2148
2149 return retval;
2150 }
2151 else if (enc_dbcs == DBCS_JPNU)
2152 {
2153 buf = alloc((unsigned)(len * 2 + 1));
2154 if (buf == NULL)
2155 return OK; /* not much we could do here... */
2156
2157 for (i = off; i < off + len; ++i)
2158 {
2159 buf[outlen++] = ScreenLines[i];
2160
2161 /* handle double-byte single-width char */
2162 if (ScreenLines[i] == 0x8e)
2163 buf[outlen++] = ScreenLines2[i];
2164 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2165 buf[outlen++] = ScreenLines[++i];
2166 }
2167
2168 buf[outlen] = NUL; /* only to aid debugging */
2169 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2170 vim_free(buf);
2171
2172 return retval;
2173 }
2174 else
2175 {
2176 return gui_outstr_nowrap(&ScreenLines[off], len,
2177 flags, fg, bg, back);
2178 }
2179}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002180#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
2182/*
2183 * Output the given string at the current cursor position. If the string is
2184 * too long to fit on the line, then it is truncated.
2185 * "flags":
2186 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2187 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002188 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 * background.
2190 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2191 * it.
2192 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2193 * FAIL (the caller should start drawing "back" chars back).
2194 */
2195 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002196gui_outstr_nowrap(
2197 char_u *s,
2198 int len,
2199 int flags,
2200 guicolor_T fg, /* colors for cursor */
2201 guicolor_T bg, /* colors for cursor */
2202 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 long_u highlight_mask;
2205 long_u hl_mask_todo;
2206 guicolor_T fg_color;
2207 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002208 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002209#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002211# ifdef FEAT_MBYTE
2212 GuiFont wide_font = NOFONT;
2213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214# ifdef FEAT_XFONTSET
2215 GuiFontset fontset = NOFONTSET;
2216# endif
2217#endif
2218 attrentry_T *aep = NULL;
2219 int draw_flags;
2220 int col = gui.col;
2221#ifdef FEAT_SIGN_ICONS
2222 int draw_sign = FALSE;
2223# ifdef FEAT_NETBEANS_INTG
2224 int multi_sign = FALSE;
2225# endif
2226#endif
2227
2228 if (len < 0)
2229 len = (int)STRLEN(s);
2230 if (len == 0)
2231 return OK;
2232
2233#ifdef FEAT_SIGN_ICONS
2234 if (*s == SIGN_BYTE
2235# ifdef FEAT_NETBEANS_INTG
2236 || *s == MULTISIGN_BYTE
2237# endif
2238 )
2239 {
2240# ifdef FEAT_NETBEANS_INTG
2241 if (*s == MULTISIGN_BYTE)
2242 multi_sign = TRUE;
2243# endif
2244 /* draw spaces instead */
2245 s = (char_u *)" ";
2246 if (len == 1 && col > 0)
2247 --col;
2248 len = 2;
2249 draw_sign = TRUE;
2250 highlight_mask = 0;
2251 }
2252 else
2253#endif
2254 if (gui.highlight_mask > HL_ALL)
2255 {
2256 aep = syn_gui_attr2entry(gui.highlight_mask);
2257 if (aep == NULL) /* highlighting not set */
2258 highlight_mask = 0;
2259 else
2260 highlight_mask = aep->ae_attr;
2261 }
2262 else
2263 highlight_mask = gui.highlight_mask;
2264 hl_mask_todo = highlight_mask;
2265
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002266#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 /* Set the font */
2268 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2269 font = aep->ae_u.gui.font;
2270# ifdef FEAT_XFONTSET
2271 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2272 fontset = aep->ae_u.gui.fontset;
2273# endif
2274 else
2275 {
2276# ifdef FEAT_XFONTSET
2277 if (gui.fontset != NOFONTSET)
2278 fontset = gui.fontset;
2279 else
2280# endif
2281 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2282 {
2283 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2284 {
2285 font = gui.boldital_font;
2286 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2287 }
2288 else if (gui.bold_font != NOFONT)
2289 {
2290 font = gui.bold_font;
2291 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2292 }
2293 else
2294 font = gui.norm_font;
2295 }
2296 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2297 {
2298 font = gui.ital_font;
2299 hl_mask_todo &= ~HL_ITALIC;
2300 }
2301 else
2302 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002303
2304# ifdef FEAT_MBYTE
2305 /*
2306 * Choose correct wide_font by font. wide_font should be set with font
2307 * at same time in above block. But it will make many "ifdef" nasty
2308 * blocks. So we do it here.
2309 */
2310 if (font == gui.boldital_font && gui.wide_boldital_font)
2311 wide_font = gui.wide_boldital_font;
2312 else if (font == gui.bold_font && gui.wide_bold_font)
2313 wide_font = gui.wide_bold_font;
2314 else if (font == gui.ital_font && gui.wide_ital_font)
2315 wide_font = gui.wide_ital_font;
2316 else if (font == gui.norm_font && gui.wide_font)
2317 wide_font = gui.wide_font;
2318# endif
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321# ifdef FEAT_XFONTSET
2322 if (fontset != NOFONTSET)
2323 gui_mch_set_fontset(fontset);
2324 else
2325# endif
2326 gui_mch_set_font(font);
2327#endif
2328
2329 draw_flags = 0;
2330
2331 /* Set the color */
2332 bg_color = gui.back_pixel;
2333 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2334 {
2335 draw_flags |= DRAW_CURSOR;
2336 fg_color = fg;
2337 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002338 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340 else if (aep != NULL)
2341 {
2342 fg_color = aep->ae_u.gui.fg_color;
2343 if (fg_color == INVALCOLOR)
2344 fg_color = gui.norm_pixel;
2345 bg_color = aep->ae_u.gui.bg_color;
2346 if (bg_color == INVALCOLOR)
2347 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002348 sp_color = aep->ae_u.gui.sp_color;
2349 if (sp_color == INVALCOLOR)
2350 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002355 sp_color = fg_color;
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2359 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002360#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 gui_mch_set_colors(bg_color, fg_color);
2362#else
2363 gui_mch_set_fg_color(bg_color);
2364 gui_mch_set_bg_color(fg_color);
2365#endif
2366 }
2367 else
2368 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002369#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 gui_mch_set_colors(fg_color, bg_color);
2371#else
2372 gui_mch_set_fg_color(fg_color);
2373 gui_mch_set_bg_color(bg_color);
2374#endif
2375 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002376 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 /* Clear the selection if we are about to write over it */
2379 if (!(flags & GUI_MON_NOCLEAR))
2380 clip_may_clear_selection(gui.row, gui.row);
2381
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 /* If there's no bold font, then fake it */
2384 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2385 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 /*
2388 * When drawing bold or italic characters the spill-over from the left
2389 * neighbor may be destroyed. Let the caller backup to start redrawing
2390 * just after a blank.
2391 */
2392 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2393 return FAIL;
2394
Bram Moolenaare60acc12011-05-10 16:41:25 +02002395#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 /* If there's no italic font, then fake it.
2397 * For GTK2, we don't need a different font for italic style. */
2398 if (hl_mask_todo & HL_ITALIC)
2399 draw_flags |= DRAW_ITALIC;
2400
2401 /* Do we underline the text? */
2402 if (hl_mask_todo & HL_UNDERLINE)
2403 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405#else
2406 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 draw_flags |= DRAW_UNDERL;
2409#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002410 /* Do we undercurl the text? */
2411 if (hl_mask_todo & HL_UNDERCURL)
2412 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002414 /* Do we strikethrough the text? */
2415 if (hl_mask_todo & HL_STRIKETHROUGH)
2416 draw_flags |= DRAW_STRIKE;
2417
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002418 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (flags & GUI_MON_TRS_CURSOR)
2420 draw_flags |= DRAW_TRANSP;
2421
2422 /*
2423 * Draw the text.
2424 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002425#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /* The value returned is the length in display cells */
2427 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2428#else
2429# ifdef FEAT_MBYTE
2430 if (enc_utf8)
2431 {
2432 int start; /* index of bytes to be drawn */
2433 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002434 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int cn; /* cellwidth of current char */
2436 int i; /* index of current char */
2437 int c; /* current char value */
2438 int cl; /* byte length of current char */
2439 int comping; /* current char is composing */
2440 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002441 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002442 int prev_wide = FALSE;
2443 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002444# ifdef WIN3264
2445 int sep_comp = FALSE; /* Don't separate composing chars. */
2446# else
2447 int sep_comp = TRUE; /* Separate composing chars. */
2448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 /* Break the string at a composing character, it has to be drawn on
2451 * top of the previous character. */
2452 start = 0;
2453 cells = 0;
2454 for (i = 0; i < len; i += cl)
2455 {
2456 c = utf_ptr2char(s + i);
2457 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 comping = utf_iscomposing(c);
2459 if (!comping) /* count cells from non-composing chars */
2460 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002461 if (!comping || sep_comp)
2462 {
2463 if (cn > 1
2464# ifdef FEAT_XFONTSET
2465 && fontset == NOFONTSET
2466# endif
2467 && wide_font != NOFONT)
2468 curr_wide = TRUE;
2469 else
2470 curr_wide = FALSE;
2471 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002472 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (cl == 0) /* hit end of string */
2474 len = i + cl; /* len must be wrong "cannot happen" */
2475
Bram Moolenaar45933962013-01-23 17:43:57 +01002476 wide_changed = curr_wide != prev_wide;
2477
2478 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002480 if (i + cl >= len || (comping && sep_comp && i > start)
2481 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002482# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 || (cn > 1
2484# ifdef FEAT_XFONTSET
2485 /* No fontset: At least draw char after wide char at
2486 * right position. */
2487 && fontset == NOFONTSET
2488# endif
2489 )
2490# endif
2491 )
2492 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002493 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 thislen = i - start;
2495 else
2496 thislen = i - start + cl;
2497 if (thislen > 0)
2498 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002499 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002500 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2502 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002503 if (prev_wide)
2504 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 start += thislen;
2506 }
2507 scol += cells;
2508 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002509 /* Adjust to not draw a character which width is changed
2510 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002511 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002513 scol -= cn;
2514 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002517# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002518 /* No fontset: draw a space to fill the gap after a wide char
2519 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2521# ifdef FEAT_XFONTSET
2522 && fontset == NOFONTSET
2523# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002524 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2526 1, draw_flags);
2527# endif
2528 }
2529 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002530 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002532# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002533 /* Carbon ATSUI autodraws composing char over previous char */
2534 gui_mch_draw_string(gui.row, scol, s + i, cl,
2535 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002536# else
2537 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2538 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 start = i + cl;
2541 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002542 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 /* The stuff below assumes "len" is the length in screen columns. */
2545 len = scol - col;
2546 }
2547 else
2548# endif
2549 {
2550 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2551# ifdef FEAT_MBYTE
2552 if (enc_dbcs == DBCS_JPNU)
2553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 /* Get the length in display cells, this can be different from the
2555 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002556 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558# endif
2559 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002560#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2563 gui.col = col + len;
2564
2565 /* May need to invert it when it's part of the selection. */
2566 if (flags & GUI_MON_NOCLEAR)
2567 clip_may_redraw_selection(gui.row, col, len);
2568
2569 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2570 {
2571 /* Invalidate the old physical cursor position if we wrote over it */
2572 if (gui.cursor_row == gui.row
2573 && gui.cursor_col >= col
2574 && gui.cursor_col < col + len)
2575 gui.cursor_is_valid = FALSE;
2576 }
2577
2578#ifdef FEAT_SIGN_ICONS
2579 if (draw_sign)
2580 /* Draw the sign on top of the spaces. */
2581 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002582# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002583 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (multi_sign)
2585 netbeans_draw_multisign_indicator(gui.row);
2586# endif
2587#endif
2588
2589 return OK;
2590}
2591
2592/*
2593 * Un-draw the cursor. Actually this just redraws the character at the given
2594 * position. The character just before it too, for when it was in bold.
2595 */
2596 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002597gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 if (gui.cursor_is_valid)
2600 {
2601#ifdef FEAT_HANGULIN
2602 if (composing_hangul
2603 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002604 {
2605 char_u *comp_buf;
2606 int comp_len;
2607
2608 comp_buf = hangul_composing_buffer_get(&comp_len);
2609 if (comp_buf)
2610 {
2611 (void)gui_outstr_nowrap(comp_buf, comp_len,
2612 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2613 gui.norm_pixel, gui.back_pixel, 0);
2614 vim_free(comp_buf);
2615 }
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else
2618 {
2619#endif
2620 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2621 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2622 && gui.cursor_col > 0)
2623 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2624 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2625#ifdef FEAT_HANGULIN
2626 if (composing_hangul)
2627 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2628 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2629 }
2630#endif
2631 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2632 * here in case it wasn't needed to undraw it. */
2633 gui.cursor_is_valid = FALSE;
2634 }
2635}
2636
2637 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002638gui_redraw(
2639 int x,
2640 int y,
2641 int w,
2642 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 int row1, col1, row2, col2;
2645
2646 row1 = Y_2_ROW(y);
2647 col1 = X_2_COL(x);
2648 row2 = Y_2_ROW(y + h - 1);
2649 col2 = X_2_COL(x + w - 1);
2650
2651 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2652
2653 /*
2654 * We may need to redraw the cursor, but don't take it upon us to change
2655 * its location after a scroll.
2656 * (maybe be more strict even and test col too?)
2657 * These things may be outside the update/clipping region and reality may
2658 * not reflect Vims internal ideas if these operations are clipped away.
2659 */
2660 if (gui.row == gui.cursor_row)
2661 gui_update_cursor(TRUE, TRUE);
2662}
2663
2664/*
2665 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2666 * from col1 to col2 (inclusive).
2667 * Return TRUE when the character before the first drawn character has
2668 * different attributes (may have to be redrawn too).
2669 */
2670 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002671gui_redraw_block(
2672 int row1,
2673 int col1,
2674 int row2,
2675 int col2,
2676 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 int old_row, old_col;
2679 long_u old_hl_mask;
2680 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002681 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int idx, len;
2683 int back, nback;
2684 int retval = FALSE;
2685#ifdef FEAT_MBYTE
2686 int orig_col1, orig_col2;
2687#endif
2688
2689 /* Don't try to update when ScreenLines is not valid */
2690 if (!screen_cleared || ScreenLines == NULL)
2691 return retval;
2692
2693 /* Don't try to draw outside the shell! */
2694 /* Check everything, strange values may be caused by a big border width */
2695 col1 = check_col(col1);
2696 col2 = check_col(col2);
2697 row1 = check_row(row1);
2698 row2 = check_row(row2);
2699
2700 /* Remember where our cursor was */
2701 old_row = gui.row;
2702 old_col = gui.col;
2703 old_hl_mask = gui.highlight_mask;
2704#ifdef FEAT_MBYTE
2705 orig_col1 = col1;
2706 orig_col2 = col2;
2707#endif
2708
2709 for (gui.row = row1; gui.row <= row2; gui.row++)
2710 {
2711#ifdef FEAT_MBYTE
2712 /* When only half of a double-wide character is in the block, include
2713 * the other half. */
2714 col1 = orig_col1;
2715 col2 = orig_col2;
2716 off = LineOffset[gui.row];
2717 if (enc_dbcs != 0)
2718 {
2719 if (col1 > 0)
2720 col1 -= dbcs_screen_head_off(ScreenLines + off,
2721 ScreenLines + off + col1);
2722 col2 += dbcs_screen_tail_off(ScreenLines + off,
2723 ScreenLines + off + col2);
2724 }
2725 else if (enc_utf8)
2726 {
2727 if (ScreenLines[off + col1] == 0)
2728 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002729# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2731 ++col2;
2732# endif
2733 }
2734#endif
2735 gui.col = col1;
2736 off = LineOffset[gui.row] + gui.col;
2737 len = col2 - col1 + 1;
2738
2739 /* Find how many chars back this highlighting starts, or where a space
2740 * is. Needed for when the bold trick is used */
2741 for (back = 0; back < col1; ++back)
2742 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2743 || ScreenLines[off - 1 - back] == ' ')
2744 break;
2745 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2746 && ScreenLines[off - 1] != ' ');
2747
2748 /* Break it up in strings of characters with the same attributes. */
2749 /* Print UTF-8 characters individually. */
2750 while (len > 0)
2751 {
2752 first_attr = ScreenAttrs[off];
2753 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002754#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (enc_utf8 && ScreenLinesUC[off] != 0)
2756 {
2757 /* output multi-byte character separately */
2758 nback = gui_screenchar(off, flags,
2759 (guicolor_T)0, (guicolor_T)0, back);
2760 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2761 idx = 2;
2762 else
2763 idx = 1;
2764 }
2765 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2766 {
2767 /* output double-byte, single-width character separately */
2768 nback = gui_screenchar(off, flags,
2769 (guicolor_T)0, (guicolor_T)0, back);
2770 idx = 1;
2771 }
2772 else
2773#endif
2774 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002775#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 for (idx = 0; idx < len; ++idx)
2777 {
2778 if (enc_utf8 && ScreenLines[off + idx] == 0)
2779 continue; /* skip second half of double-width char */
2780 if (ScreenAttrs[off + idx] != first_attr)
2781 break;
2782 }
2783 /* gui_screenstr() takes care of multibyte chars */
2784 nback = gui_screenstr(off, idx, flags,
2785 (guicolor_T)0, (guicolor_T)0, back);
2786#else
2787 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2788 idx++)
2789 {
2790# ifdef FEAT_MBYTE
2791 /* Stop at a multi-byte Unicode character. */
2792 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2793 break;
2794 if (enc_dbcs == DBCS_JPNU)
2795 {
2796 /* Stop at a double-byte single-width char. */
2797 if (ScreenLines[off + idx] == 0x8e)
2798 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002799 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 + off + idx) == 2)
2801 ++idx; /* skip second byte of double-byte char */
2802 }
2803# endif
2804 }
2805 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2806 (guicolor_T)0, (guicolor_T)0, back);
2807#endif
2808 }
2809 if (nback == FAIL)
2810 {
2811 /* Must back up to start drawing where a bold or italic word
2812 * starts. */
2813 off -= back;
2814 len += back;
2815 gui.col -= back;
2816 }
2817 else
2818 {
2819 off += idx;
2820 len -= idx;
2821 }
2822 back = 0;
2823 }
2824 }
2825
2826 /* Put the cursor back where it was */
2827 gui.row = old_row;
2828 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002829 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 return retval;
2832}
2833
2834 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002835gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 if (count <= 0)
2838 return;
2839
2840 if (row + count > gui.scroll_region_bot)
2841 /* Scrolled out of region, just blank the lines out */
2842 gui_clear_block(row, gui.scroll_region_left,
2843 gui.scroll_region_bot, gui.scroll_region_right);
2844 else
2845 {
2846 gui_mch_delete_lines(row, count);
2847
2848 /* If the cursor was in the deleted lines it's now gone. If the
2849 * cursor was in the scrolled lines adjust its position. */
2850 if (gui.cursor_row >= row
2851 && gui.cursor_col >= gui.scroll_region_left
2852 && gui.cursor_col <= gui.scroll_region_right)
2853 {
2854 if (gui.cursor_row < row + count)
2855 gui.cursor_is_valid = FALSE;
2856 else if (gui.cursor_row <= gui.scroll_region_bot)
2857 gui.cursor_row -= count;
2858 }
2859 }
2860}
2861
2862 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002863gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 if (count <= 0)
2866 return;
2867
2868 if (row + count > gui.scroll_region_bot)
2869 /* Scrolled out of region, just blank the lines out */
2870 gui_clear_block(row, gui.scroll_region_left,
2871 gui.scroll_region_bot, gui.scroll_region_right);
2872 else
2873 {
2874 gui_mch_insert_lines(row, count);
2875
2876 if (gui.cursor_row >= gui.row
2877 && gui.cursor_col >= gui.scroll_region_left
2878 && gui.cursor_col <= gui.scroll_region_right)
2879 {
2880 if (gui.cursor_row <= gui.scroll_region_bot - count)
2881 gui.cursor_row += count;
2882 else if (gui.cursor_row <= gui.scroll_region_bot)
2883 gui.cursor_is_valid = FALSE;
2884 }
2885 }
2886}
2887
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002888#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002889/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002890 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2891 */
2892 static int
2893gui_wait_for_chars_3(
2894 long wtime,
2895 int *interrupted UNUSED,
2896 int ignore_input UNUSED)
2897{
2898 return gui_mch_wait_for_chars(wtime);
2899}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002900#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002901
2902/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002903 * Returns OK if a character was found to be available within the given time,
2904 * or FAIL otherwise.
2905 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002906 static int
2907gui_wait_for_chars_or_timer(long wtime)
2908{
2909#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002910 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3, NULL, 0);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002911#else
2912 return gui_mch_wait_for_chars(wtime);
2913#endif
2914}
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916/*
2917 * The main GUI input routine. Waits for a character from the keyboard.
2918 * wtime == -1 Wait forever.
2919 * wtime == 0 Don't wait.
2920 * wtime > 0 Wait wtime milliseconds for a character.
2921 * Returns OK if a character was found to be available within the given time,
2922 * or FAIL otherwise.
2923 */
2924 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002925gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
2927 int retval;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002928#if defined(ELAPSED_FUNC) && defined(FEAT_AUTOCMD)
2929 ELAPSED_TYPE start_tv;
2930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002932#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /*
2934 * If we're going to wait a bit, update the menus and mouse shape for the
2935 * current State.
2936 */
2937 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 gui_update_menus(0);
2939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 gui_mch_update();
2942 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /* Before waiting, flush any output to the screen. */
2948 gui_mch_flush();
2949
2950 if (wtime > 0)
2951 {
2952 /* Blink when waiting for a character. Probably only does something
2953 * for showmatch() */
2954 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002955 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 return retval;
2958 }
2959
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002960#if defined(ELAPSED_FUNC) && defined(FEAT_AUTOCMD)
2961 ELAPSED_INIT(start_tv);
2962#endif
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002965 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 */
2967 gui_mch_start_blink();
2968
Bram Moolenaar3918c952005-03-15 22:34:55 +00002969 retval = FAIL;
2970 /*
2971 * We may want to trigger the CursorHold event. First wait for
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002972 * 'updatetime' and if nothing is typed within that time, and feedkeys()
2973 * wasn't used, put the K_CURSORHOLD key in the input buffer.
Bram Moolenaar3918c952005-03-15 22:34:55 +00002974 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002975 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002976 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#ifdef FEAT_AUTOCMD
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002978 else if (trigger_cursorhold()
2979# ifdef ELAPSED_FUNC
2980 && ELAPSED_FUNC(start_tv) >= p_ut
2981# endif
2982 && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002984 char_u buf[3];
2985
2986 /* Put K_CURSORHOLD in the input buffer. */
2987 buf[0] = CSI;
2988 buf[1] = KS_EXTRA;
2989 buf[2] = (int)KE_CURSORHOLD;
2990 add_to_input_buf(buf, 3);
2991
2992 retval = OK;
2993 }
2994#endif
2995
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002996 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002997 {
2998 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002999 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01003000 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 gui_mch_stop_blink();
3004 return retval;
3005}
3006
3007/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003008 * Equivalent of mch_inchar() for the GUI.
3009 */
3010 int
3011gui_inchar(
3012 char_u *buf,
3013 int maxlen,
3014 long wtime, /* milli seconds */
3015 int tb_change_cnt)
3016{
3017 if (gui_wait_for_chars(wtime, tb_change_cnt)
3018 && !typebuf_changed(tb_change_cnt))
3019 return read_from_input_buf(buf, (long)maxlen);
3020 return 0;
3021}
3022
3023/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003024 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 */
3026 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003027fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 p[0] = (char_u)(col / 128 + ' ' + 1);
3030 p[1] = (char_u)(col % 128 + ' ' + 1);
3031 p[2] = (char_u)(row / 128 + ' ' + 1);
3032 p[3] = (char_u)(row % 128 + ' ' + 1);
3033}
3034
3035/*
3036 * Generic mouse support function. Add a mouse event to the input buffer with
3037 * the given properties.
3038 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3039 * MOUSE_X1, MOUSE_X2
3040 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003041 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3042 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 * x, y --- Coordinates of mouse in pixels.
3044 * repeated_click --- TRUE if this click comes only a short time after a
3045 * previous click.
3046 * modifiers --- Bit field which may be any of the following modifiers
3047 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3048 * This function will ignore drag events where the mouse has not moved to a new
3049 * character.
3050 */
3051 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003052gui_send_mouse_event(
3053 int button,
3054 int x,
3055 int y,
3056 int repeated_click,
3057 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 static int prev_row = 0, prev_col = 0;
3060 static int prev_button = -1;
3061 static int num_clicks = 1;
3062 char_u string[10];
3063 enum key_extra button_char;
3064 int row, col;
3065#ifdef FEAT_CLIPBOARD
3066 int checkfor;
3067 int did_clip = FALSE;
3068#endif
3069
3070 /*
3071 * Scrolling may happen at any time, also while a selection is present.
3072 */
3073 switch (button)
3074 {
3075 case MOUSE_X1:
3076 button_char = KE_X1MOUSE;
3077 goto button_set;
3078 case MOUSE_X2:
3079 button_char = KE_X2MOUSE;
3080 goto button_set;
3081 case MOUSE_4:
3082 button_char = KE_MOUSEDOWN;
3083 goto button_set;
3084 case MOUSE_5:
3085 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003086 goto button_set;
3087 case MOUSE_6:
3088 button_char = KE_MOUSELEFT;
3089 goto button_set;
3090 case MOUSE_7:
3091 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092button_set:
3093 {
3094 /* Don't put events in the input queue now. */
3095 if (hold_gui_events)
3096 return;
3097
3098 string[3] = CSI;
3099 string[4] = KS_EXTRA;
3100 string[5] = (int)button_char;
3101
3102 /* Pass the pointer coordinates of the scroll event so that we
3103 * know which window to scroll. */
3104 row = gui_xy2colrow(x, y, &col);
3105 string[6] = (char_u)(col / 128 + ' ' + 1);
3106 string[7] = (char_u)(col % 128 + ' ' + 1);
3107 string[8] = (char_u)(row / 128 + ' ' + 1);
3108 string[9] = (char_u)(row % 128 + ' ' + 1);
3109
3110 if (modifiers == 0)
3111 add_to_input_buf(string + 3, 7);
3112 else
3113 {
3114 string[0] = CSI;
3115 string[1] = KS_MODIFIER;
3116 string[2] = 0;
3117 if (modifiers & MOUSE_SHIFT)
3118 string[2] |= MOD_MASK_SHIFT;
3119 if (modifiers & MOUSE_CTRL)
3120 string[2] |= MOD_MASK_CTRL;
3121 if (modifiers & MOUSE_ALT)
3122 string[2] |= MOD_MASK_ALT;
3123 add_to_input_buf(string, 10);
3124 }
3125 return;
3126 }
3127 }
3128
3129#ifdef FEAT_CLIPBOARD
3130 /* If a clipboard selection is in progress, handle it */
3131 if (clip_star.state == SELECT_IN_PROGRESS)
3132 {
3133 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3134 return;
3135 }
3136
3137 /* Determine which mouse settings to look for based on the current mode */
3138 switch (get_real_state())
3139 {
3140 case NORMAL_BUSY:
3141 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003142# ifdef FEAT_TERMINAL
3143 case TERMINAL:
3144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 case NORMAL: checkfor = MOUSE_NORMAL; break;
3146 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003147 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 case REPLACE:
3149 case REPLACE+LANGMAP:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003150# ifdef FEAT_VREPLACE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 case VREPLACE:
3152 case VREPLACE+LANGMAP:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 case INSERT:
3155 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3156 case ASKMORE:
3157 case HITRETURN: /* At the more- and hit-enter prompt pass the
3158 mouse event for a click on or below the
3159 message line. */
3160 if (Y_2_ROW(y) >= msg_row)
3161 checkfor = MOUSE_NORMAL;
3162 else
3163 checkfor = MOUSE_RETURN;
3164 break;
3165
3166 /*
3167 * On the command line, use the clipboard selection on all lines
3168 * but the command line. But not when pasting.
3169 */
3170 case CMDLINE:
3171 case CMDLINE+LANGMAP:
3172 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3173 checkfor = MOUSE_NONE;
3174 else
3175 checkfor = MOUSE_COMMAND;
3176 break;
3177
3178 default:
3179 checkfor = MOUSE_NONE;
3180 break;
3181 };
3182
3183 /*
3184 * Allow clipboard selection of text on the command line in "normal"
3185 * modes. Don't do this when dragging the status line, or extending a
3186 * Visual selection.
3187 */
3188 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003189 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 && button != MOUSE_DRAG
3191# ifdef FEAT_MOUSESHAPE
3192 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194# endif
3195 )
3196 checkfor = MOUSE_NONE;
3197
3198 /*
3199 * Use modeless selection when holding CTRL and SHIFT pressed.
3200 */
3201 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3202 checkfor = MOUSE_NONEF;
3203
3204 /*
3205 * In Ex mode, always use modeless selection.
3206 */
3207 if (exmode_active)
3208 checkfor = MOUSE_NONE;
3209
3210 /*
3211 * If the mouse settings say to not use the mouse, use the modeless
3212 * selection. But if Visual is active, assume that only the Visual area
3213 * will be selected.
3214 * Exception: On the command line, both the selection is used and a mouse
3215 * key is send.
3216 */
3217 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /* Don't do modeless selection in Visual mode. */
3220 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3221 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 /*
3224 * When 'mousemodel' is "popup", shift-left is translated to right.
3225 * But not when also using Ctrl.
3226 */
3227 if (mouse_model_popup() && button == MOUSE_LEFT
3228 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3229 {
3230 button = MOUSE_RIGHT;
3231 modifiers &= ~ MOUSE_SHIFT;
3232 }
3233
3234 /* If the selection is done, allow the right button to extend it.
3235 * If the selection is cleared, allow the right button to start it
3236 * from the cursor position. */
3237 if (button == MOUSE_RIGHT)
3238 {
3239 if (clip_star.state == SELECT_CLEARED)
3240 {
3241 if (State & CMDLINE)
3242 {
3243 col = msg_col;
3244 row = msg_row;
3245 }
3246 else
3247 {
3248 col = curwin->w_wcol;
3249 row = curwin->w_wrow + W_WINROW(curwin);
3250 }
3251 clip_start_selection(col, row, FALSE);
3252 }
3253 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3254 repeated_click);
3255 did_clip = TRUE;
3256 }
3257 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003258 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
3260 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3261 did_clip = TRUE;
3262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
3264 /* Always allow pasting */
3265 if (button != MOUSE_MIDDLE)
3266 {
3267 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3268 return;
3269 if (checkfor != MOUSE_COMMAND)
3270 button = MOUSE_LEFT;
3271 }
3272 repeated_click = FALSE;
3273 }
3274
3275 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003276 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#endif
3278
3279 /* Don't put events in the input queue now. */
3280 if (hold_gui_events)
3281 return;
3282
3283 row = gui_xy2colrow(x, y, &col);
3284
3285 /*
3286 * If we are dragging and the mouse hasn't moved far enough to be on a
3287 * different character, then don't send an event to vim.
3288 */
3289 if (button == MOUSE_DRAG)
3290 {
3291 if (row == prev_row && col == prev_col)
3292 return;
3293 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3294 if (y < 0)
3295 row = -1;
3296 }
3297
3298 /*
3299 * If topline has changed (window scrolled) since the last click, reset
3300 * repeated_click, because we don't want starting Visual mode when
3301 * clicking on a different character in the text.
3302 */
3303 if (curwin->w_topline != gui_prev_topline
3304#ifdef FEAT_DIFF
3305 || curwin->w_topfill != gui_prev_topfill
3306#endif
3307 )
3308 repeated_click = FALSE;
3309
3310 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3311 string[1] = KS_MOUSE;
3312 string[2] = KE_FILLER;
3313 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3314 {
3315 if (repeated_click)
3316 {
3317 /*
3318 * Handle multiple clicks. They only count if the mouse is still
3319 * pointing at the same character.
3320 */
3321 if (button != prev_button || row != prev_row || col != prev_col)
3322 num_clicks = 1;
3323 else if (++num_clicks > 4)
3324 num_clicks = 1;
3325 }
3326 else
3327 num_clicks = 1;
3328 prev_button = button;
3329 gui_prev_topline = curwin->w_topline;
3330#ifdef FEAT_DIFF
3331 gui_prev_topfill = curwin->w_topfill;
3332#endif
3333
3334 string[3] = (char_u)(button | 0x20);
3335 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3336 }
3337 else
3338 string[3] = (char_u)button;
3339
3340 string[3] |= modifiers;
3341 fill_mouse_coord(string + 4, col, row);
3342 add_to_input_buf(string, 8);
3343
3344 if (row < 0)
3345 prev_row = 0;
3346 else
3347 prev_row = row;
3348 prev_col = col;
3349
3350 /*
3351 * We need to make sure this is cleared since Athena doesn't tell us when
3352 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3353 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003354#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 gui.dragged_sb = SBAR_NONE;
3356#endif
3357}
3358
3359/*
3360 * Convert x and y coordinate to column and row in text window.
3361 * Corrects for multi-byte character.
3362 * returns column in "*colp" and row as return value;
3363 */
3364 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003365gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 int col = check_col(X_2_COL(x));
3368 int row = check_row(Y_2_ROW(y));
3369
3370#ifdef FEAT_MBYTE
3371 *colp = mb_fix_col(col, row);
3372#else
3373 *colp = col;
3374#endif
3375 return row;
3376}
3377
3378#if defined(FEAT_MENU) || defined(PROTO)
3379/*
3380 * Callback function for when a menu entry has been selected.
3381 */
3382 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003383gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003385 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
3387 /* Don't put events in the input queue now. */
3388 if (hold_gui_events)
3389 return;
3390
3391 bytes[0] = CSI;
3392 bytes[1] = KS_MENU;
3393 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003394 add_to_input_buf(bytes, 3);
3395 add_long_to_buf((long_u)menu, bytes);
3396 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397}
3398#endif
3399
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003400static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402/*
3403 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003404 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * in p_go.
3406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003408gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef FEAT_MENU
3411 static int prev_menu_is_active = -1;
3412#endif
3413#ifdef FEAT_TOOLBAR
3414 static int prev_toolbar = -1;
3415 int using_toolbar = FALSE;
3416#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003417#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003418 int using_tabline;
3419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#ifdef FEAT_FOOTER
3421 static int prev_footer = -1;
3422 int using_footer = FALSE;
3423#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003424#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 static int prev_tearoff = -1;
3426 int using_tearoff = FALSE;
3427#endif
3428
3429 char_u *p;
3430 int i;
3431#ifdef FEAT_MENU
3432 int grey_old, grey_new;
3433 char_u *temp;
3434#endif
3435 win_T *wp;
3436 int need_set_size;
3437 int fix_size;
3438
3439#ifdef FEAT_MENU
3440 if (oldval != NULL && gui.in_use)
3441 {
3442 /*
3443 * Check if the menu's go from grey to non-grey or vise versa.
3444 */
3445 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3446 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3447 if (grey_old != grey_new)
3448 {
3449 temp = p_go;
3450 p_go = oldval;
3451 gui_update_menus(MENU_ALL_MODES);
3452 p_go = temp;
3453 }
3454 }
3455 gui.menu_is_active = FALSE;
3456#endif
3457
3458 for (i = 0; i < 3; i++)
3459 gui.which_scrollbars[i] = FALSE;
3460 for (p = p_go; *p; p++)
3461 switch (*p)
3462 {
3463 case GO_LEFT:
3464 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3465 break;
3466 case GO_RIGHT:
3467 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 case GO_VLEFT:
3470 if (win_hasvertsplit())
3471 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3472 break;
3473 case GO_VRIGHT:
3474 if (win_hasvertsplit())
3475 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 case GO_BOT:
3478 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3479 break;
3480#ifdef FEAT_MENU
3481 case GO_MENUS:
3482 gui.menu_is_active = TRUE;
3483 break;
3484#endif
3485 case GO_GREY:
3486 /* make menu's have grey items, ignored here */
3487 break;
3488#ifdef FEAT_TOOLBAR
3489 case GO_TOOLBAR:
3490 using_toolbar = TRUE;
3491 break;
3492#endif
3493#ifdef FEAT_FOOTER
3494 case GO_FOOTER:
3495 using_footer = TRUE;
3496 break;
3497#endif
3498 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003499#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 using_tearoff = TRUE;
3501#endif
3502 break;
3503 default:
3504 /* Ignore options that are not supported */
3505 break;
3506 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (gui.in_use)
3509 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003510 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003512
3513#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003514 /* Update the GUI tab line, it may appear or disappear. This may
3515 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003516 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003517 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003518 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003519 /* We don't want a resize event change "Rows" here, save and
3520 * restore it. Resizing is handled below. */
3521 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003522 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003523 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003524 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003525 if (using_tabline)
3526 fix_size = TRUE;
3527 if (!gui_use_tabline())
3528 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3529 }
3530#endif
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 for (i = 0; i < 3; i++)
3533 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003534 /* The scrollbar needs to be updated when it is shown/unshown and
3535 * when switching tab pages. But the size only changes when it's
3536 * shown/unshown. Thus we need two places to remember whether a
3537 * scrollbar is there or not. */
3538 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003539 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003540 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 if (i == SBAR_BOTTOM)
3543 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3544 gui.which_scrollbars[i]);
3545 else
3546 {
3547 FOR_ALL_WINDOWS(wp)
3548 {
3549 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3550 }
3551 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003552 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3553 {
3554 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003555 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003556 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003557 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003558 if (gui.which_scrollbars[i])
3559 fix_size = TRUE;
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003562 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003563 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565
3566#ifdef FEAT_MENU
3567 if (gui.menu_is_active != prev_menu_is_active)
3568 {
3569 /* We don't want a resize event change "Rows" here, save and
3570 * restore it. Resizing is handled below. */
3571 i = Rows;
3572 gui_mch_enable_menu(gui.menu_is_active);
3573 Rows = i;
3574 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003575 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (gui.menu_is_active)
3577 fix_size = TRUE;
3578 }
3579#endif
3580
3581#ifdef FEAT_TOOLBAR
3582 if (using_toolbar != prev_toolbar)
3583 {
3584 gui_mch_show_toolbar(using_toolbar);
3585 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003586 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (using_toolbar)
3588 fix_size = TRUE;
3589 }
3590#endif
3591#ifdef FEAT_FOOTER
3592 if (using_footer != prev_footer)
3593 {
3594 gui_mch_enable_footer(using_footer);
3595 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003596 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (using_footer)
3598 fix_size = TRUE;
3599 }
3600#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003601#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (using_tearoff != prev_tearoff)
3603 {
3604 gui_mch_toggle_tearoffs(using_tearoff);
3605 prev_tearoff = using_tearoff;
3606 }
3607#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003608 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003609 {
3610#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003611 long prev_Columns = Columns;
3612 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /* Adjust the size of the window to make the text area keep the
3615 * same size and to avoid that part of our window is off-screen
3616 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003617 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003618
3619#ifdef FEAT_GUI_GTK
3620 /* GTK has the annoying habit of sending us resize events when
3621 * changing the window size ourselves. This mostly happens when
3622 * waiting for a character to arrive, quite unpredictably, and may
3623 * change Columns and Rows when we don't want it. Wait for a
3624 * character here to avoid this effect.
3625 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003626 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003627 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003628 * Don't change Rows when adding menu/toolbar/tabline.
3629 * Don't change Columns when adding vertical toolbar. */
3630 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003631 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003632 if ((need_set_size & RESIZE_VERT) == 0)
3633 Rows = prev_Rows;
3634 if ((need_set_size & RESIZE_HOR) == 0)
3635 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003636#endif
3637 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003638 /* When the console tabline appears or disappears the window positions
3639 * change. */
3640 if (firstwin->w_winrow != tabline_height())
3641 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643}
3644
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003645#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3646/*
3647 * Return TRUE if the GUI is taking care of the tabline.
3648 * It may still be hidden if 'showtabline' is zero.
3649 */
3650 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003651gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652{
3653 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3654}
3655
3656/*
3657 * Return TRUE if the GUI is showing the tabline.
3658 * This uses 'showtabline'.
3659 */
3660 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003661gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003662{
3663 if (!gui_use_tabline()
3664 || p_stal == 0
3665 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3666 return FALSE;
3667 return TRUE;
3668}
3669
3670/*
3671 * Update the tabline.
3672 * This may display/undisplay the tabline and update the labels.
3673 */
3674 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003675gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676{
3677 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003678 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003679
3680 if (!gui.starting && starting == 0)
3681 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003682 /* Updating the tabline uses direct GUI commands, flush
3683 * outstanding instructions first. (esp. clear screen) */
3684 out_flush();
3685 gui_mch_flush();
3686
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003687 if (!showit != !shown)
3688 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003689 if (showit != 0)
3690 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003691
3692 /* When the tabs change from hidden to shown or from shown to
3693 * hidden the size of the text area should remain the same. */
3694 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003695 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003696 }
3697}
3698
3699/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003700 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003701 */
3702 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003703get_tabline_label(
3704 tabpage_T *tp,
3705 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003706{
3707 int modified = FALSE;
3708 char_u buf[40];
3709 int wincount;
3710 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003711 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003712
Bram Moolenaar57657d82006-04-21 22:12:41 +00003713 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003714 opt = (tooltip ? &p_gtt : &p_gtl);
3715 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003716 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003717 int use_sandbox = FALSE;
3718 int save_called_emsg = called_emsg;
3719 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003720 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003721 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3722 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003723
3724 called_emsg = FALSE;
3725
3726 printer_page_num = tabpage_index(tp);
3727# ifdef FEAT_EVAL
3728 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003729 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003730# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003731 /* It's almost as going to the tabpage, but without autocommands. */
3732 curtab->tp_firstwin = firstwin;
3733 curtab->tp_lastwin = lastwin;
3734 curtab->tp_curwin = curwin;
3735 save_curtab = curtab;
3736 curtab = tp;
3737 topframe = curtab->tp_topframe;
3738 firstwin = curtab->tp_firstwin;
3739 lastwin = curtab->tp_lastwin;
3740 curwin = curtab->tp_curwin;
3741 curbuf = curwin->w_buffer;
3742
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003743 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003744 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003745 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003746 STRCPY(NameBuff, res);
3747
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003748 /* Back to the original curtab. */
3749 curtab = save_curtab;
3750 topframe = curtab->tp_topframe;
3751 firstwin = curtab->tp_firstwin;
3752 lastwin = curtab->tp_lastwin;
3753 curwin = curtab->tp_curwin;
3754 curbuf = curwin->w_buffer;
3755
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003756 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003757 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003758 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003759 called_emsg |= save_called_emsg;
3760 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003761
3762 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3763 * use a default label. */
3764 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003765 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003766 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003767 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003768 if (!tooltip)
3769 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003770
3771 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3772 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3773 if (bufIsChanged(wp->w_buffer))
3774 modified = TRUE;
3775 if (modified || wincount > 1)
3776 {
3777 if (wincount > 1)
3778 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3779 else
3780 buf[0] = NUL;
3781 if (modified)
3782 STRCAT(buf, "+");
3783 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003784 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003785 mch_memmove(NameBuff, buf, STRLEN(buf));
3786 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003787 }
3788}
3789
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003790/*
3791 * Send the event for clicking to select tab page "nr".
3792 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003793 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003794 */
3795 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003796send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003797{
3798 char_u string[3];
3799
3800 if (nr == tabpage_index(curtab))
3801 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003802
3803 /* Don't put events in the input queue now. */
3804 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003805# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003806 || cmdwin_type != 0
3807# endif
3808 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003809 {
3810 /* Set it back to the current tab page. */
3811 gui_mch_set_curtab(tabpage_index(curtab));
3812 return FALSE;
3813 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003814
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003815 string[0] = CSI;
3816 string[1] = KS_TABLINE;
3817 string[2] = KE_FILLER;
3818 add_to_input_buf(string, 3);
3819 string[0] = nr;
3820 add_to_input_buf_csi(string, 1);
3821 return TRUE;
3822}
3823
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003824/*
3825 * Send a tabline menu event
3826 */
3827 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003828send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003829{
3830 char_u string[3];
3831
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003832 /* Don't put events in the input queue now. */
3833 if (hold_gui_events)
3834 return;
3835
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003836 string[0] = CSI;
3837 string[1] = KS_TABMENU;
3838 string[2] = KE_FILLER;
3839 add_to_input_buf(string, 3);
3840 string[0] = tabidx;
3841 string[1] = (char_u)(long)event;
3842 add_to_input_buf_csi(string, 2);
3843}
3844
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847/*
3848 * Scrollbar stuff:
3849 */
3850
Bram Moolenaare45828b2006-02-15 22:12:56 +00003851/*
3852 * Remove all scrollbars. Used before switching to another tab page.
3853 */
3854 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003855gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003856{
3857 int i;
3858 win_T *wp;
3859
3860 for (i = 0; i < 3; i++)
3861 {
3862 if (i == SBAR_BOTTOM)
3863 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3864 else
3865 {
3866 FOR_ALL_WINDOWS(wp)
3867 {
3868 gui_do_scrollbar(wp, i, FALSE);
3869 }
3870 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003871 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003872 }
3873}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003876gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 static int sbar_ident = 0;
3879
3880 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3881 sb->wp = wp;
3882 sb->type = type;
3883 sb->value = 0;
3884#ifdef FEAT_GUI_ATHENA
3885 sb->pixval = 0;
3886#endif
3887 sb->size = 1;
3888 sb->max = 1;
3889 sb->top = 0;
3890 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 sb->status_height = 0;
3893 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3894}
3895
3896/*
3897 * Find the scrollbar with the given index.
3898 */
3899 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003900gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 win_T *wp;
3903
3904 if (gui.bottom_sbar.ident == ident)
3905 return &gui.bottom_sbar;
3906 FOR_ALL_WINDOWS(wp)
3907 {
3908 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3909 return &wp->w_scrollbars[SBAR_LEFT];
3910 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3911 return &wp->w_scrollbars[SBAR_RIGHT];
3912 }
3913 return NULL;
3914}
3915
3916/*
3917 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3918 *
3919 * For Win32, Macintosh and GTK+ 2:
3920 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3921 * you stop dragging the scrollbar. We get here each time the scrollbar is
3922 * dragged another pixel, but as far as the rest of vim goes, it thinks
3923 * we're just hanging in the call to DispatchMessage() in
3924 * process_message(). The DispatchMessage() call that hangs was passed a
3925 * mouse button click event in the scrollbar window. -- webb.
3926 *
3927 * Solution: Do the scrolling right here. But only when allowed.
3928 * Ignore the scrollbars while executing an external command or when there
3929 * are still characters to be processed.
3930 */
3931 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003932gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 int sb_num;
3936#ifdef USE_ON_FLY_SCROLL
3937 colnr_T old_leftcol = curwin->w_leftcol;
3938# ifdef FEAT_SCROLLBIND
3939 linenr_T old_topline = curwin->w_topline;
3940# endif
3941# ifdef FEAT_DIFF
3942 int old_topfill = curwin->w_topfill;
3943# endif
3944#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003945 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 int byte_count;
3947#endif
3948
3949 if (sb == NULL)
3950 return;
3951
3952 /* Don't put events in the input queue now. */
3953 if (hold_gui_events)
3954 return;
3955
3956#ifdef FEAT_CMDWIN
3957 if (cmdwin_type != 0 && sb->wp != curwin)
3958 return;
3959#endif
3960
3961 if (still_dragging)
3962 {
3963 if (sb->wp == NULL)
3964 gui.dragged_sb = SBAR_BOTTOM;
3965 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3966 gui.dragged_sb = SBAR_LEFT;
3967 else
3968 gui.dragged_sb = SBAR_RIGHT;
3969 gui.dragged_wp = sb->wp;
3970 }
3971 else
3972 {
3973 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003974#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003976 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 gui.dragged_wp = NULL;
3978#endif
3979 }
3980
3981 /* Vertical sbar info is kept in the first sbar (the left one) */
3982 if (sb->wp != NULL)
3983 sb = &sb->wp->w_scrollbars[0];
3984
3985 /*
3986 * Check validity of value
3987 */
3988 if (value < 0)
3989 value = 0;
3990#ifdef SCROLL_PAST_END
3991 else if (value > sb->max)
3992 value = sb->max;
3993#else
3994 if (value > sb->max - sb->size + 1)
3995 value = sb->max - sb->size + 1;
3996#endif
3997
3998 sb->value = value;
3999
4000#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00004001 /* When not allowed to do the scrolling right now, return.
4002 * This also checked input_available(), but that causes the first click in
4003 * a scrollbar to be ignored when Vim doesn't have focus. */
4004 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return;
4006#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004007#ifdef FEAT_INS_EXPAND
4008 /* Disallow scrolling the current window when the completion popup menu is
4009 * visible. */
4010 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4011 return;
4012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014#ifdef FEAT_RIGHTLEFT
4015 if (sb->wp == NULL && curwin->w_p_rl)
4016 {
4017 value = sb->max + 1 - sb->size - value;
4018 if (value < 0)
4019 value = 0;
4020 }
4021#endif
4022
4023 if (sb->wp != NULL) /* vertical scrollbar */
4024 {
4025 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4027 sb_num++;
4028 if (wp == NULL)
4029 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031#ifdef USE_ON_FLY_SCROLL
4032 current_scrollbar = sb_num;
4033 scrollbar_value = value;
4034 if (State & NORMAL)
4035 {
4036 gui_do_scroll();
4037 setcursor();
4038 }
4039 else if (State & INSERT)
4040 {
4041 ins_scroll();
4042 setcursor();
4043 }
4044 else if (State & CMDLINE)
4045 {
4046 if (msg_scrolled == 0)
4047 {
4048 gui_do_scroll();
4049 redrawcmdline();
4050 }
4051 }
4052# ifdef FEAT_FOLDING
4053 /* Value may have been changed for closed fold. */
4054 sb->value = sb->wp->w_topline - 1;
4055# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004056
4057 /* When dragging one scrollbar and there is another one at the other
4058 * side move the thumb of that one too. */
4059 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4060 gui_mch_set_scrollbar_thumb(
4061 &sb->wp->w_scrollbars[
4062 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4063 ? SBAR_LEFT : SBAR_RIGHT],
4064 sb->value, sb->size, sb->max);
4065
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066#else
4067 bytes[0] = CSI;
4068 bytes[1] = KS_VER_SCROLLBAR;
4069 bytes[2] = KE_FILLER;
4070 bytes[3] = (char_u)sb_num;
4071 byte_count = 4;
4072#endif
4073 }
4074 else
4075 {
4076#ifdef USE_ON_FLY_SCROLL
4077 scrollbar_value = value;
4078
4079 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004080 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 else if (State & INSERT)
4082 ins_horscroll();
4083 else if (State & CMDLINE)
4084 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004085 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004087 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 redrawcmdline();
4089 }
4090 }
4091 if (old_leftcol != curwin->w_leftcol)
4092 {
4093 updateWindow(curwin); /* update window, status and cmdline */
4094 setcursor();
4095 }
4096#else
4097 bytes[0] = CSI;
4098 bytes[1] = KS_HOR_SCROLLBAR;
4099 bytes[2] = KE_FILLER;
4100 byte_count = 3;
4101#endif
4102 }
4103
4104#ifdef USE_ON_FLY_SCROLL
4105# ifdef FEAT_SCROLLBIND
4106 /*
4107 * synchronize other windows, as necessary according to 'scrollbind'
4108 */
4109 if (curwin->w_p_scb
4110 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4111 || (sb->wp == curwin && (curwin->w_topline != old_topline
4112# ifdef FEAT_DIFF
4113 || curwin->w_topfill != old_topfill
4114# endif
4115 ))))
4116 {
4117 do_check_scrollbind(TRUE);
4118 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004119 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (wp->w_redr_type > 0)
4121 updateWindow(wp);
4122 setcursor();
4123 }
4124# endif
4125 out_flush();
4126 gui_update_cursor(FALSE, TRUE);
4127#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004128 add_to_input_buf(bytes, byte_count);
4129 add_long_to_buf((long_u)value, bytes);
4130 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#endif
4132}
4133
4134/*
4135 * Scrollbar stuff:
4136 */
4137
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004138/*
4139 * Called when something in the window layout has changed.
4140 */
4141 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004142gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004143{
4144 if (gui.in_use && starting == 0)
4145 {
4146 out_flush();
4147 gui_init_which_components(NULL);
4148 gui_update_scrollbars(TRUE);
4149 }
4150 need_mouse_correct = TRUE;
4151}
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004154gui_update_scrollbars(
4155 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156{
4157 win_T *wp;
4158 scrollbar_T *sb;
4159 long val, size, max; /* need 32 bits here */
4160 int which_sb;
4161 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /* Update the horizontal scrollbar */
4165 gui_update_horiz_scrollbar(force);
4166
4167#ifndef WIN3264
4168 /* Return straight away if there is neither a left nor right scrollbar.
4169 * On MS-Windows this is required anyway for scrollwheel messages. */
4170 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4171 return;
4172#endif
4173
4174 /*
4175 * Don't want to update a scrollbar while we're dragging it. But if we
4176 * have both a left and right scrollbar, and we drag one of them, we still
4177 * need to update the other one.
4178 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004179 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4180 && gui.which_scrollbars[SBAR_LEFT]
4181 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 /*
4184 * If we have two scrollbars and one of them is being dragged, just
4185 * copy the scrollbar position from the dragged one to the other one.
4186 */
4187 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4188 if (gui.dragged_wp != NULL)
4189 gui_mch_set_scrollbar_thumb(
4190 &gui.dragged_wp->w_scrollbars[which_sb],
4191 gui.dragged_wp->w_scrollbars[0].value,
4192 gui.dragged_wp->w_scrollbars[0].size,
4193 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195
4196 /* avoid that moving components around generates events */
4197 ++hold_gui_events;
4198
Bram Moolenaar45a24952016-07-24 22:25:15 +02004199 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201 if (wp->w_buffer == NULL) /* just in case */
4202 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004203 /* Skip a scrollbar that is being dragged. */
4204 if (!force && (gui.dragged_sb == SBAR_LEFT
4205 || gui.dragged_sb == SBAR_RIGHT)
4206 && gui.dragged_wp == wp)
4207 continue;
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#ifdef SCROLL_PAST_END
4210 max = wp->w_buffer->b_ml.ml_line_count - 1;
4211#else
4212 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4213#endif
4214 if (max < 0) /* empty buffer */
4215 max = 0;
4216 val = wp->w_topline - 1;
4217 size = wp->w_height;
4218#ifdef SCROLL_PAST_END
4219 if (val > max) /* just in case */
4220 val = max;
4221#else
4222 if (size > max + 1) /* just in case */
4223 size = max + 1;
4224 if (val > max - size + 1)
4225 val = max - size + 1;
4226#endif
4227 if (val < 0) /* minimal value is 0 */
4228 val = 0;
4229
4230 /*
4231 * Scrollbar at index 0 (the left one) contains all the information.
4232 * It would be the same info for left and right so we just store it for
4233 * one of them.
4234 */
4235 sb = &wp->w_scrollbars[0];
4236
4237 /*
4238 * Note: no check for valid w_botline. If it's not valid the
4239 * scrollbars will be updated later anyway.
4240 */
4241 if (size < 1 || wp->w_botline - 2 > max)
4242 {
4243 /*
4244 * This can happen during changing files. Just don't update the
4245 * scrollbar for now.
4246 */
4247 sb->height = 0; /* Force update next time */
4248 if (gui.which_scrollbars[SBAR_LEFT])
4249 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4250 if (gui.which_scrollbars[SBAR_RIGHT])
4251 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4252 continue;
4253 }
4254 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 || sb->top != wp->w_winrow
4256 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004258 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
4260 /* Height, width or position of scrollbar has changed. For
4261 * vertical split: curwin changed. */
4262 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 sb->top = wp->w_winrow;
4264 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 /* Calculate height and position in pixels */
4268 h = (sb->height + sb->status_height) * gui.char_height;
4269 y = sb->top * gui.char_height + gui.border_offset;
4270#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4271 if (gui.menu_is_active)
4272 y += gui.menu_height;
4273#endif
4274
4275#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4276 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4277# ifdef FEAT_GUI_ATHENA
4278 y += gui.toolbar_height;
4279# else
4280# ifdef FEAT_GUI_MSWIN
4281 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4282# endif
4283# endif
4284#endif
4285
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004286#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4287 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004288 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004289#endif
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 /* Height of top scrollbar includes width of top border */
4294 h += gui.border_offset;
4295 y -= gui.border_offset;
4296 }
4297 if (gui.which_scrollbars[SBAR_LEFT])
4298 {
4299 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4300 gui.left_sbar_x, y,
4301 gui.scrollbar_width, h);
4302 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4303 }
4304 if (gui.which_scrollbars[SBAR_RIGHT])
4305 {
4306 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4307 gui.right_sbar_x, y,
4308 gui.scrollbar_width, h);
4309 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4310 }
4311 }
4312
4313 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4314 * checking if the thumb moved at least a pixel. Only do this for
4315 * Athena, most other GUIs require the update anyway to make the
4316 * arrows work. */
4317#ifdef FEAT_GUI_ATHENA
4318 if (max == 0)
4319 y = 0;
4320 else
4321 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4322 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4323#else
4324 if (force || sb->value != val || sb->size != size || sb->max != max)
4325#endif
4326 {
4327 /* Thumb of scrollbar has moved */
4328 sb->value = val;
4329#ifdef FEAT_GUI_ATHENA
4330 sb->pixval = y;
4331#endif
4332 sb->size = size;
4333 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004334 if (gui.which_scrollbars[SBAR_LEFT]
4335 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4337 val, size, max);
4338 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004339 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4341 val, size, max);
4342 }
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 --hold_gui_events;
4346}
4347
4348/*
4349 * Enable or disable a scrollbar.
4350 * Check for scrollbars for vertically split windows which are not enabled
4351 * sometimes.
4352 */
4353 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004354gui_do_scrollbar(
4355 win_T *wp,
4356 int which, /* SBAR_LEFT or SBAR_RIGHT */
4357 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 int midcol = curwin->w_wincol + curwin->w_width / 2;
4360 int has_midcol = (wp->w_wincol <= midcol
4361 && wp->w_wincol + wp->w_width >= midcol);
4362
4363 /* Only enable scrollbars that contain the middle column of the current
4364 * window. */
4365 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4366 {
4367 /* Scrollbars only on one side. Don't enable scrollbars that don't
4368 * contain the middle column of the current window. */
4369 if (!has_midcol)
4370 enable = FALSE;
4371 }
4372 else
4373 {
4374 /* Scrollbars on both sides. Don't enable scrollbars that neither
4375 * contain the middle column of the current window nor are on the far
4376 * side. */
4377 if (midcol > Columns / 2)
4378 {
4379 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4380 enable = FALSE;
4381 }
4382 else
4383 {
4384 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4385 : !has_midcol)
4386 enable = FALSE;
4387 }
4388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4390}
4391
4392/*
4393 * Scroll a window according to the values set in the globals current_scrollbar
4394 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4395 * or FALSE otherwise.
4396 */
4397 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004398gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 win_T *wp, *save_wp;
4401 int i;
4402 long nlines;
4403 pos_T old_cursor;
4404 linenr_T old_topline;
4405#ifdef FEAT_DIFF
4406 int old_topfill;
4407#endif
4408
4409 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4410 if (wp == NULL)
4411 break;
4412 if (wp == NULL)
4413 /* Couldn't find window */
4414 return FALSE;
4415
4416 /*
4417 * Compute number of lines to scroll. If zero, nothing to do.
4418 */
4419 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4420 if (nlines == 0)
4421 return FALSE;
4422
4423 save_wp = curwin;
4424 old_topline = wp->w_topline;
4425#ifdef FEAT_DIFF
4426 old_topfill = wp->w_topfill;
4427#endif
4428 old_cursor = wp->w_cursor;
4429 curwin = wp;
4430 curbuf = wp->w_buffer;
4431 if (nlines < 0)
4432 scrolldown(-nlines, gui.dragged_wp == NULL);
4433 else
4434 scrollup(nlines, gui.dragged_wp == NULL);
4435 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4436 * the mouse-up event already, but we still want it to behave like when
4437 * dragging. But not the next click in an arrow. */
4438 if (gui.dragged_sb == SBAR_NONE)
4439 gui.dragged_wp = NULL;
4440
4441 if (old_topline != wp->w_topline
4442#ifdef FEAT_DIFF
4443 || old_topfill != wp->w_topfill
4444#endif
4445 )
4446 {
4447 if (p_so != 0)
4448 {
4449 cursor_correct(); /* fix window for 'so' */
4450 update_topline(); /* avoid up/down jump */
4451 }
4452 if (old_cursor.lnum != wp->w_cursor.lnum)
4453 coladvance(wp->w_curswant);
4454#ifdef FEAT_SCROLLBIND
4455 wp->w_scbind_pos = wp->w_topline;
4456#endif
4457 }
4458
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004459 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4460 validate_cursor();
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 curwin = save_wp;
4463 curbuf = save_wp->w_buffer;
4464
4465 /*
4466 * Don't call updateWindow() when nothing has changed (it will overwrite
4467 * the status line!).
4468 */
4469 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004470 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471#ifdef FEAT_DIFF
4472 || old_topfill != wp->w_topfill
4473#endif
4474 )
4475 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004476 int type = VALID;
4477
4478#ifdef FEAT_INS_EXPAND
4479 if (pum_visible())
4480 {
4481 type = NOT_VALID;
4482 wp->w_lines_valid = 0;
4483 }
4484#endif
4485 /* Don't set must_redraw here, it may cause the popup menu to
4486 * disappear when losing focus after a scrollbar drag. */
4487 if (wp->w_redr_type < type)
4488 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 updateWindow(wp); /* update window, status line, and cmdline */
4490 }
4491
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004492#ifdef FEAT_INS_EXPAND
4493 /* May need to redraw the popup menu. */
4494 if (pum_visible())
4495 pum_redraw();
4496#endif
4497
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004498 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499}
4500
4501
4502/*
4503 * Horizontal scrollbar stuff:
4504 */
4505
4506/*
4507 * Return length of line "lnum" for horizontal scrolling.
4508 */
4509 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004510scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
4512 char_u *p;
4513 colnr_T col;
4514 int w;
4515
4516 p = ml_get(lnum);
4517 col = 0;
4518 if (*p != NUL)
4519 for (;;)
4520 {
4521 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004522 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (*p == NUL) /* don't count the last character */
4524 break;
4525 col += w;
4526 }
4527 return col;
4528}
4529
4530/* Remember which line is currently the longest, so that we don't have to
4531 * search for it when scrolling horizontally. */
4532static linenr_T longest_lnum = 0;
4533
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004534/*
4535 * Find longest visible line number. If this is not possible (or not desired,
4536 * by setting 'h' in "guioptions") then the current line number is returned.
4537 */
4538 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004539gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004540{
4541 linenr_T ret = 0;
4542
4543 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4544 * line numbers, topline and botline can be invalid when displaying is
4545 * postponed. */
4546 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4547 && curwin->w_topline <= curwin->w_cursor.lnum
4548 && curwin->w_botline > curwin->w_cursor.lnum
4549 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4550 {
4551 linenr_T lnum;
4552 colnr_T n;
4553 long max = 0;
4554
4555 /* Use maximum of all visible lines. Remember the lnum of the
4556 * longest line, closest to the cursor line. Used when scrolling
4557 * below. */
4558 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4559 {
4560 n = scroll_line_len(lnum);
4561 if (n > (colnr_T)max)
4562 {
4563 max = n;
4564 ret = lnum;
4565 }
4566 else if (n == (colnr_T)max
4567 && abs((int)(lnum - curwin->w_cursor.lnum))
4568 < abs((int)(ret - curwin->w_cursor.lnum)))
4569 ret = lnum;
4570 }
4571 }
4572 else
4573 /* Use cursor line only. */
4574 ret = curwin->w_cursor.lnum;
4575
4576 return ret;
4577}
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004580gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581{
4582 long value, size, max; /* need 32 bit ints here */
4583
4584 if (!gui.which_scrollbars[SBAR_BOTTOM])
4585 return;
4586
4587 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4588 return;
4589
4590 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4591 return;
4592
4593 /*
4594 * It is possible for the cursor to be invalid if we're in the middle of
4595 * something (like changing files). If so, don't do anything for now.
4596 */
4597 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4598 {
4599 gui.bottom_sbar.value = -1;
4600 return;
4601 }
4602
Bram Moolenaar02631462017-09-22 15:20:32 +02004603 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (curwin->w_p_wrap)
4605 {
4606 value = 0;
4607#ifdef SCROLL_PAST_END
4608 max = 0;
4609#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004610 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#endif
4612 }
4613 else
4614 {
4615 value = curwin->w_leftcol;
4616
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004617 longest_lnum = gui_find_longest_lnum();
4618 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#ifdef FEAT_VIRTUALEDIT
4621 if (virtual_active())
4622 {
4623 /* May move the cursor even further to the right. */
4624 if (curwin->w_virtcol >= (colnr_T)max)
4625 max = curwin->w_virtcol;
4626 }
4627#endif
4628
4629#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004630 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#endif
4632 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004633 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 size -= curwin_col_off();
4635#ifndef SCROLL_PAST_END
4636 max -= curwin_col_off();
4637#endif
4638 }
4639
4640#ifndef SCROLL_PAST_END
4641 if (value > max - size + 1)
4642 value = max - size + 1; /* limit the value to allowable range */
4643#endif
4644
4645#ifdef FEAT_RIGHTLEFT
4646 if (curwin->w_p_rl)
4647 {
4648 value = max + 1 - size - value;
4649 if (value < 0)
4650 {
4651 size += value;
4652 value = 0;
4653 }
4654 }
4655#endif
4656 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4657 && max == gui.bottom_sbar.max)
4658 return;
4659
4660 gui.bottom_sbar.value = value;
4661 gui.bottom_sbar.size = size;
4662 gui.bottom_sbar.max = max;
4663 gui.prev_wrap = curwin->w_p_wrap;
4664
4665 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4666}
4667
4668/*
4669 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4670 */
4671 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004672gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 /* no wrapping, no scrolling */
4675 if (curwin->w_p_wrap)
4676 return FALSE;
4677
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004678 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 return FALSE;
4680
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004681 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004684 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004686 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004687 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004689 if (compute_longest_lnum)
4690 {
4691 curwin->w_cursor.lnum = gui_find_longest_lnum();
4692 curwin->w_cursor.col = 0;
4693 }
4694 /* Do a sanity check on "longest_lnum", just in case. */
4695 else if (longest_lnum >= curwin->w_topline
4696 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 curwin->w_cursor.lnum = longest_lnum;
4699 curwin->w_cursor.col = 0;
4700 }
4701 }
4702
4703 return leftcol_changed();
4704}
4705
4706/*
4707 * Check that none of the colors are the same as the background color
4708 */
4709 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004710gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4713 {
4714 gui_set_bg_color((char_u *)"White");
4715 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4716 gui_set_fg_color((char_u *)"Black");
4717 }
4718}
4719
Bram Moolenaar3918c952005-03-15 22:34:55 +00004720 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004721gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 gui.norm_pixel = gui_get_color(name);
4724 hl_set_fg_color_name(vim_strsave(name));
4725}
4726
Bram Moolenaar3918c952005-03-15 22:34:55 +00004727 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004728gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 gui.back_pixel = gui_get_color(name);
4731 hl_set_bg_color_name(vim_strsave(name));
4732}
4733
4734/*
4735 * Allocate a color by name.
4736 * Returns INVALCOLOR and gives an error message when failed.
4737 */
4738 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004739gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 guicolor_T t;
4742
4743 if (*name == NUL)
4744 return INVALCOLOR;
4745 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004748#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 && gui.in_use
4750#endif
4751 )
4752 EMSG2(_("E254: Cannot allocate color %s"), name);
4753 return t;
4754}
4755
4756/*
4757 * Return the grey value of a color (range 0-255).
4758 */
4759 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004760gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004762 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004764 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004765 + (((rgb >> 8) & 0xff) * 587)
4766 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767}
4768
4769#if defined(FEAT_GUI_X11) || defined(PROTO)
4770 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004771gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
4773 win_T *wp;
4774
4775 /* Nothing to do if GUI hasn't started yet. */
4776 if (!gui.in_use)
4777 return;
4778
4779 FOR_ALL_WINDOWS(wp)
4780 {
4781 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4782 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4783 }
4784 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4785}
4786#endif
4787
4788/*
4789 * Call this when focus has changed.
4790 */
4791 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004792gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794/*
4795 * Skip this code to avoid drawing the cursor when debugging and switching
4796 * between the debugger window and gvim.
4797 */
4798#if 1
4799 gui.in_focus = in_focus;
4800 out_flush(); /* make sure output has been written */
4801 gui_update_cursor(TRUE, FALSE);
4802
4803# ifdef FEAT_XIM
4804 xim_set_focus(in_focus);
4805# endif
4806
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004807 /* Put events in the input queue only when allowed.
4808 * ui_focus_change() isn't called directly, because it invokes
4809 * autocommands and that must not happen asynchronously. */
4810 if (!hold_gui_events)
4811 {
4812 char_u bytes[3];
4813
4814 bytes[0] = CSI;
4815 bytes[1] = KS_EXTRA;
4816 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4817 add_to_input_buf(bytes, 3);
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819#endif
4820}
4821
4822/*
4823 * Called when the mouse moved (but not when dragging).
4824 */
4825 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004826gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004829 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
Bram Moolenaard3667a22006-03-16 21:35:52 +00004831 /* Ignore this while still starting up. */
4832 if (!gui.in_use || gui.starting)
4833 return;
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#ifdef FEAT_MOUSESHAPE
4836 /* Get window pointer, and update mouse shape as well. */
4837 wp = xy2win(x, y);
4838#endif
4839
4840 /* Only handle this when 'mousefocus' set and ... */
4841 if (p_mousef
4842 && !hold_gui_events /* not holding events */
4843 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4844 && State != HITRETURN /* but not hit-return prompt */
4845 && msg_scrolled == 0 /* no scrolled message */
4846 && !need_mouse_correct /* not moving the pointer */
4847 && gui.in_focus) /* gvim in focus */
4848 {
4849 /* Don't move the mouse when it's left or right of the Vim window */
4850 if (x < 0 || x > Columns * gui.char_width)
4851 return;
4852#ifndef FEAT_MOUSESHAPE
4853 wp = xy2win(x, y);
4854#endif
4855 if (wp == curwin || wp == NULL)
4856 return; /* still in the same old window, or none at all */
4857
Bram Moolenaar9c102382006-05-03 21:26:49 +00004858 /* Ignore position in the tab pages line. */
4859 if (Y_2_ROW(y) < tabline_height())
4860 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 /*
4863 * format a mouse click on status line input
4864 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004865 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4866 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 */
4868 if (finish_op)
4869 {
4870 /* abort the current operator first */
4871 st[0] = ESC;
4872 add_to_input_buf(st, 1);
4873 }
4874 st[0] = CSI;
4875 st[1] = KS_MOUSE;
4876 st[2] = KE_FILLER;
4877 st[3] = (char_u)MOUSE_LEFT;
4878 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004879 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 wp->w_height + W_WINROW(wp));
4881
4882 add_to_input_buf(st, 8);
4883 st[3] = (char_u)MOUSE_RELEASE;
4884 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#ifdef FEAT_GUI_GTK
4886 /* Need to wake up the main loop */
4887 if (gtk_main_level() > 0)
4888 gtk_main_quit();
4889#endif
4890 }
4891}
4892
4893/*
4894 * Called when mouse should be moved to window with focus.
4895 */
4896 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004897gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 int x, y;
4900 win_T *wp = NULL;
4901
4902 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004903
4904 if (!(gui.in_use && p_mousef))
4905 return;
4906
4907 gui_mch_getmouse(&x, &y);
4908 /* Don't move the mouse when it's left or right of the Vim window */
4909 if (x < 0 || x > Columns * gui.char_width)
4910 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004911 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004912 wp = xy2win(x, y);
4913 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004915 validate_cline_row();
4916 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4917 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920}
4921
4922/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004923 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004926xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 int row;
4929 int col;
4930 win_T *wp;
4931
4932 row = Y_2_ROW(y);
4933 col = X_2_COL(x);
4934 if (row < 0 || col < 0) /* before first window */
4935 return NULL;
4936 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004937 if (wp == NULL)
4938 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004939#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (State == HITRETURN || State == ASKMORE)
4941 {
4942 if (Y_2_ROW(y) >= msg_row)
4943 update_mouseshape(SHAPE_IDX_MOREL);
4944 else
4945 update_mouseshape(SHAPE_IDX_MORE);
4946 }
4947 else if (row > wp->w_height) /* below status line */
4948 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004949 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004950 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004952 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004953 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 update_mouseshape(SHAPE_IDX_STATUS);
4955 else
4956 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004958 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959}
4960
4961/*
4962 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4963 * File names may be given to redefine the args list.
4964 */
4965 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004966ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 char_u *arg = eap->arg;
4969
4970 /*
4971 * Check for "-f" argument: foreground, don't fork.
4972 * Also don't fork when started with "gvim -f".
4973 * Do fork when using "gui -b".
4974 */
4975 if (arg[0] == '-'
4976 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004977 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 gui.dofork = (arg[1] == 'b');
4980 eap->arg = skipwhite(eap->arg + 2);
4981 }
4982 if (!gui.in_use)
4983 {
4984 /* Clear the command. Needed for when forking+exiting, to avoid part
4985 * of the argument ending up after the shell prompt. */
4986 msg_clr_eos_force();
4987 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004988#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004989 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 if (!ends_excmd(*eap->arg))
4993 ex_next(eap);
4994}
4995
4996#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004997 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998/*
4999 * This is shared between Athena, Motif and GTK.
5000 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005001static void gfp_setname(char_u *fname, void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003/*
5004 * Callback function for do_in_runtimepath().
5005 */
5006 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005007gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005009 char_u *gfp_buffer = cookie;
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (STRLEN(fname) >= MAXPATHL)
5012 *gfp_buffer = NUL;
5013 else
5014 STRCPY(gfp_buffer, fname);
5015}
5016
5017/*
5018 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5019 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5020 */
5021 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005022gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 if (STRLEN(name) > MAXPATHL - 14)
5025 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005026 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005027 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005028 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 return FAIL;
5030 return OK;
5031}
5032
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005033# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034/*
5035 * Given the name of the "icon=" argument, try finding the bitmap file for the
5036 * icon. If it is an absolute path name, use it as it is. Otherwise append
5037 * "ext" and search for it in 'runtimepath'.
5038 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5039 * contains "name".
5040 */
5041 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005042gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044 char_u buf[MAXPATHL + 1];
5045
5046 expand_env(name, buffer, MAXPATHL);
5047 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5048 STRCPY(buffer, buf);
5049}
5050# endif
5051#endif
5052
Bram Moolenaar9372a112005-12-06 19:59:18 +00005053#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005055display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 char_u *p;
5058
5059 if (isatty(2))
5060 fflush(stderr);
5061 else if (error_ga.ga_data != NULL)
5062 {
5063 /* avoid putting up a message box with blanks only */
5064 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5065 if (!isspace(*p))
5066 {
5067 /* Truncate a very long message, it will go off-screen. */
5068 if (STRLEN(p) > 2000)
5069 STRCPY(p + 2000 - 14, "...(truncated)");
5070 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005071 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073 }
5074 ga_clear(&error_ga);
5075 }
5076}
5077#endif
5078
5079#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5080/*
5081 * Return TRUE if still starting up and there is no place to enter text.
5082 * For GTK and X11 we check if stderr is not a tty, which means we were
5083 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5084 * allow typing on stdin.
5085 */
5086 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005087no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 return ((!gui.in_use || gui.starting)
5090# ifndef NO_CONSOLE
5091 && !isatty(0) && !isatty(2)
5092# endif
5093 );
5094}
5095#endif
5096
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005097#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005098 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005099 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
5101 * Update the current window and the screen.
5102 */
5103 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005104gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005106#ifdef FEAT_CONCEAL
5107 linenr_T conceal_old_cursor_line = 0;
5108 linenr_T conceal_new_cursor_line = 0;
5109 int conceal_update_lines = FALSE;
5110#endif
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 update_topline();
5113 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005114
5115#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005116 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005117 if (!finish_op && (
5118# ifdef FEAT_AUTOCMD
5119 has_cursormoved()
5120# endif
5121# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5122 ||
5123# endif
5124# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005125 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005126# endif
5127 )
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005128 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005129 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005130# ifdef FEAT_AUTOCMD
5131 if (has_cursormoved())
5132 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5133# endif
5134# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005135 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005136 {
5137 conceal_old_cursor_line = last_cursormoved.lnum;
5138 conceal_new_cursor_line = curwin->w_cursor.lnum;
5139 conceal_update_lines = TRUE;
5140 }
5141# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005142 last_cursormoved = curwin->w_cursor;
5143 }
5144#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 update_screen(0); /* may need to update the screen */
5147 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005148# if defined(FEAT_CONCEAL)
5149 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005150 && (conceal_old_cursor_line != conceal_new_cursor_line
5151 || conceal_cursor_line(curwin)
5152 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005153 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005154 if (conceal_old_cursor_line != conceal_new_cursor_line)
5155 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005156 update_single_line(curwin, conceal_new_cursor_line);
5157 curwin->w_valid &= ~VALID_CROW;
5158 }
5159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 out_flush(); /* make sure output has been written */
5161 gui_update_cursor(TRUE, FALSE);
5162 gui_mch_flush();
5163}
5164#endif
5165
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005166#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005167static void concat_esc(garray_T *gap, char_u *text, int what);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169/*
5170 * Get the text to use in a find/replace dialog. Uses the last search pattern
5171 * if the argument is empty.
5172 * Returns an allocated string.
5173 */
5174 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005175get_find_dialog_text(
5176 char_u *arg,
5177 int *wwordp, /* return: TRUE if \< \> found */
5178 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 char_u *text;
5181
5182 if (*arg == NUL)
5183 text = last_search_pat();
5184 else
5185 text = arg;
5186 if (text != NULL)
5187 {
5188 text = vim_strsave(text);
5189 if (text != NULL)
5190 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005191 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 int i;
5193
5194 /* Remove "\V" */
5195 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5196 {
5197 mch_memmove(text, text + 2, (size_t)(len - 1));
5198 len -= 2;
5199 }
5200
5201 /* Recognize "\c" and "\C" and remove. */
5202 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5203 {
5204 *mcasep = (text[1] == 'C');
5205 mch_memmove(text, text + 2, (size_t)(len - 1));
5206 len -= 2;
5207 }
5208
5209 /* Recognize "\<text\>" and remove. */
5210 if (len >= 4
5211 && STRNCMP(text, "\\<", 2) == 0
5212 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5213 {
5214 *wwordp = TRUE;
5215 mch_memmove(text, text + 2, (size_t)(len - 4));
5216 text[len - 4] = NUL;
5217 }
5218
5219 /* Recognize "\/" or "\?" and remove. */
5220 for (i = 0; i + 1 < len; ++i)
5221 if (text[i] == '\\' && (text[i + 1] == '/'
5222 || text[i + 1] == '?'))
5223 {
5224 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5225 --len;
5226 }
5227 }
5228 }
5229 return text;
5230}
5231
5232/*
5233 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5234 */
5235 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005236concat_esc(garray_T *gap, char_u *text, int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 while (*text != NUL)
5239 {
5240#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005241 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (l > 1)
5244 {
5245 while (--l >= 0)
5246 ga_append(gap, *text++);
5247 continue;
5248 }
5249#endif
5250 if (*text == what)
5251 ga_append(gap, '\\');
5252 ga_append(gap, *text);
5253 ++text;
5254 }
5255}
5256
5257/*
5258 * Handle the press of a button in the find-replace dialog.
5259 * Return TRUE when something was added to the input buffer.
5260 */
5261 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005262gui_do_findrepl(
5263 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5264 char_u *find_text,
5265 char_u *repl_text,
5266 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 garray_T ga;
5269 int i;
5270 int type = (flags & FRD_TYPE_MASK);
5271 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005272 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005273 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005274 static int busy = FALSE;
5275
5276 /* When the screen is being updated we should not change buffers and
5277 * windows structures, it may cause freed memory to be used. Also don't
5278 * do this recursively (pressing "Find" quickly several times. */
5279 if (updating_screen || busy)
5280 return FALSE;
5281
5282 /* refuse replace when text cannot be changed */
5283 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5284 return FALSE;
5285
5286 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005289 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ga_concat(&ga, (char_u *)"%s/");
5291
5292 ga_concat(&ga, (char_u *)"\\V");
5293 if (flags & FRD_MATCH_CASE)
5294 ga_concat(&ga, (char_u *)"\\C");
5295 else
5296 ga_concat(&ga, (char_u *)"\\c");
5297 if (flags & FRD_WHOLE_WORD)
5298 ga_concat(&ga, (char_u *)"\\<");
5299 if (type == FRD_REPLACEALL || down)
5300 concat_esc(&ga, find_text, '/'); /* escape slashes */
5301 else
5302 concat_esc(&ga, find_text, '?'); /* escape '?' */
5303 if (flags & FRD_WHOLE_WORD)
5304 ga_concat(&ga, (char_u *)"\\>");
5305
5306 if (type == FRD_REPLACEALL)
5307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005309 /* escape / and \ */
5310 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5311 if (p != NULL)
5312 ga_concat(&ga, p);
5313 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005315 }
5316 ga_append(&ga, NUL);
5317
5318 if (type == FRD_REPLACE)
5319 {
5320 /* Do the replacement when the text at the cursor matches. Thus no
5321 * replacement is done if the cursor was moved! */
5322 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5323 regmatch.rm_ic = 0;
5324 if (regmatch.regprog != NULL)
5325 {
5326 p = ml_get_cursor();
5327 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5328 && regmatch.startp[0] == p)
5329 {
5330 /* Clear the command line to remove any old "No match"
5331 * error. */
5332 msg_end_prompt();
5333
5334 if (u_save_cursor() == OK)
5335 {
5336 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005337 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005338
5339 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005340 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005341 ins_str(repl_text);
5342 }
5343 }
5344 else
5345 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005346 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005347 }
5348 }
5349
5350 if (type == FRD_REPLACEALL)
5351 {
5352 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005353 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 do_cmdline_cmd(ga.ga_data);
5355 }
5356 else
5357 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005358 int searchflags = SEARCH_MSG + SEARCH_MARK;
5359
5360 /* Search for the next match.
5361 * Don't skip text under cursor for single replace. */
5362 if (type == FRD_REPLACE)
5363 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 i = msg_scroll;
Bram Moolenaarcde88542015-08-11 19:14:00 +02005365 (void)do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005366 searchflags, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 msg_scroll = i; /* don't let an error message set msg_scroll */
5368 }
5369
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005370 /* Don't want to pass did_emsg to other code, it may cause disabling
5371 * syntax HL if we were busy redrawing. */
5372 did_emsg = save_did_emsg;
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (State & (NORMAL | INSERT))
5375 {
5376 gui_update_screen(); /* update the screen */
5377 msg_didout = 0; /* overwrite any message */
5378 need_wait_return = FALSE; /* don't wait for return */
5379 }
5380
5381 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005382 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 return (ga.ga_len > 0);
5384}
5385
5386#endif
5387
5388#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5389 || defined(FEAT_GUI_MSWIN) \
5390 || defined(FEAT_GUI_MAC) \
5391 || defined(PROTO)
5392
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005393static void gui_wingoto_xy(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395/*
5396 * Jump to the window at specified point (x, y).
5397 */
5398 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005399gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 int row = Y_2_ROW(y);
5402 int col = X_2_COL(x);
5403 win_T *wp;
5404
5405 if (row >= 0 && col >= 0)
5406 {
5407 wp = mouse_find_win(&row, &col);
5408 if (wp != NULL && wp != curwin)
5409 win_goto(wp);
5410 }
5411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413/*
5414 * Process file drop. Mouse cursor position, key modifiers, name of files
5415 * and count of files are given. Argument "fnames[count]" has full pathnames
5416 * of dropped files, they will be freed in this function, and caller can't use
5417 * fnames after call this function.
5418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005420gui_handle_drop(
5421 int x UNUSED,
5422 int y UNUSED,
5423 int_u modifiers,
5424 char_u **fnames,
5425 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427 int i;
5428 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005429 static int entered = FALSE;
5430
5431 /*
5432 * This function is called by event handlers. Just in case we get a
5433 * second event before the first one is handled, ignore the second one.
5434 * Not sure if this can ever happen, just in case.
5435 */
5436 if (entered)
5437 return;
5438 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440 /*
5441 * When the cursor is at the command line, add the file names to the
5442 * command line, don't edit the files.
5443 */
5444 if (State & CMDLINE)
5445 {
5446 shorten_filenames(fnames, count);
5447 for (i = 0; i < count; ++i)
5448 {
5449 if (fnames[i] != NULL)
5450 {
5451 if (i > 0)
5452 add_to_input_buf((char_u*)" ", 1);
5453
5454 /* We don't know what command is used thus we can't be sure
5455 * about which characters need to be escaped. Only escape the
5456 * most common ones. */
5457# ifdef BACKSLASH_IN_FILENAME
5458 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5459# else
5460 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5461# endif
5462 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005463 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 vim_free(p);
5465 vim_free(fnames[i]);
5466 }
5467 }
5468 vim_free(fnames);
5469 }
5470 else
5471 {
5472 /* Go to the window under mouse cursor, then shorten given "fnames" by
5473 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 shorten_filenames(fnames, count);
5476
5477 /* If Shift held down, remember the first item. */
5478 if ((modifiers & MOUSE_SHIFT) != 0)
5479 p = vim_strsave(fnames[0]);
5480 else
5481 p = NULL;
5482
5483 /* Handle the drop, :edit or :split to get to the file. This also
5484 * frees fnames[]. Skip this if there is only one item it's a
5485 * directory and Shift is held down. */
5486 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5487 && mch_isdir(fnames[0]))
5488 {
5489 vim_free(fnames[0]);
5490 vim_free(fnames);
5491 }
5492 else
5493 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5494
5495 /* If Shift held down, change to first file's directory. If the first
5496 * item is a directory, change to that directory (and let the explorer
5497 * plugin show the contents). */
5498 if (p != NULL)
5499 {
5500 if (mch_isdir(p))
5501 {
5502 if (mch_chdir((char *)p) == 0)
5503 shorten_fnames(TRUE);
5504 }
5505 else if (vim_chdirfile(p) == OK)
5506 shorten_fnames(TRUE);
5507 vim_free(p);
5508 }
5509
5510 /* Update the screen display */
5511 update_screen(NOT_VALID);
5512# ifdef FEAT_MENU
5513 gui_update_menus(0);
5514# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005515#ifdef FEAT_TITLE
5516 maketitle();
5517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 setcursor();
5519 out_flush();
5520 gui_update_cursor(FALSE, FALSE);
5521 gui_mch_flush();
5522 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005523
5524 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525}
5526#endif