blob: 0219d1cfb9fd179d04befa777b8571e06a7effa2 [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 Moolenaar7f78bd72011-09-14 19:04:39 +020040#if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X) \
41 && !defined(__APPLE__)
42# define MAY_FORK
43static void gui_do_fork __ARGS((void));
44
45static int gui_read_child_pipe __ARGS((int fd));
46
47/* Return values for gui_read_child_pipe */
48enum {
49 GUI_CHILD_IO_ERROR,
50 GUI_CHILD_OK,
51 GUI_CHILD_FAILED
52};
53
54#endif /* MAY_FORK */
55
56static void gui_attempt_start __ARGS((void));
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static int can_update_cursor = TRUE; /* can display the cursor */
59
60/*
61 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
62 * this makes the thumb indicate the part of the text that is shown. Motif
63 * can't do this.
64 */
65#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
66# define SCROLL_PAST_END
67#endif
68
69/*
70 * gui_start -- Called when user wants to start the GUI.
71 *
72 * Careful: This function can be called recursively when there is a ":gui"
73 * command in the .gvimrc file. Only the first call should fork, not the
74 * recursive call.
75 */
76 void
77gui_start()
78{
79 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 static int recursive = 0;
81
82 old_term = vim_strsave(T_NAME);
83
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 settmode(TMODE_COOK); /* stop RAW mode */
85 if (full_screen)
86 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 full_screen = FALSE;
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 ++recursive;
90
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020091#ifdef MAY_FORK
92 /*
93 * Quit the current process and continue in the child.
94 * Makes "gvim file" disconnect from the shell it was started in.
95 * Don't do this when Vim was started with "-f" or the 'f' flag is present
96 * in 'guioptions'.
97 */
98 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
99 {
100 gui_do_fork();
101 }
102 else
103#endif
104 {
105 gui_attempt_start();
106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108 if (!gui.in_use) /* failed to start GUI */
109 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200110 /* Back to old term settings
111 *
112 * FIXME: If we got here because a child process failed and flagged to
113 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
114 * hit an X11 I/O error and do a longjmp(), leaving recursive
115 * permanently set to 1. This is probably not as big a problem as it
116 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
117 * return "OK" unconditionally, so it would be very difficult to
118 * actually hit this case.
119 */
120 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 settmode(TMODE_RAW); /* restart RAW mode */
122#ifdef FEAT_TITLE
123 set_title_defaults(); /* set 'title' and 'icon' again */
124#endif
125 }
126
127 vim_free(old_term);
128
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200129#ifdef FEAT_AUTOCMD
130 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
131 * the GUIFailed event. */
132 gui_mch_update();
133 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
134 NULL, NULL, FALSE, curbuf);
135#endif
136 --recursive;
137}
138
139/*
140 * Set_termname() will call gui_init() to start the GUI.
141 * Set the "starting" flag, to indicate that the GUI will start.
142 *
143 * We don't want to open the GUI shell until after we've read .gvimrc,
144 * otherwise we don't know what font we will use, and hence we don't know
145 * what size the shell should be. So if there are errors in the .gvimrc
146 * file, they will have to go to the terminal: Set full_screen to FALSE.
147 * full_screen will be set to TRUE again by a successful termcapinit().
148 */
149 static void
150gui_attempt_start()
151{
152 static int recursive = 0;
153
154 ++recursive;
155 gui.starting = TRUE;
156
157#ifdef FEAT_GUI_GTK
158 gui.event_time = GDK_CURRENT_TIME;
159#endif
160
161 termcapinit((char_u *)"builtin_gui");
162 gui.starting = recursive - 1;
163
Bram Moolenaar9372a112005-12-06 19:59:18 +0000164#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200166 {
167# ifdef FEAT_EVAL
168 Window x11_window;
169 Display *x11_display;
170
171 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
172 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
173# endif
174
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 /* Display error messages in a dialog now. */
176 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 --recursive;
180}
181
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200182#ifdef MAY_FORK
183
184/* for waitpid() */
185# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
186# include <sys/wait.h>
187# endif
188
189/*
190 * Create a new process, by forking. In the child, start the GUI, and in
191 * the parent, exit.
192 *
193 * If something goes wrong, this will return with gui.in_use still set
194 * to FALSE, in which case the caller should continue execution without
195 * the GUI.
196 *
197 * If the child fails to start the GUI, then the child will exit and the
198 * parent will return. If the child succeeds, then the parent will exit
199 * and the child will return.
200 */
201 static void
202gui_do_fork()
203{
204#ifdef __QNXNTO__
205 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
206 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
207 gui_attempt_start();
208 return;
209#else
210 int pipefd[2]; /* pipe between parent and child */
211 int pipe_error;
212 int status;
213 int exit_status;
214 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200215
216 /* Setup a pipe between the child and the parent, so that the parent
217 * knows when the child has done the setsid() call and is allowed to
218 * exit. */
219 pipe_error = (pipe(pipefd) < 0);
220 pid = fork();
221 if (pid < 0) /* Fork error */
222 {
223 EMSG(_("E851: Failed to create a new process for the GUI"));
224 return;
225 }
226 else if (pid > 0) /* Parent */
227 {
228 /* Give the child some time to do the setsid(), otherwise the
229 * exit() may kill the child too (when starting gvim from inside a
230 * gvim). */
231 if (!pipe_error)
232 {
233 /* The read returns when the child closes the pipe (or when
234 * the child dies for some reason). */
235 close(pipefd[1]);
236 status = gui_read_child_pipe(pipefd[0]);
237 if (status == GUI_CHILD_FAILED)
238 {
239 /* The child failed to start the GUI, so the caller must
240 * continue. There may be more error information written
241 * to stderr by the child. */
242# ifdef __NeXT__
243 wait4(pid, &exit_status, 0, (struct rusage *)0);
244# else
245 waitpid(pid, &exit_status, 0);
246# endif
247 EMSG(_("E852: The child process failed to start the GUI"));
248 return;
249 }
250 else if (status == GUI_CHILD_IO_ERROR)
251 {
252 pipe_error = TRUE;
253 }
254 /* else GUI_CHILD_OK: parent exit */
255 }
256
257 if (pipe_error)
258 ui_delay(300L, TRUE);
259
260 /* When swapping screens we may need to go to the next line, e.g.,
261 * after a hit-enter prompt and using ":gui". */
262 if (newline_on_exit)
263 mch_errmsg("\r\n");
264
265 /*
266 * The parent must skip the normal exit() processing, the child
267 * will do it. For example, GTK messes up signals when exiting.
268 */
269 _exit(0);
270 }
271 /* Child */
272
273# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
274 /*
275 * Change our process group. On some systems/shells a CTRL-C in the
276 * shell where Vim was started would otherwise kill gvim!
277 */
278# if defined(HAVE_SETSID)
279 (void)setsid();
280# else
281 (void)setpgid(0, 0);
282# endif
283# endif
284 if (!pipe_error)
285 close(pipefd[0]);
286
287# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
288 /* Tell the session manager our new PID */
289 gui_mch_forked();
290# endif
291
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200292 /* Try to start the GUI */
293 gui_attempt_start();
294
295 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200296 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200297 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200298 if (gui.in_use)
299 write_eintr(pipefd[1], "ok", 3);
300 else
301 write_eintr(pipefd[1], "fail", 5);
302 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200303 }
304
305 /* If we failed to start the GUI, exit now. */
306 if (!gui.in_use)
307 exit(1);
308#endif
309}
310
311/*
312 * Read from a pipe assumed to be connected to the child process (this
313 * function is called from the parent).
314 * Return GUI_CHILD_OK if the child successfully started the GUI,
315 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
316 * some other error.
317 *
318 * The file descriptor will be closed before the function returns.
319 */
320 static int
321gui_read_child_pipe(int fd)
322{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200323 long bytes_read;
324#define READ_BUFFER_SIZE 10
325 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200326
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
328#undef READ_BUFFER_SIZE
329 close(fd);
330 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200332 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200333 if (strcmp(buffer, "ok") == 0)
334 return GUI_CHILD_OK;
335 return GUI_CHILD_FAILED;
336}
337
338#endif /* MAY_FORK */
339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340/*
341 * Call this when vim starts up, whether or not the GUI is started
342 */
343 void
344gui_prepare(argc, argv)
345 int *argc;
346 char **argv;
347{
348 gui.in_use = FALSE; /* No GUI yet (maybe later) */
349 gui.starting = FALSE; /* No GUI yet (maybe later) */
350 gui_mch_prepare(argc, argv);
351}
352
353/*
354 * Try initializing the GUI and check if it can be started.
355 * Used from main() to check early if "vim -g" can start the GUI.
356 * Used from gui_init() to prepare for starting the GUI.
357 * Returns FAIL or OK.
358 */
359 int
360gui_init_check()
361{
362 static int result = MAYBE;
363
364 if (result != MAYBE)
365 {
366 if (result == FAIL)
367 EMSG(_("E229: Cannot start the GUI"));
368 return result;
369 }
370
371 gui.shell_created = FALSE;
372 gui.dying = FALSE;
373 gui.in_focus = TRUE; /* so the guicursor setting works */
374 gui.dragged_sb = SBAR_NONE;
375 gui.dragged_wp = NULL;
376 gui.pointer_hidden = FALSE;
377 gui.col = 0;
378 gui.row = 0;
379 gui.num_cols = Columns;
380 gui.num_rows = Rows;
381
382 gui.cursor_is_valid = FALSE;
383 gui.scroll_region_top = 0;
384 gui.scroll_region_bot = Rows - 1;
385 gui.scroll_region_left = 0;
386 gui.scroll_region_right = Columns - 1;
387 gui.highlight_mask = HL_NORMAL;
388 gui.char_width = 1;
389 gui.char_height = 1;
390 gui.char_ascent = 0;
391 gui.border_width = 0;
392
393 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200394#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 gui.bold_font = NOFONT;
396 gui.ital_font = NOFONT;
397 gui.boldital_font = NOFONT;
398# ifdef FEAT_XFONTSET
399 gui.fontset = NOFONTSET;
400# endif
401#endif
402
403#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200404# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# ifdef FONTSET_ALWAYS
406 gui.menu_fontset = NOFONTSET;
407# else
408 gui.menu_font = NOFONT;
409# endif
410# endif
411 gui.menu_is_active = TRUE; /* default: include menu */
412# ifndef FEAT_GUI_GTK
413 gui.menu_height = MENU_DEFAULT_HEIGHT;
414 gui.menu_width = 0;
415# endif
416#endif
417#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
418 gui.toolbar_height = 0;
419#endif
420#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
421 gui.footer_height = 0;
422#endif
423#ifdef FEAT_BEVAL_TIP
424 gui.tooltip_fontset = NOFONTSET;
425#endif
426
427 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
428 gui.prev_wrap = -1;
429
430#ifdef ALWAYS_USE_GUI
431 result = OK;
432#else
433 result = gui_mch_init_check();
434#endif
435 return result;
436}
437
438/*
439 * This is the call which starts the GUI.
440 */
441 void
442gui_init()
443{
444 win_T *wp;
445 static int recursive = 0;
446
447 /*
448 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
449 * function will then be executed at the first call, the rest by the
450 * recursive call. This allow the shell to be opened halfway reading a
451 * gvimrc file.
452 */
453 if (!recursive)
454 {
455 ++recursive;
456
457 clip_init(TRUE);
458
459 /* If can't initialize, don't try doing the rest */
460 if (gui_init_check() == FAIL)
461 {
462 --recursive;
463 clip_init(FALSE);
464 return;
465 }
466
467 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000468 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
469 * breaks the Paste toolbar button.
470 */
471 set_option_value((char_u *)"paste", 0L, NULL, 0);
472
473 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Set up system-wide default menus.
475 */
476#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
477 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
478 {
479 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000480 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 sys_menu = FALSE;
482 }
483#endif
484
485 /*
486 * Switch on the mouse by default, unless the user changed it already.
487 * This can then be changed in the .gvimrc.
488 */
489 if (!option_was_set((char_u *)"mouse"))
490 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000491 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 /*
494 * If -U option given, use only the initializations from that file and
495 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
496 */
497 if (use_gvimrc != NULL)
498 {
499 if (STRCMP(use_gvimrc, "NONE") != 0
500 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000501 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
503 }
504 else
505 {
506 /*
507 * Get system wide defaults for gvim, only when file name defined.
508 */
509#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000510 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511#endif
512
513 /*
514 * Try to read GUI initialization commands from the following
515 * places:
516 * - environment variable GVIMINIT
517 * - the user gvimrc file (~/.gvimrc)
518 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
519 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
520 * The first that exists is used, the rest is ignored.
521 */
522 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000523 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
524 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000526 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
527 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#endif
529 )
530 {
531#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000532 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#endif
534 }
535
536 /*
537 * Read initialization commands from ".gvimrc" in current
538 * directory. This is only done if the 'exrc' option is set.
539 * Because of security reasons we disallow shell and write
540 * commands now, except for unix if the file is owned by the user
541 * or 'secure' option has been reset in environment of global
542 * ".gvimrc".
543 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
544 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
545 */
546 if (p_exrc)
547 {
548#ifdef UNIX
549 {
550 struct stat s;
551
552 /* if ".gvimrc" file is not owned by user, set 'secure'
553 * mode */
554 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
555 secure = p_secure;
556 }
557#else
558 secure = p_secure;
559#endif
560
561 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
562 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
563#ifdef SYS_GVIMRC_FILE
564 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
565 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
566#endif
567#ifdef USR_GVIMRC_FILE2
568 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
569 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
570#endif
571#ifdef USR_GVIMRC_FILE3
572 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
573 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
574#endif
575 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 if (secure == 2)
579 need_wait_return = TRUE;
580 secure = 0;
581 }
582 }
583
584 if (need_wait_return || msg_didany)
585 wait_return(TRUE);
586
587 --recursive;
588 }
589
590 /* If recursive call opened the shell, return here from the first call */
591 if (gui.in_use)
592 return;
593
594 /*
595 * Create the GUI shell.
596 */
597 gui.in_use = TRUE; /* Must be set after menus have been set up */
598 if (gui_mch_init() == FAIL)
599 goto error;
600
601 /* Avoid a delay for an error message that was printed in the terminal
602 * where Vim was started. */
603 emsg_on_display = FALSE;
604 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000605 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 need_wait_return = FALSE;
607 msg_didany = FALSE;
608
609 /*
610 * Check validity of any generic resources that may have been loaded.
611 */
612 if (gui.border_width < 0)
613 gui.border_width = 0;
614
615 /*
616 * Set up the fonts. First use a font specified with "-fn" or "-font".
617 */
618 if (font_argument != NULL)
619 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
620 if (
621#ifdef FEAT_XFONTSET
622 (*p_guifontset == NUL
623 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
624#endif
625 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
626 : p_guifont, FALSE) == FAIL)
627 {
628 EMSG(_("E665: Cannot start GUI, no valid font found"));
629 goto error2;
630 }
631#ifdef FEAT_MBYTE
632 if (gui_get_wide_font() == FAIL)
633 EMSG(_("E231: 'guifontwide' invalid"));
634#endif
635
636 gui.num_cols = Columns;
637 gui.num_rows = Rows;
638 gui_reset_scroll_region();
639
640 /* Create initial scrollbars */
641 FOR_ALL_WINDOWS(wp)
642 {
643 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
644 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
645 }
646 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
647
648#ifdef FEAT_MENU
649 gui_create_initial_menus(root_menu);
650#endif
651#ifdef FEAT_SUN_WORKSHOP
652 if (usingSunWorkShop)
653 workshop_init();
654#endif
655#ifdef FEAT_SIGN_ICONS
656 sign_gui_started();
657#endif
658
659 /* Configure the desired menu and scrollbars */
660 gui_init_which_components(NULL);
661
662 /* All components of the GUI have been created now */
663 gui.shell_created = TRUE;
664
665#ifndef FEAT_GUI_GTK
666 /* Set the shell size, adjusted for the screen size. For GTK this only
667 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000668 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#endif
670#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
671 /* Need to set the size of the menubar after all the menus have been
672 * created. */
673 gui_mch_compute_menu_height((Widget)0);
674#endif
675
676 /*
677 * Actually open the GUI shell.
678 */
679 if (gui_mch_open() != FAIL)
680 {
681#ifdef FEAT_TITLE
682 maketitle();
683 resettitle();
684#endif
685 init_gui_options();
686#ifdef FEAT_ARABIC
687 /* Our GUI can't do bidi. */
688 p_tbidi = FALSE;
689#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000690#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 /* Give GTK+ a chance to put all widget's into place. */
692 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000693
694# ifdef FEAT_MENU
695 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
696 * It was still there to make F10 work. */
697 if (vim_strchr(p_go, GO_MENUS) == NULL)
698 {
699 --gui.starting;
700 gui_mch_enable_menu(FALSE);
701 ++gui.starting;
702 gui_mch_update();
703 }
704# endif
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000707 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000709 /* When 'lines' was set while starting up the topframe may have to be
710 * resized. */
711 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000712
713#ifdef FEAT_BEVAL
714 /* Always create the Balloon Evaluation area, but disable it when
715 * 'ballooneval' is off */
716# ifdef FEAT_GUI_GTK
717 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
718 &general_beval_cb, NULL);
719# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000720# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000721 {
722 extern Widget textArea;
723 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000724 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000725 }
726# else
727# ifdef FEAT_GUI_W32
728 balloonEval = gui_mch_create_beval_area(NULL, NULL,
729 &general_beval_cb, NULL);
730# endif
731# endif
732# endif
733 if (!p_beval)
734 gui_mch_disable_beval_area(balloonEval);
735#endif
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
738 if (!im_xim_isvalid_imactivate())
739 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
740#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000741 /* When 'cmdheight' was set during startup it may not have taken
742 * effect yet. */
743 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000744 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746 return;
747 }
748
749error2:
750#ifdef FEAT_GUI_X11
751 /* undo gui_mch_init() */
752 gui_mch_uninit();
753#endif
754
755error:
756 gui.in_use = FALSE;
757 clip_init(FALSE);
758}
759
760
761 void
762gui_exit(rc)
763 int rc;
764{
765#ifndef __BEOS__
766 /* don't free the fonts, it leads to a BUS error
767 * richard@whitequeen.com Jul 99 */
768 free_highlight_fonts();
769#endif
770 gui.in_use = FALSE;
771 gui_mch_exit(rc);
772}
773
Bram Moolenaar9372a112005-12-06 19:59:18 +0000774#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000776# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777/*
778 * Called when the GUI shell is closed by the user. If there are no changed
779 * files Vim exits, otherwise there will be a dialog to ask the user what to
780 * do.
781 * When this function returns, Vim should NOT exit!
782 */
783 void
784gui_shell_closed()
785{
786 cmdmod_T save_cmdmod;
787
788 save_cmdmod = cmdmod;
789
790 /* Only exit when there are no changed files */
791 exiting = TRUE;
792# ifdef FEAT_BROWSE
793 cmdmod.browse = TRUE;
794# endif
795# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
796 cmdmod.confirm = TRUE;
797# endif
798 /* If there are changed buffers, present the user with a dialog if
799 * possible, otherwise give an error message. */
800 if (!check_changed_any(FALSE))
801 getout(0);
802
803 exiting = FALSE;
804 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000805 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807#endif
808
809/*
810 * Set the font. "font_list" is a a comma separated list of font names. The
811 * first font name that works is used. If none is found, use the default
812 * font.
813 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
814 * Return OK when able to set the font. When it failed FAIL is returned and
815 * the fonts are unchanged.
816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 int
818gui_init_font(font_list, fontset)
819 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000820 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822#define FONTLEN 320
823 char_u font_name[FONTLEN];
824 int font_list_empty = FALSE;
825 int ret = FAIL;
826
827 if (!gui.in_use)
828 return FAIL;
829
830 font_name[0] = NUL;
831 if (*font_list == NUL)
832 font_list_empty = TRUE;
833 else
834 {
835#ifdef FEAT_XFONTSET
836 /* When using a fontset, the whole list of fonts is one name. */
837 if (fontset)
838 ret = gui_mch_init_font(font_list, TRUE);
839 else
840#endif
841 while (*font_list != NUL)
842 {
843 /* Isolate one comma separated font name. */
844 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
845
846 /* Careful!!! The Win32 version of gui_mch_init_font(), when
847 * called with "*" will change p_guifont to the selected font
848 * name, which frees the old value. This makes font_list
849 * invalid. Thus when OK is returned here, font_list must no
850 * longer be used! */
851 if (gui_mch_init_font(font_name, FALSE) == OK)
852 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200853#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /* If it's a Unicode font, try setting 'guifontwide' to a
855 * similar double-width font. */
856 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
857 && strstr((char *)font_name, "10646") != NULL)
858 set_guifontwide(font_name);
859#endif
860 ret = OK;
861 break;
862 }
863 }
864 }
865
866 if (ret != OK
867 && STRCMP(font_list, "*") != 0
868 && (font_list_empty || gui.norm_font == NOFONT))
869 {
870 /*
871 * Couldn't load any font in 'font_list', keep the current font if
872 * there is one. If 'font_list' is empty, or if there is no current
873 * font, tell gui_mch_init_font() to try to find a font we can load.
874 */
875 ret = gui_mch_init_font(NULL, FALSE);
876 }
877
878 if (ret == OK)
879 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200880#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 /* Set normal font as current font */
882# ifdef FEAT_XFONTSET
883 if (gui.fontset != NOFONTSET)
884 gui_mch_set_fontset(gui.fontset);
885 else
886# endif
887 gui_mch_set_font(gui.norm_font);
888#endif
889 gui_set_shellsize(FALSE,
890#ifdef MSWIN
891 TRUE
892#else
893 FALSE
894#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000895 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897
898 return ret;
899}
900
901#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200902# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903/*
904 * Try setting 'guifontwide' to a font twice as wide as "name".
905 */
906 static void
907set_guifontwide(name)
908 char_u *name;
909{
910 int i = 0;
911 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
912 char_u *wp = NULL;
913 char_u *p;
914 GuiFont font;
915
916 wp = wide_name;
917 for (p = name; *p != NUL; ++p)
918 {
919 *wp++ = *p;
920 if (*p == '-')
921 {
922 ++i;
923 if (i == 6) /* font type: change "--" to "-*-" */
924 {
925 if (p[1] == '-')
926 *wp++ = '*';
927 }
928 else if (i == 12) /* found the width */
929 {
930 ++p;
931 i = getdigits(&p);
932 if (i != 0)
933 {
934 /* Double the width specification. */
935 sprintf((char *)wp, "%d%s", i * 2, p);
936 font = gui_mch_get_font(wide_name, FALSE);
937 if (font != NOFONT)
938 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000939 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 gui.wide_font = font;
941 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000942 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944 }
945 break;
946 }
947 }
948 }
949}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200950# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952/*
953 * Get the font for 'guifontwide'.
954 * Return FAIL for an invalid font name.
955 */
956 int
957gui_get_wide_font()
958{
959 GuiFont font = NOFONT;
960 char_u font_name[FONTLEN];
961 char_u *p;
962
963 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
964 return OK; /* Will give an error message later. */
965
966 if (p_guifontwide != NULL && *p_guifontwide != NUL)
967 {
968 for (p = p_guifontwide; *p != NUL; )
969 {
970 /* Isolate one comma separated font name. */
971 (void)copy_option_part(&p, font_name, FONTLEN, ",");
972 font = gui_mch_get_font(font_name, FALSE);
973 if (font != NOFONT)
974 break;
975 }
976 if (font == NOFONT)
977 return FAIL;
978 }
979
980 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200981#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
983 if (font != NOFONT && gui.norm_font != NOFONT
984 && pango_font_description_equal(font, gui.norm_font))
985 {
986 gui.wide_font = NOFONT;
987 gui_mch_free_font(font);
988 }
989 else
990#endif
991 gui.wide_font = font;
992 return OK;
993}
994#endif
995
996 void
997gui_set_cursor(row, col)
998 int row;
999 int col;
1000{
1001 gui.row = row;
1002 gui.col = col;
1003}
1004
1005/*
1006 * gui_check_pos - check if the cursor is on the screen.
1007 */
1008 static void
1009gui_check_pos()
1010{
1011 if (gui.row >= screen_Rows)
1012 gui.row = screen_Rows - 1;
1013 if (gui.col >= screen_Columns)
1014 gui.col = screen_Columns - 1;
1015 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1016 gui.cursor_is_valid = FALSE;
1017}
1018
1019/*
1020 * Redraw the cursor if necessary or when forced.
1021 * Careful: The contents of ScreenLines[] must match what is on the screen,
1022 * otherwise this goes wrong. May need to call out_flush() first.
1023 */
1024 void
1025gui_update_cursor(force, clear_selection)
1026 int force; /* when TRUE, update even when not moved */
1027 int clear_selection;/* clear selection under cursor */
1028{
1029 int cur_width = 0;
1030 int cur_height = 0;
1031 int old_hl_mask;
1032 int idx;
1033 int id;
1034 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1035 int cattr; /* cursor attributes */
1036 int attr;
1037 attrentry_T *aep = NULL;
1038
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001039 /* Don't update the cursor when halfway busy scrolling or the screen size
1040 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1041 if (!can_update_cursor || screen_Columns != gui.num_cols
1042 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 return;
1044
1045 gui_check_pos();
1046 if (!gui.cursor_is_valid || force
1047 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1048 {
1049 gui_undraw_cursor();
1050 if (gui.row < 0)
1051 return;
1052#ifdef USE_IM_CONTROL
1053 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1054 im_set_position(gui.row, gui.col);
1055#endif
1056 gui.cursor_row = gui.row;
1057 gui.cursor_col = gui.col;
1058
1059 /* Only write to the screen after ScreenLines[] has been initialized */
1060 if (!screen_cleared || ScreenLines == NULL)
1061 return;
1062
1063 /* Clear the selection if we are about to write over it */
1064 if (clear_selection)
1065 clip_may_clear_selection(gui.row, gui.row);
1066 /* Check that the cursor is inside the shell (resizing may have made
1067 * it invalid) */
1068 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1069 return;
1070
1071 gui.cursor_is_valid = TRUE;
1072
1073 /*
1074 * How the cursor is drawn depends on the current mode.
1075 */
1076 idx = get_shape_idx(FALSE);
1077 if (State & LANGMAP)
1078 id = shape_table[idx].id_lm;
1079 else
1080 id = shape_table[idx].id;
1081
1082 /* get the colors and attributes for the cursor. Default is inverted */
1083 cfg = INVALCOLOR;
1084 cbg = INVALCOLOR;
1085 cattr = HL_INVERSE;
1086 gui_mch_set_blinking(shape_table[idx].blinkwait,
1087 shape_table[idx].blinkon,
1088 shape_table[idx].blinkoff);
1089 if (id > 0)
1090 {
1091 cattr = syn_id2colors(id, &cfg, &cbg);
1092#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1093 {
1094 static int iid;
1095 guicolor_T fg, bg;
1096
Bram Moolenaarc236c162008-07-13 17:41:49 +00001097 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001098# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001099 preedit_get_status()
1100# else
1101 im_get_status()
1102# endif
1103 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
1105 iid = syn_name2id((char_u *)"CursorIM");
1106 if (iid > 0)
1107 {
1108 syn_id2colors(iid, &fg, &bg);
1109 if (bg != INVALCOLOR)
1110 cbg = bg;
1111 if (fg != INVALCOLOR)
1112 cfg = fg;
1113 }
1114 }
1115 }
1116#endif
1117 }
1118
1119 /*
1120 * Get the attributes for the character under the cursor.
1121 * When no cursor color was given, use the character color.
1122 */
1123 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1124 if (attr > HL_ALL)
1125 aep = syn_gui_attr2entry(attr);
1126 if (aep != NULL)
1127 {
1128 attr = aep->ae_attr;
1129 if (cfg == INVALCOLOR)
1130 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1131 : aep->ae_u.gui.fg_color);
1132 if (cbg == INVALCOLOR)
1133 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1134 : aep->ae_u.gui.bg_color);
1135 }
1136 if (cfg == INVALCOLOR)
1137 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1138 if (cbg == INVALCOLOR)
1139 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1140
1141#ifdef FEAT_XIM
1142 if (aep != NULL)
1143 {
1144 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1145 : aep->ae_u.gui.bg_color);
1146 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1147 : aep->ae_u.gui.fg_color);
1148 if (xim_bg_color == INVALCOLOR)
1149 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1150 : gui.back_pixel;
1151 if (xim_fg_color == INVALCOLOR)
1152 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1153 : gui.norm_pixel;
1154 }
1155 else
1156 {
1157 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1158 : gui.back_pixel;
1159 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1160 : gui.norm_pixel;
1161 }
1162#endif
1163
1164 attr &= ~HL_INVERSE;
1165 if (cattr & HL_INVERSE)
1166 {
1167 cc = cbg;
1168 cbg = cfg;
1169 cfg = cc;
1170 }
1171 cattr &= ~HL_INVERSE;
1172
1173 /*
1174 * When we don't have window focus, draw a hollow cursor.
1175 */
1176 if (!gui.in_focus)
1177 {
1178 gui_mch_draw_hollow_cursor(cbg);
1179 return;
1180 }
1181
1182 old_hl_mask = gui.highlight_mask;
1183 if (shape_table[idx].shape == SHAPE_BLOCK
1184#ifdef FEAT_HANGULIN
1185 || composing_hangul
1186#endif
1187 )
1188 {
1189 /*
1190 * Draw the text character with the cursor colors. Use the
1191 * character attributes plus the cursor attributes.
1192 */
1193 gui.highlight_mask = (cattr | attr);
1194#ifdef FEAT_HANGULIN
1195 if (composing_hangul)
1196 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1197 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1198 else
1199#endif
1200 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1201 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1202 }
1203 else
1204 {
1205#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1206 int col_off = FALSE;
1207#endif
1208 /*
1209 * First draw the partial cursor, then overwrite with the text
1210 * character, using a transparent background.
1211 */
1212 if (shape_table[idx].shape == SHAPE_VER)
1213 {
1214 cur_height = gui.char_height;
1215 cur_width = (gui.char_width * shape_table[idx].percentage
1216 + 99) / 100;
1217 }
1218 else
1219 {
1220 cur_height = (gui.char_height * shape_table[idx].percentage
1221 + 99) / 100;
1222 cur_width = gui.char_width;
1223 }
1224#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001225 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1226 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
1228 /* Double wide character. */
1229 if (shape_table[idx].shape != SHAPE_VER)
1230 cur_width += gui.char_width;
1231# ifdef FEAT_RIGHTLEFT
1232 if (CURSOR_BAR_RIGHT)
1233 {
1234 /* gui.col points to the left halve of the character but
1235 * the vertical line needs to be on the right halve.
1236 * A double-wide horizontal line is also drawn from the
1237 * right halve in gui_mch_draw_part_cursor(). */
1238 col_off = TRUE;
1239 ++gui.col;
1240 }
1241# endif
1242 }
1243#endif
1244 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1245#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1246 if (col_off)
1247 --gui.col;
1248#endif
1249
1250#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1251 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1252 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1253 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1254 (guicolor_T)0, (guicolor_T)0, 0);
1255#endif
1256 }
1257 gui.highlight_mask = old_hl_mask;
1258 }
1259}
1260
1261#if defined(FEAT_MENU) || defined(PROTO)
1262 void
1263gui_position_menu()
1264{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001265# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (gui.menu_is_active && gui.in_use)
1267 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1268# endif
1269}
1270#endif
1271
1272/*
1273 * Position the various GUI components (text area, menu). The vertical
1274 * scrollbars are NOT handled here. See gui_update_scrollbars().
1275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 static void
1277gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001278 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 int text_area_x;
1281 int text_area_y;
1282 int text_area_width;
1283 int text_area_height;
1284
1285 /* avoid that moving components around generates events */
1286 ++hold_gui_events;
1287
1288 text_area_x = 0;
1289 if (gui.which_scrollbars[SBAR_LEFT])
1290 text_area_x += gui.scrollbar_width;
1291
1292 text_area_y = 0;
1293#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1294 gui.menu_width = total_width;
1295 if (gui.menu_is_active)
1296 text_area_y += gui.menu_height;
1297#endif
1298#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1299 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1300 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1301#endif
1302
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001303# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001304 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001305 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001306 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1310 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1311 {
1312# ifdef FEAT_GUI_ATHENA
1313 gui_mch_set_toolbar_pos(0, text_area_y,
1314 gui.menu_width, gui.toolbar_height);
1315# endif
1316 text_area_y += gui.toolbar_height;
1317 }
1318#endif
1319
1320 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1321 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1322
1323 gui_mch_set_text_area_pos(text_area_x,
1324 text_area_y,
1325 text_area_width,
1326 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001327#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 + xim_get_status_area_height()
1329#endif
1330 );
1331#ifdef FEAT_MENU
1332 gui_position_menu();
1333#endif
1334 if (gui.which_scrollbars[SBAR_BOTTOM])
1335 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1336 text_area_x,
1337 text_area_y + text_area_height,
1338 text_area_width,
1339 gui.scrollbar_height);
1340 gui.left_sbar_x = 0;
1341 gui.right_sbar_x = text_area_x + text_area_width;
1342
1343 --hold_gui_events;
1344}
1345
Bram Moolenaar02743632005-07-25 20:42:36 +00001346/*
1347 * Get the width of the widgets and decorations to the side of the text area.
1348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 int
1350gui_get_base_width()
1351{
1352 int base_width;
1353
1354 base_width = 2 * gui.border_offset;
1355 if (gui.which_scrollbars[SBAR_LEFT])
1356 base_width += gui.scrollbar_width;
1357 if (gui.which_scrollbars[SBAR_RIGHT])
1358 base_width += gui.scrollbar_width;
1359 return base_width;
1360}
1361
Bram Moolenaar02743632005-07-25 20:42:36 +00001362/*
1363 * Get the height of the widgets and decorations above and below the text area.
1364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 int
1366gui_get_base_height()
1367{
1368 int base_height;
1369
1370 base_height = 2 * gui.border_offset;
1371 if (gui.which_scrollbars[SBAR_BOTTOM])
1372 base_height += gui.scrollbar_height;
1373#ifdef FEAT_GUI_GTK
1374 /* We can't take the sizes properly into account until anything is
1375 * realized. Therefore we recalculate all the values here just before
1376 * setting the size. (--mdcki) */
1377#else
1378# ifdef FEAT_MENU
1379 if (gui.menu_is_active)
1380 base_height += gui.menu_height;
1381# endif
1382# ifdef FEAT_TOOLBAR
1383 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1384# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1385 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1386# else
1387 base_height += gui.toolbar_height;
1388# endif
1389# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001390# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1391 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001392 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001393 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395# ifdef FEAT_FOOTER
1396 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1397 base_height += gui.footer_height;
1398# endif
1399# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1400 base_height += gui_mch_text_area_extra_height();
1401# endif
1402#endif
1403 return base_height;
1404}
1405
1406/*
1407 * Should be called after the GUI shell has been resized. Its arguments are
1408 * the new width and height of the shell in pixels.
1409 */
1410 void
1411gui_resize_shell(pixel_width, pixel_height)
1412 int pixel_width;
1413 int pixel_height;
1414{
1415 static int busy = FALSE;
1416
1417 if (!gui.shell_created) /* ignore when still initializing */
1418 return;
1419
1420 /*
1421 * Can't resize the screen while it is being redrawn. Remember the new
1422 * size and handle it later.
1423 */
1424 if (updating_screen || busy)
1425 {
1426 new_pixel_width = pixel_width;
1427 new_pixel_height = pixel_height;
1428 return;
1429 }
1430
1431again:
1432 busy = TRUE;
1433
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 /* Flush pending output before redrawing */
1435 out_flush();
1436
1437 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001438 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 gui_position_components(pixel_width);
1441
1442 gui_reset_scroll_region();
1443 /*
1444 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1445 * at the last line here (why does it have to be one row too low?).
1446 */
1447 if (State == ASKMORE || State == CONFIRM)
1448 gui.row = gui.num_rows;
1449
1450 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1451 * the safe side. */
1452 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1453 || gui.num_rows != Rows || gui.num_cols != Columns)
1454 shell_resized();
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 gui_update_scrollbars(TRUE);
1457 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001458#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 xim_set_status_area();
1460#endif
1461
1462 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 /*
1465 * We could have been called again while redrawing the screen.
1466 * Need to do it all again with the latest size then.
1467 */
1468 if (new_pixel_height)
1469 {
1470 pixel_width = new_pixel_width;
1471 pixel_height = new_pixel_height;
1472 new_pixel_width = 0;
1473 new_pixel_height = 0;
1474 goto again;
1475 }
1476}
1477
1478/*
1479 * Check if gui_resize_shell() must be called.
1480 */
1481 void
1482gui_may_resize_shell()
1483{
1484 int h, w;
1485
1486 if (new_pixel_height)
1487 {
1488 /* careful: gui_resize_shell() may postpone the resize again if we
1489 * were called indirectly by it */
1490 w = new_pixel_width;
1491 h = new_pixel_height;
1492 new_pixel_width = 0;
1493 new_pixel_height = 0;
1494 gui_resize_shell(w, h);
1495 }
1496}
1497
1498 int
1499gui_get_shellsize()
1500{
1501 Rows = gui.num_rows;
1502 Columns = gui.num_cols;
1503 return OK;
1504}
1505
1506/*
1507 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001508 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1509 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001512gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001513 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001515 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 int base_width;
1518 int base_height;
1519 int width;
1520 int height;
1521 int min_width;
1522 int min_height;
1523 int screen_w;
1524 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001525#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001526 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001527 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001528#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001529 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
1531 if (!gui.shell_created)
1532 return;
1533
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001534#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 /* If not setting to a user specified size and maximized, calculate the
1536 * number of characters that fit in the maximized window. */
1537 if (!mustset && gui_mch_maximized())
1538 {
1539 gui_mch_newfont();
1540 return;
1541 }
1542#endif
1543
1544 base_width = gui_get_base_width();
1545 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001546 if (fit_to_display)
1547 /* Remember the original window position. */
1548 gui_mch_get_winpos(&x, &y);
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#ifdef USE_SUN_WORKSHOP
1551 if (!mustset && usingSunWorkShop
1552 && workshop_get_width_height(&width, &height))
1553 {
1554 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1555 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1556 }
1557 else
1558#endif
1559 {
1560 width = Columns * gui.char_width + base_width;
1561 height = Rows * gui.char_height + base_height;
1562 }
1563
1564 if (fit_to_display)
1565 {
1566 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001567 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569 Columns = (screen_w - base_width) / gui.char_width;
1570 if (Columns < MIN_COLUMNS)
1571 Columns = MIN_COLUMNS;
1572 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001573#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001574 ++did_adjust;
1575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001577 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
1579 Rows = (screen_h - base_height) / gui.char_height;
1580 check_shellsize();
1581 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001582#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001583 ++did_adjust;
1584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001586#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001587 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1588 && height + gui.char_height >= screen_h))
1589 /* don't unmaximize if at maximum size */
1590 un_maximize = FALSE;
1591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 gui.num_cols = Columns;
1594 gui.num_rows = Rows;
1595
1596 min_width = base_width + MIN_COLUMNS * gui.char_width;
1597 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001598#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001599 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001600#endif
1601
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001602#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001603 if (un_maximize)
1604 {
1605 /* If the window size is smaller than the screen unmaximize the
1606 * window, otherwise resizing won't work. */
1607 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1608 if ((width + gui.char_width < screen_w
1609 || height + gui.char_height * 2 < screen_h)
1610 && gui_mch_maximized())
1611 gui_mch_unmaximize();
1612 }
1613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001616 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001618 if (fit_to_display && x >= 0 && y >= 0)
1619 {
1620 /* Some window managers put the Vim window left of/above the screen.
1621 * Only change the position if it wasn't already negative before
1622 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 gui_mch_update();
1624 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1625 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1626 }
1627
1628 gui_position_components(width);
1629 gui_update_scrollbars(TRUE);
1630 gui_reset_scroll_region();
1631}
1632
1633/*
1634 * Called when Rows and/or Columns has changed.
1635 */
1636 void
1637gui_new_shellsize()
1638{
1639 gui_reset_scroll_region();
1640}
1641
1642/*
1643 * Make scroll region cover whole screen.
1644 */
1645 void
1646gui_reset_scroll_region()
1647{
1648 gui.scroll_region_top = 0;
1649 gui.scroll_region_bot = gui.num_rows - 1;
1650 gui.scroll_region_left = 0;
1651 gui.scroll_region_right = gui.num_cols - 1;
1652}
1653
1654 void
1655gui_start_highlight(mask)
1656 int mask;
1657{
1658 if (mask > HL_ALL) /* highlight code */
1659 gui.highlight_mask = mask;
1660 else /* mask */
1661 gui.highlight_mask |= mask;
1662}
1663
1664 void
1665gui_stop_highlight(mask)
1666 int mask;
1667{
1668 if (mask > HL_ALL) /* highlight code */
1669 gui.highlight_mask = HL_NORMAL;
1670 else /* mask */
1671 gui.highlight_mask &= ~mask;
1672}
1673
1674/*
1675 * Clear a rectangular region of the screen from text pos (row1, col1) to
1676 * (row2, col2) inclusive.
1677 */
1678 void
1679gui_clear_block(row1, col1, row2, col2)
1680 int row1;
1681 int col1;
1682 int row2;
1683 int col2;
1684{
1685 /* Clear the selection if we are about to write over it */
1686 clip_may_clear_selection(row1, row2);
1687
1688 gui_mch_clear_block(row1, col1, row2, col2);
1689
1690 /* Invalidate cursor if it was in this block */
1691 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1692 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1693 gui.cursor_is_valid = FALSE;
1694}
1695
1696/*
1697 * Write code to update the cursor later. This avoids the need to flush the
1698 * output buffer before calling gui_update_cursor().
1699 */
1700 void
1701gui_update_cursor_later()
1702{
1703 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1704}
1705
1706 void
1707gui_write(s, len)
1708 char_u *s;
1709 int len;
1710{
1711 char_u *p;
1712 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 int force_scrollbar = FALSE;
1715 static win_T *old_curwin = NULL;
1716
1717/* #define DEBUG_GUI_WRITE */
1718#ifdef DEBUG_GUI_WRITE
1719 {
1720 int i;
1721 char_u *str;
1722
1723 printf("gui_write(%d):\n ", len);
1724 for (i = 0; i < len; i++)
1725 if (s[i] == ESC)
1726 {
1727 if (i != 0)
1728 printf("\n ");
1729 printf("<ESC>");
1730 }
1731 else
1732 {
1733 str = transchar_byte(s[i]);
1734 if (str[0] && str[1])
1735 printf("<%s>", (char *)str);
1736 else
1737 printf("%s", (char *)str);
1738 }
1739 printf("\n");
1740 }
1741#endif
1742 while (len)
1743 {
1744 if (s[0] == ESC && s[1] == '|')
1745 {
1746 p = s + 2;
1747 if (VIM_ISDIGIT(*p))
1748 {
1749 arg1 = getdigits(&p);
1750 if (p > s + len)
1751 break;
1752 if (*p == ';')
1753 {
1754 ++p;
1755 arg2 = getdigits(&p);
1756 if (p > s + len)
1757 break;
1758 }
1759 }
1760 switch (*p)
1761 {
1762 case 'C': /* Clear screen */
1763 clip_scroll_selection(9999);
1764 gui_mch_clear_all();
1765 gui.cursor_is_valid = FALSE;
1766 force_scrollbar = TRUE;
1767 break;
1768 case 'M': /* Move cursor */
1769 gui_set_cursor(arg1, arg2);
1770 break;
1771 case 's': /* force cursor (shape) update */
1772 force_cursor = TRUE;
1773 break;
1774 case 'R': /* Set scroll region */
1775 if (arg1 < arg2)
1776 {
1777 gui.scroll_region_top = arg1;
1778 gui.scroll_region_bot = arg2;
1779 }
1780 else
1781 {
1782 gui.scroll_region_top = arg2;
1783 gui.scroll_region_bot = arg1;
1784 }
1785 break;
1786#ifdef FEAT_VERTSPLIT
1787 case 'V': /* Set vertical scroll region */
1788 if (arg1 < arg2)
1789 {
1790 gui.scroll_region_left = arg1;
1791 gui.scroll_region_right = arg2;
1792 }
1793 else
1794 {
1795 gui.scroll_region_left = arg2;
1796 gui.scroll_region_right = arg1;
1797 }
1798 break;
1799#endif
1800 case 'd': /* Delete line */
1801 gui_delete_lines(gui.row, 1);
1802 break;
1803 case 'D': /* Delete lines */
1804 gui_delete_lines(gui.row, arg1);
1805 break;
1806 case 'i': /* Insert line */
1807 gui_insert_lines(gui.row, 1);
1808 break;
1809 case 'I': /* Insert lines */
1810 gui_insert_lines(gui.row, arg1);
1811 break;
1812 case '$': /* Clear to end-of-line */
1813 gui_clear_block(gui.row, gui.col, gui.row,
1814 (int)Columns - 1);
1815 break;
1816 case 'h': /* Turn on highlighting */
1817 gui_start_highlight(arg1);
1818 break;
1819 case 'H': /* Turn off highlighting */
1820 gui_stop_highlight(arg1);
1821 break;
1822 case 'f': /* flash the window (visual bell) */
1823 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1824 break;
1825 default:
1826 p = s + 1; /* Skip the ESC */
1827 break;
1828 }
1829 len -= (int)(++p - s);
1830 s = p;
1831 }
1832 else if (
1833#ifdef EBCDIC
1834 CtrlChar(s[0]) != 0 /* Ctrl character */
1835#else
1836 s[0] < 0x20 /* Ctrl character */
1837#endif
1838#ifdef FEAT_SIGN_ICONS
1839 && s[0] != SIGN_BYTE
1840# ifdef FEAT_NETBEANS_INTG
1841 && s[0] != MULTISIGN_BYTE
1842# endif
1843#endif
1844 )
1845 {
1846 if (s[0] == '\n') /* NL */
1847 {
1848 gui.col = 0;
1849 if (gui.row < gui.scroll_region_bot)
1850 gui.row++;
1851 else
1852 gui_delete_lines(gui.scroll_region_top, 1);
1853 }
1854 else if (s[0] == '\r') /* CR */
1855 {
1856 gui.col = 0;
1857 }
1858 else if (s[0] == '\b') /* Backspace */
1859 {
1860 if (gui.col)
1861 --gui.col;
1862 }
1863 else if (s[0] == Ctrl_L) /* cursor-right */
1864 {
1865 ++gui.col;
1866 }
1867 else if (s[0] == Ctrl_G) /* Beep */
1868 {
1869 gui_mch_beep();
1870 }
1871 /* Other Ctrl character: shouldn't happen! */
1872
1873 --len; /* Skip this char */
1874 ++s;
1875 }
1876 else
1877 {
1878 p = s;
1879 while (len > 0 && (
1880#ifdef EBCDIC
1881 CtrlChar(*p) == 0
1882#else
1883 *p >= 0x20
1884#endif
1885#ifdef FEAT_SIGN_ICONS
1886 || *p == SIGN_BYTE
1887# ifdef FEAT_NETBEANS_INTG
1888 || *p == MULTISIGN_BYTE
1889# endif
1890#endif
1891 ))
1892 {
1893 len--;
1894 p++;
1895 }
1896 gui_outstr(s, (int)(p - s));
1897 s = p;
1898 }
1899 }
1900
1901 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1902 * set). */
1903 if (force_cursor)
1904 gui_update_cursor(TRUE, TRUE);
1905
1906 /* When switching to another window the dragging must have stopped.
1907 * Required for GTK, dragged_sb isn't reset. */
1908 if (old_curwin != curwin)
1909 gui.dragged_sb = SBAR_NONE;
1910
1911 /* Update the scrollbars after clearing the screen or when switched
1912 * to another window.
1913 * Update the horizontal scrollbar always, it's difficult to check all
1914 * situations where it might change. */
1915 if (force_scrollbar || old_curwin != curwin)
1916 gui_update_scrollbars(force_scrollbar);
1917 else
1918 gui_update_horiz_scrollbar(FALSE);
1919 old_curwin = curwin;
1920
1921 /*
1922 * We need to make sure this is cleared since Athena doesn't tell us when
1923 * he is done dragging. Do the same for GTK.
1924 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001925#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 gui.dragged_sb = SBAR_NONE;
1927#endif
1928
1929 gui_mch_flush(); /* In case vim decides to take a nap */
1930}
1931
1932/*
1933 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1934 * produces wrong results. Call gui_dont_update_cursor() before that code and
1935 * gui_can_update_cursor() afterwards.
1936 */
1937 void
1938gui_dont_update_cursor()
1939{
1940 if (gui.in_use)
1941 {
1942 /* Undraw the cursor now, we probably can't do it after the change. */
1943 gui_undraw_cursor();
1944 can_update_cursor = FALSE;
1945 }
1946}
1947
1948 void
1949gui_can_update_cursor()
1950{
1951 can_update_cursor = TRUE;
1952 /* No need to update the cursor right now, there is always more output
1953 * after scrolling. */
1954}
1955
1956 static void
1957gui_outstr(s, len)
1958 char_u *s;
1959 int len;
1960{
1961 int this_len;
1962#ifdef FEAT_MBYTE
1963 int cells;
1964#endif
1965
1966 if (len == 0)
1967 return;
1968
1969 if (len < 0)
1970 len = (int)STRLEN(s);
1971
1972 while (len > 0)
1973 {
1974#ifdef FEAT_MBYTE
1975 if (has_mbyte)
1976 {
1977 /* Find out how many chars fit in the current line. */
1978 cells = 0;
1979 for (this_len = 0; this_len < len; )
1980 {
1981 cells += (*mb_ptr2cells)(s + this_len);
1982 if (gui.col + cells > Columns)
1983 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001984 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 if (this_len > len)
1987 this_len = len; /* don't include following composing char */
1988 }
1989 else
1990#endif
1991 if (gui.col + len > Columns)
1992 this_len = Columns - gui.col;
1993 else
1994 this_len = len;
1995
1996 (void)gui_outstr_nowrap(s, this_len,
1997 0, (guicolor_T)0, (guicolor_T)0, 0);
1998 s += this_len;
1999 len -= this_len;
2000#ifdef FEAT_MBYTE
2001 /* fill up for a double-width char that doesn't fit. */
2002 if (len > 0 && gui.col < Columns)
2003 (void)gui_outstr_nowrap((char_u *)" ", 1,
2004 0, (guicolor_T)0, (guicolor_T)0, 0);
2005#endif
2006 /* The cursor may wrap to the next line. */
2007 if (gui.col >= Columns)
2008 {
2009 gui.col = 0;
2010 gui.row++;
2011 }
2012 }
2013}
2014
2015/*
2016 * Output one character (may be one or two display cells).
2017 * Caller must check for valid "off".
2018 * Returns FAIL or OK, just like gui_outstr_nowrap().
2019 */
2020 static int
2021gui_screenchar(off, flags, fg, bg, back)
2022 int off; /* Offset from start of screen */
2023 int flags;
2024 guicolor_T fg, bg; /* colors for cursor */
2025 int back; /* backup this many chars when using bold trick */
2026{
2027#ifdef FEAT_MBYTE
2028 char_u buf[MB_MAXBYTES + 1];
2029
2030 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2031 if (enc_utf8 && ScreenLines[off] == 0)
2032 return OK;
2033
2034 if (enc_utf8 && ScreenLinesUC[off] != 0)
2035 /* Draw UTF-8 multi-byte character. */
2036 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2037 flags, fg, bg, back);
2038
2039 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2040 {
2041 buf[0] = ScreenLines[off];
2042 buf[1] = ScreenLines2[off];
2043 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2044 }
2045
2046 /* Draw non-multi-byte character or DBCS character. */
2047 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002048 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 flags, fg, bg, back);
2050#else
2051 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2052#endif
2053}
2054
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002055#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056/*
2057 * Output the string at the given screen position. This is used in place
2058 * of gui_screenchar() where possible because Pango needs as much context
2059 * as possible to work nicely. It's a lot faster as well.
2060 */
2061 static int
2062gui_screenstr(off, len, flags, fg, bg, back)
2063 int off; /* Offset from start of screen */
2064 int len; /* string length in screen cells */
2065 int flags;
2066 guicolor_T fg, bg; /* colors for cursor */
2067 int back; /* backup this many chars when using bold trick */
2068{
2069 char_u *buf;
2070 int outlen = 0;
2071 int i;
2072 int retval;
2073
2074 if (len <= 0) /* "cannot happen"? */
2075 return OK;
2076
2077 if (enc_utf8)
2078 {
2079 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2080 if (buf == NULL)
2081 return OK; /* not much we could do here... */
2082
2083 for (i = off; i < off + len; ++i)
2084 {
2085 if (ScreenLines[i] == 0)
2086 continue; /* skip second half of double-width char */
2087
2088 if (ScreenLinesUC[i] == 0)
2089 buf[outlen++] = ScreenLines[i];
2090 else
2091 outlen += utfc_char2bytes(i, buf + outlen);
2092 }
2093
2094 buf[outlen] = NUL; /* only to aid debugging */
2095 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2096 vim_free(buf);
2097
2098 return retval;
2099 }
2100 else if (enc_dbcs == DBCS_JPNU)
2101 {
2102 buf = alloc((unsigned)(len * 2 + 1));
2103 if (buf == NULL)
2104 return OK; /* not much we could do here... */
2105
2106 for (i = off; i < off + len; ++i)
2107 {
2108 buf[outlen++] = ScreenLines[i];
2109
2110 /* handle double-byte single-width char */
2111 if (ScreenLines[i] == 0x8e)
2112 buf[outlen++] = ScreenLines2[i];
2113 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2114 buf[outlen++] = ScreenLines[++i];
2115 }
2116
2117 buf[outlen] = NUL; /* only to aid debugging */
2118 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2119 vim_free(buf);
2120
2121 return retval;
2122 }
2123 else
2124 {
2125 return gui_outstr_nowrap(&ScreenLines[off], len,
2126 flags, fg, bg, back);
2127 }
2128}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002129#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131/*
2132 * Output the given string at the current cursor position. If the string is
2133 * too long to fit on the line, then it is truncated.
2134 * "flags":
2135 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2136 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002137 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 * background.
2139 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2140 * it.
2141 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2142 * FAIL (the caller should start drawing "back" chars back).
2143 */
2144 int
2145gui_outstr_nowrap(s, len, flags, fg, bg, back)
2146 char_u *s;
2147 int len;
2148 int flags;
2149 guicolor_T fg, bg; /* colors for cursor */
2150 int back; /* backup this many chars when using bold trick */
2151{
2152 long_u highlight_mask;
2153 long_u hl_mask_todo;
2154 guicolor_T fg_color;
2155 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002156 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002157#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 GuiFont font = NOFONT;
2159# ifdef FEAT_XFONTSET
2160 GuiFontset fontset = NOFONTSET;
2161# endif
2162#endif
2163 attrentry_T *aep = NULL;
2164 int draw_flags;
2165 int col = gui.col;
2166#ifdef FEAT_SIGN_ICONS
2167 int draw_sign = FALSE;
2168# ifdef FEAT_NETBEANS_INTG
2169 int multi_sign = FALSE;
2170# endif
2171#endif
2172
2173 if (len < 0)
2174 len = (int)STRLEN(s);
2175 if (len == 0)
2176 return OK;
2177
2178#ifdef FEAT_SIGN_ICONS
2179 if (*s == SIGN_BYTE
2180# ifdef FEAT_NETBEANS_INTG
2181 || *s == MULTISIGN_BYTE
2182# endif
2183 )
2184 {
2185# ifdef FEAT_NETBEANS_INTG
2186 if (*s == MULTISIGN_BYTE)
2187 multi_sign = TRUE;
2188# endif
2189 /* draw spaces instead */
2190 s = (char_u *)" ";
2191 if (len == 1 && col > 0)
2192 --col;
2193 len = 2;
2194 draw_sign = TRUE;
2195 highlight_mask = 0;
2196 }
2197 else
2198#endif
2199 if (gui.highlight_mask > HL_ALL)
2200 {
2201 aep = syn_gui_attr2entry(gui.highlight_mask);
2202 if (aep == NULL) /* highlighting not set */
2203 highlight_mask = 0;
2204 else
2205 highlight_mask = aep->ae_attr;
2206 }
2207 else
2208 highlight_mask = gui.highlight_mask;
2209 hl_mask_todo = highlight_mask;
2210
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002211#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 /* Set the font */
2213 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2214 font = aep->ae_u.gui.font;
2215# ifdef FEAT_XFONTSET
2216 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2217 fontset = aep->ae_u.gui.fontset;
2218# endif
2219 else
2220 {
2221# ifdef FEAT_XFONTSET
2222 if (gui.fontset != NOFONTSET)
2223 fontset = gui.fontset;
2224 else
2225# endif
2226 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2227 {
2228 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2229 {
2230 font = gui.boldital_font;
2231 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2232 }
2233 else if (gui.bold_font != NOFONT)
2234 {
2235 font = gui.bold_font;
2236 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2237 }
2238 else
2239 font = gui.norm_font;
2240 }
2241 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2242 {
2243 font = gui.ital_font;
2244 hl_mask_todo &= ~HL_ITALIC;
2245 }
2246 else
2247 font = gui.norm_font;
2248 }
2249# ifdef FEAT_XFONTSET
2250 if (fontset != NOFONTSET)
2251 gui_mch_set_fontset(fontset);
2252 else
2253# endif
2254 gui_mch_set_font(font);
2255#endif
2256
2257 draw_flags = 0;
2258
2259 /* Set the color */
2260 bg_color = gui.back_pixel;
2261 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2262 {
2263 draw_flags |= DRAW_CURSOR;
2264 fg_color = fg;
2265 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002266 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 else if (aep != NULL)
2269 {
2270 fg_color = aep->ae_u.gui.fg_color;
2271 if (fg_color == INVALCOLOR)
2272 fg_color = gui.norm_pixel;
2273 bg_color = aep->ae_u.gui.bg_color;
2274 if (bg_color == INVALCOLOR)
2275 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002276 sp_color = aep->ae_u.gui.sp_color;
2277 if (sp_color == INVALCOLOR)
2278 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002283 sp_color = fg_color;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2287 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002288#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 gui_mch_set_colors(bg_color, fg_color);
2290#else
2291 gui_mch_set_fg_color(bg_color);
2292 gui_mch_set_bg_color(fg_color);
2293#endif
2294 }
2295 else
2296 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002297#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 gui_mch_set_colors(fg_color, bg_color);
2299#else
2300 gui_mch_set_fg_color(fg_color);
2301 gui_mch_set_bg_color(bg_color);
2302#endif
2303 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002304 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 /* Clear the selection if we are about to write over it */
2307 if (!(flags & GUI_MON_NOCLEAR))
2308 clip_may_clear_selection(gui.row, gui.row);
2309
2310
2311#ifndef MSWIN16_FASTTEXT
2312 /* If there's no bold font, then fake it */
2313 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2314 draw_flags |= DRAW_BOLD;
2315#endif
2316
2317 /*
2318 * When drawing bold or italic characters the spill-over from the left
2319 * neighbor may be destroyed. Let the caller backup to start redrawing
2320 * just after a blank.
2321 */
2322 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2323 return FAIL;
2324
Bram Moolenaare60acc12011-05-10 16:41:25 +02002325#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 /* If there's no italic font, then fake it.
2327 * For GTK2, we don't need a different font for italic style. */
2328 if (hl_mask_todo & HL_ITALIC)
2329 draw_flags |= DRAW_ITALIC;
2330
2331 /* Do we underline the text? */
2332 if (hl_mask_todo & HL_UNDERLINE)
2333 draw_flags |= DRAW_UNDERL;
2334#else
2335 /* Do we underline the text? */
2336 if ((hl_mask_todo & HL_UNDERLINE)
2337# ifndef MSWIN16_FASTTEXT
2338 || (hl_mask_todo & HL_ITALIC)
2339# endif
2340 )
2341 draw_flags |= DRAW_UNDERL;
2342#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002343 /* Do we undercurl the text? */
2344 if (hl_mask_todo & HL_UNDERCURL)
2345 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002347 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (flags & GUI_MON_TRS_CURSOR)
2349 draw_flags |= DRAW_TRANSP;
2350
2351 /*
2352 * Draw the text.
2353 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002354#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 /* The value returned is the length in display cells */
2356 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2357#else
2358# ifdef FEAT_MBYTE
2359 if (enc_utf8)
2360 {
2361 int start; /* index of bytes to be drawn */
2362 int cells; /* cellwidth of bytes to be drawn */
2363 int thislen; /* length of bytes to be drawin */
2364 int cn; /* cellwidth of current char */
2365 int i; /* index of current char */
2366 int c; /* current char value */
2367 int cl; /* byte length of current char */
2368 int comping; /* current char is composing */
2369 int scol = col; /* screen column */
2370 int dowide; /* use 'guifontwide' */
2371
2372 /* Break the string at a composing character, it has to be drawn on
2373 * top of the previous character. */
2374 start = 0;
2375 cells = 0;
2376 for (i = 0; i < len; i += cl)
2377 {
2378 c = utf_ptr2char(s + i);
2379 cn = utf_char2cells(c);
2380 if (cn > 1
2381# ifdef FEAT_XFONTSET
2382 && fontset == NOFONTSET
2383# endif
2384 && gui.wide_font != NOFONT)
2385 dowide = TRUE;
2386 else
2387 dowide = FALSE;
2388 comping = utf_iscomposing(c);
2389 if (!comping) /* count cells from non-composing chars */
2390 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002391 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (cl == 0) /* hit end of string */
2393 len = i + cl; /* len must be wrong "cannot happen" */
2394
2395 /* print the string so far if it's the last character or there is
2396 * a composing character. */
2397 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002398# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 || (cn > 1
2400# ifdef FEAT_XFONTSET
2401 /* No fontset: At least draw char after wide char at
2402 * right position. */
2403 && fontset == NOFONTSET
2404# endif
2405 )
2406# endif
2407 )
2408 {
2409 if (comping || dowide)
2410 thislen = i - start;
2411 else
2412 thislen = i - start + cl;
2413 if (thislen > 0)
2414 {
2415 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2416 draw_flags);
2417 start += thislen;
2418 }
2419 scol += cells;
2420 cells = 0;
2421 if (dowide)
2422 {
2423 gui_mch_set_font(gui.wide_font);
2424 gui_mch_draw_string(gui.row, scol - cn,
2425 s + start, cl, draw_flags);
2426 gui_mch_set_font(font);
2427 start += cl;
2428 }
2429
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002430# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002431 /* No fontset: draw a space to fill the gap after a wide char
2432 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2434# ifdef FEAT_XFONTSET
2435 && fontset == NOFONTSET
2436# endif
2437 && !dowide)
2438 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2439 1, draw_flags);
2440# endif
2441 }
2442 /* Draw a composing char on top of the previous char. */
2443 if (comping)
2444 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002445# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002446 /* Carbon ATSUI autodraws composing char over previous char */
2447 gui_mch_draw_string(gui.row, scol, s + i, cl,
2448 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002449# else
2450 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2451 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 start = i + cl;
2454 }
2455 }
2456 /* The stuff below assumes "len" is the length in screen columns. */
2457 len = scol - col;
2458 }
2459 else
2460# endif
2461 {
2462 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2463# ifdef FEAT_MBYTE
2464 if (enc_dbcs == DBCS_JPNU)
2465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 /* Get the length in display cells, this can be different from the
2467 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002468 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470# endif
2471 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002472#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2475 gui.col = col + len;
2476
2477 /* May need to invert it when it's part of the selection. */
2478 if (flags & GUI_MON_NOCLEAR)
2479 clip_may_redraw_selection(gui.row, col, len);
2480
2481 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2482 {
2483 /* Invalidate the old physical cursor position if we wrote over it */
2484 if (gui.cursor_row == gui.row
2485 && gui.cursor_col >= col
2486 && gui.cursor_col < col + len)
2487 gui.cursor_is_valid = FALSE;
2488 }
2489
2490#ifdef FEAT_SIGN_ICONS
2491 if (draw_sign)
2492 /* Draw the sign on top of the spaces. */
2493 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002494# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002495 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (multi_sign)
2497 netbeans_draw_multisign_indicator(gui.row);
2498# endif
2499#endif
2500
2501 return OK;
2502}
2503
2504/*
2505 * Un-draw the cursor. Actually this just redraws the character at the given
2506 * position. The character just before it too, for when it was in bold.
2507 */
2508 void
2509gui_undraw_cursor()
2510{
2511 if (gui.cursor_is_valid)
2512 {
2513#ifdef FEAT_HANGULIN
2514 if (composing_hangul
2515 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2516 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2517 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2518 gui.norm_pixel, gui.back_pixel, 0);
2519 else
2520 {
2521#endif
2522 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2523 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2524 && gui.cursor_col > 0)
2525 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2526 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2527#ifdef FEAT_HANGULIN
2528 if (composing_hangul)
2529 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2530 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2531 }
2532#endif
2533 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2534 * here in case it wasn't needed to undraw it. */
2535 gui.cursor_is_valid = FALSE;
2536 }
2537}
2538
2539 void
2540gui_redraw(x, y, w, h)
2541 int x;
2542 int y;
2543 int w;
2544 int h;
2545{
2546 int row1, col1, row2, col2;
2547
2548 row1 = Y_2_ROW(y);
2549 col1 = X_2_COL(x);
2550 row2 = Y_2_ROW(y + h - 1);
2551 col2 = X_2_COL(x + w - 1);
2552
2553 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2554
2555 /*
2556 * We may need to redraw the cursor, but don't take it upon us to change
2557 * its location after a scroll.
2558 * (maybe be more strict even and test col too?)
2559 * These things may be outside the update/clipping region and reality may
2560 * not reflect Vims internal ideas if these operations are clipped away.
2561 */
2562 if (gui.row == gui.cursor_row)
2563 gui_update_cursor(TRUE, TRUE);
2564}
2565
2566/*
2567 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2568 * from col1 to col2 (inclusive).
2569 * Return TRUE when the character before the first drawn character has
2570 * different attributes (may have to be redrawn too).
2571 */
2572 int
2573gui_redraw_block(row1, col1, row2, col2, flags)
2574 int row1;
2575 int col1;
2576 int row2;
2577 int col2;
2578 int flags; /* flags for gui_outstr_nowrap() */
2579{
2580 int old_row, old_col;
2581 long_u old_hl_mask;
2582 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002583 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 int idx, len;
2585 int back, nback;
2586 int retval = FALSE;
2587#ifdef FEAT_MBYTE
2588 int orig_col1, orig_col2;
2589#endif
2590
2591 /* Don't try to update when ScreenLines is not valid */
2592 if (!screen_cleared || ScreenLines == NULL)
2593 return retval;
2594
2595 /* Don't try to draw outside the shell! */
2596 /* Check everything, strange values may be caused by a big border width */
2597 col1 = check_col(col1);
2598 col2 = check_col(col2);
2599 row1 = check_row(row1);
2600 row2 = check_row(row2);
2601
2602 /* Remember where our cursor was */
2603 old_row = gui.row;
2604 old_col = gui.col;
2605 old_hl_mask = gui.highlight_mask;
2606#ifdef FEAT_MBYTE
2607 orig_col1 = col1;
2608 orig_col2 = col2;
2609#endif
2610
2611 for (gui.row = row1; gui.row <= row2; gui.row++)
2612 {
2613#ifdef FEAT_MBYTE
2614 /* When only half of a double-wide character is in the block, include
2615 * the other half. */
2616 col1 = orig_col1;
2617 col2 = orig_col2;
2618 off = LineOffset[gui.row];
2619 if (enc_dbcs != 0)
2620 {
2621 if (col1 > 0)
2622 col1 -= dbcs_screen_head_off(ScreenLines + off,
2623 ScreenLines + off + col1);
2624 col2 += dbcs_screen_tail_off(ScreenLines + off,
2625 ScreenLines + off + col2);
2626 }
2627 else if (enc_utf8)
2628 {
2629 if (ScreenLines[off + col1] == 0)
2630 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002631# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2633 ++col2;
2634# endif
2635 }
2636#endif
2637 gui.col = col1;
2638 off = LineOffset[gui.row] + gui.col;
2639 len = col2 - col1 + 1;
2640
2641 /* Find how many chars back this highlighting starts, or where a space
2642 * is. Needed for when the bold trick is used */
2643 for (back = 0; back < col1; ++back)
2644 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2645 || ScreenLines[off - 1 - back] == ' ')
2646 break;
2647 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2648 && ScreenLines[off - 1] != ' ');
2649
2650 /* Break it up in strings of characters with the same attributes. */
2651 /* Print UTF-8 characters individually. */
2652 while (len > 0)
2653 {
2654 first_attr = ScreenAttrs[off];
2655 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002656#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (enc_utf8 && ScreenLinesUC[off] != 0)
2658 {
2659 /* output multi-byte character separately */
2660 nback = gui_screenchar(off, flags,
2661 (guicolor_T)0, (guicolor_T)0, back);
2662 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2663 idx = 2;
2664 else
2665 idx = 1;
2666 }
2667 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2668 {
2669 /* output double-byte, single-width character separately */
2670 nback = gui_screenchar(off, flags,
2671 (guicolor_T)0, (guicolor_T)0, back);
2672 idx = 1;
2673 }
2674 else
2675#endif
2676 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002677#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 for (idx = 0; idx < len; ++idx)
2679 {
2680 if (enc_utf8 && ScreenLines[off + idx] == 0)
2681 continue; /* skip second half of double-width char */
2682 if (ScreenAttrs[off + idx] != first_attr)
2683 break;
2684 }
2685 /* gui_screenstr() takes care of multibyte chars */
2686 nback = gui_screenstr(off, idx, flags,
2687 (guicolor_T)0, (guicolor_T)0, back);
2688#else
2689 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2690 idx++)
2691 {
2692# ifdef FEAT_MBYTE
2693 /* Stop at a multi-byte Unicode character. */
2694 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2695 break;
2696 if (enc_dbcs == DBCS_JPNU)
2697 {
2698 /* Stop at a double-byte single-width char. */
2699 if (ScreenLines[off + idx] == 0x8e)
2700 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002701 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 + off + idx) == 2)
2703 ++idx; /* skip second byte of double-byte char */
2704 }
2705# endif
2706 }
2707 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2708 (guicolor_T)0, (guicolor_T)0, back);
2709#endif
2710 }
2711 if (nback == FAIL)
2712 {
2713 /* Must back up to start drawing where a bold or italic word
2714 * starts. */
2715 off -= back;
2716 len += back;
2717 gui.col -= back;
2718 }
2719 else
2720 {
2721 off += idx;
2722 len -= idx;
2723 }
2724 back = 0;
2725 }
2726 }
2727
2728 /* Put the cursor back where it was */
2729 gui.row = old_row;
2730 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002731 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 return retval;
2734}
2735
2736 static void
2737gui_delete_lines(row, count)
2738 int row;
2739 int count;
2740{
2741 if (count <= 0)
2742 return;
2743
2744 if (row + count > gui.scroll_region_bot)
2745 /* Scrolled out of region, just blank the lines out */
2746 gui_clear_block(row, gui.scroll_region_left,
2747 gui.scroll_region_bot, gui.scroll_region_right);
2748 else
2749 {
2750 gui_mch_delete_lines(row, count);
2751
2752 /* If the cursor was in the deleted lines it's now gone. If the
2753 * cursor was in the scrolled lines adjust its position. */
2754 if (gui.cursor_row >= row
2755 && gui.cursor_col >= gui.scroll_region_left
2756 && gui.cursor_col <= gui.scroll_region_right)
2757 {
2758 if (gui.cursor_row < row + count)
2759 gui.cursor_is_valid = FALSE;
2760 else if (gui.cursor_row <= gui.scroll_region_bot)
2761 gui.cursor_row -= count;
2762 }
2763 }
2764}
2765
2766 static void
2767gui_insert_lines(row, count)
2768 int row;
2769 int count;
2770{
2771 if (count <= 0)
2772 return;
2773
2774 if (row + count > gui.scroll_region_bot)
2775 /* Scrolled out of region, just blank the lines out */
2776 gui_clear_block(row, gui.scroll_region_left,
2777 gui.scroll_region_bot, gui.scroll_region_right);
2778 else
2779 {
2780 gui_mch_insert_lines(row, count);
2781
2782 if (gui.cursor_row >= gui.row
2783 && gui.cursor_col >= gui.scroll_region_left
2784 && gui.cursor_col <= gui.scroll_region_right)
2785 {
2786 if (gui.cursor_row <= gui.scroll_region_bot - count)
2787 gui.cursor_row += count;
2788 else if (gui.cursor_row <= gui.scroll_region_bot)
2789 gui.cursor_is_valid = FALSE;
2790 }
2791 }
2792}
2793
2794/*
2795 * The main GUI input routine. Waits for a character from the keyboard.
2796 * wtime == -1 Wait forever.
2797 * wtime == 0 Don't wait.
2798 * wtime > 0 Wait wtime milliseconds for a character.
2799 * Returns OK if a character was found to be available within the given time,
2800 * or FAIL otherwise.
2801 */
2802 int
2803gui_wait_for_chars(wtime)
2804 long wtime;
2805{
2806 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002808#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 /*
2810 * If we're going to wait a bit, update the menus and mouse shape for the
2811 * current State.
2812 */
2813 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 gui_update_menus(0);
2815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 gui_mch_update();
2818 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 /* Before waiting, flush any output to the screen. */
2824 gui_mch_flush();
2825
2826 if (wtime > 0)
2827 {
2828 /* Blink when waiting for a character. Probably only does something
2829 * for showmatch() */
2830 gui_mch_start_blink();
2831 retval = gui_mch_wait_for_chars(wtime);
2832 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 return retval;
2834 }
2835
2836 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002837 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 */
2839 gui_mch_start_blink();
2840
Bram Moolenaar3918c952005-03-15 22:34:55 +00002841 retval = FAIL;
2842 /*
2843 * We may want to trigger the CursorHold event. First wait for
2844 * 'updatetime' and if nothing is typed within that time put the
2845 * K_CURSORHOLD key in the input buffer.
2846 */
2847 if (gui_mch_wait_for_chars(p_ut) == OK)
2848 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002850 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002852 char_u buf[3];
2853
2854 /* Put K_CURSORHOLD in the input buffer. */
2855 buf[0] = CSI;
2856 buf[1] = KS_EXTRA;
2857 buf[2] = (int)KE_CURSORHOLD;
2858 add_to_input_buf(buf, 3);
2859
2860 retval = OK;
2861 }
2862#endif
2863
2864 if (retval == FAIL)
2865 {
2866 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002867 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 gui_mch_stop_blink();
2872 return retval;
2873}
2874
2875/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002876 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
2878 static void
2879fill_mouse_coord(p, col, row)
2880 char_u *p;
2881 int col;
2882 int row;
2883{
2884 p[0] = (char_u)(col / 128 + ' ' + 1);
2885 p[1] = (char_u)(col % 128 + ' ' + 1);
2886 p[2] = (char_u)(row / 128 + ' ' + 1);
2887 p[3] = (char_u)(row % 128 + ' ' + 1);
2888}
2889
2890/*
2891 * Generic mouse support function. Add a mouse event to the input buffer with
2892 * the given properties.
2893 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2894 * MOUSE_X1, MOUSE_X2
2895 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002896 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2897 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * x, y --- Coordinates of mouse in pixels.
2899 * repeated_click --- TRUE if this click comes only a short time after a
2900 * previous click.
2901 * modifiers --- Bit field which may be any of the following modifiers
2902 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2903 * This function will ignore drag events where the mouse has not moved to a new
2904 * character.
2905 */
2906 void
2907gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2908 int button;
2909 int x;
2910 int y;
2911 int repeated_click;
2912 int_u modifiers;
2913{
2914 static int prev_row = 0, prev_col = 0;
2915 static int prev_button = -1;
2916 static int num_clicks = 1;
2917 char_u string[10];
2918 enum key_extra button_char;
2919 int row, col;
2920#ifdef FEAT_CLIPBOARD
2921 int checkfor;
2922 int did_clip = FALSE;
2923#endif
2924
2925 /*
2926 * Scrolling may happen at any time, also while a selection is present.
2927 */
2928 switch (button)
2929 {
2930 case MOUSE_X1:
2931 button_char = KE_X1MOUSE;
2932 goto button_set;
2933 case MOUSE_X2:
2934 button_char = KE_X2MOUSE;
2935 goto button_set;
2936 case MOUSE_4:
2937 button_char = KE_MOUSEDOWN;
2938 goto button_set;
2939 case MOUSE_5:
2940 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002941 goto button_set;
2942 case MOUSE_6:
2943 button_char = KE_MOUSELEFT;
2944 goto button_set;
2945 case MOUSE_7:
2946 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947button_set:
2948 {
2949 /* Don't put events in the input queue now. */
2950 if (hold_gui_events)
2951 return;
2952
2953 string[3] = CSI;
2954 string[4] = KS_EXTRA;
2955 string[5] = (int)button_char;
2956
2957 /* Pass the pointer coordinates of the scroll event so that we
2958 * know which window to scroll. */
2959 row = gui_xy2colrow(x, y, &col);
2960 string[6] = (char_u)(col / 128 + ' ' + 1);
2961 string[7] = (char_u)(col % 128 + ' ' + 1);
2962 string[8] = (char_u)(row / 128 + ' ' + 1);
2963 string[9] = (char_u)(row % 128 + ' ' + 1);
2964
2965 if (modifiers == 0)
2966 add_to_input_buf(string + 3, 7);
2967 else
2968 {
2969 string[0] = CSI;
2970 string[1] = KS_MODIFIER;
2971 string[2] = 0;
2972 if (modifiers & MOUSE_SHIFT)
2973 string[2] |= MOD_MASK_SHIFT;
2974 if (modifiers & MOUSE_CTRL)
2975 string[2] |= MOD_MASK_CTRL;
2976 if (modifiers & MOUSE_ALT)
2977 string[2] |= MOD_MASK_ALT;
2978 add_to_input_buf(string, 10);
2979 }
2980 return;
2981 }
2982 }
2983
2984#ifdef FEAT_CLIPBOARD
2985 /* If a clipboard selection is in progress, handle it */
2986 if (clip_star.state == SELECT_IN_PROGRESS)
2987 {
2988 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2989 return;
2990 }
2991
2992 /* Determine which mouse settings to look for based on the current mode */
2993 switch (get_real_state())
2994 {
2995 case NORMAL_BUSY:
2996 case OP_PENDING:
2997 case NORMAL: checkfor = MOUSE_NORMAL; break;
2998 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002999 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 case REPLACE:
3001 case REPLACE+LANGMAP:
3002#ifdef FEAT_VREPLACE
3003 case VREPLACE:
3004 case VREPLACE+LANGMAP:
3005#endif
3006 case INSERT:
3007 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3008 case ASKMORE:
3009 case HITRETURN: /* At the more- and hit-enter prompt pass the
3010 mouse event for a click on or below the
3011 message line. */
3012 if (Y_2_ROW(y) >= msg_row)
3013 checkfor = MOUSE_NORMAL;
3014 else
3015 checkfor = MOUSE_RETURN;
3016 break;
3017
3018 /*
3019 * On the command line, use the clipboard selection on all lines
3020 * but the command line. But not when pasting.
3021 */
3022 case CMDLINE:
3023 case CMDLINE+LANGMAP:
3024 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3025 checkfor = MOUSE_NONE;
3026 else
3027 checkfor = MOUSE_COMMAND;
3028 break;
3029
3030 default:
3031 checkfor = MOUSE_NONE;
3032 break;
3033 };
3034
3035 /*
3036 * Allow clipboard selection of text on the command line in "normal"
3037 * modes. Don't do this when dragging the status line, or extending a
3038 * Visual selection.
3039 */
3040 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
3041 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00003042# ifdef FEAT_WINDOWS
3043 + firstwin->w_winrow
3044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 && button != MOUSE_DRAG
3046# ifdef FEAT_MOUSESHAPE
3047 && !drag_status_line
3048# ifdef FEAT_VERTSPLIT
3049 && !drag_sep_line
3050# endif
3051# endif
3052 )
3053 checkfor = MOUSE_NONE;
3054
3055 /*
3056 * Use modeless selection when holding CTRL and SHIFT pressed.
3057 */
3058 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3059 checkfor = MOUSE_NONEF;
3060
3061 /*
3062 * In Ex mode, always use modeless selection.
3063 */
3064 if (exmode_active)
3065 checkfor = MOUSE_NONE;
3066
3067 /*
3068 * If the mouse settings say to not use the mouse, use the modeless
3069 * selection. But if Visual is active, assume that only the Visual area
3070 * will be selected.
3071 * Exception: On the command line, both the selection is used and a mouse
3072 * key is send.
3073 */
3074 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3075 {
3076#ifdef FEAT_VISUAL
3077 /* Don't do modeless selection in Visual mode. */
3078 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3079 return;
3080#endif
3081
3082 /*
3083 * When 'mousemodel' is "popup", shift-left is translated to right.
3084 * But not when also using Ctrl.
3085 */
3086 if (mouse_model_popup() && button == MOUSE_LEFT
3087 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3088 {
3089 button = MOUSE_RIGHT;
3090 modifiers &= ~ MOUSE_SHIFT;
3091 }
3092
3093 /* If the selection is done, allow the right button to extend it.
3094 * If the selection is cleared, allow the right button to start it
3095 * from the cursor position. */
3096 if (button == MOUSE_RIGHT)
3097 {
3098 if (clip_star.state == SELECT_CLEARED)
3099 {
3100 if (State & CMDLINE)
3101 {
3102 col = msg_col;
3103 row = msg_row;
3104 }
3105 else
3106 {
3107 col = curwin->w_wcol;
3108 row = curwin->w_wrow + W_WINROW(curwin);
3109 }
3110 clip_start_selection(col, row, FALSE);
3111 }
3112 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3113 repeated_click);
3114 did_clip = TRUE;
3115 }
3116 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003117 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3120 did_clip = TRUE;
3121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
3123 /* Always allow pasting */
3124 if (button != MOUSE_MIDDLE)
3125 {
3126 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3127 return;
3128 if (checkfor != MOUSE_COMMAND)
3129 button = MOUSE_LEFT;
3130 }
3131 repeated_click = FALSE;
3132 }
3133
3134 if (clip_star.state != SELECT_CLEARED && !did_clip)
3135 clip_clear_selection();
3136#endif
3137
3138 /* Don't put events in the input queue now. */
3139 if (hold_gui_events)
3140 return;
3141
3142 row = gui_xy2colrow(x, y, &col);
3143
3144 /*
3145 * If we are dragging and the mouse hasn't moved far enough to be on a
3146 * different character, then don't send an event to vim.
3147 */
3148 if (button == MOUSE_DRAG)
3149 {
3150 if (row == prev_row && col == prev_col)
3151 return;
3152 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3153 if (y < 0)
3154 row = -1;
3155 }
3156
3157 /*
3158 * If topline has changed (window scrolled) since the last click, reset
3159 * repeated_click, because we don't want starting Visual mode when
3160 * clicking on a different character in the text.
3161 */
3162 if (curwin->w_topline != gui_prev_topline
3163#ifdef FEAT_DIFF
3164 || curwin->w_topfill != gui_prev_topfill
3165#endif
3166 )
3167 repeated_click = FALSE;
3168
3169 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3170 string[1] = KS_MOUSE;
3171 string[2] = KE_FILLER;
3172 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3173 {
3174 if (repeated_click)
3175 {
3176 /*
3177 * Handle multiple clicks. They only count if the mouse is still
3178 * pointing at the same character.
3179 */
3180 if (button != prev_button || row != prev_row || col != prev_col)
3181 num_clicks = 1;
3182 else if (++num_clicks > 4)
3183 num_clicks = 1;
3184 }
3185 else
3186 num_clicks = 1;
3187 prev_button = button;
3188 gui_prev_topline = curwin->w_topline;
3189#ifdef FEAT_DIFF
3190 gui_prev_topfill = curwin->w_topfill;
3191#endif
3192
3193 string[3] = (char_u)(button | 0x20);
3194 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3195 }
3196 else
3197 string[3] = (char_u)button;
3198
3199 string[3] |= modifiers;
3200 fill_mouse_coord(string + 4, col, row);
3201 add_to_input_buf(string, 8);
3202
3203 if (row < 0)
3204 prev_row = 0;
3205 else
3206 prev_row = row;
3207 prev_col = col;
3208
3209 /*
3210 * We need to make sure this is cleared since Athena doesn't tell us when
3211 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3212 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003213#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 gui.dragged_sb = SBAR_NONE;
3215#endif
3216}
3217
3218/*
3219 * Convert x and y coordinate to column and row in text window.
3220 * Corrects for multi-byte character.
3221 * returns column in "*colp" and row as return value;
3222 */
3223 int
3224gui_xy2colrow(x, y, colp)
3225 int x;
3226 int y;
3227 int *colp;
3228{
3229 int col = check_col(X_2_COL(x));
3230 int row = check_row(Y_2_ROW(y));
3231
3232#ifdef FEAT_MBYTE
3233 *colp = mb_fix_col(col, row);
3234#else
3235 *colp = col;
3236#endif
3237 return row;
3238}
3239
3240#if defined(FEAT_MENU) || defined(PROTO)
3241/*
3242 * Callback function for when a menu entry has been selected.
3243 */
3244 void
3245gui_menu_cb(menu)
3246 vimmenu_T *menu;
3247{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003248 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 /* Don't put events in the input queue now. */
3251 if (hold_gui_events)
3252 return;
3253
3254 bytes[0] = CSI;
3255 bytes[1] = KS_MENU;
3256 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003257 add_to_input_buf(bytes, 3);
3258 add_long_to_buf((long_u)menu, bytes);
3259 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261#endif
3262
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003263static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003267 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * in p_go.
3269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 void
3271gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003272 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#ifdef FEAT_MENU
3275 static int prev_menu_is_active = -1;
3276#endif
3277#ifdef FEAT_TOOLBAR
3278 static int prev_toolbar = -1;
3279 int using_toolbar = FALSE;
3280#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003281#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003282 int using_tabline;
3283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#ifdef FEAT_FOOTER
3285 static int prev_footer = -1;
3286 int using_footer = FALSE;
3287#endif
3288#if defined(FEAT_MENU) && !defined(WIN16)
3289 static int prev_tearoff = -1;
3290 int using_tearoff = FALSE;
3291#endif
3292
3293 char_u *p;
3294 int i;
3295#ifdef FEAT_MENU
3296 int grey_old, grey_new;
3297 char_u *temp;
3298#endif
3299 win_T *wp;
3300 int need_set_size;
3301 int fix_size;
3302
3303#ifdef FEAT_MENU
3304 if (oldval != NULL && gui.in_use)
3305 {
3306 /*
3307 * Check if the menu's go from grey to non-grey or vise versa.
3308 */
3309 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3310 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3311 if (grey_old != grey_new)
3312 {
3313 temp = p_go;
3314 p_go = oldval;
3315 gui_update_menus(MENU_ALL_MODES);
3316 p_go = temp;
3317 }
3318 }
3319 gui.menu_is_active = FALSE;
3320#endif
3321
3322 for (i = 0; i < 3; i++)
3323 gui.which_scrollbars[i] = FALSE;
3324 for (p = p_go; *p; p++)
3325 switch (*p)
3326 {
3327 case GO_LEFT:
3328 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3329 break;
3330 case GO_RIGHT:
3331 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3332 break;
3333#ifdef FEAT_VERTSPLIT
3334 case GO_VLEFT:
3335 if (win_hasvertsplit())
3336 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3337 break;
3338 case GO_VRIGHT:
3339 if (win_hasvertsplit())
3340 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3341 break;
3342#endif
3343 case GO_BOT:
3344 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3345 break;
3346#ifdef FEAT_MENU
3347 case GO_MENUS:
3348 gui.menu_is_active = TRUE;
3349 break;
3350#endif
3351 case GO_GREY:
3352 /* make menu's have grey items, ignored here */
3353 break;
3354#ifdef FEAT_TOOLBAR
3355 case GO_TOOLBAR:
3356 using_toolbar = TRUE;
3357 break;
3358#endif
3359#ifdef FEAT_FOOTER
3360 case GO_FOOTER:
3361 using_footer = TRUE;
3362 break;
3363#endif
3364 case GO_TEAROFF:
3365#if defined(FEAT_MENU) && !defined(WIN16)
3366 using_tearoff = TRUE;
3367#endif
3368 break;
3369 default:
3370 /* Ignore options that are not supported */
3371 break;
3372 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (gui.in_use)
3375 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003376 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003378
3379#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003380 /* Update the GUI tab line, it may appear or disappear. This may
3381 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003382 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003383 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003384 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003385 /* We don't want a resize event change "Rows" here, save and
3386 * restore it. Resizing is handled below. */
3387 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003388 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003389 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003390 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003391 if (using_tabline)
3392 fix_size = TRUE;
3393 if (!gui_use_tabline())
3394 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3395 }
3396#endif
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 for (i = 0; i < 3; i++)
3399 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003400 /* The scrollbar needs to be updated when it is shown/unshown and
3401 * when switching tab pages. But the size only changes when it's
3402 * shown/unshown. Thus we need two places to remember whether a
3403 * scrollbar is there or not. */
3404 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003405#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003406 || gui.which_scrollbars[i]
3407 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003408#endif
3409 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 {
3411 if (i == SBAR_BOTTOM)
3412 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3413 gui.which_scrollbars[i]);
3414 else
3415 {
3416 FOR_ALL_WINDOWS(wp)
3417 {
3418 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3419 }
3420 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003421 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3422 {
3423 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003424 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003425 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003426 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003427 if (gui.which_scrollbars[i])
3428 fix_size = TRUE;
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003431#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003432 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003433#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003434 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436
3437#ifdef FEAT_MENU
3438 if (gui.menu_is_active != prev_menu_is_active)
3439 {
3440 /* We don't want a resize event change "Rows" here, save and
3441 * restore it. Resizing is handled below. */
3442 i = Rows;
3443 gui_mch_enable_menu(gui.menu_is_active);
3444 Rows = i;
3445 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003446 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (gui.menu_is_active)
3448 fix_size = TRUE;
3449 }
3450#endif
3451
3452#ifdef FEAT_TOOLBAR
3453 if (using_toolbar != prev_toolbar)
3454 {
3455 gui_mch_show_toolbar(using_toolbar);
3456 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003457 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (using_toolbar)
3459 fix_size = TRUE;
3460 }
3461#endif
3462#ifdef FEAT_FOOTER
3463 if (using_footer != prev_footer)
3464 {
3465 gui_mch_enable_footer(using_footer);
3466 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003467 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (using_footer)
3469 fix_size = TRUE;
3470 }
3471#endif
3472#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3473 if (using_tearoff != prev_tearoff)
3474 {
3475 gui_mch_toggle_tearoffs(using_tearoff);
3476 prev_tearoff = using_tearoff;
3477 }
3478#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003479 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003480 {
3481#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003482 long prev_Columns = Columns;
3483 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 /* Adjust the size of the window to make the text area keep the
3486 * same size and to avoid that part of our window is off-screen
3487 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003488 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003489
3490#ifdef FEAT_GUI_GTK
3491 /* GTK has the annoying habit of sending us resize events when
3492 * changing the window size ourselves. This mostly happens when
3493 * waiting for a character to arrive, quite unpredictably, and may
3494 * change Columns and Rows when we don't want it. Wait for a
3495 * character here to avoid this effect.
3496 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003497 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003498 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003499 * Don't change Rows when adding menu/toolbar/tabline.
3500 * Don't change Columns when adding vertical toolbar. */
3501 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003502 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003503 if ((need_set_size & RESIZE_VERT) == 0)
3504 Rows = prev_Rows;
3505 if ((need_set_size & RESIZE_HOR) == 0)
3506 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003507#endif
3508 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003509#ifdef FEAT_WINDOWS
3510 /* When the console tabline appears or disappears the window positions
3511 * change. */
3512 if (firstwin->w_winrow != tabline_height())
3513 shell_new_rows(); /* recompute window positions and heights */
3514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516}
3517
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003518#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3519/*
3520 * Return TRUE if the GUI is taking care of the tabline.
3521 * It may still be hidden if 'showtabline' is zero.
3522 */
3523 int
3524gui_use_tabline()
3525{
3526 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3527}
3528
3529/*
3530 * Return TRUE if the GUI is showing the tabline.
3531 * This uses 'showtabline'.
3532 */
3533 static int
3534gui_has_tabline()
3535{
3536 if (!gui_use_tabline()
3537 || p_stal == 0
3538 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3539 return FALSE;
3540 return TRUE;
3541}
3542
3543/*
3544 * Update the tabline.
3545 * This may display/undisplay the tabline and update the labels.
3546 */
3547 void
3548gui_update_tabline()
3549{
3550 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003551 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003552
3553 if (!gui.starting && starting == 0)
3554 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003555 /* Updating the tabline uses direct GUI commands, flush
3556 * outstanding instructions first. (esp. clear screen) */
3557 out_flush();
3558 gui_mch_flush();
3559
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003560 if (!showit != !shown)
3561 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003562 if (showit != 0)
3563 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003564
3565 /* When the tabs change from hidden to shown or from shown to
3566 * hidden the size of the text area should remain the same. */
3567 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003568 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569 }
3570}
3571
3572/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003573 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003574 */
3575 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003576get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003577 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003578 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003579{
3580 int modified = FALSE;
3581 char_u buf[40];
3582 int wincount;
3583 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003584 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003585
Bram Moolenaar57657d82006-04-21 22:12:41 +00003586 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003587 opt = (tooltip ? &p_gtt : &p_gtl);
3588 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003589 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003590 int use_sandbox = FALSE;
3591 int save_called_emsg = called_emsg;
3592 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003593 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003594 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3595 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003596
3597 called_emsg = FALSE;
3598
3599 printer_page_num = tabpage_index(tp);
3600# ifdef FEAT_EVAL
3601 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003602 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003603# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003604 /* It's almost as going to the tabpage, but without autocommands. */
3605 curtab->tp_firstwin = firstwin;
3606 curtab->tp_lastwin = lastwin;
3607 curtab->tp_curwin = curwin;
3608 save_curtab = curtab;
3609 curtab = tp;
3610 topframe = curtab->tp_topframe;
3611 firstwin = curtab->tp_firstwin;
3612 lastwin = curtab->tp_lastwin;
3613 curwin = curtab->tp_curwin;
3614 curbuf = curwin->w_buffer;
3615
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003616 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003617 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003618 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003619 STRCPY(NameBuff, res);
3620
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003621 /* Back to the original curtab. */
3622 curtab = save_curtab;
3623 topframe = curtab->tp_topframe;
3624 firstwin = curtab->tp_firstwin;
3625 lastwin = curtab->tp_lastwin;
3626 curwin = curtab->tp_curwin;
3627 curbuf = curwin->w_buffer;
3628
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003629 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003630 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003631 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003632 called_emsg |= save_called_emsg;
3633 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003634
3635 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3636 * use a default label. */
3637 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003638 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003639 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003640 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003641 if (!tooltip)
3642 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003643
3644 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3645 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3646 if (bufIsChanged(wp->w_buffer))
3647 modified = TRUE;
3648 if (modified || wincount > 1)
3649 {
3650 if (wincount > 1)
3651 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3652 else
3653 buf[0] = NUL;
3654 if (modified)
3655 STRCAT(buf, "+");
3656 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003657 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003658 mch_memmove(NameBuff, buf, STRLEN(buf));
3659 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003660 }
3661}
3662
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003663/*
3664 * Send the event for clicking to select tab page "nr".
3665 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003666 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003667 */
3668 int
3669send_tabline_event(nr)
3670 int nr;
3671{
3672 char_u string[3];
3673
3674 if (nr == tabpage_index(curtab))
3675 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003676
3677 /* Don't put events in the input queue now. */
3678 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003679# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003680 || cmdwin_type != 0
3681# endif
3682 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003683 {
3684 /* Set it back to the current tab page. */
3685 gui_mch_set_curtab(tabpage_index(curtab));
3686 return FALSE;
3687 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003688
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003689 string[0] = CSI;
3690 string[1] = KS_TABLINE;
3691 string[2] = KE_FILLER;
3692 add_to_input_buf(string, 3);
3693 string[0] = nr;
3694 add_to_input_buf_csi(string, 1);
3695 return TRUE;
3696}
3697
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003698/*
3699 * Send a tabline menu event
3700 */
3701 void
3702send_tabline_menu_event(tabidx, event)
3703 int tabidx;
3704 int event;
3705{
3706 char_u string[3];
3707
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003708 /* Don't put events in the input queue now. */
3709 if (hold_gui_events)
3710 return;
3711
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003712 string[0] = CSI;
3713 string[1] = KS_TABMENU;
3714 string[2] = KE_FILLER;
3715 add_to_input_buf(string, 3);
3716 string[0] = tabidx;
3717 string[1] = (char_u)(long)event;
3718 add_to_input_buf_csi(string, 2);
3719}
3720
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723/*
3724 * Scrollbar stuff:
3725 */
3726
Bram Moolenaare45828b2006-02-15 22:12:56 +00003727#if defined(FEAT_WINDOWS) || defined(PROTO)
3728/*
3729 * Remove all scrollbars. Used before switching to another tab page.
3730 */
3731 void
3732gui_remove_scrollbars()
3733{
3734 int i;
3735 win_T *wp;
3736
3737 for (i = 0; i < 3; i++)
3738 {
3739 if (i == SBAR_BOTTOM)
3740 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3741 else
3742 {
3743 FOR_ALL_WINDOWS(wp)
3744 {
3745 gui_do_scrollbar(wp, i, FALSE);
3746 }
3747 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003748 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003749 }
3750}
3751#endif
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 void
3754gui_create_scrollbar(sb, type, wp)
3755 scrollbar_T *sb;
3756 int type;
3757 win_T *wp;
3758{
3759 static int sbar_ident = 0;
3760
3761 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3762 sb->wp = wp;
3763 sb->type = type;
3764 sb->value = 0;
3765#ifdef FEAT_GUI_ATHENA
3766 sb->pixval = 0;
3767#endif
3768 sb->size = 1;
3769 sb->max = 1;
3770 sb->top = 0;
3771 sb->height = 0;
3772#ifdef FEAT_VERTSPLIT
3773 sb->width = 0;
3774#endif
3775 sb->status_height = 0;
3776 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3777}
3778
3779/*
3780 * Find the scrollbar with the given index.
3781 */
3782 scrollbar_T *
3783gui_find_scrollbar(ident)
3784 long ident;
3785{
3786 win_T *wp;
3787
3788 if (gui.bottom_sbar.ident == ident)
3789 return &gui.bottom_sbar;
3790 FOR_ALL_WINDOWS(wp)
3791 {
3792 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3793 return &wp->w_scrollbars[SBAR_LEFT];
3794 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3795 return &wp->w_scrollbars[SBAR_RIGHT];
3796 }
3797 return NULL;
3798}
3799
3800/*
3801 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3802 *
3803 * For Win32, Macintosh and GTK+ 2:
3804 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3805 * you stop dragging the scrollbar. We get here each time the scrollbar is
3806 * dragged another pixel, but as far as the rest of vim goes, it thinks
3807 * we're just hanging in the call to DispatchMessage() in
3808 * process_message(). The DispatchMessage() call that hangs was passed a
3809 * mouse button click event in the scrollbar window. -- webb.
3810 *
3811 * Solution: Do the scrolling right here. But only when allowed.
3812 * Ignore the scrollbars while executing an external command or when there
3813 * are still characters to be processed.
3814 */
3815 void
3816gui_drag_scrollbar(sb, value, still_dragging)
3817 scrollbar_T *sb;
3818 long value;
3819 int still_dragging;
3820{
3821#ifdef FEAT_WINDOWS
3822 win_T *wp;
3823#endif
3824 int sb_num;
3825#ifdef USE_ON_FLY_SCROLL
3826 colnr_T old_leftcol = curwin->w_leftcol;
3827# ifdef FEAT_SCROLLBIND
3828 linenr_T old_topline = curwin->w_topline;
3829# endif
3830# ifdef FEAT_DIFF
3831 int old_topfill = curwin->w_topfill;
3832# endif
3833#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003834 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int byte_count;
3836#endif
3837
3838 if (sb == NULL)
3839 return;
3840
3841 /* Don't put events in the input queue now. */
3842 if (hold_gui_events)
3843 return;
3844
3845#ifdef FEAT_CMDWIN
3846 if (cmdwin_type != 0 && sb->wp != curwin)
3847 return;
3848#endif
3849
3850 if (still_dragging)
3851 {
3852 if (sb->wp == NULL)
3853 gui.dragged_sb = SBAR_BOTTOM;
3854 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3855 gui.dragged_sb = SBAR_LEFT;
3856 else
3857 gui.dragged_sb = SBAR_RIGHT;
3858 gui.dragged_wp = sb->wp;
3859 }
3860 else
3861 {
3862 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003863#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 /* Keep the "dragged_wp" value until after the scrolling, for when the
3865 * moust button is released. GTK2 doesn't send the button-up event. */
3866 gui.dragged_wp = NULL;
3867#endif
3868 }
3869
3870 /* Vertical sbar info is kept in the first sbar (the left one) */
3871 if (sb->wp != NULL)
3872 sb = &sb->wp->w_scrollbars[0];
3873
3874 /*
3875 * Check validity of value
3876 */
3877 if (value < 0)
3878 value = 0;
3879#ifdef SCROLL_PAST_END
3880 else if (value > sb->max)
3881 value = sb->max;
3882#else
3883 if (value > sb->max - sb->size + 1)
3884 value = sb->max - sb->size + 1;
3885#endif
3886
3887 sb->value = value;
3888
3889#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003890 /* When not allowed to do the scrolling right now, return.
3891 * This also checked input_available(), but that causes the first click in
3892 * a scrollbar to be ignored when Vim doesn't have focus. */
3893 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return;
3895#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003896#ifdef FEAT_INS_EXPAND
3897 /* Disallow scrolling the current window when the completion popup menu is
3898 * visible. */
3899 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3900 return;
3901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903#ifdef FEAT_RIGHTLEFT
3904 if (sb->wp == NULL && curwin->w_p_rl)
3905 {
3906 value = sb->max + 1 - sb->size - value;
3907 if (value < 0)
3908 value = 0;
3909 }
3910#endif
3911
3912 if (sb->wp != NULL) /* vertical scrollbar */
3913 {
3914 sb_num = 0;
3915#ifdef FEAT_WINDOWS
3916 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3917 sb_num++;
3918 if (wp == NULL)
3919 return;
3920#else
3921 if (sb->wp != curwin)
3922 return;
3923#endif
3924
3925#ifdef USE_ON_FLY_SCROLL
3926 current_scrollbar = sb_num;
3927 scrollbar_value = value;
3928 if (State & NORMAL)
3929 {
3930 gui_do_scroll();
3931 setcursor();
3932 }
3933 else if (State & INSERT)
3934 {
3935 ins_scroll();
3936 setcursor();
3937 }
3938 else if (State & CMDLINE)
3939 {
3940 if (msg_scrolled == 0)
3941 {
3942 gui_do_scroll();
3943 redrawcmdline();
3944 }
3945 }
3946# ifdef FEAT_FOLDING
3947 /* Value may have been changed for closed fold. */
3948 sb->value = sb->wp->w_topline - 1;
3949# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003950
3951 /* When dragging one scrollbar and there is another one at the other
3952 * side move the thumb of that one too. */
3953 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3954 gui_mch_set_scrollbar_thumb(
3955 &sb->wp->w_scrollbars[
3956 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3957 ? SBAR_LEFT : SBAR_RIGHT],
3958 sb->value, sb->size, sb->max);
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#else
3961 bytes[0] = CSI;
3962 bytes[1] = KS_VER_SCROLLBAR;
3963 bytes[2] = KE_FILLER;
3964 bytes[3] = (char_u)sb_num;
3965 byte_count = 4;
3966#endif
3967 }
3968 else
3969 {
3970#ifdef USE_ON_FLY_SCROLL
3971 scrollbar_value = value;
3972
3973 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003974 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 else if (State & INSERT)
3976 ins_horscroll();
3977 else if (State & CMDLINE)
3978 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003979 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003981 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 redrawcmdline();
3983 }
3984 }
3985 if (old_leftcol != curwin->w_leftcol)
3986 {
3987 updateWindow(curwin); /* update window, status and cmdline */
3988 setcursor();
3989 }
3990#else
3991 bytes[0] = CSI;
3992 bytes[1] = KS_HOR_SCROLLBAR;
3993 bytes[2] = KE_FILLER;
3994 byte_count = 3;
3995#endif
3996 }
3997
3998#ifdef USE_ON_FLY_SCROLL
3999# ifdef FEAT_SCROLLBIND
4000 /*
4001 * synchronize other windows, as necessary according to 'scrollbind'
4002 */
4003 if (curwin->w_p_scb
4004 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4005 || (sb->wp == curwin && (curwin->w_topline != old_topline
4006# ifdef FEAT_DIFF
4007 || curwin->w_topfill != old_topfill
4008# endif
4009 ))))
4010 {
4011 do_check_scrollbind(TRUE);
4012 /* need to update the window right here */
4013 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4014 if (wp->w_redr_type > 0)
4015 updateWindow(wp);
4016 setcursor();
4017 }
4018# endif
4019 out_flush();
4020 gui_update_cursor(FALSE, TRUE);
4021#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004022 add_to_input_buf(bytes, byte_count);
4023 add_long_to_buf((long_u)value, bytes);
4024 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#endif
4026}
4027
4028/*
4029 * Scrollbar stuff:
4030 */
4031
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004032#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004033/*
4034 * Called when something in the window layout has changed.
4035 */
4036 void
4037gui_may_update_scrollbars()
4038{
4039 if (gui.in_use && starting == 0)
4040 {
4041 out_flush();
4042 gui_init_which_components(NULL);
4043 gui_update_scrollbars(TRUE);
4044 }
4045 need_mouse_correct = TRUE;
4046}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004047#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 void
4050gui_update_scrollbars(force)
4051 int force; /* Force all scrollbars to get updated */
4052{
4053 win_T *wp;
4054 scrollbar_T *sb;
4055 long val, size, max; /* need 32 bits here */
4056 int which_sb;
4057 int h, y;
4058#ifdef FEAT_VERTSPLIT
4059 static win_T *prev_curwin = NULL;
4060#endif
4061
4062 /* Update the horizontal scrollbar */
4063 gui_update_horiz_scrollbar(force);
4064
4065#ifndef WIN3264
4066 /* Return straight away if there is neither a left nor right scrollbar.
4067 * On MS-Windows this is required anyway for scrollwheel messages. */
4068 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4069 return;
4070#endif
4071
4072 /*
4073 * Don't want to update a scrollbar while we're dragging it. But if we
4074 * have both a left and right scrollbar, and we drag one of them, we still
4075 * need to update the other one.
4076 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004077 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4078 && gui.which_scrollbars[SBAR_LEFT]
4079 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 /*
4082 * If we have two scrollbars and one of them is being dragged, just
4083 * copy the scrollbar position from the dragged one to the other one.
4084 */
4085 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4086 if (gui.dragged_wp != NULL)
4087 gui_mch_set_scrollbar_thumb(
4088 &gui.dragged_wp->w_scrollbars[which_sb],
4089 gui.dragged_wp->w_scrollbars[0].value,
4090 gui.dragged_wp->w_scrollbars[0].size,
4091 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
4094 /* avoid that moving components around generates events */
4095 ++hold_gui_events;
4096
4097 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4098 {
4099 if (wp->w_buffer == NULL) /* just in case */
4100 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004101 /* Skip a scrollbar that is being dragged. */
4102 if (!force && (gui.dragged_sb == SBAR_LEFT
4103 || gui.dragged_sb == SBAR_RIGHT)
4104 && gui.dragged_wp == wp)
4105 continue;
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107#ifdef SCROLL_PAST_END
4108 max = wp->w_buffer->b_ml.ml_line_count - 1;
4109#else
4110 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4111#endif
4112 if (max < 0) /* empty buffer */
4113 max = 0;
4114 val = wp->w_topline - 1;
4115 size = wp->w_height;
4116#ifdef SCROLL_PAST_END
4117 if (val > max) /* just in case */
4118 val = max;
4119#else
4120 if (size > max + 1) /* just in case */
4121 size = max + 1;
4122 if (val > max - size + 1)
4123 val = max - size + 1;
4124#endif
4125 if (val < 0) /* minimal value is 0 */
4126 val = 0;
4127
4128 /*
4129 * Scrollbar at index 0 (the left one) contains all the information.
4130 * It would be the same info for left and right so we just store it for
4131 * one of them.
4132 */
4133 sb = &wp->w_scrollbars[0];
4134
4135 /*
4136 * Note: no check for valid w_botline. If it's not valid the
4137 * scrollbars will be updated later anyway.
4138 */
4139 if (size < 1 || wp->w_botline - 2 > max)
4140 {
4141 /*
4142 * This can happen during changing files. Just don't update the
4143 * scrollbar for now.
4144 */
4145 sb->height = 0; /* Force update next time */
4146 if (gui.which_scrollbars[SBAR_LEFT])
4147 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4148 if (gui.which_scrollbars[SBAR_RIGHT])
4149 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4150 continue;
4151 }
4152 if (force || sb->height != wp->w_height
4153#ifdef FEAT_WINDOWS
4154 || sb->top != wp->w_winrow
4155 || sb->status_height != wp->w_status_height
4156# ifdef FEAT_VERTSPLIT
4157 || sb->width != wp->w_width
4158 || prev_curwin != curwin
4159# endif
4160#endif
4161 )
4162 {
4163 /* Height, width or position of scrollbar has changed. For
4164 * vertical split: curwin changed. */
4165 sb->height = wp->w_height;
4166#ifdef FEAT_WINDOWS
4167 sb->top = wp->w_winrow;
4168 sb->status_height = wp->w_status_height;
4169# ifdef FEAT_VERTSPLIT
4170 sb->width = wp->w_width;
4171# endif
4172#endif
4173
4174 /* Calculate height and position in pixels */
4175 h = (sb->height + sb->status_height) * gui.char_height;
4176 y = sb->top * gui.char_height + gui.border_offset;
4177#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4178 if (gui.menu_is_active)
4179 y += gui.menu_height;
4180#endif
4181
4182#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4183 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4184# ifdef FEAT_GUI_ATHENA
4185 y += gui.toolbar_height;
4186# else
4187# ifdef FEAT_GUI_MSWIN
4188 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4189# endif
4190# endif
4191#endif
4192
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004193#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4194 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004195 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004196#endif
4197
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198#ifdef FEAT_WINDOWS
4199 if (wp->w_winrow == 0)
4200#endif
4201 {
4202 /* Height of top scrollbar includes width of top border */
4203 h += gui.border_offset;
4204 y -= gui.border_offset;
4205 }
4206 if (gui.which_scrollbars[SBAR_LEFT])
4207 {
4208 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4209 gui.left_sbar_x, y,
4210 gui.scrollbar_width, h);
4211 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4212 }
4213 if (gui.which_scrollbars[SBAR_RIGHT])
4214 {
4215 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4216 gui.right_sbar_x, y,
4217 gui.scrollbar_width, h);
4218 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4219 }
4220 }
4221
4222 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4223 * checking if the thumb moved at least a pixel. Only do this for
4224 * Athena, most other GUIs require the update anyway to make the
4225 * arrows work. */
4226#ifdef FEAT_GUI_ATHENA
4227 if (max == 0)
4228 y = 0;
4229 else
4230 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4231 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4232#else
4233 if (force || sb->value != val || sb->size != size || sb->max != max)
4234#endif
4235 {
4236 /* Thumb of scrollbar has moved */
4237 sb->value = val;
4238#ifdef FEAT_GUI_ATHENA
4239 sb->pixval = y;
4240#endif
4241 sb->size = size;
4242 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004243 if (gui.which_scrollbars[SBAR_LEFT]
4244 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4246 val, size, max);
4247 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004248 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4250 val, size, max);
4251 }
4252 }
4253#ifdef FEAT_VERTSPLIT
4254 prev_curwin = curwin;
4255#endif
4256 --hold_gui_events;
4257}
4258
4259/*
4260 * Enable or disable a scrollbar.
4261 * Check for scrollbars for vertically split windows which are not enabled
4262 * sometimes.
4263 */
4264 static void
4265gui_do_scrollbar(wp, which, enable)
4266 win_T *wp;
4267 int which; /* SBAR_LEFT or SBAR_RIGHT */
4268 int enable; /* TRUE to enable scrollbar */
4269{
4270#ifdef FEAT_VERTSPLIT
4271 int midcol = curwin->w_wincol + curwin->w_width / 2;
4272 int has_midcol = (wp->w_wincol <= midcol
4273 && wp->w_wincol + wp->w_width >= midcol);
4274
4275 /* Only enable scrollbars that contain the middle column of the current
4276 * window. */
4277 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4278 {
4279 /* Scrollbars only on one side. Don't enable scrollbars that don't
4280 * contain the middle column of the current window. */
4281 if (!has_midcol)
4282 enable = FALSE;
4283 }
4284 else
4285 {
4286 /* Scrollbars on both sides. Don't enable scrollbars that neither
4287 * contain the middle column of the current window nor are on the far
4288 * side. */
4289 if (midcol > Columns / 2)
4290 {
4291 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4292 enable = FALSE;
4293 }
4294 else
4295 {
4296 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4297 : !has_midcol)
4298 enable = FALSE;
4299 }
4300 }
4301#endif
4302 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4303}
4304
4305/*
4306 * Scroll a window according to the values set in the globals current_scrollbar
4307 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4308 * or FALSE otherwise.
4309 */
4310 int
4311gui_do_scroll()
4312{
4313 win_T *wp, *save_wp;
4314 int i;
4315 long nlines;
4316 pos_T old_cursor;
4317 linenr_T old_topline;
4318#ifdef FEAT_DIFF
4319 int old_topfill;
4320#endif
4321
4322 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4323 if (wp == NULL)
4324 break;
4325 if (wp == NULL)
4326 /* Couldn't find window */
4327 return FALSE;
4328
4329 /*
4330 * Compute number of lines to scroll. If zero, nothing to do.
4331 */
4332 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4333 if (nlines == 0)
4334 return FALSE;
4335
4336 save_wp = curwin;
4337 old_topline = wp->w_topline;
4338#ifdef FEAT_DIFF
4339 old_topfill = wp->w_topfill;
4340#endif
4341 old_cursor = wp->w_cursor;
4342 curwin = wp;
4343 curbuf = wp->w_buffer;
4344 if (nlines < 0)
4345 scrolldown(-nlines, gui.dragged_wp == NULL);
4346 else
4347 scrollup(nlines, gui.dragged_wp == NULL);
4348 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4349 * the mouse-up event already, but we still want it to behave like when
4350 * dragging. But not the next click in an arrow. */
4351 if (gui.dragged_sb == SBAR_NONE)
4352 gui.dragged_wp = NULL;
4353
4354 if (old_topline != wp->w_topline
4355#ifdef FEAT_DIFF
4356 || old_topfill != wp->w_topfill
4357#endif
4358 )
4359 {
4360 if (p_so != 0)
4361 {
4362 cursor_correct(); /* fix window for 'so' */
4363 update_topline(); /* avoid up/down jump */
4364 }
4365 if (old_cursor.lnum != wp->w_cursor.lnum)
4366 coladvance(wp->w_curswant);
4367#ifdef FEAT_SCROLLBIND
4368 wp->w_scbind_pos = wp->w_topline;
4369#endif
4370 }
4371
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004372 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4373 validate_cursor();
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 curwin = save_wp;
4376 curbuf = save_wp->w_buffer;
4377
4378 /*
4379 * Don't call updateWindow() when nothing has changed (it will overwrite
4380 * the status line!).
4381 */
4382 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004383 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#ifdef FEAT_DIFF
4385 || old_topfill != wp->w_topfill
4386#endif
4387 )
4388 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004389 int type = VALID;
4390
4391#ifdef FEAT_INS_EXPAND
4392 if (pum_visible())
4393 {
4394 type = NOT_VALID;
4395 wp->w_lines_valid = 0;
4396 }
4397#endif
4398 /* Don't set must_redraw here, it may cause the popup menu to
4399 * disappear when losing focus after a scrollbar drag. */
4400 if (wp->w_redr_type < type)
4401 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 updateWindow(wp); /* update window, status line, and cmdline */
4403 }
4404
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004405#ifdef FEAT_INS_EXPAND
4406 /* May need to redraw the popup menu. */
4407 if (pum_visible())
4408 pum_redraw();
4409#endif
4410
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4412}
4413
4414
4415/*
4416 * Horizontal scrollbar stuff:
4417 */
4418
4419/*
4420 * Return length of line "lnum" for horizontal scrolling.
4421 */
4422 static colnr_T
4423scroll_line_len(lnum)
4424 linenr_T lnum;
4425{
4426 char_u *p;
4427 colnr_T col;
4428 int w;
4429
4430 p = ml_get(lnum);
4431 col = 0;
4432 if (*p != NUL)
4433 for (;;)
4434 {
4435 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004436 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 if (*p == NUL) /* don't count the last character */
4438 break;
4439 col += w;
4440 }
4441 return col;
4442}
4443
4444/* Remember which line is currently the longest, so that we don't have to
4445 * search for it when scrolling horizontally. */
4446static linenr_T longest_lnum = 0;
4447
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004448/*
4449 * Find longest visible line number. If this is not possible (or not desired,
4450 * by setting 'h' in "guioptions") then the current line number is returned.
4451 */
4452 static linenr_T
4453gui_find_longest_lnum()
4454{
4455 linenr_T ret = 0;
4456
4457 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4458 * line numbers, topline and botline can be invalid when displaying is
4459 * postponed. */
4460 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4461 && curwin->w_topline <= curwin->w_cursor.lnum
4462 && curwin->w_botline > curwin->w_cursor.lnum
4463 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4464 {
4465 linenr_T lnum;
4466 colnr_T n;
4467 long max = 0;
4468
4469 /* Use maximum of all visible lines. Remember the lnum of the
4470 * longest line, closest to the cursor line. Used when scrolling
4471 * below. */
4472 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4473 {
4474 n = scroll_line_len(lnum);
4475 if (n > (colnr_T)max)
4476 {
4477 max = n;
4478 ret = lnum;
4479 }
4480 else if (n == (colnr_T)max
4481 && abs((int)(lnum - curwin->w_cursor.lnum))
4482 < abs((int)(ret - curwin->w_cursor.lnum)))
4483 ret = lnum;
4484 }
4485 }
4486 else
4487 /* Use cursor line only. */
4488 ret = curwin->w_cursor.lnum;
4489
4490 return ret;
4491}
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 static void
4494gui_update_horiz_scrollbar(force)
4495 int force;
4496{
4497 long value, size, max; /* need 32 bit ints here */
4498
4499 if (!gui.which_scrollbars[SBAR_BOTTOM])
4500 return;
4501
4502 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4503 return;
4504
4505 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4506 return;
4507
4508 /*
4509 * It is possible for the cursor to be invalid if we're in the middle of
4510 * something (like changing files). If so, don't do anything for now.
4511 */
4512 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4513 {
4514 gui.bottom_sbar.value = -1;
4515 return;
4516 }
4517
4518 size = W_WIDTH(curwin);
4519 if (curwin->w_p_wrap)
4520 {
4521 value = 0;
4522#ifdef SCROLL_PAST_END
4523 max = 0;
4524#else
4525 max = W_WIDTH(curwin) - 1;
4526#endif
4527 }
4528 else
4529 {
4530 value = curwin->w_leftcol;
4531
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004532 longest_lnum = gui_find_longest_lnum();
4533 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535#ifdef FEAT_VIRTUALEDIT
4536 if (virtual_active())
4537 {
4538 /* May move the cursor even further to the right. */
4539 if (curwin->w_virtcol >= (colnr_T)max)
4540 max = curwin->w_virtcol;
4541 }
4542#endif
4543
4544#ifndef SCROLL_PAST_END
4545 max += W_WIDTH(curwin) - 1;
4546#endif
4547 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004548 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 size -= curwin_col_off();
4550#ifndef SCROLL_PAST_END
4551 max -= curwin_col_off();
4552#endif
4553 }
4554
4555#ifndef SCROLL_PAST_END
4556 if (value > max - size + 1)
4557 value = max - size + 1; /* limit the value to allowable range */
4558#endif
4559
4560#ifdef FEAT_RIGHTLEFT
4561 if (curwin->w_p_rl)
4562 {
4563 value = max + 1 - size - value;
4564 if (value < 0)
4565 {
4566 size += value;
4567 value = 0;
4568 }
4569 }
4570#endif
4571 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4572 && max == gui.bottom_sbar.max)
4573 return;
4574
4575 gui.bottom_sbar.value = value;
4576 gui.bottom_sbar.size = size;
4577 gui.bottom_sbar.max = max;
4578 gui.prev_wrap = curwin->w_p_wrap;
4579
4580 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4581}
4582
4583/*
4584 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4585 */
4586 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004587gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004588 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
4591 /* no wrapping, no scrolling */
4592 if (curwin->w_p_wrap)
4593 return FALSE;
4594
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004595 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 return FALSE;
4597
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004598 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004601 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004603 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004604 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004606 if (compute_longest_lnum)
4607 {
4608 curwin->w_cursor.lnum = gui_find_longest_lnum();
4609 curwin->w_cursor.col = 0;
4610 }
4611 /* Do a sanity check on "longest_lnum", just in case. */
4612 else if (longest_lnum >= curwin->w_topline
4613 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 curwin->w_cursor.lnum = longest_lnum;
4616 curwin->w_cursor.col = 0;
4617 }
4618 }
4619
4620 return leftcol_changed();
4621}
4622
4623/*
4624 * Check that none of the colors are the same as the background color
4625 */
4626 void
4627gui_check_colors()
4628{
4629 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4630 {
4631 gui_set_bg_color((char_u *)"White");
4632 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4633 gui_set_fg_color((char_u *)"Black");
4634 }
4635}
4636
Bram Moolenaar3918c952005-03-15 22:34:55 +00004637 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638gui_set_fg_color(name)
4639 char_u *name;
4640{
4641 gui.norm_pixel = gui_get_color(name);
4642 hl_set_fg_color_name(vim_strsave(name));
4643}
4644
Bram Moolenaar3918c952005-03-15 22:34:55 +00004645 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646gui_set_bg_color(name)
4647 char_u *name;
4648{
4649 gui.back_pixel = gui_get_color(name);
4650 hl_set_bg_color_name(vim_strsave(name));
4651}
4652
4653/*
4654 * Allocate a color by name.
4655 * Returns INVALCOLOR and gives an error message when failed.
4656 */
4657 guicolor_T
4658gui_get_color(name)
4659 char_u *name;
4660{
4661 guicolor_T t;
4662
4663 if (*name == NUL)
4664 return INVALCOLOR;
4665 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004668#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 && gui.in_use
4670#endif
4671 )
4672 EMSG2(_("E254: Cannot allocate color %s"), name);
4673 return t;
4674}
4675
4676/*
4677 * Return the grey value of a color (range 0-255).
4678 */
4679 int
4680gui_get_lightness(pixel)
4681 guicolor_T pixel;
4682{
4683 long_u rgb = gui_mch_get_rgb(pixel);
4684
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004685 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004686 + (((rgb >> 8) & 0xff) * 587)
4687 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688}
4689
4690#if defined(FEAT_GUI_X11) || defined(PROTO)
4691 void
4692gui_new_scrollbar_colors()
4693{
4694 win_T *wp;
4695
4696 /* Nothing to do if GUI hasn't started yet. */
4697 if (!gui.in_use)
4698 return;
4699
4700 FOR_ALL_WINDOWS(wp)
4701 {
4702 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4703 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4704 }
4705 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4706}
4707#endif
4708
4709/*
4710 * Call this when focus has changed.
4711 */
4712 void
4713gui_focus_change(in_focus)
4714 int in_focus;
4715{
4716/*
4717 * Skip this code to avoid drawing the cursor when debugging and switching
4718 * between the debugger window and gvim.
4719 */
4720#if 1
4721 gui.in_focus = in_focus;
4722 out_flush(); /* make sure output has been written */
4723 gui_update_cursor(TRUE, FALSE);
4724
4725# ifdef FEAT_XIM
4726 xim_set_focus(in_focus);
4727# endif
4728
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004729 /* Put events in the input queue only when allowed.
4730 * ui_focus_change() isn't called directly, because it invokes
4731 * autocommands and that must not happen asynchronously. */
4732 if (!hold_gui_events)
4733 {
4734 char_u bytes[3];
4735
4736 bytes[0] = CSI;
4737 bytes[1] = KS_EXTRA;
4738 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4739 add_to_input_buf(bytes, 3);
4740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741#endif
4742}
4743
4744/*
4745 * Called when the mouse moved (but not when dragging).
4746 */
4747 void
4748gui_mouse_moved(x, y)
4749 int x;
4750 int y;
4751{
4752 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004753 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaard3667a22006-03-16 21:35:52 +00004755 /* Ignore this while still starting up. */
4756 if (!gui.in_use || gui.starting)
4757 return;
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#ifdef FEAT_MOUSESHAPE
4760 /* Get window pointer, and update mouse shape as well. */
4761 wp = xy2win(x, y);
4762#endif
4763
4764 /* Only handle this when 'mousefocus' set and ... */
4765 if (p_mousef
4766 && !hold_gui_events /* not holding events */
4767 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4768 && State != HITRETURN /* but not hit-return prompt */
4769 && msg_scrolled == 0 /* no scrolled message */
4770 && !need_mouse_correct /* not moving the pointer */
4771 && gui.in_focus) /* gvim in focus */
4772 {
4773 /* Don't move the mouse when it's left or right of the Vim window */
4774 if (x < 0 || x > Columns * gui.char_width)
4775 return;
4776#ifndef FEAT_MOUSESHAPE
4777 wp = xy2win(x, y);
4778#endif
4779 if (wp == curwin || wp == NULL)
4780 return; /* still in the same old window, or none at all */
4781
Bram Moolenaar9c102382006-05-03 21:26:49 +00004782#ifdef FEAT_WINDOWS
4783 /* Ignore position in the tab pages line. */
4784 if (Y_2_ROW(y) < tabline_height())
4785 return;
4786#endif
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 /*
4789 * format a mouse click on status line input
4790 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004791 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4792 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 */
4794 if (finish_op)
4795 {
4796 /* abort the current operator first */
4797 st[0] = ESC;
4798 add_to_input_buf(st, 1);
4799 }
4800 st[0] = CSI;
4801 st[1] = KS_MOUSE;
4802 st[2] = KE_FILLER;
4803 st[3] = (char_u)MOUSE_LEFT;
4804 fill_mouse_coord(st + 4,
4805#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004806 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#else
4808 -1,
4809#endif
4810 wp->w_height + W_WINROW(wp));
4811
4812 add_to_input_buf(st, 8);
4813 st[3] = (char_u)MOUSE_RELEASE;
4814 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815#ifdef FEAT_GUI_GTK
4816 /* Need to wake up the main loop */
4817 if (gtk_main_level() > 0)
4818 gtk_main_quit();
4819#endif
4820 }
4821}
4822
4823/*
4824 * Called when mouse should be moved to window with focus.
4825 */
4826 void
4827gui_mouse_correct()
4828{
4829 int x, y;
4830 win_T *wp = NULL;
4831
4832 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004833
4834 if (!(gui.in_use && p_mousef))
4835 return;
4836
4837 gui_mch_getmouse(&x, &y);
4838 /* Don't move the mouse when it's left or right of the Vim window */
4839 if (x < 0 || x > Columns * gui.char_width)
4840 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004841 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004842# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004843 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004844# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004845 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004846 wp = xy2win(x, y);
4847 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004849 validate_cline_row();
4850 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4851 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854}
4855
4856/*
4857 * Find window where the mouse pointer "y" coordinate is in.
4858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 static win_T *
4860xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004861 int x UNUSED;
4862 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864#ifdef FEAT_WINDOWS
4865 int row;
4866 int col;
4867 win_T *wp;
4868
4869 row = Y_2_ROW(y);
4870 col = X_2_COL(x);
4871 if (row < 0 || col < 0) /* before first window */
4872 return NULL;
4873 wp = mouse_find_win(&row, &col);
4874# ifdef FEAT_MOUSESHAPE
4875 if (State == HITRETURN || State == ASKMORE)
4876 {
4877 if (Y_2_ROW(y) >= msg_row)
4878 update_mouseshape(SHAPE_IDX_MOREL);
4879 else
4880 update_mouseshape(SHAPE_IDX_MORE);
4881 }
4882 else if (row > wp->w_height) /* below status line */
4883 update_mouseshape(SHAPE_IDX_CLINE);
4884# ifdef FEAT_VERTSPLIT
4885 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004886 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 update_mouseshape(SHAPE_IDX_VSEP);
4888# endif
4889 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004890 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 update_mouseshape(SHAPE_IDX_STATUS);
4892 else
4893 update_mouseshape(-2);
4894# endif
4895 return wp;
4896#else
4897 return firstwin;
4898#endif
4899}
4900
4901/*
4902 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4903 * File names may be given to redefine the args list.
4904 */
4905 void
4906ex_gui(eap)
4907 exarg_T *eap;
4908{
4909 char_u *arg = eap->arg;
4910
4911 /*
4912 * Check for "-f" argument: foreground, don't fork.
4913 * Also don't fork when started with "gvim -f".
4914 * Do fork when using "gui -b".
4915 */
4916 if (arg[0] == '-'
4917 && (arg[1] == 'f' || arg[1] == 'b')
4918 && (arg[2] == NUL || vim_iswhite(arg[2])))
4919 {
4920 gui.dofork = (arg[1] == 'b');
4921 eap->arg = skipwhite(eap->arg + 2);
4922 }
4923 if (!gui.in_use)
4924 {
4925 /* Clear the command. Needed for when forking+exiting, to avoid part
4926 * of the argument ending up after the shell prompt. */
4927 msg_clr_eos_force();
4928 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004929#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004930 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 if (!ends_excmd(*eap->arg))
4934 ex_next(eap);
4935}
4936
4937#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004938 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939/*
4940 * This is shared between Athena, Motif and GTK.
4941 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004942static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944/*
4945 * Callback function for do_in_runtimepath().
4946 */
4947 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004948gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952 char_u *gfp_buffer = cookie;
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (STRLEN(fname) >= MAXPATHL)
4955 *gfp_buffer = NUL;
4956 else
4957 STRCPY(gfp_buffer, fname);
4958}
4959
4960/*
4961 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4962 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4963 */
4964 int
4965gui_find_bitmap(name, buffer, ext)
4966 char_u *name;
4967 char_u *buffer;
4968 char *ext;
4969{
4970 if (STRLEN(name) > MAXPATHL - 14)
4971 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004972 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004973 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4974 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return FAIL;
4976 return OK;
4977}
4978
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004979# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980/*
4981 * Given the name of the "icon=" argument, try finding the bitmap file for the
4982 * icon. If it is an absolute path name, use it as it is. Otherwise append
4983 * "ext" and search for it in 'runtimepath'.
4984 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4985 * contains "name".
4986 */
4987 void
4988gui_find_iconfile(name, buffer, ext)
4989 char_u *name;
4990 char_u *buffer;
4991 char *ext;
4992{
4993 char_u buf[MAXPATHL + 1];
4994
4995 expand_env(name, buffer, MAXPATHL);
4996 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4997 STRCPY(buffer, buf);
4998}
4999# endif
5000#endif
5001
Bram Moolenaar9372a112005-12-06 19:59:18 +00005002#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 void
5004display_errors()
5005{
5006 char_u *p;
5007
5008 if (isatty(2))
5009 fflush(stderr);
5010 else if (error_ga.ga_data != NULL)
5011 {
5012 /* avoid putting up a message box with blanks only */
5013 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5014 if (!isspace(*p))
5015 {
5016 /* Truncate a very long message, it will go off-screen. */
5017 if (STRLEN(p) > 2000)
5018 STRCPY(p + 2000 - 14, "...(truncated)");
5019 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005020 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022 }
5023 ga_clear(&error_ga);
5024 }
5025}
5026#endif
5027
5028#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5029/*
5030 * Return TRUE if still starting up and there is no place to enter text.
5031 * For GTK and X11 we check if stderr is not a tty, which means we were
5032 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5033 * allow typing on stdin.
5034 */
5035 int
5036no_console_input()
5037{
5038 return ((!gui.in_use || gui.starting)
5039# ifndef NO_CONSOLE
5040 && !isatty(0) && !isatty(2)
5041# endif
5042 );
5043}
5044#endif
5045
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005046#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005047 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005048 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049/*
5050 * Update the current window and the screen.
5051 */
5052 void
5053gui_update_screen()
5054{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005055#ifdef FEAT_CONCEAL
5056 linenr_T conceal_old_cursor_line = 0;
5057 linenr_T conceal_new_cursor_line = 0;
5058 int conceal_update_lines = FALSE;
5059#endif
5060
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 update_topline();
5062 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005063
5064#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005065 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005066 if (!finish_op && (
5067# ifdef FEAT_AUTOCMD
5068 has_cursormoved()
5069# endif
5070# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5071 ||
5072# endif
5073# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005074 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005075# endif
5076 )
5077 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005078 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005079# ifdef FEAT_AUTOCMD
5080 if (has_cursormoved())
5081 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5082# endif
5083# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005084 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005085 {
5086 conceal_old_cursor_line = last_cursormoved.lnum;
5087 conceal_new_cursor_line = curwin->w_cursor.lnum;
5088 conceal_update_lines = TRUE;
5089 }
5090# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005091 last_cursormoved = curwin->w_cursor;
5092 }
5093#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 update_screen(0); /* may need to update the screen */
5096 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005097# if defined(FEAT_CONCEAL)
5098 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005099 && (conceal_old_cursor_line != conceal_new_cursor_line
5100 || conceal_cursor_line(curwin)
5101 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005102 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005103 if (conceal_old_cursor_line != conceal_new_cursor_line)
5104 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005105 update_single_line(curwin, conceal_new_cursor_line);
5106 curwin->w_valid &= ~VALID_CROW;
5107 }
5108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 out_flush(); /* make sure output has been written */
5110 gui_update_cursor(TRUE, FALSE);
5111 gui_mch_flush();
5112}
5113#endif
5114
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005115#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5117
5118/*
5119 * Get the text to use in a find/replace dialog. Uses the last search pattern
5120 * if the argument is empty.
5121 * Returns an allocated string.
5122 */
5123 char_u *
5124get_find_dialog_text(arg, wwordp, mcasep)
5125 char_u *arg;
5126 int *wwordp; /* return: TRUE if \< \> found */
5127 int *mcasep; /* return: TRUE if \C found */
5128{
5129 char_u *text;
5130
5131 if (*arg == NUL)
5132 text = last_search_pat();
5133 else
5134 text = arg;
5135 if (text != NULL)
5136 {
5137 text = vim_strsave(text);
5138 if (text != NULL)
5139 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005140 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 int i;
5142
5143 /* Remove "\V" */
5144 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5145 {
5146 mch_memmove(text, text + 2, (size_t)(len - 1));
5147 len -= 2;
5148 }
5149
5150 /* Recognize "\c" and "\C" and remove. */
5151 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5152 {
5153 *mcasep = (text[1] == 'C');
5154 mch_memmove(text, text + 2, (size_t)(len - 1));
5155 len -= 2;
5156 }
5157
5158 /* Recognize "\<text\>" and remove. */
5159 if (len >= 4
5160 && STRNCMP(text, "\\<", 2) == 0
5161 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5162 {
5163 *wwordp = TRUE;
5164 mch_memmove(text, text + 2, (size_t)(len - 4));
5165 text[len - 4] = NUL;
5166 }
5167
5168 /* Recognize "\/" or "\?" and remove. */
5169 for (i = 0; i + 1 < len; ++i)
5170 if (text[i] == '\\' && (text[i + 1] == '/'
5171 || text[i + 1] == '?'))
5172 {
5173 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5174 --len;
5175 }
5176 }
5177 }
5178 return text;
5179}
5180
5181/*
5182 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5183 */
5184 static void
5185concat_esc(gap, text, what)
5186 garray_T *gap;
5187 char_u *text;
5188 int what;
5189{
5190 while (*text != NUL)
5191 {
5192#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005193 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (l > 1)
5196 {
5197 while (--l >= 0)
5198 ga_append(gap, *text++);
5199 continue;
5200 }
5201#endif
5202 if (*text == what)
5203 ga_append(gap, '\\');
5204 ga_append(gap, *text);
5205 ++text;
5206 }
5207}
5208
5209/*
5210 * Handle the press of a button in the find-replace dialog.
5211 * Return TRUE when something was added to the input buffer.
5212 */
5213 int
5214gui_do_findrepl(flags, find_text, repl_text, down)
5215 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5216 char_u *find_text;
5217 char_u *repl_text;
5218 int down; /* Search downwards. */
5219{
5220 garray_T ga;
5221 int i;
5222 int type = (flags & FRD_TYPE_MASK);
5223 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005224 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005225 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005226 static int busy = FALSE;
5227
5228 /* When the screen is being updated we should not change buffers and
5229 * windows structures, it may cause freed memory to be used. Also don't
5230 * do this recursively (pressing "Find" quickly several times. */
5231 if (updating_screen || busy)
5232 return FALSE;
5233
5234 /* refuse replace when text cannot be changed */
5235 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5236 return FALSE;
5237
5238 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005241 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 ga_concat(&ga, (char_u *)"%s/");
5243
5244 ga_concat(&ga, (char_u *)"\\V");
5245 if (flags & FRD_MATCH_CASE)
5246 ga_concat(&ga, (char_u *)"\\C");
5247 else
5248 ga_concat(&ga, (char_u *)"\\c");
5249 if (flags & FRD_WHOLE_WORD)
5250 ga_concat(&ga, (char_u *)"\\<");
5251 if (type == FRD_REPLACEALL || down)
5252 concat_esc(&ga, find_text, '/'); /* escape slashes */
5253 else
5254 concat_esc(&ga, find_text, '?'); /* escape '?' */
5255 if (flags & FRD_WHOLE_WORD)
5256 ga_concat(&ga, (char_u *)"\\>");
5257
5258 if (type == FRD_REPLACEALL)
5259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005261 /* escape / and \ */
5262 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5263 if (p != NULL)
5264 ga_concat(&ga, p);
5265 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005267 }
5268 ga_append(&ga, NUL);
5269
5270 if (type == FRD_REPLACE)
5271 {
5272 /* Do the replacement when the text at the cursor matches. Thus no
5273 * replacement is done if the cursor was moved! */
5274 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5275 regmatch.rm_ic = 0;
5276 if (regmatch.regprog != NULL)
5277 {
5278 p = ml_get_cursor();
5279 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5280 && regmatch.startp[0] == p)
5281 {
5282 /* Clear the command line to remove any old "No match"
5283 * error. */
5284 msg_end_prompt();
5285
5286 if (u_save_cursor() == OK)
5287 {
5288 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005289 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005290
5291 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005292 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005293 ins_str(repl_text);
5294 }
5295 }
5296 else
5297 MSG(_("No match at cursor, finding next"));
5298 vim_free(regmatch.regprog);
5299 }
5300 }
5301
5302 if (type == FRD_REPLACEALL)
5303 {
5304 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005305 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 do_cmdline_cmd(ga.ga_data);
5307 }
5308 else
5309 {
5310 /* Search for the next match. */
5311 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005313 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 msg_scroll = i; /* don't let an error message set msg_scroll */
5315 }
5316
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005317 /* Don't want to pass did_emsg to other code, it may cause disabling
5318 * syntax HL if we were busy redrawing. */
5319 did_emsg = save_did_emsg;
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (State & (NORMAL | INSERT))
5322 {
5323 gui_update_screen(); /* update the screen */
5324 msg_didout = 0; /* overwrite any message */
5325 need_wait_return = FALSE; /* don't wait for return */
5326 }
5327
5328 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005329 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 return (ga.ga_len > 0);
5331}
5332
5333#endif
5334
5335#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5336 || defined(FEAT_GUI_MSWIN) \
5337 || defined(FEAT_GUI_MAC) \
5338 || defined(PROTO)
5339
5340#ifdef FEAT_WINDOWS
5341static void gui_wingoto_xy __ARGS((int x, int y));
5342
5343/*
5344 * Jump to the window at specified point (x, y).
5345 */
5346 static void
5347gui_wingoto_xy(x, y)
5348 int x;
5349 int y;
5350{
5351 int row = Y_2_ROW(y);
5352 int col = X_2_COL(x);
5353 win_T *wp;
5354
5355 if (row >= 0 && col >= 0)
5356 {
5357 wp = mouse_find_win(&row, &col);
5358 if (wp != NULL && wp != curwin)
5359 win_goto(wp);
5360 }
5361}
5362#endif
5363
5364/*
5365 * Process file drop. Mouse cursor position, key modifiers, name of files
5366 * and count of files are given. Argument "fnames[count]" has full pathnames
5367 * of dropped files, they will be freed in this function, and caller can't use
5368 * fnames after call this function.
5369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 void
5371gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005372 int x UNUSED;
5373 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 int_u modifiers;
5375 char_u **fnames;
5376 int count;
5377{
5378 int i;
5379 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005380 static int entered = FALSE;
5381
5382 /*
5383 * This function is called by event handlers. Just in case we get a
5384 * second event before the first one is handled, ignore the second one.
5385 * Not sure if this can ever happen, just in case.
5386 */
5387 if (entered)
5388 return;
5389 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 /*
5392 * When the cursor is at the command line, add the file names to the
5393 * command line, don't edit the files.
5394 */
5395 if (State & CMDLINE)
5396 {
5397 shorten_filenames(fnames, count);
5398 for (i = 0; i < count; ++i)
5399 {
5400 if (fnames[i] != NULL)
5401 {
5402 if (i > 0)
5403 add_to_input_buf((char_u*)" ", 1);
5404
5405 /* We don't know what command is used thus we can't be sure
5406 * about which characters need to be escaped. Only escape the
5407 * most common ones. */
5408# ifdef BACKSLASH_IN_FILENAME
5409 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5410# else
5411 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5412# endif
5413 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005414 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 vim_free(p);
5416 vim_free(fnames[i]);
5417 }
5418 }
5419 vim_free(fnames);
5420 }
5421 else
5422 {
5423 /* Go to the window under mouse cursor, then shorten given "fnames" by
5424 * current window, because a window can have local current dir. */
5425# ifdef FEAT_WINDOWS
5426 gui_wingoto_xy(x, y);
5427# endif
5428 shorten_filenames(fnames, count);
5429
5430 /* If Shift held down, remember the first item. */
5431 if ((modifiers & MOUSE_SHIFT) != 0)
5432 p = vim_strsave(fnames[0]);
5433 else
5434 p = NULL;
5435
5436 /* Handle the drop, :edit or :split to get to the file. This also
5437 * frees fnames[]. Skip this if there is only one item it's a
5438 * directory and Shift is held down. */
5439 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5440 && mch_isdir(fnames[0]))
5441 {
5442 vim_free(fnames[0]);
5443 vim_free(fnames);
5444 }
5445 else
5446 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5447
5448 /* If Shift held down, change to first file's directory. If the first
5449 * item is a directory, change to that directory (and let the explorer
5450 * plugin show the contents). */
5451 if (p != NULL)
5452 {
5453 if (mch_isdir(p))
5454 {
5455 if (mch_chdir((char *)p) == 0)
5456 shorten_fnames(TRUE);
5457 }
5458 else if (vim_chdirfile(p) == OK)
5459 shorten_fnames(TRUE);
5460 vim_free(p);
5461 }
5462
5463 /* Update the screen display */
5464 update_screen(NOT_VALID);
5465# ifdef FEAT_MENU
5466 gui_update_menus(0);
5467# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005468#ifdef FEAT_TITLE
5469 maketitle();
5470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 setcursor();
5472 out_flush();
5473 gui_update_cursor(FALSE, FALSE);
5474 gui_mch_flush();
5475 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005476
5477 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478}
5479#endif