blob: 14706553a05da00b7404ede2fade8bd9cc15b221 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static void set_guifontwide __ARGS((char_u *font_name));
18#endif
19static void gui_check_pos __ARGS((void));
20static void gui_position_components __ARGS((int));
21static void gui_outstr __ARGS((char_u *, int));
22static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020023#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +000024static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25#endif
26static void gui_delete_lines __ARGS((int row, int count));
27static void gui_insert_lines __ARGS((int row, int count));
28static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30static int gui_has_tabline __ARGS((void));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33static colnr_T scroll_line_len __ARGS((linenr_T lnum));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020034static linenr_T gui_find_longest_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static void gui_update_horiz_scrollbar __ARGS((int));
Bram Moolenaar3918c952005-03-15 22:34:55 +000036static void gui_set_fg_color __ARGS((char_u *name));
37static void gui_set_bg_color __ARGS((char_u *name));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static win_T *xy2win __ARGS((int x, int y));
39
Bram Moolenaar05514102012-08-29 16:34:27 +020040#if defined(UNIX) && !defined(MACOS_X) && !defined(__APPLE__)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020041# define MAY_FORK
42static void gui_do_fork __ARGS((void));
43
44static int gui_read_child_pipe __ARGS((int fd));
45
46/* Return values for gui_read_child_pipe */
47enum {
48 GUI_CHILD_IO_ERROR,
49 GUI_CHILD_OK,
50 GUI_CHILD_FAILED
51};
52
53#endif /* MAY_FORK */
54
55static void gui_attempt_start __ARGS((void));
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static int can_update_cursor = TRUE; /* can display the cursor */
58
59/*
60 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
61 * this makes the thumb indicate the part of the text that is shown. Motif
62 * can't do this.
63 */
64#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
65# define SCROLL_PAST_END
66#endif
67
68/*
69 * gui_start -- Called when user wants to start the GUI.
70 *
71 * Careful: This function can be called recursively when there is a ":gui"
72 * command in the .gvimrc file. Only the first call should fork, not the
73 * recursive call.
74 */
75 void
76gui_start()
77{
78 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 static int recursive = 0;
80
81 old_term = vim_strsave(T_NAME);
82
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 settmode(TMODE_COOK); /* stop RAW mode */
84 if (full_screen)
85 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 full_screen = FALSE;
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 ++recursive;
89
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090#ifdef MAY_FORK
91 /*
92 * Quit the current process and continue in the child.
93 * Makes "gvim file" disconnect from the shell it was started in.
94 * Don't do this when Vim was started with "-f" or the 'f' flag is present
95 * in 'guioptions'.
96 */
97 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1)
98 {
99 gui_do_fork();
100 }
101 else
102#endif
103 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200104#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200105 /* If there is 'f' in 'guioptions' and specify -g argument,
106 * gui_mch_init_check() was not called yet. */
107 if (gui_mch_init_check() != OK)
108 exit(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200109#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200110 gui_attempt_start();
111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
113 if (!gui.in_use) /* failed to start GUI */
114 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200115 /* Back to old term settings
116 *
117 * FIXME: If we got here because a child process failed and flagged to
118 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
119 * hit an X11 I/O error and do a longjmp(), leaving recursive
120 * permanently set to 1. This is probably not as big a problem as it
121 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
122 * return "OK" unconditionally, so it would be very difficult to
123 * actually hit this case.
124 */
125 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 settmode(TMODE_RAW); /* restart RAW mode */
127#ifdef FEAT_TITLE
128 set_title_defaults(); /* set 'title' and 'icon' again */
129#endif
130 }
131
132 vim_free(old_term);
133
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200134#ifdef FEAT_AUTOCMD
135 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
136 * the GUIFailed event. */
137 gui_mch_update();
138 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
139 NULL, NULL, FALSE, curbuf);
140#endif
141 --recursive;
142}
143
144/*
145 * Set_termname() will call gui_init() to start the GUI.
146 * Set the "starting" flag, to indicate that the GUI will start.
147 *
148 * We don't want to open the GUI shell until after we've read .gvimrc,
149 * otherwise we don't know what font we will use, and hence we don't know
150 * what size the shell should be. So if there are errors in the .gvimrc
151 * file, they will have to go to the terminal: Set full_screen to FALSE.
152 * full_screen will be set to TRUE again by a successful termcapinit().
153 */
154 static void
155gui_attempt_start()
156{
157 static int recursive = 0;
158
159 ++recursive;
160 gui.starting = TRUE;
161
162#ifdef FEAT_GUI_GTK
163 gui.event_time = GDK_CURRENT_TIME;
164#endif
165
166 termcapinit((char_u *)"builtin_gui");
167 gui.starting = recursive - 1;
168
Bram Moolenaar9372a112005-12-06 19:59:18 +0000169#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200171 {
172# ifdef FEAT_EVAL
173 Window x11_window;
174 Display *x11_display;
175
176 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
177 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
178# endif
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 /* Display error messages in a dialog now. */
181 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 --recursive;
185}
186
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200187#ifdef MAY_FORK
188
189/* for waitpid() */
190# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
191# include <sys/wait.h>
192# endif
193
194/*
195 * Create a new process, by forking. In the child, start the GUI, and in
196 * the parent, exit.
197 *
198 * If something goes wrong, this will return with gui.in_use still set
199 * to FALSE, in which case the caller should continue execution without
200 * the GUI.
201 *
202 * If the child fails to start the GUI, then the child will exit and the
203 * parent will return. If the child succeeds, then the parent will exit
204 * and the child will return.
205 */
206 static void
207gui_do_fork()
208{
209#ifdef __QNXNTO__
210 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
211 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
212 gui_attempt_start();
213 return;
214#else
215 int pipefd[2]; /* pipe between parent and child */
216 int pipe_error;
217 int status;
218 int exit_status;
219 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200220
221 /* Setup a pipe between the child and the parent, so that the parent
222 * knows when the child has done the setsid() call and is allowed to
223 * exit. */
224 pipe_error = (pipe(pipefd) < 0);
225 pid = fork();
226 if (pid < 0) /* Fork error */
227 {
228 EMSG(_("E851: Failed to create a new process for the GUI"));
229 return;
230 }
231 else if (pid > 0) /* Parent */
232 {
233 /* Give the child some time to do the setsid(), otherwise the
234 * exit() may kill the child too (when starting gvim from inside a
235 * gvim). */
236 if (!pipe_error)
237 {
238 /* The read returns when the child closes the pipe (or when
239 * the child dies for some reason). */
240 close(pipefd[1]);
241 status = gui_read_child_pipe(pipefd[0]);
242 if (status == GUI_CHILD_FAILED)
243 {
244 /* The child failed to start the GUI, so the caller must
245 * continue. There may be more error information written
246 * to stderr by the child. */
247# ifdef __NeXT__
248 wait4(pid, &exit_status, 0, (struct rusage *)0);
249# else
250 waitpid(pid, &exit_status, 0);
251# endif
252 EMSG(_("E852: The child process failed to start the GUI"));
253 return;
254 }
255 else if (status == GUI_CHILD_IO_ERROR)
256 {
257 pipe_error = TRUE;
258 }
259 /* else GUI_CHILD_OK: parent exit */
260 }
261
262 if (pipe_error)
263 ui_delay(300L, TRUE);
264
265 /* When swapping screens we may need to go to the next line, e.g.,
266 * after a hit-enter prompt and using ":gui". */
267 if (newline_on_exit)
268 mch_errmsg("\r\n");
269
270 /*
271 * The parent must skip the normal exit() processing, the child
272 * will do it. For example, GTK messes up signals when exiting.
273 */
274 _exit(0);
275 }
276 /* Child */
277
Bram Moolenaar29695702012-05-18 17:03:18 +0200278#ifdef FEAT_GUI_GTK
279 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
280 if (gui_mch_init_check() != OK)
281 exit(1);
282#endif
283
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200284# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
285 /*
286 * Change our process group. On some systems/shells a CTRL-C in the
287 * shell where Vim was started would otherwise kill gvim!
288 */
289# if defined(HAVE_SETSID)
290 (void)setsid();
291# else
292 (void)setpgid(0, 0);
293# endif
294# endif
295 if (!pipe_error)
296 close(pipefd[0]);
297
298# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
299 /* Tell the session manager our new PID */
300 gui_mch_forked();
301# endif
302
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200303 /* Try to start the GUI */
304 gui_attempt_start();
305
306 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200307 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200308 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200309 if (gui.in_use)
310 write_eintr(pipefd[1], "ok", 3);
311 else
312 write_eintr(pipefd[1], "fail", 5);
313 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200314 }
315
316 /* If we failed to start the GUI, exit now. */
317 if (!gui.in_use)
318 exit(1);
319#endif
320}
321
322/*
323 * Read from a pipe assumed to be connected to the child process (this
324 * function is called from the parent).
325 * Return GUI_CHILD_OK if the child successfully started the GUI,
326 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
327 * some other error.
328 *
329 * The file descriptor will be closed before the function returns.
330 */
331 static int
332gui_read_child_pipe(int fd)
333{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200334 long bytes_read;
335#define READ_BUFFER_SIZE 10
336 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200338 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
339#undef READ_BUFFER_SIZE
340 close(fd);
341 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200342 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200343 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200344 if (strcmp(buffer, "ok") == 0)
345 return GUI_CHILD_OK;
346 return GUI_CHILD_FAILED;
347}
348
349#endif /* MAY_FORK */
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351/*
352 * Call this when vim starts up, whether or not the GUI is started
353 */
354 void
355gui_prepare(argc, argv)
356 int *argc;
357 char **argv;
358{
359 gui.in_use = FALSE; /* No GUI yet (maybe later) */
360 gui.starting = FALSE; /* No GUI yet (maybe later) */
361 gui_mch_prepare(argc, argv);
362}
363
364/*
365 * Try initializing the GUI and check if it can be started.
366 * Used from main() to check early if "vim -g" can start the GUI.
367 * Used from gui_init() to prepare for starting the GUI.
368 * Returns FAIL or OK.
369 */
370 int
371gui_init_check()
372{
373 static int result = MAYBE;
374
375 if (result != MAYBE)
376 {
377 if (result == FAIL)
378 EMSG(_("E229: Cannot start the GUI"));
379 return result;
380 }
381
382 gui.shell_created = FALSE;
383 gui.dying = FALSE;
384 gui.in_focus = TRUE; /* so the guicursor setting works */
385 gui.dragged_sb = SBAR_NONE;
386 gui.dragged_wp = NULL;
387 gui.pointer_hidden = FALSE;
388 gui.col = 0;
389 gui.row = 0;
390 gui.num_cols = Columns;
391 gui.num_rows = Rows;
392
393 gui.cursor_is_valid = FALSE;
394 gui.scroll_region_top = 0;
395 gui.scroll_region_bot = Rows - 1;
396 gui.scroll_region_left = 0;
397 gui.scroll_region_right = Columns - 1;
398 gui.highlight_mask = HL_NORMAL;
399 gui.char_width = 1;
400 gui.char_height = 1;
401 gui.char_ascent = 0;
402 gui.border_width = 0;
403
404 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200405#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 gui.bold_font = NOFONT;
407 gui.ital_font = NOFONT;
408 gui.boldital_font = NOFONT;
409# ifdef FEAT_XFONTSET
410 gui.fontset = NOFONTSET;
411# endif
412#endif
413
414#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200415# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416# ifdef FONTSET_ALWAYS
417 gui.menu_fontset = NOFONTSET;
418# else
419 gui.menu_font = NOFONT;
420# endif
421# endif
422 gui.menu_is_active = TRUE; /* default: include menu */
423# ifndef FEAT_GUI_GTK
424 gui.menu_height = MENU_DEFAULT_HEIGHT;
425 gui.menu_width = 0;
426# endif
427#endif
428#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
429 gui.toolbar_height = 0;
430#endif
431#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
432 gui.footer_height = 0;
433#endif
434#ifdef FEAT_BEVAL_TIP
435 gui.tooltip_fontset = NOFONTSET;
436#endif
437
438 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
439 gui.prev_wrap = -1;
440
441#ifdef ALWAYS_USE_GUI
442 result = OK;
443#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200444# ifdef FEAT_GUI_GTK
445 /*
446 * Note: Don't call gtk_init_check() before fork, it will be called after
447 * the fork. When calling it before fork, it make vim hang for a while.
448 * See gui_do_fork().
449 * Use a simpler check if the GUI window can probably be opened.
450 */
451 result = gui.dofork ? gui_mch_early_init_check() : gui_mch_init_check();
452# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455#endif
456 return result;
457}
458
459/*
460 * This is the call which starts the GUI.
461 */
462 void
463gui_init()
464{
465 win_T *wp;
466 static int recursive = 0;
467
468 /*
469 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
470 * function will then be executed at the first call, the rest by the
471 * recursive call. This allow the shell to be opened halfway reading a
472 * gvimrc file.
473 */
474 if (!recursive)
475 {
476 ++recursive;
477
478 clip_init(TRUE);
479
480 /* If can't initialize, don't try doing the rest */
481 if (gui_init_check() == FAIL)
482 {
483 --recursive;
484 clip_init(FALSE);
485 return;
486 }
487
488 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000489 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
490 * breaks the Paste toolbar button.
491 */
492 set_option_value((char_u *)"paste", 0L, NULL, 0);
493
494 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * Set up system-wide default menus.
496 */
497#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
498 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
499 {
500 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000501 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 sys_menu = FALSE;
503 }
504#endif
505
506 /*
507 * Switch on the mouse by default, unless the user changed it already.
508 * This can then be changed in the .gvimrc.
509 */
510 if (!option_was_set((char_u *)"mouse"))
511 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000512 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 /*
515 * If -U option given, use only the initializations from that file and
516 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
517 */
518 if (use_gvimrc != NULL)
519 {
520 if (STRCMP(use_gvimrc, "NONE") != 0
521 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000522 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
524 }
525 else
526 {
527 /*
528 * Get system wide defaults for gvim, only when file name defined.
529 */
530#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000531 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#endif
533
534 /*
535 * Try to read GUI initialization commands from the following
536 * places:
537 * - environment variable GVIMINIT
538 * - the user gvimrc file (~/.gvimrc)
539 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
540 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
541 * The first that exists is used, the rest is ignored.
542 */
543 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000544 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
545 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000547 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
548 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549#endif
550 )
551 {
552#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000553 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#endif
555 }
556
557 /*
558 * Read initialization commands from ".gvimrc" in current
559 * directory. This is only done if the 'exrc' option is set.
560 * Because of security reasons we disallow shell and write
561 * commands now, except for unix if the file is owned by the user
562 * or 'secure' option has been reset in environment of global
563 * ".gvimrc".
564 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
565 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
566 */
567 if (p_exrc)
568 {
569#ifdef UNIX
570 {
571 struct stat s;
572
573 /* if ".gvimrc" file is not owned by user, set 'secure'
574 * mode */
575 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
576 secure = p_secure;
577 }
578#else
579 secure = p_secure;
580#endif
581
582 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
583 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
584#ifdef SYS_GVIMRC_FILE
585 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
586 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
587#endif
588#ifdef USR_GVIMRC_FILE2
589 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
590 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
591#endif
592#ifdef USR_GVIMRC_FILE3
593 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
594 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
595#endif
596 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000597 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 if (secure == 2)
600 need_wait_return = TRUE;
601 secure = 0;
602 }
603 }
604
605 if (need_wait_return || msg_didany)
606 wait_return(TRUE);
607
608 --recursive;
609 }
610
611 /* If recursive call opened the shell, return here from the first call */
612 if (gui.in_use)
613 return;
614
615 /*
616 * Create the GUI shell.
617 */
618 gui.in_use = TRUE; /* Must be set after menus have been set up */
619 if (gui_mch_init() == FAIL)
620 goto error;
621
622 /* Avoid a delay for an error message that was printed in the terminal
623 * where Vim was started. */
624 emsg_on_display = FALSE;
625 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000626 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 need_wait_return = FALSE;
628 msg_didany = FALSE;
629
630 /*
631 * Check validity of any generic resources that may have been loaded.
632 */
633 if (gui.border_width < 0)
634 gui.border_width = 0;
635
636 /*
637 * Set up the fonts. First use a font specified with "-fn" or "-font".
638 */
639 if (font_argument != NULL)
640 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
641 if (
642#ifdef FEAT_XFONTSET
643 (*p_guifontset == NUL
644 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
645#endif
646 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
647 : p_guifont, FALSE) == FAIL)
648 {
649 EMSG(_("E665: Cannot start GUI, no valid font found"));
650 goto error2;
651 }
652#ifdef FEAT_MBYTE
653 if (gui_get_wide_font() == FAIL)
654 EMSG(_("E231: 'guifontwide' invalid"));
655#endif
656
657 gui.num_cols = Columns;
658 gui.num_rows = Rows;
659 gui_reset_scroll_region();
660
661 /* Create initial scrollbars */
662 FOR_ALL_WINDOWS(wp)
663 {
664 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
665 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
666 }
667 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
668
669#ifdef FEAT_MENU
670 gui_create_initial_menus(root_menu);
671#endif
672#ifdef FEAT_SUN_WORKSHOP
673 if (usingSunWorkShop)
674 workshop_init();
675#endif
676#ifdef FEAT_SIGN_ICONS
677 sign_gui_started();
678#endif
679
680 /* Configure the desired menu and scrollbars */
681 gui_init_which_components(NULL);
682
683 /* All components of the GUI have been created now */
684 gui.shell_created = TRUE;
685
686#ifndef FEAT_GUI_GTK
687 /* Set the shell size, adjusted for the screen size. For GTK this only
688 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000689 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#endif
691#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
692 /* Need to set the size of the menubar after all the menus have been
693 * created. */
694 gui_mch_compute_menu_height((Widget)0);
695#endif
696
697 /*
698 * Actually open the GUI shell.
699 */
700 if (gui_mch_open() != FAIL)
701 {
702#ifdef FEAT_TITLE
703 maketitle();
704 resettitle();
705#endif
706 init_gui_options();
707#ifdef FEAT_ARABIC
708 /* Our GUI can't do bidi. */
709 p_tbidi = FALSE;
710#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000711#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 /* Give GTK+ a chance to put all widget's into place. */
713 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000714
715# ifdef FEAT_MENU
716 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
717 * It was still there to make F10 work. */
718 if (vim_strchr(p_go, GO_MENUS) == NULL)
719 {
720 --gui.starting;
721 gui_mch_enable_menu(FALSE);
722 ++gui.starting;
723 gui_mch_update();
724 }
725# endif
726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000728 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000730 /* When 'lines' was set while starting up the topframe may have to be
731 * resized. */
732 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000733
734#ifdef FEAT_BEVAL
735 /* Always create the Balloon Evaluation area, but disable it when
736 * 'ballooneval' is off */
737# ifdef FEAT_GUI_GTK
738 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
739 &general_beval_cb, NULL);
740# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000741# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000742 {
743 extern Widget textArea;
744 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000745 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000746 }
747# else
748# ifdef FEAT_GUI_W32
749 balloonEval = gui_mch_create_beval_area(NULL, NULL,
750 &general_beval_cb, NULL);
751# endif
752# endif
753# endif
754 if (!p_beval)
755 gui_mch_disable_beval_area(balloonEval);
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
759 if (!im_xim_isvalid_imactivate())
760 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
761#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000762 /* When 'cmdheight' was set during startup it may not have taken
763 * effect yet. */
764 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000765 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
767 return;
768 }
769
770error2:
771#ifdef FEAT_GUI_X11
772 /* undo gui_mch_init() */
773 gui_mch_uninit();
774#endif
775
776error:
777 gui.in_use = FALSE;
778 clip_init(FALSE);
779}
780
781
782 void
783gui_exit(rc)
784 int rc;
785{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* don't free the fonts, it leads to a BUS error
787 * richard@whitequeen.com Jul 99 */
788 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 gui.in_use = FALSE;
790 gui_mch_exit(rc);
791}
792
Bram Moolenaar9372a112005-12-06 19:59:18 +0000793#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000795# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796/*
797 * Called when the GUI shell is closed by the user. If there are no changed
798 * files Vim exits, otherwise there will be a dialog to ask the user what to
799 * do.
800 * When this function returns, Vim should NOT exit!
801 */
802 void
803gui_shell_closed()
804{
805 cmdmod_T save_cmdmod;
806
807 save_cmdmod = cmdmod;
808
809 /* Only exit when there are no changed files */
810 exiting = TRUE;
811# ifdef FEAT_BROWSE
812 cmdmod.browse = TRUE;
813# endif
814# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
815 cmdmod.confirm = TRUE;
816# endif
817 /* If there are changed buffers, present the user with a dialog if
818 * possible, otherwise give an error message. */
819 if (!check_changed_any(FALSE))
820 getout(0);
821
822 exiting = FALSE;
823 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000824 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826#endif
827
828/*
829 * Set the font. "font_list" is a a comma separated list of font names. The
830 * first font name that works is used. If none is found, use the default
831 * font.
832 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
833 * Return OK when able to set the font. When it failed FAIL is returned and
834 * the fonts are unchanged.
835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 int
837gui_init_font(font_list, fontset)
838 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000839 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841#define FONTLEN 320
842 char_u font_name[FONTLEN];
843 int font_list_empty = FALSE;
844 int ret = FAIL;
845
846 if (!gui.in_use)
847 return FAIL;
848
849 font_name[0] = NUL;
850 if (*font_list == NUL)
851 font_list_empty = TRUE;
852 else
853 {
854#ifdef FEAT_XFONTSET
855 /* When using a fontset, the whole list of fonts is one name. */
856 if (fontset)
857 ret = gui_mch_init_font(font_list, TRUE);
858 else
859#endif
860 while (*font_list != NUL)
861 {
862 /* Isolate one comma separated font name. */
863 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
864
865 /* Careful!!! The Win32 version of gui_mch_init_font(), when
866 * called with "*" will change p_guifont to the selected font
867 * name, which frees the old value. This makes font_list
868 * invalid. Thus when OK is returned here, font_list must no
869 * longer be used! */
870 if (gui_mch_init_font(font_name, FALSE) == OK)
871 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200872#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 /* If it's a Unicode font, try setting 'guifontwide' to a
874 * similar double-width font. */
875 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
876 && strstr((char *)font_name, "10646") != NULL)
877 set_guifontwide(font_name);
878#endif
879 ret = OK;
880 break;
881 }
882 }
883 }
884
885 if (ret != OK
886 && STRCMP(font_list, "*") != 0
887 && (font_list_empty || gui.norm_font == NOFONT))
888 {
889 /*
890 * Couldn't load any font in 'font_list', keep the current font if
891 * there is one. If 'font_list' is empty, or if there is no current
892 * font, tell gui_mch_init_font() to try to find a font we can load.
893 */
894 ret = gui_mch_init_font(NULL, FALSE);
895 }
896
897 if (ret == OK)
898 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200899#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 /* Set normal font as current font */
901# ifdef FEAT_XFONTSET
902 if (gui.fontset != NOFONTSET)
903 gui_mch_set_fontset(gui.fontset);
904 else
905# endif
906 gui_mch_set_font(gui.norm_font);
907#endif
Bram Moolenaarb0316262012-11-20 12:03:06 +0100908 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910
911 return ret;
912}
913
914#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200915# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916/*
917 * Try setting 'guifontwide' to a font twice as wide as "name".
918 */
919 static void
920set_guifontwide(name)
921 char_u *name;
922{
923 int i = 0;
924 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
925 char_u *wp = NULL;
926 char_u *p;
927 GuiFont font;
928
929 wp = wide_name;
930 for (p = name; *p != NUL; ++p)
931 {
932 *wp++ = *p;
933 if (*p == '-')
934 {
935 ++i;
936 if (i == 6) /* font type: change "--" to "-*-" */
937 {
938 if (p[1] == '-')
939 *wp++ = '*';
940 }
941 else if (i == 12) /* found the width */
942 {
943 ++p;
944 i = getdigits(&p);
945 if (i != 0)
946 {
947 /* Double the width specification. */
948 sprintf((char *)wp, "%d%s", i * 2, p);
949 font = gui_mch_get_font(wide_name, FALSE);
950 if (font != NOFONT)
951 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000952 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 gui.wide_font = font;
954 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000955 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957 }
958 break;
959 }
960 }
961 }
962}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200963# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965/*
966 * Get the font for 'guifontwide'.
967 * Return FAIL for an invalid font name.
968 */
969 int
970gui_get_wide_font()
971{
972 GuiFont font = NOFONT;
973 char_u font_name[FONTLEN];
974 char_u *p;
975
976 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
977 return OK; /* Will give an error message later. */
978
979 if (p_guifontwide != NULL && *p_guifontwide != NUL)
980 {
981 for (p = p_guifontwide; *p != NUL; )
982 {
983 /* Isolate one comma separated font name. */
984 (void)copy_option_part(&p, font_name, FONTLEN, ",");
985 font = gui_mch_get_font(font_name, FALSE);
986 if (font != NOFONT)
987 break;
988 }
989 if (font == NOFONT)
990 return FAIL;
991 }
992
993 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200994#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
996 if (font != NOFONT && gui.norm_font != NOFONT
997 && pango_font_description_equal(font, gui.norm_font))
998 {
999 gui.wide_font = NOFONT;
1000 gui_mch_free_font(font);
1001 }
1002 else
1003#endif
1004 gui.wide_font = font;
1005 return OK;
1006}
1007#endif
1008
1009 void
1010gui_set_cursor(row, col)
1011 int row;
1012 int col;
1013{
1014 gui.row = row;
1015 gui.col = col;
1016}
1017
1018/*
1019 * gui_check_pos - check if the cursor is on the screen.
1020 */
1021 static void
1022gui_check_pos()
1023{
1024 if (gui.row >= screen_Rows)
1025 gui.row = screen_Rows - 1;
1026 if (gui.col >= screen_Columns)
1027 gui.col = screen_Columns - 1;
1028 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1029 gui.cursor_is_valid = FALSE;
1030}
1031
1032/*
1033 * Redraw the cursor if necessary or when forced.
1034 * Careful: The contents of ScreenLines[] must match what is on the screen,
1035 * otherwise this goes wrong. May need to call out_flush() first.
1036 */
1037 void
1038gui_update_cursor(force, clear_selection)
1039 int force; /* when TRUE, update even when not moved */
1040 int clear_selection;/* clear selection under cursor */
1041{
1042 int cur_width = 0;
1043 int cur_height = 0;
1044 int old_hl_mask;
1045 int idx;
1046 int id;
1047 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1048 int cattr; /* cursor attributes */
1049 int attr;
1050 attrentry_T *aep = NULL;
1051
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001052 /* Don't update the cursor when halfway busy scrolling or the screen size
1053 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1054 if (!can_update_cursor || screen_Columns != gui.num_cols
1055 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 return;
1057
1058 gui_check_pos();
1059 if (!gui.cursor_is_valid || force
1060 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1061 {
1062 gui_undraw_cursor();
1063 if (gui.row < 0)
1064 return;
1065#ifdef USE_IM_CONTROL
1066 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1067 im_set_position(gui.row, gui.col);
1068#endif
1069 gui.cursor_row = gui.row;
1070 gui.cursor_col = gui.col;
1071
1072 /* Only write to the screen after ScreenLines[] has been initialized */
1073 if (!screen_cleared || ScreenLines == NULL)
1074 return;
1075
1076 /* Clear the selection if we are about to write over it */
1077 if (clear_selection)
1078 clip_may_clear_selection(gui.row, gui.row);
1079 /* Check that the cursor is inside the shell (resizing may have made
1080 * it invalid) */
1081 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1082 return;
1083
1084 gui.cursor_is_valid = TRUE;
1085
1086 /*
1087 * How the cursor is drawn depends on the current mode.
1088 */
1089 idx = get_shape_idx(FALSE);
1090 if (State & LANGMAP)
1091 id = shape_table[idx].id_lm;
1092 else
1093 id = shape_table[idx].id;
1094
1095 /* get the colors and attributes for the cursor. Default is inverted */
1096 cfg = INVALCOLOR;
1097 cbg = INVALCOLOR;
1098 cattr = HL_INVERSE;
1099 gui_mch_set_blinking(shape_table[idx].blinkwait,
1100 shape_table[idx].blinkon,
1101 shape_table[idx].blinkoff);
1102 if (id > 0)
1103 {
1104 cattr = syn_id2colors(id, &cfg, &cbg);
1105#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1106 {
1107 static int iid;
1108 guicolor_T fg, bg;
1109
Bram Moolenaarc236c162008-07-13 17:41:49 +00001110 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001111# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001112 preedit_get_status()
1113# else
1114 im_get_status()
1115# endif
1116 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
1118 iid = syn_name2id((char_u *)"CursorIM");
1119 if (iid > 0)
1120 {
1121 syn_id2colors(iid, &fg, &bg);
1122 if (bg != INVALCOLOR)
1123 cbg = bg;
1124 if (fg != INVALCOLOR)
1125 cfg = fg;
1126 }
1127 }
1128 }
1129#endif
1130 }
1131
1132 /*
1133 * Get the attributes for the character under the cursor.
1134 * When no cursor color was given, use the character color.
1135 */
1136 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1137 if (attr > HL_ALL)
1138 aep = syn_gui_attr2entry(attr);
1139 if (aep != NULL)
1140 {
1141 attr = aep->ae_attr;
1142 if (cfg == INVALCOLOR)
1143 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1144 : aep->ae_u.gui.fg_color);
1145 if (cbg == INVALCOLOR)
1146 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1147 : aep->ae_u.gui.bg_color);
1148 }
1149 if (cfg == INVALCOLOR)
1150 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1151 if (cbg == INVALCOLOR)
1152 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1153
1154#ifdef FEAT_XIM
1155 if (aep != NULL)
1156 {
1157 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1158 : aep->ae_u.gui.bg_color);
1159 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1160 : aep->ae_u.gui.fg_color);
1161 if (xim_bg_color == INVALCOLOR)
1162 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1163 : gui.back_pixel;
1164 if (xim_fg_color == INVALCOLOR)
1165 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1166 : gui.norm_pixel;
1167 }
1168 else
1169 {
1170 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1171 : gui.back_pixel;
1172 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1173 : gui.norm_pixel;
1174 }
1175#endif
1176
1177 attr &= ~HL_INVERSE;
1178 if (cattr & HL_INVERSE)
1179 {
1180 cc = cbg;
1181 cbg = cfg;
1182 cfg = cc;
1183 }
1184 cattr &= ~HL_INVERSE;
1185
1186 /*
1187 * When we don't have window focus, draw a hollow cursor.
1188 */
1189 if (!gui.in_focus)
1190 {
1191 gui_mch_draw_hollow_cursor(cbg);
1192 return;
1193 }
1194
1195 old_hl_mask = gui.highlight_mask;
1196 if (shape_table[idx].shape == SHAPE_BLOCK
1197#ifdef FEAT_HANGULIN
1198 || composing_hangul
1199#endif
1200 )
1201 {
1202 /*
1203 * Draw the text character with the cursor colors. Use the
1204 * character attributes plus the cursor attributes.
1205 */
1206 gui.highlight_mask = (cattr | attr);
1207#ifdef FEAT_HANGULIN
1208 if (composing_hangul)
1209 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1210 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1211 else
1212#endif
1213 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1214 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1215 }
1216 else
1217 {
1218#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1219 int col_off = FALSE;
1220#endif
1221 /*
1222 * First draw the partial cursor, then overwrite with the text
1223 * character, using a transparent background.
1224 */
1225 if (shape_table[idx].shape == SHAPE_VER)
1226 {
1227 cur_height = gui.char_height;
1228 cur_width = (gui.char_width * shape_table[idx].percentage
1229 + 99) / 100;
1230 }
1231 else
1232 {
1233 cur_height = (gui.char_height * shape_table[idx].percentage
1234 + 99) / 100;
1235 cur_width = gui.char_width;
1236 }
1237#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001238 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1239 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
1241 /* Double wide character. */
1242 if (shape_table[idx].shape != SHAPE_VER)
1243 cur_width += gui.char_width;
1244# ifdef FEAT_RIGHTLEFT
1245 if (CURSOR_BAR_RIGHT)
1246 {
1247 /* gui.col points to the left halve of the character but
1248 * the vertical line needs to be on the right halve.
1249 * A double-wide horizontal line is also drawn from the
1250 * right halve in gui_mch_draw_part_cursor(). */
1251 col_off = TRUE;
1252 ++gui.col;
1253 }
1254# endif
1255 }
1256#endif
1257 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1258#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1259 if (col_off)
1260 --gui.col;
1261#endif
1262
1263#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1264 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1265 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1266 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1267 (guicolor_T)0, (guicolor_T)0, 0);
1268#endif
1269 }
1270 gui.highlight_mask = old_hl_mask;
1271 }
1272}
1273
1274#if defined(FEAT_MENU) || defined(PROTO)
1275 void
1276gui_position_menu()
1277{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001278# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (gui.menu_is_active && gui.in_use)
1280 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1281# endif
1282}
1283#endif
1284
1285/*
1286 * Position the various GUI components (text area, menu). The vertical
1287 * scrollbars are NOT handled here. See gui_update_scrollbars().
1288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 static void
1290gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001291 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 int text_area_x;
1294 int text_area_y;
1295 int text_area_width;
1296 int text_area_height;
1297
1298 /* avoid that moving components around generates events */
1299 ++hold_gui_events;
1300
1301 text_area_x = 0;
1302 if (gui.which_scrollbars[SBAR_LEFT])
1303 text_area_x += gui.scrollbar_width;
1304
1305 text_area_y = 0;
1306#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1307 gui.menu_width = total_width;
1308 if (gui.menu_is_active)
1309 text_area_y += gui.menu_height;
1310#endif
1311#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1312 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1313 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1314#endif
1315
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001316# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001317 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001318 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001319 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1323 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1324 {
1325# ifdef FEAT_GUI_ATHENA
1326 gui_mch_set_toolbar_pos(0, text_area_y,
1327 gui.menu_width, gui.toolbar_height);
1328# endif
1329 text_area_y += gui.toolbar_height;
1330 }
1331#endif
1332
1333 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1334 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1335
1336 gui_mch_set_text_area_pos(text_area_x,
1337 text_area_y,
1338 text_area_width,
1339 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001340#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 + xim_get_status_area_height()
1342#endif
1343 );
1344#ifdef FEAT_MENU
1345 gui_position_menu();
1346#endif
1347 if (gui.which_scrollbars[SBAR_BOTTOM])
1348 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1349 text_area_x,
1350 text_area_y + text_area_height,
1351 text_area_width,
1352 gui.scrollbar_height);
1353 gui.left_sbar_x = 0;
1354 gui.right_sbar_x = text_area_x + text_area_width;
1355
1356 --hold_gui_events;
1357}
1358
Bram Moolenaar02743632005-07-25 20:42:36 +00001359/*
1360 * Get the width of the widgets and decorations to the side of the text area.
1361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 int
1363gui_get_base_width()
1364{
1365 int base_width;
1366
1367 base_width = 2 * gui.border_offset;
1368 if (gui.which_scrollbars[SBAR_LEFT])
1369 base_width += gui.scrollbar_width;
1370 if (gui.which_scrollbars[SBAR_RIGHT])
1371 base_width += gui.scrollbar_width;
1372 return base_width;
1373}
1374
Bram Moolenaar02743632005-07-25 20:42:36 +00001375/*
1376 * Get the height of the widgets and decorations above and below the text area.
1377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 int
1379gui_get_base_height()
1380{
1381 int base_height;
1382
1383 base_height = 2 * gui.border_offset;
1384 if (gui.which_scrollbars[SBAR_BOTTOM])
1385 base_height += gui.scrollbar_height;
1386#ifdef FEAT_GUI_GTK
1387 /* We can't take the sizes properly into account until anything is
1388 * realized. Therefore we recalculate all the values here just before
1389 * setting the size. (--mdcki) */
1390#else
1391# ifdef FEAT_MENU
1392 if (gui.menu_is_active)
1393 base_height += gui.menu_height;
1394# endif
1395# ifdef FEAT_TOOLBAR
1396 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1397# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1398 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1399# else
1400 base_height += gui.toolbar_height;
1401# endif
1402# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001403# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1404 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001405 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001406 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408# ifdef FEAT_FOOTER
1409 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1410 base_height += gui.footer_height;
1411# endif
1412# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1413 base_height += gui_mch_text_area_extra_height();
1414# endif
1415#endif
1416 return base_height;
1417}
1418
1419/*
1420 * Should be called after the GUI shell has been resized. Its arguments are
1421 * the new width and height of the shell in pixels.
1422 */
1423 void
1424gui_resize_shell(pixel_width, pixel_height)
1425 int pixel_width;
1426 int pixel_height;
1427{
1428 static int busy = FALSE;
1429
1430 if (!gui.shell_created) /* ignore when still initializing */
1431 return;
1432
1433 /*
1434 * Can't resize the screen while it is being redrawn. Remember the new
1435 * size and handle it later.
1436 */
1437 if (updating_screen || busy)
1438 {
1439 new_pixel_width = pixel_width;
1440 new_pixel_height = pixel_height;
1441 return;
1442 }
1443
1444again:
1445 busy = TRUE;
1446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* Flush pending output before redrawing */
1448 out_flush();
1449
1450 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001451 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 gui_position_components(pixel_width);
1454
1455 gui_reset_scroll_region();
1456 /*
1457 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1458 * at the last line here (why does it have to be one row too low?).
1459 */
1460 if (State == ASKMORE || State == CONFIRM)
1461 gui.row = gui.num_rows;
1462
1463 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1464 * the safe side. */
1465 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1466 || gui.num_rows != Rows || gui.num_cols != Columns)
1467 shell_resized();
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 gui_update_scrollbars(TRUE);
1470 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001471#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 xim_set_status_area();
1473#endif
1474
1475 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /*
1478 * We could have been called again while redrawing the screen.
1479 * Need to do it all again with the latest size then.
1480 */
1481 if (new_pixel_height)
1482 {
1483 pixel_width = new_pixel_width;
1484 pixel_height = new_pixel_height;
1485 new_pixel_width = 0;
1486 new_pixel_height = 0;
1487 goto again;
1488 }
1489}
1490
1491/*
1492 * Check if gui_resize_shell() must be called.
1493 */
1494 void
1495gui_may_resize_shell()
1496{
1497 int h, w;
1498
1499 if (new_pixel_height)
1500 {
1501 /* careful: gui_resize_shell() may postpone the resize again if we
1502 * were called indirectly by it */
1503 w = new_pixel_width;
1504 h = new_pixel_height;
1505 new_pixel_width = 0;
1506 new_pixel_height = 0;
1507 gui_resize_shell(w, h);
1508 }
1509}
1510
1511 int
1512gui_get_shellsize()
1513{
1514 Rows = gui.num_rows;
1515 Columns = gui.num_cols;
1516 return OK;
1517}
1518
1519/*
1520 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001521 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1522 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001525gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001526 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001528 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 int base_width;
1531 int base_height;
1532 int width;
1533 int height;
1534 int min_width;
1535 int min_height;
1536 int screen_w;
1537 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001538#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001539 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001540 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001541#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001542 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 if (!gui.shell_created)
1545 return;
1546
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001547#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 /* If not setting to a user specified size and maximized, calculate the
1549 * number of characters that fit in the maximized window. */
1550 if (!mustset && gui_mch_maximized())
1551 {
1552 gui_mch_newfont();
1553 return;
1554 }
1555#endif
1556
1557 base_width = gui_get_base_width();
1558 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001559 if (fit_to_display)
1560 /* Remember the original window position. */
1561 gui_mch_get_winpos(&x, &y);
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563#ifdef USE_SUN_WORKSHOP
1564 if (!mustset && usingSunWorkShop
1565 && workshop_get_width_height(&width, &height))
1566 {
1567 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1568 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1569 }
1570 else
1571#endif
1572 {
1573 width = Columns * gui.char_width + base_width;
1574 height = Rows * gui.char_height + base_height;
1575 }
1576
1577 if (fit_to_display)
1578 {
1579 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001580 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 Columns = (screen_w - base_width) / gui.char_width;
1583 if (Columns < MIN_COLUMNS)
1584 Columns = MIN_COLUMNS;
1585 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001586#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001587 ++did_adjust;
1588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001590 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 Rows = (screen_h - base_height) / gui.char_height;
1593 check_shellsize();
1594 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001595#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001596 ++did_adjust;
1597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001599#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001600 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1601 && height + gui.char_height >= screen_h))
1602 /* don't unmaximize if at maximum size */
1603 un_maximize = FALSE;
1604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 gui.num_cols = Columns;
1607 gui.num_rows = Rows;
1608
1609 min_width = base_width + MIN_COLUMNS * gui.char_width;
1610 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001611#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001612 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001613#endif
1614
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001615#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001616 if (un_maximize)
1617 {
1618 /* If the window size is smaller than the screen unmaximize the
1619 * window, otherwise resizing won't work. */
1620 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1621 if ((width + gui.char_width < screen_w
1622 || height + gui.char_height * 2 < screen_h)
1623 && gui_mch_maximized())
1624 gui_mch_unmaximize();
1625 }
1626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001629 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001631 if (fit_to_display && x >= 0 && y >= 0)
1632 {
1633 /* Some window managers put the Vim window left of/above the screen.
1634 * Only change the position if it wasn't already negative before
1635 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 gui_mch_update();
1637 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1638 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1639 }
1640
1641 gui_position_components(width);
1642 gui_update_scrollbars(TRUE);
1643 gui_reset_scroll_region();
1644}
1645
1646/*
1647 * Called when Rows and/or Columns has changed.
1648 */
1649 void
1650gui_new_shellsize()
1651{
1652 gui_reset_scroll_region();
1653}
1654
1655/*
1656 * Make scroll region cover whole screen.
1657 */
1658 void
1659gui_reset_scroll_region()
1660{
1661 gui.scroll_region_top = 0;
1662 gui.scroll_region_bot = gui.num_rows - 1;
1663 gui.scroll_region_left = 0;
1664 gui.scroll_region_right = gui.num_cols - 1;
1665}
1666
1667 void
1668gui_start_highlight(mask)
1669 int mask;
1670{
1671 if (mask > HL_ALL) /* highlight code */
1672 gui.highlight_mask = mask;
1673 else /* mask */
1674 gui.highlight_mask |= mask;
1675}
1676
1677 void
1678gui_stop_highlight(mask)
1679 int mask;
1680{
1681 if (mask > HL_ALL) /* highlight code */
1682 gui.highlight_mask = HL_NORMAL;
1683 else /* mask */
1684 gui.highlight_mask &= ~mask;
1685}
1686
1687/*
1688 * Clear a rectangular region of the screen from text pos (row1, col1) to
1689 * (row2, col2) inclusive.
1690 */
1691 void
1692gui_clear_block(row1, col1, row2, col2)
1693 int row1;
1694 int col1;
1695 int row2;
1696 int col2;
1697{
1698 /* Clear the selection if we are about to write over it */
1699 clip_may_clear_selection(row1, row2);
1700
1701 gui_mch_clear_block(row1, col1, row2, col2);
1702
1703 /* Invalidate cursor if it was in this block */
1704 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1705 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1706 gui.cursor_is_valid = FALSE;
1707}
1708
1709/*
1710 * Write code to update the cursor later. This avoids the need to flush the
1711 * output buffer before calling gui_update_cursor().
1712 */
1713 void
1714gui_update_cursor_later()
1715{
1716 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1717}
1718
1719 void
1720gui_write(s, len)
1721 char_u *s;
1722 int len;
1723{
1724 char_u *p;
1725 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 int force_scrollbar = FALSE;
1728 static win_T *old_curwin = NULL;
1729
1730/* #define DEBUG_GUI_WRITE */
1731#ifdef DEBUG_GUI_WRITE
1732 {
1733 int i;
1734 char_u *str;
1735
1736 printf("gui_write(%d):\n ", len);
1737 for (i = 0; i < len; i++)
1738 if (s[i] == ESC)
1739 {
1740 if (i != 0)
1741 printf("\n ");
1742 printf("<ESC>");
1743 }
1744 else
1745 {
1746 str = transchar_byte(s[i]);
1747 if (str[0] && str[1])
1748 printf("<%s>", (char *)str);
1749 else
1750 printf("%s", (char *)str);
1751 }
1752 printf("\n");
1753 }
1754#endif
1755 while (len)
1756 {
1757 if (s[0] == ESC && s[1] == '|')
1758 {
1759 p = s + 2;
1760 if (VIM_ISDIGIT(*p))
1761 {
1762 arg1 = getdigits(&p);
1763 if (p > s + len)
1764 break;
1765 if (*p == ';')
1766 {
1767 ++p;
1768 arg2 = getdigits(&p);
1769 if (p > s + len)
1770 break;
1771 }
1772 }
1773 switch (*p)
1774 {
1775 case 'C': /* Clear screen */
1776 clip_scroll_selection(9999);
1777 gui_mch_clear_all();
1778 gui.cursor_is_valid = FALSE;
1779 force_scrollbar = TRUE;
1780 break;
1781 case 'M': /* Move cursor */
1782 gui_set_cursor(arg1, arg2);
1783 break;
1784 case 's': /* force cursor (shape) update */
1785 force_cursor = TRUE;
1786 break;
1787 case 'R': /* Set scroll region */
1788 if (arg1 < arg2)
1789 {
1790 gui.scroll_region_top = arg1;
1791 gui.scroll_region_bot = arg2;
1792 }
1793 else
1794 {
1795 gui.scroll_region_top = arg2;
1796 gui.scroll_region_bot = arg1;
1797 }
1798 break;
1799#ifdef FEAT_VERTSPLIT
1800 case 'V': /* Set vertical scroll region */
1801 if (arg1 < arg2)
1802 {
1803 gui.scroll_region_left = arg1;
1804 gui.scroll_region_right = arg2;
1805 }
1806 else
1807 {
1808 gui.scroll_region_left = arg2;
1809 gui.scroll_region_right = arg1;
1810 }
1811 break;
1812#endif
1813 case 'd': /* Delete line */
1814 gui_delete_lines(gui.row, 1);
1815 break;
1816 case 'D': /* Delete lines */
1817 gui_delete_lines(gui.row, arg1);
1818 break;
1819 case 'i': /* Insert line */
1820 gui_insert_lines(gui.row, 1);
1821 break;
1822 case 'I': /* Insert lines */
1823 gui_insert_lines(gui.row, arg1);
1824 break;
1825 case '$': /* Clear to end-of-line */
1826 gui_clear_block(gui.row, gui.col, gui.row,
1827 (int)Columns - 1);
1828 break;
1829 case 'h': /* Turn on highlighting */
1830 gui_start_highlight(arg1);
1831 break;
1832 case 'H': /* Turn off highlighting */
1833 gui_stop_highlight(arg1);
1834 break;
1835 case 'f': /* flash the window (visual bell) */
1836 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1837 break;
1838 default:
1839 p = s + 1; /* Skip the ESC */
1840 break;
1841 }
1842 len -= (int)(++p - s);
1843 s = p;
1844 }
1845 else if (
1846#ifdef EBCDIC
1847 CtrlChar(s[0]) != 0 /* Ctrl character */
1848#else
1849 s[0] < 0x20 /* Ctrl character */
1850#endif
1851#ifdef FEAT_SIGN_ICONS
1852 && s[0] != SIGN_BYTE
1853# ifdef FEAT_NETBEANS_INTG
1854 && s[0] != MULTISIGN_BYTE
1855# endif
1856#endif
1857 )
1858 {
1859 if (s[0] == '\n') /* NL */
1860 {
1861 gui.col = 0;
1862 if (gui.row < gui.scroll_region_bot)
1863 gui.row++;
1864 else
1865 gui_delete_lines(gui.scroll_region_top, 1);
1866 }
1867 else if (s[0] == '\r') /* CR */
1868 {
1869 gui.col = 0;
1870 }
1871 else if (s[0] == '\b') /* Backspace */
1872 {
1873 if (gui.col)
1874 --gui.col;
1875 }
1876 else if (s[0] == Ctrl_L) /* cursor-right */
1877 {
1878 ++gui.col;
1879 }
1880 else if (s[0] == Ctrl_G) /* Beep */
1881 {
1882 gui_mch_beep();
1883 }
1884 /* Other Ctrl character: shouldn't happen! */
1885
1886 --len; /* Skip this char */
1887 ++s;
1888 }
1889 else
1890 {
1891 p = s;
1892 while (len > 0 && (
1893#ifdef EBCDIC
1894 CtrlChar(*p) == 0
1895#else
1896 *p >= 0x20
1897#endif
1898#ifdef FEAT_SIGN_ICONS
1899 || *p == SIGN_BYTE
1900# ifdef FEAT_NETBEANS_INTG
1901 || *p == MULTISIGN_BYTE
1902# endif
1903#endif
1904 ))
1905 {
1906 len--;
1907 p++;
1908 }
1909 gui_outstr(s, (int)(p - s));
1910 s = p;
1911 }
1912 }
1913
1914 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1915 * set). */
1916 if (force_cursor)
1917 gui_update_cursor(TRUE, TRUE);
1918
1919 /* When switching to another window the dragging must have stopped.
1920 * Required for GTK, dragged_sb isn't reset. */
1921 if (old_curwin != curwin)
1922 gui.dragged_sb = SBAR_NONE;
1923
1924 /* Update the scrollbars after clearing the screen or when switched
1925 * to another window.
1926 * Update the horizontal scrollbar always, it's difficult to check all
1927 * situations where it might change. */
1928 if (force_scrollbar || old_curwin != curwin)
1929 gui_update_scrollbars(force_scrollbar);
1930 else
1931 gui_update_horiz_scrollbar(FALSE);
1932 old_curwin = curwin;
1933
1934 /*
1935 * We need to make sure this is cleared since Athena doesn't tell us when
1936 * he is done dragging. Do the same for GTK.
1937 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001938#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 gui.dragged_sb = SBAR_NONE;
1940#endif
1941
1942 gui_mch_flush(); /* In case vim decides to take a nap */
1943}
1944
1945/*
1946 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1947 * produces wrong results. Call gui_dont_update_cursor() before that code and
1948 * gui_can_update_cursor() afterwards.
1949 */
1950 void
1951gui_dont_update_cursor()
1952{
1953 if (gui.in_use)
1954 {
1955 /* Undraw the cursor now, we probably can't do it after the change. */
1956 gui_undraw_cursor();
1957 can_update_cursor = FALSE;
1958 }
1959}
1960
1961 void
1962gui_can_update_cursor()
1963{
1964 can_update_cursor = TRUE;
1965 /* No need to update the cursor right now, there is always more output
1966 * after scrolling. */
1967}
1968
1969 static void
1970gui_outstr(s, len)
1971 char_u *s;
1972 int len;
1973{
1974 int this_len;
1975#ifdef FEAT_MBYTE
1976 int cells;
1977#endif
1978
1979 if (len == 0)
1980 return;
1981
1982 if (len < 0)
1983 len = (int)STRLEN(s);
1984
1985 while (len > 0)
1986 {
1987#ifdef FEAT_MBYTE
1988 if (has_mbyte)
1989 {
1990 /* Find out how many chars fit in the current line. */
1991 cells = 0;
1992 for (this_len = 0; this_len < len; )
1993 {
1994 cells += (*mb_ptr2cells)(s + this_len);
1995 if (gui.col + cells > Columns)
1996 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001997 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (this_len > len)
2000 this_len = len; /* don't include following composing char */
2001 }
2002 else
2003#endif
2004 if (gui.col + len > Columns)
2005 this_len = Columns - gui.col;
2006 else
2007 this_len = len;
2008
2009 (void)gui_outstr_nowrap(s, this_len,
2010 0, (guicolor_T)0, (guicolor_T)0, 0);
2011 s += this_len;
2012 len -= this_len;
2013#ifdef FEAT_MBYTE
2014 /* fill up for a double-width char that doesn't fit. */
2015 if (len > 0 && gui.col < Columns)
2016 (void)gui_outstr_nowrap((char_u *)" ", 1,
2017 0, (guicolor_T)0, (guicolor_T)0, 0);
2018#endif
2019 /* The cursor may wrap to the next line. */
2020 if (gui.col >= Columns)
2021 {
2022 gui.col = 0;
2023 gui.row++;
2024 }
2025 }
2026}
2027
2028/*
2029 * Output one character (may be one or two display cells).
2030 * Caller must check for valid "off".
2031 * Returns FAIL or OK, just like gui_outstr_nowrap().
2032 */
2033 static int
2034gui_screenchar(off, flags, fg, bg, back)
2035 int off; /* Offset from start of screen */
2036 int flags;
2037 guicolor_T fg, bg; /* colors for cursor */
2038 int back; /* backup this many chars when using bold trick */
2039{
2040#ifdef FEAT_MBYTE
2041 char_u buf[MB_MAXBYTES + 1];
2042
2043 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2044 if (enc_utf8 && ScreenLines[off] == 0)
2045 return OK;
2046
2047 if (enc_utf8 && ScreenLinesUC[off] != 0)
2048 /* Draw UTF-8 multi-byte character. */
2049 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2050 flags, fg, bg, back);
2051
2052 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2053 {
2054 buf[0] = ScreenLines[off];
2055 buf[1] = ScreenLines2[off];
2056 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2057 }
2058
2059 /* Draw non-multi-byte character or DBCS character. */
2060 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002061 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 flags, fg, bg, back);
2063#else
2064 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2065#endif
2066}
2067
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002068#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/*
2070 * Output the string at the given screen position. This is used in place
2071 * of gui_screenchar() where possible because Pango needs as much context
2072 * as possible to work nicely. It's a lot faster as well.
2073 */
2074 static int
2075gui_screenstr(off, len, flags, fg, bg, back)
2076 int off; /* Offset from start of screen */
2077 int len; /* string length in screen cells */
2078 int flags;
2079 guicolor_T fg, bg; /* colors for cursor */
2080 int back; /* backup this many chars when using bold trick */
2081{
2082 char_u *buf;
2083 int outlen = 0;
2084 int i;
2085 int retval;
2086
2087 if (len <= 0) /* "cannot happen"? */
2088 return OK;
2089
2090 if (enc_utf8)
2091 {
2092 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2093 if (buf == NULL)
2094 return OK; /* not much we could do here... */
2095
2096 for (i = off; i < off + len; ++i)
2097 {
2098 if (ScreenLines[i] == 0)
2099 continue; /* skip second half of double-width char */
2100
2101 if (ScreenLinesUC[i] == 0)
2102 buf[outlen++] = ScreenLines[i];
2103 else
2104 outlen += utfc_char2bytes(i, buf + outlen);
2105 }
2106
2107 buf[outlen] = NUL; /* only to aid debugging */
2108 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2109 vim_free(buf);
2110
2111 return retval;
2112 }
2113 else if (enc_dbcs == DBCS_JPNU)
2114 {
2115 buf = alloc((unsigned)(len * 2 + 1));
2116 if (buf == NULL)
2117 return OK; /* not much we could do here... */
2118
2119 for (i = off; i < off + len; ++i)
2120 {
2121 buf[outlen++] = ScreenLines[i];
2122
2123 /* handle double-byte single-width char */
2124 if (ScreenLines[i] == 0x8e)
2125 buf[outlen++] = ScreenLines2[i];
2126 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2127 buf[outlen++] = ScreenLines[++i];
2128 }
2129
2130 buf[outlen] = NUL; /* only to aid debugging */
2131 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2132 vim_free(buf);
2133
2134 return retval;
2135 }
2136 else
2137 {
2138 return gui_outstr_nowrap(&ScreenLines[off], len,
2139 flags, fg, bg, back);
2140 }
2141}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002142#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144/*
2145 * Output the given string at the current cursor position. If the string is
2146 * too long to fit on the line, then it is truncated.
2147 * "flags":
2148 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2149 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002150 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 * background.
2152 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2153 * it.
2154 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2155 * FAIL (the caller should start drawing "back" chars back).
2156 */
2157 int
2158gui_outstr_nowrap(s, len, flags, fg, bg, back)
2159 char_u *s;
2160 int len;
2161 int flags;
2162 guicolor_T fg, bg; /* colors for cursor */
2163 int back; /* backup this many chars when using bold trick */
2164{
2165 long_u highlight_mask;
2166 long_u hl_mask_todo;
2167 guicolor_T fg_color;
2168 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002169 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002170#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 GuiFont font = NOFONT;
2172# ifdef FEAT_XFONTSET
2173 GuiFontset fontset = NOFONTSET;
2174# endif
2175#endif
2176 attrentry_T *aep = NULL;
2177 int draw_flags;
2178 int col = gui.col;
2179#ifdef FEAT_SIGN_ICONS
2180 int draw_sign = FALSE;
2181# ifdef FEAT_NETBEANS_INTG
2182 int multi_sign = FALSE;
2183# endif
2184#endif
2185
2186 if (len < 0)
2187 len = (int)STRLEN(s);
2188 if (len == 0)
2189 return OK;
2190
2191#ifdef FEAT_SIGN_ICONS
2192 if (*s == SIGN_BYTE
2193# ifdef FEAT_NETBEANS_INTG
2194 || *s == MULTISIGN_BYTE
2195# endif
2196 )
2197 {
2198# ifdef FEAT_NETBEANS_INTG
2199 if (*s == MULTISIGN_BYTE)
2200 multi_sign = TRUE;
2201# endif
2202 /* draw spaces instead */
2203 s = (char_u *)" ";
2204 if (len == 1 && col > 0)
2205 --col;
2206 len = 2;
2207 draw_sign = TRUE;
2208 highlight_mask = 0;
2209 }
2210 else
2211#endif
2212 if (gui.highlight_mask > HL_ALL)
2213 {
2214 aep = syn_gui_attr2entry(gui.highlight_mask);
2215 if (aep == NULL) /* highlighting not set */
2216 highlight_mask = 0;
2217 else
2218 highlight_mask = aep->ae_attr;
2219 }
2220 else
2221 highlight_mask = gui.highlight_mask;
2222 hl_mask_todo = highlight_mask;
2223
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002224#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /* Set the font */
2226 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2227 font = aep->ae_u.gui.font;
2228# ifdef FEAT_XFONTSET
2229 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2230 fontset = aep->ae_u.gui.fontset;
2231# endif
2232 else
2233 {
2234# ifdef FEAT_XFONTSET
2235 if (gui.fontset != NOFONTSET)
2236 fontset = gui.fontset;
2237 else
2238# endif
2239 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2240 {
2241 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2242 {
2243 font = gui.boldital_font;
2244 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2245 }
2246 else if (gui.bold_font != NOFONT)
2247 {
2248 font = gui.bold_font;
2249 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2250 }
2251 else
2252 font = gui.norm_font;
2253 }
2254 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2255 {
2256 font = gui.ital_font;
2257 hl_mask_todo &= ~HL_ITALIC;
2258 }
2259 else
2260 font = gui.norm_font;
2261 }
2262# ifdef FEAT_XFONTSET
2263 if (fontset != NOFONTSET)
2264 gui_mch_set_fontset(fontset);
2265 else
2266# endif
2267 gui_mch_set_font(font);
2268#endif
2269
2270 draw_flags = 0;
2271
2272 /* Set the color */
2273 bg_color = gui.back_pixel;
2274 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2275 {
2276 draw_flags |= DRAW_CURSOR;
2277 fg_color = fg;
2278 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002279 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 else if (aep != NULL)
2282 {
2283 fg_color = aep->ae_u.gui.fg_color;
2284 if (fg_color == INVALCOLOR)
2285 fg_color = gui.norm_pixel;
2286 bg_color = aep->ae_u.gui.bg_color;
2287 if (bg_color == INVALCOLOR)
2288 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002289 sp_color = aep->ae_u.gui.sp_color;
2290 if (sp_color == INVALCOLOR)
2291 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002296 sp_color = fg_color;
2297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2300 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002301#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 gui_mch_set_colors(bg_color, fg_color);
2303#else
2304 gui_mch_set_fg_color(bg_color);
2305 gui_mch_set_bg_color(fg_color);
2306#endif
2307 }
2308 else
2309 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002310#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 gui_mch_set_colors(fg_color, bg_color);
2312#else
2313 gui_mch_set_fg_color(fg_color);
2314 gui_mch_set_bg_color(bg_color);
2315#endif
2316 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002317 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
2319 /* Clear the selection if we are about to write over it */
2320 if (!(flags & GUI_MON_NOCLEAR))
2321 clip_may_clear_selection(gui.row, gui.row);
2322
2323
2324#ifndef MSWIN16_FASTTEXT
2325 /* If there's no bold font, then fake it */
2326 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2327 draw_flags |= DRAW_BOLD;
2328#endif
2329
2330 /*
2331 * When drawing bold or italic characters the spill-over from the left
2332 * neighbor may be destroyed. Let the caller backup to start redrawing
2333 * just after a blank.
2334 */
2335 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2336 return FAIL;
2337
Bram Moolenaare60acc12011-05-10 16:41:25 +02002338#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /* If there's no italic font, then fake it.
2340 * For GTK2, we don't need a different font for italic style. */
2341 if (hl_mask_todo & HL_ITALIC)
2342 draw_flags |= DRAW_ITALIC;
2343
2344 /* Do we underline the text? */
2345 if (hl_mask_todo & HL_UNDERLINE)
2346 draw_flags |= DRAW_UNDERL;
2347#else
2348 /* Do we underline the text? */
2349 if ((hl_mask_todo & HL_UNDERLINE)
2350# ifndef MSWIN16_FASTTEXT
2351 || (hl_mask_todo & HL_ITALIC)
2352# endif
2353 )
2354 draw_flags |= DRAW_UNDERL;
2355#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002356 /* Do we undercurl the text? */
2357 if (hl_mask_todo & HL_UNDERCURL)
2358 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002360 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (flags & GUI_MON_TRS_CURSOR)
2362 draw_flags |= DRAW_TRANSP;
2363
2364 /*
2365 * Draw the text.
2366 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002367#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 /* The value returned is the length in display cells */
2369 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2370#else
2371# ifdef FEAT_MBYTE
2372 if (enc_utf8)
2373 {
2374 int start; /* index of bytes to be drawn */
2375 int cells; /* cellwidth of bytes to be drawn */
2376 int thislen; /* length of bytes to be drawin */
2377 int cn; /* cellwidth of current char */
2378 int i; /* index of current char */
2379 int c; /* current char value */
2380 int cl; /* byte length of current char */
2381 int comping; /* current char is composing */
2382 int scol = col; /* screen column */
Bram Moolenaar45933962013-01-23 17:43:57 +01002383 int curr_wide; /* use 'guifontwide' */
2384 int prev_wide = FALSE;
2385 int wide_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 /* Break the string at a composing character, it has to be drawn on
2388 * top of the previous character. */
2389 start = 0;
2390 cells = 0;
2391 for (i = 0; i < len; i += cl)
2392 {
2393 c = utf_ptr2char(s + i);
2394 cn = utf_char2cells(c);
2395 if (cn > 1
2396# ifdef FEAT_XFONTSET
2397 && fontset == NOFONTSET
2398# endif
2399 && gui.wide_font != NOFONT)
Bram Moolenaar45933962013-01-23 17:43:57 +01002400 curr_wide = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 else
Bram Moolenaar45933962013-01-23 17:43:57 +01002402 curr_wide = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 comping = utf_iscomposing(c);
2404 if (!comping) /* count cells from non-composing chars */
2405 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002406 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (cl == 0) /* hit end of string */
2408 len = i + cl; /* len must be wrong "cannot happen" */
2409
Bram Moolenaar45933962013-01-23 17:43:57 +01002410 wide_changed = curr_wide != prev_wide;
2411
2412 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 * a composing character. */
Bram Moolenaar45933962013-01-23 17:43:57 +01002414 if (i + cl >= len || (comping && i > start) || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002415# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 || (cn > 1
2417# ifdef FEAT_XFONTSET
2418 /* No fontset: At least draw char after wide char at
2419 * right position. */
2420 && fontset == NOFONTSET
2421# endif
2422 )
2423# endif
2424 )
2425 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002426 if (comping || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 thislen = i - start;
2428 else
2429 thislen = i - start + cl;
2430 if (thislen > 0)
2431 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002432 if (prev_wide)
2433 gui_mch_set_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2435 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002436 if (prev_wide)
2437 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 start += thislen;
2439 }
2440 scol += cells;
2441 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002442 /* Adjust to not draw a character which width is changed
2443 * against with last one. */
2444 if (wide_changed && !comping)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002446 scol -= cn;
2447 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002450# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002451 /* No fontset: draw a space to fill the gap after a wide char
2452 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2454# ifdef FEAT_XFONTSET
2455 && fontset == NOFONTSET
2456# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002457 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2459 1, draw_flags);
2460# endif
2461 }
2462 /* Draw a composing char on top of the previous char. */
2463 if (comping)
2464 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002465# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002466 /* Carbon ATSUI autodraws composing char over previous char */
2467 gui_mch_draw_string(gui.row, scol, s + i, cl,
2468 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002469# else
2470 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2471 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 start = i + cl;
2474 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002475 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477 /* The stuff below assumes "len" is the length in screen columns. */
2478 len = scol - col;
2479 }
2480 else
2481# endif
2482 {
2483 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2484# ifdef FEAT_MBYTE
2485 if (enc_dbcs == DBCS_JPNU)
2486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 /* Get the length in display cells, this can be different from the
2488 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002489 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491# endif
2492 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002493#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2496 gui.col = col + len;
2497
2498 /* May need to invert it when it's part of the selection. */
2499 if (flags & GUI_MON_NOCLEAR)
2500 clip_may_redraw_selection(gui.row, col, len);
2501
2502 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2503 {
2504 /* Invalidate the old physical cursor position if we wrote over it */
2505 if (gui.cursor_row == gui.row
2506 && gui.cursor_col >= col
2507 && gui.cursor_col < col + len)
2508 gui.cursor_is_valid = FALSE;
2509 }
2510
2511#ifdef FEAT_SIGN_ICONS
2512 if (draw_sign)
2513 /* Draw the sign on top of the spaces. */
2514 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002515# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002516 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (multi_sign)
2518 netbeans_draw_multisign_indicator(gui.row);
2519# endif
2520#endif
2521
2522 return OK;
2523}
2524
2525/*
2526 * Un-draw the cursor. Actually this just redraws the character at the given
2527 * position. The character just before it too, for when it was in bold.
2528 */
2529 void
2530gui_undraw_cursor()
2531{
2532 if (gui.cursor_is_valid)
2533 {
2534#ifdef FEAT_HANGULIN
2535 if (composing_hangul
2536 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2537 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2538 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2539 gui.norm_pixel, gui.back_pixel, 0);
2540 else
2541 {
2542#endif
2543 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2544 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2545 && gui.cursor_col > 0)
2546 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2547 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2548#ifdef FEAT_HANGULIN
2549 if (composing_hangul)
2550 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2551 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2552 }
2553#endif
2554 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2555 * here in case it wasn't needed to undraw it. */
2556 gui.cursor_is_valid = FALSE;
2557 }
2558}
2559
2560 void
2561gui_redraw(x, y, w, h)
2562 int x;
2563 int y;
2564 int w;
2565 int h;
2566{
2567 int row1, col1, row2, col2;
2568
2569 row1 = Y_2_ROW(y);
2570 col1 = X_2_COL(x);
2571 row2 = Y_2_ROW(y + h - 1);
2572 col2 = X_2_COL(x + w - 1);
2573
2574 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2575
2576 /*
2577 * We may need to redraw the cursor, but don't take it upon us to change
2578 * its location after a scroll.
2579 * (maybe be more strict even and test col too?)
2580 * These things may be outside the update/clipping region and reality may
2581 * not reflect Vims internal ideas if these operations are clipped away.
2582 */
2583 if (gui.row == gui.cursor_row)
2584 gui_update_cursor(TRUE, TRUE);
2585}
2586
2587/*
2588 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2589 * from col1 to col2 (inclusive).
2590 * Return TRUE when the character before the first drawn character has
2591 * different attributes (may have to be redrawn too).
2592 */
2593 int
2594gui_redraw_block(row1, col1, row2, col2, flags)
2595 int row1;
2596 int col1;
2597 int row2;
2598 int col2;
2599 int flags; /* flags for gui_outstr_nowrap() */
2600{
2601 int old_row, old_col;
2602 long_u old_hl_mask;
2603 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002604 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 int idx, len;
2606 int back, nback;
2607 int retval = FALSE;
2608#ifdef FEAT_MBYTE
2609 int orig_col1, orig_col2;
2610#endif
2611
2612 /* Don't try to update when ScreenLines is not valid */
2613 if (!screen_cleared || ScreenLines == NULL)
2614 return retval;
2615
2616 /* Don't try to draw outside the shell! */
2617 /* Check everything, strange values may be caused by a big border width */
2618 col1 = check_col(col1);
2619 col2 = check_col(col2);
2620 row1 = check_row(row1);
2621 row2 = check_row(row2);
2622
2623 /* Remember where our cursor was */
2624 old_row = gui.row;
2625 old_col = gui.col;
2626 old_hl_mask = gui.highlight_mask;
2627#ifdef FEAT_MBYTE
2628 orig_col1 = col1;
2629 orig_col2 = col2;
2630#endif
2631
2632 for (gui.row = row1; gui.row <= row2; gui.row++)
2633 {
2634#ifdef FEAT_MBYTE
2635 /* When only half of a double-wide character is in the block, include
2636 * the other half. */
2637 col1 = orig_col1;
2638 col2 = orig_col2;
2639 off = LineOffset[gui.row];
2640 if (enc_dbcs != 0)
2641 {
2642 if (col1 > 0)
2643 col1 -= dbcs_screen_head_off(ScreenLines + off,
2644 ScreenLines + off + col1);
2645 col2 += dbcs_screen_tail_off(ScreenLines + off,
2646 ScreenLines + off + col2);
2647 }
2648 else if (enc_utf8)
2649 {
2650 if (ScreenLines[off + col1] == 0)
2651 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002652# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2654 ++col2;
2655# endif
2656 }
2657#endif
2658 gui.col = col1;
2659 off = LineOffset[gui.row] + gui.col;
2660 len = col2 - col1 + 1;
2661
2662 /* Find how many chars back this highlighting starts, or where a space
2663 * is. Needed for when the bold trick is used */
2664 for (back = 0; back < col1; ++back)
2665 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2666 || ScreenLines[off - 1 - back] == ' ')
2667 break;
2668 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2669 && ScreenLines[off - 1] != ' ');
2670
2671 /* Break it up in strings of characters with the same attributes. */
2672 /* Print UTF-8 characters individually. */
2673 while (len > 0)
2674 {
2675 first_attr = ScreenAttrs[off];
2676 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002677#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (enc_utf8 && ScreenLinesUC[off] != 0)
2679 {
2680 /* output multi-byte character separately */
2681 nback = gui_screenchar(off, flags,
2682 (guicolor_T)0, (guicolor_T)0, back);
2683 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2684 idx = 2;
2685 else
2686 idx = 1;
2687 }
2688 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2689 {
2690 /* output double-byte, single-width character separately */
2691 nback = gui_screenchar(off, flags,
2692 (guicolor_T)0, (guicolor_T)0, back);
2693 idx = 1;
2694 }
2695 else
2696#endif
2697 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002698#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 for (idx = 0; idx < len; ++idx)
2700 {
2701 if (enc_utf8 && ScreenLines[off + idx] == 0)
2702 continue; /* skip second half of double-width char */
2703 if (ScreenAttrs[off + idx] != first_attr)
2704 break;
2705 }
2706 /* gui_screenstr() takes care of multibyte chars */
2707 nback = gui_screenstr(off, idx, flags,
2708 (guicolor_T)0, (guicolor_T)0, back);
2709#else
2710 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2711 idx++)
2712 {
2713# ifdef FEAT_MBYTE
2714 /* Stop at a multi-byte Unicode character. */
2715 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2716 break;
2717 if (enc_dbcs == DBCS_JPNU)
2718 {
2719 /* Stop at a double-byte single-width char. */
2720 if (ScreenLines[off + idx] == 0x8e)
2721 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002722 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 + off + idx) == 2)
2724 ++idx; /* skip second byte of double-byte char */
2725 }
2726# endif
2727 }
2728 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2729 (guicolor_T)0, (guicolor_T)0, back);
2730#endif
2731 }
2732 if (nback == FAIL)
2733 {
2734 /* Must back up to start drawing where a bold or italic word
2735 * starts. */
2736 off -= back;
2737 len += back;
2738 gui.col -= back;
2739 }
2740 else
2741 {
2742 off += idx;
2743 len -= idx;
2744 }
2745 back = 0;
2746 }
2747 }
2748
2749 /* Put the cursor back where it was */
2750 gui.row = old_row;
2751 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002752 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
2754 return retval;
2755}
2756
2757 static void
2758gui_delete_lines(row, count)
2759 int row;
2760 int count;
2761{
2762 if (count <= 0)
2763 return;
2764
2765 if (row + count > gui.scroll_region_bot)
2766 /* Scrolled out of region, just blank the lines out */
2767 gui_clear_block(row, gui.scroll_region_left,
2768 gui.scroll_region_bot, gui.scroll_region_right);
2769 else
2770 {
2771 gui_mch_delete_lines(row, count);
2772
2773 /* If the cursor was in the deleted lines it's now gone. If the
2774 * cursor was in the scrolled lines adjust its position. */
2775 if (gui.cursor_row >= row
2776 && gui.cursor_col >= gui.scroll_region_left
2777 && gui.cursor_col <= gui.scroll_region_right)
2778 {
2779 if (gui.cursor_row < row + count)
2780 gui.cursor_is_valid = FALSE;
2781 else if (gui.cursor_row <= gui.scroll_region_bot)
2782 gui.cursor_row -= count;
2783 }
2784 }
2785}
2786
2787 static void
2788gui_insert_lines(row, count)
2789 int row;
2790 int count;
2791{
2792 if (count <= 0)
2793 return;
2794
2795 if (row + count > gui.scroll_region_bot)
2796 /* Scrolled out of region, just blank the lines out */
2797 gui_clear_block(row, gui.scroll_region_left,
2798 gui.scroll_region_bot, gui.scroll_region_right);
2799 else
2800 {
2801 gui_mch_insert_lines(row, count);
2802
2803 if (gui.cursor_row >= gui.row
2804 && gui.cursor_col >= gui.scroll_region_left
2805 && gui.cursor_col <= gui.scroll_region_right)
2806 {
2807 if (gui.cursor_row <= gui.scroll_region_bot - count)
2808 gui.cursor_row += count;
2809 else if (gui.cursor_row <= gui.scroll_region_bot)
2810 gui.cursor_is_valid = FALSE;
2811 }
2812 }
2813}
2814
2815/*
2816 * The main GUI input routine. Waits for a character from the keyboard.
2817 * wtime == -1 Wait forever.
2818 * wtime == 0 Don't wait.
2819 * wtime > 0 Wait wtime milliseconds for a character.
2820 * Returns OK if a character was found to be available within the given time,
2821 * or FAIL otherwise.
2822 */
2823 int
2824gui_wait_for_chars(wtime)
2825 long wtime;
2826{
2827 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002829#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 /*
2831 * If we're going to wait a bit, update the menus and mouse shape for the
2832 * current State.
2833 */
2834 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 gui_update_menus(0);
2836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 gui_mch_update();
2839 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 /* Before waiting, flush any output to the screen. */
2845 gui_mch_flush();
2846
2847 if (wtime > 0)
2848 {
2849 /* Blink when waiting for a character. Probably only does something
2850 * for showmatch() */
2851 gui_mch_start_blink();
2852 retval = gui_mch_wait_for_chars(wtime);
2853 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 return retval;
2855 }
2856
2857 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002858 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 */
2860 gui_mch_start_blink();
2861
Bram Moolenaar3918c952005-03-15 22:34:55 +00002862 retval = FAIL;
2863 /*
2864 * We may want to trigger the CursorHold event. First wait for
2865 * 'updatetime' and if nothing is typed within that time put the
2866 * K_CURSORHOLD key in the input buffer.
2867 */
2868 if (gui_mch_wait_for_chars(p_ut) == OK)
2869 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002871 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002873 char_u buf[3];
2874
2875 /* Put K_CURSORHOLD in the input buffer. */
2876 buf[0] = CSI;
2877 buf[1] = KS_EXTRA;
2878 buf[2] = (int)KE_CURSORHOLD;
2879 add_to_input_buf(buf, 3);
2880
2881 retval = OK;
2882 }
2883#endif
2884
2885 if (retval == FAIL)
2886 {
2887 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002888 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
2892 gui_mch_stop_blink();
2893 return retval;
2894}
2895
2896/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002897 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
2899 static void
2900fill_mouse_coord(p, col, row)
2901 char_u *p;
2902 int col;
2903 int row;
2904{
2905 p[0] = (char_u)(col / 128 + ' ' + 1);
2906 p[1] = (char_u)(col % 128 + ' ' + 1);
2907 p[2] = (char_u)(row / 128 + ' ' + 1);
2908 p[3] = (char_u)(row % 128 + ' ' + 1);
2909}
2910
2911/*
2912 * Generic mouse support function. Add a mouse event to the input buffer with
2913 * the given properties.
2914 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2915 * MOUSE_X1, MOUSE_X2
2916 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002917 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2918 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 * x, y --- Coordinates of mouse in pixels.
2920 * repeated_click --- TRUE if this click comes only a short time after a
2921 * previous click.
2922 * modifiers --- Bit field which may be any of the following modifiers
2923 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2924 * This function will ignore drag events where the mouse has not moved to a new
2925 * character.
2926 */
2927 void
2928gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2929 int button;
2930 int x;
2931 int y;
2932 int repeated_click;
2933 int_u modifiers;
2934{
2935 static int prev_row = 0, prev_col = 0;
2936 static int prev_button = -1;
2937 static int num_clicks = 1;
2938 char_u string[10];
2939 enum key_extra button_char;
2940 int row, col;
2941#ifdef FEAT_CLIPBOARD
2942 int checkfor;
2943 int did_clip = FALSE;
2944#endif
2945
2946 /*
2947 * Scrolling may happen at any time, also while a selection is present.
2948 */
2949 switch (button)
2950 {
2951 case MOUSE_X1:
2952 button_char = KE_X1MOUSE;
2953 goto button_set;
2954 case MOUSE_X2:
2955 button_char = KE_X2MOUSE;
2956 goto button_set;
2957 case MOUSE_4:
2958 button_char = KE_MOUSEDOWN;
2959 goto button_set;
2960 case MOUSE_5:
2961 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002962 goto button_set;
2963 case MOUSE_6:
2964 button_char = KE_MOUSELEFT;
2965 goto button_set;
2966 case MOUSE_7:
2967 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968button_set:
2969 {
2970 /* Don't put events in the input queue now. */
2971 if (hold_gui_events)
2972 return;
2973
2974 string[3] = CSI;
2975 string[4] = KS_EXTRA;
2976 string[5] = (int)button_char;
2977
2978 /* Pass the pointer coordinates of the scroll event so that we
2979 * know which window to scroll. */
2980 row = gui_xy2colrow(x, y, &col);
2981 string[6] = (char_u)(col / 128 + ' ' + 1);
2982 string[7] = (char_u)(col % 128 + ' ' + 1);
2983 string[8] = (char_u)(row / 128 + ' ' + 1);
2984 string[9] = (char_u)(row % 128 + ' ' + 1);
2985
2986 if (modifiers == 0)
2987 add_to_input_buf(string + 3, 7);
2988 else
2989 {
2990 string[0] = CSI;
2991 string[1] = KS_MODIFIER;
2992 string[2] = 0;
2993 if (modifiers & MOUSE_SHIFT)
2994 string[2] |= MOD_MASK_SHIFT;
2995 if (modifiers & MOUSE_CTRL)
2996 string[2] |= MOD_MASK_CTRL;
2997 if (modifiers & MOUSE_ALT)
2998 string[2] |= MOD_MASK_ALT;
2999 add_to_input_buf(string, 10);
3000 }
3001 return;
3002 }
3003 }
3004
3005#ifdef FEAT_CLIPBOARD
3006 /* If a clipboard selection is in progress, handle it */
3007 if (clip_star.state == SELECT_IN_PROGRESS)
3008 {
3009 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3010 return;
3011 }
3012
3013 /* Determine which mouse settings to look for based on the current mode */
3014 switch (get_real_state())
3015 {
3016 case NORMAL_BUSY:
3017 case OP_PENDING:
3018 case NORMAL: checkfor = MOUSE_NORMAL; break;
3019 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003020 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 case REPLACE:
3022 case REPLACE+LANGMAP:
3023#ifdef FEAT_VREPLACE
3024 case VREPLACE:
3025 case VREPLACE+LANGMAP:
3026#endif
3027 case INSERT:
3028 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3029 case ASKMORE:
3030 case HITRETURN: /* At the more- and hit-enter prompt pass the
3031 mouse event for a click on or below the
3032 message line. */
3033 if (Y_2_ROW(y) >= msg_row)
3034 checkfor = MOUSE_NORMAL;
3035 else
3036 checkfor = MOUSE_RETURN;
3037 break;
3038
3039 /*
3040 * On the command line, use the clipboard selection on all lines
3041 * but the command line. But not when pasting.
3042 */
3043 case CMDLINE:
3044 case CMDLINE+LANGMAP:
3045 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3046 checkfor = MOUSE_NONE;
3047 else
3048 checkfor = MOUSE_COMMAND;
3049 break;
3050
3051 default:
3052 checkfor = MOUSE_NONE;
3053 break;
3054 };
3055
3056 /*
3057 * Allow clipboard selection of text on the command line in "normal"
3058 * modes. Don't do this when dragging the status line, or extending a
3059 * Visual selection.
3060 */
3061 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
3062 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00003063# ifdef FEAT_WINDOWS
3064 + firstwin->w_winrow
3065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 && button != MOUSE_DRAG
3067# ifdef FEAT_MOUSESHAPE
3068 && !drag_status_line
3069# ifdef FEAT_VERTSPLIT
3070 && !drag_sep_line
3071# endif
3072# endif
3073 )
3074 checkfor = MOUSE_NONE;
3075
3076 /*
3077 * Use modeless selection when holding CTRL and SHIFT pressed.
3078 */
3079 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3080 checkfor = MOUSE_NONEF;
3081
3082 /*
3083 * In Ex mode, always use modeless selection.
3084 */
3085 if (exmode_active)
3086 checkfor = MOUSE_NONE;
3087
3088 /*
3089 * If the mouse settings say to not use the mouse, use the modeless
3090 * selection. But if Visual is active, assume that only the Visual area
3091 * will be selected.
3092 * Exception: On the command line, both the selection is used and a mouse
3093 * key is send.
3094 */
3095 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3096 {
3097#ifdef FEAT_VISUAL
3098 /* Don't do modeless selection in Visual mode. */
3099 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3100 return;
3101#endif
3102
3103 /*
3104 * When 'mousemodel' is "popup", shift-left is translated to right.
3105 * But not when also using Ctrl.
3106 */
3107 if (mouse_model_popup() && button == MOUSE_LEFT
3108 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3109 {
3110 button = MOUSE_RIGHT;
3111 modifiers &= ~ MOUSE_SHIFT;
3112 }
3113
3114 /* If the selection is done, allow the right button to extend it.
3115 * If the selection is cleared, allow the right button to start it
3116 * from the cursor position. */
3117 if (button == MOUSE_RIGHT)
3118 {
3119 if (clip_star.state == SELECT_CLEARED)
3120 {
3121 if (State & CMDLINE)
3122 {
3123 col = msg_col;
3124 row = msg_row;
3125 }
3126 else
3127 {
3128 col = curwin->w_wcol;
3129 row = curwin->w_wrow + W_WINROW(curwin);
3130 }
3131 clip_start_selection(col, row, FALSE);
3132 }
3133 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3134 repeated_click);
3135 did_clip = TRUE;
3136 }
3137 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003138 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3141 did_clip = TRUE;
3142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 /* Always allow pasting */
3145 if (button != MOUSE_MIDDLE)
3146 {
3147 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3148 return;
3149 if (checkfor != MOUSE_COMMAND)
3150 button = MOUSE_LEFT;
3151 }
3152 repeated_click = FALSE;
3153 }
3154
3155 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003156 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158
3159 /* Don't put events in the input queue now. */
3160 if (hold_gui_events)
3161 return;
3162
3163 row = gui_xy2colrow(x, y, &col);
3164
3165 /*
3166 * If we are dragging and the mouse hasn't moved far enough to be on a
3167 * different character, then don't send an event to vim.
3168 */
3169 if (button == MOUSE_DRAG)
3170 {
3171 if (row == prev_row && col == prev_col)
3172 return;
3173 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3174 if (y < 0)
3175 row = -1;
3176 }
3177
3178 /*
3179 * If topline has changed (window scrolled) since the last click, reset
3180 * repeated_click, because we don't want starting Visual mode when
3181 * clicking on a different character in the text.
3182 */
3183 if (curwin->w_topline != gui_prev_topline
3184#ifdef FEAT_DIFF
3185 || curwin->w_topfill != gui_prev_topfill
3186#endif
3187 )
3188 repeated_click = FALSE;
3189
3190 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3191 string[1] = KS_MOUSE;
3192 string[2] = KE_FILLER;
3193 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3194 {
3195 if (repeated_click)
3196 {
3197 /*
3198 * Handle multiple clicks. They only count if the mouse is still
3199 * pointing at the same character.
3200 */
3201 if (button != prev_button || row != prev_row || col != prev_col)
3202 num_clicks = 1;
3203 else if (++num_clicks > 4)
3204 num_clicks = 1;
3205 }
3206 else
3207 num_clicks = 1;
3208 prev_button = button;
3209 gui_prev_topline = curwin->w_topline;
3210#ifdef FEAT_DIFF
3211 gui_prev_topfill = curwin->w_topfill;
3212#endif
3213
3214 string[3] = (char_u)(button | 0x20);
3215 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3216 }
3217 else
3218 string[3] = (char_u)button;
3219
3220 string[3] |= modifiers;
3221 fill_mouse_coord(string + 4, col, row);
3222 add_to_input_buf(string, 8);
3223
3224 if (row < 0)
3225 prev_row = 0;
3226 else
3227 prev_row = row;
3228 prev_col = col;
3229
3230 /*
3231 * We need to make sure this is cleared since Athena doesn't tell us when
3232 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3233 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003234#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 gui.dragged_sb = SBAR_NONE;
3236#endif
3237}
3238
3239/*
3240 * Convert x and y coordinate to column and row in text window.
3241 * Corrects for multi-byte character.
3242 * returns column in "*colp" and row as return value;
3243 */
3244 int
3245gui_xy2colrow(x, y, colp)
3246 int x;
3247 int y;
3248 int *colp;
3249{
3250 int col = check_col(X_2_COL(x));
3251 int row = check_row(Y_2_ROW(y));
3252
3253#ifdef FEAT_MBYTE
3254 *colp = mb_fix_col(col, row);
3255#else
3256 *colp = col;
3257#endif
3258 return row;
3259}
3260
3261#if defined(FEAT_MENU) || defined(PROTO)
3262/*
3263 * Callback function for when a menu entry has been selected.
3264 */
3265 void
3266gui_menu_cb(menu)
3267 vimmenu_T *menu;
3268{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003269 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 /* Don't put events in the input queue now. */
3272 if (hold_gui_events)
3273 return;
3274
3275 bytes[0] = CSI;
3276 bytes[1] = KS_MENU;
3277 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003278 add_to_input_buf(bytes, 3);
3279 add_long_to_buf((long_u)menu, bytes);
3280 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282#endif
3283
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003284static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286/*
3287 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003288 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * in p_go.
3290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 void
3292gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003293 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#ifdef FEAT_MENU
3296 static int prev_menu_is_active = -1;
3297#endif
3298#ifdef FEAT_TOOLBAR
3299 static int prev_toolbar = -1;
3300 int using_toolbar = FALSE;
3301#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003302#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003303 int using_tabline;
3304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#ifdef FEAT_FOOTER
3306 static int prev_footer = -1;
3307 int using_footer = FALSE;
3308#endif
3309#if defined(FEAT_MENU) && !defined(WIN16)
3310 static int prev_tearoff = -1;
3311 int using_tearoff = FALSE;
3312#endif
3313
3314 char_u *p;
3315 int i;
3316#ifdef FEAT_MENU
3317 int grey_old, grey_new;
3318 char_u *temp;
3319#endif
3320 win_T *wp;
3321 int need_set_size;
3322 int fix_size;
3323
3324#ifdef FEAT_MENU
3325 if (oldval != NULL && gui.in_use)
3326 {
3327 /*
3328 * Check if the menu's go from grey to non-grey or vise versa.
3329 */
3330 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3331 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3332 if (grey_old != grey_new)
3333 {
3334 temp = p_go;
3335 p_go = oldval;
3336 gui_update_menus(MENU_ALL_MODES);
3337 p_go = temp;
3338 }
3339 }
3340 gui.menu_is_active = FALSE;
3341#endif
3342
3343 for (i = 0; i < 3; i++)
3344 gui.which_scrollbars[i] = FALSE;
3345 for (p = p_go; *p; p++)
3346 switch (*p)
3347 {
3348 case GO_LEFT:
3349 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3350 break;
3351 case GO_RIGHT:
3352 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3353 break;
3354#ifdef FEAT_VERTSPLIT
3355 case GO_VLEFT:
3356 if (win_hasvertsplit())
3357 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3358 break;
3359 case GO_VRIGHT:
3360 if (win_hasvertsplit())
3361 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3362 break;
3363#endif
3364 case GO_BOT:
3365 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3366 break;
3367#ifdef FEAT_MENU
3368 case GO_MENUS:
3369 gui.menu_is_active = TRUE;
3370 break;
3371#endif
3372 case GO_GREY:
3373 /* make menu's have grey items, ignored here */
3374 break;
3375#ifdef FEAT_TOOLBAR
3376 case GO_TOOLBAR:
3377 using_toolbar = TRUE;
3378 break;
3379#endif
3380#ifdef FEAT_FOOTER
3381 case GO_FOOTER:
3382 using_footer = TRUE;
3383 break;
3384#endif
3385 case GO_TEAROFF:
3386#if defined(FEAT_MENU) && !defined(WIN16)
3387 using_tearoff = TRUE;
3388#endif
3389 break;
3390 default:
3391 /* Ignore options that are not supported */
3392 break;
3393 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 if (gui.in_use)
3396 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003397 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003399
3400#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003401 /* Update the GUI tab line, it may appear or disappear. This may
3402 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003403 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003404 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003405 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003406 /* We don't want a resize event change "Rows" here, save and
3407 * restore it. Resizing is handled below. */
3408 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003409 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003410 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003411 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003412 if (using_tabline)
3413 fix_size = TRUE;
3414 if (!gui_use_tabline())
3415 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3416 }
3417#endif
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 for (i = 0; i < 3; i++)
3420 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003421 /* The scrollbar needs to be updated when it is shown/unshown and
3422 * when switching tab pages. But the size only changes when it's
3423 * shown/unshown. Thus we need two places to remember whether a
3424 * scrollbar is there or not. */
3425 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003426#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003427 || gui.which_scrollbars[i]
3428 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003429#endif
3430 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
3432 if (i == SBAR_BOTTOM)
3433 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3434 gui.which_scrollbars[i]);
3435 else
3436 {
3437 FOR_ALL_WINDOWS(wp)
3438 {
3439 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3440 }
3441 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003442 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3443 {
3444 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003445 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003446 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003447 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003448 if (gui.which_scrollbars[i])
3449 fix_size = TRUE;
3450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003452#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003453 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003454#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003455 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457
3458#ifdef FEAT_MENU
3459 if (gui.menu_is_active != prev_menu_is_active)
3460 {
3461 /* We don't want a resize event change "Rows" here, save and
3462 * restore it. Resizing is handled below. */
3463 i = Rows;
3464 gui_mch_enable_menu(gui.menu_is_active);
3465 Rows = i;
3466 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003467 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (gui.menu_is_active)
3469 fix_size = TRUE;
3470 }
3471#endif
3472
3473#ifdef FEAT_TOOLBAR
3474 if (using_toolbar != prev_toolbar)
3475 {
3476 gui_mch_show_toolbar(using_toolbar);
3477 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003478 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (using_toolbar)
3480 fix_size = TRUE;
3481 }
3482#endif
3483#ifdef FEAT_FOOTER
3484 if (using_footer != prev_footer)
3485 {
3486 gui_mch_enable_footer(using_footer);
3487 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003488 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (using_footer)
3490 fix_size = TRUE;
3491 }
3492#endif
3493#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3494 if (using_tearoff != prev_tearoff)
3495 {
3496 gui_mch_toggle_tearoffs(using_tearoff);
3497 prev_tearoff = using_tearoff;
3498 }
3499#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003500 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003501 {
3502#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003503 long prev_Columns = Columns;
3504 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* Adjust the size of the window to make the text area keep the
3507 * same size and to avoid that part of our window is off-screen
3508 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003509 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003510
3511#ifdef FEAT_GUI_GTK
3512 /* GTK has the annoying habit of sending us resize events when
3513 * changing the window size ourselves. This mostly happens when
3514 * waiting for a character to arrive, quite unpredictably, and may
3515 * change Columns and Rows when we don't want it. Wait for a
3516 * character here to avoid this effect.
3517 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003518 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003519 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003520 * Don't change Rows when adding menu/toolbar/tabline.
3521 * Don't change Columns when adding vertical toolbar. */
3522 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003523 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003524 if ((need_set_size & RESIZE_VERT) == 0)
3525 Rows = prev_Rows;
3526 if ((need_set_size & RESIZE_HOR) == 0)
3527 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003528#endif
3529 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003530#ifdef FEAT_WINDOWS
3531 /* When the console tabline appears or disappears the window positions
3532 * change. */
3533 if (firstwin->w_winrow != tabline_height())
3534 shell_new_rows(); /* recompute window positions and heights */
3535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537}
3538
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003539#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3540/*
3541 * Return TRUE if the GUI is taking care of the tabline.
3542 * It may still be hidden if 'showtabline' is zero.
3543 */
3544 int
3545gui_use_tabline()
3546{
3547 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3548}
3549
3550/*
3551 * Return TRUE if the GUI is showing the tabline.
3552 * This uses 'showtabline'.
3553 */
3554 static int
3555gui_has_tabline()
3556{
3557 if (!gui_use_tabline()
3558 || p_stal == 0
3559 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3560 return FALSE;
3561 return TRUE;
3562}
3563
3564/*
3565 * Update the tabline.
3566 * This may display/undisplay the tabline and update the labels.
3567 */
3568 void
3569gui_update_tabline()
3570{
3571 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003572 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003573
3574 if (!gui.starting && starting == 0)
3575 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003576 /* Updating the tabline uses direct GUI commands, flush
3577 * outstanding instructions first. (esp. clear screen) */
3578 out_flush();
3579 gui_mch_flush();
3580
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003581 if (!showit != !shown)
3582 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583 if (showit != 0)
3584 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003585
3586 /* When the tabs change from hidden to shown or from shown to
3587 * hidden the size of the text area should remain the same. */
3588 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003589 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003590 }
3591}
3592
3593/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003594 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595 */
3596 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003597get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003598 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003599 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003600{
3601 int modified = FALSE;
3602 char_u buf[40];
3603 int wincount;
3604 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003605 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003606
Bram Moolenaar57657d82006-04-21 22:12:41 +00003607 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003608 opt = (tooltip ? &p_gtt : &p_gtl);
3609 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003610 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003611 int use_sandbox = FALSE;
3612 int save_called_emsg = called_emsg;
3613 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003614 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003615 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3616 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003617
3618 called_emsg = FALSE;
3619
3620 printer_page_num = tabpage_index(tp);
3621# ifdef FEAT_EVAL
3622 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003623 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003624# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003625 /* It's almost as going to the tabpage, but without autocommands. */
3626 curtab->tp_firstwin = firstwin;
3627 curtab->tp_lastwin = lastwin;
3628 curtab->tp_curwin = curwin;
3629 save_curtab = curtab;
3630 curtab = tp;
3631 topframe = curtab->tp_topframe;
3632 firstwin = curtab->tp_firstwin;
3633 lastwin = curtab->tp_lastwin;
3634 curwin = curtab->tp_curwin;
3635 curbuf = curwin->w_buffer;
3636
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003637 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003638 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003639 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003640 STRCPY(NameBuff, res);
3641
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003642 /* Back to the original curtab. */
3643 curtab = save_curtab;
3644 topframe = curtab->tp_topframe;
3645 firstwin = curtab->tp_firstwin;
3646 lastwin = curtab->tp_lastwin;
3647 curwin = curtab->tp_curwin;
3648 curbuf = curwin->w_buffer;
3649
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003650 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003651 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003652 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003653 called_emsg |= save_called_emsg;
3654 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003655
3656 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3657 * use a default label. */
3658 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003659 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003660 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003661 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003662 if (!tooltip)
3663 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003664
3665 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3666 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3667 if (bufIsChanged(wp->w_buffer))
3668 modified = TRUE;
3669 if (modified || wincount > 1)
3670 {
3671 if (wincount > 1)
3672 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3673 else
3674 buf[0] = NUL;
3675 if (modified)
3676 STRCAT(buf, "+");
3677 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003678 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003679 mch_memmove(NameBuff, buf, STRLEN(buf));
3680 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003681 }
3682}
3683
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003684/*
3685 * Send the event for clicking to select tab page "nr".
3686 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003687 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003688 */
3689 int
3690send_tabline_event(nr)
3691 int nr;
3692{
3693 char_u string[3];
3694
3695 if (nr == tabpage_index(curtab))
3696 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003697
3698 /* Don't put events in the input queue now. */
3699 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003700# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003701 || cmdwin_type != 0
3702# endif
3703 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003704 {
3705 /* Set it back to the current tab page. */
3706 gui_mch_set_curtab(tabpage_index(curtab));
3707 return FALSE;
3708 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003709
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003710 string[0] = CSI;
3711 string[1] = KS_TABLINE;
3712 string[2] = KE_FILLER;
3713 add_to_input_buf(string, 3);
3714 string[0] = nr;
3715 add_to_input_buf_csi(string, 1);
3716 return TRUE;
3717}
3718
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003719/*
3720 * Send a tabline menu event
3721 */
3722 void
3723send_tabline_menu_event(tabidx, event)
3724 int tabidx;
3725 int event;
3726{
3727 char_u string[3];
3728
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003729 /* Don't put events in the input queue now. */
3730 if (hold_gui_events)
3731 return;
3732
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003733 string[0] = CSI;
3734 string[1] = KS_TABMENU;
3735 string[2] = KE_FILLER;
3736 add_to_input_buf(string, 3);
3737 string[0] = tabidx;
3738 string[1] = (char_u)(long)event;
3739 add_to_input_buf_csi(string, 2);
3740}
3741
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744/*
3745 * Scrollbar stuff:
3746 */
3747
Bram Moolenaare45828b2006-02-15 22:12:56 +00003748#if defined(FEAT_WINDOWS) || defined(PROTO)
3749/*
3750 * Remove all scrollbars. Used before switching to another tab page.
3751 */
3752 void
3753gui_remove_scrollbars()
3754{
3755 int i;
3756 win_T *wp;
3757
3758 for (i = 0; i < 3; i++)
3759 {
3760 if (i == SBAR_BOTTOM)
3761 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3762 else
3763 {
3764 FOR_ALL_WINDOWS(wp)
3765 {
3766 gui_do_scrollbar(wp, i, FALSE);
3767 }
3768 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003769 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003770 }
3771}
3772#endif
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 void
3775gui_create_scrollbar(sb, type, wp)
3776 scrollbar_T *sb;
3777 int type;
3778 win_T *wp;
3779{
3780 static int sbar_ident = 0;
3781
3782 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3783 sb->wp = wp;
3784 sb->type = type;
3785 sb->value = 0;
3786#ifdef FEAT_GUI_ATHENA
3787 sb->pixval = 0;
3788#endif
3789 sb->size = 1;
3790 sb->max = 1;
3791 sb->top = 0;
3792 sb->height = 0;
3793#ifdef FEAT_VERTSPLIT
3794 sb->width = 0;
3795#endif
3796 sb->status_height = 0;
3797 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3798}
3799
3800/*
3801 * Find the scrollbar with the given index.
3802 */
3803 scrollbar_T *
3804gui_find_scrollbar(ident)
3805 long ident;
3806{
3807 win_T *wp;
3808
3809 if (gui.bottom_sbar.ident == ident)
3810 return &gui.bottom_sbar;
3811 FOR_ALL_WINDOWS(wp)
3812 {
3813 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3814 return &wp->w_scrollbars[SBAR_LEFT];
3815 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3816 return &wp->w_scrollbars[SBAR_RIGHT];
3817 }
3818 return NULL;
3819}
3820
3821/*
3822 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3823 *
3824 * For Win32, Macintosh and GTK+ 2:
3825 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3826 * you stop dragging the scrollbar. We get here each time the scrollbar is
3827 * dragged another pixel, but as far as the rest of vim goes, it thinks
3828 * we're just hanging in the call to DispatchMessage() in
3829 * process_message(). The DispatchMessage() call that hangs was passed a
3830 * mouse button click event in the scrollbar window. -- webb.
3831 *
3832 * Solution: Do the scrolling right here. But only when allowed.
3833 * Ignore the scrollbars while executing an external command or when there
3834 * are still characters to be processed.
3835 */
3836 void
3837gui_drag_scrollbar(sb, value, still_dragging)
3838 scrollbar_T *sb;
3839 long value;
3840 int still_dragging;
3841{
3842#ifdef FEAT_WINDOWS
3843 win_T *wp;
3844#endif
3845 int sb_num;
3846#ifdef USE_ON_FLY_SCROLL
3847 colnr_T old_leftcol = curwin->w_leftcol;
3848# ifdef FEAT_SCROLLBIND
3849 linenr_T old_topline = curwin->w_topline;
3850# endif
3851# ifdef FEAT_DIFF
3852 int old_topfill = curwin->w_topfill;
3853# endif
3854#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003855 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 int byte_count;
3857#endif
3858
3859 if (sb == NULL)
3860 return;
3861
3862 /* Don't put events in the input queue now. */
3863 if (hold_gui_events)
3864 return;
3865
3866#ifdef FEAT_CMDWIN
3867 if (cmdwin_type != 0 && sb->wp != curwin)
3868 return;
3869#endif
3870
3871 if (still_dragging)
3872 {
3873 if (sb->wp == NULL)
3874 gui.dragged_sb = SBAR_BOTTOM;
3875 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3876 gui.dragged_sb = SBAR_LEFT;
3877 else
3878 gui.dragged_sb = SBAR_RIGHT;
3879 gui.dragged_wp = sb->wp;
3880 }
3881 else
3882 {
3883 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003884#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 /* Keep the "dragged_wp" value until after the scrolling, for when the
3886 * moust button is released. GTK2 doesn't send the button-up event. */
3887 gui.dragged_wp = NULL;
3888#endif
3889 }
3890
3891 /* Vertical sbar info is kept in the first sbar (the left one) */
3892 if (sb->wp != NULL)
3893 sb = &sb->wp->w_scrollbars[0];
3894
3895 /*
3896 * Check validity of value
3897 */
3898 if (value < 0)
3899 value = 0;
3900#ifdef SCROLL_PAST_END
3901 else if (value > sb->max)
3902 value = sb->max;
3903#else
3904 if (value > sb->max - sb->size + 1)
3905 value = sb->max - sb->size + 1;
3906#endif
3907
3908 sb->value = value;
3909
3910#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003911 /* When not allowed to do the scrolling right now, return.
3912 * This also checked input_available(), but that causes the first click in
3913 * a scrollbar to be ignored when Vim doesn't have focus. */
3914 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return;
3916#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003917#ifdef FEAT_INS_EXPAND
3918 /* Disallow scrolling the current window when the completion popup menu is
3919 * visible. */
3920 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3921 return;
3922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924#ifdef FEAT_RIGHTLEFT
3925 if (sb->wp == NULL && curwin->w_p_rl)
3926 {
3927 value = sb->max + 1 - sb->size - value;
3928 if (value < 0)
3929 value = 0;
3930 }
3931#endif
3932
3933 if (sb->wp != NULL) /* vertical scrollbar */
3934 {
3935 sb_num = 0;
3936#ifdef FEAT_WINDOWS
3937 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3938 sb_num++;
3939 if (wp == NULL)
3940 return;
3941#else
3942 if (sb->wp != curwin)
3943 return;
3944#endif
3945
3946#ifdef USE_ON_FLY_SCROLL
3947 current_scrollbar = sb_num;
3948 scrollbar_value = value;
3949 if (State & NORMAL)
3950 {
3951 gui_do_scroll();
3952 setcursor();
3953 }
3954 else if (State & INSERT)
3955 {
3956 ins_scroll();
3957 setcursor();
3958 }
3959 else if (State & CMDLINE)
3960 {
3961 if (msg_scrolled == 0)
3962 {
3963 gui_do_scroll();
3964 redrawcmdline();
3965 }
3966 }
3967# ifdef FEAT_FOLDING
3968 /* Value may have been changed for closed fold. */
3969 sb->value = sb->wp->w_topline - 1;
3970# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003971
3972 /* When dragging one scrollbar and there is another one at the other
3973 * side move the thumb of that one too. */
3974 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3975 gui_mch_set_scrollbar_thumb(
3976 &sb->wp->w_scrollbars[
3977 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3978 ? SBAR_LEFT : SBAR_RIGHT],
3979 sb->value, sb->size, sb->max);
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#else
3982 bytes[0] = CSI;
3983 bytes[1] = KS_VER_SCROLLBAR;
3984 bytes[2] = KE_FILLER;
3985 bytes[3] = (char_u)sb_num;
3986 byte_count = 4;
3987#endif
3988 }
3989 else
3990 {
3991#ifdef USE_ON_FLY_SCROLL
3992 scrollbar_value = value;
3993
3994 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003995 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 else if (State & INSERT)
3997 ins_horscroll();
3998 else if (State & CMDLINE)
3999 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004000 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004002 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 redrawcmdline();
4004 }
4005 }
4006 if (old_leftcol != curwin->w_leftcol)
4007 {
4008 updateWindow(curwin); /* update window, status and cmdline */
4009 setcursor();
4010 }
4011#else
4012 bytes[0] = CSI;
4013 bytes[1] = KS_HOR_SCROLLBAR;
4014 bytes[2] = KE_FILLER;
4015 byte_count = 3;
4016#endif
4017 }
4018
4019#ifdef USE_ON_FLY_SCROLL
4020# ifdef FEAT_SCROLLBIND
4021 /*
4022 * synchronize other windows, as necessary according to 'scrollbind'
4023 */
4024 if (curwin->w_p_scb
4025 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4026 || (sb->wp == curwin && (curwin->w_topline != old_topline
4027# ifdef FEAT_DIFF
4028 || curwin->w_topfill != old_topfill
4029# endif
4030 ))))
4031 {
4032 do_check_scrollbind(TRUE);
4033 /* need to update the window right here */
4034 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4035 if (wp->w_redr_type > 0)
4036 updateWindow(wp);
4037 setcursor();
4038 }
4039# endif
4040 out_flush();
4041 gui_update_cursor(FALSE, TRUE);
4042#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004043 add_to_input_buf(bytes, byte_count);
4044 add_long_to_buf((long_u)value, bytes);
4045 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#endif
4047}
4048
4049/*
4050 * Scrollbar stuff:
4051 */
4052
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004053#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004054/*
4055 * Called when something in the window layout has changed.
4056 */
4057 void
4058gui_may_update_scrollbars()
4059{
4060 if (gui.in_use && starting == 0)
4061 {
4062 out_flush();
4063 gui_init_which_components(NULL);
4064 gui_update_scrollbars(TRUE);
4065 }
4066 need_mouse_correct = TRUE;
4067}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004068#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 void
4071gui_update_scrollbars(force)
4072 int force; /* Force all scrollbars to get updated */
4073{
4074 win_T *wp;
4075 scrollbar_T *sb;
4076 long val, size, max; /* need 32 bits here */
4077 int which_sb;
4078 int h, y;
4079#ifdef FEAT_VERTSPLIT
4080 static win_T *prev_curwin = NULL;
4081#endif
4082
4083 /* Update the horizontal scrollbar */
4084 gui_update_horiz_scrollbar(force);
4085
4086#ifndef WIN3264
4087 /* Return straight away if there is neither a left nor right scrollbar.
4088 * On MS-Windows this is required anyway for scrollwheel messages. */
4089 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4090 return;
4091#endif
4092
4093 /*
4094 * Don't want to update a scrollbar while we're dragging it. But if we
4095 * have both a left and right scrollbar, and we drag one of them, we still
4096 * need to update the other one.
4097 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004098 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4099 && gui.which_scrollbars[SBAR_LEFT]
4100 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 /*
4103 * If we have two scrollbars and one of them is being dragged, just
4104 * copy the scrollbar position from the dragged one to the other one.
4105 */
4106 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4107 if (gui.dragged_wp != NULL)
4108 gui_mch_set_scrollbar_thumb(
4109 &gui.dragged_wp->w_scrollbars[which_sb],
4110 gui.dragged_wp->w_scrollbars[0].value,
4111 gui.dragged_wp->w_scrollbars[0].size,
4112 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 /* avoid that moving components around generates events */
4116 ++hold_gui_events;
4117
4118 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4119 {
4120 if (wp->w_buffer == NULL) /* just in case */
4121 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004122 /* Skip a scrollbar that is being dragged. */
4123 if (!force && (gui.dragged_sb == SBAR_LEFT
4124 || gui.dragged_sb == SBAR_RIGHT)
4125 && gui.dragged_wp == wp)
4126 continue;
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#ifdef SCROLL_PAST_END
4129 max = wp->w_buffer->b_ml.ml_line_count - 1;
4130#else
4131 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4132#endif
4133 if (max < 0) /* empty buffer */
4134 max = 0;
4135 val = wp->w_topline - 1;
4136 size = wp->w_height;
4137#ifdef SCROLL_PAST_END
4138 if (val > max) /* just in case */
4139 val = max;
4140#else
4141 if (size > max + 1) /* just in case */
4142 size = max + 1;
4143 if (val > max - size + 1)
4144 val = max - size + 1;
4145#endif
4146 if (val < 0) /* minimal value is 0 */
4147 val = 0;
4148
4149 /*
4150 * Scrollbar at index 0 (the left one) contains all the information.
4151 * It would be the same info for left and right so we just store it for
4152 * one of them.
4153 */
4154 sb = &wp->w_scrollbars[0];
4155
4156 /*
4157 * Note: no check for valid w_botline. If it's not valid the
4158 * scrollbars will be updated later anyway.
4159 */
4160 if (size < 1 || wp->w_botline - 2 > max)
4161 {
4162 /*
4163 * This can happen during changing files. Just don't update the
4164 * scrollbar for now.
4165 */
4166 sb->height = 0; /* Force update next time */
4167 if (gui.which_scrollbars[SBAR_LEFT])
4168 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4169 if (gui.which_scrollbars[SBAR_RIGHT])
4170 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4171 continue;
4172 }
4173 if (force || sb->height != wp->w_height
4174#ifdef FEAT_WINDOWS
4175 || sb->top != wp->w_winrow
4176 || sb->status_height != wp->w_status_height
4177# ifdef FEAT_VERTSPLIT
4178 || sb->width != wp->w_width
4179 || prev_curwin != curwin
4180# endif
4181#endif
4182 )
4183 {
4184 /* Height, width or position of scrollbar has changed. For
4185 * vertical split: curwin changed. */
4186 sb->height = wp->w_height;
4187#ifdef FEAT_WINDOWS
4188 sb->top = wp->w_winrow;
4189 sb->status_height = wp->w_status_height;
4190# ifdef FEAT_VERTSPLIT
4191 sb->width = wp->w_width;
4192# endif
4193#endif
4194
4195 /* Calculate height and position in pixels */
4196 h = (sb->height + sb->status_height) * gui.char_height;
4197 y = sb->top * gui.char_height + gui.border_offset;
4198#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4199 if (gui.menu_is_active)
4200 y += gui.menu_height;
4201#endif
4202
4203#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4204 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4205# ifdef FEAT_GUI_ATHENA
4206 y += gui.toolbar_height;
4207# else
4208# ifdef FEAT_GUI_MSWIN
4209 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4210# endif
4211# endif
4212#endif
4213
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004214#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4215 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004216 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004217#endif
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#ifdef FEAT_WINDOWS
4220 if (wp->w_winrow == 0)
4221#endif
4222 {
4223 /* Height of top scrollbar includes width of top border */
4224 h += gui.border_offset;
4225 y -= gui.border_offset;
4226 }
4227 if (gui.which_scrollbars[SBAR_LEFT])
4228 {
4229 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4230 gui.left_sbar_x, y,
4231 gui.scrollbar_width, h);
4232 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4233 }
4234 if (gui.which_scrollbars[SBAR_RIGHT])
4235 {
4236 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4237 gui.right_sbar_x, y,
4238 gui.scrollbar_width, h);
4239 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4240 }
4241 }
4242
4243 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4244 * checking if the thumb moved at least a pixel. Only do this for
4245 * Athena, most other GUIs require the update anyway to make the
4246 * arrows work. */
4247#ifdef FEAT_GUI_ATHENA
4248 if (max == 0)
4249 y = 0;
4250 else
4251 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4252 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4253#else
4254 if (force || sb->value != val || sb->size != size || sb->max != max)
4255#endif
4256 {
4257 /* Thumb of scrollbar has moved */
4258 sb->value = val;
4259#ifdef FEAT_GUI_ATHENA
4260 sb->pixval = y;
4261#endif
4262 sb->size = size;
4263 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004264 if (gui.which_scrollbars[SBAR_LEFT]
4265 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4267 val, size, max);
4268 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004269 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4271 val, size, max);
4272 }
4273 }
4274#ifdef FEAT_VERTSPLIT
4275 prev_curwin = curwin;
4276#endif
4277 --hold_gui_events;
4278}
4279
4280/*
4281 * Enable or disable a scrollbar.
4282 * Check for scrollbars for vertically split windows which are not enabled
4283 * sometimes.
4284 */
4285 static void
4286gui_do_scrollbar(wp, which, enable)
4287 win_T *wp;
4288 int which; /* SBAR_LEFT or SBAR_RIGHT */
4289 int enable; /* TRUE to enable scrollbar */
4290{
4291#ifdef FEAT_VERTSPLIT
4292 int midcol = curwin->w_wincol + curwin->w_width / 2;
4293 int has_midcol = (wp->w_wincol <= midcol
4294 && wp->w_wincol + wp->w_width >= midcol);
4295
4296 /* Only enable scrollbars that contain the middle column of the current
4297 * window. */
4298 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4299 {
4300 /* Scrollbars only on one side. Don't enable scrollbars that don't
4301 * contain the middle column of the current window. */
4302 if (!has_midcol)
4303 enable = FALSE;
4304 }
4305 else
4306 {
4307 /* Scrollbars on both sides. Don't enable scrollbars that neither
4308 * contain the middle column of the current window nor are on the far
4309 * side. */
4310 if (midcol > Columns / 2)
4311 {
4312 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4313 enable = FALSE;
4314 }
4315 else
4316 {
4317 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4318 : !has_midcol)
4319 enable = FALSE;
4320 }
4321 }
4322#endif
4323 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4324}
4325
4326/*
4327 * Scroll a window according to the values set in the globals current_scrollbar
4328 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4329 * or FALSE otherwise.
4330 */
4331 int
4332gui_do_scroll()
4333{
4334 win_T *wp, *save_wp;
4335 int i;
4336 long nlines;
4337 pos_T old_cursor;
4338 linenr_T old_topline;
4339#ifdef FEAT_DIFF
4340 int old_topfill;
4341#endif
4342
4343 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4344 if (wp == NULL)
4345 break;
4346 if (wp == NULL)
4347 /* Couldn't find window */
4348 return FALSE;
4349
4350 /*
4351 * Compute number of lines to scroll. If zero, nothing to do.
4352 */
4353 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4354 if (nlines == 0)
4355 return FALSE;
4356
4357 save_wp = curwin;
4358 old_topline = wp->w_topline;
4359#ifdef FEAT_DIFF
4360 old_topfill = wp->w_topfill;
4361#endif
4362 old_cursor = wp->w_cursor;
4363 curwin = wp;
4364 curbuf = wp->w_buffer;
4365 if (nlines < 0)
4366 scrolldown(-nlines, gui.dragged_wp == NULL);
4367 else
4368 scrollup(nlines, gui.dragged_wp == NULL);
4369 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4370 * the mouse-up event already, but we still want it to behave like when
4371 * dragging. But not the next click in an arrow. */
4372 if (gui.dragged_sb == SBAR_NONE)
4373 gui.dragged_wp = NULL;
4374
4375 if (old_topline != wp->w_topline
4376#ifdef FEAT_DIFF
4377 || old_topfill != wp->w_topfill
4378#endif
4379 )
4380 {
4381 if (p_so != 0)
4382 {
4383 cursor_correct(); /* fix window for 'so' */
4384 update_topline(); /* avoid up/down jump */
4385 }
4386 if (old_cursor.lnum != wp->w_cursor.lnum)
4387 coladvance(wp->w_curswant);
4388#ifdef FEAT_SCROLLBIND
4389 wp->w_scbind_pos = wp->w_topline;
4390#endif
4391 }
4392
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004393 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4394 validate_cursor();
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 curwin = save_wp;
4397 curbuf = save_wp->w_buffer;
4398
4399 /*
4400 * Don't call updateWindow() when nothing has changed (it will overwrite
4401 * the status line!).
4402 */
4403 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004404 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_DIFF
4406 || old_topfill != wp->w_topfill
4407#endif
4408 )
4409 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004410 int type = VALID;
4411
4412#ifdef FEAT_INS_EXPAND
4413 if (pum_visible())
4414 {
4415 type = NOT_VALID;
4416 wp->w_lines_valid = 0;
4417 }
4418#endif
4419 /* Don't set must_redraw here, it may cause the popup menu to
4420 * disappear when losing focus after a scrollbar drag. */
4421 if (wp->w_redr_type < type)
4422 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 updateWindow(wp); /* update window, status line, and cmdline */
4424 }
4425
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004426#ifdef FEAT_INS_EXPAND
4427 /* May need to redraw the popup menu. */
4428 if (pum_visible())
4429 pum_redraw();
4430#endif
4431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4433}
4434
4435
4436/*
4437 * Horizontal scrollbar stuff:
4438 */
4439
4440/*
4441 * Return length of line "lnum" for horizontal scrolling.
4442 */
4443 static colnr_T
4444scroll_line_len(lnum)
4445 linenr_T lnum;
4446{
4447 char_u *p;
4448 colnr_T col;
4449 int w;
4450
4451 p = ml_get(lnum);
4452 col = 0;
4453 if (*p != NUL)
4454 for (;;)
4455 {
4456 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004457 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if (*p == NUL) /* don't count the last character */
4459 break;
4460 col += w;
4461 }
4462 return col;
4463}
4464
4465/* Remember which line is currently the longest, so that we don't have to
4466 * search for it when scrolling horizontally. */
4467static linenr_T longest_lnum = 0;
4468
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004469/*
4470 * Find longest visible line number. If this is not possible (or not desired,
4471 * by setting 'h' in "guioptions") then the current line number is returned.
4472 */
4473 static linenr_T
4474gui_find_longest_lnum()
4475{
4476 linenr_T ret = 0;
4477
4478 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4479 * line numbers, topline and botline can be invalid when displaying is
4480 * postponed. */
4481 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4482 && curwin->w_topline <= curwin->w_cursor.lnum
4483 && curwin->w_botline > curwin->w_cursor.lnum
4484 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4485 {
4486 linenr_T lnum;
4487 colnr_T n;
4488 long max = 0;
4489
4490 /* Use maximum of all visible lines. Remember the lnum of the
4491 * longest line, closest to the cursor line. Used when scrolling
4492 * below. */
4493 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4494 {
4495 n = scroll_line_len(lnum);
4496 if (n > (colnr_T)max)
4497 {
4498 max = n;
4499 ret = lnum;
4500 }
4501 else if (n == (colnr_T)max
4502 && abs((int)(lnum - curwin->w_cursor.lnum))
4503 < abs((int)(ret - curwin->w_cursor.lnum)))
4504 ret = lnum;
4505 }
4506 }
4507 else
4508 /* Use cursor line only. */
4509 ret = curwin->w_cursor.lnum;
4510
4511 return ret;
4512}
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 static void
4515gui_update_horiz_scrollbar(force)
4516 int force;
4517{
4518 long value, size, max; /* need 32 bit ints here */
4519
4520 if (!gui.which_scrollbars[SBAR_BOTTOM])
4521 return;
4522
4523 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4524 return;
4525
4526 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4527 return;
4528
4529 /*
4530 * It is possible for the cursor to be invalid if we're in the middle of
4531 * something (like changing files). If so, don't do anything for now.
4532 */
4533 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4534 {
4535 gui.bottom_sbar.value = -1;
4536 return;
4537 }
4538
4539 size = W_WIDTH(curwin);
4540 if (curwin->w_p_wrap)
4541 {
4542 value = 0;
4543#ifdef SCROLL_PAST_END
4544 max = 0;
4545#else
4546 max = W_WIDTH(curwin) - 1;
4547#endif
4548 }
4549 else
4550 {
4551 value = curwin->w_leftcol;
4552
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004553 longest_lnum = gui_find_longest_lnum();
4554 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#ifdef FEAT_VIRTUALEDIT
4557 if (virtual_active())
4558 {
4559 /* May move the cursor even further to the right. */
4560 if (curwin->w_virtcol >= (colnr_T)max)
4561 max = curwin->w_virtcol;
4562 }
4563#endif
4564
4565#ifndef SCROLL_PAST_END
4566 max += W_WIDTH(curwin) - 1;
4567#endif
4568 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004569 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 size -= curwin_col_off();
4571#ifndef SCROLL_PAST_END
4572 max -= curwin_col_off();
4573#endif
4574 }
4575
4576#ifndef SCROLL_PAST_END
4577 if (value > max - size + 1)
4578 value = max - size + 1; /* limit the value to allowable range */
4579#endif
4580
4581#ifdef FEAT_RIGHTLEFT
4582 if (curwin->w_p_rl)
4583 {
4584 value = max + 1 - size - value;
4585 if (value < 0)
4586 {
4587 size += value;
4588 value = 0;
4589 }
4590 }
4591#endif
4592 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4593 && max == gui.bottom_sbar.max)
4594 return;
4595
4596 gui.bottom_sbar.value = value;
4597 gui.bottom_sbar.size = size;
4598 gui.bottom_sbar.max = max;
4599 gui.prev_wrap = curwin->w_p_wrap;
4600
4601 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4602}
4603
4604/*
4605 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4606 */
4607 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004608gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004609 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004610 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 /* no wrapping, no scrolling */
4613 if (curwin->w_p_wrap)
4614 return FALSE;
4615
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004616 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return FALSE;
4618
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004619 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004622 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004624 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004625 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004627 if (compute_longest_lnum)
4628 {
4629 curwin->w_cursor.lnum = gui_find_longest_lnum();
4630 curwin->w_cursor.col = 0;
4631 }
4632 /* Do a sanity check on "longest_lnum", just in case. */
4633 else if (longest_lnum >= curwin->w_topline
4634 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
4636 curwin->w_cursor.lnum = longest_lnum;
4637 curwin->w_cursor.col = 0;
4638 }
4639 }
4640
4641 return leftcol_changed();
4642}
4643
4644/*
4645 * Check that none of the colors are the same as the background color
4646 */
4647 void
4648gui_check_colors()
4649{
4650 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4651 {
4652 gui_set_bg_color((char_u *)"White");
4653 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4654 gui_set_fg_color((char_u *)"Black");
4655 }
4656}
4657
Bram Moolenaar3918c952005-03-15 22:34:55 +00004658 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659gui_set_fg_color(name)
4660 char_u *name;
4661{
4662 gui.norm_pixel = gui_get_color(name);
4663 hl_set_fg_color_name(vim_strsave(name));
4664}
4665
Bram Moolenaar3918c952005-03-15 22:34:55 +00004666 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667gui_set_bg_color(name)
4668 char_u *name;
4669{
4670 gui.back_pixel = gui_get_color(name);
4671 hl_set_bg_color_name(vim_strsave(name));
4672}
4673
4674/*
4675 * Allocate a color by name.
4676 * Returns INVALCOLOR and gives an error message when failed.
4677 */
4678 guicolor_T
4679gui_get_color(name)
4680 char_u *name;
4681{
4682 guicolor_T t;
4683
4684 if (*name == NUL)
4685 return INVALCOLOR;
4686 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004689#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 && gui.in_use
4691#endif
4692 )
4693 EMSG2(_("E254: Cannot allocate color %s"), name);
4694 return t;
4695}
4696
4697/*
4698 * Return the grey value of a color (range 0-255).
4699 */
4700 int
4701gui_get_lightness(pixel)
4702 guicolor_T pixel;
4703{
4704 long_u rgb = gui_mch_get_rgb(pixel);
4705
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004706 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004707 + (((rgb >> 8) & 0xff) * 587)
4708 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709}
4710
4711#if defined(FEAT_GUI_X11) || defined(PROTO)
4712 void
4713gui_new_scrollbar_colors()
4714{
4715 win_T *wp;
4716
4717 /* Nothing to do if GUI hasn't started yet. */
4718 if (!gui.in_use)
4719 return;
4720
4721 FOR_ALL_WINDOWS(wp)
4722 {
4723 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4724 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4725 }
4726 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4727}
4728#endif
4729
4730/*
4731 * Call this when focus has changed.
4732 */
4733 void
4734gui_focus_change(in_focus)
4735 int in_focus;
4736{
4737/*
4738 * Skip this code to avoid drawing the cursor when debugging and switching
4739 * between the debugger window and gvim.
4740 */
4741#if 1
4742 gui.in_focus = in_focus;
4743 out_flush(); /* make sure output has been written */
4744 gui_update_cursor(TRUE, FALSE);
4745
4746# ifdef FEAT_XIM
4747 xim_set_focus(in_focus);
4748# endif
4749
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004750 /* Put events in the input queue only when allowed.
4751 * ui_focus_change() isn't called directly, because it invokes
4752 * autocommands and that must not happen asynchronously. */
4753 if (!hold_gui_events)
4754 {
4755 char_u bytes[3];
4756
4757 bytes[0] = CSI;
4758 bytes[1] = KS_EXTRA;
4759 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4760 add_to_input_buf(bytes, 3);
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#endif
4763}
4764
4765/*
4766 * Called when the mouse moved (but not when dragging).
4767 */
4768 void
4769gui_mouse_moved(x, y)
4770 int x;
4771 int y;
4772{
4773 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004774 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaard3667a22006-03-16 21:35:52 +00004776 /* Ignore this while still starting up. */
4777 if (!gui.in_use || gui.starting)
4778 return;
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#ifdef FEAT_MOUSESHAPE
4781 /* Get window pointer, and update mouse shape as well. */
4782 wp = xy2win(x, y);
4783#endif
4784
4785 /* Only handle this when 'mousefocus' set and ... */
4786 if (p_mousef
4787 && !hold_gui_events /* not holding events */
4788 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4789 && State != HITRETURN /* but not hit-return prompt */
4790 && msg_scrolled == 0 /* no scrolled message */
4791 && !need_mouse_correct /* not moving the pointer */
4792 && gui.in_focus) /* gvim in focus */
4793 {
4794 /* Don't move the mouse when it's left or right of the Vim window */
4795 if (x < 0 || x > Columns * gui.char_width)
4796 return;
4797#ifndef FEAT_MOUSESHAPE
4798 wp = xy2win(x, y);
4799#endif
4800 if (wp == curwin || wp == NULL)
4801 return; /* still in the same old window, or none at all */
4802
Bram Moolenaar9c102382006-05-03 21:26:49 +00004803#ifdef FEAT_WINDOWS
4804 /* Ignore position in the tab pages line. */
4805 if (Y_2_ROW(y) < tabline_height())
4806 return;
4807#endif
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /*
4810 * format a mouse click on status line input
4811 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004812 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4813 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 */
4815 if (finish_op)
4816 {
4817 /* abort the current operator first */
4818 st[0] = ESC;
4819 add_to_input_buf(st, 1);
4820 }
4821 st[0] = CSI;
4822 st[1] = KS_MOUSE;
4823 st[2] = KE_FILLER;
4824 st[3] = (char_u)MOUSE_LEFT;
4825 fill_mouse_coord(st + 4,
4826#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004827 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#else
4829 -1,
4830#endif
4831 wp->w_height + W_WINROW(wp));
4832
4833 add_to_input_buf(st, 8);
4834 st[3] = (char_u)MOUSE_RELEASE;
4835 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#ifdef FEAT_GUI_GTK
4837 /* Need to wake up the main loop */
4838 if (gtk_main_level() > 0)
4839 gtk_main_quit();
4840#endif
4841 }
4842}
4843
4844/*
4845 * Called when mouse should be moved to window with focus.
4846 */
4847 void
4848gui_mouse_correct()
4849{
4850 int x, y;
4851 win_T *wp = NULL;
4852
4853 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004854
4855 if (!(gui.in_use && p_mousef))
4856 return;
4857
4858 gui_mch_getmouse(&x, &y);
4859 /* Don't move the mouse when it's left or right of the Vim window */
4860 if (x < 0 || x > Columns * gui.char_width)
4861 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004862 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004863# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004864 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004865# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004866 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004867 wp = xy2win(x, y);
4868 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004870 validate_cline_row();
4871 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4872 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875}
4876
4877/*
4878 * Find window where the mouse pointer "y" coordinate is in.
4879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 static win_T *
4881xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004882 int x UNUSED;
4883 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
4885#ifdef FEAT_WINDOWS
4886 int row;
4887 int col;
4888 win_T *wp;
4889
4890 row = Y_2_ROW(y);
4891 col = X_2_COL(x);
4892 if (row < 0 || col < 0) /* before first window */
4893 return NULL;
4894 wp = mouse_find_win(&row, &col);
4895# ifdef FEAT_MOUSESHAPE
4896 if (State == HITRETURN || State == ASKMORE)
4897 {
4898 if (Y_2_ROW(y) >= msg_row)
4899 update_mouseshape(SHAPE_IDX_MOREL);
4900 else
4901 update_mouseshape(SHAPE_IDX_MORE);
4902 }
4903 else if (row > wp->w_height) /* below status line */
4904 update_mouseshape(SHAPE_IDX_CLINE);
4905# ifdef FEAT_VERTSPLIT
4906 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004907 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 update_mouseshape(SHAPE_IDX_VSEP);
4909# endif
4910 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004911 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 update_mouseshape(SHAPE_IDX_STATUS);
4913 else
4914 update_mouseshape(-2);
4915# endif
4916 return wp;
4917#else
4918 return firstwin;
4919#endif
4920}
4921
4922/*
4923 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4924 * File names may be given to redefine the args list.
4925 */
4926 void
4927ex_gui(eap)
4928 exarg_T *eap;
4929{
4930 char_u *arg = eap->arg;
4931
4932 /*
4933 * Check for "-f" argument: foreground, don't fork.
4934 * Also don't fork when started with "gvim -f".
4935 * Do fork when using "gui -b".
4936 */
4937 if (arg[0] == '-'
4938 && (arg[1] == 'f' || arg[1] == 'b')
4939 && (arg[2] == NUL || vim_iswhite(arg[2])))
4940 {
4941 gui.dofork = (arg[1] == 'b');
4942 eap->arg = skipwhite(eap->arg + 2);
4943 }
4944 if (!gui.in_use)
4945 {
4946 /* Clear the command. Needed for when forking+exiting, to avoid part
4947 * of the argument ending up after the shell prompt. */
4948 msg_clr_eos_force();
4949 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004950#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004951 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 if (!ends_excmd(*eap->arg))
4955 ex_next(eap);
4956}
4957
4958#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004959 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
4961 * This is shared between Athena, Motif and GTK.
4962 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004963static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
4965/*
4966 * Callback function for do_in_runtimepath().
4967 */
4968 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004969gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004971 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004973 char_u *gfp_buffer = cookie;
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (STRLEN(fname) >= MAXPATHL)
4976 *gfp_buffer = NUL;
4977 else
4978 STRCPY(gfp_buffer, fname);
4979}
4980
4981/*
4982 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4983 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4984 */
4985 int
4986gui_find_bitmap(name, buffer, ext)
4987 char_u *name;
4988 char_u *buffer;
4989 char *ext;
4990{
4991 if (STRLEN(name) > MAXPATHL - 14)
4992 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004993 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004994 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4995 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 return FAIL;
4997 return OK;
4998}
4999
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005000# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001/*
5002 * Given the name of the "icon=" argument, try finding the bitmap file for the
5003 * icon. If it is an absolute path name, use it as it is. Otherwise append
5004 * "ext" and search for it in 'runtimepath'.
5005 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5006 * contains "name".
5007 */
5008 void
5009gui_find_iconfile(name, buffer, ext)
5010 char_u *name;
5011 char_u *buffer;
5012 char *ext;
5013{
5014 char_u buf[MAXPATHL + 1];
5015
5016 expand_env(name, buffer, MAXPATHL);
5017 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5018 STRCPY(buffer, buf);
5019}
5020# endif
5021#endif
5022
Bram Moolenaar9372a112005-12-06 19:59:18 +00005023#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 void
5025display_errors()
5026{
5027 char_u *p;
5028
5029 if (isatty(2))
5030 fflush(stderr);
5031 else if (error_ga.ga_data != NULL)
5032 {
5033 /* avoid putting up a message box with blanks only */
5034 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5035 if (!isspace(*p))
5036 {
5037 /* Truncate a very long message, it will go off-screen. */
5038 if (STRLEN(p) > 2000)
5039 STRCPY(p + 2000 - 14, "...(truncated)");
5040 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005041 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043 }
5044 ga_clear(&error_ga);
5045 }
5046}
5047#endif
5048
5049#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5050/*
5051 * Return TRUE if still starting up and there is no place to enter text.
5052 * For GTK and X11 we check if stderr is not a tty, which means we were
5053 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5054 * allow typing on stdin.
5055 */
5056 int
5057no_console_input()
5058{
5059 return ((!gui.in_use || gui.starting)
5060# ifndef NO_CONSOLE
5061 && !isatty(0) && !isatty(2)
5062# endif
5063 );
5064}
5065#endif
5066
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005067#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005068 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005069 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070/*
5071 * Update the current window and the screen.
5072 */
5073 void
5074gui_update_screen()
5075{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005076#ifdef FEAT_CONCEAL
5077 linenr_T conceal_old_cursor_line = 0;
5078 linenr_T conceal_new_cursor_line = 0;
5079 int conceal_update_lines = FALSE;
5080#endif
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 update_topline();
5083 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005084
5085#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005086 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005087 if (!finish_op && (
5088# ifdef FEAT_AUTOCMD
5089 has_cursormoved()
5090# endif
5091# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5092 ||
5093# endif
5094# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005095 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005096# endif
5097 )
5098 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005099 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005100# ifdef FEAT_AUTOCMD
5101 if (has_cursormoved())
5102 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5103# endif
5104# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005105 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005106 {
5107 conceal_old_cursor_line = last_cursormoved.lnum;
5108 conceal_new_cursor_line = curwin->w_cursor.lnum;
5109 conceal_update_lines = TRUE;
5110 }
5111# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005112 last_cursormoved = curwin->w_cursor;
5113 }
5114#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 update_screen(0); /* may need to update the screen */
5117 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118# if defined(FEAT_CONCEAL)
5119 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005120 && (conceal_old_cursor_line != conceal_new_cursor_line
5121 || conceal_cursor_line(curwin)
5122 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005123 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005124 if (conceal_old_cursor_line != conceal_new_cursor_line)
5125 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005126 update_single_line(curwin, conceal_new_cursor_line);
5127 curwin->w_valid &= ~VALID_CROW;
5128 }
5129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 out_flush(); /* make sure output has been written */
5131 gui_update_cursor(TRUE, FALSE);
5132 gui_mch_flush();
5133}
5134#endif
5135
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005136#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5138
5139/*
5140 * Get the text to use in a find/replace dialog. Uses the last search pattern
5141 * if the argument is empty.
5142 * Returns an allocated string.
5143 */
5144 char_u *
5145get_find_dialog_text(arg, wwordp, mcasep)
5146 char_u *arg;
5147 int *wwordp; /* return: TRUE if \< \> found */
5148 int *mcasep; /* return: TRUE if \C found */
5149{
5150 char_u *text;
5151
5152 if (*arg == NUL)
5153 text = last_search_pat();
5154 else
5155 text = arg;
5156 if (text != NULL)
5157 {
5158 text = vim_strsave(text);
5159 if (text != NULL)
5160 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005161 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 int i;
5163
5164 /* Remove "\V" */
5165 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5166 {
5167 mch_memmove(text, text + 2, (size_t)(len - 1));
5168 len -= 2;
5169 }
5170
5171 /* Recognize "\c" and "\C" and remove. */
5172 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5173 {
5174 *mcasep = (text[1] == 'C');
5175 mch_memmove(text, text + 2, (size_t)(len - 1));
5176 len -= 2;
5177 }
5178
5179 /* Recognize "\<text\>" and remove. */
5180 if (len >= 4
5181 && STRNCMP(text, "\\<", 2) == 0
5182 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5183 {
5184 *wwordp = TRUE;
5185 mch_memmove(text, text + 2, (size_t)(len - 4));
5186 text[len - 4] = NUL;
5187 }
5188
5189 /* Recognize "\/" or "\?" and remove. */
5190 for (i = 0; i + 1 < len; ++i)
5191 if (text[i] == '\\' && (text[i + 1] == '/'
5192 || text[i + 1] == '?'))
5193 {
5194 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5195 --len;
5196 }
5197 }
5198 }
5199 return text;
5200}
5201
5202/*
5203 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5204 */
5205 static void
5206concat_esc(gap, text, what)
5207 garray_T *gap;
5208 char_u *text;
5209 int what;
5210{
5211 while (*text != NUL)
5212 {
5213#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005214 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005215
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (l > 1)
5217 {
5218 while (--l >= 0)
5219 ga_append(gap, *text++);
5220 continue;
5221 }
5222#endif
5223 if (*text == what)
5224 ga_append(gap, '\\');
5225 ga_append(gap, *text);
5226 ++text;
5227 }
5228}
5229
5230/*
5231 * Handle the press of a button in the find-replace dialog.
5232 * Return TRUE when something was added to the input buffer.
5233 */
5234 int
5235gui_do_findrepl(flags, find_text, repl_text, down)
5236 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5237 char_u *find_text;
5238 char_u *repl_text;
5239 int down; /* Search downwards. */
5240{
5241 garray_T ga;
5242 int i;
5243 int type = (flags & FRD_TYPE_MASK);
5244 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005245 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005246 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005247 static int busy = FALSE;
5248
5249 /* When the screen is being updated we should not change buffers and
5250 * windows structures, it may cause freed memory to be used. Also don't
5251 * do this recursively (pressing "Find" quickly several times. */
5252 if (updating_screen || busy)
5253 return FALSE;
5254
5255 /* refuse replace when text cannot be changed */
5256 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5257 return FALSE;
5258
5259 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005262 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 ga_concat(&ga, (char_u *)"%s/");
5264
5265 ga_concat(&ga, (char_u *)"\\V");
5266 if (flags & FRD_MATCH_CASE)
5267 ga_concat(&ga, (char_u *)"\\C");
5268 else
5269 ga_concat(&ga, (char_u *)"\\c");
5270 if (flags & FRD_WHOLE_WORD)
5271 ga_concat(&ga, (char_u *)"\\<");
5272 if (type == FRD_REPLACEALL || down)
5273 concat_esc(&ga, find_text, '/'); /* escape slashes */
5274 else
5275 concat_esc(&ga, find_text, '?'); /* escape '?' */
5276 if (flags & FRD_WHOLE_WORD)
5277 ga_concat(&ga, (char_u *)"\\>");
5278
5279 if (type == FRD_REPLACEALL)
5280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005282 /* escape / and \ */
5283 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5284 if (p != NULL)
5285 ga_concat(&ga, p);
5286 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005288 }
5289 ga_append(&ga, NUL);
5290
5291 if (type == FRD_REPLACE)
5292 {
5293 /* Do the replacement when the text at the cursor matches. Thus no
5294 * replacement is done if the cursor was moved! */
5295 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5296 regmatch.rm_ic = 0;
5297 if (regmatch.regprog != NULL)
5298 {
5299 p = ml_get_cursor();
5300 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5301 && regmatch.startp[0] == p)
5302 {
5303 /* Clear the command line to remove any old "No match"
5304 * error. */
5305 msg_end_prompt();
5306
5307 if (u_save_cursor() == OK)
5308 {
5309 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005310 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005311
5312 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005313 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005314 ins_str(repl_text);
5315 }
5316 }
5317 else
5318 MSG(_("No match at cursor, finding next"));
5319 vim_free(regmatch.regprog);
5320 }
5321 }
5322
5323 if (type == FRD_REPLACEALL)
5324 {
5325 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005326 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 do_cmdline_cmd(ga.ga_data);
5328 }
5329 else
5330 {
5331 /* Search for the next match. */
5332 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005334 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 msg_scroll = i; /* don't let an error message set msg_scroll */
5336 }
5337
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005338 /* Don't want to pass did_emsg to other code, it may cause disabling
5339 * syntax HL if we were busy redrawing. */
5340 did_emsg = save_did_emsg;
5341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (State & (NORMAL | INSERT))
5343 {
5344 gui_update_screen(); /* update the screen */
5345 msg_didout = 0; /* overwrite any message */
5346 need_wait_return = FALSE; /* don't wait for return */
5347 }
5348
5349 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005350 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 return (ga.ga_len > 0);
5352}
5353
5354#endif
5355
5356#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5357 || defined(FEAT_GUI_MSWIN) \
5358 || defined(FEAT_GUI_MAC) \
5359 || defined(PROTO)
5360
5361#ifdef FEAT_WINDOWS
5362static void gui_wingoto_xy __ARGS((int x, int y));
5363
5364/*
5365 * Jump to the window at specified point (x, y).
5366 */
5367 static void
5368gui_wingoto_xy(x, y)
5369 int x;
5370 int y;
5371{
5372 int row = Y_2_ROW(y);
5373 int col = X_2_COL(x);
5374 win_T *wp;
5375
5376 if (row >= 0 && col >= 0)
5377 {
5378 wp = mouse_find_win(&row, &col);
5379 if (wp != NULL && wp != curwin)
5380 win_goto(wp);
5381 }
5382}
5383#endif
5384
5385/*
5386 * Process file drop. Mouse cursor position, key modifiers, name of files
5387 * and count of files are given. Argument "fnames[count]" has full pathnames
5388 * of dropped files, they will be freed in this function, and caller can't use
5389 * fnames after call this function.
5390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 void
5392gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005393 int x UNUSED;
5394 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 int_u modifiers;
5396 char_u **fnames;
5397 int count;
5398{
5399 int i;
5400 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005401 static int entered = FALSE;
5402
5403 /*
5404 * This function is called by event handlers. Just in case we get a
5405 * second event before the first one is handled, ignore the second one.
5406 * Not sure if this can ever happen, just in case.
5407 */
5408 if (entered)
5409 return;
5410 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 /*
5413 * When the cursor is at the command line, add the file names to the
5414 * command line, don't edit the files.
5415 */
5416 if (State & CMDLINE)
5417 {
5418 shorten_filenames(fnames, count);
5419 for (i = 0; i < count; ++i)
5420 {
5421 if (fnames[i] != NULL)
5422 {
5423 if (i > 0)
5424 add_to_input_buf((char_u*)" ", 1);
5425
5426 /* We don't know what command is used thus we can't be sure
5427 * about which characters need to be escaped. Only escape the
5428 * most common ones. */
5429# ifdef BACKSLASH_IN_FILENAME
5430 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5431# else
5432 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5433# endif
5434 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005435 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 vim_free(p);
5437 vim_free(fnames[i]);
5438 }
5439 }
5440 vim_free(fnames);
5441 }
5442 else
5443 {
5444 /* Go to the window under mouse cursor, then shorten given "fnames" by
5445 * current window, because a window can have local current dir. */
5446# ifdef FEAT_WINDOWS
5447 gui_wingoto_xy(x, y);
5448# endif
5449 shorten_filenames(fnames, count);
5450
5451 /* If Shift held down, remember the first item. */
5452 if ((modifiers & MOUSE_SHIFT) != 0)
5453 p = vim_strsave(fnames[0]);
5454 else
5455 p = NULL;
5456
5457 /* Handle the drop, :edit or :split to get to the file. This also
5458 * frees fnames[]. Skip this if there is only one item it's a
5459 * directory and Shift is held down. */
5460 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5461 && mch_isdir(fnames[0]))
5462 {
5463 vim_free(fnames[0]);
5464 vim_free(fnames);
5465 }
5466 else
5467 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5468
5469 /* If Shift held down, change to first file's directory. If the first
5470 * item is a directory, change to that directory (and let the explorer
5471 * plugin show the contents). */
5472 if (p != NULL)
5473 {
5474 if (mch_isdir(p))
5475 {
5476 if (mch_chdir((char *)p) == 0)
5477 shorten_fnames(TRUE);
5478 }
5479 else if (vim_chdirfile(p) == OK)
5480 shorten_fnames(TRUE);
5481 vim_free(p);
5482 }
5483
5484 /* Update the screen display */
5485 update_screen(NOT_VALID);
5486# ifdef FEAT_MENU
5487 gui_update_menus(0);
5488# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005489#ifdef FEAT_TITLE
5490 maketitle();
5491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 setcursor();
5493 out_flush();
5494 gui_update_cursor(FALSE, FALSE);
5495 gui_mch_flush();
5496 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005497
5498 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499}
5500#endif