blob: b9b73a3fd0849d136de3818236db0fab7a3aea9e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000017static void set_guifontwide __ARGS((char_u *font_name));
18#endif
19static void gui_check_pos __ARGS((void));
20static void gui_position_components __ARGS((int));
21static void gui_outstr __ARGS((char_u *, int));
22static int gui_screenchar __ARGS((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 Moolenaar071d4272004-06-13 20:20:40 +000024static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25#endif
26static void gui_delete_lines __ARGS((int row, int count));
27static void gui_insert_lines __ARGS((int row, int count));
28static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30static int gui_has_tabline __ARGS((void));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33static colnr_T scroll_line_len __ARGS((linenr_T lnum));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020034static linenr_T gui_find_longest_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static void gui_update_horiz_scrollbar __ARGS((int));
Bram Moolenaar3918c952005-03-15 22:34:55 +000036static void gui_set_fg_color __ARGS((char_u *name));
37static void gui_set_bg_color __ARGS((char_u *name));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static win_T *xy2win __ARGS((int x, int y));
39
Bram Moolenaar05514102012-08-29 16:34:27 +020040#if defined(UNIX) && !defined(MACOS_X) && !defined(__APPLE__)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020041# define MAY_FORK
42static void gui_do_fork __ARGS((void));
43
44static int gui_read_child_pipe __ARGS((int fd));
45
46/* Return values for gui_read_child_pipe */
47enum {
48 GUI_CHILD_IO_ERROR,
49 GUI_CHILD_OK,
50 GUI_CHILD_FAILED
51};
52
53#endif /* MAY_FORK */
54
55static void gui_attempt_start __ARGS((void));
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static int can_update_cursor = TRUE; /* can display the cursor */
58
59/*
60 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
61 * this makes the thumb indicate the part of the text that is shown. Motif
62 * can't do this.
63 */
64#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
65# define SCROLL_PAST_END
66#endif
67
68/*
69 * gui_start -- Called when user wants to start the GUI.
70 *
71 * Careful: This function can be called recursively when there is a ":gui"
72 * command in the .gvimrc file. Only the first call should fork, not the
73 * recursive call.
74 */
75 void
76gui_start()
77{
78 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 static int recursive = 0;
80
81 old_term = vim_strsave(T_NAME);
82
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 settmode(TMODE_COOK); /* stop RAW mode */
84 if (full_screen)
85 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 full_screen = FALSE;
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 ++recursive;
89
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090#ifdef MAY_FORK
91 /*
92 * Quit the current process and continue in the child.
93 * Makes "gvim file" disconnect from the shell it was started in.
94 * Don't do this when Vim was started with "-f" or the 'f' flag is present
95 * in 'guioptions'.
96 */
97 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
98 {
99 gui_do_fork();
100 }
101 else
102#endif
103 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200104#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200105 /* If there is 'f' in 'guioptions' and specify -g argument,
106 * gui_mch_init_check() was not called yet. */
107 if (gui_mch_init_check() != OK)
108 exit(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200109#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200110 gui_attempt_start();
111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
113 if (!gui.in_use) /* failed to start GUI */
114 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200115 /* Back to old term settings
116 *
117 * FIXME: If we got here because a child process failed and flagged to
118 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
119 * hit an X11 I/O error and do a longjmp(), leaving recursive
120 * permanently set to 1. This is probably not as big a problem as it
121 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
122 * return "OK" unconditionally, so it would be very difficult to
123 * actually hit this case.
124 */
125 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 settmode(TMODE_RAW); /* restart RAW mode */
127#ifdef FEAT_TITLE
128 set_title_defaults(); /* set 'title' and 'icon' again */
129#endif
130 }
131
132 vim_free(old_term);
133
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200134#ifdef FEAT_AUTOCMD
135 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
136 * the GUIFailed event. */
137 gui_mch_update();
138 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
139 NULL, NULL, FALSE, curbuf);
140#endif
141 --recursive;
142}
143
144/*
145 * Set_termname() will call gui_init() to start the GUI.
146 * Set the "starting" flag, to indicate that the GUI will start.
147 *
148 * We don't want to open the GUI shell until after we've read .gvimrc,
149 * otherwise we don't know what font we will use, and hence we don't know
150 * what size the shell should be. So if there are errors in the .gvimrc
151 * file, they will have to go to the terminal: Set full_screen to FALSE.
152 * full_screen will be set to TRUE again by a successful termcapinit().
153 */
154 static void
155gui_attempt_start()
156{
157 static int recursive = 0;
158
159 ++recursive;
160 gui.starting = TRUE;
161
162#ifdef FEAT_GUI_GTK
163 gui.event_time = GDK_CURRENT_TIME;
164#endif
165
166 termcapinit((char_u *)"builtin_gui");
167 gui.starting = recursive - 1;
168
Bram Moolenaar9372a112005-12-06 19:59:18 +0000169#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200171 {
172# ifdef FEAT_EVAL
173 Window x11_window;
174 Display *x11_display;
175
176 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
177 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
178# endif
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 /* Display error messages in a dialog now. */
181 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 --recursive;
185}
186
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200187#ifdef MAY_FORK
188
189/* for waitpid() */
190# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
191# include <sys/wait.h>
192# endif
193
194/*
195 * Create a new process, by forking. In the child, start the GUI, and in
196 * the parent, exit.
197 *
198 * If something goes wrong, this will return with gui.in_use still set
199 * to FALSE, in which case the caller should continue execution without
200 * the GUI.
201 *
202 * If the child fails to start the GUI, then the child will exit and the
203 * parent will return. If the child succeeds, then the parent will exit
204 * and the child will return.
205 */
206 static void
207gui_do_fork()
208{
209#ifdef __QNXNTO__
210 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
211 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
212 gui_attempt_start();
213 return;
214#else
215 int pipefd[2]; /* pipe between parent and child */
216 int pipe_error;
217 int status;
218 int exit_status;
219 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200220
221 /* Setup a pipe between the child and the parent, so that the parent
222 * knows when the child has done the setsid() call and is allowed to
223 * exit. */
224 pipe_error = (pipe(pipefd) < 0);
225 pid = fork();
226 if (pid < 0) /* Fork error */
227 {
228 EMSG(_("E851: Failed to create a new process for the GUI"));
229 return;
230 }
231 else if (pid > 0) /* Parent */
232 {
233 /* Give the child some time to do the setsid(), otherwise the
234 * exit() may kill the child too (when starting gvim from inside a
235 * gvim). */
236 if (!pipe_error)
237 {
238 /* The read returns when the child closes the pipe (or when
239 * the child dies for some reason). */
240 close(pipefd[1]);
241 status = gui_read_child_pipe(pipefd[0]);
242 if (status == GUI_CHILD_FAILED)
243 {
244 /* The child failed to start the GUI, so the caller must
245 * continue. There may be more error information written
246 * to stderr by the child. */
247# ifdef __NeXT__
248 wait4(pid, &exit_status, 0, (struct rusage *)0);
249# else
250 waitpid(pid, &exit_status, 0);
251# endif
252 EMSG(_("E852: The child process failed to start the GUI"));
253 return;
254 }
255 else if (status == GUI_CHILD_IO_ERROR)
256 {
257 pipe_error = TRUE;
258 }
259 /* else GUI_CHILD_OK: parent exit */
260 }
261
262 if (pipe_error)
263 ui_delay(300L, TRUE);
264
265 /* When swapping screens we may need to go to the next line, e.g.,
266 * after a hit-enter prompt and using ":gui". */
267 if (newline_on_exit)
268 mch_errmsg("\r\n");
269
270 /*
271 * The parent must skip the normal exit() processing, the child
272 * will do it. For example, GTK messes up signals when exiting.
273 */
274 _exit(0);
275 }
276 /* Child */
277
Bram Moolenaar29695702012-05-18 17:03:18 +0200278#ifdef FEAT_GUI_GTK
279 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
280 if (gui_mch_init_check() != OK)
281 exit(1);
282#endif
283
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200284# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
285 /*
286 * Change our process group. On some systems/shells a CTRL-C in the
287 * shell where Vim was started would otherwise kill gvim!
288 */
289# if defined(HAVE_SETSID)
290 (void)setsid();
291# else
292 (void)setpgid(0, 0);
293# endif
294# endif
295 if (!pipe_error)
296 close(pipefd[0]);
297
298# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
299 /* Tell the session manager our new PID */
300 gui_mch_forked();
301# endif
302
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200303 /* Try to start the GUI */
304 gui_attempt_start();
305
306 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200307 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200308 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200309 if (gui.in_use)
310 write_eintr(pipefd[1], "ok", 3);
311 else
312 write_eintr(pipefd[1], "fail", 5);
313 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200314 }
315
316 /* If we failed to start the GUI, exit now. */
317 if (!gui.in_use)
318 exit(1);
319#endif
320}
321
322/*
323 * Read from a pipe assumed to be connected to the child process (this
324 * function is called from the parent).
325 * Return GUI_CHILD_OK if the child successfully started the GUI,
326 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
327 * some other error.
328 *
329 * The file descriptor will be closed before the function returns.
330 */
331 static int
332gui_read_child_pipe(int fd)
333{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200334 long bytes_read;
335#define READ_BUFFER_SIZE 10
336 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200338 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
339#undef READ_BUFFER_SIZE
340 close(fd);
341 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200342 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200343 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200344 if (strcmp(buffer, "ok") == 0)
345 return GUI_CHILD_OK;
346 return GUI_CHILD_FAILED;
347}
348
349#endif /* MAY_FORK */
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351/*
352 * Call this when vim starts up, whether or not the GUI is started
353 */
354 void
355gui_prepare(argc, argv)
356 int *argc;
357 char **argv;
358{
359 gui.in_use = FALSE; /* No GUI yet (maybe later) */
360 gui.starting = FALSE; /* No GUI yet (maybe later) */
361 gui_mch_prepare(argc, argv);
362}
363
364/*
365 * Try initializing the GUI and check if it can be started.
366 * Used from main() to check early if "vim -g" can start the GUI.
367 * Used from gui_init() to prepare for starting the GUI.
368 * Returns FAIL or OK.
369 */
370 int
371gui_init_check()
372{
373 static int result = MAYBE;
374
375 if (result != MAYBE)
376 {
377 if (result == FAIL)
378 EMSG(_("E229: Cannot start the GUI"));
379 return result;
380 }
381
382 gui.shell_created = FALSE;
383 gui.dying = FALSE;
384 gui.in_focus = TRUE; /* so the guicursor setting works */
385 gui.dragged_sb = SBAR_NONE;
386 gui.dragged_wp = NULL;
387 gui.pointer_hidden = FALSE;
388 gui.col = 0;
389 gui.row = 0;
390 gui.num_cols = Columns;
391 gui.num_rows = Rows;
392
393 gui.cursor_is_valid = FALSE;
394 gui.scroll_region_top = 0;
395 gui.scroll_region_bot = Rows - 1;
396 gui.scroll_region_left = 0;
397 gui.scroll_region_right = Columns - 1;
398 gui.highlight_mask = HL_NORMAL;
399 gui.char_width = 1;
400 gui.char_height = 1;
401 gui.char_ascent = 0;
402 gui.border_width = 0;
403
404 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200405#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 gui.bold_font = NOFONT;
407 gui.ital_font = NOFONT;
408 gui.boldital_font = NOFONT;
409# ifdef FEAT_XFONTSET
410 gui.fontset = NOFONTSET;
411# endif
412#endif
413
414#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200415# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416# ifdef FONTSET_ALWAYS
417 gui.menu_fontset = NOFONTSET;
418# else
419 gui.menu_font = NOFONT;
420# endif
421# endif
422 gui.menu_is_active = TRUE; /* default: include menu */
423# ifndef FEAT_GUI_GTK
424 gui.menu_height = MENU_DEFAULT_HEIGHT;
425 gui.menu_width = 0;
426# endif
427#endif
428#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
429 gui.toolbar_height = 0;
430#endif
431#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
432 gui.footer_height = 0;
433#endif
434#ifdef FEAT_BEVAL_TIP
435 gui.tooltip_fontset = NOFONTSET;
436#endif
437
438 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
439 gui.prev_wrap = -1;
440
441#ifdef ALWAYS_USE_GUI
442 result = OK;
443#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200444# ifdef FEAT_GUI_GTK
445 /*
446 * Note: Don't call gtk_init_check() before fork, it will be called after
447 * the fork. When calling it before fork, it make vim hang for a while.
448 * See gui_do_fork().
449 * Use a simpler check if the GUI window can probably be opened.
450 */
451 result = gui.dofork ? gui_mch_early_init_check() : gui_mch_init_check();
452# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455#endif
456 return result;
457}
458
459/*
460 * This is the call which starts the GUI.
461 */
462 void
463gui_init()
464{
465 win_T *wp;
466 static int recursive = 0;
467
468 /*
469 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
470 * function will then be executed at the first call, the rest by the
471 * recursive call. This allow the shell to be opened halfway reading a
472 * gvimrc file.
473 */
474 if (!recursive)
475 {
476 ++recursive;
477
478 clip_init(TRUE);
479
480 /* If can't initialize, don't try doing the rest */
481 if (gui_init_check() == FAIL)
482 {
483 --recursive;
484 clip_init(FALSE);
485 return;
486 }
487
488 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000489 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
490 * breaks the Paste toolbar button.
491 */
492 set_option_value((char_u *)"paste", 0L, NULL, 0);
493
494 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * Set up system-wide default menus.
496 */
497#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
498 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
499 {
500 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000501 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 sys_menu = FALSE;
503 }
504#endif
505
506 /*
507 * Switch on the mouse by default, unless the user changed it already.
508 * This can then be changed in the .gvimrc.
509 */
510 if (!option_was_set((char_u *)"mouse"))
511 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000512 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 /*
515 * If -U option given, use only the initializations from that file and
516 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
517 */
518 if (use_gvimrc != NULL)
519 {
520 if (STRCMP(use_gvimrc, "NONE") != 0
521 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000522 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
524 }
525 else
526 {
527 /*
528 * Get system wide defaults for gvim, only when file name defined.
529 */
530#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000531 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#endif
533
534 /*
535 * Try to read GUI initialization commands from the following
536 * places:
537 * - environment variable GVIMINIT
538 * - the user gvimrc file (~/.gvimrc)
539 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
540 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
541 * The first that exists is used, the rest is ignored.
542 */
543 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000544 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
545 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000547 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
548 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200550#ifdef USR_GVIMRC_FILE3
551 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
552 DOSO_GVIMRC) == FAIL
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 )
555 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200556#ifdef USR_GVIMRC_FILE4
557 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559 }
560
561 /*
562 * Read initialization commands from ".gvimrc" in current
563 * directory. This is only done if the 'exrc' option is set.
564 * Because of security reasons we disallow shell and write
565 * commands now, except for unix if the file is owned by the user
566 * or 'secure' option has been reset in environment of global
567 * ".gvimrc".
568 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
569 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
570 */
571 if (p_exrc)
572 {
573#ifdef UNIX
574 {
575 struct stat s;
576
577 /* if ".gvimrc" file is not owned by user, set 'secure'
578 * mode */
579 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
580 secure = p_secure;
581 }
582#else
583 secure = p_secure;
584#endif
585
586 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
587 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
588#ifdef SYS_GVIMRC_FILE
589 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
590 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
591#endif
592#ifdef USR_GVIMRC_FILE2
593 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
594 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
595#endif
596#ifdef USR_GVIMRC_FILE3
597 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
598 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
599#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200600#ifdef USR_GVIMRC_FILE4
601 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
602 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000605 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607 if (secure == 2)
608 need_wait_return = TRUE;
609 secure = 0;
610 }
611 }
612
613 if (need_wait_return || msg_didany)
614 wait_return(TRUE);
615
616 --recursive;
617 }
618
619 /* If recursive call opened the shell, return here from the first call */
620 if (gui.in_use)
621 return;
622
623 /*
624 * Create the GUI shell.
625 */
626 gui.in_use = TRUE; /* Must be set after menus have been set up */
627 if (gui_mch_init() == FAIL)
628 goto error;
629
630 /* Avoid a delay for an error message that was printed in the terminal
631 * where Vim was started. */
632 emsg_on_display = FALSE;
633 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000634 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 need_wait_return = FALSE;
636 msg_didany = FALSE;
637
638 /*
639 * Check validity of any generic resources that may have been loaded.
640 */
641 if (gui.border_width < 0)
642 gui.border_width = 0;
643
644 /*
645 * Set up the fonts. First use a font specified with "-fn" or "-font".
646 */
647 if (font_argument != NULL)
648 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
649 if (
650#ifdef FEAT_XFONTSET
651 (*p_guifontset == NUL
652 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
653#endif
654 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
655 : p_guifont, FALSE) == FAIL)
656 {
657 EMSG(_("E665: Cannot start GUI, no valid font found"));
658 goto error2;
659 }
660#ifdef FEAT_MBYTE
661 if (gui_get_wide_font() == FAIL)
662 EMSG(_("E231: 'guifontwide' invalid"));
663#endif
664
665 gui.num_cols = Columns;
666 gui.num_rows = Rows;
667 gui_reset_scroll_region();
668
669 /* Create initial scrollbars */
670 FOR_ALL_WINDOWS(wp)
671 {
672 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
673 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
674 }
675 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
676
677#ifdef FEAT_MENU
678 gui_create_initial_menus(root_menu);
679#endif
680#ifdef FEAT_SUN_WORKSHOP
681 if (usingSunWorkShop)
682 workshop_init();
683#endif
684#ifdef FEAT_SIGN_ICONS
685 sign_gui_started();
686#endif
687
688 /* Configure the desired menu and scrollbars */
689 gui_init_which_components(NULL);
690
691 /* All components of the GUI have been created now */
692 gui.shell_created = TRUE;
693
694#ifndef FEAT_GUI_GTK
695 /* Set the shell size, adjusted for the screen size. For GTK this only
696 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000697 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#endif
699#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
700 /* Need to set the size of the menubar after all the menus have been
701 * created. */
702 gui_mch_compute_menu_height((Widget)0);
703#endif
704
705 /*
706 * Actually open the GUI shell.
707 */
708 if (gui_mch_open() != FAIL)
709 {
710#ifdef FEAT_TITLE
711 maketitle();
712 resettitle();
713#endif
714 init_gui_options();
715#ifdef FEAT_ARABIC
716 /* Our GUI can't do bidi. */
717 p_tbidi = FALSE;
718#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000719#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 /* Give GTK+ a chance to put all widget's into place. */
721 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000722
723# ifdef FEAT_MENU
724 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
725 * It was still there to make F10 work. */
726 if (vim_strchr(p_go, GO_MENUS) == NULL)
727 {
728 --gui.starting;
729 gui_mch_enable_menu(FALSE);
730 ++gui.starting;
731 gui_mch_update();
732 }
733# endif
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000736 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000738 /* When 'lines' was set while starting up the topframe may have to be
739 * resized. */
740 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000741
742#ifdef FEAT_BEVAL
743 /* Always create the Balloon Evaluation area, but disable it when
744 * 'ballooneval' is off */
745# ifdef FEAT_GUI_GTK
746 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
747 &general_beval_cb, NULL);
748# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000749# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000750 {
751 extern Widget textArea;
752 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000753 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000754 }
755# else
756# ifdef FEAT_GUI_W32
757 balloonEval = gui_mch_create_beval_area(NULL, NULL,
758 &general_beval_cb, NULL);
759# endif
760# endif
761# endif
762 if (!p_beval)
763 gui_mch_disable_beval_area(balloonEval);
764#endif
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
767 if (!im_xim_isvalid_imactivate())
768 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
769#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000770 /* When 'cmdheight' was set during startup it may not have taken
771 * effect yet. */
772 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000773 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 return;
776 }
777
778error2:
779#ifdef FEAT_GUI_X11
780 /* undo gui_mch_init() */
781 gui_mch_uninit();
782#endif
783
784error:
785 gui.in_use = FALSE;
786 clip_init(FALSE);
787}
788
789
790 void
791gui_exit(rc)
792 int rc;
793{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* don't free the fonts, it leads to a BUS error
795 * richard@whitequeen.com Jul 99 */
796 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 gui.in_use = FALSE;
798 gui_mch_exit(rc);
799}
800
Bram Moolenaar9372a112005-12-06 19:59:18 +0000801#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000803# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804/*
805 * Called when the GUI shell is closed by the user. If there are no changed
806 * files Vim exits, otherwise there will be a dialog to ask the user what to
807 * do.
808 * When this function returns, Vim should NOT exit!
809 */
810 void
811gui_shell_closed()
812{
813 cmdmod_T save_cmdmod;
814
815 save_cmdmod = cmdmod;
816
817 /* Only exit when there are no changed files */
818 exiting = TRUE;
819# ifdef FEAT_BROWSE
820 cmdmod.browse = TRUE;
821# endif
822# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
823 cmdmod.confirm = TRUE;
824# endif
825 /* If there are changed buffers, present the user with a dialog if
826 * possible, otherwise give an error message. */
827 if (!check_changed_any(FALSE))
828 getout(0);
829
830 exiting = FALSE;
831 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000832 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834#endif
835
836/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200837 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 * first font name that works is used. If none is found, use the default
839 * font.
840 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
841 * Return OK when able to set the font. When it failed FAIL is returned and
842 * the fonts are unchanged.
843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 int
845gui_init_font(font_list, fontset)
846 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000847 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849#define FONTLEN 320
850 char_u font_name[FONTLEN];
851 int font_list_empty = FALSE;
852 int ret = FAIL;
853
854 if (!gui.in_use)
855 return FAIL;
856
857 font_name[0] = NUL;
858 if (*font_list == NUL)
859 font_list_empty = TRUE;
860 else
861 {
862#ifdef FEAT_XFONTSET
863 /* When using a fontset, the whole list of fonts is one name. */
864 if (fontset)
865 ret = gui_mch_init_font(font_list, TRUE);
866 else
867#endif
868 while (*font_list != NUL)
869 {
870 /* Isolate one comma separated font name. */
871 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
872
873 /* Careful!!! The Win32 version of gui_mch_init_font(), when
874 * called with "*" will change p_guifont to the selected font
875 * name, which frees the old value. This makes font_list
876 * invalid. Thus when OK is returned here, font_list must no
877 * longer be used! */
878 if (gui_mch_init_font(font_name, FALSE) == OK)
879 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200880#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 /* If it's a Unicode font, try setting 'guifontwide' to a
882 * similar double-width font. */
883 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
884 && strstr((char *)font_name, "10646") != NULL)
885 set_guifontwide(font_name);
886#endif
887 ret = OK;
888 break;
889 }
890 }
891 }
892
893 if (ret != OK
894 && STRCMP(font_list, "*") != 0
895 && (font_list_empty || gui.norm_font == NOFONT))
896 {
897 /*
898 * Couldn't load any font in 'font_list', keep the current font if
899 * there is one. If 'font_list' is empty, or if there is no current
900 * font, tell gui_mch_init_font() to try to find a font we can load.
901 */
902 ret = gui_mch_init_font(NULL, FALSE);
903 }
904
905 if (ret == OK)
906 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200907#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 /* Set normal font as current font */
909# ifdef FEAT_XFONTSET
910 if (gui.fontset != NOFONTSET)
911 gui_mch_set_fontset(gui.fontset);
912 else
913# endif
914 gui_mch_set_font(gui.norm_font);
915#endif
Bram Moolenaarb0316262012-11-20 12:03:06 +0100916 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 return ret;
920}
921
922#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200923# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924/*
925 * Try setting 'guifontwide' to a font twice as wide as "name".
926 */
927 static void
928set_guifontwide(name)
929 char_u *name;
930{
931 int i = 0;
932 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
933 char_u *wp = NULL;
934 char_u *p;
935 GuiFont font;
936
937 wp = wide_name;
938 for (p = name; *p != NUL; ++p)
939 {
940 *wp++ = *p;
941 if (*p == '-')
942 {
943 ++i;
944 if (i == 6) /* font type: change "--" to "-*-" */
945 {
946 if (p[1] == '-')
947 *wp++ = '*';
948 }
949 else if (i == 12) /* found the width */
950 {
951 ++p;
952 i = getdigits(&p);
953 if (i != 0)
954 {
955 /* Double the width specification. */
956 sprintf((char *)wp, "%d%s", i * 2, p);
957 font = gui_mch_get_font(wide_name, FALSE);
958 if (font != NOFONT)
959 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000960 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 gui.wide_font = font;
962 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000963 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 }
966 break;
967 }
968 }
969 }
970}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200971# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973/*
974 * Get the font for 'guifontwide'.
975 * Return FAIL for an invalid font name.
976 */
977 int
978gui_get_wide_font()
979{
980 GuiFont font = NOFONT;
981 char_u font_name[FONTLEN];
982 char_u *p;
983
984 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
985 return OK; /* Will give an error message later. */
986
987 if (p_guifontwide != NULL && *p_guifontwide != NUL)
988 {
989 for (p = p_guifontwide; *p != NUL; )
990 {
991 /* Isolate one comma separated font name. */
992 (void)copy_option_part(&p, font_name, FONTLEN, ",");
993 font = gui_mch_get_font(font_name, FALSE);
994 if (font != NOFONT)
995 break;
996 }
997 if (font == NOFONT)
998 return FAIL;
999 }
1000
1001 gui_mch_free_font(gui.wide_font);
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001002# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1004 if (font != NOFONT && gui.norm_font != NOFONT
1005 && pango_font_description_equal(font, gui.norm_font))
1006 {
1007 gui.wide_font = NOFONT;
1008 gui_mch_free_font(font);
1009 }
1010 else
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 gui.wide_font = font;
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001013# ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001014 gui_mch_wide_font_changed();
Bram Moolenaarcdffbea2013-04-03 21:11:39 +02001015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return OK;
1017}
1018#endif
1019
1020 void
1021gui_set_cursor(row, col)
1022 int row;
1023 int col;
1024{
1025 gui.row = row;
1026 gui.col = col;
1027}
1028
1029/*
1030 * gui_check_pos - check if the cursor is on the screen.
1031 */
1032 static void
1033gui_check_pos()
1034{
1035 if (gui.row >= screen_Rows)
1036 gui.row = screen_Rows - 1;
1037 if (gui.col >= screen_Columns)
1038 gui.col = screen_Columns - 1;
1039 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1040 gui.cursor_is_valid = FALSE;
1041}
1042
1043/*
1044 * Redraw the cursor if necessary or when forced.
1045 * Careful: The contents of ScreenLines[] must match what is on the screen,
1046 * otherwise this goes wrong. May need to call out_flush() first.
1047 */
1048 void
1049gui_update_cursor(force, clear_selection)
1050 int force; /* when TRUE, update even when not moved */
1051 int clear_selection;/* clear selection under cursor */
1052{
1053 int cur_width = 0;
1054 int cur_height = 0;
1055 int old_hl_mask;
1056 int idx;
1057 int id;
1058 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1059 int cattr; /* cursor attributes */
1060 int attr;
1061 attrentry_T *aep = NULL;
1062
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001063 /* Don't update the cursor when halfway busy scrolling or the screen size
1064 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1065 if (!can_update_cursor || screen_Columns != gui.num_cols
1066 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return;
1068
1069 gui_check_pos();
1070 if (!gui.cursor_is_valid || force
1071 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1072 {
1073 gui_undraw_cursor();
1074 if (gui.row < 0)
1075 return;
1076#ifdef USE_IM_CONTROL
1077 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1078 im_set_position(gui.row, gui.col);
1079#endif
1080 gui.cursor_row = gui.row;
1081 gui.cursor_col = gui.col;
1082
1083 /* Only write to the screen after ScreenLines[] has been initialized */
1084 if (!screen_cleared || ScreenLines == NULL)
1085 return;
1086
1087 /* Clear the selection if we are about to write over it */
1088 if (clear_selection)
1089 clip_may_clear_selection(gui.row, gui.row);
1090 /* Check that the cursor is inside the shell (resizing may have made
1091 * it invalid) */
1092 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1093 return;
1094
1095 gui.cursor_is_valid = TRUE;
1096
1097 /*
1098 * How the cursor is drawn depends on the current mode.
1099 */
1100 idx = get_shape_idx(FALSE);
1101 if (State & LANGMAP)
1102 id = shape_table[idx].id_lm;
1103 else
1104 id = shape_table[idx].id;
1105
1106 /* get the colors and attributes for the cursor. Default is inverted */
1107 cfg = INVALCOLOR;
1108 cbg = INVALCOLOR;
1109 cattr = HL_INVERSE;
1110 gui_mch_set_blinking(shape_table[idx].blinkwait,
1111 shape_table[idx].blinkon,
1112 shape_table[idx].blinkoff);
1113 if (id > 0)
1114 {
1115 cattr = syn_id2colors(id, &cfg, &cbg);
1116#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1117 {
1118 static int iid;
1119 guicolor_T fg, bg;
1120
Bram Moolenaarc236c162008-07-13 17:41:49 +00001121 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001122# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001123 preedit_get_status()
1124# else
1125 im_get_status()
1126# endif
1127 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
1129 iid = syn_name2id((char_u *)"CursorIM");
1130 if (iid > 0)
1131 {
1132 syn_id2colors(iid, &fg, &bg);
1133 if (bg != INVALCOLOR)
1134 cbg = bg;
1135 if (fg != INVALCOLOR)
1136 cfg = fg;
1137 }
1138 }
1139 }
1140#endif
1141 }
1142
1143 /*
1144 * Get the attributes for the character under the cursor.
1145 * When no cursor color was given, use the character color.
1146 */
1147 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1148 if (attr > HL_ALL)
1149 aep = syn_gui_attr2entry(attr);
1150 if (aep != NULL)
1151 {
1152 attr = aep->ae_attr;
1153 if (cfg == INVALCOLOR)
1154 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1155 : aep->ae_u.gui.fg_color);
1156 if (cbg == INVALCOLOR)
1157 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1158 : aep->ae_u.gui.bg_color);
1159 }
1160 if (cfg == INVALCOLOR)
1161 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1162 if (cbg == INVALCOLOR)
1163 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1164
1165#ifdef FEAT_XIM
1166 if (aep != NULL)
1167 {
1168 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1169 : aep->ae_u.gui.bg_color);
1170 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1171 : aep->ae_u.gui.fg_color);
1172 if (xim_bg_color == INVALCOLOR)
1173 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1174 : gui.back_pixel;
1175 if (xim_fg_color == INVALCOLOR)
1176 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1177 : gui.norm_pixel;
1178 }
1179 else
1180 {
1181 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1182 : gui.back_pixel;
1183 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1184 : gui.norm_pixel;
1185 }
1186#endif
1187
1188 attr &= ~HL_INVERSE;
1189 if (cattr & HL_INVERSE)
1190 {
1191 cc = cbg;
1192 cbg = cfg;
1193 cfg = cc;
1194 }
1195 cattr &= ~HL_INVERSE;
1196
1197 /*
1198 * When we don't have window focus, draw a hollow cursor.
1199 */
1200 if (!gui.in_focus)
1201 {
1202 gui_mch_draw_hollow_cursor(cbg);
1203 return;
1204 }
1205
1206 old_hl_mask = gui.highlight_mask;
1207 if (shape_table[idx].shape == SHAPE_BLOCK
1208#ifdef FEAT_HANGULIN
1209 || composing_hangul
1210#endif
1211 )
1212 {
1213 /*
1214 * Draw the text character with the cursor colors. Use the
1215 * character attributes plus the cursor attributes.
1216 */
1217 gui.highlight_mask = (cattr | attr);
1218#ifdef FEAT_HANGULIN
1219 if (composing_hangul)
1220 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1221 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1222 else
1223#endif
1224 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1225 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1226 }
1227 else
1228 {
1229#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1230 int col_off = FALSE;
1231#endif
1232 /*
1233 * First draw the partial cursor, then overwrite with the text
1234 * character, using a transparent background.
1235 */
1236 if (shape_table[idx].shape == SHAPE_VER)
1237 {
1238 cur_height = gui.char_height;
1239 cur_width = (gui.char_width * shape_table[idx].percentage
1240 + 99) / 100;
1241 }
1242 else
1243 {
1244 cur_height = (gui.char_height * shape_table[idx].percentage
1245 + 99) / 100;
1246 cur_width = gui.char_width;
1247 }
1248#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001249 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1250 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 /* Double wide character. */
1253 if (shape_table[idx].shape != SHAPE_VER)
1254 cur_width += gui.char_width;
1255# ifdef FEAT_RIGHTLEFT
1256 if (CURSOR_BAR_RIGHT)
1257 {
1258 /* gui.col points to the left halve of the character but
1259 * the vertical line needs to be on the right halve.
1260 * A double-wide horizontal line is also drawn from the
1261 * right halve in gui_mch_draw_part_cursor(). */
1262 col_off = TRUE;
1263 ++gui.col;
1264 }
1265# endif
1266 }
1267#endif
1268 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1269#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1270 if (col_off)
1271 --gui.col;
1272#endif
1273
1274#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1275 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1276 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1277 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1278 (guicolor_T)0, (guicolor_T)0, 0);
1279#endif
1280 }
1281 gui.highlight_mask = old_hl_mask;
1282 }
1283}
1284
1285#if defined(FEAT_MENU) || defined(PROTO)
1286 void
1287gui_position_menu()
1288{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001289# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (gui.menu_is_active && gui.in_use)
1291 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1292# endif
1293}
1294#endif
1295
1296/*
1297 * Position the various GUI components (text area, menu). The vertical
1298 * scrollbars are NOT handled here. See gui_update_scrollbars().
1299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 static void
1301gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001302 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303{
1304 int text_area_x;
1305 int text_area_y;
1306 int text_area_width;
1307 int text_area_height;
1308
1309 /* avoid that moving components around generates events */
1310 ++hold_gui_events;
1311
1312 text_area_x = 0;
1313 if (gui.which_scrollbars[SBAR_LEFT])
1314 text_area_x += gui.scrollbar_width;
1315
1316 text_area_y = 0;
1317#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1318 gui.menu_width = total_width;
1319 if (gui.menu_is_active)
1320 text_area_y += gui.menu_height;
1321#endif
1322#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1323 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1324 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1325#endif
1326
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001327# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001328 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001329 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001330 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001331#endif
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1334 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1335 {
1336# ifdef FEAT_GUI_ATHENA
1337 gui_mch_set_toolbar_pos(0, text_area_y,
1338 gui.menu_width, gui.toolbar_height);
1339# endif
1340 text_area_y += gui.toolbar_height;
1341 }
1342#endif
1343
1344 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1345 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1346
1347 gui_mch_set_text_area_pos(text_area_x,
1348 text_area_y,
1349 text_area_width,
1350 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001351#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 + xim_get_status_area_height()
1353#endif
1354 );
1355#ifdef FEAT_MENU
1356 gui_position_menu();
1357#endif
1358 if (gui.which_scrollbars[SBAR_BOTTOM])
1359 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1360 text_area_x,
1361 text_area_y + text_area_height,
1362 text_area_width,
1363 gui.scrollbar_height);
1364 gui.left_sbar_x = 0;
1365 gui.right_sbar_x = text_area_x + text_area_width;
1366
1367 --hold_gui_events;
1368}
1369
Bram Moolenaar02743632005-07-25 20:42:36 +00001370/*
1371 * Get the width of the widgets and decorations to the side of the text area.
1372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 int
1374gui_get_base_width()
1375{
1376 int base_width;
1377
1378 base_width = 2 * gui.border_offset;
1379 if (gui.which_scrollbars[SBAR_LEFT])
1380 base_width += gui.scrollbar_width;
1381 if (gui.which_scrollbars[SBAR_RIGHT])
1382 base_width += gui.scrollbar_width;
1383 return base_width;
1384}
1385
Bram Moolenaar02743632005-07-25 20:42:36 +00001386/*
1387 * Get the height of the widgets and decorations above and below the text area.
1388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 int
1390gui_get_base_height()
1391{
1392 int base_height;
1393
1394 base_height = 2 * gui.border_offset;
1395 if (gui.which_scrollbars[SBAR_BOTTOM])
1396 base_height += gui.scrollbar_height;
1397#ifdef FEAT_GUI_GTK
1398 /* We can't take the sizes properly into account until anything is
1399 * realized. Therefore we recalculate all the values here just before
1400 * setting the size. (--mdcki) */
1401#else
1402# ifdef FEAT_MENU
1403 if (gui.menu_is_active)
1404 base_height += gui.menu_height;
1405# endif
1406# ifdef FEAT_TOOLBAR
1407 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1408# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1409 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1410# else
1411 base_height += gui.toolbar_height;
1412# endif
1413# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001414# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1415 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001416 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001417 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419# ifdef FEAT_FOOTER
1420 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1421 base_height += gui.footer_height;
1422# endif
1423# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1424 base_height += gui_mch_text_area_extra_height();
1425# endif
1426#endif
1427 return base_height;
1428}
1429
1430/*
1431 * Should be called after the GUI shell has been resized. Its arguments are
1432 * the new width and height of the shell in pixels.
1433 */
1434 void
1435gui_resize_shell(pixel_width, pixel_height)
1436 int pixel_width;
1437 int pixel_height;
1438{
1439 static int busy = FALSE;
1440
1441 if (!gui.shell_created) /* ignore when still initializing */
1442 return;
1443
1444 /*
1445 * Can't resize the screen while it is being redrawn. Remember the new
1446 * size and handle it later.
1447 */
1448 if (updating_screen || busy)
1449 {
1450 new_pixel_width = pixel_width;
1451 new_pixel_height = pixel_height;
1452 return;
1453 }
1454
1455again:
1456 busy = TRUE;
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 /* Flush pending output before redrawing */
1459 out_flush();
1460
1461 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001462 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464 gui_position_components(pixel_width);
1465
1466 gui_reset_scroll_region();
1467 /*
1468 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1469 * at the last line here (why does it have to be one row too low?).
1470 */
1471 if (State == ASKMORE || State == CONFIRM)
1472 gui.row = gui.num_rows;
1473
1474 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1475 * the safe side. */
1476 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1477 || gui.num_rows != Rows || gui.num_cols != Columns)
1478 shell_resized();
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 gui_update_scrollbars(TRUE);
1481 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001482#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 xim_set_status_area();
1484#endif
1485
1486 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 /*
1489 * We could have been called again while redrawing the screen.
1490 * Need to do it all again with the latest size then.
1491 */
1492 if (new_pixel_height)
1493 {
1494 pixel_width = new_pixel_width;
1495 pixel_height = new_pixel_height;
1496 new_pixel_width = 0;
1497 new_pixel_height = 0;
1498 goto again;
1499 }
1500}
1501
1502/*
1503 * Check if gui_resize_shell() must be called.
1504 */
1505 void
1506gui_may_resize_shell()
1507{
1508 int h, w;
1509
1510 if (new_pixel_height)
1511 {
1512 /* careful: gui_resize_shell() may postpone the resize again if we
1513 * were called indirectly by it */
1514 w = new_pixel_width;
1515 h = new_pixel_height;
1516 new_pixel_width = 0;
1517 new_pixel_height = 0;
1518 gui_resize_shell(w, h);
1519 }
1520}
1521
1522 int
1523gui_get_shellsize()
1524{
1525 Rows = gui.num_rows;
1526 Columns = gui.num_cols;
1527 return OK;
1528}
1529
1530/*
1531 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001532 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1533 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001536gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001537 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001539 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 int base_width;
1542 int base_height;
1543 int width;
1544 int height;
1545 int min_width;
1546 int min_height;
1547 int screen_w;
1548 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001549#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001550 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001551 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001552#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001553 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 if (!gui.shell_created)
1556 return;
1557
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001558#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 /* If not setting to a user specified size and maximized, calculate the
1560 * number of characters that fit in the maximized window. */
1561 if (!mustset && gui_mch_maximized())
1562 {
1563 gui_mch_newfont();
1564 return;
1565 }
1566#endif
1567
1568 base_width = gui_get_base_width();
1569 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001570 if (fit_to_display)
1571 /* Remember the original window position. */
1572 gui_mch_get_winpos(&x, &y);
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574#ifdef USE_SUN_WORKSHOP
1575 if (!mustset && usingSunWorkShop
1576 && workshop_get_width_height(&width, &height))
1577 {
1578 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1579 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1580 }
1581 else
1582#endif
1583 {
1584 width = Columns * gui.char_width + base_width;
1585 height = Rows * gui.char_height + base_height;
1586 }
1587
1588 if (fit_to_display)
1589 {
1590 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001591 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 Columns = (screen_w - base_width) / gui.char_width;
1594 if (Columns < MIN_COLUMNS)
1595 Columns = MIN_COLUMNS;
1596 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001597#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001598 ++did_adjust;
1599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001601 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 Rows = (screen_h - base_height) / gui.char_height;
1604 check_shellsize();
1605 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001606#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001607 ++did_adjust;
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001610#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001611 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1612 && height + gui.char_height >= screen_h))
1613 /* don't unmaximize if at maximum size */
1614 un_maximize = FALSE;
1615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617 gui.num_cols = Columns;
1618 gui.num_rows = Rows;
1619
1620 min_width = base_width + MIN_COLUMNS * gui.char_width;
1621 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001622#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001623 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001624#endif
1625
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001626#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001627 if (un_maximize)
1628 {
1629 /* If the window size is smaller than the screen unmaximize the
1630 * window, otherwise resizing won't work. */
1631 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1632 if ((width + gui.char_width < screen_w
1633 || height + gui.char_height * 2 < screen_h)
1634 && gui_mch_maximized())
1635 gui_mch_unmaximize();
1636 }
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001640 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001642 if (fit_to_display && x >= 0 && y >= 0)
1643 {
1644 /* Some window managers put the Vim window left of/above the screen.
1645 * Only change the position if it wasn't already negative before
1646 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 gui_mch_update();
1648 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1649 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1650 }
1651
1652 gui_position_components(width);
1653 gui_update_scrollbars(TRUE);
1654 gui_reset_scroll_region();
1655}
1656
1657/*
1658 * Called when Rows and/or Columns has changed.
1659 */
1660 void
1661gui_new_shellsize()
1662{
1663 gui_reset_scroll_region();
1664}
1665
1666/*
1667 * Make scroll region cover whole screen.
1668 */
1669 void
1670gui_reset_scroll_region()
1671{
1672 gui.scroll_region_top = 0;
1673 gui.scroll_region_bot = gui.num_rows - 1;
1674 gui.scroll_region_left = 0;
1675 gui.scroll_region_right = gui.num_cols - 1;
1676}
1677
1678 void
1679gui_start_highlight(mask)
1680 int mask;
1681{
1682 if (mask > HL_ALL) /* highlight code */
1683 gui.highlight_mask = mask;
1684 else /* mask */
1685 gui.highlight_mask |= mask;
1686}
1687
1688 void
1689gui_stop_highlight(mask)
1690 int mask;
1691{
1692 if (mask > HL_ALL) /* highlight code */
1693 gui.highlight_mask = HL_NORMAL;
1694 else /* mask */
1695 gui.highlight_mask &= ~mask;
1696}
1697
1698/*
1699 * Clear a rectangular region of the screen from text pos (row1, col1) to
1700 * (row2, col2) inclusive.
1701 */
1702 void
1703gui_clear_block(row1, col1, row2, col2)
1704 int row1;
1705 int col1;
1706 int row2;
1707 int col2;
1708{
1709 /* Clear the selection if we are about to write over it */
1710 clip_may_clear_selection(row1, row2);
1711
1712 gui_mch_clear_block(row1, col1, row2, col2);
1713
1714 /* Invalidate cursor if it was in this block */
1715 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1716 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1717 gui.cursor_is_valid = FALSE;
1718}
1719
1720/*
1721 * Write code to update the cursor later. This avoids the need to flush the
1722 * output buffer before calling gui_update_cursor().
1723 */
1724 void
1725gui_update_cursor_later()
1726{
1727 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1728}
1729
1730 void
1731gui_write(s, len)
1732 char_u *s;
1733 int len;
1734{
1735 char_u *p;
1736 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 int force_scrollbar = FALSE;
1739 static win_T *old_curwin = NULL;
1740
1741/* #define DEBUG_GUI_WRITE */
1742#ifdef DEBUG_GUI_WRITE
1743 {
1744 int i;
1745 char_u *str;
1746
1747 printf("gui_write(%d):\n ", len);
1748 for (i = 0; i < len; i++)
1749 if (s[i] == ESC)
1750 {
1751 if (i != 0)
1752 printf("\n ");
1753 printf("<ESC>");
1754 }
1755 else
1756 {
1757 str = transchar_byte(s[i]);
1758 if (str[0] && str[1])
1759 printf("<%s>", (char *)str);
1760 else
1761 printf("%s", (char *)str);
1762 }
1763 printf("\n");
1764 }
1765#endif
1766 while (len)
1767 {
1768 if (s[0] == ESC && s[1] == '|')
1769 {
1770 p = s + 2;
1771 if (VIM_ISDIGIT(*p))
1772 {
1773 arg1 = getdigits(&p);
1774 if (p > s + len)
1775 break;
1776 if (*p == ';')
1777 {
1778 ++p;
1779 arg2 = getdigits(&p);
1780 if (p > s + len)
1781 break;
1782 }
1783 }
1784 switch (*p)
1785 {
1786 case 'C': /* Clear screen */
1787 clip_scroll_selection(9999);
1788 gui_mch_clear_all();
1789 gui.cursor_is_valid = FALSE;
1790 force_scrollbar = TRUE;
1791 break;
1792 case 'M': /* Move cursor */
1793 gui_set_cursor(arg1, arg2);
1794 break;
1795 case 's': /* force cursor (shape) update */
1796 force_cursor = TRUE;
1797 break;
1798 case 'R': /* Set scroll region */
1799 if (arg1 < arg2)
1800 {
1801 gui.scroll_region_top = arg1;
1802 gui.scroll_region_bot = arg2;
1803 }
1804 else
1805 {
1806 gui.scroll_region_top = arg2;
1807 gui.scroll_region_bot = arg1;
1808 }
1809 break;
1810#ifdef FEAT_VERTSPLIT
1811 case 'V': /* Set vertical scroll region */
1812 if (arg1 < arg2)
1813 {
1814 gui.scroll_region_left = arg1;
1815 gui.scroll_region_right = arg2;
1816 }
1817 else
1818 {
1819 gui.scroll_region_left = arg2;
1820 gui.scroll_region_right = arg1;
1821 }
1822 break;
1823#endif
1824 case 'd': /* Delete line */
1825 gui_delete_lines(gui.row, 1);
1826 break;
1827 case 'D': /* Delete lines */
1828 gui_delete_lines(gui.row, arg1);
1829 break;
1830 case 'i': /* Insert line */
1831 gui_insert_lines(gui.row, 1);
1832 break;
1833 case 'I': /* Insert lines */
1834 gui_insert_lines(gui.row, arg1);
1835 break;
1836 case '$': /* Clear to end-of-line */
1837 gui_clear_block(gui.row, gui.col, gui.row,
1838 (int)Columns - 1);
1839 break;
1840 case 'h': /* Turn on highlighting */
1841 gui_start_highlight(arg1);
1842 break;
1843 case 'H': /* Turn off highlighting */
1844 gui_stop_highlight(arg1);
1845 break;
1846 case 'f': /* flash the window (visual bell) */
1847 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1848 break;
1849 default:
1850 p = s + 1; /* Skip the ESC */
1851 break;
1852 }
1853 len -= (int)(++p - s);
1854 s = p;
1855 }
1856 else if (
1857#ifdef EBCDIC
1858 CtrlChar(s[0]) != 0 /* Ctrl character */
1859#else
1860 s[0] < 0x20 /* Ctrl character */
1861#endif
1862#ifdef FEAT_SIGN_ICONS
1863 && s[0] != SIGN_BYTE
1864# ifdef FEAT_NETBEANS_INTG
1865 && s[0] != MULTISIGN_BYTE
1866# endif
1867#endif
1868 )
1869 {
1870 if (s[0] == '\n') /* NL */
1871 {
1872 gui.col = 0;
1873 if (gui.row < gui.scroll_region_bot)
1874 gui.row++;
1875 else
1876 gui_delete_lines(gui.scroll_region_top, 1);
1877 }
1878 else if (s[0] == '\r') /* CR */
1879 {
1880 gui.col = 0;
1881 }
1882 else if (s[0] == '\b') /* Backspace */
1883 {
1884 if (gui.col)
1885 --gui.col;
1886 }
1887 else if (s[0] == Ctrl_L) /* cursor-right */
1888 {
1889 ++gui.col;
1890 }
1891 else if (s[0] == Ctrl_G) /* Beep */
1892 {
1893 gui_mch_beep();
1894 }
1895 /* Other Ctrl character: shouldn't happen! */
1896
1897 --len; /* Skip this char */
1898 ++s;
1899 }
1900 else
1901 {
1902 p = s;
1903 while (len > 0 && (
1904#ifdef EBCDIC
1905 CtrlChar(*p) == 0
1906#else
1907 *p >= 0x20
1908#endif
1909#ifdef FEAT_SIGN_ICONS
1910 || *p == SIGN_BYTE
1911# ifdef FEAT_NETBEANS_INTG
1912 || *p == MULTISIGN_BYTE
1913# endif
1914#endif
1915 ))
1916 {
1917 len--;
1918 p++;
1919 }
1920 gui_outstr(s, (int)(p - s));
1921 s = p;
1922 }
1923 }
1924
1925 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1926 * set). */
1927 if (force_cursor)
1928 gui_update_cursor(TRUE, TRUE);
1929
1930 /* When switching to another window the dragging must have stopped.
1931 * Required for GTK, dragged_sb isn't reset. */
1932 if (old_curwin != curwin)
1933 gui.dragged_sb = SBAR_NONE;
1934
1935 /* Update the scrollbars after clearing the screen or when switched
1936 * to another window.
1937 * Update the horizontal scrollbar always, it's difficult to check all
1938 * situations where it might change. */
1939 if (force_scrollbar || old_curwin != curwin)
1940 gui_update_scrollbars(force_scrollbar);
1941 else
1942 gui_update_horiz_scrollbar(FALSE);
1943 old_curwin = curwin;
1944
1945 /*
1946 * We need to make sure this is cleared since Athena doesn't tell us when
1947 * he is done dragging. Do the same for GTK.
1948 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001949#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 gui.dragged_sb = SBAR_NONE;
1951#endif
1952
1953 gui_mch_flush(); /* In case vim decides to take a nap */
1954}
1955
1956/*
1957 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1958 * produces wrong results. Call gui_dont_update_cursor() before that code and
1959 * gui_can_update_cursor() afterwards.
1960 */
1961 void
1962gui_dont_update_cursor()
1963{
1964 if (gui.in_use)
1965 {
1966 /* Undraw the cursor now, we probably can't do it after the change. */
1967 gui_undraw_cursor();
1968 can_update_cursor = FALSE;
1969 }
1970}
1971
1972 void
1973gui_can_update_cursor()
1974{
1975 can_update_cursor = TRUE;
1976 /* No need to update the cursor right now, there is always more output
1977 * after scrolling. */
1978}
1979
1980 static void
1981gui_outstr(s, len)
1982 char_u *s;
1983 int len;
1984{
1985 int this_len;
1986#ifdef FEAT_MBYTE
1987 int cells;
1988#endif
1989
1990 if (len == 0)
1991 return;
1992
1993 if (len < 0)
1994 len = (int)STRLEN(s);
1995
1996 while (len > 0)
1997 {
1998#ifdef FEAT_MBYTE
1999 if (has_mbyte)
2000 {
2001 /* Find out how many chars fit in the current line. */
2002 cells = 0;
2003 for (this_len = 0; this_len < len; )
2004 {
2005 cells += (*mb_ptr2cells)(s + this_len);
2006 if (gui.col + cells > Columns)
2007 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002008 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 if (this_len > len)
2011 this_len = len; /* don't include following composing char */
2012 }
2013 else
2014#endif
2015 if (gui.col + len > Columns)
2016 this_len = Columns - gui.col;
2017 else
2018 this_len = len;
2019
2020 (void)gui_outstr_nowrap(s, this_len,
2021 0, (guicolor_T)0, (guicolor_T)0, 0);
2022 s += this_len;
2023 len -= this_len;
2024#ifdef FEAT_MBYTE
2025 /* fill up for a double-width char that doesn't fit. */
2026 if (len > 0 && gui.col < Columns)
2027 (void)gui_outstr_nowrap((char_u *)" ", 1,
2028 0, (guicolor_T)0, (guicolor_T)0, 0);
2029#endif
2030 /* The cursor may wrap to the next line. */
2031 if (gui.col >= Columns)
2032 {
2033 gui.col = 0;
2034 gui.row++;
2035 }
2036 }
2037}
2038
2039/*
2040 * Output one character (may be one or two display cells).
2041 * Caller must check for valid "off".
2042 * Returns FAIL or OK, just like gui_outstr_nowrap().
2043 */
2044 static int
2045gui_screenchar(off, flags, fg, bg, back)
2046 int off; /* Offset from start of screen */
2047 int flags;
2048 guicolor_T fg, bg; /* colors for cursor */
2049 int back; /* backup this many chars when using bold trick */
2050{
2051#ifdef FEAT_MBYTE
2052 char_u buf[MB_MAXBYTES + 1];
2053
2054 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2055 if (enc_utf8 && ScreenLines[off] == 0)
2056 return OK;
2057
2058 if (enc_utf8 && ScreenLinesUC[off] != 0)
2059 /* Draw UTF-8 multi-byte character. */
2060 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2061 flags, fg, bg, back);
2062
2063 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2064 {
2065 buf[0] = ScreenLines[off];
2066 buf[1] = ScreenLines2[off];
2067 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2068 }
2069
2070 /* Draw non-multi-byte character or DBCS character. */
2071 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002072 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 flags, fg, bg, back);
2074#else
2075 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2076#endif
2077}
2078
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002079#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080/*
2081 * Output the string at the given screen position. This is used in place
2082 * of gui_screenchar() where possible because Pango needs as much context
2083 * as possible to work nicely. It's a lot faster as well.
2084 */
2085 static int
2086gui_screenstr(off, len, flags, fg, bg, back)
2087 int off; /* Offset from start of screen */
2088 int len; /* string length in screen cells */
2089 int flags;
2090 guicolor_T fg, bg; /* colors for cursor */
2091 int back; /* backup this many chars when using bold trick */
2092{
2093 char_u *buf;
2094 int outlen = 0;
2095 int i;
2096 int retval;
2097
2098 if (len <= 0) /* "cannot happen"? */
2099 return OK;
2100
2101 if (enc_utf8)
2102 {
2103 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2104 if (buf == NULL)
2105 return OK; /* not much we could do here... */
2106
2107 for (i = off; i < off + len; ++i)
2108 {
2109 if (ScreenLines[i] == 0)
2110 continue; /* skip second half of double-width char */
2111
2112 if (ScreenLinesUC[i] == 0)
2113 buf[outlen++] = ScreenLines[i];
2114 else
2115 outlen += utfc_char2bytes(i, buf + outlen);
2116 }
2117
2118 buf[outlen] = NUL; /* only to aid debugging */
2119 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2120 vim_free(buf);
2121
2122 return retval;
2123 }
2124 else if (enc_dbcs == DBCS_JPNU)
2125 {
2126 buf = alloc((unsigned)(len * 2 + 1));
2127 if (buf == NULL)
2128 return OK; /* not much we could do here... */
2129
2130 for (i = off; i < off + len; ++i)
2131 {
2132 buf[outlen++] = ScreenLines[i];
2133
2134 /* handle double-byte single-width char */
2135 if (ScreenLines[i] == 0x8e)
2136 buf[outlen++] = ScreenLines2[i];
2137 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2138 buf[outlen++] = ScreenLines[++i];
2139 }
2140
2141 buf[outlen] = NUL; /* only to aid debugging */
2142 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2143 vim_free(buf);
2144
2145 return retval;
2146 }
2147 else
2148 {
2149 return gui_outstr_nowrap(&ScreenLines[off], len,
2150 flags, fg, bg, back);
2151 }
2152}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002153#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155/*
2156 * Output the given string at the current cursor position. If the string is
2157 * too long to fit on the line, then it is truncated.
2158 * "flags":
2159 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2160 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002161 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * background.
2163 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2164 * it.
2165 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2166 * FAIL (the caller should start drawing "back" chars back).
2167 */
2168 int
2169gui_outstr_nowrap(s, len, flags, fg, bg, back)
2170 char_u *s;
2171 int len;
2172 int flags;
2173 guicolor_T fg, bg; /* colors for cursor */
2174 int back; /* backup this many chars when using bold trick */
2175{
2176 long_u highlight_mask;
2177 long_u hl_mask_todo;
2178 guicolor_T fg_color;
2179 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002180 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002181#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 GuiFont font = NOFONT;
2183# ifdef FEAT_XFONTSET
2184 GuiFontset fontset = NOFONTSET;
2185# endif
2186#endif
2187 attrentry_T *aep = NULL;
2188 int draw_flags;
2189 int col = gui.col;
2190#ifdef FEAT_SIGN_ICONS
2191 int draw_sign = FALSE;
2192# ifdef FEAT_NETBEANS_INTG
2193 int multi_sign = FALSE;
2194# endif
2195#endif
2196
2197 if (len < 0)
2198 len = (int)STRLEN(s);
2199 if (len == 0)
2200 return OK;
2201
2202#ifdef FEAT_SIGN_ICONS
2203 if (*s == SIGN_BYTE
2204# ifdef FEAT_NETBEANS_INTG
2205 || *s == MULTISIGN_BYTE
2206# endif
2207 )
2208 {
2209# ifdef FEAT_NETBEANS_INTG
2210 if (*s == MULTISIGN_BYTE)
2211 multi_sign = TRUE;
2212# endif
2213 /* draw spaces instead */
2214 s = (char_u *)" ";
2215 if (len == 1 && col > 0)
2216 --col;
2217 len = 2;
2218 draw_sign = TRUE;
2219 highlight_mask = 0;
2220 }
2221 else
2222#endif
2223 if (gui.highlight_mask > HL_ALL)
2224 {
2225 aep = syn_gui_attr2entry(gui.highlight_mask);
2226 if (aep == NULL) /* highlighting not set */
2227 highlight_mask = 0;
2228 else
2229 highlight_mask = aep->ae_attr;
2230 }
2231 else
2232 highlight_mask = gui.highlight_mask;
2233 hl_mask_todo = highlight_mask;
2234
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002235#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /* Set the font */
2237 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2238 font = aep->ae_u.gui.font;
2239# ifdef FEAT_XFONTSET
2240 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2241 fontset = aep->ae_u.gui.fontset;
2242# endif
2243 else
2244 {
2245# ifdef FEAT_XFONTSET
2246 if (gui.fontset != NOFONTSET)
2247 fontset = gui.fontset;
2248 else
2249# endif
2250 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2251 {
2252 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2253 {
2254 font = gui.boldital_font;
2255 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2256 }
2257 else if (gui.bold_font != NOFONT)
2258 {
2259 font = gui.bold_font;
2260 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2261 }
2262 else
2263 font = gui.norm_font;
2264 }
2265 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2266 {
2267 font = gui.ital_font;
2268 hl_mask_todo &= ~HL_ITALIC;
2269 }
2270 else
2271 font = gui.norm_font;
2272 }
2273# ifdef FEAT_XFONTSET
2274 if (fontset != NOFONTSET)
2275 gui_mch_set_fontset(fontset);
2276 else
2277# endif
2278 gui_mch_set_font(font);
2279#endif
2280
2281 draw_flags = 0;
2282
2283 /* Set the color */
2284 bg_color = gui.back_pixel;
2285 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2286 {
2287 draw_flags |= DRAW_CURSOR;
2288 fg_color = fg;
2289 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002290 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 else if (aep != NULL)
2293 {
2294 fg_color = aep->ae_u.gui.fg_color;
2295 if (fg_color == INVALCOLOR)
2296 fg_color = gui.norm_pixel;
2297 bg_color = aep->ae_u.gui.bg_color;
2298 if (bg_color == INVALCOLOR)
2299 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002300 sp_color = aep->ae_u.gui.sp_color;
2301 if (sp_color == INVALCOLOR)
2302 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002307 sp_color = fg_color;
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2311 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002312#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 gui_mch_set_colors(bg_color, fg_color);
2314#else
2315 gui_mch_set_fg_color(bg_color);
2316 gui_mch_set_bg_color(fg_color);
2317#endif
2318 }
2319 else
2320 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002321#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 gui_mch_set_colors(fg_color, bg_color);
2323#else
2324 gui_mch_set_fg_color(fg_color);
2325 gui_mch_set_bg_color(bg_color);
2326#endif
2327 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002328 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330 /* Clear the selection if we are about to write over it */
2331 if (!(flags & GUI_MON_NOCLEAR))
2332 clip_may_clear_selection(gui.row, gui.row);
2333
2334
2335#ifndef MSWIN16_FASTTEXT
2336 /* If there's no bold font, then fake it */
2337 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2338 draw_flags |= DRAW_BOLD;
2339#endif
2340
2341 /*
2342 * When drawing bold or italic characters the spill-over from the left
2343 * neighbor may be destroyed. Let the caller backup to start redrawing
2344 * just after a blank.
2345 */
2346 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2347 return FAIL;
2348
Bram Moolenaare60acc12011-05-10 16:41:25 +02002349#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /* If there's no italic font, then fake it.
2351 * For GTK2, we don't need a different font for italic style. */
2352 if (hl_mask_todo & HL_ITALIC)
2353 draw_flags |= DRAW_ITALIC;
2354
2355 /* Do we underline the text? */
2356 if (hl_mask_todo & HL_UNDERLINE)
2357 draw_flags |= DRAW_UNDERL;
2358#else
2359 /* Do we underline the text? */
2360 if ((hl_mask_todo & HL_UNDERLINE)
2361# ifndef MSWIN16_FASTTEXT
2362 || (hl_mask_todo & HL_ITALIC)
2363# endif
2364 )
2365 draw_flags |= DRAW_UNDERL;
2366#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002367 /* Do we undercurl the text? */
2368 if (hl_mask_todo & HL_UNDERCURL)
2369 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002371 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (flags & GUI_MON_TRS_CURSOR)
2373 draw_flags |= DRAW_TRANSP;
2374
2375 /*
2376 * Draw the text.
2377 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002378#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 /* The value returned is the length in display cells */
2380 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2381#else
2382# ifdef FEAT_MBYTE
2383 if (enc_utf8)
2384 {
2385 int start; /* index of bytes to be drawn */
2386 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002387 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 int cn; /* cellwidth of current char */
2389 int i; /* index of current char */
2390 int c; /* current char value */
2391 int cl; /* byte length of current char */
2392 int comping; /* current char is composing */
2393 int scol = col; /* screen column */
Bram Moolenaar45933962013-01-23 17:43:57 +01002394 int curr_wide; /* use 'guifontwide' */
2395 int prev_wide = FALSE;
2396 int wide_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /* Break the string at a composing character, it has to be drawn on
2399 * top of the previous character. */
2400 start = 0;
2401 cells = 0;
2402 for (i = 0; i < len; i += cl)
2403 {
2404 c = utf_ptr2char(s + i);
2405 cn = utf_char2cells(c);
2406 if (cn > 1
2407# ifdef FEAT_XFONTSET
2408 && fontset == NOFONTSET
2409# endif
2410 && gui.wide_font != NOFONT)
Bram Moolenaar45933962013-01-23 17:43:57 +01002411 curr_wide = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 else
Bram Moolenaar45933962013-01-23 17:43:57 +01002413 curr_wide = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 comping = utf_iscomposing(c);
2415 if (!comping) /* count cells from non-composing chars */
2416 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002417 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (cl == 0) /* hit end of string */
2419 len = i + cl; /* len must be wrong "cannot happen" */
2420
Bram Moolenaar45933962013-01-23 17:43:57 +01002421 wide_changed = curr_wide != prev_wide;
2422
2423 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 * a composing character. */
Bram Moolenaar45933962013-01-23 17:43:57 +01002425 if (i + cl >= len || (comping && i > start) || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002426# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 || (cn > 1
2428# ifdef FEAT_XFONTSET
2429 /* No fontset: At least draw char after wide char at
2430 * right position. */
2431 && fontset == NOFONTSET
2432# endif
2433 )
2434# endif
2435 )
2436 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002437 if (comping || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 thislen = i - start;
2439 else
2440 thislen = i - start + cl;
2441 if (thislen > 0)
2442 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002443 if (prev_wide)
2444 gui_mch_set_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2446 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002447 if (prev_wide)
2448 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 start += thislen;
2450 }
2451 scol += cells;
2452 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002453 /* Adjust to not draw a character which width is changed
2454 * against with last one. */
2455 if (wide_changed && !comping)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002457 scol -= cn;
2458 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002461# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002462 /* No fontset: draw a space to fill the gap after a wide char
2463 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2465# ifdef FEAT_XFONTSET
2466 && fontset == NOFONTSET
2467# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002468 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2470 1, draw_flags);
2471# endif
2472 }
2473 /* Draw a composing char on top of the previous char. */
2474 if (comping)
2475 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002476# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002477 /* Carbon ATSUI autodraws composing char over previous char */
2478 gui_mch_draw_string(gui.row, scol, s + i, cl,
2479 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002480# else
2481 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2482 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 start = i + cl;
2485 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002486 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488 /* The stuff below assumes "len" is the length in screen columns. */
2489 len = scol - col;
2490 }
2491 else
2492# endif
2493 {
2494 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2495# ifdef FEAT_MBYTE
2496 if (enc_dbcs == DBCS_JPNU)
2497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 /* Get the length in display cells, this can be different from the
2499 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002500 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502# endif
2503 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002504#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2507 gui.col = col + len;
2508
2509 /* May need to invert it when it's part of the selection. */
2510 if (flags & GUI_MON_NOCLEAR)
2511 clip_may_redraw_selection(gui.row, col, len);
2512
2513 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2514 {
2515 /* Invalidate the old physical cursor position if we wrote over it */
2516 if (gui.cursor_row == gui.row
2517 && gui.cursor_col >= col
2518 && gui.cursor_col < col + len)
2519 gui.cursor_is_valid = FALSE;
2520 }
2521
2522#ifdef FEAT_SIGN_ICONS
2523 if (draw_sign)
2524 /* Draw the sign on top of the spaces. */
2525 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002526# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002527 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (multi_sign)
2529 netbeans_draw_multisign_indicator(gui.row);
2530# endif
2531#endif
2532
2533 return OK;
2534}
2535
2536/*
2537 * Un-draw the cursor. Actually this just redraws the character at the given
2538 * position. The character just before it too, for when it was in bold.
2539 */
2540 void
2541gui_undraw_cursor()
2542{
2543 if (gui.cursor_is_valid)
2544 {
2545#ifdef FEAT_HANGULIN
2546 if (composing_hangul
2547 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2548 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2549 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2550 gui.norm_pixel, gui.back_pixel, 0);
2551 else
2552 {
2553#endif
2554 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2555 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2556 && gui.cursor_col > 0)
2557 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2558 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2559#ifdef FEAT_HANGULIN
2560 if (composing_hangul)
2561 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2562 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2563 }
2564#endif
2565 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2566 * here in case it wasn't needed to undraw it. */
2567 gui.cursor_is_valid = FALSE;
2568 }
2569}
2570
2571 void
2572gui_redraw(x, y, w, h)
2573 int x;
2574 int y;
2575 int w;
2576 int h;
2577{
2578 int row1, col1, row2, col2;
2579
2580 row1 = Y_2_ROW(y);
2581 col1 = X_2_COL(x);
2582 row2 = Y_2_ROW(y + h - 1);
2583 col2 = X_2_COL(x + w - 1);
2584
2585 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2586
2587 /*
2588 * We may need to redraw the cursor, but don't take it upon us to change
2589 * its location after a scroll.
2590 * (maybe be more strict even and test col too?)
2591 * These things may be outside the update/clipping region and reality may
2592 * not reflect Vims internal ideas if these operations are clipped away.
2593 */
2594 if (gui.row == gui.cursor_row)
2595 gui_update_cursor(TRUE, TRUE);
2596}
2597
2598/*
2599 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2600 * from col1 to col2 (inclusive).
2601 * Return TRUE when the character before the first drawn character has
2602 * different attributes (may have to be redrawn too).
2603 */
2604 int
2605gui_redraw_block(row1, col1, row2, col2, flags)
2606 int row1;
2607 int col1;
2608 int row2;
2609 int col2;
2610 int flags; /* flags for gui_outstr_nowrap() */
2611{
2612 int old_row, old_col;
2613 long_u old_hl_mask;
2614 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002615 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 int idx, len;
2617 int back, nback;
2618 int retval = FALSE;
2619#ifdef FEAT_MBYTE
2620 int orig_col1, orig_col2;
2621#endif
2622
2623 /* Don't try to update when ScreenLines is not valid */
2624 if (!screen_cleared || ScreenLines == NULL)
2625 return retval;
2626
2627 /* Don't try to draw outside the shell! */
2628 /* Check everything, strange values may be caused by a big border width */
2629 col1 = check_col(col1);
2630 col2 = check_col(col2);
2631 row1 = check_row(row1);
2632 row2 = check_row(row2);
2633
2634 /* Remember where our cursor was */
2635 old_row = gui.row;
2636 old_col = gui.col;
2637 old_hl_mask = gui.highlight_mask;
2638#ifdef FEAT_MBYTE
2639 orig_col1 = col1;
2640 orig_col2 = col2;
2641#endif
2642
2643 for (gui.row = row1; gui.row <= row2; gui.row++)
2644 {
2645#ifdef FEAT_MBYTE
2646 /* When only half of a double-wide character is in the block, include
2647 * the other half. */
2648 col1 = orig_col1;
2649 col2 = orig_col2;
2650 off = LineOffset[gui.row];
2651 if (enc_dbcs != 0)
2652 {
2653 if (col1 > 0)
2654 col1 -= dbcs_screen_head_off(ScreenLines + off,
2655 ScreenLines + off + col1);
2656 col2 += dbcs_screen_tail_off(ScreenLines + off,
2657 ScreenLines + off + col2);
2658 }
2659 else if (enc_utf8)
2660 {
2661 if (ScreenLines[off + col1] == 0)
2662 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002663# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2665 ++col2;
2666# endif
2667 }
2668#endif
2669 gui.col = col1;
2670 off = LineOffset[gui.row] + gui.col;
2671 len = col2 - col1 + 1;
2672
2673 /* Find how many chars back this highlighting starts, or where a space
2674 * is. Needed for when the bold trick is used */
2675 for (back = 0; back < col1; ++back)
2676 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2677 || ScreenLines[off - 1 - back] == ' ')
2678 break;
2679 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2680 && ScreenLines[off - 1] != ' ');
2681
2682 /* Break it up in strings of characters with the same attributes. */
2683 /* Print UTF-8 characters individually. */
2684 while (len > 0)
2685 {
2686 first_attr = ScreenAttrs[off];
2687 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002688#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (enc_utf8 && ScreenLinesUC[off] != 0)
2690 {
2691 /* output multi-byte character separately */
2692 nback = gui_screenchar(off, flags,
2693 (guicolor_T)0, (guicolor_T)0, back);
2694 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2695 idx = 2;
2696 else
2697 idx = 1;
2698 }
2699 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2700 {
2701 /* output double-byte, single-width character separately */
2702 nback = gui_screenchar(off, flags,
2703 (guicolor_T)0, (guicolor_T)0, back);
2704 idx = 1;
2705 }
2706 else
2707#endif
2708 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002709#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 for (idx = 0; idx < len; ++idx)
2711 {
2712 if (enc_utf8 && ScreenLines[off + idx] == 0)
2713 continue; /* skip second half of double-width char */
2714 if (ScreenAttrs[off + idx] != first_attr)
2715 break;
2716 }
2717 /* gui_screenstr() takes care of multibyte chars */
2718 nback = gui_screenstr(off, idx, flags,
2719 (guicolor_T)0, (guicolor_T)0, back);
2720#else
2721 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2722 idx++)
2723 {
2724# ifdef FEAT_MBYTE
2725 /* Stop at a multi-byte Unicode character. */
2726 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2727 break;
2728 if (enc_dbcs == DBCS_JPNU)
2729 {
2730 /* Stop at a double-byte single-width char. */
2731 if (ScreenLines[off + idx] == 0x8e)
2732 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002733 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 + off + idx) == 2)
2735 ++idx; /* skip second byte of double-byte char */
2736 }
2737# endif
2738 }
2739 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2740 (guicolor_T)0, (guicolor_T)0, back);
2741#endif
2742 }
2743 if (nback == FAIL)
2744 {
2745 /* Must back up to start drawing where a bold or italic word
2746 * starts. */
2747 off -= back;
2748 len += back;
2749 gui.col -= back;
2750 }
2751 else
2752 {
2753 off += idx;
2754 len -= idx;
2755 }
2756 back = 0;
2757 }
2758 }
2759
2760 /* Put the cursor back where it was */
2761 gui.row = old_row;
2762 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002763 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 return retval;
2766}
2767
2768 static void
2769gui_delete_lines(row, count)
2770 int row;
2771 int count;
2772{
2773 if (count <= 0)
2774 return;
2775
2776 if (row + count > gui.scroll_region_bot)
2777 /* Scrolled out of region, just blank the lines out */
2778 gui_clear_block(row, gui.scroll_region_left,
2779 gui.scroll_region_bot, gui.scroll_region_right);
2780 else
2781 {
2782 gui_mch_delete_lines(row, count);
2783
2784 /* If the cursor was in the deleted lines it's now gone. If the
2785 * cursor was in the scrolled lines adjust its position. */
2786 if (gui.cursor_row >= row
2787 && gui.cursor_col >= gui.scroll_region_left
2788 && gui.cursor_col <= gui.scroll_region_right)
2789 {
2790 if (gui.cursor_row < row + count)
2791 gui.cursor_is_valid = FALSE;
2792 else if (gui.cursor_row <= gui.scroll_region_bot)
2793 gui.cursor_row -= count;
2794 }
2795 }
2796}
2797
2798 static void
2799gui_insert_lines(row, count)
2800 int row;
2801 int count;
2802{
2803 if (count <= 0)
2804 return;
2805
2806 if (row + count > gui.scroll_region_bot)
2807 /* Scrolled out of region, just blank the lines out */
2808 gui_clear_block(row, gui.scroll_region_left,
2809 gui.scroll_region_bot, gui.scroll_region_right);
2810 else
2811 {
2812 gui_mch_insert_lines(row, count);
2813
2814 if (gui.cursor_row >= gui.row
2815 && gui.cursor_col >= gui.scroll_region_left
2816 && gui.cursor_col <= gui.scroll_region_right)
2817 {
2818 if (gui.cursor_row <= gui.scroll_region_bot - count)
2819 gui.cursor_row += count;
2820 else if (gui.cursor_row <= gui.scroll_region_bot)
2821 gui.cursor_is_valid = FALSE;
2822 }
2823 }
2824}
2825
2826/*
2827 * The main GUI input routine. Waits for a character from the keyboard.
2828 * wtime == -1 Wait forever.
2829 * wtime == 0 Don't wait.
2830 * wtime > 0 Wait wtime milliseconds for a character.
2831 * Returns OK if a character was found to be available within the given time,
2832 * or FAIL otherwise.
2833 */
2834 int
2835gui_wait_for_chars(wtime)
2836 long wtime;
2837{
2838 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002840#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 /*
2842 * If we're going to wait a bit, update the menus and mouse shape for the
2843 * current State.
2844 */
2845 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 gui_update_menus(0);
2847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
2849 gui_mch_update();
2850 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 /* Before waiting, flush any output to the screen. */
2856 gui_mch_flush();
2857
2858 if (wtime > 0)
2859 {
2860 /* Blink when waiting for a character. Probably only does something
2861 * for showmatch() */
2862 gui_mch_start_blink();
2863 retval = gui_mch_wait_for_chars(wtime);
2864 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 return retval;
2866 }
2867
2868 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002869 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 */
2871 gui_mch_start_blink();
2872
Bram Moolenaar3918c952005-03-15 22:34:55 +00002873 retval = FAIL;
2874 /*
2875 * We may want to trigger the CursorHold event. First wait for
2876 * 'updatetime' and if nothing is typed within that time put the
2877 * K_CURSORHOLD key in the input buffer.
2878 */
2879 if (gui_mch_wait_for_chars(p_ut) == OK)
2880 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002882 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002884 char_u buf[3];
2885
2886 /* Put K_CURSORHOLD in the input buffer. */
2887 buf[0] = CSI;
2888 buf[1] = KS_EXTRA;
2889 buf[2] = (int)KE_CURSORHOLD;
2890 add_to_input_buf(buf, 3);
2891
2892 retval = OK;
2893 }
2894#endif
2895
2896 if (retval == FAIL)
2897 {
2898 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002899 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 gui_mch_stop_blink();
2904 return retval;
2905}
2906
2907/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002908 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 */
2910 static void
2911fill_mouse_coord(p, col, row)
2912 char_u *p;
2913 int col;
2914 int row;
2915{
2916 p[0] = (char_u)(col / 128 + ' ' + 1);
2917 p[1] = (char_u)(col % 128 + ' ' + 1);
2918 p[2] = (char_u)(row / 128 + ' ' + 1);
2919 p[3] = (char_u)(row % 128 + ' ' + 1);
2920}
2921
2922/*
2923 * Generic mouse support function. Add a mouse event to the input buffer with
2924 * the given properties.
2925 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2926 * MOUSE_X1, MOUSE_X2
2927 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002928 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2929 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 * x, y --- Coordinates of mouse in pixels.
2931 * repeated_click --- TRUE if this click comes only a short time after a
2932 * previous click.
2933 * modifiers --- Bit field which may be any of the following modifiers
2934 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2935 * This function will ignore drag events where the mouse has not moved to a new
2936 * character.
2937 */
2938 void
2939gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2940 int button;
2941 int x;
2942 int y;
2943 int repeated_click;
2944 int_u modifiers;
2945{
2946 static int prev_row = 0, prev_col = 0;
2947 static int prev_button = -1;
2948 static int num_clicks = 1;
2949 char_u string[10];
2950 enum key_extra button_char;
2951 int row, col;
2952#ifdef FEAT_CLIPBOARD
2953 int checkfor;
2954 int did_clip = FALSE;
2955#endif
2956
2957 /*
2958 * Scrolling may happen at any time, also while a selection is present.
2959 */
2960 switch (button)
2961 {
2962 case MOUSE_X1:
2963 button_char = KE_X1MOUSE;
2964 goto button_set;
2965 case MOUSE_X2:
2966 button_char = KE_X2MOUSE;
2967 goto button_set;
2968 case MOUSE_4:
2969 button_char = KE_MOUSEDOWN;
2970 goto button_set;
2971 case MOUSE_5:
2972 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002973 goto button_set;
2974 case MOUSE_6:
2975 button_char = KE_MOUSELEFT;
2976 goto button_set;
2977 case MOUSE_7:
2978 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979button_set:
2980 {
2981 /* Don't put events in the input queue now. */
2982 if (hold_gui_events)
2983 return;
2984
2985 string[3] = CSI;
2986 string[4] = KS_EXTRA;
2987 string[5] = (int)button_char;
2988
2989 /* Pass the pointer coordinates of the scroll event so that we
2990 * know which window to scroll. */
2991 row = gui_xy2colrow(x, y, &col);
2992 string[6] = (char_u)(col / 128 + ' ' + 1);
2993 string[7] = (char_u)(col % 128 + ' ' + 1);
2994 string[8] = (char_u)(row / 128 + ' ' + 1);
2995 string[9] = (char_u)(row % 128 + ' ' + 1);
2996
2997 if (modifiers == 0)
2998 add_to_input_buf(string + 3, 7);
2999 else
3000 {
3001 string[0] = CSI;
3002 string[1] = KS_MODIFIER;
3003 string[2] = 0;
3004 if (modifiers & MOUSE_SHIFT)
3005 string[2] |= MOD_MASK_SHIFT;
3006 if (modifiers & MOUSE_CTRL)
3007 string[2] |= MOD_MASK_CTRL;
3008 if (modifiers & MOUSE_ALT)
3009 string[2] |= MOD_MASK_ALT;
3010 add_to_input_buf(string, 10);
3011 }
3012 return;
3013 }
3014 }
3015
3016#ifdef FEAT_CLIPBOARD
3017 /* If a clipboard selection is in progress, handle it */
3018 if (clip_star.state == SELECT_IN_PROGRESS)
3019 {
3020 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3021 return;
3022 }
3023
3024 /* Determine which mouse settings to look for based on the current mode */
3025 switch (get_real_state())
3026 {
3027 case NORMAL_BUSY:
3028 case OP_PENDING:
3029 case NORMAL: checkfor = MOUSE_NORMAL; break;
3030 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003031 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 case REPLACE:
3033 case REPLACE+LANGMAP:
3034#ifdef FEAT_VREPLACE
3035 case VREPLACE:
3036 case VREPLACE+LANGMAP:
3037#endif
3038 case INSERT:
3039 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3040 case ASKMORE:
3041 case HITRETURN: /* At the more- and hit-enter prompt pass the
3042 mouse event for a click on or below the
3043 message line. */
3044 if (Y_2_ROW(y) >= msg_row)
3045 checkfor = MOUSE_NORMAL;
3046 else
3047 checkfor = MOUSE_RETURN;
3048 break;
3049
3050 /*
3051 * On the command line, use the clipboard selection on all lines
3052 * but the command line. But not when pasting.
3053 */
3054 case CMDLINE:
3055 case CMDLINE+LANGMAP:
3056 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3057 checkfor = MOUSE_NONE;
3058 else
3059 checkfor = MOUSE_COMMAND;
3060 break;
3061
3062 default:
3063 checkfor = MOUSE_NONE;
3064 break;
3065 };
3066
3067 /*
3068 * Allow clipboard selection of text on the command line in "normal"
3069 * modes. Don't do this when dragging the status line, or extending a
3070 * Visual selection.
3071 */
3072 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
3073 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00003074# ifdef FEAT_WINDOWS
3075 + firstwin->w_winrow
3076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 && button != MOUSE_DRAG
3078# ifdef FEAT_MOUSESHAPE
3079 && !drag_status_line
3080# ifdef FEAT_VERTSPLIT
3081 && !drag_sep_line
3082# endif
3083# endif
3084 )
3085 checkfor = MOUSE_NONE;
3086
3087 /*
3088 * Use modeless selection when holding CTRL and SHIFT pressed.
3089 */
3090 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3091 checkfor = MOUSE_NONEF;
3092
3093 /*
3094 * In Ex mode, always use modeless selection.
3095 */
3096 if (exmode_active)
3097 checkfor = MOUSE_NONE;
3098
3099 /*
3100 * If the mouse settings say to not use the mouse, use the modeless
3101 * selection. But if Visual is active, assume that only the Visual area
3102 * will be selected.
3103 * Exception: On the command line, both the selection is used and a mouse
3104 * key is send.
3105 */
3106 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3107 {
3108#ifdef FEAT_VISUAL
3109 /* Don't do modeless selection in Visual mode. */
3110 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3111 return;
3112#endif
3113
3114 /*
3115 * When 'mousemodel' is "popup", shift-left is translated to right.
3116 * But not when also using Ctrl.
3117 */
3118 if (mouse_model_popup() && button == MOUSE_LEFT
3119 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3120 {
3121 button = MOUSE_RIGHT;
3122 modifiers &= ~ MOUSE_SHIFT;
3123 }
3124
3125 /* If the selection is done, allow the right button to extend it.
3126 * If the selection is cleared, allow the right button to start it
3127 * from the cursor position. */
3128 if (button == MOUSE_RIGHT)
3129 {
3130 if (clip_star.state == SELECT_CLEARED)
3131 {
3132 if (State & CMDLINE)
3133 {
3134 col = msg_col;
3135 row = msg_row;
3136 }
3137 else
3138 {
3139 col = curwin->w_wcol;
3140 row = curwin->w_wrow + W_WINROW(curwin);
3141 }
3142 clip_start_selection(col, row, FALSE);
3143 }
3144 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3145 repeated_click);
3146 did_clip = TRUE;
3147 }
3148 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003149 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
3151 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3152 did_clip = TRUE;
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /* Always allow pasting */
3156 if (button != MOUSE_MIDDLE)
3157 {
3158 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3159 return;
3160 if (checkfor != MOUSE_COMMAND)
3161 button = MOUSE_LEFT;
3162 }
3163 repeated_click = FALSE;
3164 }
3165
3166 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003167 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
3169
3170 /* Don't put events in the input queue now. */
3171 if (hold_gui_events)
3172 return;
3173
3174 row = gui_xy2colrow(x, y, &col);
3175
3176 /*
3177 * If we are dragging and the mouse hasn't moved far enough to be on a
3178 * different character, then don't send an event to vim.
3179 */
3180 if (button == MOUSE_DRAG)
3181 {
3182 if (row == prev_row && col == prev_col)
3183 return;
3184 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3185 if (y < 0)
3186 row = -1;
3187 }
3188
3189 /*
3190 * If topline has changed (window scrolled) since the last click, reset
3191 * repeated_click, because we don't want starting Visual mode when
3192 * clicking on a different character in the text.
3193 */
3194 if (curwin->w_topline != gui_prev_topline
3195#ifdef FEAT_DIFF
3196 || curwin->w_topfill != gui_prev_topfill
3197#endif
3198 )
3199 repeated_click = FALSE;
3200
3201 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3202 string[1] = KS_MOUSE;
3203 string[2] = KE_FILLER;
3204 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3205 {
3206 if (repeated_click)
3207 {
3208 /*
3209 * Handle multiple clicks. They only count if the mouse is still
3210 * pointing at the same character.
3211 */
3212 if (button != prev_button || row != prev_row || col != prev_col)
3213 num_clicks = 1;
3214 else if (++num_clicks > 4)
3215 num_clicks = 1;
3216 }
3217 else
3218 num_clicks = 1;
3219 prev_button = button;
3220 gui_prev_topline = curwin->w_topline;
3221#ifdef FEAT_DIFF
3222 gui_prev_topfill = curwin->w_topfill;
3223#endif
3224
3225 string[3] = (char_u)(button | 0x20);
3226 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3227 }
3228 else
3229 string[3] = (char_u)button;
3230
3231 string[3] |= modifiers;
3232 fill_mouse_coord(string + 4, col, row);
3233 add_to_input_buf(string, 8);
3234
3235 if (row < 0)
3236 prev_row = 0;
3237 else
3238 prev_row = row;
3239 prev_col = col;
3240
3241 /*
3242 * We need to make sure this is cleared since Athena doesn't tell us when
3243 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3244 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003245#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 gui.dragged_sb = SBAR_NONE;
3247#endif
3248}
3249
3250/*
3251 * Convert x and y coordinate to column and row in text window.
3252 * Corrects for multi-byte character.
3253 * returns column in "*colp" and row as return value;
3254 */
3255 int
3256gui_xy2colrow(x, y, colp)
3257 int x;
3258 int y;
3259 int *colp;
3260{
3261 int col = check_col(X_2_COL(x));
3262 int row = check_row(Y_2_ROW(y));
3263
3264#ifdef FEAT_MBYTE
3265 *colp = mb_fix_col(col, row);
3266#else
3267 *colp = col;
3268#endif
3269 return row;
3270}
3271
3272#if defined(FEAT_MENU) || defined(PROTO)
3273/*
3274 * Callback function for when a menu entry has been selected.
3275 */
3276 void
3277gui_menu_cb(menu)
3278 vimmenu_T *menu;
3279{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003280 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 /* Don't put events in the input queue now. */
3283 if (hold_gui_events)
3284 return;
3285
3286 bytes[0] = CSI;
3287 bytes[1] = KS_MENU;
3288 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003289 add_to_input_buf(bytes, 3);
3290 add_long_to_buf((long_u)menu, bytes);
3291 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292}
3293#endif
3294
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003295static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297/*
3298 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003299 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * in p_go.
3301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 void
3303gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003304 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#ifdef FEAT_MENU
3307 static int prev_menu_is_active = -1;
3308#endif
3309#ifdef FEAT_TOOLBAR
3310 static int prev_toolbar = -1;
3311 int using_toolbar = FALSE;
3312#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003313#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003314 int using_tabline;
3315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#ifdef FEAT_FOOTER
3317 static int prev_footer = -1;
3318 int using_footer = FALSE;
3319#endif
3320#if defined(FEAT_MENU) && !defined(WIN16)
3321 static int prev_tearoff = -1;
3322 int using_tearoff = FALSE;
3323#endif
3324
3325 char_u *p;
3326 int i;
3327#ifdef FEAT_MENU
3328 int grey_old, grey_new;
3329 char_u *temp;
3330#endif
3331 win_T *wp;
3332 int need_set_size;
3333 int fix_size;
3334
3335#ifdef FEAT_MENU
3336 if (oldval != NULL && gui.in_use)
3337 {
3338 /*
3339 * Check if the menu's go from grey to non-grey or vise versa.
3340 */
3341 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3342 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3343 if (grey_old != grey_new)
3344 {
3345 temp = p_go;
3346 p_go = oldval;
3347 gui_update_menus(MENU_ALL_MODES);
3348 p_go = temp;
3349 }
3350 }
3351 gui.menu_is_active = FALSE;
3352#endif
3353
3354 for (i = 0; i < 3; i++)
3355 gui.which_scrollbars[i] = FALSE;
3356 for (p = p_go; *p; p++)
3357 switch (*p)
3358 {
3359 case GO_LEFT:
3360 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3361 break;
3362 case GO_RIGHT:
3363 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3364 break;
3365#ifdef FEAT_VERTSPLIT
3366 case GO_VLEFT:
3367 if (win_hasvertsplit())
3368 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3369 break;
3370 case GO_VRIGHT:
3371 if (win_hasvertsplit())
3372 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3373 break;
3374#endif
3375 case GO_BOT:
3376 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3377 break;
3378#ifdef FEAT_MENU
3379 case GO_MENUS:
3380 gui.menu_is_active = TRUE;
3381 break;
3382#endif
3383 case GO_GREY:
3384 /* make menu's have grey items, ignored here */
3385 break;
3386#ifdef FEAT_TOOLBAR
3387 case GO_TOOLBAR:
3388 using_toolbar = TRUE;
3389 break;
3390#endif
3391#ifdef FEAT_FOOTER
3392 case GO_FOOTER:
3393 using_footer = TRUE;
3394 break;
3395#endif
3396 case GO_TEAROFF:
3397#if defined(FEAT_MENU) && !defined(WIN16)
3398 using_tearoff = TRUE;
3399#endif
3400 break;
3401 default:
3402 /* Ignore options that are not supported */
3403 break;
3404 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (gui.in_use)
3407 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003408 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003410
3411#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003412 /* Update the GUI tab line, it may appear or disappear. This may
3413 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003414 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003415 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003416 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003417 /* We don't want a resize event change "Rows" here, save and
3418 * restore it. Resizing is handled below. */
3419 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003420 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003421 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003422 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003423 if (using_tabline)
3424 fix_size = TRUE;
3425 if (!gui_use_tabline())
3426 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3427 }
3428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 for (i = 0; i < 3; i++)
3431 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003432 /* The scrollbar needs to be updated when it is shown/unshown and
3433 * when switching tab pages. But the size only changes when it's
3434 * shown/unshown. Thus we need two places to remember whether a
3435 * scrollbar is there or not. */
3436 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003437#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003438 || gui.which_scrollbars[i]
3439 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003440#endif
3441 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
3443 if (i == SBAR_BOTTOM)
3444 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3445 gui.which_scrollbars[i]);
3446 else
3447 {
3448 FOR_ALL_WINDOWS(wp)
3449 {
3450 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3451 }
3452 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003453 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3454 {
3455 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003456 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003457 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003458 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003459 if (gui.which_scrollbars[i])
3460 fix_size = TRUE;
3461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003463#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003464 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003465#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003466 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468
3469#ifdef FEAT_MENU
3470 if (gui.menu_is_active != prev_menu_is_active)
3471 {
3472 /* We don't want a resize event change "Rows" here, save and
3473 * restore it. Resizing is handled below. */
3474 i = Rows;
3475 gui_mch_enable_menu(gui.menu_is_active);
3476 Rows = i;
3477 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003478 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (gui.menu_is_active)
3480 fix_size = TRUE;
3481 }
3482#endif
3483
3484#ifdef FEAT_TOOLBAR
3485 if (using_toolbar != prev_toolbar)
3486 {
3487 gui_mch_show_toolbar(using_toolbar);
3488 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003489 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (using_toolbar)
3491 fix_size = TRUE;
3492 }
3493#endif
3494#ifdef FEAT_FOOTER
3495 if (using_footer != prev_footer)
3496 {
3497 gui_mch_enable_footer(using_footer);
3498 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003499 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (using_footer)
3501 fix_size = TRUE;
3502 }
3503#endif
3504#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3505 if (using_tearoff != prev_tearoff)
3506 {
3507 gui_mch_toggle_tearoffs(using_tearoff);
3508 prev_tearoff = using_tearoff;
3509 }
3510#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003511 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003512 {
3513#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003514 long prev_Columns = Columns;
3515 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 /* Adjust the size of the window to make the text area keep the
3518 * same size and to avoid that part of our window is off-screen
3519 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003520 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003521
3522#ifdef FEAT_GUI_GTK
3523 /* GTK has the annoying habit of sending us resize events when
3524 * changing the window size ourselves. This mostly happens when
3525 * waiting for a character to arrive, quite unpredictably, and may
3526 * change Columns and Rows when we don't want it. Wait for a
3527 * character here to avoid this effect.
3528 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003529 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003530 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003531 * Don't change Rows when adding menu/toolbar/tabline.
3532 * Don't change Columns when adding vertical toolbar. */
3533 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003534 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003535 if ((need_set_size & RESIZE_VERT) == 0)
3536 Rows = prev_Rows;
3537 if ((need_set_size & RESIZE_HOR) == 0)
3538 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003539#endif
3540 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003541#ifdef FEAT_WINDOWS
3542 /* When the console tabline appears or disappears the window positions
3543 * change. */
3544 if (firstwin->w_winrow != tabline_height())
3545 shell_new_rows(); /* recompute window positions and heights */
3546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548}
3549
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003550#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3551/*
3552 * Return TRUE if the GUI is taking care of the tabline.
3553 * It may still be hidden if 'showtabline' is zero.
3554 */
3555 int
3556gui_use_tabline()
3557{
3558 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3559}
3560
3561/*
3562 * Return TRUE if the GUI is showing the tabline.
3563 * This uses 'showtabline'.
3564 */
3565 static int
3566gui_has_tabline()
3567{
3568 if (!gui_use_tabline()
3569 || p_stal == 0
3570 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3571 return FALSE;
3572 return TRUE;
3573}
3574
3575/*
3576 * Update the tabline.
3577 * This may display/undisplay the tabline and update the labels.
3578 */
3579 void
3580gui_update_tabline()
3581{
3582 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003583 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584
3585 if (!gui.starting && starting == 0)
3586 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003587 /* Updating the tabline uses direct GUI commands, flush
3588 * outstanding instructions first. (esp. clear screen) */
3589 out_flush();
3590 gui_mch_flush();
3591
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003592 if (!showit != !shown)
3593 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594 if (showit != 0)
3595 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003596
3597 /* When the tabs change from hidden to shown or from shown to
3598 * hidden the size of the text area should remain the same. */
3599 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003600 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003601 }
3602}
3603
3604/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003605 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003606 */
3607 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003608get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003609 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003610 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003611{
3612 int modified = FALSE;
3613 char_u buf[40];
3614 int wincount;
3615 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003616 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003617
Bram Moolenaar57657d82006-04-21 22:12:41 +00003618 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003619 opt = (tooltip ? &p_gtt : &p_gtl);
3620 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003621 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003622 int use_sandbox = FALSE;
3623 int save_called_emsg = called_emsg;
3624 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003625 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003626 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3627 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003628
3629 called_emsg = FALSE;
3630
3631 printer_page_num = tabpage_index(tp);
3632# ifdef FEAT_EVAL
3633 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003634 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003635# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003636 /* It's almost as going to the tabpage, but without autocommands. */
3637 curtab->tp_firstwin = firstwin;
3638 curtab->tp_lastwin = lastwin;
3639 curtab->tp_curwin = curwin;
3640 save_curtab = curtab;
3641 curtab = tp;
3642 topframe = curtab->tp_topframe;
3643 firstwin = curtab->tp_firstwin;
3644 lastwin = curtab->tp_lastwin;
3645 curwin = curtab->tp_curwin;
3646 curbuf = curwin->w_buffer;
3647
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003648 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003649 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003650 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003651 STRCPY(NameBuff, res);
3652
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003653 /* Back to the original curtab. */
3654 curtab = save_curtab;
3655 topframe = curtab->tp_topframe;
3656 firstwin = curtab->tp_firstwin;
3657 lastwin = curtab->tp_lastwin;
3658 curwin = curtab->tp_curwin;
3659 curbuf = curwin->w_buffer;
3660
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003661 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003662 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003663 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003664 called_emsg |= save_called_emsg;
3665 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003666
3667 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3668 * use a default label. */
3669 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003670 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003671 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003672 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003673 if (!tooltip)
3674 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003675
3676 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3677 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3678 if (bufIsChanged(wp->w_buffer))
3679 modified = TRUE;
3680 if (modified || wincount > 1)
3681 {
3682 if (wincount > 1)
3683 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3684 else
3685 buf[0] = NUL;
3686 if (modified)
3687 STRCAT(buf, "+");
3688 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003689 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003690 mch_memmove(NameBuff, buf, STRLEN(buf));
3691 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003692 }
3693}
3694
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003695/*
3696 * Send the event for clicking to select tab page "nr".
3697 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003698 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003699 */
3700 int
3701send_tabline_event(nr)
3702 int nr;
3703{
3704 char_u string[3];
3705
3706 if (nr == tabpage_index(curtab))
3707 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003708
3709 /* Don't put events in the input queue now. */
3710 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003711# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003712 || cmdwin_type != 0
3713# endif
3714 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003715 {
3716 /* Set it back to the current tab page. */
3717 gui_mch_set_curtab(tabpage_index(curtab));
3718 return FALSE;
3719 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003720
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003721 string[0] = CSI;
3722 string[1] = KS_TABLINE;
3723 string[2] = KE_FILLER;
3724 add_to_input_buf(string, 3);
3725 string[0] = nr;
3726 add_to_input_buf_csi(string, 1);
3727 return TRUE;
3728}
3729
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003730/*
3731 * Send a tabline menu event
3732 */
3733 void
3734send_tabline_menu_event(tabidx, event)
3735 int tabidx;
3736 int event;
3737{
3738 char_u string[3];
3739
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003740 /* Don't put events in the input queue now. */
3741 if (hold_gui_events)
3742 return;
3743
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003744 string[0] = CSI;
3745 string[1] = KS_TABMENU;
3746 string[2] = KE_FILLER;
3747 add_to_input_buf(string, 3);
3748 string[0] = tabidx;
3749 string[1] = (char_u)(long)event;
3750 add_to_input_buf_csi(string, 2);
3751}
3752
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755/*
3756 * Scrollbar stuff:
3757 */
3758
Bram Moolenaare45828b2006-02-15 22:12:56 +00003759#if defined(FEAT_WINDOWS) || defined(PROTO)
3760/*
3761 * Remove all scrollbars. Used before switching to another tab page.
3762 */
3763 void
3764gui_remove_scrollbars()
3765{
3766 int i;
3767 win_T *wp;
3768
3769 for (i = 0; i < 3; i++)
3770 {
3771 if (i == SBAR_BOTTOM)
3772 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3773 else
3774 {
3775 FOR_ALL_WINDOWS(wp)
3776 {
3777 gui_do_scrollbar(wp, i, FALSE);
3778 }
3779 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003780 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003781 }
3782}
3783#endif
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 void
3786gui_create_scrollbar(sb, type, wp)
3787 scrollbar_T *sb;
3788 int type;
3789 win_T *wp;
3790{
3791 static int sbar_ident = 0;
3792
3793 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3794 sb->wp = wp;
3795 sb->type = type;
3796 sb->value = 0;
3797#ifdef FEAT_GUI_ATHENA
3798 sb->pixval = 0;
3799#endif
3800 sb->size = 1;
3801 sb->max = 1;
3802 sb->top = 0;
3803 sb->height = 0;
3804#ifdef FEAT_VERTSPLIT
3805 sb->width = 0;
3806#endif
3807 sb->status_height = 0;
3808 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3809}
3810
3811/*
3812 * Find the scrollbar with the given index.
3813 */
3814 scrollbar_T *
3815gui_find_scrollbar(ident)
3816 long ident;
3817{
3818 win_T *wp;
3819
3820 if (gui.bottom_sbar.ident == ident)
3821 return &gui.bottom_sbar;
3822 FOR_ALL_WINDOWS(wp)
3823 {
3824 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3825 return &wp->w_scrollbars[SBAR_LEFT];
3826 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3827 return &wp->w_scrollbars[SBAR_RIGHT];
3828 }
3829 return NULL;
3830}
3831
3832/*
3833 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3834 *
3835 * For Win32, Macintosh and GTK+ 2:
3836 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3837 * you stop dragging the scrollbar. We get here each time the scrollbar is
3838 * dragged another pixel, but as far as the rest of vim goes, it thinks
3839 * we're just hanging in the call to DispatchMessage() in
3840 * process_message(). The DispatchMessage() call that hangs was passed a
3841 * mouse button click event in the scrollbar window. -- webb.
3842 *
3843 * Solution: Do the scrolling right here. But only when allowed.
3844 * Ignore the scrollbars while executing an external command or when there
3845 * are still characters to be processed.
3846 */
3847 void
3848gui_drag_scrollbar(sb, value, still_dragging)
3849 scrollbar_T *sb;
3850 long value;
3851 int still_dragging;
3852{
3853#ifdef FEAT_WINDOWS
3854 win_T *wp;
3855#endif
3856 int sb_num;
3857#ifdef USE_ON_FLY_SCROLL
3858 colnr_T old_leftcol = curwin->w_leftcol;
3859# ifdef FEAT_SCROLLBIND
3860 linenr_T old_topline = curwin->w_topline;
3861# endif
3862# ifdef FEAT_DIFF
3863 int old_topfill = curwin->w_topfill;
3864# endif
3865#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003866 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 int byte_count;
3868#endif
3869
3870 if (sb == NULL)
3871 return;
3872
3873 /* Don't put events in the input queue now. */
3874 if (hold_gui_events)
3875 return;
3876
3877#ifdef FEAT_CMDWIN
3878 if (cmdwin_type != 0 && sb->wp != curwin)
3879 return;
3880#endif
3881
3882 if (still_dragging)
3883 {
3884 if (sb->wp == NULL)
3885 gui.dragged_sb = SBAR_BOTTOM;
3886 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3887 gui.dragged_sb = SBAR_LEFT;
3888 else
3889 gui.dragged_sb = SBAR_RIGHT;
3890 gui.dragged_wp = sb->wp;
3891 }
3892 else
3893 {
3894 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003895#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003897 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 gui.dragged_wp = NULL;
3899#endif
3900 }
3901
3902 /* Vertical sbar info is kept in the first sbar (the left one) */
3903 if (sb->wp != NULL)
3904 sb = &sb->wp->w_scrollbars[0];
3905
3906 /*
3907 * Check validity of value
3908 */
3909 if (value < 0)
3910 value = 0;
3911#ifdef SCROLL_PAST_END
3912 else if (value > sb->max)
3913 value = sb->max;
3914#else
3915 if (value > sb->max - sb->size + 1)
3916 value = sb->max - sb->size + 1;
3917#endif
3918
3919 sb->value = value;
3920
3921#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003922 /* When not allowed to do the scrolling right now, return.
3923 * This also checked input_available(), but that causes the first click in
3924 * a scrollbar to be ignored when Vim doesn't have focus. */
3925 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return;
3927#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003928#ifdef FEAT_INS_EXPAND
3929 /* Disallow scrolling the current window when the completion popup menu is
3930 * visible. */
3931 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3932 return;
3933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935#ifdef FEAT_RIGHTLEFT
3936 if (sb->wp == NULL && curwin->w_p_rl)
3937 {
3938 value = sb->max + 1 - sb->size - value;
3939 if (value < 0)
3940 value = 0;
3941 }
3942#endif
3943
3944 if (sb->wp != NULL) /* vertical scrollbar */
3945 {
3946 sb_num = 0;
3947#ifdef FEAT_WINDOWS
3948 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3949 sb_num++;
3950 if (wp == NULL)
3951 return;
3952#else
3953 if (sb->wp != curwin)
3954 return;
3955#endif
3956
3957#ifdef USE_ON_FLY_SCROLL
3958 current_scrollbar = sb_num;
3959 scrollbar_value = value;
3960 if (State & NORMAL)
3961 {
3962 gui_do_scroll();
3963 setcursor();
3964 }
3965 else if (State & INSERT)
3966 {
3967 ins_scroll();
3968 setcursor();
3969 }
3970 else if (State & CMDLINE)
3971 {
3972 if (msg_scrolled == 0)
3973 {
3974 gui_do_scroll();
3975 redrawcmdline();
3976 }
3977 }
3978# ifdef FEAT_FOLDING
3979 /* Value may have been changed for closed fold. */
3980 sb->value = sb->wp->w_topline - 1;
3981# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003982
3983 /* When dragging one scrollbar and there is another one at the other
3984 * side move the thumb of that one too. */
3985 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3986 gui_mch_set_scrollbar_thumb(
3987 &sb->wp->w_scrollbars[
3988 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3989 ? SBAR_LEFT : SBAR_RIGHT],
3990 sb->value, sb->size, sb->max);
3991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#else
3993 bytes[0] = CSI;
3994 bytes[1] = KS_VER_SCROLLBAR;
3995 bytes[2] = KE_FILLER;
3996 bytes[3] = (char_u)sb_num;
3997 byte_count = 4;
3998#endif
3999 }
4000 else
4001 {
4002#ifdef USE_ON_FLY_SCROLL
4003 scrollbar_value = value;
4004
4005 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004006 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 else if (State & INSERT)
4008 ins_horscroll();
4009 else if (State & CMDLINE)
4010 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004011 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004013 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 redrawcmdline();
4015 }
4016 }
4017 if (old_leftcol != curwin->w_leftcol)
4018 {
4019 updateWindow(curwin); /* update window, status and cmdline */
4020 setcursor();
4021 }
4022#else
4023 bytes[0] = CSI;
4024 bytes[1] = KS_HOR_SCROLLBAR;
4025 bytes[2] = KE_FILLER;
4026 byte_count = 3;
4027#endif
4028 }
4029
4030#ifdef USE_ON_FLY_SCROLL
4031# ifdef FEAT_SCROLLBIND
4032 /*
4033 * synchronize other windows, as necessary according to 'scrollbind'
4034 */
4035 if (curwin->w_p_scb
4036 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4037 || (sb->wp == curwin && (curwin->w_topline != old_topline
4038# ifdef FEAT_DIFF
4039 || curwin->w_topfill != old_topfill
4040# endif
4041 ))))
4042 {
4043 do_check_scrollbind(TRUE);
4044 /* need to update the window right here */
4045 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4046 if (wp->w_redr_type > 0)
4047 updateWindow(wp);
4048 setcursor();
4049 }
4050# endif
4051 out_flush();
4052 gui_update_cursor(FALSE, TRUE);
4053#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004054 add_to_input_buf(bytes, byte_count);
4055 add_long_to_buf((long_u)value, bytes);
4056 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#endif
4058}
4059
4060/*
4061 * Scrollbar stuff:
4062 */
4063
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004064#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004065/*
4066 * Called when something in the window layout has changed.
4067 */
4068 void
4069gui_may_update_scrollbars()
4070{
4071 if (gui.in_use && starting == 0)
4072 {
4073 out_flush();
4074 gui_init_which_components(NULL);
4075 gui_update_scrollbars(TRUE);
4076 }
4077 need_mouse_correct = TRUE;
4078}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004079#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 void
4082gui_update_scrollbars(force)
4083 int force; /* Force all scrollbars to get updated */
4084{
4085 win_T *wp;
4086 scrollbar_T *sb;
4087 long val, size, max; /* need 32 bits here */
4088 int which_sb;
4089 int h, y;
4090#ifdef FEAT_VERTSPLIT
4091 static win_T *prev_curwin = NULL;
4092#endif
4093
4094 /* Update the horizontal scrollbar */
4095 gui_update_horiz_scrollbar(force);
4096
4097#ifndef WIN3264
4098 /* Return straight away if there is neither a left nor right scrollbar.
4099 * On MS-Windows this is required anyway for scrollwheel messages. */
4100 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4101 return;
4102#endif
4103
4104 /*
4105 * Don't want to update a scrollbar while we're dragging it. But if we
4106 * have both a left and right scrollbar, and we drag one of them, we still
4107 * need to update the other one.
4108 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004109 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4110 && gui.which_scrollbars[SBAR_LEFT]
4111 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
4113 /*
4114 * If we have two scrollbars and one of them is being dragged, just
4115 * copy the scrollbar position from the dragged one to the other one.
4116 */
4117 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4118 if (gui.dragged_wp != NULL)
4119 gui_mch_set_scrollbar_thumb(
4120 &gui.dragged_wp->w_scrollbars[which_sb],
4121 gui.dragged_wp->w_scrollbars[0].value,
4122 gui.dragged_wp->w_scrollbars[0].size,
4123 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125
4126 /* avoid that moving components around generates events */
4127 ++hold_gui_events;
4128
4129 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4130 {
4131 if (wp->w_buffer == NULL) /* just in case */
4132 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004133 /* Skip a scrollbar that is being dragged. */
4134 if (!force && (gui.dragged_sb == SBAR_LEFT
4135 || gui.dragged_sb == SBAR_RIGHT)
4136 && gui.dragged_wp == wp)
4137 continue;
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#ifdef SCROLL_PAST_END
4140 max = wp->w_buffer->b_ml.ml_line_count - 1;
4141#else
4142 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4143#endif
4144 if (max < 0) /* empty buffer */
4145 max = 0;
4146 val = wp->w_topline - 1;
4147 size = wp->w_height;
4148#ifdef SCROLL_PAST_END
4149 if (val > max) /* just in case */
4150 val = max;
4151#else
4152 if (size > max + 1) /* just in case */
4153 size = max + 1;
4154 if (val > max - size + 1)
4155 val = max - size + 1;
4156#endif
4157 if (val < 0) /* minimal value is 0 */
4158 val = 0;
4159
4160 /*
4161 * Scrollbar at index 0 (the left one) contains all the information.
4162 * It would be the same info for left and right so we just store it for
4163 * one of them.
4164 */
4165 sb = &wp->w_scrollbars[0];
4166
4167 /*
4168 * Note: no check for valid w_botline. If it's not valid the
4169 * scrollbars will be updated later anyway.
4170 */
4171 if (size < 1 || wp->w_botline - 2 > max)
4172 {
4173 /*
4174 * This can happen during changing files. Just don't update the
4175 * scrollbar for now.
4176 */
4177 sb->height = 0; /* Force update next time */
4178 if (gui.which_scrollbars[SBAR_LEFT])
4179 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4180 if (gui.which_scrollbars[SBAR_RIGHT])
4181 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4182 continue;
4183 }
4184 if (force || sb->height != wp->w_height
4185#ifdef FEAT_WINDOWS
4186 || sb->top != wp->w_winrow
4187 || sb->status_height != wp->w_status_height
4188# ifdef FEAT_VERTSPLIT
4189 || sb->width != wp->w_width
4190 || prev_curwin != curwin
4191# endif
4192#endif
4193 )
4194 {
4195 /* Height, width or position of scrollbar has changed. For
4196 * vertical split: curwin changed. */
4197 sb->height = wp->w_height;
4198#ifdef FEAT_WINDOWS
4199 sb->top = wp->w_winrow;
4200 sb->status_height = wp->w_status_height;
4201# ifdef FEAT_VERTSPLIT
4202 sb->width = wp->w_width;
4203# endif
4204#endif
4205
4206 /* Calculate height and position in pixels */
4207 h = (sb->height + sb->status_height) * gui.char_height;
4208 y = sb->top * gui.char_height + gui.border_offset;
4209#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4210 if (gui.menu_is_active)
4211 y += gui.menu_height;
4212#endif
4213
4214#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4215 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4216# ifdef FEAT_GUI_ATHENA
4217 y += gui.toolbar_height;
4218# else
4219# ifdef FEAT_GUI_MSWIN
4220 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4221# endif
4222# endif
4223#endif
4224
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004225#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4226 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004227 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004228#endif
4229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230#ifdef FEAT_WINDOWS
4231 if (wp->w_winrow == 0)
4232#endif
4233 {
4234 /* Height of top scrollbar includes width of top border */
4235 h += gui.border_offset;
4236 y -= gui.border_offset;
4237 }
4238 if (gui.which_scrollbars[SBAR_LEFT])
4239 {
4240 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4241 gui.left_sbar_x, y,
4242 gui.scrollbar_width, h);
4243 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4244 }
4245 if (gui.which_scrollbars[SBAR_RIGHT])
4246 {
4247 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4248 gui.right_sbar_x, y,
4249 gui.scrollbar_width, h);
4250 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4251 }
4252 }
4253
4254 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4255 * checking if the thumb moved at least a pixel. Only do this for
4256 * Athena, most other GUIs require the update anyway to make the
4257 * arrows work. */
4258#ifdef FEAT_GUI_ATHENA
4259 if (max == 0)
4260 y = 0;
4261 else
4262 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4263 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4264#else
4265 if (force || sb->value != val || sb->size != size || sb->max != max)
4266#endif
4267 {
4268 /* Thumb of scrollbar has moved */
4269 sb->value = val;
4270#ifdef FEAT_GUI_ATHENA
4271 sb->pixval = y;
4272#endif
4273 sb->size = size;
4274 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004275 if (gui.which_scrollbars[SBAR_LEFT]
4276 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4278 val, size, max);
4279 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004280 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4282 val, size, max);
4283 }
4284 }
4285#ifdef FEAT_VERTSPLIT
4286 prev_curwin = curwin;
4287#endif
4288 --hold_gui_events;
4289}
4290
4291/*
4292 * Enable or disable a scrollbar.
4293 * Check for scrollbars for vertically split windows which are not enabled
4294 * sometimes.
4295 */
4296 static void
4297gui_do_scrollbar(wp, which, enable)
4298 win_T *wp;
4299 int which; /* SBAR_LEFT or SBAR_RIGHT */
4300 int enable; /* TRUE to enable scrollbar */
4301{
4302#ifdef FEAT_VERTSPLIT
4303 int midcol = curwin->w_wincol + curwin->w_width / 2;
4304 int has_midcol = (wp->w_wincol <= midcol
4305 && wp->w_wincol + wp->w_width >= midcol);
4306
4307 /* Only enable scrollbars that contain the middle column of the current
4308 * window. */
4309 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4310 {
4311 /* Scrollbars only on one side. Don't enable scrollbars that don't
4312 * contain the middle column of the current window. */
4313 if (!has_midcol)
4314 enable = FALSE;
4315 }
4316 else
4317 {
4318 /* Scrollbars on both sides. Don't enable scrollbars that neither
4319 * contain the middle column of the current window nor are on the far
4320 * side. */
4321 if (midcol > Columns / 2)
4322 {
4323 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4324 enable = FALSE;
4325 }
4326 else
4327 {
4328 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4329 : !has_midcol)
4330 enable = FALSE;
4331 }
4332 }
4333#endif
4334 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4335}
4336
4337/*
4338 * Scroll a window according to the values set in the globals current_scrollbar
4339 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4340 * or FALSE otherwise.
4341 */
4342 int
4343gui_do_scroll()
4344{
4345 win_T *wp, *save_wp;
4346 int i;
4347 long nlines;
4348 pos_T old_cursor;
4349 linenr_T old_topline;
4350#ifdef FEAT_DIFF
4351 int old_topfill;
4352#endif
4353
4354 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4355 if (wp == NULL)
4356 break;
4357 if (wp == NULL)
4358 /* Couldn't find window */
4359 return FALSE;
4360
4361 /*
4362 * Compute number of lines to scroll. If zero, nothing to do.
4363 */
4364 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4365 if (nlines == 0)
4366 return FALSE;
4367
4368 save_wp = curwin;
4369 old_topline = wp->w_topline;
4370#ifdef FEAT_DIFF
4371 old_topfill = wp->w_topfill;
4372#endif
4373 old_cursor = wp->w_cursor;
4374 curwin = wp;
4375 curbuf = wp->w_buffer;
4376 if (nlines < 0)
4377 scrolldown(-nlines, gui.dragged_wp == NULL);
4378 else
4379 scrollup(nlines, gui.dragged_wp == NULL);
4380 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4381 * the mouse-up event already, but we still want it to behave like when
4382 * dragging. But not the next click in an arrow. */
4383 if (gui.dragged_sb == SBAR_NONE)
4384 gui.dragged_wp = NULL;
4385
4386 if (old_topline != wp->w_topline
4387#ifdef FEAT_DIFF
4388 || old_topfill != wp->w_topfill
4389#endif
4390 )
4391 {
4392 if (p_so != 0)
4393 {
4394 cursor_correct(); /* fix window for 'so' */
4395 update_topline(); /* avoid up/down jump */
4396 }
4397 if (old_cursor.lnum != wp->w_cursor.lnum)
4398 coladvance(wp->w_curswant);
4399#ifdef FEAT_SCROLLBIND
4400 wp->w_scbind_pos = wp->w_topline;
4401#endif
4402 }
4403
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004404 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4405 validate_cursor();
4406
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 curwin = save_wp;
4408 curbuf = save_wp->w_buffer;
4409
4410 /*
4411 * Don't call updateWindow() when nothing has changed (it will overwrite
4412 * the status line!).
4413 */
4414 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004415 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416#ifdef FEAT_DIFF
4417 || old_topfill != wp->w_topfill
4418#endif
4419 )
4420 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004421 int type = VALID;
4422
4423#ifdef FEAT_INS_EXPAND
4424 if (pum_visible())
4425 {
4426 type = NOT_VALID;
4427 wp->w_lines_valid = 0;
4428 }
4429#endif
4430 /* Don't set must_redraw here, it may cause the popup menu to
4431 * disappear when losing focus after a scrollbar drag. */
4432 if (wp->w_redr_type < type)
4433 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 updateWindow(wp); /* update window, status line, and cmdline */
4435 }
4436
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004437#ifdef FEAT_INS_EXPAND
4438 /* May need to redraw the popup menu. */
4439 if (pum_visible())
4440 pum_redraw();
4441#endif
4442
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4444}
4445
4446
4447/*
4448 * Horizontal scrollbar stuff:
4449 */
4450
4451/*
4452 * Return length of line "lnum" for horizontal scrolling.
4453 */
4454 static colnr_T
4455scroll_line_len(lnum)
4456 linenr_T lnum;
4457{
4458 char_u *p;
4459 colnr_T col;
4460 int w;
4461
4462 p = ml_get(lnum);
4463 col = 0;
4464 if (*p != NUL)
4465 for (;;)
4466 {
4467 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004468 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (*p == NUL) /* don't count the last character */
4470 break;
4471 col += w;
4472 }
4473 return col;
4474}
4475
4476/* Remember which line is currently the longest, so that we don't have to
4477 * search for it when scrolling horizontally. */
4478static linenr_T longest_lnum = 0;
4479
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004480/*
4481 * Find longest visible line number. If this is not possible (or not desired,
4482 * by setting 'h' in "guioptions") then the current line number is returned.
4483 */
4484 static linenr_T
4485gui_find_longest_lnum()
4486{
4487 linenr_T ret = 0;
4488
4489 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4490 * line numbers, topline and botline can be invalid when displaying is
4491 * postponed. */
4492 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4493 && curwin->w_topline <= curwin->w_cursor.lnum
4494 && curwin->w_botline > curwin->w_cursor.lnum
4495 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4496 {
4497 linenr_T lnum;
4498 colnr_T n;
4499 long max = 0;
4500
4501 /* Use maximum of all visible lines. Remember the lnum of the
4502 * longest line, closest to the cursor line. Used when scrolling
4503 * below. */
4504 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4505 {
4506 n = scroll_line_len(lnum);
4507 if (n > (colnr_T)max)
4508 {
4509 max = n;
4510 ret = lnum;
4511 }
4512 else if (n == (colnr_T)max
4513 && abs((int)(lnum - curwin->w_cursor.lnum))
4514 < abs((int)(ret - curwin->w_cursor.lnum)))
4515 ret = lnum;
4516 }
4517 }
4518 else
4519 /* Use cursor line only. */
4520 ret = curwin->w_cursor.lnum;
4521
4522 return ret;
4523}
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 static void
4526gui_update_horiz_scrollbar(force)
4527 int force;
4528{
4529 long value, size, max; /* need 32 bit ints here */
4530
4531 if (!gui.which_scrollbars[SBAR_BOTTOM])
4532 return;
4533
4534 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4535 return;
4536
4537 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4538 return;
4539
4540 /*
4541 * It is possible for the cursor to be invalid if we're in the middle of
4542 * something (like changing files). If so, don't do anything for now.
4543 */
4544 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4545 {
4546 gui.bottom_sbar.value = -1;
4547 return;
4548 }
4549
4550 size = W_WIDTH(curwin);
4551 if (curwin->w_p_wrap)
4552 {
4553 value = 0;
4554#ifdef SCROLL_PAST_END
4555 max = 0;
4556#else
4557 max = W_WIDTH(curwin) - 1;
4558#endif
4559 }
4560 else
4561 {
4562 value = curwin->w_leftcol;
4563
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004564 longest_lnum = gui_find_longest_lnum();
4565 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#ifdef FEAT_VIRTUALEDIT
4568 if (virtual_active())
4569 {
4570 /* May move the cursor even further to the right. */
4571 if (curwin->w_virtcol >= (colnr_T)max)
4572 max = curwin->w_virtcol;
4573 }
4574#endif
4575
4576#ifndef SCROLL_PAST_END
4577 max += W_WIDTH(curwin) - 1;
4578#endif
4579 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004580 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 size -= curwin_col_off();
4582#ifndef SCROLL_PAST_END
4583 max -= curwin_col_off();
4584#endif
4585 }
4586
4587#ifndef SCROLL_PAST_END
4588 if (value > max - size + 1)
4589 value = max - size + 1; /* limit the value to allowable range */
4590#endif
4591
4592#ifdef FEAT_RIGHTLEFT
4593 if (curwin->w_p_rl)
4594 {
4595 value = max + 1 - size - value;
4596 if (value < 0)
4597 {
4598 size += value;
4599 value = 0;
4600 }
4601 }
4602#endif
4603 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4604 && max == gui.bottom_sbar.max)
4605 return;
4606
4607 gui.bottom_sbar.value = value;
4608 gui.bottom_sbar.size = size;
4609 gui.bottom_sbar.max = max;
4610 gui.prev_wrap = curwin->w_p_wrap;
4611
4612 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4613}
4614
4615/*
4616 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4617 */
4618 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004619gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004620 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004621 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622{
4623 /* no wrapping, no scrolling */
4624 if (curwin->w_p_wrap)
4625 return FALSE;
4626
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004627 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 return FALSE;
4629
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004630 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
4632 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004633 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004635 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004636 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004638 if (compute_longest_lnum)
4639 {
4640 curwin->w_cursor.lnum = gui_find_longest_lnum();
4641 curwin->w_cursor.col = 0;
4642 }
4643 /* Do a sanity check on "longest_lnum", just in case. */
4644 else if (longest_lnum >= curwin->w_topline
4645 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 curwin->w_cursor.lnum = longest_lnum;
4648 curwin->w_cursor.col = 0;
4649 }
4650 }
4651
4652 return leftcol_changed();
4653}
4654
4655/*
4656 * Check that none of the colors are the same as the background color
4657 */
4658 void
4659gui_check_colors()
4660{
4661 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4662 {
4663 gui_set_bg_color((char_u *)"White");
4664 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4665 gui_set_fg_color((char_u *)"Black");
4666 }
4667}
4668
Bram Moolenaar3918c952005-03-15 22:34:55 +00004669 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670gui_set_fg_color(name)
4671 char_u *name;
4672{
4673 gui.norm_pixel = gui_get_color(name);
4674 hl_set_fg_color_name(vim_strsave(name));
4675}
4676
Bram Moolenaar3918c952005-03-15 22:34:55 +00004677 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678gui_set_bg_color(name)
4679 char_u *name;
4680{
4681 gui.back_pixel = gui_get_color(name);
4682 hl_set_bg_color_name(vim_strsave(name));
4683}
4684
4685/*
4686 * Allocate a color by name.
4687 * Returns INVALCOLOR and gives an error message when failed.
4688 */
4689 guicolor_T
4690gui_get_color(name)
4691 char_u *name;
4692{
4693 guicolor_T t;
4694
4695 if (*name == NUL)
4696 return INVALCOLOR;
4697 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004700#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 && gui.in_use
4702#endif
4703 )
4704 EMSG2(_("E254: Cannot allocate color %s"), name);
4705 return t;
4706}
4707
4708/*
4709 * Return the grey value of a color (range 0-255).
4710 */
4711 int
4712gui_get_lightness(pixel)
4713 guicolor_T pixel;
4714{
4715 long_u rgb = gui_mch_get_rgb(pixel);
4716
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004717 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004718 + (((rgb >> 8) & 0xff) * 587)
4719 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
4722#if defined(FEAT_GUI_X11) || defined(PROTO)
4723 void
4724gui_new_scrollbar_colors()
4725{
4726 win_T *wp;
4727
4728 /* Nothing to do if GUI hasn't started yet. */
4729 if (!gui.in_use)
4730 return;
4731
4732 FOR_ALL_WINDOWS(wp)
4733 {
4734 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4735 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4736 }
4737 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4738}
4739#endif
4740
4741/*
4742 * Call this when focus has changed.
4743 */
4744 void
4745gui_focus_change(in_focus)
4746 int in_focus;
4747{
4748/*
4749 * Skip this code to avoid drawing the cursor when debugging and switching
4750 * between the debugger window and gvim.
4751 */
4752#if 1
4753 gui.in_focus = in_focus;
4754 out_flush(); /* make sure output has been written */
4755 gui_update_cursor(TRUE, FALSE);
4756
4757# ifdef FEAT_XIM
4758 xim_set_focus(in_focus);
4759# endif
4760
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004761 /* Put events in the input queue only when allowed.
4762 * ui_focus_change() isn't called directly, because it invokes
4763 * autocommands and that must not happen asynchronously. */
4764 if (!hold_gui_events)
4765 {
4766 char_u bytes[3];
4767
4768 bytes[0] = CSI;
4769 bytes[1] = KS_EXTRA;
4770 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4771 add_to_input_buf(bytes, 3);
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#endif
4774}
4775
4776/*
4777 * Called when the mouse moved (but not when dragging).
4778 */
4779 void
4780gui_mouse_moved(x, y)
4781 int x;
4782 int y;
4783{
4784 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004785 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaard3667a22006-03-16 21:35:52 +00004787 /* Ignore this while still starting up. */
4788 if (!gui.in_use || gui.starting)
4789 return;
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791#ifdef FEAT_MOUSESHAPE
4792 /* Get window pointer, and update mouse shape as well. */
4793 wp = xy2win(x, y);
4794#endif
4795
4796 /* Only handle this when 'mousefocus' set and ... */
4797 if (p_mousef
4798 && !hold_gui_events /* not holding events */
4799 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4800 && State != HITRETURN /* but not hit-return prompt */
4801 && msg_scrolled == 0 /* no scrolled message */
4802 && !need_mouse_correct /* not moving the pointer */
4803 && gui.in_focus) /* gvim in focus */
4804 {
4805 /* Don't move the mouse when it's left or right of the Vim window */
4806 if (x < 0 || x > Columns * gui.char_width)
4807 return;
4808#ifndef FEAT_MOUSESHAPE
4809 wp = xy2win(x, y);
4810#endif
4811 if (wp == curwin || wp == NULL)
4812 return; /* still in the same old window, or none at all */
4813
Bram Moolenaar9c102382006-05-03 21:26:49 +00004814#ifdef FEAT_WINDOWS
4815 /* Ignore position in the tab pages line. */
4816 if (Y_2_ROW(y) < tabline_height())
4817 return;
4818#endif
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 /*
4821 * format a mouse click on status line input
4822 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004823 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4824 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826 if (finish_op)
4827 {
4828 /* abort the current operator first */
4829 st[0] = ESC;
4830 add_to_input_buf(st, 1);
4831 }
4832 st[0] = CSI;
4833 st[1] = KS_MOUSE;
4834 st[2] = KE_FILLER;
4835 st[3] = (char_u)MOUSE_LEFT;
4836 fill_mouse_coord(st + 4,
4837#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004838 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#else
4840 -1,
4841#endif
4842 wp->w_height + W_WINROW(wp));
4843
4844 add_to_input_buf(st, 8);
4845 st[3] = (char_u)MOUSE_RELEASE;
4846 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#ifdef FEAT_GUI_GTK
4848 /* Need to wake up the main loop */
4849 if (gtk_main_level() > 0)
4850 gtk_main_quit();
4851#endif
4852 }
4853}
4854
4855/*
4856 * Called when mouse should be moved to window with focus.
4857 */
4858 void
4859gui_mouse_correct()
4860{
4861 int x, y;
4862 win_T *wp = NULL;
4863
4864 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004865
4866 if (!(gui.in_use && p_mousef))
4867 return;
4868
4869 gui_mch_getmouse(&x, &y);
4870 /* Don't move the mouse when it's left or right of the Vim window */
4871 if (x < 0 || x > Columns * gui.char_width)
4872 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004873 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004874# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004875 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004876# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004877 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004878 wp = xy2win(x, y);
4879 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004881 validate_cline_row();
4882 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4883 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886}
4887
4888/*
4889 * Find window where the mouse pointer "y" coordinate is in.
4890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 static win_T *
4892xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004893 int x UNUSED;
4894 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896#ifdef FEAT_WINDOWS
4897 int row;
4898 int col;
4899 win_T *wp;
4900
4901 row = Y_2_ROW(y);
4902 col = X_2_COL(x);
4903 if (row < 0 || col < 0) /* before first window */
4904 return NULL;
4905 wp = mouse_find_win(&row, &col);
4906# ifdef FEAT_MOUSESHAPE
4907 if (State == HITRETURN || State == ASKMORE)
4908 {
4909 if (Y_2_ROW(y) >= msg_row)
4910 update_mouseshape(SHAPE_IDX_MOREL);
4911 else
4912 update_mouseshape(SHAPE_IDX_MORE);
4913 }
4914 else if (row > wp->w_height) /* below status line */
4915 update_mouseshape(SHAPE_IDX_CLINE);
4916# ifdef FEAT_VERTSPLIT
4917 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004918 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 update_mouseshape(SHAPE_IDX_VSEP);
4920# endif
4921 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004922 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 update_mouseshape(SHAPE_IDX_STATUS);
4924 else
4925 update_mouseshape(-2);
4926# endif
4927 return wp;
4928#else
4929 return firstwin;
4930#endif
4931}
4932
4933/*
4934 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4935 * File names may be given to redefine the args list.
4936 */
4937 void
4938ex_gui(eap)
4939 exarg_T *eap;
4940{
4941 char_u *arg = eap->arg;
4942
4943 /*
4944 * Check for "-f" argument: foreground, don't fork.
4945 * Also don't fork when started with "gvim -f".
4946 * Do fork when using "gui -b".
4947 */
4948 if (arg[0] == '-'
4949 && (arg[1] == 'f' || arg[1] == 'b')
4950 && (arg[2] == NUL || vim_iswhite(arg[2])))
4951 {
4952 gui.dofork = (arg[1] == 'b');
4953 eap->arg = skipwhite(eap->arg + 2);
4954 }
4955 if (!gui.in_use)
4956 {
4957 /* Clear the command. Needed for when forking+exiting, to avoid part
4958 * of the argument ending up after the shell prompt. */
4959 msg_clr_eos_force();
4960 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004961#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004962 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 if (!ends_excmd(*eap->arg))
4966 ex_next(eap);
4967}
4968
4969#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004970 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971/*
4972 * This is shared between Athena, Motif and GTK.
4973 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004974static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
4976/*
4977 * Callback function for do_in_runtimepath().
4978 */
4979 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004980gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004982 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004984 char_u *gfp_buffer = cookie;
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (STRLEN(fname) >= MAXPATHL)
4987 *gfp_buffer = NUL;
4988 else
4989 STRCPY(gfp_buffer, fname);
4990}
4991
4992/*
4993 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4994 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4995 */
4996 int
4997gui_find_bitmap(name, buffer, ext)
4998 char_u *name;
4999 char_u *buffer;
5000 char *ext;
5001{
5002 if (STRLEN(name) > MAXPATHL - 14)
5003 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005004 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005005 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
5006 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 return FAIL;
5008 return OK;
5009}
5010
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005011# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012/*
5013 * Given the name of the "icon=" argument, try finding the bitmap file for the
5014 * icon. If it is an absolute path name, use it as it is. Otherwise append
5015 * "ext" and search for it in 'runtimepath'.
5016 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5017 * contains "name".
5018 */
5019 void
5020gui_find_iconfile(name, buffer, ext)
5021 char_u *name;
5022 char_u *buffer;
5023 char *ext;
5024{
5025 char_u buf[MAXPATHL + 1];
5026
5027 expand_env(name, buffer, MAXPATHL);
5028 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5029 STRCPY(buffer, buf);
5030}
5031# endif
5032#endif
5033
Bram Moolenaar9372a112005-12-06 19:59:18 +00005034#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 void
5036display_errors()
5037{
5038 char_u *p;
5039
5040 if (isatty(2))
5041 fflush(stderr);
5042 else if (error_ga.ga_data != NULL)
5043 {
5044 /* avoid putting up a message box with blanks only */
5045 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5046 if (!isspace(*p))
5047 {
5048 /* Truncate a very long message, it will go off-screen. */
5049 if (STRLEN(p) > 2000)
5050 STRCPY(p + 2000 - 14, "...(truncated)");
5051 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005052 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054 }
5055 ga_clear(&error_ga);
5056 }
5057}
5058#endif
5059
5060#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5061/*
5062 * Return TRUE if still starting up and there is no place to enter text.
5063 * For GTK and X11 we check if stderr is not a tty, which means we were
5064 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5065 * allow typing on stdin.
5066 */
5067 int
5068no_console_input()
5069{
5070 return ((!gui.in_use || gui.starting)
5071# ifndef NO_CONSOLE
5072 && !isatty(0) && !isatty(2)
5073# endif
5074 );
5075}
5076#endif
5077
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005078#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005079 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005080 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081/*
5082 * Update the current window and the screen.
5083 */
5084 void
5085gui_update_screen()
5086{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005087#ifdef FEAT_CONCEAL
5088 linenr_T conceal_old_cursor_line = 0;
5089 linenr_T conceal_new_cursor_line = 0;
5090 int conceal_update_lines = FALSE;
5091#endif
5092
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 update_topline();
5094 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005095
5096#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005097 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005098 if (!finish_op && (
5099# ifdef FEAT_AUTOCMD
5100 has_cursormoved()
5101# endif
5102# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5103 ||
5104# endif
5105# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005106 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005107# endif
5108 )
5109 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005110 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005111# ifdef FEAT_AUTOCMD
5112 if (has_cursormoved())
5113 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5114# endif
5115# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005116 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005117 {
5118 conceal_old_cursor_line = last_cursormoved.lnum;
5119 conceal_new_cursor_line = curwin->w_cursor.lnum;
5120 conceal_update_lines = TRUE;
5121 }
5122# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005123 last_cursormoved = curwin->w_cursor;
5124 }
5125#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 update_screen(0); /* may need to update the screen */
5128 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005129# if defined(FEAT_CONCEAL)
5130 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005131 && (conceal_old_cursor_line != conceal_new_cursor_line
5132 || conceal_cursor_line(curwin)
5133 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005134 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005135 if (conceal_old_cursor_line != conceal_new_cursor_line)
5136 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005137 update_single_line(curwin, conceal_new_cursor_line);
5138 curwin->w_valid &= ~VALID_CROW;
5139 }
5140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 out_flush(); /* make sure output has been written */
5142 gui_update_cursor(TRUE, FALSE);
5143 gui_mch_flush();
5144}
5145#endif
5146
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005147#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5149
5150/*
5151 * Get the text to use in a find/replace dialog. Uses the last search pattern
5152 * if the argument is empty.
5153 * Returns an allocated string.
5154 */
5155 char_u *
5156get_find_dialog_text(arg, wwordp, mcasep)
5157 char_u *arg;
5158 int *wwordp; /* return: TRUE if \< \> found */
5159 int *mcasep; /* return: TRUE if \C found */
5160{
5161 char_u *text;
5162
5163 if (*arg == NUL)
5164 text = last_search_pat();
5165 else
5166 text = arg;
5167 if (text != NULL)
5168 {
5169 text = vim_strsave(text);
5170 if (text != NULL)
5171 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005172 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 int i;
5174
5175 /* Remove "\V" */
5176 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5177 {
5178 mch_memmove(text, text + 2, (size_t)(len - 1));
5179 len -= 2;
5180 }
5181
5182 /* Recognize "\c" and "\C" and remove. */
5183 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5184 {
5185 *mcasep = (text[1] == 'C');
5186 mch_memmove(text, text + 2, (size_t)(len - 1));
5187 len -= 2;
5188 }
5189
5190 /* Recognize "\<text\>" and remove. */
5191 if (len >= 4
5192 && STRNCMP(text, "\\<", 2) == 0
5193 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5194 {
5195 *wwordp = TRUE;
5196 mch_memmove(text, text + 2, (size_t)(len - 4));
5197 text[len - 4] = NUL;
5198 }
5199
5200 /* Recognize "\/" or "\?" and remove. */
5201 for (i = 0; i + 1 < len; ++i)
5202 if (text[i] == '\\' && (text[i + 1] == '/'
5203 || text[i + 1] == '?'))
5204 {
5205 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5206 --len;
5207 }
5208 }
5209 }
5210 return text;
5211}
5212
5213/*
5214 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5215 */
5216 static void
5217concat_esc(gap, text, what)
5218 garray_T *gap;
5219 char_u *text;
5220 int what;
5221{
5222 while (*text != NUL)
5223 {
5224#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005225 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 if (l > 1)
5228 {
5229 while (--l >= 0)
5230 ga_append(gap, *text++);
5231 continue;
5232 }
5233#endif
5234 if (*text == what)
5235 ga_append(gap, '\\');
5236 ga_append(gap, *text);
5237 ++text;
5238 }
5239}
5240
5241/*
5242 * Handle the press of a button in the find-replace dialog.
5243 * Return TRUE when something was added to the input buffer.
5244 */
5245 int
5246gui_do_findrepl(flags, find_text, repl_text, down)
5247 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5248 char_u *find_text;
5249 char_u *repl_text;
5250 int down; /* Search downwards. */
5251{
5252 garray_T ga;
5253 int i;
5254 int type = (flags & FRD_TYPE_MASK);
5255 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005256 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005257 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005258 static int busy = FALSE;
5259
5260 /* When the screen is being updated we should not change buffers and
5261 * windows structures, it may cause freed memory to be used. Also don't
5262 * do this recursively (pressing "Find" quickly several times. */
5263 if (updating_screen || busy)
5264 return FALSE;
5265
5266 /* refuse replace when text cannot be changed */
5267 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5268 return FALSE;
5269
5270 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005273 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 ga_concat(&ga, (char_u *)"%s/");
5275
5276 ga_concat(&ga, (char_u *)"\\V");
5277 if (flags & FRD_MATCH_CASE)
5278 ga_concat(&ga, (char_u *)"\\C");
5279 else
5280 ga_concat(&ga, (char_u *)"\\c");
5281 if (flags & FRD_WHOLE_WORD)
5282 ga_concat(&ga, (char_u *)"\\<");
5283 if (type == FRD_REPLACEALL || down)
5284 concat_esc(&ga, find_text, '/'); /* escape slashes */
5285 else
5286 concat_esc(&ga, find_text, '?'); /* escape '?' */
5287 if (flags & FRD_WHOLE_WORD)
5288 ga_concat(&ga, (char_u *)"\\>");
5289
5290 if (type == FRD_REPLACEALL)
5291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005293 /* escape / and \ */
5294 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5295 if (p != NULL)
5296 ga_concat(&ga, p);
5297 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005299 }
5300 ga_append(&ga, NUL);
5301
5302 if (type == FRD_REPLACE)
5303 {
5304 /* Do the replacement when the text at the cursor matches. Thus no
5305 * replacement is done if the cursor was moved! */
5306 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5307 regmatch.rm_ic = 0;
5308 if (regmatch.regprog != NULL)
5309 {
5310 p = ml_get_cursor();
5311 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5312 && regmatch.startp[0] == p)
5313 {
5314 /* Clear the command line to remove any old "No match"
5315 * error. */
5316 msg_end_prompt();
5317
5318 if (u_save_cursor() == OK)
5319 {
5320 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005321 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005322
5323 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005324 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005325 ins_str(repl_text);
5326 }
5327 }
5328 else
5329 MSG(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005330 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005331 }
5332 }
5333
5334 if (type == FRD_REPLACEALL)
5335 {
5336 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005337 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 do_cmdline_cmd(ga.ga_data);
5339 }
5340 else
5341 {
5342 /* Search for the next match. */
5343 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005345 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 msg_scroll = i; /* don't let an error message set msg_scroll */
5347 }
5348
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005349 /* Don't want to pass did_emsg to other code, it may cause disabling
5350 * syntax HL if we were busy redrawing. */
5351 did_emsg = save_did_emsg;
5352
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 if (State & (NORMAL | INSERT))
5354 {
5355 gui_update_screen(); /* update the screen */
5356 msg_didout = 0; /* overwrite any message */
5357 need_wait_return = FALSE; /* don't wait for return */
5358 }
5359
5360 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005361 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 return (ga.ga_len > 0);
5363}
5364
5365#endif
5366
5367#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5368 || defined(FEAT_GUI_MSWIN) \
5369 || defined(FEAT_GUI_MAC) \
5370 || defined(PROTO)
5371
5372#ifdef FEAT_WINDOWS
5373static void gui_wingoto_xy __ARGS((int x, int y));
5374
5375/*
5376 * Jump to the window at specified point (x, y).
5377 */
5378 static void
5379gui_wingoto_xy(x, y)
5380 int x;
5381 int y;
5382{
5383 int row = Y_2_ROW(y);
5384 int col = X_2_COL(x);
5385 win_T *wp;
5386
5387 if (row >= 0 && col >= 0)
5388 {
5389 wp = mouse_find_win(&row, &col);
5390 if (wp != NULL && wp != curwin)
5391 win_goto(wp);
5392 }
5393}
5394#endif
5395
5396/*
5397 * Process file drop. Mouse cursor position, key modifiers, name of files
5398 * and count of files are given. Argument "fnames[count]" has full pathnames
5399 * of dropped files, they will be freed in this function, and caller can't use
5400 * fnames after call this function.
5401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 void
5403gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005404 int x UNUSED;
5405 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 int_u modifiers;
5407 char_u **fnames;
5408 int count;
5409{
5410 int i;
5411 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005412 static int entered = FALSE;
5413
5414 /*
5415 * This function is called by event handlers. Just in case we get a
5416 * second event before the first one is handled, ignore the second one.
5417 * Not sure if this can ever happen, just in case.
5418 */
5419 if (entered)
5420 return;
5421 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 /*
5424 * When the cursor is at the command line, add the file names to the
5425 * command line, don't edit the files.
5426 */
5427 if (State & CMDLINE)
5428 {
5429 shorten_filenames(fnames, count);
5430 for (i = 0; i < count; ++i)
5431 {
5432 if (fnames[i] != NULL)
5433 {
5434 if (i > 0)
5435 add_to_input_buf((char_u*)" ", 1);
5436
5437 /* We don't know what command is used thus we can't be sure
5438 * about which characters need to be escaped. Only escape the
5439 * most common ones. */
5440# ifdef BACKSLASH_IN_FILENAME
5441 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5442# else
5443 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5444# endif
5445 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005446 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 vim_free(p);
5448 vim_free(fnames[i]);
5449 }
5450 }
5451 vim_free(fnames);
5452 }
5453 else
5454 {
5455 /* Go to the window under mouse cursor, then shorten given "fnames" by
5456 * current window, because a window can have local current dir. */
5457# ifdef FEAT_WINDOWS
5458 gui_wingoto_xy(x, y);
5459# endif
5460 shorten_filenames(fnames, count);
5461
5462 /* If Shift held down, remember the first item. */
5463 if ((modifiers & MOUSE_SHIFT) != 0)
5464 p = vim_strsave(fnames[0]);
5465 else
5466 p = NULL;
5467
5468 /* Handle the drop, :edit or :split to get to the file. This also
5469 * frees fnames[]. Skip this if there is only one item it's a
5470 * directory and Shift is held down. */
5471 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5472 && mch_isdir(fnames[0]))
5473 {
5474 vim_free(fnames[0]);
5475 vim_free(fnames);
5476 }
5477 else
5478 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5479
5480 /* If Shift held down, change to first file's directory. If the first
5481 * item is a directory, change to that directory (and let the explorer
5482 * plugin show the contents). */
5483 if (p != NULL)
5484 {
5485 if (mch_isdir(p))
5486 {
5487 if (mch_chdir((char *)p) == 0)
5488 shorten_fnames(TRUE);
5489 }
5490 else if (vim_chdirfile(p) == OK)
5491 shorten_fnames(TRUE);
5492 vim_free(p);
5493 }
5494
5495 /* Update the screen display */
5496 update_screen(NOT_VALID);
5497# ifdef FEAT_MENU
5498 gui_update_menus(0);
5499# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005500#ifdef FEAT_TITLE
5501 maketitle();
5502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 setcursor();
5504 out_flush();
5505 gui_update_cursor(FALSE, FALSE);
5506 gui_mch_flush();
5507 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005508
5509 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511#endif