blob: c22f47716bd41b759ffb246c0da184e3a32037bd [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
2899gui_wait_for_chars_or_timer(long wtime)
2900{
2901#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002902 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3, NULL, 0);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002903#else
2904 return gui_mch_wait_for_chars(wtime);
2905#endif
2906}
2907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908/*
2909 * The main GUI input routine. Waits for a character from the keyboard.
2910 * wtime == -1 Wait forever.
2911 * wtime == 0 Don't wait.
2912 * wtime > 0 Wait wtime milliseconds for a character.
2913 * Returns OK if a character was found to be available within the given time,
2914 * or FAIL otherwise.
2915 */
2916 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002917gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002919 int retval;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002920#if defined(ELAPSED_FUNC)
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002921 elapsed_T start_tv;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002924#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 /*
2926 * If we're going to wait a bit, update the menus and mouse shape for the
2927 * current State.
2928 */
2929 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 gui_update_menus(0);
2931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 gui_mch_update();
2934 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 /* Before waiting, flush any output to the screen. */
2940 gui_mch_flush();
2941
2942 if (wtime > 0)
2943 {
2944 /* Blink when waiting for a character. Probably only does something
2945 * for showmatch() */
2946 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002947 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002948 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 return retval;
2950 }
2951
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002952#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002953 ELAPSED_INIT(start_tv);
2954#endif
2955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002957 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 */
2959 gui_mch_start_blink();
2960
Bram Moolenaar3918c952005-03-15 22:34:55 +00002961 retval = FAIL;
2962 /*
2963 * We may want to trigger the CursorHold event. First wait for
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002964 * 'updatetime' and if nothing is typed within that time, and feedkeys()
2965 * wasn't used, put the K_CURSORHOLD key in the input buffer.
Bram Moolenaar3918c952005-03-15 22:34:55 +00002966 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002967 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002968 retval = OK;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002969 else if (trigger_cursorhold()
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01002970#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002971 && ELAPSED_FUNC(start_tv) >= p_ut
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002972#endif
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002973 && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002975 char_u buf[3];
2976
2977 /* Put K_CURSORHOLD in the input buffer. */
2978 buf[0] = CSI;
2979 buf[1] = KS_EXTRA;
2980 buf[2] = (int)KE_CURSORHOLD;
2981 add_to_input_buf(buf, 3);
2982
2983 retval = OK;
2984 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002985
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002986 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00002987 {
2988 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002989 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002990 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002993 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 return retval;
2995}
2996
2997/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002998 * Equivalent of mch_inchar() for the GUI.
2999 */
3000 int
3001gui_inchar(
3002 char_u *buf,
3003 int maxlen,
3004 long wtime, /* milli seconds */
3005 int tb_change_cnt)
3006{
3007 if (gui_wait_for_chars(wtime, tb_change_cnt)
3008 && !typebuf_changed(tb_change_cnt))
3009 return read_from_input_buf(buf, (long)maxlen);
3010 return 0;
3011}
3012
3013/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003014 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 */
3016 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003017fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 p[0] = (char_u)(col / 128 + ' ' + 1);
3020 p[1] = (char_u)(col % 128 + ' ' + 1);
3021 p[2] = (char_u)(row / 128 + ' ' + 1);
3022 p[3] = (char_u)(row % 128 + ' ' + 1);
3023}
3024
3025/*
3026 * Generic mouse support function. Add a mouse event to the input buffer with
3027 * the given properties.
3028 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3029 * MOUSE_X1, MOUSE_X2
3030 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003031 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3032 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 * x, y --- Coordinates of mouse in pixels.
3034 * repeated_click --- TRUE if this click comes only a short time after a
3035 * previous click.
3036 * modifiers --- Bit field which may be any of the following modifiers
3037 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3038 * This function will ignore drag events where the mouse has not moved to a new
3039 * character.
3040 */
3041 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003042gui_send_mouse_event(
3043 int button,
3044 int x,
3045 int y,
3046 int repeated_click,
3047 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 static int prev_row = 0, prev_col = 0;
3050 static int prev_button = -1;
3051 static int num_clicks = 1;
3052 char_u string[10];
3053 enum key_extra button_char;
3054 int row, col;
3055#ifdef FEAT_CLIPBOARD
3056 int checkfor;
3057 int did_clip = FALSE;
3058#endif
3059
3060 /*
3061 * Scrolling may happen at any time, also while a selection is present.
3062 */
3063 switch (button)
3064 {
3065 case MOUSE_X1:
3066 button_char = KE_X1MOUSE;
3067 goto button_set;
3068 case MOUSE_X2:
3069 button_char = KE_X2MOUSE;
3070 goto button_set;
3071 case MOUSE_4:
3072 button_char = KE_MOUSEDOWN;
3073 goto button_set;
3074 case MOUSE_5:
3075 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003076 goto button_set;
3077 case MOUSE_6:
3078 button_char = KE_MOUSELEFT;
3079 goto button_set;
3080 case MOUSE_7:
3081 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082button_set:
3083 {
3084 /* Don't put events in the input queue now. */
3085 if (hold_gui_events)
3086 return;
3087
3088 string[3] = CSI;
3089 string[4] = KS_EXTRA;
3090 string[5] = (int)button_char;
3091
3092 /* Pass the pointer coordinates of the scroll event so that we
3093 * know which window to scroll. */
3094 row = gui_xy2colrow(x, y, &col);
3095 string[6] = (char_u)(col / 128 + ' ' + 1);
3096 string[7] = (char_u)(col % 128 + ' ' + 1);
3097 string[8] = (char_u)(row / 128 + ' ' + 1);
3098 string[9] = (char_u)(row % 128 + ' ' + 1);
3099
3100 if (modifiers == 0)
3101 add_to_input_buf(string + 3, 7);
3102 else
3103 {
3104 string[0] = CSI;
3105 string[1] = KS_MODIFIER;
3106 string[2] = 0;
3107 if (modifiers & MOUSE_SHIFT)
3108 string[2] |= MOD_MASK_SHIFT;
3109 if (modifiers & MOUSE_CTRL)
3110 string[2] |= MOD_MASK_CTRL;
3111 if (modifiers & MOUSE_ALT)
3112 string[2] |= MOD_MASK_ALT;
3113 add_to_input_buf(string, 10);
3114 }
3115 return;
3116 }
3117 }
3118
3119#ifdef FEAT_CLIPBOARD
3120 /* If a clipboard selection is in progress, handle it */
3121 if (clip_star.state == SELECT_IN_PROGRESS)
3122 {
3123 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3124 return;
3125 }
3126
3127 /* Determine which mouse settings to look for based on the current mode */
3128 switch (get_real_state())
3129 {
3130 case NORMAL_BUSY:
3131 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003132# ifdef FEAT_TERMINAL
3133 case TERMINAL:
3134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 case NORMAL: checkfor = MOUSE_NORMAL; break;
3136 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003137 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 case REPLACE:
3139 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 case VREPLACE:
3141 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 case INSERT:
3143 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3144 case ASKMORE:
3145 case HITRETURN: /* At the more- and hit-enter prompt pass the
3146 mouse event for a click on or below the
3147 message line. */
3148 if (Y_2_ROW(y) >= msg_row)
3149 checkfor = MOUSE_NORMAL;
3150 else
3151 checkfor = MOUSE_RETURN;
3152 break;
3153
3154 /*
3155 * On the command line, use the clipboard selection on all lines
3156 * but the command line. But not when pasting.
3157 */
3158 case CMDLINE:
3159 case CMDLINE+LANGMAP:
3160 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3161 checkfor = MOUSE_NONE;
3162 else
3163 checkfor = MOUSE_COMMAND;
3164 break;
3165
3166 default:
3167 checkfor = MOUSE_NONE;
3168 break;
3169 };
3170
3171 /*
3172 * Allow clipboard selection of text on the command line in "normal"
3173 * modes. Don't do this when dragging the status line, or extending a
3174 * Visual selection.
3175 */
3176 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003177 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 && button != MOUSE_DRAG
3179# ifdef FEAT_MOUSESHAPE
3180 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182# endif
3183 )
3184 checkfor = MOUSE_NONE;
3185
3186 /*
3187 * Use modeless selection when holding CTRL and SHIFT pressed.
3188 */
3189 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3190 checkfor = MOUSE_NONEF;
3191
3192 /*
3193 * In Ex mode, always use modeless selection.
3194 */
3195 if (exmode_active)
3196 checkfor = MOUSE_NONE;
3197
3198 /*
3199 * If the mouse settings say to not use the mouse, use the modeless
3200 * selection. But if Visual is active, assume that only the Visual area
3201 * will be selected.
3202 * Exception: On the command line, both the selection is used and a mouse
3203 * key is send.
3204 */
3205 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 /* Don't do modeless selection in Visual mode. */
3208 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 /*
3212 * When 'mousemodel' is "popup", shift-left is translated to right.
3213 * But not when also using Ctrl.
3214 */
3215 if (mouse_model_popup() && button == MOUSE_LEFT
3216 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3217 {
3218 button = MOUSE_RIGHT;
3219 modifiers &= ~ MOUSE_SHIFT;
3220 }
3221
3222 /* If the selection is done, allow the right button to extend it.
3223 * If the selection is cleared, allow the right button to start it
3224 * from the cursor position. */
3225 if (button == MOUSE_RIGHT)
3226 {
3227 if (clip_star.state == SELECT_CLEARED)
3228 {
3229 if (State & CMDLINE)
3230 {
3231 col = msg_col;
3232 row = msg_row;
3233 }
3234 else
3235 {
3236 col = curwin->w_wcol;
3237 row = curwin->w_wrow + W_WINROW(curwin);
3238 }
3239 clip_start_selection(col, row, FALSE);
3240 }
3241 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3242 repeated_click);
3243 did_clip = TRUE;
3244 }
3245 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003246 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3249 did_clip = TRUE;
3250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /* Always allow pasting */
3253 if (button != MOUSE_MIDDLE)
3254 {
3255 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3256 return;
3257 if (checkfor != MOUSE_COMMAND)
3258 button = MOUSE_LEFT;
3259 }
3260 repeated_click = FALSE;
3261 }
3262
3263 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003264 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#endif
3266
3267 /* Don't put events in the input queue now. */
3268 if (hold_gui_events)
3269 return;
3270
3271 row = gui_xy2colrow(x, y, &col);
3272
3273 /*
3274 * If we are dragging and the mouse hasn't moved far enough to be on a
3275 * different character, then don't send an event to vim.
3276 */
3277 if (button == MOUSE_DRAG)
3278 {
3279 if (row == prev_row && col == prev_col)
3280 return;
3281 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3282 if (y < 0)
3283 row = -1;
3284 }
3285
3286 /*
3287 * If topline has changed (window scrolled) since the last click, reset
3288 * repeated_click, because we don't want starting Visual mode when
3289 * clicking on a different character in the text.
3290 */
3291 if (curwin->w_topline != gui_prev_topline
3292#ifdef FEAT_DIFF
3293 || curwin->w_topfill != gui_prev_topfill
3294#endif
3295 )
3296 repeated_click = FALSE;
3297
3298 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3299 string[1] = KS_MOUSE;
3300 string[2] = KE_FILLER;
3301 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3302 {
3303 if (repeated_click)
3304 {
3305 /*
3306 * Handle multiple clicks. They only count if the mouse is still
3307 * pointing at the same character.
3308 */
3309 if (button != prev_button || row != prev_row || col != prev_col)
3310 num_clicks = 1;
3311 else if (++num_clicks > 4)
3312 num_clicks = 1;
3313 }
3314 else
3315 num_clicks = 1;
3316 prev_button = button;
3317 gui_prev_topline = curwin->w_topline;
3318#ifdef FEAT_DIFF
3319 gui_prev_topfill = curwin->w_topfill;
3320#endif
3321
3322 string[3] = (char_u)(button | 0x20);
3323 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3324 }
3325 else
3326 string[3] = (char_u)button;
3327
3328 string[3] |= modifiers;
3329 fill_mouse_coord(string + 4, col, row);
3330 add_to_input_buf(string, 8);
3331
3332 if (row < 0)
3333 prev_row = 0;
3334 else
3335 prev_row = row;
3336 prev_col = col;
3337
3338 /*
3339 * We need to make sure this is cleared since Athena doesn't tell us when
3340 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3341 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003342#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 gui.dragged_sb = SBAR_NONE;
3344#endif
3345}
3346
3347/*
3348 * Convert x and y coordinate to column and row in text window.
3349 * Corrects for multi-byte character.
3350 * returns column in "*colp" and row as return value;
3351 */
3352 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003353gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 int col = check_col(X_2_COL(x));
3356 int row = check_row(Y_2_ROW(y));
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return row;
3360}
3361
3362#if defined(FEAT_MENU) || defined(PROTO)
3363/*
3364 * Callback function for when a menu entry has been selected.
3365 */
3366 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003367gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003369 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 /* Don't put events in the input queue now. */
3372 if (hold_gui_events)
3373 return;
3374
3375 bytes[0] = CSI;
3376 bytes[1] = KS_MENU;
3377 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003378 add_to_input_buf(bytes, 3);
3379 add_long_to_buf((long_u)menu, bytes);
3380 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381}
3382#endif
3383
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003384static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386/*
3387 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003388 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * in p_go.
3390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003392gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#ifdef FEAT_MENU
3395 static int prev_menu_is_active = -1;
3396#endif
3397#ifdef FEAT_TOOLBAR
3398 static int prev_toolbar = -1;
3399 int using_toolbar = FALSE;
3400#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003401#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003402 int using_tabline;
3403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#ifdef FEAT_FOOTER
3405 static int prev_footer = -1;
3406 int using_footer = FALSE;
3407#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003408#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 static int prev_tearoff = -1;
3410 int using_tearoff = FALSE;
3411#endif
3412
3413 char_u *p;
3414 int i;
3415#ifdef FEAT_MENU
3416 int grey_old, grey_new;
3417 char_u *temp;
3418#endif
3419 win_T *wp;
3420 int need_set_size;
3421 int fix_size;
3422
3423#ifdef FEAT_MENU
3424 if (oldval != NULL && gui.in_use)
3425 {
3426 /*
3427 * Check if the menu's go from grey to non-grey or vise versa.
3428 */
3429 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3430 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3431 if (grey_old != grey_new)
3432 {
3433 temp = p_go;
3434 p_go = oldval;
3435 gui_update_menus(MENU_ALL_MODES);
3436 p_go = temp;
3437 }
3438 }
3439 gui.menu_is_active = FALSE;
3440#endif
3441
3442 for (i = 0; i < 3; i++)
3443 gui.which_scrollbars[i] = FALSE;
3444 for (p = p_go; *p; p++)
3445 switch (*p)
3446 {
3447 case GO_LEFT:
3448 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3449 break;
3450 case GO_RIGHT:
3451 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3452 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 case GO_VLEFT:
3454 if (win_hasvertsplit())
3455 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3456 break;
3457 case GO_VRIGHT:
3458 if (win_hasvertsplit())
3459 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 case GO_BOT:
3462 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3463 break;
3464#ifdef FEAT_MENU
3465 case GO_MENUS:
3466 gui.menu_is_active = TRUE;
3467 break;
3468#endif
3469 case GO_GREY:
3470 /* make menu's have grey items, ignored here */
3471 break;
3472#ifdef FEAT_TOOLBAR
3473 case GO_TOOLBAR:
3474 using_toolbar = TRUE;
3475 break;
3476#endif
3477#ifdef FEAT_FOOTER
3478 case GO_FOOTER:
3479 using_footer = TRUE;
3480 break;
3481#endif
3482 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 using_tearoff = TRUE;
3485#endif
3486 break;
3487 default:
3488 /* Ignore options that are not supported */
3489 break;
3490 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (gui.in_use)
3493 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003494 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003496
3497#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003498 /* Update the GUI tab line, it may appear or disappear. This may
3499 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003500 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003501 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003502 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003503 /* We don't want a resize event change "Rows" here, save and
3504 * restore it. Resizing is handled below. */
3505 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003506 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003507 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003508 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003509 if (using_tabline)
3510 fix_size = TRUE;
3511 if (!gui_use_tabline())
3512 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3513 }
3514#endif
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 for (i = 0; i < 3; i++)
3517 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003518 /* The scrollbar needs to be updated when it is shown/unshown and
3519 * when switching tab pages. But the size only changes when it's
3520 * shown/unshown. Thus we need two places to remember whether a
3521 * scrollbar is there or not. */
3522 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003523 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003524 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if (i == SBAR_BOTTOM)
3527 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3528 gui.which_scrollbars[i]);
3529 else
3530 {
3531 FOR_ALL_WINDOWS(wp)
3532 {
3533 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3534 }
3535 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003536 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3537 {
3538 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003539 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003540 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003541 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003542 if (gui.which_scrollbars[i])
3543 fix_size = TRUE;
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003546 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003547 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549
3550#ifdef FEAT_MENU
3551 if (gui.menu_is_active != prev_menu_is_active)
3552 {
3553 /* We don't want a resize event change "Rows" here, save and
3554 * restore it. Resizing is handled below. */
3555 i = Rows;
3556 gui_mch_enable_menu(gui.menu_is_active);
3557 Rows = i;
3558 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003559 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (gui.menu_is_active)
3561 fix_size = TRUE;
3562 }
3563#endif
3564
3565#ifdef FEAT_TOOLBAR
3566 if (using_toolbar != prev_toolbar)
3567 {
3568 gui_mch_show_toolbar(using_toolbar);
3569 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003570 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (using_toolbar)
3572 fix_size = TRUE;
3573 }
3574#endif
3575#ifdef FEAT_FOOTER
3576 if (using_footer != prev_footer)
3577 {
3578 gui_mch_enable_footer(using_footer);
3579 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003580 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (using_footer)
3582 fix_size = TRUE;
3583 }
3584#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003585#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (using_tearoff != prev_tearoff)
3587 {
3588 gui_mch_toggle_tearoffs(using_tearoff);
3589 prev_tearoff = using_tearoff;
3590 }
3591#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003592 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003593 {
3594#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003595 long prev_Columns = Columns;
3596 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 /* Adjust the size of the window to make the text area keep the
3599 * same size and to avoid that part of our window is off-screen
3600 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003601 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003602
3603#ifdef FEAT_GUI_GTK
3604 /* GTK has the annoying habit of sending us resize events when
3605 * changing the window size ourselves. This mostly happens when
3606 * waiting for a character to arrive, quite unpredictably, and may
3607 * change Columns and Rows when we don't want it. Wait for a
3608 * character here to avoid this effect.
3609 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003610 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003611 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003612 * Don't change Rows when adding menu/toolbar/tabline.
3613 * Don't change Columns when adding vertical toolbar. */
3614 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003615 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003616 if ((need_set_size & RESIZE_VERT) == 0)
3617 Rows = prev_Rows;
3618 if ((need_set_size & RESIZE_HOR) == 0)
3619 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003620#endif
3621 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003622 /* When the console tabline appears or disappears the window positions
3623 * change. */
3624 if (firstwin->w_winrow != tabline_height())
3625 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627}
3628
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003629#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3630/*
3631 * Return TRUE if the GUI is taking care of the tabline.
3632 * It may still be hidden if 'showtabline' is zero.
3633 */
3634 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003635gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003636{
3637 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3638}
3639
3640/*
3641 * Return TRUE if the GUI is showing the tabline.
3642 * This uses 'showtabline'.
3643 */
3644 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003645gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003646{
3647 if (!gui_use_tabline()
3648 || p_stal == 0
3649 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3650 return FALSE;
3651 return TRUE;
3652}
3653
3654/*
3655 * Update the tabline.
3656 * This may display/undisplay the tabline and update the labels.
3657 */
3658 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003659gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003660{
3661 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003662 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003663
3664 if (!gui.starting && starting == 0)
3665 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003666 /* Updating the tabline uses direct GUI commands, flush
3667 * outstanding instructions first. (esp. clear screen) */
3668 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003669
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003670 if (!showit != !shown)
3671 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003672 if (showit != 0)
3673 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003674
3675 /* When the tabs change from hidden to shown or from shown to
3676 * hidden the size of the text area should remain the same. */
3677 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003678 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003679 }
3680}
3681
3682/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003683 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684 */
3685 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003686get_tabline_label(
3687 tabpage_T *tp,
3688 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003689{
3690 int modified = FALSE;
3691 char_u buf[40];
3692 int wincount;
3693 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003694 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003695
Bram Moolenaar57657d82006-04-21 22:12:41 +00003696 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003697 opt = (tooltip ? &p_gtt : &p_gtl);
3698 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003699 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003700 int use_sandbox = FALSE;
3701 int save_called_emsg = called_emsg;
3702 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003703 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003704 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3705 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003706
3707 called_emsg = FALSE;
3708
3709 printer_page_num = tabpage_index(tp);
3710# ifdef FEAT_EVAL
3711 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003712 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003713# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003714 /* It's almost as going to the tabpage, but without autocommands. */
3715 curtab->tp_firstwin = firstwin;
3716 curtab->tp_lastwin = lastwin;
3717 curtab->tp_curwin = curwin;
3718 save_curtab = curtab;
3719 curtab = tp;
3720 topframe = curtab->tp_topframe;
3721 firstwin = curtab->tp_firstwin;
3722 lastwin = curtab->tp_lastwin;
3723 curwin = curtab->tp_curwin;
3724 curbuf = curwin->w_buffer;
3725
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003726 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003727 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003728 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003729 STRCPY(NameBuff, res);
3730
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003731 /* Back to the original curtab. */
3732 curtab = save_curtab;
3733 topframe = curtab->tp_topframe;
3734 firstwin = curtab->tp_firstwin;
3735 lastwin = curtab->tp_lastwin;
3736 curwin = curtab->tp_curwin;
3737 curbuf = curwin->w_buffer;
3738
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003739 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003740 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003741 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003742 called_emsg |= save_called_emsg;
3743 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003744
3745 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3746 * use a default label. */
3747 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003748 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003749 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003750 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003751 if (!tooltip)
3752 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003753
3754 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3755 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3756 if (bufIsChanged(wp->w_buffer))
3757 modified = TRUE;
3758 if (modified || wincount > 1)
3759 {
3760 if (wincount > 1)
3761 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3762 else
3763 buf[0] = NUL;
3764 if (modified)
3765 STRCAT(buf, "+");
3766 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003767 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003768 mch_memmove(NameBuff, buf, STRLEN(buf));
3769 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003770 }
3771}
3772
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003773/*
3774 * Send the event for clicking to select tab page "nr".
3775 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003776 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003777 */
3778 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003779send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003780{
3781 char_u string[3];
3782
3783 if (nr == tabpage_index(curtab))
3784 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003785
3786 /* Don't put events in the input queue now. */
3787 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003788# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003789 || cmdwin_type != 0
3790# endif
3791 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003792 {
3793 /* Set it back to the current tab page. */
3794 gui_mch_set_curtab(tabpage_index(curtab));
3795 return FALSE;
3796 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003797
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003798 string[0] = CSI;
3799 string[1] = KS_TABLINE;
3800 string[2] = KE_FILLER;
3801 add_to_input_buf(string, 3);
3802 string[0] = nr;
3803 add_to_input_buf_csi(string, 1);
3804 return TRUE;
3805}
3806
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003807/*
3808 * Send a tabline menu event
3809 */
3810 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003811send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003812{
3813 char_u string[3];
3814
Bram Moolenaar29547192018-12-11 20:39:19 +01003815 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003816 if (hold_gui_events)
3817 return;
3818
Bram Moolenaar29547192018-12-11 20:39:19 +01003819 // Cannot close the last tabpage.
3820 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3821 return;
3822
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003823 string[0] = CSI;
3824 string[1] = KS_TABMENU;
3825 string[2] = KE_FILLER;
3826 add_to_input_buf(string, 3);
3827 string[0] = tabidx;
3828 string[1] = (char_u)(long)event;
3829 add_to_input_buf_csi(string, 2);
3830}
3831
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834/*
3835 * Scrollbar stuff:
3836 */
3837
Bram Moolenaare45828b2006-02-15 22:12:56 +00003838/*
3839 * Remove all scrollbars. Used before switching to another tab page.
3840 */
3841 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003842gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003843{
3844 int i;
3845 win_T *wp;
3846
3847 for (i = 0; i < 3; i++)
3848 {
3849 if (i == SBAR_BOTTOM)
3850 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3851 else
3852 {
3853 FOR_ALL_WINDOWS(wp)
3854 {
3855 gui_do_scrollbar(wp, i, FALSE);
3856 }
3857 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003858 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003859 }
3860}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003863gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 static int sbar_ident = 0;
3866
3867 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3868 sb->wp = wp;
3869 sb->type = type;
3870 sb->value = 0;
3871#ifdef FEAT_GUI_ATHENA
3872 sb->pixval = 0;
3873#endif
3874 sb->size = 1;
3875 sb->max = 1;
3876 sb->top = 0;
3877 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 sb->status_height = 0;
3880 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3881}
3882
3883/*
3884 * Find the scrollbar with the given index.
3885 */
3886 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003887gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 win_T *wp;
3890
3891 if (gui.bottom_sbar.ident == ident)
3892 return &gui.bottom_sbar;
3893 FOR_ALL_WINDOWS(wp)
3894 {
3895 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3896 return &wp->w_scrollbars[SBAR_LEFT];
3897 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3898 return &wp->w_scrollbars[SBAR_RIGHT];
3899 }
3900 return NULL;
3901}
3902
3903/*
3904 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3905 *
3906 * For Win32, Macintosh and GTK+ 2:
3907 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3908 * you stop dragging the scrollbar. We get here each time the scrollbar is
3909 * dragged another pixel, but as far as the rest of vim goes, it thinks
3910 * we're just hanging in the call to DispatchMessage() in
3911 * process_message(). The DispatchMessage() call that hangs was passed a
3912 * mouse button click event in the scrollbar window. -- webb.
3913 *
3914 * Solution: Do the scrolling right here. But only when allowed.
3915 * Ignore the scrollbars while executing an external command or when there
3916 * are still characters to be processed.
3917 */
3918 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003919gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 int sb_num;
3923#ifdef USE_ON_FLY_SCROLL
3924 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# ifdef FEAT_DIFF
3927 int old_topfill = curwin->w_topfill;
3928# endif
3929#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003930 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 int byte_count;
3932#endif
3933
3934 if (sb == NULL)
3935 return;
3936
3937 /* Don't put events in the input queue now. */
3938 if (hold_gui_events)
3939 return;
3940
3941#ifdef FEAT_CMDWIN
3942 if (cmdwin_type != 0 && sb->wp != curwin)
3943 return;
3944#endif
3945
3946 if (still_dragging)
3947 {
3948 if (sb->wp == NULL)
3949 gui.dragged_sb = SBAR_BOTTOM;
3950 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3951 gui.dragged_sb = SBAR_LEFT;
3952 else
3953 gui.dragged_sb = SBAR_RIGHT;
3954 gui.dragged_wp = sb->wp;
3955 }
3956 else
3957 {
3958 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003959#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003961 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 gui.dragged_wp = NULL;
3963#endif
3964 }
3965
3966 /* Vertical sbar info is kept in the first sbar (the left one) */
3967 if (sb->wp != NULL)
3968 sb = &sb->wp->w_scrollbars[0];
3969
3970 /*
3971 * Check validity of value
3972 */
3973 if (value < 0)
3974 value = 0;
3975#ifdef SCROLL_PAST_END
3976 else if (value > sb->max)
3977 value = sb->max;
3978#else
3979 if (value > sb->max - sb->size + 1)
3980 value = sb->max - sb->size + 1;
3981#endif
3982
3983 sb->value = value;
3984
3985#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003986 /* When not allowed to do the scrolling right now, return.
3987 * This also checked input_available(), but that causes the first click in
3988 * a scrollbar to be ignored when Vim doesn't have focus. */
3989 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 return;
3991#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003992#ifdef FEAT_INS_EXPAND
3993 /* Disallow scrolling the current window when the completion popup menu is
3994 * visible. */
3995 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3996 return;
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999#ifdef FEAT_RIGHTLEFT
4000 if (sb->wp == NULL && curwin->w_p_rl)
4001 {
4002 value = sb->max + 1 - sb->size - value;
4003 if (value < 0)
4004 value = 0;
4005 }
4006#endif
4007
4008 if (sb->wp != NULL) /* vertical scrollbar */
4009 {
4010 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4012 sb_num++;
4013 if (wp == NULL)
4014 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016#ifdef USE_ON_FLY_SCROLL
4017 current_scrollbar = sb_num;
4018 scrollbar_value = value;
4019 if (State & NORMAL)
4020 {
4021 gui_do_scroll();
4022 setcursor();
4023 }
4024 else if (State & INSERT)
4025 {
4026 ins_scroll();
4027 setcursor();
4028 }
4029 else if (State & CMDLINE)
4030 {
4031 if (msg_scrolled == 0)
4032 {
4033 gui_do_scroll();
4034 redrawcmdline();
4035 }
4036 }
4037# ifdef FEAT_FOLDING
4038 /* Value may have been changed for closed fold. */
4039 sb->value = sb->wp->w_topline - 1;
4040# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004041
4042 /* When dragging one scrollbar and there is another one at the other
4043 * side move the thumb of that one too. */
4044 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4045 gui_mch_set_scrollbar_thumb(
4046 &sb->wp->w_scrollbars[
4047 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4048 ? SBAR_LEFT : SBAR_RIGHT],
4049 sb->value, sb->size, sb->max);
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#else
4052 bytes[0] = CSI;
4053 bytes[1] = KS_VER_SCROLLBAR;
4054 bytes[2] = KE_FILLER;
4055 bytes[3] = (char_u)sb_num;
4056 byte_count = 4;
4057#endif
4058 }
4059 else
4060 {
4061#ifdef USE_ON_FLY_SCROLL
4062 scrollbar_value = value;
4063
4064 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004065 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 else if (State & INSERT)
4067 ins_horscroll();
4068 else if (State & CMDLINE)
4069 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004070 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004072 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 redrawcmdline();
4074 }
4075 }
4076 if (old_leftcol != curwin->w_leftcol)
4077 {
4078 updateWindow(curwin); /* update window, status and cmdline */
4079 setcursor();
4080 }
4081#else
4082 bytes[0] = CSI;
4083 bytes[1] = KS_HOR_SCROLLBAR;
4084 bytes[2] = KE_FILLER;
4085 byte_count = 3;
4086#endif
4087 }
4088
4089#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 /*
4091 * synchronize other windows, as necessary according to 'scrollbind'
4092 */
4093 if (curwin->w_p_scb
4094 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4095 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004096# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 ))))
4100 {
4101 do_check_scrollbind(TRUE);
4102 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004103 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (wp->w_redr_type > 0)
4105 updateWindow(wp);
4106 setcursor();
4107 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004108 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004110 add_to_input_buf(bytes, byte_count);
4111 add_long_to_buf((long_u)value, bytes);
4112 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#endif
4114}
4115
4116/*
4117 * Scrollbar stuff:
4118 */
4119
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004120/*
4121 * Called when something in the window layout has changed.
4122 */
4123 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004124gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004125{
4126 if (gui.in_use && starting == 0)
4127 {
4128 out_flush();
4129 gui_init_which_components(NULL);
4130 gui_update_scrollbars(TRUE);
4131 }
4132 need_mouse_correct = TRUE;
4133}
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004136gui_update_scrollbars(
4137 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 win_T *wp;
4140 scrollbar_T *sb;
4141 long val, size, max; /* need 32 bits here */
4142 int which_sb;
4143 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 /* Update the horizontal scrollbar */
4147 gui_update_horiz_scrollbar(force);
4148
4149#ifndef WIN3264
4150 /* Return straight away if there is neither a left nor right scrollbar.
4151 * On MS-Windows this is required anyway for scrollwheel messages. */
4152 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4153 return;
4154#endif
4155
4156 /*
4157 * Don't want to update a scrollbar while we're dragging it. But if we
4158 * have both a left and right scrollbar, and we drag one of them, we still
4159 * need to update the other one.
4160 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004161 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4162 && gui.which_scrollbars[SBAR_LEFT]
4163 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 /*
4166 * If we have two scrollbars and one of them is being dragged, just
4167 * copy the scrollbar position from the dragged one to the other one.
4168 */
4169 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4170 if (gui.dragged_wp != NULL)
4171 gui_mch_set_scrollbar_thumb(
4172 &gui.dragged_wp->w_scrollbars[which_sb],
4173 gui.dragged_wp->w_scrollbars[0].value,
4174 gui.dragged_wp->w_scrollbars[0].size,
4175 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177
4178 /* avoid that moving components around generates events */
4179 ++hold_gui_events;
4180
Bram Moolenaar45a24952016-07-24 22:25:15 +02004181 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 if (wp->w_buffer == NULL) /* just in case */
4184 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185 /* Skip a scrollbar that is being dragged. */
4186 if (!force && (gui.dragged_sb == SBAR_LEFT
4187 || gui.dragged_sb == SBAR_RIGHT)
4188 && gui.dragged_wp == wp)
4189 continue;
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#ifdef SCROLL_PAST_END
4192 max = wp->w_buffer->b_ml.ml_line_count - 1;
4193#else
4194 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4195#endif
4196 if (max < 0) /* empty buffer */
4197 max = 0;
4198 val = wp->w_topline - 1;
4199 size = wp->w_height;
4200#ifdef SCROLL_PAST_END
4201 if (val > max) /* just in case */
4202 val = max;
4203#else
4204 if (size > max + 1) /* just in case */
4205 size = max + 1;
4206 if (val > max - size + 1)
4207 val = max - size + 1;
4208#endif
4209 if (val < 0) /* minimal value is 0 */
4210 val = 0;
4211
4212 /*
4213 * Scrollbar at index 0 (the left one) contains all the information.
4214 * It would be the same info for left and right so we just store it for
4215 * one of them.
4216 */
4217 sb = &wp->w_scrollbars[0];
4218
4219 /*
4220 * Note: no check for valid w_botline. If it's not valid the
4221 * scrollbars will be updated later anyway.
4222 */
4223 if (size < 1 || wp->w_botline - 2 > max)
4224 {
4225 /*
4226 * This can happen during changing files. Just don't update the
4227 * scrollbar for now.
4228 */
4229 sb->height = 0; /* Force update next time */
4230 if (gui.which_scrollbars[SBAR_LEFT])
4231 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4232 if (gui.which_scrollbars[SBAR_RIGHT])
4233 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4234 continue;
4235 }
4236 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 || sb->top != wp->w_winrow
4238 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004240 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
4242 /* Height, width or position of scrollbar has changed. For
4243 * vertical split: curwin changed. */
4244 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 sb->top = wp->w_winrow;
4246 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /* Calculate height and position in pixels */
4250 h = (sb->height + sb->status_height) * gui.char_height;
4251 y = sb->top * gui.char_height + gui.border_offset;
4252#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4253 if (gui.menu_is_active)
4254 y += gui.menu_height;
4255#endif
4256
4257#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4258 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4259# ifdef FEAT_GUI_ATHENA
4260 y += gui.toolbar_height;
4261# else
4262# ifdef FEAT_GUI_MSWIN
4263 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4264# endif
4265# endif
4266#endif
4267
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004268#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4269 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004270 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004271#endif
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 /* Height of top scrollbar includes width of top border */
4276 h += gui.border_offset;
4277 y -= gui.border_offset;
4278 }
4279 if (gui.which_scrollbars[SBAR_LEFT])
4280 {
4281 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4282 gui.left_sbar_x, y,
4283 gui.scrollbar_width, h);
4284 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4285 }
4286 if (gui.which_scrollbars[SBAR_RIGHT])
4287 {
4288 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4289 gui.right_sbar_x, y,
4290 gui.scrollbar_width, h);
4291 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4292 }
4293 }
4294
4295 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4296 * checking if the thumb moved at least a pixel. Only do this for
4297 * Athena, most other GUIs require the update anyway to make the
4298 * arrows work. */
4299#ifdef FEAT_GUI_ATHENA
4300 if (max == 0)
4301 y = 0;
4302 else
4303 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4304 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4305#else
4306 if (force || sb->value != val || sb->size != size || sb->max != max)
4307#endif
4308 {
4309 /* Thumb of scrollbar has moved */
4310 sb->value = val;
4311#ifdef FEAT_GUI_ATHENA
4312 sb->pixval = y;
4313#endif
4314 sb->size = size;
4315 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004316 if (gui.which_scrollbars[SBAR_LEFT]
4317 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4319 val, size, max);
4320 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004321 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4323 val, size, max);
4324 }
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 --hold_gui_events;
4328}
4329
4330/*
4331 * Enable or disable a scrollbar.
4332 * Check for scrollbars for vertically split windows which are not enabled
4333 * sometimes.
4334 */
4335 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004336gui_do_scrollbar(
4337 win_T *wp,
4338 int which, /* SBAR_LEFT or SBAR_RIGHT */
4339 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 int midcol = curwin->w_wincol + curwin->w_width / 2;
4342 int has_midcol = (wp->w_wincol <= midcol
4343 && wp->w_wincol + wp->w_width >= midcol);
4344
4345 /* Only enable scrollbars that contain the middle column of the current
4346 * window. */
4347 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4348 {
4349 /* Scrollbars only on one side. Don't enable scrollbars that don't
4350 * contain the middle column of the current window. */
4351 if (!has_midcol)
4352 enable = FALSE;
4353 }
4354 else
4355 {
4356 /* Scrollbars on both sides. Don't enable scrollbars that neither
4357 * contain the middle column of the current window nor are on the far
4358 * side. */
4359 if (midcol > Columns / 2)
4360 {
4361 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4362 enable = FALSE;
4363 }
4364 else
4365 {
4366 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4367 : !has_midcol)
4368 enable = FALSE;
4369 }
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4372}
4373
4374/*
4375 * Scroll a window according to the values set in the globals current_scrollbar
4376 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4377 * or FALSE otherwise.
4378 */
4379 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004380gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 win_T *wp, *save_wp;
4383 int i;
4384 long nlines;
4385 pos_T old_cursor;
4386 linenr_T old_topline;
4387#ifdef FEAT_DIFF
4388 int old_topfill;
4389#endif
4390
4391 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4392 if (wp == NULL)
4393 break;
4394 if (wp == NULL)
4395 /* Couldn't find window */
4396 return FALSE;
4397
4398 /*
4399 * Compute number of lines to scroll. If zero, nothing to do.
4400 */
4401 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4402 if (nlines == 0)
4403 return FALSE;
4404
4405 save_wp = curwin;
4406 old_topline = wp->w_topline;
4407#ifdef FEAT_DIFF
4408 old_topfill = wp->w_topfill;
4409#endif
4410 old_cursor = wp->w_cursor;
4411 curwin = wp;
4412 curbuf = wp->w_buffer;
4413 if (nlines < 0)
4414 scrolldown(-nlines, gui.dragged_wp == NULL);
4415 else
4416 scrollup(nlines, gui.dragged_wp == NULL);
4417 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4418 * the mouse-up event already, but we still want it to behave like when
4419 * dragging. But not the next click in an arrow. */
4420 if (gui.dragged_sb == SBAR_NONE)
4421 gui.dragged_wp = NULL;
4422
4423 if (old_topline != wp->w_topline
4424#ifdef FEAT_DIFF
4425 || old_topfill != wp->w_topfill
4426#endif
4427 )
4428 {
4429 if (p_so != 0)
4430 {
4431 cursor_correct(); /* fix window for 'so' */
4432 update_topline(); /* avoid up/down jump */
4433 }
4434 if (old_cursor.lnum != wp->w_cursor.lnum)
4435 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004439 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4440 validate_cursor();
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 curwin = save_wp;
4443 curbuf = save_wp->w_buffer;
4444
4445 /*
4446 * Don't call updateWindow() when nothing has changed (it will overwrite
4447 * the status line!).
4448 */
4449 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004450 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#ifdef FEAT_DIFF
4452 || old_topfill != wp->w_topfill
4453#endif
4454 )
4455 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004456 int type = VALID;
4457
4458#ifdef FEAT_INS_EXPAND
4459 if (pum_visible())
4460 {
4461 type = NOT_VALID;
4462 wp->w_lines_valid = 0;
4463 }
4464#endif
4465 /* Don't set must_redraw here, it may cause the popup menu to
4466 * disappear when losing focus after a scrollbar drag. */
4467 if (wp->w_redr_type < type)
4468 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004469 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004471 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004474#ifdef FEAT_INS_EXPAND
4475 /* May need to redraw the popup menu. */
4476 if (pum_visible())
4477 pum_redraw();
4478#endif
4479
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004480 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481}
4482
4483
4484/*
4485 * Horizontal scrollbar stuff:
4486 */
4487
4488/*
4489 * Return length of line "lnum" for horizontal scrolling.
4490 */
4491 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004492scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 char_u *p;
4495 colnr_T col;
4496 int w;
4497
4498 p = ml_get(lnum);
4499 col = 0;
4500 if (*p != NUL)
4501 for (;;)
4502 {
4503 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004504 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (*p == NUL) /* don't count the last character */
4506 break;
4507 col += w;
4508 }
4509 return col;
4510}
4511
4512/* Remember which line is currently the longest, so that we don't have to
4513 * search for it when scrolling horizontally. */
4514static linenr_T longest_lnum = 0;
4515
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004516/*
4517 * Find longest visible line number. If this is not possible (or not desired,
4518 * by setting 'h' in "guioptions") then the current line number is returned.
4519 */
4520 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004521gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004522{
4523 linenr_T ret = 0;
4524
4525 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4526 * line numbers, topline and botline can be invalid when displaying is
4527 * postponed. */
4528 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4529 && curwin->w_topline <= curwin->w_cursor.lnum
4530 && curwin->w_botline > curwin->w_cursor.lnum
4531 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4532 {
4533 linenr_T lnum;
4534 colnr_T n;
4535 long max = 0;
4536
4537 /* Use maximum of all visible lines. Remember the lnum of the
4538 * longest line, closest to the cursor line. Used when scrolling
4539 * below. */
4540 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4541 {
4542 n = scroll_line_len(lnum);
4543 if (n > (colnr_T)max)
4544 {
4545 max = n;
4546 ret = lnum;
4547 }
4548 else if (n == (colnr_T)max
4549 && abs((int)(lnum - curwin->w_cursor.lnum))
4550 < abs((int)(ret - curwin->w_cursor.lnum)))
4551 ret = lnum;
4552 }
4553 }
4554 else
4555 /* Use cursor line only. */
4556 ret = curwin->w_cursor.lnum;
4557
4558 return ret;
4559}
4560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004562gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563{
4564 long value, size, max; /* need 32 bit ints here */
4565
4566 if (!gui.which_scrollbars[SBAR_BOTTOM])
4567 return;
4568
4569 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4570 return;
4571
4572 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4573 return;
4574
4575 /*
4576 * It is possible for the cursor to be invalid if we're in the middle of
4577 * something (like changing files). If so, don't do anything for now.
4578 */
4579 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4580 {
4581 gui.bottom_sbar.value = -1;
4582 return;
4583 }
4584
Bram Moolenaar02631462017-09-22 15:20:32 +02004585 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (curwin->w_p_wrap)
4587 {
4588 value = 0;
4589#ifdef SCROLL_PAST_END
4590 max = 0;
4591#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004592 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593#endif
4594 }
4595 else
4596 {
4597 value = curwin->w_leftcol;
4598
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004599 longest_lnum = gui_find_longest_lnum();
4600 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (virtual_active())
4603 {
4604 /* May move the cursor even further to the right. */
4605 if (curwin->w_virtcol >= (colnr_T)max)
4606 max = curwin->w_virtcol;
4607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004610 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#endif
4612 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004613 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 size -= curwin_col_off();
4615#ifndef SCROLL_PAST_END
4616 max -= curwin_col_off();
4617#endif
4618 }
4619
4620#ifndef SCROLL_PAST_END
4621 if (value > max - size + 1)
4622 value = max - size + 1; /* limit the value to allowable range */
4623#endif
4624
4625#ifdef FEAT_RIGHTLEFT
4626 if (curwin->w_p_rl)
4627 {
4628 value = max + 1 - size - value;
4629 if (value < 0)
4630 {
4631 size += value;
4632 value = 0;
4633 }
4634 }
4635#endif
4636 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4637 && max == gui.bottom_sbar.max)
4638 return;
4639
4640 gui.bottom_sbar.value = value;
4641 gui.bottom_sbar.size = size;
4642 gui.bottom_sbar.max = max;
4643 gui.prev_wrap = curwin->w_p_wrap;
4644
4645 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4646}
4647
4648/*
4649 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4650 */
4651 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004652gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 /* no wrapping, no scrolling */
4655 if (curwin->w_p_wrap)
4656 return FALSE;
4657
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004658 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 return FALSE;
4660
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004661 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
4663 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004664 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004666 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004667 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004669 if (compute_longest_lnum)
4670 {
4671 curwin->w_cursor.lnum = gui_find_longest_lnum();
4672 curwin->w_cursor.col = 0;
4673 }
4674 /* Do a sanity check on "longest_lnum", just in case. */
4675 else if (longest_lnum >= curwin->w_topline
4676 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
4678 curwin->w_cursor.lnum = longest_lnum;
4679 curwin->w_cursor.col = 0;
4680 }
4681 }
4682
4683 return leftcol_changed();
4684}
4685
4686/*
4687 * Check that none of the colors are the same as the background color
4688 */
4689 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004690gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4693 {
4694 gui_set_bg_color((char_u *)"White");
4695 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4696 gui_set_fg_color((char_u *)"Black");
4697 }
4698}
4699
Bram Moolenaar3918c952005-03-15 22:34:55 +00004700 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004701gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 gui.norm_pixel = gui_get_color(name);
4704 hl_set_fg_color_name(vim_strsave(name));
4705}
4706
Bram Moolenaar3918c952005-03-15 22:34:55 +00004707 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004708gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 gui.back_pixel = gui_get_color(name);
4711 hl_set_bg_color_name(vim_strsave(name));
4712}
4713
4714/*
4715 * Allocate a color by name.
4716 * Returns INVALCOLOR and gives an error message when failed.
4717 */
4718 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004719gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 guicolor_T t;
4722
4723 if (*name == NUL)
4724 return INVALCOLOR;
4725 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004728#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 && gui.in_use
4730#endif
4731 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004732 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return t;
4734}
4735
4736/*
4737 * Return the grey value of a color (range 0-255).
4738 */
4739 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004740gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004742 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004744 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004745 + (((rgb >> 8) & 0xff) * 587)
4746 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
4749#if defined(FEAT_GUI_X11) || defined(PROTO)
4750 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004751gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 win_T *wp;
4754
4755 /* Nothing to do if GUI hasn't started yet. */
4756 if (!gui.in_use)
4757 return;
4758
4759 FOR_ALL_WINDOWS(wp)
4760 {
4761 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4762 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4763 }
4764 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4765}
4766#endif
4767
4768/*
4769 * Call this when focus has changed.
4770 */
4771 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004772gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774/*
4775 * Skip this code to avoid drawing the cursor when debugging and switching
4776 * between the debugger window and gvim.
4777 */
4778#if 1
4779 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004780 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782# ifdef FEAT_XIM
4783 xim_set_focus(in_focus);
4784# endif
4785
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004786 /* Put events in the input queue only when allowed.
4787 * ui_focus_change() isn't called directly, because it invokes
4788 * autocommands and that must not happen asynchronously. */
4789 if (!hold_gui_events)
4790 {
4791 char_u bytes[3];
4792
4793 bytes[0] = CSI;
4794 bytes[1] = KS_EXTRA;
4795 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4796 add_to_input_buf(bytes, 3);
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#endif
4799}
4800
4801/*
4802 * Called when the mouse moved (but not when dragging).
4803 */
4804 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004805gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004808 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaard3667a22006-03-16 21:35:52 +00004810 /* Ignore this while still starting up. */
4811 if (!gui.in_use || gui.starting)
4812 return;
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#ifdef FEAT_MOUSESHAPE
4815 /* Get window pointer, and update mouse shape as well. */
4816 wp = xy2win(x, y);
4817#endif
4818
4819 /* Only handle this when 'mousefocus' set and ... */
4820 if (p_mousef
4821 && !hold_gui_events /* not holding events */
4822 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4823 && State != HITRETURN /* but not hit-return prompt */
4824 && msg_scrolled == 0 /* no scrolled message */
4825 && !need_mouse_correct /* not moving the pointer */
4826 && gui.in_focus) /* gvim in focus */
4827 {
4828 /* Don't move the mouse when it's left or right of the Vim window */
4829 if (x < 0 || x > Columns * gui.char_width)
4830 return;
4831#ifndef FEAT_MOUSESHAPE
4832 wp = xy2win(x, y);
4833#endif
4834 if (wp == curwin || wp == NULL)
4835 return; /* still in the same old window, or none at all */
4836
Bram Moolenaar9c102382006-05-03 21:26:49 +00004837 /* Ignore position in the tab pages line. */
4838 if (Y_2_ROW(y) < tabline_height())
4839 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 /*
4842 * format a mouse click on status line input
4843 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004844 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4845 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
4847 if (finish_op)
4848 {
4849 /* abort the current operator first */
4850 st[0] = ESC;
4851 add_to_input_buf(st, 1);
4852 }
4853 st[0] = CSI;
4854 st[1] = KS_MOUSE;
4855 st[2] = KE_FILLER;
4856 st[3] = (char_u)MOUSE_LEFT;
4857 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004858 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 wp->w_height + W_WINROW(wp));
4860
4861 add_to_input_buf(st, 8);
4862 st[3] = (char_u)MOUSE_RELEASE;
4863 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#ifdef FEAT_GUI_GTK
4865 /* Need to wake up the main loop */
4866 if (gtk_main_level() > 0)
4867 gtk_main_quit();
4868#endif
4869 }
4870}
4871
4872/*
4873 * Called when mouse should be moved to window with focus.
4874 */
4875 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004876gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
4878 int x, y;
4879 win_T *wp = NULL;
4880
4881 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004882
4883 if (!(gui.in_use && p_mousef))
4884 return;
4885
4886 gui_mch_getmouse(&x, &y);
4887 /* Don't move the mouse when it's left or right of the Vim window */
4888 if (x < 0 || x > Columns * gui.char_width)
4889 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004890 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004891 wp = xy2win(x, y);
4892 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004894 validate_cline_row();
4895 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4896 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899}
4900
4901/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004902 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004905xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 int row;
4908 int col;
4909 win_T *wp;
4910
4911 row = Y_2_ROW(y);
4912 col = X_2_COL(x);
4913 if (row < 0 || col < 0) /* before first window */
4914 return NULL;
4915 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004916 if (wp == NULL)
4917 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004918#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (State == HITRETURN || State == ASKMORE)
4920 {
4921 if (Y_2_ROW(y) >= msg_row)
4922 update_mouseshape(SHAPE_IDX_MOREL);
4923 else
4924 update_mouseshape(SHAPE_IDX_MORE);
4925 }
4926 else if (row > wp->w_height) /* below status line */
4927 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004928 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004929 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004931 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004932 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 update_mouseshape(SHAPE_IDX_STATUS);
4934 else
4935 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004937 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
4939
4940/*
4941 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4942 * File names may be given to redefine the args list.
4943 */
4944 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004945ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 char_u *arg = eap->arg;
4948
4949 /*
4950 * Check for "-f" argument: foreground, don't fork.
4951 * Also don't fork when started with "gvim -f".
4952 * Do fork when using "gui -b".
4953 */
4954 if (arg[0] == '-'
4955 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004956 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 gui.dofork = (arg[1] == 'b');
4959 eap->arg = skipwhite(eap->arg + 2);
4960 }
4961 if (!gui.in_use)
4962 {
4963 /* Clear the command. Needed for when forking+exiting, to avoid part
4964 * of the argument ending up after the shell prompt. */
4965 msg_clr_eos_force();
4966 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004967#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004968 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 if (!ends_excmd(*eap->arg))
4972 ex_next(eap);
4973}
4974
4975#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004976 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977/*
4978 * This is shared between Athena, Motif and GTK.
4979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981/*
4982 * Callback function for do_in_runtimepath().
4983 */
4984 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004985gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004987 char_u *gfp_buffer = cookie;
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (STRLEN(fname) >= MAXPATHL)
4990 *gfp_buffer = NUL;
4991 else
4992 STRCPY(gfp_buffer, fname);
4993}
4994
4995/*
4996 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4997 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4998 */
4999 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005000gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 if (STRLEN(name) > MAXPATHL - 14)
5003 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005004 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005005 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 return FAIL;
5008 return OK;
5009}
5010
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005011# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012/*
5013 * Given the name of the "icon=" argument, try finding the bitmap file for the
5014 * icon. If it is an absolute path name, use it as it is. Otherwise append
5015 * "ext" and search for it in 'runtimepath'.
5016 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5017 * contains "name".
5018 */
5019 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005020gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
5022 char_u buf[MAXPATHL + 1];
5023
5024 expand_env(name, buffer, MAXPATHL);
5025 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5026 STRCPY(buffer, buf);
5027}
5028# endif
5029#endif
5030
Bram Moolenaar9372a112005-12-06 19:59:18 +00005031#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005033display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 char_u *p;
5036
5037 if (isatty(2))
5038 fflush(stderr);
5039 else if (error_ga.ga_data != NULL)
5040 {
5041 /* avoid putting up a message box with blanks only */
5042 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5043 if (!isspace(*p))
5044 {
5045 /* Truncate a very long message, it will go off-screen. */
5046 if (STRLEN(p) > 2000)
5047 STRCPY(p + 2000 - 14, "...(truncated)");
5048 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005049 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 break;
5051 }
5052 ga_clear(&error_ga);
5053 }
5054}
5055#endif
5056
5057#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5058/*
5059 * Return TRUE if still starting up and there is no place to enter text.
5060 * For GTK and X11 we check if stderr is not a tty, which means we were
5061 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5062 * allow typing on stdin.
5063 */
5064 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005065no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
5067 return ((!gui.in_use || gui.starting)
5068# ifndef NO_CONSOLE
5069 && !isatty(0) && !isatty(2)
5070# endif
5071 );
5072}
5073#endif
5074
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005075#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005076 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005077 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078/*
5079 * Update the current window and the screen.
5080 */
5081 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005082gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005084# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005085 linenr_T conceal_old_cursor_line = 0;
5086 linenr_T conceal_new_cursor_line = 0;
5087 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005088# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 update_topline();
5091 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005092
Bram Moolenaarec80df72008-05-07 19:46:51 +00005093 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005094 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005095# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005096 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005097# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005098 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005099 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005100 if (has_cursormoved())
5101 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005102# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005103 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005104 {
5105 conceal_old_cursor_line = last_cursormoved.lnum;
5106 conceal_new_cursor_line = curwin->w_cursor.lnum;
5107 conceal_update_lines = TRUE;
5108 }
5109# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005110 last_cursormoved = curwin->w_cursor;
5111 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005112
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005113# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005114 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005115 && (conceal_old_cursor_line != conceal_new_cursor_line
5116 || conceal_cursor_line(curwin)
5117 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005119 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005120 redrawWinline(curwin, conceal_old_cursor_line);
5121 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005122 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005123 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005124 }
5125# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005126 update_screen(0); /* may need to update the screen */
5127 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005128 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129}
5130#endif
5131
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005132#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133/*
5134 * Get the text to use in a find/replace dialog. Uses the last search pattern
5135 * if the argument is empty.
5136 * Returns an allocated string.
5137 */
5138 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005139get_find_dialog_text(
5140 char_u *arg,
5141 int *wwordp, /* return: TRUE if \< \> found */
5142 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 char_u *text;
5145
5146 if (*arg == NUL)
5147 text = last_search_pat();
5148 else
5149 text = arg;
5150 if (text != NULL)
5151 {
5152 text = vim_strsave(text);
5153 if (text != NULL)
5154 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005155 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 int i;
5157
5158 /* Remove "\V" */
5159 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5160 {
5161 mch_memmove(text, text + 2, (size_t)(len - 1));
5162 len -= 2;
5163 }
5164
5165 /* Recognize "\c" and "\C" and remove. */
5166 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5167 {
5168 *mcasep = (text[1] == 'C');
5169 mch_memmove(text, text + 2, (size_t)(len - 1));
5170 len -= 2;
5171 }
5172
5173 /* Recognize "\<text\>" and remove. */
5174 if (len >= 4
5175 && STRNCMP(text, "\\<", 2) == 0
5176 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5177 {
5178 *wwordp = TRUE;
5179 mch_memmove(text, text + 2, (size_t)(len - 4));
5180 text[len - 4] = NUL;
5181 }
5182
5183 /* Recognize "\/" or "\?" and remove. */
5184 for (i = 0; i + 1 < len; ++i)
5185 if (text[i] == '\\' && (text[i + 1] == '/'
5186 || text[i + 1] == '?'))
5187 {
5188 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5189 --len;
5190 }
5191 }
5192 }
5193 return text;
5194}
5195
5196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 * Handle the press of a button in the find-replace dialog.
5198 * Return TRUE when something was added to the input buffer.
5199 */
5200 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005201gui_do_findrepl(
5202 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5203 char_u *find_text,
5204 char_u *repl_text,
5205 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207 garray_T ga;
5208 int i;
5209 int type = (flags & FRD_TYPE_MASK);
5210 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005211 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005212 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005213 static int busy = FALSE;
5214
5215 /* When the screen is being updated we should not change buffers and
5216 * windows structures, it may cause freed memory to be used. Also don't
5217 * do this recursively (pressing "Find" quickly several times. */
5218 if (updating_screen || busy)
5219 return FALSE;
5220
5221 /* refuse replace when text cannot be changed */
5222 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5223 return FALSE;
5224
5225 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005228 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 ga_concat(&ga, (char_u *)"%s/");
5230
5231 ga_concat(&ga, (char_u *)"\\V");
5232 if (flags & FRD_MATCH_CASE)
5233 ga_concat(&ga, (char_u *)"\\C");
5234 else
5235 ga_concat(&ga, (char_u *)"\\c");
5236 if (flags & FRD_WHOLE_WORD)
5237 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005238 /* escape / and \ */
5239 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5240 if (p != NULL)
5241 ga_concat(&ga, p);
5242 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (flags & FRD_WHOLE_WORD)
5244 ga_concat(&ga, (char_u *)"\\>");
5245
5246 if (type == FRD_REPLACEALL)
5247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005249 /* escape / and \ */
5250 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5251 if (p != NULL)
5252 ga_concat(&ga, p);
5253 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005255 }
5256 ga_append(&ga, NUL);
5257
5258 if (type == FRD_REPLACE)
5259 {
5260 /* Do the replacement when the text at the cursor matches. Thus no
5261 * replacement is done if the cursor was moved! */
5262 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5263 regmatch.rm_ic = 0;
5264 if (regmatch.regprog != NULL)
5265 {
5266 p = ml_get_cursor();
5267 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5268 && regmatch.startp[0] == p)
5269 {
5270 /* Clear the command line to remove any old "No match"
5271 * error. */
5272 msg_end_prompt();
5273
5274 if (u_save_cursor() == OK)
5275 {
5276 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005277 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005278
5279 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005280 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005281 ins_str(repl_text);
5282 }
5283 }
5284 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005285 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005286 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005287 }
5288 }
5289
5290 if (type == FRD_REPLACEALL)
5291 {
5292 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005293 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 do_cmdline_cmd(ga.ga_data);
5295 }
5296 else
5297 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005298 int searchflags = SEARCH_MSG + SEARCH_MARK;
5299
5300 /* Search for the next match.
5301 * Don't skip text under cursor for single replace. */
5302 if (type == FRD_REPLACE)
5303 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005305 if (down)
5306 {
5307 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5308 }
5309 else
5310 {
5311 /* We need to escape '?' if and only if we are searching in the up
5312 * direction */
5313 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5314 if (p != NULL)
5315 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5316 vim_free(p);
5317 }
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 msg_scroll = i; /* don't let an error message set msg_scroll */
5320 }
5321
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005322 /* Don't want to pass did_emsg to other code, it may cause disabling
5323 * syntax HL if we were busy redrawing. */
5324 did_emsg = save_did_emsg;
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 if (State & (NORMAL | INSERT))
5327 {
5328 gui_update_screen(); /* update the screen */
5329 msg_didout = 0; /* overwrite any message */
5330 need_wait_return = FALSE; /* don't wait for return */
5331 }
5332
5333 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005334 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 return (ga.ga_len > 0);
5336}
5337
5338#endif
5339
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005340#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341/*
5342 * Jump to the window at specified point (x, y).
5343 */
5344 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005345gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 int row = Y_2_ROW(y);
5348 int col = X_2_COL(x);
5349 win_T *wp;
5350
5351 if (row >= 0 && col >= 0)
5352 {
5353 wp = mouse_find_win(&row, &col);
5354 if (wp != NULL && wp != curwin)
5355 win_goto(wp);
5356 }
5357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
5359/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005360 * Function passed to handle_drop() for the actions to be done after the
5361 * argument list has been updated.
5362 */
5363 static void
5364drop_callback(void *cookie)
5365{
5366 char_u *p = cookie;
5367
5368 /* If Shift held down, change to first file's directory. If the first
5369 * item is a directory, change to that directory (and let the explorer
5370 * plugin show the contents). */
5371 if (p != NULL)
5372 {
5373 if (mch_isdir(p))
5374 {
5375 if (mch_chdir((char *)p) == 0)
5376 shorten_fnames(TRUE);
5377 }
5378 else if (vim_chdirfile(p, "drop") == OK)
5379 shorten_fnames(TRUE);
5380 vim_free(p);
5381 }
5382
5383 /* Update the screen display */
5384 update_screen(NOT_VALID);
5385# ifdef FEAT_MENU
5386 gui_update_menus(0);
5387# endif
5388#ifdef FEAT_TITLE
5389 maketitle();
5390#endif
5391 setcursor();
5392 out_flush_cursor(FALSE, FALSE);
5393}
5394
5395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 * Process file drop. Mouse cursor position, key modifiers, name of files
5397 * and count of files are given. Argument "fnames[count]" has full pathnames
5398 * of dropped files, they will be freed in this function, and caller can't use
5399 * fnames after call this function.
5400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005402gui_handle_drop(
5403 int x UNUSED,
5404 int y UNUSED,
5405 int_u modifiers,
5406 char_u **fnames,
5407 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 int i;
5410 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005411 static int entered = FALSE;
5412
5413 /*
5414 * This function is called by event handlers. Just in case we get a
5415 * second event before the first one is handled, ignore the second one.
5416 * Not sure if this can ever happen, just in case.
5417 */
5418 if (entered)
5419 return;
5420 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 /*
5423 * When the cursor is at the command line, add the file names to the
5424 * command line, don't edit the files.
5425 */
5426 if (State & CMDLINE)
5427 {
5428 shorten_filenames(fnames, count);
5429 for (i = 0; i < count; ++i)
5430 {
5431 if (fnames[i] != NULL)
5432 {
5433 if (i > 0)
5434 add_to_input_buf((char_u*)" ", 1);
5435
5436 /* We don't know what command is used thus we can't be sure
5437 * about which characters need to be escaped. Only escape the
5438 * most common ones. */
5439# ifdef BACKSLASH_IN_FILENAME
5440 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5441# else
5442 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5443# endif
5444 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005445 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 vim_free(p);
5447 vim_free(fnames[i]);
5448 }
5449 }
5450 vim_free(fnames);
5451 }
5452 else
5453 {
5454 /* Go to the window under mouse cursor, then shorten given "fnames" by
5455 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 shorten_filenames(fnames, count);
5458
5459 /* If Shift held down, remember the first item. */
5460 if ((modifiers & MOUSE_SHIFT) != 0)
5461 p = vim_strsave(fnames[0]);
5462 else
5463 p = NULL;
5464
5465 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005466 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 * directory and Shift is held down. */
5468 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5469 && mch_isdir(fnames[0]))
5470 {
5471 vim_free(fnames[0]);
5472 vim_free(fnames);
5473 }
5474 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005475 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5476 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005478
5479 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480}
5481#endif