blob: 6e5d58b5c6c480ade9ac5e14c6c21192d7e1a0db [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;
Bram Moolenaar0f272122013-01-23 18:37:40 +01001005#ifdef FEAT_GUI_MSWIN
1006 gui_mch_wide_font_changed();
1007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 return OK;
1009}
1010#endif
1011
1012 void
1013gui_set_cursor(row, col)
1014 int row;
1015 int col;
1016{
1017 gui.row = row;
1018 gui.col = col;
1019}
1020
1021/*
1022 * gui_check_pos - check if the cursor is on the screen.
1023 */
1024 static void
1025gui_check_pos()
1026{
1027 if (gui.row >= screen_Rows)
1028 gui.row = screen_Rows - 1;
1029 if (gui.col >= screen_Columns)
1030 gui.col = screen_Columns - 1;
1031 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1032 gui.cursor_is_valid = FALSE;
1033}
1034
1035/*
1036 * Redraw the cursor if necessary or when forced.
1037 * Careful: The contents of ScreenLines[] must match what is on the screen,
1038 * otherwise this goes wrong. May need to call out_flush() first.
1039 */
1040 void
1041gui_update_cursor(force, clear_selection)
1042 int force; /* when TRUE, update even when not moved */
1043 int clear_selection;/* clear selection under cursor */
1044{
1045 int cur_width = 0;
1046 int cur_height = 0;
1047 int old_hl_mask;
1048 int idx;
1049 int id;
1050 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1051 int cattr; /* cursor attributes */
1052 int attr;
1053 attrentry_T *aep = NULL;
1054
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001055 /* Don't update the cursor when halfway busy scrolling or the screen size
1056 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1057 if (!can_update_cursor || screen_Columns != gui.num_cols
1058 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 return;
1060
1061 gui_check_pos();
1062 if (!gui.cursor_is_valid || force
1063 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1064 {
1065 gui_undraw_cursor();
1066 if (gui.row < 0)
1067 return;
1068#ifdef USE_IM_CONTROL
1069 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1070 im_set_position(gui.row, gui.col);
1071#endif
1072 gui.cursor_row = gui.row;
1073 gui.cursor_col = gui.col;
1074
1075 /* Only write to the screen after ScreenLines[] has been initialized */
1076 if (!screen_cleared || ScreenLines == NULL)
1077 return;
1078
1079 /* Clear the selection if we are about to write over it */
1080 if (clear_selection)
1081 clip_may_clear_selection(gui.row, gui.row);
1082 /* Check that the cursor is inside the shell (resizing may have made
1083 * it invalid) */
1084 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1085 return;
1086
1087 gui.cursor_is_valid = TRUE;
1088
1089 /*
1090 * How the cursor is drawn depends on the current mode.
1091 */
1092 idx = get_shape_idx(FALSE);
1093 if (State & LANGMAP)
1094 id = shape_table[idx].id_lm;
1095 else
1096 id = shape_table[idx].id;
1097
1098 /* get the colors and attributes for the cursor. Default is inverted */
1099 cfg = INVALCOLOR;
1100 cbg = INVALCOLOR;
1101 cattr = HL_INVERSE;
1102 gui_mch_set_blinking(shape_table[idx].blinkwait,
1103 shape_table[idx].blinkon,
1104 shape_table[idx].blinkoff);
1105 if (id > 0)
1106 {
1107 cattr = syn_id2colors(id, &cfg, &cbg);
1108#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1109 {
1110 static int iid;
1111 guicolor_T fg, bg;
1112
Bram Moolenaarc236c162008-07-13 17:41:49 +00001113 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001114# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001115 preedit_get_status()
1116# else
1117 im_get_status()
1118# endif
1119 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 iid = syn_name2id((char_u *)"CursorIM");
1122 if (iid > 0)
1123 {
1124 syn_id2colors(iid, &fg, &bg);
1125 if (bg != INVALCOLOR)
1126 cbg = bg;
1127 if (fg != INVALCOLOR)
1128 cfg = fg;
1129 }
1130 }
1131 }
1132#endif
1133 }
1134
1135 /*
1136 * Get the attributes for the character under the cursor.
1137 * When no cursor color was given, use the character color.
1138 */
1139 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1140 if (attr > HL_ALL)
1141 aep = syn_gui_attr2entry(attr);
1142 if (aep != NULL)
1143 {
1144 attr = aep->ae_attr;
1145 if (cfg == INVALCOLOR)
1146 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1147 : aep->ae_u.gui.fg_color);
1148 if (cbg == INVALCOLOR)
1149 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1150 : aep->ae_u.gui.bg_color);
1151 }
1152 if (cfg == INVALCOLOR)
1153 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1154 if (cbg == INVALCOLOR)
1155 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1156
1157#ifdef FEAT_XIM
1158 if (aep != NULL)
1159 {
1160 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1161 : aep->ae_u.gui.bg_color);
1162 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1163 : aep->ae_u.gui.fg_color);
1164 if (xim_bg_color == INVALCOLOR)
1165 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1166 : gui.back_pixel;
1167 if (xim_fg_color == INVALCOLOR)
1168 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1169 : gui.norm_pixel;
1170 }
1171 else
1172 {
1173 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1174 : gui.back_pixel;
1175 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1176 : gui.norm_pixel;
1177 }
1178#endif
1179
1180 attr &= ~HL_INVERSE;
1181 if (cattr & HL_INVERSE)
1182 {
1183 cc = cbg;
1184 cbg = cfg;
1185 cfg = cc;
1186 }
1187 cattr &= ~HL_INVERSE;
1188
1189 /*
1190 * When we don't have window focus, draw a hollow cursor.
1191 */
1192 if (!gui.in_focus)
1193 {
1194 gui_mch_draw_hollow_cursor(cbg);
1195 return;
1196 }
1197
1198 old_hl_mask = gui.highlight_mask;
1199 if (shape_table[idx].shape == SHAPE_BLOCK
1200#ifdef FEAT_HANGULIN
1201 || composing_hangul
1202#endif
1203 )
1204 {
1205 /*
1206 * Draw the text character with the cursor colors. Use the
1207 * character attributes plus the cursor attributes.
1208 */
1209 gui.highlight_mask = (cattr | attr);
1210#ifdef FEAT_HANGULIN
1211 if (composing_hangul)
1212 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1213 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1214 else
1215#endif
1216 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1217 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1218 }
1219 else
1220 {
1221#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1222 int col_off = FALSE;
1223#endif
1224 /*
1225 * First draw the partial cursor, then overwrite with the text
1226 * character, using a transparent background.
1227 */
1228 if (shape_table[idx].shape == SHAPE_VER)
1229 {
1230 cur_height = gui.char_height;
1231 cur_width = (gui.char_width * shape_table[idx].percentage
1232 + 99) / 100;
1233 }
1234 else
1235 {
1236 cur_height = (gui.char_height * shape_table[idx].percentage
1237 + 99) / 100;
1238 cur_width = gui.char_width;
1239 }
1240#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001241 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1242 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 /* Double wide character. */
1245 if (shape_table[idx].shape != SHAPE_VER)
1246 cur_width += gui.char_width;
1247# ifdef FEAT_RIGHTLEFT
1248 if (CURSOR_BAR_RIGHT)
1249 {
1250 /* gui.col points to the left halve of the character but
1251 * the vertical line needs to be on the right halve.
1252 * A double-wide horizontal line is also drawn from the
1253 * right halve in gui_mch_draw_part_cursor(). */
1254 col_off = TRUE;
1255 ++gui.col;
1256 }
1257# endif
1258 }
1259#endif
1260 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1261#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1262 if (col_off)
1263 --gui.col;
1264#endif
1265
1266#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1267 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1268 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1269 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1270 (guicolor_T)0, (guicolor_T)0, 0);
1271#endif
1272 }
1273 gui.highlight_mask = old_hl_mask;
1274 }
1275}
1276
1277#if defined(FEAT_MENU) || defined(PROTO)
1278 void
1279gui_position_menu()
1280{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001281# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (gui.menu_is_active && gui.in_use)
1283 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1284# endif
1285}
1286#endif
1287
1288/*
1289 * Position the various GUI components (text area, menu). The vertical
1290 * scrollbars are NOT handled here. See gui_update_scrollbars().
1291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 static void
1293gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001294 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 int text_area_x;
1297 int text_area_y;
1298 int text_area_width;
1299 int text_area_height;
1300
1301 /* avoid that moving components around generates events */
1302 ++hold_gui_events;
1303
1304 text_area_x = 0;
1305 if (gui.which_scrollbars[SBAR_LEFT])
1306 text_area_x += gui.scrollbar_width;
1307
1308 text_area_y = 0;
1309#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1310 gui.menu_width = total_width;
1311 if (gui.menu_is_active)
1312 text_area_y += gui.menu_height;
1313#endif
1314#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1315 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1316 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1317#endif
1318
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001319# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001320 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001321 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001322 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001323#endif
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1326 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1327 {
1328# ifdef FEAT_GUI_ATHENA
1329 gui_mch_set_toolbar_pos(0, text_area_y,
1330 gui.menu_width, gui.toolbar_height);
1331# endif
1332 text_area_y += gui.toolbar_height;
1333 }
1334#endif
1335
1336 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1337 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1338
1339 gui_mch_set_text_area_pos(text_area_x,
1340 text_area_y,
1341 text_area_width,
1342 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001343#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 + xim_get_status_area_height()
1345#endif
1346 );
1347#ifdef FEAT_MENU
1348 gui_position_menu();
1349#endif
1350 if (gui.which_scrollbars[SBAR_BOTTOM])
1351 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1352 text_area_x,
1353 text_area_y + text_area_height,
1354 text_area_width,
1355 gui.scrollbar_height);
1356 gui.left_sbar_x = 0;
1357 gui.right_sbar_x = text_area_x + text_area_width;
1358
1359 --hold_gui_events;
1360}
1361
Bram Moolenaar02743632005-07-25 20:42:36 +00001362/*
1363 * Get the width of the widgets and decorations to the side of the text area.
1364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 int
1366gui_get_base_width()
1367{
1368 int base_width;
1369
1370 base_width = 2 * gui.border_offset;
1371 if (gui.which_scrollbars[SBAR_LEFT])
1372 base_width += gui.scrollbar_width;
1373 if (gui.which_scrollbars[SBAR_RIGHT])
1374 base_width += gui.scrollbar_width;
1375 return base_width;
1376}
1377
Bram Moolenaar02743632005-07-25 20:42:36 +00001378/*
1379 * Get the height of the widgets and decorations above and below the text area.
1380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 int
1382gui_get_base_height()
1383{
1384 int base_height;
1385
1386 base_height = 2 * gui.border_offset;
1387 if (gui.which_scrollbars[SBAR_BOTTOM])
1388 base_height += gui.scrollbar_height;
1389#ifdef FEAT_GUI_GTK
1390 /* We can't take the sizes properly into account until anything is
1391 * realized. Therefore we recalculate all the values here just before
1392 * setting the size. (--mdcki) */
1393#else
1394# ifdef FEAT_MENU
1395 if (gui.menu_is_active)
1396 base_height += gui.menu_height;
1397# endif
1398# ifdef FEAT_TOOLBAR
1399 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1400# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1401 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1402# else
1403 base_height += gui.toolbar_height;
1404# endif
1405# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001406# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1407 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001408 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001409 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411# ifdef FEAT_FOOTER
1412 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1413 base_height += gui.footer_height;
1414# endif
1415# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1416 base_height += gui_mch_text_area_extra_height();
1417# endif
1418#endif
1419 return base_height;
1420}
1421
1422/*
1423 * Should be called after the GUI shell has been resized. Its arguments are
1424 * the new width and height of the shell in pixels.
1425 */
1426 void
1427gui_resize_shell(pixel_width, pixel_height)
1428 int pixel_width;
1429 int pixel_height;
1430{
1431 static int busy = FALSE;
1432
1433 if (!gui.shell_created) /* ignore when still initializing */
1434 return;
1435
1436 /*
1437 * Can't resize the screen while it is being redrawn. Remember the new
1438 * size and handle it later.
1439 */
1440 if (updating_screen || busy)
1441 {
1442 new_pixel_width = pixel_width;
1443 new_pixel_height = pixel_height;
1444 return;
1445 }
1446
1447again:
1448 busy = TRUE;
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /* Flush pending output before redrawing */
1451 out_flush();
1452
1453 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001454 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456 gui_position_components(pixel_width);
1457
1458 gui_reset_scroll_region();
1459 /*
1460 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1461 * at the last line here (why does it have to be one row too low?).
1462 */
1463 if (State == ASKMORE || State == CONFIRM)
1464 gui.row = gui.num_rows;
1465
1466 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1467 * the safe side. */
1468 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1469 || gui.num_rows != Rows || gui.num_cols != Columns)
1470 shell_resized();
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 gui_update_scrollbars(TRUE);
1473 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001474#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 xim_set_status_area();
1476#endif
1477
1478 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 /*
1481 * We could have been called again while redrawing the screen.
1482 * Need to do it all again with the latest size then.
1483 */
1484 if (new_pixel_height)
1485 {
1486 pixel_width = new_pixel_width;
1487 pixel_height = new_pixel_height;
1488 new_pixel_width = 0;
1489 new_pixel_height = 0;
1490 goto again;
1491 }
1492}
1493
1494/*
1495 * Check if gui_resize_shell() must be called.
1496 */
1497 void
1498gui_may_resize_shell()
1499{
1500 int h, w;
1501
1502 if (new_pixel_height)
1503 {
1504 /* careful: gui_resize_shell() may postpone the resize again if we
1505 * were called indirectly by it */
1506 w = new_pixel_width;
1507 h = new_pixel_height;
1508 new_pixel_width = 0;
1509 new_pixel_height = 0;
1510 gui_resize_shell(w, h);
1511 }
1512}
1513
1514 int
1515gui_get_shellsize()
1516{
1517 Rows = gui.num_rows;
1518 Columns = gui.num_cols;
1519 return OK;
1520}
1521
1522/*
1523 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001524 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1525 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001528gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001529 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001531 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 int base_width;
1534 int base_height;
1535 int width;
1536 int height;
1537 int min_width;
1538 int min_height;
1539 int screen_w;
1540 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001541#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001542 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001543 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001544#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001545 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
1547 if (!gui.shell_created)
1548 return;
1549
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001550#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 /* If not setting to a user specified size and maximized, calculate the
1552 * number of characters that fit in the maximized window. */
1553 if (!mustset && gui_mch_maximized())
1554 {
1555 gui_mch_newfont();
1556 return;
1557 }
1558#endif
1559
1560 base_width = gui_get_base_width();
1561 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001562 if (fit_to_display)
1563 /* Remember the original window position. */
1564 gui_mch_get_winpos(&x, &y);
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#ifdef USE_SUN_WORKSHOP
1567 if (!mustset && usingSunWorkShop
1568 && workshop_get_width_height(&width, &height))
1569 {
1570 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1571 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1572 }
1573 else
1574#endif
1575 {
1576 width = Columns * gui.char_width + base_width;
1577 height = Rows * gui.char_height + base_height;
1578 }
1579
1580 if (fit_to_display)
1581 {
1582 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001583 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 Columns = (screen_w - base_width) / gui.char_width;
1586 if (Columns < MIN_COLUMNS)
1587 Columns = MIN_COLUMNS;
1588 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001589#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001590 ++did_adjust;
1591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001593 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 Rows = (screen_h - base_height) / gui.char_height;
1596 check_shellsize();
1597 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001598#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001599 ++did_adjust;
1600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001602#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001603 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1604 && height + gui.char_height >= screen_h))
1605 /* don't unmaximize if at maximum size */
1606 un_maximize = FALSE;
1607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 gui.num_cols = Columns;
1610 gui.num_rows = Rows;
1611
1612 min_width = base_width + MIN_COLUMNS * gui.char_width;
1613 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001614#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001615 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001616#endif
1617
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001618#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001619 if (un_maximize)
1620 {
1621 /* If the window size is smaller than the screen unmaximize the
1622 * window, otherwise resizing won't work. */
1623 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1624 if ((width + gui.char_width < screen_w
1625 || height + gui.char_height * 2 < screen_h)
1626 && gui_mch_maximized())
1627 gui_mch_unmaximize();
1628 }
1629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001632 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001634 if (fit_to_display && x >= 0 && y >= 0)
1635 {
1636 /* Some window managers put the Vim window left of/above the screen.
1637 * Only change the position if it wasn't already negative before
1638 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 gui_mch_update();
1640 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1641 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1642 }
1643
1644 gui_position_components(width);
1645 gui_update_scrollbars(TRUE);
1646 gui_reset_scroll_region();
1647}
1648
1649/*
1650 * Called when Rows and/or Columns has changed.
1651 */
1652 void
1653gui_new_shellsize()
1654{
1655 gui_reset_scroll_region();
1656}
1657
1658/*
1659 * Make scroll region cover whole screen.
1660 */
1661 void
1662gui_reset_scroll_region()
1663{
1664 gui.scroll_region_top = 0;
1665 gui.scroll_region_bot = gui.num_rows - 1;
1666 gui.scroll_region_left = 0;
1667 gui.scroll_region_right = gui.num_cols - 1;
1668}
1669
1670 void
1671gui_start_highlight(mask)
1672 int mask;
1673{
1674 if (mask > HL_ALL) /* highlight code */
1675 gui.highlight_mask = mask;
1676 else /* mask */
1677 gui.highlight_mask |= mask;
1678}
1679
1680 void
1681gui_stop_highlight(mask)
1682 int mask;
1683{
1684 if (mask > HL_ALL) /* highlight code */
1685 gui.highlight_mask = HL_NORMAL;
1686 else /* mask */
1687 gui.highlight_mask &= ~mask;
1688}
1689
1690/*
1691 * Clear a rectangular region of the screen from text pos (row1, col1) to
1692 * (row2, col2) inclusive.
1693 */
1694 void
1695gui_clear_block(row1, col1, row2, col2)
1696 int row1;
1697 int col1;
1698 int row2;
1699 int col2;
1700{
1701 /* Clear the selection if we are about to write over it */
1702 clip_may_clear_selection(row1, row2);
1703
1704 gui_mch_clear_block(row1, col1, row2, col2);
1705
1706 /* Invalidate cursor if it was in this block */
1707 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1708 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1709 gui.cursor_is_valid = FALSE;
1710}
1711
1712/*
1713 * Write code to update the cursor later. This avoids the need to flush the
1714 * output buffer before calling gui_update_cursor().
1715 */
1716 void
1717gui_update_cursor_later()
1718{
1719 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1720}
1721
1722 void
1723gui_write(s, len)
1724 char_u *s;
1725 int len;
1726{
1727 char_u *p;
1728 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 int force_scrollbar = FALSE;
1731 static win_T *old_curwin = NULL;
1732
1733/* #define DEBUG_GUI_WRITE */
1734#ifdef DEBUG_GUI_WRITE
1735 {
1736 int i;
1737 char_u *str;
1738
1739 printf("gui_write(%d):\n ", len);
1740 for (i = 0; i < len; i++)
1741 if (s[i] == ESC)
1742 {
1743 if (i != 0)
1744 printf("\n ");
1745 printf("<ESC>");
1746 }
1747 else
1748 {
1749 str = transchar_byte(s[i]);
1750 if (str[0] && str[1])
1751 printf("<%s>", (char *)str);
1752 else
1753 printf("%s", (char *)str);
1754 }
1755 printf("\n");
1756 }
1757#endif
1758 while (len)
1759 {
1760 if (s[0] == ESC && s[1] == '|')
1761 {
1762 p = s + 2;
1763 if (VIM_ISDIGIT(*p))
1764 {
1765 arg1 = getdigits(&p);
1766 if (p > s + len)
1767 break;
1768 if (*p == ';')
1769 {
1770 ++p;
1771 arg2 = getdigits(&p);
1772 if (p > s + len)
1773 break;
1774 }
1775 }
1776 switch (*p)
1777 {
1778 case 'C': /* Clear screen */
1779 clip_scroll_selection(9999);
1780 gui_mch_clear_all();
1781 gui.cursor_is_valid = FALSE;
1782 force_scrollbar = TRUE;
1783 break;
1784 case 'M': /* Move cursor */
1785 gui_set_cursor(arg1, arg2);
1786 break;
1787 case 's': /* force cursor (shape) update */
1788 force_cursor = TRUE;
1789 break;
1790 case 'R': /* Set scroll region */
1791 if (arg1 < arg2)
1792 {
1793 gui.scroll_region_top = arg1;
1794 gui.scroll_region_bot = arg2;
1795 }
1796 else
1797 {
1798 gui.scroll_region_top = arg2;
1799 gui.scroll_region_bot = arg1;
1800 }
1801 break;
1802#ifdef FEAT_VERTSPLIT
1803 case 'V': /* Set vertical scroll region */
1804 if (arg1 < arg2)
1805 {
1806 gui.scroll_region_left = arg1;
1807 gui.scroll_region_right = arg2;
1808 }
1809 else
1810 {
1811 gui.scroll_region_left = arg2;
1812 gui.scroll_region_right = arg1;
1813 }
1814 break;
1815#endif
1816 case 'd': /* Delete line */
1817 gui_delete_lines(gui.row, 1);
1818 break;
1819 case 'D': /* Delete lines */
1820 gui_delete_lines(gui.row, arg1);
1821 break;
1822 case 'i': /* Insert line */
1823 gui_insert_lines(gui.row, 1);
1824 break;
1825 case 'I': /* Insert lines */
1826 gui_insert_lines(gui.row, arg1);
1827 break;
1828 case '$': /* Clear to end-of-line */
1829 gui_clear_block(gui.row, gui.col, gui.row,
1830 (int)Columns - 1);
1831 break;
1832 case 'h': /* Turn on highlighting */
1833 gui_start_highlight(arg1);
1834 break;
1835 case 'H': /* Turn off highlighting */
1836 gui_stop_highlight(arg1);
1837 break;
1838 case 'f': /* flash the window (visual bell) */
1839 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1840 break;
1841 default:
1842 p = s + 1; /* Skip the ESC */
1843 break;
1844 }
1845 len -= (int)(++p - s);
1846 s = p;
1847 }
1848 else if (
1849#ifdef EBCDIC
1850 CtrlChar(s[0]) != 0 /* Ctrl character */
1851#else
1852 s[0] < 0x20 /* Ctrl character */
1853#endif
1854#ifdef FEAT_SIGN_ICONS
1855 && s[0] != SIGN_BYTE
1856# ifdef FEAT_NETBEANS_INTG
1857 && s[0] != MULTISIGN_BYTE
1858# endif
1859#endif
1860 )
1861 {
1862 if (s[0] == '\n') /* NL */
1863 {
1864 gui.col = 0;
1865 if (gui.row < gui.scroll_region_bot)
1866 gui.row++;
1867 else
1868 gui_delete_lines(gui.scroll_region_top, 1);
1869 }
1870 else if (s[0] == '\r') /* CR */
1871 {
1872 gui.col = 0;
1873 }
1874 else if (s[0] == '\b') /* Backspace */
1875 {
1876 if (gui.col)
1877 --gui.col;
1878 }
1879 else if (s[0] == Ctrl_L) /* cursor-right */
1880 {
1881 ++gui.col;
1882 }
1883 else if (s[0] == Ctrl_G) /* Beep */
1884 {
1885 gui_mch_beep();
1886 }
1887 /* Other Ctrl character: shouldn't happen! */
1888
1889 --len; /* Skip this char */
1890 ++s;
1891 }
1892 else
1893 {
1894 p = s;
1895 while (len > 0 && (
1896#ifdef EBCDIC
1897 CtrlChar(*p) == 0
1898#else
1899 *p >= 0x20
1900#endif
1901#ifdef FEAT_SIGN_ICONS
1902 || *p == SIGN_BYTE
1903# ifdef FEAT_NETBEANS_INTG
1904 || *p == MULTISIGN_BYTE
1905# endif
1906#endif
1907 ))
1908 {
1909 len--;
1910 p++;
1911 }
1912 gui_outstr(s, (int)(p - s));
1913 s = p;
1914 }
1915 }
1916
1917 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1918 * set). */
1919 if (force_cursor)
1920 gui_update_cursor(TRUE, TRUE);
1921
1922 /* When switching to another window the dragging must have stopped.
1923 * Required for GTK, dragged_sb isn't reset. */
1924 if (old_curwin != curwin)
1925 gui.dragged_sb = SBAR_NONE;
1926
1927 /* Update the scrollbars after clearing the screen or when switched
1928 * to another window.
1929 * Update the horizontal scrollbar always, it's difficult to check all
1930 * situations where it might change. */
1931 if (force_scrollbar || old_curwin != curwin)
1932 gui_update_scrollbars(force_scrollbar);
1933 else
1934 gui_update_horiz_scrollbar(FALSE);
1935 old_curwin = curwin;
1936
1937 /*
1938 * We need to make sure this is cleared since Athena doesn't tell us when
1939 * he is done dragging. Do the same for GTK.
1940 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001941#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 gui.dragged_sb = SBAR_NONE;
1943#endif
1944
1945 gui_mch_flush(); /* In case vim decides to take a nap */
1946}
1947
1948/*
1949 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1950 * produces wrong results. Call gui_dont_update_cursor() before that code and
1951 * gui_can_update_cursor() afterwards.
1952 */
1953 void
1954gui_dont_update_cursor()
1955{
1956 if (gui.in_use)
1957 {
1958 /* Undraw the cursor now, we probably can't do it after the change. */
1959 gui_undraw_cursor();
1960 can_update_cursor = FALSE;
1961 }
1962}
1963
1964 void
1965gui_can_update_cursor()
1966{
1967 can_update_cursor = TRUE;
1968 /* No need to update the cursor right now, there is always more output
1969 * after scrolling. */
1970}
1971
1972 static void
1973gui_outstr(s, len)
1974 char_u *s;
1975 int len;
1976{
1977 int this_len;
1978#ifdef FEAT_MBYTE
1979 int cells;
1980#endif
1981
1982 if (len == 0)
1983 return;
1984
1985 if (len < 0)
1986 len = (int)STRLEN(s);
1987
1988 while (len > 0)
1989 {
1990#ifdef FEAT_MBYTE
1991 if (has_mbyte)
1992 {
1993 /* Find out how many chars fit in the current line. */
1994 cells = 0;
1995 for (this_len = 0; this_len < len; )
1996 {
1997 cells += (*mb_ptr2cells)(s + this_len);
1998 if (gui.col + cells > Columns)
1999 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002000 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 if (this_len > len)
2003 this_len = len; /* don't include following composing char */
2004 }
2005 else
2006#endif
2007 if (gui.col + len > Columns)
2008 this_len = Columns - gui.col;
2009 else
2010 this_len = len;
2011
2012 (void)gui_outstr_nowrap(s, this_len,
2013 0, (guicolor_T)0, (guicolor_T)0, 0);
2014 s += this_len;
2015 len -= this_len;
2016#ifdef FEAT_MBYTE
2017 /* fill up for a double-width char that doesn't fit. */
2018 if (len > 0 && gui.col < Columns)
2019 (void)gui_outstr_nowrap((char_u *)" ", 1,
2020 0, (guicolor_T)0, (guicolor_T)0, 0);
2021#endif
2022 /* The cursor may wrap to the next line. */
2023 if (gui.col >= Columns)
2024 {
2025 gui.col = 0;
2026 gui.row++;
2027 }
2028 }
2029}
2030
2031/*
2032 * Output one character (may be one or two display cells).
2033 * Caller must check for valid "off".
2034 * Returns FAIL or OK, just like gui_outstr_nowrap().
2035 */
2036 static int
2037gui_screenchar(off, flags, fg, bg, back)
2038 int off; /* Offset from start of screen */
2039 int flags;
2040 guicolor_T fg, bg; /* colors for cursor */
2041 int back; /* backup this many chars when using bold trick */
2042{
2043#ifdef FEAT_MBYTE
2044 char_u buf[MB_MAXBYTES + 1];
2045
2046 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2047 if (enc_utf8 && ScreenLines[off] == 0)
2048 return OK;
2049
2050 if (enc_utf8 && ScreenLinesUC[off] != 0)
2051 /* Draw UTF-8 multi-byte character. */
2052 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2053 flags, fg, bg, back);
2054
2055 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2056 {
2057 buf[0] = ScreenLines[off];
2058 buf[1] = ScreenLines2[off];
2059 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2060 }
2061
2062 /* Draw non-multi-byte character or DBCS character. */
2063 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002064 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 flags, fg, bg, back);
2066#else
2067 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
2068#endif
2069}
2070
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002071#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072/*
2073 * Output the string at the given screen position. This is used in place
2074 * of gui_screenchar() where possible because Pango needs as much context
2075 * as possible to work nicely. It's a lot faster as well.
2076 */
2077 static int
2078gui_screenstr(off, len, flags, fg, bg, back)
2079 int off; /* Offset from start of screen */
2080 int len; /* string length in screen cells */
2081 int flags;
2082 guicolor_T fg, bg; /* colors for cursor */
2083 int back; /* backup this many chars when using bold trick */
2084{
2085 char_u *buf;
2086 int outlen = 0;
2087 int i;
2088 int retval;
2089
2090 if (len <= 0) /* "cannot happen"? */
2091 return OK;
2092
2093 if (enc_utf8)
2094 {
2095 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2096 if (buf == NULL)
2097 return OK; /* not much we could do here... */
2098
2099 for (i = off; i < off + len; ++i)
2100 {
2101 if (ScreenLines[i] == 0)
2102 continue; /* skip second half of double-width char */
2103
2104 if (ScreenLinesUC[i] == 0)
2105 buf[outlen++] = ScreenLines[i];
2106 else
2107 outlen += utfc_char2bytes(i, buf + outlen);
2108 }
2109
2110 buf[outlen] = NUL; /* only to aid debugging */
2111 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2112 vim_free(buf);
2113
2114 return retval;
2115 }
2116 else if (enc_dbcs == DBCS_JPNU)
2117 {
2118 buf = alloc((unsigned)(len * 2 + 1));
2119 if (buf == NULL)
2120 return OK; /* not much we could do here... */
2121
2122 for (i = off; i < off + len; ++i)
2123 {
2124 buf[outlen++] = ScreenLines[i];
2125
2126 /* handle double-byte single-width char */
2127 if (ScreenLines[i] == 0x8e)
2128 buf[outlen++] = ScreenLines2[i];
2129 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2130 buf[outlen++] = ScreenLines[++i];
2131 }
2132
2133 buf[outlen] = NUL; /* only to aid debugging */
2134 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2135 vim_free(buf);
2136
2137 return retval;
2138 }
2139 else
2140 {
2141 return gui_outstr_nowrap(&ScreenLines[off], len,
2142 flags, fg, bg, back);
2143 }
2144}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002145#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147/*
2148 * Output the given string at the current cursor position. If the string is
2149 * too long to fit on the line, then it is truncated.
2150 * "flags":
2151 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2152 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002153 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * background.
2155 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2156 * it.
2157 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2158 * FAIL (the caller should start drawing "back" chars back).
2159 */
2160 int
2161gui_outstr_nowrap(s, len, flags, fg, bg, back)
2162 char_u *s;
2163 int len;
2164 int flags;
2165 guicolor_T fg, bg; /* colors for cursor */
2166 int back; /* backup this many chars when using bold trick */
2167{
2168 long_u highlight_mask;
2169 long_u hl_mask_todo;
2170 guicolor_T fg_color;
2171 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002172 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002173#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 GuiFont font = NOFONT;
2175# ifdef FEAT_XFONTSET
2176 GuiFontset fontset = NOFONTSET;
2177# endif
2178#endif
2179 attrentry_T *aep = NULL;
2180 int draw_flags;
2181 int col = gui.col;
2182#ifdef FEAT_SIGN_ICONS
2183 int draw_sign = FALSE;
2184# ifdef FEAT_NETBEANS_INTG
2185 int multi_sign = FALSE;
2186# endif
2187#endif
2188
2189 if (len < 0)
2190 len = (int)STRLEN(s);
2191 if (len == 0)
2192 return OK;
2193
2194#ifdef FEAT_SIGN_ICONS
2195 if (*s == SIGN_BYTE
2196# ifdef FEAT_NETBEANS_INTG
2197 || *s == MULTISIGN_BYTE
2198# endif
2199 )
2200 {
2201# ifdef FEAT_NETBEANS_INTG
2202 if (*s == MULTISIGN_BYTE)
2203 multi_sign = TRUE;
2204# endif
2205 /* draw spaces instead */
2206 s = (char_u *)" ";
2207 if (len == 1 && col > 0)
2208 --col;
2209 len = 2;
2210 draw_sign = TRUE;
2211 highlight_mask = 0;
2212 }
2213 else
2214#endif
2215 if (gui.highlight_mask > HL_ALL)
2216 {
2217 aep = syn_gui_attr2entry(gui.highlight_mask);
2218 if (aep == NULL) /* highlighting not set */
2219 highlight_mask = 0;
2220 else
2221 highlight_mask = aep->ae_attr;
2222 }
2223 else
2224 highlight_mask = gui.highlight_mask;
2225 hl_mask_todo = highlight_mask;
2226
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002227#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 /* Set the font */
2229 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2230 font = aep->ae_u.gui.font;
2231# ifdef FEAT_XFONTSET
2232 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2233 fontset = aep->ae_u.gui.fontset;
2234# endif
2235 else
2236 {
2237# ifdef FEAT_XFONTSET
2238 if (gui.fontset != NOFONTSET)
2239 fontset = gui.fontset;
2240 else
2241# endif
2242 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2243 {
2244 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2245 {
2246 font = gui.boldital_font;
2247 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2248 }
2249 else if (gui.bold_font != NOFONT)
2250 {
2251 font = gui.bold_font;
2252 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2253 }
2254 else
2255 font = gui.norm_font;
2256 }
2257 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2258 {
2259 font = gui.ital_font;
2260 hl_mask_todo &= ~HL_ITALIC;
2261 }
2262 else
2263 font = gui.norm_font;
2264 }
2265# ifdef FEAT_XFONTSET
2266 if (fontset != NOFONTSET)
2267 gui_mch_set_fontset(fontset);
2268 else
2269# endif
2270 gui_mch_set_font(font);
2271#endif
2272
2273 draw_flags = 0;
2274
2275 /* Set the color */
2276 bg_color = gui.back_pixel;
2277 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2278 {
2279 draw_flags |= DRAW_CURSOR;
2280 fg_color = fg;
2281 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002282 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 else if (aep != NULL)
2285 {
2286 fg_color = aep->ae_u.gui.fg_color;
2287 if (fg_color == INVALCOLOR)
2288 fg_color = gui.norm_pixel;
2289 bg_color = aep->ae_u.gui.bg_color;
2290 if (bg_color == INVALCOLOR)
2291 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002292 sp_color = aep->ae_u.gui.sp_color;
2293 if (sp_color == INVALCOLOR)
2294 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002299 sp_color = fg_color;
2300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2303 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002304#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 gui_mch_set_colors(bg_color, fg_color);
2306#else
2307 gui_mch_set_fg_color(bg_color);
2308 gui_mch_set_bg_color(fg_color);
2309#endif
2310 }
2311 else
2312 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002313#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 gui_mch_set_colors(fg_color, bg_color);
2315#else
2316 gui_mch_set_fg_color(fg_color);
2317 gui_mch_set_bg_color(bg_color);
2318#endif
2319 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002320 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 /* Clear the selection if we are about to write over it */
2323 if (!(flags & GUI_MON_NOCLEAR))
2324 clip_may_clear_selection(gui.row, gui.row);
2325
2326
2327#ifndef MSWIN16_FASTTEXT
2328 /* If there's no bold font, then fake it */
2329 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2330 draw_flags |= DRAW_BOLD;
2331#endif
2332
2333 /*
2334 * When drawing bold or italic characters the spill-over from the left
2335 * neighbor may be destroyed. Let the caller backup to start redrawing
2336 * just after a blank.
2337 */
2338 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2339 return FAIL;
2340
Bram Moolenaare60acc12011-05-10 16:41:25 +02002341#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 /* If there's no italic font, then fake it.
2343 * For GTK2, we don't need a different font for italic style. */
2344 if (hl_mask_todo & HL_ITALIC)
2345 draw_flags |= DRAW_ITALIC;
2346
2347 /* Do we underline the text? */
2348 if (hl_mask_todo & HL_UNDERLINE)
2349 draw_flags |= DRAW_UNDERL;
2350#else
2351 /* Do we underline the text? */
2352 if ((hl_mask_todo & HL_UNDERLINE)
2353# ifndef MSWIN16_FASTTEXT
2354 || (hl_mask_todo & HL_ITALIC)
2355# endif
2356 )
2357 draw_flags |= DRAW_UNDERL;
2358#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002359 /* Do we undercurl the text? */
2360 if (hl_mask_todo & HL_UNDERCURL)
2361 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002363 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (flags & GUI_MON_TRS_CURSOR)
2365 draw_flags |= DRAW_TRANSP;
2366
2367 /*
2368 * Draw the text.
2369 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002370#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /* The value returned is the length in display cells */
2372 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2373#else
2374# ifdef FEAT_MBYTE
2375 if (enc_utf8)
2376 {
2377 int start; /* index of bytes to be drawn */
2378 int cells; /* cellwidth of bytes to be drawn */
2379 int thislen; /* length of bytes to be drawin */
2380 int cn; /* cellwidth of current char */
2381 int i; /* index of current char */
2382 int c; /* current char value */
2383 int cl; /* byte length of current char */
2384 int comping; /* current char is composing */
2385 int scol = col; /* screen column */
Bram Moolenaar45933962013-01-23 17:43:57 +01002386 int curr_wide; /* use 'guifontwide' */
2387 int prev_wide = FALSE;
2388 int wide_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 /* Break the string at a composing character, it has to be drawn on
2391 * top of the previous character. */
2392 start = 0;
2393 cells = 0;
2394 for (i = 0; i < len; i += cl)
2395 {
2396 c = utf_ptr2char(s + i);
2397 cn = utf_char2cells(c);
2398 if (cn > 1
2399# ifdef FEAT_XFONTSET
2400 && fontset == NOFONTSET
2401# endif
2402 && gui.wide_font != NOFONT)
Bram Moolenaar45933962013-01-23 17:43:57 +01002403 curr_wide = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 else
Bram Moolenaar45933962013-01-23 17:43:57 +01002405 curr_wide = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 comping = utf_iscomposing(c);
2407 if (!comping) /* count cells from non-composing chars */
2408 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002409 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (cl == 0) /* hit end of string */
2411 len = i + cl; /* len must be wrong "cannot happen" */
2412
Bram Moolenaar45933962013-01-23 17:43:57 +01002413 wide_changed = curr_wide != prev_wide;
2414
2415 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * a composing character. */
Bram Moolenaar45933962013-01-23 17:43:57 +01002417 if (i + cl >= len || (comping && i > start) || wide_changed
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002418# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 || (cn > 1
2420# ifdef FEAT_XFONTSET
2421 /* No fontset: At least draw char after wide char at
2422 * right position. */
2423 && fontset == NOFONTSET
2424# endif
2425 )
2426# endif
2427 )
2428 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002429 if (comping || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 thislen = i - start;
2431 else
2432 thislen = i - start + cl;
2433 if (thislen > 0)
2434 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002435 if (prev_wide)
2436 gui_mch_set_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2438 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002439 if (prev_wide)
2440 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 start += thislen;
2442 }
2443 scol += cells;
2444 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002445 /* Adjust to not draw a character which width is changed
2446 * against with last one. */
2447 if (wide_changed && !comping)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002449 scol -= cn;
2450 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
2452
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002453# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002454 /* No fontset: draw a space to fill the gap after a wide char
2455 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2457# ifdef FEAT_XFONTSET
2458 && fontset == NOFONTSET
2459# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002460 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2462 1, draw_flags);
2463# endif
2464 }
2465 /* Draw a composing char on top of the previous char. */
2466 if (comping)
2467 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002468# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002469 /* Carbon ATSUI autodraws composing char over previous char */
2470 gui_mch_draw_string(gui.row, scol, s + i, cl,
2471 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002472# else
2473 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2474 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 start = i + cl;
2477 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002478 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480 /* The stuff below assumes "len" is the length in screen columns. */
2481 len = scol - col;
2482 }
2483 else
2484# endif
2485 {
2486 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2487# ifdef FEAT_MBYTE
2488 if (enc_dbcs == DBCS_JPNU)
2489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /* Get the length in display cells, this can be different from the
2491 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002492 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494# endif
2495 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002496#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2499 gui.col = col + len;
2500
2501 /* May need to invert it when it's part of the selection. */
2502 if (flags & GUI_MON_NOCLEAR)
2503 clip_may_redraw_selection(gui.row, col, len);
2504
2505 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2506 {
2507 /* Invalidate the old physical cursor position if we wrote over it */
2508 if (gui.cursor_row == gui.row
2509 && gui.cursor_col >= col
2510 && gui.cursor_col < col + len)
2511 gui.cursor_is_valid = FALSE;
2512 }
2513
2514#ifdef FEAT_SIGN_ICONS
2515 if (draw_sign)
2516 /* Draw the sign on top of the spaces. */
2517 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002518# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002519 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (multi_sign)
2521 netbeans_draw_multisign_indicator(gui.row);
2522# endif
2523#endif
2524
2525 return OK;
2526}
2527
2528/*
2529 * Un-draw the cursor. Actually this just redraws the character at the given
2530 * position. The character just before it too, for when it was in bold.
2531 */
2532 void
2533gui_undraw_cursor()
2534{
2535 if (gui.cursor_is_valid)
2536 {
2537#ifdef FEAT_HANGULIN
2538 if (composing_hangul
2539 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2540 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2541 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2542 gui.norm_pixel, gui.back_pixel, 0);
2543 else
2544 {
2545#endif
2546 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2547 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2548 && gui.cursor_col > 0)
2549 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2550 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2551#ifdef FEAT_HANGULIN
2552 if (composing_hangul)
2553 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2554 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2555 }
2556#endif
2557 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2558 * here in case it wasn't needed to undraw it. */
2559 gui.cursor_is_valid = FALSE;
2560 }
2561}
2562
2563 void
2564gui_redraw(x, y, w, h)
2565 int x;
2566 int y;
2567 int w;
2568 int h;
2569{
2570 int row1, col1, row2, col2;
2571
2572 row1 = Y_2_ROW(y);
2573 col1 = X_2_COL(x);
2574 row2 = Y_2_ROW(y + h - 1);
2575 col2 = X_2_COL(x + w - 1);
2576
2577 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2578
2579 /*
2580 * We may need to redraw the cursor, but don't take it upon us to change
2581 * its location after a scroll.
2582 * (maybe be more strict even and test col too?)
2583 * These things may be outside the update/clipping region and reality may
2584 * not reflect Vims internal ideas if these operations are clipped away.
2585 */
2586 if (gui.row == gui.cursor_row)
2587 gui_update_cursor(TRUE, TRUE);
2588}
2589
2590/*
2591 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2592 * from col1 to col2 (inclusive).
2593 * Return TRUE when the character before the first drawn character has
2594 * different attributes (may have to be redrawn too).
2595 */
2596 int
2597gui_redraw_block(row1, col1, row2, col2, flags)
2598 int row1;
2599 int col1;
2600 int row2;
2601 int col2;
2602 int flags; /* flags for gui_outstr_nowrap() */
2603{
2604 int old_row, old_col;
2605 long_u old_hl_mask;
2606 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002607 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 int idx, len;
2609 int back, nback;
2610 int retval = FALSE;
2611#ifdef FEAT_MBYTE
2612 int orig_col1, orig_col2;
2613#endif
2614
2615 /* Don't try to update when ScreenLines is not valid */
2616 if (!screen_cleared || ScreenLines == NULL)
2617 return retval;
2618
2619 /* Don't try to draw outside the shell! */
2620 /* Check everything, strange values may be caused by a big border width */
2621 col1 = check_col(col1);
2622 col2 = check_col(col2);
2623 row1 = check_row(row1);
2624 row2 = check_row(row2);
2625
2626 /* Remember where our cursor was */
2627 old_row = gui.row;
2628 old_col = gui.col;
2629 old_hl_mask = gui.highlight_mask;
2630#ifdef FEAT_MBYTE
2631 orig_col1 = col1;
2632 orig_col2 = col2;
2633#endif
2634
2635 for (gui.row = row1; gui.row <= row2; gui.row++)
2636 {
2637#ifdef FEAT_MBYTE
2638 /* When only half of a double-wide character is in the block, include
2639 * the other half. */
2640 col1 = orig_col1;
2641 col2 = orig_col2;
2642 off = LineOffset[gui.row];
2643 if (enc_dbcs != 0)
2644 {
2645 if (col1 > 0)
2646 col1 -= dbcs_screen_head_off(ScreenLines + off,
2647 ScreenLines + off + col1);
2648 col2 += dbcs_screen_tail_off(ScreenLines + off,
2649 ScreenLines + off + col2);
2650 }
2651 else if (enc_utf8)
2652 {
2653 if (ScreenLines[off + col1] == 0)
2654 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002655# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2657 ++col2;
2658# endif
2659 }
2660#endif
2661 gui.col = col1;
2662 off = LineOffset[gui.row] + gui.col;
2663 len = col2 - col1 + 1;
2664
2665 /* Find how many chars back this highlighting starts, or where a space
2666 * is. Needed for when the bold trick is used */
2667 for (back = 0; back < col1; ++back)
2668 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2669 || ScreenLines[off - 1 - back] == ' ')
2670 break;
2671 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2672 && ScreenLines[off - 1] != ' ');
2673
2674 /* Break it up in strings of characters with the same attributes. */
2675 /* Print UTF-8 characters individually. */
2676 while (len > 0)
2677 {
2678 first_attr = ScreenAttrs[off];
2679 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002680#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (enc_utf8 && ScreenLinesUC[off] != 0)
2682 {
2683 /* output multi-byte character separately */
2684 nback = gui_screenchar(off, flags,
2685 (guicolor_T)0, (guicolor_T)0, back);
2686 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2687 idx = 2;
2688 else
2689 idx = 1;
2690 }
2691 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2692 {
2693 /* output double-byte, single-width character separately */
2694 nback = gui_screenchar(off, flags,
2695 (guicolor_T)0, (guicolor_T)0, back);
2696 idx = 1;
2697 }
2698 else
2699#endif
2700 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002701#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 for (idx = 0; idx < len; ++idx)
2703 {
2704 if (enc_utf8 && ScreenLines[off + idx] == 0)
2705 continue; /* skip second half of double-width char */
2706 if (ScreenAttrs[off + idx] != first_attr)
2707 break;
2708 }
2709 /* gui_screenstr() takes care of multibyte chars */
2710 nback = gui_screenstr(off, idx, flags,
2711 (guicolor_T)0, (guicolor_T)0, back);
2712#else
2713 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2714 idx++)
2715 {
2716# ifdef FEAT_MBYTE
2717 /* Stop at a multi-byte Unicode character. */
2718 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2719 break;
2720 if (enc_dbcs == DBCS_JPNU)
2721 {
2722 /* Stop at a double-byte single-width char. */
2723 if (ScreenLines[off + idx] == 0x8e)
2724 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002725 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 + off + idx) == 2)
2727 ++idx; /* skip second byte of double-byte char */
2728 }
2729# endif
2730 }
2731 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2732 (guicolor_T)0, (guicolor_T)0, back);
2733#endif
2734 }
2735 if (nback == FAIL)
2736 {
2737 /* Must back up to start drawing where a bold or italic word
2738 * starts. */
2739 off -= back;
2740 len += back;
2741 gui.col -= back;
2742 }
2743 else
2744 {
2745 off += idx;
2746 len -= idx;
2747 }
2748 back = 0;
2749 }
2750 }
2751
2752 /* Put the cursor back where it was */
2753 gui.row = old_row;
2754 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002755 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 return retval;
2758}
2759
2760 static void
2761gui_delete_lines(row, count)
2762 int row;
2763 int count;
2764{
2765 if (count <= 0)
2766 return;
2767
2768 if (row + count > gui.scroll_region_bot)
2769 /* Scrolled out of region, just blank the lines out */
2770 gui_clear_block(row, gui.scroll_region_left,
2771 gui.scroll_region_bot, gui.scroll_region_right);
2772 else
2773 {
2774 gui_mch_delete_lines(row, count);
2775
2776 /* If the cursor was in the deleted lines it's now gone. If the
2777 * cursor was in the scrolled lines adjust its position. */
2778 if (gui.cursor_row >= row
2779 && gui.cursor_col >= gui.scroll_region_left
2780 && gui.cursor_col <= gui.scroll_region_right)
2781 {
2782 if (gui.cursor_row < row + count)
2783 gui.cursor_is_valid = FALSE;
2784 else if (gui.cursor_row <= gui.scroll_region_bot)
2785 gui.cursor_row -= count;
2786 }
2787 }
2788}
2789
2790 static void
2791gui_insert_lines(row, count)
2792 int row;
2793 int count;
2794{
2795 if (count <= 0)
2796 return;
2797
2798 if (row + count > gui.scroll_region_bot)
2799 /* Scrolled out of region, just blank the lines out */
2800 gui_clear_block(row, gui.scroll_region_left,
2801 gui.scroll_region_bot, gui.scroll_region_right);
2802 else
2803 {
2804 gui_mch_insert_lines(row, count);
2805
2806 if (gui.cursor_row >= gui.row
2807 && gui.cursor_col >= gui.scroll_region_left
2808 && gui.cursor_col <= gui.scroll_region_right)
2809 {
2810 if (gui.cursor_row <= gui.scroll_region_bot - count)
2811 gui.cursor_row += count;
2812 else if (gui.cursor_row <= gui.scroll_region_bot)
2813 gui.cursor_is_valid = FALSE;
2814 }
2815 }
2816}
2817
2818/*
2819 * The main GUI input routine. Waits for a character from the keyboard.
2820 * wtime == -1 Wait forever.
2821 * wtime == 0 Don't wait.
2822 * wtime > 0 Wait wtime milliseconds for a character.
2823 * Returns OK if a character was found to be available within the given time,
2824 * or FAIL otherwise.
2825 */
2826 int
2827gui_wait_for_chars(wtime)
2828 long wtime;
2829{
2830 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002832#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 /*
2834 * If we're going to wait a bit, update the menus and mouse shape for the
2835 * current State.
2836 */
2837 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 gui_update_menus(0);
2839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 gui_mch_update();
2842 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847 /* Before waiting, flush any output to the screen. */
2848 gui_mch_flush();
2849
2850 if (wtime > 0)
2851 {
2852 /* Blink when waiting for a character. Probably only does something
2853 * for showmatch() */
2854 gui_mch_start_blink();
2855 retval = gui_mch_wait_for_chars(wtime);
2856 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 return retval;
2858 }
2859
2860 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002861 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
2863 gui_mch_start_blink();
2864
Bram Moolenaar3918c952005-03-15 22:34:55 +00002865 retval = FAIL;
2866 /*
2867 * We may want to trigger the CursorHold event. First wait for
2868 * 'updatetime' and if nothing is typed within that time put the
2869 * K_CURSORHOLD key in the input buffer.
2870 */
2871 if (gui_mch_wait_for_chars(p_ut) == OK)
2872 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002874 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002876 char_u buf[3];
2877
2878 /* Put K_CURSORHOLD in the input buffer. */
2879 buf[0] = CSI;
2880 buf[1] = KS_EXTRA;
2881 buf[2] = (int)KE_CURSORHOLD;
2882 add_to_input_buf(buf, 3);
2883
2884 retval = OK;
2885 }
2886#endif
2887
2888 if (retval == FAIL)
2889 {
2890 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002891 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
2895 gui_mch_stop_blink();
2896 return retval;
2897}
2898
2899/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002900 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
2902 static void
2903fill_mouse_coord(p, col, row)
2904 char_u *p;
2905 int col;
2906 int row;
2907{
2908 p[0] = (char_u)(col / 128 + ' ' + 1);
2909 p[1] = (char_u)(col % 128 + ' ' + 1);
2910 p[2] = (char_u)(row / 128 + ' ' + 1);
2911 p[3] = (char_u)(row % 128 + ' ' + 1);
2912}
2913
2914/*
2915 * Generic mouse support function. Add a mouse event to the input buffer with
2916 * the given properties.
2917 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2918 * MOUSE_X1, MOUSE_X2
2919 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002920 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2921 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 * x, y --- Coordinates of mouse in pixels.
2923 * repeated_click --- TRUE if this click comes only a short time after a
2924 * previous click.
2925 * modifiers --- Bit field which may be any of the following modifiers
2926 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2927 * This function will ignore drag events where the mouse has not moved to a new
2928 * character.
2929 */
2930 void
2931gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2932 int button;
2933 int x;
2934 int y;
2935 int repeated_click;
2936 int_u modifiers;
2937{
2938 static int prev_row = 0, prev_col = 0;
2939 static int prev_button = -1;
2940 static int num_clicks = 1;
2941 char_u string[10];
2942 enum key_extra button_char;
2943 int row, col;
2944#ifdef FEAT_CLIPBOARD
2945 int checkfor;
2946 int did_clip = FALSE;
2947#endif
2948
2949 /*
2950 * Scrolling may happen at any time, also while a selection is present.
2951 */
2952 switch (button)
2953 {
2954 case MOUSE_X1:
2955 button_char = KE_X1MOUSE;
2956 goto button_set;
2957 case MOUSE_X2:
2958 button_char = KE_X2MOUSE;
2959 goto button_set;
2960 case MOUSE_4:
2961 button_char = KE_MOUSEDOWN;
2962 goto button_set;
2963 case MOUSE_5:
2964 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002965 goto button_set;
2966 case MOUSE_6:
2967 button_char = KE_MOUSELEFT;
2968 goto button_set;
2969 case MOUSE_7:
2970 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971button_set:
2972 {
2973 /* Don't put events in the input queue now. */
2974 if (hold_gui_events)
2975 return;
2976
2977 string[3] = CSI;
2978 string[4] = KS_EXTRA;
2979 string[5] = (int)button_char;
2980
2981 /* Pass the pointer coordinates of the scroll event so that we
2982 * know which window to scroll. */
2983 row = gui_xy2colrow(x, y, &col);
2984 string[6] = (char_u)(col / 128 + ' ' + 1);
2985 string[7] = (char_u)(col % 128 + ' ' + 1);
2986 string[8] = (char_u)(row / 128 + ' ' + 1);
2987 string[9] = (char_u)(row % 128 + ' ' + 1);
2988
2989 if (modifiers == 0)
2990 add_to_input_buf(string + 3, 7);
2991 else
2992 {
2993 string[0] = CSI;
2994 string[1] = KS_MODIFIER;
2995 string[2] = 0;
2996 if (modifiers & MOUSE_SHIFT)
2997 string[2] |= MOD_MASK_SHIFT;
2998 if (modifiers & MOUSE_CTRL)
2999 string[2] |= MOD_MASK_CTRL;
3000 if (modifiers & MOUSE_ALT)
3001 string[2] |= MOD_MASK_ALT;
3002 add_to_input_buf(string, 10);
3003 }
3004 return;
3005 }
3006 }
3007
3008#ifdef FEAT_CLIPBOARD
3009 /* If a clipboard selection is in progress, handle it */
3010 if (clip_star.state == SELECT_IN_PROGRESS)
3011 {
3012 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3013 return;
3014 }
3015
3016 /* Determine which mouse settings to look for based on the current mode */
3017 switch (get_real_state())
3018 {
3019 case NORMAL_BUSY:
3020 case OP_PENDING:
3021 case NORMAL: checkfor = MOUSE_NORMAL; break;
3022 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003023 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 case REPLACE:
3025 case REPLACE+LANGMAP:
3026#ifdef FEAT_VREPLACE
3027 case VREPLACE:
3028 case VREPLACE+LANGMAP:
3029#endif
3030 case INSERT:
3031 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3032 case ASKMORE:
3033 case HITRETURN: /* At the more- and hit-enter prompt pass the
3034 mouse event for a click on or below the
3035 message line. */
3036 if (Y_2_ROW(y) >= msg_row)
3037 checkfor = MOUSE_NORMAL;
3038 else
3039 checkfor = MOUSE_RETURN;
3040 break;
3041
3042 /*
3043 * On the command line, use the clipboard selection on all lines
3044 * but the command line. But not when pasting.
3045 */
3046 case CMDLINE:
3047 case CMDLINE+LANGMAP:
3048 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3049 checkfor = MOUSE_NONE;
3050 else
3051 checkfor = MOUSE_COMMAND;
3052 break;
3053
3054 default:
3055 checkfor = MOUSE_NONE;
3056 break;
3057 };
3058
3059 /*
3060 * Allow clipboard selection of text on the command line in "normal"
3061 * modes. Don't do this when dragging the status line, or extending a
3062 * Visual selection.
3063 */
3064 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
3065 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00003066# ifdef FEAT_WINDOWS
3067 + firstwin->w_winrow
3068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 && button != MOUSE_DRAG
3070# ifdef FEAT_MOUSESHAPE
3071 && !drag_status_line
3072# ifdef FEAT_VERTSPLIT
3073 && !drag_sep_line
3074# endif
3075# endif
3076 )
3077 checkfor = MOUSE_NONE;
3078
3079 /*
3080 * Use modeless selection when holding CTRL and SHIFT pressed.
3081 */
3082 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3083 checkfor = MOUSE_NONEF;
3084
3085 /*
3086 * In Ex mode, always use modeless selection.
3087 */
3088 if (exmode_active)
3089 checkfor = MOUSE_NONE;
3090
3091 /*
3092 * If the mouse settings say to not use the mouse, use the modeless
3093 * selection. But if Visual is active, assume that only the Visual area
3094 * will be selected.
3095 * Exception: On the command line, both the selection is used and a mouse
3096 * key is send.
3097 */
3098 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3099 {
3100#ifdef FEAT_VISUAL
3101 /* Don't do modeless selection in Visual mode. */
3102 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3103 return;
3104#endif
3105
3106 /*
3107 * When 'mousemodel' is "popup", shift-left is translated to right.
3108 * But not when also using Ctrl.
3109 */
3110 if (mouse_model_popup() && button == MOUSE_LEFT
3111 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3112 {
3113 button = MOUSE_RIGHT;
3114 modifiers &= ~ MOUSE_SHIFT;
3115 }
3116
3117 /* If the selection is done, allow the right button to extend it.
3118 * If the selection is cleared, allow the right button to start it
3119 * from the cursor position. */
3120 if (button == MOUSE_RIGHT)
3121 {
3122 if (clip_star.state == SELECT_CLEARED)
3123 {
3124 if (State & CMDLINE)
3125 {
3126 col = msg_col;
3127 row = msg_row;
3128 }
3129 else
3130 {
3131 col = curwin->w_wcol;
3132 row = curwin->w_wrow + W_WINROW(curwin);
3133 }
3134 clip_start_selection(col, row, FALSE);
3135 }
3136 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3137 repeated_click);
3138 did_clip = TRUE;
3139 }
3140 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003141 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3144 did_clip = TRUE;
3145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 /* Always allow pasting */
3148 if (button != MOUSE_MIDDLE)
3149 {
3150 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3151 return;
3152 if (checkfor != MOUSE_COMMAND)
3153 button = MOUSE_LEFT;
3154 }
3155 repeated_click = FALSE;
3156 }
3157
3158 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003159 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160#endif
3161
3162 /* Don't put events in the input queue now. */
3163 if (hold_gui_events)
3164 return;
3165
3166 row = gui_xy2colrow(x, y, &col);
3167
3168 /*
3169 * If we are dragging and the mouse hasn't moved far enough to be on a
3170 * different character, then don't send an event to vim.
3171 */
3172 if (button == MOUSE_DRAG)
3173 {
3174 if (row == prev_row && col == prev_col)
3175 return;
3176 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3177 if (y < 0)
3178 row = -1;
3179 }
3180
3181 /*
3182 * If topline has changed (window scrolled) since the last click, reset
3183 * repeated_click, because we don't want starting Visual mode when
3184 * clicking on a different character in the text.
3185 */
3186 if (curwin->w_topline != gui_prev_topline
3187#ifdef FEAT_DIFF
3188 || curwin->w_topfill != gui_prev_topfill
3189#endif
3190 )
3191 repeated_click = FALSE;
3192
3193 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3194 string[1] = KS_MOUSE;
3195 string[2] = KE_FILLER;
3196 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3197 {
3198 if (repeated_click)
3199 {
3200 /*
3201 * Handle multiple clicks. They only count if the mouse is still
3202 * pointing at the same character.
3203 */
3204 if (button != prev_button || row != prev_row || col != prev_col)
3205 num_clicks = 1;
3206 else if (++num_clicks > 4)
3207 num_clicks = 1;
3208 }
3209 else
3210 num_clicks = 1;
3211 prev_button = button;
3212 gui_prev_topline = curwin->w_topline;
3213#ifdef FEAT_DIFF
3214 gui_prev_topfill = curwin->w_topfill;
3215#endif
3216
3217 string[3] = (char_u)(button | 0x20);
3218 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3219 }
3220 else
3221 string[3] = (char_u)button;
3222
3223 string[3] |= modifiers;
3224 fill_mouse_coord(string + 4, col, row);
3225 add_to_input_buf(string, 8);
3226
3227 if (row < 0)
3228 prev_row = 0;
3229 else
3230 prev_row = row;
3231 prev_col = col;
3232
3233 /*
3234 * We need to make sure this is cleared since Athena doesn't tell us when
3235 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3236 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003237#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 gui.dragged_sb = SBAR_NONE;
3239#endif
3240}
3241
3242/*
3243 * Convert x and y coordinate to column and row in text window.
3244 * Corrects for multi-byte character.
3245 * returns column in "*colp" and row as return value;
3246 */
3247 int
3248gui_xy2colrow(x, y, colp)
3249 int x;
3250 int y;
3251 int *colp;
3252{
3253 int col = check_col(X_2_COL(x));
3254 int row = check_row(Y_2_ROW(y));
3255
3256#ifdef FEAT_MBYTE
3257 *colp = mb_fix_col(col, row);
3258#else
3259 *colp = col;
3260#endif
3261 return row;
3262}
3263
3264#if defined(FEAT_MENU) || defined(PROTO)
3265/*
3266 * Callback function for when a menu entry has been selected.
3267 */
3268 void
3269gui_menu_cb(menu)
3270 vimmenu_T *menu;
3271{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003272 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 /* Don't put events in the input queue now. */
3275 if (hold_gui_events)
3276 return;
3277
3278 bytes[0] = CSI;
3279 bytes[1] = KS_MENU;
3280 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003281 add_to_input_buf(bytes, 3);
3282 add_long_to_buf((long_u)menu, bytes);
3283 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285#endif
3286
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003287static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289/*
3290 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003291 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 * in p_go.
3293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 void
3295gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003296 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#ifdef FEAT_MENU
3299 static int prev_menu_is_active = -1;
3300#endif
3301#ifdef FEAT_TOOLBAR
3302 static int prev_toolbar = -1;
3303 int using_toolbar = FALSE;
3304#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003305#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003306 int using_tabline;
3307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_FOOTER
3309 static int prev_footer = -1;
3310 int using_footer = FALSE;
3311#endif
3312#if defined(FEAT_MENU) && !defined(WIN16)
3313 static int prev_tearoff = -1;
3314 int using_tearoff = FALSE;
3315#endif
3316
3317 char_u *p;
3318 int i;
3319#ifdef FEAT_MENU
3320 int grey_old, grey_new;
3321 char_u *temp;
3322#endif
3323 win_T *wp;
3324 int need_set_size;
3325 int fix_size;
3326
3327#ifdef FEAT_MENU
3328 if (oldval != NULL && gui.in_use)
3329 {
3330 /*
3331 * Check if the menu's go from grey to non-grey or vise versa.
3332 */
3333 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3334 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3335 if (grey_old != grey_new)
3336 {
3337 temp = p_go;
3338 p_go = oldval;
3339 gui_update_menus(MENU_ALL_MODES);
3340 p_go = temp;
3341 }
3342 }
3343 gui.menu_is_active = FALSE;
3344#endif
3345
3346 for (i = 0; i < 3; i++)
3347 gui.which_scrollbars[i] = FALSE;
3348 for (p = p_go; *p; p++)
3349 switch (*p)
3350 {
3351 case GO_LEFT:
3352 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3353 break;
3354 case GO_RIGHT:
3355 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3356 break;
3357#ifdef FEAT_VERTSPLIT
3358 case GO_VLEFT:
3359 if (win_hasvertsplit())
3360 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3361 break;
3362 case GO_VRIGHT:
3363 if (win_hasvertsplit())
3364 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3365 break;
3366#endif
3367 case GO_BOT:
3368 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3369 break;
3370#ifdef FEAT_MENU
3371 case GO_MENUS:
3372 gui.menu_is_active = TRUE;
3373 break;
3374#endif
3375 case GO_GREY:
3376 /* make menu's have grey items, ignored here */
3377 break;
3378#ifdef FEAT_TOOLBAR
3379 case GO_TOOLBAR:
3380 using_toolbar = TRUE;
3381 break;
3382#endif
3383#ifdef FEAT_FOOTER
3384 case GO_FOOTER:
3385 using_footer = TRUE;
3386 break;
3387#endif
3388 case GO_TEAROFF:
3389#if defined(FEAT_MENU) && !defined(WIN16)
3390 using_tearoff = TRUE;
3391#endif
3392 break;
3393 default:
3394 /* Ignore options that are not supported */
3395 break;
3396 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (gui.in_use)
3399 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003400 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003402
3403#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003404 /* Update the GUI tab line, it may appear or disappear. This may
3405 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003406 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003407 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003408 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003409 /* We don't want a resize event change "Rows" here, save and
3410 * restore it. Resizing is handled below. */
3411 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003412 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003413 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003414 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003415 if (using_tabline)
3416 fix_size = TRUE;
3417 if (!gui_use_tabline())
3418 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3419 }
3420#endif
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 for (i = 0; i < 3; i++)
3423 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003424 /* The scrollbar needs to be updated when it is shown/unshown and
3425 * when switching tab pages. But the size only changes when it's
3426 * shown/unshown. Thus we need two places to remember whether a
3427 * scrollbar is there or not. */
3428 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003429#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003430 || gui.which_scrollbars[i]
3431 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003432#endif
3433 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
3435 if (i == SBAR_BOTTOM)
3436 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3437 gui.which_scrollbars[i]);
3438 else
3439 {
3440 FOR_ALL_WINDOWS(wp)
3441 {
3442 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3443 }
3444 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003445 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3446 {
3447 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003448 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003449 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003450 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003451 if (gui.which_scrollbars[i])
3452 fix_size = TRUE;
3453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003455#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003456 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003457#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003458 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460
3461#ifdef FEAT_MENU
3462 if (gui.menu_is_active != prev_menu_is_active)
3463 {
3464 /* We don't want a resize event change "Rows" here, save and
3465 * restore it. Resizing is handled below. */
3466 i = Rows;
3467 gui_mch_enable_menu(gui.menu_is_active);
3468 Rows = i;
3469 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003470 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (gui.menu_is_active)
3472 fix_size = TRUE;
3473 }
3474#endif
3475
3476#ifdef FEAT_TOOLBAR
3477 if (using_toolbar != prev_toolbar)
3478 {
3479 gui_mch_show_toolbar(using_toolbar);
3480 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003481 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (using_toolbar)
3483 fix_size = TRUE;
3484 }
3485#endif
3486#ifdef FEAT_FOOTER
3487 if (using_footer != prev_footer)
3488 {
3489 gui_mch_enable_footer(using_footer);
3490 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003491 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (using_footer)
3493 fix_size = TRUE;
3494 }
3495#endif
3496#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3497 if (using_tearoff != prev_tearoff)
3498 {
3499 gui_mch_toggle_tearoffs(using_tearoff);
3500 prev_tearoff = using_tearoff;
3501 }
3502#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003503 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003504 {
3505#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003506 long prev_Columns = Columns;
3507 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 /* Adjust the size of the window to make the text area keep the
3510 * same size and to avoid that part of our window is off-screen
3511 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003512 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003513
3514#ifdef FEAT_GUI_GTK
3515 /* GTK has the annoying habit of sending us resize events when
3516 * changing the window size ourselves. This mostly happens when
3517 * waiting for a character to arrive, quite unpredictably, and may
3518 * change Columns and Rows when we don't want it. Wait for a
3519 * character here to avoid this effect.
3520 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003521 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003522 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003523 * Don't change Rows when adding menu/toolbar/tabline.
3524 * Don't change Columns when adding vertical toolbar. */
3525 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003526 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003527 if ((need_set_size & RESIZE_VERT) == 0)
3528 Rows = prev_Rows;
3529 if ((need_set_size & RESIZE_HOR) == 0)
3530 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003531#endif
3532 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003533#ifdef FEAT_WINDOWS
3534 /* When the console tabline appears or disappears the window positions
3535 * change. */
3536 if (firstwin->w_winrow != tabline_height())
3537 shell_new_rows(); /* recompute window positions and heights */
3538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540}
3541
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003542#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3543/*
3544 * Return TRUE if the GUI is taking care of the tabline.
3545 * It may still be hidden if 'showtabline' is zero.
3546 */
3547 int
3548gui_use_tabline()
3549{
3550 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3551}
3552
3553/*
3554 * Return TRUE if the GUI is showing the tabline.
3555 * This uses 'showtabline'.
3556 */
3557 static int
3558gui_has_tabline()
3559{
3560 if (!gui_use_tabline()
3561 || p_stal == 0
3562 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3563 return FALSE;
3564 return TRUE;
3565}
3566
3567/*
3568 * Update the tabline.
3569 * This may display/undisplay the tabline and update the labels.
3570 */
3571 void
3572gui_update_tabline()
3573{
3574 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003575 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003576
3577 if (!gui.starting && starting == 0)
3578 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003579 /* Updating the tabline uses direct GUI commands, flush
3580 * outstanding instructions first. (esp. clear screen) */
3581 out_flush();
3582 gui_mch_flush();
3583
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003584 if (!showit != !shown)
3585 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586 if (showit != 0)
3587 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003588
3589 /* When the tabs change from hidden to shown or from shown to
3590 * hidden the size of the text area should remain the same. */
3591 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003592 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003593 }
3594}
3595
3596/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003597 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003598 */
3599 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003600get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003601 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003602 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603{
3604 int modified = FALSE;
3605 char_u buf[40];
3606 int wincount;
3607 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003608 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003609
Bram Moolenaar57657d82006-04-21 22:12:41 +00003610 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003611 opt = (tooltip ? &p_gtt : &p_gtl);
3612 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003613 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003614 int use_sandbox = FALSE;
3615 int save_called_emsg = called_emsg;
3616 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003617 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003618 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3619 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003620
3621 called_emsg = FALSE;
3622
3623 printer_page_num = tabpage_index(tp);
3624# ifdef FEAT_EVAL
3625 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003626 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003627# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003628 /* It's almost as going to the tabpage, but without autocommands. */
3629 curtab->tp_firstwin = firstwin;
3630 curtab->tp_lastwin = lastwin;
3631 curtab->tp_curwin = curwin;
3632 save_curtab = curtab;
3633 curtab = tp;
3634 topframe = curtab->tp_topframe;
3635 firstwin = curtab->tp_firstwin;
3636 lastwin = curtab->tp_lastwin;
3637 curwin = curtab->tp_curwin;
3638 curbuf = curwin->w_buffer;
3639
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003640 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003641 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003642 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003643 STRCPY(NameBuff, res);
3644
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003645 /* Back to the original curtab. */
3646 curtab = save_curtab;
3647 topframe = curtab->tp_topframe;
3648 firstwin = curtab->tp_firstwin;
3649 lastwin = curtab->tp_lastwin;
3650 curwin = curtab->tp_curwin;
3651 curbuf = curwin->w_buffer;
3652
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003653 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003654 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003655 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003656 called_emsg |= save_called_emsg;
3657 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003658
3659 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3660 * use a default label. */
3661 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003662 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003663 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003664 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003665 if (!tooltip)
3666 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003667
3668 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3669 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3670 if (bufIsChanged(wp->w_buffer))
3671 modified = TRUE;
3672 if (modified || wincount > 1)
3673 {
3674 if (wincount > 1)
3675 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3676 else
3677 buf[0] = NUL;
3678 if (modified)
3679 STRCAT(buf, "+");
3680 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003681 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003682 mch_memmove(NameBuff, buf, STRLEN(buf));
3683 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684 }
3685}
3686
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003687/*
3688 * Send the event for clicking to select tab page "nr".
3689 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003690 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003691 */
3692 int
3693send_tabline_event(nr)
3694 int nr;
3695{
3696 char_u string[3];
3697
3698 if (nr == tabpage_index(curtab))
3699 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003700
3701 /* Don't put events in the input queue now. */
3702 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003703# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003704 || cmdwin_type != 0
3705# endif
3706 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003707 {
3708 /* Set it back to the current tab page. */
3709 gui_mch_set_curtab(tabpage_index(curtab));
3710 return FALSE;
3711 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003712
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003713 string[0] = CSI;
3714 string[1] = KS_TABLINE;
3715 string[2] = KE_FILLER;
3716 add_to_input_buf(string, 3);
3717 string[0] = nr;
3718 add_to_input_buf_csi(string, 1);
3719 return TRUE;
3720}
3721
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003722/*
3723 * Send a tabline menu event
3724 */
3725 void
3726send_tabline_menu_event(tabidx, event)
3727 int tabidx;
3728 int event;
3729{
3730 char_u string[3];
3731
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003732 /* Don't put events in the input queue now. */
3733 if (hold_gui_events)
3734 return;
3735
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003736 string[0] = CSI;
3737 string[1] = KS_TABMENU;
3738 string[2] = KE_FILLER;
3739 add_to_input_buf(string, 3);
3740 string[0] = tabidx;
3741 string[1] = (char_u)(long)event;
3742 add_to_input_buf_csi(string, 2);
3743}
3744
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
3747/*
3748 * Scrollbar stuff:
3749 */
3750
Bram Moolenaare45828b2006-02-15 22:12:56 +00003751#if defined(FEAT_WINDOWS) || defined(PROTO)
3752/*
3753 * Remove all scrollbars. Used before switching to another tab page.
3754 */
3755 void
3756gui_remove_scrollbars()
3757{
3758 int i;
3759 win_T *wp;
3760
3761 for (i = 0; i < 3; i++)
3762 {
3763 if (i == SBAR_BOTTOM)
3764 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3765 else
3766 {
3767 FOR_ALL_WINDOWS(wp)
3768 {
3769 gui_do_scrollbar(wp, i, FALSE);
3770 }
3771 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003772 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003773 }
3774}
3775#endif
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 void
3778gui_create_scrollbar(sb, type, wp)
3779 scrollbar_T *sb;
3780 int type;
3781 win_T *wp;
3782{
3783 static int sbar_ident = 0;
3784
3785 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3786 sb->wp = wp;
3787 sb->type = type;
3788 sb->value = 0;
3789#ifdef FEAT_GUI_ATHENA
3790 sb->pixval = 0;
3791#endif
3792 sb->size = 1;
3793 sb->max = 1;
3794 sb->top = 0;
3795 sb->height = 0;
3796#ifdef FEAT_VERTSPLIT
3797 sb->width = 0;
3798#endif
3799 sb->status_height = 0;
3800 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3801}
3802
3803/*
3804 * Find the scrollbar with the given index.
3805 */
3806 scrollbar_T *
3807gui_find_scrollbar(ident)
3808 long ident;
3809{
3810 win_T *wp;
3811
3812 if (gui.bottom_sbar.ident == ident)
3813 return &gui.bottom_sbar;
3814 FOR_ALL_WINDOWS(wp)
3815 {
3816 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3817 return &wp->w_scrollbars[SBAR_LEFT];
3818 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3819 return &wp->w_scrollbars[SBAR_RIGHT];
3820 }
3821 return NULL;
3822}
3823
3824/*
3825 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3826 *
3827 * For Win32, Macintosh and GTK+ 2:
3828 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3829 * you stop dragging the scrollbar. We get here each time the scrollbar is
3830 * dragged another pixel, but as far as the rest of vim goes, it thinks
3831 * we're just hanging in the call to DispatchMessage() in
3832 * process_message(). The DispatchMessage() call that hangs was passed a
3833 * mouse button click event in the scrollbar window. -- webb.
3834 *
3835 * Solution: Do the scrolling right here. But only when allowed.
3836 * Ignore the scrollbars while executing an external command or when there
3837 * are still characters to be processed.
3838 */
3839 void
3840gui_drag_scrollbar(sb, value, still_dragging)
3841 scrollbar_T *sb;
3842 long value;
3843 int still_dragging;
3844{
3845#ifdef FEAT_WINDOWS
3846 win_T *wp;
3847#endif
3848 int sb_num;
3849#ifdef USE_ON_FLY_SCROLL
3850 colnr_T old_leftcol = curwin->w_leftcol;
3851# ifdef FEAT_SCROLLBIND
3852 linenr_T old_topline = curwin->w_topline;
3853# endif
3854# ifdef FEAT_DIFF
3855 int old_topfill = curwin->w_topfill;
3856# endif
3857#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003858 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 int byte_count;
3860#endif
3861
3862 if (sb == NULL)
3863 return;
3864
3865 /* Don't put events in the input queue now. */
3866 if (hold_gui_events)
3867 return;
3868
3869#ifdef FEAT_CMDWIN
3870 if (cmdwin_type != 0 && sb->wp != curwin)
3871 return;
3872#endif
3873
3874 if (still_dragging)
3875 {
3876 if (sb->wp == NULL)
3877 gui.dragged_sb = SBAR_BOTTOM;
3878 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3879 gui.dragged_sb = SBAR_LEFT;
3880 else
3881 gui.dragged_sb = SBAR_RIGHT;
3882 gui.dragged_wp = sb->wp;
3883 }
3884 else
3885 {
3886 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003887#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 /* Keep the "dragged_wp" value until after the scrolling, for when the
3889 * moust button is released. GTK2 doesn't send the button-up event. */
3890 gui.dragged_wp = NULL;
3891#endif
3892 }
3893
3894 /* Vertical sbar info is kept in the first sbar (the left one) */
3895 if (sb->wp != NULL)
3896 sb = &sb->wp->w_scrollbars[0];
3897
3898 /*
3899 * Check validity of value
3900 */
3901 if (value < 0)
3902 value = 0;
3903#ifdef SCROLL_PAST_END
3904 else if (value > sb->max)
3905 value = sb->max;
3906#else
3907 if (value > sb->max - sb->size + 1)
3908 value = sb->max - sb->size + 1;
3909#endif
3910
3911 sb->value = value;
3912
3913#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003914 /* When not allowed to do the scrolling right now, return.
3915 * This also checked input_available(), but that causes the first click in
3916 * a scrollbar to be ignored when Vim doesn't have focus. */
3917 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 return;
3919#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003920#ifdef FEAT_INS_EXPAND
3921 /* Disallow scrolling the current window when the completion popup menu is
3922 * visible. */
3923 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3924 return;
3925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927#ifdef FEAT_RIGHTLEFT
3928 if (sb->wp == NULL && curwin->w_p_rl)
3929 {
3930 value = sb->max + 1 - sb->size - value;
3931 if (value < 0)
3932 value = 0;
3933 }
3934#endif
3935
3936 if (sb->wp != NULL) /* vertical scrollbar */
3937 {
3938 sb_num = 0;
3939#ifdef FEAT_WINDOWS
3940 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3941 sb_num++;
3942 if (wp == NULL)
3943 return;
3944#else
3945 if (sb->wp != curwin)
3946 return;
3947#endif
3948
3949#ifdef USE_ON_FLY_SCROLL
3950 current_scrollbar = sb_num;
3951 scrollbar_value = value;
3952 if (State & NORMAL)
3953 {
3954 gui_do_scroll();
3955 setcursor();
3956 }
3957 else if (State & INSERT)
3958 {
3959 ins_scroll();
3960 setcursor();
3961 }
3962 else if (State & CMDLINE)
3963 {
3964 if (msg_scrolled == 0)
3965 {
3966 gui_do_scroll();
3967 redrawcmdline();
3968 }
3969 }
3970# ifdef FEAT_FOLDING
3971 /* Value may have been changed for closed fold. */
3972 sb->value = sb->wp->w_topline - 1;
3973# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003974
3975 /* When dragging one scrollbar and there is another one at the other
3976 * side move the thumb of that one too. */
3977 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3978 gui_mch_set_scrollbar_thumb(
3979 &sb->wp->w_scrollbars[
3980 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3981 ? SBAR_LEFT : SBAR_RIGHT],
3982 sb->value, sb->size, sb->max);
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#else
3985 bytes[0] = CSI;
3986 bytes[1] = KS_VER_SCROLLBAR;
3987 bytes[2] = KE_FILLER;
3988 bytes[3] = (char_u)sb_num;
3989 byte_count = 4;
3990#endif
3991 }
3992 else
3993 {
3994#ifdef USE_ON_FLY_SCROLL
3995 scrollbar_value = value;
3996
3997 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003998 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 else if (State & INSERT)
4000 ins_horscroll();
4001 else if (State & CMDLINE)
4002 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004003 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004005 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 redrawcmdline();
4007 }
4008 }
4009 if (old_leftcol != curwin->w_leftcol)
4010 {
4011 updateWindow(curwin); /* update window, status and cmdline */
4012 setcursor();
4013 }
4014#else
4015 bytes[0] = CSI;
4016 bytes[1] = KS_HOR_SCROLLBAR;
4017 bytes[2] = KE_FILLER;
4018 byte_count = 3;
4019#endif
4020 }
4021
4022#ifdef USE_ON_FLY_SCROLL
4023# ifdef FEAT_SCROLLBIND
4024 /*
4025 * synchronize other windows, as necessary according to 'scrollbind'
4026 */
4027 if (curwin->w_p_scb
4028 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4029 || (sb->wp == curwin && (curwin->w_topline != old_topline
4030# ifdef FEAT_DIFF
4031 || curwin->w_topfill != old_topfill
4032# endif
4033 ))))
4034 {
4035 do_check_scrollbind(TRUE);
4036 /* need to update the window right here */
4037 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4038 if (wp->w_redr_type > 0)
4039 updateWindow(wp);
4040 setcursor();
4041 }
4042# endif
4043 out_flush();
4044 gui_update_cursor(FALSE, TRUE);
4045#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004046 add_to_input_buf(bytes, byte_count);
4047 add_long_to_buf((long_u)value, bytes);
4048 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
4050}
4051
4052/*
4053 * Scrollbar stuff:
4054 */
4055
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004056#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004057/*
4058 * Called when something in the window layout has changed.
4059 */
4060 void
4061gui_may_update_scrollbars()
4062{
4063 if (gui.in_use && starting == 0)
4064 {
4065 out_flush();
4066 gui_init_which_components(NULL);
4067 gui_update_scrollbars(TRUE);
4068 }
4069 need_mouse_correct = TRUE;
4070}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004071#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 void
4074gui_update_scrollbars(force)
4075 int force; /* Force all scrollbars to get updated */
4076{
4077 win_T *wp;
4078 scrollbar_T *sb;
4079 long val, size, max; /* need 32 bits here */
4080 int which_sb;
4081 int h, y;
4082#ifdef FEAT_VERTSPLIT
4083 static win_T *prev_curwin = NULL;
4084#endif
4085
4086 /* Update the horizontal scrollbar */
4087 gui_update_horiz_scrollbar(force);
4088
4089#ifndef WIN3264
4090 /* Return straight away if there is neither a left nor right scrollbar.
4091 * On MS-Windows this is required anyway for scrollwheel messages. */
4092 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4093 return;
4094#endif
4095
4096 /*
4097 * Don't want to update a scrollbar while we're dragging it. But if we
4098 * have both a left and right scrollbar, and we drag one of them, we still
4099 * need to update the other one.
4100 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004101 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4102 && gui.which_scrollbars[SBAR_LEFT]
4103 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
4105 /*
4106 * If we have two scrollbars and one of them is being dragged, just
4107 * copy the scrollbar position from the dragged one to the other one.
4108 */
4109 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4110 if (gui.dragged_wp != NULL)
4111 gui_mch_set_scrollbar_thumb(
4112 &gui.dragged_wp->w_scrollbars[which_sb],
4113 gui.dragged_wp->w_scrollbars[0].value,
4114 gui.dragged_wp->w_scrollbars[0].size,
4115 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /* avoid that moving components around generates events */
4119 ++hold_gui_events;
4120
4121 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4122 {
4123 if (wp->w_buffer == NULL) /* just in case */
4124 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004125 /* Skip a scrollbar that is being dragged. */
4126 if (!force && (gui.dragged_sb == SBAR_LEFT
4127 || gui.dragged_sb == SBAR_RIGHT)
4128 && gui.dragged_wp == wp)
4129 continue;
4130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#ifdef SCROLL_PAST_END
4132 max = wp->w_buffer->b_ml.ml_line_count - 1;
4133#else
4134 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4135#endif
4136 if (max < 0) /* empty buffer */
4137 max = 0;
4138 val = wp->w_topline - 1;
4139 size = wp->w_height;
4140#ifdef SCROLL_PAST_END
4141 if (val > max) /* just in case */
4142 val = max;
4143#else
4144 if (size > max + 1) /* just in case */
4145 size = max + 1;
4146 if (val > max - size + 1)
4147 val = max - size + 1;
4148#endif
4149 if (val < 0) /* minimal value is 0 */
4150 val = 0;
4151
4152 /*
4153 * Scrollbar at index 0 (the left one) contains all the information.
4154 * It would be the same info for left and right so we just store it for
4155 * one of them.
4156 */
4157 sb = &wp->w_scrollbars[0];
4158
4159 /*
4160 * Note: no check for valid w_botline. If it's not valid the
4161 * scrollbars will be updated later anyway.
4162 */
4163 if (size < 1 || wp->w_botline - 2 > max)
4164 {
4165 /*
4166 * This can happen during changing files. Just don't update the
4167 * scrollbar for now.
4168 */
4169 sb->height = 0; /* Force update next time */
4170 if (gui.which_scrollbars[SBAR_LEFT])
4171 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4172 if (gui.which_scrollbars[SBAR_RIGHT])
4173 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4174 continue;
4175 }
4176 if (force || sb->height != wp->w_height
4177#ifdef FEAT_WINDOWS
4178 || sb->top != wp->w_winrow
4179 || sb->status_height != wp->w_status_height
4180# ifdef FEAT_VERTSPLIT
4181 || sb->width != wp->w_width
4182 || prev_curwin != curwin
4183# endif
4184#endif
4185 )
4186 {
4187 /* Height, width or position of scrollbar has changed. For
4188 * vertical split: curwin changed. */
4189 sb->height = wp->w_height;
4190#ifdef FEAT_WINDOWS
4191 sb->top = wp->w_winrow;
4192 sb->status_height = wp->w_status_height;
4193# ifdef FEAT_VERTSPLIT
4194 sb->width = wp->w_width;
4195# endif
4196#endif
4197
4198 /* Calculate height and position in pixels */
4199 h = (sb->height + sb->status_height) * gui.char_height;
4200 y = sb->top * gui.char_height + gui.border_offset;
4201#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4202 if (gui.menu_is_active)
4203 y += gui.menu_height;
4204#endif
4205
4206#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4207 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4208# ifdef FEAT_GUI_ATHENA
4209 y += gui.toolbar_height;
4210# else
4211# ifdef FEAT_GUI_MSWIN
4212 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4213# endif
4214# endif
4215#endif
4216
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004217#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4218 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004219 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004220#endif
4221
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef FEAT_WINDOWS
4223 if (wp->w_winrow == 0)
4224#endif
4225 {
4226 /* Height of top scrollbar includes width of top border */
4227 h += gui.border_offset;
4228 y -= gui.border_offset;
4229 }
4230 if (gui.which_scrollbars[SBAR_LEFT])
4231 {
4232 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4233 gui.left_sbar_x, y,
4234 gui.scrollbar_width, h);
4235 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4236 }
4237 if (gui.which_scrollbars[SBAR_RIGHT])
4238 {
4239 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4240 gui.right_sbar_x, y,
4241 gui.scrollbar_width, h);
4242 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4243 }
4244 }
4245
4246 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4247 * checking if the thumb moved at least a pixel. Only do this for
4248 * Athena, most other GUIs require the update anyway to make the
4249 * arrows work. */
4250#ifdef FEAT_GUI_ATHENA
4251 if (max == 0)
4252 y = 0;
4253 else
4254 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4255 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4256#else
4257 if (force || sb->value != val || sb->size != size || sb->max != max)
4258#endif
4259 {
4260 /* Thumb of scrollbar has moved */
4261 sb->value = val;
4262#ifdef FEAT_GUI_ATHENA
4263 sb->pixval = y;
4264#endif
4265 sb->size = size;
4266 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004267 if (gui.which_scrollbars[SBAR_LEFT]
4268 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4270 val, size, max);
4271 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004272 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4274 val, size, max);
4275 }
4276 }
4277#ifdef FEAT_VERTSPLIT
4278 prev_curwin = curwin;
4279#endif
4280 --hold_gui_events;
4281}
4282
4283/*
4284 * Enable or disable a scrollbar.
4285 * Check for scrollbars for vertically split windows which are not enabled
4286 * sometimes.
4287 */
4288 static void
4289gui_do_scrollbar(wp, which, enable)
4290 win_T *wp;
4291 int which; /* SBAR_LEFT or SBAR_RIGHT */
4292 int enable; /* TRUE to enable scrollbar */
4293{
4294#ifdef FEAT_VERTSPLIT
4295 int midcol = curwin->w_wincol + curwin->w_width / 2;
4296 int has_midcol = (wp->w_wincol <= midcol
4297 && wp->w_wincol + wp->w_width >= midcol);
4298
4299 /* Only enable scrollbars that contain the middle column of the current
4300 * window. */
4301 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4302 {
4303 /* Scrollbars only on one side. Don't enable scrollbars that don't
4304 * contain the middle column of the current window. */
4305 if (!has_midcol)
4306 enable = FALSE;
4307 }
4308 else
4309 {
4310 /* Scrollbars on both sides. Don't enable scrollbars that neither
4311 * contain the middle column of the current window nor are on the far
4312 * side. */
4313 if (midcol > Columns / 2)
4314 {
4315 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4316 enable = FALSE;
4317 }
4318 else
4319 {
4320 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4321 : !has_midcol)
4322 enable = FALSE;
4323 }
4324 }
4325#endif
4326 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4327}
4328
4329/*
4330 * Scroll a window according to the values set in the globals current_scrollbar
4331 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4332 * or FALSE otherwise.
4333 */
4334 int
4335gui_do_scroll()
4336{
4337 win_T *wp, *save_wp;
4338 int i;
4339 long nlines;
4340 pos_T old_cursor;
4341 linenr_T old_topline;
4342#ifdef FEAT_DIFF
4343 int old_topfill;
4344#endif
4345
4346 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4347 if (wp == NULL)
4348 break;
4349 if (wp == NULL)
4350 /* Couldn't find window */
4351 return FALSE;
4352
4353 /*
4354 * Compute number of lines to scroll. If zero, nothing to do.
4355 */
4356 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4357 if (nlines == 0)
4358 return FALSE;
4359
4360 save_wp = curwin;
4361 old_topline = wp->w_topline;
4362#ifdef FEAT_DIFF
4363 old_topfill = wp->w_topfill;
4364#endif
4365 old_cursor = wp->w_cursor;
4366 curwin = wp;
4367 curbuf = wp->w_buffer;
4368 if (nlines < 0)
4369 scrolldown(-nlines, gui.dragged_wp == NULL);
4370 else
4371 scrollup(nlines, gui.dragged_wp == NULL);
4372 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4373 * the mouse-up event already, but we still want it to behave like when
4374 * dragging. But not the next click in an arrow. */
4375 if (gui.dragged_sb == SBAR_NONE)
4376 gui.dragged_wp = NULL;
4377
4378 if (old_topline != wp->w_topline
4379#ifdef FEAT_DIFF
4380 || old_topfill != wp->w_topfill
4381#endif
4382 )
4383 {
4384 if (p_so != 0)
4385 {
4386 cursor_correct(); /* fix window for 'so' */
4387 update_topline(); /* avoid up/down jump */
4388 }
4389 if (old_cursor.lnum != wp->w_cursor.lnum)
4390 coladvance(wp->w_curswant);
4391#ifdef FEAT_SCROLLBIND
4392 wp->w_scbind_pos = wp->w_topline;
4393#endif
4394 }
4395
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004396 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4397 validate_cursor();
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 curwin = save_wp;
4400 curbuf = save_wp->w_buffer;
4401
4402 /*
4403 * Don't call updateWindow() when nothing has changed (it will overwrite
4404 * the status line!).
4405 */
4406 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004407 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408#ifdef FEAT_DIFF
4409 || old_topfill != wp->w_topfill
4410#endif
4411 )
4412 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004413 int type = VALID;
4414
4415#ifdef FEAT_INS_EXPAND
4416 if (pum_visible())
4417 {
4418 type = NOT_VALID;
4419 wp->w_lines_valid = 0;
4420 }
4421#endif
4422 /* Don't set must_redraw here, it may cause the popup menu to
4423 * disappear when losing focus after a scrollbar drag. */
4424 if (wp->w_redr_type < type)
4425 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 updateWindow(wp); /* update window, status line, and cmdline */
4427 }
4428
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004429#ifdef FEAT_INS_EXPAND
4430 /* May need to redraw the popup menu. */
4431 if (pum_visible())
4432 pum_redraw();
4433#endif
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4436}
4437
4438
4439/*
4440 * Horizontal scrollbar stuff:
4441 */
4442
4443/*
4444 * Return length of line "lnum" for horizontal scrolling.
4445 */
4446 static colnr_T
4447scroll_line_len(lnum)
4448 linenr_T lnum;
4449{
4450 char_u *p;
4451 colnr_T col;
4452 int w;
4453
4454 p = ml_get(lnum);
4455 col = 0;
4456 if (*p != NUL)
4457 for (;;)
4458 {
4459 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004460 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (*p == NUL) /* don't count the last character */
4462 break;
4463 col += w;
4464 }
4465 return col;
4466}
4467
4468/* Remember which line is currently the longest, so that we don't have to
4469 * search for it when scrolling horizontally. */
4470static linenr_T longest_lnum = 0;
4471
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004472/*
4473 * Find longest visible line number. If this is not possible (or not desired,
4474 * by setting 'h' in "guioptions") then the current line number is returned.
4475 */
4476 static linenr_T
4477gui_find_longest_lnum()
4478{
4479 linenr_T ret = 0;
4480
4481 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4482 * line numbers, topline and botline can be invalid when displaying is
4483 * postponed. */
4484 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4485 && curwin->w_topline <= curwin->w_cursor.lnum
4486 && curwin->w_botline > curwin->w_cursor.lnum
4487 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4488 {
4489 linenr_T lnum;
4490 colnr_T n;
4491 long max = 0;
4492
4493 /* Use maximum of all visible lines. Remember the lnum of the
4494 * longest line, closest to the cursor line. Used when scrolling
4495 * below. */
4496 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4497 {
4498 n = scroll_line_len(lnum);
4499 if (n > (colnr_T)max)
4500 {
4501 max = n;
4502 ret = lnum;
4503 }
4504 else if (n == (colnr_T)max
4505 && abs((int)(lnum - curwin->w_cursor.lnum))
4506 < abs((int)(ret - curwin->w_cursor.lnum)))
4507 ret = lnum;
4508 }
4509 }
4510 else
4511 /* Use cursor line only. */
4512 ret = curwin->w_cursor.lnum;
4513
4514 return ret;
4515}
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 static void
4518gui_update_horiz_scrollbar(force)
4519 int force;
4520{
4521 long value, size, max; /* need 32 bit ints here */
4522
4523 if (!gui.which_scrollbars[SBAR_BOTTOM])
4524 return;
4525
4526 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4527 return;
4528
4529 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4530 return;
4531
4532 /*
4533 * It is possible for the cursor to be invalid if we're in the middle of
4534 * something (like changing files). If so, don't do anything for now.
4535 */
4536 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4537 {
4538 gui.bottom_sbar.value = -1;
4539 return;
4540 }
4541
4542 size = W_WIDTH(curwin);
4543 if (curwin->w_p_wrap)
4544 {
4545 value = 0;
4546#ifdef SCROLL_PAST_END
4547 max = 0;
4548#else
4549 max = W_WIDTH(curwin) - 1;
4550#endif
4551 }
4552 else
4553 {
4554 value = curwin->w_leftcol;
4555
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004556 longest_lnum = gui_find_longest_lnum();
4557 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559#ifdef FEAT_VIRTUALEDIT
4560 if (virtual_active())
4561 {
4562 /* May move the cursor even further to the right. */
4563 if (curwin->w_virtcol >= (colnr_T)max)
4564 max = curwin->w_virtcol;
4565 }
4566#endif
4567
4568#ifndef SCROLL_PAST_END
4569 max += W_WIDTH(curwin) - 1;
4570#endif
4571 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004572 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 size -= curwin_col_off();
4574#ifndef SCROLL_PAST_END
4575 max -= curwin_col_off();
4576#endif
4577 }
4578
4579#ifndef SCROLL_PAST_END
4580 if (value > max - size + 1)
4581 value = max - size + 1; /* limit the value to allowable range */
4582#endif
4583
4584#ifdef FEAT_RIGHTLEFT
4585 if (curwin->w_p_rl)
4586 {
4587 value = max + 1 - size - value;
4588 if (value < 0)
4589 {
4590 size += value;
4591 value = 0;
4592 }
4593 }
4594#endif
4595 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4596 && max == gui.bottom_sbar.max)
4597 return;
4598
4599 gui.bottom_sbar.value = value;
4600 gui.bottom_sbar.size = size;
4601 gui.bottom_sbar.max = max;
4602 gui.prev_wrap = curwin->w_p_wrap;
4603
4604 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4605}
4606
4607/*
4608 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4609 */
4610 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004611gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004612 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004613 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
4615 /* no wrapping, no scrolling */
4616 if (curwin->w_p_wrap)
4617 return FALSE;
4618
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004619 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 return FALSE;
4621
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004622 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004625 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004627 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004628 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004630 if (compute_longest_lnum)
4631 {
4632 curwin->w_cursor.lnum = gui_find_longest_lnum();
4633 curwin->w_cursor.col = 0;
4634 }
4635 /* Do a sanity check on "longest_lnum", just in case. */
4636 else if (longest_lnum >= curwin->w_topline
4637 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
4639 curwin->w_cursor.lnum = longest_lnum;
4640 curwin->w_cursor.col = 0;
4641 }
4642 }
4643
4644 return leftcol_changed();
4645}
4646
4647/*
4648 * Check that none of the colors are the same as the background color
4649 */
4650 void
4651gui_check_colors()
4652{
4653 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4654 {
4655 gui_set_bg_color((char_u *)"White");
4656 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4657 gui_set_fg_color((char_u *)"Black");
4658 }
4659}
4660
Bram Moolenaar3918c952005-03-15 22:34:55 +00004661 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662gui_set_fg_color(name)
4663 char_u *name;
4664{
4665 gui.norm_pixel = gui_get_color(name);
4666 hl_set_fg_color_name(vim_strsave(name));
4667}
4668
Bram Moolenaar3918c952005-03-15 22:34:55 +00004669 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670gui_set_bg_color(name)
4671 char_u *name;
4672{
4673 gui.back_pixel = gui_get_color(name);
4674 hl_set_bg_color_name(vim_strsave(name));
4675}
4676
4677/*
4678 * Allocate a color by name.
4679 * Returns INVALCOLOR and gives an error message when failed.
4680 */
4681 guicolor_T
4682gui_get_color(name)
4683 char_u *name;
4684{
4685 guicolor_T t;
4686
4687 if (*name == NUL)
4688 return INVALCOLOR;
4689 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004692#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 && gui.in_use
4694#endif
4695 )
4696 EMSG2(_("E254: Cannot allocate color %s"), name);
4697 return t;
4698}
4699
4700/*
4701 * Return the grey value of a color (range 0-255).
4702 */
4703 int
4704gui_get_lightness(pixel)
4705 guicolor_T pixel;
4706{
4707 long_u rgb = gui_mch_get_rgb(pixel);
4708
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004709 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004710 + (((rgb >> 8) & 0xff) * 587)
4711 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712}
4713
4714#if defined(FEAT_GUI_X11) || defined(PROTO)
4715 void
4716gui_new_scrollbar_colors()
4717{
4718 win_T *wp;
4719
4720 /* Nothing to do if GUI hasn't started yet. */
4721 if (!gui.in_use)
4722 return;
4723
4724 FOR_ALL_WINDOWS(wp)
4725 {
4726 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4727 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4728 }
4729 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4730}
4731#endif
4732
4733/*
4734 * Call this when focus has changed.
4735 */
4736 void
4737gui_focus_change(in_focus)
4738 int in_focus;
4739{
4740/*
4741 * Skip this code to avoid drawing the cursor when debugging and switching
4742 * between the debugger window and gvim.
4743 */
4744#if 1
4745 gui.in_focus = in_focus;
4746 out_flush(); /* make sure output has been written */
4747 gui_update_cursor(TRUE, FALSE);
4748
4749# ifdef FEAT_XIM
4750 xim_set_focus(in_focus);
4751# endif
4752
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004753 /* Put events in the input queue only when allowed.
4754 * ui_focus_change() isn't called directly, because it invokes
4755 * autocommands and that must not happen asynchronously. */
4756 if (!hold_gui_events)
4757 {
4758 char_u bytes[3];
4759
4760 bytes[0] = CSI;
4761 bytes[1] = KS_EXTRA;
4762 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4763 add_to_input_buf(bytes, 3);
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765#endif
4766}
4767
4768/*
4769 * Called when the mouse moved (but not when dragging).
4770 */
4771 void
4772gui_mouse_moved(x, y)
4773 int x;
4774 int y;
4775{
4776 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004777 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
Bram Moolenaard3667a22006-03-16 21:35:52 +00004779 /* Ignore this while still starting up. */
4780 if (!gui.in_use || gui.starting)
4781 return;
4782
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783#ifdef FEAT_MOUSESHAPE
4784 /* Get window pointer, and update mouse shape as well. */
4785 wp = xy2win(x, y);
4786#endif
4787
4788 /* Only handle this when 'mousefocus' set and ... */
4789 if (p_mousef
4790 && !hold_gui_events /* not holding events */
4791 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4792 && State != HITRETURN /* but not hit-return prompt */
4793 && msg_scrolled == 0 /* no scrolled message */
4794 && !need_mouse_correct /* not moving the pointer */
4795 && gui.in_focus) /* gvim in focus */
4796 {
4797 /* Don't move the mouse when it's left or right of the Vim window */
4798 if (x < 0 || x > Columns * gui.char_width)
4799 return;
4800#ifndef FEAT_MOUSESHAPE
4801 wp = xy2win(x, y);
4802#endif
4803 if (wp == curwin || wp == NULL)
4804 return; /* still in the same old window, or none at all */
4805
Bram Moolenaar9c102382006-05-03 21:26:49 +00004806#ifdef FEAT_WINDOWS
4807 /* Ignore position in the tab pages line. */
4808 if (Y_2_ROW(y) < tabline_height())
4809 return;
4810#endif
4811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
4813 * format a mouse click on status line input
4814 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004815 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4816 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 */
4818 if (finish_op)
4819 {
4820 /* abort the current operator first */
4821 st[0] = ESC;
4822 add_to_input_buf(st, 1);
4823 }
4824 st[0] = CSI;
4825 st[1] = KS_MOUSE;
4826 st[2] = KE_FILLER;
4827 st[3] = (char_u)MOUSE_LEFT;
4828 fill_mouse_coord(st + 4,
4829#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004830 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#else
4832 -1,
4833#endif
4834 wp->w_height + W_WINROW(wp));
4835
4836 add_to_input_buf(st, 8);
4837 st[3] = (char_u)MOUSE_RELEASE;
4838 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#ifdef FEAT_GUI_GTK
4840 /* Need to wake up the main loop */
4841 if (gtk_main_level() > 0)
4842 gtk_main_quit();
4843#endif
4844 }
4845}
4846
4847/*
4848 * Called when mouse should be moved to window with focus.
4849 */
4850 void
4851gui_mouse_correct()
4852{
4853 int x, y;
4854 win_T *wp = NULL;
4855
4856 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004857
4858 if (!(gui.in_use && p_mousef))
4859 return;
4860
4861 gui_mch_getmouse(&x, &y);
4862 /* Don't move the mouse when it's left or right of the Vim window */
4863 if (x < 0 || x > Columns * gui.char_width)
4864 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004865 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004866# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004867 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004868# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004869 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004870 wp = xy2win(x, y);
4871 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004873 validate_cline_row();
4874 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4875 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878}
4879
4880/*
4881 * Find window where the mouse pointer "y" coordinate is in.
4882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 static win_T *
4884xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004885 int x UNUSED;
4886 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
4888#ifdef FEAT_WINDOWS
4889 int row;
4890 int col;
4891 win_T *wp;
4892
4893 row = Y_2_ROW(y);
4894 col = X_2_COL(x);
4895 if (row < 0 || col < 0) /* before first window */
4896 return NULL;
4897 wp = mouse_find_win(&row, &col);
4898# ifdef FEAT_MOUSESHAPE
4899 if (State == HITRETURN || State == ASKMORE)
4900 {
4901 if (Y_2_ROW(y) >= msg_row)
4902 update_mouseshape(SHAPE_IDX_MOREL);
4903 else
4904 update_mouseshape(SHAPE_IDX_MORE);
4905 }
4906 else if (row > wp->w_height) /* below status line */
4907 update_mouseshape(SHAPE_IDX_CLINE);
4908# ifdef FEAT_VERTSPLIT
4909 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004910 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 update_mouseshape(SHAPE_IDX_VSEP);
4912# endif
4913 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004914 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 update_mouseshape(SHAPE_IDX_STATUS);
4916 else
4917 update_mouseshape(-2);
4918# endif
4919 return wp;
4920#else
4921 return firstwin;
4922#endif
4923}
4924
4925/*
4926 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4927 * File names may be given to redefine the args list.
4928 */
4929 void
4930ex_gui(eap)
4931 exarg_T *eap;
4932{
4933 char_u *arg = eap->arg;
4934
4935 /*
4936 * Check for "-f" argument: foreground, don't fork.
4937 * Also don't fork when started with "gvim -f".
4938 * Do fork when using "gui -b".
4939 */
4940 if (arg[0] == '-'
4941 && (arg[1] == 'f' || arg[1] == 'b')
4942 && (arg[2] == NUL || vim_iswhite(arg[2])))
4943 {
4944 gui.dofork = (arg[1] == 'b');
4945 eap->arg = skipwhite(eap->arg + 2);
4946 }
4947 if (!gui.in_use)
4948 {
4949 /* Clear the command. Needed for when forking+exiting, to avoid part
4950 * of the argument ending up after the shell prompt. */
4951 msg_clr_eos_force();
4952 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004953#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004954 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 if (!ends_excmd(*eap->arg))
4958 ex_next(eap);
4959}
4960
4961#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004962 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * This is shared between Athena, Motif and GTK.
4965 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004966static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968/*
4969 * Callback function for do_in_runtimepath().
4970 */
4971 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004972gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004974 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976 char_u *gfp_buffer = cookie;
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (STRLEN(fname) >= MAXPATHL)
4979 *gfp_buffer = NUL;
4980 else
4981 STRCPY(gfp_buffer, fname);
4982}
4983
4984/*
4985 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4986 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4987 */
4988 int
4989gui_find_bitmap(name, buffer, ext)
4990 char_u *name;
4991 char_u *buffer;
4992 char *ext;
4993{
4994 if (STRLEN(name) > MAXPATHL - 14)
4995 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004996 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004997 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4998 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return FAIL;
5000 return OK;
5001}
5002
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005003# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004/*
5005 * Given the name of the "icon=" argument, try finding the bitmap file for the
5006 * icon. If it is an absolute path name, use it as it is. Otherwise append
5007 * "ext" and search for it in 'runtimepath'.
5008 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5009 * contains "name".
5010 */
5011 void
5012gui_find_iconfile(name, buffer, ext)
5013 char_u *name;
5014 char_u *buffer;
5015 char *ext;
5016{
5017 char_u buf[MAXPATHL + 1];
5018
5019 expand_env(name, buffer, MAXPATHL);
5020 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5021 STRCPY(buffer, buf);
5022}
5023# endif
5024#endif
5025
Bram Moolenaar9372a112005-12-06 19:59:18 +00005026#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 void
5028display_errors()
5029{
5030 char_u *p;
5031
5032 if (isatty(2))
5033 fflush(stderr);
5034 else if (error_ga.ga_data != NULL)
5035 {
5036 /* avoid putting up a message box with blanks only */
5037 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5038 if (!isspace(*p))
5039 {
5040 /* Truncate a very long message, it will go off-screen. */
5041 if (STRLEN(p) > 2000)
5042 STRCPY(p + 2000 - 14, "...(truncated)");
5043 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005044 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 break;
5046 }
5047 ga_clear(&error_ga);
5048 }
5049}
5050#endif
5051
5052#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5053/*
5054 * Return TRUE if still starting up and there is no place to enter text.
5055 * For GTK and X11 we check if stderr is not a tty, which means we were
5056 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5057 * allow typing on stdin.
5058 */
5059 int
5060no_console_input()
5061{
5062 return ((!gui.in_use || gui.starting)
5063# ifndef NO_CONSOLE
5064 && !isatty(0) && !isatty(2)
5065# endif
5066 );
5067}
5068#endif
5069
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005070#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005071 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005072 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073/*
5074 * Update the current window and the screen.
5075 */
5076 void
5077gui_update_screen()
5078{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005079#ifdef FEAT_CONCEAL
5080 linenr_T conceal_old_cursor_line = 0;
5081 linenr_T conceal_new_cursor_line = 0;
5082 int conceal_update_lines = FALSE;
5083#endif
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 update_topline();
5086 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005087
5088#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00005089 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005090 if (!finish_op && (
5091# ifdef FEAT_AUTOCMD
5092 has_cursormoved()
5093# endif
5094# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
5095 ||
5096# endif
5097# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005098 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005099# endif
5100 )
5101 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005102 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005103# ifdef FEAT_AUTOCMD
5104 if (has_cursormoved())
5105 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
5106# endif
5107# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005108 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005109 {
5110 conceal_old_cursor_line = last_cursormoved.lnum;
5111 conceal_new_cursor_line = curwin->w_cursor.lnum;
5112 conceal_update_lines = TRUE;
5113 }
5114# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005115 last_cursormoved = curwin->w_cursor;
5116 }
5117#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 update_screen(0); /* may need to update the screen */
5120 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005121# if defined(FEAT_CONCEAL)
5122 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005123 && (conceal_old_cursor_line != conceal_new_cursor_line
5124 || conceal_cursor_line(curwin)
5125 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005126 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005127 if (conceal_old_cursor_line != conceal_new_cursor_line)
5128 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005129 update_single_line(curwin, conceal_new_cursor_line);
5130 curwin->w_valid &= ~VALID_CROW;
5131 }
5132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 out_flush(); /* make sure output has been written */
5134 gui_update_cursor(TRUE, FALSE);
5135 gui_mch_flush();
5136}
5137#endif
5138
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005139#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5141
5142/*
5143 * Get the text to use in a find/replace dialog. Uses the last search pattern
5144 * if the argument is empty.
5145 * Returns an allocated string.
5146 */
5147 char_u *
5148get_find_dialog_text(arg, wwordp, mcasep)
5149 char_u *arg;
5150 int *wwordp; /* return: TRUE if \< \> found */
5151 int *mcasep; /* return: TRUE if \C found */
5152{
5153 char_u *text;
5154
5155 if (*arg == NUL)
5156 text = last_search_pat();
5157 else
5158 text = arg;
5159 if (text != NULL)
5160 {
5161 text = vim_strsave(text);
5162 if (text != NULL)
5163 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005164 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 int i;
5166
5167 /* Remove "\V" */
5168 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5169 {
5170 mch_memmove(text, text + 2, (size_t)(len - 1));
5171 len -= 2;
5172 }
5173
5174 /* Recognize "\c" and "\C" and remove. */
5175 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5176 {
5177 *mcasep = (text[1] == 'C');
5178 mch_memmove(text, text + 2, (size_t)(len - 1));
5179 len -= 2;
5180 }
5181
5182 /* Recognize "\<text\>" and remove. */
5183 if (len >= 4
5184 && STRNCMP(text, "\\<", 2) == 0
5185 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5186 {
5187 *wwordp = TRUE;
5188 mch_memmove(text, text + 2, (size_t)(len - 4));
5189 text[len - 4] = NUL;
5190 }
5191
5192 /* Recognize "\/" or "\?" and remove. */
5193 for (i = 0; i + 1 < len; ++i)
5194 if (text[i] == '\\' && (text[i + 1] == '/'
5195 || text[i + 1] == '?'))
5196 {
5197 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5198 --len;
5199 }
5200 }
5201 }
5202 return text;
5203}
5204
5205/*
5206 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5207 */
5208 static void
5209concat_esc(gap, text, what)
5210 garray_T *gap;
5211 char_u *text;
5212 int what;
5213{
5214 while (*text != NUL)
5215 {
5216#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005217 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (l > 1)
5220 {
5221 while (--l >= 0)
5222 ga_append(gap, *text++);
5223 continue;
5224 }
5225#endif
5226 if (*text == what)
5227 ga_append(gap, '\\');
5228 ga_append(gap, *text);
5229 ++text;
5230 }
5231}
5232
5233/*
5234 * Handle the press of a button in the find-replace dialog.
5235 * Return TRUE when something was added to the input buffer.
5236 */
5237 int
5238gui_do_findrepl(flags, find_text, repl_text, down)
5239 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5240 char_u *find_text;
5241 char_u *repl_text;
5242 int down; /* Search downwards. */
5243{
5244 garray_T ga;
5245 int i;
5246 int type = (flags & FRD_TYPE_MASK);
5247 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005248 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005249 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005250 static int busy = FALSE;
5251
5252 /* When the screen is being updated we should not change buffers and
5253 * windows structures, it may cause freed memory to be used. Also don't
5254 * do this recursively (pressing "Find" quickly several times. */
5255 if (updating_screen || busy)
5256 return FALSE;
5257
5258 /* refuse replace when text cannot be changed */
5259 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5260 return FALSE;
5261
5262 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005265 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ga_concat(&ga, (char_u *)"%s/");
5267
5268 ga_concat(&ga, (char_u *)"\\V");
5269 if (flags & FRD_MATCH_CASE)
5270 ga_concat(&ga, (char_u *)"\\C");
5271 else
5272 ga_concat(&ga, (char_u *)"\\c");
5273 if (flags & FRD_WHOLE_WORD)
5274 ga_concat(&ga, (char_u *)"\\<");
5275 if (type == FRD_REPLACEALL || down)
5276 concat_esc(&ga, find_text, '/'); /* escape slashes */
5277 else
5278 concat_esc(&ga, find_text, '?'); /* escape '?' */
5279 if (flags & FRD_WHOLE_WORD)
5280 ga_concat(&ga, (char_u *)"\\>");
5281
5282 if (type == FRD_REPLACEALL)
5283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005285 /* escape / and \ */
5286 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5287 if (p != NULL)
5288 ga_concat(&ga, p);
5289 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005291 }
5292 ga_append(&ga, NUL);
5293
5294 if (type == FRD_REPLACE)
5295 {
5296 /* Do the replacement when the text at the cursor matches. Thus no
5297 * replacement is done if the cursor was moved! */
5298 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5299 regmatch.rm_ic = 0;
5300 if (regmatch.regprog != NULL)
5301 {
5302 p = ml_get_cursor();
5303 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5304 && regmatch.startp[0] == p)
5305 {
5306 /* Clear the command line to remove any old "No match"
5307 * error. */
5308 msg_end_prompt();
5309
5310 if (u_save_cursor() == OK)
5311 {
5312 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005313 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005314
5315 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005316 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005317 ins_str(repl_text);
5318 }
5319 }
5320 else
5321 MSG(_("No match at cursor, finding next"));
5322 vim_free(regmatch.regprog);
5323 }
5324 }
5325
5326 if (type == FRD_REPLACEALL)
5327 {
5328 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005329 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 do_cmdline_cmd(ga.ga_data);
5331 }
5332 else
5333 {
5334 /* Search for the next match. */
5335 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005337 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 msg_scroll = i; /* don't let an error message set msg_scroll */
5339 }
5340
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005341 /* Don't want to pass did_emsg to other code, it may cause disabling
5342 * syntax HL if we were busy redrawing. */
5343 did_emsg = save_did_emsg;
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (State & (NORMAL | INSERT))
5346 {
5347 gui_update_screen(); /* update the screen */
5348 msg_didout = 0; /* overwrite any message */
5349 need_wait_return = FALSE; /* don't wait for return */
5350 }
5351
5352 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005353 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return (ga.ga_len > 0);
5355}
5356
5357#endif
5358
5359#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5360 || defined(FEAT_GUI_MSWIN) \
5361 || defined(FEAT_GUI_MAC) \
5362 || defined(PROTO)
5363
5364#ifdef FEAT_WINDOWS
5365static void gui_wingoto_xy __ARGS((int x, int y));
5366
5367/*
5368 * Jump to the window at specified point (x, y).
5369 */
5370 static void
5371gui_wingoto_xy(x, y)
5372 int x;
5373 int y;
5374{
5375 int row = Y_2_ROW(y);
5376 int col = X_2_COL(x);
5377 win_T *wp;
5378
5379 if (row >= 0 && col >= 0)
5380 {
5381 wp = mouse_find_win(&row, &col);
5382 if (wp != NULL && wp != curwin)
5383 win_goto(wp);
5384 }
5385}
5386#endif
5387
5388/*
5389 * Process file drop. Mouse cursor position, key modifiers, name of files
5390 * and count of files are given. Argument "fnames[count]" has full pathnames
5391 * of dropped files, they will be freed in this function, and caller can't use
5392 * fnames after call this function.
5393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 void
5395gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005396 int x UNUSED;
5397 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 int_u modifiers;
5399 char_u **fnames;
5400 int count;
5401{
5402 int i;
5403 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005404 static int entered = FALSE;
5405
5406 /*
5407 * This function is called by event handlers. Just in case we get a
5408 * second event before the first one is handled, ignore the second one.
5409 * Not sure if this can ever happen, just in case.
5410 */
5411 if (entered)
5412 return;
5413 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415 /*
5416 * When the cursor is at the command line, add the file names to the
5417 * command line, don't edit the files.
5418 */
5419 if (State & CMDLINE)
5420 {
5421 shorten_filenames(fnames, count);
5422 for (i = 0; i < count; ++i)
5423 {
5424 if (fnames[i] != NULL)
5425 {
5426 if (i > 0)
5427 add_to_input_buf((char_u*)" ", 1);
5428
5429 /* We don't know what command is used thus we can't be sure
5430 * about which characters need to be escaped. Only escape the
5431 * most common ones. */
5432# ifdef BACKSLASH_IN_FILENAME
5433 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5434# else
5435 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5436# endif
5437 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005438 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 vim_free(p);
5440 vim_free(fnames[i]);
5441 }
5442 }
5443 vim_free(fnames);
5444 }
5445 else
5446 {
5447 /* Go to the window under mouse cursor, then shorten given "fnames" by
5448 * current window, because a window can have local current dir. */
5449# ifdef FEAT_WINDOWS
5450 gui_wingoto_xy(x, y);
5451# endif
5452 shorten_filenames(fnames, count);
5453
5454 /* If Shift held down, remember the first item. */
5455 if ((modifiers & MOUSE_SHIFT) != 0)
5456 p = vim_strsave(fnames[0]);
5457 else
5458 p = NULL;
5459
5460 /* Handle the drop, :edit or :split to get to the file. This also
5461 * frees fnames[]. Skip this if there is only one item it's a
5462 * directory and Shift is held down. */
5463 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5464 && mch_isdir(fnames[0]))
5465 {
5466 vim_free(fnames[0]);
5467 vim_free(fnames);
5468 }
5469 else
5470 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5471
5472 /* If Shift held down, change to first file's directory. If the first
5473 * item is a directory, change to that directory (and let the explorer
5474 * plugin show the contents). */
5475 if (p != NULL)
5476 {
5477 if (mch_isdir(p))
5478 {
5479 if (mch_chdir((char *)p) == 0)
5480 shorten_fnames(TRUE);
5481 }
5482 else if (vim_chdirfile(p) == OK)
5483 shorten_fnames(TRUE);
5484 vim_free(p);
5485 }
5486
5487 /* Update the screen display */
5488 update_screen(NOT_VALID);
5489# ifdef FEAT_MENU
5490 gui_update_menus(0);
5491# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005492#ifdef FEAT_TITLE
5493 maketitle();
5494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 setcursor();
5496 out_flush();
5497 gui_update_cursor(FALSE, FALSE);
5498 gui_mch_flush();
5499 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005500
5501 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502}
5503#endif