blob: 39968954a8734abd3e98df33ba446afa25aea53a [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 Moolenaar13505972019-01-24 15:04:48 +010016#if !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010020static void gui_outstr(char_u *, int);
21static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010022static void gui_delete_lines(int row, int count);
23static void gui_insert_lines(int row, int count);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000024#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000026#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010027static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010028static void gui_update_horiz_scrollbar(int);
29static void gui_set_fg_color(char_u *name);
30static void gui_set_bg_color(char_u *name);
31static win_T *xy2win(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020033#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020035
Bram Moolenaard25c16e2016-01-29 22:13:30 +010036static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020037
38/* Return values for gui_read_child_pipe */
39enum {
40 GUI_CHILD_IO_ERROR,
41 GUI_CHILD_OK,
42 GUI_CHILD_FAILED
43};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020044#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020045
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020047
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int can_update_cursor = TRUE; /* can display the cursor */
Bram Moolenaara338adc2018-01-31 20:51:47 +010049static int disable_flush = 0; /* If > 0, gui_mch_flush() is disabled. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
51/*
52 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
53 * this makes the thumb indicate the part of the text that is shown. Motif
54 * can't do this.
55 */
56#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
57# define SCROLL_PAST_END
58#endif
59
60/*
61 * gui_start -- Called when user wants to start the GUI.
62 *
63 * Careful: This function can be called recursively when there is a ":gui"
64 * command in the .gvimrc file. Only the first call should fork, not the
65 * recursive call.
66 */
67 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +010068gui_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069{
70 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 static int recursive = 0;
72
73 old_term = vim_strsave(T_NAME);
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 settmode(TMODE_COOK); /* stop RAW mode */
76 if (full_screen)
77 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 full_screen = FALSE;
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 ++recursive;
81
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020082#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020083 /*
84 * Quit the current process and continue in the child.
85 * Makes "gvim file" disconnect from the shell it was started in.
86 * Don't do this when Vim was started with "-f" or the 'f' flag is present
87 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020088 * Don't do this when there is a running job, we can only get the status
89 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020091 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
92# ifdef FEAT_JOB_CHANNEL
93 && !job_any_running()
94# endif
95 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020096 {
97 gui_do_fork();
98 }
99 else
100#endif
101 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200102#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200103 /* If there is 'f' in 'guioptions' and specify -g argument,
104 * gui_mch_init_check() was not called yet. */
105 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100106 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200107#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200108 gui_attempt_start();
109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111 if (!gui.in_use) /* failed to start GUI */
112 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200113 /* Back to old term settings
114 *
115 * FIXME: If we got here because a child process failed and flagged to
116 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
117 * hit an X11 I/O error and do a longjmp(), leaving recursive
118 * permanently set to 1. This is probably not as big a problem as it
119 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
120 * return "OK" unconditionally, so it would be very difficult to
121 * actually hit this case.
122 */
123 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 settmode(TMODE_RAW); /* restart RAW mode */
125#ifdef FEAT_TITLE
126 set_title_defaults(); /* set 'title' and 'icon' again */
127#endif
128 }
129
130 vim_free(old_term);
131
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200132 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
133 * the GUIFailed event. */
134 gui_mch_update();
135 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
136 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200137 --recursive;
138}
139
140/*
141 * Set_termname() will call gui_init() to start the GUI.
142 * Set the "starting" flag, to indicate that the GUI will start.
143 *
144 * We don't want to open the GUI shell until after we've read .gvimrc,
145 * otherwise we don't know what font we will use, and hence we don't know
146 * what size the shell should be. So if there are errors in the .gvimrc
147 * file, they will have to go to the terminal: Set full_screen to FALSE.
148 * full_screen will be set to TRUE again by a successful termcapinit().
149 */
150 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100151gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200152{
153 static int recursive = 0;
154
155 ++recursive;
156 gui.starting = TRUE;
157
158#ifdef FEAT_GUI_GTK
159 gui.event_time = GDK_CURRENT_TIME;
160#endif
161
162 termcapinit((char_u *)"builtin_gui");
163 gui.starting = recursive - 1;
164
Bram Moolenaar9372a112005-12-06 19:59:18 +0000165#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {
168# ifdef FEAT_EVAL
169 Window x11_window;
170 Display *x11_display;
171
172 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
173 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
174# endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 /* Display error messages in a dialog now. */
177 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 --recursive;
181}
182
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200183#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200184
185/* for waitpid() */
186# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
187# include <sys/wait.h>
188# endif
189
190/*
191 * Create a new process, by forking. In the child, start the GUI, and in
192 * the parent, exit.
193 *
194 * If something goes wrong, this will return with gui.in_use still set
195 * to FALSE, in which case the caller should continue execution without
196 * the GUI.
197 *
198 * If the child fails to start the GUI, then the child will exit and the
199 * parent will return. If the child succeeds, then the parent will exit
200 * and the child will return.
201 */
202 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100203gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200204{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200205 int pipefd[2]; /* pipe between parent and child */
206 int pipe_error;
207 int status;
208 int exit_status;
209 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200210
211 /* Setup a pipe between the child and the parent, so that the parent
212 * knows when the child has done the setsid() call and is allowed to
213 * exit. */
214 pipe_error = (pipe(pipefd) < 0);
215 pid = fork();
216 if (pid < 0) /* Fork error */
217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200219 return;
220 }
221 else if (pid > 0) /* Parent */
222 {
223 /* Give the child some time to do the setsid(), otherwise the
224 * exit() may kill the child too (when starting gvim from inside a
225 * gvim). */
226 if (!pipe_error)
227 {
228 /* The read returns when the child closes the pipe (or when
229 * the child dies for some reason). */
230 close(pipefd[1]);
231 status = gui_read_child_pipe(pipefd[0]);
232 if (status == GUI_CHILD_FAILED)
233 {
234 /* The child failed to start the GUI, so the caller must
235 * continue. There may be more error information written
236 * to stderr by the child. */
237# ifdef __NeXT__
238 wait4(pid, &exit_status, 0, (struct rusage *)0);
239# else
240 waitpid(pid, &exit_status, 0);
241# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200243 return;
244 }
245 else if (status == GUI_CHILD_IO_ERROR)
246 {
247 pipe_error = TRUE;
248 }
249 /* else GUI_CHILD_OK: parent exit */
250 }
251
252 if (pipe_error)
253 ui_delay(300L, TRUE);
254
255 /* When swapping screens we may need to go to the next line, e.g.,
256 * after a hit-enter prompt and using ":gui". */
257 if (newline_on_exit)
258 mch_errmsg("\r\n");
259
260 /*
261 * The parent must skip the normal exit() processing, the child
262 * will do it. For example, GTK messes up signals when exiting.
263 */
264 _exit(0);
265 }
266 /* Child */
267
Bram Moolenaar29695702012-05-18 17:03:18 +0200268#ifdef FEAT_GUI_GTK
269 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
270 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100271 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200272#endif
273
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200274# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
275 /*
276 * Change our process group. On some systems/shells a CTRL-C in the
277 * shell where Vim was started would otherwise kill gvim!
278 */
279# if defined(HAVE_SETSID)
280 (void)setsid();
281# else
282 (void)setpgid(0, 0);
283# endif
284# endif
285 if (!pipe_error)
286 close(pipefd[0]);
287
288# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
289 /* Tell the session manager our new PID */
290 gui_mch_forked();
291# endif
292
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200293 /* Try to start the GUI */
294 gui_attempt_start();
295
296 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200297 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200298 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200299 if (gui.in_use)
300 write_eintr(pipefd[1], "ok", 3);
301 else
302 write_eintr(pipefd[1], "fail", 5);
303 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200304 }
305
306 /* If we failed to start the GUI, exit now. */
307 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100308 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200309}
310
311/*
312 * Read from a pipe assumed to be connected to the child process (this
313 * function is called from the parent).
314 * Return GUI_CHILD_OK if the child successfully started the GUI,
315 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
316 * some other error.
317 *
318 * The file descriptor will be closed before the function returns.
319 */
320 static int
321gui_read_child_pipe(int fd)
322{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200323 long bytes_read;
324#define READ_BUFFER_SIZE 10
325 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200326
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
328#undef READ_BUFFER_SIZE
329 close(fd);
330 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200332 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200333 if (strcmp(buffer, "ok") == 0)
334 return GUI_CHILD_OK;
335 return GUI_CHILD_FAILED;
336}
337
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200338#endif /* GUI_MAY_FORK */
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340/*
341 * Call this when vim starts up, whether or not the GUI is started
342 */
343 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100344gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346 gui.in_use = FALSE; /* No GUI yet (maybe later) */
347 gui.starting = FALSE; /* No GUI yet (maybe later) */
348 gui_mch_prepare(argc, argv);
349}
350
351/*
352 * Try initializing the GUI and check if it can be started.
353 * Used from main() to check early if "vim -g" can start the GUI.
354 * Used from gui_init() to prepare for starting the GUI.
355 * Returns FAIL or OK.
356 */
357 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100358gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360 static int result = MAYBE;
361
362 if (result != MAYBE)
363 {
364 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 return result;
367 }
368
369 gui.shell_created = FALSE;
370 gui.dying = FALSE;
371 gui.in_focus = TRUE; /* so the guicursor setting works */
372 gui.dragged_sb = SBAR_NONE;
373 gui.dragged_wp = NULL;
374 gui.pointer_hidden = FALSE;
375 gui.col = 0;
376 gui.row = 0;
377 gui.num_cols = Columns;
378 gui.num_rows = Rows;
379
380 gui.cursor_is_valid = FALSE;
381 gui.scroll_region_top = 0;
382 gui.scroll_region_bot = Rows - 1;
383 gui.scroll_region_left = 0;
384 gui.scroll_region_right = Columns - 1;
385 gui.highlight_mask = HL_NORMAL;
386 gui.char_width = 1;
387 gui.char_height = 1;
388 gui.char_ascent = 0;
389 gui.border_width = 0;
390
391 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200392#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 gui.bold_font = NOFONT;
394 gui.ital_font = NOFONT;
395 gui.boldital_font = NOFONT;
396# ifdef FEAT_XFONTSET
397 gui.fontset = NOFONTSET;
398# endif
399#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200400 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100401#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200402 gui.wide_bold_font = NOFONT;
403 gui.wide_ital_font = NOFONT;
404 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200408# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# ifdef FONTSET_ALWAYS
410 gui.menu_fontset = NOFONTSET;
411# else
412 gui.menu_font = NOFONT;
413# endif
414# endif
415 gui.menu_is_active = TRUE; /* default: include menu */
416# ifndef FEAT_GUI_GTK
417 gui.menu_height = MENU_DEFAULT_HEIGHT;
418 gui.menu_width = 0;
419# endif
420#endif
421#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
422 gui.toolbar_height = 0;
423#endif
424#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
425 gui.footer_height = 0;
426#endif
427#ifdef FEAT_BEVAL_TIP
428 gui.tooltip_fontset = NOFONTSET;
429#endif
430
431 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
432 gui.prev_wrap = -1;
433
434#ifdef ALWAYS_USE_GUI
435 result = OK;
436#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200437# ifdef FEAT_GUI_GTK
438 /*
439 * Note: Don't call gtk_init_check() before fork, it will be called after
440 * the fork. When calling it before fork, it make vim hang for a while.
441 * See gui_do_fork().
442 * Use a simpler check if the GUI window can probably be opened.
443 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200444 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200445# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448#endif
449 return result;
450}
451
452/*
453 * This is the call which starts the GUI.
454 */
455 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100456gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 win_T *wp;
459 static int recursive = 0;
460
461 /*
462 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
463 * function will then be executed at the first call, the rest by the
464 * recursive call. This allow the shell to be opened halfway reading a
465 * gvimrc file.
466 */
467 if (!recursive)
468 {
469 ++recursive;
470
471 clip_init(TRUE);
472
473 /* If can't initialize, don't try doing the rest */
474 if (gui_init_check() == FAIL)
475 {
476 --recursive;
477 clip_init(FALSE);
478 return;
479 }
480
481 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000482 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
483 * breaks the Paste toolbar button.
484 */
485 set_option_value((char_u *)"paste", 0L, NULL, 0);
486
487 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 * Set up system-wide default menus.
489 */
490#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
491 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
492 {
493 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000494 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 sys_menu = FALSE;
496 }
497#endif
498
499 /*
500 * Switch on the mouse by default, unless the user changed it already.
501 * This can then be changed in the .gvimrc.
502 */
503 if (!option_was_set((char_u *)"mouse"))
504 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000505 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
507 /*
508 * If -U option given, use only the initializations from that file and
509 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
510 */
511 if (use_gvimrc != NULL)
512 {
513 if (STRCMP(use_gvimrc, "NONE") != 0
514 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000515 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100516 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
520 /*
521 * Get system wide defaults for gvim, only when file name defined.
522 */
523#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000524 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526
527 /*
528 * Try to read GUI initialization commands from the following
529 * places:
530 * - environment variable GVIMINIT
531 * - the user gvimrc file (~/.gvimrc)
532 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
533 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
534 * The first that exists is used, the rest is ignored.
535 */
536 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000537 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
538 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000540 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
541 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200543#ifdef USR_GVIMRC_FILE3
544 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
545 DOSO_GVIMRC) == FAIL
546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 )
548 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200549#ifdef USR_GVIMRC_FILE4
550 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
552 }
553
554 /*
555 * Read initialization commands from ".gvimrc" in current
556 * directory. This is only done if the 'exrc' option is set.
557 * Because of security reasons we disallow shell and write
558 * commands now, except for unix if the file is owned by the user
559 * or 'secure' option has been reset in environment of global
560 * ".gvimrc".
561 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
562 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
563 */
564 if (p_exrc)
565 {
566#ifdef UNIX
567 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200568 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
570 /* if ".gvimrc" file is not owned by user, set 'secure'
571 * mode */
572 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
573 secure = p_secure;
574 }
575#else
576 secure = p_secure;
577#endif
578
579 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
580 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
581#ifdef SYS_GVIMRC_FILE
582 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
583 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
584#endif
585#ifdef USR_GVIMRC_FILE2
586 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
587 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
588#endif
589#ifdef USR_GVIMRC_FILE3
590 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
591 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
592#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200593#ifdef USR_GVIMRC_FILE4
594 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
595 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000598 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600 if (secure == 2)
601 need_wait_return = TRUE;
602 secure = 0;
603 }
604 }
605
606 if (need_wait_return || msg_didany)
607 wait_return(TRUE);
608
609 --recursive;
610 }
611
612 /* If recursive call opened the shell, return here from the first call */
613 if (gui.in_use)
614 return;
615
616 /*
617 * Create the GUI shell.
618 */
619 gui.in_use = TRUE; /* Must be set after menus have been set up */
620 if (gui_mch_init() == FAIL)
621 goto error;
622
623 /* Avoid a delay for an error message that was printed in the terminal
624 * where Vim was started. */
625 emsg_on_display = FALSE;
626 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100627 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 need_wait_return = FALSE;
629 msg_didany = FALSE;
630
631 /*
632 * Check validity of any generic resources that may have been loaded.
633 */
634 if (gui.border_width < 0)
635 gui.border_width = 0;
636
637 /*
638 * Set up the fonts. First use a font specified with "-fn" or "-font".
639 */
640 if (font_argument != NULL)
641 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
642 if (
643#ifdef FEAT_XFONTSET
644 (*p_guifontset == NUL
645 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
646#endif
647 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
648 : p_guifont, FALSE) == FAIL)
649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100650 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 goto error2;
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100654 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656 gui.num_cols = Columns;
657 gui.num_rows = Rows;
658 gui_reset_scroll_region();
659
660 /* Create initial scrollbars */
661 FOR_ALL_WINDOWS(wp)
662 {
663 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
664 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
665 }
666 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
667
668#ifdef FEAT_MENU
669 gui_create_initial_menus(root_menu);
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_SIGN_ICONS
672 sign_gui_started();
673#endif
674
675 /* Configure the desired menu and scrollbars */
676 gui_init_which_components(NULL);
677
678 /* All components of the GUI have been created now */
679 gui.shell_created = TRUE;
680
681#ifndef FEAT_GUI_GTK
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100682 // Set the shell size, adjusted for the screen size. For GTK this only
683 // works after the shell has been opened, thus it is further down.
684 // For MS-Windows pass FALSE for "mustset" to make --windowid work.
685 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#endif
687#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
688 /* Need to set the size of the menubar after all the menus have been
689 * created. */
690 gui_mch_compute_menu_height((Widget)0);
691#endif
692
693 /*
694 * Actually open the GUI shell.
695 */
696 if (gui_mch_open() != FAIL)
697 {
698#ifdef FEAT_TITLE
699 maketitle();
700 resettitle();
701#endif
702 init_gui_options();
703#ifdef FEAT_ARABIC
704 /* Our GUI can't do bidi. */
705 p_tbidi = FALSE;
706#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000707#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 /* Give GTK+ a chance to put all widget's into place. */
709 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000710
711# ifdef FEAT_MENU
712 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
713 * It was still there to make F10 work. */
714 if (vim_strchr(p_go, GO_MENUS) == NULL)
715 {
716 --gui.starting;
717 gui_mch_enable_menu(FALSE);
718 ++gui.starting;
719 gui_mch_update();
720 }
721# endif
722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 /* Now make sure the shell fits on the screen. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100724 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000726 /* When 'lines' was set while starting up the topframe may have to be
727 * resized. */
728 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000729
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100730#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000731 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100732 * 'ballooneval' is off. */
733 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200734 {
735# ifdef FEAT_VARTABS
736 vim_free(balloonEval->vts);
737# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100738 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200739 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100740 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000741# ifdef FEAT_GUI_GTK
742 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
743 &general_beval_cb, NULL);
744# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000745# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000746 {
747 extern Widget textArea;
748 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000749 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000750 }
751# else
752# ifdef FEAT_GUI_W32
753 balloonEval = gui_mch_create_beval_area(NULL, NULL,
754 &general_beval_cb, NULL);
755# endif
756# endif
757# endif
758 if (!p_beval)
759 gui_mch_disable_beval_area(balloonEval);
760#endif
761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
763 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000766 /* When 'cmdheight' was set during startup it may not have taken
767 * effect yet. */
768 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000769 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 return;
772 }
773
774error2:
775#ifdef FEAT_GUI_X11
776 /* undo gui_mch_init() */
777 gui_mch_uninit();
778#endif
779
780error:
781 gui.in_use = FALSE;
782 clip_init(FALSE);
783}
784
785
786 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100787gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 /* don't free the fonts, it leads to a BUS error
790 * richard@whitequeen.com Jul 99 */
791 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 gui.in_use = FALSE;
793 gui_mch_exit(rc);
794}
795
Bram Moolenaar9372a112005-12-06 19:59:18 +0000796#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000798# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799/*
800 * Called when the GUI shell is closed by the user. If there are no changed
801 * files Vim exits, otherwise there will be a dialog to ask the user what to
802 * do.
803 * When this function returns, Vim should NOT exit!
804 */
805 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100806gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 cmdmod_T save_cmdmod;
809
810 save_cmdmod = cmdmod;
811
812 /* Only exit when there are no changed files */
813 exiting = TRUE;
814# ifdef FEAT_BROWSE
815 cmdmod.browse = TRUE;
816# endif
817# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
818 cmdmod.confirm = TRUE;
819# endif
820 /* If there are changed buffers, present the user with a dialog if
821 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100822 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 getout(0);
824
825 exiting = FALSE;
826 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000827 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829#endif
830
831/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200832 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * first font name that works is used. If none is found, use the default
834 * font.
835 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
836 * Return OK when able to set the font. When it failed FAIL is returned and
837 * the fonts are unchanged.
838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100840gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
842#define FONTLEN 320
843 char_u font_name[FONTLEN];
844 int font_list_empty = FALSE;
845 int ret = FAIL;
846
847 if (!gui.in_use)
848 return FAIL;
849
850 font_name[0] = NUL;
851 if (*font_list == NUL)
852 font_list_empty = TRUE;
853 else
854 {
855#ifdef FEAT_XFONTSET
856 /* When using a fontset, the whole list of fonts is one name. */
857 if (fontset)
858 ret = gui_mch_init_font(font_list, TRUE);
859 else
860#endif
861 while (*font_list != NUL)
862 {
863 /* Isolate one comma separated font name. */
864 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
865
866 /* Careful!!! The Win32 version of gui_mch_init_font(), when
867 * called with "*" will change p_guifont to the selected font
868 * name, which frees the old value. This makes font_list
869 * invalid. Thus when OK is returned here, font_list must no
870 * longer be used! */
871 if (gui_mch_init_font(font_name, FALSE) == OK)
872 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100873#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 /* If it's a Unicode font, try setting 'guifontwide' to a
875 * similar double-width font. */
876 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
877 && strstr((char *)font_name, "10646") != NULL)
878 set_guifontwide(font_name);
879#endif
880 ret = OK;
881 break;
882 }
883 }
884 }
885
886 if (ret != OK
887 && STRCMP(font_list, "*") != 0
888 && (font_list_empty || gui.norm_font == NOFONT))
889 {
890 /*
891 * Couldn't load any font in 'font_list', keep the current font if
892 * there is one. If 'font_list' is empty, or if there is no current
893 * font, tell gui_mch_init_font() to try to find a font we can load.
894 */
895 ret = gui_mch_init_font(NULL, FALSE);
896 }
897
898 if (ret == OK)
899 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200900#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 /* Set normal font as current font */
902# ifdef FEAT_XFONTSET
903 if (gui.fontset != NOFONTSET)
904 gui_mch_set_fontset(gui.fontset);
905 else
906# endif
907 gui_mch_set_font(gui.norm_font);
908#endif
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100909 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911
912 return ret;
913}
914
Bram Moolenaar13505972019-01-24 15:04:48 +0100915#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916/*
917 * Try setting 'guifontwide' to a font twice as wide as "name".
918 */
919 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100920set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 int i = 0;
923 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
924 char_u *wp = NULL;
925 char_u *p;
926 GuiFont font;
927
928 wp = wide_name;
929 for (p = name; *p != NUL; ++p)
930 {
931 *wp++ = *p;
932 if (*p == '-')
933 {
934 ++i;
935 if (i == 6) /* font type: change "--" to "-*-" */
936 {
937 if (p[1] == '-')
938 *wp++ = '*';
939 }
940 else if (i == 12) /* found the width */
941 {
942 ++p;
943 i = getdigits(&p);
944 if (i != 0)
945 {
946 /* Double the width specification. */
947 sprintf((char *)wp, "%d%s", i * 2, p);
948 font = gui_mch_get_font(wide_name, FALSE);
949 if (font != NOFONT)
950 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000951 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 gui.wide_font = font;
953 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000954 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 }
957 break;
958 }
959 }
960 }
961}
Bram Moolenaar13505972019-01-24 15:04:48 +0100962#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964/*
965 * Get the font for 'guifontwide'.
966 * Return FAIL for an invalid font name.
967 */
968 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100969gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 GuiFont font = NOFONT;
972 char_u font_name[FONTLEN];
973 char_u *p;
974
975 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
976 return OK; /* Will give an error message later. */
977
978 if (p_guifontwide != NULL && *p_guifontwide != NUL)
979 {
980 for (p = p_guifontwide; *p != NUL; )
981 {
982 /* Isolate one comma separated font name. */
983 (void)copy_option_part(&p, font_name, FONTLEN, ",");
984 font = gui_mch_get_font(font_name, FALSE);
985 if (font != NOFONT)
986 break;
987 }
988 if (font == NOFONT)
989 return FAIL;
990 }
991
992 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +0100993#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
995 if (font != NOFONT && gui.norm_font != NOFONT
996 && pango_font_description_equal(font, gui.norm_font))
997 {
998 gui.wide_font = NOFONT;
999 gui_mch_free_font(font);
1000 }
1001 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001004#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001005 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001006#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001007 /*
1008 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1009 * support those fonts for 'guifontwide'.
1010 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 return OK;
1013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001016gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
1018 gui.row = row;
1019 gui.col = col;
1020}
1021
1022/*
1023 * gui_check_pos - check if the cursor is on the screen.
1024 */
1025 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001026gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 if (gui.row >= screen_Rows)
1029 gui.row = screen_Rows - 1;
1030 if (gui.col >= screen_Columns)
1031 gui.col = screen_Columns - 1;
1032 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1033 gui.cursor_is_valid = FALSE;
1034}
1035
1036/*
1037 * Redraw the cursor if necessary or when forced.
1038 * Careful: The contents of ScreenLines[] must match what is on the screen,
1039 * otherwise this goes wrong. May need to call out_flush() first.
1040 */
1041 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001042gui_update_cursor(
1043 int force, /* when TRUE, update even when not moved */
1044 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 int cur_width = 0;
1047 int cur_height = 0;
1048 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001049 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001051#ifdef FEAT_TERMINAL
1052 guicolor_T shape_fg = INVALCOLOR;
1053 guicolor_T shape_bg = INVALCOLOR;
1054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1056 int cattr; /* cursor attributes */
1057 int attr;
1058 attrentry_T *aep = NULL;
1059
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001060 /* Don't update the cursor when halfway busy scrolling or the screen size
1061 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1062 if (!can_update_cursor || screen_Columns != gui.num_cols
1063 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 return;
1065
1066 gui_check_pos();
1067 if (!gui.cursor_is_valid || force
1068 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1069 {
1070 gui_undraw_cursor();
1071 if (gui.row < 0)
1072 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001073#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1075 im_set_position(gui.row, gui.col);
1076#endif
1077 gui.cursor_row = gui.row;
1078 gui.cursor_col = gui.col;
1079
1080 /* Only write to the screen after ScreenLines[] has been initialized */
1081 if (!screen_cleared || ScreenLines == NULL)
1082 return;
1083
1084 /* Clear the selection if we are about to write over it */
1085 if (clear_selection)
1086 clip_may_clear_selection(gui.row, gui.row);
1087 /* Check that the cursor is inside the shell (resizing may have made
1088 * it invalid) */
1089 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1090 return;
1091
1092 gui.cursor_is_valid = TRUE;
1093
1094 /*
1095 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001096 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001098#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001099 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001100 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001102#endif
1103 shape = &shape_table[get_shape_idx(FALSE)];
1104 if (State & LANGMAP)
1105 id = shape->id_lm;
1106 else
1107 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 /* get the colors and attributes for the cursor. Default is inverted */
1110 cfg = INVALCOLOR;
1111 cbg = INVALCOLOR;
1112 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001113 gui_mch_set_blinking(shape->blinkwait,
1114 shape->blinkon,
1115 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001116 if (shape->blinkwait == 0 || shape->blinkon == 0
1117 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001118 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001119#ifdef FEAT_TERMINAL
1120 if (shape_bg != INVALCOLOR)
1121 {
1122 cattr = 0;
1123 cfg = shape_fg;
1124 cbg = shape_bg;
1125 }
1126 else
1127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (id > 0)
1129 {
1130 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001131#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
1133 static int iid;
1134 guicolor_T fg, bg;
1135
Bram Moolenaarc236c162008-07-13 17:41:49 +00001136 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001137# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001138 preedit_get_status()
1139# else
1140 im_get_status()
1141# endif
1142 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 iid = syn_name2id((char_u *)"CursorIM");
1145 if (iid > 0)
1146 {
1147 syn_id2colors(iid, &fg, &bg);
1148 if (bg != INVALCOLOR)
1149 cbg = bg;
1150 if (fg != INVALCOLOR)
1151 cfg = fg;
1152 }
1153 }
1154 }
1155#endif
1156 }
1157
1158 /*
1159 * Get the attributes for the character under the cursor.
1160 * When no cursor color was given, use the character color.
1161 */
1162 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1163 if (attr > HL_ALL)
1164 aep = syn_gui_attr2entry(attr);
1165 if (aep != NULL)
1166 {
1167 attr = aep->ae_attr;
1168 if (cfg == INVALCOLOR)
1169 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1170 : aep->ae_u.gui.fg_color);
1171 if (cbg == INVALCOLOR)
1172 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1173 : aep->ae_u.gui.bg_color);
1174 }
1175 if (cfg == INVALCOLOR)
1176 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1177 if (cbg == INVALCOLOR)
1178 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1179
1180#ifdef FEAT_XIM
1181 if (aep != NULL)
1182 {
1183 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1184 : aep->ae_u.gui.bg_color);
1185 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1186 : aep->ae_u.gui.fg_color);
1187 if (xim_bg_color == INVALCOLOR)
1188 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1189 : gui.back_pixel;
1190 if (xim_fg_color == INVALCOLOR)
1191 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1192 : gui.norm_pixel;
1193 }
1194 else
1195 {
1196 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1197 : gui.back_pixel;
1198 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1199 : gui.norm_pixel;
1200 }
1201#endif
1202
1203 attr &= ~HL_INVERSE;
1204 if (cattr & HL_INVERSE)
1205 {
1206 cc = cbg;
1207 cbg = cfg;
1208 cfg = cc;
1209 }
1210 cattr &= ~HL_INVERSE;
1211
1212 /*
1213 * When we don't have window focus, draw a hollow cursor.
1214 */
1215 if (!gui.in_focus)
1216 {
1217 gui_mch_draw_hollow_cursor(cbg);
1218 return;
1219 }
1220
1221 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001222 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef FEAT_HANGULIN
1224 || composing_hangul
1225#endif
1226 )
1227 {
1228 /*
1229 * Draw the text character with the cursor colors. Use the
1230 * character attributes plus the cursor attributes.
1231 */
1232 gui.highlight_mask = (cattr | attr);
1233#ifdef FEAT_HANGULIN
1234 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001235 {
1236 char_u *comp_buf;
1237 int comp_len;
1238
1239 comp_buf = hangul_composing_buffer_get(&comp_len);
1240 if (comp_buf)
1241 {
1242 (void)gui_outstr_nowrap(comp_buf, comp_len,
1243 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1244 cfg, cbg, 0);
1245 vim_free(comp_buf);
1246 }
1247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 else
1249#endif
1250 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1251 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1252 }
1253 else
1254 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001255#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 int col_off = FALSE;
1257#endif
1258 /*
1259 * First draw the partial cursor, then overwrite with the text
1260 * character, using a transparent background.
1261 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001262 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001265 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 else
1268 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001269 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 cur_width = gui.char_width;
1271 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001272 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1273 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 {
1275 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001276 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001278#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (CURSOR_BAR_RIGHT)
1280 {
1281 /* gui.col points to the left halve of the character but
1282 * the vertical line needs to be on the right halve.
1283 * A double-wide horizontal line is also drawn from the
1284 * right halve in gui_mch_draw_part_cursor(). */
1285 col_off = TRUE;
1286 ++gui.col;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001291#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (col_off)
1293 --gui.col;
1294#endif
1295
1296#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1297 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1298 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1299 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1300 (guicolor_T)0, (guicolor_T)0, 0);
1301#endif
1302 }
1303 gui.highlight_mask = old_hl_mask;
1304 }
1305}
1306
1307#if defined(FEAT_MENU) || defined(PROTO)
1308 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001309gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001311# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (gui.menu_is_active && gui.in_use)
1313 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1314# endif
1315}
1316#endif
1317
1318/*
1319 * Position the various GUI components (text area, menu). The vertical
1320 * scrollbars are NOT handled here. See gui_update_scrollbars().
1321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001323gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
1325 int text_area_x;
1326 int text_area_y;
1327 int text_area_width;
1328 int text_area_height;
1329
1330 /* avoid that moving components around generates events */
1331 ++hold_gui_events;
1332
1333 text_area_x = 0;
1334 if (gui.which_scrollbars[SBAR_LEFT])
1335 text_area_x += gui.scrollbar_width;
1336
1337 text_area_y = 0;
1338#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1339 gui.menu_width = total_width;
1340 if (gui.menu_is_active)
1341 text_area_y += gui.menu_height;
1342#endif
1343#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1344 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1345 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1346#endif
1347
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001348# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001349 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001350 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001351 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1355 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1356 {
1357# ifdef FEAT_GUI_ATHENA
1358 gui_mch_set_toolbar_pos(0, text_area_y,
1359 gui.menu_width, gui.toolbar_height);
1360# endif
1361 text_area_y += gui.toolbar_height;
1362 }
1363#endif
1364
1365 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1366 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1367
1368 gui_mch_set_text_area_pos(text_area_x,
1369 text_area_y,
1370 text_area_width,
1371 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001372#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 + xim_get_status_area_height()
1374#endif
1375 );
1376#ifdef FEAT_MENU
1377 gui_position_menu();
1378#endif
1379 if (gui.which_scrollbars[SBAR_BOTTOM])
1380 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1381 text_area_x,
1382 text_area_y + text_area_height,
1383 text_area_width,
1384 gui.scrollbar_height);
1385 gui.left_sbar_x = 0;
1386 gui.right_sbar_x = text_area_x + text_area_width;
1387
1388 --hold_gui_events;
1389}
1390
Bram Moolenaar02743632005-07-25 20:42:36 +00001391/*
1392 * Get the width of the widgets and decorations to the side of the text area.
1393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001395gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
1397 int base_width;
1398
1399 base_width = 2 * gui.border_offset;
1400 if (gui.which_scrollbars[SBAR_LEFT])
1401 base_width += gui.scrollbar_width;
1402 if (gui.which_scrollbars[SBAR_RIGHT])
1403 base_width += gui.scrollbar_width;
1404 return base_width;
1405}
1406
Bram Moolenaar02743632005-07-25 20:42:36 +00001407/*
1408 * Get the height of the widgets and decorations above and below the text area.
1409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001411gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 int base_height;
1414
1415 base_height = 2 * gui.border_offset;
1416 if (gui.which_scrollbars[SBAR_BOTTOM])
1417 base_height += gui.scrollbar_height;
1418#ifdef FEAT_GUI_GTK
1419 /* We can't take the sizes properly into account until anything is
1420 * realized. Therefore we recalculate all the values here just before
1421 * setting the size. (--mdcki) */
1422#else
1423# ifdef FEAT_MENU
1424 if (gui.menu_is_active)
1425 base_height += gui.menu_height;
1426# endif
1427# ifdef FEAT_TOOLBAR
1428 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1429# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1430 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1431# else
1432 base_height += gui.toolbar_height;
1433# endif
1434# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001435# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1436 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001437 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001438 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440# ifdef FEAT_FOOTER
1441 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1442 base_height += gui.footer_height;
1443# endif
1444# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1445 base_height += gui_mch_text_area_extra_height();
1446# endif
1447#endif
1448 return base_height;
1449}
1450
1451/*
1452 * Should be called after the GUI shell has been resized. Its arguments are
1453 * the new width and height of the shell in pixels.
1454 */
1455 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001456gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 static int busy = FALSE;
1459
1460 if (!gui.shell_created) /* ignore when still initializing */
1461 return;
1462
1463 /*
1464 * Can't resize the screen while it is being redrawn. Remember the new
1465 * size and handle it later.
1466 */
1467 if (updating_screen || busy)
1468 {
1469 new_pixel_width = pixel_width;
1470 new_pixel_height = pixel_height;
1471 return;
1472 }
1473
1474again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001475 new_pixel_width = 0;
1476 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 busy = TRUE;
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 /* Flush pending output before redrawing */
1480 out_flush();
1481
1482 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001483 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /*
1489 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1490 * at the last line here (why does it have to be one row too low?).
1491 */
1492 if (State == ASKMORE || State == CONFIRM)
1493 gui.row = gui.num_rows;
1494
1495 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1496 * the safe side. */
1497 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1498 || gui.num_rows != Rows || gui.num_cols != Columns)
1499 shell_resized();
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 gui_update_scrollbars(TRUE);
1502 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001503#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 xim_set_status_area();
1505#endif
1506
1507 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001508
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001509 /* We may have been called again while redrawing the screen.
1510 * Need to do it all again with the latest size then. But only if the size
1511 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (new_pixel_height)
1513 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001514 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1515 {
1516 new_pixel_width = 0;
1517 new_pixel_height = 0;
1518 }
1519 else
1520 {
1521 pixel_width = new_pixel_width;
1522 pixel_height = new_pixel_height;
1523 goto again;
1524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526}
1527
1528/*
1529 * Check if gui_resize_shell() must be called.
1530 */
1531 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001532gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 /* careful: gui_resize_shell() may postpone the resize again if we
1536 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001537 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538}
1539
1540 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001541gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
1543 Rows = gui.num_rows;
1544 Columns = gui.num_cols;
1545 return OK;
1546}
1547
1548/*
1549 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001550 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1551 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001552 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1553 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001556gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001557 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001558 int fit_to_display,
1559 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 int base_width;
1562 int base_height;
1563 int width;
1564 int height;
1565 int min_width;
1566 int min_height;
1567 int screen_w;
1568 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001569#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001570 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001571 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001572#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001573 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 if (!gui.shell_created)
1576 return;
1577
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001578#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /* If not setting to a user specified size and maximized, calculate the
1580 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001581 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1582 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 gui_mch_newfont();
1585 return;
1586 }
1587#endif
1588
1589 base_width = gui_get_base_width();
1590 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001591 if (fit_to_display)
1592 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001593 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001594
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001595 width = Columns * gui.char_width + base_width;
1596 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598 if (fit_to_display)
1599 {
1600 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001601 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 Columns = (screen_w - base_width) / gui.char_width;
1604 if (Columns < MIN_COLUMNS)
1605 Columns = MIN_COLUMNS;
1606 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001607#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001608 ++did_adjust;
1609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001611 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 Rows = (screen_h - base_height) / gui.char_height;
1614 check_shellsize();
1615 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001616#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001617 ++did_adjust;
1618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001620#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001621 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1622 && height + gui.char_height >= screen_h))
1623 /* don't unmaximize if at maximum size */
1624 un_maximize = FALSE;
1625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001627 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 gui.num_cols = Columns;
1629 gui.num_rows = Rows;
1630
1631 min_width = base_width + MIN_COLUMNS * gui.char_width;
1632 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001633 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001634
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001635#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001636 if (un_maximize)
1637 {
1638 /* If the window size is smaller than the screen unmaximize the
1639 * window, otherwise resizing won't work. */
1640 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1641 if ((width + gui.char_width < screen_w
1642 || height + gui.char_height * 2 < screen_h)
1643 && gui_mch_maximized())
1644 gui_mch_unmaximize();
1645 }
1646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001649 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001651 if (fit_to_display && x >= 0 && y >= 0)
1652 {
1653 /* Some window managers put the Vim window left of/above the screen.
1654 * Only change the position if it wasn't already negative before
1655 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 gui_mch_update();
1657 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1658 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1659 }
1660
1661 gui_position_components(width);
1662 gui_update_scrollbars(TRUE);
1663 gui_reset_scroll_region();
1664}
1665
1666/*
1667 * Called when Rows and/or Columns has changed.
1668 */
1669 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001670gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 gui_reset_scroll_region();
1673}
1674
1675/*
1676 * Make scroll region cover whole screen.
1677 */
1678 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001679gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681 gui.scroll_region_top = 0;
1682 gui.scroll_region_bot = gui.num_rows - 1;
1683 gui.scroll_region_left = 0;
1684 gui.scroll_region_right = gui.num_cols - 1;
1685}
1686
1687 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001688gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 if (mask > HL_ALL) /* highlight code */
1691 gui.highlight_mask = mask;
1692 else /* mask */
1693 gui.highlight_mask |= mask;
1694}
1695
1696 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001697gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 if (mask > HL_ALL) /* highlight code */
1700 gui.highlight_mask = HL_NORMAL;
1701 else /* mask */
1702 gui.highlight_mask &= ~mask;
1703}
1704
1705/*
1706 * Clear a rectangular region of the screen from text pos (row1, col1) to
1707 * (row2, col2) inclusive.
1708 */
1709 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001710gui_clear_block(
1711 int row1,
1712 int col1,
1713 int row2,
1714 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 /* Clear the selection if we are about to write over it */
1717 clip_may_clear_selection(row1, row2);
1718
1719 gui_mch_clear_block(row1, col1, row2, col2);
1720
1721 /* Invalidate cursor if it was in this block */
1722 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1723 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1724 gui.cursor_is_valid = FALSE;
1725}
1726
1727/*
1728 * Write code to update the cursor later. This avoids the need to flush the
1729 * output buffer before calling gui_update_cursor().
1730 */
1731 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001732gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001734 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
1736
1737 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001738gui_write(
1739 char_u *s,
1740 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 char_u *p;
1743 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 int force_scrollbar = FALSE;
1746 static win_T *old_curwin = NULL;
1747
1748/* #define DEBUG_GUI_WRITE */
1749#ifdef DEBUG_GUI_WRITE
1750 {
1751 int i;
1752 char_u *str;
1753
1754 printf("gui_write(%d):\n ", len);
1755 for (i = 0; i < len; i++)
1756 if (s[i] == ESC)
1757 {
1758 if (i != 0)
1759 printf("\n ");
1760 printf("<ESC>");
1761 }
1762 else
1763 {
1764 str = transchar_byte(s[i]);
1765 if (str[0] && str[1])
1766 printf("<%s>", (char *)str);
1767 else
1768 printf("%s", (char *)str);
1769 }
1770 printf("\n");
1771 }
1772#endif
1773 while (len)
1774 {
1775 if (s[0] == ESC && s[1] == '|')
1776 {
1777 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001778 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
1780 arg1 = getdigits(&p);
1781 if (p > s + len)
1782 break;
1783 if (*p == ';')
1784 {
1785 ++p;
1786 arg2 = getdigits(&p);
1787 if (p > s + len)
1788 break;
1789 }
1790 }
1791 switch (*p)
1792 {
1793 case 'C': /* Clear screen */
1794 clip_scroll_selection(9999);
1795 gui_mch_clear_all();
1796 gui.cursor_is_valid = FALSE;
1797 force_scrollbar = TRUE;
1798 break;
1799 case 'M': /* Move cursor */
1800 gui_set_cursor(arg1, arg2);
1801 break;
1802 case 's': /* force cursor (shape) update */
1803 force_cursor = TRUE;
1804 break;
1805 case 'R': /* Set scroll region */
1806 if (arg1 < arg2)
1807 {
1808 gui.scroll_region_top = arg1;
1809 gui.scroll_region_bot = arg2;
1810 }
1811 else
1812 {
1813 gui.scroll_region_top = arg2;
1814 gui.scroll_region_bot = arg1;
1815 }
1816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 case 'V': /* Set vertical scroll region */
1818 if (arg1 < arg2)
1819 {
1820 gui.scroll_region_left = arg1;
1821 gui.scroll_region_right = arg2;
1822 }
1823 else
1824 {
1825 gui.scroll_region_left = arg2;
1826 gui.scroll_region_right = arg1;
1827 }
1828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 case 'd': /* Delete line */
1830 gui_delete_lines(gui.row, 1);
1831 break;
1832 case 'D': /* Delete lines */
1833 gui_delete_lines(gui.row, arg1);
1834 break;
1835 case 'i': /* Insert line */
1836 gui_insert_lines(gui.row, 1);
1837 break;
1838 case 'I': /* Insert lines */
1839 gui_insert_lines(gui.row, arg1);
1840 break;
1841 case '$': /* Clear to end-of-line */
1842 gui_clear_block(gui.row, gui.col, gui.row,
1843 (int)Columns - 1);
1844 break;
1845 case 'h': /* Turn on highlighting */
1846 gui_start_highlight(arg1);
1847 break;
1848 case 'H': /* Turn off highlighting */
1849 gui_stop_highlight(arg1);
1850 break;
1851 case 'f': /* flash the window (visual bell) */
1852 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1853 break;
1854 default:
1855 p = s + 1; /* Skip the ESC */
1856 break;
1857 }
1858 len -= (int)(++p - s);
1859 s = p;
1860 }
1861 else if (
1862#ifdef EBCDIC
1863 CtrlChar(s[0]) != 0 /* Ctrl character */
1864#else
1865 s[0] < 0x20 /* Ctrl character */
1866#endif
1867#ifdef FEAT_SIGN_ICONS
1868 && s[0] != SIGN_BYTE
1869# ifdef FEAT_NETBEANS_INTG
1870 && s[0] != MULTISIGN_BYTE
1871# endif
1872#endif
1873 )
1874 {
1875 if (s[0] == '\n') /* NL */
1876 {
1877 gui.col = 0;
1878 if (gui.row < gui.scroll_region_bot)
1879 gui.row++;
1880 else
1881 gui_delete_lines(gui.scroll_region_top, 1);
1882 }
1883 else if (s[0] == '\r') /* CR */
1884 {
1885 gui.col = 0;
1886 }
1887 else if (s[0] == '\b') /* Backspace */
1888 {
1889 if (gui.col)
1890 --gui.col;
1891 }
1892 else if (s[0] == Ctrl_L) /* cursor-right */
1893 {
1894 ++gui.col;
1895 }
1896 else if (s[0] == Ctrl_G) /* Beep */
1897 {
1898 gui_mch_beep();
1899 }
1900 /* Other Ctrl character: shouldn't happen! */
1901
1902 --len; /* Skip this char */
1903 ++s;
1904 }
1905 else
1906 {
1907 p = s;
1908 while (len > 0 && (
1909#ifdef EBCDIC
1910 CtrlChar(*p) == 0
1911#else
1912 *p >= 0x20
1913#endif
1914#ifdef FEAT_SIGN_ICONS
1915 || *p == SIGN_BYTE
1916# ifdef FEAT_NETBEANS_INTG
1917 || *p == MULTISIGN_BYTE
1918# endif
1919#endif
1920 ))
1921 {
1922 len--;
1923 p++;
1924 }
1925 gui_outstr(s, (int)(p - s));
1926 s = p;
1927 }
1928 }
1929
1930 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1931 * set). */
1932 if (force_cursor)
1933 gui_update_cursor(TRUE, TRUE);
1934
1935 /* When switching to another window the dragging must have stopped.
1936 * Required for GTK, dragged_sb isn't reset. */
1937 if (old_curwin != curwin)
1938 gui.dragged_sb = SBAR_NONE;
1939
1940 /* Update the scrollbars after clearing the screen or when switched
1941 * to another window.
1942 * Update the horizontal scrollbar always, it's difficult to check all
1943 * situations where it might change. */
1944 if (force_scrollbar || old_curwin != curwin)
1945 gui_update_scrollbars(force_scrollbar);
1946 else
1947 gui_update_horiz_scrollbar(FALSE);
1948 old_curwin = curwin;
1949
1950 /*
1951 * We need to make sure this is cleared since Athena doesn't tell us when
1952 * he is done dragging. Do the same for GTK.
1953 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001954#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 gui.dragged_sb = SBAR_NONE;
1956#endif
1957
Bram Moolenaara338adc2018-01-31 20:51:47 +01001958 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959}
1960
1961/*
1962 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1963 * produces wrong results. Call gui_dont_update_cursor() before that code and
1964 * gui_can_update_cursor() afterwards.
1965 */
1966 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001967gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
1969 if (gui.in_use)
1970 {
1971 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001972 if (undraw)
1973 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 can_update_cursor = FALSE;
1975 }
1976}
1977
1978 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001979gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 can_update_cursor = TRUE;
1982 /* No need to update the cursor right now, there is always more output
1983 * after scrolling. */
1984}
1985
Bram Moolenaara338adc2018-01-31 20:51:47 +01001986/*
1987 * Disable issuing gui_mch_flush().
1988 */
1989 void
1990gui_disable_flush(void)
1991{
1992 ++disable_flush;
1993}
1994
1995/*
1996 * Enable issuing gui_mch_flush().
1997 */
1998 void
1999gui_enable_flush(void)
2000{
2001 --disable_flush;
2002}
2003
2004/*
2005 * Issue gui_mch_flush() if it is not disabled.
2006 */
2007 void
2008gui_may_flush(void)
2009{
2010 if (disable_flush == 0)
2011 gui_mch_flush();
2012}
2013
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002015gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 if (len == 0)
2021 return;
2022
2023 if (len < 0)
2024 len = (int)STRLEN(s);
2025
2026 while (len > 0)
2027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (has_mbyte)
2029 {
2030 /* Find out how many chars fit in the current line. */
2031 cells = 0;
2032 for (this_len = 0; this_len < len; )
2033 {
2034 cells += (*mb_ptr2cells)(s + this_len);
2035 if (gui.col + cells > Columns)
2036 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002037 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 if (this_len > len)
2040 this_len = len; /* don't include following composing char */
2041 }
2042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (gui.col + len > Columns)
2044 this_len = Columns - gui.col;
2045 else
2046 this_len = len;
2047
2048 (void)gui_outstr_nowrap(s, this_len,
2049 0, (guicolor_T)0, (guicolor_T)0, 0);
2050 s += this_len;
2051 len -= this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 /* fill up for a double-width char that doesn't fit. */
2053 if (len > 0 && gui.col < Columns)
2054 (void)gui_outstr_nowrap((char_u *)" ", 1,
2055 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 /* The cursor may wrap to the next line. */
2057 if (gui.col >= Columns)
2058 {
2059 gui.col = 0;
2060 gui.row++;
2061 }
2062 }
2063}
2064
2065/*
2066 * Output one character (may be one or two display cells).
2067 * Caller must check for valid "off".
2068 * Returns FAIL or OK, just like gui_outstr_nowrap().
2069 */
2070 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002071gui_screenchar(
2072 int off, /* Offset from start of screen */
2073 int flags,
2074 guicolor_T fg, /* colors for cursor */
2075 guicolor_T bg, /* colors for cursor */
2076 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 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);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100}
2101
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002102#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103/*
2104 * Output the string at the given screen position. This is used in place
2105 * of gui_screenchar() where possible because Pango needs as much context
2106 * as possible to work nicely. It's a lot faster as well.
2107 */
2108 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002109gui_screenstr(
2110 int off, /* Offset from start of screen */
2111 int len, /* string length in screen cells */
2112 int flags,
2113 guicolor_T fg, /* colors for cursor */
2114 guicolor_T bg, /* colors for cursor */
2115 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 char_u *buf;
2118 int outlen = 0;
2119 int i;
2120 int retval;
2121
2122 if (len <= 0) /* "cannot happen"? */
2123 return OK;
2124
2125 if (enc_utf8)
2126 {
2127 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2128 if (buf == NULL)
2129 return OK; /* not much we could do here... */
2130
2131 for (i = off; i < off + len; ++i)
2132 {
2133 if (ScreenLines[i] == 0)
2134 continue; /* skip second half of double-width char */
2135
2136 if (ScreenLinesUC[i] == 0)
2137 buf[outlen++] = ScreenLines[i];
2138 else
2139 outlen += utfc_char2bytes(i, buf + outlen);
2140 }
2141
2142 buf[outlen] = NUL; /* only to aid debugging */
2143 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2144 vim_free(buf);
2145
2146 return retval;
2147 }
2148 else if (enc_dbcs == DBCS_JPNU)
2149 {
2150 buf = alloc((unsigned)(len * 2 + 1));
2151 if (buf == NULL)
2152 return OK; /* not much we could do here... */
2153
2154 for (i = off; i < off + len; ++i)
2155 {
2156 buf[outlen++] = ScreenLines[i];
2157
2158 /* handle double-byte single-width char */
2159 if (ScreenLines[i] == 0x8e)
2160 buf[outlen++] = ScreenLines2[i];
2161 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2162 buf[outlen++] = ScreenLines[++i];
2163 }
2164
2165 buf[outlen] = NUL; /* only to aid debugging */
2166 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2167 vim_free(buf);
2168
2169 return retval;
2170 }
2171 else
2172 {
2173 return gui_outstr_nowrap(&ScreenLines[off], len,
2174 flags, fg, bg, back);
2175 }
2176}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002177#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179/*
2180 * Output the given string at the current cursor position. If the string is
2181 * too long to fit on the line, then it is truncated.
2182 * "flags":
2183 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2184 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002185 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 * background.
2187 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2188 * it.
2189 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2190 * FAIL (the caller should start drawing "back" chars back).
2191 */
2192 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002193gui_outstr_nowrap(
2194 char_u *s,
2195 int len,
2196 int flags,
2197 guicolor_T fg, /* colors for cursor */
2198 guicolor_T bg, /* colors for cursor */
2199 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 long_u highlight_mask;
2202 long_u hl_mask_todo;
2203 guicolor_T fg_color;
2204 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002205 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002206#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002208 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209# ifdef FEAT_XFONTSET
2210 GuiFontset fontset = NOFONTSET;
2211# endif
2212#endif
2213 attrentry_T *aep = NULL;
2214 int draw_flags;
2215 int col = gui.col;
2216#ifdef FEAT_SIGN_ICONS
2217 int draw_sign = FALSE;
2218# ifdef FEAT_NETBEANS_INTG
2219 int multi_sign = FALSE;
2220# endif
2221#endif
2222
2223 if (len < 0)
2224 len = (int)STRLEN(s);
2225 if (len == 0)
2226 return OK;
2227
2228#ifdef FEAT_SIGN_ICONS
2229 if (*s == SIGN_BYTE
2230# ifdef FEAT_NETBEANS_INTG
2231 || *s == MULTISIGN_BYTE
2232# endif
2233 )
2234 {
2235# ifdef FEAT_NETBEANS_INTG
2236 if (*s == MULTISIGN_BYTE)
2237 multi_sign = TRUE;
2238# endif
2239 /* draw spaces instead */
2240 s = (char_u *)" ";
2241 if (len == 1 && col > 0)
2242 --col;
2243 len = 2;
2244 draw_sign = TRUE;
2245 highlight_mask = 0;
2246 }
2247 else
2248#endif
2249 if (gui.highlight_mask > HL_ALL)
2250 {
2251 aep = syn_gui_attr2entry(gui.highlight_mask);
2252 if (aep == NULL) /* highlighting not set */
2253 highlight_mask = 0;
2254 else
2255 highlight_mask = aep->ae_attr;
2256 }
2257 else
2258 highlight_mask = gui.highlight_mask;
2259 hl_mask_todo = highlight_mask;
2260
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002261#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 /* Set the font */
2263 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2264 font = aep->ae_u.gui.font;
2265# ifdef FEAT_XFONTSET
2266 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2267 fontset = aep->ae_u.gui.fontset;
2268# endif
2269 else
2270 {
2271# ifdef FEAT_XFONTSET
2272 if (gui.fontset != NOFONTSET)
2273 fontset = gui.fontset;
2274 else
2275# endif
2276 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2277 {
2278 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2279 {
2280 font = gui.boldital_font;
2281 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2282 }
2283 else if (gui.bold_font != NOFONT)
2284 {
2285 font = gui.bold_font;
2286 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2287 }
2288 else
2289 font = gui.norm_font;
2290 }
2291 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2292 {
2293 font = gui.ital_font;
2294 hl_mask_todo &= ~HL_ITALIC;
2295 }
2296 else
2297 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002298
Bram Moolenaardb250522013-06-17 22:43:25 +02002299 /*
2300 * Choose correct wide_font by font. wide_font should be set with font
2301 * at same time in above block. But it will make many "ifdef" nasty
2302 * blocks. So we do it here.
2303 */
2304 if (font == gui.boldital_font && gui.wide_boldital_font)
2305 wide_font = gui.wide_boldital_font;
2306 else if (font == gui.bold_font && gui.wide_bold_font)
2307 wide_font = gui.wide_bold_font;
2308 else if (font == gui.ital_font && gui.wide_ital_font)
2309 wide_font = gui.wide_ital_font;
2310 else if (font == gui.norm_font && gui.wide_font)
2311 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313# ifdef FEAT_XFONTSET
2314 if (fontset != NOFONTSET)
2315 gui_mch_set_fontset(fontset);
2316 else
2317# endif
2318 gui_mch_set_font(font);
2319#endif
2320
2321 draw_flags = 0;
2322
2323 /* Set the color */
2324 bg_color = gui.back_pixel;
2325 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2326 {
2327 draw_flags |= DRAW_CURSOR;
2328 fg_color = fg;
2329 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002330 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332 else if (aep != NULL)
2333 {
2334 fg_color = aep->ae_u.gui.fg_color;
2335 if (fg_color == INVALCOLOR)
2336 fg_color = gui.norm_pixel;
2337 bg_color = aep->ae_u.gui.bg_color;
2338 if (bg_color == INVALCOLOR)
2339 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002340 sp_color = aep->ae_u.gui.sp_color;
2341 if (sp_color == INVALCOLOR)
2342 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002347 sp_color = fg_color;
2348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2351 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002352#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 gui_mch_set_colors(bg_color, fg_color);
2354#else
2355 gui_mch_set_fg_color(bg_color);
2356 gui_mch_set_bg_color(fg_color);
2357#endif
2358 }
2359 else
2360 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002361#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 gui_mch_set_colors(fg_color, bg_color);
2363#else
2364 gui_mch_set_fg_color(fg_color);
2365 gui_mch_set_bg_color(bg_color);
2366#endif
2367 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002368 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* Clear the selection if we are about to write over it */
2371 if (!(flags & GUI_MON_NOCLEAR))
2372 clip_may_clear_selection(gui.row, gui.row);
2373
2374
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 /* If there's no bold font, then fake it */
2376 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2377 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 /*
2380 * When drawing bold or italic characters the spill-over from the left
2381 * neighbor may be destroyed. Let the caller backup to start redrawing
2382 * just after a blank.
2383 */
2384 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2385 return FAIL;
2386
Bram Moolenaare60acc12011-05-10 16:41:25 +02002387#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 /* If there's no italic font, then fake it.
2389 * For GTK2, we don't need a different font for italic style. */
2390 if (hl_mask_todo & HL_ITALIC)
2391 draw_flags |= DRAW_ITALIC;
2392
2393 /* Do we underline the text? */
2394 if (hl_mask_todo & HL_UNDERLINE)
2395 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#else
2398 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 draw_flags |= DRAW_UNDERL;
2401#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002402 /* Do we undercurl the text? */
2403 if (hl_mask_todo & HL_UNDERCURL)
2404 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002406 /* Do we strikethrough the text? */
2407 if (hl_mask_todo & HL_STRIKETHROUGH)
2408 draw_flags |= DRAW_STRIKE;
2409
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002410 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (flags & GUI_MON_TRS_CURSOR)
2412 draw_flags |= DRAW_TRANSP;
2413
2414 /*
2415 * Draw the text.
2416 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002417#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 /* The value returned is the length in display cells */
2419 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2420#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 if (enc_utf8)
2422 {
2423 int start; /* index of bytes to be drawn */
2424 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002425 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 int cn; /* cellwidth of current char */
2427 int i; /* index of current char */
2428 int c; /* current char value */
2429 int cl; /* byte length of current char */
2430 int comping; /* current char is composing */
2431 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002432 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002433 int prev_wide = FALSE;
2434 int wide_changed;
Bram Moolenaar13505972019-01-24 15:04:48 +01002435# ifdef WIN3264
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002436 int sep_comp = FALSE; /* Don't separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002437# else
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002438 int sep_comp = TRUE; /* Separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 /* Break the string at a composing character, it has to be drawn on
2442 * top of the previous character. */
2443 start = 0;
2444 cells = 0;
2445 for (i = 0; i < len; i += cl)
2446 {
2447 c = utf_ptr2char(s + i);
2448 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 comping = utf_iscomposing(c);
2450 if (!comping) /* count cells from non-composing chars */
2451 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002452 if (!comping || sep_comp)
2453 {
2454 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002455# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002456 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002457# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002458 && wide_font != NOFONT)
2459 curr_wide = TRUE;
2460 else
2461 curr_wide = FALSE;
2462 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002463 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (cl == 0) /* hit end of string */
2465 len = i + cl; /* len must be wrong "cannot happen" */
2466
Bram Moolenaar45933962013-01-23 17:43:57 +01002467 wide_changed = curr_wide != prev_wide;
2468
2469 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002471 if (i + cl >= len || (comping && sep_comp && i > start)
2472 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002473# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002475# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /* No fontset: At least draw char after wide char at
2477 * right position. */
2478 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002480 )
2481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 )
2483 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002484 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 thislen = i - start;
2486 else
2487 thislen = i - start + cl;
2488 if (thislen > 0)
2489 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002490 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002491 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2493 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002494 if (prev_wide)
2495 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 start += thislen;
2497 }
2498 scol += cells;
2499 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002500 /* Adjust to not draw a character which width is changed
2501 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002502 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002504 scol -= cn;
2505 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507
Bram Moolenaar13505972019-01-24 15:04:48 +01002508# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002509 /* No fontset: draw a space to fill the gap after a wide char
2510 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002512# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002514# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002515 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2517 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002521 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002523# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002524 /* Carbon ATSUI autodraws composing char over previous char */
2525 gui_mch_draw_string(gui.row, scol, s + i, cl,
2526 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002527# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002528 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2529 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 start = i + cl;
2532 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002533 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535 /* The stuff below assumes "len" is the length in screen columns. */
2536 len = scol - col;
2537 }
2538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (enc_dbcs == DBCS_JPNU)
2542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /* Get the length in display cells, this can be different from the
2544 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002545 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002548#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2551 gui.col = col + len;
2552
2553 /* May need to invert it when it's part of the selection. */
2554 if (flags & GUI_MON_NOCLEAR)
2555 clip_may_redraw_selection(gui.row, col, len);
2556
2557 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2558 {
2559 /* Invalidate the old physical cursor position if we wrote over it */
2560 if (gui.cursor_row == gui.row
2561 && gui.cursor_col >= col
2562 && gui.cursor_col < col + len)
2563 gui.cursor_is_valid = FALSE;
2564 }
2565
2566#ifdef FEAT_SIGN_ICONS
2567 if (draw_sign)
2568 /* Draw the sign on top of the spaces. */
2569 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002570# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002571 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (multi_sign)
2573 netbeans_draw_multisign_indicator(gui.row);
2574# endif
2575#endif
2576
2577 return OK;
2578}
2579
2580/*
2581 * Un-draw the cursor. Actually this just redraws the character at the given
2582 * position. The character just before it too, for when it was in bold.
2583 */
2584 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002585gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 if (gui.cursor_is_valid)
2588 {
2589#ifdef FEAT_HANGULIN
2590 if (composing_hangul
2591 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002592 {
2593 char_u *comp_buf;
2594 int comp_len;
2595
2596 comp_buf = hangul_composing_buffer_get(&comp_len);
2597 if (comp_buf)
2598 {
2599 (void)gui_outstr_nowrap(comp_buf, comp_len,
2600 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2601 gui.norm_pixel, gui.back_pixel, 0);
2602 vim_free(comp_buf);
2603 }
2604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 else
2606 {
2607#endif
2608 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2609 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2610 && gui.cursor_col > 0)
2611 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2612 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2613#ifdef FEAT_HANGULIN
2614 if (composing_hangul)
2615 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2616 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2617 }
2618#endif
2619 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2620 * here in case it wasn't needed to undraw it. */
2621 gui.cursor_is_valid = FALSE;
2622 }
2623}
2624
2625 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002626gui_redraw(
2627 int x,
2628 int y,
2629 int w,
2630 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 int row1, col1, row2, col2;
2633
2634 row1 = Y_2_ROW(y);
2635 col1 = X_2_COL(x);
2636 row2 = Y_2_ROW(y + h - 1);
2637 col2 = X_2_COL(x + w - 1);
2638
2639 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2640
2641 /*
2642 * We may need to redraw the cursor, but don't take it upon us to change
2643 * its location after a scroll.
2644 * (maybe be more strict even and test col too?)
2645 * These things may be outside the update/clipping region and reality may
2646 * not reflect Vims internal ideas if these operations are clipped away.
2647 */
2648 if (gui.row == gui.cursor_row)
2649 gui_update_cursor(TRUE, TRUE);
2650}
2651
2652/*
2653 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2654 * from col1 to col2 (inclusive).
2655 * Return TRUE when the character before the first drawn character has
2656 * different attributes (may have to be redrawn too).
2657 */
2658 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002659gui_redraw_block(
2660 int row1,
2661 int col1,
2662 int row2,
2663 int col2,
2664 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666 int old_row, old_col;
2667 long_u old_hl_mask;
2668 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002669 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 int idx, len;
2671 int back, nback;
2672 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 /* Don't try to update when ScreenLines is not valid */
2676 if (!screen_cleared || ScreenLines == NULL)
2677 return retval;
2678
2679 /* Don't try to draw outside the shell! */
2680 /* Check everything, strange values may be caused by a big border width */
2681 col1 = check_col(col1);
2682 col2 = check_col(col2);
2683 row1 = check_row(row1);
2684 row2 = check_row(row2);
2685
2686 /* Remember where our cursor was */
2687 old_row = gui.row;
2688 old_col = gui.col;
2689 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 orig_col1 = col1;
2691 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 for (gui.row = row1; gui.row <= row2; gui.row++)
2694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 /* When only half of a double-wide character is in the block, include
2696 * the other half. */
2697 col1 = orig_col1;
2698 col2 = orig_col2;
2699 off = LineOffset[gui.row];
2700 if (enc_dbcs != 0)
2701 {
2702 if (col1 > 0)
2703 col1 -= dbcs_screen_head_off(ScreenLines + off,
2704 ScreenLines + off + col1);
2705 col2 += dbcs_screen_tail_off(ScreenLines + off,
2706 ScreenLines + off + col2);
2707 }
2708 else if (enc_utf8)
2709 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002710 if (ScreenLines[off + col1] == 0)
2711 {
2712 if (col1 > 0)
2713 --col1;
2714 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002715 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002716 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002717 // Make this IEMSGN when it no longer breaks Travis CI.
2718 vim_snprintf((char *)IObuff, IOSIZE,
2719 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2720 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002721 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002722 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002723 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002724#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2726 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 gui.col = col1;
2730 off = LineOffset[gui.row] + gui.col;
2731 len = col2 - col1 + 1;
2732
2733 /* Find how many chars back this highlighting starts, or where a space
2734 * is. Needed for when the bold trick is used */
2735 for (back = 0; back < col1; ++back)
2736 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2737 || ScreenLines[off - 1 - back] == ' ')
2738 break;
2739 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2740 && ScreenLines[off - 1] != ' ');
2741
2742 /* Break it up in strings of characters with the same attributes. */
2743 /* Print UTF-8 characters individually. */
2744 while (len > 0)
2745 {
2746 first_attr = ScreenAttrs[off];
2747 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002748#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (enc_utf8 && ScreenLinesUC[off] != 0)
2750 {
2751 /* output multi-byte character separately */
2752 nback = gui_screenchar(off, flags,
2753 (guicolor_T)0, (guicolor_T)0, back);
2754 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2755 idx = 2;
2756 else
2757 idx = 1;
2758 }
2759 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2760 {
2761 /* output double-byte, single-width character separately */
2762 nback = gui_screenchar(off, flags,
2763 (guicolor_T)0, (guicolor_T)0, back);
2764 idx = 1;
2765 }
2766 else
2767#endif
2768 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002769#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 for (idx = 0; idx < len; ++idx)
2771 {
2772 if (enc_utf8 && ScreenLines[off + idx] == 0)
2773 continue; /* skip second half of double-width char */
2774 if (ScreenAttrs[off + idx] != first_attr)
2775 break;
2776 }
2777 /* gui_screenstr() takes care of multibyte chars */
2778 nback = gui_screenstr(off, idx, flags,
2779 (guicolor_T)0, (guicolor_T)0, back);
2780#else
2781 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2782 idx++)
2783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /* Stop at a multi-byte Unicode character. */
2785 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2786 break;
2787 if (enc_dbcs == DBCS_JPNU)
2788 {
2789 /* Stop at a double-byte single-width char. */
2790 if (ScreenLines[off + idx] == 0x8e)
2791 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002792 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 + off + idx) == 2)
2794 ++idx; /* skip second byte of double-byte char */
2795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2798 (guicolor_T)0, (guicolor_T)0, back);
2799#endif
2800 }
2801 if (nback == FAIL)
2802 {
2803 /* Must back up to start drawing where a bold or italic word
2804 * starts. */
2805 off -= back;
2806 len += back;
2807 gui.col -= back;
2808 }
2809 else
2810 {
2811 off += idx;
2812 len -= idx;
2813 }
2814 back = 0;
2815 }
2816 }
2817
2818 /* Put the cursor back where it was */
2819 gui.row = old_row;
2820 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002821 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 return retval;
2824}
2825
2826 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002827gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 if (count <= 0)
2830 return;
2831
2832 if (row + count > gui.scroll_region_bot)
2833 /* Scrolled out of region, just blank the lines out */
2834 gui_clear_block(row, gui.scroll_region_left,
2835 gui.scroll_region_bot, gui.scroll_region_right);
2836 else
2837 {
2838 gui_mch_delete_lines(row, count);
2839
2840 /* If the cursor was in the deleted lines it's now gone. If the
2841 * cursor was in the scrolled lines adjust its position. */
2842 if (gui.cursor_row >= row
2843 && gui.cursor_col >= gui.scroll_region_left
2844 && gui.cursor_col <= gui.scroll_region_right)
2845 {
2846 if (gui.cursor_row < row + count)
2847 gui.cursor_is_valid = FALSE;
2848 else if (gui.cursor_row <= gui.scroll_region_bot)
2849 gui.cursor_row -= count;
2850 }
2851 }
2852}
2853
2854 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002855gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 if (count <= 0)
2858 return;
2859
2860 if (row + count > gui.scroll_region_bot)
2861 /* Scrolled out of region, just blank the lines out */
2862 gui_clear_block(row, gui.scroll_region_left,
2863 gui.scroll_region_bot, gui.scroll_region_right);
2864 else
2865 {
2866 gui_mch_insert_lines(row, count);
2867
2868 if (gui.cursor_row >= gui.row
2869 && gui.cursor_col >= gui.scroll_region_left
2870 && gui.cursor_col <= gui.scroll_region_right)
2871 {
2872 if (gui.cursor_row <= gui.scroll_region_bot - count)
2873 gui.cursor_row += count;
2874 else if (gui.cursor_row <= gui.scroll_region_bot)
2875 gui.cursor_is_valid = FALSE;
2876 }
2877 }
2878}
2879
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002880#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002881/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002882 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2883 */
2884 static int
2885gui_wait_for_chars_3(
2886 long wtime,
2887 int *interrupted UNUSED,
2888 int ignore_input UNUSED)
2889{
2890 return gui_mch_wait_for_chars(wtime);
2891}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002892#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002893
2894/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002895 * Returns OK if a character was found to be available within the given time,
2896 * or FAIL otherwise.
2897 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002898 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002899gui_wait_for_chars_or_timer(
2900 long wtime,
2901 int *interrupted UNUSED,
2902 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002903{
2904#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002905 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2906 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002907#else
2908 return gui_mch_wait_for_chars(wtime);
2909#endif
2910}
2911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912/*
2913 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002914 * "wtime" == -1 Wait forever.
2915 * "wtime" == 0 Don't wait.
2916 * "wtime" > 0 Wait wtime milliseconds for a character.
2917 *
2918 * Returns the number of characters read or zero when timed out or interrupted.
2919 * "buf" may be NULL, in which case a non-zero number is returned if characters
2920 * are available.
2921 */
2922 static int
2923gui_wait_for_chars_buf(
2924 char_u *buf,
2925 int maxlen,
2926 long wtime, // don't use "time", MIPS cannot handle it
2927 int tb_change_cnt)
2928{
2929 int retval;
2930
2931#ifdef FEAT_MENU
2932 // If we're going to wait a bit, update the menus and mouse shape for the
2933 // current State.
2934 if (wtime != 0)
2935 gui_update_menus(0);
2936#endif
2937
2938 gui_mch_update();
2939 if (input_available()) // Got char, return immediately
2940 {
2941 if (buf != NULL && !typebuf_changed(tb_change_cnt))
2942 return read_from_input_buf(buf, (long)maxlen);
2943 return 0;
2944 }
2945 if (wtime == 0) // Don't wait for char
2946 return FAIL;
2947
2948 // Before waiting, flush any output to the screen.
2949 gui_mch_flush();
2950
2951 // Blink while waiting for a character.
2952 gui_mch_start_blink();
2953
2954 // Common function to loop until "wtime" is met, while handling timers and
2955 // other callbacks.
2956 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
2957 gui_wait_for_chars_or_timer, NULL);
2958
2959 gui_mch_stop_blink(TRUE);
2960
2961 return retval;
2962}
2963
2964/*
2965 * Wait for a character from the keyboard without actually reading it.
2966 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 * wtime == -1 Wait forever.
2968 * wtime == 0 Don't wait.
2969 * wtime > 0 Wait wtime milliseconds for a character.
2970 * Returns OK if a character was found to be available within the given time,
2971 * or FAIL otherwise.
2972 */
2973 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002974gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002976 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977}
2978
2979/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002980 * Equivalent of mch_inchar() for the GUI.
2981 */
2982 int
2983gui_inchar(
2984 char_u *buf,
2985 int maxlen,
2986 long wtime, /* milli seconds */
2987 int tb_change_cnt)
2988{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002989 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002990}
2991
2992/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002993 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 */
2995 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002996fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 p[0] = (char_u)(col / 128 + ' ' + 1);
2999 p[1] = (char_u)(col % 128 + ' ' + 1);
3000 p[2] = (char_u)(row / 128 + ' ' + 1);
3001 p[3] = (char_u)(row % 128 + ' ' + 1);
3002}
3003
3004/*
3005 * Generic mouse support function. Add a mouse event to the input buffer with
3006 * the given properties.
3007 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3008 * MOUSE_X1, MOUSE_X2
3009 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003010 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3011 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 * x, y --- Coordinates of mouse in pixels.
3013 * repeated_click --- TRUE if this click comes only a short time after a
3014 * previous click.
3015 * modifiers --- Bit field which may be any of the following modifiers
3016 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3017 * This function will ignore drag events where the mouse has not moved to a new
3018 * character.
3019 */
3020 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003021gui_send_mouse_event(
3022 int button,
3023 int x,
3024 int y,
3025 int repeated_click,
3026 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 static int prev_row = 0, prev_col = 0;
3029 static int prev_button = -1;
3030 static int num_clicks = 1;
3031 char_u string[10];
3032 enum key_extra button_char;
3033 int row, col;
3034#ifdef FEAT_CLIPBOARD
3035 int checkfor;
3036 int did_clip = FALSE;
3037#endif
3038
3039 /*
3040 * Scrolling may happen at any time, also while a selection is present.
3041 */
3042 switch (button)
3043 {
3044 case MOUSE_X1:
3045 button_char = KE_X1MOUSE;
3046 goto button_set;
3047 case MOUSE_X2:
3048 button_char = KE_X2MOUSE;
3049 goto button_set;
3050 case MOUSE_4:
3051 button_char = KE_MOUSEDOWN;
3052 goto button_set;
3053 case MOUSE_5:
3054 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003055 goto button_set;
3056 case MOUSE_6:
3057 button_char = KE_MOUSELEFT;
3058 goto button_set;
3059 case MOUSE_7:
3060 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061button_set:
3062 {
3063 /* Don't put events in the input queue now. */
3064 if (hold_gui_events)
3065 return;
3066
3067 string[3] = CSI;
3068 string[4] = KS_EXTRA;
3069 string[5] = (int)button_char;
3070
3071 /* Pass the pointer coordinates of the scroll event so that we
3072 * know which window to scroll. */
3073 row = gui_xy2colrow(x, y, &col);
3074 string[6] = (char_u)(col / 128 + ' ' + 1);
3075 string[7] = (char_u)(col % 128 + ' ' + 1);
3076 string[8] = (char_u)(row / 128 + ' ' + 1);
3077 string[9] = (char_u)(row % 128 + ' ' + 1);
3078
3079 if (modifiers == 0)
3080 add_to_input_buf(string + 3, 7);
3081 else
3082 {
3083 string[0] = CSI;
3084 string[1] = KS_MODIFIER;
3085 string[2] = 0;
3086 if (modifiers & MOUSE_SHIFT)
3087 string[2] |= MOD_MASK_SHIFT;
3088 if (modifiers & MOUSE_CTRL)
3089 string[2] |= MOD_MASK_CTRL;
3090 if (modifiers & MOUSE_ALT)
3091 string[2] |= MOD_MASK_ALT;
3092 add_to_input_buf(string, 10);
3093 }
3094 return;
3095 }
3096 }
3097
3098#ifdef FEAT_CLIPBOARD
3099 /* If a clipboard selection is in progress, handle it */
3100 if (clip_star.state == SELECT_IN_PROGRESS)
3101 {
3102 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3103 return;
3104 }
3105
3106 /* Determine which mouse settings to look for based on the current mode */
3107 switch (get_real_state())
3108 {
3109 case NORMAL_BUSY:
3110 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003111# ifdef FEAT_TERMINAL
3112 case TERMINAL:
3113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 case NORMAL: checkfor = MOUSE_NORMAL; break;
3115 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003116 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 case REPLACE:
3118 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 case VREPLACE:
3120 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 case INSERT:
3122 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3123 case ASKMORE:
3124 case HITRETURN: /* At the more- and hit-enter prompt pass the
3125 mouse event for a click on or below the
3126 message line. */
3127 if (Y_2_ROW(y) >= msg_row)
3128 checkfor = MOUSE_NORMAL;
3129 else
3130 checkfor = MOUSE_RETURN;
3131 break;
3132
3133 /*
3134 * On the command line, use the clipboard selection on all lines
3135 * but the command line. But not when pasting.
3136 */
3137 case CMDLINE:
3138 case CMDLINE+LANGMAP:
3139 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3140 checkfor = MOUSE_NONE;
3141 else
3142 checkfor = MOUSE_COMMAND;
3143 break;
3144
3145 default:
3146 checkfor = MOUSE_NONE;
3147 break;
3148 };
3149
3150 /*
3151 * Allow clipboard selection of text on the command line in "normal"
3152 * modes. Don't do this when dragging the status line, or extending a
3153 * Visual selection.
3154 */
3155 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003156 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 && button != MOUSE_DRAG
3158# ifdef FEAT_MOUSESHAPE
3159 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161# endif
3162 )
3163 checkfor = MOUSE_NONE;
3164
3165 /*
3166 * Use modeless selection when holding CTRL and SHIFT pressed.
3167 */
3168 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3169 checkfor = MOUSE_NONEF;
3170
3171 /*
3172 * In Ex mode, always use modeless selection.
3173 */
3174 if (exmode_active)
3175 checkfor = MOUSE_NONE;
3176
3177 /*
3178 * If the mouse settings say to not use the mouse, use the modeless
3179 * selection. But if Visual is active, assume that only the Visual area
3180 * will be selected.
3181 * Exception: On the command line, both the selection is used and a mouse
3182 * key is send.
3183 */
3184 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* Don't do modeless selection in Visual mode. */
3187 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3188 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 /*
3191 * When 'mousemodel' is "popup", shift-left is translated to right.
3192 * But not when also using Ctrl.
3193 */
3194 if (mouse_model_popup() && button == MOUSE_LEFT
3195 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3196 {
3197 button = MOUSE_RIGHT;
3198 modifiers &= ~ MOUSE_SHIFT;
3199 }
3200
3201 /* If the selection is done, allow the right button to extend it.
3202 * If the selection is cleared, allow the right button to start it
3203 * from the cursor position. */
3204 if (button == MOUSE_RIGHT)
3205 {
3206 if (clip_star.state == SELECT_CLEARED)
3207 {
3208 if (State & CMDLINE)
3209 {
3210 col = msg_col;
3211 row = msg_row;
3212 }
3213 else
3214 {
3215 col = curwin->w_wcol;
3216 row = curwin->w_wrow + W_WINROW(curwin);
3217 }
3218 clip_start_selection(col, row, FALSE);
3219 }
3220 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3221 repeated_click);
3222 did_clip = TRUE;
3223 }
3224 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003225 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3228 did_clip = TRUE;
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 /* Always allow pasting */
3232 if (button != MOUSE_MIDDLE)
3233 {
3234 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3235 return;
3236 if (checkfor != MOUSE_COMMAND)
3237 button = MOUSE_LEFT;
3238 }
3239 repeated_click = FALSE;
3240 }
3241
3242 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003243 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#endif
3245
3246 /* Don't put events in the input queue now. */
3247 if (hold_gui_events)
3248 return;
3249
3250 row = gui_xy2colrow(x, y, &col);
3251
3252 /*
3253 * If we are dragging and the mouse hasn't moved far enough to be on a
3254 * different character, then don't send an event to vim.
3255 */
3256 if (button == MOUSE_DRAG)
3257 {
3258 if (row == prev_row && col == prev_col)
3259 return;
3260 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3261 if (y < 0)
3262 row = -1;
3263 }
3264
3265 /*
3266 * If topline has changed (window scrolled) since the last click, reset
3267 * repeated_click, because we don't want starting Visual mode when
3268 * clicking on a different character in the text.
3269 */
3270 if (curwin->w_topline != gui_prev_topline
3271#ifdef FEAT_DIFF
3272 || curwin->w_topfill != gui_prev_topfill
3273#endif
3274 )
3275 repeated_click = FALSE;
3276
3277 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3278 string[1] = KS_MOUSE;
3279 string[2] = KE_FILLER;
3280 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3281 {
3282 if (repeated_click)
3283 {
3284 /*
3285 * Handle multiple clicks. They only count if the mouse is still
3286 * pointing at the same character.
3287 */
3288 if (button != prev_button || row != prev_row || col != prev_col)
3289 num_clicks = 1;
3290 else if (++num_clicks > 4)
3291 num_clicks = 1;
3292 }
3293 else
3294 num_clicks = 1;
3295 prev_button = button;
3296 gui_prev_topline = curwin->w_topline;
3297#ifdef FEAT_DIFF
3298 gui_prev_topfill = curwin->w_topfill;
3299#endif
3300
3301 string[3] = (char_u)(button | 0x20);
3302 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3303 }
3304 else
3305 string[3] = (char_u)button;
3306
3307 string[3] |= modifiers;
3308 fill_mouse_coord(string + 4, col, row);
3309 add_to_input_buf(string, 8);
3310
3311 if (row < 0)
3312 prev_row = 0;
3313 else
3314 prev_row = row;
3315 prev_col = col;
3316
3317 /*
3318 * We need to make sure this is cleared since Athena doesn't tell us when
3319 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3320 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003321#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 gui.dragged_sb = SBAR_NONE;
3323#endif
3324}
3325
3326/*
3327 * Convert x and y coordinate to column and row in text window.
3328 * Corrects for multi-byte character.
3329 * returns column in "*colp" and row as return value;
3330 */
3331 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003332gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 int col = check_col(X_2_COL(x));
3335 int row = check_row(Y_2_ROW(y));
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 return row;
3339}
3340
3341#if defined(FEAT_MENU) || defined(PROTO)
3342/*
3343 * Callback function for when a menu entry has been selected.
3344 */
3345 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003346gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003348 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 /* Don't put events in the input queue now. */
3351 if (hold_gui_events)
3352 return;
3353
3354 bytes[0] = CSI;
3355 bytes[1] = KS_MENU;
3356 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003357 add_to_input_buf(bytes, 3);
3358 add_long_to_buf((long_u)menu, bytes);
3359 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360}
3361#endif
3362
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003363static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365/*
3366 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003367 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * in p_go.
3369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003371gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#ifdef FEAT_MENU
3374 static int prev_menu_is_active = -1;
3375#endif
3376#ifdef FEAT_TOOLBAR
3377 static int prev_toolbar = -1;
3378 int using_toolbar = FALSE;
3379#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003380#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003381 int using_tabline;
3382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#ifdef FEAT_FOOTER
3384 static int prev_footer = -1;
3385 int using_footer = FALSE;
3386#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 static int prev_tearoff = -1;
3389 int using_tearoff = FALSE;
3390#endif
3391
3392 char_u *p;
3393 int i;
3394#ifdef FEAT_MENU
3395 int grey_old, grey_new;
3396 char_u *temp;
3397#endif
3398 win_T *wp;
3399 int need_set_size;
3400 int fix_size;
3401
3402#ifdef FEAT_MENU
3403 if (oldval != NULL && gui.in_use)
3404 {
3405 /*
3406 * Check if the menu's go from grey to non-grey or vise versa.
3407 */
3408 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3409 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3410 if (grey_old != grey_new)
3411 {
3412 temp = p_go;
3413 p_go = oldval;
3414 gui_update_menus(MENU_ALL_MODES);
3415 p_go = temp;
3416 }
3417 }
3418 gui.menu_is_active = FALSE;
3419#endif
3420
3421 for (i = 0; i < 3; i++)
3422 gui.which_scrollbars[i] = FALSE;
3423 for (p = p_go; *p; p++)
3424 switch (*p)
3425 {
3426 case GO_LEFT:
3427 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3428 break;
3429 case GO_RIGHT:
3430 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 case GO_VLEFT:
3433 if (win_hasvertsplit())
3434 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3435 break;
3436 case GO_VRIGHT:
3437 if (win_hasvertsplit())
3438 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 case GO_BOT:
3441 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3442 break;
3443#ifdef FEAT_MENU
3444 case GO_MENUS:
3445 gui.menu_is_active = TRUE;
3446 break;
3447#endif
3448 case GO_GREY:
3449 /* make menu's have grey items, ignored here */
3450 break;
3451#ifdef FEAT_TOOLBAR
3452 case GO_TOOLBAR:
3453 using_toolbar = TRUE;
3454 break;
3455#endif
3456#ifdef FEAT_FOOTER
3457 case GO_FOOTER:
3458 using_footer = TRUE;
3459 break;
3460#endif
3461 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 using_tearoff = TRUE;
3464#endif
3465 break;
3466 default:
3467 /* Ignore options that are not supported */
3468 break;
3469 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (gui.in_use)
3472 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003473 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003475
3476#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003477 /* Update the GUI tab line, it may appear or disappear. This may
3478 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003479 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003480 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003481 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003482 /* We don't want a resize event change "Rows" here, save and
3483 * restore it. Resizing is handled below. */
3484 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003485 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003486 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003487 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003488 if (using_tabline)
3489 fix_size = TRUE;
3490 if (!gui_use_tabline())
3491 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3492 }
3493#endif
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 for (i = 0; i < 3; i++)
3496 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003497 /* The scrollbar needs to be updated when it is shown/unshown and
3498 * when switching tab pages. But the size only changes when it's
3499 * shown/unshown. Thus we need two places to remember whether a
3500 * scrollbar is there or not. */
3501 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003502 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003503 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 if (i == SBAR_BOTTOM)
3506 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3507 gui.which_scrollbars[i]);
3508 else
3509 {
3510 FOR_ALL_WINDOWS(wp)
3511 {
3512 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3513 }
3514 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003515 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3516 {
3517 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003518 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003519 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003520 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003521 if (gui.which_scrollbars[i])
3522 fix_size = TRUE;
3523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003525 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003526 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528
3529#ifdef FEAT_MENU
3530 if (gui.menu_is_active != prev_menu_is_active)
3531 {
3532 /* We don't want a resize event change "Rows" here, save and
3533 * restore it. Resizing is handled below. */
3534 i = Rows;
3535 gui_mch_enable_menu(gui.menu_is_active);
3536 Rows = i;
3537 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003538 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (gui.menu_is_active)
3540 fix_size = TRUE;
3541 }
3542#endif
3543
3544#ifdef FEAT_TOOLBAR
3545 if (using_toolbar != prev_toolbar)
3546 {
3547 gui_mch_show_toolbar(using_toolbar);
3548 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003549 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (using_toolbar)
3551 fix_size = TRUE;
3552 }
3553#endif
3554#ifdef FEAT_FOOTER
3555 if (using_footer != prev_footer)
3556 {
3557 gui_mch_enable_footer(using_footer);
3558 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003559 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (using_footer)
3561 fix_size = TRUE;
3562 }
3563#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (using_tearoff != prev_tearoff)
3566 {
3567 gui_mch_toggle_tearoffs(using_tearoff);
3568 prev_tearoff = using_tearoff;
3569 }
3570#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003571 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003572 {
3573#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003574 long prev_Columns = Columns;
3575 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /* Adjust the size of the window to make the text area keep the
3578 * same size and to avoid that part of our window is off-screen
3579 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003580 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003581
3582#ifdef FEAT_GUI_GTK
3583 /* GTK has the annoying habit of sending us resize events when
3584 * changing the window size ourselves. This mostly happens when
3585 * waiting for a character to arrive, quite unpredictably, and may
3586 * change Columns and Rows when we don't want it. Wait for a
3587 * character here to avoid this effect.
3588 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003589 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003590 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003591 * Don't change Rows when adding menu/toolbar/tabline.
3592 * Don't change Columns when adding vertical toolbar. */
3593 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003594 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003595 if ((need_set_size & RESIZE_VERT) == 0)
3596 Rows = prev_Rows;
3597 if ((need_set_size & RESIZE_HOR) == 0)
3598 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003599#endif
3600 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003601 /* When the console tabline appears or disappears the window positions
3602 * change. */
3603 if (firstwin->w_winrow != tabline_height())
3604 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606}
3607
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003608#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3609/*
3610 * Return TRUE if the GUI is taking care of the tabline.
3611 * It may still be hidden if 'showtabline' is zero.
3612 */
3613 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003614gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003615{
3616 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3617}
3618
3619/*
3620 * Return TRUE if the GUI is showing the tabline.
3621 * This uses 'showtabline'.
3622 */
3623 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003624gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003625{
3626 if (!gui_use_tabline()
3627 || p_stal == 0
3628 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3629 return FALSE;
3630 return TRUE;
3631}
3632
3633/*
3634 * Update the tabline.
3635 * This may display/undisplay the tabline and update the labels.
3636 */
3637 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003638gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003639{
3640 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003641 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003642
3643 if (!gui.starting && starting == 0)
3644 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003645 /* Updating the tabline uses direct GUI commands, flush
3646 * outstanding instructions first. (esp. clear screen) */
3647 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003648
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003649 if (!showit != !shown)
3650 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003651 if (showit != 0)
3652 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003653
3654 /* When the tabs change from hidden to shown or from shown to
3655 * hidden the size of the text area should remain the same. */
3656 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003657 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003658 }
3659}
3660
3661/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003662 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003663 */
3664 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003665get_tabline_label(
3666 tabpage_T *tp,
3667 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003668{
3669 int modified = FALSE;
3670 char_u buf[40];
3671 int wincount;
3672 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003673 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674
Bram Moolenaar57657d82006-04-21 22:12:41 +00003675 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003676 opt = (tooltip ? &p_gtt : &p_gtl);
3677 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003679 int use_sandbox = FALSE;
3680 int save_called_emsg = called_emsg;
3681 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003682 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003683 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3684 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003685
3686 called_emsg = FALSE;
3687
3688 printer_page_num = tabpage_index(tp);
3689# ifdef FEAT_EVAL
3690 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003691 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003692# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003693 /* It's almost as going to the tabpage, but without autocommands. */
3694 curtab->tp_firstwin = firstwin;
3695 curtab->tp_lastwin = lastwin;
3696 curtab->tp_curwin = curwin;
3697 save_curtab = curtab;
3698 curtab = tp;
3699 topframe = curtab->tp_topframe;
3700 firstwin = curtab->tp_firstwin;
3701 lastwin = curtab->tp_lastwin;
3702 curwin = curtab->tp_curwin;
3703 curbuf = curwin->w_buffer;
3704
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003705 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003706 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003707 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003708 STRCPY(NameBuff, res);
3709
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003710 /* Back to the original curtab. */
3711 curtab = save_curtab;
3712 topframe = curtab->tp_topframe;
3713 firstwin = curtab->tp_firstwin;
3714 lastwin = curtab->tp_lastwin;
3715 curwin = curtab->tp_curwin;
3716 curbuf = curwin->w_buffer;
3717
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003718 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003719 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003720 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003721 called_emsg |= save_called_emsg;
3722 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003723
3724 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3725 * use a default label. */
3726 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003727 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003728 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003729 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003730 if (!tooltip)
3731 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003732
3733 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3734 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3735 if (bufIsChanged(wp->w_buffer))
3736 modified = TRUE;
3737 if (modified || wincount > 1)
3738 {
3739 if (wincount > 1)
3740 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3741 else
3742 buf[0] = NUL;
3743 if (modified)
3744 STRCAT(buf, "+");
3745 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003746 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003747 mch_memmove(NameBuff, buf, STRLEN(buf));
3748 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003749 }
3750}
3751
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003752/*
3753 * Send the event for clicking to select tab page "nr".
3754 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003755 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003756 */
3757 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003758send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003759{
3760 char_u string[3];
3761
3762 if (nr == tabpage_index(curtab))
3763 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003764
3765 /* Don't put events in the input queue now. */
3766 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003767# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003768 || cmdwin_type != 0
3769# endif
3770 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003771 {
3772 /* Set it back to the current tab page. */
3773 gui_mch_set_curtab(tabpage_index(curtab));
3774 return FALSE;
3775 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003776
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003777 string[0] = CSI;
3778 string[1] = KS_TABLINE;
3779 string[2] = KE_FILLER;
3780 add_to_input_buf(string, 3);
3781 string[0] = nr;
3782 add_to_input_buf_csi(string, 1);
3783 return TRUE;
3784}
3785
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003786/*
3787 * Send a tabline menu event
3788 */
3789 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003790send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003791{
3792 char_u string[3];
3793
Bram Moolenaar29547192018-12-11 20:39:19 +01003794 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003795 if (hold_gui_events)
3796 return;
3797
Bram Moolenaar29547192018-12-11 20:39:19 +01003798 // Cannot close the last tabpage.
3799 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3800 return;
3801
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003802 string[0] = CSI;
3803 string[1] = KS_TABMENU;
3804 string[2] = KE_FILLER;
3805 add_to_input_buf(string, 3);
3806 string[0] = tabidx;
3807 string[1] = (char_u)(long)event;
3808 add_to_input_buf_csi(string, 2);
3809}
3810
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813/*
3814 * Scrollbar stuff:
3815 */
3816
Bram Moolenaare45828b2006-02-15 22:12:56 +00003817/*
3818 * Remove all scrollbars. Used before switching to another tab page.
3819 */
3820 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003821gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003822{
3823 int i;
3824 win_T *wp;
3825
3826 for (i = 0; i < 3; i++)
3827 {
3828 if (i == SBAR_BOTTOM)
3829 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3830 else
3831 {
3832 FOR_ALL_WINDOWS(wp)
3833 {
3834 gui_do_scrollbar(wp, i, FALSE);
3835 }
3836 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003837 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003838 }
3839}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003842gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
3844 static int sbar_ident = 0;
3845
3846 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3847 sb->wp = wp;
3848 sb->type = type;
3849 sb->value = 0;
3850#ifdef FEAT_GUI_ATHENA
3851 sb->pixval = 0;
3852#endif
3853 sb->size = 1;
3854 sb->max = 1;
3855 sb->top = 0;
3856 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 sb->status_height = 0;
3859 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3860}
3861
3862/*
3863 * Find the scrollbar with the given index.
3864 */
3865 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003866gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 win_T *wp;
3869
3870 if (gui.bottom_sbar.ident == ident)
3871 return &gui.bottom_sbar;
3872 FOR_ALL_WINDOWS(wp)
3873 {
3874 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3875 return &wp->w_scrollbars[SBAR_LEFT];
3876 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3877 return &wp->w_scrollbars[SBAR_RIGHT];
3878 }
3879 return NULL;
3880}
3881
3882/*
3883 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3884 *
3885 * For Win32, Macintosh and GTK+ 2:
3886 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3887 * you stop dragging the scrollbar. We get here each time the scrollbar is
3888 * dragged another pixel, but as far as the rest of vim goes, it thinks
3889 * we're just hanging in the call to DispatchMessage() in
3890 * process_message(). The DispatchMessage() call that hangs was passed a
3891 * mouse button click event in the scrollbar window. -- webb.
3892 *
3893 * Solution: Do the scrolling right here. But only when allowed.
3894 * Ignore the scrollbars while executing an external command or when there
3895 * are still characters to be processed.
3896 */
3897 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003898gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 int sb_num;
3902#ifdef USE_ON_FLY_SCROLL
3903 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905# ifdef FEAT_DIFF
3906 int old_topfill = curwin->w_topfill;
3907# endif
3908#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003909 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 int byte_count;
3911#endif
3912
3913 if (sb == NULL)
3914 return;
3915
3916 /* Don't put events in the input queue now. */
3917 if (hold_gui_events)
3918 return;
3919
3920#ifdef FEAT_CMDWIN
3921 if (cmdwin_type != 0 && sb->wp != curwin)
3922 return;
3923#endif
3924
3925 if (still_dragging)
3926 {
3927 if (sb->wp == NULL)
3928 gui.dragged_sb = SBAR_BOTTOM;
3929 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3930 gui.dragged_sb = SBAR_LEFT;
3931 else
3932 gui.dragged_sb = SBAR_RIGHT;
3933 gui.dragged_wp = sb->wp;
3934 }
3935 else
3936 {
3937 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003938#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003940 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 gui.dragged_wp = NULL;
3942#endif
3943 }
3944
3945 /* Vertical sbar info is kept in the first sbar (the left one) */
3946 if (sb->wp != NULL)
3947 sb = &sb->wp->w_scrollbars[0];
3948
3949 /*
3950 * Check validity of value
3951 */
3952 if (value < 0)
3953 value = 0;
3954#ifdef SCROLL_PAST_END
3955 else if (value > sb->max)
3956 value = sb->max;
3957#else
3958 if (value > sb->max - sb->size + 1)
3959 value = sb->max - sb->size + 1;
3960#endif
3961
3962 sb->value = value;
3963
3964#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003965 /* When not allowed to do the scrolling right now, return.
3966 * This also checked input_available(), but that causes the first click in
3967 * a scrollbar to be ignored when Vim doesn't have focus. */
3968 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return;
3970#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003971#ifdef FEAT_INS_EXPAND
3972 /* Disallow scrolling the current window when the completion popup menu is
3973 * visible. */
3974 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3975 return;
3976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978#ifdef FEAT_RIGHTLEFT
3979 if (sb->wp == NULL && curwin->w_p_rl)
3980 {
3981 value = sb->max + 1 - sb->size - value;
3982 if (value < 0)
3983 value = 0;
3984 }
3985#endif
3986
3987 if (sb->wp != NULL) /* vertical scrollbar */
3988 {
3989 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3991 sb_num++;
3992 if (wp == NULL)
3993 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995#ifdef USE_ON_FLY_SCROLL
3996 current_scrollbar = sb_num;
3997 scrollbar_value = value;
3998 if (State & NORMAL)
3999 {
4000 gui_do_scroll();
4001 setcursor();
4002 }
4003 else if (State & INSERT)
4004 {
4005 ins_scroll();
4006 setcursor();
4007 }
4008 else if (State & CMDLINE)
4009 {
4010 if (msg_scrolled == 0)
4011 {
4012 gui_do_scroll();
4013 redrawcmdline();
4014 }
4015 }
4016# ifdef FEAT_FOLDING
4017 /* Value may have been changed for closed fold. */
4018 sb->value = sb->wp->w_topline - 1;
4019# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004020
4021 /* When dragging one scrollbar and there is another one at the other
4022 * side move the thumb of that one too. */
4023 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4024 gui_mch_set_scrollbar_thumb(
4025 &sb->wp->w_scrollbars[
4026 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4027 ? SBAR_LEFT : SBAR_RIGHT],
4028 sb->value, sb->size, sb->max);
4029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#else
4031 bytes[0] = CSI;
4032 bytes[1] = KS_VER_SCROLLBAR;
4033 bytes[2] = KE_FILLER;
4034 bytes[3] = (char_u)sb_num;
4035 byte_count = 4;
4036#endif
4037 }
4038 else
4039 {
4040#ifdef USE_ON_FLY_SCROLL
4041 scrollbar_value = value;
4042
4043 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004044 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 else if (State & INSERT)
4046 ins_horscroll();
4047 else if (State & CMDLINE)
4048 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004049 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004051 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 redrawcmdline();
4053 }
4054 }
4055 if (old_leftcol != curwin->w_leftcol)
4056 {
4057 updateWindow(curwin); /* update window, status and cmdline */
4058 setcursor();
4059 }
4060#else
4061 bytes[0] = CSI;
4062 bytes[1] = KS_HOR_SCROLLBAR;
4063 bytes[2] = KE_FILLER;
4064 byte_count = 3;
4065#endif
4066 }
4067
4068#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /*
4070 * synchronize other windows, as necessary according to 'scrollbind'
4071 */
4072 if (curwin->w_p_scb
4073 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4074 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004075# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 ))))
4079 {
4080 do_check_scrollbind(TRUE);
4081 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004082 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (wp->w_redr_type > 0)
4084 updateWindow(wp);
4085 setcursor();
4086 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004087 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004089 add_to_input_buf(bytes, byte_count);
4090 add_long_to_buf((long_u)value, bytes);
4091 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#endif
4093}
4094
4095/*
4096 * Scrollbar stuff:
4097 */
4098
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004099/*
4100 * Called when something in the window layout has changed.
4101 */
4102 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004103gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004104{
4105 if (gui.in_use && starting == 0)
4106 {
4107 out_flush();
4108 gui_init_which_components(NULL);
4109 gui_update_scrollbars(TRUE);
4110 }
4111 need_mouse_correct = TRUE;
4112}
4113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004115gui_update_scrollbars(
4116 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 win_T *wp;
4119 scrollbar_T *sb;
4120 long val, size, max; /* need 32 bits here */
4121 int which_sb;
4122 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125 /* Update the horizontal scrollbar */
4126 gui_update_horiz_scrollbar(force);
4127
4128#ifndef WIN3264
4129 /* Return straight away if there is neither a left nor right scrollbar.
4130 * On MS-Windows this is required anyway for scrollwheel messages. */
4131 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4132 return;
4133#endif
4134
4135 /*
4136 * Don't want to update a scrollbar while we're dragging it. But if we
4137 * have both a left and right scrollbar, and we drag one of them, we still
4138 * need to update the other one.
4139 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004140 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4141 && gui.which_scrollbars[SBAR_LEFT]
4142 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
4144 /*
4145 * If we have two scrollbars and one of them is being dragged, just
4146 * copy the scrollbar position from the dragged one to the other one.
4147 */
4148 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4149 if (gui.dragged_wp != NULL)
4150 gui_mch_set_scrollbar_thumb(
4151 &gui.dragged_wp->w_scrollbars[which_sb],
4152 gui.dragged_wp->w_scrollbars[0].value,
4153 gui.dragged_wp->w_scrollbars[0].size,
4154 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 /* avoid that moving components around generates events */
4158 ++hold_gui_events;
4159
Bram Moolenaar45a24952016-07-24 22:25:15 +02004160 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 {
4162 if (wp->w_buffer == NULL) /* just in case */
4163 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004164 /* Skip a scrollbar that is being dragged. */
4165 if (!force && (gui.dragged_sb == SBAR_LEFT
4166 || gui.dragged_sb == SBAR_RIGHT)
4167 && gui.dragged_wp == wp)
4168 continue;
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170#ifdef SCROLL_PAST_END
4171 max = wp->w_buffer->b_ml.ml_line_count - 1;
4172#else
4173 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4174#endif
4175 if (max < 0) /* empty buffer */
4176 max = 0;
4177 val = wp->w_topline - 1;
4178 size = wp->w_height;
4179#ifdef SCROLL_PAST_END
4180 if (val > max) /* just in case */
4181 val = max;
4182#else
4183 if (size > max + 1) /* just in case */
4184 size = max + 1;
4185 if (val > max - size + 1)
4186 val = max - size + 1;
4187#endif
4188 if (val < 0) /* minimal value is 0 */
4189 val = 0;
4190
4191 /*
4192 * Scrollbar at index 0 (the left one) contains all the information.
4193 * It would be the same info for left and right so we just store it for
4194 * one of them.
4195 */
4196 sb = &wp->w_scrollbars[0];
4197
4198 /*
4199 * Note: no check for valid w_botline. If it's not valid the
4200 * scrollbars will be updated later anyway.
4201 */
4202 if (size < 1 || wp->w_botline - 2 > max)
4203 {
4204 /*
4205 * This can happen during changing files. Just don't update the
4206 * scrollbar for now.
4207 */
4208 sb->height = 0; /* Force update next time */
4209 if (gui.which_scrollbars[SBAR_LEFT])
4210 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4211 if (gui.which_scrollbars[SBAR_RIGHT])
4212 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4213 continue;
4214 }
4215 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 || sb->top != wp->w_winrow
4217 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004219 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 /* Height, width or position of scrollbar has changed. For
4222 * vertical split: curwin changed. */
4223 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 sb->top = wp->w_winrow;
4225 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /* Calculate height and position in pixels */
4229 h = (sb->height + sb->status_height) * gui.char_height;
4230 y = sb->top * gui.char_height + gui.border_offset;
4231#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4232 if (gui.menu_is_active)
4233 y += gui.menu_height;
4234#endif
4235
4236#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4237 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4238# ifdef FEAT_GUI_ATHENA
4239 y += gui.toolbar_height;
4240# else
4241# ifdef FEAT_GUI_MSWIN
4242 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4243# endif
4244# endif
4245#endif
4246
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004247#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4248 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004249 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004250#endif
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 /* Height of top scrollbar includes width of top border */
4255 h += gui.border_offset;
4256 y -= gui.border_offset;
4257 }
4258 if (gui.which_scrollbars[SBAR_LEFT])
4259 {
4260 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4261 gui.left_sbar_x, y,
4262 gui.scrollbar_width, h);
4263 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4264 }
4265 if (gui.which_scrollbars[SBAR_RIGHT])
4266 {
4267 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4268 gui.right_sbar_x, y,
4269 gui.scrollbar_width, h);
4270 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4271 }
4272 }
4273
4274 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4275 * checking if the thumb moved at least a pixel. Only do this for
4276 * Athena, most other GUIs require the update anyway to make the
4277 * arrows work. */
4278#ifdef FEAT_GUI_ATHENA
4279 if (max == 0)
4280 y = 0;
4281 else
4282 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4283 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4284#else
4285 if (force || sb->value != val || sb->size != size || sb->max != max)
4286#endif
4287 {
4288 /* Thumb of scrollbar has moved */
4289 sb->value = val;
4290#ifdef FEAT_GUI_ATHENA
4291 sb->pixval = y;
4292#endif
4293 sb->size = size;
4294 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004295 if (gui.which_scrollbars[SBAR_LEFT]
4296 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4298 val, size, max);
4299 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004300 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4302 val, size, max);
4303 }
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 --hold_gui_events;
4307}
4308
4309/*
4310 * Enable or disable a scrollbar.
4311 * Check for scrollbars for vertically split windows which are not enabled
4312 * sometimes.
4313 */
4314 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004315gui_do_scrollbar(
4316 win_T *wp,
4317 int which, /* SBAR_LEFT or SBAR_RIGHT */
4318 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 int midcol = curwin->w_wincol + curwin->w_width / 2;
4321 int has_midcol = (wp->w_wincol <= midcol
4322 && wp->w_wincol + wp->w_width >= midcol);
4323
4324 /* Only enable scrollbars that contain the middle column of the current
4325 * window. */
4326 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4327 {
4328 /* Scrollbars only on one side. Don't enable scrollbars that don't
4329 * contain the middle column of the current window. */
4330 if (!has_midcol)
4331 enable = FALSE;
4332 }
4333 else
4334 {
4335 /* Scrollbars on both sides. Don't enable scrollbars that neither
4336 * contain the middle column of the current window nor are on the far
4337 * side. */
4338 if (midcol > Columns / 2)
4339 {
4340 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4341 enable = FALSE;
4342 }
4343 else
4344 {
4345 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4346 : !has_midcol)
4347 enable = FALSE;
4348 }
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4351}
4352
4353/*
4354 * Scroll a window according to the values set in the globals current_scrollbar
4355 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4356 * or FALSE otherwise.
4357 */
4358 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004359gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360{
4361 win_T *wp, *save_wp;
4362 int i;
4363 long nlines;
4364 pos_T old_cursor;
4365 linenr_T old_topline;
4366#ifdef FEAT_DIFF
4367 int old_topfill;
4368#endif
4369
4370 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4371 if (wp == NULL)
4372 break;
4373 if (wp == NULL)
4374 /* Couldn't find window */
4375 return FALSE;
4376
4377 /*
4378 * Compute number of lines to scroll. If zero, nothing to do.
4379 */
4380 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4381 if (nlines == 0)
4382 return FALSE;
4383
4384 save_wp = curwin;
4385 old_topline = wp->w_topline;
4386#ifdef FEAT_DIFF
4387 old_topfill = wp->w_topfill;
4388#endif
4389 old_cursor = wp->w_cursor;
4390 curwin = wp;
4391 curbuf = wp->w_buffer;
4392 if (nlines < 0)
4393 scrolldown(-nlines, gui.dragged_wp == NULL);
4394 else
4395 scrollup(nlines, gui.dragged_wp == NULL);
4396 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4397 * the mouse-up event already, but we still want it to behave like when
4398 * dragging. But not the next click in an arrow. */
4399 if (gui.dragged_sb == SBAR_NONE)
4400 gui.dragged_wp = NULL;
4401
4402 if (old_topline != wp->w_topline
4403#ifdef FEAT_DIFF
4404 || old_topfill != wp->w_topfill
4405#endif
4406 )
4407 {
4408 if (p_so != 0)
4409 {
4410 cursor_correct(); /* fix window for 'so' */
4411 update_topline(); /* avoid up/down jump */
4412 }
4413 if (old_cursor.lnum != wp->w_cursor.lnum)
4414 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004418 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4419 validate_cursor();
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 curwin = save_wp;
4422 curbuf = save_wp->w_buffer;
4423
4424 /*
4425 * Don't call updateWindow() when nothing has changed (it will overwrite
4426 * the status line!).
4427 */
4428 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004429 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#ifdef FEAT_DIFF
4431 || old_topfill != wp->w_topfill
4432#endif
4433 )
4434 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004435 int type = VALID;
4436
4437#ifdef FEAT_INS_EXPAND
4438 if (pum_visible())
4439 {
4440 type = NOT_VALID;
4441 wp->w_lines_valid = 0;
4442 }
4443#endif
4444 /* Don't set must_redraw here, it may cause the popup menu to
4445 * disappear when losing focus after a scrollbar drag. */
4446 if (wp->w_redr_type < type)
4447 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004448 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004450 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004453#ifdef FEAT_INS_EXPAND
4454 /* May need to redraw the popup menu. */
4455 if (pum_visible())
4456 pum_redraw();
4457#endif
4458
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004459 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460}
4461
4462
4463/*
4464 * Horizontal scrollbar stuff:
4465 */
4466
4467/*
4468 * Return length of line "lnum" for horizontal scrolling.
4469 */
4470 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004471scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473 char_u *p;
4474 colnr_T col;
4475 int w;
4476
4477 p = ml_get(lnum);
4478 col = 0;
4479 if (*p != NUL)
4480 for (;;)
4481 {
4482 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004483 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (*p == NUL) /* don't count the last character */
4485 break;
4486 col += w;
4487 }
4488 return col;
4489}
4490
4491/* Remember which line is currently the longest, so that we don't have to
4492 * search for it when scrolling horizontally. */
4493static linenr_T longest_lnum = 0;
4494
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004495/*
4496 * Find longest visible line number. If this is not possible (or not desired,
4497 * by setting 'h' in "guioptions") then the current line number is returned.
4498 */
4499 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004500gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004501{
4502 linenr_T ret = 0;
4503
4504 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4505 * line numbers, topline and botline can be invalid when displaying is
4506 * postponed. */
4507 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4508 && curwin->w_topline <= curwin->w_cursor.lnum
4509 && curwin->w_botline > curwin->w_cursor.lnum
4510 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4511 {
4512 linenr_T lnum;
4513 colnr_T n;
4514 long max = 0;
4515
4516 /* Use maximum of all visible lines. Remember the lnum of the
4517 * longest line, closest to the cursor line. Used when scrolling
4518 * below. */
4519 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4520 {
4521 n = scroll_line_len(lnum);
4522 if (n > (colnr_T)max)
4523 {
4524 max = n;
4525 ret = lnum;
4526 }
4527 else if (n == (colnr_T)max
4528 && abs((int)(lnum - curwin->w_cursor.lnum))
4529 < abs((int)(ret - curwin->w_cursor.lnum)))
4530 ret = lnum;
4531 }
4532 }
4533 else
4534 /* Use cursor line only. */
4535 ret = curwin->w_cursor.lnum;
4536
4537 return ret;
4538}
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004541gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
4543 long value, size, max; /* need 32 bit ints here */
4544
4545 if (!gui.which_scrollbars[SBAR_BOTTOM])
4546 return;
4547
4548 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4549 return;
4550
4551 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4552 return;
4553
4554 /*
4555 * It is possible for the cursor to be invalid if we're in the middle of
4556 * something (like changing files). If so, don't do anything for now.
4557 */
4558 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4559 {
4560 gui.bottom_sbar.value = -1;
4561 return;
4562 }
4563
Bram Moolenaar02631462017-09-22 15:20:32 +02004564 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (curwin->w_p_wrap)
4566 {
4567 value = 0;
4568#ifdef SCROLL_PAST_END
4569 max = 0;
4570#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004571 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#endif
4573 }
4574 else
4575 {
4576 value = curwin->w_leftcol;
4577
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004578 longest_lnum = gui_find_longest_lnum();
4579 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (virtual_active())
4582 {
4583 /* May move the cursor even further to the right. */
4584 if (curwin->w_virtcol >= (colnr_T)max)
4585 max = curwin->w_virtcol;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004589 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#endif
4591 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004592 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 size -= curwin_col_off();
4594#ifndef SCROLL_PAST_END
4595 max -= curwin_col_off();
4596#endif
4597 }
4598
4599#ifndef SCROLL_PAST_END
4600 if (value > max - size + 1)
4601 value = max - size + 1; /* limit the value to allowable range */
4602#endif
4603
4604#ifdef FEAT_RIGHTLEFT
4605 if (curwin->w_p_rl)
4606 {
4607 value = max + 1 - size - value;
4608 if (value < 0)
4609 {
4610 size += value;
4611 value = 0;
4612 }
4613 }
4614#endif
4615 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4616 && max == gui.bottom_sbar.max)
4617 return;
4618
4619 gui.bottom_sbar.value = value;
4620 gui.bottom_sbar.size = size;
4621 gui.bottom_sbar.max = max;
4622 gui.prev_wrap = curwin->w_p_wrap;
4623
4624 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4625}
4626
4627/*
4628 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4629 */
4630 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004631gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 /* no wrapping, no scrolling */
4634 if (curwin->w_p_wrap)
4635 return FALSE;
4636
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004637 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 return FALSE;
4639
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004640 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004643 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004645 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004646 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004648 if (compute_longest_lnum)
4649 {
4650 curwin->w_cursor.lnum = gui_find_longest_lnum();
4651 curwin->w_cursor.col = 0;
4652 }
4653 /* Do a sanity check on "longest_lnum", just in case. */
4654 else if (longest_lnum >= curwin->w_topline
4655 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
4657 curwin->w_cursor.lnum = longest_lnum;
4658 curwin->w_cursor.col = 0;
4659 }
4660 }
4661
4662 return leftcol_changed();
4663}
4664
4665/*
4666 * Check that none of the colors are the same as the background color
4667 */
4668 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004669gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4672 {
4673 gui_set_bg_color((char_u *)"White");
4674 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4675 gui_set_fg_color((char_u *)"Black");
4676 }
4677}
4678
Bram Moolenaar3918c952005-03-15 22:34:55 +00004679 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004680gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
4682 gui.norm_pixel = gui_get_color(name);
4683 hl_set_fg_color_name(vim_strsave(name));
4684}
4685
Bram Moolenaar3918c952005-03-15 22:34:55 +00004686 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004687gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689 gui.back_pixel = gui_get_color(name);
4690 hl_set_bg_color_name(vim_strsave(name));
4691}
4692
4693/*
4694 * Allocate a color by name.
4695 * Returns INVALCOLOR and gives an error message when failed.
4696 */
4697 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004698gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 guicolor_T t;
4701
4702 if (*name == NUL)
4703 return INVALCOLOR;
4704 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004707#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 && gui.in_use
4709#endif
4710 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004711 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return t;
4713}
4714
4715/*
4716 * Return the grey value of a color (range 0-255).
4717 */
4718 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004719gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004721 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004723 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004724 + (((rgb >> 8) & 0xff) * 587)
4725 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726}
4727
4728#if defined(FEAT_GUI_X11) || defined(PROTO)
4729 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004730gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 win_T *wp;
4733
4734 /* Nothing to do if GUI hasn't started yet. */
4735 if (!gui.in_use)
4736 return;
4737
4738 FOR_ALL_WINDOWS(wp)
4739 {
4740 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4741 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4742 }
4743 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4744}
4745#endif
4746
4747/*
4748 * Call this when focus has changed.
4749 */
4750 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004751gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753/*
4754 * Skip this code to avoid drawing the cursor when debugging and switching
4755 * between the debugger window and gvim.
4756 */
4757#if 1
4758 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004759 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761# ifdef FEAT_XIM
4762 xim_set_focus(in_focus);
4763# endif
4764
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004765 /* Put events in the input queue only when allowed.
4766 * ui_focus_change() isn't called directly, because it invokes
4767 * autocommands and that must not happen asynchronously. */
4768 if (!hold_gui_events)
4769 {
4770 char_u bytes[3];
4771
4772 bytes[0] = CSI;
4773 bytes[1] = KS_EXTRA;
4774 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4775 add_to_input_buf(bytes, 3);
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777#endif
4778}
4779
4780/*
4781 * Called when the mouse moved (but not when dragging).
4782 */
4783 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004784gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004787 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaard3667a22006-03-16 21:35:52 +00004789 /* Ignore this while still starting up. */
4790 if (!gui.in_use || gui.starting)
4791 return;
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#ifdef FEAT_MOUSESHAPE
4794 /* Get window pointer, and update mouse shape as well. */
4795 wp = xy2win(x, y);
4796#endif
4797
4798 /* Only handle this when 'mousefocus' set and ... */
4799 if (p_mousef
4800 && !hold_gui_events /* not holding events */
4801 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4802 && State != HITRETURN /* but not hit-return prompt */
4803 && msg_scrolled == 0 /* no scrolled message */
4804 && !need_mouse_correct /* not moving the pointer */
4805 && gui.in_focus) /* gvim in focus */
4806 {
4807 /* Don't move the mouse when it's left or right of the Vim window */
4808 if (x < 0 || x > Columns * gui.char_width)
4809 return;
4810#ifndef FEAT_MOUSESHAPE
4811 wp = xy2win(x, y);
4812#endif
4813 if (wp == curwin || wp == NULL)
4814 return; /* still in the same old window, or none at all */
4815
Bram Moolenaar9c102382006-05-03 21:26:49 +00004816 /* Ignore position in the tab pages line. */
4817 if (Y_2_ROW(y) < tabline_height())
4818 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 /*
4821 * format a mouse click on status line input
4822 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004823 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4824 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826 if (finish_op)
4827 {
4828 /* abort the current operator first */
4829 st[0] = ESC;
4830 add_to_input_buf(st, 1);
4831 }
4832 st[0] = CSI;
4833 st[1] = KS_MOUSE;
4834 st[2] = KE_FILLER;
4835 st[3] = (char_u)MOUSE_LEFT;
4836 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004837 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 wp->w_height + W_WINROW(wp));
4839
4840 add_to_input_buf(st, 8);
4841 st[3] = (char_u)MOUSE_RELEASE;
4842 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#ifdef FEAT_GUI_GTK
4844 /* Need to wake up the main loop */
4845 if (gtk_main_level() > 0)
4846 gtk_main_quit();
4847#endif
4848 }
4849}
4850
4851/*
4852 * Called when mouse should be moved to window with focus.
4853 */
4854 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004855gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
4857 int x, y;
4858 win_T *wp = NULL;
4859
4860 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004861
4862 if (!(gui.in_use && p_mousef))
4863 return;
4864
4865 gui_mch_getmouse(&x, &y);
4866 /* Don't move the mouse when it's left or right of the Vim window */
4867 if (x < 0 || x > Columns * gui.char_width)
4868 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004869 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004870 wp = xy2win(x, y);
4871 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004873 validate_cline_row();
4874 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4875 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878}
4879
4880/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004881 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004884xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 int row;
4887 int col;
4888 win_T *wp;
4889
4890 row = Y_2_ROW(y);
4891 col = X_2_COL(x);
4892 if (row < 0 || col < 0) /* before first window */
4893 return NULL;
4894 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004895 if (wp == NULL)
4896 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004897#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (State == HITRETURN || State == ASKMORE)
4899 {
4900 if (Y_2_ROW(y) >= msg_row)
4901 update_mouseshape(SHAPE_IDX_MOREL);
4902 else
4903 update_mouseshape(SHAPE_IDX_MORE);
4904 }
4905 else if (row > wp->w_height) /* below status line */
4906 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004907 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004908 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004910 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004911 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 update_mouseshape(SHAPE_IDX_STATUS);
4913 else
4914 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004916 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917}
4918
4919/*
4920 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4921 * File names may be given to redefine the args list.
4922 */
4923 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004924ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 char_u *arg = eap->arg;
4927
4928 /*
4929 * Check for "-f" argument: foreground, don't fork.
4930 * Also don't fork when started with "gvim -f".
4931 * Do fork when using "gui -b".
4932 */
4933 if (arg[0] == '-'
4934 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004935 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 gui.dofork = (arg[1] == 'b');
4938 eap->arg = skipwhite(eap->arg + 2);
4939 }
4940 if (!gui.in_use)
4941 {
4942 /* Clear the command. Needed for when forking+exiting, to avoid part
4943 * of the argument ending up after the shell prompt. */
4944 msg_clr_eos_force();
4945 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004946#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004947 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 if (!ends_excmd(*eap->arg))
4951 ex_next(eap);
4952}
4953
4954#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004955 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956/*
4957 * This is shared between Athena, Motif and GTK.
4958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960/*
4961 * Callback function for do_in_runtimepath().
4962 */
4963 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004964gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004966 char_u *gfp_buffer = cookie;
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (STRLEN(fname) >= MAXPATHL)
4969 *gfp_buffer = NUL;
4970 else
4971 STRCPY(gfp_buffer, fname);
4972}
4973
4974/*
4975 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4976 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4977 */
4978 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004979gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
4981 if (STRLEN(name) > MAXPATHL - 14)
4982 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004983 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004984 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004985 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 return FAIL;
4987 return OK;
4988}
4989
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004990# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991/*
4992 * Given the name of the "icon=" argument, try finding the bitmap file for the
4993 * icon. If it is an absolute path name, use it as it is. Otherwise append
4994 * "ext" and search for it in 'runtimepath'.
4995 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4996 * contains "name".
4997 */
4998 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004999gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 char_u buf[MAXPATHL + 1];
5002
5003 expand_env(name, buffer, MAXPATHL);
5004 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5005 STRCPY(buffer, buf);
5006}
5007# endif
5008#endif
5009
Bram Moolenaar9372a112005-12-06 19:59:18 +00005010#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005012display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 char_u *p;
5015
5016 if (isatty(2))
5017 fflush(stderr);
5018 else if (error_ga.ga_data != NULL)
5019 {
5020 /* avoid putting up a message box with blanks only */
5021 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5022 if (!isspace(*p))
5023 {
5024 /* Truncate a very long message, it will go off-screen. */
5025 if (STRLEN(p) > 2000)
5026 STRCPY(p + 2000 - 14, "...(truncated)");
5027 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005028 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
5031 ga_clear(&error_ga);
5032 }
5033}
5034#endif
5035
5036#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5037/*
5038 * Return TRUE if still starting up and there is no place to enter text.
5039 * For GTK and X11 we check if stderr is not a tty, which means we were
5040 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5041 * allow typing on stdin.
5042 */
5043 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005044no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046 return ((!gui.in_use || gui.starting)
5047# ifndef NO_CONSOLE
5048 && !isatty(0) && !isatty(2)
5049# endif
5050 );
5051}
5052#endif
5053
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005054#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005055 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005056 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057/*
5058 * Update the current window and the screen.
5059 */
5060 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005061gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005063# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005064 linenr_T conceal_old_cursor_line = 0;
5065 linenr_T conceal_new_cursor_line = 0;
5066 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005067# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 update_topline();
5070 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005071
Bram Moolenaarec80df72008-05-07 19:46:51 +00005072 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005073 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005074# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005075 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005076# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005077 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005078 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005079 if (has_cursormoved())
5080 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005081# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005082 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005083 {
5084 conceal_old_cursor_line = last_cursormoved.lnum;
5085 conceal_new_cursor_line = curwin->w_cursor.lnum;
5086 conceal_update_lines = TRUE;
5087 }
5088# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005089 last_cursormoved = curwin->w_cursor;
5090 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005091
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005092# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005093 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005094 && (conceal_old_cursor_line != conceal_new_cursor_line
5095 || conceal_cursor_line(curwin)
5096 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005097 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005098 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005099 redrawWinline(curwin, conceal_old_cursor_line);
5100 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005101 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005102 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005103 }
5104# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005105 update_screen(0); /* may need to update the screen */
5106 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005107 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108}
5109#endif
5110
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005111#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112/*
5113 * Get the text to use in a find/replace dialog. Uses the last search pattern
5114 * if the argument is empty.
5115 * Returns an allocated string.
5116 */
5117 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005118get_find_dialog_text(
5119 char_u *arg,
5120 int *wwordp, /* return: TRUE if \< \> found */
5121 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 char_u *text;
5124
5125 if (*arg == NUL)
5126 text = last_search_pat();
5127 else
5128 text = arg;
5129 if (text != NULL)
5130 {
5131 text = vim_strsave(text);
5132 if (text != NULL)
5133 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005134 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 int i;
5136
5137 /* Remove "\V" */
5138 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5139 {
5140 mch_memmove(text, text + 2, (size_t)(len - 1));
5141 len -= 2;
5142 }
5143
5144 /* Recognize "\c" and "\C" and remove. */
5145 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5146 {
5147 *mcasep = (text[1] == 'C');
5148 mch_memmove(text, text + 2, (size_t)(len - 1));
5149 len -= 2;
5150 }
5151
5152 /* Recognize "\<text\>" and remove. */
5153 if (len >= 4
5154 && STRNCMP(text, "\\<", 2) == 0
5155 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5156 {
5157 *wwordp = TRUE;
5158 mch_memmove(text, text + 2, (size_t)(len - 4));
5159 text[len - 4] = NUL;
5160 }
5161
5162 /* Recognize "\/" or "\?" and remove. */
5163 for (i = 0; i + 1 < len; ++i)
5164 if (text[i] == '\\' && (text[i + 1] == '/'
5165 || text[i + 1] == '?'))
5166 {
5167 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5168 --len;
5169 }
5170 }
5171 }
5172 return text;
5173}
5174
5175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 * Handle the press of a button in the find-replace dialog.
5177 * Return TRUE when something was added to the input buffer.
5178 */
5179 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005180gui_do_findrepl(
5181 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5182 char_u *find_text,
5183 char_u *repl_text,
5184 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 garray_T ga;
5187 int i;
5188 int type = (flags & FRD_TYPE_MASK);
5189 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005190 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005191 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005192 static int busy = FALSE;
5193
5194 /* When the screen is being updated we should not change buffers and
5195 * windows structures, it may cause freed memory to be used. Also don't
5196 * do this recursively (pressing "Find" quickly several times. */
5197 if (updating_screen || busy)
5198 return FALSE;
5199
5200 /* refuse replace when text cannot be changed */
5201 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5202 return FALSE;
5203
5204 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005207 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ga_concat(&ga, (char_u *)"%s/");
5209
5210 ga_concat(&ga, (char_u *)"\\V");
5211 if (flags & FRD_MATCH_CASE)
5212 ga_concat(&ga, (char_u *)"\\C");
5213 else
5214 ga_concat(&ga, (char_u *)"\\c");
5215 if (flags & FRD_WHOLE_WORD)
5216 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005217 /* escape / and \ */
5218 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5219 if (p != NULL)
5220 ga_concat(&ga, p);
5221 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (flags & FRD_WHOLE_WORD)
5223 ga_concat(&ga, (char_u *)"\\>");
5224
5225 if (type == FRD_REPLACEALL)
5226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005228 /* escape / and \ */
5229 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5230 if (p != NULL)
5231 ga_concat(&ga, p);
5232 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005234 }
5235 ga_append(&ga, NUL);
5236
5237 if (type == FRD_REPLACE)
5238 {
5239 /* Do the replacement when the text at the cursor matches. Thus no
5240 * replacement is done if the cursor was moved! */
5241 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5242 regmatch.rm_ic = 0;
5243 if (regmatch.regprog != NULL)
5244 {
5245 p = ml_get_cursor();
5246 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5247 && regmatch.startp[0] == p)
5248 {
5249 /* Clear the command line to remove any old "No match"
5250 * error. */
5251 msg_end_prompt();
5252
5253 if (u_save_cursor() == OK)
5254 {
5255 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005256 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005257
5258 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005259 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005260 ins_str(repl_text);
5261 }
5262 }
5263 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005264 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005265 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005266 }
5267 }
5268
5269 if (type == FRD_REPLACEALL)
5270 {
5271 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005272 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 do_cmdline_cmd(ga.ga_data);
5274 }
5275 else
5276 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005277 int searchflags = SEARCH_MSG + SEARCH_MARK;
5278
5279 /* Search for the next match.
5280 * Don't skip text under cursor for single replace. */
5281 if (type == FRD_REPLACE)
5282 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005284 if (down)
5285 {
5286 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5287 }
5288 else
5289 {
5290 /* We need to escape '?' if and only if we are searching in the up
5291 * direction */
5292 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5293 if (p != NULL)
5294 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5295 vim_free(p);
5296 }
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 msg_scroll = i; /* don't let an error message set msg_scroll */
5299 }
5300
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005301 /* Don't want to pass did_emsg to other code, it may cause disabling
5302 * syntax HL if we were busy redrawing. */
5303 did_emsg = save_did_emsg;
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (State & (NORMAL | INSERT))
5306 {
5307 gui_update_screen(); /* update the screen */
5308 msg_didout = 0; /* overwrite any message */
5309 need_wait_return = FALSE; /* don't wait for return */
5310 }
5311
5312 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005313 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 return (ga.ga_len > 0);
5315}
5316
5317#endif
5318
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005319#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
5321 * Jump to the window at specified point (x, y).
5322 */
5323 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005324gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 int row = Y_2_ROW(y);
5327 int col = X_2_COL(x);
5328 win_T *wp;
5329
5330 if (row >= 0 && col >= 0)
5331 {
5332 wp = mouse_find_win(&row, &col);
5333 if (wp != NULL && wp != curwin)
5334 win_goto(wp);
5335 }
5336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
5338/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005339 * Function passed to handle_drop() for the actions to be done after the
5340 * argument list has been updated.
5341 */
5342 static void
5343drop_callback(void *cookie)
5344{
5345 char_u *p = cookie;
5346
5347 /* If Shift held down, change to first file's directory. If the first
5348 * item is a directory, change to that directory (and let the explorer
5349 * plugin show the contents). */
5350 if (p != NULL)
5351 {
5352 if (mch_isdir(p))
5353 {
5354 if (mch_chdir((char *)p) == 0)
5355 shorten_fnames(TRUE);
5356 }
5357 else if (vim_chdirfile(p, "drop") == OK)
5358 shorten_fnames(TRUE);
5359 vim_free(p);
5360 }
5361
5362 /* Update the screen display */
5363 update_screen(NOT_VALID);
5364# ifdef FEAT_MENU
5365 gui_update_menus(0);
5366# endif
5367#ifdef FEAT_TITLE
5368 maketitle();
5369#endif
5370 setcursor();
5371 out_flush_cursor(FALSE, FALSE);
5372}
5373
5374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 * Process file drop. Mouse cursor position, key modifiers, name of files
5376 * and count of files are given. Argument "fnames[count]" has full pathnames
5377 * of dropped files, they will be freed in this function, and caller can't use
5378 * fnames after call this function.
5379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005381gui_handle_drop(
5382 int x UNUSED,
5383 int y UNUSED,
5384 int_u modifiers,
5385 char_u **fnames,
5386 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 int i;
5389 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005390 static int entered = FALSE;
5391
5392 /*
5393 * This function is called by event handlers. Just in case we get a
5394 * second event before the first one is handled, ignore the second one.
5395 * Not sure if this can ever happen, just in case.
5396 */
5397 if (entered)
5398 return;
5399 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401 /*
5402 * When the cursor is at the command line, add the file names to the
5403 * command line, don't edit the files.
5404 */
5405 if (State & CMDLINE)
5406 {
5407 shorten_filenames(fnames, count);
5408 for (i = 0; i < count; ++i)
5409 {
5410 if (fnames[i] != NULL)
5411 {
5412 if (i > 0)
5413 add_to_input_buf((char_u*)" ", 1);
5414
5415 /* We don't know what command is used thus we can't be sure
5416 * about which characters need to be escaped. Only escape the
5417 * most common ones. */
5418# ifdef BACKSLASH_IN_FILENAME
5419 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5420# else
5421 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5422# endif
5423 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005424 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 vim_free(p);
5426 vim_free(fnames[i]);
5427 }
5428 }
5429 vim_free(fnames);
5430 }
5431 else
5432 {
5433 /* Go to the window under mouse cursor, then shorten given "fnames" by
5434 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 shorten_filenames(fnames, count);
5437
5438 /* If Shift held down, remember the first item. */
5439 if ((modifiers & MOUSE_SHIFT) != 0)
5440 p = vim_strsave(fnames[0]);
5441 else
5442 p = NULL;
5443
5444 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005445 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 * directory and Shift is held down. */
5447 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5448 && mch_isdir(fnames[0]))
5449 {
5450 vim_free(fnames[0]);
5451 vim_free(fnames);
5452 }
5453 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005454 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5455 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005457
5458 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460#endif