blob: b216121cfe9a002f073421fadd01ebc484c93eee [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static void set_guifontwide __ARGS((char_u *font_name));
18#endif
19static void gui_check_pos __ARGS((void));
20static void gui_position_components __ARGS((int));
21static void gui_outstr __ARGS((char_u *, int));
22static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020023#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +000024static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25#endif
26static void gui_delete_lines __ARGS((int row, int count));
27static void gui_insert_lines __ARGS((int row, int count));
28static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30static int gui_has_tabline __ARGS((void));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33static colnr_T scroll_line_len __ARGS((linenr_T lnum));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020034static linenr_T gui_find_longest_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static void gui_update_horiz_scrollbar __ARGS((int));
Bram Moolenaar3918c952005-03-15 22:34:55 +000036static void gui_set_fg_color __ARGS((char_u *name));
37static void gui_set_bg_color __ARGS((char_u *name));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static win_T *xy2win __ARGS((int x, int y));
39
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020040#if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X) \
41 && !defined(__APPLE__)
42# define MAY_FORK
43static void gui_do_fork __ARGS((void));
44
45static int gui_read_child_pipe __ARGS((int fd));
46
47/* Return values for gui_read_child_pipe */
48enum {
49 GUI_CHILD_IO_ERROR,
50 GUI_CHILD_OK,
51 GUI_CHILD_FAILED
52};
53
54#endif /* MAY_FORK */
55
56static void gui_attempt_start __ARGS((void));
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static int can_update_cursor = TRUE; /* can display the cursor */
59
60/*
61 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
62 * this makes the thumb indicate the part of the text that is shown. Motif
63 * can't do this.
64 */
65#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
66# define SCROLL_PAST_END
67#endif
68
69/*
70 * gui_start -- Called when user wants to start the GUI.
71 *
72 * Careful: This function can be called recursively when there is a ":gui"
73 * command in the .gvimrc file. Only the first call should fork, not the
74 * recursive call.
75 */
76 void
77gui_start()
78{
79 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 static int recursive = 0;
81
82 old_term = vim_strsave(T_NAME);
83
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 settmode(TMODE_COOK); /* stop RAW mode */
85 if (full_screen)
86 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 full_screen = FALSE;
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 ++recursive;
90
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020091#ifdef MAY_FORK
92 /*
93 * Quit the current process and continue in the child.
94 * Makes "gvim file" disconnect from the shell it was started in.
95 * Don't do this when Vim was started with "-f" or the 'f' flag is present
96 * in 'guioptions'.
97 */
98 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
99 {
100 gui_do_fork();
101 }
102 else
103#endif
104 {
105 gui_attempt_start();
106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108 if (!gui.in_use) /* failed to start GUI */
109 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200110 /* Back to old term settings
111 *
112 * FIXME: If we got here because a child process failed and flagged to
113 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
114 * hit an X11 I/O error and do a longjmp(), leaving recursive
115 * permanently set to 1. This is probably not as big a problem as it
116 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
117 * return "OK" unconditionally, so it would be very difficult to
118 * actually hit this case.
119 */
120 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 settmode(TMODE_RAW); /* restart RAW mode */
122#ifdef FEAT_TITLE
123 set_title_defaults(); /* set 'title' and 'icon' again */
124#endif
125 }
126
127 vim_free(old_term);
128
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200129#ifdef FEAT_AUTOCMD
130 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
131 * the GUIFailed event. */
132 gui_mch_update();
133 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
134 NULL, NULL, FALSE, curbuf);
135#endif
136 --recursive;
137}
138
139/*
140 * Set_termname() will call gui_init() to start the GUI.
141 * Set the "starting" flag, to indicate that the GUI will start.
142 *
143 * We don't want to open the GUI shell until after we've read .gvimrc,
144 * otherwise we don't know what font we will use, and hence we don't know
145 * what size the shell should be. So if there are errors in the .gvimrc
146 * file, they will have to go to the terminal: Set full_screen to FALSE.
147 * full_screen will be set to TRUE again by a successful termcapinit().
148 */
149 static void
150gui_attempt_start()
151{
152 static int recursive = 0;
153
154 ++recursive;
155 gui.starting = TRUE;
156
157#ifdef FEAT_GUI_GTK
158 gui.event_time = GDK_CURRENT_TIME;
159#endif
160
161 termcapinit((char_u *)"builtin_gui");
162 gui.starting = recursive - 1;
163
Bram Moolenaar9372a112005-12-06 19:59:18 +0000164#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200166 {
167# ifdef FEAT_EVAL
168 Window x11_window;
169 Display *x11_display;
170
171 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
172 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
173# endif
174
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 /* Display error messages in a dialog now. */
176 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 --recursive;
180}
181
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200182#ifdef MAY_FORK
183
184/* for waitpid() */
185# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
186# include <sys/wait.h>
187# endif
188
189/*
190 * Create a new process, by forking. In the child, start the GUI, and in
191 * the parent, exit.
192 *
193 * If something goes wrong, this will return with gui.in_use still set
194 * to FALSE, in which case the caller should continue execution without
195 * the GUI.
196 *
197 * If the child fails to start the GUI, then the child will exit and the
198 * parent will return. If the child succeeds, then the parent will exit
199 * and the child will return.
200 */
201 static void
202gui_do_fork()
203{
204#ifdef __QNXNTO__
205 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
206 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
207 gui_attempt_start();
208 return;
209#else
210 int pipefd[2]; /* pipe between parent and child */
211 int pipe_error;
212 int status;
213 int exit_status;
214 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200215
216 /* Setup a pipe between the child and the parent, so that the parent
217 * knows when the child has done the setsid() call and is allowed to
218 * exit. */
219 pipe_error = (pipe(pipefd) < 0);
220 pid = fork();
221 if (pid < 0) /* Fork error */
222 {
223 EMSG(_("E851: Failed to create a new process for the GUI"));
224 return;
225 }
226 else if (pid > 0) /* Parent */
227 {
228 /* Give the child some time to do the setsid(), otherwise the
229 * exit() may kill the child too (when starting gvim from inside a
230 * gvim). */
231 if (!pipe_error)
232 {
233 /* The read returns when the child closes the pipe (or when
234 * the child dies for some reason). */
235 close(pipefd[1]);
236 status = gui_read_child_pipe(pipefd[0]);
237 if (status == GUI_CHILD_FAILED)
238 {
239 /* The child failed to start the GUI, so the caller must
240 * continue. There may be more error information written
241 * to stderr by the child. */
242# ifdef __NeXT__
243 wait4(pid, &exit_status, 0, (struct rusage *)0);
244# else
245 waitpid(pid, &exit_status, 0);
246# endif
247 EMSG(_("E852: The child process failed to start the GUI"));
248 return;
249 }
250 else if (status == GUI_CHILD_IO_ERROR)
251 {
252 pipe_error = TRUE;
253 }
254 /* else GUI_CHILD_OK: parent exit */
255 }
256
257 if (pipe_error)
258 ui_delay(300L, TRUE);
259
260 /* When swapping screens we may need to go to the next line, e.g.,
261 * after a hit-enter prompt and using ":gui". */
262 if (newline_on_exit)
263 mch_errmsg("\r\n");
264
265 /*
266 * The parent must skip the normal exit() processing, the child
267 * will do it. For example, GTK messes up signals when exiting.
268 */
269 _exit(0);
270 }
271 /* Child */
272
Bram Moolenaar29695702012-05-18 17:03:18 +0200273#ifdef FEAT_GUI_GTK
274 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
275 if (gui_mch_init_check() != OK)
276 exit(1);
277#endif
278
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200279# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
280 /*
281 * Change our process group. On some systems/shells a CTRL-C in the
282 * shell where Vim was started would otherwise kill gvim!
283 */
284# if defined(HAVE_SETSID)
285 (void)setsid();
286# else
287 (void)setpgid(0, 0);
288# endif
289# endif
290 if (!pipe_error)
291 close(pipefd[0]);
292
293# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
294 /* Tell the session manager our new PID */
295 gui_mch_forked();
296# endif
297
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200298 /* Try to start the GUI */
299 gui_attempt_start();
300
301 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200302 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200303 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200304 if (gui.in_use)
305 write_eintr(pipefd[1], "ok", 3);
306 else
307 write_eintr(pipefd[1], "fail", 5);
308 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200309 }
310
311 /* If we failed to start the GUI, exit now. */
312 if (!gui.in_use)
313 exit(1);
314#endif
315}
316
317/*
318 * Read from a pipe assumed to be connected to the child process (this
319 * function is called from the parent).
320 * Return GUI_CHILD_OK if the child successfully started the GUI,
321 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
322 * some other error.
323 *
324 * The file descriptor will be closed before the function returns.
325 */
326 static int
327gui_read_child_pipe(int fd)
328{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200329 long bytes_read;
330#define READ_BUFFER_SIZE 10
331 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200332
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200333 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
334#undef READ_BUFFER_SIZE
335 close(fd);
336 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200338 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200339 if (strcmp(buffer, "ok") == 0)
340 return GUI_CHILD_OK;
341 return GUI_CHILD_FAILED;
342}
343
344#endif /* MAY_FORK */
345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346/*
347 * Call this when vim starts up, whether or not the GUI is started
348 */
349 void
350gui_prepare(argc, argv)
351 int *argc;
352 char **argv;
353{
354 gui.in_use = FALSE; /* No GUI yet (maybe later) */
355 gui.starting = FALSE; /* No GUI yet (maybe later) */
356 gui_mch_prepare(argc, argv);
357}
358
359/*
360 * Try initializing the GUI and check if it can be started.
361 * Used from main() to check early if "vim -g" can start the GUI.
362 * Used from gui_init() to prepare for starting the GUI.
363 * Returns FAIL or OK.
364 */
365 int
366gui_init_check()
367{
368 static int result = MAYBE;
369
370 if (result != MAYBE)
371 {
372 if (result == FAIL)
373 EMSG(_("E229: Cannot start the GUI"));
374 return result;
375 }
376
377 gui.shell_created = FALSE;
378 gui.dying = FALSE;
379 gui.in_focus = TRUE; /* so the guicursor setting works */
380 gui.dragged_sb = SBAR_NONE;
381 gui.dragged_wp = NULL;
382 gui.pointer_hidden = FALSE;
383 gui.col = 0;
384 gui.row = 0;
385 gui.num_cols = Columns;
386 gui.num_rows = Rows;
387
388 gui.cursor_is_valid = FALSE;
389 gui.scroll_region_top = 0;
390 gui.scroll_region_bot = Rows - 1;
391 gui.scroll_region_left = 0;
392 gui.scroll_region_right = Columns - 1;
393 gui.highlight_mask = HL_NORMAL;
394 gui.char_width = 1;
395 gui.char_height = 1;
396 gui.char_ascent = 0;
397 gui.border_width = 0;
398
399 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200400#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 gui.bold_font = NOFONT;
402 gui.ital_font = NOFONT;
403 gui.boldital_font = NOFONT;
404# ifdef FEAT_XFONTSET
405 gui.fontset = NOFONTSET;
406# endif
407#endif
408
409#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200410# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411# ifdef FONTSET_ALWAYS
412 gui.menu_fontset = NOFONTSET;
413# else
414 gui.menu_font = NOFONT;
415# endif
416# endif
417 gui.menu_is_active = TRUE; /* default: include menu */
418# ifndef FEAT_GUI_GTK
419 gui.menu_height = MENU_DEFAULT_HEIGHT;
420 gui.menu_width = 0;
421# endif
422#endif
423#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
424 gui.toolbar_height = 0;
425#endif
426#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
427 gui.footer_height = 0;
428#endif
429#ifdef FEAT_BEVAL_TIP
430 gui.tooltip_fontset = NOFONTSET;
431#endif
432
433 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
434 gui.prev_wrap = -1;
435
436#ifdef ALWAYS_USE_GUI
437 result = OK;
438#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200439# ifdef FEAT_GUI_GTK
440 /*
441 * Note: Don't call gtk_init_check() before fork, it will be called after
442 * the fork. When calling it before fork, it make vim hang for a while.
443 * See gui_do_fork().
444 * Use a simpler check if the GUI window can probably be opened.
445 */
446 result = gui.dofork ? gui_mch_early_init_check() : gui_mch_init_check();
447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#endif
451 return result;
452}
453
454/*
455 * This is the call which starts the GUI.
456 */
457 void
458gui_init()
459{
460 win_T *wp;
461 static int recursive = 0;
462
463 /*
464 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
465 * function will then be executed at the first call, the rest by the
466 * recursive call. This allow the shell to be opened halfway reading a
467 * gvimrc file.
468 */
469 if (!recursive)
470 {
471 ++recursive;
472
473 clip_init(TRUE);
474
475 /* If can't initialize, don't try doing the rest */
476 if (gui_init_check() == FAIL)
477 {
478 --recursive;
479 clip_init(FALSE);
480 return;
481 }
482
483 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000484 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
485 * breaks the Paste toolbar button.
486 */
487 set_option_value((char_u *)"paste", 0L, NULL, 0);
488
489 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Set up system-wide default menus.
491 */
492#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
493 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
494 {
495 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000496 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 sys_menu = FALSE;
498 }
499#endif
500
501 /*
502 * Switch on the mouse by default, unless the user changed it already.
503 * This can then be changed in the .gvimrc.
504 */
505 if (!option_was_set((char_u *)"mouse"))
506 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000507 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
509 /*
510 * If -U option given, use only the initializations from that file and
511 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
512 */
513 if (use_gvimrc != NULL)
514 {
515 if (STRCMP(use_gvimrc, "NONE") != 0
516 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000517 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
519 }
520 else
521 {
522 /*
523 * Get system wide defaults for gvim, only when file name defined.
524 */
525#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000526 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#endif
528
529 /*
530 * Try to read GUI initialization commands from the following
531 * places:
532 * - environment variable GVIMINIT
533 * - the user gvimrc file (~/.gvimrc)
534 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
535 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
536 * The first that exists is used, the rest is ignored.
537 */
538 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000539 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
540 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000542 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
543 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544#endif
545 )
546 {
547#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000548 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549#endif
550 }
551
552 /*
553 * Read initialization commands from ".gvimrc" in current
554 * directory. This is only done if the 'exrc' option is set.
555 * Because of security reasons we disallow shell and write
556 * commands now, except for unix if the file is owned by the user
557 * or 'secure' option has been reset in environment of global
558 * ".gvimrc".
559 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
560 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
561 */
562 if (p_exrc)
563 {
564#ifdef UNIX
565 {
566 struct stat s;
567
568 /* if ".gvimrc" file is not owned by user, set 'secure'
569 * mode */
570 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
571 secure = p_secure;
572 }
573#else
574 secure = p_secure;
575#endif
576
577 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
578 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
579#ifdef SYS_GVIMRC_FILE
580 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
581 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
582#endif
583#ifdef USR_GVIMRC_FILE2
584 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
585 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
586#endif
587#ifdef USR_GVIMRC_FILE3
588 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
589 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
590#endif
591 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000592 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 if (secure == 2)
595 need_wait_return = TRUE;
596 secure = 0;
597 }
598 }
599
600 if (need_wait_return || msg_didany)
601 wait_return(TRUE);
602
603 --recursive;
604 }
605
606 /* If recursive call opened the shell, return here from the first call */
607 if (gui.in_use)
608 return;
609
610 /*
611 * Create the GUI shell.
612 */
613 gui.in_use = TRUE; /* Must be set after menus have been set up */
614 if (gui_mch_init() == FAIL)
615 goto error;
616
617 /* Avoid a delay for an error message that was printed in the terminal
618 * where Vim was started. */
619 emsg_on_display = FALSE;
620 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 need_wait_return = FALSE;
623 msg_didany = FALSE;
624
625 /*
626 * Check validity of any generic resources that may have been loaded.
627 */
628 if (gui.border_width < 0)
629 gui.border_width = 0;
630
631 /*
632 * Set up the fonts. First use a font specified with "-fn" or "-font".
633 */
634 if (font_argument != NULL)
635 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
636 if (
637#ifdef FEAT_XFONTSET
638 (*p_guifontset == NUL
639 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
640#endif
641 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
642 : p_guifont, FALSE) == FAIL)
643 {
644 EMSG(_("E665: Cannot start GUI, no valid font found"));
645 goto error2;
646 }
647#ifdef FEAT_MBYTE
648 if (gui_get_wide_font() == FAIL)
649 EMSG(_("E231: 'guifontwide' invalid"));
650#endif
651
652 gui.num_cols = Columns;
653 gui.num_rows = Rows;
654 gui_reset_scroll_region();
655
656 /* Create initial scrollbars */
657 FOR_ALL_WINDOWS(wp)
658 {
659 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
660 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
661 }
662 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
663
664#ifdef FEAT_MENU
665 gui_create_initial_menus(root_menu);
666#endif
667#ifdef FEAT_SUN_WORKSHOP
668 if (usingSunWorkShop)
669 workshop_init();
670#endif
671#ifdef FEAT_SIGN_ICONS
672 sign_gui_started();
673#endif
674
675 /* Configure the desired menu and scrollbars */
676 gui_init_which_components(NULL);
677
678 /* All components of the GUI have been created now */
679 gui.shell_created = TRUE;
680
681#ifndef FEAT_GUI_GTK
682 /* Set the shell size, adjusted for the screen size. For GTK this only
683 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000684 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#endif
686#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
687 /* Need to set the size of the menubar after all the menus have been
688 * created. */
689 gui_mch_compute_menu_height((Widget)0);
690#endif
691
692 /*
693 * Actually open the GUI shell.
694 */
695 if (gui_mch_open() != FAIL)
696 {
697#ifdef FEAT_TITLE
698 maketitle();
699 resettitle();
700#endif
701 init_gui_options();
702#ifdef FEAT_ARABIC
703 /* Our GUI can't do bidi. */
704 p_tbidi = FALSE;
705#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000706#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 /* Give GTK+ a chance to put all widget's into place. */
708 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000709
710# ifdef FEAT_MENU
711 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
712 * It was still there to make F10 work. */
713 if (vim_strchr(p_go, GO_MENUS) == NULL)
714 {
715 --gui.starting;
716 gui_mch_enable_menu(FALSE);
717 ++gui.starting;
718 gui_mch_update();
719 }
720# endif
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000723 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000725 /* When 'lines' was set while starting up the topframe may have to be
726 * resized. */
727 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000728
729#ifdef FEAT_BEVAL
730 /* Always create the Balloon Evaluation area, but disable it when
731 * 'ballooneval' is off */
732# ifdef FEAT_GUI_GTK
733 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
734 &general_beval_cb, NULL);
735# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000736# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000737 {
738 extern Widget textArea;
739 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000740 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000741 }
742# else
743# ifdef FEAT_GUI_W32
744 balloonEval = gui_mch_create_beval_area(NULL, NULL,
745 &general_beval_cb, NULL);
746# endif
747# endif
748# endif
749 if (!p_beval)
750 gui_mch_disable_beval_area(balloonEval);
751#endif
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
754 if (!im_xim_isvalid_imactivate())
755 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
756#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000757 /* When 'cmdheight' was set during startup it may not have taken
758 * effect yet. */
759 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000760 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
762 return;
763 }
764
765error2:
766#ifdef FEAT_GUI_X11
767 /* undo gui_mch_init() */
768 gui_mch_uninit();
769#endif
770
771error:
772 gui.in_use = FALSE;
773 clip_init(FALSE);
774}
775
776
777 void
778gui_exit(rc)
779 int rc;
780{
781#ifndef __BEOS__
782 /* don't free the fonts, it leads to a BUS error
783 * richard@whitequeen.com Jul 99 */
784 free_highlight_fonts();
785#endif
786 gui.in_use = FALSE;
787 gui_mch_exit(rc);
788}
789
Bram Moolenaar9372a112005-12-06 19:59:18 +0000790#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000792# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
794 * Called when the GUI shell is closed by the user. If there are no changed
795 * files Vim exits, otherwise there will be a dialog to ask the user what to
796 * do.
797 * When this function returns, Vim should NOT exit!
798 */
799 void
800gui_shell_closed()
801{
802 cmdmod_T save_cmdmod;
803
804 save_cmdmod = cmdmod;
805
806 /* Only exit when there are no changed files */
807 exiting = TRUE;
808# ifdef FEAT_BROWSE
809 cmdmod.browse = TRUE;
810# endif
811# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
812 cmdmod.confirm = TRUE;
813# endif
814 /* If there are changed buffers, present the user with a dialog if
815 * possible, otherwise give an error message. */
816 if (!check_changed_any(FALSE))
817 getout(0);
818
819 exiting = FALSE;
820 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000821 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823#endif
824
825/*
826 * Set the font. "font_list" is a a comma separated list of font names. The
827 * first font name that works is used. If none is found, use the default
828 * font.
829 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
830 * Return OK when able to set the font. When it failed FAIL is returned and
831 * the fonts are unchanged.
832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 int
834gui_init_font(font_list, fontset)
835 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000836 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838#define FONTLEN 320
839 char_u font_name[FONTLEN];
840 int font_list_empty = FALSE;
841 int ret = FAIL;
842
843 if (!gui.in_use)
844 return FAIL;
845
846 font_name[0] = NUL;
847 if (*font_list == NUL)
848 font_list_empty = TRUE;
849 else
850 {
851#ifdef FEAT_XFONTSET
852 /* When using a fontset, the whole list of fonts is one name. */
853 if (fontset)
854 ret = gui_mch_init_font(font_list, TRUE);
855 else
856#endif
857 while (*font_list != NUL)
858 {
859 /* Isolate one comma separated font name. */
860 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
861
862 /* Careful!!! The Win32 version of gui_mch_init_font(), when
863 * called with "*" will change p_guifont to the selected font
864 * name, which frees the old value. This makes font_list
865 * invalid. Thus when OK is returned here, font_list must no
866 * longer be used! */
867 if (gui_mch_init_font(font_name, FALSE) == OK)
868 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200869#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 /* If it's a Unicode font, try setting 'guifontwide' to a
871 * similar double-width font. */
872 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
873 && strstr((char *)font_name, "10646") != NULL)
874 set_guifontwide(font_name);
875#endif
876 ret = OK;
877 break;
878 }
879 }
880 }
881
882 if (ret != OK
883 && STRCMP(font_list, "*") != 0
884 && (font_list_empty || gui.norm_font == NOFONT))
885 {
886 /*
887 * Couldn't load any font in 'font_list', keep the current font if
888 * there is one. If 'font_list' is empty, or if there is no current
889 * font, tell gui_mch_init_font() to try to find a font we can load.
890 */
891 ret = gui_mch_init_font(NULL, FALSE);
892 }
893
894 if (ret == OK)
895 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200896#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 /* Set normal font as current font */
898# ifdef FEAT_XFONTSET
899 if (gui.fontset != NOFONTSET)
900 gui_mch_set_fontset(gui.fontset);
901 else
902# endif
903 gui_mch_set_font(gui.norm_font);
904#endif
905 gui_set_shellsize(FALSE,
906#ifdef MSWIN
907 TRUE
908#else
909 FALSE
910#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000911 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913
914 return ret;
915}
916
917#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200918# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919/*
920 * Try setting 'guifontwide' to a font twice as wide as "name".
921 */
922 static void
923set_guifontwide(name)
924 char_u *name;
925{
926 int i = 0;
927 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
928 char_u *wp = NULL;
929 char_u *p;
930 GuiFont font;
931
932 wp = wide_name;
933 for (p = name; *p != NUL; ++p)
934 {
935 *wp++ = *p;
936 if (*p == '-')
937 {
938 ++i;
939 if (i == 6) /* font type: change "--" to "-*-" */
940 {
941 if (p[1] == '-')
942 *wp++ = '*';
943 }
944 else if (i == 12) /* found the width */
945 {
946 ++p;
947 i = getdigits(&p);
948 if (i != 0)
949 {
950 /* Double the width specification. */
951 sprintf((char *)wp, "%d%s", i * 2, p);
952 font = gui_mch_get_font(wide_name, FALSE);
953 if (font != NOFONT)
954 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000955 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 gui.wide_font = font;
957 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000958 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960 }
961 break;
962 }
963 }
964 }
965}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200966# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968/*
969 * Get the font for 'guifontwide'.
970 * Return FAIL for an invalid font name.
971 */
972 int
973gui_get_wide_font()
974{
975 GuiFont font = NOFONT;
976 char_u font_name[FONTLEN];
977 char_u *p;
978
979 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
980 return OK; /* Will give an error message later. */
981
982 if (p_guifontwide != NULL && *p_guifontwide != NUL)
983 {
984 for (p = p_guifontwide; *p != NUL; )
985 {
986 /* Isolate one comma separated font name. */
987 (void)copy_option_part(&p, font_name, FONTLEN, ",");
988 font = gui_mch_get_font(font_name, FALSE);
989 if (font != NOFONT)
990 break;
991 }
992 if (font == NOFONT)
993 return FAIL;
994 }
995
996 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200997#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
999 if (font != NOFONT && gui.norm_font != NOFONT
1000 && pango_font_description_equal(font, gui.norm_font))
1001 {
1002 gui.wide_font = NOFONT;
1003 gui_mch_free_font(font);
1004 }
1005 else
1006#endif
1007 gui.wide_font = font;
1008 return OK;
1009}
1010#endif
1011
1012 void
1013gui_set_cursor(row, col)
1014 int row;
1015 int col;
1016{
1017 gui.row = row;
1018 gui.col = col;
1019}
1020
1021/*
1022 * gui_check_pos - check if the cursor is on the screen.
1023 */
1024 static void
1025gui_check_pos()
1026{
1027 if (gui.row >= screen_Rows)
1028 gui.row = screen_Rows - 1;
1029 if (gui.col >= screen_Columns)
1030 gui.col = screen_Columns - 1;
1031 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1032 gui.cursor_is_valid = FALSE;
1033}
1034
1035/*
1036 * Redraw the cursor if necessary or when forced.
1037 * Careful: The contents of ScreenLines[] must match what is on the screen,
1038 * otherwise this goes wrong. May need to call out_flush() first.
1039 */
1040 void
1041gui_update_cursor(force, clear_selection)
1042 int force; /* when TRUE, update even when not moved */
1043 int clear_selection;/* clear selection under cursor */
1044{
1045 int cur_width = 0;
1046 int cur_height = 0;
1047 int old_hl_mask;
1048 int idx;
1049 int id;
1050 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1051 int cattr; /* cursor attributes */
1052 int attr;
1053 attrentry_T *aep = NULL;
1054
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001055 /* Don't update the cursor when halfway busy scrolling or the screen size
1056 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1057 if (!can_update_cursor || screen_Columns != gui.num_cols
1058 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 return;
1060
1061 gui_check_pos();
1062 if (!gui.cursor_is_valid || force
1063 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1064 {
1065 gui_undraw_cursor();
1066 if (gui.row < 0)
1067 return;
1068#ifdef USE_IM_CONTROL
1069 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1070 im_set_position(gui.row, gui.col);
1071#endif
1072 gui.cursor_row = gui.row;
1073 gui.cursor_col = gui.col;
1074
1075 /* Only write to the screen after ScreenLines[] has been initialized */
1076 if (!screen_cleared || ScreenLines == NULL)
1077 return;
1078
1079 /* Clear the selection if we are about to write over it */
1080 if (clear_selection)
1081 clip_may_clear_selection(gui.row, gui.row);
1082 /* Check that the cursor is inside the shell (resizing may have made
1083 * it invalid) */
1084 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1085 return;
1086
1087 gui.cursor_is_valid = TRUE;
1088
1089 /*
1090 * How the cursor is drawn depends on the current mode.
1091 */
1092 idx = get_shape_idx(FALSE);
1093 if (State & LANGMAP)
1094 id = shape_table[idx].id_lm;
1095 else
1096 id = shape_table[idx].id;
1097
1098 /* get the colors and attributes for the cursor. Default is inverted */
1099 cfg = INVALCOLOR;
1100 cbg = INVALCOLOR;
1101 cattr = HL_INVERSE;
1102 gui_mch_set_blinking(shape_table[idx].blinkwait,
1103 shape_table[idx].blinkon,
1104 shape_table[idx].blinkoff);
1105 if (id > 0)
1106 {
1107 cattr = syn_id2colors(id, &cfg, &cbg);
1108#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1109 {
1110 static int iid;
1111 guicolor_T fg, bg;
1112
Bram Moolenaarc236c162008-07-13 17:41:49 +00001113 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001114# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001115 preedit_get_status()
1116# else
1117 im_get_status()
1118# endif
1119 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 iid = syn_name2id((char_u *)"CursorIM");
1122 if (iid > 0)
1123 {
1124 syn_id2colors(iid, &fg, &bg);
1125 if (bg != INVALCOLOR)
1126 cbg = bg;
1127 if (fg != INVALCOLOR)
1128 cfg = fg;
1129 }
1130 }
1131 }
1132#endif
1133 }
1134
1135 /*
1136 * Get the attributes for the character under the cursor.
1137 * When no cursor color was given, use the character color.
1138 */
1139 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1140 if (attr > HL_ALL)
1141 aep = syn_gui_attr2entry(attr);
1142 if (aep != NULL)
1143 {
1144 attr = aep->ae_attr;
1145 if (cfg == INVALCOLOR)
1146 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1147 : aep->ae_u.gui.fg_color);
1148 if (cbg == INVALCOLOR)
1149 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1150 : aep->ae_u.gui.bg_color);
1151 }
1152 if (cfg == INVALCOLOR)
1153 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1154 if (cbg == INVALCOLOR)
1155 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1156
1157#ifdef FEAT_XIM
1158 if (aep != NULL)
1159 {
1160 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1161 : aep->ae_u.gui.bg_color);
1162 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1163 : aep->ae_u.gui.fg_color);
1164 if (xim_bg_color == INVALCOLOR)
1165 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1166 : gui.back_pixel;
1167 if (xim_fg_color == INVALCOLOR)
1168 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1169 : gui.norm_pixel;
1170 }
1171 else
1172 {
1173 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1174 : gui.back_pixel;
1175 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1176 : gui.norm_pixel;
1177 }
1178#endif
1179
1180 attr &= ~HL_INVERSE;
1181 if (cattr & HL_INVERSE)
1182 {
1183 cc = cbg;
1184 cbg = cfg;
1185 cfg = cc;
1186 }
1187 cattr &= ~HL_INVERSE;
1188
1189 /*
1190 * When we don't have window focus, draw a hollow cursor.
1191 */
1192 if (!gui.in_focus)
1193 {
1194 gui_mch_draw_hollow_cursor(cbg);
1195 return;
1196 }
1197
1198 old_hl_mask = gui.highlight_mask;
1199 if (shape_table[idx].shape == SHAPE_BLOCK
1200#ifdef FEAT_HANGULIN
1201 || composing_hangul
1202#endif
1203 )
1204 {
1205 /*
1206 * Draw the text character with the cursor colors. Use the
1207 * character attributes plus the cursor attributes.
1208 */
1209 gui.highlight_mask = (cattr | attr);
1210#ifdef FEAT_HANGULIN
1211 if (composing_hangul)
1212 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1213 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1214 else
1215#endif
1216 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1217 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1218 }
1219 else
1220 {
1221#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1222 int col_off = FALSE;
1223#endif
1224 /*
1225 * First draw the partial cursor, then overwrite with the text
1226 * character, using a transparent background.
1227 */
1228 if (shape_table[idx].shape == SHAPE_VER)
1229 {
1230 cur_height = gui.char_height;
1231 cur_width = (gui.char_width * shape_table[idx].percentage
1232 + 99) / 100;
1233 }
1234 else
1235 {
1236 cur_height = (gui.char_height * shape_table[idx].percentage
1237 + 99) / 100;
1238 cur_width = gui.char_width;
1239 }
1240#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001241 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1242 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 /* Double wide character. */
1245 if (shape_table[idx].shape != SHAPE_VER)
1246 cur_width += gui.char_width;
1247# ifdef FEAT_RIGHTLEFT
1248 if (CURSOR_BAR_RIGHT)
1249 {
1250 /* gui.col points to the left halve of the character but
1251 * the vertical line needs to be on the right halve.
1252 * A double-wide horizontal line is also drawn from the
1253 * right halve in gui_mch_draw_part_cursor(). */
1254 col_off = TRUE;
1255 ++gui.col;
1256 }
1257# endif
1258 }
1259#endif
1260 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1261#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1262 if (col_off)
1263 --gui.col;
1264#endif
1265
1266#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1267 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1268 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1269 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1270 (guicolor_T)0, (guicolor_T)0, 0);
1271#endif
1272 }
1273 gui.highlight_mask = old_hl_mask;
1274 }
1275}
1276
1277#if defined(FEAT_MENU) || defined(PROTO)
1278 void
1279gui_position_menu()
1280{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001281# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (gui.menu_is_active && gui.in_use)
1283 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1284# endif
1285}
1286#endif
1287
1288/*
1289 * Position the various GUI components (text area, menu). The vertical
1290 * scrollbars are NOT handled here. See gui_update_scrollbars().
1291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 static void
1293gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001294 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 int text_area_x;
1297 int text_area_y;
1298 int text_area_width;
1299 int text_area_height;
1300
1301 /* avoid that moving components around generates events */
1302 ++hold_gui_events;
1303
1304 text_area_x = 0;
1305 if (gui.which_scrollbars[SBAR_LEFT])
1306 text_area_x += gui.scrollbar_width;
1307
1308 text_area_y = 0;
1309#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1310 gui.menu_width = total_width;
1311 if (gui.menu_is_active)
1312 text_area_y += gui.menu_height;
1313#endif
1314#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1315 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1316 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1317#endif
1318
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001319# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001320 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001321 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001322 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001323#endif
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1326 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1327 {
1328# ifdef FEAT_GUI_ATHENA
1329 gui_mch_set_toolbar_pos(0, text_area_y,
1330 gui.menu_width, gui.toolbar_height);
1331# endif
1332 text_area_y += gui.toolbar_height;
1333 }
1334#endif
1335
1336 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1337 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1338
1339 gui_mch_set_text_area_pos(text_area_x,
1340 text_area_y,
1341 text_area_width,
1342 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001343#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 + xim_get_status_area_height()
1345#endif
1346 );
1347#ifdef FEAT_MENU
1348 gui_position_menu();
1349#endif
1350 if (gui.which_scrollbars[SBAR_BOTTOM])
1351 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1352 text_area_x,
1353 text_area_y + text_area_height,
1354 text_area_width,
1355 gui.scrollbar_height);
1356 gui.left_sbar_x = 0;
1357 gui.right_sbar_x = text_area_x + text_area_width;
1358
1359 --hold_gui_events;
1360}
1361
Bram Moolenaar02743632005-07-25 20:42:36 +00001362/*
1363 * Get the width of the widgets and decorations to the side of the text area.
1364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 int
1366gui_get_base_width()
1367{
1368 int base_width;
1369
1370 base_width = 2 * gui.border_offset;
1371 if (gui.which_scrollbars[SBAR_LEFT])
1372 base_width += gui.scrollbar_width;
1373 if (gui.which_scrollbars[SBAR_RIGHT])
1374 base_width += gui.scrollbar_width;
1375 return base_width;
1376}
1377
Bram Moolenaar02743632005-07-25 20:42:36 +00001378/*
1379 * Get the height of the widgets and decorations above and below the text area.
1380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 int
1382gui_get_base_height()
1383{
1384 int base_height;
1385
1386 base_height = 2 * gui.border_offset;
1387 if (gui.which_scrollbars[SBAR_BOTTOM])
1388 base_height += gui.scrollbar_height;
1389#ifdef FEAT_GUI_GTK
1390 /* We can't take the sizes properly into account until anything is
1391 * realized. Therefore we recalculate all the values here just before
1392 * setting the size. (--mdcki) */
1393#else
1394# ifdef FEAT_MENU
1395 if (gui.menu_is_active)
1396 base_height += gui.menu_height;
1397# endif
1398# ifdef FEAT_TOOLBAR
1399 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1400# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1401 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1402# else
1403 base_height += gui.toolbar_height;
1404# endif
1405# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001406# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1407 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001408 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001409 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411# ifdef FEAT_FOOTER
1412 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1413 base_height += gui.footer_height;
1414# endif
1415# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1416 base_height += gui_mch_text_area_extra_height();
1417# endif
1418#endif
1419 return base_height;
1420}
1421
1422/*
1423 * Should be called after the GUI shell has been resized. Its arguments are
1424 * the new width and height of the shell in pixels.
1425 */
1426 void
1427gui_resize_shell(pixel_width, pixel_height)
1428 int pixel_width;
1429 int pixel_height;
1430{
1431 static int busy = FALSE;
1432
1433 if (!gui.shell_created) /* ignore when still initializing */
1434 return;
1435
1436 /*
1437 * Can't resize the screen while it is being redrawn. Remember the new
1438 * size and handle it later.
1439 */
1440 if (updating_screen || busy)
1441 {
1442 new_pixel_width = pixel_width;
1443 new_pixel_height = pixel_height;
1444 return;
1445 }
1446
1447again:
1448 busy = TRUE;
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /* Flush pending output before redrawing */
1451 out_flush();
1452
1453 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001454 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456 gui_position_components(pixel_width);
1457
1458 gui_reset_scroll_region();
1459 /*
1460 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1461 * at the last line here (why does it have to be one row too low?).
1462 */
1463 if (State == ASKMORE || State == CONFIRM)
1464 gui.row = gui.num_rows;
1465
1466 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1467 * the safe side. */
1468 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1469 || gui.num_rows != Rows || gui.num_cols != Columns)
1470 shell_resized();
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 gui_update_scrollbars(TRUE);
1473 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001474#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 xim_set_status_area();
1476#endif
1477
1478 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /*
1481 * We could have been called again while redrawing the screen.
1482 * Need to do it all again with the latest size then.
1483 */
1484 if (new_pixel_height)
1485 {
1486 pixel_width = new_pixel_width;
1487 pixel_height = new_pixel_height;
1488 new_pixel_width = 0;
1489 new_pixel_height = 0;
1490 goto again;
1491 }
1492}
1493
1494/*
1495 * Check if gui_resize_shell() must be called.
1496 */
1497 void
1498gui_may_resize_shell()
1499{
1500 int h, w;
1501
1502 if (new_pixel_height)
1503 {
1504 /* careful: gui_resize_shell() may postpone the resize again if we
1505 * were called indirectly by it */
1506 w = new_pixel_width;
1507 h = new_pixel_height;
1508 new_pixel_width = 0;
1509 new_pixel_height = 0;
1510 gui_resize_shell(w, h);
1511 }
1512}
1513
1514 int
1515gui_get_shellsize()
1516{
1517 Rows = gui.num_rows;
1518 Columns = gui.num_cols;
1519 return OK;
1520}
1521
1522/*
1523 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001524 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1525 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001528gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001529 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001531 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 int base_width;
1534 int base_height;
1535 int width;
1536 int height;
1537 int min_width;
1538 int min_height;
1539 int screen_w;
1540 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001541#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001542 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001543 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001544#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001545 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
1547 if (!gui.shell_created)
1548 return;
1549
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001550#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 /* If not setting to a user specified size and maximized, calculate the
1552 * number of characters that fit in the maximized window. */
1553 if (!mustset && gui_mch_maximized())
1554 {
1555 gui_mch_newfont();
1556 return;
1557 }
1558#endif
1559
1560 base_width = gui_get_base_width();
1561 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001562 if (fit_to_display)
1563 /* Remember the original window position. */
1564 gui_mch_get_winpos(&x, &y);
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#ifdef USE_SUN_WORKSHOP
1567 if (!mustset && usingSunWorkShop
1568 && workshop_get_width_height(&width, &height))
1569 {
1570 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1571 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1572 }
1573 else
1574#endif
1575 {
1576 width = Columns * gui.char_width + base_width;
1577 height = Rows * gui.char_height + base_height;
1578 }
1579
1580 if (fit_to_display)
1581 {
1582 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001583 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 Columns = (screen_w - base_width) / gui.char_width;
1586 if (Columns < MIN_COLUMNS)
1587 Columns = MIN_COLUMNS;
1588 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001589#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001590 ++did_adjust;
1591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001593 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 Rows = (screen_h - base_height) / gui.char_height;
1596 check_shellsize();
1597 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001598#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001599 ++did_adjust;
1600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001602#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001603 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1604 && height + gui.char_height >= screen_h))
1605 /* don't unmaximize if at maximum size */
1606 un_maximize = FALSE;
1607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 gui.num_cols = Columns;
1610 gui.num_rows = Rows;
1611
1612 min_width = base_width + MIN_COLUMNS * gui.char_width;
1613 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001614#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001615 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001616#endif
1617
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001618#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001619 if (un_maximize)
1620 {
1621 /* If the window size is smaller than the screen unmaximize the
1622 * window, otherwise resizing won't work. */
1623 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1624 if ((width + gui.char_width < screen_w
1625 || height + gui.char_height * 2 < screen_h)
1626 && gui_mch_maximized())
1627 gui_mch_unmaximize();
1628 }
1629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001632 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001634 if (fit_to_display && x >= 0 && y >= 0)
1635 {
1636 /* Some window managers put the Vim window left of/above the screen.
1637 * Only change the position if it wasn't already negative before
1638 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 gui_mch_update();
1640 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1641 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1642 }
1643
1644 gui_position_components(width);
1645 gui_update_scrollbars(TRUE);
1646 gui_reset_scroll_region();
1647}
1648
1649/*
1650 * Called when Rows and/or Columns has changed.
1651 */
1652 void
1653gui_new_shellsize()
1654{
1655 gui_reset_scroll_region();
1656}
1657
1658/*
1659 * Make scroll region cover whole screen.
1660 */
1661 void
1662gui_reset_scroll_region()
1663{
1664 gui.scroll_region_top = 0;
1665 gui.scroll_region_bot = gui.num_rows - 1;
1666 gui.scroll_region_left = 0;
1667 gui.scroll_region_right = gui.num_cols - 1;
1668}
1669
1670 void
1671gui_start_highlight(mask)
1672 int mask;
1673{
1674 if (mask > HL_ALL) /* highlight code */
1675 gui.highlight_mask = mask;
1676 else /* mask */
1677 gui.highlight_mask |= mask;
1678}
1679
1680 void
1681gui_stop_highlight(mask)
1682 int mask;
1683{
1684 if (mask > HL_ALL) /* highlight code */
1685 gui.highlight_mask = HL_NORMAL;
1686 else /* mask */
1687 gui.highlight_mask &= ~mask;
1688}
1689
1690/*
1691 * Clear a rectangular region of the screen from text pos (row1, col1) to
1692 * (row2, col2) inclusive.
1693 */
1694 void
1695gui_clear_block(row1, col1, row2, col2)
1696 int row1;
1697 int col1;
1698 int row2;
1699 int col2;
1700{
1701 /* Clear the selection if we are about to write over it */
1702 clip_may_clear_selection(row1, row2);
1703
1704 gui_mch_clear_block(row1, col1, row2, col2);
1705
1706 /* Invalidate cursor if it was in this block */
1707 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1708 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1709 gui.cursor_is_valid = FALSE;
1710}
1711
1712/*
1713 * Write code to update the cursor later. This avoids the need to flush the
1714 * output buffer before calling gui_update_cursor().
1715 */
1716 void
1717gui_update_cursor_later()
1718{
1719 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1720}
1721
1722 void
1723gui_write(s, len)
1724 char_u *s;
1725 int len;
1726{
1727 char_u *p;
1728 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 int force_scrollbar = FALSE;
1731 static win_T *old_curwin = NULL;
1732
1733/* #define DEBUG_GUI_WRITE */
1734#ifdef DEBUG_GUI_WRITE
1735 {
1736 int i;
1737 char_u *str;
1738
1739 printf("gui_write(%d):\n ", len);
1740 for (i = 0; i < len; i++)
1741 if (s[i] == ESC)
1742 {
1743 if (i != 0)
1744 printf("\n ");
1745 printf("<ESC>");
1746 }
1747 else
1748 {
1749 str = transchar_byte(s[i]);
1750 if (str[0] && str[1])
1751 printf("<%s>", (char *)str);
1752 else
1753 printf("%s", (char *)str);
1754 }
1755 printf("\n");
1756 }
1757#endif
1758 while (len)
1759 {
1760 if (s[0] == ESC && s[1] == '|')
1761 {
1762 p = s + 2;
1763 if (VIM_ISDIGIT(*p))
1764 {
1765 arg1 = getdigits(&p);
1766 if (p > s + len)
1767 break;
1768 if (*p == ';')
1769 {
1770 ++p;
1771 arg2 = getdigits(&p);
1772 if (p > s + len)
1773 break;
1774 }
1775 }
1776 switch (*p)
1777 {
1778 case 'C': /* Clear screen */
1779 clip_scroll_selection(9999);
1780 gui_mch_clear_all();
1781 gui.cursor_is_valid = FALSE;
1782 force_scrollbar = TRUE;
1783 break;
1784 case 'M': /* Move cursor */
1785 gui_set_cursor(arg1, arg2);
1786 break;
1787 case 's': /* force cursor (shape) update */
1788 force_cursor = TRUE;
1789 break;
1790 case 'R': /* Set scroll region */
1791 if (arg1 < arg2)
1792 {
1793 gui.scroll_region_top = arg1;
1794 gui.scroll_region_bot = arg2;
1795 }
1796 else
1797 {
1798 gui.scroll_region_top = arg2;
1799 gui.scroll_region_bot = arg1;
1800 }
1801 break;
1802#ifdef FEAT_VERTSPLIT
1803 case 'V': /* Set vertical scroll region */
1804 if (arg1 < arg2)
1805 {
1806 gui.scroll_region_left = arg1;
1807 gui.scroll_region_right = arg2;
1808 }
1809 else
1810 {
1811 gui.scroll_region_left = arg2;
1812 gui.scroll_region_right = arg1;
1813 }
1814 break;
1815#endif
1816 case 'd': /* Delete line */
1817 gui_delete_lines(gui.row, 1);
1818 break;
1819 case 'D': /* Delete lines */
1820 gui_delete_lines(gui.row, arg1);
1821 break;
1822 case 'i': /* Insert line */
1823 gui_insert_lines(gui.row, 1);
1824 break;
1825 case 'I': /* Insert lines */
1826 gui_insert_lines(gui.row, arg1);
1827 break;
1828 case '$': /* Clear to end-of-line */
1829 gui_clear_block(gui.row, gui.col, gui.row,
1830 (int)Columns - 1);
1831 break;
1832 case 'h': /* Turn on highlighting */
1833 gui_start_highlight(arg1);
1834 break;
1835 case 'H': /* Turn off highlighting */
1836 gui_stop_highlight(arg1);
1837 break;
1838 case 'f': /* flash the window (visual bell) */
1839 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1840 break;
1841 default:
1842 p = s + 1; /* Skip the ESC */
1843 break;
1844 }
1845 len -= (int)(++p - s);
1846 s = p;
1847 }
1848 else if (
1849#ifdef EBCDIC
1850 CtrlChar(s[0]) != 0 /* Ctrl character */
1851#else
1852 s[0] < 0x20 /* Ctrl character */
1853#endif
1854#ifdef FEAT_SIGN_ICONS
1855 && s[0] != SIGN_BYTE
1856# ifdef FEAT_NETBEANS_INTG
1857 && s[0] != MULTISIGN_BYTE
1858# endif
1859#endif
1860 )
1861 {
1862 if (s[0] == '\n') /* NL */
1863 {
1864 gui.col = 0;
1865 if (gui.row < gui.scroll_region_bot)
1866 gui.row++;
1867 else
1868 gui_delete_lines(gui.scroll_region_top, 1);
1869 }
1870 else if (s[0] == '\r') /* CR */
1871 {
1872 gui.col = 0;
1873 }
1874 else if (s[0] == '\b') /* Backspace */
1875 {
1876 if (gui.col)
1877 --gui.col;
1878 }
1879 else if (s[0] == Ctrl_L) /* cursor-right */
1880 {
1881 ++gui.col;
1882 }
1883 else if (s[0] == Ctrl_G) /* Beep */
1884 {
1885 gui_mch_beep();
1886 }
1887 /* Other Ctrl character: shouldn't happen! */
1888
1889 --len; /* Skip this char */
1890 ++s;
1891 }
1892 else
1893 {
1894 p = s;
1895 while (len > 0 && (
1896#ifdef EBCDIC
1897 CtrlChar(*p) == 0
1898#else
1899 *p >= 0x20
1900#endif
1901#ifdef FEAT_SIGN_ICONS
1902 || *p == SIGN_BYTE
1903# ifdef FEAT_NETBEANS_INTG
1904 || *p == MULTISIGN_BYTE
1905# endif
1906#endif
1907 ))
1908 {
1909 len--;
1910 p++;
1911 }
1912 gui_outstr(s, (int)(p - s));
1913 s = p;
1914 }
1915 }
1916
1917 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1918 * set). */
1919 if (force_cursor)
1920 gui_update_cursor(TRUE, TRUE);
1921
1922 /* When switching to another window the dragging must have stopped.
1923 * Required for GTK, dragged_sb isn't reset. */
1924 if (old_curwin != curwin)
1925 gui.dragged_sb = SBAR_NONE;
1926
1927 /* Update the scrollbars after clearing the screen or when switched
1928 * to another window.
1929 * Update the horizontal scrollbar always, it's difficult to check all
1930 * situations where it might change. */
1931 if (force_scrollbar || old_curwin != curwin)
1932 gui_update_scrollbars(force_scrollbar);
1933 else
1934 gui_update_horiz_scrollbar(FALSE);
1935 old_curwin = curwin;
1936
1937 /*
1938 * We need to make sure this is cleared since Athena doesn't tell us when
1939 * he is done dragging. Do the same for GTK.
1940 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001941#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 gui.dragged_sb = SBAR_NONE;
1943#endif
1944
1945 gui_mch_flush(); /* In case vim decides to take a nap */
1946}
1947
1948/*
1949 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1950 * produces wrong results. Call gui_dont_update_cursor() before that code and
1951 * gui_can_update_cursor() afterwards.
1952 */
1953 void
1954gui_dont_update_cursor()
1955{
1956 if (gui.in_use)
1957 {
1958 /* Undraw the cursor now, we probably can't do it after the change. */
1959 gui_undraw_cursor();
1960 can_update_cursor = FALSE;
1961 }
1962}
1963
1964 void
1965gui_can_update_cursor()
1966{
1967 can_update_cursor = TRUE;
1968 /* No need to update the cursor right now, there is always more output
1969 * after scrolling. */
1970}
1971
1972 static void
1973gui_outstr(s, len)
1974 char_u *s;
1975 int len;
1976{
1977 int this_len;
1978#ifdef FEAT_MBYTE
1979 int cells;
1980#endif
1981
1982 if (len == 0)
1983 return;
1984
1985 if (len < 0)
1986 len = (int)STRLEN(s);
1987
1988 while (len > 0)
1989 {
1990#ifdef FEAT_MBYTE
1991 if (has_mbyte)
1992 {
1993 /* Find out how many chars fit in the current line. */
1994 cells = 0;
1995 for (this_len = 0; this_len < len; )
1996 {
1997 cells += (*mb_ptr2cells)(s + this_len);
1998 if (gui.col + cells > Columns)
1999 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002000 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 if (this_len > len)
2003 this_len = len; /* don't include following composing char */
2004 }
2005 else
2006#endif
2007 if (gui.col + len > Columns)
2008 this_len = Columns - gui.col;
2009 else
2010 this_len = len;
2011
2012 (void)gui_outstr_nowrap(s, this_len,
2013 0, (guicolor_T)0, (guicolor_T)0, 0);
2014 s += this_len;
2015 len -= this_len;
2016#ifdef FEAT_MBYTE
2017 /* fill up for a double-width char that doesn't fit. */
2018 if (len > 0 && gui.col < Columns)
2019 (void)gui_outstr_nowrap((char_u *)" ", 1,
2020 0, (guicolor_T)0, (guicolor_T)0, 0);
2021#endif
2022 /* The cursor may wrap to the next line. */
2023 if (gui.col >= Columns)
2024 {
2025 gui.col = 0;
2026 gui.row++;
2027 }
2028 }
2029}
2030
2031/*
2032 * Output one character (may be one or two display cells).
2033 * Caller must check for valid "off".
2034 * Returns FAIL or OK, just like gui_outstr_nowrap().
2035 */
2036 static int
2037gui_screenchar(off, flags, fg, bg, back)
2038 int off; /* Offset from start of screen */
2039 int flags;
2040 guicolor_T fg, bg; /* colors for cursor */
2041 int back; /* backup this many chars when using bold trick */
2042{
2043#ifdef FEAT_MBYTE
2044 char_u buf[MB_MAXBYTES + 1];
2045
2046 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2047 if (enc_utf8 && ScreenLines[off] == 0)
2048 return OK;
2049
2050 if (enc_utf8 && ScreenLinesUC[off] != 0)
2051 /* Draw UTF-8 multi-byte character. */
2052 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2053 flags, fg, bg, back);
2054
2055 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2056 {
2057 buf[0] = ScreenLines[off];
2058 buf[1] = ScreenLines2[off];
2059 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2060 }
2061
2062 /* Draw non-multi-byte character or DBCS character. */
2063 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002064 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 flags, fg, bg, back);
2066#else
2067 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2068#endif
2069}
2070
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002071#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072/*
2073 * Output the string at the given screen position. This is used in place
2074 * of gui_screenchar() where possible because Pango needs as much context
2075 * as possible to work nicely. It's a lot faster as well.
2076 */
2077 static int
2078gui_screenstr(off, len, flags, fg, bg, back)
2079 int off; /* Offset from start of screen */
2080 int len; /* string length in screen cells */
2081 int flags;
2082 guicolor_T fg, bg; /* colors for cursor */
2083 int back; /* backup this many chars when using bold trick */
2084{
2085 char_u *buf;
2086 int outlen = 0;
2087 int i;
2088 int retval;
2089
2090 if (len <= 0) /* "cannot happen"? */
2091 return OK;
2092
2093 if (enc_utf8)
2094 {
2095 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2096 if (buf == NULL)
2097 return OK; /* not much we could do here... */
2098
2099 for (i = off; i < off + len; ++i)
2100 {
2101 if (ScreenLines[i] == 0)
2102 continue; /* skip second half of double-width char */
2103
2104 if (ScreenLinesUC[i] == 0)
2105 buf[outlen++] = ScreenLines[i];
2106 else
2107 outlen += utfc_char2bytes(i, buf + outlen);
2108 }
2109
2110 buf[outlen] = NUL; /* only to aid debugging */
2111 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2112 vim_free(buf);
2113
2114 return retval;
2115 }
2116 else if (enc_dbcs == DBCS_JPNU)
2117 {
2118 buf = alloc((unsigned)(len * 2 + 1));
2119 if (buf == NULL)
2120 return OK; /* not much we could do here... */
2121
2122 for (i = off; i < off + len; ++i)
2123 {
2124 buf[outlen++] = ScreenLines[i];
2125
2126 /* handle double-byte single-width char */
2127 if (ScreenLines[i] == 0x8e)
2128 buf[outlen++] = ScreenLines2[i];
2129 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2130 buf[outlen++] = ScreenLines[++i];
2131 }
2132
2133 buf[outlen] = NUL; /* only to aid debugging */
2134 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2135 vim_free(buf);
2136
2137 return retval;
2138 }
2139 else
2140 {
2141 return gui_outstr_nowrap(&ScreenLines[off], len,
2142 flags, fg, bg, back);
2143 }
2144}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002145#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147/*
2148 * Output the given string at the current cursor position. If the string is
2149 * too long to fit on the line, then it is truncated.
2150 * "flags":
2151 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2152 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002153 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * background.
2155 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2156 * it.
2157 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2158 * FAIL (the caller should start drawing "back" chars back).
2159 */
2160 int
2161gui_outstr_nowrap(s, len, flags, fg, bg, back)
2162 char_u *s;
2163 int len;
2164 int flags;
2165 guicolor_T fg, bg; /* colors for cursor */
2166 int back; /* backup this many chars when using bold trick */
2167{
2168 long_u highlight_mask;
2169 long_u hl_mask_todo;
2170 guicolor_T fg_color;
2171 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002172 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002173#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 GuiFont font = NOFONT;
2175# ifdef FEAT_XFONTSET
2176 GuiFontset fontset = NOFONTSET;
2177# endif
2178#endif
2179 attrentry_T *aep = NULL;
2180 int draw_flags;
2181 int col = gui.col;
2182#ifdef FEAT_SIGN_ICONS
2183 int draw_sign = FALSE;
2184# ifdef FEAT_NETBEANS_INTG
2185 int multi_sign = FALSE;
2186# endif
2187#endif
2188
2189 if (len < 0)
2190 len = (int)STRLEN(s);
2191 if (len == 0)
2192 return OK;
2193
2194#ifdef FEAT_SIGN_ICONS
2195 if (*s == SIGN_BYTE
2196# ifdef FEAT_NETBEANS_INTG
2197 || *s == MULTISIGN_BYTE
2198# endif
2199 )
2200 {
2201# ifdef FEAT_NETBEANS_INTG
2202 if (*s == MULTISIGN_BYTE)
2203 multi_sign = TRUE;
2204# endif
2205 /* draw spaces instead */
2206 s = (char_u *)" ";
2207 if (len == 1 && col > 0)
2208 --col;
2209 len = 2;
2210 draw_sign = TRUE;
2211 highlight_mask = 0;
2212 }
2213 else
2214#endif
2215 if (gui.highlight_mask > HL_ALL)
2216 {
2217 aep = syn_gui_attr2entry(gui.highlight_mask);
2218 if (aep == NULL) /* highlighting not set */
2219 highlight_mask = 0;
2220 else
2221 highlight_mask = aep->ae_attr;
2222 }
2223 else
2224 highlight_mask = gui.highlight_mask;
2225 hl_mask_todo = highlight_mask;
2226
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002227#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 /* Set the font */
2229 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2230 font = aep->ae_u.gui.font;
2231# ifdef FEAT_XFONTSET
2232 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2233 fontset = aep->ae_u.gui.fontset;
2234# endif
2235 else
2236 {
2237# ifdef FEAT_XFONTSET
2238 if (gui.fontset != NOFONTSET)
2239 fontset = gui.fontset;
2240 else
2241# endif
2242 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2243 {
2244 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2245 {
2246 font = gui.boldital_font;
2247 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2248 }
2249 else if (gui.bold_font != NOFONT)
2250 {
2251 font = gui.bold_font;
2252 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2253 }
2254 else
2255 font = gui.norm_font;
2256 }
2257 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2258 {
2259 font = gui.ital_font;
2260 hl_mask_todo &= ~HL_ITALIC;
2261 }
2262 else
2263 font = gui.norm_font;
2264 }
2265# ifdef FEAT_XFONTSET
2266 if (fontset != NOFONTSET)
2267 gui_mch_set_fontset(fontset);
2268 else
2269# endif
2270 gui_mch_set_font(font);
2271#endif
2272
2273 draw_flags = 0;
2274
2275 /* Set the color */
2276 bg_color = gui.back_pixel;
2277 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2278 {
2279 draw_flags |= DRAW_CURSOR;
2280 fg_color = fg;
2281 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002282 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 else if (aep != NULL)
2285 {
2286 fg_color = aep->ae_u.gui.fg_color;
2287 if (fg_color == INVALCOLOR)
2288 fg_color = gui.norm_pixel;
2289 bg_color = aep->ae_u.gui.bg_color;
2290 if (bg_color == INVALCOLOR)
2291 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002292 sp_color = aep->ae_u.gui.sp_color;
2293 if (sp_color == INVALCOLOR)
2294 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002299 sp_color = fg_color;
2300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2303 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002304#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 gui_mch_set_colors(bg_color, fg_color);
2306#else
2307 gui_mch_set_fg_color(bg_color);
2308 gui_mch_set_bg_color(fg_color);
2309#endif
2310 }
2311 else
2312 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002313#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 gui_mch_set_colors(fg_color, bg_color);
2315#else
2316 gui_mch_set_fg_color(fg_color);
2317 gui_mch_set_bg_color(bg_color);
2318#endif
2319 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002320 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 /* Clear the selection if we are about to write over it */
2323 if (!(flags & GUI_MON_NOCLEAR))
2324 clip_may_clear_selection(gui.row, gui.row);
2325
2326
2327#ifndef MSWIN16_FASTTEXT
2328 /* If there's no bold font, then fake it */
2329 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2330 draw_flags |= DRAW_BOLD;
2331#endif
2332
2333 /*
2334 * When drawing bold or italic characters the spill-over from the left
2335 * neighbor may be destroyed. Let the caller backup to start redrawing
2336 * just after a blank.
2337 */
2338 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2339 return FAIL;
2340
Bram Moolenaare60acc12011-05-10 16:41:25 +02002341#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 /* If there's no italic font, then fake it.
2343 * For GTK2, we don't need a different font for italic style. */
2344 if (hl_mask_todo & HL_ITALIC)
2345 draw_flags |= DRAW_ITALIC;
2346
2347 /* Do we underline the text? */
2348 if (hl_mask_todo & HL_UNDERLINE)
2349 draw_flags |= DRAW_UNDERL;
2350#else
2351 /* Do we underline the text? */
2352 if ((hl_mask_todo & HL_UNDERLINE)
2353# ifndef MSWIN16_FASTTEXT
2354 || (hl_mask_todo & HL_ITALIC)
2355# endif
2356 )
2357 draw_flags |= DRAW_UNDERL;
2358#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002359 /* Do we undercurl the text? */
2360 if (hl_mask_todo & HL_UNDERCURL)
2361 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002363 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (flags & GUI_MON_TRS_CURSOR)
2365 draw_flags |= DRAW_TRANSP;
2366
2367 /*
2368 * Draw the text.
2369 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002370#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /* The value returned is the length in display cells */
2372 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2373#else
2374# ifdef FEAT_MBYTE
2375 if (enc_utf8)
2376 {
2377 int start; /* index of bytes to be drawn */
2378 int cells; /* cellwidth of bytes to be drawn */
2379 int thislen; /* length of bytes to be drawin */
2380 int cn; /* cellwidth of current char */
2381 int i; /* index of current char */
2382 int c; /* current char value */
2383 int cl; /* byte length of current char */
2384 int comping; /* current char is composing */
2385 int scol = col; /* screen column */
2386 int dowide; /* use 'guifontwide' */
2387
2388 /* Break the string at a composing character, it has to be drawn on
2389 * top of the previous character. */
2390 start = 0;
2391 cells = 0;
2392 for (i = 0; i < len; i += cl)
2393 {
2394 c = utf_ptr2char(s + i);
2395 cn = utf_char2cells(c);
2396 if (cn > 1
2397# ifdef FEAT_XFONTSET
2398 && fontset == NOFONTSET
2399# endif
2400 && gui.wide_font != NOFONT)
2401 dowide = TRUE;
2402 else
2403 dowide = FALSE;
2404 comping = utf_iscomposing(c);
2405 if (!comping) /* count cells from non-composing chars */
2406 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002407 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (cl == 0) /* hit end of string */
2409 len = i + cl; /* len must be wrong "cannot happen" */
2410
2411 /* print the string so far if it's the last character or there is
2412 * a composing character. */
2413 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002414# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 || (cn > 1
2416# ifdef FEAT_XFONTSET
2417 /* No fontset: At least draw char after wide char at
2418 * right position. */
2419 && fontset == NOFONTSET
2420# endif
2421 )
2422# endif
2423 )
2424 {
2425 if (comping || dowide)
2426 thislen = i - start;
2427 else
2428 thislen = i - start + cl;
2429 if (thislen > 0)
2430 {
2431 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2432 draw_flags);
2433 start += thislen;
2434 }
2435 scol += cells;
2436 cells = 0;
2437 if (dowide)
2438 {
2439 gui_mch_set_font(gui.wide_font);
2440 gui_mch_draw_string(gui.row, scol - cn,
2441 s + start, cl, draw_flags);
2442 gui_mch_set_font(font);
2443 start += cl;
2444 }
2445
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002446# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002447 /* No fontset: draw a space to fill the gap after a wide char
2448 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2450# ifdef FEAT_XFONTSET
2451 && fontset == NOFONTSET
2452# endif
2453 && !dowide)
2454 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2455 1, draw_flags);
2456# endif
2457 }
2458 /* Draw a composing char on top of the previous char. */
2459 if (comping)
2460 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002461# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002462 /* Carbon ATSUI autodraws composing char over previous char */
2463 gui_mch_draw_string(gui.row, scol, s + i, cl,
2464 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002465# else
2466 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2467 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 start = i + cl;
2470 }
2471 }
2472 /* The stuff below assumes "len" is the length in screen columns. */
2473 len = scol - col;
2474 }
2475 else
2476# endif
2477 {
2478 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2479# ifdef FEAT_MBYTE
2480 if (enc_dbcs == DBCS_JPNU)
2481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 /* Get the length in display cells, this can be different from the
2483 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002484 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486# endif
2487 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002488#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2491 gui.col = col + len;
2492
2493 /* May need to invert it when it's part of the selection. */
2494 if (flags & GUI_MON_NOCLEAR)
2495 clip_may_redraw_selection(gui.row, col, len);
2496
2497 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2498 {
2499 /* Invalidate the old physical cursor position if we wrote over it */
2500 if (gui.cursor_row == gui.row
2501 && gui.cursor_col >= col
2502 && gui.cursor_col < col + len)
2503 gui.cursor_is_valid = FALSE;
2504 }
2505
2506#ifdef FEAT_SIGN_ICONS
2507 if (draw_sign)
2508 /* Draw the sign on top of the spaces. */
2509 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002510# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002511 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (multi_sign)
2513 netbeans_draw_multisign_indicator(gui.row);
2514# endif
2515#endif
2516
2517 return OK;
2518}
2519
2520/*
2521 * Un-draw the cursor. Actually this just redraws the character at the given
2522 * position. The character just before it too, for when it was in bold.
2523 */
2524 void
2525gui_undraw_cursor()
2526{
2527 if (gui.cursor_is_valid)
2528 {
2529#ifdef FEAT_HANGULIN
2530 if (composing_hangul
2531 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2532 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2533 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2534 gui.norm_pixel, gui.back_pixel, 0);
2535 else
2536 {
2537#endif
2538 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2539 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2540 && gui.cursor_col > 0)
2541 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2542 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2543#ifdef FEAT_HANGULIN
2544 if (composing_hangul)
2545 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2546 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2547 }
2548#endif
2549 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2550 * here in case it wasn't needed to undraw it. */
2551 gui.cursor_is_valid = FALSE;
2552 }
2553}
2554
2555 void
2556gui_redraw(x, y, w, h)
2557 int x;
2558 int y;
2559 int w;
2560 int h;
2561{
2562 int row1, col1, row2, col2;
2563
2564 row1 = Y_2_ROW(y);
2565 col1 = X_2_COL(x);
2566 row2 = Y_2_ROW(y + h - 1);
2567 col2 = X_2_COL(x + w - 1);
2568
2569 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2570
2571 /*
2572 * We may need to redraw the cursor, but don't take it upon us to change
2573 * its location after a scroll.
2574 * (maybe be more strict even and test col too?)
2575 * These things may be outside the update/clipping region and reality may
2576 * not reflect Vims internal ideas if these operations are clipped away.
2577 */
2578 if (gui.row == gui.cursor_row)
2579 gui_update_cursor(TRUE, TRUE);
2580}
2581
2582/*
2583 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2584 * from col1 to col2 (inclusive).
2585 * Return TRUE when the character before the first drawn character has
2586 * different attributes (may have to be redrawn too).
2587 */
2588 int
2589gui_redraw_block(row1, col1, row2, col2, flags)
2590 int row1;
2591 int col1;
2592 int row2;
2593 int col2;
2594 int flags; /* flags for gui_outstr_nowrap() */
2595{
2596 int old_row, old_col;
2597 long_u old_hl_mask;
2598 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002599 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 int idx, len;
2601 int back, nback;
2602 int retval = FALSE;
2603#ifdef FEAT_MBYTE
2604 int orig_col1, orig_col2;
2605#endif
2606
2607 /* Don't try to update when ScreenLines is not valid */
2608 if (!screen_cleared || ScreenLines == NULL)
2609 return retval;
2610
2611 /* Don't try to draw outside the shell! */
2612 /* Check everything, strange values may be caused by a big border width */
2613 col1 = check_col(col1);
2614 col2 = check_col(col2);
2615 row1 = check_row(row1);
2616 row2 = check_row(row2);
2617
2618 /* Remember where our cursor was */
2619 old_row = gui.row;
2620 old_col = gui.col;
2621 old_hl_mask = gui.highlight_mask;
2622#ifdef FEAT_MBYTE
2623 orig_col1 = col1;
2624 orig_col2 = col2;
2625#endif
2626
2627 for (gui.row = row1; gui.row <= row2; gui.row++)
2628 {
2629#ifdef FEAT_MBYTE
2630 /* When only half of a double-wide character is in the block, include
2631 * the other half. */
2632 col1 = orig_col1;
2633 col2 = orig_col2;
2634 off = LineOffset[gui.row];
2635 if (enc_dbcs != 0)
2636 {
2637 if (col1 > 0)
2638 col1 -= dbcs_screen_head_off(ScreenLines + off,
2639 ScreenLines + off + col1);
2640 col2 += dbcs_screen_tail_off(ScreenLines + off,
2641 ScreenLines + off + col2);
2642 }
2643 else if (enc_utf8)
2644 {
2645 if (ScreenLines[off + col1] == 0)
2646 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002647# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2649 ++col2;
2650# endif
2651 }
2652#endif
2653 gui.col = col1;
2654 off = LineOffset[gui.row] + gui.col;
2655 len = col2 - col1 + 1;
2656
2657 /* Find how many chars back this highlighting starts, or where a space
2658 * is. Needed for when the bold trick is used */
2659 for (back = 0; back < col1; ++back)
2660 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2661 || ScreenLines[off - 1 - back] == ' ')
2662 break;
2663 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2664 && ScreenLines[off - 1] != ' ');
2665
2666 /* Break it up in strings of characters with the same attributes. */
2667 /* Print UTF-8 characters individually. */
2668 while (len > 0)
2669 {
2670 first_attr = ScreenAttrs[off];
2671 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002672#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (enc_utf8 && ScreenLinesUC[off] != 0)
2674 {
2675 /* output multi-byte character separately */
2676 nback = gui_screenchar(off, flags,
2677 (guicolor_T)0, (guicolor_T)0, back);
2678 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2679 idx = 2;
2680 else
2681 idx = 1;
2682 }
2683 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2684 {
2685 /* output double-byte, single-width character separately */
2686 nback = gui_screenchar(off, flags,
2687 (guicolor_T)0, (guicolor_T)0, back);
2688 idx = 1;
2689 }
2690 else
2691#endif
2692 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002693#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 for (idx = 0; idx < len; ++idx)
2695 {
2696 if (enc_utf8 && ScreenLines[off + idx] == 0)
2697 continue; /* skip second half of double-width char */
2698 if (ScreenAttrs[off + idx] != first_attr)
2699 break;
2700 }
2701 /* gui_screenstr() takes care of multibyte chars */
2702 nback = gui_screenstr(off, idx, flags,
2703 (guicolor_T)0, (guicolor_T)0, back);
2704#else
2705 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2706 idx++)
2707 {
2708# ifdef FEAT_MBYTE
2709 /* Stop at a multi-byte Unicode character. */
2710 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2711 break;
2712 if (enc_dbcs == DBCS_JPNU)
2713 {
2714 /* Stop at a double-byte single-width char. */
2715 if (ScreenLines[off + idx] == 0x8e)
2716 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002717 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 + off + idx) == 2)
2719 ++idx; /* skip second byte of double-byte char */
2720 }
2721# endif
2722 }
2723 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2724 (guicolor_T)0, (guicolor_T)0, back);
2725#endif
2726 }
2727 if (nback == FAIL)
2728 {
2729 /* Must back up to start drawing where a bold or italic word
2730 * starts. */
2731 off -= back;
2732 len += back;
2733 gui.col -= back;
2734 }
2735 else
2736 {
2737 off += idx;
2738 len -= idx;
2739 }
2740 back = 0;
2741 }
2742 }
2743
2744 /* Put the cursor back where it was */
2745 gui.row = old_row;
2746 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002747 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 return retval;
2750}
2751
2752 static void
2753gui_delete_lines(row, count)
2754 int row;
2755 int count;
2756{
2757 if (count <= 0)
2758 return;
2759
2760 if (row + count > gui.scroll_region_bot)
2761 /* Scrolled out of region, just blank the lines out */
2762 gui_clear_block(row, gui.scroll_region_left,
2763 gui.scroll_region_bot, gui.scroll_region_right);
2764 else
2765 {
2766 gui_mch_delete_lines(row, count);
2767
2768 /* If the cursor was in the deleted lines it's now gone. If the
2769 * cursor was in the scrolled lines adjust its position. */
2770 if (gui.cursor_row >= row
2771 && gui.cursor_col >= gui.scroll_region_left
2772 && gui.cursor_col <= gui.scroll_region_right)
2773 {
2774 if (gui.cursor_row < row + count)
2775 gui.cursor_is_valid = FALSE;
2776 else if (gui.cursor_row <= gui.scroll_region_bot)
2777 gui.cursor_row -= count;
2778 }
2779 }
2780}
2781
2782 static void
2783gui_insert_lines(row, count)
2784 int row;
2785 int count;
2786{
2787 if (count <= 0)
2788 return;
2789
2790 if (row + count > gui.scroll_region_bot)
2791 /* Scrolled out of region, just blank the lines out */
2792 gui_clear_block(row, gui.scroll_region_left,
2793 gui.scroll_region_bot, gui.scroll_region_right);
2794 else
2795 {
2796 gui_mch_insert_lines(row, count);
2797
2798 if (gui.cursor_row >= gui.row
2799 && gui.cursor_col >= gui.scroll_region_left
2800 && gui.cursor_col <= gui.scroll_region_right)
2801 {
2802 if (gui.cursor_row <= gui.scroll_region_bot - count)
2803 gui.cursor_row += count;
2804 else if (gui.cursor_row <= gui.scroll_region_bot)
2805 gui.cursor_is_valid = FALSE;
2806 }
2807 }
2808}
2809
2810/*
2811 * The main GUI input routine. Waits for a character from the keyboard.
2812 * wtime == -1 Wait forever.
2813 * wtime == 0 Don't wait.
2814 * wtime > 0 Wait wtime milliseconds for a character.
2815 * Returns OK if a character was found to be available within the given time,
2816 * or FAIL otherwise.
2817 */
2818 int
2819gui_wait_for_chars(wtime)
2820 long wtime;
2821{
2822 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002824#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 /*
2826 * If we're going to wait a bit, update the menus and mouse shape for the
2827 * current State.
2828 */
2829 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 gui_update_menus(0);
2831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 gui_mch_update();
2834 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 /* Before waiting, flush any output to the screen. */
2840 gui_mch_flush();
2841
2842 if (wtime > 0)
2843 {
2844 /* Blink when waiting for a character. Probably only does something
2845 * for showmatch() */
2846 gui_mch_start_blink();
2847 retval = gui_mch_wait_for_chars(wtime);
2848 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 return retval;
2850 }
2851
2852 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002853 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 */
2855 gui_mch_start_blink();
2856
Bram Moolenaar3918c952005-03-15 22:34:55 +00002857 retval = FAIL;
2858 /*
2859 * We may want to trigger the CursorHold event. First wait for
2860 * 'updatetime' and if nothing is typed within that time put the
2861 * K_CURSORHOLD key in the input buffer.
2862 */
2863 if (gui_mch_wait_for_chars(p_ut) == OK)
2864 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002866 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002868 char_u buf[3];
2869
2870 /* Put K_CURSORHOLD in the input buffer. */
2871 buf[0] = CSI;
2872 buf[1] = KS_EXTRA;
2873 buf[2] = (int)KE_CURSORHOLD;
2874 add_to_input_buf(buf, 3);
2875
2876 retval = OK;
2877 }
2878#endif
2879
2880 if (retval == FAIL)
2881 {
2882 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002883 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887 gui_mch_stop_blink();
2888 return retval;
2889}
2890
2891/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002892 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 */
2894 static void
2895fill_mouse_coord(p, col, row)
2896 char_u *p;
2897 int col;
2898 int row;
2899{
2900 p[0] = (char_u)(col / 128 + ' ' + 1);
2901 p[1] = (char_u)(col % 128 + ' ' + 1);
2902 p[2] = (char_u)(row / 128 + ' ' + 1);
2903 p[3] = (char_u)(row % 128 + ' ' + 1);
2904}
2905
2906/*
2907 * Generic mouse support function. Add a mouse event to the input buffer with
2908 * the given properties.
2909 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2910 * MOUSE_X1, MOUSE_X2
2911 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002912 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2913 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 * x, y --- Coordinates of mouse in pixels.
2915 * repeated_click --- TRUE if this click comes only a short time after a
2916 * previous click.
2917 * modifiers --- Bit field which may be any of the following modifiers
2918 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2919 * This function will ignore drag events where the mouse has not moved to a new
2920 * character.
2921 */
2922 void
2923gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2924 int button;
2925 int x;
2926 int y;
2927 int repeated_click;
2928 int_u modifiers;
2929{
2930 static int prev_row = 0, prev_col = 0;
2931 static int prev_button = -1;
2932 static int num_clicks = 1;
2933 char_u string[10];
2934 enum key_extra button_char;
2935 int row, col;
2936#ifdef FEAT_CLIPBOARD
2937 int checkfor;
2938 int did_clip = FALSE;
2939#endif
2940
2941 /*
2942 * Scrolling may happen at any time, also while a selection is present.
2943 */
2944 switch (button)
2945 {
2946 case MOUSE_X1:
2947 button_char = KE_X1MOUSE;
2948 goto button_set;
2949 case MOUSE_X2:
2950 button_char = KE_X2MOUSE;
2951 goto button_set;
2952 case MOUSE_4:
2953 button_char = KE_MOUSEDOWN;
2954 goto button_set;
2955 case MOUSE_5:
2956 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002957 goto button_set;
2958 case MOUSE_6:
2959 button_char = KE_MOUSELEFT;
2960 goto button_set;
2961 case MOUSE_7:
2962 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963button_set:
2964 {
2965 /* Don't put events in the input queue now. */
2966 if (hold_gui_events)
2967 return;
2968
2969 string[3] = CSI;
2970 string[4] = KS_EXTRA;
2971 string[5] = (int)button_char;
2972
2973 /* Pass the pointer coordinates of the scroll event so that we
2974 * know which window to scroll. */
2975 row = gui_xy2colrow(x, y, &col);
2976 string[6] = (char_u)(col / 128 + ' ' + 1);
2977 string[7] = (char_u)(col % 128 + ' ' + 1);
2978 string[8] = (char_u)(row / 128 + ' ' + 1);
2979 string[9] = (char_u)(row % 128 + ' ' + 1);
2980
2981 if (modifiers == 0)
2982 add_to_input_buf(string + 3, 7);
2983 else
2984 {
2985 string[0] = CSI;
2986 string[1] = KS_MODIFIER;
2987 string[2] = 0;
2988 if (modifiers & MOUSE_SHIFT)
2989 string[2] |= MOD_MASK_SHIFT;
2990 if (modifiers & MOUSE_CTRL)
2991 string[2] |= MOD_MASK_CTRL;
2992 if (modifiers & MOUSE_ALT)
2993 string[2] |= MOD_MASK_ALT;
2994 add_to_input_buf(string, 10);
2995 }
2996 return;
2997 }
2998 }
2999
3000#ifdef FEAT_CLIPBOARD
3001 /* If a clipboard selection is in progress, handle it */
3002 if (clip_star.state == SELECT_IN_PROGRESS)
3003 {
3004 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3005 return;
3006 }
3007
3008 /* Determine which mouse settings to look for based on the current mode */
3009 switch (get_real_state())
3010 {
3011 case NORMAL_BUSY:
3012 case OP_PENDING:
3013 case NORMAL: checkfor = MOUSE_NORMAL; break;
3014 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003015 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 case REPLACE:
3017 case REPLACE+LANGMAP:
3018#ifdef FEAT_VREPLACE
3019 case VREPLACE:
3020 case VREPLACE+LANGMAP:
3021#endif
3022 case INSERT:
3023 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3024 case ASKMORE:
3025 case HITRETURN: /* At the more- and hit-enter prompt pass the
3026 mouse event for a click on or below the
3027 message line. */
3028 if (Y_2_ROW(y) >= msg_row)
3029 checkfor = MOUSE_NORMAL;
3030 else
3031 checkfor = MOUSE_RETURN;
3032 break;
3033
3034 /*
3035 * On the command line, use the clipboard selection on all lines
3036 * but the command line. But not when pasting.
3037 */
3038 case CMDLINE:
3039 case CMDLINE+LANGMAP:
3040 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3041 checkfor = MOUSE_NONE;
3042 else
3043 checkfor = MOUSE_COMMAND;
3044 break;
3045
3046 default:
3047 checkfor = MOUSE_NONE;
3048 break;
3049 };
3050
3051 /*
3052 * Allow clipboard selection of text on the command line in "normal"
3053 * modes. Don't do this when dragging the status line, or extending a
3054 * Visual selection.
3055 */
3056 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
3057 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00003058# ifdef FEAT_WINDOWS
3059 + firstwin->w_winrow
3060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 && button != MOUSE_DRAG
3062# ifdef FEAT_MOUSESHAPE
3063 && !drag_status_line
3064# ifdef FEAT_VERTSPLIT
3065 && !drag_sep_line
3066# endif
3067# endif
3068 )
3069 checkfor = MOUSE_NONE;
3070
3071 /*
3072 * Use modeless selection when holding CTRL and SHIFT pressed.
3073 */
3074 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3075 checkfor = MOUSE_NONEF;
3076
3077 /*
3078 * In Ex mode, always use modeless selection.
3079 */
3080 if (exmode_active)
3081 checkfor = MOUSE_NONE;
3082
3083 /*
3084 * If the mouse settings say to not use the mouse, use the modeless
3085 * selection. But if Visual is active, assume that only the Visual area
3086 * will be selected.
3087 * Exception: On the command line, both the selection is used and a mouse
3088 * key is send.
3089 */
3090 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3091 {
3092#ifdef FEAT_VISUAL
3093 /* Don't do modeless selection in Visual mode. */
3094 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3095 return;
3096#endif
3097
3098 /*
3099 * When 'mousemodel' is "popup", shift-left is translated to right.
3100 * But not when also using Ctrl.
3101 */
3102 if (mouse_model_popup() && button == MOUSE_LEFT
3103 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3104 {
3105 button = MOUSE_RIGHT;
3106 modifiers &= ~ MOUSE_SHIFT;
3107 }
3108
3109 /* If the selection is done, allow the right button to extend it.
3110 * If the selection is cleared, allow the right button to start it
3111 * from the cursor position. */
3112 if (button == MOUSE_RIGHT)
3113 {
3114 if (clip_star.state == SELECT_CLEARED)
3115 {
3116 if (State & CMDLINE)
3117 {
3118 col = msg_col;
3119 row = msg_row;
3120 }
3121 else
3122 {
3123 col = curwin->w_wcol;
3124 row = curwin->w_wrow + W_WINROW(curwin);
3125 }
3126 clip_start_selection(col, row, FALSE);
3127 }
3128 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3129 repeated_click);
3130 did_clip = TRUE;
3131 }
3132 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003133 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3136 did_clip = TRUE;
3137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 /* Always allow pasting */
3140 if (button != MOUSE_MIDDLE)
3141 {
3142 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3143 return;
3144 if (checkfor != MOUSE_COMMAND)
3145 button = MOUSE_LEFT;
3146 }
3147 repeated_click = FALSE;
3148 }
3149
3150 if (clip_star.state != SELECT_CLEARED && !did_clip)
3151 clip_clear_selection();
3152#endif
3153
3154 /* Don't put events in the input queue now. */
3155 if (hold_gui_events)
3156 return;
3157
3158 row = gui_xy2colrow(x, y, &col);
3159
3160 /*
3161 * If we are dragging and the mouse hasn't moved far enough to be on a
3162 * different character, then don't send an event to vim.
3163 */
3164 if (button == MOUSE_DRAG)
3165 {
3166 if (row == prev_row && col == prev_col)
3167 return;
3168 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3169 if (y < 0)
3170 row = -1;
3171 }
3172
3173 /*
3174 * If topline has changed (window scrolled) since the last click, reset
3175 * repeated_click, because we don't want starting Visual mode when
3176 * clicking on a different character in the text.
3177 */
3178 if (curwin->w_topline != gui_prev_topline
3179#ifdef FEAT_DIFF
3180 || curwin->w_topfill != gui_prev_topfill
3181#endif
3182 )
3183 repeated_click = FALSE;
3184
3185 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3186 string[1] = KS_MOUSE;
3187 string[2] = KE_FILLER;
3188 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3189 {
3190 if (repeated_click)
3191 {
3192 /*
3193 * Handle multiple clicks. They only count if the mouse is still
3194 * pointing at the same character.
3195 */
3196 if (button != prev_button || row != prev_row || col != prev_col)
3197 num_clicks = 1;
3198 else if (++num_clicks > 4)
3199 num_clicks = 1;
3200 }
3201 else
3202 num_clicks = 1;
3203 prev_button = button;
3204 gui_prev_topline = curwin->w_topline;
3205#ifdef FEAT_DIFF
3206 gui_prev_topfill = curwin->w_topfill;
3207#endif
3208
3209 string[3] = (char_u)(button | 0x20);
3210 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3211 }
3212 else
3213 string[3] = (char_u)button;
3214
3215 string[3] |= modifiers;
3216 fill_mouse_coord(string + 4, col, row);
3217 add_to_input_buf(string, 8);
3218
3219 if (row < 0)
3220 prev_row = 0;
3221 else
3222 prev_row = row;
3223 prev_col = col;
3224
3225 /*
3226 * We need to make sure this is cleared since Athena doesn't tell us when
3227 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3228 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003229#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 gui.dragged_sb = SBAR_NONE;
3231#endif
3232}
3233
3234/*
3235 * Convert x and y coordinate to column and row in text window.
3236 * Corrects for multi-byte character.
3237 * returns column in "*colp" and row as return value;
3238 */
3239 int
3240gui_xy2colrow(x, y, colp)
3241 int x;
3242 int y;
3243 int *colp;
3244{
3245 int col = check_col(X_2_COL(x));
3246 int row = check_row(Y_2_ROW(y));
3247
3248#ifdef FEAT_MBYTE
3249 *colp = mb_fix_col(col, row);
3250#else
3251 *colp = col;
3252#endif
3253 return row;
3254}
3255
3256#if defined(FEAT_MENU) || defined(PROTO)
3257/*
3258 * Callback function for when a menu entry has been selected.
3259 */
3260 void
3261gui_menu_cb(menu)
3262 vimmenu_T *menu;
3263{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003264 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 /* Don't put events in the input queue now. */
3267 if (hold_gui_events)
3268 return;
3269
3270 bytes[0] = CSI;
3271 bytes[1] = KS_MENU;
3272 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003273 add_to_input_buf(bytes, 3);
3274 add_long_to_buf((long_u)menu, bytes);
3275 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276}
3277#endif
3278
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003279static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281/*
3282 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003283 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 * in p_go.
3285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 void
3287gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003288 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#ifdef FEAT_MENU
3291 static int prev_menu_is_active = -1;
3292#endif
3293#ifdef FEAT_TOOLBAR
3294 static int prev_toolbar = -1;
3295 int using_toolbar = FALSE;
3296#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003297#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003298 int using_tabline;
3299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef FEAT_FOOTER
3301 static int prev_footer = -1;
3302 int using_footer = FALSE;
3303#endif
3304#if defined(FEAT_MENU) && !defined(WIN16)
3305 static int prev_tearoff = -1;
3306 int using_tearoff = FALSE;
3307#endif
3308
3309 char_u *p;
3310 int i;
3311#ifdef FEAT_MENU
3312 int grey_old, grey_new;
3313 char_u *temp;
3314#endif
3315 win_T *wp;
3316 int need_set_size;
3317 int fix_size;
3318
3319#ifdef FEAT_MENU
3320 if (oldval != NULL && gui.in_use)
3321 {
3322 /*
3323 * Check if the menu's go from grey to non-grey or vise versa.
3324 */
3325 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3326 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3327 if (grey_old != grey_new)
3328 {
3329 temp = p_go;
3330 p_go = oldval;
3331 gui_update_menus(MENU_ALL_MODES);
3332 p_go = temp;
3333 }
3334 }
3335 gui.menu_is_active = FALSE;
3336#endif
3337
3338 for (i = 0; i < 3; i++)
3339 gui.which_scrollbars[i] = FALSE;
3340 for (p = p_go; *p; p++)
3341 switch (*p)
3342 {
3343 case GO_LEFT:
3344 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3345 break;
3346 case GO_RIGHT:
3347 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3348 break;
3349#ifdef FEAT_VERTSPLIT
3350 case GO_VLEFT:
3351 if (win_hasvertsplit())
3352 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3353 break;
3354 case GO_VRIGHT:
3355 if (win_hasvertsplit())
3356 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3357 break;
3358#endif
3359 case GO_BOT:
3360 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3361 break;
3362#ifdef FEAT_MENU
3363 case GO_MENUS:
3364 gui.menu_is_active = TRUE;
3365 break;
3366#endif
3367 case GO_GREY:
3368 /* make menu's have grey items, ignored here */
3369 break;
3370#ifdef FEAT_TOOLBAR
3371 case GO_TOOLBAR:
3372 using_toolbar = TRUE;
3373 break;
3374#endif
3375#ifdef FEAT_FOOTER
3376 case GO_FOOTER:
3377 using_footer = TRUE;
3378 break;
3379#endif
3380 case GO_TEAROFF:
3381#if defined(FEAT_MENU) && !defined(WIN16)
3382 using_tearoff = TRUE;
3383#endif
3384 break;
3385 default:
3386 /* Ignore options that are not supported */
3387 break;
3388 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (gui.in_use)
3391 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003392 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003394
3395#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003396 /* Update the GUI tab line, it may appear or disappear. This may
3397 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003398 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003399 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003400 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003401 /* We don't want a resize event change "Rows" here, save and
3402 * restore it. Resizing is handled below. */
3403 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003404 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003405 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003406 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003407 if (using_tabline)
3408 fix_size = TRUE;
3409 if (!gui_use_tabline())
3410 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3411 }
3412#endif
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 for (i = 0; i < 3; i++)
3415 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003416 /* The scrollbar needs to be updated when it is shown/unshown and
3417 * when switching tab pages. But the size only changes when it's
3418 * shown/unshown. Thus we need two places to remember whether a
3419 * scrollbar is there or not. */
3420 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003421#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003422 || gui.which_scrollbars[i]
3423 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003424#endif
3425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 if (i == SBAR_BOTTOM)
3428 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3429 gui.which_scrollbars[i]);
3430 else
3431 {
3432 FOR_ALL_WINDOWS(wp)
3433 {
3434 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3435 }
3436 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003437 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3438 {
3439 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003440 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003441 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003442 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003443 if (gui.which_scrollbars[i])
3444 fix_size = TRUE;
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003447#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003448 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003449#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003450 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452
3453#ifdef FEAT_MENU
3454 if (gui.menu_is_active != prev_menu_is_active)
3455 {
3456 /* We don't want a resize event change "Rows" here, save and
3457 * restore it. Resizing is handled below. */
3458 i = Rows;
3459 gui_mch_enable_menu(gui.menu_is_active);
3460 Rows = i;
3461 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003462 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (gui.menu_is_active)
3464 fix_size = TRUE;
3465 }
3466#endif
3467
3468#ifdef FEAT_TOOLBAR
3469 if (using_toolbar != prev_toolbar)
3470 {
3471 gui_mch_show_toolbar(using_toolbar);
3472 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003473 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (using_toolbar)
3475 fix_size = TRUE;
3476 }
3477#endif
3478#ifdef FEAT_FOOTER
3479 if (using_footer != prev_footer)
3480 {
3481 gui_mch_enable_footer(using_footer);
3482 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003483 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (using_footer)
3485 fix_size = TRUE;
3486 }
3487#endif
3488#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3489 if (using_tearoff != prev_tearoff)
3490 {
3491 gui_mch_toggle_tearoffs(using_tearoff);
3492 prev_tearoff = using_tearoff;
3493 }
3494#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003495 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003496 {
3497#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003498 long prev_Columns = Columns;
3499 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 /* Adjust the size of the window to make the text area keep the
3502 * same size and to avoid that part of our window is off-screen
3503 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003504 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003505
3506#ifdef FEAT_GUI_GTK
3507 /* GTK has the annoying habit of sending us resize events when
3508 * changing the window size ourselves. This mostly happens when
3509 * waiting for a character to arrive, quite unpredictably, and may
3510 * change Columns and Rows when we don't want it. Wait for a
3511 * character here to avoid this effect.
3512 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003513 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003514 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003515 * Don't change Rows when adding menu/toolbar/tabline.
3516 * Don't change Columns when adding vertical toolbar. */
3517 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003518 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003519 if ((need_set_size & RESIZE_VERT) == 0)
3520 Rows = prev_Rows;
3521 if ((need_set_size & RESIZE_HOR) == 0)
3522 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003523#endif
3524 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003525#ifdef FEAT_WINDOWS
3526 /* When the console tabline appears or disappears the window positions
3527 * change. */
3528 if (firstwin->w_winrow != tabline_height())
3529 shell_new_rows(); /* recompute window positions and heights */
3530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532}
3533
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003534#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3535/*
3536 * Return TRUE if the GUI is taking care of the tabline.
3537 * It may still be hidden if 'showtabline' is zero.
3538 */
3539 int
3540gui_use_tabline()
3541{
3542 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3543}
3544
3545/*
3546 * Return TRUE if the GUI is showing the tabline.
3547 * This uses 'showtabline'.
3548 */
3549 static int
3550gui_has_tabline()
3551{
3552 if (!gui_use_tabline()
3553 || p_stal == 0
3554 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3555 return FALSE;
3556 return TRUE;
3557}
3558
3559/*
3560 * Update the tabline.
3561 * This may display/undisplay the tabline and update the labels.
3562 */
3563 void
3564gui_update_tabline()
3565{
3566 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003567 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003568
3569 if (!gui.starting && starting == 0)
3570 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003571 /* Updating the tabline uses direct GUI commands, flush
3572 * outstanding instructions first. (esp. clear screen) */
3573 out_flush();
3574 gui_mch_flush();
3575
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003576 if (!showit != !shown)
3577 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003578 if (showit != 0)
3579 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003580
3581 /* When the tabs change from hidden to shown or from shown to
3582 * hidden the size of the text area should remain the same. */
3583 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003584 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003585 }
3586}
3587
3588/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003589 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003590 */
3591 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003592get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003593 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003594 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595{
3596 int modified = FALSE;
3597 char_u buf[40];
3598 int wincount;
3599 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003600 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003601
Bram Moolenaar57657d82006-04-21 22:12:41 +00003602 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003603 opt = (tooltip ? &p_gtt : &p_gtl);
3604 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003605 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003606 int use_sandbox = FALSE;
3607 int save_called_emsg = called_emsg;
3608 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003609 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003610 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3611 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003612
3613 called_emsg = FALSE;
3614
3615 printer_page_num = tabpage_index(tp);
3616# ifdef FEAT_EVAL
3617 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003618 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003619# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003620 /* It's almost as going to the tabpage, but without autocommands. */
3621 curtab->tp_firstwin = firstwin;
3622 curtab->tp_lastwin = lastwin;
3623 curtab->tp_curwin = curwin;
3624 save_curtab = curtab;
3625 curtab = tp;
3626 topframe = curtab->tp_topframe;
3627 firstwin = curtab->tp_firstwin;
3628 lastwin = curtab->tp_lastwin;
3629 curwin = curtab->tp_curwin;
3630 curbuf = curwin->w_buffer;
3631
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003632 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003633 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003634 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003635 STRCPY(NameBuff, res);
3636
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003637 /* Back to the original curtab. */
3638 curtab = save_curtab;
3639 topframe = curtab->tp_topframe;
3640 firstwin = curtab->tp_firstwin;
3641 lastwin = curtab->tp_lastwin;
3642 curwin = curtab->tp_curwin;
3643 curbuf = curwin->w_buffer;
3644
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003645 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003646 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003647 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003648 called_emsg |= save_called_emsg;
3649 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003650
3651 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3652 * use a default label. */
3653 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003654 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003655 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003656 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003657 if (!tooltip)
3658 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003659
3660 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3661 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3662 if (bufIsChanged(wp->w_buffer))
3663 modified = TRUE;
3664 if (modified || wincount > 1)
3665 {
3666 if (wincount > 1)
3667 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3668 else
3669 buf[0] = NUL;
3670 if (modified)
3671 STRCAT(buf, "+");
3672 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003673 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003674 mch_memmove(NameBuff, buf, STRLEN(buf));
3675 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676 }
3677}
3678
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003679/*
3680 * Send the event for clicking to select tab page "nr".
3681 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003682 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003683 */
3684 int
3685send_tabline_event(nr)
3686 int nr;
3687{
3688 char_u string[3];
3689
3690 if (nr == tabpage_index(curtab))
3691 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003692
3693 /* Don't put events in the input queue now. */
3694 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003695# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003696 || cmdwin_type != 0
3697# endif
3698 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003699 {
3700 /* Set it back to the current tab page. */
3701 gui_mch_set_curtab(tabpage_index(curtab));
3702 return FALSE;
3703 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003704
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003705 string[0] = CSI;
3706 string[1] = KS_TABLINE;
3707 string[2] = KE_FILLER;
3708 add_to_input_buf(string, 3);
3709 string[0] = nr;
3710 add_to_input_buf_csi(string, 1);
3711 return TRUE;
3712}
3713
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003714/*
3715 * Send a tabline menu event
3716 */
3717 void
3718send_tabline_menu_event(tabidx, event)
3719 int tabidx;
3720 int event;
3721{
3722 char_u string[3];
3723
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003724 /* Don't put events in the input queue now. */
3725 if (hold_gui_events)
3726 return;
3727
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003728 string[0] = CSI;
3729 string[1] = KS_TABMENU;
3730 string[2] = KE_FILLER;
3731 add_to_input_buf(string, 3);
3732 string[0] = tabidx;
3733 string[1] = (char_u)(long)event;
3734 add_to_input_buf_csi(string, 2);
3735}
3736
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739/*
3740 * Scrollbar stuff:
3741 */
3742
Bram Moolenaare45828b2006-02-15 22:12:56 +00003743#if defined(FEAT_WINDOWS) || defined(PROTO)
3744/*
3745 * Remove all scrollbars. Used before switching to another tab page.
3746 */
3747 void
3748gui_remove_scrollbars()
3749{
3750 int i;
3751 win_T *wp;
3752
3753 for (i = 0; i < 3; i++)
3754 {
3755 if (i == SBAR_BOTTOM)
3756 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3757 else
3758 {
3759 FOR_ALL_WINDOWS(wp)
3760 {
3761 gui_do_scrollbar(wp, i, FALSE);
3762 }
3763 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003764 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003765 }
3766}
3767#endif
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 void
3770gui_create_scrollbar(sb, type, wp)
3771 scrollbar_T *sb;
3772 int type;
3773 win_T *wp;
3774{
3775 static int sbar_ident = 0;
3776
3777 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3778 sb->wp = wp;
3779 sb->type = type;
3780 sb->value = 0;
3781#ifdef FEAT_GUI_ATHENA
3782 sb->pixval = 0;
3783#endif
3784 sb->size = 1;
3785 sb->max = 1;
3786 sb->top = 0;
3787 sb->height = 0;
3788#ifdef FEAT_VERTSPLIT
3789 sb->width = 0;
3790#endif
3791 sb->status_height = 0;
3792 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3793}
3794
3795/*
3796 * Find the scrollbar with the given index.
3797 */
3798 scrollbar_T *
3799gui_find_scrollbar(ident)
3800 long ident;
3801{
3802 win_T *wp;
3803
3804 if (gui.bottom_sbar.ident == ident)
3805 return &gui.bottom_sbar;
3806 FOR_ALL_WINDOWS(wp)
3807 {
3808 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3809 return &wp->w_scrollbars[SBAR_LEFT];
3810 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3811 return &wp->w_scrollbars[SBAR_RIGHT];
3812 }
3813 return NULL;
3814}
3815
3816/*
3817 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3818 *
3819 * For Win32, Macintosh and GTK+ 2:
3820 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3821 * you stop dragging the scrollbar. We get here each time the scrollbar is
3822 * dragged another pixel, but as far as the rest of vim goes, it thinks
3823 * we're just hanging in the call to DispatchMessage() in
3824 * process_message(). The DispatchMessage() call that hangs was passed a
3825 * mouse button click event in the scrollbar window. -- webb.
3826 *
3827 * Solution: Do the scrolling right here. But only when allowed.
3828 * Ignore the scrollbars while executing an external command or when there
3829 * are still characters to be processed.
3830 */
3831 void
3832gui_drag_scrollbar(sb, value, still_dragging)
3833 scrollbar_T *sb;
3834 long value;
3835 int still_dragging;
3836{
3837#ifdef FEAT_WINDOWS
3838 win_T *wp;
3839#endif
3840 int sb_num;
3841#ifdef USE_ON_FLY_SCROLL
3842 colnr_T old_leftcol = curwin->w_leftcol;
3843# ifdef FEAT_SCROLLBIND
3844 linenr_T old_topline = curwin->w_topline;
3845# endif
3846# ifdef FEAT_DIFF
3847 int old_topfill = curwin->w_topfill;
3848# endif
3849#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003850 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 int byte_count;
3852#endif
3853
3854 if (sb == NULL)
3855 return;
3856
3857 /* Don't put events in the input queue now. */
3858 if (hold_gui_events)
3859 return;
3860
3861#ifdef FEAT_CMDWIN
3862 if (cmdwin_type != 0 && sb->wp != curwin)
3863 return;
3864#endif
3865
3866 if (still_dragging)
3867 {
3868 if (sb->wp == NULL)
3869 gui.dragged_sb = SBAR_BOTTOM;
3870 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3871 gui.dragged_sb = SBAR_LEFT;
3872 else
3873 gui.dragged_sb = SBAR_RIGHT;
3874 gui.dragged_wp = sb->wp;
3875 }
3876 else
3877 {
3878 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003879#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 /* Keep the "dragged_wp" value until after the scrolling, for when the
3881 * moust button is released. GTK2 doesn't send the button-up event. */
3882 gui.dragged_wp = NULL;
3883#endif
3884 }
3885
3886 /* Vertical sbar info is kept in the first sbar (the left one) */
3887 if (sb->wp != NULL)
3888 sb = &sb->wp->w_scrollbars[0];
3889
3890 /*
3891 * Check validity of value
3892 */
3893 if (value < 0)
3894 value = 0;
3895#ifdef SCROLL_PAST_END
3896 else if (value > sb->max)
3897 value = sb->max;
3898#else
3899 if (value > sb->max - sb->size + 1)
3900 value = sb->max - sb->size + 1;
3901#endif
3902
3903 sb->value = value;
3904
3905#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003906 /* When not allowed to do the scrolling right now, return.
3907 * This also checked input_available(), but that causes the first click in
3908 * a scrollbar to be ignored when Vim doesn't have focus. */
3909 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return;
3911#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003912#ifdef FEAT_INS_EXPAND
3913 /* Disallow scrolling the current window when the completion popup menu is
3914 * visible. */
3915 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3916 return;
3917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919#ifdef FEAT_RIGHTLEFT
3920 if (sb->wp == NULL && curwin->w_p_rl)
3921 {
3922 value = sb->max + 1 - sb->size - value;
3923 if (value < 0)
3924 value = 0;
3925 }
3926#endif
3927
3928 if (sb->wp != NULL) /* vertical scrollbar */
3929 {
3930 sb_num = 0;
3931#ifdef FEAT_WINDOWS
3932 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3933 sb_num++;
3934 if (wp == NULL)
3935 return;
3936#else
3937 if (sb->wp != curwin)
3938 return;
3939#endif
3940
3941#ifdef USE_ON_FLY_SCROLL
3942 current_scrollbar = sb_num;
3943 scrollbar_value = value;
3944 if (State & NORMAL)
3945 {
3946 gui_do_scroll();
3947 setcursor();
3948 }
3949 else if (State & INSERT)
3950 {
3951 ins_scroll();
3952 setcursor();
3953 }
3954 else if (State & CMDLINE)
3955 {
3956 if (msg_scrolled == 0)
3957 {
3958 gui_do_scroll();
3959 redrawcmdline();
3960 }
3961 }
3962# ifdef FEAT_FOLDING
3963 /* Value may have been changed for closed fold. */
3964 sb->value = sb->wp->w_topline - 1;
3965# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003966
3967 /* When dragging one scrollbar and there is another one at the other
3968 * side move the thumb of that one too. */
3969 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3970 gui_mch_set_scrollbar_thumb(
3971 &sb->wp->w_scrollbars[
3972 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3973 ? SBAR_LEFT : SBAR_RIGHT],
3974 sb->value, sb->size, sb->max);
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#else
3977 bytes[0] = CSI;
3978 bytes[1] = KS_VER_SCROLLBAR;
3979 bytes[2] = KE_FILLER;
3980 bytes[3] = (char_u)sb_num;
3981 byte_count = 4;
3982#endif
3983 }
3984 else
3985 {
3986#ifdef USE_ON_FLY_SCROLL
3987 scrollbar_value = value;
3988
3989 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003990 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else if (State & INSERT)
3992 ins_horscroll();
3993 else if (State & CMDLINE)
3994 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003995 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003997 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 redrawcmdline();
3999 }
4000 }
4001 if (old_leftcol != curwin->w_leftcol)
4002 {
4003 updateWindow(curwin); /* update window, status and cmdline */
4004 setcursor();
4005 }
4006#else
4007 bytes[0] = CSI;
4008 bytes[1] = KS_HOR_SCROLLBAR;
4009 bytes[2] = KE_FILLER;
4010 byte_count = 3;
4011#endif
4012 }
4013
4014#ifdef USE_ON_FLY_SCROLL
4015# ifdef FEAT_SCROLLBIND
4016 /*
4017 * synchronize other windows, as necessary according to 'scrollbind'
4018 */
4019 if (curwin->w_p_scb
4020 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4021 || (sb->wp == curwin && (curwin->w_topline != old_topline
4022# ifdef FEAT_DIFF
4023 || curwin->w_topfill != old_topfill
4024# endif
4025 ))))
4026 {
4027 do_check_scrollbind(TRUE);
4028 /* need to update the window right here */
4029 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4030 if (wp->w_redr_type > 0)
4031 updateWindow(wp);
4032 setcursor();
4033 }
4034# endif
4035 out_flush();
4036 gui_update_cursor(FALSE, TRUE);
4037#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004038 add_to_input_buf(bytes, byte_count);
4039 add_long_to_buf((long_u)value, bytes);
4040 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#endif
4042}
4043
4044/*
4045 * Scrollbar stuff:
4046 */
4047
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004048#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004049/*
4050 * Called when something in the window layout has changed.
4051 */
4052 void
4053gui_may_update_scrollbars()
4054{
4055 if (gui.in_use && starting == 0)
4056 {
4057 out_flush();
4058 gui_init_which_components(NULL);
4059 gui_update_scrollbars(TRUE);
4060 }
4061 need_mouse_correct = TRUE;
4062}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004063#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 void
4066gui_update_scrollbars(force)
4067 int force; /* Force all scrollbars to get updated */
4068{
4069 win_T *wp;
4070 scrollbar_T *sb;
4071 long val, size, max; /* need 32 bits here */
4072 int which_sb;
4073 int h, y;
4074#ifdef FEAT_VERTSPLIT
4075 static win_T *prev_curwin = NULL;
4076#endif
4077
4078 /* Update the horizontal scrollbar */
4079 gui_update_horiz_scrollbar(force);
4080
4081#ifndef WIN3264
4082 /* Return straight away if there is neither a left nor right scrollbar.
4083 * On MS-Windows this is required anyway for scrollwheel messages. */
4084 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4085 return;
4086#endif
4087
4088 /*
4089 * Don't want to update a scrollbar while we're dragging it. But if we
4090 * have both a left and right scrollbar, and we drag one of them, we still
4091 * need to update the other one.
4092 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004093 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4094 && gui.which_scrollbars[SBAR_LEFT]
4095 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
4097 /*
4098 * If we have two scrollbars and one of them is being dragged, just
4099 * copy the scrollbar position from the dragged one to the other one.
4100 */
4101 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4102 if (gui.dragged_wp != NULL)
4103 gui_mch_set_scrollbar_thumb(
4104 &gui.dragged_wp->w_scrollbars[which_sb],
4105 gui.dragged_wp->w_scrollbars[0].value,
4106 gui.dragged_wp->w_scrollbars[0].size,
4107 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109
4110 /* avoid that moving components around generates events */
4111 ++hold_gui_events;
4112
4113 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4114 {
4115 if (wp->w_buffer == NULL) /* just in case */
4116 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004117 /* Skip a scrollbar that is being dragged. */
4118 if (!force && (gui.dragged_sb == SBAR_LEFT
4119 || gui.dragged_sb == SBAR_RIGHT)
4120 && gui.dragged_wp == wp)
4121 continue;
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#ifdef SCROLL_PAST_END
4124 max = wp->w_buffer->b_ml.ml_line_count - 1;
4125#else
4126 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4127#endif
4128 if (max < 0) /* empty buffer */
4129 max = 0;
4130 val = wp->w_topline - 1;
4131 size = wp->w_height;
4132#ifdef SCROLL_PAST_END
4133 if (val > max) /* just in case */
4134 val = max;
4135#else
4136 if (size > max + 1) /* just in case */
4137 size = max + 1;
4138 if (val > max - size + 1)
4139 val = max - size + 1;
4140#endif
4141 if (val < 0) /* minimal value is 0 */
4142 val = 0;
4143
4144 /*
4145 * Scrollbar at index 0 (the left one) contains all the information.
4146 * It would be the same info for left and right so we just store it for
4147 * one of them.
4148 */
4149 sb = &wp->w_scrollbars[0];
4150
4151 /*
4152 * Note: no check for valid w_botline. If it's not valid the
4153 * scrollbars will be updated later anyway.
4154 */
4155 if (size < 1 || wp->w_botline - 2 > max)
4156 {
4157 /*
4158 * This can happen during changing files. Just don't update the
4159 * scrollbar for now.
4160 */
4161 sb->height = 0; /* Force update next time */
4162 if (gui.which_scrollbars[SBAR_LEFT])
4163 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4164 if (gui.which_scrollbars[SBAR_RIGHT])
4165 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4166 continue;
4167 }
4168 if (force || sb->height != wp->w_height
4169#ifdef FEAT_WINDOWS
4170 || sb->top != wp->w_winrow
4171 || sb->status_height != wp->w_status_height
4172# ifdef FEAT_VERTSPLIT
4173 || sb->width != wp->w_width
4174 || prev_curwin != curwin
4175# endif
4176#endif
4177 )
4178 {
4179 /* Height, width or position of scrollbar has changed. For
4180 * vertical split: curwin changed. */
4181 sb->height = wp->w_height;
4182#ifdef FEAT_WINDOWS
4183 sb->top = wp->w_winrow;
4184 sb->status_height = wp->w_status_height;
4185# ifdef FEAT_VERTSPLIT
4186 sb->width = wp->w_width;
4187# endif
4188#endif
4189
4190 /* Calculate height and position in pixels */
4191 h = (sb->height + sb->status_height) * gui.char_height;
4192 y = sb->top * gui.char_height + gui.border_offset;
4193#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4194 if (gui.menu_is_active)
4195 y += gui.menu_height;
4196#endif
4197
4198#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4199 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4200# ifdef FEAT_GUI_ATHENA
4201 y += gui.toolbar_height;
4202# else
4203# ifdef FEAT_GUI_MSWIN
4204 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4205# endif
4206# endif
4207#endif
4208
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004209#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4210 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004211 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004212#endif
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#ifdef FEAT_WINDOWS
4215 if (wp->w_winrow == 0)
4216#endif
4217 {
4218 /* Height of top scrollbar includes width of top border */
4219 h += gui.border_offset;
4220 y -= gui.border_offset;
4221 }
4222 if (gui.which_scrollbars[SBAR_LEFT])
4223 {
4224 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4225 gui.left_sbar_x, y,
4226 gui.scrollbar_width, h);
4227 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4228 }
4229 if (gui.which_scrollbars[SBAR_RIGHT])
4230 {
4231 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4232 gui.right_sbar_x, y,
4233 gui.scrollbar_width, h);
4234 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4235 }
4236 }
4237
4238 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4239 * checking if the thumb moved at least a pixel. Only do this for
4240 * Athena, most other GUIs require the update anyway to make the
4241 * arrows work. */
4242#ifdef FEAT_GUI_ATHENA
4243 if (max == 0)
4244 y = 0;
4245 else
4246 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4247 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4248#else
4249 if (force || sb->value != val || sb->size != size || sb->max != max)
4250#endif
4251 {
4252 /* Thumb of scrollbar has moved */
4253 sb->value = val;
4254#ifdef FEAT_GUI_ATHENA
4255 sb->pixval = y;
4256#endif
4257 sb->size = size;
4258 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004259 if (gui.which_scrollbars[SBAR_LEFT]
4260 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4262 val, size, max);
4263 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004264 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4266 val, size, max);
4267 }
4268 }
4269#ifdef FEAT_VERTSPLIT
4270 prev_curwin = curwin;
4271#endif
4272 --hold_gui_events;
4273}
4274
4275/*
4276 * Enable or disable a scrollbar.
4277 * Check for scrollbars for vertically split windows which are not enabled
4278 * sometimes.
4279 */
4280 static void
4281gui_do_scrollbar(wp, which, enable)
4282 win_T *wp;
4283 int which; /* SBAR_LEFT or SBAR_RIGHT */
4284 int enable; /* TRUE to enable scrollbar */
4285{
4286#ifdef FEAT_VERTSPLIT
4287 int midcol = curwin->w_wincol + curwin->w_width / 2;
4288 int has_midcol = (wp->w_wincol <= midcol
4289 && wp->w_wincol + wp->w_width >= midcol);
4290
4291 /* Only enable scrollbars that contain the middle column of the current
4292 * window. */
4293 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4294 {
4295 /* Scrollbars only on one side. Don't enable scrollbars that don't
4296 * contain the middle column of the current window. */
4297 if (!has_midcol)
4298 enable = FALSE;
4299 }
4300 else
4301 {
4302 /* Scrollbars on both sides. Don't enable scrollbars that neither
4303 * contain the middle column of the current window nor are on the far
4304 * side. */
4305 if (midcol > Columns / 2)
4306 {
4307 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4308 enable = FALSE;
4309 }
4310 else
4311 {
4312 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4313 : !has_midcol)
4314 enable = FALSE;
4315 }
4316 }
4317#endif
4318 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4319}
4320
4321/*
4322 * Scroll a window according to the values set in the globals current_scrollbar
4323 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4324 * or FALSE otherwise.
4325 */
4326 int
4327gui_do_scroll()
4328{
4329 win_T *wp, *save_wp;
4330 int i;
4331 long nlines;
4332 pos_T old_cursor;
4333 linenr_T old_topline;
4334#ifdef FEAT_DIFF
4335 int old_topfill;
4336#endif
4337
4338 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4339 if (wp == NULL)
4340 break;
4341 if (wp == NULL)
4342 /* Couldn't find window */
4343 return FALSE;
4344
4345 /*
4346 * Compute number of lines to scroll. If zero, nothing to do.
4347 */
4348 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4349 if (nlines == 0)
4350 return FALSE;
4351
4352 save_wp = curwin;
4353 old_topline = wp->w_topline;
4354#ifdef FEAT_DIFF
4355 old_topfill = wp->w_topfill;
4356#endif
4357 old_cursor = wp->w_cursor;
4358 curwin = wp;
4359 curbuf = wp->w_buffer;
4360 if (nlines < 0)
4361 scrolldown(-nlines, gui.dragged_wp == NULL);
4362 else
4363 scrollup(nlines, gui.dragged_wp == NULL);
4364 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4365 * the mouse-up event already, but we still want it to behave like when
4366 * dragging. But not the next click in an arrow. */
4367 if (gui.dragged_sb == SBAR_NONE)
4368 gui.dragged_wp = NULL;
4369
4370 if (old_topline != wp->w_topline
4371#ifdef FEAT_DIFF
4372 || old_topfill != wp->w_topfill
4373#endif
4374 )
4375 {
4376 if (p_so != 0)
4377 {
4378 cursor_correct(); /* fix window for 'so' */
4379 update_topline(); /* avoid up/down jump */
4380 }
4381 if (old_cursor.lnum != wp->w_cursor.lnum)
4382 coladvance(wp->w_curswant);
4383#ifdef FEAT_SCROLLBIND
4384 wp->w_scbind_pos = wp->w_topline;
4385#endif
4386 }
4387
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004388 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4389 validate_cursor();
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 curwin = save_wp;
4392 curbuf = save_wp->w_buffer;
4393
4394 /*
4395 * Don't call updateWindow() when nothing has changed (it will overwrite
4396 * the status line!).
4397 */
4398 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004399 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#ifdef FEAT_DIFF
4401 || old_topfill != wp->w_topfill
4402#endif
4403 )
4404 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004405 int type = VALID;
4406
4407#ifdef FEAT_INS_EXPAND
4408 if (pum_visible())
4409 {
4410 type = NOT_VALID;
4411 wp->w_lines_valid = 0;
4412 }
4413#endif
4414 /* Don't set must_redraw here, it may cause the popup menu to
4415 * disappear when losing focus after a scrollbar drag. */
4416 if (wp->w_redr_type < type)
4417 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 updateWindow(wp); /* update window, status line, and cmdline */
4419 }
4420
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004421#ifdef FEAT_INS_EXPAND
4422 /* May need to redraw the popup menu. */
4423 if (pum_visible())
4424 pum_redraw();
4425#endif
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4428}
4429
4430
4431/*
4432 * Horizontal scrollbar stuff:
4433 */
4434
4435/*
4436 * Return length of line "lnum" for horizontal scrolling.
4437 */
4438 static colnr_T
4439scroll_line_len(lnum)
4440 linenr_T lnum;
4441{
4442 char_u *p;
4443 colnr_T col;
4444 int w;
4445
4446 p = ml_get(lnum);
4447 col = 0;
4448 if (*p != NUL)
4449 for (;;)
4450 {
4451 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004452 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (*p == NUL) /* don't count the last character */
4454 break;
4455 col += w;
4456 }
4457 return col;
4458}
4459
4460/* Remember which line is currently the longest, so that we don't have to
4461 * search for it when scrolling horizontally. */
4462static linenr_T longest_lnum = 0;
4463
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004464/*
4465 * Find longest visible line number. If this is not possible (or not desired,
4466 * by setting 'h' in "guioptions") then the current line number is returned.
4467 */
4468 static linenr_T
4469gui_find_longest_lnum()
4470{
4471 linenr_T ret = 0;
4472
4473 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4474 * line numbers, topline and botline can be invalid when displaying is
4475 * postponed. */
4476 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4477 && curwin->w_topline <= curwin->w_cursor.lnum
4478 && curwin->w_botline > curwin->w_cursor.lnum
4479 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4480 {
4481 linenr_T lnum;
4482 colnr_T n;
4483 long max = 0;
4484
4485 /* Use maximum of all visible lines. Remember the lnum of the
4486 * longest line, closest to the cursor line. Used when scrolling
4487 * below. */
4488 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4489 {
4490 n = scroll_line_len(lnum);
4491 if (n > (colnr_T)max)
4492 {
4493 max = n;
4494 ret = lnum;
4495 }
4496 else if (n == (colnr_T)max
4497 && abs((int)(lnum - curwin->w_cursor.lnum))
4498 < abs((int)(ret - curwin->w_cursor.lnum)))
4499 ret = lnum;
4500 }
4501 }
4502 else
4503 /* Use cursor line only. */
4504 ret = curwin->w_cursor.lnum;
4505
4506 return ret;
4507}
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 static void
4510gui_update_horiz_scrollbar(force)
4511 int force;
4512{
4513 long value, size, max; /* need 32 bit ints here */
4514
4515 if (!gui.which_scrollbars[SBAR_BOTTOM])
4516 return;
4517
4518 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4519 return;
4520
4521 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4522 return;
4523
4524 /*
4525 * It is possible for the cursor to be invalid if we're in the middle of
4526 * something (like changing files). If so, don't do anything for now.
4527 */
4528 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4529 {
4530 gui.bottom_sbar.value = -1;
4531 return;
4532 }
4533
4534 size = W_WIDTH(curwin);
4535 if (curwin->w_p_wrap)
4536 {
4537 value = 0;
4538#ifdef SCROLL_PAST_END
4539 max = 0;
4540#else
4541 max = W_WIDTH(curwin) - 1;
4542#endif
4543 }
4544 else
4545 {
4546 value = curwin->w_leftcol;
4547
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004548 longest_lnum = gui_find_longest_lnum();
4549 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#ifdef FEAT_VIRTUALEDIT
4552 if (virtual_active())
4553 {
4554 /* May move the cursor even further to the right. */
4555 if (curwin->w_virtcol >= (colnr_T)max)
4556 max = curwin->w_virtcol;
4557 }
4558#endif
4559
4560#ifndef SCROLL_PAST_END
4561 max += W_WIDTH(curwin) - 1;
4562#endif
4563 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004564 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 size -= curwin_col_off();
4566#ifndef SCROLL_PAST_END
4567 max -= curwin_col_off();
4568#endif
4569 }
4570
4571#ifndef SCROLL_PAST_END
4572 if (value > max - size + 1)
4573 value = max - size + 1; /* limit the value to allowable range */
4574#endif
4575
4576#ifdef FEAT_RIGHTLEFT
4577 if (curwin->w_p_rl)
4578 {
4579 value = max + 1 - size - value;
4580 if (value < 0)
4581 {
4582 size += value;
4583 value = 0;
4584 }
4585 }
4586#endif
4587 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4588 && max == gui.bottom_sbar.max)
4589 return;
4590
4591 gui.bottom_sbar.value = value;
4592 gui.bottom_sbar.size = size;
4593 gui.bottom_sbar.max = max;
4594 gui.prev_wrap = curwin->w_p_wrap;
4595
4596 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4597}
4598
4599/*
4600 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4601 */
4602 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004603gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004604 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004605 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 /* no wrapping, no scrolling */
4608 if (curwin->w_p_wrap)
4609 return FALSE;
4610
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004611 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 return FALSE;
4613
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004614 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004617 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004619 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004620 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004622 if (compute_longest_lnum)
4623 {
4624 curwin->w_cursor.lnum = gui_find_longest_lnum();
4625 curwin->w_cursor.col = 0;
4626 }
4627 /* Do a sanity check on "longest_lnum", just in case. */
4628 else if (longest_lnum >= curwin->w_topline
4629 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
4631 curwin->w_cursor.lnum = longest_lnum;
4632 curwin->w_cursor.col = 0;
4633 }
4634 }
4635
4636 return leftcol_changed();
4637}
4638
4639/*
4640 * Check that none of the colors are the same as the background color
4641 */
4642 void
4643gui_check_colors()
4644{
4645 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4646 {
4647 gui_set_bg_color((char_u *)"White");
4648 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4649 gui_set_fg_color((char_u *)"Black");
4650 }
4651}
4652
Bram Moolenaar3918c952005-03-15 22:34:55 +00004653 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654gui_set_fg_color(name)
4655 char_u *name;
4656{
4657 gui.norm_pixel = gui_get_color(name);
4658 hl_set_fg_color_name(vim_strsave(name));
4659}
4660
Bram Moolenaar3918c952005-03-15 22:34:55 +00004661 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662gui_set_bg_color(name)
4663 char_u *name;
4664{
4665 gui.back_pixel = gui_get_color(name);
4666 hl_set_bg_color_name(vim_strsave(name));
4667}
4668
4669/*
4670 * Allocate a color by name.
4671 * Returns INVALCOLOR and gives an error message when failed.
4672 */
4673 guicolor_T
4674gui_get_color(name)
4675 char_u *name;
4676{
4677 guicolor_T t;
4678
4679 if (*name == NUL)
4680 return INVALCOLOR;
4681 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004684#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 && gui.in_use
4686#endif
4687 )
4688 EMSG2(_("E254: Cannot allocate color %s"), name);
4689 return t;
4690}
4691
4692/*
4693 * Return the grey value of a color (range 0-255).
4694 */
4695 int
4696gui_get_lightness(pixel)
4697 guicolor_T pixel;
4698{
4699 long_u rgb = gui_mch_get_rgb(pixel);
4700
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004701 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004702 + (((rgb >> 8) & 0xff) * 587)
4703 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704}
4705
4706#if defined(FEAT_GUI_X11) || defined(PROTO)
4707 void
4708gui_new_scrollbar_colors()
4709{
4710 win_T *wp;
4711
4712 /* Nothing to do if GUI hasn't started yet. */
4713 if (!gui.in_use)
4714 return;
4715
4716 FOR_ALL_WINDOWS(wp)
4717 {
4718 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4719 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4720 }
4721 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4722}
4723#endif
4724
4725/*
4726 * Call this when focus has changed.
4727 */
4728 void
4729gui_focus_change(in_focus)
4730 int in_focus;
4731{
4732/*
4733 * Skip this code to avoid drawing the cursor when debugging and switching
4734 * between the debugger window and gvim.
4735 */
4736#if 1
4737 gui.in_focus = in_focus;
4738 out_flush(); /* make sure output has been written */
4739 gui_update_cursor(TRUE, FALSE);
4740
4741# ifdef FEAT_XIM
4742 xim_set_focus(in_focus);
4743# endif
4744
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004745 /* Put events in the input queue only when allowed.
4746 * ui_focus_change() isn't called directly, because it invokes
4747 * autocommands and that must not happen asynchronously. */
4748 if (!hold_gui_events)
4749 {
4750 char_u bytes[3];
4751
4752 bytes[0] = CSI;
4753 bytes[1] = KS_EXTRA;
4754 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4755 add_to_input_buf(bytes, 3);
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#endif
4758}
4759
4760/*
4761 * Called when the mouse moved (but not when dragging).
4762 */
4763 void
4764gui_mouse_moved(x, y)
4765 int x;
4766 int y;
4767{
4768 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004769 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaard3667a22006-03-16 21:35:52 +00004771 /* Ignore this while still starting up. */
4772 if (!gui.in_use || gui.starting)
4773 return;
4774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#ifdef FEAT_MOUSESHAPE
4776 /* Get window pointer, and update mouse shape as well. */
4777 wp = xy2win(x, y);
4778#endif
4779
4780 /* Only handle this when 'mousefocus' set and ... */
4781 if (p_mousef
4782 && !hold_gui_events /* not holding events */
4783 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4784 && State != HITRETURN /* but not hit-return prompt */
4785 && msg_scrolled == 0 /* no scrolled message */
4786 && !need_mouse_correct /* not moving the pointer */
4787 && gui.in_focus) /* gvim in focus */
4788 {
4789 /* Don't move the mouse when it's left or right of the Vim window */
4790 if (x < 0 || x > Columns * gui.char_width)
4791 return;
4792#ifndef FEAT_MOUSESHAPE
4793 wp = xy2win(x, y);
4794#endif
4795 if (wp == curwin || wp == NULL)
4796 return; /* still in the same old window, or none at all */
4797
Bram Moolenaar9c102382006-05-03 21:26:49 +00004798#ifdef FEAT_WINDOWS
4799 /* Ignore position in the tab pages line. */
4800 if (Y_2_ROW(y) < tabline_height())
4801 return;
4802#endif
4803
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 /*
4805 * format a mouse click on status line input
4806 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004807 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4808 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 */
4810 if (finish_op)
4811 {
4812 /* abort the current operator first */
4813 st[0] = ESC;
4814 add_to_input_buf(st, 1);
4815 }
4816 st[0] = CSI;
4817 st[1] = KS_MOUSE;
4818 st[2] = KE_FILLER;
4819 st[3] = (char_u)MOUSE_LEFT;
4820 fill_mouse_coord(st + 4,
4821#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004822 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#else
4824 -1,
4825#endif
4826 wp->w_height + W_WINROW(wp));
4827
4828 add_to_input_buf(st, 8);
4829 st[3] = (char_u)MOUSE_RELEASE;
4830 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#ifdef FEAT_GUI_GTK
4832 /* Need to wake up the main loop */
4833 if (gtk_main_level() > 0)
4834 gtk_main_quit();
4835#endif
4836 }
4837}
4838
4839/*
4840 * Called when mouse should be moved to window with focus.
4841 */
4842 void
4843gui_mouse_correct()
4844{
4845 int x, y;
4846 win_T *wp = NULL;
4847
4848 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004849
4850 if (!(gui.in_use && p_mousef))
4851 return;
4852
4853 gui_mch_getmouse(&x, &y);
4854 /* Don't move the mouse when it's left or right of the Vim window */
4855 if (x < 0 || x > Columns * gui.char_width)
4856 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004857 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004858# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004859 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004860# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004861 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004862 wp = xy2win(x, y);
4863 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004865 validate_cline_row();
4866 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4867 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870}
4871
4872/*
4873 * Find window where the mouse pointer "y" coordinate is in.
4874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 static win_T *
4876xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004877 int x UNUSED;
4878 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
4880#ifdef FEAT_WINDOWS
4881 int row;
4882 int col;
4883 win_T *wp;
4884
4885 row = Y_2_ROW(y);
4886 col = X_2_COL(x);
4887 if (row < 0 || col < 0) /* before first window */
4888 return NULL;
4889 wp = mouse_find_win(&row, &col);
4890# ifdef FEAT_MOUSESHAPE
4891 if (State == HITRETURN || State == ASKMORE)
4892 {
4893 if (Y_2_ROW(y) >= msg_row)
4894 update_mouseshape(SHAPE_IDX_MOREL);
4895 else
4896 update_mouseshape(SHAPE_IDX_MORE);
4897 }
4898 else if (row > wp->w_height) /* below status line */
4899 update_mouseshape(SHAPE_IDX_CLINE);
4900# ifdef FEAT_VERTSPLIT
4901 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004902 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 update_mouseshape(SHAPE_IDX_VSEP);
4904# endif
4905 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004906 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 update_mouseshape(SHAPE_IDX_STATUS);
4908 else
4909 update_mouseshape(-2);
4910# endif
4911 return wp;
4912#else
4913 return firstwin;
4914#endif
4915}
4916
4917/*
4918 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4919 * File names may be given to redefine the args list.
4920 */
4921 void
4922ex_gui(eap)
4923 exarg_T *eap;
4924{
4925 char_u *arg = eap->arg;
4926
4927 /*
4928 * Check for "-f" argument: foreground, don't fork.
4929 * Also don't fork when started with "gvim -f".
4930 * Do fork when using "gui -b".
4931 */
4932 if (arg[0] == '-'
4933 && (arg[1] == 'f' || arg[1] == 'b')
4934 && (arg[2] == NUL || vim_iswhite(arg[2])))
4935 {
4936 gui.dofork = (arg[1] == 'b');
4937 eap->arg = skipwhite(eap->arg + 2);
4938 }
4939 if (!gui.in_use)
4940 {
4941 /* Clear the command. Needed for when forking+exiting, to avoid part
4942 * of the argument ending up after the shell prompt. */
4943 msg_clr_eos_force();
4944 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004945#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004946 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 if (!ends_excmd(*eap->arg))
4950 ex_next(eap);
4951}
4952
4953#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004954 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955/*
4956 * This is shared between Athena, Motif and GTK.
4957 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004958static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960/*
4961 * Callback function for do_in_runtimepath().
4962 */
4963 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004964gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004966 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004968 char_u *gfp_buffer = cookie;
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (STRLEN(fname) >= MAXPATHL)
4971 *gfp_buffer = NUL;
4972 else
4973 STRCPY(gfp_buffer, fname);
4974}
4975
4976/*
4977 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4978 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4979 */
4980 int
4981gui_find_bitmap(name, buffer, ext)
4982 char_u *name;
4983 char_u *buffer;
4984 char *ext;
4985{
4986 if (STRLEN(name) > MAXPATHL - 14)
4987 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004988 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004989 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4990 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 return FAIL;
4992 return OK;
4993}
4994
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004995# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996/*
4997 * Given the name of the "icon=" argument, try finding the bitmap file for the
4998 * icon. If it is an absolute path name, use it as it is. Otherwise append
4999 * "ext" and search for it in 'runtimepath'.
5000 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5001 * contains "name".
5002 */
5003 void
5004gui_find_iconfile(name, buffer, ext)
5005 char_u *name;
5006 char_u *buffer;
5007 char *ext;
5008{
5009 char_u buf[MAXPATHL + 1];
5010
5011 expand_env(name, buffer, MAXPATHL);
5012 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5013 STRCPY(buffer, buf);
5014}
5015# endif
5016#endif
5017
Bram Moolenaar9372a112005-12-06 19:59:18 +00005018#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 void
5020display_errors()
5021{
5022 char_u *p;
5023
5024 if (isatty(2))
5025 fflush(stderr);
5026 else if (error_ga.ga_data != NULL)
5027 {
5028 /* avoid putting up a message box with blanks only */
5029 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5030 if (!isspace(*p))
5031 {
5032 /* Truncate a very long message, it will go off-screen. */
5033 if (STRLEN(p) > 2000)
5034 STRCPY(p + 2000 - 14, "...(truncated)");
5035 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005036 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
5039 ga_clear(&error_ga);
5040 }
5041}
5042#endif
5043
5044#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5045/*
5046 * Return TRUE if still starting up and there is no place to enter text.
5047 * For GTK and X11 we check if stderr is not a tty, which means we were
5048 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5049 * allow typing on stdin.
5050 */
5051 int
5052no_console_input()
5053{
5054 return ((!gui.in_use || gui.starting)
5055# ifndef NO_CONSOLE
5056 && !isatty(0) && !isatty(2)
5057# endif
5058 );
5059}
5060#endif
5061
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005062#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005063 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005064 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065/*
5066 * Update the current window and the screen.
5067 */
5068 void
5069gui_update_screen()
5070{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005071#ifdef FEAT_CONCEAL
5072 linenr_T conceal_old_cursor_line = 0;
5073 linenr_T conceal_new_cursor_line = 0;
5074 int conceal_update_lines = FALSE;
5075#endif
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 update_topline();
5078 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005079
5080#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005081 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005082 if (!finish_op && (
5083# ifdef FEAT_AUTOCMD
5084 has_cursormoved()
5085# endif
5086# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5087 ||
5088# endif
5089# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005090 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005091# endif
5092 )
5093 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005094 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005095# ifdef FEAT_AUTOCMD
5096 if (has_cursormoved())
5097 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5098# endif
5099# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005100 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005101 {
5102 conceal_old_cursor_line = last_cursormoved.lnum;
5103 conceal_new_cursor_line = curwin->w_cursor.lnum;
5104 conceal_update_lines = TRUE;
5105 }
5106# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005107 last_cursormoved = curwin->w_cursor;
5108 }
5109#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 update_screen(0); /* may need to update the screen */
5112 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005113# if defined(FEAT_CONCEAL)
5114 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005115 && (conceal_old_cursor_line != conceal_new_cursor_line
5116 || conceal_cursor_line(curwin)
5117 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005119 if (conceal_old_cursor_line != conceal_new_cursor_line)
5120 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005121 update_single_line(curwin, conceal_new_cursor_line);
5122 curwin->w_valid &= ~VALID_CROW;
5123 }
5124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 out_flush(); /* make sure output has been written */
5126 gui_update_cursor(TRUE, FALSE);
5127 gui_mch_flush();
5128}
5129#endif
5130
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005131#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5133
5134/*
5135 * Get the text to use in a find/replace dialog. Uses the last search pattern
5136 * if the argument is empty.
5137 * Returns an allocated string.
5138 */
5139 char_u *
5140get_find_dialog_text(arg, wwordp, mcasep)
5141 char_u *arg;
5142 int *wwordp; /* return: TRUE if \< \> found */
5143 int *mcasep; /* return: TRUE if \C found */
5144{
5145 char_u *text;
5146
5147 if (*arg == NUL)
5148 text = last_search_pat();
5149 else
5150 text = arg;
5151 if (text != NULL)
5152 {
5153 text = vim_strsave(text);
5154 if (text != NULL)
5155 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005156 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 int i;
5158
5159 /* Remove "\V" */
5160 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5161 {
5162 mch_memmove(text, text + 2, (size_t)(len - 1));
5163 len -= 2;
5164 }
5165
5166 /* Recognize "\c" and "\C" and remove. */
5167 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5168 {
5169 *mcasep = (text[1] == 'C');
5170 mch_memmove(text, text + 2, (size_t)(len - 1));
5171 len -= 2;
5172 }
5173
5174 /* Recognize "\<text\>" and remove. */
5175 if (len >= 4
5176 && STRNCMP(text, "\\<", 2) == 0
5177 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5178 {
5179 *wwordp = TRUE;
5180 mch_memmove(text, text + 2, (size_t)(len - 4));
5181 text[len - 4] = NUL;
5182 }
5183
5184 /* Recognize "\/" or "\?" and remove. */
5185 for (i = 0; i + 1 < len; ++i)
5186 if (text[i] == '\\' && (text[i + 1] == '/'
5187 || text[i + 1] == '?'))
5188 {
5189 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5190 --len;
5191 }
5192 }
5193 }
5194 return text;
5195}
5196
5197/*
5198 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5199 */
5200 static void
5201concat_esc(gap, text, what)
5202 garray_T *gap;
5203 char_u *text;
5204 int what;
5205{
5206 while (*text != NUL)
5207 {
5208#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005209 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (l > 1)
5212 {
5213 while (--l >= 0)
5214 ga_append(gap, *text++);
5215 continue;
5216 }
5217#endif
5218 if (*text == what)
5219 ga_append(gap, '\\');
5220 ga_append(gap, *text);
5221 ++text;
5222 }
5223}
5224
5225/*
5226 * Handle the press of a button in the find-replace dialog.
5227 * Return TRUE when something was added to the input buffer.
5228 */
5229 int
5230gui_do_findrepl(flags, find_text, repl_text, down)
5231 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5232 char_u *find_text;
5233 char_u *repl_text;
5234 int down; /* Search downwards. */
5235{
5236 garray_T ga;
5237 int i;
5238 int type = (flags & FRD_TYPE_MASK);
5239 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005240 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005241 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005242 static int busy = FALSE;
5243
5244 /* When the screen is being updated we should not change buffers and
5245 * windows structures, it may cause freed memory to be used. Also don't
5246 * do this recursively (pressing "Find" quickly several times. */
5247 if (updating_screen || busy)
5248 return FALSE;
5249
5250 /* refuse replace when text cannot be changed */
5251 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5252 return FALSE;
5253
5254 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005257 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 ga_concat(&ga, (char_u *)"%s/");
5259
5260 ga_concat(&ga, (char_u *)"\\V");
5261 if (flags & FRD_MATCH_CASE)
5262 ga_concat(&ga, (char_u *)"\\C");
5263 else
5264 ga_concat(&ga, (char_u *)"\\c");
5265 if (flags & FRD_WHOLE_WORD)
5266 ga_concat(&ga, (char_u *)"\\<");
5267 if (type == FRD_REPLACEALL || down)
5268 concat_esc(&ga, find_text, '/'); /* escape slashes */
5269 else
5270 concat_esc(&ga, find_text, '?'); /* escape '?' */
5271 if (flags & FRD_WHOLE_WORD)
5272 ga_concat(&ga, (char_u *)"\\>");
5273
5274 if (type == FRD_REPLACEALL)
5275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005277 /* escape / and \ */
5278 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5279 if (p != NULL)
5280 ga_concat(&ga, p);
5281 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005283 }
5284 ga_append(&ga, NUL);
5285
5286 if (type == FRD_REPLACE)
5287 {
5288 /* Do the replacement when the text at the cursor matches. Thus no
5289 * replacement is done if the cursor was moved! */
5290 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5291 regmatch.rm_ic = 0;
5292 if (regmatch.regprog != NULL)
5293 {
5294 p = ml_get_cursor();
5295 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5296 && regmatch.startp[0] == p)
5297 {
5298 /* Clear the command line to remove any old "No match"
5299 * error. */
5300 msg_end_prompt();
5301
5302 if (u_save_cursor() == OK)
5303 {
5304 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005305 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005306
5307 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005308 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005309 ins_str(repl_text);
5310 }
5311 }
5312 else
5313 MSG(_("No match at cursor, finding next"));
5314 vim_free(regmatch.regprog);
5315 }
5316 }
5317
5318 if (type == FRD_REPLACEALL)
5319 {
5320 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005321 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 do_cmdline_cmd(ga.ga_data);
5323 }
5324 else
5325 {
5326 /* Search for the next match. */
5327 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005329 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 msg_scroll = i; /* don't let an error message set msg_scroll */
5331 }
5332
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005333 /* Don't want to pass did_emsg to other code, it may cause disabling
5334 * syntax HL if we were busy redrawing. */
5335 did_emsg = save_did_emsg;
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (State & (NORMAL | INSERT))
5338 {
5339 gui_update_screen(); /* update the screen */
5340 msg_didout = 0; /* overwrite any message */
5341 need_wait_return = FALSE; /* don't wait for return */
5342 }
5343
5344 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005345 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return (ga.ga_len > 0);
5347}
5348
5349#endif
5350
5351#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5352 || defined(FEAT_GUI_MSWIN) \
5353 || defined(FEAT_GUI_MAC) \
5354 || defined(PROTO)
5355
5356#ifdef FEAT_WINDOWS
5357static void gui_wingoto_xy __ARGS((int x, int y));
5358
5359/*
5360 * Jump to the window at specified point (x, y).
5361 */
5362 static void
5363gui_wingoto_xy(x, y)
5364 int x;
5365 int y;
5366{
5367 int row = Y_2_ROW(y);
5368 int col = X_2_COL(x);
5369 win_T *wp;
5370
5371 if (row >= 0 && col >= 0)
5372 {
5373 wp = mouse_find_win(&row, &col);
5374 if (wp != NULL && wp != curwin)
5375 win_goto(wp);
5376 }
5377}
5378#endif
5379
5380/*
5381 * Process file drop. Mouse cursor position, key modifiers, name of files
5382 * and count of files are given. Argument "fnames[count]" has full pathnames
5383 * of dropped files, they will be freed in this function, and caller can't use
5384 * fnames after call this function.
5385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 void
5387gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005388 int x UNUSED;
5389 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 int_u modifiers;
5391 char_u **fnames;
5392 int count;
5393{
5394 int i;
5395 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005396 static int entered = FALSE;
5397
5398 /*
5399 * This function is called by event handlers. Just in case we get a
5400 * second event before the first one is handled, ignore the second one.
5401 * Not sure if this can ever happen, just in case.
5402 */
5403 if (entered)
5404 return;
5405 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
5407 /*
5408 * When the cursor is at the command line, add the file names to the
5409 * command line, don't edit the files.
5410 */
5411 if (State & CMDLINE)
5412 {
5413 shorten_filenames(fnames, count);
5414 for (i = 0; i < count; ++i)
5415 {
5416 if (fnames[i] != NULL)
5417 {
5418 if (i > 0)
5419 add_to_input_buf((char_u*)" ", 1);
5420
5421 /* We don't know what command is used thus we can't be sure
5422 * about which characters need to be escaped. Only escape the
5423 * most common ones. */
5424# ifdef BACKSLASH_IN_FILENAME
5425 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5426# else
5427 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5428# endif
5429 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005430 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 vim_free(p);
5432 vim_free(fnames[i]);
5433 }
5434 }
5435 vim_free(fnames);
5436 }
5437 else
5438 {
5439 /* Go to the window under mouse cursor, then shorten given "fnames" by
5440 * current window, because a window can have local current dir. */
5441# ifdef FEAT_WINDOWS
5442 gui_wingoto_xy(x, y);
5443# endif
5444 shorten_filenames(fnames, count);
5445
5446 /* If Shift held down, remember the first item. */
5447 if ((modifiers & MOUSE_SHIFT) != 0)
5448 p = vim_strsave(fnames[0]);
5449 else
5450 p = NULL;
5451
5452 /* Handle the drop, :edit or :split to get to the file. This also
5453 * frees fnames[]. Skip this if there is only one item it's a
5454 * directory and Shift is held down. */
5455 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5456 && mch_isdir(fnames[0]))
5457 {
5458 vim_free(fnames[0]);
5459 vim_free(fnames);
5460 }
5461 else
5462 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5463
5464 /* If Shift held down, change to first file's directory. If the first
5465 * item is a directory, change to that directory (and let the explorer
5466 * plugin show the contents). */
5467 if (p != NULL)
5468 {
5469 if (mch_isdir(p))
5470 {
5471 if (mch_chdir((char *)p) == 0)
5472 shorten_fnames(TRUE);
5473 }
5474 else if (vim_chdirfile(p) == OK)
5475 shorten_fnames(TRUE);
5476 vim_free(p);
5477 }
5478
5479 /* Update the screen display */
5480 update_screen(NOT_VALID);
5481# ifdef FEAT_MENU
5482 gui_update_menus(0);
5483# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005484#ifdef FEAT_TITLE
5485 maketitle();
5486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 setcursor();
5488 out_flush();
5489 gui_update_cursor(FALSE, FALSE);
5490 gui_mch_flush();
5491 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005492
5493 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494}
5495#endif