blob: b961108ed87a4df1253eaf6c080b414d6801380a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
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 {
218 EMSG(_("E851: Failed to create a new process for the GUI"));
219 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
242 EMSG(_("E852: The child process failed to start the GUI"));
243 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)
365 EMSG(_("E229: Cannot start the GUI"));
366 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#ifdef FEAT_MBYTE
401 gui.wide_font = NOFONT;
402# ifndef FEAT_GUI_GTK
403 gui.wide_bold_font = NOFONT;
404 gui.wide_ital_font = NOFONT;
405 gui.wide_boldital_font = NOFONT;
406# endif
407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200410# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411# ifdef FONTSET_ALWAYS
412 gui.menu_fontset = NOFONTSET;
413# else
414 gui.menu_font = NOFONT;
415# endif
416# endif
417 gui.menu_is_active = TRUE; /* default: include menu */
418# ifndef FEAT_GUI_GTK
419 gui.menu_height = MENU_DEFAULT_HEIGHT;
420 gui.menu_width = 0;
421# endif
422#endif
423#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
424 gui.toolbar_height = 0;
425#endif
426#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
427 gui.footer_height = 0;
428#endif
429#ifdef FEAT_BEVAL_TIP
430 gui.tooltip_fontset = NOFONTSET;
431#endif
432
433 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
434 gui.prev_wrap = -1;
435
436#ifdef ALWAYS_USE_GUI
437 result = OK;
438#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200439# ifdef FEAT_GUI_GTK
440 /*
441 * Note: Don't call gtk_init_check() before fork, it will be called after
442 * the fork. When calling it before fork, it make vim hang for a while.
443 * See gui_do_fork().
444 * Use a simpler check if the GUI window can probably be opened.
445 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200446 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#endif
451 return result;
452}
453
454/*
455 * This is the call which starts the GUI.
456 */
457 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100458gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 win_T *wp;
461 static int recursive = 0;
462
463 /*
464 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
465 * function will then be executed at the first call, the rest by the
466 * recursive call. This allow the shell to be opened halfway reading a
467 * gvimrc file.
468 */
469 if (!recursive)
470 {
471 ++recursive;
472
473 clip_init(TRUE);
474
475 /* If can't initialize, don't try doing the rest */
476 if (gui_init_check() == FAIL)
477 {
478 --recursive;
479 clip_init(FALSE);
480 return;
481 }
482
483 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000484 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
485 * breaks the Paste toolbar button.
486 */
487 set_option_value((char_u *)"paste", 0L, NULL, 0);
488
489 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Set up system-wide default menus.
491 */
492#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
493 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
494 {
495 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000496 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 sys_menu = FALSE;
498 }
499#endif
500
501 /*
502 * Switch on the mouse by default, unless the user changed it already.
503 * This can then be changed in the .gvimrc.
504 */
505 if (!option_was_set((char_u *)"mouse"))
506 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000507 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
509 /*
510 * If -U option given, use only the initializations from that file and
511 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
512 */
513 if (use_gvimrc != NULL)
514 {
515 if (STRCMP(use_gvimrc, "NONE") != 0
516 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000517 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
519 }
520 else
521 {
522 /*
523 * Get system wide defaults for gvim, only when file name defined.
524 */
525#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000526 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#endif
528
529 /*
530 * Try to read GUI initialization commands from the following
531 * places:
532 * - environment variable GVIMINIT
533 * - the user gvimrc file (~/.gvimrc)
534 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
535 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
536 * The first that exists is used, the rest is ignored.
537 */
538 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000539 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
540 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000542 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
543 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200545#ifdef USR_GVIMRC_FILE3
546 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
547 DOSO_GVIMRC) == FAIL
548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 )
550 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200551#ifdef USR_GVIMRC_FILE4
552 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#endif
554 }
555
556 /*
557 * Read initialization commands from ".gvimrc" in current
558 * directory. This is only done if the 'exrc' option is set.
559 * Because of security reasons we disallow shell and write
560 * commands now, except for unix if the file is owned by the user
561 * or 'secure' option has been reset in environment of global
562 * ".gvimrc".
563 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
564 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
565 */
566 if (p_exrc)
567 {
568#ifdef UNIX
569 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200570 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 /* if ".gvimrc" file is not owned by user, set 'secure'
573 * mode */
574 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
575 secure = p_secure;
576 }
577#else
578 secure = p_secure;
579#endif
580
581 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
582 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
583#ifdef SYS_GVIMRC_FILE
584 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
585 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
586#endif
587#ifdef USR_GVIMRC_FILE2
588 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
589 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
590#endif
591#ifdef USR_GVIMRC_FILE3
592 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
593 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
594#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200595#ifdef USR_GVIMRC_FILE4
596 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
597 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000600 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602 if (secure == 2)
603 need_wait_return = TRUE;
604 secure = 0;
605 }
606 }
607
608 if (need_wait_return || msg_didany)
609 wait_return(TRUE);
610
611 --recursive;
612 }
613
614 /* If recursive call opened the shell, return here from the first call */
615 if (gui.in_use)
616 return;
617
618 /*
619 * Create the GUI shell.
620 */
621 gui.in_use = TRUE; /* Must be set after menus have been set up */
622 if (gui_mch_init() == FAIL)
623 goto error;
624
625 /* Avoid a delay for an error message that was printed in the terminal
626 * where Vim was started. */
627 emsg_on_display = FALSE;
628 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100629 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 need_wait_return = FALSE;
631 msg_didany = FALSE;
632
633 /*
634 * Check validity of any generic resources that may have been loaded.
635 */
636 if (gui.border_width < 0)
637 gui.border_width = 0;
638
639 /*
640 * Set up the fonts. First use a font specified with "-fn" or "-font".
641 */
642 if (font_argument != NULL)
643 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
644 if (
645#ifdef FEAT_XFONTSET
646 (*p_guifontset == NUL
647 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
648#endif
649 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
650 : p_guifont, FALSE) == FAIL)
651 {
652 EMSG(_("E665: Cannot start GUI, no valid font found"));
653 goto error2;
654 }
655#ifdef FEAT_MBYTE
656 if (gui_get_wide_font() == FAIL)
657 EMSG(_("E231: 'guifontwide' invalid"));
658#endif
659
660 gui.num_cols = Columns;
661 gui.num_rows = Rows;
662 gui_reset_scroll_region();
663
664 /* Create initial scrollbars */
665 FOR_ALL_WINDOWS(wp)
666 {
667 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
668 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
669 }
670 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
671
672#ifdef FEAT_MENU
673 gui_create_initial_menus(root_menu);
674#endif
675#ifdef FEAT_SUN_WORKSHOP
676 if (usingSunWorkShop)
677 workshop_init();
678#endif
679#ifdef FEAT_SIGN_ICONS
680 sign_gui_started();
681#endif
682
683 /* Configure the desired menu and scrollbars */
684 gui_init_which_components(NULL);
685
686 /* All components of the GUI have been created now */
687 gui.shell_created = TRUE;
688
689#ifndef FEAT_GUI_GTK
690 /* Set the shell size, adjusted for the screen size. For GTK this only
691 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100692 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#endif
694#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
695 /* Need to set the size of the menubar after all the menus have been
696 * created. */
697 gui_mch_compute_menu_height((Widget)0);
698#endif
699
700 /*
701 * Actually open the GUI shell.
702 */
703 if (gui_mch_open() != FAIL)
704 {
705#ifdef FEAT_TITLE
706 maketitle();
707 resettitle();
708#endif
709 init_gui_options();
710#ifdef FEAT_ARABIC
711 /* Our GUI can't do bidi. */
712 p_tbidi = FALSE;
713#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000714#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 /* Give GTK+ a chance to put all widget's into place. */
716 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000717
718# ifdef FEAT_MENU
719 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
720 * It was still there to make F10 work. */
721 if (vim_strchr(p_go, GO_MENUS) == NULL)
722 {
723 --gui.starting;
724 gui_mch_enable_menu(FALSE);
725 ++gui.starting;
726 gui_mch_update();
727 }
728# endif
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 /* Now make sure the shell fits on the screen. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100731 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000733 /* When 'lines' was set while starting up the topframe may have to be
734 * resized. */
735 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000736
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100737#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000738 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100739 * 'ballooneval' is off. */
740 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200741 {
742# ifdef FEAT_VARTABS
743 vim_free(balloonEval->vts);
744# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100745 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200746 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100747 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000748# ifdef FEAT_GUI_GTK
749 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
750 &general_beval_cb, NULL);
751# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000752# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000753 {
754 extern Widget textArea;
755 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000756 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000757 }
758# else
759# ifdef FEAT_GUI_W32
760 balloonEval = gui_mch_create_beval_area(NULL, NULL,
761 &general_beval_cb, NULL);
762# endif
763# endif
764# endif
765 if (!p_beval)
766 gui_mch_disable_beval_area(balloonEval);
767#endif
768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
770 if (!im_xim_isvalid_imactivate())
771 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
772#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000773 /* When 'cmdheight' was set during startup it may not have taken
774 * effect yet. */
775 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000776 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 return;
779 }
780
781error2:
782#ifdef FEAT_GUI_X11
783 /* undo gui_mch_init() */
784 gui_mch_uninit();
785#endif
786
787error:
788 gui.in_use = FALSE;
789 clip_init(FALSE);
790}
791
792
793 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100794gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 /* don't free the fonts, it leads to a BUS error
797 * richard@whitequeen.com Jul 99 */
798 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 gui.in_use = FALSE;
800 gui_mch_exit(rc);
801}
802
Bram Moolenaar9372a112005-12-06 19:59:18 +0000803#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000805# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806/*
807 * Called when the GUI shell is closed by the user. If there are no changed
808 * files Vim exits, otherwise there will be a dialog to ask the user what to
809 * do.
810 * When this function returns, Vim should NOT exit!
811 */
812 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100813gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 cmdmod_T save_cmdmod;
816
817 save_cmdmod = cmdmod;
818
819 /* Only exit when there are no changed files */
820 exiting = TRUE;
821# ifdef FEAT_BROWSE
822 cmdmod.browse = TRUE;
823# endif
824# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
825 cmdmod.confirm = TRUE;
826# endif
827 /* If there are changed buffers, present the user with a dialog if
828 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100829 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 getout(0);
831
832 exiting = FALSE;
833 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000834 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835}
836#endif
837
838/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200839 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 * first font name that works is used. If none is found, use the default
841 * font.
842 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
843 * Return OK when able to set the font. When it failed FAIL is returned and
844 * the fonts are unchanged.
845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100847gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849#define FONTLEN 320
850 char_u font_name[FONTLEN];
851 int font_list_empty = FALSE;
852 int ret = FAIL;
853
854 if (!gui.in_use)
855 return FAIL;
856
857 font_name[0] = NUL;
858 if (*font_list == NUL)
859 font_list_empty = TRUE;
860 else
861 {
862#ifdef FEAT_XFONTSET
863 /* When using a fontset, the whole list of fonts is one name. */
864 if (fontset)
865 ret = gui_mch_init_font(font_list, TRUE);
866 else
867#endif
868 while (*font_list != NUL)
869 {
870 /* Isolate one comma separated font name. */
871 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
872
873 /* Careful!!! The Win32 version of gui_mch_init_font(), when
874 * called with "*" will change p_guifont to the selected font
875 * name, which frees the old value. This makes font_list
876 * invalid. Thus when OK is returned here, font_list must no
877 * longer be used! */
878 if (gui_mch_init_font(font_name, FALSE) == OK)
879 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200880#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 /* If it's a Unicode font, try setting 'guifontwide' to a
882 * similar double-width font. */
883 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
884 && strstr((char *)font_name, "10646") != NULL)
885 set_guifontwide(font_name);
886#endif
887 ret = OK;
888 break;
889 }
890 }
891 }
892
893 if (ret != OK
894 && STRCMP(font_list, "*") != 0
895 && (font_list_empty || gui.norm_font == NOFONT))
896 {
897 /*
898 * Couldn't load any font in 'font_list', keep the current font if
899 * there is one. If 'font_list' is empty, or if there is no current
900 * font, tell gui_mch_init_font() to try to find a font we can load.
901 */
902 ret = gui_mch_init_font(NULL, FALSE);
903 }
904
905 if (ret == OK)
906 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200907#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 /* Set normal font as current font */
909# ifdef FEAT_XFONTSET
910 if (gui.fontset != NOFONTSET)
911 gui_mch_set_fontset(gui.fontset);
912 else
913# endif
914 gui_mch_set_font(gui.norm_font);
915#endif
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100916 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 return ret;
920}
921
922#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200923# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924/*
925 * Try setting 'guifontwide' to a font twice as wide as "name".
926 */
927 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100928set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 int i = 0;
931 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
932 char_u *wp = NULL;
933 char_u *p;
934 GuiFont font;
935
936 wp = wide_name;
937 for (p = name; *p != NUL; ++p)
938 {
939 *wp++ = *p;
940 if (*p == '-')
941 {
942 ++i;
943 if (i == 6) /* font type: change "--" to "-*-" */
944 {
945 if (p[1] == '-')
946 *wp++ = '*';
947 }
948 else if (i == 12) /* found the width */
949 {
950 ++p;
951 i = getdigits(&p);
952 if (i != 0)
953 {
954 /* Double the width specification. */
955 sprintf((char *)wp, "%d%s", i * 2, p);
956 font = gui_mch_get_font(wide_name, FALSE);
957 if (font != NOFONT)
958 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000959 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 gui.wide_font = font;
961 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000962 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
964 }
965 break;
966 }
967 }
968 }
969}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200970# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972/*
973 * Get the font for 'guifontwide'.
974 * Return FAIL for an invalid font name.
975 */
976 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100977gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 GuiFont font = NOFONT;
980 char_u font_name[FONTLEN];
981 char_u *p;
982
983 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
984 return OK; /* Will give an error message later. */
985
986 if (p_guifontwide != NULL && *p_guifontwide != NUL)
987 {
988 for (p = p_guifontwide; *p != NUL; )
989 {
990 /* Isolate one comma separated font name. */
991 (void)copy_option_part(&p, font_name, FONTLEN, ",");
992 font = gui_mch_get_font(font_name, FALSE);
993 if (font != NOFONT)
994 break;
995 }
996 if (font == NOFONT)
997 return FAIL;
998 }
999
1000 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001001# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1003 if (font != NOFONT && gui.norm_font != NOFONT
1004 && pango_font_description_equal(font, gui.norm_font))
1005 {
1006 gui.wide_font = NOFONT;
1007 gui_mch_free_font(font);
1008 }
1009 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001012# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001013 gui_mch_wide_font_changed();
Bram Moolenaardb250522013-06-17 22:43:25 +02001014# else
1015 /*
1016 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1017 * support those fonts for 'guifontwide'.
1018 */
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 return OK;
1021}
1022#endif
1023
1024 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001025gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
1027 gui.row = row;
1028 gui.col = col;
1029}
1030
1031/*
1032 * gui_check_pos - check if the cursor is on the screen.
1033 */
1034 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001035gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037 if (gui.row >= screen_Rows)
1038 gui.row = screen_Rows - 1;
1039 if (gui.col >= screen_Columns)
1040 gui.col = screen_Columns - 1;
1041 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1042 gui.cursor_is_valid = FALSE;
1043}
1044
1045/*
1046 * Redraw the cursor if necessary or when forced.
1047 * Careful: The contents of ScreenLines[] must match what is on the screen,
1048 * otherwise this goes wrong. May need to call out_flush() first.
1049 */
1050 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001051gui_update_cursor(
1052 int force, /* when TRUE, update even when not moved */
1053 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 int cur_width = 0;
1056 int cur_height = 0;
1057 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001058 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001060#ifdef FEAT_TERMINAL
1061 guicolor_T shape_fg = INVALCOLOR;
1062 guicolor_T shape_bg = INVALCOLOR;
1063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1065 int cattr; /* cursor attributes */
1066 int attr;
1067 attrentry_T *aep = NULL;
1068
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001069 /* Don't update the cursor when halfway busy scrolling or the screen size
1070 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1071 if (!can_update_cursor || screen_Columns != gui.num_cols
1072 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 return;
1074
1075 gui_check_pos();
1076 if (!gui.cursor_is_valid || force
1077 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1078 {
1079 gui_undraw_cursor();
1080 if (gui.row < 0)
1081 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001082#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1084 im_set_position(gui.row, gui.col);
1085#endif
1086 gui.cursor_row = gui.row;
1087 gui.cursor_col = gui.col;
1088
1089 /* Only write to the screen after ScreenLines[] has been initialized */
1090 if (!screen_cleared || ScreenLines == NULL)
1091 return;
1092
1093 /* Clear the selection if we are about to write over it */
1094 if (clear_selection)
1095 clip_may_clear_selection(gui.row, gui.row);
1096 /* Check that the cursor is inside the shell (resizing may have made
1097 * it invalid) */
1098 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1099 return;
1100
1101 gui.cursor_is_valid = TRUE;
1102
1103 /*
1104 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001105 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001107#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001108 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001109 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001111#endif
1112 shape = &shape_table[get_shape_idx(FALSE)];
1113 if (State & LANGMAP)
1114 id = shape->id_lm;
1115 else
1116 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 /* get the colors and attributes for the cursor. Default is inverted */
1119 cfg = INVALCOLOR;
1120 cbg = INVALCOLOR;
1121 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001122 gui_mch_set_blinking(shape->blinkwait,
1123 shape->blinkon,
1124 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001125 if (shape->blinkwait == 0 || shape->blinkon == 0
1126 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001127 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001128#ifdef FEAT_TERMINAL
1129 if (shape_bg != INVALCOLOR)
1130 {
1131 cattr = 0;
1132 cfg = shape_fg;
1133 cbg = shape_bg;
1134 }
1135 else
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (id > 0)
1138 {
1139 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001140#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 static int iid;
1143 guicolor_T fg, bg;
1144
Bram Moolenaarc236c162008-07-13 17:41:49 +00001145 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001146# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001147 preedit_get_status()
1148# else
1149 im_get_status()
1150# endif
1151 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 iid = syn_name2id((char_u *)"CursorIM");
1154 if (iid > 0)
1155 {
1156 syn_id2colors(iid, &fg, &bg);
1157 if (bg != INVALCOLOR)
1158 cbg = bg;
1159 if (fg != INVALCOLOR)
1160 cfg = fg;
1161 }
1162 }
1163 }
1164#endif
1165 }
1166
1167 /*
1168 * Get the attributes for the character under the cursor.
1169 * When no cursor color was given, use the character color.
1170 */
1171 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1172 if (attr > HL_ALL)
1173 aep = syn_gui_attr2entry(attr);
1174 if (aep != NULL)
1175 {
1176 attr = aep->ae_attr;
1177 if (cfg == INVALCOLOR)
1178 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1179 : aep->ae_u.gui.fg_color);
1180 if (cbg == INVALCOLOR)
1181 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1182 : aep->ae_u.gui.bg_color);
1183 }
1184 if (cfg == INVALCOLOR)
1185 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1186 if (cbg == INVALCOLOR)
1187 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1188
1189#ifdef FEAT_XIM
1190 if (aep != NULL)
1191 {
1192 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1193 : aep->ae_u.gui.bg_color);
1194 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1195 : aep->ae_u.gui.fg_color);
1196 if (xim_bg_color == INVALCOLOR)
1197 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1198 : gui.back_pixel;
1199 if (xim_fg_color == INVALCOLOR)
1200 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1201 : gui.norm_pixel;
1202 }
1203 else
1204 {
1205 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1206 : gui.back_pixel;
1207 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1208 : gui.norm_pixel;
1209 }
1210#endif
1211
1212 attr &= ~HL_INVERSE;
1213 if (cattr & HL_INVERSE)
1214 {
1215 cc = cbg;
1216 cbg = cfg;
1217 cfg = cc;
1218 }
1219 cattr &= ~HL_INVERSE;
1220
1221 /*
1222 * When we don't have window focus, draw a hollow cursor.
1223 */
1224 if (!gui.in_focus)
1225 {
1226 gui_mch_draw_hollow_cursor(cbg);
1227 return;
1228 }
1229
1230 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001231 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#ifdef FEAT_HANGULIN
1233 || composing_hangul
1234#endif
1235 )
1236 {
1237 /*
1238 * Draw the text character with the cursor colors. Use the
1239 * character attributes plus the cursor attributes.
1240 */
1241 gui.highlight_mask = (cattr | attr);
1242#ifdef FEAT_HANGULIN
1243 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001244 {
1245 char_u *comp_buf;
1246 int comp_len;
1247
1248 comp_buf = hangul_composing_buffer_get(&comp_len);
1249 if (comp_buf)
1250 {
1251 (void)gui_outstr_nowrap(comp_buf, comp_len,
1252 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1253 cfg, cbg, 0);
1254 vim_free(comp_buf);
1255 }
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 else
1258#endif
1259 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1260 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1261 }
1262 else
1263 {
1264#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1265 int col_off = FALSE;
1266#endif
1267 /*
1268 * First draw the partial cursor, then overwrite with the text
1269 * character, using a transparent background.
1270 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001271 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
1273 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001274 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 else
1277 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001278 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 cur_width = gui.char_width;
1280 }
1281#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001282 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1283 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
1285 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001286 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 cur_width += gui.char_width;
1288# ifdef FEAT_RIGHTLEFT
1289 if (CURSOR_BAR_RIGHT)
1290 {
1291 /* gui.col points to the left halve of the character but
1292 * the vertical line needs to be on the right halve.
1293 * A double-wide horizontal line is also drawn from the
1294 * right halve in gui_mch_draw_part_cursor(). */
1295 col_off = TRUE;
1296 ++gui.col;
1297 }
1298# endif
1299 }
1300#endif
1301 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1302#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1303 if (col_off)
1304 --gui.col;
1305#endif
1306
1307#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1308 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1309 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1310 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1311 (guicolor_T)0, (guicolor_T)0, 0);
1312#endif
1313 }
1314 gui.highlight_mask = old_hl_mask;
1315 }
1316}
1317
1318#if defined(FEAT_MENU) || defined(PROTO)
1319 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001320gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001322# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (gui.menu_is_active && gui.in_use)
1324 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1325# endif
1326}
1327#endif
1328
1329/*
1330 * Position the various GUI components (text area, menu). The vertical
1331 * scrollbars are NOT handled here. See gui_update_scrollbars().
1332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001334gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 int text_area_x;
1337 int text_area_y;
1338 int text_area_width;
1339 int text_area_height;
1340
1341 /* avoid that moving components around generates events */
1342 ++hold_gui_events;
1343
1344 text_area_x = 0;
1345 if (gui.which_scrollbars[SBAR_LEFT])
1346 text_area_x += gui.scrollbar_width;
1347
1348 text_area_y = 0;
1349#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1350 gui.menu_width = total_width;
1351 if (gui.menu_is_active)
1352 text_area_y += gui.menu_height;
1353#endif
1354#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1355 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1356 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1357#endif
1358
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001359# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001360 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001361 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001362 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1366 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1367 {
1368# ifdef FEAT_GUI_ATHENA
1369 gui_mch_set_toolbar_pos(0, text_area_y,
1370 gui.menu_width, gui.toolbar_height);
1371# endif
1372 text_area_y += gui.toolbar_height;
1373 }
1374#endif
1375
1376 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1377 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1378
1379 gui_mch_set_text_area_pos(text_area_x,
1380 text_area_y,
1381 text_area_width,
1382 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001383#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 + xim_get_status_area_height()
1385#endif
1386 );
1387#ifdef FEAT_MENU
1388 gui_position_menu();
1389#endif
1390 if (gui.which_scrollbars[SBAR_BOTTOM])
1391 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1392 text_area_x,
1393 text_area_y + text_area_height,
1394 text_area_width,
1395 gui.scrollbar_height);
1396 gui.left_sbar_x = 0;
1397 gui.right_sbar_x = text_area_x + text_area_width;
1398
1399 --hold_gui_events;
1400}
1401
Bram Moolenaar02743632005-07-25 20:42:36 +00001402/*
1403 * Get the width of the widgets and decorations to the side of the text area.
1404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001406gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 int base_width;
1409
1410 base_width = 2 * gui.border_offset;
1411 if (gui.which_scrollbars[SBAR_LEFT])
1412 base_width += gui.scrollbar_width;
1413 if (gui.which_scrollbars[SBAR_RIGHT])
1414 base_width += gui.scrollbar_width;
1415 return base_width;
1416}
1417
Bram Moolenaar02743632005-07-25 20:42:36 +00001418/*
1419 * Get the height of the widgets and decorations above and below the text area.
1420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001422gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 int base_height;
1425
1426 base_height = 2 * gui.border_offset;
1427 if (gui.which_scrollbars[SBAR_BOTTOM])
1428 base_height += gui.scrollbar_height;
1429#ifdef FEAT_GUI_GTK
1430 /* We can't take the sizes properly into account until anything is
1431 * realized. Therefore we recalculate all the values here just before
1432 * setting the size. (--mdcki) */
1433#else
1434# ifdef FEAT_MENU
1435 if (gui.menu_is_active)
1436 base_height += gui.menu_height;
1437# endif
1438# ifdef FEAT_TOOLBAR
1439 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1440# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1441 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1442# else
1443 base_height += gui.toolbar_height;
1444# endif
1445# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001446# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1447 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001448 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001449 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451# ifdef FEAT_FOOTER
1452 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1453 base_height += gui.footer_height;
1454# endif
1455# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1456 base_height += gui_mch_text_area_extra_height();
1457# endif
1458#endif
1459 return base_height;
1460}
1461
1462/*
1463 * Should be called after the GUI shell has been resized. Its arguments are
1464 * the new width and height of the shell in pixels.
1465 */
1466 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001467gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 static int busy = FALSE;
1470
1471 if (!gui.shell_created) /* ignore when still initializing */
1472 return;
1473
1474 /*
1475 * Can't resize the screen while it is being redrawn. Remember the new
1476 * size and handle it later.
1477 */
1478 if (updating_screen || busy)
1479 {
1480 new_pixel_width = pixel_width;
1481 new_pixel_height = pixel_height;
1482 return;
1483 }
1484
1485again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001486 new_pixel_width = 0;
1487 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 busy = TRUE;
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 /* Flush pending output before redrawing */
1491 out_flush();
1492
1493 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001494 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 /*
1500 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1501 * at the last line here (why does it have to be one row too low?).
1502 */
1503 if (State == ASKMORE || State == CONFIRM)
1504 gui.row = gui.num_rows;
1505
1506 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1507 * the safe side. */
1508 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1509 || gui.num_rows != Rows || gui.num_cols != Columns)
1510 shell_resized();
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 gui_update_scrollbars(TRUE);
1513 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001514#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 xim_set_status_area();
1516#endif
1517
1518 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001519
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001520 /* We may have been called again while redrawing the screen.
1521 * Need to do it all again with the latest size then. But only if the size
1522 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 if (new_pixel_height)
1524 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001525 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1526 {
1527 new_pixel_width = 0;
1528 new_pixel_height = 0;
1529 }
1530 else
1531 {
1532 pixel_width = new_pixel_width;
1533 pixel_height = new_pixel_height;
1534 goto again;
1535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537}
1538
1539/*
1540 * Check if gui_resize_shell() must be called.
1541 */
1542 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001543gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 /* careful: gui_resize_shell() may postpone the resize again if we
1547 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001548 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549}
1550
1551 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001552gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 Rows = gui.num_rows;
1555 Columns = gui.num_cols;
1556 return OK;
1557}
1558
1559/*
1560 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001561 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1562 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001563 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1564 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001567gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001568 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001569 int fit_to_display,
1570 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 int base_width;
1573 int base_height;
1574 int width;
1575 int height;
1576 int min_width;
1577 int min_height;
1578 int screen_w;
1579 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001580#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001581 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001582 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001583#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001584 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 if (!gui.shell_created)
1587 return;
1588
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001589#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 /* If not setting to a user specified size and maximized, calculate the
1591 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001592 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1593 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 gui_mch_newfont();
1596 return;
1597 }
1598#endif
1599
1600 base_width = gui_get_base_width();
1601 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001602 if (fit_to_display)
1603 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001604 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606#ifdef USE_SUN_WORKSHOP
1607 if (!mustset && usingSunWorkShop
1608 && workshop_get_width_height(&width, &height))
1609 {
1610 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1611 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1612 }
1613 else
1614#endif
1615 {
1616 width = Columns * gui.char_width + base_width;
1617 height = Rows * gui.char_height + base_height;
1618 }
1619
1620 if (fit_to_display)
1621 {
1622 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001623 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 Columns = (screen_w - base_width) / gui.char_width;
1626 if (Columns < MIN_COLUMNS)
1627 Columns = MIN_COLUMNS;
1628 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001629#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001630 ++did_adjust;
1631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001633 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 Rows = (screen_h - base_height) / gui.char_height;
1636 check_shellsize();
1637 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001638#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001639 ++did_adjust;
1640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001642#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001643 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1644 && height + gui.char_height >= screen_h))
1645 /* don't unmaximize if at maximum size */
1646 un_maximize = FALSE;
1647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001649 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 gui.num_cols = Columns;
1651 gui.num_rows = Rows;
1652
1653 min_width = base_width + MIN_COLUMNS * gui.char_width;
1654 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001655 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001656
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001657#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001658 if (un_maximize)
1659 {
1660 /* If the window size is smaller than the screen unmaximize the
1661 * window, otherwise resizing won't work. */
1662 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1663 if ((width + gui.char_width < screen_w
1664 || height + gui.char_height * 2 < screen_h)
1665 && gui_mch_maximized())
1666 gui_mch_unmaximize();
1667 }
1668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001671 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001673 if (fit_to_display && x >= 0 && y >= 0)
1674 {
1675 /* Some window managers put the Vim window left of/above the screen.
1676 * Only change the position if it wasn't already negative before
1677 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 gui_mch_update();
1679 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1680 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1681 }
1682
1683 gui_position_components(width);
1684 gui_update_scrollbars(TRUE);
1685 gui_reset_scroll_region();
1686}
1687
1688/*
1689 * Called when Rows and/or Columns has changed.
1690 */
1691 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001692gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 gui_reset_scroll_region();
1695}
1696
1697/*
1698 * Make scroll region cover whole screen.
1699 */
1700 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001701gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 gui.scroll_region_top = 0;
1704 gui.scroll_region_bot = gui.num_rows - 1;
1705 gui.scroll_region_left = 0;
1706 gui.scroll_region_right = gui.num_cols - 1;
1707}
1708
1709 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001710gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 if (mask > HL_ALL) /* highlight code */
1713 gui.highlight_mask = mask;
1714 else /* mask */
1715 gui.highlight_mask |= mask;
1716}
1717
1718 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001719gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 if (mask > HL_ALL) /* highlight code */
1722 gui.highlight_mask = HL_NORMAL;
1723 else /* mask */
1724 gui.highlight_mask &= ~mask;
1725}
1726
1727/*
1728 * Clear a rectangular region of the screen from text pos (row1, col1) to
1729 * (row2, col2) inclusive.
1730 */
1731 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001732gui_clear_block(
1733 int row1,
1734 int col1,
1735 int row2,
1736 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
1738 /* Clear the selection if we are about to write over it */
1739 clip_may_clear_selection(row1, row2);
1740
1741 gui_mch_clear_block(row1, col1, row2, col2);
1742
1743 /* Invalidate cursor if it was in this block */
1744 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1745 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1746 gui.cursor_is_valid = FALSE;
1747}
1748
1749/*
1750 * Write code to update the cursor later. This avoids the need to flush the
1751 * output buffer before calling gui_update_cursor().
1752 */
1753 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001754gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001756 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757}
1758
1759 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001760gui_write(
1761 char_u *s,
1762 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 char_u *p;
1765 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 int force_scrollbar = FALSE;
1768 static win_T *old_curwin = NULL;
1769
1770/* #define DEBUG_GUI_WRITE */
1771#ifdef DEBUG_GUI_WRITE
1772 {
1773 int i;
1774 char_u *str;
1775
1776 printf("gui_write(%d):\n ", len);
1777 for (i = 0; i < len; i++)
1778 if (s[i] == ESC)
1779 {
1780 if (i != 0)
1781 printf("\n ");
1782 printf("<ESC>");
1783 }
1784 else
1785 {
1786 str = transchar_byte(s[i]);
1787 if (str[0] && str[1])
1788 printf("<%s>", (char *)str);
1789 else
1790 printf("%s", (char *)str);
1791 }
1792 printf("\n");
1793 }
1794#endif
1795 while (len)
1796 {
1797 if (s[0] == ESC && s[1] == '|')
1798 {
1799 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001800 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 arg1 = getdigits(&p);
1803 if (p > s + len)
1804 break;
1805 if (*p == ';')
1806 {
1807 ++p;
1808 arg2 = getdigits(&p);
1809 if (p > s + len)
1810 break;
1811 }
1812 }
1813 switch (*p)
1814 {
1815 case 'C': /* Clear screen */
1816 clip_scroll_selection(9999);
1817 gui_mch_clear_all();
1818 gui.cursor_is_valid = FALSE;
1819 force_scrollbar = TRUE;
1820 break;
1821 case 'M': /* Move cursor */
1822 gui_set_cursor(arg1, arg2);
1823 break;
1824 case 's': /* force cursor (shape) update */
1825 force_cursor = TRUE;
1826 break;
1827 case 'R': /* Set scroll region */
1828 if (arg1 < arg2)
1829 {
1830 gui.scroll_region_top = arg1;
1831 gui.scroll_region_bot = arg2;
1832 }
1833 else
1834 {
1835 gui.scroll_region_top = arg2;
1836 gui.scroll_region_bot = arg1;
1837 }
1838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 case 'V': /* Set vertical scroll region */
1840 if (arg1 < arg2)
1841 {
1842 gui.scroll_region_left = arg1;
1843 gui.scroll_region_right = arg2;
1844 }
1845 else
1846 {
1847 gui.scroll_region_left = arg2;
1848 gui.scroll_region_right = arg1;
1849 }
1850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 case 'd': /* Delete line */
1852 gui_delete_lines(gui.row, 1);
1853 break;
1854 case 'D': /* Delete lines */
1855 gui_delete_lines(gui.row, arg1);
1856 break;
1857 case 'i': /* Insert line */
1858 gui_insert_lines(gui.row, 1);
1859 break;
1860 case 'I': /* Insert lines */
1861 gui_insert_lines(gui.row, arg1);
1862 break;
1863 case '$': /* Clear to end-of-line */
1864 gui_clear_block(gui.row, gui.col, gui.row,
1865 (int)Columns - 1);
1866 break;
1867 case 'h': /* Turn on highlighting */
1868 gui_start_highlight(arg1);
1869 break;
1870 case 'H': /* Turn off highlighting */
1871 gui_stop_highlight(arg1);
1872 break;
1873 case 'f': /* flash the window (visual bell) */
1874 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1875 break;
1876 default:
1877 p = s + 1; /* Skip the ESC */
1878 break;
1879 }
1880 len -= (int)(++p - s);
1881 s = p;
1882 }
1883 else if (
1884#ifdef EBCDIC
1885 CtrlChar(s[0]) != 0 /* Ctrl character */
1886#else
1887 s[0] < 0x20 /* Ctrl character */
1888#endif
1889#ifdef FEAT_SIGN_ICONS
1890 && s[0] != SIGN_BYTE
1891# ifdef FEAT_NETBEANS_INTG
1892 && s[0] != MULTISIGN_BYTE
1893# endif
1894#endif
1895 )
1896 {
1897 if (s[0] == '\n') /* NL */
1898 {
1899 gui.col = 0;
1900 if (gui.row < gui.scroll_region_bot)
1901 gui.row++;
1902 else
1903 gui_delete_lines(gui.scroll_region_top, 1);
1904 }
1905 else if (s[0] == '\r') /* CR */
1906 {
1907 gui.col = 0;
1908 }
1909 else if (s[0] == '\b') /* Backspace */
1910 {
1911 if (gui.col)
1912 --gui.col;
1913 }
1914 else if (s[0] == Ctrl_L) /* cursor-right */
1915 {
1916 ++gui.col;
1917 }
1918 else if (s[0] == Ctrl_G) /* Beep */
1919 {
1920 gui_mch_beep();
1921 }
1922 /* Other Ctrl character: shouldn't happen! */
1923
1924 --len; /* Skip this char */
1925 ++s;
1926 }
1927 else
1928 {
1929 p = s;
1930 while (len > 0 && (
1931#ifdef EBCDIC
1932 CtrlChar(*p) == 0
1933#else
1934 *p >= 0x20
1935#endif
1936#ifdef FEAT_SIGN_ICONS
1937 || *p == SIGN_BYTE
1938# ifdef FEAT_NETBEANS_INTG
1939 || *p == MULTISIGN_BYTE
1940# endif
1941#endif
1942 ))
1943 {
1944 len--;
1945 p++;
1946 }
1947 gui_outstr(s, (int)(p - s));
1948 s = p;
1949 }
1950 }
1951
1952 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1953 * set). */
1954 if (force_cursor)
1955 gui_update_cursor(TRUE, TRUE);
1956
1957 /* When switching to another window the dragging must have stopped.
1958 * Required for GTK, dragged_sb isn't reset. */
1959 if (old_curwin != curwin)
1960 gui.dragged_sb = SBAR_NONE;
1961
1962 /* Update the scrollbars after clearing the screen or when switched
1963 * to another window.
1964 * Update the horizontal scrollbar always, it's difficult to check all
1965 * situations where it might change. */
1966 if (force_scrollbar || old_curwin != curwin)
1967 gui_update_scrollbars(force_scrollbar);
1968 else
1969 gui_update_horiz_scrollbar(FALSE);
1970 old_curwin = curwin;
1971
1972 /*
1973 * We need to make sure this is cleared since Athena doesn't tell us when
1974 * he is done dragging. Do the same for GTK.
1975 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001976#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 gui.dragged_sb = SBAR_NONE;
1978#endif
1979
Bram Moolenaara338adc2018-01-31 20:51:47 +01001980 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981}
1982
1983/*
1984 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1985 * produces wrong results. Call gui_dont_update_cursor() before that code and
1986 * gui_can_update_cursor() afterwards.
1987 */
1988 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001989gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 if (gui.in_use)
1992 {
1993 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001994 if (undraw)
1995 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 can_update_cursor = FALSE;
1997 }
1998}
1999
2000 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002001gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 can_update_cursor = TRUE;
2004 /* No need to update the cursor right now, there is always more output
2005 * after scrolling. */
2006}
2007
Bram Moolenaara338adc2018-01-31 20:51:47 +01002008/*
2009 * Disable issuing gui_mch_flush().
2010 */
2011 void
2012gui_disable_flush(void)
2013{
2014 ++disable_flush;
2015}
2016
2017/*
2018 * Enable issuing gui_mch_flush().
2019 */
2020 void
2021gui_enable_flush(void)
2022{
2023 --disable_flush;
2024}
2025
2026/*
2027 * Issue gui_mch_flush() if it is not disabled.
2028 */
2029 void
2030gui_may_flush(void)
2031{
2032 if (disable_flush == 0)
2033 gui_mch_flush();
2034}
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002037gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 int this_len;
2040#ifdef FEAT_MBYTE
2041 int cells;
2042#endif
2043
2044 if (len == 0)
2045 return;
2046
2047 if (len < 0)
2048 len = (int)STRLEN(s);
2049
2050 while (len > 0)
2051 {
2052#ifdef FEAT_MBYTE
2053 if (has_mbyte)
2054 {
2055 /* Find out how many chars fit in the current line. */
2056 cells = 0;
2057 for (this_len = 0; this_len < len; )
2058 {
2059 cells += (*mb_ptr2cells)(s + this_len);
2060 if (gui.col + cells > Columns)
2061 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002062 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 if (this_len > len)
2065 this_len = len; /* don't include following composing char */
2066 }
2067 else
2068#endif
2069 if (gui.col + len > Columns)
2070 this_len = Columns - gui.col;
2071 else
2072 this_len = len;
2073
2074 (void)gui_outstr_nowrap(s, this_len,
2075 0, (guicolor_T)0, (guicolor_T)0, 0);
2076 s += this_len;
2077 len -= this_len;
2078#ifdef FEAT_MBYTE
2079 /* fill up for a double-width char that doesn't fit. */
2080 if (len > 0 && gui.col < Columns)
2081 (void)gui_outstr_nowrap((char_u *)" ", 1,
2082 0, (guicolor_T)0, (guicolor_T)0, 0);
2083#endif
2084 /* The cursor may wrap to the next line. */
2085 if (gui.col >= Columns)
2086 {
2087 gui.col = 0;
2088 gui.row++;
2089 }
2090 }
2091}
2092
2093/*
2094 * Output one character (may be one or two display cells).
2095 * Caller must check for valid "off".
2096 * Returns FAIL or OK, just like gui_outstr_nowrap().
2097 */
2098 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002099gui_screenchar(
2100 int off, /* Offset from start of screen */
2101 int flags,
2102 guicolor_T fg, /* colors for cursor */
2103 guicolor_T bg, /* colors for cursor */
2104 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105{
2106#ifdef FEAT_MBYTE
2107 char_u buf[MB_MAXBYTES + 1];
2108
2109 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2110 if (enc_utf8 && ScreenLines[off] == 0)
2111 return OK;
2112
2113 if (enc_utf8 && ScreenLinesUC[off] != 0)
2114 /* Draw UTF-8 multi-byte character. */
2115 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2116 flags, fg, bg, back);
2117
2118 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2119 {
2120 buf[0] = ScreenLines[off];
2121 buf[1] = ScreenLines2[off];
2122 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2123 }
2124
2125 /* Draw non-multi-byte character or DBCS character. */
2126 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002127 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 flags, fg, bg, back);
2129#else
2130 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2131#endif
2132}
2133
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002134#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135/*
2136 * Output the string at the given screen position. This is used in place
2137 * of gui_screenchar() where possible because Pango needs as much context
2138 * as possible to work nicely. It's a lot faster as well.
2139 */
2140 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002141gui_screenstr(
2142 int off, /* Offset from start of screen */
2143 int len, /* string length in screen cells */
2144 int flags,
2145 guicolor_T fg, /* colors for cursor */
2146 guicolor_T bg, /* colors for cursor */
2147 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 char_u *buf;
2150 int outlen = 0;
2151 int i;
2152 int retval;
2153
2154 if (len <= 0) /* "cannot happen"? */
2155 return OK;
2156
2157 if (enc_utf8)
2158 {
2159 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2160 if (buf == NULL)
2161 return OK; /* not much we could do here... */
2162
2163 for (i = off; i < off + len; ++i)
2164 {
2165 if (ScreenLines[i] == 0)
2166 continue; /* skip second half of double-width char */
2167
2168 if (ScreenLinesUC[i] == 0)
2169 buf[outlen++] = ScreenLines[i];
2170 else
2171 outlen += utfc_char2bytes(i, buf + outlen);
2172 }
2173
2174 buf[outlen] = NUL; /* only to aid debugging */
2175 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2176 vim_free(buf);
2177
2178 return retval;
2179 }
2180 else if (enc_dbcs == DBCS_JPNU)
2181 {
2182 buf = alloc((unsigned)(len * 2 + 1));
2183 if (buf == NULL)
2184 return OK; /* not much we could do here... */
2185
2186 for (i = off; i < off + len; ++i)
2187 {
2188 buf[outlen++] = ScreenLines[i];
2189
2190 /* handle double-byte single-width char */
2191 if (ScreenLines[i] == 0x8e)
2192 buf[outlen++] = ScreenLines2[i];
2193 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2194 buf[outlen++] = ScreenLines[++i];
2195 }
2196
2197 buf[outlen] = NUL; /* only to aid debugging */
2198 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2199 vim_free(buf);
2200
2201 return retval;
2202 }
2203 else
2204 {
2205 return gui_outstr_nowrap(&ScreenLines[off], len,
2206 flags, fg, bg, back);
2207 }
2208}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002209#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
2211/*
2212 * Output the given string at the current cursor position. If the string is
2213 * too long to fit on the line, then it is truncated.
2214 * "flags":
2215 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2216 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002217 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 * background.
2219 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2220 * it.
2221 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2222 * FAIL (the caller should start drawing "back" chars back).
2223 */
2224 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002225gui_outstr_nowrap(
2226 char_u *s,
2227 int len,
2228 int flags,
2229 guicolor_T fg, /* colors for cursor */
2230 guicolor_T bg, /* colors for cursor */
2231 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 long_u highlight_mask;
2234 long_u hl_mask_todo;
2235 guicolor_T fg_color;
2236 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002237 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002238#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002240# ifdef FEAT_MBYTE
2241 GuiFont wide_font = NOFONT;
2242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243# ifdef FEAT_XFONTSET
2244 GuiFontset fontset = NOFONTSET;
2245# endif
2246#endif
2247 attrentry_T *aep = NULL;
2248 int draw_flags;
2249 int col = gui.col;
2250#ifdef FEAT_SIGN_ICONS
2251 int draw_sign = FALSE;
2252# ifdef FEAT_NETBEANS_INTG
2253 int multi_sign = FALSE;
2254# endif
2255#endif
2256
2257 if (len < 0)
2258 len = (int)STRLEN(s);
2259 if (len == 0)
2260 return OK;
2261
2262#ifdef FEAT_SIGN_ICONS
2263 if (*s == SIGN_BYTE
2264# ifdef FEAT_NETBEANS_INTG
2265 || *s == MULTISIGN_BYTE
2266# endif
2267 )
2268 {
2269# ifdef FEAT_NETBEANS_INTG
2270 if (*s == MULTISIGN_BYTE)
2271 multi_sign = TRUE;
2272# endif
2273 /* draw spaces instead */
2274 s = (char_u *)" ";
2275 if (len == 1 && col > 0)
2276 --col;
2277 len = 2;
2278 draw_sign = TRUE;
2279 highlight_mask = 0;
2280 }
2281 else
2282#endif
2283 if (gui.highlight_mask > HL_ALL)
2284 {
2285 aep = syn_gui_attr2entry(gui.highlight_mask);
2286 if (aep == NULL) /* highlighting not set */
2287 highlight_mask = 0;
2288 else
2289 highlight_mask = aep->ae_attr;
2290 }
2291 else
2292 highlight_mask = gui.highlight_mask;
2293 hl_mask_todo = highlight_mask;
2294
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002295#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 /* Set the font */
2297 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2298 font = aep->ae_u.gui.font;
2299# ifdef FEAT_XFONTSET
2300 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2301 fontset = aep->ae_u.gui.fontset;
2302# endif
2303 else
2304 {
2305# ifdef FEAT_XFONTSET
2306 if (gui.fontset != NOFONTSET)
2307 fontset = gui.fontset;
2308 else
2309# endif
2310 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2311 {
2312 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2313 {
2314 font = gui.boldital_font;
2315 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2316 }
2317 else if (gui.bold_font != NOFONT)
2318 {
2319 font = gui.bold_font;
2320 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2321 }
2322 else
2323 font = gui.norm_font;
2324 }
2325 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2326 {
2327 font = gui.ital_font;
2328 hl_mask_todo &= ~HL_ITALIC;
2329 }
2330 else
2331 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002332
2333# ifdef FEAT_MBYTE
2334 /*
2335 * Choose correct wide_font by font. wide_font should be set with font
2336 * at same time in above block. But it will make many "ifdef" nasty
2337 * blocks. So we do it here.
2338 */
2339 if (font == gui.boldital_font && gui.wide_boldital_font)
2340 wide_font = gui.wide_boldital_font;
2341 else if (font == gui.bold_font && gui.wide_bold_font)
2342 wide_font = gui.wide_bold_font;
2343 else if (font == gui.ital_font && gui.wide_ital_font)
2344 wide_font = gui.wide_ital_font;
2345 else if (font == gui.norm_font && gui.wide_font)
2346 wide_font = gui.wide_font;
2347# endif
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350# ifdef FEAT_XFONTSET
2351 if (fontset != NOFONTSET)
2352 gui_mch_set_fontset(fontset);
2353 else
2354# endif
2355 gui_mch_set_font(font);
2356#endif
2357
2358 draw_flags = 0;
2359
2360 /* Set the color */
2361 bg_color = gui.back_pixel;
2362 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2363 {
2364 draw_flags |= DRAW_CURSOR;
2365 fg_color = fg;
2366 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002367 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369 else if (aep != NULL)
2370 {
2371 fg_color = aep->ae_u.gui.fg_color;
2372 if (fg_color == INVALCOLOR)
2373 fg_color = gui.norm_pixel;
2374 bg_color = aep->ae_u.gui.bg_color;
2375 if (bg_color == INVALCOLOR)
2376 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002377 sp_color = aep->ae_u.gui.sp_color;
2378 if (sp_color == INVALCOLOR)
2379 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
2381 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002384 sp_color = fg_color;
2385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2388 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002389#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 gui_mch_set_colors(bg_color, fg_color);
2391#else
2392 gui_mch_set_fg_color(bg_color);
2393 gui_mch_set_bg_color(fg_color);
2394#endif
2395 }
2396 else
2397 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002398#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 gui_mch_set_colors(fg_color, bg_color);
2400#else
2401 gui_mch_set_fg_color(fg_color);
2402 gui_mch_set_bg_color(bg_color);
2403#endif
2404 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002405 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 /* Clear the selection if we are about to write over it */
2408 if (!(flags & GUI_MON_NOCLEAR))
2409 clip_may_clear_selection(gui.row, gui.row);
2410
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 /* If there's no bold font, then fake it */
2413 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2414 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /*
2417 * When drawing bold or italic characters the spill-over from the left
2418 * neighbor may be destroyed. Let the caller backup to start redrawing
2419 * just after a blank.
2420 */
2421 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2422 return FAIL;
2423
Bram Moolenaare60acc12011-05-10 16:41:25 +02002424#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /* If there's no italic font, then fake it.
2426 * For GTK2, we don't need a different font for italic style. */
2427 if (hl_mask_todo & HL_ITALIC)
2428 draw_flags |= DRAW_ITALIC;
2429
2430 /* Do we underline the text? */
2431 if (hl_mask_todo & HL_UNDERLINE)
2432 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#else
2435 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 draw_flags |= DRAW_UNDERL;
2438#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002439 /* Do we undercurl the text? */
2440 if (hl_mask_todo & HL_UNDERCURL)
2441 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002443 /* Do we strikethrough the text? */
2444 if (hl_mask_todo & HL_STRIKETHROUGH)
2445 draw_flags |= DRAW_STRIKE;
2446
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002447 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (flags & GUI_MON_TRS_CURSOR)
2449 draw_flags |= DRAW_TRANSP;
2450
2451 /*
2452 * Draw the text.
2453 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002454#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /* The value returned is the length in display cells */
2456 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2457#else
2458# ifdef FEAT_MBYTE
2459 if (enc_utf8)
2460 {
2461 int start; /* index of bytes to be drawn */
2462 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002463 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 int cn; /* cellwidth of current char */
2465 int i; /* index of current char */
2466 int c; /* current char value */
2467 int cl; /* byte length of current char */
2468 int comping; /* current char is composing */
2469 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002470 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002471 int prev_wide = FALSE;
2472 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002473# ifdef WIN3264
2474 int sep_comp = FALSE; /* Don't separate composing chars. */
2475# else
2476 int sep_comp = TRUE; /* Separate composing chars. */
2477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 /* Break the string at a composing character, it has to be drawn on
2480 * top of the previous character. */
2481 start = 0;
2482 cells = 0;
2483 for (i = 0; i < len; i += cl)
2484 {
2485 c = utf_ptr2char(s + i);
2486 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 comping = utf_iscomposing(c);
2488 if (!comping) /* count cells from non-composing chars */
2489 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002490 if (!comping || sep_comp)
2491 {
2492 if (cn > 1
2493# ifdef FEAT_XFONTSET
2494 && fontset == NOFONTSET
2495# endif
2496 && wide_font != NOFONT)
2497 curr_wide = TRUE;
2498 else
2499 curr_wide = FALSE;
2500 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (cl == 0) /* hit end of string */
2503 len = i + cl; /* len must be wrong "cannot happen" */
2504
Bram Moolenaar45933962013-01-23 17:43:57 +01002505 wide_changed = curr_wide != prev_wide;
2506
2507 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002509 if (i + cl >= len || (comping && sep_comp && i > start)
2510 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002511# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 || (cn > 1
2513# ifdef FEAT_XFONTSET
2514 /* No fontset: At least draw char after wide char at
2515 * right position. */
2516 && fontset == NOFONTSET
2517# endif
2518 )
2519# endif
2520 )
2521 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002522 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 thislen = i - start;
2524 else
2525 thislen = i - start + cl;
2526 if (thislen > 0)
2527 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002528 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002529 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2531 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002532 if (prev_wide)
2533 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 start += thislen;
2535 }
2536 scol += cells;
2537 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002538 /* Adjust to not draw a character which width is changed
2539 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002540 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002542 scol -= cn;
2543 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002546# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002547 /* No fontset: draw a space to fill the gap after a wide char
2548 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2550# ifdef FEAT_XFONTSET
2551 && fontset == NOFONTSET
2552# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002553 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2555 1, draw_flags);
2556# endif
2557 }
2558 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002559 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002561# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002562 /* Carbon ATSUI autodraws composing char over previous char */
2563 gui_mch_draw_string(gui.row, scol, s + i, cl,
2564 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002565# else
2566 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2567 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 start = i + cl;
2570 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002571 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573 /* The stuff below assumes "len" is the length in screen columns. */
2574 len = scol - col;
2575 }
2576 else
2577# endif
2578 {
2579 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2580# ifdef FEAT_MBYTE
2581 if (enc_dbcs == DBCS_JPNU)
2582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 /* Get the length in display cells, this can be different from the
2584 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002585 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587# endif
2588 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002589#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2592 gui.col = col + len;
2593
2594 /* May need to invert it when it's part of the selection. */
2595 if (flags & GUI_MON_NOCLEAR)
2596 clip_may_redraw_selection(gui.row, col, len);
2597
2598 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2599 {
2600 /* Invalidate the old physical cursor position if we wrote over it */
2601 if (gui.cursor_row == gui.row
2602 && gui.cursor_col >= col
2603 && gui.cursor_col < col + len)
2604 gui.cursor_is_valid = FALSE;
2605 }
2606
2607#ifdef FEAT_SIGN_ICONS
2608 if (draw_sign)
2609 /* Draw the sign on top of the spaces. */
2610 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002611# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002612 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (multi_sign)
2614 netbeans_draw_multisign_indicator(gui.row);
2615# endif
2616#endif
2617
2618 return OK;
2619}
2620
2621/*
2622 * Un-draw the cursor. Actually this just redraws the character at the given
2623 * position. The character just before it too, for when it was in bold.
2624 */
2625 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002626gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 if (gui.cursor_is_valid)
2629 {
2630#ifdef FEAT_HANGULIN
2631 if (composing_hangul
2632 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002633 {
2634 char_u *comp_buf;
2635 int comp_len;
2636
2637 comp_buf = hangul_composing_buffer_get(&comp_len);
2638 if (comp_buf)
2639 {
2640 (void)gui_outstr_nowrap(comp_buf, comp_len,
2641 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2642 gui.norm_pixel, gui.back_pixel, 0);
2643 vim_free(comp_buf);
2644 }
2645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 else
2647 {
2648#endif
2649 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2650 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2651 && gui.cursor_col > 0)
2652 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2653 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2654#ifdef FEAT_HANGULIN
2655 if (composing_hangul)
2656 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2657 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2658 }
2659#endif
2660 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2661 * here in case it wasn't needed to undraw it. */
2662 gui.cursor_is_valid = FALSE;
2663 }
2664}
2665
2666 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002667gui_redraw(
2668 int x,
2669 int y,
2670 int w,
2671 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 int row1, col1, row2, col2;
2674
2675 row1 = Y_2_ROW(y);
2676 col1 = X_2_COL(x);
2677 row2 = Y_2_ROW(y + h - 1);
2678 col2 = X_2_COL(x + w - 1);
2679
2680 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2681
2682 /*
2683 * We may need to redraw the cursor, but don't take it upon us to change
2684 * its location after a scroll.
2685 * (maybe be more strict even and test col too?)
2686 * These things may be outside the update/clipping region and reality may
2687 * not reflect Vims internal ideas if these operations are clipped away.
2688 */
2689 if (gui.row == gui.cursor_row)
2690 gui_update_cursor(TRUE, TRUE);
2691}
2692
2693/*
2694 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2695 * from col1 to col2 (inclusive).
2696 * Return TRUE when the character before the first drawn character has
2697 * different attributes (may have to be redrawn too).
2698 */
2699 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002700gui_redraw_block(
2701 int row1,
2702 int col1,
2703 int row2,
2704 int col2,
2705 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 int old_row, old_col;
2708 long_u old_hl_mask;
2709 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002710 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 int idx, len;
2712 int back, nback;
2713 int retval = FALSE;
2714#ifdef FEAT_MBYTE
2715 int orig_col1, orig_col2;
2716#endif
2717
2718 /* Don't try to update when ScreenLines is not valid */
2719 if (!screen_cleared || ScreenLines == NULL)
2720 return retval;
2721
2722 /* Don't try to draw outside the shell! */
2723 /* Check everything, strange values may be caused by a big border width */
2724 col1 = check_col(col1);
2725 col2 = check_col(col2);
2726 row1 = check_row(row1);
2727 row2 = check_row(row2);
2728
2729 /* Remember where our cursor was */
2730 old_row = gui.row;
2731 old_col = gui.col;
2732 old_hl_mask = gui.highlight_mask;
2733#ifdef FEAT_MBYTE
2734 orig_col1 = col1;
2735 orig_col2 = col2;
2736#endif
2737
2738 for (gui.row = row1; gui.row <= row2; gui.row++)
2739 {
2740#ifdef FEAT_MBYTE
2741 /* When only half of a double-wide character is in the block, include
2742 * the other half. */
2743 col1 = orig_col1;
2744 col2 = orig_col2;
2745 off = LineOffset[gui.row];
2746 if (enc_dbcs != 0)
2747 {
2748 if (col1 > 0)
2749 col1 -= dbcs_screen_head_off(ScreenLines + off,
2750 ScreenLines + off + col1);
2751 col2 += dbcs_screen_tail_off(ScreenLines + off,
2752 ScreenLines + off + col2);
2753 }
2754 else if (enc_utf8)
2755 {
Bram Moolenaar10600db2018-12-05 19:46:07 +01002756 // FIXME: how can the first character ever be zero?
2757 if (col1 > 0 && ScreenLines[off + col1] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002759# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2761 ++col2;
2762# endif
2763 }
2764#endif
2765 gui.col = col1;
2766 off = LineOffset[gui.row] + gui.col;
2767 len = col2 - col1 + 1;
2768
2769 /* Find how many chars back this highlighting starts, or where a space
2770 * is. Needed for when the bold trick is used */
2771 for (back = 0; back < col1; ++back)
2772 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2773 || ScreenLines[off - 1 - back] == ' ')
2774 break;
2775 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2776 && ScreenLines[off - 1] != ' ');
2777
2778 /* Break it up in strings of characters with the same attributes. */
2779 /* Print UTF-8 characters individually. */
2780 while (len > 0)
2781 {
2782 first_attr = ScreenAttrs[off];
2783 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002784#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (enc_utf8 && ScreenLinesUC[off] != 0)
2786 {
2787 /* output multi-byte character separately */
2788 nback = gui_screenchar(off, flags,
2789 (guicolor_T)0, (guicolor_T)0, back);
2790 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2791 idx = 2;
2792 else
2793 idx = 1;
2794 }
2795 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2796 {
2797 /* output double-byte, single-width character separately */
2798 nback = gui_screenchar(off, flags,
2799 (guicolor_T)0, (guicolor_T)0, back);
2800 idx = 1;
2801 }
2802 else
2803#endif
2804 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002805#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 for (idx = 0; idx < len; ++idx)
2807 {
2808 if (enc_utf8 && ScreenLines[off + idx] == 0)
2809 continue; /* skip second half of double-width char */
2810 if (ScreenAttrs[off + idx] != first_attr)
2811 break;
2812 }
2813 /* gui_screenstr() takes care of multibyte chars */
2814 nback = gui_screenstr(off, idx, flags,
2815 (guicolor_T)0, (guicolor_T)0, back);
2816#else
2817 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2818 idx++)
2819 {
2820# ifdef FEAT_MBYTE
2821 /* Stop at a multi-byte Unicode character. */
2822 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2823 break;
2824 if (enc_dbcs == DBCS_JPNU)
2825 {
2826 /* Stop at a double-byte single-width char. */
2827 if (ScreenLines[off + idx] == 0x8e)
2828 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002829 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 + off + idx) == 2)
2831 ++idx; /* skip second byte of double-byte char */
2832 }
2833# endif
2834 }
2835 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2836 (guicolor_T)0, (guicolor_T)0, back);
2837#endif
2838 }
2839 if (nback == FAIL)
2840 {
2841 /* Must back up to start drawing where a bold or italic word
2842 * starts. */
2843 off -= back;
2844 len += back;
2845 gui.col -= back;
2846 }
2847 else
2848 {
2849 off += idx;
2850 len -= idx;
2851 }
2852 back = 0;
2853 }
2854 }
2855
2856 /* Put the cursor back where it was */
2857 gui.row = old_row;
2858 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002859 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 return retval;
2862}
2863
2864 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002865gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 if (count <= 0)
2868 return;
2869
2870 if (row + count > gui.scroll_region_bot)
2871 /* Scrolled out of region, just blank the lines out */
2872 gui_clear_block(row, gui.scroll_region_left,
2873 gui.scroll_region_bot, gui.scroll_region_right);
2874 else
2875 {
2876 gui_mch_delete_lines(row, count);
2877
2878 /* If the cursor was in the deleted lines it's now gone. If the
2879 * cursor was in the scrolled lines adjust its position. */
2880 if (gui.cursor_row >= row
2881 && gui.cursor_col >= gui.scroll_region_left
2882 && gui.cursor_col <= gui.scroll_region_right)
2883 {
2884 if (gui.cursor_row < row + count)
2885 gui.cursor_is_valid = FALSE;
2886 else if (gui.cursor_row <= gui.scroll_region_bot)
2887 gui.cursor_row -= count;
2888 }
2889 }
2890}
2891
2892 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002893gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 if (count <= 0)
2896 return;
2897
2898 if (row + count > gui.scroll_region_bot)
2899 /* Scrolled out of region, just blank the lines out */
2900 gui_clear_block(row, gui.scroll_region_left,
2901 gui.scroll_region_bot, gui.scroll_region_right);
2902 else
2903 {
2904 gui_mch_insert_lines(row, count);
2905
2906 if (gui.cursor_row >= gui.row
2907 && gui.cursor_col >= gui.scroll_region_left
2908 && gui.cursor_col <= gui.scroll_region_right)
2909 {
2910 if (gui.cursor_row <= gui.scroll_region_bot - count)
2911 gui.cursor_row += count;
2912 else if (gui.cursor_row <= gui.scroll_region_bot)
2913 gui.cursor_is_valid = FALSE;
2914 }
2915 }
2916}
2917
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002918#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002919/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002920 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2921 */
2922 static int
2923gui_wait_for_chars_3(
2924 long wtime,
2925 int *interrupted UNUSED,
2926 int ignore_input UNUSED)
2927{
2928 return gui_mch_wait_for_chars(wtime);
2929}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002930#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002931
2932/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002933 * Returns OK if a character was found to be available within the given time,
2934 * or FAIL otherwise.
2935 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002936 static int
2937gui_wait_for_chars_or_timer(long wtime)
2938{
2939#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002940 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3, NULL, 0);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002941#else
2942 return gui_mch_wait_for_chars(wtime);
2943#endif
2944}
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946/*
2947 * The main GUI input routine. Waits for a character from the keyboard.
2948 * wtime == -1 Wait forever.
2949 * wtime == 0 Don't wait.
2950 * wtime > 0 Wait wtime milliseconds for a character.
2951 * Returns OK if a character was found to be available within the given time,
2952 * or FAIL otherwise.
2953 */
2954 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002955gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 int retval;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002958#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002959 ELAPSED_TYPE start_tv;
2960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002962#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 /*
2964 * If we're going to wait a bit, update the menus and mouse shape for the
2965 * current State.
2966 */
2967 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 gui_update_menus(0);
2969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971 gui_mch_update();
2972 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
2977 /* Before waiting, flush any output to the screen. */
2978 gui_mch_flush();
2979
2980 if (wtime > 0)
2981 {
2982 /* Blink when waiting for a character. Probably only does something
2983 * for showmatch() */
2984 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002985 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002986 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 return retval;
2988 }
2989
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002990#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002991 ELAPSED_INIT(start_tv);
2992#endif
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002995 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 */
2997 gui_mch_start_blink();
2998
Bram Moolenaar3918c952005-03-15 22:34:55 +00002999 retval = FAIL;
3000 /*
3001 * We may want to trigger the CursorHold event. First wait for
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003002 * 'updatetime' and if nothing is typed within that time, and feedkeys()
3003 * wasn't used, put the K_CURSORHOLD key in the input buffer.
Bram Moolenaar3918c952005-03-15 22:34:55 +00003004 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01003005 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003006 retval = OK;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003007 else if (trigger_cursorhold()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003008#ifdef ELAPSED_FUNC
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003009 && ELAPSED_FUNC(start_tv) >= p_ut
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003010#endif
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003011 && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00003013 char_u buf[3];
3014
3015 /* Put K_CURSORHOLD in the input buffer. */
3016 buf[0] = CSI;
3017 buf[1] = KS_EXTRA;
3018 buf[2] = (int)KE_CURSORHOLD;
3019 add_to_input_buf(buf, 3);
3020
3021 retval = OK;
3022 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00003023
Bram Moolenaar9472eec2017-06-05 13:31:56 +02003024 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003025 {
3026 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00003027 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01003028 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003031 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 return retval;
3033}
3034
3035/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003036 * Equivalent of mch_inchar() for the GUI.
3037 */
3038 int
3039gui_inchar(
3040 char_u *buf,
3041 int maxlen,
3042 long wtime, /* milli seconds */
3043 int tb_change_cnt)
3044{
3045 if (gui_wait_for_chars(wtime, tb_change_cnt)
3046 && !typebuf_changed(tb_change_cnt))
3047 return read_from_input_buf(buf, (long)maxlen);
3048 return 0;
3049}
3050
3051/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003052 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 */
3054 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003055fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 p[0] = (char_u)(col / 128 + ' ' + 1);
3058 p[1] = (char_u)(col % 128 + ' ' + 1);
3059 p[2] = (char_u)(row / 128 + ' ' + 1);
3060 p[3] = (char_u)(row % 128 + ' ' + 1);
3061}
3062
3063/*
3064 * Generic mouse support function. Add a mouse event to the input buffer with
3065 * the given properties.
3066 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3067 * MOUSE_X1, MOUSE_X2
3068 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003069 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3070 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 * x, y --- Coordinates of mouse in pixels.
3072 * repeated_click --- TRUE if this click comes only a short time after a
3073 * previous click.
3074 * modifiers --- Bit field which may be any of the following modifiers
3075 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3076 * This function will ignore drag events where the mouse has not moved to a new
3077 * character.
3078 */
3079 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003080gui_send_mouse_event(
3081 int button,
3082 int x,
3083 int y,
3084 int repeated_click,
3085 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 static int prev_row = 0, prev_col = 0;
3088 static int prev_button = -1;
3089 static int num_clicks = 1;
3090 char_u string[10];
3091 enum key_extra button_char;
3092 int row, col;
3093#ifdef FEAT_CLIPBOARD
3094 int checkfor;
3095 int did_clip = FALSE;
3096#endif
3097
3098 /*
3099 * Scrolling may happen at any time, also while a selection is present.
3100 */
3101 switch (button)
3102 {
3103 case MOUSE_X1:
3104 button_char = KE_X1MOUSE;
3105 goto button_set;
3106 case MOUSE_X2:
3107 button_char = KE_X2MOUSE;
3108 goto button_set;
3109 case MOUSE_4:
3110 button_char = KE_MOUSEDOWN;
3111 goto button_set;
3112 case MOUSE_5:
3113 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003114 goto button_set;
3115 case MOUSE_6:
3116 button_char = KE_MOUSELEFT;
3117 goto button_set;
3118 case MOUSE_7:
3119 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120button_set:
3121 {
3122 /* Don't put events in the input queue now. */
3123 if (hold_gui_events)
3124 return;
3125
3126 string[3] = CSI;
3127 string[4] = KS_EXTRA;
3128 string[5] = (int)button_char;
3129
3130 /* Pass the pointer coordinates of the scroll event so that we
3131 * know which window to scroll. */
3132 row = gui_xy2colrow(x, y, &col);
3133 string[6] = (char_u)(col / 128 + ' ' + 1);
3134 string[7] = (char_u)(col % 128 + ' ' + 1);
3135 string[8] = (char_u)(row / 128 + ' ' + 1);
3136 string[9] = (char_u)(row % 128 + ' ' + 1);
3137
3138 if (modifiers == 0)
3139 add_to_input_buf(string + 3, 7);
3140 else
3141 {
3142 string[0] = CSI;
3143 string[1] = KS_MODIFIER;
3144 string[2] = 0;
3145 if (modifiers & MOUSE_SHIFT)
3146 string[2] |= MOD_MASK_SHIFT;
3147 if (modifiers & MOUSE_CTRL)
3148 string[2] |= MOD_MASK_CTRL;
3149 if (modifiers & MOUSE_ALT)
3150 string[2] |= MOD_MASK_ALT;
3151 add_to_input_buf(string, 10);
3152 }
3153 return;
3154 }
3155 }
3156
3157#ifdef FEAT_CLIPBOARD
3158 /* If a clipboard selection is in progress, handle it */
3159 if (clip_star.state == SELECT_IN_PROGRESS)
3160 {
3161 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3162 return;
3163 }
3164
3165 /* Determine which mouse settings to look for based on the current mode */
3166 switch (get_real_state())
3167 {
3168 case NORMAL_BUSY:
3169 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003170# ifdef FEAT_TERMINAL
3171 case TERMINAL:
3172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 case NORMAL: checkfor = MOUSE_NORMAL; break;
3174 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003175 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 case REPLACE:
3177 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 case VREPLACE:
3179 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 case INSERT:
3181 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3182 case ASKMORE:
3183 case HITRETURN: /* At the more- and hit-enter prompt pass the
3184 mouse event for a click on or below the
3185 message line. */
3186 if (Y_2_ROW(y) >= msg_row)
3187 checkfor = MOUSE_NORMAL;
3188 else
3189 checkfor = MOUSE_RETURN;
3190 break;
3191
3192 /*
3193 * On the command line, use the clipboard selection on all lines
3194 * but the command line. But not when pasting.
3195 */
3196 case CMDLINE:
3197 case CMDLINE+LANGMAP:
3198 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3199 checkfor = MOUSE_NONE;
3200 else
3201 checkfor = MOUSE_COMMAND;
3202 break;
3203
3204 default:
3205 checkfor = MOUSE_NONE;
3206 break;
3207 };
3208
3209 /*
3210 * Allow clipboard selection of text on the command line in "normal"
3211 * modes. Don't do this when dragging the status line, or extending a
3212 * Visual selection.
3213 */
3214 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003215 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 && button != MOUSE_DRAG
3217# ifdef FEAT_MOUSESHAPE
3218 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220# endif
3221 )
3222 checkfor = MOUSE_NONE;
3223
3224 /*
3225 * Use modeless selection when holding CTRL and SHIFT pressed.
3226 */
3227 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3228 checkfor = MOUSE_NONEF;
3229
3230 /*
3231 * In Ex mode, always use modeless selection.
3232 */
3233 if (exmode_active)
3234 checkfor = MOUSE_NONE;
3235
3236 /*
3237 * If the mouse settings say to not use the mouse, use the modeless
3238 * selection. But if Visual is active, assume that only the Visual area
3239 * will be selected.
3240 * Exception: On the command line, both the selection is used and a mouse
3241 * key is send.
3242 */
3243 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 /* Don't do modeless selection in Visual mode. */
3246 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3247 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 /*
3250 * When 'mousemodel' is "popup", shift-left is translated to right.
3251 * But not when also using Ctrl.
3252 */
3253 if (mouse_model_popup() && button == MOUSE_LEFT
3254 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3255 {
3256 button = MOUSE_RIGHT;
3257 modifiers &= ~ MOUSE_SHIFT;
3258 }
3259
3260 /* If the selection is done, allow the right button to extend it.
3261 * If the selection is cleared, allow the right button to start it
3262 * from the cursor position. */
3263 if (button == MOUSE_RIGHT)
3264 {
3265 if (clip_star.state == SELECT_CLEARED)
3266 {
3267 if (State & CMDLINE)
3268 {
3269 col = msg_col;
3270 row = msg_row;
3271 }
3272 else
3273 {
3274 col = curwin->w_wcol;
3275 row = curwin->w_wrow + W_WINROW(curwin);
3276 }
3277 clip_start_selection(col, row, FALSE);
3278 }
3279 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3280 repeated_click);
3281 did_clip = TRUE;
3282 }
3283 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003284 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3287 did_clip = TRUE;
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290 /* Always allow pasting */
3291 if (button != MOUSE_MIDDLE)
3292 {
3293 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3294 return;
3295 if (checkfor != MOUSE_COMMAND)
3296 button = MOUSE_LEFT;
3297 }
3298 repeated_click = FALSE;
3299 }
3300
3301 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003302 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#endif
3304
3305 /* Don't put events in the input queue now. */
3306 if (hold_gui_events)
3307 return;
3308
3309 row = gui_xy2colrow(x, y, &col);
3310
3311 /*
3312 * If we are dragging and the mouse hasn't moved far enough to be on a
3313 * different character, then don't send an event to vim.
3314 */
3315 if (button == MOUSE_DRAG)
3316 {
3317 if (row == prev_row && col == prev_col)
3318 return;
3319 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3320 if (y < 0)
3321 row = -1;
3322 }
3323
3324 /*
3325 * If topline has changed (window scrolled) since the last click, reset
3326 * repeated_click, because we don't want starting Visual mode when
3327 * clicking on a different character in the text.
3328 */
3329 if (curwin->w_topline != gui_prev_topline
3330#ifdef FEAT_DIFF
3331 || curwin->w_topfill != gui_prev_topfill
3332#endif
3333 )
3334 repeated_click = FALSE;
3335
3336 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3337 string[1] = KS_MOUSE;
3338 string[2] = KE_FILLER;
3339 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3340 {
3341 if (repeated_click)
3342 {
3343 /*
3344 * Handle multiple clicks. They only count if the mouse is still
3345 * pointing at the same character.
3346 */
3347 if (button != prev_button || row != prev_row || col != prev_col)
3348 num_clicks = 1;
3349 else if (++num_clicks > 4)
3350 num_clicks = 1;
3351 }
3352 else
3353 num_clicks = 1;
3354 prev_button = button;
3355 gui_prev_topline = curwin->w_topline;
3356#ifdef FEAT_DIFF
3357 gui_prev_topfill = curwin->w_topfill;
3358#endif
3359
3360 string[3] = (char_u)(button | 0x20);
3361 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3362 }
3363 else
3364 string[3] = (char_u)button;
3365
3366 string[3] |= modifiers;
3367 fill_mouse_coord(string + 4, col, row);
3368 add_to_input_buf(string, 8);
3369
3370 if (row < 0)
3371 prev_row = 0;
3372 else
3373 prev_row = row;
3374 prev_col = col;
3375
3376 /*
3377 * We need to make sure this is cleared since Athena doesn't tell us when
3378 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3379 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003380#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 gui.dragged_sb = SBAR_NONE;
3382#endif
3383}
3384
3385/*
3386 * Convert x and y coordinate to column and row in text window.
3387 * Corrects for multi-byte character.
3388 * returns column in "*colp" and row as return value;
3389 */
3390 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003391gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 int col = check_col(X_2_COL(x));
3394 int row = check_row(Y_2_ROW(y));
3395
3396#ifdef FEAT_MBYTE
3397 *colp = mb_fix_col(col, row);
3398#else
3399 *colp = col;
3400#endif
3401 return row;
3402}
3403
3404#if defined(FEAT_MENU) || defined(PROTO)
3405/*
3406 * Callback function for when a menu entry has been selected.
3407 */
3408 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003409gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003411 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 /* Don't put events in the input queue now. */
3414 if (hold_gui_events)
3415 return;
3416
3417 bytes[0] = CSI;
3418 bytes[1] = KS_MENU;
3419 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003420 add_to_input_buf(bytes, 3);
3421 add_long_to_buf((long_u)menu, bytes);
3422 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423}
3424#endif
3425
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003426static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428/*
3429 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003430 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 * in p_go.
3432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003434gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#ifdef FEAT_MENU
3437 static int prev_menu_is_active = -1;
3438#endif
3439#ifdef FEAT_TOOLBAR
3440 static int prev_toolbar = -1;
3441 int using_toolbar = FALSE;
3442#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003443#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003444 int using_tabline;
3445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#ifdef FEAT_FOOTER
3447 static int prev_footer = -1;
3448 int using_footer = FALSE;
3449#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003450#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 static int prev_tearoff = -1;
3452 int using_tearoff = FALSE;
3453#endif
3454
3455 char_u *p;
3456 int i;
3457#ifdef FEAT_MENU
3458 int grey_old, grey_new;
3459 char_u *temp;
3460#endif
3461 win_T *wp;
3462 int need_set_size;
3463 int fix_size;
3464
3465#ifdef FEAT_MENU
3466 if (oldval != NULL && gui.in_use)
3467 {
3468 /*
3469 * Check if the menu's go from grey to non-grey or vise versa.
3470 */
3471 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3472 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3473 if (grey_old != grey_new)
3474 {
3475 temp = p_go;
3476 p_go = oldval;
3477 gui_update_menus(MENU_ALL_MODES);
3478 p_go = temp;
3479 }
3480 }
3481 gui.menu_is_active = FALSE;
3482#endif
3483
3484 for (i = 0; i < 3; i++)
3485 gui.which_scrollbars[i] = FALSE;
3486 for (p = p_go; *p; p++)
3487 switch (*p)
3488 {
3489 case GO_LEFT:
3490 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3491 break;
3492 case GO_RIGHT:
3493 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 case GO_VLEFT:
3496 if (win_hasvertsplit())
3497 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3498 break;
3499 case GO_VRIGHT:
3500 if (win_hasvertsplit())
3501 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 case GO_BOT:
3504 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3505 break;
3506#ifdef FEAT_MENU
3507 case GO_MENUS:
3508 gui.menu_is_active = TRUE;
3509 break;
3510#endif
3511 case GO_GREY:
3512 /* make menu's have grey items, ignored here */
3513 break;
3514#ifdef FEAT_TOOLBAR
3515 case GO_TOOLBAR:
3516 using_toolbar = TRUE;
3517 break;
3518#endif
3519#ifdef FEAT_FOOTER
3520 case GO_FOOTER:
3521 using_footer = TRUE;
3522 break;
3523#endif
3524 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003525#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 using_tearoff = TRUE;
3527#endif
3528 break;
3529 default:
3530 /* Ignore options that are not supported */
3531 break;
3532 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (gui.in_use)
3535 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003536 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003538
3539#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003540 /* Update the GUI tab line, it may appear or disappear. This may
3541 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003542 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003543 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003544 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003545 /* We don't want a resize event change "Rows" here, save and
3546 * restore it. Resizing is handled below. */
3547 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003548 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003549 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003550 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003551 if (using_tabline)
3552 fix_size = TRUE;
3553 if (!gui_use_tabline())
3554 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3555 }
3556#endif
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 for (i = 0; i < 3; i++)
3559 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003560 /* The scrollbar needs to be updated when it is shown/unshown and
3561 * when switching tab pages. But the size only changes when it's
3562 * shown/unshown. Thus we need two places to remember whether a
3563 * scrollbar is there or not. */
3564 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003565 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003566 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 if (i == SBAR_BOTTOM)
3569 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3570 gui.which_scrollbars[i]);
3571 else
3572 {
3573 FOR_ALL_WINDOWS(wp)
3574 {
3575 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3576 }
3577 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003578 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3579 {
3580 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003581 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003582 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003583 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003584 if (gui.which_scrollbars[i])
3585 fix_size = TRUE;
3586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003588 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003589 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591
3592#ifdef FEAT_MENU
3593 if (gui.menu_is_active != prev_menu_is_active)
3594 {
3595 /* We don't want a resize event change "Rows" here, save and
3596 * restore it. Resizing is handled below. */
3597 i = Rows;
3598 gui_mch_enable_menu(gui.menu_is_active);
3599 Rows = i;
3600 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003601 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (gui.menu_is_active)
3603 fix_size = TRUE;
3604 }
3605#endif
3606
3607#ifdef FEAT_TOOLBAR
3608 if (using_toolbar != prev_toolbar)
3609 {
3610 gui_mch_show_toolbar(using_toolbar);
3611 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003612 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (using_toolbar)
3614 fix_size = TRUE;
3615 }
3616#endif
3617#ifdef FEAT_FOOTER
3618 if (using_footer != prev_footer)
3619 {
3620 gui_mch_enable_footer(using_footer);
3621 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003622 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (using_footer)
3624 fix_size = TRUE;
3625 }
3626#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (using_tearoff != prev_tearoff)
3629 {
3630 gui_mch_toggle_tearoffs(using_tearoff);
3631 prev_tearoff = using_tearoff;
3632 }
3633#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003634 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003635 {
3636#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003637 long prev_Columns = Columns;
3638 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 /* Adjust the size of the window to make the text area keep the
3641 * same size and to avoid that part of our window is off-screen
3642 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003643 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003644
3645#ifdef FEAT_GUI_GTK
3646 /* GTK has the annoying habit of sending us resize events when
3647 * changing the window size ourselves. This mostly happens when
3648 * waiting for a character to arrive, quite unpredictably, and may
3649 * change Columns and Rows when we don't want it. Wait for a
3650 * character here to avoid this effect.
3651 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003652 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003653 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003654 * Don't change Rows when adding menu/toolbar/tabline.
3655 * Don't change Columns when adding vertical toolbar. */
3656 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003657 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003658 if ((need_set_size & RESIZE_VERT) == 0)
3659 Rows = prev_Rows;
3660 if ((need_set_size & RESIZE_HOR) == 0)
3661 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003662#endif
3663 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003664 /* When the console tabline appears or disappears the window positions
3665 * change. */
3666 if (firstwin->w_winrow != tabline_height())
3667 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669}
3670
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003671#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3672/*
3673 * Return TRUE if the GUI is taking care of the tabline.
3674 * It may still be hidden if 'showtabline' is zero.
3675 */
3676 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003677gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678{
3679 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3680}
3681
3682/*
3683 * Return TRUE if the GUI is showing the tabline.
3684 * This uses 'showtabline'.
3685 */
3686 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003687gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688{
3689 if (!gui_use_tabline()
3690 || p_stal == 0
3691 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3692 return FALSE;
3693 return TRUE;
3694}
3695
3696/*
3697 * Update the tabline.
3698 * This may display/undisplay the tabline and update the labels.
3699 */
3700 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003701gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003702{
3703 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003704 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705
3706 if (!gui.starting && starting == 0)
3707 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003708 /* Updating the tabline uses direct GUI commands, flush
3709 * outstanding instructions first. (esp. clear screen) */
3710 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003711
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003712 if (!showit != !shown)
3713 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003714 if (showit != 0)
3715 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003716
3717 /* When the tabs change from hidden to shown or from shown to
3718 * hidden the size of the text area should remain the same. */
3719 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003720 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003721 }
3722}
3723
3724/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003725 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003726 */
3727 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003728get_tabline_label(
3729 tabpage_T *tp,
3730 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003731{
3732 int modified = FALSE;
3733 char_u buf[40];
3734 int wincount;
3735 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003736 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003737
Bram Moolenaar57657d82006-04-21 22:12:41 +00003738 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003739 opt = (tooltip ? &p_gtt : &p_gtl);
3740 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003742 int use_sandbox = FALSE;
3743 int save_called_emsg = called_emsg;
3744 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003745 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003746 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3747 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003748
3749 called_emsg = FALSE;
3750
3751 printer_page_num = tabpage_index(tp);
3752# ifdef FEAT_EVAL
3753 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003754 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003755# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003756 /* It's almost as going to the tabpage, but without autocommands. */
3757 curtab->tp_firstwin = firstwin;
3758 curtab->tp_lastwin = lastwin;
3759 curtab->tp_curwin = curwin;
3760 save_curtab = curtab;
3761 curtab = tp;
3762 topframe = curtab->tp_topframe;
3763 firstwin = curtab->tp_firstwin;
3764 lastwin = curtab->tp_lastwin;
3765 curwin = curtab->tp_curwin;
3766 curbuf = curwin->w_buffer;
3767
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003768 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003769 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003770 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003771 STRCPY(NameBuff, res);
3772
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003773 /* Back to the original curtab. */
3774 curtab = save_curtab;
3775 topframe = curtab->tp_topframe;
3776 firstwin = curtab->tp_firstwin;
3777 lastwin = curtab->tp_lastwin;
3778 curwin = curtab->tp_curwin;
3779 curbuf = curwin->w_buffer;
3780
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003781 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003782 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003783 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003784 called_emsg |= save_called_emsg;
3785 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003786
3787 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3788 * use a default label. */
3789 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003790 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003791 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003792 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003793 if (!tooltip)
3794 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003795
3796 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3797 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3798 if (bufIsChanged(wp->w_buffer))
3799 modified = TRUE;
3800 if (modified || wincount > 1)
3801 {
3802 if (wincount > 1)
3803 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3804 else
3805 buf[0] = NUL;
3806 if (modified)
3807 STRCAT(buf, "+");
3808 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003809 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003810 mch_memmove(NameBuff, buf, STRLEN(buf));
3811 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003812 }
3813}
3814
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003815/*
3816 * Send the event for clicking to select tab page "nr".
3817 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003818 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003819 */
3820 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003821send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003822{
3823 char_u string[3];
3824
3825 if (nr == tabpage_index(curtab))
3826 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003827
3828 /* Don't put events in the input queue now. */
3829 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003830# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003831 || cmdwin_type != 0
3832# endif
3833 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003834 {
3835 /* Set it back to the current tab page. */
3836 gui_mch_set_curtab(tabpage_index(curtab));
3837 return FALSE;
3838 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003839
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003840 string[0] = CSI;
3841 string[1] = KS_TABLINE;
3842 string[2] = KE_FILLER;
3843 add_to_input_buf(string, 3);
3844 string[0] = nr;
3845 add_to_input_buf_csi(string, 1);
3846 return TRUE;
3847}
3848
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003849/*
3850 * Send a tabline menu event
3851 */
3852 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003853send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003854{
3855 char_u string[3];
3856
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003857 /* Don't put events in the input queue now. */
3858 if (hold_gui_events)
3859 return;
3860
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003861 string[0] = CSI;
3862 string[1] = KS_TABMENU;
3863 string[2] = KE_FILLER;
3864 add_to_input_buf(string, 3);
3865 string[0] = tabidx;
3866 string[1] = (char_u)(long)event;
3867 add_to_input_buf_csi(string, 2);
3868}
3869
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872/*
3873 * Scrollbar stuff:
3874 */
3875
Bram Moolenaare45828b2006-02-15 22:12:56 +00003876/*
3877 * Remove all scrollbars. Used before switching to another tab page.
3878 */
3879 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003880gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003881{
3882 int i;
3883 win_T *wp;
3884
3885 for (i = 0; i < 3; i++)
3886 {
3887 if (i == SBAR_BOTTOM)
3888 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3889 else
3890 {
3891 FOR_ALL_WINDOWS(wp)
3892 {
3893 gui_do_scrollbar(wp, i, FALSE);
3894 }
3895 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003896 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003897 }
3898}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003901gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 static int sbar_ident = 0;
3904
3905 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3906 sb->wp = wp;
3907 sb->type = type;
3908 sb->value = 0;
3909#ifdef FEAT_GUI_ATHENA
3910 sb->pixval = 0;
3911#endif
3912 sb->size = 1;
3913 sb->max = 1;
3914 sb->top = 0;
3915 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 sb->status_height = 0;
3918 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3919}
3920
3921/*
3922 * Find the scrollbar with the given index.
3923 */
3924 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003925gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 win_T *wp;
3928
3929 if (gui.bottom_sbar.ident == ident)
3930 return &gui.bottom_sbar;
3931 FOR_ALL_WINDOWS(wp)
3932 {
3933 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3934 return &wp->w_scrollbars[SBAR_LEFT];
3935 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3936 return &wp->w_scrollbars[SBAR_RIGHT];
3937 }
3938 return NULL;
3939}
3940
3941/*
3942 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3943 *
3944 * For Win32, Macintosh and GTK+ 2:
3945 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3946 * you stop dragging the scrollbar. We get here each time the scrollbar is
3947 * dragged another pixel, but as far as the rest of vim goes, it thinks
3948 * we're just hanging in the call to DispatchMessage() in
3949 * process_message(). The DispatchMessage() call that hangs was passed a
3950 * mouse button click event in the scrollbar window. -- webb.
3951 *
3952 * Solution: Do the scrolling right here. But only when allowed.
3953 * Ignore the scrollbars while executing an external command or when there
3954 * are still characters to be processed.
3955 */
3956 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003957gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 int sb_num;
3961#ifdef USE_ON_FLY_SCROLL
3962 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964# ifdef FEAT_DIFF
3965 int old_topfill = curwin->w_topfill;
3966# endif
3967#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003968 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int byte_count;
3970#endif
3971
3972 if (sb == NULL)
3973 return;
3974
3975 /* Don't put events in the input queue now. */
3976 if (hold_gui_events)
3977 return;
3978
3979#ifdef FEAT_CMDWIN
3980 if (cmdwin_type != 0 && sb->wp != curwin)
3981 return;
3982#endif
3983
3984 if (still_dragging)
3985 {
3986 if (sb->wp == NULL)
3987 gui.dragged_sb = SBAR_BOTTOM;
3988 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3989 gui.dragged_sb = SBAR_LEFT;
3990 else
3991 gui.dragged_sb = SBAR_RIGHT;
3992 gui.dragged_wp = sb->wp;
3993 }
3994 else
3995 {
3996 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003997#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003999 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 gui.dragged_wp = NULL;
4001#endif
4002 }
4003
4004 /* Vertical sbar info is kept in the first sbar (the left one) */
4005 if (sb->wp != NULL)
4006 sb = &sb->wp->w_scrollbars[0];
4007
4008 /*
4009 * Check validity of value
4010 */
4011 if (value < 0)
4012 value = 0;
4013#ifdef SCROLL_PAST_END
4014 else if (value > sb->max)
4015 value = sb->max;
4016#else
4017 if (value > sb->max - sb->size + 1)
4018 value = sb->max - sb->size + 1;
4019#endif
4020
4021 sb->value = value;
4022
4023#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00004024 /* When not allowed to do the scrolling right now, return.
4025 * This also checked input_available(), but that causes the first click in
4026 * a scrollbar to be ignored when Vim doesn't have focus. */
4027 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return;
4029#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004030#ifdef FEAT_INS_EXPAND
4031 /* Disallow scrolling the current window when the completion popup menu is
4032 * visible. */
4033 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4034 return;
4035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037#ifdef FEAT_RIGHTLEFT
4038 if (sb->wp == NULL && curwin->w_p_rl)
4039 {
4040 value = sb->max + 1 - sb->size - value;
4041 if (value < 0)
4042 value = 0;
4043 }
4044#endif
4045
4046 if (sb->wp != NULL) /* vertical scrollbar */
4047 {
4048 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4050 sb_num++;
4051 if (wp == NULL)
4052 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054#ifdef USE_ON_FLY_SCROLL
4055 current_scrollbar = sb_num;
4056 scrollbar_value = value;
4057 if (State & NORMAL)
4058 {
4059 gui_do_scroll();
4060 setcursor();
4061 }
4062 else if (State & INSERT)
4063 {
4064 ins_scroll();
4065 setcursor();
4066 }
4067 else if (State & CMDLINE)
4068 {
4069 if (msg_scrolled == 0)
4070 {
4071 gui_do_scroll();
4072 redrawcmdline();
4073 }
4074 }
4075# ifdef FEAT_FOLDING
4076 /* Value may have been changed for closed fold. */
4077 sb->value = sb->wp->w_topline - 1;
4078# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004079
4080 /* When dragging one scrollbar and there is another one at the other
4081 * side move the thumb of that one too. */
4082 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4083 gui_mch_set_scrollbar_thumb(
4084 &sb->wp->w_scrollbars[
4085 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4086 ? SBAR_LEFT : SBAR_RIGHT],
4087 sb->value, sb->size, sb->max);
4088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089#else
4090 bytes[0] = CSI;
4091 bytes[1] = KS_VER_SCROLLBAR;
4092 bytes[2] = KE_FILLER;
4093 bytes[3] = (char_u)sb_num;
4094 byte_count = 4;
4095#endif
4096 }
4097 else
4098 {
4099#ifdef USE_ON_FLY_SCROLL
4100 scrollbar_value = value;
4101
4102 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004103 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 else if (State & INSERT)
4105 ins_horscroll();
4106 else if (State & CMDLINE)
4107 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004108 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004110 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 redrawcmdline();
4112 }
4113 }
4114 if (old_leftcol != curwin->w_leftcol)
4115 {
4116 updateWindow(curwin); /* update window, status and cmdline */
4117 setcursor();
4118 }
4119#else
4120 bytes[0] = CSI;
4121 bytes[1] = KS_HOR_SCROLLBAR;
4122 bytes[2] = KE_FILLER;
4123 byte_count = 3;
4124#endif
4125 }
4126
4127#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 /*
4129 * synchronize other windows, as necessary according to 'scrollbind'
4130 */
4131 if (curwin->w_p_scb
4132 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4133 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004134# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 ))))
4138 {
4139 do_check_scrollbind(TRUE);
4140 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004141 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 if (wp->w_redr_type > 0)
4143 updateWindow(wp);
4144 setcursor();
4145 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004146 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004148 add_to_input_buf(bytes, byte_count);
4149 add_long_to_buf((long_u)value, bytes);
4150 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152}
4153
4154/*
4155 * Scrollbar stuff:
4156 */
4157
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004158/*
4159 * Called when something in the window layout has changed.
4160 */
4161 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004162gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004163{
4164 if (gui.in_use && starting == 0)
4165 {
4166 out_flush();
4167 gui_init_which_components(NULL);
4168 gui_update_scrollbars(TRUE);
4169 }
4170 need_mouse_correct = TRUE;
4171}
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004174gui_update_scrollbars(
4175 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 win_T *wp;
4178 scrollbar_T *sb;
4179 long val, size, max; /* need 32 bits here */
4180 int which_sb;
4181 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 /* Update the horizontal scrollbar */
4185 gui_update_horiz_scrollbar(force);
4186
4187#ifndef WIN3264
4188 /* Return straight away if there is neither a left nor right scrollbar.
4189 * On MS-Windows this is required anyway for scrollwheel messages. */
4190 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4191 return;
4192#endif
4193
4194 /*
4195 * Don't want to update a scrollbar while we're dragging it. But if we
4196 * have both a left and right scrollbar, and we drag one of them, we still
4197 * need to update the other one.
4198 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4200 && gui.which_scrollbars[SBAR_LEFT]
4201 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /*
4204 * If we have two scrollbars and one of them is being dragged, just
4205 * copy the scrollbar position from the dragged one to the other one.
4206 */
4207 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4208 if (gui.dragged_wp != NULL)
4209 gui_mch_set_scrollbar_thumb(
4210 &gui.dragged_wp->w_scrollbars[which_sb],
4211 gui.dragged_wp->w_scrollbars[0].value,
4212 gui.dragged_wp->w_scrollbars[0].size,
4213 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215
4216 /* avoid that moving components around generates events */
4217 ++hold_gui_events;
4218
Bram Moolenaar45a24952016-07-24 22:25:15 +02004219 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 if (wp->w_buffer == NULL) /* just in case */
4222 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004223 /* Skip a scrollbar that is being dragged. */
4224 if (!force && (gui.dragged_sb == SBAR_LEFT
4225 || gui.dragged_sb == SBAR_RIGHT)
4226 && gui.dragged_wp == wp)
4227 continue;
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#ifdef SCROLL_PAST_END
4230 max = wp->w_buffer->b_ml.ml_line_count - 1;
4231#else
4232 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4233#endif
4234 if (max < 0) /* empty buffer */
4235 max = 0;
4236 val = wp->w_topline - 1;
4237 size = wp->w_height;
4238#ifdef SCROLL_PAST_END
4239 if (val > max) /* just in case */
4240 val = max;
4241#else
4242 if (size > max + 1) /* just in case */
4243 size = max + 1;
4244 if (val > max - size + 1)
4245 val = max - size + 1;
4246#endif
4247 if (val < 0) /* minimal value is 0 */
4248 val = 0;
4249
4250 /*
4251 * Scrollbar at index 0 (the left one) contains all the information.
4252 * It would be the same info for left and right so we just store it for
4253 * one of them.
4254 */
4255 sb = &wp->w_scrollbars[0];
4256
4257 /*
4258 * Note: no check for valid w_botline. If it's not valid the
4259 * scrollbars will be updated later anyway.
4260 */
4261 if (size < 1 || wp->w_botline - 2 > max)
4262 {
4263 /*
4264 * This can happen during changing files. Just don't update the
4265 * scrollbar for now.
4266 */
4267 sb->height = 0; /* Force update next time */
4268 if (gui.which_scrollbars[SBAR_LEFT])
4269 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4270 if (gui.which_scrollbars[SBAR_RIGHT])
4271 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4272 continue;
4273 }
4274 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 || sb->top != wp->w_winrow
4276 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004278 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 /* Height, width or position of scrollbar has changed. For
4281 * vertical split: curwin changed. */
4282 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 sb->top = wp->w_winrow;
4284 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /* Calculate height and position in pixels */
4288 h = (sb->height + sb->status_height) * gui.char_height;
4289 y = sb->top * gui.char_height + gui.border_offset;
4290#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4291 if (gui.menu_is_active)
4292 y += gui.menu_height;
4293#endif
4294
4295#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4296 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4297# ifdef FEAT_GUI_ATHENA
4298 y += gui.toolbar_height;
4299# else
4300# ifdef FEAT_GUI_MSWIN
4301 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4302# endif
4303# endif
4304#endif
4305
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004306#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4307 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004308 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004309#endif
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 /* Height of top scrollbar includes width of top border */
4314 h += gui.border_offset;
4315 y -= gui.border_offset;
4316 }
4317 if (gui.which_scrollbars[SBAR_LEFT])
4318 {
4319 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4320 gui.left_sbar_x, y,
4321 gui.scrollbar_width, h);
4322 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4323 }
4324 if (gui.which_scrollbars[SBAR_RIGHT])
4325 {
4326 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4327 gui.right_sbar_x, y,
4328 gui.scrollbar_width, h);
4329 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4330 }
4331 }
4332
4333 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4334 * checking if the thumb moved at least a pixel. Only do this for
4335 * Athena, most other GUIs require the update anyway to make the
4336 * arrows work. */
4337#ifdef FEAT_GUI_ATHENA
4338 if (max == 0)
4339 y = 0;
4340 else
4341 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4342 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4343#else
4344 if (force || sb->value != val || sb->size != size || sb->max != max)
4345#endif
4346 {
4347 /* Thumb of scrollbar has moved */
4348 sb->value = val;
4349#ifdef FEAT_GUI_ATHENA
4350 sb->pixval = y;
4351#endif
4352 sb->size = size;
4353 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004354 if (gui.which_scrollbars[SBAR_LEFT]
4355 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4357 val, size, max);
4358 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004359 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4361 val, size, max);
4362 }
4363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 --hold_gui_events;
4366}
4367
4368/*
4369 * Enable or disable a scrollbar.
4370 * Check for scrollbars for vertically split windows which are not enabled
4371 * sometimes.
4372 */
4373 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004374gui_do_scrollbar(
4375 win_T *wp,
4376 int which, /* SBAR_LEFT or SBAR_RIGHT */
4377 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 int midcol = curwin->w_wincol + curwin->w_width / 2;
4380 int has_midcol = (wp->w_wincol <= midcol
4381 && wp->w_wincol + wp->w_width >= midcol);
4382
4383 /* Only enable scrollbars that contain the middle column of the current
4384 * window. */
4385 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4386 {
4387 /* Scrollbars only on one side. Don't enable scrollbars that don't
4388 * contain the middle column of the current window. */
4389 if (!has_midcol)
4390 enable = FALSE;
4391 }
4392 else
4393 {
4394 /* Scrollbars on both sides. Don't enable scrollbars that neither
4395 * contain the middle column of the current window nor are on the far
4396 * side. */
4397 if (midcol > Columns / 2)
4398 {
4399 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4400 enable = FALSE;
4401 }
4402 else
4403 {
4404 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4405 : !has_midcol)
4406 enable = FALSE;
4407 }
4408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4410}
4411
4412/*
4413 * Scroll a window according to the values set in the globals current_scrollbar
4414 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4415 * or FALSE otherwise.
4416 */
4417 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004418gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 win_T *wp, *save_wp;
4421 int i;
4422 long nlines;
4423 pos_T old_cursor;
4424 linenr_T old_topline;
4425#ifdef FEAT_DIFF
4426 int old_topfill;
4427#endif
4428
4429 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4430 if (wp == NULL)
4431 break;
4432 if (wp == NULL)
4433 /* Couldn't find window */
4434 return FALSE;
4435
4436 /*
4437 * Compute number of lines to scroll. If zero, nothing to do.
4438 */
4439 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4440 if (nlines == 0)
4441 return FALSE;
4442
4443 save_wp = curwin;
4444 old_topline = wp->w_topline;
4445#ifdef FEAT_DIFF
4446 old_topfill = wp->w_topfill;
4447#endif
4448 old_cursor = wp->w_cursor;
4449 curwin = wp;
4450 curbuf = wp->w_buffer;
4451 if (nlines < 0)
4452 scrolldown(-nlines, gui.dragged_wp == NULL);
4453 else
4454 scrollup(nlines, gui.dragged_wp == NULL);
4455 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4456 * the mouse-up event already, but we still want it to behave like when
4457 * dragging. But not the next click in an arrow. */
4458 if (gui.dragged_sb == SBAR_NONE)
4459 gui.dragged_wp = NULL;
4460
4461 if (old_topline != wp->w_topline
4462#ifdef FEAT_DIFF
4463 || old_topfill != wp->w_topfill
4464#endif
4465 )
4466 {
4467 if (p_so != 0)
4468 {
4469 cursor_correct(); /* fix window for 'so' */
4470 update_topline(); /* avoid up/down jump */
4471 }
4472 if (old_cursor.lnum != wp->w_cursor.lnum)
4473 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004477 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4478 validate_cursor();
4479
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 curwin = save_wp;
4481 curbuf = save_wp->w_buffer;
4482
4483 /*
4484 * Don't call updateWindow() when nothing has changed (it will overwrite
4485 * the status line!).
4486 */
4487 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004488 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489#ifdef FEAT_DIFF
4490 || old_topfill != wp->w_topfill
4491#endif
4492 )
4493 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004494 int type = VALID;
4495
4496#ifdef FEAT_INS_EXPAND
4497 if (pum_visible())
4498 {
4499 type = NOT_VALID;
4500 wp->w_lines_valid = 0;
4501 }
4502#endif
4503 /* Don't set must_redraw here, it may cause the popup menu to
4504 * disappear when losing focus after a scrollbar drag. */
4505 if (wp->w_redr_type < type)
4506 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004507 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004509 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004512#ifdef FEAT_INS_EXPAND
4513 /* May need to redraw the popup menu. */
4514 if (pum_visible())
4515 pum_redraw();
4516#endif
4517
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004518 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519}
4520
4521
4522/*
4523 * Horizontal scrollbar stuff:
4524 */
4525
4526/*
4527 * Return length of line "lnum" for horizontal scrolling.
4528 */
4529 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004530scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
4532 char_u *p;
4533 colnr_T col;
4534 int w;
4535
4536 p = ml_get(lnum);
4537 col = 0;
4538 if (*p != NUL)
4539 for (;;)
4540 {
4541 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004542 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (*p == NUL) /* don't count the last character */
4544 break;
4545 col += w;
4546 }
4547 return col;
4548}
4549
4550/* Remember which line is currently the longest, so that we don't have to
4551 * search for it when scrolling horizontally. */
4552static linenr_T longest_lnum = 0;
4553
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004554/*
4555 * Find longest visible line number. If this is not possible (or not desired,
4556 * by setting 'h' in "guioptions") then the current line number is returned.
4557 */
4558 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004559gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004560{
4561 linenr_T ret = 0;
4562
4563 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4564 * line numbers, topline and botline can be invalid when displaying is
4565 * postponed. */
4566 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4567 && curwin->w_topline <= curwin->w_cursor.lnum
4568 && curwin->w_botline > curwin->w_cursor.lnum
4569 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4570 {
4571 linenr_T lnum;
4572 colnr_T n;
4573 long max = 0;
4574
4575 /* Use maximum of all visible lines. Remember the lnum of the
4576 * longest line, closest to the cursor line. Used when scrolling
4577 * below. */
4578 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4579 {
4580 n = scroll_line_len(lnum);
4581 if (n > (colnr_T)max)
4582 {
4583 max = n;
4584 ret = lnum;
4585 }
4586 else if (n == (colnr_T)max
4587 && abs((int)(lnum - curwin->w_cursor.lnum))
4588 < abs((int)(ret - curwin->w_cursor.lnum)))
4589 ret = lnum;
4590 }
4591 }
4592 else
4593 /* Use cursor line only. */
4594 ret = curwin->w_cursor.lnum;
4595
4596 return ret;
4597}
4598
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004600gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
4602 long value, size, max; /* need 32 bit ints here */
4603
4604 if (!gui.which_scrollbars[SBAR_BOTTOM])
4605 return;
4606
4607 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4608 return;
4609
4610 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4611 return;
4612
4613 /*
4614 * It is possible for the cursor to be invalid if we're in the middle of
4615 * something (like changing files). If so, don't do anything for now.
4616 */
4617 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4618 {
4619 gui.bottom_sbar.value = -1;
4620 return;
4621 }
4622
Bram Moolenaar02631462017-09-22 15:20:32 +02004623 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (curwin->w_p_wrap)
4625 {
4626 value = 0;
4627#ifdef SCROLL_PAST_END
4628 max = 0;
4629#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004630 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#endif
4632 }
4633 else
4634 {
4635 value = curwin->w_leftcol;
4636
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004637 longest_lnum = gui_find_longest_lnum();
4638 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#ifdef FEAT_VIRTUALEDIT
4641 if (virtual_active())
4642 {
4643 /* May move the cursor even further to the right. */
4644 if (curwin->w_virtcol >= (colnr_T)max)
4645 max = curwin->w_virtcol;
4646 }
4647#endif
4648
4649#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004650 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651#endif
4652 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004653 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 size -= curwin_col_off();
4655#ifndef SCROLL_PAST_END
4656 max -= curwin_col_off();
4657#endif
4658 }
4659
4660#ifndef SCROLL_PAST_END
4661 if (value > max - size + 1)
4662 value = max - size + 1; /* limit the value to allowable range */
4663#endif
4664
4665#ifdef FEAT_RIGHTLEFT
4666 if (curwin->w_p_rl)
4667 {
4668 value = max + 1 - size - value;
4669 if (value < 0)
4670 {
4671 size += value;
4672 value = 0;
4673 }
4674 }
4675#endif
4676 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4677 && max == gui.bottom_sbar.max)
4678 return;
4679
4680 gui.bottom_sbar.value = value;
4681 gui.bottom_sbar.size = size;
4682 gui.bottom_sbar.max = max;
4683 gui.prev_wrap = curwin->w_p_wrap;
4684
4685 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4686}
4687
4688/*
4689 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4690 */
4691 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004692gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 /* no wrapping, no scrolling */
4695 if (curwin->w_p_wrap)
4696 return FALSE;
4697
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004698 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 return FALSE;
4700
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004701 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004704 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004706 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004707 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004709 if (compute_longest_lnum)
4710 {
4711 curwin->w_cursor.lnum = gui_find_longest_lnum();
4712 curwin->w_cursor.col = 0;
4713 }
4714 /* Do a sanity check on "longest_lnum", just in case. */
4715 else if (longest_lnum >= curwin->w_topline
4716 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
4718 curwin->w_cursor.lnum = longest_lnum;
4719 curwin->w_cursor.col = 0;
4720 }
4721 }
4722
4723 return leftcol_changed();
4724}
4725
4726/*
4727 * Check that none of the colors are the same as the background color
4728 */
4729 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004730gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4733 {
4734 gui_set_bg_color((char_u *)"White");
4735 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4736 gui_set_fg_color((char_u *)"Black");
4737 }
4738}
4739
Bram Moolenaar3918c952005-03-15 22:34:55 +00004740 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004741gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 gui.norm_pixel = gui_get_color(name);
4744 hl_set_fg_color_name(vim_strsave(name));
4745}
4746
Bram Moolenaar3918c952005-03-15 22:34:55 +00004747 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004748gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 gui.back_pixel = gui_get_color(name);
4751 hl_set_bg_color_name(vim_strsave(name));
4752}
4753
4754/*
4755 * Allocate a color by name.
4756 * Returns INVALCOLOR and gives an error message when failed.
4757 */
4758 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004759gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 guicolor_T t;
4762
4763 if (*name == NUL)
4764 return INVALCOLOR;
4765 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004768#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 && gui.in_use
4770#endif
4771 )
4772 EMSG2(_("E254: Cannot allocate color %s"), name);
4773 return t;
4774}
4775
4776/*
4777 * Return the grey value of a color (range 0-255).
4778 */
4779 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004780gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004782 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004784 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004785 + (((rgb >> 8) & 0xff) * 587)
4786 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787}
4788
4789#if defined(FEAT_GUI_X11) || defined(PROTO)
4790 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004791gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 win_T *wp;
4794
4795 /* Nothing to do if GUI hasn't started yet. */
4796 if (!gui.in_use)
4797 return;
4798
4799 FOR_ALL_WINDOWS(wp)
4800 {
4801 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4802 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4803 }
4804 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4805}
4806#endif
4807
4808/*
4809 * Call this when focus has changed.
4810 */
4811 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004812gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814/*
4815 * Skip this code to avoid drawing the cursor when debugging and switching
4816 * between the debugger window and gvim.
4817 */
4818#if 1
4819 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004820 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822# ifdef FEAT_XIM
4823 xim_set_focus(in_focus);
4824# endif
4825
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004826 /* Put events in the input queue only when allowed.
4827 * ui_focus_change() isn't called directly, because it invokes
4828 * autocommands and that must not happen asynchronously. */
4829 if (!hold_gui_events)
4830 {
4831 char_u bytes[3];
4832
4833 bytes[0] = CSI;
4834 bytes[1] = KS_EXTRA;
4835 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4836 add_to_input_buf(bytes, 3);
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
4839}
4840
4841/*
4842 * Called when the mouse moved (but not when dragging).
4843 */
4844 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004845gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
4847 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004848 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaard3667a22006-03-16 21:35:52 +00004850 /* Ignore this while still starting up. */
4851 if (!gui.in_use || gui.starting)
4852 return;
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#ifdef FEAT_MOUSESHAPE
4855 /* Get window pointer, and update mouse shape as well. */
4856 wp = xy2win(x, y);
4857#endif
4858
4859 /* Only handle this when 'mousefocus' set and ... */
4860 if (p_mousef
4861 && !hold_gui_events /* not holding events */
4862 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4863 && State != HITRETURN /* but not hit-return prompt */
4864 && msg_scrolled == 0 /* no scrolled message */
4865 && !need_mouse_correct /* not moving the pointer */
4866 && gui.in_focus) /* gvim in focus */
4867 {
4868 /* Don't move the mouse when it's left or right of the Vim window */
4869 if (x < 0 || x > Columns * gui.char_width)
4870 return;
4871#ifndef FEAT_MOUSESHAPE
4872 wp = xy2win(x, y);
4873#endif
4874 if (wp == curwin || wp == NULL)
4875 return; /* still in the same old window, or none at all */
4876
Bram Moolenaar9c102382006-05-03 21:26:49 +00004877 /* Ignore position in the tab pages line. */
4878 if (Y_2_ROW(y) < tabline_height())
4879 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 /*
4882 * format a mouse click on status line input
4883 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004884 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4885 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 */
4887 if (finish_op)
4888 {
4889 /* abort the current operator first */
4890 st[0] = ESC;
4891 add_to_input_buf(st, 1);
4892 }
4893 st[0] = CSI;
4894 st[1] = KS_MOUSE;
4895 st[2] = KE_FILLER;
4896 st[3] = (char_u)MOUSE_LEFT;
4897 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004898 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 wp->w_height + W_WINROW(wp));
4900
4901 add_to_input_buf(st, 8);
4902 st[3] = (char_u)MOUSE_RELEASE;
4903 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#ifdef FEAT_GUI_GTK
4905 /* Need to wake up the main loop */
4906 if (gtk_main_level() > 0)
4907 gtk_main_quit();
4908#endif
4909 }
4910}
4911
4912/*
4913 * Called when mouse should be moved to window with focus.
4914 */
4915 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004916gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 int x, y;
4919 win_T *wp = NULL;
4920
4921 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004922
4923 if (!(gui.in_use && p_mousef))
4924 return;
4925
4926 gui_mch_getmouse(&x, &y);
4927 /* Don't move the mouse when it's left or right of the Vim window */
4928 if (x < 0 || x > Columns * gui.char_width)
4929 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004930 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004931 wp = xy2win(x, y);
4932 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004934 validate_cline_row();
4935 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4936 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939}
4940
4941/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004942 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004945xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int row;
4948 int col;
4949 win_T *wp;
4950
4951 row = Y_2_ROW(y);
4952 col = X_2_COL(x);
4953 if (row < 0 || col < 0) /* before first window */
4954 return NULL;
4955 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004956 if (wp == NULL)
4957 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004958#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (State == HITRETURN || State == ASKMORE)
4960 {
4961 if (Y_2_ROW(y) >= msg_row)
4962 update_mouseshape(SHAPE_IDX_MOREL);
4963 else
4964 update_mouseshape(SHAPE_IDX_MORE);
4965 }
4966 else if (row > wp->w_height) /* below status line */
4967 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004968 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004969 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004971 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004972 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 update_mouseshape(SHAPE_IDX_STATUS);
4974 else
4975 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004977 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978}
4979
4980/*
4981 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4982 * File names may be given to redefine the args list.
4983 */
4984 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004985ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 char_u *arg = eap->arg;
4988
4989 /*
4990 * Check for "-f" argument: foreground, don't fork.
4991 * Also don't fork when started with "gvim -f".
4992 * Do fork when using "gui -b".
4993 */
4994 if (arg[0] == '-'
4995 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004996 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 gui.dofork = (arg[1] == 'b');
4999 eap->arg = skipwhite(eap->arg + 2);
5000 }
5001 if (!gui.in_use)
5002 {
5003 /* Clear the command. Needed for when forking+exiting, to avoid part
5004 * of the argument ending up after the shell prompt. */
5005 msg_clr_eos_force();
5006 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005007#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005008 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 if (!ends_excmd(*eap->arg))
5012 ex_next(eap);
5013}
5014
5015#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00005016 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017/*
5018 * This is shared between Athena, Motif and GTK.
5019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
5021/*
5022 * Callback function for do_in_runtimepath().
5023 */
5024 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005025gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005027 char_u *gfp_buffer = cookie;
5028
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (STRLEN(fname) >= MAXPATHL)
5030 *gfp_buffer = NUL;
5031 else
5032 STRCPY(gfp_buffer, fname);
5033}
5034
5035/*
5036 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5037 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5038 */
5039 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005040gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 if (STRLEN(name) > MAXPATHL - 14)
5043 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005044 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005045 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005046 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 return FAIL;
5048 return OK;
5049}
5050
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005051# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052/*
5053 * Given the name of the "icon=" argument, try finding the bitmap file for the
5054 * icon. If it is an absolute path name, use it as it is. Otherwise append
5055 * "ext" and search for it in 'runtimepath'.
5056 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5057 * contains "name".
5058 */
5059 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005060gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061{
5062 char_u buf[MAXPATHL + 1];
5063
5064 expand_env(name, buffer, MAXPATHL);
5065 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5066 STRCPY(buffer, buf);
5067}
5068# endif
5069#endif
5070
Bram Moolenaar9372a112005-12-06 19:59:18 +00005071#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005073display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
5075 char_u *p;
5076
5077 if (isatty(2))
5078 fflush(stderr);
5079 else if (error_ga.ga_data != NULL)
5080 {
5081 /* avoid putting up a message box with blanks only */
5082 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5083 if (!isspace(*p))
5084 {
5085 /* Truncate a very long message, it will go off-screen. */
5086 if (STRLEN(p) > 2000)
5087 STRCPY(p + 2000 - 14, "...(truncated)");
5088 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005089 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091 }
5092 ga_clear(&error_ga);
5093 }
5094}
5095#endif
5096
5097#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5098/*
5099 * Return TRUE if still starting up and there is no place to enter text.
5100 * For GTK and X11 we check if stderr is not a tty, which means we were
5101 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5102 * allow typing on stdin.
5103 */
5104 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005105no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106{
5107 return ((!gui.in_use || gui.starting)
5108# ifndef NO_CONSOLE
5109 && !isatty(0) && !isatty(2)
5110# endif
5111 );
5112}
5113#endif
5114
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005115#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005116 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005117 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118/*
5119 * Update the current window and the screen.
5120 */
5121 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005122gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005124# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005125 linenr_T conceal_old_cursor_line = 0;
5126 linenr_T conceal_new_cursor_line = 0;
5127 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005128# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 update_topline();
5131 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005132
Bram Moolenaarec80df72008-05-07 19:46:51 +00005133 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005134 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005135# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005136 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005137# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005138 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005139 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005140 if (has_cursormoved())
5141 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005142# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005143 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144 {
5145 conceal_old_cursor_line = last_cursormoved.lnum;
5146 conceal_new_cursor_line = curwin->w_cursor.lnum;
5147 conceal_update_lines = TRUE;
5148 }
5149# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005150 last_cursormoved = curwin->w_cursor;
5151 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 update_screen(0); /* may need to update the screen */
5154 setcursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005155# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005156 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005157 && (conceal_old_cursor_line != conceal_new_cursor_line
5158 || conceal_cursor_line(curwin)
5159 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005160 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005161 if (conceal_old_cursor_line != conceal_new_cursor_line)
5162 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005163 update_single_line(curwin, conceal_new_cursor_line);
5164 curwin->w_valid &= ~VALID_CROW;
5165 }
5166# endif
Bram Moolenaara338adc2018-01-31 20:51:47 +01005167 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168}
5169#endif
5170
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005171#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172/*
5173 * Get the text to use in a find/replace dialog. Uses the last search pattern
5174 * if the argument is empty.
5175 * Returns an allocated string.
5176 */
5177 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005178get_find_dialog_text(
5179 char_u *arg,
5180 int *wwordp, /* return: TRUE if \< \> found */
5181 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 char_u *text;
5184
5185 if (*arg == NUL)
5186 text = last_search_pat();
5187 else
5188 text = arg;
5189 if (text != NULL)
5190 {
5191 text = vim_strsave(text);
5192 if (text != NULL)
5193 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005194 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 int i;
5196
5197 /* Remove "\V" */
5198 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5199 {
5200 mch_memmove(text, text + 2, (size_t)(len - 1));
5201 len -= 2;
5202 }
5203
5204 /* Recognize "\c" and "\C" and remove. */
5205 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5206 {
5207 *mcasep = (text[1] == 'C');
5208 mch_memmove(text, text + 2, (size_t)(len - 1));
5209 len -= 2;
5210 }
5211
5212 /* Recognize "\<text\>" and remove. */
5213 if (len >= 4
5214 && STRNCMP(text, "\\<", 2) == 0
5215 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5216 {
5217 *wwordp = TRUE;
5218 mch_memmove(text, text + 2, (size_t)(len - 4));
5219 text[len - 4] = NUL;
5220 }
5221
5222 /* Recognize "\/" or "\?" and remove. */
5223 for (i = 0; i + 1 < len; ++i)
5224 if (text[i] == '\\' && (text[i + 1] == '/'
5225 || text[i + 1] == '?'))
5226 {
5227 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5228 --len;
5229 }
5230 }
5231 }
5232 return text;
5233}
5234
5235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 * Handle the press of a button in the find-replace dialog.
5237 * Return TRUE when something was added to the input buffer.
5238 */
5239 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005240gui_do_findrepl(
5241 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5242 char_u *find_text,
5243 char_u *repl_text,
5244 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 garray_T ga;
5247 int i;
5248 int type = (flags & FRD_TYPE_MASK);
5249 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005250 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005251 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005252 static int busy = FALSE;
5253
5254 /* When the screen is being updated we should not change buffers and
5255 * windows structures, it may cause freed memory to be used. Also don't
5256 * do this recursively (pressing "Find" quickly several times. */
5257 if (updating_screen || busy)
5258 return FALSE;
5259
5260 /* refuse replace when text cannot be changed */
5261 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5262 return FALSE;
5263
5264 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005267 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 ga_concat(&ga, (char_u *)"%s/");
5269
5270 ga_concat(&ga, (char_u *)"\\V");
5271 if (flags & FRD_MATCH_CASE)
5272 ga_concat(&ga, (char_u *)"\\C");
5273 else
5274 ga_concat(&ga, (char_u *)"\\c");
5275 if (flags & FRD_WHOLE_WORD)
5276 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005277 /* escape / and \ */
5278 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5279 if (p != NULL)
5280 ga_concat(&ga, p);
5281 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (flags & FRD_WHOLE_WORD)
5283 ga_concat(&ga, (char_u *)"\\>");
5284
5285 if (type == FRD_REPLACEALL)
5286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005288 /* escape / and \ */
5289 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5290 if (p != NULL)
5291 ga_concat(&ga, p);
5292 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005294 }
5295 ga_append(&ga, NUL);
5296
5297 if (type == FRD_REPLACE)
5298 {
5299 /* Do the replacement when the text at the cursor matches. Thus no
5300 * replacement is done if the cursor was moved! */
5301 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5302 regmatch.rm_ic = 0;
5303 if (regmatch.regprog != NULL)
5304 {
5305 p = ml_get_cursor();
5306 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5307 && regmatch.startp[0] == p)
5308 {
5309 /* Clear the command line to remove any old "No match"
5310 * error. */
5311 msg_end_prompt();
5312
5313 if (u_save_cursor() == OK)
5314 {
5315 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005316 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005317
5318 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005319 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005320 ins_str(repl_text);
5321 }
5322 }
5323 else
5324 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005325 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005326 }
5327 }
5328
5329 if (type == FRD_REPLACEALL)
5330 {
5331 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005332 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 do_cmdline_cmd(ga.ga_data);
5334 }
5335 else
5336 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005337 int searchflags = SEARCH_MSG + SEARCH_MARK;
5338
5339 /* Search for the next match.
5340 * Don't skip text under cursor for single replace. */
5341 if (type == FRD_REPLACE)
5342 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005344 if (down)
5345 {
5346 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5347 }
5348 else
5349 {
5350 /* We need to escape '?' if and only if we are searching in the up
5351 * direction */
5352 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5353 if (p != NULL)
5354 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5355 vim_free(p);
5356 }
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 msg_scroll = i; /* don't let an error message set msg_scroll */
5359 }
5360
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005361 /* Don't want to pass did_emsg to other code, it may cause disabling
5362 * syntax HL if we were busy redrawing. */
5363 did_emsg = save_did_emsg;
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 if (State & (NORMAL | INSERT))
5366 {
5367 gui_update_screen(); /* update the screen */
5368 msg_didout = 0; /* overwrite any message */
5369 need_wait_return = FALSE; /* don't wait for return */
5370 }
5371
5372 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005373 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return (ga.ga_len > 0);
5375}
5376
5377#endif
5378
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005379#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380/*
5381 * Jump to the window at specified point (x, y).
5382 */
5383 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005384gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
5386 int row = Y_2_ROW(y);
5387 int col = X_2_COL(x);
5388 win_T *wp;
5389
5390 if (row >= 0 && col >= 0)
5391 {
5392 wp = mouse_find_win(&row, &col);
5393 if (wp != NULL && wp != curwin)
5394 win_goto(wp);
5395 }
5396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005399 * Function passed to handle_drop() for the actions to be done after the
5400 * argument list has been updated.
5401 */
5402 static void
5403drop_callback(void *cookie)
5404{
5405 char_u *p = cookie;
5406
5407 /* If Shift held down, change to first file's directory. If the first
5408 * item is a directory, change to that directory (and let the explorer
5409 * plugin show the contents). */
5410 if (p != NULL)
5411 {
5412 if (mch_isdir(p))
5413 {
5414 if (mch_chdir((char *)p) == 0)
5415 shorten_fnames(TRUE);
5416 }
5417 else if (vim_chdirfile(p, "drop") == OK)
5418 shorten_fnames(TRUE);
5419 vim_free(p);
5420 }
5421
5422 /* Update the screen display */
5423 update_screen(NOT_VALID);
5424# ifdef FEAT_MENU
5425 gui_update_menus(0);
5426# endif
5427#ifdef FEAT_TITLE
5428 maketitle();
5429#endif
5430 setcursor();
5431 out_flush_cursor(FALSE, FALSE);
5432}
5433
5434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 * Process file drop. Mouse cursor position, key modifiers, name of files
5436 * and count of files are given. Argument "fnames[count]" has full pathnames
5437 * of dropped files, they will be freed in this function, and caller can't use
5438 * fnames after call this function.
5439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005441gui_handle_drop(
5442 int x UNUSED,
5443 int y UNUSED,
5444 int_u modifiers,
5445 char_u **fnames,
5446 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 int i;
5449 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005450 static int entered = FALSE;
5451
5452 /*
5453 * This function is called by event handlers. Just in case we get a
5454 * second event before the first one is handled, ignore the second one.
5455 * Not sure if this can ever happen, just in case.
5456 */
5457 if (entered)
5458 return;
5459 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461 /*
5462 * When the cursor is at the command line, add the file names to the
5463 * command line, don't edit the files.
5464 */
5465 if (State & CMDLINE)
5466 {
5467 shorten_filenames(fnames, count);
5468 for (i = 0; i < count; ++i)
5469 {
5470 if (fnames[i] != NULL)
5471 {
5472 if (i > 0)
5473 add_to_input_buf((char_u*)" ", 1);
5474
5475 /* We don't know what command is used thus we can't be sure
5476 * about which characters need to be escaped. Only escape the
5477 * most common ones. */
5478# ifdef BACKSLASH_IN_FILENAME
5479 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5480# else
5481 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5482# endif
5483 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005484 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 vim_free(p);
5486 vim_free(fnames[i]);
5487 }
5488 }
5489 vim_free(fnames);
5490 }
5491 else
5492 {
5493 /* Go to the window under mouse cursor, then shorten given "fnames" by
5494 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 shorten_filenames(fnames, count);
5497
5498 /* If Shift held down, remember the first item. */
5499 if ((modifiers & MOUSE_SHIFT) != 0)
5500 p = vim_strsave(fnames[0]);
5501 else
5502 p = NULL;
5503
5504 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005505 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * directory and Shift is held down. */
5507 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5508 && mch_isdir(fnames[0]))
5509 {
5510 vim_free(fnames[0]);
5511 vim_free(fnames);
5512 }
5513 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005514 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5515 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005517
5518 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519}
5520#endif