blob: 15adb1f1dff20e591b4fd8510cdc278cd0bdb5da [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)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200748 {
749# ifdef FEAT_VARTABS
750 vim_free(balloonEval->vts);
751# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100752 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200753 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100754 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000755# ifdef FEAT_GUI_GTK
756 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
757 &general_beval_cb, NULL);
758# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000759# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000760 {
761 extern Widget textArea;
762 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000763 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000764 }
765# else
766# ifdef FEAT_GUI_W32
767 balloonEval = gui_mch_create_beval_area(NULL, NULL,
768 &general_beval_cb, NULL);
769# endif
770# endif
771# endif
772 if (!p_beval)
773 gui_mch_disable_beval_area(balloonEval);
774#endif
775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
777 if (!im_xim_isvalid_imactivate())
778 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
779#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000780 /* When 'cmdheight' was set during startup it may not have taken
781 * effect yet. */
782 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000783 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
785 return;
786 }
787
788error2:
789#ifdef FEAT_GUI_X11
790 /* undo gui_mch_init() */
791 gui_mch_uninit();
792#endif
793
794error:
795 gui.in_use = FALSE;
796 clip_init(FALSE);
797}
798
799
800 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100801gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* don't free the fonts, it leads to a BUS error
804 * richard@whitequeen.com Jul 99 */
805 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 gui.in_use = FALSE;
807 gui_mch_exit(rc);
808}
809
Bram Moolenaar9372a112005-12-06 19:59:18 +0000810#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000812# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
814 * Called when the GUI shell is closed by the user. If there are no changed
815 * files Vim exits, otherwise there will be a dialog to ask the user what to
816 * do.
817 * When this function returns, Vim should NOT exit!
818 */
819 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100820gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 cmdmod_T save_cmdmod;
823
824 save_cmdmod = cmdmod;
825
826 /* Only exit when there are no changed files */
827 exiting = TRUE;
828# ifdef FEAT_BROWSE
829 cmdmod.browse = TRUE;
830# endif
831# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
832 cmdmod.confirm = TRUE;
833# endif
834 /* If there are changed buffers, present the user with a dialog if
835 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100836 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 getout(0);
838
839 exiting = FALSE;
840 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000841 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843#endif
844
845/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200846 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * first font name that works is used. If none is found, use the default
848 * font.
849 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
850 * Return OK when able to set the font. When it failed FAIL is returned and
851 * the fonts are unchanged.
852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100854gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856#define FONTLEN 320
857 char_u font_name[FONTLEN];
858 int font_list_empty = FALSE;
859 int ret = FAIL;
860
861 if (!gui.in_use)
862 return FAIL;
863
864 font_name[0] = NUL;
865 if (*font_list == NUL)
866 font_list_empty = TRUE;
867 else
868 {
869#ifdef FEAT_XFONTSET
870 /* When using a fontset, the whole list of fonts is one name. */
871 if (fontset)
872 ret = gui_mch_init_font(font_list, TRUE);
873 else
874#endif
875 while (*font_list != NUL)
876 {
877 /* Isolate one comma separated font name. */
878 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
879
880 /* Careful!!! The Win32 version of gui_mch_init_font(), when
881 * called with "*" will change p_guifont to the selected font
882 * name, which frees the old value. This makes font_list
883 * invalid. Thus when OK is returned here, font_list must no
884 * longer be used! */
885 if (gui_mch_init_font(font_name, FALSE) == OK)
886 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200887#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 /* If it's a Unicode font, try setting 'guifontwide' to a
889 * similar double-width font. */
890 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
891 && strstr((char *)font_name, "10646") != NULL)
892 set_guifontwide(font_name);
893#endif
894 ret = OK;
895 break;
896 }
897 }
898 }
899
900 if (ret != OK
901 && STRCMP(font_list, "*") != 0
902 && (font_list_empty || gui.norm_font == NOFONT))
903 {
904 /*
905 * Couldn't load any font in 'font_list', keep the current font if
906 * there is one. If 'font_list' is empty, or if there is no current
907 * font, tell gui_mch_init_font() to try to find a font we can load.
908 */
909 ret = gui_mch_init_font(NULL, FALSE);
910 }
911
912 if (ret == OK)
913 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200914#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 /* Set normal font as current font */
916# ifdef FEAT_XFONTSET
917 if (gui.fontset != NOFONTSET)
918 gui_mch_set_fontset(gui.fontset);
919 else
920# endif
921 gui_mch_set_font(gui.norm_font);
922#endif
Bram Moolenaar8ac44152017-11-09 18:33:29 +0100923 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925
926 return ret;
927}
928
929#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200930# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931/*
932 * Try setting 'guifontwide' to a font twice as wide as "name".
933 */
934 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100935set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 int i = 0;
938 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
939 char_u *wp = NULL;
940 char_u *p;
941 GuiFont font;
942
943 wp = wide_name;
944 for (p = name; *p != NUL; ++p)
945 {
946 *wp++ = *p;
947 if (*p == '-')
948 {
949 ++i;
950 if (i == 6) /* font type: change "--" to "-*-" */
951 {
952 if (p[1] == '-')
953 *wp++ = '*';
954 }
955 else if (i == 12) /* found the width */
956 {
957 ++p;
958 i = getdigits(&p);
959 if (i != 0)
960 {
961 /* Double the width specification. */
962 sprintf((char *)wp, "%d%s", i * 2, p);
963 font = gui_mch_get_font(wide_name, FALSE);
964 if (font != NOFONT)
965 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000966 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 gui.wide_font = font;
968 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000969 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 }
972 break;
973 }
974 }
975 }
976}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200977# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979/*
980 * Get the font for 'guifontwide'.
981 * Return FAIL for an invalid font name.
982 */
983 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100984gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 GuiFont font = NOFONT;
987 char_u font_name[FONTLEN];
988 char_u *p;
989
990 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
991 return OK; /* Will give an error message later. */
992
993 if (p_guifontwide != NULL && *p_guifontwide != NUL)
994 {
995 for (p = p_guifontwide; *p != NUL; )
996 {
997 /* Isolate one comma separated font name. */
998 (void)copy_option_part(&p, font_name, FONTLEN, ",");
999 font = gui_mch_get_font(font_name, FALSE);
1000 if (font != NOFONT)
1001 break;
1002 }
1003 if (font == NOFONT)
1004 return FAIL;
1005 }
1006
1007 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001008# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1010 if (font != NOFONT && gui.norm_font != NOFONT
1011 && pango_font_description_equal(font, gui.norm_font))
1012 {
1013 gui.wide_font = NOFONT;
1014 gui_mch_free_font(font);
1015 }
1016 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001019# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001020 gui_mch_wide_font_changed();
Bram Moolenaardb250522013-06-17 22:43:25 +02001021# else
1022 /*
1023 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1024 * support those fonts for 'guifontwide'.
1025 */
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001026# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 return OK;
1028}
1029#endif
1030
1031 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001032gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 gui.row = row;
1035 gui.col = col;
1036}
1037
1038/*
1039 * gui_check_pos - check if the cursor is on the screen.
1040 */
1041 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001042gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 if (gui.row >= screen_Rows)
1045 gui.row = screen_Rows - 1;
1046 if (gui.col >= screen_Columns)
1047 gui.col = screen_Columns - 1;
1048 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1049 gui.cursor_is_valid = FALSE;
1050}
1051
1052/*
1053 * Redraw the cursor if necessary or when forced.
1054 * Careful: The contents of ScreenLines[] must match what is on the screen,
1055 * otherwise this goes wrong. May need to call out_flush() first.
1056 */
1057 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001058gui_update_cursor(
1059 int force, /* when TRUE, update even when not moved */
1060 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
1062 int cur_width = 0;
1063 int cur_height = 0;
1064 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001065 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001067#ifdef FEAT_TERMINAL
1068 guicolor_T shape_fg = INVALCOLOR;
1069 guicolor_T shape_bg = INVALCOLOR;
1070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1072 int cattr; /* cursor attributes */
1073 int attr;
1074 attrentry_T *aep = NULL;
1075
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001076 /* Don't update the cursor when halfway busy scrolling or the screen size
1077 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1078 if (!can_update_cursor || screen_Columns != gui.num_cols
1079 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 return;
1081
1082 gui_check_pos();
1083 if (!gui.cursor_is_valid || force
1084 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1085 {
1086 gui_undraw_cursor();
1087 if (gui.row < 0)
1088 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001089#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1091 im_set_position(gui.row, gui.col);
1092#endif
1093 gui.cursor_row = gui.row;
1094 gui.cursor_col = gui.col;
1095
1096 /* Only write to the screen after ScreenLines[] has been initialized */
1097 if (!screen_cleared || ScreenLines == NULL)
1098 return;
1099
1100 /* Clear the selection if we are about to write over it */
1101 if (clear_selection)
1102 clip_may_clear_selection(gui.row, gui.row);
1103 /* Check that the cursor is inside the shell (resizing may have made
1104 * it invalid) */
1105 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1106 return;
1107
1108 gui.cursor_is_valid = TRUE;
1109
1110 /*
1111 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001112 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001114#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001115 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001116 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001118#endif
1119 shape = &shape_table[get_shape_idx(FALSE)];
1120 if (State & LANGMAP)
1121 id = shape->id_lm;
1122 else
1123 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 /* get the colors and attributes for the cursor. Default is inverted */
1126 cfg = INVALCOLOR;
1127 cbg = INVALCOLOR;
1128 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001129 gui_mch_set_blinking(shape->blinkwait,
1130 shape->blinkon,
1131 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001132 if (shape->blinkwait == 0 || shape->blinkon == 0
1133 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001134 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001135#ifdef FEAT_TERMINAL
1136 if (shape_bg != INVALCOLOR)
1137 {
1138 cattr = 0;
1139 cfg = shape_fg;
1140 cbg = shape_bg;
1141 }
1142 else
1143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (id > 0)
1145 {
1146 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001147#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 static int iid;
1150 guicolor_T fg, bg;
1151
Bram Moolenaarc236c162008-07-13 17:41:49 +00001152 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001153# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001154 preedit_get_status()
1155# else
1156 im_get_status()
1157# endif
1158 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 iid = syn_name2id((char_u *)"CursorIM");
1161 if (iid > 0)
1162 {
1163 syn_id2colors(iid, &fg, &bg);
1164 if (bg != INVALCOLOR)
1165 cbg = bg;
1166 if (fg != INVALCOLOR)
1167 cfg = fg;
1168 }
1169 }
1170 }
1171#endif
1172 }
1173
1174 /*
1175 * Get the attributes for the character under the cursor.
1176 * When no cursor color was given, use the character color.
1177 */
1178 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1179 if (attr > HL_ALL)
1180 aep = syn_gui_attr2entry(attr);
1181 if (aep != NULL)
1182 {
1183 attr = aep->ae_attr;
1184 if (cfg == INVALCOLOR)
1185 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1186 : aep->ae_u.gui.fg_color);
1187 if (cbg == INVALCOLOR)
1188 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1189 : aep->ae_u.gui.bg_color);
1190 }
1191 if (cfg == INVALCOLOR)
1192 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1193 if (cbg == INVALCOLOR)
1194 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1195
1196#ifdef FEAT_XIM
1197 if (aep != NULL)
1198 {
1199 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1200 : aep->ae_u.gui.bg_color);
1201 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1202 : aep->ae_u.gui.fg_color);
1203 if (xim_bg_color == INVALCOLOR)
1204 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1205 : gui.back_pixel;
1206 if (xim_fg_color == INVALCOLOR)
1207 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1208 : gui.norm_pixel;
1209 }
1210 else
1211 {
1212 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1213 : gui.back_pixel;
1214 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1215 : gui.norm_pixel;
1216 }
1217#endif
1218
1219 attr &= ~HL_INVERSE;
1220 if (cattr & HL_INVERSE)
1221 {
1222 cc = cbg;
1223 cbg = cfg;
1224 cfg = cc;
1225 }
1226 cattr &= ~HL_INVERSE;
1227
1228 /*
1229 * When we don't have window focus, draw a hollow cursor.
1230 */
1231 if (!gui.in_focus)
1232 {
1233 gui_mch_draw_hollow_cursor(cbg);
1234 return;
1235 }
1236
1237 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001238 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#ifdef FEAT_HANGULIN
1240 || composing_hangul
1241#endif
1242 )
1243 {
1244 /*
1245 * Draw the text character with the cursor colors. Use the
1246 * character attributes plus the cursor attributes.
1247 */
1248 gui.highlight_mask = (cattr | attr);
1249#ifdef FEAT_HANGULIN
1250 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001251 {
1252 char_u *comp_buf;
1253 int comp_len;
1254
1255 comp_buf = hangul_composing_buffer_get(&comp_len);
1256 if (comp_buf)
1257 {
1258 (void)gui_outstr_nowrap(comp_buf, comp_len,
1259 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1260 cfg, cbg, 0);
1261 vim_free(comp_buf);
1262 }
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 else
1265#endif
1266 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1267 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1268 }
1269 else
1270 {
1271#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1272 int col_off = FALSE;
1273#endif
1274 /*
1275 * First draw the partial cursor, then overwrite with the text
1276 * character, using a transparent background.
1277 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001278 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001281 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
1284 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001285 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 cur_width = gui.char_width;
1287 }
1288#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001289 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1290 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 {
1292 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001293 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 cur_width += gui.char_width;
1295# ifdef FEAT_RIGHTLEFT
1296 if (CURSOR_BAR_RIGHT)
1297 {
1298 /* gui.col points to the left halve of the character but
1299 * the vertical line needs to be on the right halve.
1300 * A double-wide horizontal line is also drawn from the
1301 * right halve in gui_mch_draw_part_cursor(). */
1302 col_off = TRUE;
1303 ++gui.col;
1304 }
1305# endif
1306 }
1307#endif
1308 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1309#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1310 if (col_off)
1311 --gui.col;
1312#endif
1313
1314#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1315 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1316 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1317 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1318 (guicolor_T)0, (guicolor_T)0, 0);
1319#endif
1320 }
1321 gui.highlight_mask = old_hl_mask;
1322 }
1323}
1324
1325#if defined(FEAT_MENU) || defined(PROTO)
1326 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001327gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001329# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (gui.menu_is_active && gui.in_use)
1331 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1332# endif
1333}
1334#endif
1335
1336/*
1337 * Position the various GUI components (text area, menu). The vertical
1338 * scrollbars are NOT handled here. See gui_update_scrollbars().
1339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001341gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 int text_area_x;
1344 int text_area_y;
1345 int text_area_width;
1346 int text_area_height;
1347
1348 /* avoid that moving components around generates events */
1349 ++hold_gui_events;
1350
1351 text_area_x = 0;
1352 if (gui.which_scrollbars[SBAR_LEFT])
1353 text_area_x += gui.scrollbar_width;
1354
1355 text_area_y = 0;
1356#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1357 gui.menu_width = total_width;
1358 if (gui.menu_is_active)
1359 text_area_y += gui.menu_height;
1360#endif
1361#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1362 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1363 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1364#endif
1365
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001366# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001367 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001368 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001369 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001370#endif
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1373 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1374 {
1375# ifdef FEAT_GUI_ATHENA
1376 gui_mch_set_toolbar_pos(0, text_area_y,
1377 gui.menu_width, gui.toolbar_height);
1378# endif
1379 text_area_y += gui.toolbar_height;
1380 }
1381#endif
1382
1383 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1384 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1385
1386 gui_mch_set_text_area_pos(text_area_x,
1387 text_area_y,
1388 text_area_width,
1389 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001390#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 + xim_get_status_area_height()
1392#endif
1393 );
1394#ifdef FEAT_MENU
1395 gui_position_menu();
1396#endif
1397 if (gui.which_scrollbars[SBAR_BOTTOM])
1398 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1399 text_area_x,
1400 text_area_y + text_area_height,
1401 text_area_width,
1402 gui.scrollbar_height);
1403 gui.left_sbar_x = 0;
1404 gui.right_sbar_x = text_area_x + text_area_width;
1405
1406 --hold_gui_events;
1407}
1408
Bram Moolenaar02743632005-07-25 20:42:36 +00001409/*
1410 * Get the width of the widgets and decorations to the side of the text area.
1411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001413gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 int base_width;
1416
1417 base_width = 2 * gui.border_offset;
1418 if (gui.which_scrollbars[SBAR_LEFT])
1419 base_width += gui.scrollbar_width;
1420 if (gui.which_scrollbars[SBAR_RIGHT])
1421 base_width += gui.scrollbar_width;
1422 return base_width;
1423}
1424
Bram Moolenaar02743632005-07-25 20:42:36 +00001425/*
1426 * Get the height of the widgets and decorations above and below the text area.
1427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001429gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 int base_height;
1432
1433 base_height = 2 * gui.border_offset;
1434 if (gui.which_scrollbars[SBAR_BOTTOM])
1435 base_height += gui.scrollbar_height;
1436#ifdef FEAT_GUI_GTK
1437 /* We can't take the sizes properly into account until anything is
1438 * realized. Therefore we recalculate all the values here just before
1439 * setting the size. (--mdcki) */
1440#else
1441# ifdef FEAT_MENU
1442 if (gui.menu_is_active)
1443 base_height += gui.menu_height;
1444# endif
1445# ifdef FEAT_TOOLBAR
1446 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1447# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1448 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1449# else
1450 base_height += gui.toolbar_height;
1451# endif
1452# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001453# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1454 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001455 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001456 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458# ifdef FEAT_FOOTER
1459 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1460 base_height += gui.footer_height;
1461# endif
1462# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1463 base_height += gui_mch_text_area_extra_height();
1464# endif
1465#endif
1466 return base_height;
1467}
1468
1469/*
1470 * Should be called after the GUI shell has been resized. Its arguments are
1471 * the new width and height of the shell in pixels.
1472 */
1473 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001474gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 static int busy = FALSE;
1477
1478 if (!gui.shell_created) /* ignore when still initializing */
1479 return;
1480
1481 /*
1482 * Can't resize the screen while it is being redrawn. Remember the new
1483 * size and handle it later.
1484 */
1485 if (updating_screen || busy)
1486 {
1487 new_pixel_width = pixel_width;
1488 new_pixel_height = pixel_height;
1489 return;
1490 }
1491
1492again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001493 new_pixel_width = 0;
1494 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 busy = TRUE;
1496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 /* Flush pending output before redrawing */
1498 out_flush();
1499
1500 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001501 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
1503 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 /*
1507 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1508 * at the last line here (why does it have to be one row too low?).
1509 */
1510 if (State == ASKMORE || State == CONFIRM)
1511 gui.row = gui.num_rows;
1512
1513 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1514 * the safe side. */
1515 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1516 || gui.num_rows != Rows || gui.num_cols != Columns)
1517 shell_resized();
1518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 gui_update_scrollbars(TRUE);
1520 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001521#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 xim_set_status_area();
1523#endif
1524
1525 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001526
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001527 /* We may have been called again while redrawing the screen.
1528 * Need to do it all again with the latest size then. But only if the size
1529 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (new_pixel_height)
1531 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001532 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1533 {
1534 new_pixel_width = 0;
1535 new_pixel_height = 0;
1536 }
1537 else
1538 {
1539 pixel_width = new_pixel_width;
1540 pixel_height = new_pixel_height;
1541 goto again;
1542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544}
1545
1546/*
1547 * Check if gui_resize_shell() must be called.
1548 */
1549 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001550gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 /* careful: gui_resize_shell() may postpone the resize again if we
1554 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001555 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556}
1557
1558 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001559gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 Rows = gui.num_rows;
1562 Columns = gui.num_cols;
1563 return OK;
1564}
1565
1566/*
1567 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001568 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1569 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001570 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1571 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001574gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001575 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001576 int fit_to_display,
1577 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 int base_width;
1580 int base_height;
1581 int width;
1582 int height;
1583 int min_width;
1584 int min_height;
1585 int screen_w;
1586 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001587#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001588 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001589 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001590#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001591 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 if (!gui.shell_created)
1594 return;
1595
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001596#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 /* If not setting to a user specified size and maximized, calculate the
1598 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001599 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1600 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 gui_mch_newfont();
1603 return;
1604 }
1605#endif
1606
1607 base_width = gui_get_base_width();
1608 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001609 if (fit_to_display)
1610 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001611 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#ifdef USE_SUN_WORKSHOP
1614 if (!mustset && usingSunWorkShop
1615 && workshop_get_width_height(&width, &height))
1616 {
1617 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1618 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1619 }
1620 else
1621#endif
1622 {
1623 width = Columns * gui.char_width + base_width;
1624 height = Rows * gui.char_height + base_height;
1625 }
1626
1627 if (fit_to_display)
1628 {
1629 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001630 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 Columns = (screen_w - base_width) / gui.char_width;
1633 if (Columns < MIN_COLUMNS)
1634 Columns = MIN_COLUMNS;
1635 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001636#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001637 ++did_adjust;
1638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001640 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 Rows = (screen_h - base_height) / gui.char_height;
1643 check_shellsize();
1644 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001645#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001646 ++did_adjust;
1647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001649#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001650 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1651 && height + gui.char_height >= screen_h))
1652 /* don't unmaximize if at maximum size */
1653 un_maximize = FALSE;
1654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001656 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 gui.num_cols = Columns;
1658 gui.num_rows = Rows;
1659
1660 min_width = base_width + MIN_COLUMNS * gui.char_width;
1661 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001662 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001663
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001664#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001665 if (un_maximize)
1666 {
1667 /* If the window size is smaller than the screen unmaximize the
1668 * window, otherwise resizing won't work. */
1669 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1670 if ((width + gui.char_width < screen_w
1671 || height + gui.char_height * 2 < screen_h)
1672 && gui_mch_maximized())
1673 gui_mch_unmaximize();
1674 }
1675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001678 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001680 if (fit_to_display && x >= 0 && y >= 0)
1681 {
1682 /* Some window managers put the Vim window left of/above the screen.
1683 * Only change the position if it wasn't already negative before
1684 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 gui_mch_update();
1686 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1687 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1688 }
1689
1690 gui_position_components(width);
1691 gui_update_scrollbars(TRUE);
1692 gui_reset_scroll_region();
1693}
1694
1695/*
1696 * Called when Rows and/or Columns has changed.
1697 */
1698 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001699gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 gui_reset_scroll_region();
1702}
1703
1704/*
1705 * Make scroll region cover whole screen.
1706 */
1707 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001708gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 gui.scroll_region_top = 0;
1711 gui.scroll_region_bot = gui.num_rows - 1;
1712 gui.scroll_region_left = 0;
1713 gui.scroll_region_right = gui.num_cols - 1;
1714}
1715
1716 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001717gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 if (mask > HL_ALL) /* highlight code */
1720 gui.highlight_mask = mask;
1721 else /* mask */
1722 gui.highlight_mask |= mask;
1723}
1724
1725 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001726gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 if (mask > HL_ALL) /* highlight code */
1729 gui.highlight_mask = HL_NORMAL;
1730 else /* mask */
1731 gui.highlight_mask &= ~mask;
1732}
1733
1734/*
1735 * Clear a rectangular region of the screen from text pos (row1, col1) to
1736 * (row2, col2) inclusive.
1737 */
1738 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001739gui_clear_block(
1740 int row1,
1741 int col1,
1742 int row2,
1743 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
1745 /* Clear the selection if we are about to write over it */
1746 clip_may_clear_selection(row1, row2);
1747
1748 gui_mch_clear_block(row1, col1, row2, col2);
1749
1750 /* Invalidate cursor if it was in this block */
1751 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1752 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1753 gui.cursor_is_valid = FALSE;
1754}
1755
1756/*
1757 * Write code to update the cursor later. This avoids the need to flush the
1758 * output buffer before calling gui_update_cursor().
1759 */
1760 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001761gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001763 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
1766 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001767gui_write(
1768 char_u *s,
1769 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771 char_u *p;
1772 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 int force_scrollbar = FALSE;
1775 static win_T *old_curwin = NULL;
1776
1777/* #define DEBUG_GUI_WRITE */
1778#ifdef DEBUG_GUI_WRITE
1779 {
1780 int i;
1781 char_u *str;
1782
1783 printf("gui_write(%d):\n ", len);
1784 for (i = 0; i < len; i++)
1785 if (s[i] == ESC)
1786 {
1787 if (i != 0)
1788 printf("\n ");
1789 printf("<ESC>");
1790 }
1791 else
1792 {
1793 str = transchar_byte(s[i]);
1794 if (str[0] && str[1])
1795 printf("<%s>", (char *)str);
1796 else
1797 printf("%s", (char *)str);
1798 }
1799 printf("\n");
1800 }
1801#endif
1802 while (len)
1803 {
1804 if (s[0] == ESC && s[1] == '|')
1805 {
1806 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001807 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
1809 arg1 = getdigits(&p);
1810 if (p > s + len)
1811 break;
1812 if (*p == ';')
1813 {
1814 ++p;
1815 arg2 = getdigits(&p);
1816 if (p > s + len)
1817 break;
1818 }
1819 }
1820 switch (*p)
1821 {
1822 case 'C': /* Clear screen */
1823 clip_scroll_selection(9999);
1824 gui_mch_clear_all();
1825 gui.cursor_is_valid = FALSE;
1826 force_scrollbar = TRUE;
1827 break;
1828 case 'M': /* Move cursor */
1829 gui_set_cursor(arg1, arg2);
1830 break;
1831 case 's': /* force cursor (shape) update */
1832 force_cursor = TRUE;
1833 break;
1834 case 'R': /* Set scroll region */
1835 if (arg1 < arg2)
1836 {
1837 gui.scroll_region_top = arg1;
1838 gui.scroll_region_bot = arg2;
1839 }
1840 else
1841 {
1842 gui.scroll_region_top = arg2;
1843 gui.scroll_region_bot = arg1;
1844 }
1845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 case 'V': /* Set vertical scroll region */
1847 if (arg1 < arg2)
1848 {
1849 gui.scroll_region_left = arg1;
1850 gui.scroll_region_right = arg2;
1851 }
1852 else
1853 {
1854 gui.scroll_region_left = arg2;
1855 gui.scroll_region_right = arg1;
1856 }
1857 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 case 'd': /* Delete line */
1859 gui_delete_lines(gui.row, 1);
1860 break;
1861 case 'D': /* Delete lines */
1862 gui_delete_lines(gui.row, arg1);
1863 break;
1864 case 'i': /* Insert line */
1865 gui_insert_lines(gui.row, 1);
1866 break;
1867 case 'I': /* Insert lines */
1868 gui_insert_lines(gui.row, arg1);
1869 break;
1870 case '$': /* Clear to end-of-line */
1871 gui_clear_block(gui.row, gui.col, gui.row,
1872 (int)Columns - 1);
1873 break;
1874 case 'h': /* Turn on highlighting */
1875 gui_start_highlight(arg1);
1876 break;
1877 case 'H': /* Turn off highlighting */
1878 gui_stop_highlight(arg1);
1879 break;
1880 case 'f': /* flash the window (visual bell) */
1881 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1882 break;
1883 default:
1884 p = s + 1; /* Skip the ESC */
1885 break;
1886 }
1887 len -= (int)(++p - s);
1888 s = p;
1889 }
1890 else if (
1891#ifdef EBCDIC
1892 CtrlChar(s[0]) != 0 /* Ctrl character */
1893#else
1894 s[0] < 0x20 /* Ctrl character */
1895#endif
1896#ifdef FEAT_SIGN_ICONS
1897 && s[0] != SIGN_BYTE
1898# ifdef FEAT_NETBEANS_INTG
1899 && s[0] != MULTISIGN_BYTE
1900# endif
1901#endif
1902 )
1903 {
1904 if (s[0] == '\n') /* NL */
1905 {
1906 gui.col = 0;
1907 if (gui.row < gui.scroll_region_bot)
1908 gui.row++;
1909 else
1910 gui_delete_lines(gui.scroll_region_top, 1);
1911 }
1912 else if (s[0] == '\r') /* CR */
1913 {
1914 gui.col = 0;
1915 }
1916 else if (s[0] == '\b') /* Backspace */
1917 {
1918 if (gui.col)
1919 --gui.col;
1920 }
1921 else if (s[0] == Ctrl_L) /* cursor-right */
1922 {
1923 ++gui.col;
1924 }
1925 else if (s[0] == Ctrl_G) /* Beep */
1926 {
1927 gui_mch_beep();
1928 }
1929 /* Other Ctrl character: shouldn't happen! */
1930
1931 --len; /* Skip this char */
1932 ++s;
1933 }
1934 else
1935 {
1936 p = s;
1937 while (len > 0 && (
1938#ifdef EBCDIC
1939 CtrlChar(*p) == 0
1940#else
1941 *p >= 0x20
1942#endif
1943#ifdef FEAT_SIGN_ICONS
1944 || *p == SIGN_BYTE
1945# ifdef FEAT_NETBEANS_INTG
1946 || *p == MULTISIGN_BYTE
1947# endif
1948#endif
1949 ))
1950 {
1951 len--;
1952 p++;
1953 }
1954 gui_outstr(s, (int)(p - s));
1955 s = p;
1956 }
1957 }
1958
1959 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1960 * set). */
1961 if (force_cursor)
1962 gui_update_cursor(TRUE, TRUE);
1963
1964 /* When switching to another window the dragging must have stopped.
1965 * Required for GTK, dragged_sb isn't reset. */
1966 if (old_curwin != curwin)
1967 gui.dragged_sb = SBAR_NONE;
1968
1969 /* Update the scrollbars after clearing the screen or when switched
1970 * to another window.
1971 * Update the horizontal scrollbar always, it's difficult to check all
1972 * situations where it might change. */
1973 if (force_scrollbar || old_curwin != curwin)
1974 gui_update_scrollbars(force_scrollbar);
1975 else
1976 gui_update_horiz_scrollbar(FALSE);
1977 old_curwin = curwin;
1978
1979 /*
1980 * We need to make sure this is cleared since Athena doesn't tell us when
1981 * he is done dragging. Do the same for GTK.
1982 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001983#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 gui.dragged_sb = SBAR_NONE;
1985#endif
1986
Bram Moolenaara338adc2018-01-31 20:51:47 +01001987 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988}
1989
1990/*
1991 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1992 * produces wrong results. Call gui_dont_update_cursor() before that code and
1993 * gui_can_update_cursor() afterwards.
1994 */
1995 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001996gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 if (gui.in_use)
1999 {
2000 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02002001 if (undraw)
2002 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 can_update_cursor = FALSE;
2004 }
2005}
2006
2007 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002008gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 can_update_cursor = TRUE;
2011 /* No need to update the cursor right now, there is always more output
2012 * after scrolling. */
2013}
2014
Bram Moolenaara338adc2018-01-31 20:51:47 +01002015/*
2016 * Disable issuing gui_mch_flush().
2017 */
2018 void
2019gui_disable_flush(void)
2020{
2021 ++disable_flush;
2022}
2023
2024/*
2025 * Enable issuing gui_mch_flush().
2026 */
2027 void
2028gui_enable_flush(void)
2029{
2030 --disable_flush;
2031}
2032
2033/*
2034 * Issue gui_mch_flush() if it is not disabled.
2035 */
2036 void
2037gui_may_flush(void)
2038{
2039 if (disable_flush == 0)
2040 gui_mch_flush();
2041}
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002044gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 int this_len;
2047#ifdef FEAT_MBYTE
2048 int cells;
2049#endif
2050
2051 if (len == 0)
2052 return;
2053
2054 if (len < 0)
2055 len = (int)STRLEN(s);
2056
2057 while (len > 0)
2058 {
2059#ifdef FEAT_MBYTE
2060 if (has_mbyte)
2061 {
2062 /* Find out how many chars fit in the current line. */
2063 cells = 0;
2064 for (this_len = 0; this_len < len; )
2065 {
2066 cells += (*mb_ptr2cells)(s + this_len);
2067 if (gui.col + cells > Columns)
2068 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002069 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 if (this_len > len)
2072 this_len = len; /* don't include following composing char */
2073 }
2074 else
2075#endif
2076 if (gui.col + len > Columns)
2077 this_len = Columns - gui.col;
2078 else
2079 this_len = len;
2080
2081 (void)gui_outstr_nowrap(s, this_len,
2082 0, (guicolor_T)0, (guicolor_T)0, 0);
2083 s += this_len;
2084 len -= this_len;
2085#ifdef FEAT_MBYTE
2086 /* fill up for a double-width char that doesn't fit. */
2087 if (len > 0 && gui.col < Columns)
2088 (void)gui_outstr_nowrap((char_u *)" ", 1,
2089 0, (guicolor_T)0, (guicolor_T)0, 0);
2090#endif
2091 /* The cursor may wrap to the next line. */
2092 if (gui.col >= Columns)
2093 {
2094 gui.col = 0;
2095 gui.row++;
2096 }
2097 }
2098}
2099
2100/*
2101 * Output one character (may be one or two display cells).
2102 * Caller must check for valid "off".
2103 * Returns FAIL or OK, just like gui_outstr_nowrap().
2104 */
2105 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002106gui_screenchar(
2107 int off, /* Offset from start of screen */
2108 int flags,
2109 guicolor_T fg, /* colors for cursor */
2110 guicolor_T bg, /* colors for cursor */
2111 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113#ifdef FEAT_MBYTE
2114 char_u buf[MB_MAXBYTES + 1];
2115
2116 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2117 if (enc_utf8 && ScreenLines[off] == 0)
2118 return OK;
2119
2120 if (enc_utf8 && ScreenLinesUC[off] != 0)
2121 /* Draw UTF-8 multi-byte character. */
2122 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2123 flags, fg, bg, back);
2124
2125 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2126 {
2127 buf[0] = ScreenLines[off];
2128 buf[1] = ScreenLines2[off];
2129 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2130 }
2131
2132 /* Draw non-multi-byte character or DBCS character. */
2133 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002134 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 flags, fg, bg, back);
2136#else
2137 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2138#endif
2139}
2140
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002141#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142/*
2143 * Output the string at the given screen position. This is used in place
2144 * of gui_screenchar() where possible because Pango needs as much context
2145 * as possible to work nicely. It's a lot faster as well.
2146 */
2147 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002148gui_screenstr(
2149 int off, /* Offset from start of screen */
2150 int len, /* string length in screen cells */
2151 int flags,
2152 guicolor_T fg, /* colors for cursor */
2153 guicolor_T bg, /* colors for cursor */
2154 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
2156 char_u *buf;
2157 int outlen = 0;
2158 int i;
2159 int retval;
2160
2161 if (len <= 0) /* "cannot happen"? */
2162 return OK;
2163
2164 if (enc_utf8)
2165 {
2166 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2167 if (buf == NULL)
2168 return OK; /* not much we could do here... */
2169
2170 for (i = off; i < off + len; ++i)
2171 {
2172 if (ScreenLines[i] == 0)
2173 continue; /* skip second half of double-width char */
2174
2175 if (ScreenLinesUC[i] == 0)
2176 buf[outlen++] = ScreenLines[i];
2177 else
2178 outlen += utfc_char2bytes(i, buf + outlen);
2179 }
2180
2181 buf[outlen] = NUL; /* only to aid debugging */
2182 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2183 vim_free(buf);
2184
2185 return retval;
2186 }
2187 else if (enc_dbcs == DBCS_JPNU)
2188 {
2189 buf = alloc((unsigned)(len * 2 + 1));
2190 if (buf == NULL)
2191 return OK; /* not much we could do here... */
2192
2193 for (i = off; i < off + len; ++i)
2194 {
2195 buf[outlen++] = ScreenLines[i];
2196
2197 /* handle double-byte single-width char */
2198 if (ScreenLines[i] == 0x8e)
2199 buf[outlen++] = ScreenLines2[i];
2200 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2201 buf[outlen++] = ScreenLines[++i];
2202 }
2203
2204 buf[outlen] = NUL; /* only to aid debugging */
2205 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2206 vim_free(buf);
2207
2208 return retval;
2209 }
2210 else
2211 {
2212 return gui_outstr_nowrap(&ScreenLines[off], len,
2213 flags, fg, bg, back);
2214 }
2215}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002216#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218/*
2219 * Output the given string at the current cursor position. If the string is
2220 * too long to fit on the line, then it is truncated.
2221 * "flags":
2222 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2223 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002224 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * background.
2226 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2227 * it.
2228 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2229 * FAIL (the caller should start drawing "back" chars back).
2230 */
2231 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002232gui_outstr_nowrap(
2233 char_u *s,
2234 int len,
2235 int flags,
2236 guicolor_T fg, /* colors for cursor */
2237 guicolor_T bg, /* colors for cursor */
2238 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 long_u highlight_mask;
2241 long_u hl_mask_todo;
2242 guicolor_T fg_color;
2243 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002244 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002245#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002247# ifdef FEAT_MBYTE
2248 GuiFont wide_font = NOFONT;
2249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250# ifdef FEAT_XFONTSET
2251 GuiFontset fontset = NOFONTSET;
2252# endif
2253#endif
2254 attrentry_T *aep = NULL;
2255 int draw_flags;
2256 int col = gui.col;
2257#ifdef FEAT_SIGN_ICONS
2258 int draw_sign = FALSE;
2259# ifdef FEAT_NETBEANS_INTG
2260 int multi_sign = FALSE;
2261# endif
2262#endif
2263
2264 if (len < 0)
2265 len = (int)STRLEN(s);
2266 if (len == 0)
2267 return OK;
2268
2269#ifdef FEAT_SIGN_ICONS
2270 if (*s == SIGN_BYTE
2271# ifdef FEAT_NETBEANS_INTG
2272 || *s == MULTISIGN_BYTE
2273# endif
2274 )
2275 {
2276# ifdef FEAT_NETBEANS_INTG
2277 if (*s == MULTISIGN_BYTE)
2278 multi_sign = TRUE;
2279# endif
2280 /* draw spaces instead */
2281 s = (char_u *)" ";
2282 if (len == 1 && col > 0)
2283 --col;
2284 len = 2;
2285 draw_sign = TRUE;
2286 highlight_mask = 0;
2287 }
2288 else
2289#endif
2290 if (gui.highlight_mask > HL_ALL)
2291 {
2292 aep = syn_gui_attr2entry(gui.highlight_mask);
2293 if (aep == NULL) /* highlighting not set */
2294 highlight_mask = 0;
2295 else
2296 highlight_mask = aep->ae_attr;
2297 }
2298 else
2299 highlight_mask = gui.highlight_mask;
2300 hl_mask_todo = highlight_mask;
2301
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 /* Set the font */
2304 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2305 font = aep->ae_u.gui.font;
2306# ifdef FEAT_XFONTSET
2307 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2308 fontset = aep->ae_u.gui.fontset;
2309# endif
2310 else
2311 {
2312# ifdef FEAT_XFONTSET
2313 if (gui.fontset != NOFONTSET)
2314 fontset = gui.fontset;
2315 else
2316# endif
2317 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2318 {
2319 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2320 {
2321 font = gui.boldital_font;
2322 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2323 }
2324 else if (gui.bold_font != NOFONT)
2325 {
2326 font = gui.bold_font;
2327 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2328 }
2329 else
2330 font = gui.norm_font;
2331 }
2332 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2333 {
2334 font = gui.ital_font;
2335 hl_mask_todo &= ~HL_ITALIC;
2336 }
2337 else
2338 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002339
2340# ifdef FEAT_MBYTE
2341 /*
2342 * Choose correct wide_font by font. wide_font should be set with font
2343 * at same time in above block. But it will make many "ifdef" nasty
2344 * blocks. So we do it here.
2345 */
2346 if (font == gui.boldital_font && gui.wide_boldital_font)
2347 wide_font = gui.wide_boldital_font;
2348 else if (font == gui.bold_font && gui.wide_bold_font)
2349 wide_font = gui.wide_bold_font;
2350 else if (font == gui.ital_font && gui.wide_ital_font)
2351 wide_font = gui.wide_ital_font;
2352 else if (font == gui.norm_font && gui.wide_font)
2353 wide_font = gui.wide_font;
2354# endif
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357# ifdef FEAT_XFONTSET
2358 if (fontset != NOFONTSET)
2359 gui_mch_set_fontset(fontset);
2360 else
2361# endif
2362 gui_mch_set_font(font);
2363#endif
2364
2365 draw_flags = 0;
2366
2367 /* Set the color */
2368 bg_color = gui.back_pixel;
2369 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2370 {
2371 draw_flags |= DRAW_CURSOR;
2372 fg_color = fg;
2373 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002374 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376 else if (aep != NULL)
2377 {
2378 fg_color = aep->ae_u.gui.fg_color;
2379 if (fg_color == INVALCOLOR)
2380 fg_color = gui.norm_pixel;
2381 bg_color = aep->ae_u.gui.bg_color;
2382 if (bg_color == INVALCOLOR)
2383 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002384 sp_color = aep->ae_u.gui.sp_color;
2385 if (sp_color == INVALCOLOR)
2386 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002391 sp_color = fg_color;
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2395 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002396#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 gui_mch_set_colors(bg_color, fg_color);
2398#else
2399 gui_mch_set_fg_color(bg_color);
2400 gui_mch_set_bg_color(fg_color);
2401#endif
2402 }
2403 else
2404 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002405#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 gui_mch_set_colors(fg_color, bg_color);
2407#else
2408 gui_mch_set_fg_color(fg_color);
2409 gui_mch_set_bg_color(bg_color);
2410#endif
2411 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002412 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 /* Clear the selection if we are about to write over it */
2415 if (!(flags & GUI_MON_NOCLEAR))
2416 clip_may_clear_selection(gui.row, gui.row);
2417
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 /* If there's no bold font, then fake it */
2420 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2421 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 /*
2424 * When drawing bold or italic characters the spill-over from the left
2425 * neighbor may be destroyed. Let the caller backup to start redrawing
2426 * just after a blank.
2427 */
2428 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2429 return FAIL;
2430
Bram Moolenaare60acc12011-05-10 16:41:25 +02002431#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 /* If there's no italic font, then fake it.
2433 * For GTK2, we don't need a different font for italic style. */
2434 if (hl_mask_todo & HL_ITALIC)
2435 draw_flags |= DRAW_ITALIC;
2436
2437 /* Do we underline the text? */
2438 if (hl_mask_todo & HL_UNDERLINE)
2439 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441#else
2442 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 draw_flags |= DRAW_UNDERL;
2445#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002446 /* Do we undercurl the text? */
2447 if (hl_mask_todo & HL_UNDERCURL)
2448 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002450 /* Do we strikethrough the text? */
2451 if (hl_mask_todo & HL_STRIKETHROUGH)
2452 draw_flags |= DRAW_STRIKE;
2453
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002454 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (flags & GUI_MON_TRS_CURSOR)
2456 draw_flags |= DRAW_TRANSP;
2457
2458 /*
2459 * Draw the text.
2460 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002461#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /* The value returned is the length in display cells */
2463 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2464#else
2465# ifdef FEAT_MBYTE
2466 if (enc_utf8)
2467 {
2468 int start; /* index of bytes to be drawn */
2469 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002470 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 int cn; /* cellwidth of current char */
2472 int i; /* index of current char */
2473 int c; /* current char value */
2474 int cl; /* byte length of current char */
2475 int comping; /* current char is composing */
2476 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002477 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002478 int prev_wide = FALSE;
2479 int wide_changed;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002480# ifdef WIN3264
2481 int sep_comp = FALSE; /* Don't separate composing chars. */
2482# else
2483 int sep_comp = TRUE; /* Separate composing chars. */
2484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 /* Break the string at a composing character, it has to be drawn on
2487 * top of the previous character. */
2488 start = 0;
2489 cells = 0;
2490 for (i = 0; i < len; i += cl)
2491 {
2492 c = utf_ptr2char(s + i);
2493 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 comping = utf_iscomposing(c);
2495 if (!comping) /* count cells from non-composing chars */
2496 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002497 if (!comping || sep_comp)
2498 {
2499 if (cn > 1
2500# ifdef FEAT_XFONTSET
2501 && fontset == NOFONTSET
2502# endif
2503 && wide_font != NOFONT)
2504 curr_wide = TRUE;
2505 else
2506 curr_wide = FALSE;
2507 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002508 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (cl == 0) /* hit end of string */
2510 len = i + cl; /* len must be wrong "cannot happen" */
2511
Bram Moolenaar45933962013-01-23 17:43:57 +01002512 wide_changed = curr_wide != prev_wide;
2513
2514 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002516 if (i + cl >= len || (comping && sep_comp && i > start)
2517 || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002518# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 || (cn > 1
2520# ifdef FEAT_XFONTSET
2521 /* No fontset: At least draw char after wide char at
2522 * right position. */
2523 && fontset == NOFONTSET
2524# endif
2525 )
2526# endif
2527 )
2528 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002529 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 thislen = i - start;
2531 else
2532 thislen = i - start + cl;
2533 if (thislen > 0)
2534 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002535 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002536 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2538 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002539 if (prev_wide)
2540 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 start += thislen;
2542 }
2543 scol += cells;
2544 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002545 /* Adjust to not draw a character which width is changed
2546 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002547 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002549 scol -= cn;
2550 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002553# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002554 /* No fontset: draw a space to fill the gap after a wide char
2555 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2557# ifdef FEAT_XFONTSET
2558 && fontset == NOFONTSET
2559# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002560 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2562 1, draw_flags);
2563# endif
2564 }
2565 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002566 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaard0573012017-10-28 21:11:06 +02002568# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002569 /* Carbon ATSUI autodraws composing char over previous char */
2570 gui_mch_draw_string(gui.row, scol, s + i, cl,
2571 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002572# else
2573 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2574 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 start = i + cl;
2577 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002578 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 /* The stuff below assumes "len" is the length in screen columns. */
2581 len = scol - col;
2582 }
2583 else
2584# endif
2585 {
2586 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2587# ifdef FEAT_MBYTE
2588 if (enc_dbcs == DBCS_JPNU)
2589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 /* Get the length in display cells, this can be different from the
2591 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002592 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594# endif
2595 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002596#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2599 gui.col = col + len;
2600
2601 /* May need to invert it when it's part of the selection. */
2602 if (flags & GUI_MON_NOCLEAR)
2603 clip_may_redraw_selection(gui.row, col, len);
2604
2605 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2606 {
2607 /* Invalidate the old physical cursor position if we wrote over it */
2608 if (gui.cursor_row == gui.row
2609 && gui.cursor_col >= col
2610 && gui.cursor_col < col + len)
2611 gui.cursor_is_valid = FALSE;
2612 }
2613
2614#ifdef FEAT_SIGN_ICONS
2615 if (draw_sign)
2616 /* Draw the sign on top of the spaces. */
2617 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002618# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002619 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (multi_sign)
2621 netbeans_draw_multisign_indicator(gui.row);
2622# endif
2623#endif
2624
2625 return OK;
2626}
2627
2628/*
2629 * Un-draw the cursor. Actually this just redraws the character at the given
2630 * position. The character just before it too, for when it was in bold.
2631 */
2632 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002633gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 if (gui.cursor_is_valid)
2636 {
2637#ifdef FEAT_HANGULIN
2638 if (composing_hangul
2639 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002640 {
2641 char_u *comp_buf;
2642 int comp_len;
2643
2644 comp_buf = hangul_composing_buffer_get(&comp_len);
2645 if (comp_buf)
2646 {
2647 (void)gui_outstr_nowrap(comp_buf, comp_len,
2648 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2649 gui.norm_pixel, gui.back_pixel, 0);
2650 vim_free(comp_buf);
2651 }
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 else
2654 {
2655#endif
2656 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2657 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2658 && gui.cursor_col > 0)
2659 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2660 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2661#ifdef FEAT_HANGULIN
2662 if (composing_hangul)
2663 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2664 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2665 }
2666#endif
2667 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2668 * here in case it wasn't needed to undraw it. */
2669 gui.cursor_is_valid = FALSE;
2670 }
2671}
2672
2673 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002674gui_redraw(
2675 int x,
2676 int y,
2677 int w,
2678 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 int row1, col1, row2, col2;
2681
2682 row1 = Y_2_ROW(y);
2683 col1 = X_2_COL(x);
2684 row2 = Y_2_ROW(y + h - 1);
2685 col2 = X_2_COL(x + w - 1);
2686
2687 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2688
2689 /*
2690 * We may need to redraw the cursor, but don't take it upon us to change
2691 * its location after a scroll.
2692 * (maybe be more strict even and test col too?)
2693 * These things may be outside the update/clipping region and reality may
2694 * not reflect Vims internal ideas if these operations are clipped away.
2695 */
2696 if (gui.row == gui.cursor_row)
2697 gui_update_cursor(TRUE, TRUE);
2698}
2699
2700/*
2701 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2702 * from col1 to col2 (inclusive).
2703 * Return TRUE when the character before the first drawn character has
2704 * different attributes (may have to be redrawn too).
2705 */
2706 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002707gui_redraw_block(
2708 int row1,
2709 int col1,
2710 int row2,
2711 int col2,
2712 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 int old_row, old_col;
2715 long_u old_hl_mask;
2716 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002717 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 int idx, len;
2719 int back, nback;
2720 int retval = FALSE;
2721#ifdef FEAT_MBYTE
2722 int orig_col1, orig_col2;
2723#endif
2724
2725 /* Don't try to update when ScreenLines is not valid */
2726 if (!screen_cleared || ScreenLines == NULL)
2727 return retval;
2728
2729 /* Don't try to draw outside the shell! */
2730 /* Check everything, strange values may be caused by a big border width */
2731 col1 = check_col(col1);
2732 col2 = check_col(col2);
2733 row1 = check_row(row1);
2734 row2 = check_row(row2);
2735
2736 /* Remember where our cursor was */
2737 old_row = gui.row;
2738 old_col = gui.col;
2739 old_hl_mask = gui.highlight_mask;
2740#ifdef FEAT_MBYTE
2741 orig_col1 = col1;
2742 orig_col2 = col2;
2743#endif
2744
2745 for (gui.row = row1; gui.row <= row2; gui.row++)
2746 {
2747#ifdef FEAT_MBYTE
2748 /* When only half of a double-wide character is in the block, include
2749 * the other half. */
2750 col1 = orig_col1;
2751 col2 = orig_col2;
2752 off = LineOffset[gui.row];
2753 if (enc_dbcs != 0)
2754 {
2755 if (col1 > 0)
2756 col1 -= dbcs_screen_head_off(ScreenLines + off,
2757 ScreenLines + off + col1);
2758 col2 += dbcs_screen_tail_off(ScreenLines + off,
2759 ScreenLines + off + col2);
2760 }
2761 else if (enc_utf8)
2762 {
2763 if (ScreenLines[off + col1] == 0)
2764 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002765# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2767 ++col2;
2768# endif
2769 }
2770#endif
2771 gui.col = col1;
2772 off = LineOffset[gui.row] + gui.col;
2773 len = col2 - col1 + 1;
2774
2775 /* Find how many chars back this highlighting starts, or where a space
2776 * is. Needed for when the bold trick is used */
2777 for (back = 0; back < col1; ++back)
2778 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2779 || ScreenLines[off - 1 - back] == ' ')
2780 break;
2781 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2782 && ScreenLines[off - 1] != ' ');
2783
2784 /* Break it up in strings of characters with the same attributes. */
2785 /* Print UTF-8 characters individually. */
2786 while (len > 0)
2787 {
2788 first_attr = ScreenAttrs[off];
2789 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002790#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (enc_utf8 && ScreenLinesUC[off] != 0)
2792 {
2793 /* output multi-byte character separately */
2794 nback = gui_screenchar(off, flags,
2795 (guicolor_T)0, (guicolor_T)0, back);
2796 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2797 idx = 2;
2798 else
2799 idx = 1;
2800 }
2801 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2802 {
2803 /* output double-byte, single-width character separately */
2804 nback = gui_screenchar(off, flags,
2805 (guicolor_T)0, (guicolor_T)0, back);
2806 idx = 1;
2807 }
2808 else
2809#endif
2810 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002811#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 for (idx = 0; idx < len; ++idx)
2813 {
2814 if (enc_utf8 && ScreenLines[off + idx] == 0)
2815 continue; /* skip second half of double-width char */
2816 if (ScreenAttrs[off + idx] != first_attr)
2817 break;
2818 }
2819 /* gui_screenstr() takes care of multibyte chars */
2820 nback = gui_screenstr(off, idx, flags,
2821 (guicolor_T)0, (guicolor_T)0, back);
2822#else
2823 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2824 idx++)
2825 {
2826# ifdef FEAT_MBYTE
2827 /* Stop at a multi-byte Unicode character. */
2828 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2829 break;
2830 if (enc_dbcs == DBCS_JPNU)
2831 {
2832 /* Stop at a double-byte single-width char. */
2833 if (ScreenLines[off + idx] == 0x8e)
2834 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002835 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 + off + idx) == 2)
2837 ++idx; /* skip second byte of double-byte char */
2838 }
2839# endif
2840 }
2841 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2842 (guicolor_T)0, (guicolor_T)0, back);
2843#endif
2844 }
2845 if (nback == FAIL)
2846 {
2847 /* Must back up to start drawing where a bold or italic word
2848 * starts. */
2849 off -= back;
2850 len += back;
2851 gui.col -= back;
2852 }
2853 else
2854 {
2855 off += idx;
2856 len -= idx;
2857 }
2858 back = 0;
2859 }
2860 }
2861
2862 /* Put the cursor back where it was */
2863 gui.row = old_row;
2864 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002865 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 return retval;
2868}
2869
2870 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002871gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 if (count <= 0)
2874 return;
2875
2876 if (row + count > gui.scroll_region_bot)
2877 /* Scrolled out of region, just blank the lines out */
2878 gui_clear_block(row, gui.scroll_region_left,
2879 gui.scroll_region_bot, gui.scroll_region_right);
2880 else
2881 {
2882 gui_mch_delete_lines(row, count);
2883
2884 /* If the cursor was in the deleted lines it's now gone. If the
2885 * cursor was in the scrolled lines adjust its position. */
2886 if (gui.cursor_row >= row
2887 && gui.cursor_col >= gui.scroll_region_left
2888 && gui.cursor_col <= gui.scroll_region_right)
2889 {
2890 if (gui.cursor_row < row + count)
2891 gui.cursor_is_valid = FALSE;
2892 else if (gui.cursor_row <= gui.scroll_region_bot)
2893 gui.cursor_row -= count;
2894 }
2895 }
2896}
2897
2898 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002899gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 if (count <= 0)
2902 return;
2903
2904 if (row + count > gui.scroll_region_bot)
2905 /* Scrolled out of region, just blank the lines out */
2906 gui_clear_block(row, gui.scroll_region_left,
2907 gui.scroll_region_bot, gui.scroll_region_right);
2908 else
2909 {
2910 gui_mch_insert_lines(row, count);
2911
2912 if (gui.cursor_row >= gui.row
2913 && gui.cursor_col >= gui.scroll_region_left
2914 && gui.cursor_col <= gui.scroll_region_right)
2915 {
2916 if (gui.cursor_row <= gui.scroll_region_bot - count)
2917 gui.cursor_row += count;
2918 else if (gui.cursor_row <= gui.scroll_region_bot)
2919 gui.cursor_is_valid = FALSE;
2920 }
2921 }
2922}
2923
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002924#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002925/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002926 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2927 */
2928 static int
2929gui_wait_for_chars_3(
2930 long wtime,
2931 int *interrupted UNUSED,
2932 int ignore_input UNUSED)
2933{
2934 return gui_mch_wait_for_chars(wtime);
2935}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002936#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002937
2938/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002939 * Returns OK if a character was found to be available within the given time,
2940 * or FAIL otherwise.
2941 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002942 static int
2943gui_wait_for_chars_or_timer(long wtime)
2944{
2945#ifdef FEAT_TIMERS
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002946 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3, NULL, 0);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002947#else
2948 return gui_mch_wait_for_chars(wtime);
2949#endif
2950}
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952/*
2953 * The main GUI input routine. Waits for a character from the keyboard.
2954 * wtime == -1 Wait forever.
2955 * wtime == 0 Don't wait.
2956 * wtime > 0 Wait wtime milliseconds for a character.
2957 * Returns OK if a character was found to be available within the given time,
2958 * or FAIL otherwise.
2959 */
2960 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002961gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 int retval;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002964#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002965 ELAPSED_TYPE start_tv;
2966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002968#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /*
2970 * If we're going to wait a bit, update the menus and mouse shape for the
2971 * current State.
2972 */
2973 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 gui_update_menus(0);
2975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
2977 gui_mch_update();
2978 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 /* Before waiting, flush any output to the screen. */
2984 gui_mch_flush();
2985
2986 if (wtime > 0)
2987 {
2988 /* Blink when waiting for a character. Probably only does something
2989 * for showmatch() */
2990 gui_mch_start_blink();
Bram Moolenaar975b5272016-03-15 23:10:59 +01002991 retval = gui_wait_for_chars_or_timer(wtime);
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002992 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 return retval;
2994 }
2995
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002996#if defined(ELAPSED_FUNC)
Bram Moolenaar4af031d2017-12-19 10:02:43 +01002997 ELAPSED_INIT(start_tv);
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00003001 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
3003 gui_mch_start_blink();
3004
Bram Moolenaar3918c952005-03-15 22:34:55 +00003005 retval = FAIL;
3006 /*
3007 * We may want to trigger the CursorHold event. First wait for
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003008 * 'updatetime' and if nothing is typed within that time, and feedkeys()
3009 * wasn't used, put the K_CURSORHOLD key in the input buffer.
Bram Moolenaar3918c952005-03-15 22:34:55 +00003010 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01003011 if (gui_wait_for_chars_or_timer(p_ut) == OK)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003012 retval = OK;
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003013 else if (trigger_cursorhold()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003014#ifdef ELAPSED_FUNC
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003015 && ELAPSED_FUNC(start_tv) >= p_ut
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003016#endif
Bram Moolenaar4af031d2017-12-19 10:02:43 +01003017 && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00003019 char_u buf[3];
3020
3021 /* Put K_CURSORHOLD in the input buffer. */
3022 buf[0] = CSI;
3023 buf[1] = KS_EXTRA;
3024 buf[2] = (int)KE_CURSORHOLD;
3025 add_to_input_buf(buf, 3);
3026
3027 retval = OK;
3028 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00003029
Bram Moolenaar9472eec2017-06-05 13:31:56 +02003030 if (retval == FAIL && typebuf.tb_change_cnt == tb_change_cnt)
Bram Moolenaar3918c952005-03-15 22:34:55 +00003031 {
3032 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00003033 before_blocking();
Bram Moolenaar975b5272016-03-15 23:10:59 +01003034 retval = gui_wait_for_chars_or_timer(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01003037 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 return retval;
3039}
3040
3041/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003042 * Equivalent of mch_inchar() for the GUI.
3043 */
3044 int
3045gui_inchar(
3046 char_u *buf,
3047 int maxlen,
3048 long wtime, /* milli seconds */
3049 int tb_change_cnt)
3050{
3051 if (gui_wait_for_chars(wtime, tb_change_cnt)
3052 && !typebuf_changed(tb_change_cnt))
3053 return read_from_input_buf(buf, (long)maxlen);
3054 return 0;
3055}
3056
3057/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003058 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
3060 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003061fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 p[0] = (char_u)(col / 128 + ' ' + 1);
3064 p[1] = (char_u)(col % 128 + ' ' + 1);
3065 p[2] = (char_u)(row / 128 + ' ' + 1);
3066 p[3] = (char_u)(row % 128 + ' ' + 1);
3067}
3068
3069/*
3070 * Generic mouse support function. Add a mouse event to the input buffer with
3071 * the given properties.
3072 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3073 * MOUSE_X1, MOUSE_X2
3074 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003075 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3076 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 * x, y --- Coordinates of mouse in pixels.
3078 * repeated_click --- TRUE if this click comes only a short time after a
3079 * previous click.
3080 * modifiers --- Bit field which may be any of the following modifiers
3081 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3082 * This function will ignore drag events where the mouse has not moved to a new
3083 * character.
3084 */
3085 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003086gui_send_mouse_event(
3087 int button,
3088 int x,
3089 int y,
3090 int repeated_click,
3091 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 static int prev_row = 0, prev_col = 0;
3094 static int prev_button = -1;
3095 static int num_clicks = 1;
3096 char_u string[10];
3097 enum key_extra button_char;
3098 int row, col;
3099#ifdef FEAT_CLIPBOARD
3100 int checkfor;
3101 int did_clip = FALSE;
3102#endif
3103
3104 /*
3105 * Scrolling may happen at any time, also while a selection is present.
3106 */
3107 switch (button)
3108 {
3109 case MOUSE_X1:
3110 button_char = KE_X1MOUSE;
3111 goto button_set;
3112 case MOUSE_X2:
3113 button_char = KE_X2MOUSE;
3114 goto button_set;
3115 case MOUSE_4:
3116 button_char = KE_MOUSEDOWN;
3117 goto button_set;
3118 case MOUSE_5:
3119 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003120 goto button_set;
3121 case MOUSE_6:
3122 button_char = KE_MOUSELEFT;
3123 goto button_set;
3124 case MOUSE_7:
3125 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126button_set:
3127 {
3128 /* Don't put events in the input queue now. */
3129 if (hold_gui_events)
3130 return;
3131
3132 string[3] = CSI;
3133 string[4] = KS_EXTRA;
3134 string[5] = (int)button_char;
3135
3136 /* Pass the pointer coordinates of the scroll event so that we
3137 * know which window to scroll. */
3138 row = gui_xy2colrow(x, y, &col);
3139 string[6] = (char_u)(col / 128 + ' ' + 1);
3140 string[7] = (char_u)(col % 128 + ' ' + 1);
3141 string[8] = (char_u)(row / 128 + ' ' + 1);
3142 string[9] = (char_u)(row % 128 + ' ' + 1);
3143
3144 if (modifiers == 0)
3145 add_to_input_buf(string + 3, 7);
3146 else
3147 {
3148 string[0] = CSI;
3149 string[1] = KS_MODIFIER;
3150 string[2] = 0;
3151 if (modifiers & MOUSE_SHIFT)
3152 string[2] |= MOD_MASK_SHIFT;
3153 if (modifiers & MOUSE_CTRL)
3154 string[2] |= MOD_MASK_CTRL;
3155 if (modifiers & MOUSE_ALT)
3156 string[2] |= MOD_MASK_ALT;
3157 add_to_input_buf(string, 10);
3158 }
3159 return;
3160 }
3161 }
3162
3163#ifdef FEAT_CLIPBOARD
3164 /* If a clipboard selection is in progress, handle it */
3165 if (clip_star.state == SELECT_IN_PROGRESS)
3166 {
3167 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3168 return;
3169 }
3170
3171 /* Determine which mouse settings to look for based on the current mode */
3172 switch (get_real_state())
3173 {
3174 case NORMAL_BUSY:
3175 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003176# ifdef FEAT_TERMINAL
3177 case TERMINAL:
3178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 case NORMAL: checkfor = MOUSE_NORMAL; break;
3180 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003181 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 case REPLACE:
3183 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 case VREPLACE:
3185 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 case INSERT:
3187 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3188 case ASKMORE:
3189 case HITRETURN: /* At the more- and hit-enter prompt pass the
3190 mouse event for a click on or below the
3191 message line. */
3192 if (Y_2_ROW(y) >= msg_row)
3193 checkfor = MOUSE_NORMAL;
3194 else
3195 checkfor = MOUSE_RETURN;
3196 break;
3197
3198 /*
3199 * On the command line, use the clipboard selection on all lines
3200 * but the command line. But not when pasting.
3201 */
3202 case CMDLINE:
3203 case CMDLINE+LANGMAP:
3204 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3205 checkfor = MOUSE_NONE;
3206 else
3207 checkfor = MOUSE_COMMAND;
3208 break;
3209
3210 default:
3211 checkfor = MOUSE_NONE;
3212 break;
3213 };
3214
3215 /*
3216 * Allow clipboard selection of text on the command line in "normal"
3217 * modes. Don't do this when dragging the status line, or extending a
3218 * Visual selection.
3219 */
3220 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003221 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 && button != MOUSE_DRAG
3223# ifdef FEAT_MOUSESHAPE
3224 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226# endif
3227 )
3228 checkfor = MOUSE_NONE;
3229
3230 /*
3231 * Use modeless selection when holding CTRL and SHIFT pressed.
3232 */
3233 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3234 checkfor = MOUSE_NONEF;
3235
3236 /*
3237 * In Ex mode, always use modeless selection.
3238 */
3239 if (exmode_active)
3240 checkfor = MOUSE_NONE;
3241
3242 /*
3243 * If the mouse settings say to not use the mouse, use the modeless
3244 * selection. But if Visual is active, assume that only the Visual area
3245 * will be selected.
3246 * Exception: On the command line, both the selection is used and a mouse
3247 * key is send.
3248 */
3249 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 /* Don't do modeless selection in Visual mode. */
3252 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3253 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 /*
3256 * When 'mousemodel' is "popup", shift-left is translated to right.
3257 * But not when also using Ctrl.
3258 */
3259 if (mouse_model_popup() && button == MOUSE_LEFT
3260 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3261 {
3262 button = MOUSE_RIGHT;
3263 modifiers &= ~ MOUSE_SHIFT;
3264 }
3265
3266 /* If the selection is done, allow the right button to extend it.
3267 * If the selection is cleared, allow the right button to start it
3268 * from the cursor position. */
3269 if (button == MOUSE_RIGHT)
3270 {
3271 if (clip_star.state == SELECT_CLEARED)
3272 {
3273 if (State & CMDLINE)
3274 {
3275 col = msg_col;
3276 row = msg_row;
3277 }
3278 else
3279 {
3280 col = curwin->w_wcol;
3281 row = curwin->w_wrow + W_WINROW(curwin);
3282 }
3283 clip_start_selection(col, row, FALSE);
3284 }
3285 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3286 repeated_click);
3287 did_clip = TRUE;
3288 }
3289 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003290 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3293 did_clip = TRUE;
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 /* Always allow pasting */
3297 if (button != MOUSE_MIDDLE)
3298 {
3299 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3300 return;
3301 if (checkfor != MOUSE_COMMAND)
3302 button = MOUSE_LEFT;
3303 }
3304 repeated_click = FALSE;
3305 }
3306
3307 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003308 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
3310
3311 /* Don't put events in the input queue now. */
3312 if (hold_gui_events)
3313 return;
3314
3315 row = gui_xy2colrow(x, y, &col);
3316
3317 /*
3318 * If we are dragging and the mouse hasn't moved far enough to be on a
3319 * different character, then don't send an event to vim.
3320 */
3321 if (button == MOUSE_DRAG)
3322 {
3323 if (row == prev_row && col == prev_col)
3324 return;
3325 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3326 if (y < 0)
3327 row = -1;
3328 }
3329
3330 /*
3331 * If topline has changed (window scrolled) since the last click, reset
3332 * repeated_click, because we don't want starting Visual mode when
3333 * clicking on a different character in the text.
3334 */
3335 if (curwin->w_topline != gui_prev_topline
3336#ifdef FEAT_DIFF
3337 || curwin->w_topfill != gui_prev_topfill
3338#endif
3339 )
3340 repeated_click = FALSE;
3341
3342 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3343 string[1] = KS_MOUSE;
3344 string[2] = KE_FILLER;
3345 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3346 {
3347 if (repeated_click)
3348 {
3349 /*
3350 * Handle multiple clicks. They only count if the mouse is still
3351 * pointing at the same character.
3352 */
3353 if (button != prev_button || row != prev_row || col != prev_col)
3354 num_clicks = 1;
3355 else if (++num_clicks > 4)
3356 num_clicks = 1;
3357 }
3358 else
3359 num_clicks = 1;
3360 prev_button = button;
3361 gui_prev_topline = curwin->w_topline;
3362#ifdef FEAT_DIFF
3363 gui_prev_topfill = curwin->w_topfill;
3364#endif
3365
3366 string[3] = (char_u)(button | 0x20);
3367 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3368 }
3369 else
3370 string[3] = (char_u)button;
3371
3372 string[3] |= modifiers;
3373 fill_mouse_coord(string + 4, col, row);
3374 add_to_input_buf(string, 8);
3375
3376 if (row < 0)
3377 prev_row = 0;
3378 else
3379 prev_row = row;
3380 prev_col = col;
3381
3382 /*
3383 * We need to make sure this is cleared since Athena doesn't tell us when
3384 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3385 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003386#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 gui.dragged_sb = SBAR_NONE;
3388#endif
3389}
3390
3391/*
3392 * Convert x and y coordinate to column and row in text window.
3393 * Corrects for multi-byte character.
3394 * returns column in "*colp" and row as return value;
3395 */
3396 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003397gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 int col = check_col(X_2_COL(x));
3400 int row = check_row(Y_2_ROW(y));
3401
3402#ifdef FEAT_MBYTE
3403 *colp = mb_fix_col(col, row);
3404#else
3405 *colp = col;
3406#endif
3407 return row;
3408}
3409
3410#if defined(FEAT_MENU) || defined(PROTO)
3411/*
3412 * Callback function for when a menu entry has been selected.
3413 */
3414 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003415gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003417 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 /* Don't put events in the input queue now. */
3420 if (hold_gui_events)
3421 return;
3422
3423 bytes[0] = CSI;
3424 bytes[1] = KS_MENU;
3425 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003426 add_to_input_buf(bytes, 3);
3427 add_long_to_buf((long_u)menu, bytes);
3428 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429}
3430#endif
3431
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003432static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434/*
3435 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003436 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * in p_go.
3438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003440gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#ifdef FEAT_MENU
3443 static int prev_menu_is_active = -1;
3444#endif
3445#ifdef FEAT_TOOLBAR
3446 static int prev_toolbar = -1;
3447 int using_toolbar = FALSE;
3448#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003449#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003450 int using_tabline;
3451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#ifdef FEAT_FOOTER
3453 static int prev_footer = -1;
3454 int using_footer = FALSE;
3455#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 static int prev_tearoff = -1;
3458 int using_tearoff = FALSE;
3459#endif
3460
3461 char_u *p;
3462 int i;
3463#ifdef FEAT_MENU
3464 int grey_old, grey_new;
3465 char_u *temp;
3466#endif
3467 win_T *wp;
3468 int need_set_size;
3469 int fix_size;
3470
3471#ifdef FEAT_MENU
3472 if (oldval != NULL && gui.in_use)
3473 {
3474 /*
3475 * Check if the menu's go from grey to non-grey or vise versa.
3476 */
3477 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3478 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3479 if (grey_old != grey_new)
3480 {
3481 temp = p_go;
3482 p_go = oldval;
3483 gui_update_menus(MENU_ALL_MODES);
3484 p_go = temp;
3485 }
3486 }
3487 gui.menu_is_active = FALSE;
3488#endif
3489
3490 for (i = 0; i < 3; i++)
3491 gui.which_scrollbars[i] = FALSE;
3492 for (p = p_go; *p; p++)
3493 switch (*p)
3494 {
3495 case GO_LEFT:
3496 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3497 break;
3498 case GO_RIGHT:
3499 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 case GO_VLEFT:
3502 if (win_hasvertsplit())
3503 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3504 break;
3505 case GO_VRIGHT:
3506 if (win_hasvertsplit())
3507 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 case GO_BOT:
3510 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3511 break;
3512#ifdef FEAT_MENU
3513 case GO_MENUS:
3514 gui.menu_is_active = TRUE;
3515 break;
3516#endif
3517 case GO_GREY:
3518 /* make menu's have grey items, ignored here */
3519 break;
3520#ifdef FEAT_TOOLBAR
3521 case GO_TOOLBAR:
3522 using_toolbar = TRUE;
3523 break;
3524#endif
3525#ifdef FEAT_FOOTER
3526 case GO_FOOTER:
3527 using_footer = TRUE;
3528 break;
3529#endif
3530 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 using_tearoff = TRUE;
3533#endif
3534 break;
3535 default:
3536 /* Ignore options that are not supported */
3537 break;
3538 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (gui.in_use)
3541 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003542 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003544
3545#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003546 /* Update the GUI tab line, it may appear or disappear. This may
3547 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003548 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003549 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003550 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003551 /* We don't want a resize event change "Rows" here, save and
3552 * restore it. Resizing is handled below. */
3553 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003554 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003555 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003556 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003557 if (using_tabline)
3558 fix_size = TRUE;
3559 if (!gui_use_tabline())
3560 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3561 }
3562#endif
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 for (i = 0; i < 3; i++)
3565 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003566 /* The scrollbar needs to be updated when it is shown/unshown and
3567 * when switching tab pages. But the size only changes when it's
3568 * shown/unshown. Thus we need two places to remember whether a
3569 * scrollbar is there or not. */
3570 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003571 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003572 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 if (i == SBAR_BOTTOM)
3575 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3576 gui.which_scrollbars[i]);
3577 else
3578 {
3579 FOR_ALL_WINDOWS(wp)
3580 {
3581 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3582 }
3583 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003584 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3585 {
3586 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003587 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003588 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003589 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003590 if (gui.which_scrollbars[i])
3591 fix_size = TRUE;
3592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003594 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003595 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597
3598#ifdef FEAT_MENU
3599 if (gui.menu_is_active != prev_menu_is_active)
3600 {
3601 /* We don't want a resize event change "Rows" here, save and
3602 * restore it. Resizing is handled below. */
3603 i = Rows;
3604 gui_mch_enable_menu(gui.menu_is_active);
3605 Rows = i;
3606 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003607 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 if (gui.menu_is_active)
3609 fix_size = TRUE;
3610 }
3611#endif
3612
3613#ifdef FEAT_TOOLBAR
3614 if (using_toolbar != prev_toolbar)
3615 {
3616 gui_mch_show_toolbar(using_toolbar);
3617 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003618 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (using_toolbar)
3620 fix_size = TRUE;
3621 }
3622#endif
3623#ifdef FEAT_FOOTER
3624 if (using_footer != prev_footer)
3625 {
3626 gui_mch_enable_footer(using_footer);
3627 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003628 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (using_footer)
3630 fix_size = TRUE;
3631 }
3632#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633#if defined(FEAT_MENU) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (using_tearoff != prev_tearoff)
3635 {
3636 gui_mch_toggle_tearoffs(using_tearoff);
3637 prev_tearoff = using_tearoff;
3638 }
3639#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003640 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003641 {
3642#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003643 long prev_Columns = Columns;
3644 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 /* Adjust the size of the window to make the text area keep the
3647 * same size and to avoid that part of our window is off-screen
3648 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003649 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003650
3651#ifdef FEAT_GUI_GTK
3652 /* GTK has the annoying habit of sending us resize events when
3653 * changing the window size ourselves. This mostly happens when
3654 * waiting for a character to arrive, quite unpredictably, and may
3655 * change Columns and Rows when we don't want it. Wait for a
3656 * character here to avoid this effect.
3657 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003658 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003659 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003660 * Don't change Rows when adding menu/toolbar/tabline.
3661 * Don't change Columns when adding vertical toolbar. */
3662 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003663 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003664 if ((need_set_size & RESIZE_VERT) == 0)
3665 Rows = prev_Rows;
3666 if ((need_set_size & RESIZE_HOR) == 0)
3667 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003668#endif
3669 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003670 /* When the console tabline appears or disappears the window positions
3671 * change. */
3672 if (firstwin->w_winrow != tabline_height())
3673 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675}
3676
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003677#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3678/*
3679 * Return TRUE if the GUI is taking care of the tabline.
3680 * It may still be hidden if 'showtabline' is zero.
3681 */
3682 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003683gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684{
3685 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3686}
3687
3688/*
3689 * Return TRUE if the GUI is showing the tabline.
3690 * This uses 'showtabline'.
3691 */
3692 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003693gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003694{
3695 if (!gui_use_tabline()
3696 || p_stal == 0
3697 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3698 return FALSE;
3699 return TRUE;
3700}
3701
3702/*
3703 * Update the tabline.
3704 * This may display/undisplay the tabline and update the labels.
3705 */
3706 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003707gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003708{
3709 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003710 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003711
3712 if (!gui.starting && starting == 0)
3713 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003714 /* Updating the tabline uses direct GUI commands, flush
3715 * outstanding instructions first. (esp. clear screen) */
3716 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003717
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003718 if (!showit != !shown)
3719 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003720 if (showit != 0)
3721 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003722
3723 /* When the tabs change from hidden to shown or from shown to
3724 * hidden the size of the text area should remain the same. */
3725 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003726 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003727 }
3728}
3729
3730/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003731 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003732 */
3733 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003734get_tabline_label(
3735 tabpage_T *tp,
3736 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003737{
3738 int modified = FALSE;
3739 char_u buf[40];
3740 int wincount;
3741 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003742 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003743
Bram Moolenaar57657d82006-04-21 22:12:41 +00003744 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003745 opt = (tooltip ? &p_gtt : &p_gtl);
3746 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003747 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003748 int use_sandbox = FALSE;
3749 int save_called_emsg = called_emsg;
3750 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003751 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003752 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3753 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003754
3755 called_emsg = FALSE;
3756
3757 printer_page_num = tabpage_index(tp);
3758# ifdef FEAT_EVAL
3759 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003760 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003761# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003762 /* It's almost as going to the tabpage, but without autocommands. */
3763 curtab->tp_firstwin = firstwin;
3764 curtab->tp_lastwin = lastwin;
3765 curtab->tp_curwin = curwin;
3766 save_curtab = curtab;
3767 curtab = tp;
3768 topframe = curtab->tp_topframe;
3769 firstwin = curtab->tp_firstwin;
3770 lastwin = curtab->tp_lastwin;
3771 curwin = curtab->tp_curwin;
3772 curbuf = curwin->w_buffer;
3773
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003774 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003775 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003776 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003777 STRCPY(NameBuff, res);
3778
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003779 /* Back to the original curtab. */
3780 curtab = save_curtab;
3781 topframe = curtab->tp_topframe;
3782 firstwin = curtab->tp_firstwin;
3783 lastwin = curtab->tp_lastwin;
3784 curwin = curtab->tp_curwin;
3785 curbuf = curwin->w_buffer;
3786
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003787 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003788 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003789 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003790 called_emsg |= save_called_emsg;
3791 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003792
3793 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3794 * use a default label. */
3795 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003796 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003797 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003798 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003799 if (!tooltip)
3800 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003801
3802 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3803 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3804 if (bufIsChanged(wp->w_buffer))
3805 modified = TRUE;
3806 if (modified || wincount > 1)
3807 {
3808 if (wincount > 1)
3809 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3810 else
3811 buf[0] = NUL;
3812 if (modified)
3813 STRCAT(buf, "+");
3814 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003815 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003816 mch_memmove(NameBuff, buf, STRLEN(buf));
3817 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003818 }
3819}
3820
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003821/*
3822 * Send the event for clicking to select tab page "nr".
3823 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003824 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003825 */
3826 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003827send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003828{
3829 char_u string[3];
3830
3831 if (nr == tabpage_index(curtab))
3832 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003833
3834 /* Don't put events in the input queue now. */
3835 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003836# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003837 || cmdwin_type != 0
3838# endif
3839 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003840 {
3841 /* Set it back to the current tab page. */
3842 gui_mch_set_curtab(tabpage_index(curtab));
3843 return FALSE;
3844 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003845
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003846 string[0] = CSI;
3847 string[1] = KS_TABLINE;
3848 string[2] = KE_FILLER;
3849 add_to_input_buf(string, 3);
3850 string[0] = nr;
3851 add_to_input_buf_csi(string, 1);
3852 return TRUE;
3853}
3854
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003855/*
3856 * Send a tabline menu event
3857 */
3858 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003859send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003860{
3861 char_u string[3];
3862
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003863 /* Don't put events in the input queue now. */
3864 if (hold_gui_events)
3865 return;
3866
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003867 string[0] = CSI;
3868 string[1] = KS_TABMENU;
3869 string[2] = KE_FILLER;
3870 add_to_input_buf(string, 3);
3871 string[0] = tabidx;
3872 string[1] = (char_u)(long)event;
3873 add_to_input_buf_csi(string, 2);
3874}
3875
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
3878/*
3879 * Scrollbar stuff:
3880 */
3881
Bram Moolenaare45828b2006-02-15 22:12:56 +00003882/*
3883 * Remove all scrollbars. Used before switching to another tab page.
3884 */
3885 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003886gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003887{
3888 int i;
3889 win_T *wp;
3890
3891 for (i = 0; i < 3; i++)
3892 {
3893 if (i == SBAR_BOTTOM)
3894 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3895 else
3896 {
3897 FOR_ALL_WINDOWS(wp)
3898 {
3899 gui_do_scrollbar(wp, i, FALSE);
3900 }
3901 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003902 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003903 }
3904}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003907gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 static int sbar_ident = 0;
3910
3911 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3912 sb->wp = wp;
3913 sb->type = type;
3914 sb->value = 0;
3915#ifdef FEAT_GUI_ATHENA
3916 sb->pixval = 0;
3917#endif
3918 sb->size = 1;
3919 sb->max = 1;
3920 sb->top = 0;
3921 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 sb->status_height = 0;
3924 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3925}
3926
3927/*
3928 * Find the scrollbar with the given index.
3929 */
3930 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003931gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
3933 win_T *wp;
3934
3935 if (gui.bottom_sbar.ident == ident)
3936 return &gui.bottom_sbar;
3937 FOR_ALL_WINDOWS(wp)
3938 {
3939 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3940 return &wp->w_scrollbars[SBAR_LEFT];
3941 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3942 return &wp->w_scrollbars[SBAR_RIGHT];
3943 }
3944 return NULL;
3945}
3946
3947/*
3948 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3949 *
3950 * For Win32, Macintosh and GTK+ 2:
3951 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3952 * you stop dragging the scrollbar. We get here each time the scrollbar is
3953 * dragged another pixel, but as far as the rest of vim goes, it thinks
3954 * we're just hanging in the call to DispatchMessage() in
3955 * process_message(). The DispatchMessage() call that hangs was passed a
3956 * mouse button click event in the scrollbar window. -- webb.
3957 *
3958 * Solution: Do the scrolling right here. But only when allowed.
3959 * Ignore the scrollbars while executing an external command or when there
3960 * are still characters to be processed.
3961 */
3962 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003963gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int sb_num;
3967#ifdef USE_ON_FLY_SCROLL
3968 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970# ifdef FEAT_DIFF
3971 int old_topfill = curwin->w_topfill;
3972# endif
3973#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003974 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int byte_count;
3976#endif
3977
3978 if (sb == NULL)
3979 return;
3980
3981 /* Don't put events in the input queue now. */
3982 if (hold_gui_events)
3983 return;
3984
3985#ifdef FEAT_CMDWIN
3986 if (cmdwin_type != 0 && sb->wp != curwin)
3987 return;
3988#endif
3989
3990 if (still_dragging)
3991 {
3992 if (sb->wp == NULL)
3993 gui.dragged_sb = SBAR_BOTTOM;
3994 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3995 gui.dragged_sb = SBAR_LEFT;
3996 else
3997 gui.dragged_sb = SBAR_RIGHT;
3998 gui.dragged_wp = sb->wp;
3999 }
4000 else
4001 {
4002 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004003#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004005 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 gui.dragged_wp = NULL;
4007#endif
4008 }
4009
4010 /* Vertical sbar info is kept in the first sbar (the left one) */
4011 if (sb->wp != NULL)
4012 sb = &sb->wp->w_scrollbars[0];
4013
4014 /*
4015 * Check validity of value
4016 */
4017 if (value < 0)
4018 value = 0;
4019#ifdef SCROLL_PAST_END
4020 else if (value > sb->max)
4021 value = sb->max;
4022#else
4023 if (value > sb->max - sb->size + 1)
4024 value = sb->max - sb->size + 1;
4025#endif
4026
4027 sb->value = value;
4028
4029#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00004030 /* When not allowed to do the scrolling right now, return.
4031 * This also checked input_available(), but that causes the first click in
4032 * a scrollbar to be ignored when Vim doesn't have focus. */
4033 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return;
4035#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004036#ifdef FEAT_INS_EXPAND
4037 /* Disallow scrolling the current window when the completion popup menu is
4038 * visible. */
4039 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4040 return;
4041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
4043#ifdef FEAT_RIGHTLEFT
4044 if (sb->wp == NULL && curwin->w_p_rl)
4045 {
4046 value = sb->max + 1 - sb->size - value;
4047 if (value < 0)
4048 value = 0;
4049 }
4050#endif
4051
4052 if (sb->wp != NULL) /* vertical scrollbar */
4053 {
4054 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4056 sb_num++;
4057 if (wp == NULL)
4058 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060#ifdef USE_ON_FLY_SCROLL
4061 current_scrollbar = sb_num;
4062 scrollbar_value = value;
4063 if (State & NORMAL)
4064 {
4065 gui_do_scroll();
4066 setcursor();
4067 }
4068 else if (State & INSERT)
4069 {
4070 ins_scroll();
4071 setcursor();
4072 }
4073 else if (State & CMDLINE)
4074 {
4075 if (msg_scrolled == 0)
4076 {
4077 gui_do_scroll();
4078 redrawcmdline();
4079 }
4080 }
4081# ifdef FEAT_FOLDING
4082 /* Value may have been changed for closed fold. */
4083 sb->value = sb->wp->w_topline - 1;
4084# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004085
4086 /* When dragging one scrollbar and there is another one at the other
4087 * side move the thumb of that one too. */
4088 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4089 gui_mch_set_scrollbar_thumb(
4090 &sb->wp->w_scrollbars[
4091 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4092 ? SBAR_LEFT : SBAR_RIGHT],
4093 sb->value, sb->size, sb->max);
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#else
4096 bytes[0] = CSI;
4097 bytes[1] = KS_VER_SCROLLBAR;
4098 bytes[2] = KE_FILLER;
4099 bytes[3] = (char_u)sb_num;
4100 byte_count = 4;
4101#endif
4102 }
4103 else
4104 {
4105#ifdef USE_ON_FLY_SCROLL
4106 scrollbar_value = value;
4107
4108 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004109 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 else if (State & INSERT)
4111 ins_horscroll();
4112 else if (State & CMDLINE)
4113 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004114 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004116 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 redrawcmdline();
4118 }
4119 }
4120 if (old_leftcol != curwin->w_leftcol)
4121 {
4122 updateWindow(curwin); /* update window, status and cmdline */
4123 setcursor();
4124 }
4125#else
4126 bytes[0] = CSI;
4127 bytes[1] = KS_HOR_SCROLLBAR;
4128 bytes[2] = KE_FILLER;
4129 byte_count = 3;
4130#endif
4131 }
4132
4133#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * synchronize other windows, as necessary according to 'scrollbind'
4136 */
4137 if (curwin->w_p_scb
4138 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4139 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004140# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 ))))
4144 {
4145 do_check_scrollbind(TRUE);
4146 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004147 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (wp->w_redr_type > 0)
4149 updateWindow(wp);
4150 setcursor();
4151 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004152 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004154 add_to_input_buf(bytes, byte_count);
4155 add_long_to_buf((long_u)value, bytes);
4156 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157#endif
4158}
4159
4160/*
4161 * Scrollbar stuff:
4162 */
4163
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004164/*
4165 * Called when something in the window layout has changed.
4166 */
4167 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004168gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004169{
4170 if (gui.in_use && starting == 0)
4171 {
4172 out_flush();
4173 gui_init_which_components(NULL);
4174 gui_update_scrollbars(TRUE);
4175 }
4176 need_mouse_correct = TRUE;
4177}
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004180gui_update_scrollbars(
4181 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 win_T *wp;
4184 scrollbar_T *sb;
4185 long val, size, max; /* need 32 bits here */
4186 int which_sb;
4187 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
4190 /* Update the horizontal scrollbar */
4191 gui_update_horiz_scrollbar(force);
4192
4193#ifndef WIN3264
4194 /* Return straight away if there is neither a left nor right scrollbar.
4195 * On MS-Windows this is required anyway for scrollwheel messages. */
4196 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4197 return;
4198#endif
4199
4200 /*
4201 * Don't want to update a scrollbar while we're dragging it. But if we
4202 * have both a left and right scrollbar, and we drag one of them, we still
4203 * need to update the other one.
4204 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004205 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4206 && gui.which_scrollbars[SBAR_LEFT]
4207 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209 /*
4210 * If we have two scrollbars and one of them is being dragged, just
4211 * copy the scrollbar position from the dragged one to the other one.
4212 */
4213 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4214 if (gui.dragged_wp != NULL)
4215 gui_mch_set_scrollbar_thumb(
4216 &gui.dragged_wp->w_scrollbars[which_sb],
4217 gui.dragged_wp->w_scrollbars[0].value,
4218 gui.dragged_wp->w_scrollbars[0].size,
4219 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221
4222 /* avoid that moving components around generates events */
4223 ++hold_gui_events;
4224
Bram Moolenaar45a24952016-07-24 22:25:15 +02004225 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 if (wp->w_buffer == NULL) /* just in case */
4228 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004229 /* Skip a scrollbar that is being dragged. */
4230 if (!force && (gui.dragged_sb == SBAR_LEFT
4231 || gui.dragged_sb == SBAR_RIGHT)
4232 && gui.dragged_wp == wp)
4233 continue;
4234
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#ifdef SCROLL_PAST_END
4236 max = wp->w_buffer->b_ml.ml_line_count - 1;
4237#else
4238 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4239#endif
4240 if (max < 0) /* empty buffer */
4241 max = 0;
4242 val = wp->w_topline - 1;
4243 size = wp->w_height;
4244#ifdef SCROLL_PAST_END
4245 if (val > max) /* just in case */
4246 val = max;
4247#else
4248 if (size > max + 1) /* just in case */
4249 size = max + 1;
4250 if (val > max - size + 1)
4251 val = max - size + 1;
4252#endif
4253 if (val < 0) /* minimal value is 0 */
4254 val = 0;
4255
4256 /*
4257 * Scrollbar at index 0 (the left one) contains all the information.
4258 * It would be the same info for left and right so we just store it for
4259 * one of them.
4260 */
4261 sb = &wp->w_scrollbars[0];
4262
4263 /*
4264 * Note: no check for valid w_botline. If it's not valid the
4265 * scrollbars will be updated later anyway.
4266 */
4267 if (size < 1 || wp->w_botline - 2 > max)
4268 {
4269 /*
4270 * This can happen during changing files. Just don't update the
4271 * scrollbar for now.
4272 */
4273 sb->height = 0; /* Force update next time */
4274 if (gui.which_scrollbars[SBAR_LEFT])
4275 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4276 if (gui.which_scrollbars[SBAR_RIGHT])
4277 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4278 continue;
4279 }
4280 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 || sb->top != wp->w_winrow
4282 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004284 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 /* Height, width or position of scrollbar has changed. For
4287 * vertical split: curwin changed. */
4288 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 sb->top = wp->w_winrow;
4290 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 /* Calculate height and position in pixels */
4294 h = (sb->height + sb->status_height) * gui.char_height;
4295 y = sb->top * gui.char_height + gui.border_offset;
4296#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4297 if (gui.menu_is_active)
4298 y += gui.menu_height;
4299#endif
4300
4301#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4302 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4303# ifdef FEAT_GUI_ATHENA
4304 y += gui.toolbar_height;
4305# else
4306# ifdef FEAT_GUI_MSWIN
4307 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4308# endif
4309# endif
4310#endif
4311
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004312#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4313 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004314 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004315#endif
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 /* Height of top scrollbar includes width of top border */
4320 h += gui.border_offset;
4321 y -= gui.border_offset;
4322 }
4323 if (gui.which_scrollbars[SBAR_LEFT])
4324 {
4325 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4326 gui.left_sbar_x, y,
4327 gui.scrollbar_width, h);
4328 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4329 }
4330 if (gui.which_scrollbars[SBAR_RIGHT])
4331 {
4332 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4333 gui.right_sbar_x, y,
4334 gui.scrollbar_width, h);
4335 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4336 }
4337 }
4338
4339 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4340 * checking if the thumb moved at least a pixel. Only do this for
4341 * Athena, most other GUIs require the update anyway to make the
4342 * arrows work. */
4343#ifdef FEAT_GUI_ATHENA
4344 if (max == 0)
4345 y = 0;
4346 else
4347 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4348 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4349#else
4350 if (force || sb->value != val || sb->size != size || sb->max != max)
4351#endif
4352 {
4353 /* Thumb of scrollbar has moved */
4354 sb->value = val;
4355#ifdef FEAT_GUI_ATHENA
4356 sb->pixval = y;
4357#endif
4358 sb->size = size;
4359 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004360 if (gui.which_scrollbars[SBAR_LEFT]
4361 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4363 val, size, max);
4364 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004365 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4367 val, size, max);
4368 }
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 --hold_gui_events;
4372}
4373
4374/*
4375 * Enable or disable a scrollbar.
4376 * Check for scrollbars for vertically split windows which are not enabled
4377 * sometimes.
4378 */
4379 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004380gui_do_scrollbar(
4381 win_T *wp,
4382 int which, /* SBAR_LEFT or SBAR_RIGHT */
4383 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int midcol = curwin->w_wincol + curwin->w_width / 2;
4386 int has_midcol = (wp->w_wincol <= midcol
4387 && wp->w_wincol + wp->w_width >= midcol);
4388
4389 /* Only enable scrollbars that contain the middle column of the current
4390 * window. */
4391 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4392 {
4393 /* Scrollbars only on one side. Don't enable scrollbars that don't
4394 * contain the middle column of the current window. */
4395 if (!has_midcol)
4396 enable = FALSE;
4397 }
4398 else
4399 {
4400 /* Scrollbars on both sides. Don't enable scrollbars that neither
4401 * contain the middle column of the current window nor are on the far
4402 * side. */
4403 if (midcol > Columns / 2)
4404 {
4405 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4406 enable = FALSE;
4407 }
4408 else
4409 {
4410 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4411 : !has_midcol)
4412 enable = FALSE;
4413 }
4414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4416}
4417
4418/*
4419 * Scroll a window according to the values set in the globals current_scrollbar
4420 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4421 * or FALSE otherwise.
4422 */
4423 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004424gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 win_T *wp, *save_wp;
4427 int i;
4428 long nlines;
4429 pos_T old_cursor;
4430 linenr_T old_topline;
4431#ifdef FEAT_DIFF
4432 int old_topfill;
4433#endif
4434
4435 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4436 if (wp == NULL)
4437 break;
4438 if (wp == NULL)
4439 /* Couldn't find window */
4440 return FALSE;
4441
4442 /*
4443 * Compute number of lines to scroll. If zero, nothing to do.
4444 */
4445 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4446 if (nlines == 0)
4447 return FALSE;
4448
4449 save_wp = curwin;
4450 old_topline = wp->w_topline;
4451#ifdef FEAT_DIFF
4452 old_topfill = wp->w_topfill;
4453#endif
4454 old_cursor = wp->w_cursor;
4455 curwin = wp;
4456 curbuf = wp->w_buffer;
4457 if (nlines < 0)
4458 scrolldown(-nlines, gui.dragged_wp == NULL);
4459 else
4460 scrollup(nlines, gui.dragged_wp == NULL);
4461 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4462 * the mouse-up event already, but we still want it to behave like when
4463 * dragging. But not the next click in an arrow. */
4464 if (gui.dragged_sb == SBAR_NONE)
4465 gui.dragged_wp = NULL;
4466
4467 if (old_topline != wp->w_topline
4468#ifdef FEAT_DIFF
4469 || old_topfill != wp->w_topfill
4470#endif
4471 )
4472 {
4473 if (p_so != 0)
4474 {
4475 cursor_correct(); /* fix window for 'so' */
4476 update_topline(); /* avoid up/down jump */
4477 }
4478 if (old_cursor.lnum != wp->w_cursor.lnum)
4479 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004483 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4484 validate_cursor();
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 curwin = save_wp;
4487 curbuf = save_wp->w_buffer;
4488
4489 /*
4490 * Don't call updateWindow() when nothing has changed (it will overwrite
4491 * the status line!).
4492 */
4493 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004494 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#ifdef FEAT_DIFF
4496 || old_topfill != wp->w_topfill
4497#endif
4498 )
4499 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004500 int type = VALID;
4501
4502#ifdef FEAT_INS_EXPAND
4503 if (pum_visible())
4504 {
4505 type = NOT_VALID;
4506 wp->w_lines_valid = 0;
4507 }
4508#endif
4509 /* Don't set must_redraw here, it may cause the popup menu to
4510 * disappear when losing focus after a scrollbar drag. */
4511 if (wp->w_redr_type < type)
4512 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004513 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004515 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004518#ifdef FEAT_INS_EXPAND
4519 /* May need to redraw the popup menu. */
4520 if (pum_visible())
4521 pum_redraw();
4522#endif
4523
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004524 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525}
4526
4527
4528/*
4529 * Horizontal scrollbar stuff:
4530 */
4531
4532/*
4533 * Return length of line "lnum" for horizontal scrolling.
4534 */
4535 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004536scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
4538 char_u *p;
4539 colnr_T col;
4540 int w;
4541
4542 p = ml_get(lnum);
4543 col = 0;
4544 if (*p != NUL)
4545 for (;;)
4546 {
4547 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004548 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (*p == NUL) /* don't count the last character */
4550 break;
4551 col += w;
4552 }
4553 return col;
4554}
4555
4556/* Remember which line is currently the longest, so that we don't have to
4557 * search for it when scrolling horizontally. */
4558static linenr_T longest_lnum = 0;
4559
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004560/*
4561 * Find longest visible line number. If this is not possible (or not desired,
4562 * by setting 'h' in "guioptions") then the current line number is returned.
4563 */
4564 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004565gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004566{
4567 linenr_T ret = 0;
4568
4569 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4570 * line numbers, topline and botline can be invalid when displaying is
4571 * postponed. */
4572 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4573 && curwin->w_topline <= curwin->w_cursor.lnum
4574 && curwin->w_botline > curwin->w_cursor.lnum
4575 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4576 {
4577 linenr_T lnum;
4578 colnr_T n;
4579 long max = 0;
4580
4581 /* Use maximum of all visible lines. Remember the lnum of the
4582 * longest line, closest to the cursor line. Used when scrolling
4583 * below. */
4584 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4585 {
4586 n = scroll_line_len(lnum);
4587 if (n > (colnr_T)max)
4588 {
4589 max = n;
4590 ret = lnum;
4591 }
4592 else if (n == (colnr_T)max
4593 && abs((int)(lnum - curwin->w_cursor.lnum))
4594 < abs((int)(ret - curwin->w_cursor.lnum)))
4595 ret = lnum;
4596 }
4597 }
4598 else
4599 /* Use cursor line only. */
4600 ret = curwin->w_cursor.lnum;
4601
4602 return ret;
4603}
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004606gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
4608 long value, size, max; /* need 32 bit ints here */
4609
4610 if (!gui.which_scrollbars[SBAR_BOTTOM])
4611 return;
4612
4613 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4614 return;
4615
4616 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4617 return;
4618
4619 /*
4620 * It is possible for the cursor to be invalid if we're in the middle of
4621 * something (like changing files). If so, don't do anything for now.
4622 */
4623 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4624 {
4625 gui.bottom_sbar.value = -1;
4626 return;
4627 }
4628
Bram Moolenaar02631462017-09-22 15:20:32 +02004629 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (curwin->w_p_wrap)
4631 {
4632 value = 0;
4633#ifdef SCROLL_PAST_END
4634 max = 0;
4635#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004636 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638 }
4639 else
4640 {
4641 value = curwin->w_leftcol;
4642
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004643 longest_lnum = gui_find_longest_lnum();
4644 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#ifdef FEAT_VIRTUALEDIT
4647 if (virtual_active())
4648 {
4649 /* May move the cursor even further to the right. */
4650 if (curwin->w_virtcol >= (colnr_T)max)
4651 max = curwin->w_virtcol;
4652 }
4653#endif
4654
4655#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004656 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#endif
4658 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004659 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 size -= curwin_col_off();
4661#ifndef SCROLL_PAST_END
4662 max -= curwin_col_off();
4663#endif
4664 }
4665
4666#ifndef SCROLL_PAST_END
4667 if (value > max - size + 1)
4668 value = max - size + 1; /* limit the value to allowable range */
4669#endif
4670
4671#ifdef FEAT_RIGHTLEFT
4672 if (curwin->w_p_rl)
4673 {
4674 value = max + 1 - size - value;
4675 if (value < 0)
4676 {
4677 size += value;
4678 value = 0;
4679 }
4680 }
4681#endif
4682 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4683 && max == gui.bottom_sbar.max)
4684 return;
4685
4686 gui.bottom_sbar.value = value;
4687 gui.bottom_sbar.size = size;
4688 gui.bottom_sbar.max = max;
4689 gui.prev_wrap = curwin->w_p_wrap;
4690
4691 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4692}
4693
4694/*
4695 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4696 */
4697 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004698gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 /* no wrapping, no scrolling */
4701 if (curwin->w_p_wrap)
4702 return FALSE;
4703
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004704 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FALSE;
4706
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004707 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004710 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004712 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004713 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004715 if (compute_longest_lnum)
4716 {
4717 curwin->w_cursor.lnum = gui_find_longest_lnum();
4718 curwin->w_cursor.col = 0;
4719 }
4720 /* Do a sanity check on "longest_lnum", just in case. */
4721 else if (longest_lnum >= curwin->w_topline
4722 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
4724 curwin->w_cursor.lnum = longest_lnum;
4725 curwin->w_cursor.col = 0;
4726 }
4727 }
4728
4729 return leftcol_changed();
4730}
4731
4732/*
4733 * Check that none of the colors are the same as the background color
4734 */
4735 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004736gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
4738 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4739 {
4740 gui_set_bg_color((char_u *)"White");
4741 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4742 gui_set_fg_color((char_u *)"Black");
4743 }
4744}
4745
Bram Moolenaar3918c952005-03-15 22:34:55 +00004746 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004747gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 gui.norm_pixel = gui_get_color(name);
4750 hl_set_fg_color_name(vim_strsave(name));
4751}
4752
Bram Moolenaar3918c952005-03-15 22:34:55 +00004753 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004754gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 gui.back_pixel = gui_get_color(name);
4757 hl_set_bg_color_name(vim_strsave(name));
4758}
4759
4760/*
4761 * Allocate a color by name.
4762 * Returns INVALCOLOR and gives an error message when failed.
4763 */
4764 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004765gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
4767 guicolor_T t;
4768
4769 if (*name == NUL)
4770 return INVALCOLOR;
4771 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004774#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 && gui.in_use
4776#endif
4777 )
4778 EMSG2(_("E254: Cannot allocate color %s"), name);
4779 return t;
4780}
4781
4782/*
4783 * Return the grey value of a color (range 0-255).
4784 */
4785 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004786gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004788 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004790 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004791 + (((rgb >> 8) & 0xff) * 587)
4792 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793}
4794
4795#if defined(FEAT_GUI_X11) || defined(PROTO)
4796 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004797gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799 win_T *wp;
4800
4801 /* Nothing to do if GUI hasn't started yet. */
4802 if (!gui.in_use)
4803 return;
4804
4805 FOR_ALL_WINDOWS(wp)
4806 {
4807 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4808 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4809 }
4810 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4811}
4812#endif
4813
4814/*
4815 * Call this when focus has changed.
4816 */
4817 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004818gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
4820/*
4821 * Skip this code to avoid drawing the cursor when debugging and switching
4822 * between the debugger window and gvim.
4823 */
4824#if 1
4825 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004826 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828# ifdef FEAT_XIM
4829 xim_set_focus(in_focus);
4830# endif
4831
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004832 /* Put events in the input queue only when allowed.
4833 * ui_focus_change() isn't called directly, because it invokes
4834 * autocommands and that must not happen asynchronously. */
4835 if (!hold_gui_events)
4836 {
4837 char_u bytes[3];
4838
4839 bytes[0] = CSI;
4840 bytes[1] = KS_EXTRA;
4841 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4842 add_to_input_buf(bytes, 3);
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#endif
4845}
4846
4847/*
4848 * Called when the mouse moved (but not when dragging).
4849 */
4850 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004851gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
4853 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004854 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaard3667a22006-03-16 21:35:52 +00004856 /* Ignore this while still starting up. */
4857 if (!gui.in_use || gui.starting)
4858 return;
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860#ifdef FEAT_MOUSESHAPE
4861 /* Get window pointer, and update mouse shape as well. */
4862 wp = xy2win(x, y);
4863#endif
4864
4865 /* Only handle this when 'mousefocus' set and ... */
4866 if (p_mousef
4867 && !hold_gui_events /* not holding events */
4868 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4869 && State != HITRETURN /* but not hit-return prompt */
4870 && msg_scrolled == 0 /* no scrolled message */
4871 && !need_mouse_correct /* not moving the pointer */
4872 && gui.in_focus) /* gvim in focus */
4873 {
4874 /* Don't move the mouse when it's left or right of the Vim window */
4875 if (x < 0 || x > Columns * gui.char_width)
4876 return;
4877#ifndef FEAT_MOUSESHAPE
4878 wp = xy2win(x, y);
4879#endif
4880 if (wp == curwin || wp == NULL)
4881 return; /* still in the same old window, or none at all */
4882
Bram Moolenaar9c102382006-05-03 21:26:49 +00004883 /* Ignore position in the tab pages line. */
4884 if (Y_2_ROW(y) < tabline_height())
4885 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 /*
4888 * format a mouse click on status line input
4889 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004890 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4891 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
4893 if (finish_op)
4894 {
4895 /* abort the current operator first */
4896 st[0] = ESC;
4897 add_to_input_buf(st, 1);
4898 }
4899 st[0] = CSI;
4900 st[1] = KS_MOUSE;
4901 st[2] = KE_FILLER;
4902 st[3] = (char_u)MOUSE_LEFT;
4903 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004904 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 wp->w_height + W_WINROW(wp));
4906
4907 add_to_input_buf(st, 8);
4908 st[3] = (char_u)MOUSE_RELEASE;
4909 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_GUI_GTK
4911 /* Need to wake up the main loop */
4912 if (gtk_main_level() > 0)
4913 gtk_main_quit();
4914#endif
4915 }
4916}
4917
4918/*
4919 * Called when mouse should be moved to window with focus.
4920 */
4921 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004922gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 int x, y;
4925 win_T *wp = NULL;
4926
4927 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004928
4929 if (!(gui.in_use && p_mousef))
4930 return;
4931
4932 gui_mch_getmouse(&x, &y);
4933 /* Don't move the mouse when it's left or right of the Vim window */
4934 if (x < 0 || x > Columns * gui.char_width)
4935 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004936 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004937 wp = xy2win(x, y);
4938 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004940 validate_cline_row();
4941 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4942 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945}
4946
4947/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004948 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 static win_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004951xy2win(int x UNUSED, int y UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 int row;
4954 int col;
4955 win_T *wp;
4956
4957 row = Y_2_ROW(y);
4958 col = X_2_COL(x);
4959 if (row < 0 || col < 0) /* before first window */
4960 return NULL;
4961 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004962 if (wp == NULL)
4963 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004964#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (State == HITRETURN || State == ASKMORE)
4966 {
4967 if (Y_2_ROW(y) >= msg_row)
4968 update_mouseshape(SHAPE_IDX_MOREL);
4969 else
4970 update_mouseshape(SHAPE_IDX_MORE);
4971 }
4972 else if (row > wp->w_height) /* below status line */
4973 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004974 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004975 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004977 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004978 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 update_mouseshape(SHAPE_IDX_STATUS);
4980 else
4981 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004983 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984}
4985
4986/*
4987 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4988 * File names may be given to redefine the args list.
4989 */
4990 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004991ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
4993 char_u *arg = eap->arg;
4994
4995 /*
4996 * Check for "-f" argument: foreground, don't fork.
4997 * Also don't fork when started with "gvim -f".
4998 * Do fork when using "gui -b".
4999 */
5000 if (arg[0] == '-'
5001 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005002 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 gui.dofork = (arg[1] == 'b');
5005 eap->arg = skipwhite(eap->arg + 2);
5006 }
5007 if (!gui.in_use)
5008 {
5009 /* Clear the command. Needed for when forking+exiting, to avoid part
5010 * of the argument ending up after the shell prompt. */
5011 msg_clr_eos_force();
5012 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005013#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005014 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 if (!ends_excmd(*eap->arg))
5018 ex_next(eap);
5019}
5020
5021#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00005022 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * This is shared between Athena, Motif and GTK.
5025 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005026static void gfp_setname(char_u *fname, void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028/*
5029 * Callback function for do_in_runtimepath().
5030 */
5031 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005032gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005034 char_u *gfp_buffer = cookie;
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (STRLEN(fname) >= MAXPATHL)
5037 *gfp_buffer = NUL;
5038 else
5039 STRCPY(gfp_buffer, fname);
5040}
5041
5042/*
5043 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5044 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5045 */
5046 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005047gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048{
5049 if (STRLEN(name) > MAXPATHL - 14)
5050 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005051 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005052 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 return FAIL;
5055 return OK;
5056}
5057
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005058# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059/*
5060 * Given the name of the "icon=" argument, try finding the bitmap file for the
5061 * icon. If it is an absolute path name, use it as it is. Otherwise append
5062 * "ext" and search for it in 'runtimepath'.
5063 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5064 * contains "name".
5065 */
5066 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005067gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
5069 char_u buf[MAXPATHL + 1];
5070
5071 expand_env(name, buffer, MAXPATHL);
5072 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5073 STRCPY(buffer, buf);
5074}
5075# endif
5076#endif
5077
Bram Moolenaar9372a112005-12-06 19:59:18 +00005078#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005080display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 char_u *p;
5083
5084 if (isatty(2))
5085 fflush(stderr);
5086 else if (error_ga.ga_data != NULL)
5087 {
5088 /* avoid putting up a message box with blanks only */
5089 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5090 if (!isspace(*p))
5091 {
5092 /* Truncate a very long message, it will go off-screen. */
5093 if (STRLEN(p) > 2000)
5094 STRCPY(p + 2000 - 14, "...(truncated)");
5095 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005096 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 }
5099 ga_clear(&error_ga);
5100 }
5101}
5102#endif
5103
5104#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5105/*
5106 * Return TRUE if still starting up and there is no place to enter text.
5107 * For GTK and X11 we check if stderr is not a tty, which means we were
5108 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5109 * allow typing on stdin.
5110 */
5111 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005112no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
5114 return ((!gui.in_use || gui.starting)
5115# ifndef NO_CONSOLE
5116 && !isatty(0) && !isatty(2)
5117# endif
5118 );
5119}
5120#endif
5121
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005122#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005123 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005124 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125/*
5126 * Update the current window and the screen.
5127 */
5128 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005129gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005131# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005132 linenr_T conceal_old_cursor_line = 0;
5133 linenr_T conceal_new_cursor_line = 0;
5134 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005135# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 update_topline();
5138 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005139
Bram Moolenaarec80df72008-05-07 19:46:51 +00005140 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005141 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005142# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005143 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005145 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005146 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005147 if (has_cursormoved())
5148 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005149# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005150 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005151 {
5152 conceal_old_cursor_line = last_cursormoved.lnum;
5153 conceal_new_cursor_line = curwin->w_cursor.lnum;
5154 conceal_update_lines = TRUE;
5155 }
5156# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005157 last_cursormoved = curwin->w_cursor;
5158 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 update_screen(0); /* may need to update the screen */
5161 setcursor();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005162# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005163 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005164 && (conceal_old_cursor_line != conceal_new_cursor_line
5165 || conceal_cursor_line(curwin)
5166 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005167 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005168 if (conceal_old_cursor_line != conceal_new_cursor_line)
5169 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005170 update_single_line(curwin, conceal_new_cursor_line);
5171 curwin->w_valid &= ~VALID_CROW;
5172 }
5173# endif
Bram Moolenaara338adc2018-01-31 20:51:47 +01005174 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175}
5176#endif
5177
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005178#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179/*
5180 * Get the text to use in a find/replace dialog. Uses the last search pattern
5181 * if the argument is empty.
5182 * Returns an allocated string.
5183 */
5184 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005185get_find_dialog_text(
5186 char_u *arg,
5187 int *wwordp, /* return: TRUE if \< \> found */
5188 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 char_u *text;
5191
5192 if (*arg == NUL)
5193 text = last_search_pat();
5194 else
5195 text = arg;
5196 if (text != NULL)
5197 {
5198 text = vim_strsave(text);
5199 if (text != NULL)
5200 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005201 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 int i;
5203
5204 /* Remove "\V" */
5205 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5206 {
5207 mch_memmove(text, text + 2, (size_t)(len - 1));
5208 len -= 2;
5209 }
5210
5211 /* Recognize "\c" and "\C" and remove. */
5212 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5213 {
5214 *mcasep = (text[1] == 'C');
5215 mch_memmove(text, text + 2, (size_t)(len - 1));
5216 len -= 2;
5217 }
5218
5219 /* Recognize "\<text\>" and remove. */
5220 if (len >= 4
5221 && STRNCMP(text, "\\<", 2) == 0
5222 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5223 {
5224 *wwordp = TRUE;
5225 mch_memmove(text, text + 2, (size_t)(len - 4));
5226 text[len - 4] = NUL;
5227 }
5228
5229 /* Recognize "\/" or "\?" and remove. */
5230 for (i = 0; i + 1 < len; ++i)
5231 if (text[i] == '\\' && (text[i + 1] == '/'
5232 || text[i + 1] == '?'))
5233 {
5234 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5235 --len;
5236 }
5237 }
5238 }
5239 return text;
5240}
5241
5242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 * Handle the press of a button in the find-replace dialog.
5244 * Return TRUE when something was added to the input buffer.
5245 */
5246 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005247gui_do_findrepl(
5248 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5249 char_u *find_text,
5250 char_u *repl_text,
5251 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 garray_T ga;
5254 int i;
5255 int type = (flags & FRD_TYPE_MASK);
5256 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005257 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005258 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005259 static int busy = FALSE;
5260
5261 /* When the screen is being updated we should not change buffers and
5262 * windows structures, it may cause freed memory to be used. Also don't
5263 * do this recursively (pressing "Find" quickly several times. */
5264 if (updating_screen || busy)
5265 return FALSE;
5266
5267 /* refuse replace when text cannot be changed */
5268 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5269 return FALSE;
5270
5271 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005274 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 ga_concat(&ga, (char_u *)"%s/");
5276
5277 ga_concat(&ga, (char_u *)"\\V");
5278 if (flags & FRD_MATCH_CASE)
5279 ga_concat(&ga, (char_u *)"\\C");
5280 else
5281 ga_concat(&ga, (char_u *)"\\c");
5282 if (flags & FRD_WHOLE_WORD)
5283 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005284 /* escape / and \ */
5285 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5286 if (p != NULL)
5287 ga_concat(&ga, p);
5288 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (flags & FRD_WHOLE_WORD)
5290 ga_concat(&ga, (char_u *)"\\>");
5291
5292 if (type == FRD_REPLACEALL)
5293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005295 /* escape / and \ */
5296 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5297 if (p != NULL)
5298 ga_concat(&ga, p);
5299 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005301 }
5302 ga_append(&ga, NUL);
5303
5304 if (type == FRD_REPLACE)
5305 {
5306 /* Do the replacement when the text at the cursor matches. Thus no
5307 * replacement is done if the cursor was moved! */
5308 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5309 regmatch.rm_ic = 0;
5310 if (regmatch.regprog != NULL)
5311 {
5312 p = ml_get_cursor();
5313 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5314 && regmatch.startp[0] == p)
5315 {
5316 /* Clear the command line to remove any old "No match"
5317 * error. */
5318 msg_end_prompt();
5319
5320 if (u_save_cursor() == OK)
5321 {
5322 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005323 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005324
5325 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005326 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005327 ins_str(repl_text);
5328 }
5329 }
5330 else
5331 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005332 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005333 }
5334 }
5335
5336 if (type == FRD_REPLACEALL)
5337 {
5338 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005339 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 do_cmdline_cmd(ga.ga_data);
5341 }
5342 else
5343 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005344 int searchflags = SEARCH_MSG + SEARCH_MARK;
5345
5346 /* Search for the next match.
5347 * Don't skip text under cursor for single replace. */
5348 if (type == FRD_REPLACE)
5349 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005351 if (down)
5352 {
5353 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5354 }
5355 else
5356 {
5357 /* We need to escape '?' if and only if we are searching in the up
5358 * direction */
5359 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5360 if (p != NULL)
5361 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5362 vim_free(p);
5363 }
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 msg_scroll = i; /* don't let an error message set msg_scroll */
5366 }
5367
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005368 /* Don't want to pass did_emsg to other code, it may cause disabling
5369 * syntax HL if we were busy redrawing. */
5370 did_emsg = save_did_emsg;
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 if (State & (NORMAL | INSERT))
5373 {
5374 gui_update_screen(); /* update the screen */
5375 msg_didout = 0; /* overwrite any message */
5376 need_wait_return = FALSE; /* don't wait for return */
5377 }
5378
5379 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005380 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 return (ga.ga_len > 0);
5382}
5383
5384#endif
5385
5386#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5387 || defined(FEAT_GUI_MSWIN) \
5388 || defined(FEAT_GUI_MAC) \
5389 || defined(PROTO)
5390
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005391static void gui_wingoto_xy(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393/*
5394 * Jump to the window at specified point (x, y).
5395 */
5396 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005397gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 int row = Y_2_ROW(y);
5400 int col = X_2_COL(x);
5401 win_T *wp;
5402
5403 if (row >= 0 && col >= 0)
5404 {
5405 wp = mouse_find_win(&row, &col);
5406 if (wp != NULL && wp != curwin)
5407 win_goto(wp);
5408 }
5409}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411/*
5412 * Process file drop. Mouse cursor position, key modifiers, name of files
5413 * and count of files are given. Argument "fnames[count]" has full pathnames
5414 * of dropped files, they will be freed in this function, and caller can't use
5415 * fnames after call this function.
5416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005418gui_handle_drop(
5419 int x UNUSED,
5420 int y UNUSED,
5421 int_u modifiers,
5422 char_u **fnames,
5423 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 int i;
5426 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005427 static int entered = FALSE;
5428
5429 /*
5430 * This function is called by event handlers. Just in case we get a
5431 * second event before the first one is handled, ignore the second one.
5432 * Not sure if this can ever happen, just in case.
5433 */
5434 if (entered)
5435 return;
5436 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
5438 /*
5439 * When the cursor is at the command line, add the file names to the
5440 * command line, don't edit the files.
5441 */
5442 if (State & CMDLINE)
5443 {
5444 shorten_filenames(fnames, count);
5445 for (i = 0; i < count; ++i)
5446 {
5447 if (fnames[i] != NULL)
5448 {
5449 if (i > 0)
5450 add_to_input_buf((char_u*)" ", 1);
5451
5452 /* We don't know what command is used thus we can't be sure
5453 * about which characters need to be escaped. Only escape the
5454 * most common ones. */
5455# ifdef BACKSLASH_IN_FILENAME
5456 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5457# else
5458 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5459# endif
5460 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005461 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 vim_free(p);
5463 vim_free(fnames[i]);
5464 }
5465 }
5466 vim_free(fnames);
5467 }
5468 else
5469 {
5470 /* Go to the window under mouse cursor, then shorten given "fnames" by
5471 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 shorten_filenames(fnames, count);
5474
5475 /* If Shift held down, remember the first item. */
5476 if ((modifiers & MOUSE_SHIFT) != 0)
5477 p = vim_strsave(fnames[0]);
5478 else
5479 p = NULL;
5480
5481 /* Handle the drop, :edit or :split to get to the file. This also
5482 * frees fnames[]. Skip this if there is only one item it's a
5483 * directory and Shift is held down. */
5484 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5485 && mch_isdir(fnames[0]))
5486 {
5487 vim_free(fnames[0]);
5488 vim_free(fnames);
5489 }
5490 else
5491 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5492
5493 /* If Shift held down, change to first file's directory. If the first
5494 * item is a directory, change to that directory (and let the explorer
5495 * plugin show the contents). */
5496 if (p != NULL)
5497 {
5498 if (mch_isdir(p))
5499 {
5500 if (mch_chdir((char *)p) == 0)
5501 shorten_fnames(TRUE);
5502 }
Bram Moolenaarf4aba792018-02-03 19:17:36 +01005503 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 shorten_fnames(TRUE);
5505 vim_free(p);
5506 }
5507
5508 /* Update the screen display */
5509 update_screen(NOT_VALID);
5510# ifdef FEAT_MENU
5511 gui_update_menus(0);
5512# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005513#ifdef FEAT_TITLE
5514 maketitle();
5515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005517 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005519
5520 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522#endif