blob: ed80844f45f9ed50f0bf3d65d5c26071da7c8b04 [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 {
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200105 /* If there is 'f' in 'guioptions' and specify -g argument,
106 * gui_mch_init_check() was not called yet. */
107 if (gui_mch_init_check() != OK)
108 exit(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200109 gui_attempt_start();
110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112 if (!gui.in_use) /* failed to start GUI */
113 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200114 /* Back to old term settings
115 *
116 * FIXME: If we got here because a child process failed and flagged to
117 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
118 * hit an X11 I/O error and do a longjmp(), leaving recursive
119 * permanently set to 1. This is probably not as big a problem as it
120 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
121 * return "OK" unconditionally, so it would be very difficult to
122 * actually hit this case.
123 */
124 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 settmode(TMODE_RAW); /* restart RAW mode */
126#ifdef FEAT_TITLE
127 set_title_defaults(); /* set 'title' and 'icon' again */
128#endif
129 }
130
131 vim_free(old_term);
132
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200133#ifdef FEAT_AUTOCMD
134 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
135 * the GUIFailed event. */
136 gui_mch_update();
137 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
138 NULL, NULL, FALSE, curbuf);
139#endif
140 --recursive;
141}
142
143/*
144 * Set_termname() will call gui_init() to start the GUI.
145 * Set the "starting" flag, to indicate that the GUI will start.
146 *
147 * We don't want to open the GUI shell until after we've read .gvimrc,
148 * otherwise we don't know what font we will use, and hence we don't know
149 * what size the shell should be. So if there are errors in the .gvimrc
150 * file, they will have to go to the terminal: Set full_screen to FALSE.
151 * full_screen will be set to TRUE again by a successful termcapinit().
152 */
153 static void
154gui_attempt_start()
155{
156 static int recursive = 0;
157
158 ++recursive;
159 gui.starting = TRUE;
160
161#ifdef FEAT_GUI_GTK
162 gui.event_time = GDK_CURRENT_TIME;
163#endif
164
165 termcapinit((char_u *)"builtin_gui");
166 gui.starting = recursive - 1;
167
Bram Moolenaar9372a112005-12-06 19:59:18 +0000168#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {
171# ifdef FEAT_EVAL
172 Window x11_window;
173 Display *x11_display;
174
175 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
176 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
177# endif
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 /* Display error messages in a dialog now. */
180 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 --recursive;
184}
185
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200186#ifdef MAY_FORK
187
188/* for waitpid() */
189# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
190# include <sys/wait.h>
191# endif
192
193/*
194 * Create a new process, by forking. In the child, start the GUI, and in
195 * the parent, exit.
196 *
197 * If something goes wrong, this will return with gui.in_use still set
198 * to FALSE, in which case the caller should continue execution without
199 * the GUI.
200 *
201 * If the child fails to start the GUI, then the child will exit and the
202 * parent will return. If the child succeeds, then the parent will exit
203 * and the child will return.
204 */
205 static void
206gui_do_fork()
207{
208#ifdef __QNXNTO__
209 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
210 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
211 gui_attempt_start();
212 return;
213#else
214 int pipefd[2]; /* pipe between parent and child */
215 int pipe_error;
216 int status;
217 int exit_status;
218 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200219
220 /* Setup a pipe between the child and the parent, so that the parent
221 * knows when the child has done the setsid() call and is allowed to
222 * exit. */
223 pipe_error = (pipe(pipefd) < 0);
224 pid = fork();
225 if (pid < 0) /* Fork error */
226 {
227 EMSG(_("E851: Failed to create a new process for the GUI"));
228 return;
229 }
230 else if (pid > 0) /* Parent */
231 {
232 /* Give the child some time to do the setsid(), otherwise the
233 * exit() may kill the child too (when starting gvim from inside a
234 * gvim). */
235 if (!pipe_error)
236 {
237 /* The read returns when the child closes the pipe (or when
238 * the child dies for some reason). */
239 close(pipefd[1]);
240 status = gui_read_child_pipe(pipefd[0]);
241 if (status == GUI_CHILD_FAILED)
242 {
243 /* The child failed to start the GUI, so the caller must
244 * continue. There may be more error information written
245 * to stderr by the child. */
246# ifdef __NeXT__
247 wait4(pid, &exit_status, 0, (struct rusage *)0);
248# else
249 waitpid(pid, &exit_status, 0);
250# endif
251 EMSG(_("E852: The child process failed to start the GUI"));
252 return;
253 }
254 else if (status == GUI_CHILD_IO_ERROR)
255 {
256 pipe_error = TRUE;
257 }
258 /* else GUI_CHILD_OK: parent exit */
259 }
260
261 if (pipe_error)
262 ui_delay(300L, TRUE);
263
264 /* When swapping screens we may need to go to the next line, e.g.,
265 * after a hit-enter prompt and using ":gui". */
266 if (newline_on_exit)
267 mch_errmsg("\r\n");
268
269 /*
270 * The parent must skip the normal exit() processing, the child
271 * will do it. For example, GTK messes up signals when exiting.
272 */
273 _exit(0);
274 }
275 /* Child */
276
Bram Moolenaar29695702012-05-18 17:03:18 +0200277#ifdef FEAT_GUI_GTK
278 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
279 if (gui_mch_init_check() != OK)
280 exit(1);
281#endif
282
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200283# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
284 /*
285 * Change our process group. On some systems/shells a CTRL-C in the
286 * shell where Vim was started would otherwise kill gvim!
287 */
288# if defined(HAVE_SETSID)
289 (void)setsid();
290# else
291 (void)setpgid(0, 0);
292# endif
293# endif
294 if (!pipe_error)
295 close(pipefd[0]);
296
297# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
298 /* Tell the session manager our new PID */
299 gui_mch_forked();
300# endif
301
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200302 /* Try to start the GUI */
303 gui_attempt_start();
304
305 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200306 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200307 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200308 if (gui.in_use)
309 write_eintr(pipefd[1], "ok", 3);
310 else
311 write_eintr(pipefd[1], "fail", 5);
312 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200313 }
314
315 /* If we failed to start the GUI, exit now. */
316 if (!gui.in_use)
317 exit(1);
318#endif
319}
320
321/*
322 * Read from a pipe assumed to be connected to the child process (this
323 * function is called from the parent).
324 * Return GUI_CHILD_OK if the child successfully started the GUI,
325 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
326 * some other error.
327 *
328 * The file descriptor will be closed before the function returns.
329 */
330 static int
331gui_read_child_pipe(int fd)
332{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200333 long bytes_read;
334#define READ_BUFFER_SIZE 10
335 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200336
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200337 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
338#undef READ_BUFFER_SIZE
339 close(fd);
340 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200341 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200342 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200343 if (strcmp(buffer, "ok") == 0)
344 return GUI_CHILD_OK;
345 return GUI_CHILD_FAILED;
346}
347
348#endif /* MAY_FORK */
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350/*
351 * Call this when vim starts up, whether or not the GUI is started
352 */
353 void
354gui_prepare(argc, argv)
355 int *argc;
356 char **argv;
357{
358 gui.in_use = FALSE; /* No GUI yet (maybe later) */
359 gui.starting = FALSE; /* No GUI yet (maybe later) */
360 gui_mch_prepare(argc, argv);
361}
362
363/*
364 * Try initializing the GUI and check if it can be started.
365 * Used from main() to check early if "vim -g" can start the GUI.
366 * Used from gui_init() to prepare for starting the GUI.
367 * Returns FAIL or OK.
368 */
369 int
370gui_init_check()
371{
372 static int result = MAYBE;
373
374 if (result != MAYBE)
375 {
376 if (result == FAIL)
377 EMSG(_("E229: Cannot start the GUI"));
378 return result;
379 }
380
381 gui.shell_created = FALSE;
382 gui.dying = FALSE;
383 gui.in_focus = TRUE; /* so the guicursor setting works */
384 gui.dragged_sb = SBAR_NONE;
385 gui.dragged_wp = NULL;
386 gui.pointer_hidden = FALSE;
387 gui.col = 0;
388 gui.row = 0;
389 gui.num_cols = Columns;
390 gui.num_rows = Rows;
391
392 gui.cursor_is_valid = FALSE;
393 gui.scroll_region_top = 0;
394 gui.scroll_region_bot = Rows - 1;
395 gui.scroll_region_left = 0;
396 gui.scroll_region_right = Columns - 1;
397 gui.highlight_mask = HL_NORMAL;
398 gui.char_width = 1;
399 gui.char_height = 1;
400 gui.char_ascent = 0;
401 gui.border_width = 0;
402
403 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200404#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 gui.bold_font = NOFONT;
406 gui.ital_font = NOFONT;
407 gui.boldital_font = NOFONT;
408# ifdef FEAT_XFONTSET
409 gui.fontset = NOFONTSET;
410# endif
411#endif
412
413#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200414# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415# ifdef FONTSET_ALWAYS
416 gui.menu_fontset = NOFONTSET;
417# else
418 gui.menu_font = NOFONT;
419# endif
420# endif
421 gui.menu_is_active = TRUE; /* default: include menu */
422# ifndef FEAT_GUI_GTK
423 gui.menu_height = MENU_DEFAULT_HEIGHT;
424 gui.menu_width = 0;
425# endif
426#endif
427#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
428 gui.toolbar_height = 0;
429#endif
430#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
431 gui.footer_height = 0;
432#endif
433#ifdef FEAT_BEVAL_TIP
434 gui.tooltip_fontset = NOFONTSET;
435#endif
436
437 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
438 gui.prev_wrap = -1;
439
440#ifdef ALWAYS_USE_GUI
441 result = OK;
442#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200443# ifdef FEAT_GUI_GTK
444 /*
445 * Note: Don't call gtk_init_check() before fork, it will be called after
446 * the fork. When calling it before fork, it make vim hang for a while.
447 * See gui_do_fork().
448 * Use a simpler check if the GUI window can probably be opened.
449 */
450 result = gui.dofork ? gui_mch_early_init_check() : gui_mch_init_check();
451# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#endif
455 return result;
456}
457
458/*
459 * This is the call which starts the GUI.
460 */
461 void
462gui_init()
463{
464 win_T *wp;
465 static int recursive = 0;
466
467 /*
468 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
469 * function will then be executed at the first call, the rest by the
470 * recursive call. This allow the shell to be opened halfway reading a
471 * gvimrc file.
472 */
473 if (!recursive)
474 {
475 ++recursive;
476
477 clip_init(TRUE);
478
479 /* If can't initialize, don't try doing the rest */
480 if (gui_init_check() == FAIL)
481 {
482 --recursive;
483 clip_init(FALSE);
484 return;
485 }
486
487 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000488 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
489 * breaks the Paste toolbar button.
490 */
491 set_option_value((char_u *)"paste", 0L, NULL, 0);
492
493 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 * Set up system-wide default menus.
495 */
496#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
497 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
498 {
499 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000500 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 sys_menu = FALSE;
502 }
503#endif
504
505 /*
506 * Switch on the mouse by default, unless the user changed it already.
507 * This can then be changed in the .gvimrc.
508 */
509 if (!option_was_set((char_u *)"mouse"))
510 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000511 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
513 /*
514 * If -U option given, use only the initializations from that file and
515 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
516 */
517 if (use_gvimrc != NULL)
518 {
519 if (STRCMP(use_gvimrc, "NONE") != 0
520 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000521 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
523 }
524 else
525 {
526 /*
527 * Get system wide defaults for gvim, only when file name defined.
528 */
529#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000530 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531#endif
532
533 /*
534 * Try to read GUI initialization commands from the following
535 * places:
536 * - environment variable GVIMINIT
537 * - the user gvimrc file (~/.gvimrc)
538 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
539 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
540 * The first that exists is used, the rest is ignored.
541 */
542 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000543 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
544 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000546 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
547 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548#endif
549 )
550 {
551#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000552 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#endif
554 }
555
556 /*
557 * Read initialization commands from ".gvimrc" in current
558 * directory. This is only done if the 'exrc' option is set.
559 * Because of security reasons we disallow shell and write
560 * commands now, except for unix if the file is owned by the user
561 * or 'secure' option has been reset in environment of global
562 * ".gvimrc".
563 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
564 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
565 */
566 if (p_exrc)
567 {
568#ifdef UNIX
569 {
570 struct stat s;
571
572 /* if ".gvimrc" file is not owned by user, set 'secure'
573 * mode */
574 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
575 secure = p_secure;
576 }
577#else
578 secure = p_secure;
579#endif
580
581 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
582 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
583#ifdef SYS_GVIMRC_FILE
584 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
585 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
586#endif
587#ifdef USR_GVIMRC_FILE2
588 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
589 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
590#endif
591#ifdef USR_GVIMRC_FILE3
592 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
593 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
594#endif
595 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000596 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598 if (secure == 2)
599 need_wait_return = TRUE;
600 secure = 0;
601 }
602 }
603
604 if (need_wait_return || msg_didany)
605 wait_return(TRUE);
606
607 --recursive;
608 }
609
610 /* If recursive call opened the shell, return here from the first call */
611 if (gui.in_use)
612 return;
613
614 /*
615 * Create the GUI shell.
616 */
617 gui.in_use = TRUE; /* Must be set after menus have been set up */
618 if (gui_mch_init() == FAIL)
619 goto error;
620
621 /* Avoid a delay for an error message that was printed in the terminal
622 * where Vim was started. */
623 emsg_on_display = FALSE;
624 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 need_wait_return = FALSE;
627 msg_didany = FALSE;
628
629 /*
630 * Check validity of any generic resources that may have been loaded.
631 */
632 if (gui.border_width < 0)
633 gui.border_width = 0;
634
635 /*
636 * Set up the fonts. First use a font specified with "-fn" or "-font".
637 */
638 if (font_argument != NULL)
639 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
640 if (
641#ifdef FEAT_XFONTSET
642 (*p_guifontset == NUL
643 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
644#endif
645 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
646 : p_guifont, FALSE) == FAIL)
647 {
648 EMSG(_("E665: Cannot start GUI, no valid font found"));
649 goto error2;
650 }
651#ifdef FEAT_MBYTE
652 if (gui_get_wide_font() == FAIL)
653 EMSG(_("E231: 'guifontwide' invalid"));
654#endif
655
656 gui.num_cols = Columns;
657 gui.num_rows = Rows;
658 gui_reset_scroll_region();
659
660 /* Create initial scrollbars */
661 FOR_ALL_WINDOWS(wp)
662 {
663 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
664 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
665 }
666 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
667
668#ifdef FEAT_MENU
669 gui_create_initial_menus(root_menu);
670#endif
671#ifdef FEAT_SUN_WORKSHOP
672 if (usingSunWorkShop)
673 workshop_init();
674#endif
675#ifdef FEAT_SIGN_ICONS
676 sign_gui_started();
677#endif
678
679 /* Configure the desired menu and scrollbars */
680 gui_init_which_components(NULL);
681
682 /* All components of the GUI have been created now */
683 gui.shell_created = TRUE;
684
685#ifndef FEAT_GUI_GTK
686 /* Set the shell size, adjusted for the screen size. For GTK this only
687 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000688 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#endif
690#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
691 /* Need to set the size of the menubar after all the menus have been
692 * created. */
693 gui_mch_compute_menu_height((Widget)0);
694#endif
695
696 /*
697 * Actually open the GUI shell.
698 */
699 if (gui_mch_open() != FAIL)
700 {
701#ifdef FEAT_TITLE
702 maketitle();
703 resettitle();
704#endif
705 init_gui_options();
706#ifdef FEAT_ARABIC
707 /* Our GUI can't do bidi. */
708 p_tbidi = FALSE;
709#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000710#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 /* Give GTK+ a chance to put all widget's into place. */
712 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000713
714# ifdef FEAT_MENU
715 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
716 * It was still there to make F10 work. */
717 if (vim_strchr(p_go, GO_MENUS) == NULL)
718 {
719 --gui.starting;
720 gui_mch_enable_menu(FALSE);
721 ++gui.starting;
722 gui_mch_update();
723 }
724# endif
725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000727 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000729 /* When 'lines' was set while starting up the topframe may have to be
730 * resized. */
731 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000732
733#ifdef FEAT_BEVAL
734 /* Always create the Balloon Evaluation area, but disable it when
735 * 'ballooneval' is off */
736# ifdef FEAT_GUI_GTK
737 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
738 &general_beval_cb, NULL);
739# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000740# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000741 {
742 extern Widget textArea;
743 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000744 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000745 }
746# else
747# ifdef FEAT_GUI_W32
748 balloonEval = gui_mch_create_beval_area(NULL, NULL,
749 &general_beval_cb, NULL);
750# endif
751# endif
752# endif
753 if (!p_beval)
754 gui_mch_disable_beval_area(balloonEval);
755#endif
756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
758 if (!im_xim_isvalid_imactivate())
759 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
760#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000761 /* When 'cmdheight' was set during startup it may not have taken
762 * effect yet. */
763 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000764 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 return;
767 }
768
769error2:
770#ifdef FEAT_GUI_X11
771 /* undo gui_mch_init() */
772 gui_mch_uninit();
773#endif
774
775error:
776 gui.in_use = FALSE;
777 clip_init(FALSE);
778}
779
780
781 void
782gui_exit(rc)
783 int rc;
784{
785#ifndef __BEOS__
786 /* don't free the fonts, it leads to a BUS error
787 * richard@whitequeen.com Jul 99 */
788 free_highlight_fonts();
789#endif
790 gui.in_use = FALSE;
791 gui_mch_exit(rc);
792}
793
Bram Moolenaar9372a112005-12-06 19:59:18 +0000794#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000796# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797/*
798 * Called when the GUI shell is closed by the user. If there are no changed
799 * files Vim exits, otherwise there will be a dialog to ask the user what to
800 * do.
801 * When this function returns, Vim should NOT exit!
802 */
803 void
804gui_shell_closed()
805{
806 cmdmod_T save_cmdmod;
807
808 save_cmdmod = cmdmod;
809
810 /* Only exit when there are no changed files */
811 exiting = TRUE;
812# ifdef FEAT_BROWSE
813 cmdmod.browse = TRUE;
814# endif
815# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
816 cmdmod.confirm = TRUE;
817# endif
818 /* If there are changed buffers, present the user with a dialog if
819 * possible, otherwise give an error message. */
820 if (!check_changed_any(FALSE))
821 getout(0);
822
823 exiting = FALSE;
824 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000825 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826}
827#endif
828
829/*
830 * Set the font. "font_list" is a a comma separated list of font names. The
831 * first font name that works is used. If none is found, use the default
832 * font.
833 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
834 * Return OK when able to set the font. When it failed FAIL is returned and
835 * the fonts are unchanged.
836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 int
838gui_init_font(font_list, fontset)
839 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000840 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
842#define FONTLEN 320
843 char_u font_name[FONTLEN];
844 int font_list_empty = FALSE;
845 int ret = FAIL;
846
847 if (!gui.in_use)
848 return FAIL;
849
850 font_name[0] = NUL;
851 if (*font_list == NUL)
852 font_list_empty = TRUE;
853 else
854 {
855#ifdef FEAT_XFONTSET
856 /* When using a fontset, the whole list of fonts is one name. */
857 if (fontset)
858 ret = gui_mch_init_font(font_list, TRUE);
859 else
860#endif
861 while (*font_list != NUL)
862 {
863 /* Isolate one comma separated font name. */
864 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
865
866 /* Careful!!! The Win32 version of gui_mch_init_font(), when
867 * called with "*" will change p_guifont to the selected font
868 * name, which frees the old value. This makes font_list
869 * invalid. Thus when OK is returned here, font_list must no
870 * longer be used! */
871 if (gui_mch_init_font(font_name, FALSE) == OK)
872 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200873#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 /* If it's a Unicode font, try setting 'guifontwide' to a
875 * similar double-width font. */
876 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
877 && strstr((char *)font_name, "10646") != NULL)
878 set_guifontwide(font_name);
879#endif
880 ret = OK;
881 break;
882 }
883 }
884 }
885
886 if (ret != OK
887 && STRCMP(font_list, "*") != 0
888 && (font_list_empty || gui.norm_font == NOFONT))
889 {
890 /*
891 * Couldn't load any font in 'font_list', keep the current font if
892 * there is one. If 'font_list' is empty, or if there is no current
893 * font, tell gui_mch_init_font() to try to find a font we can load.
894 */
895 ret = gui_mch_init_font(NULL, FALSE);
896 }
897
898 if (ret == OK)
899 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200900#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 /* Set normal font as current font */
902# ifdef FEAT_XFONTSET
903 if (gui.fontset != NOFONTSET)
904 gui_mch_set_fontset(gui.fontset);
905 else
906# endif
907 gui_mch_set_font(gui.norm_font);
908#endif
909 gui_set_shellsize(FALSE,
910#ifdef MSWIN
911 TRUE
912#else
913 FALSE
914#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000915 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917
918 return ret;
919}
920
921#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200922# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923/*
924 * Try setting 'guifontwide' to a font twice as wide as "name".
925 */
926 static void
927set_guifontwide(name)
928 char_u *name;
929{
930 int i = 0;
931 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
932 char_u *wp = NULL;
933 char_u *p;
934 GuiFont font;
935
936 wp = wide_name;
937 for (p = name; *p != NUL; ++p)
938 {
939 *wp++ = *p;
940 if (*p == '-')
941 {
942 ++i;
943 if (i == 6) /* font type: change "--" to "-*-" */
944 {
945 if (p[1] == '-')
946 *wp++ = '*';
947 }
948 else if (i == 12) /* found the width */
949 {
950 ++p;
951 i = getdigits(&p);
952 if (i != 0)
953 {
954 /* Double the width specification. */
955 sprintf((char *)wp, "%d%s", i * 2, p);
956 font = gui_mch_get_font(wide_name, FALSE);
957 if (font != NOFONT)
958 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000959 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 gui.wide_font = font;
961 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000962 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
964 }
965 break;
966 }
967 }
968 }
969}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200970# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972/*
973 * Get the font for 'guifontwide'.
974 * Return FAIL for an invalid font name.
975 */
976 int
977gui_get_wide_font()
978{
979 GuiFont font = NOFONT;
980 char_u font_name[FONTLEN];
981 char_u *p;
982
983 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
984 return OK; /* Will give an error message later. */
985
986 if (p_guifontwide != NULL && *p_guifontwide != NUL)
987 {
988 for (p = p_guifontwide; *p != NUL; )
989 {
990 /* Isolate one comma separated font name. */
991 (void)copy_option_part(&p, font_name, FONTLEN, ",");
992 font = gui_mch_get_font(font_name, FALSE);
993 if (font != NOFONT)
994 break;
995 }
996 if (font == NOFONT)
997 return FAIL;
998 }
999
1000 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001001#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1003 if (font != NOFONT && gui.norm_font != NOFONT
1004 && pango_font_description_equal(font, gui.norm_font))
1005 {
1006 gui.wide_font = NOFONT;
1007 gui_mch_free_font(font);
1008 }
1009 else
1010#endif
1011 gui.wide_font = font;
1012 return OK;
1013}
1014#endif
1015
1016 void
1017gui_set_cursor(row, col)
1018 int row;
1019 int col;
1020{
1021 gui.row = row;
1022 gui.col = col;
1023}
1024
1025/*
1026 * gui_check_pos - check if the cursor is on the screen.
1027 */
1028 static void
1029gui_check_pos()
1030{
1031 if (gui.row >= screen_Rows)
1032 gui.row = screen_Rows - 1;
1033 if (gui.col >= screen_Columns)
1034 gui.col = screen_Columns - 1;
1035 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1036 gui.cursor_is_valid = FALSE;
1037}
1038
1039/*
1040 * Redraw the cursor if necessary or when forced.
1041 * Careful: The contents of ScreenLines[] must match what is on the screen,
1042 * otherwise this goes wrong. May need to call out_flush() first.
1043 */
1044 void
1045gui_update_cursor(force, clear_selection)
1046 int force; /* when TRUE, update even when not moved */
1047 int clear_selection;/* clear selection under cursor */
1048{
1049 int cur_width = 0;
1050 int cur_height = 0;
1051 int old_hl_mask;
1052 int idx;
1053 int id;
1054 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1055 int cattr; /* cursor attributes */
1056 int attr;
1057 attrentry_T *aep = NULL;
1058
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001059 /* Don't update the cursor when halfway busy scrolling or the screen size
1060 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1061 if (!can_update_cursor || screen_Columns != gui.num_cols
1062 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 return;
1064
1065 gui_check_pos();
1066 if (!gui.cursor_is_valid || force
1067 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1068 {
1069 gui_undraw_cursor();
1070 if (gui.row < 0)
1071 return;
1072#ifdef USE_IM_CONTROL
1073 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1074 im_set_position(gui.row, gui.col);
1075#endif
1076 gui.cursor_row = gui.row;
1077 gui.cursor_col = gui.col;
1078
1079 /* Only write to the screen after ScreenLines[] has been initialized */
1080 if (!screen_cleared || ScreenLines == NULL)
1081 return;
1082
1083 /* Clear the selection if we are about to write over it */
1084 if (clear_selection)
1085 clip_may_clear_selection(gui.row, gui.row);
1086 /* Check that the cursor is inside the shell (resizing may have made
1087 * it invalid) */
1088 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1089 return;
1090
1091 gui.cursor_is_valid = TRUE;
1092
1093 /*
1094 * How the cursor is drawn depends on the current mode.
1095 */
1096 idx = get_shape_idx(FALSE);
1097 if (State & LANGMAP)
1098 id = shape_table[idx].id_lm;
1099 else
1100 id = shape_table[idx].id;
1101
1102 /* get the colors and attributes for the cursor. Default is inverted */
1103 cfg = INVALCOLOR;
1104 cbg = INVALCOLOR;
1105 cattr = HL_INVERSE;
1106 gui_mch_set_blinking(shape_table[idx].blinkwait,
1107 shape_table[idx].blinkon,
1108 shape_table[idx].blinkoff);
1109 if (id > 0)
1110 {
1111 cattr = syn_id2colors(id, &cfg, &cbg);
1112#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1113 {
1114 static int iid;
1115 guicolor_T fg, bg;
1116
Bram Moolenaarc236c162008-07-13 17:41:49 +00001117 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001118# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001119 preedit_get_status()
1120# else
1121 im_get_status()
1122# endif
1123 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
1125 iid = syn_name2id((char_u *)"CursorIM");
1126 if (iid > 0)
1127 {
1128 syn_id2colors(iid, &fg, &bg);
1129 if (bg != INVALCOLOR)
1130 cbg = bg;
1131 if (fg != INVALCOLOR)
1132 cfg = fg;
1133 }
1134 }
1135 }
1136#endif
1137 }
1138
1139 /*
1140 * Get the attributes for the character under the cursor.
1141 * When no cursor color was given, use the character color.
1142 */
1143 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1144 if (attr > HL_ALL)
1145 aep = syn_gui_attr2entry(attr);
1146 if (aep != NULL)
1147 {
1148 attr = aep->ae_attr;
1149 if (cfg == INVALCOLOR)
1150 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1151 : aep->ae_u.gui.fg_color);
1152 if (cbg == INVALCOLOR)
1153 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1154 : aep->ae_u.gui.bg_color);
1155 }
1156 if (cfg == INVALCOLOR)
1157 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1158 if (cbg == INVALCOLOR)
1159 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1160
1161#ifdef FEAT_XIM
1162 if (aep != NULL)
1163 {
1164 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1165 : aep->ae_u.gui.bg_color);
1166 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1167 : aep->ae_u.gui.fg_color);
1168 if (xim_bg_color == INVALCOLOR)
1169 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1170 : gui.back_pixel;
1171 if (xim_fg_color == INVALCOLOR)
1172 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1173 : gui.norm_pixel;
1174 }
1175 else
1176 {
1177 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1178 : gui.back_pixel;
1179 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1180 : gui.norm_pixel;
1181 }
1182#endif
1183
1184 attr &= ~HL_INVERSE;
1185 if (cattr & HL_INVERSE)
1186 {
1187 cc = cbg;
1188 cbg = cfg;
1189 cfg = cc;
1190 }
1191 cattr &= ~HL_INVERSE;
1192
1193 /*
1194 * When we don't have window focus, draw a hollow cursor.
1195 */
1196 if (!gui.in_focus)
1197 {
1198 gui_mch_draw_hollow_cursor(cbg);
1199 return;
1200 }
1201
1202 old_hl_mask = gui.highlight_mask;
1203 if (shape_table[idx].shape == SHAPE_BLOCK
1204#ifdef FEAT_HANGULIN
1205 || composing_hangul
1206#endif
1207 )
1208 {
1209 /*
1210 * Draw the text character with the cursor colors. Use the
1211 * character attributes plus the cursor attributes.
1212 */
1213 gui.highlight_mask = (cattr | attr);
1214#ifdef FEAT_HANGULIN
1215 if (composing_hangul)
1216 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1217 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1218 else
1219#endif
1220 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1221 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1222 }
1223 else
1224 {
1225#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1226 int col_off = FALSE;
1227#endif
1228 /*
1229 * First draw the partial cursor, then overwrite with the text
1230 * character, using a transparent background.
1231 */
1232 if (shape_table[idx].shape == SHAPE_VER)
1233 {
1234 cur_height = gui.char_height;
1235 cur_width = (gui.char_width * shape_table[idx].percentage
1236 + 99) / 100;
1237 }
1238 else
1239 {
1240 cur_height = (gui.char_height * shape_table[idx].percentage
1241 + 99) / 100;
1242 cur_width = gui.char_width;
1243 }
1244#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001245 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1246 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 /* Double wide character. */
1249 if (shape_table[idx].shape != SHAPE_VER)
1250 cur_width += gui.char_width;
1251# ifdef FEAT_RIGHTLEFT
1252 if (CURSOR_BAR_RIGHT)
1253 {
1254 /* gui.col points to the left halve of the character but
1255 * the vertical line needs to be on the right halve.
1256 * A double-wide horizontal line is also drawn from the
1257 * right halve in gui_mch_draw_part_cursor(). */
1258 col_off = TRUE;
1259 ++gui.col;
1260 }
1261# endif
1262 }
1263#endif
1264 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1265#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1266 if (col_off)
1267 --gui.col;
1268#endif
1269
1270#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1271 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1272 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1273 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1274 (guicolor_T)0, (guicolor_T)0, 0);
1275#endif
1276 }
1277 gui.highlight_mask = old_hl_mask;
1278 }
1279}
1280
1281#if defined(FEAT_MENU) || defined(PROTO)
1282 void
1283gui_position_menu()
1284{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001285# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (gui.menu_is_active && gui.in_use)
1287 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1288# endif
1289}
1290#endif
1291
1292/*
1293 * Position the various GUI components (text area, menu). The vertical
1294 * scrollbars are NOT handled here. See gui_update_scrollbars().
1295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 static void
1297gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001298 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int text_area_x;
1301 int text_area_y;
1302 int text_area_width;
1303 int text_area_height;
1304
1305 /* avoid that moving components around generates events */
1306 ++hold_gui_events;
1307
1308 text_area_x = 0;
1309 if (gui.which_scrollbars[SBAR_LEFT])
1310 text_area_x += gui.scrollbar_width;
1311
1312 text_area_y = 0;
1313#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1314 gui.menu_width = total_width;
1315 if (gui.menu_is_active)
1316 text_area_y += gui.menu_height;
1317#endif
1318#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1319 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1320 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1321#endif
1322
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001323# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001324 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001325 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001326 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001327#endif
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1330 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1331 {
1332# ifdef FEAT_GUI_ATHENA
1333 gui_mch_set_toolbar_pos(0, text_area_y,
1334 gui.menu_width, gui.toolbar_height);
1335# endif
1336 text_area_y += gui.toolbar_height;
1337 }
1338#endif
1339
1340 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1341 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1342
1343 gui_mch_set_text_area_pos(text_area_x,
1344 text_area_y,
1345 text_area_width,
1346 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001347#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 + xim_get_status_area_height()
1349#endif
1350 );
1351#ifdef FEAT_MENU
1352 gui_position_menu();
1353#endif
1354 if (gui.which_scrollbars[SBAR_BOTTOM])
1355 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1356 text_area_x,
1357 text_area_y + text_area_height,
1358 text_area_width,
1359 gui.scrollbar_height);
1360 gui.left_sbar_x = 0;
1361 gui.right_sbar_x = text_area_x + text_area_width;
1362
1363 --hold_gui_events;
1364}
1365
Bram Moolenaar02743632005-07-25 20:42:36 +00001366/*
1367 * Get the width of the widgets and decorations to the side of the text area.
1368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 int
1370gui_get_base_width()
1371{
1372 int base_width;
1373
1374 base_width = 2 * gui.border_offset;
1375 if (gui.which_scrollbars[SBAR_LEFT])
1376 base_width += gui.scrollbar_width;
1377 if (gui.which_scrollbars[SBAR_RIGHT])
1378 base_width += gui.scrollbar_width;
1379 return base_width;
1380}
1381
Bram Moolenaar02743632005-07-25 20:42:36 +00001382/*
1383 * Get the height of the widgets and decorations above and below the text area.
1384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int
1386gui_get_base_height()
1387{
1388 int base_height;
1389
1390 base_height = 2 * gui.border_offset;
1391 if (gui.which_scrollbars[SBAR_BOTTOM])
1392 base_height += gui.scrollbar_height;
1393#ifdef FEAT_GUI_GTK
1394 /* We can't take the sizes properly into account until anything is
1395 * realized. Therefore we recalculate all the values here just before
1396 * setting the size. (--mdcki) */
1397#else
1398# ifdef FEAT_MENU
1399 if (gui.menu_is_active)
1400 base_height += gui.menu_height;
1401# endif
1402# ifdef FEAT_TOOLBAR
1403 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1404# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1405 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1406# else
1407 base_height += gui.toolbar_height;
1408# endif
1409# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001410# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1411 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001412 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001413 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415# ifdef FEAT_FOOTER
1416 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1417 base_height += gui.footer_height;
1418# endif
1419# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1420 base_height += gui_mch_text_area_extra_height();
1421# endif
1422#endif
1423 return base_height;
1424}
1425
1426/*
1427 * Should be called after the GUI shell has been resized. Its arguments are
1428 * the new width and height of the shell in pixels.
1429 */
1430 void
1431gui_resize_shell(pixel_width, pixel_height)
1432 int pixel_width;
1433 int pixel_height;
1434{
1435 static int busy = FALSE;
1436
1437 if (!gui.shell_created) /* ignore when still initializing */
1438 return;
1439
1440 /*
1441 * Can't resize the screen while it is being redrawn. Remember the new
1442 * size and handle it later.
1443 */
1444 if (updating_screen || busy)
1445 {
1446 new_pixel_width = pixel_width;
1447 new_pixel_height = pixel_height;
1448 return;
1449 }
1450
1451again:
1452 busy = TRUE;
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /* Flush pending output before redrawing */
1455 out_flush();
1456
1457 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001458 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460 gui_position_components(pixel_width);
1461
1462 gui_reset_scroll_region();
1463 /*
1464 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1465 * at the last line here (why does it have to be one row too low?).
1466 */
1467 if (State == ASKMORE || State == CONFIRM)
1468 gui.row = gui.num_rows;
1469
1470 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1471 * the safe side. */
1472 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1473 || gui.num_rows != Rows || gui.num_cols != Columns)
1474 shell_resized();
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 gui_update_scrollbars(TRUE);
1477 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001478#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 xim_set_status_area();
1480#endif
1481
1482 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 /*
1485 * We could have been called again while redrawing the screen.
1486 * Need to do it all again with the latest size then.
1487 */
1488 if (new_pixel_height)
1489 {
1490 pixel_width = new_pixel_width;
1491 pixel_height = new_pixel_height;
1492 new_pixel_width = 0;
1493 new_pixel_height = 0;
1494 goto again;
1495 }
1496}
1497
1498/*
1499 * Check if gui_resize_shell() must be called.
1500 */
1501 void
1502gui_may_resize_shell()
1503{
1504 int h, w;
1505
1506 if (new_pixel_height)
1507 {
1508 /* careful: gui_resize_shell() may postpone the resize again if we
1509 * were called indirectly by it */
1510 w = new_pixel_width;
1511 h = new_pixel_height;
1512 new_pixel_width = 0;
1513 new_pixel_height = 0;
1514 gui_resize_shell(w, h);
1515 }
1516}
1517
1518 int
1519gui_get_shellsize()
1520{
1521 Rows = gui.num_rows;
1522 Columns = gui.num_cols;
1523 return OK;
1524}
1525
1526/*
1527 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001528 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1529 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001532gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001533 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001535 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 int base_width;
1538 int base_height;
1539 int width;
1540 int height;
1541 int min_width;
1542 int min_height;
1543 int screen_w;
1544 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001545#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001546 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001547 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001548#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001549 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
1551 if (!gui.shell_created)
1552 return;
1553
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001554#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 /* If not setting to a user specified size and maximized, calculate the
1556 * number of characters that fit in the maximized window. */
1557 if (!mustset && gui_mch_maximized())
1558 {
1559 gui_mch_newfont();
1560 return;
1561 }
1562#endif
1563
1564 base_width = gui_get_base_width();
1565 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001566 if (fit_to_display)
1567 /* Remember the original window position. */
1568 gui_mch_get_winpos(&x, &y);
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#ifdef USE_SUN_WORKSHOP
1571 if (!mustset && usingSunWorkShop
1572 && workshop_get_width_height(&width, &height))
1573 {
1574 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1575 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1576 }
1577 else
1578#endif
1579 {
1580 width = Columns * gui.char_width + base_width;
1581 height = Rows * gui.char_height + base_height;
1582 }
1583
1584 if (fit_to_display)
1585 {
1586 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001587 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 Columns = (screen_w - base_width) / gui.char_width;
1590 if (Columns < MIN_COLUMNS)
1591 Columns = MIN_COLUMNS;
1592 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001593#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001594 ++did_adjust;
1595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001597 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 Rows = (screen_h - base_height) / gui.char_height;
1600 check_shellsize();
1601 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001602#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001603 ++did_adjust;
1604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001606#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001607 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1608 && height + gui.char_height >= screen_h))
1609 /* don't unmaximize if at maximum size */
1610 un_maximize = FALSE;
1611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 gui.num_cols = Columns;
1614 gui.num_rows = Rows;
1615
1616 min_width = base_width + MIN_COLUMNS * gui.char_width;
1617 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001618#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001619 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001620#endif
1621
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001622#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001623 if (un_maximize)
1624 {
1625 /* If the window size is smaller than the screen unmaximize the
1626 * window, otherwise resizing won't work. */
1627 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1628 if ((width + gui.char_width < screen_w
1629 || height + gui.char_height * 2 < screen_h)
1630 && gui_mch_maximized())
1631 gui_mch_unmaximize();
1632 }
1633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
1635 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001636 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001638 if (fit_to_display && x >= 0 && y >= 0)
1639 {
1640 /* Some window managers put the Vim window left of/above the screen.
1641 * Only change the position if it wasn't already negative before
1642 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 gui_mch_update();
1644 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1645 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1646 }
1647
1648 gui_position_components(width);
1649 gui_update_scrollbars(TRUE);
1650 gui_reset_scroll_region();
1651}
1652
1653/*
1654 * Called when Rows and/or Columns has changed.
1655 */
1656 void
1657gui_new_shellsize()
1658{
1659 gui_reset_scroll_region();
1660}
1661
1662/*
1663 * Make scroll region cover whole screen.
1664 */
1665 void
1666gui_reset_scroll_region()
1667{
1668 gui.scroll_region_top = 0;
1669 gui.scroll_region_bot = gui.num_rows - 1;
1670 gui.scroll_region_left = 0;
1671 gui.scroll_region_right = gui.num_cols - 1;
1672}
1673
1674 void
1675gui_start_highlight(mask)
1676 int mask;
1677{
1678 if (mask > HL_ALL) /* highlight code */
1679 gui.highlight_mask = mask;
1680 else /* mask */
1681 gui.highlight_mask |= mask;
1682}
1683
1684 void
1685gui_stop_highlight(mask)
1686 int mask;
1687{
1688 if (mask > HL_ALL) /* highlight code */
1689 gui.highlight_mask = HL_NORMAL;
1690 else /* mask */
1691 gui.highlight_mask &= ~mask;
1692}
1693
1694/*
1695 * Clear a rectangular region of the screen from text pos (row1, col1) to
1696 * (row2, col2) inclusive.
1697 */
1698 void
1699gui_clear_block(row1, col1, row2, col2)
1700 int row1;
1701 int col1;
1702 int row2;
1703 int col2;
1704{
1705 /* Clear the selection if we are about to write over it */
1706 clip_may_clear_selection(row1, row2);
1707
1708 gui_mch_clear_block(row1, col1, row2, col2);
1709
1710 /* Invalidate cursor if it was in this block */
1711 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1712 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1713 gui.cursor_is_valid = FALSE;
1714}
1715
1716/*
1717 * Write code to update the cursor later. This avoids the need to flush the
1718 * output buffer before calling gui_update_cursor().
1719 */
1720 void
1721gui_update_cursor_later()
1722{
1723 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1724}
1725
1726 void
1727gui_write(s, len)
1728 char_u *s;
1729 int len;
1730{
1731 char_u *p;
1732 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 int force_scrollbar = FALSE;
1735 static win_T *old_curwin = NULL;
1736
1737/* #define DEBUG_GUI_WRITE */
1738#ifdef DEBUG_GUI_WRITE
1739 {
1740 int i;
1741 char_u *str;
1742
1743 printf("gui_write(%d):\n ", len);
1744 for (i = 0; i < len; i++)
1745 if (s[i] == ESC)
1746 {
1747 if (i != 0)
1748 printf("\n ");
1749 printf("<ESC>");
1750 }
1751 else
1752 {
1753 str = transchar_byte(s[i]);
1754 if (str[0] && str[1])
1755 printf("<%s>", (char *)str);
1756 else
1757 printf("%s", (char *)str);
1758 }
1759 printf("\n");
1760 }
1761#endif
1762 while (len)
1763 {
1764 if (s[0] == ESC && s[1] == '|')
1765 {
1766 p = s + 2;
1767 if (VIM_ISDIGIT(*p))
1768 {
1769 arg1 = getdigits(&p);
1770 if (p > s + len)
1771 break;
1772 if (*p == ';')
1773 {
1774 ++p;
1775 arg2 = getdigits(&p);
1776 if (p > s + len)
1777 break;
1778 }
1779 }
1780 switch (*p)
1781 {
1782 case 'C': /* Clear screen */
1783 clip_scroll_selection(9999);
1784 gui_mch_clear_all();
1785 gui.cursor_is_valid = FALSE;
1786 force_scrollbar = TRUE;
1787 break;
1788 case 'M': /* Move cursor */
1789 gui_set_cursor(arg1, arg2);
1790 break;
1791 case 's': /* force cursor (shape) update */
1792 force_cursor = TRUE;
1793 break;
1794 case 'R': /* Set scroll region */
1795 if (arg1 < arg2)
1796 {
1797 gui.scroll_region_top = arg1;
1798 gui.scroll_region_bot = arg2;
1799 }
1800 else
1801 {
1802 gui.scroll_region_top = arg2;
1803 gui.scroll_region_bot = arg1;
1804 }
1805 break;
1806#ifdef FEAT_VERTSPLIT
1807 case 'V': /* Set vertical scroll region */
1808 if (arg1 < arg2)
1809 {
1810 gui.scroll_region_left = arg1;
1811 gui.scroll_region_right = arg2;
1812 }
1813 else
1814 {
1815 gui.scroll_region_left = arg2;
1816 gui.scroll_region_right = arg1;
1817 }
1818 break;
1819#endif
1820 case 'd': /* Delete line */
1821 gui_delete_lines(gui.row, 1);
1822 break;
1823 case 'D': /* Delete lines */
1824 gui_delete_lines(gui.row, arg1);
1825 break;
1826 case 'i': /* Insert line */
1827 gui_insert_lines(gui.row, 1);
1828 break;
1829 case 'I': /* Insert lines */
1830 gui_insert_lines(gui.row, arg1);
1831 break;
1832 case '$': /* Clear to end-of-line */
1833 gui_clear_block(gui.row, gui.col, gui.row,
1834 (int)Columns - 1);
1835 break;
1836 case 'h': /* Turn on highlighting */
1837 gui_start_highlight(arg1);
1838 break;
1839 case 'H': /* Turn off highlighting */
1840 gui_stop_highlight(arg1);
1841 break;
1842 case 'f': /* flash the window (visual bell) */
1843 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1844 break;
1845 default:
1846 p = s + 1; /* Skip the ESC */
1847 break;
1848 }
1849 len -= (int)(++p - s);
1850 s = p;
1851 }
1852 else if (
1853#ifdef EBCDIC
1854 CtrlChar(s[0]) != 0 /* Ctrl character */
1855#else
1856 s[0] < 0x20 /* Ctrl character */
1857#endif
1858#ifdef FEAT_SIGN_ICONS
1859 && s[0] != SIGN_BYTE
1860# ifdef FEAT_NETBEANS_INTG
1861 && s[0] != MULTISIGN_BYTE
1862# endif
1863#endif
1864 )
1865 {
1866 if (s[0] == '\n') /* NL */
1867 {
1868 gui.col = 0;
1869 if (gui.row < gui.scroll_region_bot)
1870 gui.row++;
1871 else
1872 gui_delete_lines(gui.scroll_region_top, 1);
1873 }
1874 else if (s[0] == '\r') /* CR */
1875 {
1876 gui.col = 0;
1877 }
1878 else if (s[0] == '\b') /* Backspace */
1879 {
1880 if (gui.col)
1881 --gui.col;
1882 }
1883 else if (s[0] == Ctrl_L) /* cursor-right */
1884 {
1885 ++gui.col;
1886 }
1887 else if (s[0] == Ctrl_G) /* Beep */
1888 {
1889 gui_mch_beep();
1890 }
1891 /* Other Ctrl character: shouldn't happen! */
1892
1893 --len; /* Skip this char */
1894 ++s;
1895 }
1896 else
1897 {
1898 p = s;
1899 while (len > 0 && (
1900#ifdef EBCDIC
1901 CtrlChar(*p) == 0
1902#else
1903 *p >= 0x20
1904#endif
1905#ifdef FEAT_SIGN_ICONS
1906 || *p == SIGN_BYTE
1907# ifdef FEAT_NETBEANS_INTG
1908 || *p == MULTISIGN_BYTE
1909# endif
1910#endif
1911 ))
1912 {
1913 len--;
1914 p++;
1915 }
1916 gui_outstr(s, (int)(p - s));
1917 s = p;
1918 }
1919 }
1920
1921 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1922 * set). */
1923 if (force_cursor)
1924 gui_update_cursor(TRUE, TRUE);
1925
1926 /* When switching to another window the dragging must have stopped.
1927 * Required for GTK, dragged_sb isn't reset. */
1928 if (old_curwin != curwin)
1929 gui.dragged_sb = SBAR_NONE;
1930
1931 /* Update the scrollbars after clearing the screen or when switched
1932 * to another window.
1933 * Update the horizontal scrollbar always, it's difficult to check all
1934 * situations where it might change. */
1935 if (force_scrollbar || old_curwin != curwin)
1936 gui_update_scrollbars(force_scrollbar);
1937 else
1938 gui_update_horiz_scrollbar(FALSE);
1939 old_curwin = curwin;
1940
1941 /*
1942 * We need to make sure this is cleared since Athena doesn't tell us when
1943 * he is done dragging. Do the same for GTK.
1944 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001945#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 gui.dragged_sb = SBAR_NONE;
1947#endif
1948
1949 gui_mch_flush(); /* In case vim decides to take a nap */
1950}
1951
1952/*
1953 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1954 * produces wrong results. Call gui_dont_update_cursor() before that code and
1955 * gui_can_update_cursor() afterwards.
1956 */
1957 void
1958gui_dont_update_cursor()
1959{
1960 if (gui.in_use)
1961 {
1962 /* Undraw the cursor now, we probably can't do it after the change. */
1963 gui_undraw_cursor();
1964 can_update_cursor = FALSE;
1965 }
1966}
1967
1968 void
1969gui_can_update_cursor()
1970{
1971 can_update_cursor = TRUE;
1972 /* No need to update the cursor right now, there is always more output
1973 * after scrolling. */
1974}
1975
1976 static void
1977gui_outstr(s, len)
1978 char_u *s;
1979 int len;
1980{
1981 int this_len;
1982#ifdef FEAT_MBYTE
1983 int cells;
1984#endif
1985
1986 if (len == 0)
1987 return;
1988
1989 if (len < 0)
1990 len = (int)STRLEN(s);
1991
1992 while (len > 0)
1993 {
1994#ifdef FEAT_MBYTE
1995 if (has_mbyte)
1996 {
1997 /* Find out how many chars fit in the current line. */
1998 cells = 0;
1999 for (this_len = 0; this_len < len; )
2000 {
2001 cells += (*mb_ptr2cells)(s + this_len);
2002 if (gui.col + cells > Columns)
2003 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002004 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 if (this_len > len)
2007 this_len = len; /* don't include following composing char */
2008 }
2009 else
2010#endif
2011 if (gui.col + len > Columns)
2012 this_len = Columns - gui.col;
2013 else
2014 this_len = len;
2015
2016 (void)gui_outstr_nowrap(s, this_len,
2017 0, (guicolor_T)0, (guicolor_T)0, 0);
2018 s += this_len;
2019 len -= this_len;
2020#ifdef FEAT_MBYTE
2021 /* fill up for a double-width char that doesn't fit. */
2022 if (len > 0 && gui.col < Columns)
2023 (void)gui_outstr_nowrap((char_u *)" ", 1,
2024 0, (guicolor_T)0, (guicolor_T)0, 0);
2025#endif
2026 /* The cursor may wrap to the next line. */
2027 if (gui.col >= Columns)
2028 {
2029 gui.col = 0;
2030 gui.row++;
2031 }
2032 }
2033}
2034
2035/*
2036 * Output one character (may be one or two display cells).
2037 * Caller must check for valid "off".
2038 * Returns FAIL or OK, just like gui_outstr_nowrap().
2039 */
2040 static int
2041gui_screenchar(off, flags, fg, bg, back)
2042 int off; /* Offset from start of screen */
2043 int flags;
2044 guicolor_T fg, bg; /* colors for cursor */
2045 int back; /* backup this many chars when using bold trick */
2046{
2047#ifdef FEAT_MBYTE
2048 char_u buf[MB_MAXBYTES + 1];
2049
2050 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2051 if (enc_utf8 && ScreenLines[off] == 0)
2052 return OK;
2053
2054 if (enc_utf8 && ScreenLinesUC[off] != 0)
2055 /* Draw UTF-8 multi-byte character. */
2056 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2057 flags, fg, bg, back);
2058
2059 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2060 {
2061 buf[0] = ScreenLines[off];
2062 buf[1] = ScreenLines2[off];
2063 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2064 }
2065
2066 /* Draw non-multi-byte character or DBCS character. */
2067 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002068 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 flags, fg, bg, back);
2070#else
2071 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2072#endif
2073}
2074
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002075#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076/*
2077 * Output the string at the given screen position. This is used in place
2078 * of gui_screenchar() where possible because Pango needs as much context
2079 * as possible to work nicely. It's a lot faster as well.
2080 */
2081 static int
2082gui_screenstr(off, len, flags, fg, bg, back)
2083 int off; /* Offset from start of screen */
2084 int len; /* string length in screen cells */
2085 int flags;
2086 guicolor_T fg, bg; /* colors for cursor */
2087 int back; /* backup this many chars when using bold trick */
2088{
2089 char_u *buf;
2090 int outlen = 0;
2091 int i;
2092 int retval;
2093
2094 if (len <= 0) /* "cannot happen"? */
2095 return OK;
2096
2097 if (enc_utf8)
2098 {
2099 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2100 if (buf == NULL)
2101 return OK; /* not much we could do here... */
2102
2103 for (i = off; i < off + len; ++i)
2104 {
2105 if (ScreenLines[i] == 0)
2106 continue; /* skip second half of double-width char */
2107
2108 if (ScreenLinesUC[i] == 0)
2109 buf[outlen++] = ScreenLines[i];
2110 else
2111 outlen += utfc_char2bytes(i, buf + outlen);
2112 }
2113
2114 buf[outlen] = NUL; /* only to aid debugging */
2115 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2116 vim_free(buf);
2117
2118 return retval;
2119 }
2120 else if (enc_dbcs == DBCS_JPNU)
2121 {
2122 buf = alloc((unsigned)(len * 2 + 1));
2123 if (buf == NULL)
2124 return OK; /* not much we could do here... */
2125
2126 for (i = off; i < off + len; ++i)
2127 {
2128 buf[outlen++] = ScreenLines[i];
2129
2130 /* handle double-byte single-width char */
2131 if (ScreenLines[i] == 0x8e)
2132 buf[outlen++] = ScreenLines2[i];
2133 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2134 buf[outlen++] = ScreenLines[++i];
2135 }
2136
2137 buf[outlen] = NUL; /* only to aid debugging */
2138 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2139 vim_free(buf);
2140
2141 return retval;
2142 }
2143 else
2144 {
2145 return gui_outstr_nowrap(&ScreenLines[off], len,
2146 flags, fg, bg, back);
2147 }
2148}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002149#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151/*
2152 * Output the given string at the current cursor position. If the string is
2153 * too long to fit on the line, then it is truncated.
2154 * "flags":
2155 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2156 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002157 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 * background.
2159 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2160 * it.
2161 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2162 * FAIL (the caller should start drawing "back" chars back).
2163 */
2164 int
2165gui_outstr_nowrap(s, len, flags, fg, bg, back)
2166 char_u *s;
2167 int len;
2168 int flags;
2169 guicolor_T fg, bg; /* colors for cursor */
2170 int back; /* backup this many chars when using bold trick */
2171{
2172 long_u highlight_mask;
2173 long_u hl_mask_todo;
2174 guicolor_T fg_color;
2175 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002176 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002177#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 GuiFont font = NOFONT;
2179# ifdef FEAT_XFONTSET
2180 GuiFontset fontset = NOFONTSET;
2181# endif
2182#endif
2183 attrentry_T *aep = NULL;
2184 int draw_flags;
2185 int col = gui.col;
2186#ifdef FEAT_SIGN_ICONS
2187 int draw_sign = FALSE;
2188# ifdef FEAT_NETBEANS_INTG
2189 int multi_sign = FALSE;
2190# endif
2191#endif
2192
2193 if (len < 0)
2194 len = (int)STRLEN(s);
2195 if (len == 0)
2196 return OK;
2197
2198#ifdef FEAT_SIGN_ICONS
2199 if (*s == SIGN_BYTE
2200# ifdef FEAT_NETBEANS_INTG
2201 || *s == MULTISIGN_BYTE
2202# endif
2203 )
2204 {
2205# ifdef FEAT_NETBEANS_INTG
2206 if (*s == MULTISIGN_BYTE)
2207 multi_sign = TRUE;
2208# endif
2209 /* draw spaces instead */
2210 s = (char_u *)" ";
2211 if (len == 1 && col > 0)
2212 --col;
2213 len = 2;
2214 draw_sign = TRUE;
2215 highlight_mask = 0;
2216 }
2217 else
2218#endif
2219 if (gui.highlight_mask > HL_ALL)
2220 {
2221 aep = syn_gui_attr2entry(gui.highlight_mask);
2222 if (aep == NULL) /* highlighting not set */
2223 highlight_mask = 0;
2224 else
2225 highlight_mask = aep->ae_attr;
2226 }
2227 else
2228 highlight_mask = gui.highlight_mask;
2229 hl_mask_todo = highlight_mask;
2230
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002231#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 /* Set the font */
2233 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2234 font = aep->ae_u.gui.font;
2235# ifdef FEAT_XFONTSET
2236 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2237 fontset = aep->ae_u.gui.fontset;
2238# endif
2239 else
2240 {
2241# ifdef FEAT_XFONTSET
2242 if (gui.fontset != NOFONTSET)
2243 fontset = gui.fontset;
2244 else
2245# endif
2246 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2247 {
2248 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2249 {
2250 font = gui.boldital_font;
2251 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2252 }
2253 else if (gui.bold_font != NOFONT)
2254 {
2255 font = gui.bold_font;
2256 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2257 }
2258 else
2259 font = gui.norm_font;
2260 }
2261 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2262 {
2263 font = gui.ital_font;
2264 hl_mask_todo &= ~HL_ITALIC;
2265 }
2266 else
2267 font = gui.norm_font;
2268 }
2269# ifdef FEAT_XFONTSET
2270 if (fontset != NOFONTSET)
2271 gui_mch_set_fontset(fontset);
2272 else
2273# endif
2274 gui_mch_set_font(font);
2275#endif
2276
2277 draw_flags = 0;
2278
2279 /* Set the color */
2280 bg_color = gui.back_pixel;
2281 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2282 {
2283 draw_flags |= DRAW_CURSOR;
2284 fg_color = fg;
2285 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002286 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 else if (aep != NULL)
2289 {
2290 fg_color = aep->ae_u.gui.fg_color;
2291 if (fg_color == INVALCOLOR)
2292 fg_color = gui.norm_pixel;
2293 bg_color = aep->ae_u.gui.bg_color;
2294 if (bg_color == INVALCOLOR)
2295 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002296 sp_color = aep->ae_u.gui.sp_color;
2297 if (sp_color == INVALCOLOR)
2298 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002303 sp_color = fg_color;
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2307 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002308#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 gui_mch_set_colors(bg_color, fg_color);
2310#else
2311 gui_mch_set_fg_color(bg_color);
2312 gui_mch_set_bg_color(fg_color);
2313#endif
2314 }
2315 else
2316 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002317#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 gui_mch_set_colors(fg_color, bg_color);
2319#else
2320 gui_mch_set_fg_color(fg_color);
2321 gui_mch_set_bg_color(bg_color);
2322#endif
2323 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002324 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 /* Clear the selection if we are about to write over it */
2327 if (!(flags & GUI_MON_NOCLEAR))
2328 clip_may_clear_selection(gui.row, gui.row);
2329
2330
2331#ifndef MSWIN16_FASTTEXT
2332 /* If there's no bold font, then fake it */
2333 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2334 draw_flags |= DRAW_BOLD;
2335#endif
2336
2337 /*
2338 * When drawing bold or italic characters the spill-over from the left
2339 * neighbor may be destroyed. Let the caller backup to start redrawing
2340 * just after a blank.
2341 */
2342 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2343 return FAIL;
2344
Bram Moolenaare60acc12011-05-10 16:41:25 +02002345#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 /* If there's no italic font, then fake it.
2347 * For GTK2, we don't need a different font for italic style. */
2348 if (hl_mask_todo & HL_ITALIC)
2349 draw_flags |= DRAW_ITALIC;
2350
2351 /* Do we underline the text? */
2352 if (hl_mask_todo & HL_UNDERLINE)
2353 draw_flags |= DRAW_UNDERL;
2354#else
2355 /* Do we underline the text? */
2356 if ((hl_mask_todo & HL_UNDERLINE)
2357# ifndef MSWIN16_FASTTEXT
2358 || (hl_mask_todo & HL_ITALIC)
2359# endif
2360 )
2361 draw_flags |= DRAW_UNDERL;
2362#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002363 /* Do we undercurl the text? */
2364 if (hl_mask_todo & HL_UNDERCURL)
2365 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002367 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (flags & GUI_MON_TRS_CURSOR)
2369 draw_flags |= DRAW_TRANSP;
2370
2371 /*
2372 * Draw the text.
2373 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002374#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 /* The value returned is the length in display cells */
2376 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2377#else
2378# ifdef FEAT_MBYTE
2379 if (enc_utf8)
2380 {
2381 int start; /* index of bytes to be drawn */
2382 int cells; /* cellwidth of bytes to be drawn */
2383 int thislen; /* length of bytes to be drawin */
2384 int cn; /* cellwidth of current char */
2385 int i; /* index of current char */
2386 int c; /* current char value */
2387 int cl; /* byte length of current char */
2388 int comping; /* current char is composing */
2389 int scol = col; /* screen column */
2390 int dowide; /* use 'guifontwide' */
2391
2392 /* Break the string at a composing character, it has to be drawn on
2393 * top of the previous character. */
2394 start = 0;
2395 cells = 0;
2396 for (i = 0; i < len; i += cl)
2397 {
2398 c = utf_ptr2char(s + i);
2399 cn = utf_char2cells(c);
2400 if (cn > 1
2401# ifdef FEAT_XFONTSET
2402 && fontset == NOFONTSET
2403# endif
2404 && gui.wide_font != NOFONT)
2405 dowide = TRUE;
2406 else
2407 dowide = FALSE;
2408 comping = utf_iscomposing(c);
2409 if (!comping) /* count cells from non-composing chars */
2410 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002411 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (cl == 0) /* hit end of string */
2413 len = i + cl; /* len must be wrong "cannot happen" */
2414
2415 /* print the string so far if it's the last character or there is
2416 * a composing character. */
2417 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002418# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 || (cn > 1
2420# ifdef FEAT_XFONTSET
2421 /* No fontset: At least draw char after wide char at
2422 * right position. */
2423 && fontset == NOFONTSET
2424# endif
2425 )
2426# endif
2427 )
2428 {
2429 if (comping || dowide)
2430 thislen = i - start;
2431 else
2432 thislen = i - start + cl;
2433 if (thislen > 0)
2434 {
2435 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2436 draw_flags);
2437 start += thislen;
2438 }
2439 scol += cells;
2440 cells = 0;
2441 if (dowide)
2442 {
2443 gui_mch_set_font(gui.wide_font);
2444 gui_mch_draw_string(gui.row, scol - cn,
2445 s + start, cl, draw_flags);
2446 gui_mch_set_font(font);
2447 start += cl;
2448 }
2449
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002450# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002451 /* No fontset: draw a space to fill the gap after a wide char
2452 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2454# ifdef FEAT_XFONTSET
2455 && fontset == NOFONTSET
2456# endif
2457 && !dowide)
2458 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2459 1, draw_flags);
2460# endif
2461 }
2462 /* Draw a composing char on top of the previous char. */
2463 if (comping)
2464 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002465# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002466 /* Carbon ATSUI autodraws composing char over previous char */
2467 gui_mch_draw_string(gui.row, scol, s + i, cl,
2468 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002469# else
2470 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2471 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 start = i + cl;
2474 }
2475 }
2476 /* The stuff below assumes "len" is the length in screen columns. */
2477 len = scol - col;
2478 }
2479 else
2480# endif
2481 {
2482 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2483# ifdef FEAT_MBYTE
2484 if (enc_dbcs == DBCS_JPNU)
2485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 /* Get the length in display cells, this can be different from the
2487 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002488 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490# endif
2491 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002492#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2495 gui.col = col + len;
2496
2497 /* May need to invert it when it's part of the selection. */
2498 if (flags & GUI_MON_NOCLEAR)
2499 clip_may_redraw_selection(gui.row, col, len);
2500
2501 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2502 {
2503 /* Invalidate the old physical cursor position if we wrote over it */
2504 if (gui.cursor_row == gui.row
2505 && gui.cursor_col >= col
2506 && gui.cursor_col < col + len)
2507 gui.cursor_is_valid = FALSE;
2508 }
2509
2510#ifdef FEAT_SIGN_ICONS
2511 if (draw_sign)
2512 /* Draw the sign on top of the spaces. */
2513 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002514# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002515 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 if (multi_sign)
2517 netbeans_draw_multisign_indicator(gui.row);
2518# endif
2519#endif
2520
2521 return OK;
2522}
2523
2524/*
2525 * Un-draw the cursor. Actually this just redraws the character at the given
2526 * position. The character just before it too, for when it was in bold.
2527 */
2528 void
2529gui_undraw_cursor()
2530{
2531 if (gui.cursor_is_valid)
2532 {
2533#ifdef FEAT_HANGULIN
2534 if (composing_hangul
2535 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2536 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2537 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2538 gui.norm_pixel, gui.back_pixel, 0);
2539 else
2540 {
2541#endif
2542 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2543 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2544 && gui.cursor_col > 0)
2545 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2546 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2547#ifdef FEAT_HANGULIN
2548 if (composing_hangul)
2549 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2550 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2551 }
2552#endif
2553 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2554 * here in case it wasn't needed to undraw it. */
2555 gui.cursor_is_valid = FALSE;
2556 }
2557}
2558
2559 void
2560gui_redraw(x, y, w, h)
2561 int x;
2562 int y;
2563 int w;
2564 int h;
2565{
2566 int row1, col1, row2, col2;
2567
2568 row1 = Y_2_ROW(y);
2569 col1 = X_2_COL(x);
2570 row2 = Y_2_ROW(y + h - 1);
2571 col2 = X_2_COL(x + w - 1);
2572
2573 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2574
2575 /*
2576 * We may need to redraw the cursor, but don't take it upon us to change
2577 * its location after a scroll.
2578 * (maybe be more strict even and test col too?)
2579 * These things may be outside the update/clipping region and reality may
2580 * not reflect Vims internal ideas if these operations are clipped away.
2581 */
2582 if (gui.row == gui.cursor_row)
2583 gui_update_cursor(TRUE, TRUE);
2584}
2585
2586/*
2587 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2588 * from col1 to col2 (inclusive).
2589 * Return TRUE when the character before the first drawn character has
2590 * different attributes (may have to be redrawn too).
2591 */
2592 int
2593gui_redraw_block(row1, col1, row2, col2, flags)
2594 int row1;
2595 int col1;
2596 int row2;
2597 int col2;
2598 int flags; /* flags for gui_outstr_nowrap() */
2599{
2600 int old_row, old_col;
2601 long_u old_hl_mask;
2602 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002603 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int idx, len;
2605 int back, nback;
2606 int retval = FALSE;
2607#ifdef FEAT_MBYTE
2608 int orig_col1, orig_col2;
2609#endif
2610
2611 /* Don't try to update when ScreenLines is not valid */
2612 if (!screen_cleared || ScreenLines == NULL)
2613 return retval;
2614
2615 /* Don't try to draw outside the shell! */
2616 /* Check everything, strange values may be caused by a big border width */
2617 col1 = check_col(col1);
2618 col2 = check_col(col2);
2619 row1 = check_row(row1);
2620 row2 = check_row(row2);
2621
2622 /* Remember where our cursor was */
2623 old_row = gui.row;
2624 old_col = gui.col;
2625 old_hl_mask = gui.highlight_mask;
2626#ifdef FEAT_MBYTE
2627 orig_col1 = col1;
2628 orig_col2 = col2;
2629#endif
2630
2631 for (gui.row = row1; gui.row <= row2; gui.row++)
2632 {
2633#ifdef FEAT_MBYTE
2634 /* When only half of a double-wide character is in the block, include
2635 * the other half. */
2636 col1 = orig_col1;
2637 col2 = orig_col2;
2638 off = LineOffset[gui.row];
2639 if (enc_dbcs != 0)
2640 {
2641 if (col1 > 0)
2642 col1 -= dbcs_screen_head_off(ScreenLines + off,
2643 ScreenLines + off + col1);
2644 col2 += dbcs_screen_tail_off(ScreenLines + off,
2645 ScreenLines + off + col2);
2646 }
2647 else if (enc_utf8)
2648 {
2649 if (ScreenLines[off + col1] == 0)
2650 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002651# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2653 ++col2;
2654# endif
2655 }
2656#endif
2657 gui.col = col1;
2658 off = LineOffset[gui.row] + gui.col;
2659 len = col2 - col1 + 1;
2660
2661 /* Find how many chars back this highlighting starts, or where a space
2662 * is. Needed for when the bold trick is used */
2663 for (back = 0; back < col1; ++back)
2664 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2665 || ScreenLines[off - 1 - back] == ' ')
2666 break;
2667 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2668 && ScreenLines[off - 1] != ' ');
2669
2670 /* Break it up in strings of characters with the same attributes. */
2671 /* Print UTF-8 characters individually. */
2672 while (len > 0)
2673 {
2674 first_attr = ScreenAttrs[off];
2675 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002676#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (enc_utf8 && ScreenLinesUC[off] != 0)
2678 {
2679 /* output multi-byte character separately */
2680 nback = gui_screenchar(off, flags,
2681 (guicolor_T)0, (guicolor_T)0, back);
2682 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2683 idx = 2;
2684 else
2685 idx = 1;
2686 }
2687 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2688 {
2689 /* output double-byte, single-width character separately */
2690 nback = gui_screenchar(off, flags,
2691 (guicolor_T)0, (guicolor_T)0, back);
2692 idx = 1;
2693 }
2694 else
2695#endif
2696 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002697#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 for (idx = 0; idx < len; ++idx)
2699 {
2700 if (enc_utf8 && ScreenLines[off + idx] == 0)
2701 continue; /* skip second half of double-width char */
2702 if (ScreenAttrs[off + idx] != first_attr)
2703 break;
2704 }
2705 /* gui_screenstr() takes care of multibyte chars */
2706 nback = gui_screenstr(off, idx, flags,
2707 (guicolor_T)0, (guicolor_T)0, back);
2708#else
2709 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2710 idx++)
2711 {
2712# ifdef FEAT_MBYTE
2713 /* Stop at a multi-byte Unicode character. */
2714 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2715 break;
2716 if (enc_dbcs == DBCS_JPNU)
2717 {
2718 /* Stop at a double-byte single-width char. */
2719 if (ScreenLines[off + idx] == 0x8e)
2720 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002721 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 + off + idx) == 2)
2723 ++idx; /* skip second byte of double-byte char */
2724 }
2725# endif
2726 }
2727 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2728 (guicolor_T)0, (guicolor_T)0, back);
2729#endif
2730 }
2731 if (nback == FAIL)
2732 {
2733 /* Must back up to start drawing where a bold or italic word
2734 * starts. */
2735 off -= back;
2736 len += back;
2737 gui.col -= back;
2738 }
2739 else
2740 {
2741 off += idx;
2742 len -= idx;
2743 }
2744 back = 0;
2745 }
2746 }
2747
2748 /* Put the cursor back where it was */
2749 gui.row = old_row;
2750 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002751 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 return retval;
2754}
2755
2756 static void
2757gui_delete_lines(row, count)
2758 int row;
2759 int count;
2760{
2761 if (count <= 0)
2762 return;
2763
2764 if (row + count > gui.scroll_region_bot)
2765 /* Scrolled out of region, just blank the lines out */
2766 gui_clear_block(row, gui.scroll_region_left,
2767 gui.scroll_region_bot, gui.scroll_region_right);
2768 else
2769 {
2770 gui_mch_delete_lines(row, count);
2771
2772 /* If the cursor was in the deleted lines it's now gone. If the
2773 * cursor was in the scrolled lines adjust its position. */
2774 if (gui.cursor_row >= row
2775 && gui.cursor_col >= gui.scroll_region_left
2776 && gui.cursor_col <= gui.scroll_region_right)
2777 {
2778 if (gui.cursor_row < row + count)
2779 gui.cursor_is_valid = FALSE;
2780 else if (gui.cursor_row <= gui.scroll_region_bot)
2781 gui.cursor_row -= count;
2782 }
2783 }
2784}
2785
2786 static void
2787gui_insert_lines(row, count)
2788 int row;
2789 int count;
2790{
2791 if (count <= 0)
2792 return;
2793
2794 if (row + count > gui.scroll_region_bot)
2795 /* Scrolled out of region, just blank the lines out */
2796 gui_clear_block(row, gui.scroll_region_left,
2797 gui.scroll_region_bot, gui.scroll_region_right);
2798 else
2799 {
2800 gui_mch_insert_lines(row, count);
2801
2802 if (gui.cursor_row >= gui.row
2803 && gui.cursor_col >= gui.scroll_region_left
2804 && gui.cursor_col <= gui.scroll_region_right)
2805 {
2806 if (gui.cursor_row <= gui.scroll_region_bot - count)
2807 gui.cursor_row += count;
2808 else if (gui.cursor_row <= gui.scroll_region_bot)
2809 gui.cursor_is_valid = FALSE;
2810 }
2811 }
2812}
2813
2814/*
2815 * The main GUI input routine. Waits for a character from the keyboard.
2816 * wtime == -1 Wait forever.
2817 * wtime == 0 Don't wait.
2818 * wtime > 0 Wait wtime milliseconds for a character.
2819 * Returns OK if a character was found to be available within the given time,
2820 * or FAIL otherwise.
2821 */
2822 int
2823gui_wait_for_chars(wtime)
2824 long wtime;
2825{
2826 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002828#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 /*
2830 * If we're going to wait a bit, update the menus and mouse shape for the
2831 * current State.
2832 */
2833 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 gui_update_menus(0);
2835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
2837 gui_mch_update();
2838 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
2843 /* Before waiting, flush any output to the screen. */
2844 gui_mch_flush();
2845
2846 if (wtime > 0)
2847 {
2848 /* Blink when waiting for a character. Probably only does something
2849 * for showmatch() */
2850 gui_mch_start_blink();
2851 retval = gui_mch_wait_for_chars(wtime);
2852 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return retval;
2854 }
2855
2856 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002857 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 */
2859 gui_mch_start_blink();
2860
Bram Moolenaar3918c952005-03-15 22:34:55 +00002861 retval = FAIL;
2862 /*
2863 * We may want to trigger the CursorHold event. First wait for
2864 * 'updatetime' and if nothing is typed within that time put the
2865 * K_CURSORHOLD key in the input buffer.
2866 */
2867 if (gui_mch_wait_for_chars(p_ut) == OK)
2868 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002870 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002872 char_u buf[3];
2873
2874 /* Put K_CURSORHOLD in the input buffer. */
2875 buf[0] = CSI;
2876 buf[1] = KS_EXTRA;
2877 buf[2] = (int)KE_CURSORHOLD;
2878 add_to_input_buf(buf, 3);
2879
2880 retval = OK;
2881 }
2882#endif
2883
2884 if (retval == FAIL)
2885 {
2886 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002887 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
2891 gui_mch_stop_blink();
2892 return retval;
2893}
2894
2895/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002896 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 */
2898 static void
2899fill_mouse_coord(p, col, row)
2900 char_u *p;
2901 int col;
2902 int row;
2903{
2904 p[0] = (char_u)(col / 128 + ' ' + 1);
2905 p[1] = (char_u)(col % 128 + ' ' + 1);
2906 p[2] = (char_u)(row / 128 + ' ' + 1);
2907 p[3] = (char_u)(row % 128 + ' ' + 1);
2908}
2909
2910/*
2911 * Generic mouse support function. Add a mouse event to the input buffer with
2912 * the given properties.
2913 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2914 * MOUSE_X1, MOUSE_X2
2915 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002916 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2917 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 * x, y --- Coordinates of mouse in pixels.
2919 * repeated_click --- TRUE if this click comes only a short time after a
2920 * previous click.
2921 * modifiers --- Bit field which may be any of the following modifiers
2922 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2923 * This function will ignore drag events where the mouse has not moved to a new
2924 * character.
2925 */
2926 void
2927gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2928 int button;
2929 int x;
2930 int y;
2931 int repeated_click;
2932 int_u modifiers;
2933{
2934 static int prev_row = 0, prev_col = 0;
2935 static int prev_button = -1;
2936 static int num_clicks = 1;
2937 char_u string[10];
2938 enum key_extra button_char;
2939 int row, col;
2940#ifdef FEAT_CLIPBOARD
2941 int checkfor;
2942 int did_clip = FALSE;
2943#endif
2944
2945 /*
2946 * Scrolling may happen at any time, also while a selection is present.
2947 */
2948 switch (button)
2949 {
2950 case MOUSE_X1:
2951 button_char = KE_X1MOUSE;
2952 goto button_set;
2953 case MOUSE_X2:
2954 button_char = KE_X2MOUSE;
2955 goto button_set;
2956 case MOUSE_4:
2957 button_char = KE_MOUSEDOWN;
2958 goto button_set;
2959 case MOUSE_5:
2960 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002961 goto button_set;
2962 case MOUSE_6:
2963 button_char = KE_MOUSELEFT;
2964 goto button_set;
2965 case MOUSE_7:
2966 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967button_set:
2968 {
2969 /* Don't put events in the input queue now. */
2970 if (hold_gui_events)
2971 return;
2972
2973 string[3] = CSI;
2974 string[4] = KS_EXTRA;
2975 string[5] = (int)button_char;
2976
2977 /* Pass the pointer coordinates of the scroll event so that we
2978 * know which window to scroll. */
2979 row = gui_xy2colrow(x, y, &col);
2980 string[6] = (char_u)(col / 128 + ' ' + 1);
2981 string[7] = (char_u)(col % 128 + ' ' + 1);
2982 string[8] = (char_u)(row / 128 + ' ' + 1);
2983 string[9] = (char_u)(row % 128 + ' ' + 1);
2984
2985 if (modifiers == 0)
2986 add_to_input_buf(string + 3, 7);
2987 else
2988 {
2989 string[0] = CSI;
2990 string[1] = KS_MODIFIER;
2991 string[2] = 0;
2992 if (modifiers & MOUSE_SHIFT)
2993 string[2] |= MOD_MASK_SHIFT;
2994 if (modifiers & MOUSE_CTRL)
2995 string[2] |= MOD_MASK_CTRL;
2996 if (modifiers & MOUSE_ALT)
2997 string[2] |= MOD_MASK_ALT;
2998 add_to_input_buf(string, 10);
2999 }
3000 return;
3001 }
3002 }
3003
3004#ifdef FEAT_CLIPBOARD
3005 /* If a clipboard selection is in progress, handle it */
3006 if (clip_star.state == SELECT_IN_PROGRESS)
3007 {
3008 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3009 return;
3010 }
3011
3012 /* Determine which mouse settings to look for based on the current mode */
3013 switch (get_real_state())
3014 {
3015 case NORMAL_BUSY:
3016 case OP_PENDING:
3017 case NORMAL: checkfor = MOUSE_NORMAL; break;
3018 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003019 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 case REPLACE:
3021 case REPLACE+LANGMAP:
3022#ifdef FEAT_VREPLACE
3023 case VREPLACE:
3024 case VREPLACE+LANGMAP:
3025#endif
3026 case INSERT:
3027 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3028 case ASKMORE:
3029 case HITRETURN: /* At the more- and hit-enter prompt pass the
3030 mouse event for a click on or below the
3031 message line. */
3032 if (Y_2_ROW(y) >= msg_row)
3033 checkfor = MOUSE_NORMAL;
3034 else
3035 checkfor = MOUSE_RETURN;
3036 break;
3037
3038 /*
3039 * On the command line, use the clipboard selection on all lines
3040 * but the command line. But not when pasting.
3041 */
3042 case CMDLINE:
3043 case CMDLINE+LANGMAP:
3044 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3045 checkfor = MOUSE_NONE;
3046 else
3047 checkfor = MOUSE_COMMAND;
3048 break;
3049
3050 default:
3051 checkfor = MOUSE_NONE;
3052 break;
3053 };
3054
3055 /*
3056 * Allow clipboard selection of text on the command line in "normal"
3057 * modes. Don't do this when dragging the status line, or extending a
3058 * Visual selection.
3059 */
3060 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
3061 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00003062# ifdef FEAT_WINDOWS
3063 + firstwin->w_winrow
3064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 && button != MOUSE_DRAG
3066# ifdef FEAT_MOUSESHAPE
3067 && !drag_status_line
3068# ifdef FEAT_VERTSPLIT
3069 && !drag_sep_line
3070# endif
3071# endif
3072 )
3073 checkfor = MOUSE_NONE;
3074
3075 /*
3076 * Use modeless selection when holding CTRL and SHIFT pressed.
3077 */
3078 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3079 checkfor = MOUSE_NONEF;
3080
3081 /*
3082 * In Ex mode, always use modeless selection.
3083 */
3084 if (exmode_active)
3085 checkfor = MOUSE_NONE;
3086
3087 /*
3088 * If the mouse settings say to not use the mouse, use the modeless
3089 * selection. But if Visual is active, assume that only the Visual area
3090 * will be selected.
3091 * Exception: On the command line, both the selection is used and a mouse
3092 * key is send.
3093 */
3094 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3095 {
3096#ifdef FEAT_VISUAL
3097 /* Don't do modeless selection in Visual mode. */
3098 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3099 return;
3100#endif
3101
3102 /*
3103 * When 'mousemodel' is "popup", shift-left is translated to right.
3104 * But not when also using Ctrl.
3105 */
3106 if (mouse_model_popup() && button == MOUSE_LEFT
3107 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3108 {
3109 button = MOUSE_RIGHT;
3110 modifiers &= ~ MOUSE_SHIFT;
3111 }
3112
3113 /* If the selection is done, allow the right button to extend it.
3114 * If the selection is cleared, allow the right button to start it
3115 * from the cursor position. */
3116 if (button == MOUSE_RIGHT)
3117 {
3118 if (clip_star.state == SELECT_CLEARED)
3119 {
3120 if (State & CMDLINE)
3121 {
3122 col = msg_col;
3123 row = msg_row;
3124 }
3125 else
3126 {
3127 col = curwin->w_wcol;
3128 row = curwin->w_wrow + W_WINROW(curwin);
3129 }
3130 clip_start_selection(col, row, FALSE);
3131 }
3132 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3133 repeated_click);
3134 did_clip = TRUE;
3135 }
3136 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003137 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3140 did_clip = TRUE;
3141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 /* Always allow pasting */
3144 if (button != MOUSE_MIDDLE)
3145 {
3146 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3147 return;
3148 if (checkfor != MOUSE_COMMAND)
3149 button = MOUSE_LEFT;
3150 }
3151 repeated_click = FALSE;
3152 }
3153
3154 if (clip_star.state != SELECT_CLEARED && !did_clip)
3155 clip_clear_selection();
3156#endif
3157
3158 /* Don't put events in the input queue now. */
3159 if (hold_gui_events)
3160 return;
3161
3162 row = gui_xy2colrow(x, y, &col);
3163
3164 /*
3165 * If we are dragging and the mouse hasn't moved far enough to be on a
3166 * different character, then don't send an event to vim.
3167 */
3168 if (button == MOUSE_DRAG)
3169 {
3170 if (row == prev_row && col == prev_col)
3171 return;
3172 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3173 if (y < 0)
3174 row = -1;
3175 }
3176
3177 /*
3178 * If topline has changed (window scrolled) since the last click, reset
3179 * repeated_click, because we don't want starting Visual mode when
3180 * clicking on a different character in the text.
3181 */
3182 if (curwin->w_topline != gui_prev_topline
3183#ifdef FEAT_DIFF
3184 || curwin->w_topfill != gui_prev_topfill
3185#endif
3186 )
3187 repeated_click = FALSE;
3188
3189 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3190 string[1] = KS_MOUSE;
3191 string[2] = KE_FILLER;
3192 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3193 {
3194 if (repeated_click)
3195 {
3196 /*
3197 * Handle multiple clicks. They only count if the mouse is still
3198 * pointing at the same character.
3199 */
3200 if (button != prev_button || row != prev_row || col != prev_col)
3201 num_clicks = 1;
3202 else if (++num_clicks > 4)
3203 num_clicks = 1;
3204 }
3205 else
3206 num_clicks = 1;
3207 prev_button = button;
3208 gui_prev_topline = curwin->w_topline;
3209#ifdef FEAT_DIFF
3210 gui_prev_topfill = curwin->w_topfill;
3211#endif
3212
3213 string[3] = (char_u)(button | 0x20);
3214 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3215 }
3216 else
3217 string[3] = (char_u)button;
3218
3219 string[3] |= modifiers;
3220 fill_mouse_coord(string + 4, col, row);
3221 add_to_input_buf(string, 8);
3222
3223 if (row < 0)
3224 prev_row = 0;
3225 else
3226 prev_row = row;
3227 prev_col = col;
3228
3229 /*
3230 * We need to make sure this is cleared since Athena doesn't tell us when
3231 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3232 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003233#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 gui.dragged_sb = SBAR_NONE;
3235#endif
3236}
3237
3238/*
3239 * Convert x and y coordinate to column and row in text window.
3240 * Corrects for multi-byte character.
3241 * returns column in "*colp" and row as return value;
3242 */
3243 int
3244gui_xy2colrow(x, y, colp)
3245 int x;
3246 int y;
3247 int *colp;
3248{
3249 int col = check_col(X_2_COL(x));
3250 int row = check_row(Y_2_ROW(y));
3251
3252#ifdef FEAT_MBYTE
3253 *colp = mb_fix_col(col, row);
3254#else
3255 *colp = col;
3256#endif
3257 return row;
3258}
3259
3260#if defined(FEAT_MENU) || defined(PROTO)
3261/*
3262 * Callback function for when a menu entry has been selected.
3263 */
3264 void
3265gui_menu_cb(menu)
3266 vimmenu_T *menu;
3267{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003268 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270 /* Don't put events in the input queue now. */
3271 if (hold_gui_events)
3272 return;
3273
3274 bytes[0] = CSI;
3275 bytes[1] = KS_MENU;
3276 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003277 add_to_input_buf(bytes, 3);
3278 add_long_to_buf((long_u)menu, bytes);
3279 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281#endif
3282
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003283static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285/*
3286 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003287 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * in p_go.
3289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 void
3291gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003292 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#ifdef FEAT_MENU
3295 static int prev_menu_is_active = -1;
3296#endif
3297#ifdef FEAT_TOOLBAR
3298 static int prev_toolbar = -1;
3299 int using_toolbar = FALSE;
3300#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003301#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003302 int using_tabline;
3303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_FOOTER
3305 static int prev_footer = -1;
3306 int using_footer = FALSE;
3307#endif
3308#if defined(FEAT_MENU) && !defined(WIN16)
3309 static int prev_tearoff = -1;
3310 int using_tearoff = FALSE;
3311#endif
3312
3313 char_u *p;
3314 int i;
3315#ifdef FEAT_MENU
3316 int grey_old, grey_new;
3317 char_u *temp;
3318#endif
3319 win_T *wp;
3320 int need_set_size;
3321 int fix_size;
3322
3323#ifdef FEAT_MENU
3324 if (oldval != NULL && gui.in_use)
3325 {
3326 /*
3327 * Check if the menu's go from grey to non-grey or vise versa.
3328 */
3329 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3330 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3331 if (grey_old != grey_new)
3332 {
3333 temp = p_go;
3334 p_go = oldval;
3335 gui_update_menus(MENU_ALL_MODES);
3336 p_go = temp;
3337 }
3338 }
3339 gui.menu_is_active = FALSE;
3340#endif
3341
3342 for (i = 0; i < 3; i++)
3343 gui.which_scrollbars[i] = FALSE;
3344 for (p = p_go; *p; p++)
3345 switch (*p)
3346 {
3347 case GO_LEFT:
3348 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3349 break;
3350 case GO_RIGHT:
3351 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3352 break;
3353#ifdef FEAT_VERTSPLIT
3354 case GO_VLEFT:
3355 if (win_hasvertsplit())
3356 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3357 break;
3358 case GO_VRIGHT:
3359 if (win_hasvertsplit())
3360 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3361 break;
3362#endif
3363 case GO_BOT:
3364 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3365 break;
3366#ifdef FEAT_MENU
3367 case GO_MENUS:
3368 gui.menu_is_active = TRUE;
3369 break;
3370#endif
3371 case GO_GREY:
3372 /* make menu's have grey items, ignored here */
3373 break;
3374#ifdef FEAT_TOOLBAR
3375 case GO_TOOLBAR:
3376 using_toolbar = TRUE;
3377 break;
3378#endif
3379#ifdef FEAT_FOOTER
3380 case GO_FOOTER:
3381 using_footer = TRUE;
3382 break;
3383#endif
3384 case GO_TEAROFF:
3385#if defined(FEAT_MENU) && !defined(WIN16)
3386 using_tearoff = TRUE;
3387#endif
3388 break;
3389 default:
3390 /* Ignore options that are not supported */
3391 break;
3392 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (gui.in_use)
3395 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003396 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003398
3399#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003400 /* Update the GUI tab line, it may appear or disappear. This may
3401 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003402 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003403 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003404 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003405 /* We don't want a resize event change "Rows" here, save and
3406 * restore it. Resizing is handled below. */
3407 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003408 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003409 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003410 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003411 if (using_tabline)
3412 fix_size = TRUE;
3413 if (!gui_use_tabline())
3414 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3415 }
3416#endif
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 for (i = 0; i < 3; i++)
3419 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003420 /* The scrollbar needs to be updated when it is shown/unshown and
3421 * when switching tab pages. But the size only changes when it's
3422 * shown/unshown. Thus we need two places to remember whether a
3423 * scrollbar is there or not. */
3424 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003425#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003426 || gui.which_scrollbars[i]
3427 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003428#endif
3429 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
3431 if (i == SBAR_BOTTOM)
3432 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3433 gui.which_scrollbars[i]);
3434 else
3435 {
3436 FOR_ALL_WINDOWS(wp)
3437 {
3438 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3439 }
3440 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003441 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3442 {
3443 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003444 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003445 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003446 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003447 if (gui.which_scrollbars[i])
3448 fix_size = TRUE;
3449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003451#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003452 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003453#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003454 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456
3457#ifdef FEAT_MENU
3458 if (gui.menu_is_active != prev_menu_is_active)
3459 {
3460 /* We don't want a resize event change "Rows" here, save and
3461 * restore it. Resizing is handled below. */
3462 i = Rows;
3463 gui_mch_enable_menu(gui.menu_is_active);
3464 Rows = i;
3465 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003466 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (gui.menu_is_active)
3468 fix_size = TRUE;
3469 }
3470#endif
3471
3472#ifdef FEAT_TOOLBAR
3473 if (using_toolbar != prev_toolbar)
3474 {
3475 gui_mch_show_toolbar(using_toolbar);
3476 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003477 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (using_toolbar)
3479 fix_size = TRUE;
3480 }
3481#endif
3482#ifdef FEAT_FOOTER
3483 if (using_footer != prev_footer)
3484 {
3485 gui_mch_enable_footer(using_footer);
3486 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003487 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (using_footer)
3489 fix_size = TRUE;
3490 }
3491#endif
3492#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3493 if (using_tearoff != prev_tearoff)
3494 {
3495 gui_mch_toggle_tearoffs(using_tearoff);
3496 prev_tearoff = using_tearoff;
3497 }
3498#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003499 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003500 {
3501#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003502 long prev_Columns = Columns;
3503 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /* Adjust the size of the window to make the text area keep the
3506 * same size and to avoid that part of our window is off-screen
3507 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003508 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003509
3510#ifdef FEAT_GUI_GTK
3511 /* GTK has the annoying habit of sending us resize events when
3512 * changing the window size ourselves. This mostly happens when
3513 * waiting for a character to arrive, quite unpredictably, and may
3514 * change Columns and Rows when we don't want it. Wait for a
3515 * character here to avoid this effect.
3516 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003517 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003518 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003519 * Don't change Rows when adding menu/toolbar/tabline.
3520 * Don't change Columns when adding vertical toolbar. */
3521 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003522 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003523 if ((need_set_size & RESIZE_VERT) == 0)
3524 Rows = prev_Rows;
3525 if ((need_set_size & RESIZE_HOR) == 0)
3526 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003527#endif
3528 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003529#ifdef FEAT_WINDOWS
3530 /* When the console tabline appears or disappears the window positions
3531 * change. */
3532 if (firstwin->w_winrow != tabline_height())
3533 shell_new_rows(); /* recompute window positions and heights */
3534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536}
3537
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003538#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3539/*
3540 * Return TRUE if the GUI is taking care of the tabline.
3541 * It may still be hidden if 'showtabline' is zero.
3542 */
3543 int
3544gui_use_tabline()
3545{
3546 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3547}
3548
3549/*
3550 * Return TRUE if the GUI is showing the tabline.
3551 * This uses 'showtabline'.
3552 */
3553 static int
3554gui_has_tabline()
3555{
3556 if (!gui_use_tabline()
3557 || p_stal == 0
3558 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3559 return FALSE;
3560 return TRUE;
3561}
3562
3563/*
3564 * Update the tabline.
3565 * This may display/undisplay the tabline and update the labels.
3566 */
3567 void
3568gui_update_tabline()
3569{
3570 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003571 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003572
3573 if (!gui.starting && starting == 0)
3574 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003575 /* Updating the tabline uses direct GUI commands, flush
3576 * outstanding instructions first. (esp. clear screen) */
3577 out_flush();
3578 gui_mch_flush();
3579
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003580 if (!showit != !shown)
3581 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003582 if (showit != 0)
3583 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003584
3585 /* When the tabs change from hidden to shown or from shown to
3586 * hidden the size of the text area should remain the same. */
3587 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003588 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003589 }
3590}
3591
3592/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003593 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594 */
3595 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003596get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003597 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003598 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003599{
3600 int modified = FALSE;
3601 char_u buf[40];
3602 int wincount;
3603 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003604 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003605
Bram Moolenaar57657d82006-04-21 22:12:41 +00003606 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003607 opt = (tooltip ? &p_gtt : &p_gtl);
3608 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003609 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003610 int use_sandbox = FALSE;
3611 int save_called_emsg = called_emsg;
3612 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003613 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003614 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3615 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003616
3617 called_emsg = FALSE;
3618
3619 printer_page_num = tabpage_index(tp);
3620# ifdef FEAT_EVAL
3621 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003622 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003623# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003624 /* It's almost as going to the tabpage, but without autocommands. */
3625 curtab->tp_firstwin = firstwin;
3626 curtab->tp_lastwin = lastwin;
3627 curtab->tp_curwin = curwin;
3628 save_curtab = curtab;
3629 curtab = tp;
3630 topframe = curtab->tp_topframe;
3631 firstwin = curtab->tp_firstwin;
3632 lastwin = curtab->tp_lastwin;
3633 curwin = curtab->tp_curwin;
3634 curbuf = curwin->w_buffer;
3635
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003636 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003637 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003638 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003639 STRCPY(NameBuff, res);
3640
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003641 /* Back to the original curtab. */
3642 curtab = save_curtab;
3643 topframe = curtab->tp_topframe;
3644 firstwin = curtab->tp_firstwin;
3645 lastwin = curtab->tp_lastwin;
3646 curwin = curtab->tp_curwin;
3647 curbuf = curwin->w_buffer;
3648
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003649 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003650 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003651 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003652 called_emsg |= save_called_emsg;
3653 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003654
3655 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3656 * use a default label. */
3657 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003658 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003659 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003660 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003661 if (!tooltip)
3662 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003663
3664 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3665 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3666 if (bufIsChanged(wp->w_buffer))
3667 modified = TRUE;
3668 if (modified || wincount > 1)
3669 {
3670 if (wincount > 1)
3671 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3672 else
3673 buf[0] = NUL;
3674 if (modified)
3675 STRCAT(buf, "+");
3676 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003677 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003678 mch_memmove(NameBuff, buf, STRLEN(buf));
3679 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003680 }
3681}
3682
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003683/*
3684 * Send the event for clicking to select tab page "nr".
3685 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003686 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003687 */
3688 int
3689send_tabline_event(nr)
3690 int nr;
3691{
3692 char_u string[3];
3693
3694 if (nr == tabpage_index(curtab))
3695 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003696
3697 /* Don't put events in the input queue now. */
3698 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003699# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003700 || cmdwin_type != 0
3701# endif
3702 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003703 {
3704 /* Set it back to the current tab page. */
3705 gui_mch_set_curtab(tabpage_index(curtab));
3706 return FALSE;
3707 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003708
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003709 string[0] = CSI;
3710 string[1] = KS_TABLINE;
3711 string[2] = KE_FILLER;
3712 add_to_input_buf(string, 3);
3713 string[0] = nr;
3714 add_to_input_buf_csi(string, 1);
3715 return TRUE;
3716}
3717
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003718/*
3719 * Send a tabline menu event
3720 */
3721 void
3722send_tabline_menu_event(tabidx, event)
3723 int tabidx;
3724 int event;
3725{
3726 char_u string[3];
3727
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003728 /* Don't put events in the input queue now. */
3729 if (hold_gui_events)
3730 return;
3731
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003732 string[0] = CSI;
3733 string[1] = KS_TABMENU;
3734 string[2] = KE_FILLER;
3735 add_to_input_buf(string, 3);
3736 string[0] = tabidx;
3737 string[1] = (char_u)(long)event;
3738 add_to_input_buf_csi(string, 2);
3739}
3740
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743/*
3744 * Scrollbar stuff:
3745 */
3746
Bram Moolenaare45828b2006-02-15 22:12:56 +00003747#if defined(FEAT_WINDOWS) || defined(PROTO)
3748/*
3749 * Remove all scrollbars. Used before switching to another tab page.
3750 */
3751 void
3752gui_remove_scrollbars()
3753{
3754 int i;
3755 win_T *wp;
3756
3757 for (i = 0; i < 3; i++)
3758 {
3759 if (i == SBAR_BOTTOM)
3760 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3761 else
3762 {
3763 FOR_ALL_WINDOWS(wp)
3764 {
3765 gui_do_scrollbar(wp, i, FALSE);
3766 }
3767 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003768 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003769 }
3770}
3771#endif
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 void
3774gui_create_scrollbar(sb, type, wp)
3775 scrollbar_T *sb;
3776 int type;
3777 win_T *wp;
3778{
3779 static int sbar_ident = 0;
3780
3781 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3782 sb->wp = wp;
3783 sb->type = type;
3784 sb->value = 0;
3785#ifdef FEAT_GUI_ATHENA
3786 sb->pixval = 0;
3787#endif
3788 sb->size = 1;
3789 sb->max = 1;
3790 sb->top = 0;
3791 sb->height = 0;
3792#ifdef FEAT_VERTSPLIT
3793 sb->width = 0;
3794#endif
3795 sb->status_height = 0;
3796 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3797}
3798
3799/*
3800 * Find the scrollbar with the given index.
3801 */
3802 scrollbar_T *
3803gui_find_scrollbar(ident)
3804 long ident;
3805{
3806 win_T *wp;
3807
3808 if (gui.bottom_sbar.ident == ident)
3809 return &gui.bottom_sbar;
3810 FOR_ALL_WINDOWS(wp)
3811 {
3812 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3813 return &wp->w_scrollbars[SBAR_LEFT];
3814 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3815 return &wp->w_scrollbars[SBAR_RIGHT];
3816 }
3817 return NULL;
3818}
3819
3820/*
3821 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3822 *
3823 * For Win32, Macintosh and GTK+ 2:
3824 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3825 * you stop dragging the scrollbar. We get here each time the scrollbar is
3826 * dragged another pixel, but as far as the rest of vim goes, it thinks
3827 * we're just hanging in the call to DispatchMessage() in
3828 * process_message(). The DispatchMessage() call that hangs was passed a
3829 * mouse button click event in the scrollbar window. -- webb.
3830 *
3831 * Solution: Do the scrolling right here. But only when allowed.
3832 * Ignore the scrollbars while executing an external command or when there
3833 * are still characters to be processed.
3834 */
3835 void
3836gui_drag_scrollbar(sb, value, still_dragging)
3837 scrollbar_T *sb;
3838 long value;
3839 int still_dragging;
3840{
3841#ifdef FEAT_WINDOWS
3842 win_T *wp;
3843#endif
3844 int sb_num;
3845#ifdef USE_ON_FLY_SCROLL
3846 colnr_T old_leftcol = curwin->w_leftcol;
3847# ifdef FEAT_SCROLLBIND
3848 linenr_T old_topline = curwin->w_topline;
3849# endif
3850# ifdef FEAT_DIFF
3851 int old_topfill = curwin->w_topfill;
3852# endif
3853#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003854 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 int byte_count;
3856#endif
3857
3858 if (sb == NULL)
3859 return;
3860
3861 /* Don't put events in the input queue now. */
3862 if (hold_gui_events)
3863 return;
3864
3865#ifdef FEAT_CMDWIN
3866 if (cmdwin_type != 0 && sb->wp != curwin)
3867 return;
3868#endif
3869
3870 if (still_dragging)
3871 {
3872 if (sb->wp == NULL)
3873 gui.dragged_sb = SBAR_BOTTOM;
3874 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3875 gui.dragged_sb = SBAR_LEFT;
3876 else
3877 gui.dragged_sb = SBAR_RIGHT;
3878 gui.dragged_wp = sb->wp;
3879 }
3880 else
3881 {
3882 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003883#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 /* Keep the "dragged_wp" value until after the scrolling, for when the
3885 * moust button is released. GTK2 doesn't send the button-up event. */
3886 gui.dragged_wp = NULL;
3887#endif
3888 }
3889
3890 /* Vertical sbar info is kept in the first sbar (the left one) */
3891 if (sb->wp != NULL)
3892 sb = &sb->wp->w_scrollbars[0];
3893
3894 /*
3895 * Check validity of value
3896 */
3897 if (value < 0)
3898 value = 0;
3899#ifdef SCROLL_PAST_END
3900 else if (value > sb->max)
3901 value = sb->max;
3902#else
3903 if (value > sb->max - sb->size + 1)
3904 value = sb->max - sb->size + 1;
3905#endif
3906
3907 sb->value = value;
3908
3909#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003910 /* When not allowed to do the scrolling right now, return.
3911 * This also checked input_available(), but that causes the first click in
3912 * a scrollbar to be ignored when Vim doesn't have focus. */
3913 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return;
3915#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003916#ifdef FEAT_INS_EXPAND
3917 /* Disallow scrolling the current window when the completion popup menu is
3918 * visible. */
3919 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3920 return;
3921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923#ifdef FEAT_RIGHTLEFT
3924 if (sb->wp == NULL && curwin->w_p_rl)
3925 {
3926 value = sb->max + 1 - sb->size - value;
3927 if (value < 0)
3928 value = 0;
3929 }
3930#endif
3931
3932 if (sb->wp != NULL) /* vertical scrollbar */
3933 {
3934 sb_num = 0;
3935#ifdef FEAT_WINDOWS
3936 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3937 sb_num++;
3938 if (wp == NULL)
3939 return;
3940#else
3941 if (sb->wp != curwin)
3942 return;
3943#endif
3944
3945#ifdef USE_ON_FLY_SCROLL
3946 current_scrollbar = sb_num;
3947 scrollbar_value = value;
3948 if (State & NORMAL)
3949 {
3950 gui_do_scroll();
3951 setcursor();
3952 }
3953 else if (State & INSERT)
3954 {
3955 ins_scroll();
3956 setcursor();
3957 }
3958 else if (State & CMDLINE)
3959 {
3960 if (msg_scrolled == 0)
3961 {
3962 gui_do_scroll();
3963 redrawcmdline();
3964 }
3965 }
3966# ifdef FEAT_FOLDING
3967 /* Value may have been changed for closed fold. */
3968 sb->value = sb->wp->w_topline - 1;
3969# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003970
3971 /* When dragging one scrollbar and there is another one at the other
3972 * side move the thumb of that one too. */
3973 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3974 gui_mch_set_scrollbar_thumb(
3975 &sb->wp->w_scrollbars[
3976 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3977 ? SBAR_LEFT : SBAR_RIGHT],
3978 sb->value, sb->size, sb->max);
3979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#else
3981 bytes[0] = CSI;
3982 bytes[1] = KS_VER_SCROLLBAR;
3983 bytes[2] = KE_FILLER;
3984 bytes[3] = (char_u)sb_num;
3985 byte_count = 4;
3986#endif
3987 }
3988 else
3989 {
3990#ifdef USE_ON_FLY_SCROLL
3991 scrollbar_value = value;
3992
3993 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003994 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else if (State & INSERT)
3996 ins_horscroll();
3997 else if (State & CMDLINE)
3998 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003999 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004001 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 redrawcmdline();
4003 }
4004 }
4005 if (old_leftcol != curwin->w_leftcol)
4006 {
4007 updateWindow(curwin); /* update window, status and cmdline */
4008 setcursor();
4009 }
4010#else
4011 bytes[0] = CSI;
4012 bytes[1] = KS_HOR_SCROLLBAR;
4013 bytes[2] = KE_FILLER;
4014 byte_count = 3;
4015#endif
4016 }
4017
4018#ifdef USE_ON_FLY_SCROLL
4019# ifdef FEAT_SCROLLBIND
4020 /*
4021 * synchronize other windows, as necessary according to 'scrollbind'
4022 */
4023 if (curwin->w_p_scb
4024 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4025 || (sb->wp == curwin && (curwin->w_topline != old_topline
4026# ifdef FEAT_DIFF
4027 || curwin->w_topfill != old_topfill
4028# endif
4029 ))))
4030 {
4031 do_check_scrollbind(TRUE);
4032 /* need to update the window right here */
4033 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4034 if (wp->w_redr_type > 0)
4035 updateWindow(wp);
4036 setcursor();
4037 }
4038# endif
4039 out_flush();
4040 gui_update_cursor(FALSE, TRUE);
4041#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004042 add_to_input_buf(bytes, byte_count);
4043 add_long_to_buf((long_u)value, bytes);
4044 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045#endif
4046}
4047
4048/*
4049 * Scrollbar stuff:
4050 */
4051
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004052#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004053/*
4054 * Called when something in the window layout has changed.
4055 */
4056 void
4057gui_may_update_scrollbars()
4058{
4059 if (gui.in_use && starting == 0)
4060 {
4061 out_flush();
4062 gui_init_which_components(NULL);
4063 gui_update_scrollbars(TRUE);
4064 }
4065 need_mouse_correct = TRUE;
4066}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004067#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 void
4070gui_update_scrollbars(force)
4071 int force; /* Force all scrollbars to get updated */
4072{
4073 win_T *wp;
4074 scrollbar_T *sb;
4075 long val, size, max; /* need 32 bits here */
4076 int which_sb;
4077 int h, y;
4078#ifdef FEAT_VERTSPLIT
4079 static win_T *prev_curwin = NULL;
4080#endif
4081
4082 /* Update the horizontal scrollbar */
4083 gui_update_horiz_scrollbar(force);
4084
4085#ifndef WIN3264
4086 /* Return straight away if there is neither a left nor right scrollbar.
4087 * On MS-Windows this is required anyway for scrollwheel messages. */
4088 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4089 return;
4090#endif
4091
4092 /*
4093 * Don't want to update a scrollbar while we're dragging it. But if we
4094 * have both a left and right scrollbar, and we drag one of them, we still
4095 * need to update the other one.
4096 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004097 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4098 && gui.which_scrollbars[SBAR_LEFT]
4099 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
4101 /*
4102 * If we have two scrollbars and one of them is being dragged, just
4103 * copy the scrollbar position from the dragged one to the other one.
4104 */
4105 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4106 if (gui.dragged_wp != NULL)
4107 gui_mch_set_scrollbar_thumb(
4108 &gui.dragged_wp->w_scrollbars[which_sb],
4109 gui.dragged_wp->w_scrollbars[0].value,
4110 gui.dragged_wp->w_scrollbars[0].size,
4111 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 /* avoid that moving components around generates events */
4115 ++hold_gui_events;
4116
4117 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4118 {
4119 if (wp->w_buffer == NULL) /* just in case */
4120 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004121 /* Skip a scrollbar that is being dragged. */
4122 if (!force && (gui.dragged_sb == SBAR_LEFT
4123 || gui.dragged_sb == SBAR_RIGHT)
4124 && gui.dragged_wp == wp)
4125 continue;
4126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#ifdef SCROLL_PAST_END
4128 max = wp->w_buffer->b_ml.ml_line_count - 1;
4129#else
4130 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4131#endif
4132 if (max < 0) /* empty buffer */
4133 max = 0;
4134 val = wp->w_topline - 1;
4135 size = wp->w_height;
4136#ifdef SCROLL_PAST_END
4137 if (val > max) /* just in case */
4138 val = max;
4139#else
4140 if (size > max + 1) /* just in case */
4141 size = max + 1;
4142 if (val > max - size + 1)
4143 val = max - size + 1;
4144#endif
4145 if (val < 0) /* minimal value is 0 */
4146 val = 0;
4147
4148 /*
4149 * Scrollbar at index 0 (the left one) contains all the information.
4150 * It would be the same info for left and right so we just store it for
4151 * one of them.
4152 */
4153 sb = &wp->w_scrollbars[0];
4154
4155 /*
4156 * Note: no check for valid w_botline. If it's not valid the
4157 * scrollbars will be updated later anyway.
4158 */
4159 if (size < 1 || wp->w_botline - 2 > max)
4160 {
4161 /*
4162 * This can happen during changing files. Just don't update the
4163 * scrollbar for now.
4164 */
4165 sb->height = 0; /* Force update next time */
4166 if (gui.which_scrollbars[SBAR_LEFT])
4167 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4168 if (gui.which_scrollbars[SBAR_RIGHT])
4169 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4170 continue;
4171 }
4172 if (force || sb->height != wp->w_height
4173#ifdef FEAT_WINDOWS
4174 || sb->top != wp->w_winrow
4175 || sb->status_height != wp->w_status_height
4176# ifdef FEAT_VERTSPLIT
4177 || sb->width != wp->w_width
4178 || prev_curwin != curwin
4179# endif
4180#endif
4181 )
4182 {
4183 /* Height, width or position of scrollbar has changed. For
4184 * vertical split: curwin changed. */
4185 sb->height = wp->w_height;
4186#ifdef FEAT_WINDOWS
4187 sb->top = wp->w_winrow;
4188 sb->status_height = wp->w_status_height;
4189# ifdef FEAT_VERTSPLIT
4190 sb->width = wp->w_width;
4191# endif
4192#endif
4193
4194 /* Calculate height and position in pixels */
4195 h = (sb->height + sb->status_height) * gui.char_height;
4196 y = sb->top * gui.char_height + gui.border_offset;
4197#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4198 if (gui.menu_is_active)
4199 y += gui.menu_height;
4200#endif
4201
4202#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4203 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4204# ifdef FEAT_GUI_ATHENA
4205 y += gui.toolbar_height;
4206# else
4207# ifdef FEAT_GUI_MSWIN
4208 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4209# endif
4210# endif
4211#endif
4212
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004213#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4214 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004215 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004216#endif
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#ifdef FEAT_WINDOWS
4219 if (wp->w_winrow == 0)
4220#endif
4221 {
4222 /* Height of top scrollbar includes width of top border */
4223 h += gui.border_offset;
4224 y -= gui.border_offset;
4225 }
4226 if (gui.which_scrollbars[SBAR_LEFT])
4227 {
4228 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4229 gui.left_sbar_x, y,
4230 gui.scrollbar_width, h);
4231 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4232 }
4233 if (gui.which_scrollbars[SBAR_RIGHT])
4234 {
4235 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4236 gui.right_sbar_x, y,
4237 gui.scrollbar_width, h);
4238 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4239 }
4240 }
4241
4242 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4243 * checking if the thumb moved at least a pixel. Only do this for
4244 * Athena, most other GUIs require the update anyway to make the
4245 * arrows work. */
4246#ifdef FEAT_GUI_ATHENA
4247 if (max == 0)
4248 y = 0;
4249 else
4250 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4251 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4252#else
4253 if (force || sb->value != val || sb->size != size || sb->max != max)
4254#endif
4255 {
4256 /* Thumb of scrollbar has moved */
4257 sb->value = val;
4258#ifdef FEAT_GUI_ATHENA
4259 sb->pixval = y;
4260#endif
4261 sb->size = size;
4262 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004263 if (gui.which_scrollbars[SBAR_LEFT]
4264 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4266 val, size, max);
4267 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004268 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4270 val, size, max);
4271 }
4272 }
4273#ifdef FEAT_VERTSPLIT
4274 prev_curwin = curwin;
4275#endif
4276 --hold_gui_events;
4277}
4278
4279/*
4280 * Enable or disable a scrollbar.
4281 * Check for scrollbars for vertically split windows which are not enabled
4282 * sometimes.
4283 */
4284 static void
4285gui_do_scrollbar(wp, which, enable)
4286 win_T *wp;
4287 int which; /* SBAR_LEFT or SBAR_RIGHT */
4288 int enable; /* TRUE to enable scrollbar */
4289{
4290#ifdef FEAT_VERTSPLIT
4291 int midcol = curwin->w_wincol + curwin->w_width / 2;
4292 int has_midcol = (wp->w_wincol <= midcol
4293 && wp->w_wincol + wp->w_width >= midcol);
4294
4295 /* Only enable scrollbars that contain the middle column of the current
4296 * window. */
4297 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4298 {
4299 /* Scrollbars only on one side. Don't enable scrollbars that don't
4300 * contain the middle column of the current window. */
4301 if (!has_midcol)
4302 enable = FALSE;
4303 }
4304 else
4305 {
4306 /* Scrollbars on both sides. Don't enable scrollbars that neither
4307 * contain the middle column of the current window nor are on the far
4308 * side. */
4309 if (midcol > Columns / 2)
4310 {
4311 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4312 enable = FALSE;
4313 }
4314 else
4315 {
4316 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4317 : !has_midcol)
4318 enable = FALSE;
4319 }
4320 }
4321#endif
4322 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4323}
4324
4325/*
4326 * Scroll a window according to the values set in the globals current_scrollbar
4327 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4328 * or FALSE otherwise.
4329 */
4330 int
4331gui_do_scroll()
4332{
4333 win_T *wp, *save_wp;
4334 int i;
4335 long nlines;
4336 pos_T old_cursor;
4337 linenr_T old_topline;
4338#ifdef FEAT_DIFF
4339 int old_topfill;
4340#endif
4341
4342 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4343 if (wp == NULL)
4344 break;
4345 if (wp == NULL)
4346 /* Couldn't find window */
4347 return FALSE;
4348
4349 /*
4350 * Compute number of lines to scroll. If zero, nothing to do.
4351 */
4352 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4353 if (nlines == 0)
4354 return FALSE;
4355
4356 save_wp = curwin;
4357 old_topline = wp->w_topline;
4358#ifdef FEAT_DIFF
4359 old_topfill = wp->w_topfill;
4360#endif
4361 old_cursor = wp->w_cursor;
4362 curwin = wp;
4363 curbuf = wp->w_buffer;
4364 if (nlines < 0)
4365 scrolldown(-nlines, gui.dragged_wp == NULL);
4366 else
4367 scrollup(nlines, gui.dragged_wp == NULL);
4368 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4369 * the mouse-up event already, but we still want it to behave like when
4370 * dragging. But not the next click in an arrow. */
4371 if (gui.dragged_sb == SBAR_NONE)
4372 gui.dragged_wp = NULL;
4373
4374 if (old_topline != wp->w_topline
4375#ifdef FEAT_DIFF
4376 || old_topfill != wp->w_topfill
4377#endif
4378 )
4379 {
4380 if (p_so != 0)
4381 {
4382 cursor_correct(); /* fix window for 'so' */
4383 update_topline(); /* avoid up/down jump */
4384 }
4385 if (old_cursor.lnum != wp->w_cursor.lnum)
4386 coladvance(wp->w_curswant);
4387#ifdef FEAT_SCROLLBIND
4388 wp->w_scbind_pos = wp->w_topline;
4389#endif
4390 }
4391
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004392 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4393 validate_cursor();
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 curwin = save_wp;
4396 curbuf = save_wp->w_buffer;
4397
4398 /*
4399 * Don't call updateWindow() when nothing has changed (it will overwrite
4400 * the status line!).
4401 */
4402 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004403 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#ifdef FEAT_DIFF
4405 || old_topfill != wp->w_topfill
4406#endif
4407 )
4408 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004409 int type = VALID;
4410
4411#ifdef FEAT_INS_EXPAND
4412 if (pum_visible())
4413 {
4414 type = NOT_VALID;
4415 wp->w_lines_valid = 0;
4416 }
4417#endif
4418 /* Don't set must_redraw here, it may cause the popup menu to
4419 * disappear when losing focus after a scrollbar drag. */
4420 if (wp->w_redr_type < type)
4421 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 updateWindow(wp); /* update window, status line, and cmdline */
4423 }
4424
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004425#ifdef FEAT_INS_EXPAND
4426 /* May need to redraw the popup menu. */
4427 if (pum_visible())
4428 pum_redraw();
4429#endif
4430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4432}
4433
4434
4435/*
4436 * Horizontal scrollbar stuff:
4437 */
4438
4439/*
4440 * Return length of line "lnum" for horizontal scrolling.
4441 */
4442 static colnr_T
4443scroll_line_len(lnum)
4444 linenr_T lnum;
4445{
4446 char_u *p;
4447 colnr_T col;
4448 int w;
4449
4450 p = ml_get(lnum);
4451 col = 0;
4452 if (*p != NUL)
4453 for (;;)
4454 {
4455 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004456 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (*p == NUL) /* don't count the last character */
4458 break;
4459 col += w;
4460 }
4461 return col;
4462}
4463
4464/* Remember which line is currently the longest, so that we don't have to
4465 * search for it when scrolling horizontally. */
4466static linenr_T longest_lnum = 0;
4467
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004468/*
4469 * Find longest visible line number. If this is not possible (or not desired,
4470 * by setting 'h' in "guioptions") then the current line number is returned.
4471 */
4472 static linenr_T
4473gui_find_longest_lnum()
4474{
4475 linenr_T ret = 0;
4476
4477 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4478 * line numbers, topline and botline can be invalid when displaying is
4479 * postponed. */
4480 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4481 && curwin->w_topline <= curwin->w_cursor.lnum
4482 && curwin->w_botline > curwin->w_cursor.lnum
4483 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4484 {
4485 linenr_T lnum;
4486 colnr_T n;
4487 long max = 0;
4488
4489 /* Use maximum of all visible lines. Remember the lnum of the
4490 * longest line, closest to the cursor line. Used when scrolling
4491 * below. */
4492 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4493 {
4494 n = scroll_line_len(lnum);
4495 if (n > (colnr_T)max)
4496 {
4497 max = n;
4498 ret = lnum;
4499 }
4500 else if (n == (colnr_T)max
4501 && abs((int)(lnum - curwin->w_cursor.lnum))
4502 < abs((int)(ret - curwin->w_cursor.lnum)))
4503 ret = lnum;
4504 }
4505 }
4506 else
4507 /* Use cursor line only. */
4508 ret = curwin->w_cursor.lnum;
4509
4510 return ret;
4511}
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 static void
4514gui_update_horiz_scrollbar(force)
4515 int force;
4516{
4517 long value, size, max; /* need 32 bit ints here */
4518
4519 if (!gui.which_scrollbars[SBAR_BOTTOM])
4520 return;
4521
4522 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4523 return;
4524
4525 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4526 return;
4527
4528 /*
4529 * It is possible for the cursor to be invalid if we're in the middle of
4530 * something (like changing files). If so, don't do anything for now.
4531 */
4532 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4533 {
4534 gui.bottom_sbar.value = -1;
4535 return;
4536 }
4537
4538 size = W_WIDTH(curwin);
4539 if (curwin->w_p_wrap)
4540 {
4541 value = 0;
4542#ifdef SCROLL_PAST_END
4543 max = 0;
4544#else
4545 max = W_WIDTH(curwin) - 1;
4546#endif
4547 }
4548 else
4549 {
4550 value = curwin->w_leftcol;
4551
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004552 longest_lnum = gui_find_longest_lnum();
4553 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#ifdef FEAT_VIRTUALEDIT
4556 if (virtual_active())
4557 {
4558 /* May move the cursor even further to the right. */
4559 if (curwin->w_virtcol >= (colnr_T)max)
4560 max = curwin->w_virtcol;
4561 }
4562#endif
4563
4564#ifndef SCROLL_PAST_END
4565 max += W_WIDTH(curwin) - 1;
4566#endif
4567 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004568 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 size -= curwin_col_off();
4570#ifndef SCROLL_PAST_END
4571 max -= curwin_col_off();
4572#endif
4573 }
4574
4575#ifndef SCROLL_PAST_END
4576 if (value > max - size + 1)
4577 value = max - size + 1; /* limit the value to allowable range */
4578#endif
4579
4580#ifdef FEAT_RIGHTLEFT
4581 if (curwin->w_p_rl)
4582 {
4583 value = max + 1 - size - value;
4584 if (value < 0)
4585 {
4586 size += value;
4587 value = 0;
4588 }
4589 }
4590#endif
4591 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4592 && max == gui.bottom_sbar.max)
4593 return;
4594
4595 gui.bottom_sbar.value = value;
4596 gui.bottom_sbar.size = size;
4597 gui.bottom_sbar.max = max;
4598 gui.prev_wrap = curwin->w_p_wrap;
4599
4600 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4601}
4602
4603/*
4604 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4605 */
4606 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004607gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004608 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 /* no wrapping, no scrolling */
4612 if (curwin->w_p_wrap)
4613 return FALSE;
4614
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004615 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 return FALSE;
4617
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004618 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004621 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004623 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004624 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004626 if (compute_longest_lnum)
4627 {
4628 curwin->w_cursor.lnum = gui_find_longest_lnum();
4629 curwin->w_cursor.col = 0;
4630 }
4631 /* Do a sanity check on "longest_lnum", just in case. */
4632 else if (longest_lnum >= curwin->w_topline
4633 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 curwin->w_cursor.lnum = longest_lnum;
4636 curwin->w_cursor.col = 0;
4637 }
4638 }
4639
4640 return leftcol_changed();
4641}
4642
4643/*
4644 * Check that none of the colors are the same as the background color
4645 */
4646 void
4647gui_check_colors()
4648{
4649 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4650 {
4651 gui_set_bg_color((char_u *)"White");
4652 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4653 gui_set_fg_color((char_u *)"Black");
4654 }
4655}
4656
Bram Moolenaar3918c952005-03-15 22:34:55 +00004657 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658gui_set_fg_color(name)
4659 char_u *name;
4660{
4661 gui.norm_pixel = gui_get_color(name);
4662 hl_set_fg_color_name(vim_strsave(name));
4663}
4664
Bram Moolenaar3918c952005-03-15 22:34:55 +00004665 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666gui_set_bg_color(name)
4667 char_u *name;
4668{
4669 gui.back_pixel = gui_get_color(name);
4670 hl_set_bg_color_name(vim_strsave(name));
4671}
4672
4673/*
4674 * Allocate a color by name.
4675 * Returns INVALCOLOR and gives an error message when failed.
4676 */
4677 guicolor_T
4678gui_get_color(name)
4679 char_u *name;
4680{
4681 guicolor_T t;
4682
4683 if (*name == NUL)
4684 return INVALCOLOR;
4685 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004688#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 && gui.in_use
4690#endif
4691 )
4692 EMSG2(_("E254: Cannot allocate color %s"), name);
4693 return t;
4694}
4695
4696/*
4697 * Return the grey value of a color (range 0-255).
4698 */
4699 int
4700gui_get_lightness(pixel)
4701 guicolor_T pixel;
4702{
4703 long_u rgb = gui_mch_get_rgb(pixel);
4704
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004705 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004706 + (((rgb >> 8) & 0xff) * 587)
4707 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708}
4709
4710#if defined(FEAT_GUI_X11) || defined(PROTO)
4711 void
4712gui_new_scrollbar_colors()
4713{
4714 win_T *wp;
4715
4716 /* Nothing to do if GUI hasn't started yet. */
4717 if (!gui.in_use)
4718 return;
4719
4720 FOR_ALL_WINDOWS(wp)
4721 {
4722 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4723 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4724 }
4725 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4726}
4727#endif
4728
4729/*
4730 * Call this when focus has changed.
4731 */
4732 void
4733gui_focus_change(in_focus)
4734 int in_focus;
4735{
4736/*
4737 * Skip this code to avoid drawing the cursor when debugging and switching
4738 * between the debugger window and gvim.
4739 */
4740#if 1
4741 gui.in_focus = in_focus;
4742 out_flush(); /* make sure output has been written */
4743 gui_update_cursor(TRUE, FALSE);
4744
4745# ifdef FEAT_XIM
4746 xim_set_focus(in_focus);
4747# endif
4748
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004749 /* Put events in the input queue only when allowed.
4750 * ui_focus_change() isn't called directly, because it invokes
4751 * autocommands and that must not happen asynchronously. */
4752 if (!hold_gui_events)
4753 {
4754 char_u bytes[3];
4755
4756 bytes[0] = CSI;
4757 bytes[1] = KS_EXTRA;
4758 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4759 add_to_input_buf(bytes, 3);
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762}
4763
4764/*
4765 * Called when the mouse moved (but not when dragging).
4766 */
4767 void
4768gui_mouse_moved(x, y)
4769 int x;
4770 int y;
4771{
4772 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004773 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaard3667a22006-03-16 21:35:52 +00004775 /* Ignore this while still starting up. */
4776 if (!gui.in_use || gui.starting)
4777 return;
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779#ifdef FEAT_MOUSESHAPE
4780 /* Get window pointer, and update mouse shape as well. */
4781 wp = xy2win(x, y);
4782#endif
4783
4784 /* Only handle this when 'mousefocus' set and ... */
4785 if (p_mousef
4786 && !hold_gui_events /* not holding events */
4787 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4788 && State != HITRETURN /* but not hit-return prompt */
4789 && msg_scrolled == 0 /* no scrolled message */
4790 && !need_mouse_correct /* not moving the pointer */
4791 && gui.in_focus) /* gvim in focus */
4792 {
4793 /* Don't move the mouse when it's left or right of the Vim window */
4794 if (x < 0 || x > Columns * gui.char_width)
4795 return;
4796#ifndef FEAT_MOUSESHAPE
4797 wp = xy2win(x, y);
4798#endif
4799 if (wp == curwin || wp == NULL)
4800 return; /* still in the same old window, or none at all */
4801
Bram Moolenaar9c102382006-05-03 21:26:49 +00004802#ifdef FEAT_WINDOWS
4803 /* Ignore position in the tab pages line. */
4804 if (Y_2_ROW(y) < tabline_height())
4805 return;
4806#endif
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /*
4809 * format a mouse click on status line input
4810 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004811 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4812 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 */
4814 if (finish_op)
4815 {
4816 /* abort the current operator first */
4817 st[0] = ESC;
4818 add_to_input_buf(st, 1);
4819 }
4820 st[0] = CSI;
4821 st[1] = KS_MOUSE;
4822 st[2] = KE_FILLER;
4823 st[3] = (char_u)MOUSE_LEFT;
4824 fill_mouse_coord(st + 4,
4825#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004826 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#else
4828 -1,
4829#endif
4830 wp->w_height + W_WINROW(wp));
4831
4832 add_to_input_buf(st, 8);
4833 st[3] = (char_u)MOUSE_RELEASE;
4834 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#ifdef FEAT_GUI_GTK
4836 /* Need to wake up the main loop */
4837 if (gtk_main_level() > 0)
4838 gtk_main_quit();
4839#endif
4840 }
4841}
4842
4843/*
4844 * Called when mouse should be moved to window with focus.
4845 */
4846 void
4847gui_mouse_correct()
4848{
4849 int x, y;
4850 win_T *wp = NULL;
4851
4852 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004853
4854 if (!(gui.in_use && p_mousef))
4855 return;
4856
4857 gui_mch_getmouse(&x, &y);
4858 /* Don't move the mouse when it's left or right of the Vim window */
4859 if (x < 0 || x > Columns * gui.char_width)
4860 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004861 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004862# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004863 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004864# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004865 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004866 wp = xy2win(x, y);
4867 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004869 validate_cline_row();
4870 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4871 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874}
4875
4876/*
4877 * Find window where the mouse pointer "y" coordinate is in.
4878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 static win_T *
4880xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004881 int x UNUSED;
4882 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884#ifdef FEAT_WINDOWS
4885 int row;
4886 int col;
4887 win_T *wp;
4888
4889 row = Y_2_ROW(y);
4890 col = X_2_COL(x);
4891 if (row < 0 || col < 0) /* before first window */
4892 return NULL;
4893 wp = mouse_find_win(&row, &col);
4894# ifdef FEAT_MOUSESHAPE
4895 if (State == HITRETURN || State == ASKMORE)
4896 {
4897 if (Y_2_ROW(y) >= msg_row)
4898 update_mouseshape(SHAPE_IDX_MOREL);
4899 else
4900 update_mouseshape(SHAPE_IDX_MORE);
4901 }
4902 else if (row > wp->w_height) /* below status line */
4903 update_mouseshape(SHAPE_IDX_CLINE);
4904# ifdef FEAT_VERTSPLIT
4905 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004906 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 update_mouseshape(SHAPE_IDX_VSEP);
4908# endif
4909 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004910 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 update_mouseshape(SHAPE_IDX_STATUS);
4912 else
4913 update_mouseshape(-2);
4914# endif
4915 return wp;
4916#else
4917 return firstwin;
4918#endif
4919}
4920
4921/*
4922 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4923 * File names may be given to redefine the args list.
4924 */
4925 void
4926ex_gui(eap)
4927 exarg_T *eap;
4928{
4929 char_u *arg = eap->arg;
4930
4931 /*
4932 * Check for "-f" argument: foreground, don't fork.
4933 * Also don't fork when started with "gvim -f".
4934 * Do fork when using "gui -b".
4935 */
4936 if (arg[0] == '-'
4937 && (arg[1] == 'f' || arg[1] == 'b')
4938 && (arg[2] == NUL || vim_iswhite(arg[2])))
4939 {
4940 gui.dofork = (arg[1] == 'b');
4941 eap->arg = skipwhite(eap->arg + 2);
4942 }
4943 if (!gui.in_use)
4944 {
4945 /* Clear the command. Needed for when forking+exiting, to avoid part
4946 * of the argument ending up after the shell prompt. */
4947 msg_clr_eos_force();
4948 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004949#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004950 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 if (!ends_excmd(*eap->arg))
4954 ex_next(eap);
4955}
4956
4957#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004958 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959/*
4960 * This is shared between Athena, Motif and GTK.
4961 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004962static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964/*
4965 * Callback function for do_in_runtimepath().
4966 */
4967 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004968gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004970 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004972 char_u *gfp_buffer = cookie;
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (STRLEN(fname) >= MAXPATHL)
4975 *gfp_buffer = NUL;
4976 else
4977 STRCPY(gfp_buffer, fname);
4978}
4979
4980/*
4981 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4982 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4983 */
4984 int
4985gui_find_bitmap(name, buffer, ext)
4986 char_u *name;
4987 char_u *buffer;
4988 char *ext;
4989{
4990 if (STRLEN(name) > MAXPATHL - 14)
4991 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004992 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004993 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4994 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 return FAIL;
4996 return OK;
4997}
4998
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004999# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000/*
5001 * Given the name of the "icon=" argument, try finding the bitmap file for the
5002 * icon. If it is an absolute path name, use it as it is. Otherwise append
5003 * "ext" and search for it in 'runtimepath'.
5004 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5005 * contains "name".
5006 */
5007 void
5008gui_find_iconfile(name, buffer, ext)
5009 char_u *name;
5010 char_u *buffer;
5011 char *ext;
5012{
5013 char_u buf[MAXPATHL + 1];
5014
5015 expand_env(name, buffer, MAXPATHL);
5016 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5017 STRCPY(buffer, buf);
5018}
5019# endif
5020#endif
5021
Bram Moolenaar9372a112005-12-06 19:59:18 +00005022#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 void
5024display_errors()
5025{
5026 char_u *p;
5027
5028 if (isatty(2))
5029 fflush(stderr);
5030 else if (error_ga.ga_data != NULL)
5031 {
5032 /* avoid putting up a message box with blanks only */
5033 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5034 if (!isspace(*p))
5035 {
5036 /* Truncate a very long message, it will go off-screen. */
5037 if (STRLEN(p) > 2000)
5038 STRCPY(p + 2000 - 14, "...(truncated)");
5039 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005040 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
5042 }
5043 ga_clear(&error_ga);
5044 }
5045}
5046#endif
5047
5048#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5049/*
5050 * Return TRUE if still starting up and there is no place to enter text.
5051 * For GTK and X11 we check if stderr is not a tty, which means we were
5052 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5053 * allow typing on stdin.
5054 */
5055 int
5056no_console_input()
5057{
5058 return ((!gui.in_use || gui.starting)
5059# ifndef NO_CONSOLE
5060 && !isatty(0) && !isatty(2)
5061# endif
5062 );
5063}
5064#endif
5065
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005066#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005067 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005068 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069/*
5070 * Update the current window and the screen.
5071 */
5072 void
5073gui_update_screen()
5074{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005075#ifdef FEAT_CONCEAL
5076 linenr_T conceal_old_cursor_line = 0;
5077 linenr_T conceal_new_cursor_line = 0;
5078 int conceal_update_lines = FALSE;
5079#endif
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 update_topline();
5082 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005083
5084#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005085 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005086 if (!finish_op && (
5087# ifdef FEAT_AUTOCMD
5088 has_cursormoved()
5089# endif
5090# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5091 ||
5092# endif
5093# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005094 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005095# endif
5096 )
5097 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005098 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005099# ifdef FEAT_AUTOCMD
5100 if (has_cursormoved())
5101 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5102# endif
5103# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005104 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005105 {
5106 conceal_old_cursor_line = last_cursormoved.lnum;
5107 conceal_new_cursor_line = curwin->w_cursor.lnum;
5108 conceal_update_lines = TRUE;
5109 }
5110# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005111 last_cursormoved = curwin->w_cursor;
5112 }
5113#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 update_screen(0); /* may need to update the screen */
5116 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005117# if defined(FEAT_CONCEAL)
5118 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005119 && (conceal_old_cursor_line != conceal_new_cursor_line
5120 || conceal_cursor_line(curwin)
5121 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005122 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005123 if (conceal_old_cursor_line != conceal_new_cursor_line)
5124 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005125 update_single_line(curwin, conceal_new_cursor_line);
5126 curwin->w_valid &= ~VALID_CROW;
5127 }
5128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 out_flush(); /* make sure output has been written */
5130 gui_update_cursor(TRUE, FALSE);
5131 gui_mch_flush();
5132}
5133#endif
5134
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005135#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5137
5138/*
5139 * Get the text to use in a find/replace dialog. Uses the last search pattern
5140 * if the argument is empty.
5141 * Returns an allocated string.
5142 */
5143 char_u *
5144get_find_dialog_text(arg, wwordp, mcasep)
5145 char_u *arg;
5146 int *wwordp; /* return: TRUE if \< \> found */
5147 int *mcasep; /* return: TRUE if \C found */
5148{
5149 char_u *text;
5150
5151 if (*arg == NUL)
5152 text = last_search_pat();
5153 else
5154 text = arg;
5155 if (text != NULL)
5156 {
5157 text = vim_strsave(text);
5158 if (text != NULL)
5159 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005160 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 int i;
5162
5163 /* Remove "\V" */
5164 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5165 {
5166 mch_memmove(text, text + 2, (size_t)(len - 1));
5167 len -= 2;
5168 }
5169
5170 /* Recognize "\c" and "\C" and remove. */
5171 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5172 {
5173 *mcasep = (text[1] == 'C');
5174 mch_memmove(text, text + 2, (size_t)(len - 1));
5175 len -= 2;
5176 }
5177
5178 /* Recognize "\<text\>" and remove. */
5179 if (len >= 4
5180 && STRNCMP(text, "\\<", 2) == 0
5181 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5182 {
5183 *wwordp = TRUE;
5184 mch_memmove(text, text + 2, (size_t)(len - 4));
5185 text[len - 4] = NUL;
5186 }
5187
5188 /* Recognize "\/" or "\?" and remove. */
5189 for (i = 0; i + 1 < len; ++i)
5190 if (text[i] == '\\' && (text[i + 1] == '/'
5191 || text[i + 1] == '?'))
5192 {
5193 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5194 --len;
5195 }
5196 }
5197 }
5198 return text;
5199}
5200
5201/*
5202 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5203 */
5204 static void
5205concat_esc(gap, text, what)
5206 garray_T *gap;
5207 char_u *text;
5208 int what;
5209{
5210 while (*text != NUL)
5211 {
5212#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005213 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (l > 1)
5216 {
5217 while (--l >= 0)
5218 ga_append(gap, *text++);
5219 continue;
5220 }
5221#endif
5222 if (*text == what)
5223 ga_append(gap, '\\');
5224 ga_append(gap, *text);
5225 ++text;
5226 }
5227}
5228
5229/*
5230 * Handle the press of a button in the find-replace dialog.
5231 * Return TRUE when something was added to the input buffer.
5232 */
5233 int
5234gui_do_findrepl(flags, find_text, repl_text, down)
5235 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5236 char_u *find_text;
5237 char_u *repl_text;
5238 int down; /* Search downwards. */
5239{
5240 garray_T ga;
5241 int i;
5242 int type = (flags & FRD_TYPE_MASK);
5243 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005244 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005245 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005246 static int busy = FALSE;
5247
5248 /* When the screen is being updated we should not change buffers and
5249 * windows structures, it may cause freed memory to be used. Also don't
5250 * do this recursively (pressing "Find" quickly several times. */
5251 if (updating_screen || busy)
5252 return FALSE;
5253
5254 /* refuse replace when text cannot be changed */
5255 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5256 return FALSE;
5257
5258 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005261 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 ga_concat(&ga, (char_u *)"%s/");
5263
5264 ga_concat(&ga, (char_u *)"\\V");
5265 if (flags & FRD_MATCH_CASE)
5266 ga_concat(&ga, (char_u *)"\\C");
5267 else
5268 ga_concat(&ga, (char_u *)"\\c");
5269 if (flags & FRD_WHOLE_WORD)
5270 ga_concat(&ga, (char_u *)"\\<");
5271 if (type == FRD_REPLACEALL || down)
5272 concat_esc(&ga, find_text, '/'); /* escape slashes */
5273 else
5274 concat_esc(&ga, find_text, '?'); /* escape '?' */
5275 if (flags & FRD_WHOLE_WORD)
5276 ga_concat(&ga, (char_u *)"\\>");
5277
5278 if (type == FRD_REPLACEALL)
5279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005281 /* escape / and \ */
5282 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5283 if (p != NULL)
5284 ga_concat(&ga, p);
5285 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005287 }
5288 ga_append(&ga, NUL);
5289
5290 if (type == FRD_REPLACE)
5291 {
5292 /* Do the replacement when the text at the cursor matches. Thus no
5293 * replacement is done if the cursor was moved! */
5294 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5295 regmatch.rm_ic = 0;
5296 if (regmatch.regprog != NULL)
5297 {
5298 p = ml_get_cursor();
5299 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5300 && regmatch.startp[0] == p)
5301 {
5302 /* Clear the command line to remove any old "No match"
5303 * error. */
5304 msg_end_prompt();
5305
5306 if (u_save_cursor() == OK)
5307 {
5308 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005309 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005310
5311 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005312 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005313 ins_str(repl_text);
5314 }
5315 }
5316 else
5317 MSG(_("No match at cursor, finding next"));
5318 vim_free(regmatch.regprog);
5319 }
5320 }
5321
5322 if (type == FRD_REPLACEALL)
5323 {
5324 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005325 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 do_cmdline_cmd(ga.ga_data);
5327 }
5328 else
5329 {
5330 /* Search for the next match. */
5331 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005333 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 msg_scroll = i; /* don't let an error message set msg_scroll */
5335 }
5336
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005337 /* Don't want to pass did_emsg to other code, it may cause disabling
5338 * syntax HL if we were busy redrawing. */
5339 did_emsg = save_did_emsg;
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (State & (NORMAL | INSERT))
5342 {
5343 gui_update_screen(); /* update the screen */
5344 msg_didout = 0; /* overwrite any message */
5345 need_wait_return = FALSE; /* don't wait for return */
5346 }
5347
5348 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005349 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return (ga.ga_len > 0);
5351}
5352
5353#endif
5354
5355#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5356 || defined(FEAT_GUI_MSWIN) \
5357 || defined(FEAT_GUI_MAC) \
5358 || defined(PROTO)
5359
5360#ifdef FEAT_WINDOWS
5361static void gui_wingoto_xy __ARGS((int x, int y));
5362
5363/*
5364 * Jump to the window at specified point (x, y).
5365 */
5366 static void
5367gui_wingoto_xy(x, y)
5368 int x;
5369 int y;
5370{
5371 int row = Y_2_ROW(y);
5372 int col = X_2_COL(x);
5373 win_T *wp;
5374
5375 if (row >= 0 && col >= 0)
5376 {
5377 wp = mouse_find_win(&row, &col);
5378 if (wp != NULL && wp != curwin)
5379 win_goto(wp);
5380 }
5381}
5382#endif
5383
5384/*
5385 * Process file drop. Mouse cursor position, key modifiers, name of files
5386 * and count of files are given. Argument "fnames[count]" has full pathnames
5387 * of dropped files, they will be freed in this function, and caller can't use
5388 * fnames after call this function.
5389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 void
5391gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005392 int x UNUSED;
5393 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 int_u modifiers;
5395 char_u **fnames;
5396 int count;
5397{
5398 int i;
5399 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005400 static int entered = FALSE;
5401
5402 /*
5403 * This function is called by event handlers. Just in case we get a
5404 * second event before the first one is handled, ignore the second one.
5405 * Not sure if this can ever happen, just in case.
5406 */
5407 if (entered)
5408 return;
5409 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 /*
5412 * When the cursor is at the command line, add the file names to the
5413 * command line, don't edit the files.
5414 */
5415 if (State & CMDLINE)
5416 {
5417 shorten_filenames(fnames, count);
5418 for (i = 0; i < count; ++i)
5419 {
5420 if (fnames[i] != NULL)
5421 {
5422 if (i > 0)
5423 add_to_input_buf((char_u*)" ", 1);
5424
5425 /* We don't know what command is used thus we can't be sure
5426 * about which characters need to be escaped. Only escape the
5427 * most common ones. */
5428# ifdef BACKSLASH_IN_FILENAME
5429 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5430# else
5431 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5432# endif
5433 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005434 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 vim_free(p);
5436 vim_free(fnames[i]);
5437 }
5438 }
5439 vim_free(fnames);
5440 }
5441 else
5442 {
5443 /* Go to the window under mouse cursor, then shorten given "fnames" by
5444 * current window, because a window can have local current dir. */
5445# ifdef FEAT_WINDOWS
5446 gui_wingoto_xy(x, y);
5447# endif
5448 shorten_filenames(fnames, count);
5449
5450 /* If Shift held down, remember the first item. */
5451 if ((modifiers & MOUSE_SHIFT) != 0)
5452 p = vim_strsave(fnames[0]);
5453 else
5454 p = NULL;
5455
5456 /* Handle the drop, :edit or :split to get to the file. This also
5457 * frees fnames[]. Skip this if there is only one item it's a
5458 * directory and Shift is held down. */
5459 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5460 && mch_isdir(fnames[0]))
5461 {
5462 vim_free(fnames[0]);
5463 vim_free(fnames);
5464 }
5465 else
5466 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5467
5468 /* If Shift held down, change to first file's directory. If the first
5469 * item is a directory, change to that directory (and let the explorer
5470 * plugin show the contents). */
5471 if (p != NULL)
5472 {
5473 if (mch_isdir(p))
5474 {
5475 if (mch_chdir((char *)p) == 0)
5476 shorten_fnames(TRUE);
5477 }
5478 else if (vim_chdirfile(p) == OK)
5479 shorten_fnames(TRUE);
5480 vim_free(p);
5481 }
5482
5483 /* Update the screen display */
5484 update_screen(NOT_VALID);
5485# ifdef FEAT_MENU
5486 gui_update_menus(0);
5487# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005488#ifdef FEAT_TITLE
5489 maketitle();
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 setcursor();
5492 out_flush();
5493 gui_update_cursor(FALSE, FALSE);
5494 gui_mch_flush();
5495 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005496
5497 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498}
5499#endif