Don't pass 'long' vars over process boundary
[MacVim.git] / src / gui.c
blob1eb0e4230de07f98288902a1c71701652ed6a512
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
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 */
11 #include "vim.h"
13 /* Structure containing all the GUI information */
14 gui_T gui;
16 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
17 static void set_guifontwide __ARGS((char_u *font_name));
18 #endif
19 static void gui_check_pos __ARGS((void));
20 static void gui_position_components __ARGS((int));
21 static void gui_outstr __ARGS((char_u *, int));
22 static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
23 #ifdef HAVE_GTK2
24 static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25 #endif
26 static void gui_delete_lines __ARGS((int row, int count));
27 static void gui_insert_lines __ARGS((int row, int count));
28 static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
29 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30 static int gui_has_tabline __ARGS((void));
31 #endif
32 static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33 static colnr_T scroll_line_len __ARGS((linenr_T lnum));
34 static void gui_update_horiz_scrollbar __ARGS((int));
35 static void gui_set_fg_color __ARGS((char_u *name));
36 static void gui_set_bg_color __ARGS((char_u *name));
37 static win_T *xy2win __ARGS((int x, int y));
39 static int can_update_cursor = TRUE; /* can display the cursor */
42 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
43 * this makes the thumb indicate the part of the text that is shown. Motif
44 * can't do this.
46 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
47 # define SCROLL_PAST_END
48 #endif
51 * gui_start -- Called when user wants to start the GUI.
53 * Careful: This function can be called recursively when there is a ":gui"
54 * command in the .gvimrc file. Only the first call should fork, not the
55 * recursive call.
57 void
58 gui_start()
60 char_u *old_term;
61 #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X)
62 /* By the time we get here Mac OS X will already have forked (it does so
63 * right after scanning the command line) so don't do anything here. This
64 * means that "f" in 'guioptions' cannot be supported.
66 # define MAY_FORK
67 int dofork = TRUE;
68 #endif
69 static int recursive = 0;
71 old_term = vim_strsave(T_NAME);
74 * Set_termname() will call gui_init() to start the GUI.
75 * Set the "starting" flag, to indicate that the GUI will start.
77 * We don't want to open the GUI shell until after we've read .gvimrc,
78 * otherwise we don't know what font we will use, and hence we don't know
79 * what size the shell should be. So if there are errors in the .gvimrc
80 * file, they will have to go to the terminal: Set full_screen to FALSE.
81 * full_screen will be set to TRUE again by a successful termcapinit().
83 settmode(TMODE_COOK); /* stop RAW mode */
84 if (full_screen)
85 cursor_on(); /* needed for ":gui" in .vimrc */
86 gui.starting = TRUE;
87 full_screen = FALSE;
89 #ifdef MAY_FORK
90 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
91 dofork = FALSE;
92 #endif
93 ++recursive;
95 termcapinit((char_u *)"builtin_gui");
96 gui.starting = recursive - 1;
98 if (!gui.in_use) /* failed to start GUI */
100 termcapinit(old_term); /* back to old term settings */
101 settmode(TMODE_RAW); /* restart RAW mode */
102 #ifdef FEAT_TITLE
103 set_title_defaults(); /* set 'title' and 'icon' again */
104 #endif
107 vim_free(old_term);
109 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
110 if (gui.in_use)
111 /* Display error messages in a dialog now. */
112 display_errors();
113 #endif
115 #if defined(MAY_FORK) && !defined(__QNXNTO__)
117 * Quit the current process and continue in the child.
118 * Makes "gvim file" disconnect from the shell it was started in.
119 * Don't do this when Vim was started with "-f" or the 'f' flag is present
120 * in 'guioptions'.
122 if (gui.in_use && dofork)
124 int pipefd[2]; /* pipe between parent and child */
125 int pipe_error;
126 char dummy;
127 pid_t pid = -1;
129 /* Setup a pipe between the child and the parent, so that the parent
130 * knows when the child has done the setsid() call and is allowed to
131 * exit. */
132 pipe_error = (pipe(pipefd) < 0);
133 pid = fork();
134 if (pid > 0) /* Parent */
136 /* Give the child some time to do the setsid(), otherwise the
137 * exit() may kill the child too (when starting gvim from inside a
138 * gvim). */
139 if (pipe_error)
140 ui_delay(300L, TRUE);
141 else
143 /* The read returns when the child closes the pipe (or when
144 * the child dies for some reason). */
145 close(pipefd[1]);
146 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
147 close(pipefd[0]);
150 /* When swapping screens we may need to go to the next line, e.g.,
151 * after a hit-enter prompt and using ":gui". */
152 if (newline_on_exit)
153 mch_errmsg("\r\n");
156 * The parent must skip the normal exit() processing, the child
157 * will do it. For example, GTK messes up signals when exiting.
159 _exit(0);
162 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
164 * Change our process group. On some systems/shells a CTRL-C in the
165 * shell where Vim was started would otherwise kill gvim!
167 if (pid == 0) /* child */
168 # if defined(HAVE_SETSID)
169 (void)setsid();
170 # else
171 (void)setpgid(0, 0);
172 # endif
173 # endif
174 if (!pipe_error)
176 close(pipefd[0]);
177 close(pipefd[1]);
180 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
181 /* Tell the session manager our new PID */
182 gui_mch_forked();
183 # endif
185 #else
186 # if defined(__QNXNTO__)
187 if (gui.in_use && dofork)
188 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
189 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
190 # endif
191 #endif
193 #ifdef FEAT_AUTOCMD
194 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
195 * the GUIFailed event. */
196 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
197 NULL, NULL, FALSE, curbuf);
198 #endif
200 --recursive;
204 * Call this when vim starts up, whether or not the GUI is started
206 void
207 gui_prepare(argc, argv)
208 int *argc;
209 char **argv;
211 gui.in_use = FALSE; /* No GUI yet (maybe later) */
212 gui.starting = FALSE; /* No GUI yet (maybe later) */
213 gui_mch_prepare(argc, argv);
217 * Try initializing the GUI and check if it can be started.
218 * Used from main() to check early if "vim -g" can start the GUI.
219 * Used from gui_init() to prepare for starting the GUI.
220 * Returns FAIL or OK.
223 gui_init_check()
225 static int result = MAYBE;
227 if (result != MAYBE)
229 if (result == FAIL)
230 EMSG(_("E229: Cannot start the GUI"));
231 return result;
234 gui.shell_created = FALSE;
235 gui.dying = FALSE;
236 gui.in_focus = TRUE; /* so the guicursor setting works */
237 gui.dragged_sb = SBAR_NONE;
238 gui.dragged_wp = NULL;
239 gui.pointer_hidden = FALSE;
240 gui.col = 0;
241 gui.row = 0;
242 gui.num_cols = Columns;
243 gui.num_rows = Rows;
245 gui.cursor_is_valid = FALSE;
246 gui.scroll_region_top = 0;
247 gui.scroll_region_bot = Rows - 1;
248 gui.scroll_region_left = 0;
249 gui.scroll_region_right = Columns - 1;
250 gui.highlight_mask = HL_NORMAL;
251 gui.char_width = 1;
252 gui.char_height = 1;
253 gui.char_ascent = 0;
254 gui.border_width = 0;
256 gui.norm_font = NOFONT;
257 #ifndef HAVE_GTK2
258 gui.bold_font = NOFONT;
259 gui.ital_font = NOFONT;
260 gui.boldital_font = NOFONT;
261 # ifdef FEAT_XFONTSET
262 gui.fontset = NOFONTSET;
263 # endif
264 #endif
266 #ifdef FEAT_MENU
267 # ifndef HAVE_GTK2
268 # ifdef FONTSET_ALWAYS
269 gui.menu_fontset = NOFONTSET;
270 # else
271 gui.menu_font = NOFONT;
272 # endif
273 # endif
274 gui.menu_is_active = TRUE; /* default: include menu */
275 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
276 gui.menu_height = MENU_DEFAULT_HEIGHT;
277 gui.menu_width = 0;
278 # endif
279 #endif
280 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
281 gui.toolbar_height = 0;
282 #endif
283 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
284 gui.footer_height = 0;
285 #endif
286 #ifdef FEAT_BEVAL_TIP
287 gui.tooltip_fontset = NOFONTSET;
288 #endif
290 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
291 gui.prev_wrap = -1;
293 #ifdef ALWAYS_USE_GUI
294 result = OK;
295 #else
296 result = gui_mch_init_check();
297 #endif
298 return result;
302 * This is the call which starts the GUI.
304 void
305 gui_init()
307 win_T *wp;
308 static int recursive = 0;
311 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
312 * function will then be executed at the first call, the rest by the
313 * recursive call. This allow the shell to be opened halfway reading a
314 * gvimrc file.
316 if (!recursive)
318 ++recursive;
320 clip_init(TRUE);
322 /* If can't initialize, don't try doing the rest */
323 if (gui_init_check() == FAIL)
325 --recursive;
326 clip_init(FALSE);
327 return;
331 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
332 * breaks the Paste toolbar button.
334 set_option_value((char_u *)"paste", 0L, NULL, 0);
337 * Set up system-wide default menus.
339 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
340 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
342 sys_menu = TRUE;
343 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
344 sys_menu = FALSE;
346 #endif
349 * Switch on the mouse by default, unless the user changed it already.
350 * This can then be changed in the .gvimrc.
352 if (!option_was_set((char_u *)"mouse"))
353 set_string_option_direct((char_u *)"mouse", -1,
354 (char_u *)"a", OPT_FREE, SID_NONE);
357 * If -U option given, use only the initializations from that file and
358 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
360 if (use_gvimrc != NULL)
362 if (STRCMP(use_gvimrc, "NONE") != 0
363 && STRCMP(use_gvimrc, "NORC") != 0
364 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
365 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
367 else
370 * Get system wide defaults for gvim, only when file name defined.
372 #ifdef SYS_GVIMRC_FILE
373 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
374 #endif
377 * Try to read GUI initialization commands from the following
378 * places:
379 * - environment variable GVIMINIT
380 * - the user gvimrc file (~/.gvimrc)
381 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
382 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
383 * The first that exists is used, the rest is ignored.
385 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
386 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
387 DOSO_GVIMRC) == FAIL
388 #ifdef USR_GVIMRC_FILE2
389 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
390 DOSO_GVIMRC) == FAIL
391 #endif
394 #ifdef USR_GVIMRC_FILE3
395 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
396 #endif
400 * Read initialization commands from ".gvimrc" in current
401 * directory. This is only done if the 'exrc' option is set.
402 * Because of security reasons we disallow shell and write
403 * commands now, except for unix if the file is owned by the user
404 * or 'secure' option has been reset in environment of global
405 * ".gvimrc".
406 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
407 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
409 if (p_exrc)
411 #ifdef UNIX
413 struct stat s;
415 /* if ".gvimrc" file is not owned by user, set 'secure'
416 * mode */
417 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
418 secure = p_secure;
420 #else
421 secure = p_secure;
422 #endif
424 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
425 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
426 #ifdef SYS_GVIMRC_FILE
427 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
428 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
429 #endif
430 #ifdef USR_GVIMRC_FILE2
431 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
432 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
433 #endif
434 #ifdef USR_GVIMRC_FILE3
435 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
436 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
437 #endif
439 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
441 if (secure == 2)
442 need_wait_return = TRUE;
443 secure = 0;
447 if (need_wait_return || msg_didany)
448 wait_return(TRUE);
450 --recursive;
453 /* If recursive call opened the shell, return here from the first call */
454 if (gui.in_use)
455 return;
458 * Create the GUI shell.
460 gui.in_use = TRUE; /* Must be set after menus have been set up */
461 if (gui_mch_init() == FAIL)
462 goto error;
464 /* Avoid a delay for an error message that was printed in the terminal
465 * where Vim was started. */
466 emsg_on_display = FALSE;
467 msg_scrolled = 0;
468 clear_sb_text();
469 need_wait_return = FALSE;
470 msg_didany = FALSE;
473 * Check validity of any generic resources that may have been loaded.
475 if (gui.border_width < 0)
476 gui.border_width = 0;
479 * Set up the fonts. First use a font specified with "-fn" or "-font".
481 if (font_argument != NULL)
482 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
483 if (
484 #ifdef FEAT_XFONTSET
485 (*p_guifontset == NUL
486 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
487 #endif
488 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
489 : p_guifont, FALSE) == FAIL)
491 EMSG(_("E665: Cannot start GUI, no valid font found"));
492 goto error2;
494 #ifdef FEAT_MBYTE
495 if (gui_get_wide_font() == FAIL)
496 EMSG(_("E231: 'guifontwide' invalid"));
497 #endif
499 gui.num_cols = Columns;
500 gui.num_rows = Rows;
501 gui_reset_scroll_region();
503 /* Create initial scrollbars */
504 FOR_ALL_WINDOWS(wp)
506 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
507 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
509 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
511 #ifdef FEAT_MENU
512 gui_create_initial_menus(root_menu);
513 #endif
514 #ifdef FEAT_SUN_WORKSHOP
515 if (usingSunWorkShop)
516 workshop_init();
517 #endif
518 #ifdef FEAT_SIGN_ICONS
519 sign_gui_started();
520 #endif
522 /* Configure the desired menu and scrollbars */
523 gui_init_which_components(NULL);
525 /* All components of the GUI have been created now */
526 gui.shell_created = TRUE;
528 #ifndef FEAT_GUI_GTK
529 /* Set the shell size, adjusted for the screen size. For GTK this only
530 * works after the shell has been opened, thus it is further down. */
531 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
532 #endif
533 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
534 /* Need to set the size of the menubar after all the menus have been
535 * created. */
536 gui_mch_compute_menu_height((Widget)0);
537 #endif
540 * Actually open the GUI shell.
542 if (gui_mch_open() != FAIL)
544 #ifdef FEAT_TITLE
545 maketitle();
546 resettitle();
547 #endif
548 init_gui_options();
549 #ifdef FEAT_ARABIC
550 /* Our GUI can't do bidi. */
551 p_tbidi = FALSE;
552 #endif
553 #if defined(FEAT_GUI_GTK)
554 /* Give GTK+ a chance to put all widget's into place. */
555 gui_mch_update();
557 # ifdef FEAT_MENU
558 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
559 * It was still there to make F10 work. */
560 if (vim_strchr(p_go, GO_MENUS) == NULL)
562 --gui.starting;
563 gui_mch_enable_menu(FALSE);
564 ++gui.starting;
565 gui_mch_update();
567 # endif
569 /* Now make sure the shell fits on the screen. */
570 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
571 #endif
572 /* When 'lines' was set while starting up the topframe may have to be
573 * resized. */
574 win_new_shellsize();
576 #ifdef FEAT_BEVAL
577 /* Always create the Balloon Evaluation area, but disable it when
578 * 'ballooneval' is off */
579 # ifdef FEAT_GUI_GTK
580 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
581 &general_beval_cb, NULL);
582 # else
583 # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
585 extern Widget textArea;
586 balloonEval = gui_mch_create_beval_area(textArea, NULL,
587 &general_beval_cb, NULL);
589 # else
590 # ifdef FEAT_GUI_W32
591 balloonEval = gui_mch_create_beval_area(NULL, NULL,
592 &general_beval_cb, NULL);
593 # endif
594 # endif
595 # endif
596 if (!p_beval)
597 gui_mch_disable_beval_area(balloonEval);
598 #endif
600 #ifdef FEAT_NETBEANS_INTG
601 if (starting == 0 && usingNetbeans)
602 /* Tell the client that it can start sending commands. */
603 netbeans_startup_done();
604 #endif
605 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
606 if (!im_xim_isvalid_imactivate())
607 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
608 #endif
609 /* When 'cmdheight' was set during startup it may not have taken
610 * effect yet. */
611 if (p_ch != 1L)
612 command_height();
614 return;
617 error2:
618 #ifdef FEAT_GUI_X11
619 /* undo gui_mch_init() */
620 gui_mch_uninit();
621 #endif
623 error:
624 gui.in_use = FALSE;
625 clip_init(FALSE);
629 void
630 gui_exit(rc)
631 int rc;
633 #ifndef __BEOS__
634 /* don't free the fonts, it leads to a BUS error
635 * richard@whitequeen.com Jul 99 */
636 free_highlight_fonts();
637 #endif
638 gui.in_use = FALSE;
639 gui_mch_exit(rc);
642 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
643 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
644 || defined(PROTO) || defined(FEAT_GUI_MACVIM)
645 # define NEED_GUI_UPDATE_SCREEN 1
647 * Called when the GUI shell is closed by the user. If there are no changed
648 * files Vim exits, otherwise there will be a dialog to ask the user what to
649 * do.
650 * When this function returns, Vim should NOT exit!
652 void
653 gui_shell_closed()
655 cmdmod_T save_cmdmod;
657 save_cmdmod = cmdmod;
659 /* Only exit when there are no changed files */
660 exiting = TRUE;
661 # ifdef FEAT_BROWSE
662 cmdmod.browse = TRUE;
663 # endif
664 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
665 cmdmod.confirm = TRUE;
666 # endif
667 /* If there are changed buffers, present the user with a dialog if
668 * possible, otherwise give an error message. */
669 if (!check_changed_any(FALSE))
670 getout(0);
672 exiting = FALSE;
673 cmdmod = save_cmdmod;
674 gui_update_screen(); /* redraw, window may show changed buffer */
676 #endif
679 * Set the font. "font_list" is a a comma separated list of font names. The
680 * first font name that works is used. If none is found, use the default
681 * font.
682 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
683 * Return OK when able to set the font. When it failed FAIL is returned and
684 * the fonts are unchanged.
687 gui_init_font(font_list, fontset)
688 char_u *font_list;
689 int fontset UNUSED;
691 #define FONTLEN 320
692 char_u font_name[FONTLEN];
693 int font_list_empty = FALSE;
694 int ret = FAIL;
696 if (!gui.in_use)
697 return FAIL;
699 font_name[0] = NUL;
700 if (*font_list == NUL)
701 font_list_empty = TRUE;
702 else
704 #ifdef FEAT_XFONTSET
705 /* When using a fontset, the whole list of fonts is one name. */
706 if (fontset)
707 ret = gui_mch_init_font(font_list, TRUE);
708 else
709 #endif
710 while (*font_list != NUL)
712 /* Isolate one comma separated font name. */
713 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
715 #if defined(FEAT_GUI_MACVIM)
716 /* The font dialog is modeless in Mac OS X, so when
717 * gui_mch_init_font() is called with "*" it brings up the
718 * dialog and returns immediately. In this case we don't want
719 * it to be called again with NULL, so return here. */
720 if (STRCMP(font_name, "*") == 0) {
721 gui_mch_init_font(font_name, FALSE);
722 return FALSE;
724 #endif
726 /* Careful!!! The Win32 version of gui_mch_init_font(), when
727 * called with "*" will change p_guifont to the selected font
728 * name, which frees the old value. This makes font_list
729 * invalid. Thus when OK is returned here, font_list must no
730 * longer be used! */
731 if (gui_mch_init_font(font_name, FALSE) == OK)
733 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
734 /* If it's a Unicode font, try setting 'guifontwide' to a
735 * similar double-width font. */
736 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
737 && strstr((char *)font_name, "10646") != NULL)
738 set_guifontwide(font_name);
739 #endif
740 ret = OK;
741 break;
746 if (ret != OK
747 && STRCMP(font_list, "*") != 0
748 && (font_list_empty || gui.norm_font == NOFONT))
751 * Couldn't load any font in 'font_list', keep the current font if
752 * there is one. If 'font_list' is empty, or if there is no current
753 * font, tell gui_mch_init_font() to try to find a font we can load.
755 ret = gui_mch_init_font(NULL, FALSE);
758 if (ret == OK)
760 #ifndef HAVE_GTK2
761 /* Set normal font as current font */
762 # ifdef FEAT_XFONTSET
763 if (gui.fontset != NOFONTSET)
764 gui_mch_set_fontset(gui.fontset);
765 else
766 # endif
767 gui_mch_set_font(gui.norm_font);
768 #endif
769 gui_set_shellsize(FALSE,
770 #ifdef MSWIN
771 TRUE
772 #else
773 FALSE
774 #endif
775 , RESIZE_BOTH);
778 return ret;
781 #if defined(FEAT_MBYTE) || defined(PROTO)
782 # ifndef HAVE_GTK2
784 * Try setting 'guifontwide' to a font twice as wide as "name".
786 static void
787 set_guifontwide(name)
788 char_u *name;
790 int i = 0;
791 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
792 char_u *wp = NULL;
793 char_u *p;
794 GuiFont font;
796 wp = wide_name;
797 for (p = name; *p != NUL; ++p)
799 *wp++ = *p;
800 if (*p == '-')
802 ++i;
803 if (i == 6) /* font type: change "--" to "-*-" */
805 if (p[1] == '-')
806 *wp++ = '*';
808 else if (i == 12) /* found the width */
810 ++p;
811 i = getdigits(&p);
812 if (i != 0)
814 /* Double the width specification. */
815 sprintf((char *)wp, "%d%s", i * 2, p);
816 font = gui_mch_get_font(wide_name, FALSE);
817 if (font != NOFONT)
819 gui_mch_free_font(gui.wide_font);
820 gui.wide_font = font;
821 set_string_option_direct((char_u *)"gfw", -1,
822 wide_name, OPT_FREE, 0);
825 break;
830 # endif /* !HAVE_GTK2 */
833 * Get the font for 'guifontwide'.
834 * Return FAIL for an invalid font name.
837 gui_get_wide_font()
839 GuiFont font = NOFONT;
840 char_u font_name[FONTLEN];
841 char_u *p;
843 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
844 return OK; /* Will give an error message later. */
846 if (p_guifontwide != NULL && *p_guifontwide != NUL)
848 for (p = p_guifontwide; *p != NUL; )
850 /* Isolate one comma separated font name. */
851 (void)copy_option_part(&p, font_name, FONTLEN, ",");
852 font = gui_mch_get_font(font_name, FALSE);
853 if (font != NOFONT)
854 break;
856 if (font == NOFONT)
857 return FAIL;
860 gui_mch_free_font(gui.wide_font);
861 #ifdef HAVE_GTK2
862 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
863 if (font != NOFONT && gui.norm_font != NOFONT
864 && pango_font_description_equal(font, gui.norm_font))
866 gui.wide_font = NOFONT;
867 gui_mch_free_font(font);
869 else
870 #endif
871 gui.wide_font = font;
872 return OK;
874 #endif
876 void
877 gui_set_cursor(row, col)
878 int row;
879 int col;
881 gui.row = row;
882 gui.col = col;
886 * gui_check_pos - check if the cursor is on the screen.
888 static void
889 gui_check_pos()
891 if (gui.row >= screen_Rows)
892 gui.row = screen_Rows - 1;
893 if (gui.col >= screen_Columns)
894 gui.col = screen_Columns - 1;
895 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
896 gui.cursor_is_valid = FALSE;
900 * Redraw the cursor if necessary or when forced.
901 * Careful: The contents of ScreenLines[] must match what is on the screen,
902 * otherwise this goes wrong. May need to call out_flush() first.
904 void
905 gui_update_cursor(force, clear_selection)
906 int force; /* when TRUE, update even when not moved */
907 int clear_selection;/* clear selection under cursor */
909 int cur_width = 0;
910 int cur_height = 0;
911 int old_hl_mask;
912 int idx;
913 int id;
914 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
915 int cattr; /* cursor attributes */
916 int attr;
917 attrentry_T *aep = NULL;
919 /* Don't update the cursor when halfway busy scrolling or the screen size
920 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
921 if (!can_update_cursor || screen_Columns != gui.num_cols
922 || screen_Rows != gui.num_rows)
923 return;
925 gui_check_pos();
926 if (!gui.cursor_is_valid || force
927 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
929 gui_undraw_cursor();
930 if (gui.row < 0)
931 return;
932 #ifdef USE_IM_CONTROL
933 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
934 im_set_position(gui.row, gui.col);
935 #endif
936 gui.cursor_row = gui.row;
937 gui.cursor_col = gui.col;
939 /* Only write to the screen after ScreenLines[] has been initialized */
940 if (!screen_cleared || ScreenLines == NULL)
941 return;
943 /* Clear the selection if we are about to write over it */
944 if (clear_selection)
945 clip_may_clear_selection(gui.row, gui.row);
946 /* Check that the cursor is inside the shell (resizing may have made
947 * it invalid) */
948 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
949 return;
951 gui.cursor_is_valid = TRUE;
954 * How the cursor is drawn depends on the current mode.
956 idx = get_shape_idx(FALSE);
957 if (State & LANGMAP)
958 id = shape_table[idx].id_lm;
959 else
960 id = shape_table[idx].id;
962 /* get the colors and attributes for the cursor. Default is inverted */
963 cfg = INVALCOLOR;
964 cbg = INVALCOLOR;
965 cattr = HL_INVERSE;
966 gui_mch_set_blinking(shape_table[idx].blinkwait,
967 shape_table[idx].blinkon,
968 shape_table[idx].blinkoff);
969 if (id > 0)
971 cattr = syn_id2colors(id, &cfg, &cbg);
972 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
974 static int iid;
975 guicolor_T fg, bg;
977 if (
978 # if defined(HAVE_GTK2) && !defined(FEAT_HANGULIN)
979 preedit_get_status()
980 # else
981 im_get_status()
982 # endif
985 iid = syn_name2id((char_u *)"CursorIM");
986 if (iid > 0)
988 syn_id2colors(iid, &fg, &bg);
989 if (bg != INVALCOLOR)
990 cbg = bg;
991 if (fg != INVALCOLOR)
992 cfg = fg;
996 #endif
1000 * Get the attributes for the character under the cursor.
1001 * When no cursor color was given, use the character color.
1003 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1004 if (attr > HL_ALL)
1005 aep = syn_gui_attr2entry(attr);
1006 if (aep != NULL)
1008 attr = aep->ae_attr;
1009 if (cfg == INVALCOLOR)
1010 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1011 : aep->ae_u.gui.fg_color);
1012 if (cbg == INVALCOLOR)
1013 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1014 : aep->ae_u.gui.bg_color);
1016 if (cfg == INVALCOLOR)
1017 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1018 if (cbg == INVALCOLOR)
1019 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1021 #ifdef FEAT_XIM
1022 if (aep != NULL)
1024 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1025 : aep->ae_u.gui.bg_color);
1026 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1027 : aep->ae_u.gui.fg_color);
1028 if (xim_bg_color == INVALCOLOR)
1029 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1030 : gui.back_pixel;
1031 if (xim_fg_color == INVALCOLOR)
1032 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1033 : gui.norm_pixel;
1035 else
1037 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1038 : gui.back_pixel;
1039 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1040 : gui.norm_pixel;
1042 #endif
1044 attr &= ~HL_INVERSE;
1045 if (cattr & HL_INVERSE)
1047 cc = cbg;
1048 cbg = cfg;
1049 cfg = cc;
1051 cattr &= ~HL_INVERSE;
1054 * When we don't have window focus, draw a hollow cursor.
1056 if (!gui.in_focus)
1058 gui_mch_draw_hollow_cursor(cbg);
1059 return;
1062 old_hl_mask = gui.highlight_mask;
1063 if (shape_table[idx].shape == SHAPE_BLOCK
1064 #ifdef FEAT_HANGULIN
1065 || composing_hangul
1066 #endif
1070 * Draw the text character with the cursor colors. Use the
1071 * character attributes plus the cursor attributes.
1073 gui.highlight_mask = (cattr | attr);
1074 #ifdef FEAT_HANGULIN
1075 if (composing_hangul)
1076 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1077 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1078 else
1079 #endif
1080 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1081 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1083 else
1085 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1086 int col_off = FALSE;
1087 #endif
1089 * First draw the partial cursor, then overwrite with the text
1090 * character, using a transparent background.
1092 if (shape_table[idx].shape == SHAPE_VER)
1094 cur_height = gui.char_height;
1095 cur_width = (gui.char_width * shape_table[idx].percentage
1096 + 99) / 100;
1098 else
1100 cur_height = (gui.char_height * shape_table[idx].percentage
1101 + 99) / 100;
1102 cur_width = gui.char_width;
1104 #ifdef FEAT_MBYTE
1105 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1106 LineOffset[gui.row] + screen_Columns) > 1)
1108 /* Double wide character. */
1109 if (shape_table[idx].shape != SHAPE_VER)
1110 cur_width += gui.char_width;
1111 # ifdef FEAT_RIGHTLEFT
1112 if (CURSOR_BAR_RIGHT)
1114 /* gui.col points to the left halve of the character but
1115 * the vertical line needs to be on the right halve.
1116 * A double-wide horizontal line is also drawn from the
1117 * right halve in gui_mch_draw_part_cursor(). */
1118 col_off = TRUE;
1119 ++gui.col;
1121 # endif
1123 #endif
1124 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1125 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1126 if (col_off)
1127 --gui.col;
1128 #endif
1130 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1131 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1132 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1133 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1134 (guicolor_T)0, (guicolor_T)0, 0);
1135 #endif
1137 gui.highlight_mask = old_hl_mask;
1141 #if defined(FEAT_MENU) || defined(PROTO)
1142 void
1143 gui_position_menu()
1145 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
1146 || defined(FEAT_GUI_MACVIM))
1147 if (gui.menu_is_active && gui.in_use)
1148 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1149 # endif
1151 #endif
1154 * Position the various GUI components (text area, menu). The vertical
1155 * scrollbars are NOT handled here. See gui_update_scrollbars().
1157 static void
1158 gui_position_components(total_width)
1159 int total_width UNUSED;
1161 int text_area_x;
1162 int text_area_y;
1163 int text_area_width;
1164 int text_area_height;
1166 /* avoid that moving components around generates events */
1167 ++hold_gui_events;
1169 text_area_x = 0;
1170 if (gui.which_scrollbars[SBAR_LEFT])
1171 text_area_x += gui.scrollbar_width;
1173 text_area_y = 0;
1174 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
1175 || defined(FEAT_GUI_MACVIM))
1176 gui.menu_width = total_width;
1177 if (gui.menu_is_active)
1178 text_area_y += gui.menu_height;
1179 #endif
1180 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1181 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1182 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1183 #endif
1185 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1186 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1187 if (gui_has_tabline())
1188 text_area_y += gui.tabline_height;
1189 #endif
1191 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1192 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1194 # ifdef FEAT_GUI_ATHENA
1195 gui_mch_set_toolbar_pos(0, text_area_y,
1196 gui.menu_width, gui.toolbar_height);
1197 # endif
1198 text_area_y += gui.toolbar_height;
1200 #endif
1202 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1203 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1205 gui_mch_set_text_area_pos(text_area_x,
1206 text_area_y,
1207 text_area_width,
1208 text_area_height
1209 #if defined(FEAT_XIM) && !(defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM))
1210 + xim_get_status_area_height()
1211 #endif
1213 #ifdef FEAT_MENU
1214 gui_position_menu();
1215 #endif
1216 if (gui.which_scrollbars[SBAR_BOTTOM])
1217 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1218 text_area_x,
1219 text_area_y + text_area_height,
1220 text_area_width,
1221 gui.scrollbar_height);
1222 gui.left_sbar_x = 0;
1223 gui.right_sbar_x = text_area_x + text_area_width;
1225 --hold_gui_events;
1229 * Get the width of the widgets and decorations to the side of the text area.
1232 gui_get_base_width()
1234 int base_width;
1236 base_width = 2 * gui.border_offset;
1237 if (gui.which_scrollbars[SBAR_LEFT])
1238 base_width += gui.scrollbar_width;
1239 if (gui.which_scrollbars[SBAR_RIGHT])
1240 base_width += gui.scrollbar_width;
1241 return base_width;
1245 * Get the height of the widgets and decorations above and below the text area.
1248 gui_get_base_height()
1250 int base_height;
1252 base_height = 2 * gui.border_offset;
1253 if (gui.which_scrollbars[SBAR_BOTTOM])
1254 base_height += gui.scrollbar_height;
1255 #ifdef FEAT_GUI_GTK
1256 /* We can't take the sizes properly into account until anything is
1257 * realized. Therefore we recalculate all the values here just before
1258 * setting the size. (--mdcki) */
1259 #elif !defined(FEAT_GUI_MACVIM)
1260 # ifdef FEAT_MENU
1261 if (gui.menu_is_active)
1262 base_height += gui.menu_height;
1263 # endif
1264 # ifdef FEAT_TOOLBAR
1265 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1266 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1267 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1268 # else
1269 base_height += gui.toolbar_height;
1270 # endif
1271 # endif
1272 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1273 || defined(FEAT_GUI_MOTIF))
1274 if (gui_has_tabline())
1275 base_height += gui.tabline_height;
1276 # endif
1277 # ifdef FEAT_FOOTER
1278 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1279 base_height += gui.footer_height;
1280 # endif
1281 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1282 base_height += gui_mch_text_area_extra_height();
1283 # endif
1284 #endif
1285 return base_height;
1289 * Should be called after the GUI shell has been resized. Its arguments are
1290 * the new width and height of the shell in pixels.
1292 void
1293 gui_resize_shell(pixel_width, pixel_height)
1294 int pixel_width;
1295 int pixel_height;
1297 static int busy = FALSE;
1299 if (!gui.shell_created) /* ignore when still initializing */
1300 return;
1303 * Can't resize the screen while it is being redrawn. Remember the new
1304 * size and handle it later.
1306 if (updating_screen || busy)
1308 new_pixel_width = pixel_width;
1309 new_pixel_height = pixel_height;
1310 return;
1313 again:
1314 busy = TRUE;
1316 /* Flush pending output before redrawing */
1317 out_flush();
1319 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1320 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
1322 gui_position_components(pixel_width);
1324 gui_reset_scroll_region();
1326 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1327 * at the last line here (why does it have to be one row too low?).
1329 if (State == ASKMORE || State == CONFIRM)
1330 gui.row = gui.num_rows;
1332 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1333 * the safe side. */
1334 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1335 || gui.num_rows != Rows || gui.num_cols != Columns)
1336 shell_resized();
1338 gui_update_scrollbars(TRUE);
1339 gui_update_cursor(FALSE, TRUE);
1340 #if defined(FEAT_XIM) && !(defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM))
1341 xim_set_status_area();
1342 #endif
1344 busy = FALSE;
1347 * We could have been called again while redrawing the screen.
1348 * Need to do it all again with the latest size then.
1350 if (new_pixel_height)
1352 pixel_width = new_pixel_width;
1353 pixel_height = new_pixel_height;
1354 new_pixel_width = 0;
1355 new_pixel_height = 0;
1356 goto again;
1361 * Check if gui_resize_shell() must be called.
1363 void
1364 gui_may_resize_shell()
1366 int h, w;
1368 if (new_pixel_height)
1370 /* careful: gui_resize_shell() may postpone the resize again if we
1371 * were called indirectly by it */
1372 w = new_pixel_width;
1373 h = new_pixel_height;
1374 new_pixel_width = 0;
1375 new_pixel_height = 0;
1376 gui_resize_shell(w, h);
1381 gui_get_shellsize()
1383 Rows = gui.num_rows;
1384 Columns = gui.num_cols;
1385 return OK;
1389 * Set the size of the Vim shell according to Rows and Columns.
1390 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1391 * on the screen.
1393 void
1394 gui_set_shellsize(mustset, fit_to_display, direction)
1395 int mustset UNUSED; /* set by the user */
1396 int fit_to_display;
1397 int direction; /* RESIZE_HOR, RESIZE_VER */
1399 int base_width;
1400 int base_height;
1401 int width;
1402 int height;
1403 int min_width;
1404 int min_height;
1405 int screen_w;
1406 int screen_h;
1408 if (!gui.shell_created)
1409 return;
1411 #ifdef MSWIN
1412 /* If not setting to a user specified size and maximized, calculate the
1413 * number of characters that fit in the maximized window. */
1414 if (!mustset && gui_mch_maximized())
1416 gui_mch_newfont();
1417 return;
1419 #endif
1421 base_width = gui_get_base_width();
1422 base_height = gui_get_base_height();
1423 #ifdef USE_SUN_WORKSHOP
1424 if (!mustset && usingSunWorkShop
1425 && workshop_get_width_height(&width, &height))
1427 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1428 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1430 else
1431 #endif
1433 width = Columns * gui.char_width + base_width;
1434 height = Rows * gui.char_height + base_height;
1437 if (fit_to_display)
1439 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1440 if ((direction & RESIZE_HOR) && width > screen_w)
1442 Columns = (screen_w - base_width) / gui.char_width;
1443 if (Columns < MIN_COLUMNS)
1444 Columns = MIN_COLUMNS;
1445 width = Columns * gui.char_width + base_width;
1447 if ((direction & RESIZE_VERT) && height > screen_h)
1449 Rows = (screen_h - base_height) / gui.char_height;
1450 check_shellsize();
1451 height = Rows * gui.char_height + base_height;
1454 gui.num_cols = Columns;
1455 gui.num_rows = Rows;
1457 min_width = base_width + MIN_COLUMNS * gui.char_width;
1458 min_height = base_height + MIN_LINES * gui.char_height;
1459 # ifdef FEAT_WINDOWS
1460 min_height += tabline_height() * gui.char_height;
1461 # endif
1463 gui_mch_set_shellsize(width, height, min_width, min_height,
1464 base_width, base_height, direction);
1465 if (fit_to_display)
1467 int x, y;
1469 /* Some window managers put the Vim window left of/above the screen. */
1470 gui_mch_update();
1471 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1472 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1475 gui_position_components(width);
1476 gui_update_scrollbars(TRUE);
1477 gui_reset_scroll_region();
1481 * Called when Rows and/or Columns has changed.
1483 void
1484 gui_new_shellsize()
1486 gui_reset_scroll_region();
1490 * Make scroll region cover whole screen.
1492 void
1493 gui_reset_scroll_region()
1495 gui.scroll_region_top = 0;
1496 gui.scroll_region_bot = gui.num_rows - 1;
1497 gui.scroll_region_left = 0;
1498 gui.scroll_region_right = gui.num_cols - 1;
1501 void
1502 gui_start_highlight(mask)
1503 int mask;
1505 if (mask > HL_ALL) /* highlight code */
1506 gui.highlight_mask = mask;
1507 else /* mask */
1508 gui.highlight_mask |= mask;
1511 void
1512 gui_stop_highlight(mask)
1513 int mask;
1515 if (mask > HL_ALL) /* highlight code */
1516 gui.highlight_mask = HL_NORMAL;
1517 else /* mask */
1518 gui.highlight_mask &= ~mask;
1522 * Clear a rectangular region of the screen from text pos (row1, col1) to
1523 * (row2, col2) inclusive.
1525 void
1526 gui_clear_block(row1, col1, row2, col2)
1527 int row1;
1528 int col1;
1529 int row2;
1530 int col2;
1532 /* Clear the selection if we are about to write over it */
1533 clip_may_clear_selection(row1, row2);
1535 gui_mch_clear_block(row1, col1, row2, col2);
1537 /* Invalidate cursor if it was in this block */
1538 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1539 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1540 gui.cursor_is_valid = FALSE;
1544 * Write code to update the cursor later. This avoids the need to flush the
1545 * output buffer before calling gui_update_cursor().
1547 void
1548 gui_update_cursor_later()
1550 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1553 void
1554 gui_write(s, len)
1555 char_u *s;
1556 int len;
1558 char_u *p;
1559 int arg1 = 0, arg2 = 0;
1560 /* this doesn't make sense, disabled until someone can explain why it
1561 * would be needed */
1562 #if 0 && (defined(RISCOS) || defined(WIN16))
1563 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1564 our redraw speed will suffer */
1565 #else
1566 int force_cursor = FALSE; /* force cursor update */
1567 #endif
1568 int force_scrollbar = FALSE;
1569 static win_T *old_curwin = NULL;
1571 /* #define DEBUG_GUI_WRITE */
1572 #ifdef DEBUG_GUI_WRITE
1574 int i;
1575 char_u *str;
1577 printf("gui_write(%d):\n ", len);
1578 for (i = 0; i < len; i++)
1579 if (s[i] == ESC)
1581 if (i != 0)
1582 printf("\n ");
1583 printf("<ESC>");
1585 else
1587 str = transchar_byte(s[i]);
1588 if (str[0] && str[1])
1589 printf("<%s>", (char *)str);
1590 else
1591 printf("%s", (char *)str);
1593 printf("\n");
1595 #endif
1596 while (len)
1598 if (s[0] == ESC && s[1] == '|')
1600 p = s + 2;
1601 if (VIM_ISDIGIT(*p))
1603 arg1 = getdigits(&p);
1604 if (p > s + len)
1605 break;
1606 if (*p == ';')
1608 ++p;
1609 arg2 = getdigits(&p);
1610 if (p > s + len)
1611 break;
1614 switch (*p)
1616 case 'C': /* Clear screen */
1617 clip_scroll_selection(9999);
1618 gui_mch_clear_all();
1619 gui.cursor_is_valid = FALSE;
1620 force_scrollbar = TRUE;
1621 break;
1622 case 'M': /* Move cursor */
1623 gui_set_cursor(arg1, arg2);
1624 break;
1625 case 's': /* force cursor (shape) update */
1626 force_cursor = TRUE;
1627 break;
1628 case 'R': /* Set scroll region */
1629 if (arg1 < arg2)
1631 gui.scroll_region_top = arg1;
1632 gui.scroll_region_bot = arg2;
1634 else
1636 gui.scroll_region_top = arg2;
1637 gui.scroll_region_bot = arg1;
1639 break;
1640 #ifdef FEAT_VERTSPLIT
1641 case 'V': /* Set vertical scroll region */
1642 if (arg1 < arg2)
1644 gui.scroll_region_left = arg1;
1645 gui.scroll_region_right = arg2;
1647 else
1649 gui.scroll_region_left = arg2;
1650 gui.scroll_region_right = arg1;
1652 break;
1653 #endif
1654 case 'd': /* Delete line */
1655 gui_delete_lines(gui.row, 1);
1656 break;
1657 case 'D': /* Delete lines */
1658 gui_delete_lines(gui.row, arg1);
1659 break;
1660 case 'i': /* Insert line */
1661 gui_insert_lines(gui.row, 1);
1662 break;
1663 case 'I': /* Insert lines */
1664 gui_insert_lines(gui.row, arg1);
1665 break;
1666 case '$': /* Clear to end-of-line */
1667 gui_clear_block(gui.row, gui.col, gui.row,
1668 (int)Columns - 1);
1669 break;
1670 case 'h': /* Turn on highlighting */
1671 gui_start_highlight(arg1);
1672 break;
1673 case 'H': /* Turn off highlighting */
1674 gui_stop_highlight(arg1);
1675 break;
1676 case 'f': /* flash the window (visual bell) */
1677 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1678 break;
1679 default:
1680 p = s + 1; /* Skip the ESC */
1681 break;
1683 len -= (int)(++p - s);
1684 s = p;
1686 else if (
1687 #ifdef EBCDIC
1688 CtrlChar(s[0]) != 0 /* Ctrl character */
1689 #else
1690 s[0] < 0x20 /* Ctrl character */
1691 #endif
1692 #ifdef FEAT_SIGN_ICONS
1693 && s[0] != SIGN_BYTE
1694 # ifdef FEAT_NETBEANS_INTG
1695 && s[0] != MULTISIGN_BYTE
1696 # endif
1697 #endif
1700 if (s[0] == '\n') /* NL */
1702 gui.col = 0;
1703 if (gui.row < gui.scroll_region_bot)
1704 gui.row++;
1705 else
1706 gui_delete_lines(gui.scroll_region_top, 1);
1708 else if (s[0] == '\r') /* CR */
1710 gui.col = 0;
1712 else if (s[0] == '\b') /* Backspace */
1714 if (gui.col)
1715 --gui.col;
1717 else if (s[0] == Ctrl_L) /* cursor-right */
1719 ++gui.col;
1721 else if (s[0] == Ctrl_G) /* Beep */
1723 gui_mch_beep();
1725 /* Other Ctrl character: shouldn't happen! */
1727 --len; /* Skip this char */
1728 ++s;
1730 else
1732 p = s;
1733 while (len > 0 && (
1734 #ifdef EBCDIC
1735 CtrlChar(*p) == 0
1736 #else
1737 *p >= 0x20
1738 #endif
1739 #ifdef FEAT_SIGN_ICONS
1740 || *p == SIGN_BYTE
1741 # ifdef FEAT_NETBEANS_INTG
1742 || *p == MULTISIGN_BYTE
1743 # endif
1744 #endif
1747 len--;
1748 p++;
1750 gui_outstr(s, (int)(p - s));
1751 s = p;
1755 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1756 * set). */
1757 if (force_cursor)
1758 gui_update_cursor(TRUE, TRUE);
1760 /* When switching to another window the dragging must have stopped.
1761 * Required for GTK, dragged_sb isn't reset. */
1762 if (old_curwin != curwin)
1763 gui.dragged_sb = SBAR_NONE;
1765 /* Update the scrollbars after clearing the screen or when switched
1766 * to another window.
1767 * Update the horizontal scrollbar always, it's difficult to check all
1768 * situations where it might change. */
1769 if (force_scrollbar || old_curwin != curwin)
1770 gui_update_scrollbars(force_scrollbar);
1771 else
1772 gui_update_horiz_scrollbar(FALSE);
1773 old_curwin = curwin;
1776 * We need to make sure this is cleared since Athena doesn't tell us when
1777 * he is done dragging. Do the same for GTK.
1779 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1780 gui.dragged_sb = SBAR_NONE;
1781 #endif
1783 gui_mch_flush(); /* In case vim decides to take a nap */
1787 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1788 * produces wrong results. Call gui_dont_update_cursor() before that code and
1789 * gui_can_update_cursor() afterwards.
1791 void
1792 gui_dont_update_cursor()
1794 if (gui.in_use)
1796 /* Undraw the cursor now, we probably can't do it after the change. */
1797 gui_undraw_cursor();
1798 can_update_cursor = FALSE;
1802 void
1803 gui_can_update_cursor()
1805 can_update_cursor = TRUE;
1806 /* No need to update the cursor right now, there is always more output
1807 * after scrolling. */
1810 static void
1811 gui_outstr(s, len)
1812 char_u *s;
1813 int len;
1815 int this_len;
1816 #ifdef FEAT_MBYTE
1817 int cells;
1818 #endif
1820 if (len == 0)
1821 return;
1823 if (len < 0)
1824 len = (int)STRLEN(s);
1826 while (len > 0)
1828 #ifdef FEAT_MBYTE
1829 if (has_mbyte)
1831 /* Find out how many chars fit in the current line. */
1832 cells = 0;
1833 for (this_len = 0; this_len < len; )
1835 cells += (*mb_ptr2cells)(s + this_len);
1836 if (gui.col + cells > Columns)
1837 break;
1838 this_len += (*mb_ptr2len)(s + this_len);
1840 if (this_len > len)
1841 this_len = len; /* don't include following composing char */
1843 else
1844 #endif
1845 if (gui.col + len > Columns)
1846 this_len = Columns - gui.col;
1847 else
1848 this_len = len;
1850 (void)gui_outstr_nowrap(s, this_len,
1851 0, (guicolor_T)0, (guicolor_T)0, 0);
1852 s += this_len;
1853 len -= this_len;
1854 #ifdef FEAT_MBYTE
1855 /* fill up for a double-width char that doesn't fit. */
1856 if (len > 0 && gui.col < Columns)
1857 (void)gui_outstr_nowrap((char_u *)" ", 1,
1858 0, (guicolor_T)0, (guicolor_T)0, 0);
1859 #endif
1860 /* The cursor may wrap to the next line. */
1861 if (gui.col >= Columns)
1863 gui.col = 0;
1864 gui.row++;
1870 * Output one character (may be one or two display cells).
1871 * Caller must check for valid "off".
1872 * Returns FAIL or OK, just like gui_outstr_nowrap().
1874 static int
1875 gui_screenchar(off, flags, fg, bg, back)
1876 int off; /* Offset from start of screen */
1877 int flags;
1878 guicolor_T fg, bg; /* colors for cursor */
1879 int back; /* backup this many chars when using bold trick */
1881 #ifdef FEAT_MBYTE
1882 char_u buf[MB_MAXBYTES + 1];
1884 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1885 if (enc_utf8 && ScreenLines[off] == 0)
1886 return OK;
1888 if (enc_utf8 && ScreenLinesUC[off] != 0)
1889 /* Draw UTF-8 multi-byte character. */
1890 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1891 flags, fg, bg, back);
1893 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1895 buf[0] = ScreenLines[off];
1896 buf[1] = ScreenLines2[off];
1897 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1900 /* Draw non-multi-byte character or DBCS character. */
1901 return gui_outstr_nowrap(ScreenLines + off,
1902 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1903 flags, fg, bg, back);
1904 #else
1905 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1906 #endif
1909 #ifdef HAVE_GTK2
1911 * Output the string at the given screen position. This is used in place
1912 * of gui_screenchar() where possible because Pango needs as much context
1913 * as possible to work nicely. It's a lot faster as well.
1915 static int
1916 gui_screenstr(off, len, flags, fg, bg, back)
1917 int off; /* Offset from start of screen */
1918 int len; /* string length in screen cells */
1919 int flags;
1920 guicolor_T fg, bg; /* colors for cursor */
1921 int back; /* backup this many chars when using bold trick */
1923 char_u *buf;
1924 int outlen = 0;
1925 int i;
1926 int retval;
1928 if (len <= 0) /* "cannot happen"? */
1929 return OK;
1931 if (enc_utf8)
1933 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1934 if (buf == NULL)
1935 return OK; /* not much we could do here... */
1937 for (i = off; i < off + len; ++i)
1939 if (ScreenLines[i] == 0)
1940 continue; /* skip second half of double-width char */
1942 if (ScreenLinesUC[i] == 0)
1943 buf[outlen++] = ScreenLines[i];
1944 else
1945 outlen += utfc_char2bytes(i, buf + outlen);
1948 buf[outlen] = NUL; /* only to aid debugging */
1949 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1950 vim_free(buf);
1952 return retval;
1954 else if (enc_dbcs == DBCS_JPNU)
1956 buf = alloc((unsigned)(len * 2 + 1));
1957 if (buf == NULL)
1958 return OK; /* not much we could do here... */
1960 for (i = off; i < off + len; ++i)
1962 buf[outlen++] = ScreenLines[i];
1964 /* handle double-byte single-width char */
1965 if (ScreenLines[i] == 0x8e)
1966 buf[outlen++] = ScreenLines2[i];
1967 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1968 buf[outlen++] = ScreenLines[++i];
1971 buf[outlen] = NUL; /* only to aid debugging */
1972 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1973 vim_free(buf);
1975 return retval;
1977 else
1979 return gui_outstr_nowrap(&ScreenLines[off], len,
1980 flags, fg, bg, back);
1983 #endif /* HAVE_GTK2 */
1986 * Output the given string at the current cursor position. If the string is
1987 * too long to fit on the line, then it is truncated.
1988 * "flags":
1989 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1990 * actually draw (an inverted) cursor.
1991 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
1992 * background.
1993 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1994 * it.
1995 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1996 * FAIL (the caller should start drawing "back" chars back).
1999 gui_outstr_nowrap(s, len, flags, fg, bg, back)
2000 char_u *s;
2001 int len;
2002 int flags;
2003 guicolor_T fg, bg; /* colors for cursor */
2004 int back; /* backup this many chars when using bold trick */
2006 long_u highlight_mask;
2007 long_u hl_mask_todo;
2008 guicolor_T fg_color;
2009 guicolor_T bg_color;
2010 guicolor_T sp_color;
2011 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2012 GuiFont font = NOFONT;
2013 # ifdef FEAT_XFONTSET
2014 GuiFontset fontset = NOFONTSET;
2015 # endif
2016 #endif
2017 attrentry_T *aep = NULL;
2018 int draw_flags;
2019 int col = gui.col;
2020 #ifdef FEAT_SIGN_ICONS
2021 int draw_sign = FALSE;
2022 # ifdef FEAT_NETBEANS_INTG
2023 int multi_sign = FALSE;
2024 # endif
2025 #endif
2027 if (len < 0)
2028 len = (int)STRLEN(s);
2029 if (len == 0)
2030 return OK;
2032 #ifdef FEAT_SIGN_ICONS
2033 if (*s == SIGN_BYTE
2034 # ifdef FEAT_NETBEANS_INTG
2035 || *s == MULTISIGN_BYTE
2036 # endif
2039 # ifdef FEAT_NETBEANS_INTG
2040 if (*s == MULTISIGN_BYTE)
2041 multi_sign = TRUE;
2042 # endif
2043 /* draw spaces instead */
2044 s = (char_u *)" ";
2045 if (len == 1 && col > 0)
2046 --col;
2047 len = 2;
2048 draw_sign = TRUE;
2049 highlight_mask = 0;
2051 else
2052 #endif
2053 if (gui.highlight_mask > HL_ALL)
2055 aep = syn_gui_attr2entry(gui.highlight_mask);
2056 if (aep == NULL) /* highlighting not set */
2057 highlight_mask = 0;
2058 else
2059 highlight_mask = aep->ae_attr;
2061 else
2062 highlight_mask = gui.highlight_mask;
2063 hl_mask_todo = highlight_mask;
2065 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2066 /* Set the font */
2067 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2068 font = aep->ae_u.gui.font;
2069 # ifdef FEAT_XFONTSET
2070 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2071 fontset = aep->ae_u.gui.fontset;
2072 # endif
2073 else
2075 # ifdef FEAT_XFONTSET
2076 if (gui.fontset != NOFONTSET)
2077 fontset = gui.fontset;
2078 else
2079 # endif
2080 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2082 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2084 font = gui.boldital_font;
2085 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2087 else if (gui.bold_font != NOFONT)
2089 font = gui.bold_font;
2090 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2092 else
2093 font = gui.norm_font;
2095 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2097 font = gui.ital_font;
2098 hl_mask_todo &= ~HL_ITALIC;
2100 else
2101 font = gui.norm_font;
2103 # ifdef FEAT_XFONTSET
2104 if (fontset != NOFONTSET)
2105 gui_mch_set_fontset(fontset);
2106 else
2107 # endif
2108 gui_mch_set_font(font);
2109 #endif
2111 draw_flags = 0;
2113 /* Set the color */
2114 bg_color = gui.back_pixel;
2115 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2117 draw_flags |= DRAW_CURSOR;
2118 fg_color = fg;
2119 bg_color = bg;
2120 sp_color = fg;
2122 else if (aep != NULL)
2124 fg_color = aep->ae_u.gui.fg_color;
2125 if (fg_color == INVALCOLOR)
2126 fg_color = gui.norm_pixel;
2127 bg_color = aep->ae_u.gui.bg_color;
2128 if (bg_color == INVALCOLOR)
2129 bg_color = gui.back_pixel;
2130 sp_color = aep->ae_u.gui.sp_color;
2131 if (sp_color == INVALCOLOR)
2132 sp_color = fg_color;
2134 else
2136 fg_color = gui.norm_pixel;
2137 sp_color = fg_color;
2140 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2142 #if defined(AMIGA) || defined(RISCOS)
2143 gui_mch_set_colors(bg_color, fg_color);
2144 #else
2145 gui_mch_set_fg_color(bg_color);
2146 gui_mch_set_bg_color(fg_color);
2147 #endif
2149 else
2151 #if defined(AMIGA) || defined(RISCOS)
2152 gui_mch_set_colors(fg_color, bg_color);
2153 #else
2154 gui_mch_set_fg_color(fg_color);
2155 gui_mch_set_bg_color(bg_color);
2156 #endif
2158 gui_mch_set_sp_color(sp_color);
2160 /* Clear the selection if we are about to write over it */
2161 if (!(flags & GUI_MON_NOCLEAR))
2162 clip_may_clear_selection(gui.row, gui.row);
2165 #ifndef MSWIN16_FASTTEXT
2166 /* If there's no bold font, then fake it */
2167 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2168 draw_flags |= DRAW_BOLD;
2169 #endif
2172 * When drawing bold or italic characters the spill-over from the left
2173 * neighbor may be destroyed. Let the caller backup to start redrawing
2174 * just after a blank.
2176 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2177 return FAIL;
2179 #if defined(RISCOS) || defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
2180 /* If there's no italic font, then fake it.
2181 * For GTK2, we don't need a different font for italic style. */
2182 if (hl_mask_todo & HL_ITALIC)
2183 draw_flags |= DRAW_ITALIC;
2185 /* Do we underline the text? */
2186 if (hl_mask_todo & HL_UNDERLINE)
2187 draw_flags |= DRAW_UNDERL;
2188 #else
2189 /* Do we underline the text? */
2190 if ((hl_mask_todo & HL_UNDERLINE)
2191 # ifndef MSWIN16_FASTTEXT
2192 || (hl_mask_todo & HL_ITALIC)
2193 # endif
2195 draw_flags |= DRAW_UNDERL;
2196 #endif
2197 /* Do we undercurl the text? */
2198 if (hl_mask_todo & HL_UNDERCURL)
2199 draw_flags |= DRAW_UNDERC;
2201 /* Do we draw transparently? */
2202 if (flags & GUI_MON_TRS_CURSOR)
2203 draw_flags |= DRAW_TRANSP;
2206 * Draw the text.
2208 #ifdef HAVE_GTK2
2209 /* The value returned is the length in display cells */
2210 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2211 #elif defined(FEAT_GUI_MACVIM) && defined(FEAT_MBYTE)
2212 /* The value returned is the length in display cells */
2213 len = gui_macvim_draw_string(gui.row, col, s, len, draw_flags);
2214 #else
2215 # ifdef FEAT_MBYTE
2216 if (enc_utf8)
2218 int start; /* index of bytes to be drawn */
2219 int cells; /* cellwidth of bytes to be drawn */
2220 int thislen; /* length of bytes to be drawin */
2221 int cn; /* cellwidth of current char */
2222 int i; /* index of current char */
2223 int c; /* current char value */
2224 int cl; /* byte length of current char */
2225 int comping; /* current char is composing */
2226 int scol = col; /* screen column */
2227 int dowide; /* use 'guifontwide' */
2229 /* Break the string at a composing character, it has to be drawn on
2230 * top of the previous character. */
2231 start = 0;
2232 cells = 0;
2233 for (i = 0; i < len; i += cl)
2235 c = utf_ptr2char(s + i);
2236 cn = utf_char2cells(c);
2237 if (cn > 1
2238 # ifdef FEAT_XFONTSET
2239 && fontset == NOFONTSET
2240 # endif
2241 && gui.wide_font != NOFONT)
2242 dowide = TRUE;
2243 else
2244 dowide = FALSE;
2245 comping = utf_iscomposing(c);
2246 if (!comping) /* count cells from non-composing chars */
2247 cells += cn;
2248 cl = utf_ptr2len(s + i);
2249 if (cl == 0) /* hit end of string */
2250 len = i + cl; /* len must be wrong "cannot happen" */
2252 /* print the string so far if it's the last character or there is
2253 * a composing character. */
2254 if (i + cl >= len || (comping && i > start) || dowide
2255 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2256 || (cn > 1
2257 # ifdef FEAT_XFONTSET
2258 /* No fontset: At least draw char after wide char at
2259 * right position. */
2260 && fontset == NOFONTSET
2261 # endif
2263 # endif
2266 if (comping || dowide)
2267 thislen = i - start;
2268 else
2269 thislen = i - start + cl;
2270 if (thislen > 0)
2272 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2273 draw_flags);
2274 start += thislen;
2276 scol += cells;
2277 cells = 0;
2278 if (dowide)
2280 gui_mch_set_font(gui.wide_font);
2281 gui_mch_draw_string(gui.row, scol - cn,
2282 s + start, cl, draw_flags);
2283 gui_mch_set_font(font);
2284 start += cl;
2287 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2288 /* No fontset: draw a space to fill the gap after a wide char
2289 * */
2290 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2291 # ifdef FEAT_XFONTSET
2292 && fontset == NOFONTSET
2293 # endif
2294 && !dowide)
2295 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2296 1, draw_flags);
2297 # endif
2299 /* Draw a composing char on top of the previous char. */
2300 if (comping)
2302 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2303 /* Carbon ATSUI autodraws composing char over previous char */
2304 gui_mch_draw_string(gui.row, scol, s + i, cl,
2305 draw_flags | DRAW_TRANSP);
2306 # else
2307 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2308 draw_flags | DRAW_TRANSP);
2309 # endif
2310 start = i + cl;
2313 /* The stuff below assumes "len" is the length in screen columns. */
2314 len = scol - col;
2316 else
2317 # endif
2319 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2320 # ifdef FEAT_MBYTE
2321 if (enc_dbcs == DBCS_JPNU)
2323 int clen = 0;
2324 int i;
2326 /* Get the length in display cells, this can be different from the
2327 * number of bytes for "euc-jp". */
2328 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2329 clen += (*mb_ptr2cells)(s + i);
2330 len = clen;
2332 # endif
2334 #endif /* !HAVE_GTK2 */
2336 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2337 gui.col = col + len;
2339 /* May need to invert it when it's part of the selection. */
2340 if (flags & GUI_MON_NOCLEAR)
2341 clip_may_redraw_selection(gui.row, col, len);
2343 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2345 /* Invalidate the old physical cursor position if we wrote over it */
2346 if (gui.cursor_row == gui.row
2347 && gui.cursor_col >= col
2348 && gui.cursor_col < col + len)
2349 gui.cursor_is_valid = FALSE;
2352 #ifdef FEAT_SIGN_ICONS
2353 if (draw_sign)
2354 /* Draw the sign on top of the spaces. */
2355 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2356 # ifdef FEAT_NETBEANS_INTG
2357 if (multi_sign)
2358 netbeans_draw_multisign_indicator(gui.row);
2359 # endif
2360 #endif
2362 return OK;
2366 * Un-draw the cursor. Actually this just redraws the character at the given
2367 * position. The character just before it too, for when it was in bold.
2369 void
2370 gui_undraw_cursor()
2372 if (gui.cursor_is_valid)
2374 #ifdef FEAT_HANGULIN
2375 if (composing_hangul
2376 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2377 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2378 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2379 gui.norm_pixel, gui.back_pixel, 0);
2380 else
2382 #endif
2383 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2384 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2385 && gui.cursor_col > 0)
2386 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2387 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2388 #ifdef FEAT_HANGULIN
2389 if (composing_hangul)
2390 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2391 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2393 #endif
2394 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2395 * here in case it wasn't needed to undraw it. */
2396 gui.cursor_is_valid = FALSE;
2400 void
2401 gui_redraw(x, y, w, h)
2402 int x;
2403 int y;
2404 int w;
2405 int h;
2407 int row1, col1, row2, col2;
2409 row1 = Y_2_ROW(y);
2410 col1 = X_2_COL(x);
2411 row2 = Y_2_ROW(y + h - 1);
2412 col2 = X_2_COL(x + w - 1);
2414 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2417 * We may need to redraw the cursor, but don't take it upon us to change
2418 * its location after a scroll.
2419 * (maybe be more strict even and test col too?)
2420 * These things may be outside the update/clipping region and reality may
2421 * not reflect Vims internal ideas if these operations are clipped away.
2423 if (gui.row == gui.cursor_row)
2424 gui_update_cursor(TRUE, TRUE);
2428 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2429 * from col1 to col2 (inclusive).
2430 * Return TRUE when the character before the first drawn character has
2431 * different attributes (may have to be redrawn too).
2434 gui_redraw_block(row1, col1, row2, col2, flags)
2435 int row1;
2436 int col1;
2437 int row2;
2438 int col2;
2439 int flags; /* flags for gui_outstr_nowrap() */
2441 int old_row, old_col;
2442 long_u old_hl_mask;
2443 int off;
2444 sattr_T first_attr;
2445 int idx, len;
2446 int back, nback;
2447 int retval = FALSE;
2448 #ifdef FEAT_MBYTE
2449 int orig_col1, orig_col2;
2450 #endif
2452 /* Don't try to update when ScreenLines is not valid */
2453 if (!screen_cleared || ScreenLines == NULL)
2454 return retval;
2456 /* Don't try to draw outside the shell! */
2457 /* Check everything, strange values may be caused by a big border width */
2458 col1 = check_col(col1);
2459 col2 = check_col(col2);
2460 row1 = check_row(row1);
2461 row2 = check_row(row2);
2463 /* Remember where our cursor was */
2464 old_row = gui.row;
2465 old_col = gui.col;
2466 old_hl_mask = gui.highlight_mask;
2467 #ifdef FEAT_MBYTE
2468 orig_col1 = col1;
2469 orig_col2 = col2;
2470 #endif
2472 for (gui.row = row1; gui.row <= row2; gui.row++)
2474 #ifdef FEAT_MBYTE
2475 /* When only half of a double-wide character is in the block, include
2476 * the other half. */
2477 col1 = orig_col1;
2478 col2 = orig_col2;
2479 off = LineOffset[gui.row];
2480 if (enc_dbcs != 0)
2482 if (col1 > 0)
2483 col1 -= dbcs_screen_head_off(ScreenLines + off,
2484 ScreenLines + off + col1);
2485 col2 += dbcs_screen_tail_off(ScreenLines + off,
2486 ScreenLines + off + col2);
2488 else if (enc_utf8)
2490 if (ScreenLines[off + col1] == 0)
2491 --col1;
2492 # ifdef HAVE_GTK2
2493 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2494 ++col2;
2495 # endif
2497 #endif
2498 gui.col = col1;
2499 off = LineOffset[gui.row] + gui.col;
2500 len = col2 - col1 + 1;
2502 /* Find how many chars back this highlighting starts, or where a space
2503 * is. Needed for when the bold trick is used */
2504 for (back = 0; back < col1; ++back)
2505 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2506 || ScreenLines[off - 1 - back] == ' ')
2507 break;
2508 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2509 && ScreenLines[off - 1] != ' ');
2511 /* Break it up in strings of characters with the same attributes. */
2512 /* Print UTF-8 characters individually. */
2513 while (len > 0)
2515 first_attr = ScreenAttrs[off];
2516 gui.highlight_mask = first_attr;
2517 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2518 if (enc_utf8 && ScreenLinesUC[off] != 0)
2520 /* output multi-byte character separately */
2521 nback = gui_screenchar(off, flags,
2522 (guicolor_T)0, (guicolor_T)0, back);
2523 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2524 idx = 2;
2525 else
2526 idx = 1;
2528 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2530 /* output double-byte, single-width character separately */
2531 nback = gui_screenchar(off, flags,
2532 (guicolor_T)0, (guicolor_T)0, back);
2533 idx = 1;
2535 else
2536 #endif
2538 #ifdef HAVE_GTK2
2539 for (idx = 0; idx < len; ++idx)
2541 if (enc_utf8 && ScreenLines[off + idx] == 0)
2542 continue; /* skip second half of double-width char */
2543 if (ScreenAttrs[off + idx] != first_attr)
2544 break;
2546 /* gui_screenstr() takes care of multibyte chars */
2547 nback = gui_screenstr(off, idx, flags,
2548 (guicolor_T)0, (guicolor_T)0, back);
2549 #else
2550 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2551 idx++)
2553 # ifdef FEAT_MBYTE
2554 /* Stop at a multi-byte Unicode character. */
2555 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2556 break;
2557 if (enc_dbcs == DBCS_JPNU)
2559 /* Stop at a double-byte single-width char. */
2560 if (ScreenLines[off + idx] == 0x8e)
2561 break;
2562 if (len > 1 && (*mb_ptr2len)(ScreenLines
2563 + off + idx) == 2)
2564 ++idx; /* skip second byte of double-byte char */
2566 # endif
2568 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2569 (guicolor_T)0, (guicolor_T)0, back);
2570 #endif
2572 if (nback == FAIL)
2574 /* Must back up to start drawing where a bold or italic word
2575 * starts. */
2576 off -= back;
2577 len += back;
2578 gui.col -= back;
2580 else
2582 off += idx;
2583 len -= idx;
2585 back = 0;
2589 /* Put the cursor back where it was */
2590 gui.row = old_row;
2591 gui.col = old_col;
2592 gui.highlight_mask = (int)old_hl_mask;
2594 return retval;
2597 static void
2598 gui_delete_lines(row, count)
2599 int row;
2600 int count;
2602 if (count <= 0)
2603 return;
2605 if (row + count > gui.scroll_region_bot)
2606 /* Scrolled out of region, just blank the lines out */
2607 gui_clear_block(row, gui.scroll_region_left,
2608 gui.scroll_region_bot, gui.scroll_region_right);
2609 else
2611 gui_mch_delete_lines(row, count);
2613 /* If the cursor was in the deleted lines it's now gone. If the
2614 * cursor was in the scrolled lines adjust its position. */
2615 if (gui.cursor_row >= row
2616 && gui.cursor_col >= gui.scroll_region_left
2617 && gui.cursor_col <= gui.scroll_region_right)
2619 if (gui.cursor_row < row + count)
2620 gui.cursor_is_valid = FALSE;
2621 else if (gui.cursor_row <= gui.scroll_region_bot)
2622 gui.cursor_row -= count;
2627 static void
2628 gui_insert_lines(row, count)
2629 int row;
2630 int count;
2632 if (count <= 0)
2633 return;
2635 if (row + count > gui.scroll_region_bot)
2636 /* Scrolled out of region, just blank the lines out */
2637 gui_clear_block(row, gui.scroll_region_left,
2638 gui.scroll_region_bot, gui.scroll_region_right);
2639 else
2641 gui_mch_insert_lines(row, count);
2643 if (gui.cursor_row >= gui.row
2644 && gui.cursor_col >= gui.scroll_region_left
2645 && gui.cursor_col <= gui.scroll_region_right)
2647 if (gui.cursor_row <= gui.scroll_region_bot - count)
2648 gui.cursor_row += count;
2649 else if (gui.cursor_row <= gui.scroll_region_bot)
2650 gui.cursor_is_valid = FALSE;
2656 * The main GUI input routine. Waits for a character from the keyboard.
2657 * wtime == -1 Wait forever.
2658 * wtime == 0 Don't wait.
2659 * wtime > 0 Wait wtime milliseconds for a character.
2660 * Returns OK if a character was found to be available within the given time,
2661 * or FAIL otherwise.
2664 gui_wait_for_chars(wtime)
2665 long wtime;
2667 int retval;
2670 * If we're going to wait a bit, update the menus and mouse shape for the
2671 * current State.
2673 if (wtime != 0)
2675 #ifdef FEAT_MENU
2676 gui_update_menus(0);
2677 #endif
2680 gui_mch_update();
2681 if (input_available()) /* Got char, return immediately */
2682 return OK;
2683 if (wtime == 0) /* Don't wait for char */
2684 return FAIL;
2686 /* Before waiting, flush any output to the screen. */
2687 gui_mch_flush();
2689 if (wtime > 0)
2691 /* Blink when waiting for a character. Probably only does something
2692 * for showmatch() */
2693 gui_mch_start_blink();
2694 retval = gui_mch_wait_for_chars(wtime);
2695 gui_mch_stop_blink();
2696 return retval;
2700 * While we are waiting indefinitely for a character, blink the cursor.
2702 gui_mch_start_blink();
2704 retval = FAIL;
2706 * We may want to trigger the CursorHold event. First wait for
2707 * 'updatetime' and if nothing is typed within that time put the
2708 * K_CURSORHOLD key in the input buffer.
2710 if (gui_mch_wait_for_chars(p_ut) == OK)
2711 retval = OK;
2712 #ifdef FEAT_AUTOCMD
2713 else if (trigger_cursorhold())
2715 char_u buf[3];
2717 /* Put K_CURSORHOLD in the input buffer. */
2718 buf[0] = CSI;
2719 buf[1] = KS_EXTRA;
2720 buf[2] = (int)KE_CURSORHOLD;
2721 add_to_input_buf(buf, 3);
2723 retval = OK;
2725 #endif
2727 if (retval == FAIL)
2729 /* Blocking wait. */
2730 before_blocking();
2731 retval = gui_mch_wait_for_chars(-1L);
2734 gui_mch_stop_blink();
2735 return retval;
2739 * Fill p[4] with mouse coordinates encoded for check_termcode().
2741 static void
2742 fill_mouse_coord(p, col, row)
2743 char_u *p;
2744 int col;
2745 int row;
2747 p[0] = (char_u)(col / 128 + ' ' + 1);
2748 p[1] = (char_u)(col % 128 + ' ' + 1);
2749 p[2] = (char_u)(row / 128 + ' ' + 1);
2750 p[3] = (char_u)(row % 128 + ' ' + 1);
2754 * Generic mouse support function. Add a mouse event to the input buffer with
2755 * the given properties.
2756 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2757 * MOUSE_X1, MOUSE_X2
2758 * MOUSE_DRAG, or MOUSE_RELEASE.
2759 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2760 * x, y --- Coordinates of mouse in pixels.
2761 * repeated_click --- TRUE if this click comes only a short time after a
2762 * previous click.
2763 * modifiers --- Bit field which may be any of the following modifiers
2764 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2765 * This function will ignore drag events where the mouse has not moved to a new
2766 * character.
2768 void
2769 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2770 int button;
2771 int x;
2772 int y;
2773 int repeated_click;
2774 int_u modifiers;
2776 static int prev_row = 0, prev_col = 0;
2777 static int prev_button = -1;
2778 static int num_clicks = 1;
2779 char_u string[10];
2780 enum key_extra button_char;
2781 int row, col;
2782 #ifdef FEAT_CLIPBOARD
2783 int checkfor;
2784 int did_clip = FALSE;
2785 #endif
2788 * Scrolling may happen at any time, also while a selection is present.
2790 switch (button)
2792 case MOUSE_X1:
2793 button_char = KE_X1MOUSE;
2794 goto button_set;
2795 case MOUSE_X2:
2796 button_char = KE_X2MOUSE;
2797 goto button_set;
2798 case MOUSE_4:
2799 button_char = KE_MOUSEDOWN;
2800 goto button_set;
2801 case MOUSE_5:
2802 button_char = KE_MOUSEUP;
2803 button_set:
2805 /* Don't put events in the input queue now. */
2806 if (hold_gui_events)
2807 return;
2809 string[3] = CSI;
2810 string[4] = KS_EXTRA;
2811 string[5] = (int)button_char;
2813 /* Pass the pointer coordinates of the scroll event so that we
2814 * know which window to scroll. */
2815 row = gui_xy2colrow(x, y, &col);
2816 string[6] = (char_u)(col / 128 + ' ' + 1);
2817 string[7] = (char_u)(col % 128 + ' ' + 1);
2818 string[8] = (char_u)(row / 128 + ' ' + 1);
2819 string[9] = (char_u)(row % 128 + ' ' + 1);
2821 if (modifiers == 0)
2822 add_to_input_buf(string + 3, 7);
2823 else
2825 string[0] = CSI;
2826 string[1] = KS_MODIFIER;
2827 string[2] = 0;
2828 if (modifiers & MOUSE_SHIFT)
2829 string[2] |= MOD_MASK_SHIFT;
2830 if (modifiers & MOUSE_CTRL)
2831 string[2] |= MOD_MASK_CTRL;
2832 if (modifiers & MOUSE_ALT)
2833 string[2] |= MOD_MASK_ALT;
2834 add_to_input_buf(string, 10);
2836 return;
2840 #ifdef FEAT_CLIPBOARD
2841 /* If a clipboard selection is in progress, handle it */
2842 if (clip_star.state == SELECT_IN_PROGRESS)
2844 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2845 return;
2848 /* Determine which mouse settings to look for based on the current mode */
2849 switch (get_real_state())
2851 case NORMAL_BUSY:
2852 case OP_PENDING:
2853 case NORMAL: checkfor = MOUSE_NORMAL; break;
2854 case VISUAL: checkfor = MOUSE_VISUAL; break;
2855 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2856 case REPLACE:
2857 case REPLACE+LANGMAP:
2858 #ifdef FEAT_VREPLACE
2859 case VREPLACE:
2860 case VREPLACE+LANGMAP:
2861 #endif
2862 case INSERT:
2863 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2864 case ASKMORE:
2865 case HITRETURN: /* At the more- and hit-enter prompt pass the
2866 mouse event for a click on or below the
2867 message line. */
2868 if (Y_2_ROW(y) >= msg_row)
2869 checkfor = MOUSE_NORMAL;
2870 else
2871 checkfor = MOUSE_RETURN;
2872 break;
2875 * On the command line, use the clipboard selection on all lines
2876 * but the command line. But not when pasting.
2878 case CMDLINE:
2879 case CMDLINE+LANGMAP:
2880 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2881 checkfor = MOUSE_NONE;
2882 else
2883 checkfor = MOUSE_COMMAND;
2884 break;
2886 default:
2887 checkfor = MOUSE_NONE;
2888 break;
2892 * Allow clipboard selection of text on the command line in "normal"
2893 * modes. Don't do this when dragging the status line, or extending a
2894 * Visual selection.
2896 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2897 && Y_2_ROW(y) >= topframe->fr_height
2898 # ifdef FEAT_WINDOWS
2899 + firstwin->w_winrow
2900 # endif
2901 && button != MOUSE_DRAG
2902 # ifdef FEAT_MOUSESHAPE
2903 && !drag_status_line
2904 # ifdef FEAT_VERTSPLIT
2905 && !drag_sep_line
2906 # endif
2907 # endif
2909 checkfor = MOUSE_NONE;
2912 * Use modeless selection when holding CTRL and SHIFT pressed.
2914 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2915 checkfor = MOUSE_NONEF;
2918 * In Ex mode, always use modeless selection.
2920 if (exmode_active)
2921 checkfor = MOUSE_NONE;
2924 * If the mouse settings say to not use the mouse, use the modeless
2925 * selection. But if Visual is active, assume that only the Visual area
2926 * will be selected.
2927 * Exception: On the command line, both the selection is used and a mouse
2928 * key is send.
2930 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2932 #ifdef FEAT_VISUAL
2933 /* Don't do modeless selection in Visual mode. */
2934 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2935 return;
2936 #endif
2939 * When 'mousemodel' is "popup", shift-left is translated to right.
2940 * But not when also using Ctrl.
2942 if (mouse_model_popup() && button == MOUSE_LEFT
2943 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2945 button = MOUSE_RIGHT;
2946 modifiers &= ~ MOUSE_SHIFT;
2949 /* If the selection is done, allow the right button to extend it.
2950 * If the selection is cleared, allow the right button to start it
2951 * from the cursor position. */
2952 if (button == MOUSE_RIGHT)
2954 if (clip_star.state == SELECT_CLEARED)
2956 if (State & CMDLINE)
2958 col = msg_col;
2959 row = msg_row;
2961 else
2963 col = curwin->w_wcol;
2964 row = curwin->w_wrow + W_WINROW(curwin);
2966 clip_start_selection(col, row, FALSE);
2968 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2969 repeated_click);
2970 did_clip = TRUE;
2972 /* Allow the left button to start the selection */
2973 else if (button ==
2974 # ifdef RISCOS
2975 /* Only start a drag on a drag event. Otherwise
2976 * we don't get a release event. */
2977 MOUSE_DRAG
2978 # else
2979 MOUSE_LEFT
2980 # endif
2983 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2984 did_clip = TRUE;
2986 # ifdef RISCOS
2987 else if (button == MOUSE_LEFT)
2989 clip_clear_selection();
2990 did_clip = TRUE;
2992 # endif
2994 /* Always allow pasting */
2995 if (button != MOUSE_MIDDLE)
2997 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2998 return;
2999 if (checkfor != MOUSE_COMMAND)
3000 button = MOUSE_LEFT;
3002 repeated_click = FALSE;
3005 if (clip_star.state != SELECT_CLEARED && !did_clip)
3006 clip_clear_selection();
3007 #endif
3009 /* Don't put events in the input queue now. */
3010 if (hold_gui_events)
3011 return;
3013 row = gui_xy2colrow(x, y, &col);
3016 * If we are dragging and the mouse hasn't moved far enough to be on a
3017 * different character, then don't send an event to vim.
3019 if (button == MOUSE_DRAG)
3021 if (row == prev_row && col == prev_col)
3022 return;
3023 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3024 if (y < 0)
3025 row = -1;
3029 * If topline has changed (window scrolled) since the last click, reset
3030 * repeated_click, because we don't want starting Visual mode when
3031 * clicking on a different character in the text.
3033 if (curwin->w_topline != gui_prev_topline
3034 #ifdef FEAT_DIFF
3035 || curwin->w_topfill != gui_prev_topfill
3036 #endif
3038 repeated_click = FALSE;
3040 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3041 string[1] = KS_MOUSE;
3042 string[2] = KE_FILLER;
3043 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3045 if (repeated_click)
3048 * Handle multiple clicks. They only count if the mouse is still
3049 * pointing at the same character.
3051 if (button != prev_button || row != prev_row || col != prev_col)
3052 num_clicks = 1;
3053 else if (++num_clicks > 4)
3054 num_clicks = 1;
3056 else
3057 num_clicks = 1;
3058 prev_button = button;
3059 gui_prev_topline = curwin->w_topline;
3060 #ifdef FEAT_DIFF
3061 gui_prev_topfill = curwin->w_topfill;
3062 #endif
3064 string[3] = (char_u)(button | 0x20);
3065 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3067 else
3068 string[3] = (char_u)button;
3070 string[3] |= modifiers;
3071 fill_mouse_coord(string + 4, col, row);
3072 add_to_input_buf(string, 8);
3074 if (row < 0)
3075 prev_row = 0;
3076 else
3077 prev_row = row;
3078 prev_col = col;
3081 * We need to make sure this is cleared since Athena doesn't tell us when
3082 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3084 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3085 gui.dragged_sb = SBAR_NONE;
3086 #endif
3090 * Convert x and y coordinate to column and row in text window.
3091 * Corrects for multi-byte character.
3092 * returns column in "*colp" and row as return value;
3095 gui_xy2colrow(x, y, colp)
3096 int x;
3097 int y;
3098 int *colp;
3100 int col = check_col(X_2_COL(x));
3101 int row = check_row(Y_2_ROW(y));
3103 #ifdef FEAT_MBYTE
3104 *colp = mb_fix_col(col, row);
3105 #else
3106 *colp = col;
3107 #endif
3108 return row;
3111 #if defined(FEAT_MENU) || defined(PROTO)
3113 * Callback function for when a menu entry has been selected.
3115 void
3116 gui_menu_cb(menu)
3117 vimmenu_T *menu;
3119 char_u bytes[sizeof(long_u)];
3121 /* Don't put events in the input queue now. */
3122 if (hold_gui_events)
3123 return;
3125 bytes[0] = CSI;
3126 bytes[1] = KS_MENU;
3127 bytes[2] = KE_FILLER;
3128 add_to_input_buf(bytes, 3);
3129 add_long_to_buf((long_u)menu, bytes);
3130 add_to_input_buf_csi(bytes, sizeof(long_u));
3132 #endif
3134 static int prev_which_scrollbars[3];
3137 * Set which components are present.
3138 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3139 * in p_go.
3141 void
3142 gui_init_which_components(oldval)
3143 char_u *oldval UNUSED;
3145 #ifdef FEAT_MENU
3146 static int prev_menu_is_active = -1;
3147 #endif
3148 #ifdef FEAT_TOOLBAR
3149 static int prev_toolbar = -1;
3150 int using_toolbar = FALSE;
3151 #endif
3152 #ifdef FEAT_GUI_TABLINE
3153 int using_tabline;
3154 #endif
3155 #ifdef FEAT_FOOTER
3156 static int prev_footer = -1;
3157 int using_footer = FALSE;
3158 #endif
3159 #if defined(FEAT_MENU) && !defined(WIN16)
3160 static int prev_tearoff = -1;
3161 int using_tearoff = FALSE;
3162 #endif
3164 char_u *p;
3165 int i;
3166 #ifdef FEAT_MENU
3167 int grey_old, grey_new;
3168 char_u *temp;
3169 #endif
3170 win_T *wp;
3171 int need_set_size;
3172 int fix_size;
3174 #ifdef FEAT_MENU
3175 if (oldval != NULL && gui.in_use)
3178 * Check if the menu's go from grey to non-grey or vise versa.
3180 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3181 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3182 if (grey_old != grey_new)
3184 temp = p_go;
3185 p_go = oldval;
3186 gui_update_menus(MENU_ALL_MODES);
3187 p_go = temp;
3190 gui.menu_is_active = FALSE;
3191 #endif
3193 for (i = 0; i < 3; i++)
3194 gui.which_scrollbars[i] = FALSE;
3195 for (p = p_go; *p; p++)
3196 switch (*p)
3198 case GO_LEFT:
3199 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3200 break;
3201 case GO_RIGHT:
3202 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3203 break;
3204 #ifdef FEAT_VERTSPLIT
3205 case GO_VLEFT:
3206 if (win_hasvertsplit())
3207 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3208 break;
3209 case GO_VRIGHT:
3210 if (win_hasvertsplit())
3211 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3212 break;
3213 #endif
3214 case GO_BOT:
3215 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3216 break;
3217 #ifdef FEAT_MENU
3218 case GO_MENUS:
3219 gui.menu_is_active = TRUE;
3220 break;
3221 #endif
3222 case GO_GREY:
3223 /* make menu's have grey items, ignored here */
3224 break;
3225 #ifdef FEAT_TOOLBAR
3226 case GO_TOOLBAR:
3227 using_toolbar = TRUE;
3228 break;
3229 #endif
3230 #ifdef FEAT_FOOTER
3231 case GO_FOOTER:
3232 using_footer = TRUE;
3233 break;
3234 #endif
3235 case GO_TEAROFF:
3236 #if defined(FEAT_MENU) && !defined(WIN16)
3237 using_tearoff = TRUE;
3238 #endif
3239 break;
3240 default:
3241 /* Ignore options that are not supported */
3242 break;
3245 if (gui.in_use)
3247 need_set_size = 0;
3248 fix_size = FALSE;
3250 #ifdef FEAT_GUI_TABLINE
3251 /* Update the GUI tab line, it may appear or disappear. This may
3252 * cause the non-GUI tab line to disappear or appear. */
3253 using_tabline = gui_has_tabline();
3254 if (!gui_mch_showing_tabline() != !using_tabline)
3256 /* We don't want a resize event change "Rows" here, save and
3257 * restore it. Resizing is handled below. */
3258 i = Rows;
3259 gui_update_tabline();
3260 Rows = i;
3261 need_set_size |= RESIZE_VERT;
3262 if (using_tabline)
3263 fix_size = TRUE;
3264 if (!gui_use_tabline())
3265 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3267 #endif
3269 for (i = 0; i < 3; i++)
3271 /* The scrollbar needs to be updated when it is shown/unshown and
3272 * when switching tab pages. But the size only changes when it's
3273 * shown/unshown. Thus we need two places to remember whether a
3274 * scrollbar is there or not. */
3275 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3276 #ifdef FEAT_WINDOWS
3277 || gui.which_scrollbars[i]
3278 != curtab->tp_prev_which_scrollbars[i]
3279 #endif
3282 if (i == SBAR_BOTTOM)
3283 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3284 gui.which_scrollbars[i]);
3285 else
3287 FOR_ALL_WINDOWS(wp)
3289 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3292 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3294 if (i == SBAR_BOTTOM)
3295 need_set_size |= RESIZE_VERT;
3296 else
3297 need_set_size |= RESIZE_HOR;
3298 if (gui.which_scrollbars[i])
3299 fix_size = TRUE;
3302 #ifdef FEAT_WINDOWS
3303 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3304 #endif
3305 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3308 #ifdef FEAT_MENU
3309 if (gui.menu_is_active != prev_menu_is_active)
3311 /* We don't want a resize event change "Rows" here, save and
3312 * restore it. Resizing is handled below. */
3313 i = Rows;
3314 gui_mch_enable_menu(gui.menu_is_active);
3315 Rows = i;
3316 prev_menu_is_active = gui.menu_is_active;
3317 need_set_size |= RESIZE_VERT;
3318 if (gui.menu_is_active)
3319 fix_size = TRUE;
3321 #endif
3323 #ifdef FEAT_TOOLBAR
3324 if (using_toolbar != prev_toolbar)
3326 gui_mch_show_toolbar(using_toolbar);
3327 prev_toolbar = using_toolbar;
3328 need_set_size |= RESIZE_VERT;
3329 if (using_toolbar)
3330 fix_size = TRUE;
3332 #endif
3333 #ifdef FEAT_FOOTER
3334 if (using_footer != prev_footer)
3336 gui_mch_enable_footer(using_footer);
3337 prev_footer = using_footer;
3338 need_set_size |= RESIZE_VERT;
3339 if (using_footer)
3340 fix_size = TRUE;
3342 #endif
3343 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3344 if (using_tearoff != prev_tearoff)
3346 gui_mch_toggle_tearoffs(using_tearoff);
3347 prev_tearoff = using_tearoff;
3349 #endif
3350 if (need_set_size != 0)
3352 #ifdef FEAT_GUI_GTK
3353 long prev_Columns = Columns;
3354 long prev_Rows = Rows;
3355 #endif
3356 /* Adjust the size of the window to make the text area keep the
3357 * same size and to avoid that part of our window is off-screen
3358 * and a scrollbar can't be used, for example. */
3359 gui_set_shellsize(FALSE, fix_size, need_set_size);
3361 #ifdef FEAT_GUI_GTK
3362 /* GTK has the annoying habit of sending us resize events when
3363 * changing the window size ourselves. This mostly happens when
3364 * waiting for a character to arrive, quite unpredictably, and may
3365 * change Columns and Rows when we don't want it. Wait for a
3366 * character here to avoid this effect.
3367 * If you remove this, please test this command for resizing
3368 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3369 * Don't do this while starting up though.
3370 * Don't change Rows when adding menu/toolbar/tabline.
3371 * Don't change Columns when adding vertical toolbar. */
3372 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3373 (void)char_avail();
3374 if ((need_set_size & RESIZE_VERT) == 0)
3375 Rows = prev_Rows;
3376 if ((need_set_size & RESIZE_HOR) == 0)
3377 Columns = prev_Columns;
3378 #endif
3380 #ifdef FEAT_WINDOWS
3381 /* When the console tabline appears or disappears the window positions
3382 * change. */
3383 if (firstwin->w_winrow != tabline_height())
3384 shell_new_rows(); /* recompute window positions and heights */
3385 #endif
3389 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3391 * Return TRUE if the GUI is taking care of the tabline.
3392 * It may still be hidden if 'showtabline' is zero.
3395 gui_use_tabline()
3397 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3401 * Return TRUE if the GUI is showing the tabline.
3402 * This uses 'showtabline'.
3404 static int
3405 gui_has_tabline()
3407 if (!gui_use_tabline()
3408 || p_stal == 0
3409 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3410 return FALSE;
3411 return TRUE;
3415 * Update the tabline.
3416 * This may display/undisplay the tabline and update the labels.
3418 void
3419 gui_update_tabline()
3421 int showit = gui_has_tabline();
3422 int shown = gui_mch_showing_tabline();
3424 if (!gui.starting && starting == 0)
3426 /* Updating the tabline uses direct GUI commands, flush
3427 * outstanding instructions first. (esp. clear screen) */
3428 out_flush();
3429 gui_mch_flush();
3431 if (!showit != !shown)
3432 gui_mch_show_tabline(showit);
3433 if (showit != 0)
3434 gui_mch_update_tabline();
3436 /* When the tabs change from hidden to shown or from shown to
3437 * hidden the size of the text area should remain the same. */
3438 if (!showit != !shown)
3439 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3444 * Get the label or tooltip for tab page "tp" into NameBuff[].
3446 void
3447 get_tabline_label(tp, tooltip)
3448 tabpage_T *tp;
3449 int tooltip; /* TRUE: get tooltip */
3451 int modified = FALSE;
3452 char_u buf[40];
3453 int wincount;
3454 win_T *wp;
3455 char_u **opt;
3457 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3458 opt = (tooltip ? &p_gtt : &p_gtl);
3459 if (**opt != NUL)
3461 int use_sandbox = FALSE;
3462 int save_called_emsg = called_emsg;
3463 char_u res[MAXPATHL];
3464 tabpage_T *save_curtab;
3465 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3466 : "guitablabel");
3468 called_emsg = FALSE;
3470 printer_page_num = tabpage_index(tp);
3471 # ifdef FEAT_EVAL
3472 set_vim_var_nr(VV_LNUM, printer_page_num);
3473 use_sandbox = was_set_insecurely(opt_name, 0);
3474 # endif
3475 /* It's almost as going to the tabpage, but without autocommands. */
3476 curtab->tp_firstwin = firstwin;
3477 curtab->tp_lastwin = lastwin;
3478 curtab->tp_curwin = curwin;
3479 save_curtab = curtab;
3480 curtab = tp;
3481 topframe = curtab->tp_topframe;
3482 firstwin = curtab->tp_firstwin;
3483 lastwin = curtab->tp_lastwin;
3484 curwin = curtab->tp_curwin;
3485 curbuf = curwin->w_buffer;
3487 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3488 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3489 0, (int)Columns, NULL, NULL);
3490 STRCPY(NameBuff, res);
3492 /* Back to the original curtab. */
3493 curtab = save_curtab;
3494 topframe = curtab->tp_topframe;
3495 firstwin = curtab->tp_firstwin;
3496 lastwin = curtab->tp_lastwin;
3497 curwin = curtab->tp_curwin;
3498 curbuf = curwin->w_buffer;
3500 if (called_emsg)
3501 set_string_option_direct(opt_name, -1,
3502 (char_u *)"", OPT_FREE, SID_ERROR);
3503 called_emsg |= save_called_emsg;
3506 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3507 * use a default label. */
3508 if (**opt == NUL || *NameBuff == NUL)
3510 /* Get the buffer name into NameBuff[] and shorten it. */
3511 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3512 if (!tooltip)
3513 shorten_dir(NameBuff);
3515 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3516 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3517 if (bufIsChanged(wp->w_buffer))
3518 modified = TRUE;
3519 if (modified || wincount > 1)
3521 if (wincount > 1)
3522 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3523 else
3524 buf[0] = NUL;
3525 if (modified)
3526 STRCAT(buf, "+");
3527 STRCAT(buf, " ");
3528 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
3529 mch_memmove(NameBuff, buf, STRLEN(buf));
3535 * Send the event for clicking to select tab page "nr".
3536 * Returns TRUE if it was done, FALSE when skipped because we are already at
3537 * that tab page or the cmdline window is open.
3540 send_tabline_event(nr)
3541 int nr;
3543 char_u string[3];
3545 if (nr == tabpage_index(curtab))
3546 return FALSE;
3548 /* Don't put events in the input queue now. */
3549 if (hold_gui_events
3550 # ifdef FEAT_CMDWIN
3551 || cmdwin_type != 0
3552 # endif
3555 /* Set it back to the current tab page. */
3556 gui_mch_set_curtab(tabpage_index(curtab));
3557 return FALSE;
3560 string[0] = CSI;
3561 string[1] = KS_TABLINE;
3562 string[2] = KE_FILLER;
3563 add_to_input_buf(string, 3);
3564 string[0] = nr;
3565 add_to_input_buf_csi(string, 1);
3566 return TRUE;
3570 * Send a tabline menu event
3572 void
3573 send_tabline_menu_event(tabidx, event)
3574 int tabidx;
3575 int event;
3577 char_u string[3];
3579 /* Don't put events in the input queue now. */
3580 if (hold_gui_events)
3581 return;
3583 string[0] = CSI;
3584 string[1] = KS_TABMENU;
3585 string[2] = KE_FILLER;
3586 add_to_input_buf(string, 3);
3587 string[0] = tabidx;
3588 string[1] = (char_u)(long)event;
3589 add_to_input_buf_csi(string, 2);
3592 #endif
3595 * Scrollbar stuff:
3598 #if defined(FEAT_WINDOWS) || defined(PROTO)
3600 * Remove all scrollbars. Used before switching to another tab page.
3602 void
3603 gui_remove_scrollbars()
3605 int i;
3606 win_T *wp;
3608 for (i = 0; i < 3; i++)
3610 if (i == SBAR_BOTTOM)
3611 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3612 else
3614 FOR_ALL_WINDOWS(wp)
3616 gui_do_scrollbar(wp, i, FALSE);
3619 curtab->tp_prev_which_scrollbars[i] = -1;
3622 #endif
3624 void
3625 gui_create_scrollbar(sb, type, wp)
3626 scrollbar_T *sb;
3627 int type;
3628 win_T *wp;
3630 #ifdef FEAT_GUI_MACVIM
3631 /* This is passed over to another process, make sure it fits in 32 bit */
3632 static int32_t sbar_ident = 0;
3633 #else
3634 static int sbar_ident = 0;
3635 #endif
3637 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3638 sb->wp = wp;
3639 sb->type = type;
3640 sb->value = 0;
3641 #ifdef FEAT_GUI_ATHENA
3642 sb->pixval = 0;
3643 #endif
3644 sb->size = 1;
3645 sb->max = 1;
3646 sb->top = 0;
3647 sb->height = 0;
3648 #ifdef FEAT_VERTSPLIT
3649 sb->width = 0;
3650 #endif
3651 sb->status_height = 0;
3652 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3656 * Find the scrollbar with the given index.
3658 scrollbar_T *
3659 gui_find_scrollbar(ident)
3660 long ident;
3662 win_T *wp;
3664 if (gui.bottom_sbar.ident == ident)
3665 return &gui.bottom_sbar;
3666 FOR_ALL_WINDOWS(wp)
3668 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3669 return &wp->w_scrollbars[SBAR_LEFT];
3670 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3671 return &wp->w_scrollbars[SBAR_RIGHT];
3673 return NULL;
3677 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3679 * For Win32, Macintosh and GTK+ 2:
3680 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3681 * you stop dragging the scrollbar. We get here each time the scrollbar is
3682 * dragged another pixel, but as far as the rest of vim goes, it thinks
3683 * we're just hanging in the call to DispatchMessage() in
3684 * process_message(). The DispatchMessage() call that hangs was passed a
3685 * mouse button click event in the scrollbar window. -- webb.
3687 * Solution: Do the scrolling right here. But only when allowed.
3688 * Ignore the scrollbars while executing an external command or when there
3689 * are still characters to be processed.
3691 void
3692 gui_drag_scrollbar(sb, value, still_dragging)
3693 scrollbar_T *sb;
3694 long value;
3695 int still_dragging;
3697 #ifdef FEAT_WINDOWS
3698 win_T *wp;
3699 #endif
3700 int sb_num;
3701 #ifdef USE_ON_FLY_SCROLL
3702 colnr_T old_leftcol = curwin->w_leftcol;
3703 # ifdef FEAT_SCROLLBIND
3704 linenr_T old_topline = curwin->w_topline;
3705 # endif
3706 # ifdef FEAT_DIFF
3707 int old_topfill = curwin->w_topfill;
3708 # endif
3709 #else
3710 char_u bytes[sizeof(long_u)];
3711 int byte_count;
3712 #endif
3714 if (sb == NULL)
3715 return;
3717 /* Don't put events in the input queue now. */
3718 if (hold_gui_events)
3719 return;
3721 #ifdef FEAT_CMDWIN
3722 if (cmdwin_type != 0 && sb->wp != curwin)
3723 return;
3724 #endif
3726 if (still_dragging)
3728 if (sb->wp == NULL)
3729 gui.dragged_sb = SBAR_BOTTOM;
3730 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3731 gui.dragged_sb = SBAR_LEFT;
3732 else
3733 gui.dragged_sb = SBAR_RIGHT;
3734 gui.dragged_wp = sb->wp;
3736 else
3738 gui.dragged_sb = SBAR_NONE;
3739 #ifdef HAVE_GTK2
3740 /* Keep the "dragged_wp" value until after the scrolling, for when the
3741 * moust button is released. GTK2 doesn't send the button-up event. */
3742 gui.dragged_wp = NULL;
3743 #endif
3746 /* Vertical sbar info is kept in the first sbar (the left one) */
3747 if (sb->wp != NULL)
3748 sb = &sb->wp->w_scrollbars[0];
3751 * Check validity of value
3753 if (value < 0)
3754 value = 0;
3755 #ifdef SCROLL_PAST_END
3756 else if (value > sb->max)
3757 value = sb->max;
3758 #else
3759 if (value > sb->max - sb->size + 1)
3760 value = sb->max - sb->size + 1;
3761 #endif
3763 sb->value = value;
3765 #ifdef USE_ON_FLY_SCROLL
3766 /* When not allowed to do the scrolling right now, return.
3767 * This also checked input_available(), but that causes the first click in
3768 * a scrollbar to be ignored when Vim doesn't have focus. */
3769 if (dont_scroll)
3770 return;
3771 #endif
3772 #ifdef FEAT_INS_EXPAND
3773 /* Disallow scrolling the current window when the completion popup menu is
3774 * visible. */
3775 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3776 return;
3777 #endif
3779 #ifdef FEAT_RIGHTLEFT
3780 if (sb->wp == NULL && curwin->w_p_rl)
3782 value = sb->max + 1 - sb->size - value;
3783 if (value < 0)
3784 value = 0;
3786 #endif
3788 if (sb->wp != NULL) /* vertical scrollbar */
3790 sb_num = 0;
3791 #ifdef FEAT_WINDOWS
3792 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3793 sb_num++;
3794 if (wp == NULL)
3795 return;
3796 #else
3797 if (sb->wp != curwin)
3798 return;
3799 #endif
3801 #ifdef USE_ON_FLY_SCROLL
3802 current_scrollbar = sb_num;
3803 scrollbar_value = value;
3804 if (State & NORMAL)
3806 gui_do_scroll();
3807 setcursor();
3809 else if (State & INSERT)
3811 ins_scroll();
3812 setcursor();
3814 else if (State & CMDLINE)
3816 if (msg_scrolled == 0)
3818 gui_do_scroll();
3819 redrawcmdline();
3822 # ifdef FEAT_FOLDING
3823 /* Value may have been changed for closed fold. */
3824 sb->value = sb->wp->w_topline - 1;
3825 # endif
3827 /* When dragging one scrollbar and there is another one at the other
3828 * side move the thumb of that one too. */
3829 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3830 gui_mch_set_scrollbar_thumb(
3831 &sb->wp->w_scrollbars[
3832 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3833 ? SBAR_LEFT : SBAR_RIGHT],
3834 sb->value, sb->size, sb->max);
3836 #else
3837 bytes[0] = CSI;
3838 bytes[1] = KS_VER_SCROLLBAR;
3839 bytes[2] = KE_FILLER;
3840 bytes[3] = (char_u)sb_num;
3841 byte_count = 4;
3842 #endif
3844 else
3846 #ifdef USE_ON_FLY_SCROLL
3847 scrollbar_value = value;
3849 if (State & NORMAL)
3850 gui_do_horiz_scroll();
3851 else if (State & INSERT)
3852 ins_horscroll();
3853 else if (State & CMDLINE)
3855 if (msg_scrolled == 0)
3857 gui_do_horiz_scroll();
3858 redrawcmdline();
3861 if (old_leftcol != curwin->w_leftcol)
3863 updateWindow(curwin); /* update window, status and cmdline */
3864 setcursor();
3866 #else
3867 bytes[0] = CSI;
3868 bytes[1] = KS_HOR_SCROLLBAR;
3869 bytes[2] = KE_FILLER;
3870 byte_count = 3;
3871 #endif
3874 #ifdef USE_ON_FLY_SCROLL
3875 # ifdef FEAT_SCROLLBIND
3877 * synchronize other windows, as necessary according to 'scrollbind'
3879 if (curwin->w_p_scb
3880 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3881 || (sb->wp == curwin && (curwin->w_topline != old_topline
3882 # ifdef FEAT_DIFF
3883 || curwin->w_topfill != old_topfill
3884 # endif
3885 ))))
3887 do_check_scrollbind(TRUE);
3888 /* need to update the window right here */
3889 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3890 if (wp->w_redr_type > 0)
3891 updateWindow(wp);
3892 setcursor();
3894 # endif
3895 out_flush();
3896 gui_update_cursor(FALSE, TRUE);
3897 #else
3898 add_to_input_buf(bytes, byte_count);
3899 add_long_to_buf((long_u)value, bytes);
3900 add_to_input_buf_csi(bytes, sizeof(long_u));
3901 #endif
3905 * Scrollbar stuff:
3909 * Called when something in the window layout has changed.
3911 void
3912 gui_may_update_scrollbars()
3914 if (gui.in_use && starting == 0)
3916 out_flush();
3917 gui_init_which_components(NULL);
3918 gui_update_scrollbars(TRUE);
3920 need_mouse_correct = TRUE;
3923 void
3924 gui_update_scrollbars(force)
3925 int force; /* Force all scrollbars to get updated */
3927 win_T *wp;
3928 scrollbar_T *sb;
3929 long val, size, max; /* need 32 bits here */
3930 int which_sb;
3931 int h, y;
3932 #ifdef FEAT_VERTSPLIT
3933 static win_T *prev_curwin = NULL;
3934 #endif
3936 /* Update the horizontal scrollbar */
3937 gui_update_horiz_scrollbar(force);
3939 #ifndef WIN3264
3940 /* Return straight away if there is neither a left nor right scrollbar.
3941 * On MS-Windows this is required anyway for scrollwheel messages. */
3942 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3943 return;
3944 #endif
3947 * Don't want to update a scrollbar while we're dragging it. But if we
3948 * have both a left and right scrollbar, and we drag one of them, we still
3949 * need to update the other one.
3951 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3952 && gui.which_scrollbars[SBAR_LEFT]
3953 && gui.which_scrollbars[SBAR_RIGHT])
3956 * If we have two scrollbars and one of them is being dragged, just
3957 * copy the scrollbar position from the dragged one to the other one.
3959 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3960 if (gui.dragged_wp != NULL)
3961 gui_mch_set_scrollbar_thumb(
3962 &gui.dragged_wp->w_scrollbars[which_sb],
3963 gui.dragged_wp->w_scrollbars[0].value,
3964 gui.dragged_wp->w_scrollbars[0].size,
3965 gui.dragged_wp->w_scrollbars[0].max);
3968 /* avoid that moving components around generates events */
3969 ++hold_gui_events;
3971 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3973 if (wp->w_buffer == NULL) /* just in case */
3974 continue;
3975 /* Skip a scrollbar that is being dragged. */
3976 if (!force && (gui.dragged_sb == SBAR_LEFT
3977 || gui.dragged_sb == SBAR_RIGHT)
3978 && gui.dragged_wp == wp)
3979 continue;
3981 #ifdef SCROLL_PAST_END
3982 max = wp->w_buffer->b_ml.ml_line_count - 1;
3983 #else
3984 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3985 #endif
3986 if (max < 0) /* empty buffer */
3987 max = 0;
3988 val = wp->w_topline - 1;
3989 size = wp->w_height;
3990 #ifdef SCROLL_PAST_END
3991 if (val > max) /* just in case */
3992 val = max;
3993 #else
3994 if (size > max + 1) /* just in case */
3995 size = max + 1;
3996 if (val > max - size + 1)
3997 val = max - size + 1;
3998 #endif
3999 if (val < 0) /* minimal value is 0 */
4000 val = 0;
4003 * Scrollbar at index 0 (the left one) contains all the information.
4004 * It would be the same info for left and right so we just store it for
4005 * one of them.
4007 sb = &wp->w_scrollbars[0];
4010 * Note: no check for valid w_botline. If it's not valid the
4011 * scrollbars will be updated later anyway.
4013 if (size < 1 || wp->w_botline - 2 > max)
4016 * This can happen during changing files. Just don't update the
4017 * scrollbar for now.
4019 sb->height = 0; /* Force update next time */
4020 if (gui.which_scrollbars[SBAR_LEFT])
4021 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4022 if (gui.which_scrollbars[SBAR_RIGHT])
4023 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4024 continue;
4026 if (force || sb->height != wp->w_height
4027 #ifdef FEAT_WINDOWS
4028 || sb->top != wp->w_winrow
4029 || sb->status_height != wp->w_status_height
4030 # ifdef FEAT_VERTSPLIT
4031 || sb->width != wp->w_width
4032 || prev_curwin != curwin
4033 # endif
4034 #endif
4037 /* Height, width or position of scrollbar has changed. For
4038 * vertical split: curwin changed. */
4039 sb->height = wp->w_height;
4040 #ifdef FEAT_WINDOWS
4041 sb->top = wp->w_winrow;
4042 sb->status_height = wp->w_status_height;
4043 # ifdef FEAT_VERTSPLIT
4044 sb->width = wp->w_width;
4045 # endif
4046 #endif
4048 /* Calculate height and position in pixels */
4049 h = (sb->height + sb->status_height) * gui.char_height;
4050 y = sb->top * gui.char_height + gui.border_offset;
4051 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4052 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MACVIM))
4053 if (gui.menu_is_active)
4054 y += gui.menu_height;
4055 #endif
4057 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4058 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4059 # ifdef FEAT_GUI_ATHENA
4060 y += gui.toolbar_height;
4061 # else
4062 # ifdef FEAT_GUI_MSWIN
4063 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4064 # endif
4065 # endif
4066 #endif
4068 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4069 if (gui_has_tabline())
4070 y += gui.tabline_height;
4071 #endif
4073 #ifdef FEAT_WINDOWS
4074 if (wp->w_winrow == 0)
4075 #endif
4077 /* Height of top scrollbar includes width of top border */
4078 h += gui.border_offset;
4079 y -= gui.border_offset;
4081 if (gui.which_scrollbars[SBAR_LEFT])
4083 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4084 gui.left_sbar_x, y,
4085 gui.scrollbar_width, h);
4086 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4088 if (gui.which_scrollbars[SBAR_RIGHT])
4090 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4091 gui.right_sbar_x, y,
4092 gui.scrollbar_width, h);
4093 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4097 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4098 * checking if the thumb moved at least a pixel. Only do this for
4099 * Athena, most other GUIs require the update anyway to make the
4100 * arrows work. */
4101 #ifdef FEAT_GUI_ATHENA
4102 if (max == 0)
4103 y = 0;
4104 else
4105 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4106 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4107 #else
4108 if (force || sb->value != val || sb->size != size || sb->max != max)
4109 #endif
4111 /* Thumb of scrollbar has moved */
4112 sb->value = val;
4113 #ifdef FEAT_GUI_ATHENA
4114 sb->pixval = y;
4115 #endif
4116 sb->size = size;
4117 sb->max = max;
4118 if (gui.which_scrollbars[SBAR_LEFT]
4119 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4120 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4121 val, size, max);
4122 if (gui.which_scrollbars[SBAR_RIGHT]
4123 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4124 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4125 val, size, max);
4128 #ifdef FEAT_VERTSPLIT
4129 prev_curwin = curwin;
4130 #endif
4131 --hold_gui_events;
4135 * Enable or disable a scrollbar.
4136 * Check for scrollbars for vertically split windows which are not enabled
4137 * sometimes.
4139 static void
4140 gui_do_scrollbar(wp, which, enable)
4141 win_T *wp;
4142 int which; /* SBAR_LEFT or SBAR_RIGHT */
4143 int enable; /* TRUE to enable scrollbar */
4145 #ifdef FEAT_VERTSPLIT
4146 int midcol = curwin->w_wincol + curwin->w_width / 2;
4147 int has_midcol = (wp->w_wincol <= midcol
4148 && wp->w_wincol + wp->w_width >= midcol);
4150 /* Only enable scrollbars that contain the middle column of the current
4151 * window. */
4152 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4154 /* Scrollbars only on one side. Don't enable scrollbars that don't
4155 * contain the middle column of the current window. */
4156 if (!has_midcol)
4157 enable = FALSE;
4159 else
4161 /* Scrollbars on both sides. Don't enable scrollbars that neither
4162 * contain the middle column of the current window nor are on the far
4163 * side. */
4164 if (midcol > Columns / 2)
4166 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4167 enable = FALSE;
4169 else
4171 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4172 : !has_midcol)
4173 enable = FALSE;
4176 #endif
4177 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4181 * Scroll a window according to the values set in the globals current_scrollbar
4182 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4183 * or FALSE otherwise.
4186 gui_do_scroll()
4188 win_T *wp, *save_wp;
4189 int i;
4190 long nlines;
4191 pos_T old_cursor;
4192 linenr_T old_topline;
4193 #ifdef FEAT_DIFF
4194 int old_topfill;
4195 #endif
4197 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4198 if (wp == NULL)
4199 break;
4200 if (wp == NULL)
4201 /* Couldn't find window */
4202 return FALSE;
4205 * Compute number of lines to scroll. If zero, nothing to do.
4207 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4208 if (nlines == 0)
4209 return FALSE;
4211 save_wp = curwin;
4212 old_topline = wp->w_topline;
4213 #ifdef FEAT_DIFF
4214 old_topfill = wp->w_topfill;
4215 #endif
4216 old_cursor = wp->w_cursor;
4217 curwin = wp;
4218 curbuf = wp->w_buffer;
4219 if (nlines < 0)
4220 scrolldown(-nlines, gui.dragged_wp == NULL);
4221 else
4222 scrollup(nlines, gui.dragged_wp == NULL);
4223 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4224 * the mouse-up event already, but we still want it to behave like when
4225 * dragging. But not the next click in an arrow. */
4226 if (gui.dragged_sb == SBAR_NONE)
4227 gui.dragged_wp = NULL;
4229 if (old_topline != wp->w_topline
4230 #ifdef FEAT_DIFF
4231 || old_topfill != wp->w_topfill
4232 #endif
4235 if (p_so != 0)
4237 cursor_correct(); /* fix window for 'so' */
4238 update_topline(); /* avoid up/down jump */
4240 if (old_cursor.lnum != wp->w_cursor.lnum)
4241 coladvance(wp->w_curswant);
4242 #ifdef FEAT_SCROLLBIND
4243 wp->w_scbind_pos = wp->w_topline;
4244 #endif
4247 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4248 validate_cursor();
4250 curwin = save_wp;
4251 curbuf = save_wp->w_buffer;
4254 * Don't call updateWindow() when nothing has changed (it will overwrite
4255 * the status line!).
4257 if (old_topline != wp->w_topline
4258 || wp->w_redr_type != 0
4259 #ifdef FEAT_DIFF
4260 || old_topfill != wp->w_topfill
4261 #endif
4264 int type = VALID;
4266 #ifdef FEAT_INS_EXPAND
4267 if (pum_visible())
4269 type = NOT_VALID;
4270 wp->w_lines_valid = 0;
4272 #endif
4273 /* Don't set must_redraw here, it may cause the popup menu to
4274 * disappear when losing focus after a scrollbar drag. */
4275 if (wp->w_redr_type < type)
4276 wp->w_redr_type = type;
4277 updateWindow(wp); /* update window, status line, and cmdline */
4280 #ifdef FEAT_INS_EXPAND
4281 /* May need to redraw the popup menu. */
4282 if (pum_visible())
4283 pum_redraw();
4284 #endif
4286 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4291 * Horizontal scrollbar stuff:
4295 * Return length of line "lnum" for horizontal scrolling.
4297 static colnr_T
4298 scroll_line_len(lnum)
4299 linenr_T lnum;
4301 char_u *p;
4302 colnr_T col;
4303 int w;
4305 p = ml_get(lnum);
4306 col = 0;
4307 if (*p != NUL)
4308 for (;;)
4310 w = chartabsize(p, col);
4311 mb_ptr_adv(p);
4312 if (*p == NUL) /* don't count the last character */
4313 break;
4314 col += w;
4316 return col;
4319 /* Remember which line is currently the longest, so that we don't have to
4320 * search for it when scrolling horizontally. */
4321 static linenr_T longest_lnum = 0;
4323 static void
4324 gui_update_horiz_scrollbar(force)
4325 int force;
4327 long value, size, max; /* need 32 bit ints here */
4329 if (!gui.which_scrollbars[SBAR_BOTTOM])
4330 return;
4332 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4333 return;
4335 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4336 return;
4339 * It is possible for the cursor to be invalid if we're in the middle of
4340 * something (like changing files). If so, don't do anything for now.
4342 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4344 gui.bottom_sbar.value = -1;
4345 return;
4348 size = W_WIDTH(curwin);
4349 if (curwin->w_p_wrap)
4351 value = 0;
4352 #ifdef SCROLL_PAST_END
4353 max = 0;
4354 #else
4355 max = W_WIDTH(curwin) - 1;
4356 #endif
4358 else
4360 value = curwin->w_leftcol;
4362 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4363 * line numbers, topline and botline can be invalid when displaying is
4364 * postponed. */
4365 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4366 && curwin->w_topline <= curwin->w_cursor.lnum
4367 && curwin->w_botline > curwin->w_cursor.lnum
4368 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4370 linenr_T lnum;
4371 colnr_T n;
4373 /* Use maximum of all visible lines. Remember the lnum of the
4374 * longest line, clostest to the cursor line. Used when scrolling
4375 * below. */
4376 max = 0;
4377 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4379 n = scroll_line_len(lnum);
4380 if (n > (colnr_T)max)
4382 max = n;
4383 longest_lnum = lnum;
4385 else if (n == (colnr_T)max
4386 && abs((int)(lnum - curwin->w_cursor.lnum))
4387 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4388 longest_lnum = lnum;
4391 else
4392 /* Use cursor line only. */
4393 max = scroll_line_len(curwin->w_cursor.lnum);
4394 #ifdef FEAT_VIRTUALEDIT
4395 if (virtual_active())
4397 /* May move the cursor even further to the right. */
4398 if (curwin->w_virtcol >= (colnr_T)max)
4399 max = curwin->w_virtcol;
4401 #endif
4403 #ifndef SCROLL_PAST_END
4404 max += W_WIDTH(curwin) - 1;
4405 #endif
4406 /* The line number isn't scrolled, thus there is less space when
4407 * 'number' is set (also for 'foldcolumn'). */
4408 size -= curwin_col_off();
4409 #ifndef SCROLL_PAST_END
4410 max -= curwin_col_off();
4411 #endif
4414 #ifndef SCROLL_PAST_END
4415 if (value > max - size + 1)
4416 value = max - size + 1; /* limit the value to allowable range */
4417 #endif
4419 #ifdef FEAT_RIGHTLEFT
4420 if (curwin->w_p_rl)
4422 value = max + 1 - size - value;
4423 if (value < 0)
4425 size += value;
4426 value = 0;
4429 #endif
4430 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4431 && max == gui.bottom_sbar.max)
4432 return;
4434 gui.bottom_sbar.value = value;
4435 gui.bottom_sbar.size = size;
4436 gui.bottom_sbar.max = max;
4437 gui.prev_wrap = curwin->w_p_wrap;
4439 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4443 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4446 gui_do_horiz_scroll()
4448 /* no wrapping, no scrolling */
4449 if (curwin->w_p_wrap)
4450 return FALSE;
4452 if ((long_u)curwin->w_leftcol == scrollbar_value)
4453 return FALSE;
4455 curwin->w_leftcol = (colnr_T)scrollbar_value;
4457 /* When the line of the cursor is too short, move the cursor to the
4458 * longest visible line. Do a sanity check on "longest_lnum", just in
4459 * case. */
4460 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4461 && longest_lnum >= curwin->w_topline
4462 && longest_lnum < curwin->w_botline
4463 && !virtual_active())
4465 if (scrollbar_value > (long_u)scroll_line_len(curwin->w_cursor.lnum))
4467 curwin->w_cursor.lnum = longest_lnum;
4468 curwin->w_cursor.col = 0;
4472 return leftcol_changed();
4476 * Check that none of the colors are the same as the background color
4478 void
4479 gui_check_colors()
4481 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4483 gui_set_bg_color((char_u *)"White");
4484 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4485 gui_set_fg_color((char_u *)"Black");
4489 static void
4490 gui_set_fg_color(name)
4491 char_u *name;
4493 gui.norm_pixel = gui_get_color(name);
4494 hl_set_fg_color_name(vim_strsave(name));
4497 static void
4498 gui_set_bg_color(name)
4499 char_u *name;
4501 gui.back_pixel = gui_get_color(name);
4502 hl_set_bg_color_name(vim_strsave(name));
4506 * Allocate a color by name.
4507 * Returns INVALCOLOR and gives an error message when failed.
4509 guicolor_T
4510 gui_get_color(name)
4511 char_u *name;
4513 guicolor_T t;
4515 if (*name == NUL)
4516 return INVALCOLOR;
4517 t = gui_mch_get_color(name);
4519 if (t == INVALCOLOR
4520 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4521 && gui.in_use
4522 #endif
4524 EMSG2(_("E254: Cannot allocate color %s"), name);
4525 return t;
4529 * Return the grey value of a color (range 0-255).
4532 gui_get_lightness(pixel)
4533 guicolor_T pixel;
4535 long_u rgb = gui_mch_get_rgb(pixel);
4537 return (int)( (((rgb >> 16) & 0xff) * 299)
4538 + (((rgb >> 8) & 0xff) * 587)
4539 + ((rgb & 0xff) * 114)) / 1000;
4542 #if defined(FEAT_GUI_X11) || defined(PROTO)
4543 void
4544 gui_new_scrollbar_colors()
4546 win_T *wp;
4548 /* Nothing to do if GUI hasn't started yet. */
4549 if (!gui.in_use)
4550 return;
4552 FOR_ALL_WINDOWS(wp)
4554 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4555 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4557 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4559 #endif
4562 * Call this when focus has changed.
4564 void
4565 gui_focus_change(in_focus)
4566 int in_focus;
4569 * Skip this code to avoid drawing the cursor when debugging and switching
4570 * between the debugger window and gvim.
4572 #if 1
4573 gui.in_focus = in_focus;
4574 out_flush(); /* make sure output has been written */
4575 gui_update_cursor(TRUE, FALSE);
4577 # ifdef FEAT_XIM
4578 xim_set_focus(in_focus);
4579 # endif
4581 /* Put events in the input queue only when allowed.
4582 * ui_focus_change() isn't called directly, because it invokes
4583 * autocommands and that must not happen asynchronously. */
4584 if (!hold_gui_events)
4586 char_u bytes[3];
4588 bytes[0] = CSI;
4589 bytes[1] = KS_EXTRA;
4590 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4591 add_to_input_buf(bytes, 3);
4593 #endif
4597 * Called when the mouse moved (but not when dragging).
4599 void
4600 gui_mouse_moved(x, y)
4601 int x;
4602 int y;
4604 win_T *wp;
4605 char_u st[8];
4607 /* Ignore this while still starting up. */
4608 if (!gui.in_use || gui.starting)
4609 return;
4611 #ifdef FEAT_MOUSESHAPE
4612 /* Get window pointer, and update mouse shape as well. */
4613 wp = xy2win(x, y);
4614 #endif
4616 /* Only handle this when 'mousefocus' set and ... */
4617 if (p_mousef
4618 && !hold_gui_events /* not holding events */
4619 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4620 && State != HITRETURN /* but not hit-return prompt */
4621 && msg_scrolled == 0 /* no scrolled message */
4622 && !need_mouse_correct /* not moving the pointer */
4623 && gui.in_focus) /* gvim in focus */
4625 /* Don't move the mouse when it's left or right of the Vim window */
4626 if (x < 0 || x > Columns * gui.char_width)
4627 return;
4628 #ifndef FEAT_MOUSESHAPE
4629 wp = xy2win(x, y);
4630 #endif
4631 if (wp == curwin || wp == NULL)
4632 return; /* still in the same old window, or none at all */
4634 #ifdef FEAT_WINDOWS
4635 /* Ignore position in the tab pages line. */
4636 if (Y_2_ROW(y) < tabline_height())
4637 return;
4638 #endif
4641 * format a mouse click on status line input
4642 * ala gui_send_mouse_event(0, x, y, 0, 0);
4643 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4644 * generate a K_LEFTMOUSE_NM key code.
4646 if (finish_op)
4648 /* abort the current operator first */
4649 st[0] = ESC;
4650 add_to_input_buf(st, 1);
4652 st[0] = CSI;
4653 st[1] = KS_MOUSE;
4654 st[2] = KE_FILLER;
4655 st[3] = (char_u)MOUSE_LEFT;
4656 fill_mouse_coord(st + 4,
4657 #ifdef FEAT_VERTSPLIT
4658 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4659 #else
4661 #endif
4662 wp->w_height + W_WINROW(wp));
4664 add_to_input_buf(st, 8);
4665 st[3] = (char_u)MOUSE_RELEASE;
4666 add_to_input_buf(st, 8);
4667 #ifdef FEAT_GUI_GTK
4668 /* Need to wake up the main loop */
4669 if (gtk_main_level() > 0)
4670 gtk_main_quit();
4671 #endif
4676 * Called when mouse should be moved to window with focus.
4678 void
4679 gui_mouse_correct()
4681 int x, y;
4682 win_T *wp = NULL;
4684 need_mouse_correct = FALSE;
4686 if (!(gui.in_use && p_mousef))
4687 return;
4689 gui_mch_getmouse(&x, &y);
4690 /* Don't move the mouse when it's left or right of the Vim window */
4691 if (x < 0 || x > Columns * gui.char_width)
4692 return;
4693 if (y >= 0
4694 # ifdef FEAT_WINDOWS
4695 && Y_2_ROW(y) >= tabline_height()
4696 # endif
4698 wp = xy2win(x, y);
4699 if (wp != curwin && wp != NULL) /* If in other than current window */
4701 validate_cline_row();
4702 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4703 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4704 + (gui.char_height) / 2);
4709 * Find window where the mouse pointer "y" coordinate is in.
4711 static win_T *
4712 xy2win(x, y)
4713 int x UNUSED;
4714 int y UNUSED;
4716 #ifdef FEAT_WINDOWS
4717 int row;
4718 int col;
4719 win_T *wp;
4721 row = Y_2_ROW(y);
4722 col = X_2_COL(x);
4723 if (row < 0 || col < 0) /* before first window */
4724 return NULL;
4725 wp = mouse_find_win(&row, &col);
4726 # ifdef FEAT_MOUSESHAPE
4727 if (State == HITRETURN || State == ASKMORE)
4729 if (Y_2_ROW(y) >= msg_row)
4730 update_mouseshape(SHAPE_IDX_MOREL);
4731 else
4732 update_mouseshape(SHAPE_IDX_MORE);
4734 else if (row > wp->w_height) /* below status line */
4735 update_mouseshape(SHAPE_IDX_CLINE);
4736 # ifdef FEAT_VERTSPLIT
4737 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4738 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4739 update_mouseshape(SHAPE_IDX_VSEP);
4740 # endif
4741 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4742 && row == wp->w_height && msg_scrolled == 0)
4743 update_mouseshape(SHAPE_IDX_STATUS);
4744 else
4745 update_mouseshape(-2);
4746 # endif
4747 return wp;
4748 #else
4749 return firstwin;
4750 #endif
4754 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4755 * File names may be given to redefine the args list.
4757 void
4758 ex_gui(eap)
4759 exarg_T *eap;
4761 char_u *arg = eap->arg;
4764 * Check for "-f" argument: foreground, don't fork.
4765 * Also don't fork when started with "gvim -f".
4766 * Do fork when using "gui -b".
4767 * Note that Mac OS X will never fork on :gui since it can only fork on
4768 * startup right after scanning the command line.
4770 if (arg[0] == '-'
4771 && (arg[1] == 'f' || arg[1] == 'b')
4772 && (arg[2] == NUL || vim_iswhite(arg[2])))
4774 gui.dofork = (arg[1] == 'b');
4775 eap->arg = skipwhite(eap->arg + 2);
4777 if (!gui.in_use)
4779 /* Clear the command. Needed for when forking+exiting, to avoid part
4780 * of the argument ending up after the shell prompt. */
4781 msg_clr_eos_force();
4782 gui_start();
4784 if (!ends_excmd(*eap->arg))
4785 ex_next(eap);
4788 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4789 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR) \
4790 || defined(FEAT_GUI_MACVIM)) || defined(PROTO)
4792 * This is shared between Athena, Motif and GTK.
4794 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4797 * Callback function for do_in_runtimepath().
4799 static void
4800 gfp_setname(fname, cookie)
4801 char_u *fname;
4802 void *cookie;
4804 char_u *gfp_buffer = cookie;
4806 if (STRLEN(fname) >= MAXPATHL)
4807 *gfp_buffer = NUL;
4808 else
4809 STRCPY(gfp_buffer, fname);
4813 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4814 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4817 gui_find_bitmap(name, buffer, ext)
4818 char_u *name;
4819 char_u *buffer;
4820 char *ext;
4822 if (STRLEN(name) > MAXPATHL - 14)
4823 return FAIL;
4824 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4825 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4826 || *buffer == NUL)
4827 return FAIL;
4828 return OK;
4831 # if !defined(HAVE_GTK2) || defined(PROTO)
4833 * Given the name of the "icon=" argument, try finding the bitmap file for the
4834 * icon. If it is an absolute path name, use it as it is. Otherwise append
4835 * "ext" and search for it in 'runtimepath'.
4836 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4837 * contains "name".
4839 void
4840 gui_find_iconfile(name, buffer, ext)
4841 char_u *name;
4842 char_u *buffer;
4843 char *ext;
4845 char_u buf[MAXPATHL + 1];
4847 expand_env(name, buffer, MAXPATHL);
4848 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4849 STRCPY(buffer, buf);
4851 # endif
4852 #endif
4854 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4855 void
4856 display_errors()
4858 char_u *p;
4860 if (isatty(2))
4861 fflush(stderr);
4862 else if (error_ga.ga_data != NULL)
4864 /* avoid putting up a message box with blanks only */
4865 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4866 if (!isspace(*p))
4868 /* Truncate a very long message, it will go off-screen. */
4869 if (STRLEN(p) > 2000)
4870 STRCPY(p + 2000 - 14, "...(truncated)");
4871 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4872 p, (char_u *)_("&Ok"), 1, NULL);
4873 break;
4875 ga_clear(&error_ga);
4878 #endif
4880 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4882 * Return TRUE if still starting up and there is no place to enter text.
4883 * For GTK and X11 we check if stderr is not a tty, which means we were
4884 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4885 * allow typing on stdin.
4888 no_console_input()
4890 return ((!gui.in_use || gui.starting)
4891 # ifndef NO_CONSOLE
4892 && !isatty(0) && !isatty(2)
4893 # endif
4896 #endif
4898 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4899 || defined(NEED_GUI_UPDATE_SCREEN) \
4900 || defined(PROTO)
4902 * Update the current window and the screen.
4904 void
4905 gui_update_screen()
4907 update_topline();
4908 validate_cursor();
4909 #ifdef FEAT_AUTOCMD
4910 /* Trigger CursorMoved if the cursor moved. */
4911 if (!finish_op && has_cursormoved()
4912 && !equalpos(last_cursormoved, curwin->w_cursor))
4914 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4915 last_cursormoved = curwin->w_cursor;
4917 #endif
4918 update_screen(0); /* may need to update the screen */
4919 setcursor();
4920 out_flush(); /* make sure output has been written */
4921 gui_update_cursor(TRUE, FALSE);
4922 gui_mch_flush();
4924 #endif
4926 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4927 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4930 * Get the text to use in a find/replace dialog. Uses the last search pattern
4931 * if the argument is empty.
4932 * Returns an allocated string.
4934 char_u *
4935 get_find_dialog_text(arg, wwordp, mcasep)
4936 char_u *arg;
4937 int *wwordp; /* return: TRUE if \< \> found */
4938 int *mcasep; /* return: TRUE if \C found */
4940 char_u *text;
4942 if (*arg == NUL)
4943 text = last_search_pat();
4944 else
4945 text = arg;
4946 if (text != NULL)
4948 text = vim_strsave(text);
4949 if (text != NULL)
4951 int len = (int)STRLEN(text);
4952 int i;
4954 /* Remove "\V" */
4955 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4957 mch_memmove(text, text + 2, (size_t)(len - 1));
4958 len -= 2;
4961 /* Recognize "\c" and "\C" and remove. */
4962 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4964 *mcasep = (text[1] == 'C');
4965 mch_memmove(text, text + 2, (size_t)(len - 1));
4966 len -= 2;
4969 /* Recognize "\<text\>" and remove. */
4970 if (len >= 4
4971 && STRNCMP(text, "\\<", 2) == 0
4972 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4974 *wwordp = TRUE;
4975 mch_memmove(text, text + 2, (size_t)(len - 4));
4976 text[len - 4] = NUL;
4979 /* Recognize "\/" or "\?" and remove. */
4980 for (i = 0; i + 1 < len; ++i)
4981 if (text[i] == '\\' && (text[i + 1] == '/'
4982 || text[i + 1] == '?'))
4984 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4985 --len;
4989 return text;
4993 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4995 static void
4996 concat_esc(gap, text, what)
4997 garray_T *gap;
4998 char_u *text;
4999 int what;
5001 while (*text != NUL)
5003 #ifdef FEAT_MBYTE
5004 int l = (*mb_ptr2len)(text);
5006 if (l > 1)
5008 while (--l >= 0)
5009 ga_append(gap, *text++);
5010 continue;
5012 #endif
5013 if (*text == what)
5014 ga_append(gap, '\\');
5015 ga_append(gap, *text);
5016 ++text;
5021 * Handle the press of a button in the find-replace dialog.
5022 * Return TRUE when something was added to the input buffer.
5025 gui_do_findrepl(flags, find_text, repl_text, down)
5026 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5027 char_u *find_text;
5028 char_u *repl_text;
5029 int down; /* Search downwards. */
5031 garray_T ga;
5032 int i;
5033 int type = (flags & FRD_TYPE_MASK);
5034 char_u *p;
5035 regmatch_T regmatch;
5036 int save_did_emsg = did_emsg;
5037 static int busy = FALSE;
5039 /* When the screen is being updated we should not change buffers and
5040 * windows structures, it may cause freed memory to be used. Also don't
5041 * do this recursively (pressing "Find" quickly several times. */
5042 if (updating_screen || busy)
5043 return FALSE;
5045 /* refuse replace when text cannot be changed */
5046 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5047 return FALSE;
5049 busy = TRUE;
5051 ga_init2(&ga, 1, 100);
5052 if (type == FRD_REPLACEALL)
5053 ga_concat(&ga, (char_u *)"%s/");
5055 ga_concat(&ga, (char_u *)"\\V");
5056 if (flags & FRD_MATCH_CASE)
5057 ga_concat(&ga, (char_u *)"\\C");
5058 else
5059 ga_concat(&ga, (char_u *)"\\c");
5060 if (flags & FRD_WHOLE_WORD)
5061 ga_concat(&ga, (char_u *)"\\<");
5062 if (type == FRD_REPLACEALL || down)
5063 concat_esc(&ga, find_text, '/'); /* escape slashes */
5064 else
5065 concat_esc(&ga, find_text, '?'); /* escape '?' */
5066 if (flags & FRD_WHOLE_WORD)
5067 ga_concat(&ga, (char_u *)"\\>");
5069 if (type == FRD_REPLACEALL)
5071 ga_concat(&ga, (char_u *)"/");
5072 /* escape / and \ */
5073 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5074 if (p != NULL)
5075 ga_concat(&ga, p);
5076 vim_free(p);
5077 ga_concat(&ga, (char_u *)"/g");
5079 ga_append(&ga, NUL);
5081 if (type == FRD_REPLACE)
5083 /* Do the replacement when the text at the cursor matches. Thus no
5084 * replacement is done if the cursor was moved! */
5085 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5086 regmatch.rm_ic = 0;
5087 if (regmatch.regprog != NULL)
5089 p = ml_get_cursor();
5090 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5091 && regmatch.startp[0] == p)
5093 /* Clear the command line to remove any old "No match"
5094 * error. */
5095 msg_end_prompt();
5097 if (u_save_cursor() == OK)
5099 /* A button was pressed thus undo should be synced. */
5100 u_sync(FALSE);
5102 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5103 FALSE, FALSE);
5104 ins_str(repl_text);
5107 else
5108 MSG(_("No match at cursor, finding next"));
5109 vim_free(regmatch.regprog);
5113 if (type == FRD_REPLACEALL)
5115 /* A button was pressed, thus undo should be synced. */
5116 u_sync(FALSE);
5117 do_cmdline_cmd(ga.ga_data);
5119 else
5121 /* Search for the next match. */
5122 i = msg_scroll;
5123 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5124 SEARCH_MSG + SEARCH_MARK, NULL);
5125 msg_scroll = i; /* don't let an error message set msg_scroll */
5128 /* Don't want to pass did_emsg to other code, it may cause disabling
5129 * syntax HL if we were busy redrawing. */
5130 did_emsg = save_did_emsg;
5132 if (State & (NORMAL | INSERT))
5134 gui_update_screen(); /* update the screen */
5135 msg_didout = 0; /* overwrite any message */
5136 need_wait_return = FALSE; /* don't wait for return */
5139 vim_free(ga.ga_data);
5140 busy = FALSE;
5141 return (ga.ga_len > 0);
5144 #endif
5146 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5147 || defined(FEAT_GUI_MSWIN) \
5148 || defined(FEAT_GUI_MAC) \
5149 || defined(PROTO) \
5150 || defined(FEAT_GUI_MACVIM)
5152 #ifdef FEAT_WINDOWS
5153 static void gui_wingoto_xy __ARGS((int x, int y));
5156 * Jump to the window at specified point (x, y).
5158 static void
5159 gui_wingoto_xy(x, y)
5160 int x;
5161 int y;
5163 int row = Y_2_ROW(y);
5164 int col = X_2_COL(x);
5165 win_T *wp;
5167 if (row >= 0 && col >= 0)
5169 wp = mouse_find_win(&row, &col);
5170 if (wp != NULL && wp != curwin)
5171 win_goto(wp);
5174 #endif
5177 * Process file drop. Mouse cursor position, key modifiers, name of files
5178 * and count of files are given. Argument "fnames[count]" has full pathnames
5179 * of dropped files, they will be freed in this function, and caller can't use
5180 * fnames after call this function.
5182 void
5183 gui_handle_drop(x, y, modifiers, fnames, count)
5184 int x UNUSED;
5185 int y UNUSED;
5186 int_u modifiers;
5187 char_u **fnames;
5188 int count;
5190 int i;
5191 char_u *p;
5192 static int entered = FALSE;
5195 * This function is called by event handlers. Just in case we get a
5196 * second event before the first one is handled, ignore the second one.
5197 * Not sure if this can ever happen, just in case.
5199 if (entered)
5200 return;
5201 entered = TRUE;
5204 * When the cursor is at the command line, add the file names to the
5205 * command line, don't edit the files.
5207 if (State & CMDLINE)
5209 shorten_filenames(fnames, count);
5210 for (i = 0; i < count; ++i)
5212 if (fnames[i] != NULL)
5214 if (i > 0)
5215 add_to_input_buf((char_u*)" ", 1);
5217 /* We don't know what command is used thus we can't be sure
5218 * about which characters need to be escaped. Only escape the
5219 * most common ones. */
5220 # ifdef BACKSLASH_IN_FILENAME
5221 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5222 # else
5223 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5224 # endif
5225 if (p != NULL)
5226 add_to_input_buf_csi(p, (int)STRLEN(p));
5227 vim_free(p);
5228 vim_free(fnames[i]);
5231 vim_free(fnames);
5233 else
5235 /* Go to the window under mouse cursor, then shorten given "fnames" by
5236 * current window, because a window can have local current dir. */
5237 # ifdef FEAT_WINDOWS
5238 gui_wingoto_xy(x, y);
5239 # endif
5240 shorten_filenames(fnames, count);
5242 /* If Shift held down, remember the first item. */
5243 if ((modifiers & MOUSE_SHIFT) != 0)
5244 p = vim_strsave(fnames[0]);
5245 else
5246 p = NULL;
5248 /* Handle the drop, :edit or :split to get to the file. This also
5249 * frees fnames[]. Skip this if there is only one item it's a
5250 * directory and Shift is held down. */
5251 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5252 && mch_isdir(fnames[0]))
5254 vim_free(fnames[0]);
5255 vim_free(fnames);
5257 else
5258 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5260 /* If Shift held down, change to first file's directory. If the first
5261 * item is a directory, change to that directory (and let the explorer
5262 * plugin show the contents). */
5263 if (p != NULL)
5265 if (mch_isdir(p))
5267 if (mch_chdir((char *)p) == 0)
5268 shorten_fnames(TRUE);
5270 else if (vim_chdirfile(p) == OK)
5271 shorten_fnames(TRUE);
5272 vim_free(p);
5275 /* Update the screen display */
5276 update_screen(NOT_VALID);
5277 # ifdef FEAT_MENU
5278 gui_update_menus(0);
5279 # endif
5280 setcursor();
5281 out_flush();
5282 gui_update_cursor(FALSE, FALSE);
5283 gui_mch_flush();
5286 entered = FALSE;
5288 #endif