blob: 87ea5ce2b0dd8d7d5d2e2642ea759eac2f9a60ca [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
20static void gui_position_components(int);
21static void gui_outstr(char_u *, int);
22static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020023#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010024static int gui_screenstr(int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010026static void gui_delete_lines(int row, int count);
27static void gui_insert_lines(int row, int count);
28static void fill_mouse_coord(char_u *p, int col, int row);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010030static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000031#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010032static void gui_do_scrollbar(win_T *wp, int which, int enable);
33static colnr_T scroll_line_len(linenr_T lnum);
34static linenr_T gui_find_longest_lnum(void);
35static void gui_update_horiz_scrollbar(int);
36static void gui_set_fg_color(char_u *name);
37static void gui_set_bg_color(char_u *name);
38static win_T *xy2win(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020040#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020042
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020044
45/* Return values for gui_read_child_pipe */
46enum {
47 GUI_CHILD_IO_ERROR,
48 GUI_CHILD_OK,
49 GUI_CHILD_FAILED
50};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020051#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020052
Bram Moolenaard25c16e2016-01-29 22:13:30 +010053static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020054
Bram Moolenaar071d4272004-06-13 20:20:40 +000055static int can_update_cursor = TRUE; /* can display the cursor */
Bram Moolenaara338adc2018-01-31 20:51:47 +010056static int disable_flush = 0; /* If > 0, gui_mch_flush() is disabled. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
59 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
60 * this makes the thumb indicate the part of the text that is shown. Motif
61 * can't do this.
62 */
63#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
64# define SCROLL_PAST_END
65#endif
66
67/*
68 * gui_start -- Called when user wants to start the GUI.
69 *
70 * Careful: This function can be called recursively when there is a ":gui"
71 * command in the .gvimrc file. Only the first call should fork, not the
72 * recursive call.
73 */
74 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +010075gui_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 static int recursive = 0;
79
80 old_term = vim_strsave(T_NAME);
81
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 settmode(TMODE_COOK); /* stop RAW mode */
83 if (full_screen)
84 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 full_screen = FALSE;
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 ++recursive;
88
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020089#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 /*
91 * Quit the current process and continue in the child.
92 * Makes "gvim file" disconnect from the shell it was started in.
93 * Don't do this when Vim was started with "-f" or the 'f' flag is present
94 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020095 * Don't do this when there is a running job, we can only get the status
96 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020097 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020098 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
99# ifdef FEAT_JOB_CHANNEL
100 && !job_any_running()
101# endif
102 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200103 {
104 gui_do_fork();
105 }
106 else
107#endif
108 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200109#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200110 /* If there is 'f' in 'guioptions' and specify -g argument,
111 * gui_mch_init_check() was not called yet. */
112 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100113 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200114#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200115 gui_attempt_start();
116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
118 if (!gui.in_use) /* failed to start GUI */
119 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200120 /* Back to old term settings
121 *
122 * FIXME: If we got here because a child process failed and flagged to
123 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
124 * hit an X11 I/O error and do a longjmp(), leaving recursive
125 * permanently set to 1. This is probably not as big a problem as it
126 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
127 * return "OK" unconditionally, so it would be very difficult to
128 * actually hit this case.
129 */
130 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 settmode(TMODE_RAW); /* restart RAW mode */
132#ifdef FEAT_TITLE
133 set_title_defaults(); /* set 'title' and 'icon' again */
134#endif
135 }
136
137 vim_free(old_term);
138
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200139 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
140 * the GUIFailed event. */
141 gui_mch_update();
142 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
143 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200144 --recursive;
145}
146
147/*
148 * Set_termname() will call gui_init() to start the GUI.
149 * Set the "starting" flag, to indicate that the GUI will start.
150 *
151 * We don't want to open the GUI shell until after we've read .gvimrc,
152 * otherwise we don't know what font we will use, and hence we don't know
153 * what size the shell should be. So if there are errors in the .gvimrc
154 * file, they will have to go to the terminal: Set full_screen to FALSE.
155 * full_screen will be set to TRUE again by a successful termcapinit().
156 */
157 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100158gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200159{
160 static int recursive = 0;
161
162 ++recursive;
163 gui.starting = TRUE;
164
165#ifdef FEAT_GUI_GTK
166 gui.event_time = GDK_CURRENT_TIME;
167#endif
168
169 termcapinit((char_u *)"builtin_gui");
170 gui.starting = recursive - 1;
171
Bram Moolenaar9372a112005-12-06 19:59:18 +0000172#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200174 {
175# ifdef FEAT_EVAL
176 Window x11_window;
177 Display *x11_display;
178
179 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
180 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
181# endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 /* Display error messages in a dialog now. */
184 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 --recursive;
188}
189
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200190#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200191
192/* for waitpid() */
193# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
194# include <sys/wait.h>
195# endif
196
197/*
198 * Create a new process, by forking. In the child, start the GUI, and in
199 * the parent, exit.
200 *
201 * If something goes wrong, this will return with gui.in_use still set
202 * to FALSE, in which case the caller should continue execution without
203 * the GUI.
204 *
205 * If the child fails to start the GUI, then the child will exit and the
206 * parent will return. If the child succeeds, then the parent will exit
207 * and the child will return.
208 */
209 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100210gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200211{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200212 int pipefd[2]; /* pipe between parent and child */
213 int pipe_error;
214 int status;
215 int exit_status;
216 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200217
218 /* Setup a pipe between the child and the parent, so that the parent
219 * knows when the child has done the setsid() call and is allowed to
220 * exit. */
221 pipe_error = (pipe(pipefd) < 0);
222 pid = fork();
223 if (pid < 0) /* Fork error */
224 {
225 EMSG(_("E851: Failed to create a new process for the GUI"));
226 return;
227 }
228 else if (pid > 0) /* Parent */
229 {
230 /* Give the child some time to do the setsid(), otherwise the
231 * exit() may kill the child too (when starting gvim from inside a
232 * gvim). */
233 if (!pipe_error)
234 {
235 /* The read returns when the child closes the pipe (or when
236 * the child dies for some reason). */
237 close(pipefd[1]);
238 status = gui_read_child_pipe(pipefd[0]);
239 if (status == GUI_CHILD_FAILED)
240 {
241 /* The child failed to start the GUI, so the caller must
242 * continue. There may be more error information written
243 * to stderr by the child. */
244# ifdef __NeXT__
245 wait4(pid, &exit_status, 0, (struct rusage *)0);
246# else
247 waitpid(pid, &exit_status, 0);
248# endif
249 EMSG(_("E852: The child process failed to start the GUI"));
250 return;
251 }
252 else if (status == GUI_CHILD_IO_ERROR)
253 {
254 pipe_error = TRUE;
255 }
256 /* else GUI_CHILD_OK: parent exit */
257 }
258
259 if (pipe_error)
260 ui_delay(300L, TRUE);
261
262 /* When swapping screens we may need to go to the next line, e.g.,
263 * after a hit-enter prompt and using ":gui". */
264 if (newline_on_exit)
265 mch_errmsg("\r\n");
266
267 /*
268 * The parent must skip the normal exit() processing, the child
269 * will do it. For example, GTK messes up signals when exiting.
270 */
271 _exit(0);
272 }
273 /* Child */
274
Bram Moolenaar29695702012-05-18 17:03:18 +0200275#ifdef FEAT_GUI_GTK
276 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
277 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100278 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200279#endif
280
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200281# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
282 /*
283 * Change our process group. On some systems/shells a CTRL-C in the
284 * shell where Vim was started would otherwise kill gvim!
285 */
286# if defined(HAVE_SETSID)
287 (void)setsid();
288# else
289 (void)setpgid(0, 0);
290# endif
291# endif
292 if (!pipe_error)
293 close(pipefd[0]);
294
295# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
296 /* Tell the session manager our new PID */
297 gui_mch_forked();
298# endif
299
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200300 /* Try to start the GUI */
301 gui_attempt_start();
302
303 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200304 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200305 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200306 if (gui.in_use)
307 write_eintr(pipefd[1], "ok", 3);
308 else
309 write_eintr(pipefd[1], "fail", 5);
310 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200311 }
312
313 /* If we failed to start the GUI, exit now. */
314 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100315 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200316}
317
318/*
319 * Read from a pipe assumed to be connected to the child process (this
320 * function is called from the parent).
321 * Return GUI_CHILD_OK if the child successfully started the GUI,
322 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
323 * some other error.
324 *
325 * The file descriptor will be closed before the function returns.
326 */
327 static int
328gui_read_child_pipe(int fd)
329{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200330 long bytes_read;
331#define READ_BUFFER_SIZE 10
332 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200333
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200334 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
335#undef READ_BUFFER_SIZE
336 close(fd);
337 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200338 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200339 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200340 if (strcmp(buffer, "ok") == 0)
341 return GUI_CHILD_OK;
342 return GUI_CHILD_FAILED;
343}
344
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200345#endif /* GUI_MAY_FORK */
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Call this when vim starts up, whether or not the GUI is started
349 */
350 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100351gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
353 gui.in_use = FALSE; /* No GUI yet (maybe later) */
354 gui.starting = FALSE; /* No GUI yet (maybe later) */
355 gui_mch_prepare(argc, argv);
356}
357
358/*
359 * Try initializing the GUI and check if it can be started.
360 * Used from main() to check early if "vim -g" can start the GUI.
361 * Used from gui_init() to prepare for starting the GUI.
362 * Returns FAIL or OK.
363 */
364 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100365gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366{
367 static int result = MAYBE;
368
369 if (result != MAYBE)
370 {
371 if (result == FAIL)
372 EMSG(_("E229: Cannot start the GUI"));
373 return result;
374 }
375
376 gui.shell_created = FALSE;
377 gui.dying = FALSE;
378 gui.in_focus = TRUE; /* so the guicursor setting works */
379 gui.dragged_sb = SBAR_NONE;
380 gui.dragged_wp = NULL;
381 gui.pointer_hidden = FALSE;
382 gui.col = 0;
383 gui.row = 0;
384 gui.num_cols = Columns;
385 gui.num_rows = Rows;
386
387 gui.cursor_is_valid = FALSE;
388 gui.scroll_region_top = 0;
389 gui.scroll_region_bot = Rows - 1;
390 gui.scroll_region_left = 0;
391 gui.scroll_region_right = Columns - 1;
392 gui.highlight_mask = HL_NORMAL;
393 gui.char_width = 1;
394 gui.char_height = 1;
395 gui.char_ascent = 0;
396 gui.border_width = 0;
397
398 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200399#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 gui.bold_font = NOFONT;
401 gui.ital_font = NOFONT;
402 gui.boldital_font = NOFONT;
403# ifdef FEAT_XFONTSET
404 gui.fontset = NOFONTSET;
405# endif
406#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200407#ifdef FEAT_MBYTE
408 gui.wide_font = NOFONT;
409# ifndef FEAT_GUI_GTK
410 gui.wide_bold_font = NOFONT;
411 gui.wide_ital_font = NOFONT;
412 gui.wide_boldital_font = NOFONT;
413# endif
414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200417# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418# ifdef FONTSET_ALWAYS
419 gui.menu_fontset = NOFONTSET;
420# else
421 gui.menu_font = NOFONT;
422# endif
423# endif
424 gui.menu_is_active = TRUE; /* default: include menu */
425# ifndef FEAT_GUI_GTK
426 gui.menu_height = MENU_DEFAULT_HEIGHT;
427 gui.menu_width = 0;
428# endif
429#endif
430#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
431 gui.toolbar_height = 0;
432#endif
433#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
434 gui.footer_height = 0;
435#endif
436#ifdef FEAT_BEVAL_TIP
437 gui.tooltip_fontset = NOFONTSET;
438#endif
439
440 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
441 gui.prev_wrap = -1;
442
443#ifdef ALWAYS_USE_GUI
444 result = OK;
445#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200446# ifdef FEAT_GUI_GTK
447 /*
448 * Note: Don't call gtk_init_check() before fork, it will be called after
449 * the fork. When calling it before fork, it make vim hang for a while.
450 * See gui_do_fork().
451 * Use a simpler check if the GUI window can probably be opened.
452 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200453 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200454# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457#endif
458 return result;
459}
460
461/*
462 * This is the call which starts the GUI.
463 */
464 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100465gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 win_T *wp;
468 static int recursive = 0;
469
470 /*
471 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
472 * function will then be executed at the first call, the rest by the
473 * recursive call. This allow the shell to be opened halfway reading a
474 * gvimrc file.
475 */
476 if (!recursive)
477 {
478 ++recursive;
479
480 clip_init(TRUE);
481
482 /* If can't initialize, don't try doing the rest */
483 if (gui_init_check() == FAIL)
484 {
485 --recursive;
486 clip_init(FALSE);
487 return;
488 }
489
490 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000491 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
492 * breaks the Paste toolbar button.
493 */
494 set_option_value((char_u *)"paste", 0L, NULL, 0);
495
496 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 * Set up system-wide default menus.
498 */
499#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
500 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
501 {
502 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000503 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 sys_menu = FALSE;
505 }
506#endif
507
508 /*
509 * Switch on the mouse by default, unless the user changed it already.
510 * This can then be changed in the .gvimrc.
511 */
512 if (!option_was_set((char_u *)"mouse"))
513 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000514 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516 /*
517 * If -U option given, use only the initializations from that file and
518 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
519 */
520 if (use_gvimrc != NULL)
521 {
522 if (STRCMP(use_gvimrc, "NONE") != 0
523 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000524 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
526 }
527 else
528 {
529 /*
530 * Get system wide defaults for gvim, only when file name defined.
531 */
532#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000533 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#endif
535
536 /*
537 * Try to read GUI initialization commands from the following
538 * places:
539 * - environment variable GVIMINIT
540 * - the user gvimrc file (~/.gvimrc)
541 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
542 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
543 * The first that exists is used, the rest is ignored.
544 */
545 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000546 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
547 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000549 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
550 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200552#ifdef USR_GVIMRC_FILE3
553 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
554 DOSO_GVIMRC) == FAIL
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 )
557 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200558#ifdef USR_GVIMRC_FILE4
559 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561 }
562
563 /*
564 * Read initialization commands from ".gvimrc" in current
565 * directory. This is only done if the 'exrc' option is set.
566 * Because of security reasons we disallow shell and write
567 * commands now, except for unix if the file is owned by the user
568 * or 'secure' option has been reset in environment of global
569 * ".gvimrc".
570 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
571 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
572 */
573 if (p_exrc)
574 {
575#ifdef UNIX
576 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200577 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
579 /* if ".gvimrc" file is not owned by user, set 'secure'
580 * mode */
581 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
582 secure = p_secure;
583 }
584#else
585 secure = p_secure;
586#endif
587
588 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
589 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
590#ifdef SYS_GVIMRC_FILE
591 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
592 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
593#endif
594#ifdef USR_GVIMRC_FILE2
595 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
596 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
597#endif
598#ifdef USR_GVIMRC_FILE3
599 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
600 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
601#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200602#ifdef USR_GVIMRC_FILE4
603 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
604 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000607 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609 if (secure == 2)
610 need_wait_return = TRUE;
611 secure = 0;
612 }
613 }
614
615 if (need_wait_return || msg_didany)
616 wait_return(TRUE);
617
618 --recursive;
619 }
620
621 /* If recursive call opened the shell, return here from the first call */
622 if (gui.in_use)
623 return;
624
625 /*
626 * Create the GUI shell.
627 */
628 gui.in_use = TRUE; /* Must be set after menus have been set up */
629 if (gui_mch_init() == FAIL)
630 goto error;
631
632 /* Avoid a delay for an error message that was printed in the terminal
633 * where Vim was started. */
634 emsg_on_display = FALSE;
635 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100636 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 need_wait_return = FALSE;
638 msg_didany = FALSE;
639
640 /*
641 * Check validity of any generic resources that may have been loaded.
642 */
643 if (gui.border_width < 0)
644 gui.border_width = 0;
645
646 /*
647 * Set up the fonts. First use a font specified with "-fn" or "-font".
648 */
649 if (font_argument != NULL)
650 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
651 if (
652#ifdef FEAT_XFONTSET
653 (*p_guifontset == NUL
654 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
655#endif
656 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
657 : p_guifont, FALSE) == FAIL)
658 {
659 EMSG(_("E665: Cannot start GUI, no valid font found"));
660 goto error2;
661 }
662#ifdef FEAT_MBYTE
663 if (gui_get_wide_font() == FAIL)
664 EMSG(_("E231: 'guifontwide' invalid"));
665#endif
666
667 gui.num_cols = Columns;
668 gui.num_rows = Rows;
669 gui_reset_scroll_region();
670
671 /* Create initial scrollbars */
672 FOR_ALL_WINDOWS(wp)
673 {
674 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
675 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
676 }
677 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
678
679#ifdef FEAT_MENU
680 gui_create_initial_menus(root_menu);
681#endif
682#ifdef FEAT_SUN_WORKSHOP
683 if (usingSunWorkShop)
684 workshop_init();
685#endif
686#ifdef FEAT_SIGN_ICONS
687 sign_gui_started();
688#endif
689
690 /* Configure the desired menu and scrollbars */
691 gui_init_which_components(NULL);
692
693 /* All components of the GUI have been created now */
694 gui.shell_created = TRUE;
695
696#ifndef FEAT_GUI_GTK
697 /* Set the shell size, adjusted for the screen size. For GTK this only
698 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100699 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#endif
701#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
702 /* Need to set the size of the menubar after all the menus have been
703 * created. */
704 gui_mch_compute_menu_height((Widget)0);
705#endif
706
707 /*
708 * Actually open the GUI shell.
709 */
710 if (gui_mch_open() != FAIL)
711 {
712#ifdef FEAT_TITLE
713 maketitle();
714 resettitle();
715#endif
716 init_gui_options();
717#ifdef FEAT_ARABIC
718 /* Our GUI can't do bidi. */
719 p_tbidi = FALSE;
720#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000721#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 /* Give GTK+ a chance to put all widget's into place. */
723 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000724
725# ifdef FEAT_MENU
726 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
727 * It was still there to make F10 work. */
728 if (vim_strchr(p_go, GO_MENUS) == NULL)
729 {
730 --gui.starting;
731 gui_mch_enable_menu(FALSE);
732 ++gui.starting;
733 gui_mch_update();
734 }
735# endif
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 /* Now make sure the shell fits on the screen. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100738 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000740 /* When 'lines' was set while starting up the topframe may have to be
741 * resized. */
742 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000743
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100744#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000745 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100746 * 'ballooneval' is off. */
747 if (balloonEval != NULL)
748 vim_free(balloonEval);
749 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000750# ifdef FEAT_GUI_GTK
751 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
752 &general_beval_cb, NULL);
753# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000754# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000755 {
756 extern Widget textArea;
757 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000758 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000759 }
760# else
761# ifdef FEAT_GUI_W32
762 balloonEval = gui_mch_create_beval_area(NULL, NULL,
763 &general_beval_cb, NULL);
764# endif
765# endif
766# endif
767 if (!p_beval)
768 gui_mch_disable_beval_area(balloonEval);
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
772 if (!im_xim_isvalid_imactivate())
773 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
774#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000775 /* When 'cmdheight' was set during startup it may not have taken
776 * effect yet. */
777 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000778 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 return;
781 }
782
783error2:
784#ifdef FEAT_GUI_X11
785 /* undo gui_mch_init() */
786 gui_mch_uninit();
787#endif
788
789error:
790 gui.in_use = FALSE;
791 clip_init(FALSE);
792}
793
794
795 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100796gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /* don't free the fonts, it leads to a BUS error
799 * richard@whitequeen.com Jul 99 */
800 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 gui.in_use = FALSE;
802 gui_mch_exit(rc);
803}
804
Bram Moolenaar9372a112005-12-06 19:59:18 +0000805#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000807# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Called when the GUI shell is closed by the user. If there are no changed
810 * files Vim exits, otherwise there will be a dialog to ask the user what to
811 * do.
812 * When this function returns, Vim should NOT exit!
813 */
814 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100815gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 cmdmod_T save_cmdmod;
818
819 save_cmdmod = cmdmod;
820
821 /* Only exit when there are no changed files */
822 exiting = TRUE;
823# ifdef FEAT_BROWSE
824 cmdmod.browse = TRUE;
825# endif
826# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
827 cmdmod.confirm = TRUE;
828# endif
829 /* If there are changed buffers, present the user with a dialog if
830 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100831 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 getout(0);
833
834 exiting = FALSE;
835 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000836 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838#endif
839
840/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200841 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 * first font name that works is used. If none is found, use the default
843 * font.
844 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
845 * Return OK when able to set the font. When it failed FAIL is returned and
846 * the fonts are unchanged.
847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100849gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851#define FONTLEN 320
852 char_u font_name[FONTLEN];
853 int font_list_empty = FALSE;
854 int ret = FAIL;
855
856 if (!gui.in_use)
857 return FAIL;
858
859 font_name[0] = NUL;
860 if (*font_list == NUL)
861 font_list_empty = TRUE;
862 else
863 {
864#ifdef FEAT_XFONTSET
865 /* When using a fontset, the whole list of fonts is one name. */
866 if (fontset)
867 ret = gui_mch_init_font(font_list, TRUE);
868 else
869#endif
870 while (*font_list != NUL)
871 {
872 /* Isolate one comma separated font name. */
873 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
874
875 /* Careful!!! The Win32 version of gui_mch_init_font(), when
876 * called with "*" will change p_guifont to the selected font
877 * name, which frees the old value. This makes font_list
878 * invalid. Thus when OK is returned here, font_list must no
879 * longer be used! */
880 if (gui_mch_init_font(font_name, FALSE) == OK)
881 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200882#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 /* If it's a Unicode font, try setting 'guifontwide' to a
884 * similar double-width font. */
885 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
886 && strstr((char *)font_name, "10646") != NULL)
887 set_guifontwide(font_name);
888#endif
889 ret = OK;
890 break;
891 }
892 }
893 }
894
895 if (ret != OK
896 && STRCMP(font_list, "*") != 0
897 && (font_list_empty || gui.norm_font == NOFONT))
898 {
899 /*
900 * Couldn't load any font in 'font_list', keep the current font if
901 * there is one. If 'font_list' is empty, or if there is no current
902 * font, tell gui_mch_init_font() to try to find a font we can load.
903 */
904 ret = gui_mch_init_font(NULL, FALSE);
905 }
906
907 if (ret == OK)
908 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200909#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 /* Set normal font as current font */
911# ifdef FEAT_XFONTSET
912 if (gui.fontset != NOFONTSET)
913 gui_mch_set_fontset(gui.fontset);
914 else
915# endif
916 gui_mch_set_font(gui.norm_font);
917#endif
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100918 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 return ret;
922}
923
924#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200925# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
927 * Try setting 'guifontwide' to a font twice as wide as "name".
928 */
929 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100930set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 int i = 0;
933 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
934 char_u *wp = NULL;
935 char_u *p;
936 GuiFont font;
937
938 wp = wide_name;
939 for (p = name; *p != NUL; ++p)
940 {
941 *wp++ = *p;
942 if (*p == '-')
943 {
944 ++i;
945 if (i == 6) /* font type: change "--" to "-*-" */
946 {
947 if (p[1] == '-')
948 *wp++ = '*';
949 }
950 else if (i == 12) /* found the width */
951 {
952 ++p;
953 i = getdigits(&p);
954 if (i != 0)
955 {
956 /* Double the width specification. */
957 sprintf((char *)wp, "%d%s", i * 2, p);
958 font = gui_mch_get_font(wide_name, FALSE);
959 if (font != NOFONT)
960 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000961 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 gui.wide_font = font;
963 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000964 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 }
967 break;
968 }
969 }
970 }
971}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200972# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974/*
975 * Get the font for 'guifontwide'.
976 * Return FAIL for an invalid font name.
977 */
978 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100979gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 GuiFont font = NOFONT;
982 char_u font_name[FONTLEN];
983 char_u *p;
984
985 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
986 return OK; /* Will give an error message later. */
987
988 if (p_guifontwide != NULL && *p_guifontwide != NUL)
989 {
990 for (p = p_guifontwide; *p != NUL; )
991 {
992 /* Isolate one comma separated font name. */
993 (void)copy_option_part(&p, font_name, FONTLEN, ",");
994 font = gui_mch_get_font(font_name, FALSE);
995 if (font != NOFONT)
996 break;
997 }
998 if (font == NOFONT)
999 return FAIL;
1000 }
1001
1002 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001003# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1005 if (font != NOFONT && gui.norm_font != NOFONT
1006 && pango_font_description_equal(font, gui.norm_font))
1007 {
1008 gui.wide_font = NOFONT;
1009 gui_mch_free_font(font);
1010 }
1011 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001014# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001015 gui_mch_wide_font_changed();
Bram Moolenaardb250522013-06-17 22:43:25 +02001016# else
1017 /*
1018 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1019 * support those fonts for 'guifontwide'.
1020 */
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 return OK;
1023}
1024#endif
1025
1026 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001027gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 gui.row = row;
1030 gui.col = col;
1031}
1032
1033/*
1034 * gui_check_pos - check if the cursor is on the screen.
1035 */
1036 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001037gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 if (gui.row >= screen_Rows)
1040 gui.row = screen_Rows - 1;
1041 if (gui.col >= screen_Columns)
1042 gui.col = screen_Columns - 1;
1043 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1044 gui.cursor_is_valid = FALSE;
1045}
1046
1047/*
1048 * Redraw the cursor if necessary or when forced.
1049 * Careful: The contents of ScreenLines[] must match what is on the screen,
1050 * otherwise this goes wrong. May need to call out_flush() first.
1051 */
1052 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001053gui_update_cursor(
1054 int force, /* when TRUE, update even when not moved */
1055 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
1057 int cur_width = 0;
1058 int cur_height = 0;
1059 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001060 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001062#ifdef FEAT_TERMINAL
1063 guicolor_T shape_fg = INVALCOLOR;
1064 guicolor_T shape_bg = INVALCOLOR;
1065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1067 int cattr; /* cursor attributes */
1068 int attr;
1069 attrentry_T *aep = NULL;
1070
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001071 /* Don't update the cursor when halfway busy scrolling or the screen size
1072 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1073 if (!can_update_cursor || screen_Columns != gui.num_cols
1074 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 return;
1076
1077 gui_check_pos();
1078 if (!gui.cursor_is_valid || force
1079 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1080 {
1081 gui_undraw_cursor();
1082 if (gui.row < 0)
1083 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001084#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1086 im_set_position(gui.row, gui.col);
1087#endif
1088 gui.cursor_row = gui.row;
1089 gui.cursor_col = gui.col;
1090
1091 /* Only write to the screen after ScreenLines[] has been initialized */
1092 if (!screen_cleared || ScreenLines == NULL)
1093 return;
1094
1095 /* Clear the selection if we are about to write over it */
1096 if (clear_selection)
1097 clip_may_clear_selection(gui.row, gui.row);
1098 /* Check that the cursor is inside the shell (resizing may have made
1099 * it invalid) */
1100 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1101 return;
1102
1103 gui.cursor_is_valid = TRUE;
1104
1105 /*
1106 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001107 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001109#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001110 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001111 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001113#endif
1114 shape = &shape_table[get_shape_idx(FALSE)];
1115 if (State & LANGMAP)
1116 id = shape->id_lm;
1117 else
1118 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120 /* get the colors and attributes for the cursor. Default is inverted */
1121 cfg = INVALCOLOR;
1122 cbg = INVALCOLOR;
1123 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001124 gui_mch_set_blinking(shape->blinkwait,
1125 shape->blinkon,
1126 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001127 if (shape->blinkwait == 0 || shape->blinkon == 0
1128 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001129 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001130#ifdef FEAT_TERMINAL
1131 if (shape_bg != INVALCOLOR)
1132 {
1133 cattr = 0;
1134 cfg = shape_fg;
1135 cbg = shape_bg;
1136 }
1137 else
1138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 if (id > 0)
1140 {
1141 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001142#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 static int iid;
1145 guicolor_T fg, bg;
1146
Bram Moolenaarc236c162008-07-13 17:41:49 +00001147 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001148# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001149 preedit_get_status()
1150# else
1151 im_get_status()
1152# endif
1153 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 iid = syn_name2id((char_u *)"CursorIM");
1156 if (iid > 0)
1157 {
1158 syn_id2colors(iid, &fg, &bg);
1159 if (bg != INVALCOLOR)
1160 cbg = bg;
1161 if (fg != INVALCOLOR)
1162 cfg = fg;
1163 }
1164 }
1165 }
1166#endif
1167 }
1168
1169 /*
1170 * Get the attributes for the character under the cursor.
1171 * When no cursor color was given, use the character color.
1172 */
1173 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1174 if (attr > HL_ALL)
1175 aep = syn_gui_attr2entry(attr);
1176 if (aep != NULL)
1177 {
1178 attr = aep->ae_attr;
1179 if (cfg == INVALCOLOR)
1180 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1181 : aep->ae_u.gui.fg_color);
1182 if (cbg == INVALCOLOR)
1183 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1184 : aep->ae_u.gui.bg_color);
1185 }
1186 if (cfg == INVALCOLOR)
1187 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1188 if (cbg == INVALCOLOR)
1189 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1190
1191#ifdef FEAT_XIM
1192 if (aep != NULL)
1193 {
1194 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1195 : aep->ae_u.gui.bg_color);
1196 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1197 : aep->ae_u.gui.fg_color);
1198 if (xim_bg_color == INVALCOLOR)
1199 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1200 : gui.back_pixel;
1201 if (xim_fg_color == INVALCOLOR)
1202 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1203 : gui.norm_pixel;
1204 }
1205 else
1206 {
1207 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1208 : gui.back_pixel;
1209 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1210 : gui.norm_pixel;
1211 }
1212#endif
1213
1214 attr &= ~HL_INVERSE;
1215 if (cattr & HL_INVERSE)
1216 {
1217 cc = cbg;
1218 cbg = cfg;
1219 cfg = cc;
1220 }
1221 cattr &= ~HL_INVERSE;
1222
1223 /*
1224 * When we don't have window focus, draw a hollow cursor.
1225 */
1226 if (!gui.in_focus)
1227 {
1228 gui_mch_draw_hollow_cursor(cbg);
1229 return;
1230 }
1231
1232 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001233 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#ifdef FEAT_HANGULIN
1235 || composing_hangul
1236#endif
1237 )
1238 {
1239 /*
1240 * Draw the text character with the cursor colors. Use the
1241 * character attributes plus the cursor attributes.
1242 */
1243 gui.highlight_mask = (cattr | attr);
1244#ifdef FEAT_HANGULIN
1245 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001246 {
1247 char_u *comp_buf;
1248 int comp_len;
1249
1250 comp_buf = hangul_composing_buffer_get(&comp_len);
1251 if (comp_buf)
1252 {
1253 (void)gui_outstr_nowrap(comp_buf, comp_len,
1254 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1255 cfg, cbg, 0);
1256 vim_free(comp_buf);
1257 }
1258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else
1260#endif
1261 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1262 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1263 }
1264 else
1265 {
1266#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1267 int col_off = FALSE;
1268#endif
1269 /*
1270 * First draw the partial cursor, then overwrite with the text
1271 * character, using a transparent background.
1272 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001273 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 {
1275 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001276 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 else
1279 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001280 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 cur_width = gui.char_width;
1282 }
1283#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001284 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1285 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001288 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 cur_width += gui.char_width;
1290# ifdef FEAT_RIGHTLEFT
1291 if (CURSOR_BAR_RIGHT)
1292 {
1293 /* gui.col points to the left halve of the character but
1294 * the vertical line needs to be on the right halve.
1295 * A double-wide horizontal line is also drawn from the
1296 * right halve in gui_mch_draw_part_cursor(). */
1297 col_off = TRUE;
1298 ++gui.col;
1299 }
1300# endif
1301 }
1302#endif
1303 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1304#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1305 if (col_off)
1306 --gui.col;
1307#endif
1308
1309#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1310 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1311 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1312 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1313 (guicolor_T)0, (guicolor_T)0, 0);
1314#endif
1315 }
1316 gui.highlight_mask = old_hl_mask;
1317 }
1318}
1319
1320#if defined(FEAT_MENU) || defined(PROTO)
1321 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001322gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001324# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (gui.menu_is_active && gui.in_use)
1326 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1327# endif
1328}
1329#endif
1330
1331/*
1332 * Position the various GUI components (text area, menu). The vertical
1333 * scrollbars are NOT handled here. See gui_update_scrollbars().
1334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001336gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 int text_area_x;
1339 int text_area_y;
1340 int text_area_width;
1341 int text_area_height;
1342
1343 /* avoid that moving components around generates events */
1344 ++hold_gui_events;
1345
1346 text_area_x = 0;
1347 if (gui.which_scrollbars[SBAR_LEFT])
1348 text_area_x += gui.scrollbar_width;
1349
1350 text_area_y = 0;
1351#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1352 gui.menu_width = total_width;
1353 if (gui.menu_is_active)
1354 text_area_y += gui.menu_height;
1355#endif
1356#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1357 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1358 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1359#endif
1360
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001361# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001362 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001363 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001364 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1368 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1369 {
1370# ifdef FEAT_GUI_ATHENA
1371 gui_mch_set_toolbar_pos(0, text_area_y,
1372 gui.menu_width, gui.toolbar_height);
1373# endif
1374 text_area_y += gui.toolbar_height;
1375 }
1376#endif
1377
1378 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1379 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1380
1381 gui_mch_set_text_area_pos(text_area_x,
1382 text_area_y,
1383 text_area_width,
1384 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001385#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 + xim_get_status_area_height()
1387#endif
1388 );
1389#ifdef FEAT_MENU
1390 gui_position_menu();
1391#endif
1392 if (gui.which_scrollbars[SBAR_BOTTOM])
1393 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1394 text_area_x,
1395 text_area_y + text_area_height,
1396 text_area_width,
1397 gui.scrollbar_height);
1398 gui.left_sbar_x = 0;
1399 gui.right_sbar_x = text_area_x + text_area_width;
1400
1401 --hold_gui_events;
1402}
1403
Bram Moolenaar02743632005-07-25 20:42:36 +00001404/*
1405 * Get the width of the widgets and decorations to the side of the text area.
1406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001408gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 int base_width;
1411
1412 base_width = 2 * gui.border_offset;
1413 if (gui.which_scrollbars[SBAR_LEFT])
1414 base_width += gui.scrollbar_width;
1415 if (gui.which_scrollbars[SBAR_RIGHT])
1416 base_width += gui.scrollbar_width;
1417 return base_width;
1418}
1419
Bram Moolenaar02743632005-07-25 20:42:36 +00001420/*
1421 * Get the height of the widgets and decorations above and below the text area.
1422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001424gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 int base_height;
1427
1428 base_height = 2 * gui.border_offset;
1429 if (gui.which_scrollbars[SBAR_BOTTOM])
1430 base_height += gui.scrollbar_height;
1431#ifdef FEAT_GUI_GTK
1432 /* We can't take the sizes properly into account until anything is
1433 * realized. Therefore we recalculate all the values here just before
1434 * setting the size. (--mdcki) */
1435#else
1436# ifdef FEAT_MENU
1437 if (gui.menu_is_active)
1438 base_height += gui.menu_height;
1439# endif
1440# ifdef FEAT_TOOLBAR
1441 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1442# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1443 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1444# else
1445 base_height += gui.toolbar_height;
1446# endif
1447# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001448# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1449 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001450 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001451 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453# ifdef FEAT_FOOTER
1454 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1455 base_height += gui.footer_height;
1456# endif
1457# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1458 base_height += gui_mch_text_area_extra_height();
1459# endif
1460#endif
1461 return base_height;
1462}
1463
1464/*
1465 * Should be called after the GUI shell has been resized. Its arguments are
1466 * the new width and height of the shell in pixels.
1467 */
1468 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001469gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 static int busy = FALSE;
1472
1473 if (!gui.shell_created) /* ignore when still initializing */
1474 return;
1475
1476 /*
1477 * Can't resize the screen while it is being redrawn. Remember the new
1478 * size and handle it later.
1479 */
1480 if (updating_screen || busy)
1481 {
1482 new_pixel_width = pixel_width;
1483 new_pixel_height = pixel_height;
1484 return;
1485 }
1486
1487again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001488 new_pixel_width = 0;
1489 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 busy = TRUE;
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 /* Flush pending output before redrawing */
1493 out_flush();
1494
1495 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001496 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
1498 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 /*
1502 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1503 * at the last line here (why does it have to be one row too low?).
1504 */
1505 if (State == ASKMORE || State == CONFIRM)
1506 gui.row = gui.num_rows;
1507
1508 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1509 * the safe side. */
1510 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1511 || gui.num_rows != Rows || gui.num_cols != Columns)
1512 shell_resized();
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 gui_update_scrollbars(TRUE);
1515 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001516#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 xim_set_status_area();
1518#endif
1519
1520 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001521
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001522 /* We may have been called again while redrawing the screen.
1523 * Need to do it all again with the latest size then. But only if the size
1524 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (new_pixel_height)
1526 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001527 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1528 {
1529 new_pixel_width = 0;
1530 new_pixel_height = 0;
1531 }
1532 else
1533 {
1534 pixel_width = new_pixel_width;
1535 pixel_height = new_pixel_height;
1536 goto again;
1537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539}
1540
1541/*
1542 * Check if gui_resize_shell() must be called.
1543 */
1544 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001545gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 /* careful: gui_resize_shell() may postpone the resize again if we
1549 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001550 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
1553 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001554gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 Rows = gui.num_rows;
1557 Columns = gui.num_cols;
1558 return OK;
1559}
1560
1561/*
1562 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001563 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1564 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001565 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1566 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001569gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001570 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001571 int fit_to_display,
1572 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 int base_width;
1575 int base_height;
1576 int width;
1577 int height;
1578 int min_width;
1579 int min_height;
1580 int screen_w;
1581 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001582#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001583 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001584 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001585#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001586 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 if (!gui.shell_created)
1589 return;
1590
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001591#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* If not setting to a user specified size and maximized, calculate the
1593 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001594 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1595 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 gui_mch_newfont();
1598 return;
1599 }
1600#endif
1601
1602 base_width = gui_get_base_width();
1603 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001604 if (fit_to_display)
1605 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001606 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608#ifdef USE_SUN_WORKSHOP
1609 if (!mustset && usingSunWorkShop
1610 && workshop_get_width_height(&width, &height))
1611 {
1612 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1613 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1614 }
1615 else
1616#endif
1617 {
1618 width = Columns * gui.char_width + base_width;
1619 height = Rows * gui.char_height + base_height;
1620 }
1621
1622 if (fit_to_display)
1623 {
1624 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001625 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 Columns = (screen_w - base_width) / gui.char_width;
1628 if (Columns < MIN_COLUMNS)
1629 Columns = MIN_COLUMNS;
1630 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001631#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001632 ++did_adjust;
1633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001635 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 {
1637 Rows = (screen_h - base_height) / gui.char_height;
1638 check_shellsize();
1639 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001640#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001641 ++did_adjust;
1642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001644#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001645 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1646 && height + gui.char_height >= screen_h))
1647 /* don't unmaximize if at maximum size */
1648 un_maximize = FALSE;
1649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001651 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 gui.num_cols = Columns;
1653 gui.num_rows = Rows;
1654
1655 min_width = base_width + MIN_COLUMNS * gui.char_width;
1656 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001657 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001658
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001659#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001660 if (un_maximize)
1661 {
1662 /* If the window size is smaller than the screen unmaximize the
1663 * window, otherwise resizing won't work. */
1664 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1665 if ((width + gui.char_width < screen_w
1666 || height + gui.char_height * 2 < screen_h)
1667 && gui_mch_maximized())
1668 gui_mch_unmaximize();
1669 }
1670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001673 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001675 if (fit_to_display && x >= 0 && y >= 0)
1676 {
1677 /* Some window managers put the Vim window left of/above the screen.
1678 * Only change the position if it wasn't already negative before
1679 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 gui_mch_update();
1681 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1682 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1683 }
1684
1685 gui_position_components(width);
1686 gui_update_scrollbars(TRUE);
1687 gui_reset_scroll_region();
1688}
1689
1690/*
1691 * Called when Rows and/or Columns has changed.
1692 */
1693 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001694gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 gui_reset_scroll_region();
1697}
1698
1699/*
1700 * Make scroll region cover whole screen.
1701 */
1702 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001703gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 gui.scroll_region_top = 0;
1706 gui.scroll_region_bot = gui.num_rows - 1;
1707 gui.scroll_region_left = 0;
1708 gui.scroll_region_right = gui.num_cols - 1;
1709}
1710
1711 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001712gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 if (mask > HL_ALL) /* highlight code */
1715 gui.highlight_mask = mask;
1716 else /* mask */
1717 gui.highlight_mask |= mask;
1718}
1719
1720 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001721gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 if (mask > HL_ALL) /* highlight code */
1724 gui.highlight_mask = HL_NORMAL;
1725 else /* mask */
1726 gui.highlight_mask &= ~mask;
1727}
1728
1729/*
1730 * Clear a rectangular region of the screen from text pos (row1, col1) to
1731 * (row2, col2) inclusive.
1732 */
1733 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001734gui_clear_block(
1735 int row1,
1736 int col1,
1737 int row2,
1738 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 /* Clear the selection if we are about to write over it */
1741 clip_may_clear_selection(row1, row2);
1742
1743 gui_mch_clear_block(row1, col1, row2, col2);
1744
1745 /* Invalidate cursor if it was in this block */
1746 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1747 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1748 gui.cursor_is_valid = FALSE;
1749}
1750
1751/*
1752 * Write code to update the cursor later. This avoids the need to flush the
1753 * output buffer before calling gui_update_cursor().
1754 */
1755 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001756gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001758 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759}
1760
1761 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001762gui_write(
1763 char_u *s,
1764 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 char_u *p;
1767 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 int force_scrollbar = FALSE;
1770 static win_T *old_curwin = NULL;
1771
1772/* #define DEBUG_GUI_WRITE */
1773#ifdef DEBUG_GUI_WRITE
1774 {
1775 int i;
1776 char_u *str;
1777
1778 printf("gui_write(%d):\n ", len);
1779 for (i = 0; i < len; i++)
1780 if (s[i] == ESC)
1781 {
1782 if (i != 0)
1783 printf("\n ");
1784 printf("<ESC>");
1785 }
1786 else
1787 {
1788 str = transchar_byte(s[i]);
1789 if (str[0] && str[1])
1790 printf("<%s>", (char *)str);
1791 else
1792 printf("%s", (char *)str);
1793 }
1794 printf("\n");
1795 }
1796#endif
1797 while (len)
1798 {
1799 if (s[0] == ESC && s[1] == '|')
1800 {
1801 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001802 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
1804 arg1 = getdigits(&p);
1805 if (p > s + len)
1806 break;
1807 if (*p == ';')
1808 {
1809 ++p;
1810 arg2 = getdigits(&p);
1811 if (p > s + len)
1812 break;
1813 }
1814 }
1815 switch (*p)
1816 {
1817 case 'C': /* Clear screen */
1818 clip_scroll_selection(9999);
1819 gui_mch_clear_all();
1820 gui.cursor_is_valid = FALSE;
1821 force_scrollbar = TRUE;
1822 break;
1823 case 'M': /* Move cursor */
1824 gui_set_cursor(arg1, arg2);
1825 break;
1826 case 's': /* force cursor (shape) update */
1827 force_cursor = TRUE;
1828 break;
1829 case 'R': /* Set scroll region */
1830 if (arg1 < arg2)
1831 {
1832 gui.scroll_region_top = arg1;
1833 gui.scroll_region_bot = arg2;
1834 }
1835 else
1836 {
1837 gui.scroll_region_top = arg2;
1838 gui.scroll_region_bot = arg1;
1839 }
1840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 case 'V': /* Set vertical scroll region */
1842 if (arg1 < arg2)
1843 {
1844 gui.scroll_region_left = arg1;
1845 gui.scroll_region_right = arg2;
1846 }
1847 else
1848 {
1849 gui.scroll_region_left = arg2;
1850 gui.scroll_region_right = arg1;
1851 }
1852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 case 'd': /* Delete line */
1854 gui_delete_lines(gui.row, 1);
1855 break;
1856 case 'D': /* Delete lines */
1857 gui_delete_lines(gui.row, arg1);
1858 break;
1859 case 'i': /* Insert line */
1860 gui_insert_lines(gui.row, 1);
1861 break;
1862 case 'I': /* Insert lines */
1863 gui_insert_lines(gui.row, arg1);
1864 break;
1865 case '$': /* Clear to end-of-line */
1866 gui_clear_block(gui.row, gui.col, gui.row,
1867 (int)Columns - 1);
1868 break;
1869 case 'h': /* Turn on highlighting */
1870 gui_start_highlight(arg1);
1871 break;
1872 case 'H': /* Turn off highlighting */
1873 gui_stop_highlight(arg1);
1874 break;
1875 case 'f': /* flash the window (visual bell) */
1876 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1877 break;
1878 default:
1879 p = s + 1; /* Skip the ESC */
1880 break;
1881 }
1882 len -= (int)(++p - s);
1883 s = p;
1884 }
1885 else if (
1886#ifdef EBCDIC
1887 CtrlChar(s[0]) != 0 /* Ctrl character */
1888#else
1889 s[0] < 0x20 /* Ctrl character */
1890#endif
1891#ifdef FEAT_SIGN_ICONS
1892 && s[0] != SIGN_BYTE
1893# ifdef FEAT_NETBEANS_INTG
1894 && s[0] != MULTISIGN_BYTE
1895# endif
1896#endif
1897 )
1898 {
1899 if (s[0] == '\n') /* NL */
1900 {
1901 gui.col = 0;
1902 if (gui.row < gui.scroll_region_bot)
1903 gui.row++;
1904 else
1905 gui_delete_lines(gui.scroll_region_top, 1);
1906 }
1907 else if (s[0] == '\r') /* CR */
1908 {
1909 gui.col = 0;
1910 }
1911 else if (s[0] == '\b') /* Backspace */
1912 {
1913 if (gui.col)
1914 --gui.col;
1915 }
1916 else if (s[0] == Ctrl_L) /* cursor-right */
1917 {
1918 ++gui.col;
1919 }
1920 else if (s[0] == Ctrl_G) /* Beep */
1921 {
1922 gui_mch_beep();
1923 }
1924 /* Other Ctrl character: shouldn't happen! */
1925
1926 --len; /* Skip this char */
1927 ++s;
1928 }
1929 else
1930 {
1931 p = s;
1932 while (len > 0 && (
1933#ifdef EBCDIC
1934 CtrlChar(*p) == 0
1935#else
1936 *p >= 0x20
1937#endif
1938#ifdef FEAT_SIGN_ICONS
1939 || *p == SIGN_BYTE
1940# ifdef FEAT_NETBEANS_INTG
1941 || *p == MULTISIGN_BYTE
1942# endif
1943#endif
1944 ))
1945 {
1946 len--;
1947 p++;
1948 }
1949 gui_outstr(s, (int)(p - s));
1950 s = p;
1951 }
1952 }
1953
1954 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1955 * set). */
1956 if (force_cursor)
1957 gui_update_cursor(TRUE, TRUE);
1958
1959 /* When switching to another window the dragging must have stopped.
1960 * Required for GTK, dragged_sb isn't reset. */
1961 if (old_curwin != curwin)
1962 gui.dragged_sb = SBAR_NONE;
1963
1964 /* Update the scrollbars after clearing the screen or when switched
1965 * to another window.
1966 * Update the horizontal scrollbar always, it's difficult to check all
1967 * situations where it might change. */
1968 if (force_scrollbar || old_curwin != curwin)
1969 gui_update_scrollbars(force_scrollbar);
1970 else
1971 gui_update_horiz_scrollbar(FALSE);
1972 old_curwin = curwin;
1973
1974 /*
1975 * We need to make sure this is cleared since Athena doesn't tell us when
1976 * he is done dragging. Do the same for GTK.
1977 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001978#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 gui.dragged_sb = SBAR_NONE;
1980#endif
1981
Bram Moolenaara338adc2018-01-31 20:51:47 +01001982 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983}
1984
1985/*
1986 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1987 * produces wrong results. Call gui_dont_update_cursor() before that code and
1988 * gui_can_update_cursor() afterwards.
1989 */
1990 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001991gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 if (gui.in_use)
1994 {
1995 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001996 if (undraw)
1997 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 can_update_cursor = FALSE;
1999 }
2000}
2001
2002 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002003gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 can_update_cursor = TRUE;
2006 /* No need to update the cursor right now, there is always more output
2007 * after scrolling. */
2008}
2009
Bram Moolenaara338adc2018-01-31 20:51:47 +01002010/*
2011 * Disable issuing gui_mch_flush().
2012 */
2013 void
2014gui_disable_flush(void)
2015{
2016 ++disable_flush;
2017}
2018
2019/*
2020 * Enable issuing gui_mch_flush().
2021 */
2022 void
2023gui_enable_flush(void)
2024{
2025 --disable_flush;
2026}
2027
2028/*
2029 * Issue gui_mch_flush() if it is not disabled.
2030 */
2031 void
2032gui_may_flush(void)
2033{
2034 if (disable_flush == 0)
2035 gui_mch_flush();
2036}
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002039gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
2041 int this_len;
2042#ifdef FEAT_MBYTE
2043 int cells;
2044#endif
2045
2046 if (len == 0)
2047 return;
2048
2049 if (len < 0)
2050 len = (int)STRLEN(s);
2051
2052 while (len > 0)
2053 {
2054#ifdef FEAT_MBYTE
2055 if (has_mbyte)
2056 {
2057 /* Find out how many chars fit in the current line. */
2058 cells = 0;
2059 for (this_len = 0; this_len < len; )
2060 {
2061 cells += (*mb_ptr2cells)(s + this_len);
2062 if (gui.col + cells > Columns)
2063 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002064 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066 if (this_len > len)
2067 this_len = len; /* don't include following composing char */
2068 }
2069 else
2070#endif
2071 if (gui.col + len > Columns)
2072 this_len = Columns - gui.col;
2073 else
2074 this_len = len;
2075
2076 (void)gui_outstr_nowrap(s, this_len,
2077 0, (guicolor_T)0, (guicolor_T)0, 0);
2078 s += this_len;
2079 len -= this_len;
2080#ifdef FEAT_MBYTE
2081 /* fill up for a double-width char that doesn't fit. */
2082 if (len > 0 && gui.col < Columns)
2083 (void)gui_outstr_nowrap((char_u *)" ", 1,
2084 0, (guicolor_T)0, (guicolor_T)0, 0);
2085#endif
2086 /* The cursor may wrap to the next line. */
2087 if (gui.col >= Columns)
2088 {
2089 gui.col = 0;
2090 gui.row++;
2091 }
2092 }
2093}
2094
2095/*
2096 * Output one character (may be one or two display cells).
2097 * Caller must check for valid "off".
2098 * Returns FAIL or OK, just like gui_outstr_nowrap().
2099 */
2100 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002101gui_screenchar(
2102 int off, /* Offset from start of screen */
2103 int flags,
2104 guicolor_T fg, /* colors for cursor */
2105 guicolor_T bg, /* colors for cursor */
2106 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
2108#ifdef FEAT_MBYTE
2109 char_u buf[MB_MAXBYTES + 1];
2110
2111 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2112 if (enc_utf8 && ScreenLines[off] == 0)
2113 return OK;
2114
2115 if (enc_utf8 && ScreenLinesUC[off] != 0)
2116 /* Draw UTF-8 multi-byte character. */
2117 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2118 flags, fg, bg, back);
2119
2120 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2121 {
2122 buf[0] = ScreenLines[off];
2123 buf[1] = ScreenLines2[off];
2124 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2125 }
2126
2127 /* Draw non-multi-byte character or DBCS character. */
2128 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002129 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 flags, fg, bg, back);
2131#else
2132 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2133#endif
2134}
2135
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002136#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137/*
2138 * Output the string at the given screen position. This is used in place
2139 * of gui_screenchar() where possible because Pango needs as much context
2140 * as possible to work nicely. It's a lot faster as well.
2141 */
2142 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002143gui_screenstr(
2144 int off, /* Offset from start of screen */
2145 int len, /* string length in screen cells */
2146 int flags,
2147 guicolor_T fg, /* colors for cursor */
2148 guicolor_T bg, /* colors for cursor */
2149 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
2151 char_u *buf;
2152 int outlen = 0;
2153 int i;
2154 int retval;
2155
2156 if (len <= 0) /* "cannot happen"? */
2157 return OK;
2158
2159 if (enc_utf8)
2160 {
2161 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2162 if (buf == NULL)
2163 return OK; /* not much we could do here... */
2164
2165 for (i = off; i < off + len; ++i)
2166 {
2167 if (ScreenLines[i] == 0)
2168 continue; /* skip second half of double-width char */
2169
2170 if (ScreenLinesUC[i] == 0)
2171 buf[outlen++] = ScreenLines[i];
2172 else
2173 outlen += utfc_char2bytes(i, buf + outlen);
2174 }
2175
2176 buf[outlen] = NUL; /* only to aid debugging */
2177 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2178 vim_free(buf);
2179
2180 return retval;
2181 }
2182 else if (enc_dbcs == DBCS_JPNU)
2183 {
2184 buf = alloc((unsigned)(len * 2 + 1));
2185 if (buf == NULL)
2186 return OK; /* not much we could do here... */
2187
2188 for (i = off; i < off + len; ++i)
2189 {
2190 buf[outlen++] = ScreenLines[i];
2191
2192 /* handle double-byte single-width char */
2193 if (ScreenLines[i] == 0x8e)
2194 buf[outlen++] = ScreenLines2[i];
2195 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2196 buf[outlen++] = ScreenLines[++i];
2197 }
2198
2199 buf[outlen] = NUL; /* only to aid debugging */
2200 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2201 vim_free(buf);
2202
2203 return retval;
2204 }
2205 else
2206 {
2207 return gui_outstr_nowrap(&ScreenLines[off], len,
2208 flags, fg, bg, back);
2209 }
2210}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002211#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213/*
2214 * Output the given string at the current cursor position. If the string is
2215 * too long to fit on the line, then it is truncated.
2216 * "flags":
2217 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2218 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002219 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * background.
2221 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2222 * it.
2223 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2224 * FAIL (the caller should start drawing "back" chars back).
2225 */
2226 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002227gui_outstr_nowrap(
2228 char_u *s,
2229 int len,
2230 int flags,
2231 guicolor_T fg, /* colors for cursor */
2232 guicolor_T bg, /* colors for cursor */
2233 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 long_u highlight_mask;
2236 long_u hl_mask_todo;
2237 guicolor_T fg_color;
2238 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002239 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002240#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002242# ifdef FEAT_MBYTE
2243 GuiFont wide_font = NOFONT;
2244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245# ifdef FEAT_XFONTSET
2246 GuiFontset fontset = NOFONTSET;
2247# endif
2248#endif
2249 attrentry_T *aep = NULL;
2250 int draw_flags;
2251 int col = gui.col;
2252#ifdef FEAT_SIGN_ICONS
2253 int draw_sign = FALSE;
2254# ifdef FEAT_NETBEANS_INTG
2255 int multi_sign = FALSE;
2256# endif
2257#endif
2258
2259 if (len < 0)
2260 len = (int)STRLEN(s);
2261 if (len == 0)
2262 return OK;
2263
2264#ifdef FEAT_SIGN_ICONS
2265 if (*s == SIGN_BYTE
2266# ifdef FEAT_NETBEANS_INTG
2267 || *s == MULTISIGN_BYTE
2268# endif
2269 )
2270 {
2271# ifdef FEAT_NETBEANS_INTG
2272 if (*s == MULTISIGN_BYTE)
2273 multi_sign = TRUE;
2274# endif
2275 /* draw spaces instead */
2276 s = (char_u *)" ";
2277 if (len == 1 && col > 0)
2278 --col;
2279 len = 2;
2280 draw_sign = TRUE;
2281 highlight_mask = 0;
2282 }
2283 else
2284#endif
2285 if (gui.highlight_mask > HL_ALL)
2286 {
2287 aep = syn_gui_attr2entry(gui.highlight_mask);
2288 if (aep == NULL) /* highlighting not set */
2289 highlight_mask = 0;
2290 else
2291 highlight_mask = aep->ae_attr;
2292 }
2293 else
2294 highlight_mask = gui.highlight_mask;
2295 hl_mask_todo = highlight_mask;
2296
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002297#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 /* Set the font */
2299 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2300 font = aep->ae_u.gui.font;
2301# ifdef FEAT_XFONTSET
2302 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2303 fontset = aep->ae_u.gui.fontset;
2304# endif
2305 else
2306 {
2307# ifdef FEAT_XFONTSET
2308 if (gui.fontset != NOFONTSET)
2309 fontset = gui.fontset;
2310 else
2311# endif
2312 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2313 {
2314 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2315 {
2316 font = gui.boldital_font;
2317 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2318 }
2319 else if (gui.bold_font != NOFONT)
2320 {
2321 font = gui.bold_font;
2322 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2323 }
2324 else
2325 font = gui.norm_font;
2326 }
2327 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2328 {
2329 font = gui.ital_font;
2330 hl_mask_todo &= ~HL_ITALIC;
2331 }
2332 else
2333 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002334
2335# ifdef FEAT_MBYTE
2336 /*
2337 * Choose correct wide_font by font. wide_font should be set with font
2338 * at same time in above block. But it will make many "ifdef" nasty
2339 * blocks. So we do it here.
2340 */
2341 if (font == gui.boldital_font && gui.wide_boldital_font)
2342 wide_font = gui.wide_boldital_font;
2343 else if (font == gui.bold_font && gui.wide_bold_font)
2344 wide_font = gui.wide_bold_font;
2345 else if (font == gui.ital_font && gui.wide_ital_font)
2346 wide_font = gui.wide_ital_font;
2347 else if (font == gui.norm_font && gui.wide_font)
2348 wide_font = gui.wide_font;
2349# endif
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352# ifdef FEAT_XFONTSET
2353 if (fontset != NOFONTSET)
2354 gui_mch_set_fontset(fontset);
2355 else
2356# endif
2357 gui_mch_set_font(font);
2358#endif
2359
2360 draw_flags = 0;
2361
2362 /* Set the color */
2363 bg_color = gui.back_pixel;
2364 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2365 {
2366 draw_flags |= DRAW_CURSOR;
2367 fg_color = fg;
2368 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002369 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371 else if (aep != NULL)
2372 {
2373 fg_color = aep->ae_u.gui.fg_color;
2374 if (fg_color == INVALCOLOR)
2375 fg_color = gui.norm_pixel;
2376 bg_color = aep->ae_u.gui.bg_color;
2377 if (bg_color == INVALCOLOR)
2378 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002379 sp_color = aep->ae_u.gui.sp_color;
2380 if (sp_color == INVALCOLOR)
2381 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
2383 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002386 sp_color = fg_color;
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2390 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002391#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 gui_mch_set_colors(bg_color, fg_color);
2393#else
2394 gui_mch_set_fg_color(bg_color);
2395 gui_mch_set_bg_color(fg_color);
2396#endif
2397 }
2398 else
2399 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002400#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 gui_mch_set_colors(fg_color, bg_color);
2402#else
2403 gui_mch_set_fg_color(fg_color);
2404 gui_mch_set_bg_color(bg_color);
2405#endif
2406 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002407 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /* Clear the selection if we are about to write over it */
2410 if (!(flags & GUI_MON_NOCLEAR))
2411 clip_may_clear_selection(gui.row, gui.row);
2412
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 /* If there's no bold font, then fake it */
2415 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2416 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
2418 /*
2419 * When drawing bold or italic characters the spill-over from the left
2420 * neighbor may be destroyed. Let the caller backup to start redrawing
2421 * just after a blank.
2422 */
2423 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2424 return FAIL;
2425
Bram Moolenaare60acc12011-05-10 16:41:25 +02002426#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /* If there's no italic font, then fake it.
2428 * For GTK2, we don't need a different font for italic style. */
2429 if (hl_mask_todo & HL_ITALIC)
2430 draw_flags |= DRAW_ITALIC;
2431
2432 /* Do we underline the text? */
2433 if (hl_mask_todo & HL_UNDERLINE)
2434 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#else
2437 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 draw_flags |= DRAW_UNDERL;
2440#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002441 /* Do we undercurl the text? */
2442 if (hl_mask_todo & HL_UNDERCURL)
2443 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002445 /* Do we strikethrough the text? */
2446 if (hl_mask_todo & HL_STRIKETHROUGH)
2447 draw_flags |= DRAW_STRIKE;
2448
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002449 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 if (flags & GUI_MON_TRS_CURSOR)
2451 draw_flags |= DRAW_TRANSP;
2452
2453 /*
2454 * Draw the text.
2455 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002456#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /* The value returned is the length in display cells */
2458 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2459#else
2460# ifdef FEAT_MBYTE
2461 if (enc_utf8)
2462 {
2463 int start; /* index of bytes to be drawn */
2464 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002465 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 int cn; /* cellwidth of current char */
2467 int i; /* index of current char */
2468 int c; /* current char value */
2469 int cl; /* byte length of current char */
2470 int comping; /* current char is composing */
2471 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002472 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002473 int prev_wide = FALSE;
2474 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002475# ifdef WIN3264
2476 int sep_comp = FALSE; /* Don't separate composing chars. */
2477# else
2478 int sep_comp = TRUE; /* Separate composing chars. */
2479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /* Break the string at a composing character, it has to be drawn on
2482 * top of the previous character. */
2483 start = 0;
2484 cells = 0;
2485 for (i = 0; i < len; i += cl)
2486 {
2487 c = utf_ptr2char(s + i);
2488 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 comping = utf_iscomposing(c);
2490 if (!comping) /* count cells from non-composing chars */
2491 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002492 if (!comping || sep_comp)
2493 {
2494 if (cn > 1
2495# ifdef FEAT_XFONTSET
2496 && fontset == NOFONTSET
2497# endif
2498 && wide_font != NOFONT)
2499 curr_wide = TRUE;
2500 else
2501 curr_wide = FALSE;
2502 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002503 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (cl == 0) /* hit end of string */
2505 len = i + cl; /* len must be wrong "cannot happen" */
2506
Bram Moolenaar45933962013-01-23 17:43:57 +01002507 wide_changed = curr_wide != prev_wide;
2508
2509 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002511 if (i + cl >= len || (comping && sep_comp && i > start)
2512 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002513# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 || (cn > 1
2515# ifdef FEAT_XFONTSET
2516 /* No fontset: At least draw char after wide char at
2517 * right position. */
2518 && fontset == NOFONTSET
2519# endif
2520 )
2521# endif
2522 )
2523 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002524 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 thislen = i - start;
2526 else
2527 thislen = i - start + cl;
2528 if (thislen > 0)
2529 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002530 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002531 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2533 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002534 if (prev_wide)
2535 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 start += thislen;
2537 }
2538 scol += cells;
2539 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002540 /* Adjust to not draw a character which width is changed
2541 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002542 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002544 scol -= cn;
2545 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002548# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002549 /* No fontset: draw a space to fill the gap after a wide char
2550 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2552# ifdef FEAT_XFONTSET
2553 && fontset == NOFONTSET
2554# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002555 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2557 1, draw_flags);
2558# endif
2559 }
2560 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002561 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002563# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002564 /* Carbon ATSUI autodraws composing char over previous char */
2565 gui_mch_draw_string(gui.row, scol, s + i, cl,
2566 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002567# else
2568 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2569 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 start = i + cl;
2572 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002573 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 /* The stuff below assumes "len" is the length in screen columns. */
2576 len = scol - col;
2577 }
2578 else
2579# endif
2580 {
2581 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2582# ifdef FEAT_MBYTE
2583 if (enc_dbcs == DBCS_JPNU)
2584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /* Get the length in display cells, this can be different from the
2586 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002587 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589# endif
2590 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002591#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2594 gui.col = col + len;
2595
2596 /* May need to invert it when it's part of the selection. */
2597 if (flags & GUI_MON_NOCLEAR)
2598 clip_may_redraw_selection(gui.row, col, len);
2599
2600 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2601 {
2602 /* Invalidate the old physical cursor position if we wrote over it */
2603 if (gui.cursor_row == gui.row
2604 && gui.cursor_col >= col
2605 && gui.cursor_col < col + len)
2606 gui.cursor_is_valid = FALSE;
2607 }
2608
2609#ifdef FEAT_SIGN_ICONS
2610 if (draw_sign)
2611 /* Draw the sign on top of the spaces. */
2612 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002613# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002614 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (multi_sign)
2616 netbeans_draw_multisign_indicator(gui.row);
2617# endif
2618#endif
2619
2620 return OK;
2621}
2622
2623/*
2624 * Un-draw the cursor. Actually this just redraws the character at the given
2625 * position. The character just before it too, for when it was in bold.
2626 */
2627 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002628gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 if (gui.cursor_is_valid)
2631 {
2632#ifdef FEAT_HANGULIN
2633 if (composing_hangul
2634 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002635 {
2636 char_u *comp_buf;
2637 int comp_len;
2638
2639 comp_buf = hangul_composing_buffer_get(&comp_len);
2640 if (comp_buf)
2641 {
2642 (void)gui_outstr_nowrap(comp_buf, comp_len,
2643 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2644 gui.norm_pixel, gui.back_pixel, 0);
2645 vim_free(comp_buf);
2646 }
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 else
2649 {
2650#endif
2651 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2652 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2653 && gui.cursor_col > 0)
2654 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2655 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2656#ifdef FEAT_HANGULIN
2657 if (composing_hangul)
2658 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2659 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2660 }
2661#endif
2662 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2663 * here in case it wasn't needed to undraw it. */
2664 gui.cursor_is_valid = FALSE;
2665 }
2666}
2667
2668 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002669gui_redraw(
2670 int x,
2671 int y,
2672 int w,
2673 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 int row1, col1, row2, col2;
2676
2677 row1 = Y_2_ROW(y);
2678 col1 = X_2_COL(x);
2679 row2 = Y_2_ROW(y + h - 1);
2680 col2 = X_2_COL(x + w - 1);
2681
2682 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2683
2684 /*
2685 * We may need to redraw the cursor, but don't take it upon us to change
2686 * its location after a scroll.
2687 * (maybe be more strict even and test col too?)
2688 * These things may be outside the update/clipping region and reality may
2689 * not reflect Vims internal ideas if these operations are clipped away.
2690 */
2691 if (gui.row == gui.cursor_row)
2692 gui_update_cursor(TRUE, TRUE);
2693}
2694
2695/*
2696 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2697 * from col1 to col2 (inclusive).
2698 * Return TRUE when the character before the first drawn character has
2699 * different attributes (may have to be redrawn too).
2700 */
2701 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002702gui_redraw_block(
2703 int row1,
2704 int col1,
2705 int row2,
2706 int col2,
2707 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 int old_row, old_col;
2710 long_u old_hl_mask;
2711 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002712 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 int idx, len;
2714 int back, nback;
2715 int retval = FALSE;
2716#ifdef FEAT_MBYTE
2717 int orig_col1, orig_col2;
2718#endif
2719
2720 /* Don't try to update when ScreenLines is not valid */
2721 if (!screen_cleared || ScreenLines == NULL)
2722 return retval;
2723
2724 /* Don't try to draw outside the shell! */
2725 /* Check everything, strange values may be caused by a big border width */
2726 col1 = check_col(col1);
2727 col2 = check_col(col2);
2728 row1 = check_row(row1);
2729 row2 = check_row(row2);
2730
2731 /* Remember where our cursor was */
2732 old_row = gui.row;
2733 old_col = gui.col;
2734 old_hl_mask = gui.highlight_mask;
2735#ifdef FEAT_MBYTE
2736 orig_col1 = col1;
2737 orig_col2 = col2;
2738#endif
2739
2740 for (gui.row = row1; gui.row <= row2; gui.row++)
2741 {
2742#ifdef FEAT_MBYTE
2743 /* When only half of a double-wide character is in the block, include
2744 * the other half. */
2745 col1 = orig_col1;
2746 col2 = orig_col2;
2747 off = LineOffset[gui.row];
2748 if (enc_dbcs != 0)
2749 {
2750 if (col1 > 0)
2751 col1 -= dbcs_screen_head_off(ScreenLines + off,
2752 ScreenLines + off + col1);
2753 col2 += dbcs_screen_tail_off(ScreenLines + off,
2754 ScreenLines + off + col2);
2755 }
2756 else if (enc_utf8)
2757 {
2758 if (ScreenLines[off + col1] == 0)
2759 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002760# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2762 ++col2;
2763# endif
2764 }
2765#endif
2766 gui.col = col1;
2767 off = LineOffset[gui.row] + gui.col;
2768 len = col2 - col1 + 1;
2769
2770 /* Find how many chars back this highlighting starts, or where a space
2771 * is. Needed for when the bold trick is used */
2772 for (back = 0; back < col1; ++back)
2773 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2774 || ScreenLines[off - 1 - back] == ' ')
2775 break;
2776 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2777 && ScreenLines[off - 1] != ' ');
2778
2779 /* Break it up in strings of characters with the same attributes. */
2780 /* Print UTF-8 characters individually. */
2781 while (len > 0)
2782 {
2783 first_attr = ScreenAttrs[off];
2784 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002785#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (enc_utf8 && ScreenLinesUC[off] != 0)
2787 {
2788 /* output multi-byte character separately */
2789 nback = gui_screenchar(off, flags,
2790 (guicolor_T)0, (guicolor_T)0, back);
2791 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2792 idx = 2;
2793 else
2794 idx = 1;
2795 }
2796 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2797 {
2798 /* output double-byte, single-width character separately */
2799 nback = gui_screenchar(off, flags,
2800 (guicolor_T)0, (guicolor_T)0, back);
2801 idx = 1;
2802 }
2803 else
2804#endif
2805 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002806#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 for (idx = 0; idx < len; ++idx)
2808 {
2809 if (enc_utf8 && ScreenLines[off + idx] == 0)
2810 continue; /* skip second half of double-width char */
2811 if (ScreenAttrs[off + idx] != first_attr)
2812 break;
2813 }
2814 /* gui_screenstr() takes care of multibyte chars */
2815 nback = gui_screenstr(off, idx, flags,
2816 (guicolor_T)0, (guicolor_T)0, back);
2817#else
2818 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2819 idx++)
2820 {
2821# ifdef FEAT_MBYTE
2822 /* Stop at a multi-byte Unicode character. */
2823 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2824 break;
2825 if (enc_dbcs == DBCS_JPNU)
2826 {
2827 /* Stop at a double-byte single-width char. */
2828 if (ScreenLines[off + idx] == 0x8e)
2829 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002830 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 + off + idx) == 2)
2832 ++idx; /* skip second byte of double-byte char */
2833 }
2834# endif
2835 }
2836 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2837 (guicolor_T)0, (guicolor_T)0, back);
2838#endif
2839 }
2840 if (nback == FAIL)
2841 {
2842 /* Must back up to start drawing where a bold or italic word
2843 * starts. */
2844 off -= back;
2845 len += back;
2846 gui.col -= back;
2847 }
2848 else
2849 {
2850 off += idx;
2851 len -= idx;
2852 }
2853 back = 0;
2854 }
2855 }
2856
2857 /* Put the cursor back where it was */
2858 gui.row = old_row;
2859 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002860 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 return retval;
2863}
2864
2865 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002866gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 if (count <= 0)
2869 return;
2870
2871 if (row + count > gui.scroll_region_bot)
2872 /* Scrolled out of region, just blank the lines out */
2873 gui_clear_block(row, gui.scroll_region_left,
2874 gui.scroll_region_bot, gui.scroll_region_right);
2875 else
2876 {
2877 gui_mch_delete_lines(row, count);
2878
2879 /* If the cursor was in the deleted lines it's now gone. If the
2880 * cursor was in the scrolled lines adjust its position. */
2881 if (gui.cursor_row >= row
2882 && gui.cursor_col >= gui.scroll_region_left
2883 && gui.cursor_col <= gui.scroll_region_right)
2884 {
2885 if (gui.cursor_row < row + count)
2886 gui.cursor_is_valid = FALSE;
2887 else if (gui.cursor_row <= gui.scroll_region_bot)
2888 gui.cursor_row -= count;
2889 }
2890 }
2891}
2892
2893 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002894gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 if (count <= 0)
2897 return;
2898
2899 if (row + count > gui.scroll_region_bot)
2900 /* Scrolled out of region, just blank the lines out */
2901 gui_clear_block(row, gui.scroll_region_left,
2902 gui.scroll_region_bot, gui.scroll_region_right);
2903 else
2904 {
2905 gui_mch_insert_lines(row, count);
2906
2907 if (gui.cursor_row >= gui.row
2908 && gui.cursor_col >= gui.scroll_region_left
2909 && gui.cursor_col <= gui.scroll_region_right)
2910 {
2911 if (gui.cursor_row <= gui.scroll_region_bot - count)
2912 gui.cursor_row += count;
2913 else if (gui.cursor_row <= gui.scroll_region_bot)
2914 gui.cursor_is_valid = FALSE;
2915 }
2916 }
2917}
2918
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002919#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002920/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002921 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2922 */
2923 static int
2924gui_wait_for_chars_3(
2925 long wtime,
2926 int *interrupted UNUSED,
2927 int ignore_input UNUSED)
2928{
2929 return gui_mch_wait_for_chars(wtime);
2930}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002931#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002932
2933/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002934 * Returns OK if a character was found to be available within the given time,
2935 * or FAIL otherwise.
2936 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002937 static int
2938gui_wait_for_chars_or_timer(long wtime)
2939{
2940#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002941 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3, NULL, 0);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002942#else
2943 return gui_mch_wait_for_chars(wtime);
2944#endif
2945}
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947/*
2948 * The main GUI input routine. Waits for a character from the keyboard.
2949 * wtime == -1 Wait forever.
2950 * wtime == 0 Don't wait.
2951 * wtime > 0 Wait wtime milliseconds for a character.
2952 * Returns OK if a character was found to be available within the given time,
2953 * or FAIL otherwise.
2954 */
2955 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002956gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
2958 int retval;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002959#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002960 ELAPSED_TYPE start_tv;
2961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002963#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 /*
2965 * If we're going to wait a bit, update the menus and mouse shape for the
2966 * current State.
2967 */
2968 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 gui_update_menus(0);
2970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 gui_mch_update();
2973 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
2978 /* Before waiting, flush any output to the screen. */
2979 gui_mch_flush();
2980
2981 if (wtime > 0)
2982 {
2983 /* Blink when waiting for a character. Probably only does something
2984 * for showmatch() */
2985 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002986 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002987 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 return retval;
2989 }
2990
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002991#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002992 ELAPSED_INIT(start_tv);
2993#endif
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002996 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 */
2998 gui_mch_start_blink();
2999
Bram Moolenaar3918c952005-03-15 22:34:55 +00003000 retval = FAIL;
3001 /*
3002 * We may want to trigger the CursorHold event. First wait for
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003003 * 'updatetime' and if nothing is typed within that time, and feedkeys()
3004 * wasn't used, put the K_CURSORHOLD key in the input buffer.
Bram Moolenaar3918c952005-03-15 22:34:55 +00003005 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01003006 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003007 retval = OK;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003008 else if (trigger_cursorhold()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003009#ifdef ELAPSED_FUNC
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003010 && ELAPSED_FUNC(start_tv) >= p_ut
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003011#endif
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003012 && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00003014 char_u buf[3];
3015
3016 /* Put K_CURSORHOLD in the input buffer. */
3017 buf[0] = CSI;
3018 buf[1] = KS_EXTRA;
3019 buf[2] = (int)KE_CURSORHOLD;
3020 add_to_input_buf(buf, 3);
3021
3022 retval = OK;
3023 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00003024
Bram Moolenaar9472eec2017-06-05 13:31:56 +02003025 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003026 {
3027 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00003028 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01003029 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003032 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 return retval;
3034}
3035
3036/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003037 * Equivalent of mch_inchar() for the GUI.
3038 */
3039 int
3040gui_inchar(
3041 char_u *buf,
3042 int maxlen,
3043 long wtime, /* milli seconds */
3044 int tb_change_cnt)
3045{
3046 if (gui_wait_for_chars(wtime, tb_change_cnt)
3047 && !typebuf_changed(tb_change_cnt))
3048 return read_from_input_buf(buf, (long)maxlen);
3049 return 0;
3050}
3051
3052/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003053 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 */
3055 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003056fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 p[0] = (char_u)(col / 128 + ' ' + 1);
3059 p[1] = (char_u)(col % 128 + ' ' + 1);
3060 p[2] = (char_u)(row / 128 + ' ' + 1);
3061 p[3] = (char_u)(row % 128 + ' ' + 1);
3062}
3063
3064/*
3065 * Generic mouse support function. Add a mouse event to the input buffer with
3066 * the given properties.
3067 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3068 * MOUSE_X1, MOUSE_X2
3069 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003070 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3071 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * x, y --- Coordinates of mouse in pixels.
3073 * repeated_click --- TRUE if this click comes only a short time after a
3074 * previous click.
3075 * modifiers --- Bit field which may be any of the following modifiers
3076 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3077 * This function will ignore drag events where the mouse has not moved to a new
3078 * character.
3079 */
3080 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003081gui_send_mouse_event(
3082 int button,
3083 int x,
3084 int y,
3085 int repeated_click,
3086 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 static int prev_row = 0, prev_col = 0;
3089 static int prev_button = -1;
3090 static int num_clicks = 1;
3091 char_u string[10];
3092 enum key_extra button_char;
3093 int row, col;
3094#ifdef FEAT_CLIPBOARD
3095 int checkfor;
3096 int did_clip = FALSE;
3097#endif
3098
3099 /*
3100 * Scrolling may happen at any time, also while a selection is present.
3101 */
3102 switch (button)
3103 {
3104 case MOUSE_X1:
3105 button_char = KE_X1MOUSE;
3106 goto button_set;
3107 case MOUSE_X2:
3108 button_char = KE_X2MOUSE;
3109 goto button_set;
3110 case MOUSE_4:
3111 button_char = KE_MOUSEDOWN;
3112 goto button_set;
3113 case MOUSE_5:
3114 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003115 goto button_set;
3116 case MOUSE_6:
3117 button_char = KE_MOUSELEFT;
3118 goto button_set;
3119 case MOUSE_7:
3120 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121button_set:
3122 {
3123 /* Don't put events in the input queue now. */
3124 if (hold_gui_events)
3125 return;
3126
3127 string[3] = CSI;
3128 string[4] = KS_EXTRA;
3129 string[5] = (int)button_char;
3130
3131 /* Pass the pointer coordinates of the scroll event so that we
3132 * know which window to scroll. */
3133 row = gui_xy2colrow(x, y, &col);
3134 string[6] = (char_u)(col / 128 + ' ' + 1);
3135 string[7] = (char_u)(col % 128 + ' ' + 1);
3136 string[8] = (char_u)(row / 128 + ' ' + 1);
3137 string[9] = (char_u)(row % 128 + ' ' + 1);
3138
3139 if (modifiers == 0)
3140 add_to_input_buf(string + 3, 7);
3141 else
3142 {
3143 string[0] = CSI;
3144 string[1] = KS_MODIFIER;
3145 string[2] = 0;
3146 if (modifiers & MOUSE_SHIFT)
3147 string[2] |= MOD_MASK_SHIFT;
3148 if (modifiers & MOUSE_CTRL)
3149 string[2] |= MOD_MASK_CTRL;
3150 if (modifiers & MOUSE_ALT)
3151 string[2] |= MOD_MASK_ALT;
3152 add_to_input_buf(string, 10);
3153 }
3154 return;
3155 }
3156 }
3157
3158#ifdef FEAT_CLIPBOARD
3159 /* If a clipboard selection is in progress, handle it */
3160 if (clip_star.state == SELECT_IN_PROGRESS)
3161 {
3162 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3163 return;
3164 }
3165
3166 /* Determine which mouse settings to look for based on the current mode */
3167 switch (get_real_state())
3168 {
3169 case NORMAL_BUSY:
3170 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003171# ifdef FEAT_TERMINAL
3172 case TERMINAL:
3173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 case NORMAL: checkfor = MOUSE_NORMAL; break;
3175 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003176 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 case REPLACE:
3178 case REPLACE+LANGMAP:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003179# ifdef FEAT_VREPLACE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 case VREPLACE:
3181 case VREPLACE+LANGMAP:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 case INSERT:
3184 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3185 case ASKMORE:
3186 case HITRETURN: /* At the more- and hit-enter prompt pass the
3187 mouse event for a click on or below the
3188 message line. */
3189 if (Y_2_ROW(y) >= msg_row)
3190 checkfor = MOUSE_NORMAL;
3191 else
3192 checkfor = MOUSE_RETURN;
3193 break;
3194
3195 /*
3196 * On the command line, use the clipboard selection on all lines
3197 * but the command line. But not when pasting.
3198 */
3199 case CMDLINE:
3200 case CMDLINE+LANGMAP:
3201 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3202 checkfor = MOUSE_NONE;
3203 else
3204 checkfor = MOUSE_COMMAND;
3205 break;
3206
3207 default:
3208 checkfor = MOUSE_NONE;
3209 break;
3210 };
3211
3212 /*
3213 * Allow clipboard selection of text on the command line in "normal"
3214 * modes. Don't do this when dragging the status line, or extending a
3215 * Visual selection.
3216 */
3217 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003218 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 && button != MOUSE_DRAG
3220# ifdef FEAT_MOUSESHAPE
3221 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223# endif
3224 )
3225 checkfor = MOUSE_NONE;
3226
3227 /*
3228 * Use modeless selection when holding CTRL and SHIFT pressed.
3229 */
3230 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3231 checkfor = MOUSE_NONEF;
3232
3233 /*
3234 * In Ex mode, always use modeless selection.
3235 */
3236 if (exmode_active)
3237 checkfor = MOUSE_NONE;
3238
3239 /*
3240 * If the mouse settings say to not use the mouse, use the modeless
3241 * selection. But if Visual is active, assume that only the Visual area
3242 * will be selected.
3243 * Exception: On the command line, both the selection is used and a mouse
3244 * key is send.
3245 */
3246 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 /* Don't do modeless selection in Visual mode. */
3249 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3250 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /*
3253 * When 'mousemodel' is "popup", shift-left is translated to right.
3254 * But not when also using Ctrl.
3255 */
3256 if (mouse_model_popup() && button == MOUSE_LEFT
3257 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3258 {
3259 button = MOUSE_RIGHT;
3260 modifiers &= ~ MOUSE_SHIFT;
3261 }
3262
3263 /* If the selection is done, allow the right button to extend it.
3264 * If the selection is cleared, allow the right button to start it
3265 * from the cursor position. */
3266 if (button == MOUSE_RIGHT)
3267 {
3268 if (clip_star.state == SELECT_CLEARED)
3269 {
3270 if (State & CMDLINE)
3271 {
3272 col = msg_col;
3273 row = msg_row;
3274 }
3275 else
3276 {
3277 col = curwin->w_wcol;
3278 row = curwin->w_wrow + W_WINROW(curwin);
3279 }
3280 clip_start_selection(col, row, FALSE);
3281 }
3282 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3283 repeated_click);
3284 did_clip = TRUE;
3285 }
3286 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003287 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
3289 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3290 did_clip = TRUE;
3291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 /* Always allow pasting */
3294 if (button != MOUSE_MIDDLE)
3295 {
3296 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3297 return;
3298 if (checkfor != MOUSE_COMMAND)
3299 button = MOUSE_LEFT;
3300 }
3301 repeated_click = FALSE;
3302 }
3303
3304 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003305 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
3307
3308 /* Don't put events in the input queue now. */
3309 if (hold_gui_events)
3310 return;
3311
3312 row = gui_xy2colrow(x, y, &col);
3313
3314 /*
3315 * If we are dragging and the mouse hasn't moved far enough to be on a
3316 * different character, then don't send an event to vim.
3317 */
3318 if (button == MOUSE_DRAG)
3319 {
3320 if (row == prev_row && col == prev_col)
3321 return;
3322 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3323 if (y < 0)
3324 row = -1;
3325 }
3326
3327 /*
3328 * If topline has changed (window scrolled) since the last click, reset
3329 * repeated_click, because we don't want starting Visual mode when
3330 * clicking on a different character in the text.
3331 */
3332 if (curwin->w_topline != gui_prev_topline
3333#ifdef FEAT_DIFF
3334 || curwin->w_topfill != gui_prev_topfill
3335#endif
3336 )
3337 repeated_click = FALSE;
3338
3339 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3340 string[1] = KS_MOUSE;
3341 string[2] = KE_FILLER;
3342 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3343 {
3344 if (repeated_click)
3345 {
3346 /*
3347 * Handle multiple clicks. They only count if the mouse is still
3348 * pointing at the same character.
3349 */
3350 if (button != prev_button || row != prev_row || col != prev_col)
3351 num_clicks = 1;
3352 else if (++num_clicks > 4)
3353 num_clicks = 1;
3354 }
3355 else
3356 num_clicks = 1;
3357 prev_button = button;
3358 gui_prev_topline = curwin->w_topline;
3359#ifdef FEAT_DIFF
3360 gui_prev_topfill = curwin->w_topfill;
3361#endif
3362
3363 string[3] = (char_u)(button | 0x20);
3364 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3365 }
3366 else
3367 string[3] = (char_u)button;
3368
3369 string[3] |= modifiers;
3370 fill_mouse_coord(string + 4, col, row);
3371 add_to_input_buf(string, 8);
3372
3373 if (row < 0)
3374 prev_row = 0;
3375 else
3376 prev_row = row;
3377 prev_col = col;
3378
3379 /*
3380 * We need to make sure this is cleared since Athena doesn't tell us when
3381 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3382 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003383#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 gui.dragged_sb = SBAR_NONE;
3385#endif
3386}
3387
3388/*
3389 * Convert x and y coordinate to column and row in text window.
3390 * Corrects for multi-byte character.
3391 * returns column in "*colp" and row as return value;
3392 */
3393 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003394gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 int col = check_col(X_2_COL(x));
3397 int row = check_row(Y_2_ROW(y));
3398
3399#ifdef FEAT_MBYTE
3400 *colp = mb_fix_col(col, row);
3401#else
3402 *colp = col;
3403#endif
3404 return row;
3405}
3406
3407#if defined(FEAT_MENU) || defined(PROTO)
3408/*
3409 * Callback function for when a menu entry has been selected.
3410 */
3411 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003412gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003414 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 /* Don't put events in the input queue now. */
3417 if (hold_gui_events)
3418 return;
3419
3420 bytes[0] = CSI;
3421 bytes[1] = KS_MENU;
3422 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003423 add_to_input_buf(bytes, 3);
3424 add_long_to_buf((long_u)menu, bytes);
3425 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426}
3427#endif
3428
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003429static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431/*
3432 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003433 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * in p_go.
3435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003437gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#ifdef FEAT_MENU
3440 static int prev_menu_is_active = -1;
3441#endif
3442#ifdef FEAT_TOOLBAR
3443 static int prev_toolbar = -1;
3444 int using_toolbar = FALSE;
3445#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003446#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003447 int using_tabline;
3448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#ifdef FEAT_FOOTER
3450 static int prev_footer = -1;
3451 int using_footer = FALSE;
3452#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003453#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 static int prev_tearoff = -1;
3455 int using_tearoff = FALSE;
3456#endif
3457
3458 char_u *p;
3459 int i;
3460#ifdef FEAT_MENU
3461 int grey_old, grey_new;
3462 char_u *temp;
3463#endif
3464 win_T *wp;
3465 int need_set_size;
3466 int fix_size;
3467
3468#ifdef FEAT_MENU
3469 if (oldval != NULL && gui.in_use)
3470 {
3471 /*
3472 * Check if the menu's go from grey to non-grey or vise versa.
3473 */
3474 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3475 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3476 if (grey_old != grey_new)
3477 {
3478 temp = p_go;
3479 p_go = oldval;
3480 gui_update_menus(MENU_ALL_MODES);
3481 p_go = temp;
3482 }
3483 }
3484 gui.menu_is_active = FALSE;
3485#endif
3486
3487 for (i = 0; i < 3; i++)
3488 gui.which_scrollbars[i] = FALSE;
3489 for (p = p_go; *p; p++)
3490 switch (*p)
3491 {
3492 case GO_LEFT:
3493 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3494 break;
3495 case GO_RIGHT:
3496 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 case GO_VLEFT:
3499 if (win_hasvertsplit())
3500 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3501 break;
3502 case GO_VRIGHT:
3503 if (win_hasvertsplit())
3504 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 case GO_BOT:
3507 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3508 break;
3509#ifdef FEAT_MENU
3510 case GO_MENUS:
3511 gui.menu_is_active = TRUE;
3512 break;
3513#endif
3514 case GO_GREY:
3515 /* make menu's have grey items, ignored here */
3516 break;
3517#ifdef FEAT_TOOLBAR
3518 case GO_TOOLBAR:
3519 using_toolbar = TRUE;
3520 break;
3521#endif
3522#ifdef FEAT_FOOTER
3523 case GO_FOOTER:
3524 using_footer = TRUE;
3525 break;
3526#endif
3527 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003528#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 using_tearoff = TRUE;
3530#endif
3531 break;
3532 default:
3533 /* Ignore options that are not supported */
3534 break;
3535 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (gui.in_use)
3538 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003539 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003541
3542#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003543 /* Update the GUI tab line, it may appear or disappear. This may
3544 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003545 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003546 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003547 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003548 /* We don't want a resize event change "Rows" here, save and
3549 * restore it. Resizing is handled below. */
3550 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003551 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003552 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003553 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003554 if (using_tabline)
3555 fix_size = TRUE;
3556 if (!gui_use_tabline())
3557 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3558 }
3559#endif
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 for (i = 0; i < 3; i++)
3562 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003563 /* The scrollbar needs to be updated when it is shown/unshown and
3564 * when switching tab pages. But the size only changes when it's
3565 * shown/unshown. Thus we need two places to remember whether a
3566 * scrollbar is there or not. */
3567 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003568 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003569 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 if (i == SBAR_BOTTOM)
3572 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3573 gui.which_scrollbars[i]);
3574 else
3575 {
3576 FOR_ALL_WINDOWS(wp)
3577 {
3578 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3579 }
3580 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003581 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3582 {
3583 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003584 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003585 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003586 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003587 if (gui.which_scrollbars[i])
3588 fix_size = TRUE;
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003591 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003592 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594
3595#ifdef FEAT_MENU
3596 if (gui.menu_is_active != prev_menu_is_active)
3597 {
3598 /* We don't want a resize event change "Rows" here, save and
3599 * restore it. Resizing is handled below. */
3600 i = Rows;
3601 gui_mch_enable_menu(gui.menu_is_active);
3602 Rows = i;
3603 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003604 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (gui.menu_is_active)
3606 fix_size = TRUE;
3607 }
3608#endif
3609
3610#ifdef FEAT_TOOLBAR
3611 if (using_toolbar != prev_toolbar)
3612 {
3613 gui_mch_show_toolbar(using_toolbar);
3614 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003615 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (using_toolbar)
3617 fix_size = TRUE;
3618 }
3619#endif
3620#ifdef FEAT_FOOTER
3621 if (using_footer != prev_footer)
3622 {
3623 gui_mch_enable_footer(using_footer);
3624 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003625 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (using_footer)
3627 fix_size = TRUE;
3628 }
3629#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (using_tearoff != prev_tearoff)
3632 {
3633 gui_mch_toggle_tearoffs(using_tearoff);
3634 prev_tearoff = using_tearoff;
3635 }
3636#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003637 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003638 {
3639#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003640 long prev_Columns = Columns;
3641 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 /* Adjust the size of the window to make the text area keep the
3644 * same size and to avoid that part of our window is off-screen
3645 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003646 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003647
3648#ifdef FEAT_GUI_GTK
3649 /* GTK has the annoying habit of sending us resize events when
3650 * changing the window size ourselves. This mostly happens when
3651 * waiting for a character to arrive, quite unpredictably, and may
3652 * change Columns and Rows when we don't want it. Wait for a
3653 * character here to avoid this effect.
3654 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003655 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003656 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003657 * Don't change Rows when adding menu/toolbar/tabline.
3658 * Don't change Columns when adding vertical toolbar. */
3659 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003660 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003661 if ((need_set_size & RESIZE_VERT) == 0)
3662 Rows = prev_Rows;
3663 if ((need_set_size & RESIZE_HOR) == 0)
3664 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003665#endif
3666 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003667 /* When the console tabline appears or disappears the window positions
3668 * change. */
3669 if (firstwin->w_winrow != tabline_height())
3670 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672}
3673
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3675/*
3676 * Return TRUE if the GUI is taking care of the tabline.
3677 * It may still be hidden if 'showtabline' is zero.
3678 */
3679 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003680gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003681{
3682 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3683}
3684
3685/*
3686 * Return TRUE if the GUI is showing the tabline.
3687 * This uses 'showtabline'.
3688 */
3689 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003690gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003691{
3692 if (!gui_use_tabline()
3693 || p_stal == 0
3694 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3695 return FALSE;
3696 return TRUE;
3697}
3698
3699/*
3700 * Update the tabline.
3701 * This may display/undisplay the tabline and update the labels.
3702 */
3703 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003704gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705{
3706 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003707 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003708
3709 if (!gui.starting && starting == 0)
3710 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003711 /* Updating the tabline uses direct GUI commands, flush
3712 * outstanding instructions first. (esp. clear screen) */
3713 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003714
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003715 if (!showit != !shown)
3716 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003717 if (showit != 0)
3718 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003719
3720 /* When the tabs change from hidden to shown or from shown to
3721 * hidden the size of the text area should remain the same. */
3722 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003723 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003724 }
3725}
3726
3727/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003728 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003729 */
3730 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003731get_tabline_label(
3732 tabpage_T *tp,
3733 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003734{
3735 int modified = FALSE;
3736 char_u buf[40];
3737 int wincount;
3738 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003739 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003740
Bram Moolenaar57657d82006-04-21 22:12:41 +00003741 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003742 opt = (tooltip ? &p_gtt : &p_gtl);
3743 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003744 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003745 int use_sandbox = FALSE;
3746 int save_called_emsg = called_emsg;
3747 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003748 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003749 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3750 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003751
3752 called_emsg = FALSE;
3753
3754 printer_page_num = tabpage_index(tp);
3755# ifdef FEAT_EVAL
3756 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003757 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003758# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003759 /* It's almost as going to the tabpage, but without autocommands. */
3760 curtab->tp_firstwin = firstwin;
3761 curtab->tp_lastwin = lastwin;
3762 curtab->tp_curwin = curwin;
3763 save_curtab = curtab;
3764 curtab = tp;
3765 topframe = curtab->tp_topframe;
3766 firstwin = curtab->tp_firstwin;
3767 lastwin = curtab->tp_lastwin;
3768 curwin = curtab->tp_curwin;
3769 curbuf = curwin->w_buffer;
3770
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003771 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003772 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003773 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003774 STRCPY(NameBuff, res);
3775
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003776 /* Back to the original curtab. */
3777 curtab = save_curtab;
3778 topframe = curtab->tp_topframe;
3779 firstwin = curtab->tp_firstwin;
3780 lastwin = curtab->tp_lastwin;
3781 curwin = curtab->tp_curwin;
3782 curbuf = curwin->w_buffer;
3783
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003784 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003785 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003786 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003787 called_emsg |= save_called_emsg;
3788 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003789
3790 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3791 * use a default label. */
3792 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003793 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003794 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003795 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003796 if (!tooltip)
3797 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003798
3799 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3800 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3801 if (bufIsChanged(wp->w_buffer))
3802 modified = TRUE;
3803 if (modified || wincount > 1)
3804 {
3805 if (wincount > 1)
3806 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3807 else
3808 buf[0] = NUL;
3809 if (modified)
3810 STRCAT(buf, "+");
3811 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003812 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003813 mch_memmove(NameBuff, buf, STRLEN(buf));
3814 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003815 }
3816}
3817
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003818/*
3819 * Send the event for clicking to select tab page "nr".
3820 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003821 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003822 */
3823 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003824send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003825{
3826 char_u string[3];
3827
3828 if (nr == tabpage_index(curtab))
3829 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003830
3831 /* Don't put events in the input queue now. */
3832 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003833# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003834 || cmdwin_type != 0
3835# endif
3836 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003837 {
3838 /* Set it back to the current tab page. */
3839 gui_mch_set_curtab(tabpage_index(curtab));
3840 return FALSE;
3841 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003842
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003843 string[0] = CSI;
3844 string[1] = KS_TABLINE;
3845 string[2] = KE_FILLER;
3846 add_to_input_buf(string, 3);
3847 string[0] = nr;
3848 add_to_input_buf_csi(string, 1);
3849 return TRUE;
3850}
3851
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003852/*
3853 * Send a tabline menu event
3854 */
3855 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003856send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003857{
3858 char_u string[3];
3859
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003860 /* Don't put events in the input queue now. */
3861 if (hold_gui_events)
3862 return;
3863
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003864 string[0] = CSI;
3865 string[1] = KS_TABMENU;
3866 string[2] = KE_FILLER;
3867 add_to_input_buf(string, 3);
3868 string[0] = tabidx;
3869 string[1] = (char_u)(long)event;
3870 add_to_input_buf_csi(string, 2);
3871}
3872
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
3875/*
3876 * Scrollbar stuff:
3877 */
3878
Bram Moolenaare45828b2006-02-15 22:12:56 +00003879/*
3880 * Remove all scrollbars. Used before switching to another tab page.
3881 */
3882 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003883gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003884{
3885 int i;
3886 win_T *wp;
3887
3888 for (i = 0; i < 3; i++)
3889 {
3890 if (i == SBAR_BOTTOM)
3891 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3892 else
3893 {
3894 FOR_ALL_WINDOWS(wp)
3895 {
3896 gui_do_scrollbar(wp, i, FALSE);
3897 }
3898 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003899 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003900 }
3901}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003904gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906 static int sbar_ident = 0;
3907
3908 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3909 sb->wp = wp;
3910 sb->type = type;
3911 sb->value = 0;
3912#ifdef FEAT_GUI_ATHENA
3913 sb->pixval = 0;
3914#endif
3915 sb->size = 1;
3916 sb->max = 1;
3917 sb->top = 0;
3918 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 sb->status_height = 0;
3921 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3922}
3923
3924/*
3925 * Find the scrollbar with the given index.
3926 */
3927 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003928gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 win_T *wp;
3931
3932 if (gui.bottom_sbar.ident == ident)
3933 return &gui.bottom_sbar;
3934 FOR_ALL_WINDOWS(wp)
3935 {
3936 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3937 return &wp->w_scrollbars[SBAR_LEFT];
3938 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3939 return &wp->w_scrollbars[SBAR_RIGHT];
3940 }
3941 return NULL;
3942}
3943
3944/*
3945 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3946 *
3947 * For Win32, Macintosh and GTK+ 2:
3948 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3949 * you stop dragging the scrollbar. We get here each time the scrollbar is
3950 * dragged another pixel, but as far as the rest of vim goes, it thinks
3951 * we're just hanging in the call to DispatchMessage() in
3952 * process_message(). The DispatchMessage() call that hangs was passed a
3953 * mouse button click event in the scrollbar window. -- webb.
3954 *
3955 * Solution: Do the scrolling right here. But only when allowed.
3956 * Ignore the scrollbars while executing an external command or when there
3957 * are still characters to be processed.
3958 */
3959 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003960gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int sb_num;
3964#ifdef USE_ON_FLY_SCROLL
3965 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967# ifdef FEAT_DIFF
3968 int old_topfill = curwin->w_topfill;
3969# endif
3970#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003971 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 int byte_count;
3973#endif
3974
3975 if (sb == NULL)
3976 return;
3977
3978 /* Don't put events in the input queue now. */
3979 if (hold_gui_events)
3980 return;
3981
3982#ifdef FEAT_CMDWIN
3983 if (cmdwin_type != 0 && sb->wp != curwin)
3984 return;
3985#endif
3986
3987 if (still_dragging)
3988 {
3989 if (sb->wp == NULL)
3990 gui.dragged_sb = SBAR_BOTTOM;
3991 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3992 gui.dragged_sb = SBAR_LEFT;
3993 else
3994 gui.dragged_sb = SBAR_RIGHT;
3995 gui.dragged_wp = sb->wp;
3996 }
3997 else
3998 {
3999 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004000#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004002 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 gui.dragged_wp = NULL;
4004#endif
4005 }
4006
4007 /* Vertical sbar info is kept in the first sbar (the left one) */
4008 if (sb->wp != NULL)
4009 sb = &sb->wp->w_scrollbars[0];
4010
4011 /*
4012 * Check validity of value
4013 */
4014 if (value < 0)
4015 value = 0;
4016#ifdef SCROLL_PAST_END
4017 else if (value > sb->max)
4018 value = sb->max;
4019#else
4020 if (value > sb->max - sb->size + 1)
4021 value = sb->max - sb->size + 1;
4022#endif
4023
4024 sb->value = value;
4025
4026#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00004027 /* When not allowed to do the scrolling right now, return.
4028 * This also checked input_available(), but that causes the first click in
4029 * a scrollbar to be ignored when Vim doesn't have focus. */
4030 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 return;
4032#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004033#ifdef FEAT_INS_EXPAND
4034 /* Disallow scrolling the current window when the completion popup menu is
4035 * visible. */
4036 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4037 return;
4038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040#ifdef FEAT_RIGHTLEFT
4041 if (sb->wp == NULL && curwin->w_p_rl)
4042 {
4043 value = sb->max + 1 - sb->size - value;
4044 if (value < 0)
4045 value = 0;
4046 }
4047#endif
4048
4049 if (sb->wp != NULL) /* vertical scrollbar */
4050 {
4051 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4053 sb_num++;
4054 if (wp == NULL)
4055 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057#ifdef USE_ON_FLY_SCROLL
4058 current_scrollbar = sb_num;
4059 scrollbar_value = value;
4060 if (State & NORMAL)
4061 {
4062 gui_do_scroll();
4063 setcursor();
4064 }
4065 else if (State & INSERT)
4066 {
4067 ins_scroll();
4068 setcursor();
4069 }
4070 else if (State & CMDLINE)
4071 {
4072 if (msg_scrolled == 0)
4073 {
4074 gui_do_scroll();
4075 redrawcmdline();
4076 }
4077 }
4078# ifdef FEAT_FOLDING
4079 /* Value may have been changed for closed fold. */
4080 sb->value = sb->wp->w_topline - 1;
4081# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004082
4083 /* When dragging one scrollbar and there is another one at the other
4084 * side move the thumb of that one too. */
4085 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4086 gui_mch_set_scrollbar_thumb(
4087 &sb->wp->w_scrollbars[
4088 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4089 ? SBAR_LEFT : SBAR_RIGHT],
4090 sb->value, sb->size, sb->max);
4091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#else
4093 bytes[0] = CSI;
4094 bytes[1] = KS_VER_SCROLLBAR;
4095 bytes[2] = KE_FILLER;
4096 bytes[3] = (char_u)sb_num;
4097 byte_count = 4;
4098#endif
4099 }
4100 else
4101 {
4102#ifdef USE_ON_FLY_SCROLL
4103 scrollbar_value = value;
4104
4105 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004106 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else if (State & INSERT)
4108 ins_horscroll();
4109 else if (State & CMDLINE)
4110 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004111 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004113 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 redrawcmdline();
4115 }
4116 }
4117 if (old_leftcol != curwin->w_leftcol)
4118 {
4119 updateWindow(curwin); /* update window, status and cmdline */
4120 setcursor();
4121 }
4122#else
4123 bytes[0] = CSI;
4124 bytes[1] = KS_HOR_SCROLLBAR;
4125 bytes[2] = KE_FILLER;
4126 byte_count = 3;
4127#endif
4128 }
4129
4130#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /*
4132 * synchronize other windows, as necessary according to 'scrollbind'
4133 */
4134 if (curwin->w_p_scb
4135 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4136 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004137# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 ))))
4141 {
4142 do_check_scrollbind(TRUE);
4143 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004144 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (wp->w_redr_type > 0)
4146 updateWindow(wp);
4147 setcursor();
4148 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004149 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004151 add_to_input_buf(bytes, byte_count);
4152 add_long_to_buf((long_u)value, bytes);
4153 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#endif
4155}
4156
4157/*
4158 * Scrollbar stuff:
4159 */
4160
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004161/*
4162 * Called when something in the window layout has changed.
4163 */
4164 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004165gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004166{
4167 if (gui.in_use && starting == 0)
4168 {
4169 out_flush();
4170 gui_init_which_components(NULL);
4171 gui_update_scrollbars(TRUE);
4172 }
4173 need_mouse_correct = TRUE;
4174}
4175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004177gui_update_scrollbars(
4178 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 win_T *wp;
4181 scrollbar_T *sb;
4182 long val, size, max; /* need 32 bits here */
4183 int which_sb;
4184 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
4187 /* Update the horizontal scrollbar */
4188 gui_update_horiz_scrollbar(force);
4189
4190#ifndef WIN3264
4191 /* Return straight away if there is neither a left nor right scrollbar.
4192 * On MS-Windows this is required anyway for scrollwheel messages. */
4193 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4194 return;
4195#endif
4196
4197 /*
4198 * Don't want to update a scrollbar while we're dragging it. But if we
4199 * have both a left and right scrollbar, and we drag one of them, we still
4200 * need to update the other one.
4201 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004202 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4203 && gui.which_scrollbars[SBAR_LEFT]
4204 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
4206 /*
4207 * If we have two scrollbars and one of them is being dragged, just
4208 * copy the scrollbar position from the dragged one to the other one.
4209 */
4210 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4211 if (gui.dragged_wp != NULL)
4212 gui_mch_set_scrollbar_thumb(
4213 &gui.dragged_wp->w_scrollbars[which_sb],
4214 gui.dragged_wp->w_scrollbars[0].value,
4215 gui.dragged_wp->w_scrollbars[0].size,
4216 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218
4219 /* avoid that moving components around generates events */
4220 ++hold_gui_events;
4221
Bram Moolenaar45a24952016-07-24 22:25:15 +02004222 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 if (wp->w_buffer == NULL) /* just in case */
4225 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004226 /* Skip a scrollbar that is being dragged. */
4227 if (!force && (gui.dragged_sb == SBAR_LEFT
4228 || gui.dragged_sb == SBAR_RIGHT)
4229 && gui.dragged_wp == wp)
4230 continue;
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#ifdef SCROLL_PAST_END
4233 max = wp->w_buffer->b_ml.ml_line_count - 1;
4234#else
4235 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4236#endif
4237 if (max < 0) /* empty buffer */
4238 max = 0;
4239 val = wp->w_topline - 1;
4240 size = wp->w_height;
4241#ifdef SCROLL_PAST_END
4242 if (val > max) /* just in case */
4243 val = max;
4244#else
4245 if (size > max + 1) /* just in case */
4246 size = max + 1;
4247 if (val > max - size + 1)
4248 val = max - size + 1;
4249#endif
4250 if (val < 0) /* minimal value is 0 */
4251 val = 0;
4252
4253 /*
4254 * Scrollbar at index 0 (the left one) contains all the information.
4255 * It would be the same info for left and right so we just store it for
4256 * one of them.
4257 */
4258 sb = &wp->w_scrollbars[0];
4259
4260 /*
4261 * Note: no check for valid w_botline. If it's not valid the
4262 * scrollbars will be updated later anyway.
4263 */
4264 if (size < 1 || wp->w_botline - 2 > max)
4265 {
4266 /*
4267 * This can happen during changing files. Just don't update the
4268 * scrollbar for now.
4269 */
4270 sb->height = 0; /* Force update next time */
4271 if (gui.which_scrollbars[SBAR_LEFT])
4272 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4273 if (gui.which_scrollbars[SBAR_RIGHT])
4274 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4275 continue;
4276 }
4277 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 || sb->top != wp->w_winrow
4279 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004281 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 /* Height, width or position of scrollbar has changed. For
4284 * vertical split: curwin changed. */
4285 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 sb->top = wp->w_winrow;
4287 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 /* Calculate height and position in pixels */
4291 h = (sb->height + sb->status_height) * gui.char_height;
4292 y = sb->top * gui.char_height + gui.border_offset;
4293#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4294 if (gui.menu_is_active)
4295 y += gui.menu_height;
4296#endif
4297
4298#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4299 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4300# ifdef FEAT_GUI_ATHENA
4301 y += gui.toolbar_height;
4302# else
4303# ifdef FEAT_GUI_MSWIN
4304 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4305# endif
4306# endif
4307#endif
4308
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004309#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4310 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004312#endif
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 /* Height of top scrollbar includes width of top border */
4317 h += gui.border_offset;
4318 y -= gui.border_offset;
4319 }
4320 if (gui.which_scrollbars[SBAR_LEFT])
4321 {
4322 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4323 gui.left_sbar_x, y,
4324 gui.scrollbar_width, h);
4325 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4326 }
4327 if (gui.which_scrollbars[SBAR_RIGHT])
4328 {
4329 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4330 gui.right_sbar_x, y,
4331 gui.scrollbar_width, h);
4332 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4333 }
4334 }
4335
4336 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4337 * checking if the thumb moved at least a pixel. Only do this for
4338 * Athena, most other GUIs require the update anyway to make the
4339 * arrows work. */
4340#ifdef FEAT_GUI_ATHENA
4341 if (max == 0)
4342 y = 0;
4343 else
4344 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4345 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4346#else
4347 if (force || sb->value != val || sb->size != size || sb->max != max)
4348#endif
4349 {
4350 /* Thumb of scrollbar has moved */
4351 sb->value = val;
4352#ifdef FEAT_GUI_ATHENA
4353 sb->pixval = y;
4354#endif
4355 sb->size = size;
4356 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004357 if (gui.which_scrollbars[SBAR_LEFT]
4358 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4360 val, size, max);
4361 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004362 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4364 val, size, max);
4365 }
4366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 --hold_gui_events;
4369}
4370
4371/*
4372 * Enable or disable a scrollbar.
4373 * Check for scrollbars for vertically split windows which are not enabled
4374 * sometimes.
4375 */
4376 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004377gui_do_scrollbar(
4378 win_T *wp,
4379 int which, /* SBAR_LEFT or SBAR_RIGHT */
4380 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 int midcol = curwin->w_wincol + curwin->w_width / 2;
4383 int has_midcol = (wp->w_wincol <= midcol
4384 && wp->w_wincol + wp->w_width >= midcol);
4385
4386 /* Only enable scrollbars that contain the middle column of the current
4387 * window. */
4388 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4389 {
4390 /* Scrollbars only on one side. Don't enable scrollbars that don't
4391 * contain the middle column of the current window. */
4392 if (!has_midcol)
4393 enable = FALSE;
4394 }
4395 else
4396 {
4397 /* Scrollbars on both sides. Don't enable scrollbars that neither
4398 * contain the middle column of the current window nor are on the far
4399 * side. */
4400 if (midcol > Columns / 2)
4401 {
4402 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4403 enable = FALSE;
4404 }
4405 else
4406 {
4407 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4408 : !has_midcol)
4409 enable = FALSE;
4410 }
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4413}
4414
4415/*
4416 * Scroll a window according to the values set in the globals current_scrollbar
4417 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4418 * or FALSE otherwise.
4419 */
4420 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004421gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 win_T *wp, *save_wp;
4424 int i;
4425 long nlines;
4426 pos_T old_cursor;
4427 linenr_T old_topline;
4428#ifdef FEAT_DIFF
4429 int old_topfill;
4430#endif
4431
4432 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4433 if (wp == NULL)
4434 break;
4435 if (wp == NULL)
4436 /* Couldn't find window */
4437 return FALSE;
4438
4439 /*
4440 * Compute number of lines to scroll. If zero, nothing to do.
4441 */
4442 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4443 if (nlines == 0)
4444 return FALSE;
4445
4446 save_wp = curwin;
4447 old_topline = wp->w_topline;
4448#ifdef FEAT_DIFF
4449 old_topfill = wp->w_topfill;
4450#endif
4451 old_cursor = wp->w_cursor;
4452 curwin = wp;
4453 curbuf = wp->w_buffer;
4454 if (nlines < 0)
4455 scrolldown(-nlines, gui.dragged_wp == NULL);
4456 else
4457 scrollup(nlines, gui.dragged_wp == NULL);
4458 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4459 * the mouse-up event already, but we still want it to behave like when
4460 * dragging. But not the next click in an arrow. */
4461 if (gui.dragged_sb == SBAR_NONE)
4462 gui.dragged_wp = NULL;
4463
4464 if (old_topline != wp->w_topline
4465#ifdef FEAT_DIFF
4466 || old_topfill != wp->w_topfill
4467#endif
4468 )
4469 {
4470 if (p_so != 0)
4471 {
4472 cursor_correct(); /* fix window for 'so' */
4473 update_topline(); /* avoid up/down jump */
4474 }
4475 if (old_cursor.lnum != wp->w_cursor.lnum)
4476 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004480 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4481 validate_cursor();
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 curwin = save_wp;
4484 curbuf = save_wp->w_buffer;
4485
4486 /*
4487 * Don't call updateWindow() when nothing has changed (it will overwrite
4488 * the status line!).
4489 */
4490 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004491 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492#ifdef FEAT_DIFF
4493 || old_topfill != wp->w_topfill
4494#endif
4495 )
4496 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004497 int type = VALID;
4498
4499#ifdef FEAT_INS_EXPAND
4500 if (pum_visible())
4501 {
4502 type = NOT_VALID;
4503 wp->w_lines_valid = 0;
4504 }
4505#endif
4506 /* Don't set must_redraw here, it may cause the popup menu to
4507 * disappear when losing focus after a scrollbar drag. */
4508 if (wp->w_redr_type < type)
4509 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004510 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004512 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004515#ifdef FEAT_INS_EXPAND
4516 /* May need to redraw the popup menu. */
4517 if (pum_visible())
4518 pum_redraw();
4519#endif
4520
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004521 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522}
4523
4524
4525/*
4526 * Horizontal scrollbar stuff:
4527 */
4528
4529/*
4530 * Return length of line "lnum" for horizontal scrolling.
4531 */
4532 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004533scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
4535 char_u *p;
4536 colnr_T col;
4537 int w;
4538
4539 p = ml_get(lnum);
4540 col = 0;
4541 if (*p != NUL)
4542 for (;;)
4543 {
4544 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004545 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (*p == NUL) /* don't count the last character */
4547 break;
4548 col += w;
4549 }
4550 return col;
4551}
4552
4553/* Remember which line is currently the longest, so that we don't have to
4554 * search for it when scrolling horizontally. */
4555static linenr_T longest_lnum = 0;
4556
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004557/*
4558 * Find longest visible line number. If this is not possible (or not desired,
4559 * by setting 'h' in "guioptions") then the current line number is returned.
4560 */
4561 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004562gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004563{
4564 linenr_T ret = 0;
4565
4566 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4567 * line numbers, topline and botline can be invalid when displaying is
4568 * postponed. */
4569 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4570 && curwin->w_topline <= curwin->w_cursor.lnum
4571 && curwin->w_botline > curwin->w_cursor.lnum
4572 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4573 {
4574 linenr_T lnum;
4575 colnr_T n;
4576 long max = 0;
4577
4578 /* Use maximum of all visible lines. Remember the lnum of the
4579 * longest line, closest to the cursor line. Used when scrolling
4580 * below. */
4581 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4582 {
4583 n = scroll_line_len(lnum);
4584 if (n > (colnr_T)max)
4585 {
4586 max = n;
4587 ret = lnum;
4588 }
4589 else if (n == (colnr_T)max
4590 && abs((int)(lnum - curwin->w_cursor.lnum))
4591 < abs((int)(ret - curwin->w_cursor.lnum)))
4592 ret = lnum;
4593 }
4594 }
4595 else
4596 /* Use cursor line only. */
4597 ret = curwin->w_cursor.lnum;
4598
4599 return ret;
4600}
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004603gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 long value, size, max; /* need 32 bit ints here */
4606
4607 if (!gui.which_scrollbars[SBAR_BOTTOM])
4608 return;
4609
4610 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4611 return;
4612
4613 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4614 return;
4615
4616 /*
4617 * It is possible for the cursor to be invalid if we're in the middle of
4618 * something (like changing files). If so, don't do anything for now.
4619 */
4620 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4621 {
4622 gui.bottom_sbar.value = -1;
4623 return;
4624 }
4625
Bram Moolenaar02631462017-09-22 15:20:32 +02004626 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (curwin->w_p_wrap)
4628 {
4629 value = 0;
4630#ifdef SCROLL_PAST_END
4631 max = 0;
4632#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004633 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634#endif
4635 }
4636 else
4637 {
4638 value = curwin->w_leftcol;
4639
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004640 longest_lnum = gui_find_longest_lnum();
4641 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_VIRTUALEDIT
4644 if (virtual_active())
4645 {
4646 /* May move the cursor even further to the right. */
4647 if (curwin->w_virtcol >= (colnr_T)max)
4648 max = curwin->w_virtcol;
4649 }
4650#endif
4651
4652#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004653 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004656 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 size -= curwin_col_off();
4658#ifndef SCROLL_PAST_END
4659 max -= curwin_col_off();
4660#endif
4661 }
4662
4663#ifndef SCROLL_PAST_END
4664 if (value > max - size + 1)
4665 value = max - size + 1; /* limit the value to allowable range */
4666#endif
4667
4668#ifdef FEAT_RIGHTLEFT
4669 if (curwin->w_p_rl)
4670 {
4671 value = max + 1 - size - value;
4672 if (value < 0)
4673 {
4674 size += value;
4675 value = 0;
4676 }
4677 }
4678#endif
4679 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4680 && max == gui.bottom_sbar.max)
4681 return;
4682
4683 gui.bottom_sbar.value = value;
4684 gui.bottom_sbar.size = size;
4685 gui.bottom_sbar.max = max;
4686 gui.prev_wrap = curwin->w_p_wrap;
4687
4688 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4689}
4690
4691/*
4692 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4693 */
4694 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004695gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 /* no wrapping, no scrolling */
4698 if (curwin->w_p_wrap)
4699 return FALSE;
4700
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004701 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return FALSE;
4703
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004704 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004707 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004709 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004710 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004712 if (compute_longest_lnum)
4713 {
4714 curwin->w_cursor.lnum = gui_find_longest_lnum();
4715 curwin->w_cursor.col = 0;
4716 }
4717 /* Do a sanity check on "longest_lnum", just in case. */
4718 else if (longest_lnum >= curwin->w_topline
4719 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 curwin->w_cursor.lnum = longest_lnum;
4722 curwin->w_cursor.col = 0;
4723 }
4724 }
4725
4726 return leftcol_changed();
4727}
4728
4729/*
4730 * Check that none of the colors are the same as the background color
4731 */
4732 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004733gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4736 {
4737 gui_set_bg_color((char_u *)"White");
4738 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4739 gui_set_fg_color((char_u *)"Black");
4740 }
4741}
4742
Bram Moolenaar3918c952005-03-15 22:34:55 +00004743 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004744gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 gui.norm_pixel = gui_get_color(name);
4747 hl_set_fg_color_name(vim_strsave(name));
4748}
4749
Bram Moolenaar3918c952005-03-15 22:34:55 +00004750 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004751gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 gui.back_pixel = gui_get_color(name);
4754 hl_set_bg_color_name(vim_strsave(name));
4755}
4756
4757/*
4758 * Allocate a color by name.
4759 * Returns INVALCOLOR and gives an error message when failed.
4760 */
4761 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004762gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 guicolor_T t;
4765
4766 if (*name == NUL)
4767 return INVALCOLOR;
4768 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004771#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 && gui.in_use
4773#endif
4774 )
4775 EMSG2(_("E254: Cannot allocate color %s"), name);
4776 return t;
4777}
4778
4779/*
4780 * Return the grey value of a color (range 0-255).
4781 */
4782 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004783gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004785 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004787 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004788 + (((rgb >> 8) & 0xff) * 587)
4789 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790}
4791
4792#if defined(FEAT_GUI_X11) || defined(PROTO)
4793 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004794gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 win_T *wp;
4797
4798 /* Nothing to do if GUI hasn't started yet. */
4799 if (!gui.in_use)
4800 return;
4801
4802 FOR_ALL_WINDOWS(wp)
4803 {
4804 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4805 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4806 }
4807 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4808}
4809#endif
4810
4811/*
4812 * Call this when focus has changed.
4813 */
4814 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004815gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817/*
4818 * Skip this code to avoid drawing the cursor when debugging and switching
4819 * between the debugger window and gvim.
4820 */
4821#if 1
4822 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004823 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825# ifdef FEAT_XIM
4826 xim_set_focus(in_focus);
4827# endif
4828
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004829 /* Put events in the input queue only when allowed.
4830 * ui_focus_change() isn't called directly, because it invokes
4831 * autocommands and that must not happen asynchronously. */
4832 if (!hold_gui_events)
4833 {
4834 char_u bytes[3];
4835
4836 bytes[0] = CSI;
4837 bytes[1] = KS_EXTRA;
4838 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4839 add_to_input_buf(bytes, 3);
4840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
4842}
4843
4844/*
4845 * Called when the mouse moved (but not when dragging).
4846 */
4847 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004848gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004851 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Bram Moolenaard3667a22006-03-16 21:35:52 +00004853 /* Ignore this while still starting up. */
4854 if (!gui.in_use || gui.starting)
4855 return;
4856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_MOUSESHAPE
4858 /* Get window pointer, and update mouse shape as well. */
4859 wp = xy2win(x, y);
4860#endif
4861
4862 /* Only handle this when 'mousefocus' set and ... */
4863 if (p_mousef
4864 && !hold_gui_events /* not holding events */
4865 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4866 && State != HITRETURN /* but not hit-return prompt */
4867 && msg_scrolled == 0 /* no scrolled message */
4868 && !need_mouse_correct /* not moving the pointer */
4869 && gui.in_focus) /* gvim in focus */
4870 {
4871 /* Don't move the mouse when it's left or right of the Vim window */
4872 if (x < 0 || x > Columns * gui.char_width)
4873 return;
4874#ifndef FEAT_MOUSESHAPE
4875 wp = xy2win(x, y);
4876#endif
4877 if (wp == curwin || wp == NULL)
4878 return; /* still in the same old window, or none at all */
4879
Bram Moolenaar9c102382006-05-03 21:26:49 +00004880 /* Ignore position in the tab pages line. */
4881 if (Y_2_ROW(y) < tabline_height())
4882 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 /*
4885 * format a mouse click on status line input
4886 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004887 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4888 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 */
4890 if (finish_op)
4891 {
4892 /* abort the current operator first */
4893 st[0] = ESC;
4894 add_to_input_buf(st, 1);
4895 }
4896 st[0] = CSI;
4897 st[1] = KS_MOUSE;
4898 st[2] = KE_FILLER;
4899 st[3] = (char_u)MOUSE_LEFT;
4900 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004901 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 wp->w_height + W_WINROW(wp));
4903
4904 add_to_input_buf(st, 8);
4905 st[3] = (char_u)MOUSE_RELEASE;
4906 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#ifdef FEAT_GUI_GTK
4908 /* Need to wake up the main loop */
4909 if (gtk_main_level() > 0)
4910 gtk_main_quit();
4911#endif
4912 }
4913}
4914
4915/*
4916 * Called when mouse should be moved to window with focus.
4917 */
4918 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004919gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 int x, y;
4922 win_T *wp = NULL;
4923
4924 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004925
4926 if (!(gui.in_use && p_mousef))
4927 return;
4928
4929 gui_mch_getmouse(&x, &y);
4930 /* Don't move the mouse when it's left or right of the Vim window */
4931 if (x < 0 || x > Columns * gui.char_width)
4932 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004933 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004934 wp = xy2win(x, y);
4935 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004937 validate_cline_row();
4938 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4939 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942}
4943
4944/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004945 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004948xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 int row;
4951 int col;
4952 win_T *wp;
4953
4954 row = Y_2_ROW(y);
4955 col = X_2_COL(x);
4956 if (row < 0 || col < 0) /* before first window */
4957 return NULL;
4958 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004959 if (wp == NULL)
4960 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004961#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (State == HITRETURN || State == ASKMORE)
4963 {
4964 if (Y_2_ROW(y) >= msg_row)
4965 update_mouseshape(SHAPE_IDX_MOREL);
4966 else
4967 update_mouseshape(SHAPE_IDX_MORE);
4968 }
4969 else if (row > wp->w_height) /* below status line */
4970 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004971 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004972 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004974 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004975 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 update_mouseshape(SHAPE_IDX_STATUS);
4977 else
4978 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004980 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981}
4982
4983/*
4984 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4985 * File names may be given to redefine the args list.
4986 */
4987 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004988ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 char_u *arg = eap->arg;
4991
4992 /*
4993 * Check for "-f" argument: foreground, don't fork.
4994 * Also don't fork when started with "gvim -f".
4995 * Do fork when using "gui -b".
4996 */
4997 if (arg[0] == '-'
4998 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004999 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 gui.dofork = (arg[1] == 'b');
5002 eap->arg = skipwhite(eap->arg + 2);
5003 }
5004 if (!gui.in_use)
5005 {
5006 /* Clear the command. Needed for when forking+exiting, to avoid part
5007 * of the argument ending up after the shell prompt. */
5008 msg_clr_eos_force();
5009 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005010#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005011 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 if (!ends_excmd(*eap->arg))
5015 ex_next(eap);
5016}
5017
5018#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00005019 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020/*
5021 * This is shared between Athena, Motif and GTK.
5022 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005023static void gfp_setname(char_u *fname, void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025/*
5026 * Callback function for do_in_runtimepath().
5027 */
5028 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005029gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031 char_u *gfp_buffer = cookie;
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (STRLEN(fname) >= MAXPATHL)
5034 *gfp_buffer = NUL;
5035 else
5036 STRCPY(gfp_buffer, fname);
5037}
5038
5039/*
5040 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5041 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5042 */
5043 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005044gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046 if (STRLEN(name) > MAXPATHL - 14)
5047 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005048 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005049 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005050 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 return FAIL;
5052 return OK;
5053}
5054
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005055# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056/*
5057 * Given the name of the "icon=" argument, try finding the bitmap file for the
5058 * icon. If it is an absolute path name, use it as it is. Otherwise append
5059 * "ext" and search for it in 'runtimepath'.
5060 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5061 * contains "name".
5062 */
5063 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005064gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 char_u buf[MAXPATHL + 1];
5067
5068 expand_env(name, buffer, MAXPATHL);
5069 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5070 STRCPY(buffer, buf);
5071}
5072# endif
5073#endif
5074
Bram Moolenaar9372a112005-12-06 19:59:18 +00005075#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005077display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 char_u *p;
5080
5081 if (isatty(2))
5082 fflush(stderr);
5083 else if (error_ga.ga_data != NULL)
5084 {
5085 /* avoid putting up a message box with blanks only */
5086 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5087 if (!isspace(*p))
5088 {
5089 /* Truncate a very long message, it will go off-screen. */
5090 if (STRLEN(p) > 2000)
5091 STRCPY(p + 2000 - 14, "...(truncated)");
5092 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005093 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095 }
5096 ga_clear(&error_ga);
5097 }
5098}
5099#endif
5100
5101#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5102/*
5103 * Return TRUE if still starting up and there is no place to enter text.
5104 * For GTK and X11 we check if stderr is not a tty, which means we were
5105 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5106 * allow typing on stdin.
5107 */
5108 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005109no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
5111 return ((!gui.in_use || gui.starting)
5112# ifndef NO_CONSOLE
5113 && !isatty(0) && !isatty(2)
5114# endif
5115 );
5116}
5117#endif
5118
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005119#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005120 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005121 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122/*
5123 * Update the current window and the screen.
5124 */
5125 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005126gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005128# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005129 linenr_T conceal_old_cursor_line = 0;
5130 linenr_T conceal_new_cursor_line = 0;
5131 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005132# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005133
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 update_topline();
5135 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005136
Bram Moolenaarec80df72008-05-07 19:46:51 +00005137 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005138 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005139# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005140 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005141# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005142 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005143 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144 if (has_cursormoved())
5145 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005146# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005147 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005148 {
5149 conceal_old_cursor_line = last_cursormoved.lnum;
5150 conceal_new_cursor_line = curwin->w_cursor.lnum;
5151 conceal_update_lines = TRUE;
5152 }
5153# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005154 last_cursormoved = curwin->w_cursor;
5155 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 update_screen(0); /* may need to update the screen */
5158 setcursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005159# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005160 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005161 && (conceal_old_cursor_line != conceal_new_cursor_line
5162 || conceal_cursor_line(curwin)
5163 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005164 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005165 if (conceal_old_cursor_line != conceal_new_cursor_line)
5166 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005167 update_single_line(curwin, conceal_new_cursor_line);
5168 curwin->w_valid &= ~VALID_CROW;
5169 }
5170# endif
Bram Moolenaara338adc2018-01-31 20:51:47 +01005171 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172}
5173#endif
5174
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005175#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176/*
5177 * Get the text to use in a find/replace dialog. Uses the last search pattern
5178 * if the argument is empty.
5179 * Returns an allocated string.
5180 */
5181 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005182get_find_dialog_text(
5183 char_u *arg,
5184 int *wwordp, /* return: TRUE if \< \> found */
5185 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 char_u *text;
5188
5189 if (*arg == NUL)
5190 text = last_search_pat();
5191 else
5192 text = arg;
5193 if (text != NULL)
5194 {
5195 text = vim_strsave(text);
5196 if (text != NULL)
5197 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005198 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 int i;
5200
5201 /* Remove "\V" */
5202 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5203 {
5204 mch_memmove(text, text + 2, (size_t)(len - 1));
5205 len -= 2;
5206 }
5207
5208 /* Recognize "\c" and "\C" and remove. */
5209 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5210 {
5211 *mcasep = (text[1] == 'C');
5212 mch_memmove(text, text + 2, (size_t)(len - 1));
5213 len -= 2;
5214 }
5215
5216 /* Recognize "\<text\>" and remove. */
5217 if (len >= 4
5218 && STRNCMP(text, "\\<", 2) == 0
5219 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5220 {
5221 *wwordp = TRUE;
5222 mch_memmove(text, text + 2, (size_t)(len - 4));
5223 text[len - 4] = NUL;
5224 }
5225
5226 /* Recognize "\/" or "\?" and remove. */
5227 for (i = 0; i + 1 < len; ++i)
5228 if (text[i] == '\\' && (text[i + 1] == '/'
5229 || text[i + 1] == '?'))
5230 {
5231 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5232 --len;
5233 }
5234 }
5235 }
5236 return text;
5237}
5238
5239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 * Handle the press of a button in the find-replace dialog.
5241 * Return TRUE when something was added to the input buffer.
5242 */
5243 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005244gui_do_findrepl(
5245 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5246 char_u *find_text,
5247 char_u *repl_text,
5248 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
5250 garray_T ga;
5251 int i;
5252 int type = (flags & FRD_TYPE_MASK);
5253 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005254 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005255 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005256 static int busy = FALSE;
5257
5258 /* When the screen is being updated we should not change buffers and
5259 * windows structures, it may cause freed memory to be used. Also don't
5260 * do this recursively (pressing "Find" quickly several times. */
5261 if (updating_screen || busy)
5262 return FALSE;
5263
5264 /* refuse replace when text cannot be changed */
5265 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5266 return FALSE;
5267
5268 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005271 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 ga_concat(&ga, (char_u *)"%s/");
5273
5274 ga_concat(&ga, (char_u *)"\\V");
5275 if (flags & FRD_MATCH_CASE)
5276 ga_concat(&ga, (char_u *)"\\C");
5277 else
5278 ga_concat(&ga, (char_u *)"\\c");
5279 if (flags & FRD_WHOLE_WORD)
5280 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005281 /* escape / and \ */
5282 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5283 if (p != NULL)
5284 ga_concat(&ga, p);
5285 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (flags & FRD_WHOLE_WORD)
5287 ga_concat(&ga, (char_u *)"\\>");
5288
5289 if (type == FRD_REPLACEALL)
5290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005292 /* escape / and \ */
5293 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5294 if (p != NULL)
5295 ga_concat(&ga, p);
5296 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005298 }
5299 ga_append(&ga, NUL);
5300
5301 if (type == FRD_REPLACE)
5302 {
5303 /* Do the replacement when the text at the cursor matches. Thus no
5304 * replacement is done if the cursor was moved! */
5305 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5306 regmatch.rm_ic = 0;
5307 if (regmatch.regprog != NULL)
5308 {
5309 p = ml_get_cursor();
5310 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5311 && regmatch.startp[0] == p)
5312 {
5313 /* Clear the command line to remove any old "No match"
5314 * error. */
5315 msg_end_prompt();
5316
5317 if (u_save_cursor() == OK)
5318 {
5319 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005320 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005321
5322 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005323 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005324 ins_str(repl_text);
5325 }
5326 }
5327 else
5328 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005329 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005330 }
5331 }
5332
5333 if (type == FRD_REPLACEALL)
5334 {
5335 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005336 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 do_cmdline_cmd(ga.ga_data);
5338 }
5339 else
5340 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005341 int searchflags = SEARCH_MSG + SEARCH_MARK;
5342
5343 /* Search for the next match.
5344 * Don't skip text under cursor for single replace. */
5345 if (type == FRD_REPLACE)
5346 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005348 if (down)
5349 {
5350 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5351 }
5352 else
5353 {
5354 /* We need to escape '?' if and only if we are searching in the up
5355 * direction */
5356 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5357 if (p != NULL)
5358 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5359 vim_free(p);
5360 }
5361
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 msg_scroll = i; /* don't let an error message set msg_scroll */
5363 }
5364
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005365 /* Don't want to pass did_emsg to other code, it may cause disabling
5366 * syntax HL if we were busy redrawing. */
5367 did_emsg = save_did_emsg;
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 if (State & (NORMAL | INSERT))
5370 {
5371 gui_update_screen(); /* update the screen */
5372 msg_didout = 0; /* overwrite any message */
5373 need_wait_return = FALSE; /* don't wait for return */
5374 }
5375
5376 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005377 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return (ga.ga_len > 0);
5379}
5380
5381#endif
5382
5383#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5384 || defined(FEAT_GUI_MSWIN) \
5385 || defined(FEAT_GUI_MAC) \
5386 || defined(PROTO)
5387
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005388static void gui_wingoto_xy(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390/*
5391 * Jump to the window at specified point (x, y).
5392 */
5393 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005394gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 int row = Y_2_ROW(y);
5397 int col = X_2_COL(x);
5398 win_T *wp;
5399
5400 if (row >= 0 && col >= 0)
5401 {
5402 wp = mouse_find_win(&row, &col);
5403 if (wp != NULL && wp != curwin)
5404 win_goto(wp);
5405 }
5406}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408/*
5409 * Process file drop. Mouse cursor position, key modifiers, name of files
5410 * and count of files are given. Argument "fnames[count]" has full pathnames
5411 * of dropped files, they will be freed in this function, and caller can't use
5412 * fnames after call this function.
5413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005415gui_handle_drop(
5416 int x UNUSED,
5417 int y UNUSED,
5418 int_u modifiers,
5419 char_u **fnames,
5420 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 int i;
5423 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005424 static int entered = FALSE;
5425
5426 /*
5427 * This function is called by event handlers. Just in case we get a
5428 * second event before the first one is handled, ignore the second one.
5429 * Not sure if this can ever happen, just in case.
5430 */
5431 if (entered)
5432 return;
5433 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435 /*
5436 * When the cursor is at the command line, add the file names to the
5437 * command line, don't edit the files.
5438 */
5439 if (State & CMDLINE)
5440 {
5441 shorten_filenames(fnames, count);
5442 for (i = 0; i < count; ++i)
5443 {
5444 if (fnames[i] != NULL)
5445 {
5446 if (i > 0)
5447 add_to_input_buf((char_u*)" ", 1);
5448
5449 /* We don't know what command is used thus we can't be sure
5450 * about which characters need to be escaped. Only escape the
5451 * most common ones. */
5452# ifdef BACKSLASH_IN_FILENAME
5453 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5454# else
5455 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5456# endif
5457 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005458 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 vim_free(p);
5460 vim_free(fnames[i]);
5461 }
5462 }
5463 vim_free(fnames);
5464 }
5465 else
5466 {
5467 /* Go to the window under mouse cursor, then shorten given "fnames" by
5468 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 shorten_filenames(fnames, count);
5471
5472 /* If Shift held down, remember the first item. */
5473 if ((modifiers & MOUSE_SHIFT) != 0)
5474 p = vim_strsave(fnames[0]);
5475 else
5476 p = NULL;
5477
5478 /* Handle the drop, :edit or :split to get to the file. This also
5479 * frees fnames[]. Skip this if there is only one item it's a
5480 * directory and Shift is held down. */
5481 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5482 && mch_isdir(fnames[0]))
5483 {
5484 vim_free(fnames[0]);
5485 vim_free(fnames);
5486 }
5487 else
5488 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5489
5490 /* If Shift held down, change to first file's directory. If the first
5491 * item is a directory, change to that directory (and let the explorer
5492 * plugin show the contents). */
5493 if (p != NULL)
5494 {
5495 if (mch_isdir(p))
5496 {
5497 if (mch_chdir((char *)p) == 0)
5498 shorten_fnames(TRUE);
5499 }
Bram Moolenaarf4aba792018-02-03 19:17:36 +01005500 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 shorten_fnames(TRUE);
5502 vim_free(p);
5503 }
5504
5505 /* Update the screen display */
5506 update_screen(NOT_VALID);
5507# ifdef FEAT_MENU
5508 gui_update_menus(0);
5509# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005510#ifdef FEAT_TITLE
5511 maketitle();
5512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005514 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005516
5517 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518}
5519#endif