Merge branch 'MacVim'
[MacVim/KaoriYa.git] / src / gui.c
blobdb52d52a7c7a0f2c102c260c16af7bc089408a4b
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;
1407 #ifdef HAVE_GTK2
1408 int un_maximize = mustset;
1409 int did_adjust = 0;
1410 #endif
1411 int x = -1, y = -1;
1413 if (!gui.shell_created)
1414 return;
1416 #ifdef MSWIN
1417 /* If not setting to a user specified size and maximized, calculate the
1418 * number of characters that fit in the maximized window. */
1419 if (!mustset && gui_mch_maximized())
1421 gui_mch_newfont();
1422 return;
1424 #endif
1426 base_width = gui_get_base_width();
1427 base_height = gui_get_base_height();
1428 if (fit_to_display)
1429 /* Remember the original window position. */
1430 gui_mch_get_winpos(&x, &y);
1432 #ifdef USE_SUN_WORKSHOP
1433 if (!mustset && usingSunWorkShop
1434 && workshop_get_width_height(&width, &height))
1436 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1437 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1439 else
1440 #endif
1442 width = Columns * gui.char_width + base_width;
1443 height = Rows * gui.char_height + base_height;
1446 if (fit_to_display)
1448 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1449 if ((direction & RESIZE_HOR) && width > screen_w)
1451 Columns = (screen_w - base_width) / gui.char_width;
1452 if (Columns < MIN_COLUMNS)
1453 Columns = MIN_COLUMNS;
1454 width = Columns * gui.char_width + base_width;
1455 #ifdef HAVE_GTK2
1456 ++did_adjust;
1457 #endif
1459 if ((direction & RESIZE_VERT) && height > screen_h)
1461 Rows = (screen_h - base_height) / gui.char_height;
1462 check_shellsize();
1463 height = Rows * gui.char_height + base_height;
1464 #ifdef HAVE_GTK2
1465 ++did_adjust;
1466 #endif
1468 #ifdef HAVE_GTK2
1469 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1470 && height + gui.char_height >= screen_h))
1471 /* don't unmaximize if at maximum size */
1472 un_maximize = FALSE;
1473 #endif
1475 gui.num_cols = Columns;
1476 gui.num_rows = Rows;
1478 min_width = base_width + MIN_COLUMNS * gui.char_width;
1479 min_height = base_height + MIN_LINES * gui.char_height;
1480 #ifdef FEAT_WINDOWS
1481 min_height += tabline_height() * gui.char_height;
1482 #endif
1484 #ifdef HAVE_GTK2
1485 if (un_maximize)
1487 /* If the window size is smaller than the screen unmaximize the
1488 * window, otherwise resizing won't work. */
1489 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1490 if ((width + gui.char_width < screen_w
1491 || height + gui.char_height * 2 < screen_h)
1492 && gui_mch_maximized())
1493 gui_mch_unmaximize();
1495 #endif
1497 gui_mch_set_shellsize(width, height, min_width, min_height,
1498 base_width, base_height, direction);
1500 if (fit_to_display && x >= 0 && y >= 0)
1502 /* Some window managers put the Vim window left of/above the screen.
1503 * Only change the position if it wasn't already negative before
1504 * (happens on MS-Windows with a secondary monitor). */
1505 gui_mch_update();
1506 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1507 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1510 gui_position_components(width);
1511 gui_update_scrollbars(TRUE);
1512 gui_reset_scroll_region();
1516 * Called when Rows and/or Columns has changed.
1518 void
1519 gui_new_shellsize()
1521 gui_reset_scroll_region();
1525 * Make scroll region cover whole screen.
1527 void
1528 gui_reset_scroll_region()
1530 gui.scroll_region_top = 0;
1531 gui.scroll_region_bot = gui.num_rows - 1;
1532 gui.scroll_region_left = 0;
1533 gui.scroll_region_right = gui.num_cols - 1;
1536 void
1537 gui_start_highlight(mask)
1538 int mask;
1540 if (mask > HL_ALL) /* highlight code */
1541 gui.highlight_mask = mask;
1542 else /* mask */
1543 gui.highlight_mask |= mask;
1546 void
1547 gui_stop_highlight(mask)
1548 int mask;
1550 if (mask > HL_ALL) /* highlight code */
1551 gui.highlight_mask = HL_NORMAL;
1552 else /* mask */
1553 gui.highlight_mask &= ~mask;
1557 * Clear a rectangular region of the screen from text pos (row1, col1) to
1558 * (row2, col2) inclusive.
1560 void
1561 gui_clear_block(row1, col1, row2, col2)
1562 int row1;
1563 int col1;
1564 int row2;
1565 int col2;
1567 /* Clear the selection if we are about to write over it */
1568 clip_may_clear_selection(row1, row2);
1570 gui_mch_clear_block(row1, col1, row2, col2);
1572 /* Invalidate cursor if it was in this block */
1573 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1574 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1575 gui.cursor_is_valid = FALSE;
1579 * Write code to update the cursor later. This avoids the need to flush the
1580 * output buffer before calling gui_update_cursor().
1582 void
1583 gui_update_cursor_later()
1585 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1588 void
1589 gui_write(s, len)
1590 char_u *s;
1591 int len;
1593 char_u *p;
1594 int arg1 = 0, arg2 = 0;
1595 /* this doesn't make sense, disabled until someone can explain why it
1596 * would be needed */
1597 #if 0 && (defined(RISCOS) || defined(WIN16))
1598 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1599 our redraw speed will suffer */
1600 #else
1601 int force_cursor = FALSE; /* force cursor update */
1602 #endif
1603 int force_scrollbar = FALSE;
1604 static win_T *old_curwin = NULL;
1606 /* #define DEBUG_GUI_WRITE */
1607 #ifdef DEBUG_GUI_WRITE
1609 int i;
1610 char_u *str;
1612 printf("gui_write(%d):\n ", len);
1613 for (i = 0; i < len; i++)
1614 if (s[i] == ESC)
1616 if (i != 0)
1617 printf("\n ");
1618 printf("<ESC>");
1620 else
1622 str = transchar_byte(s[i]);
1623 if (str[0] && str[1])
1624 printf("<%s>", (char *)str);
1625 else
1626 printf("%s", (char *)str);
1628 printf("\n");
1630 #endif
1631 while (len)
1633 if (s[0] == ESC && s[1] == '|')
1635 p = s + 2;
1636 if (VIM_ISDIGIT(*p))
1638 arg1 = getdigits(&p);
1639 if (p > s + len)
1640 break;
1641 if (*p == ';')
1643 ++p;
1644 arg2 = getdigits(&p);
1645 if (p > s + len)
1646 break;
1649 switch (*p)
1651 case 'C': /* Clear screen */
1652 clip_scroll_selection(9999);
1653 gui_mch_clear_all();
1654 gui.cursor_is_valid = FALSE;
1655 force_scrollbar = TRUE;
1656 break;
1657 case 'M': /* Move cursor */
1658 gui_set_cursor(arg1, arg2);
1659 break;
1660 case 's': /* force cursor (shape) update */
1661 force_cursor = TRUE;
1662 break;
1663 case 'R': /* Set scroll region */
1664 if (arg1 < arg2)
1666 gui.scroll_region_top = arg1;
1667 gui.scroll_region_bot = arg2;
1669 else
1671 gui.scroll_region_top = arg2;
1672 gui.scroll_region_bot = arg1;
1674 break;
1675 #ifdef FEAT_VERTSPLIT
1676 case 'V': /* Set vertical scroll region */
1677 if (arg1 < arg2)
1679 gui.scroll_region_left = arg1;
1680 gui.scroll_region_right = arg2;
1682 else
1684 gui.scroll_region_left = arg2;
1685 gui.scroll_region_right = arg1;
1687 break;
1688 #endif
1689 case 'd': /* Delete line */
1690 gui_delete_lines(gui.row, 1);
1691 break;
1692 case 'D': /* Delete lines */
1693 gui_delete_lines(gui.row, arg1);
1694 break;
1695 case 'i': /* Insert line */
1696 gui_insert_lines(gui.row, 1);
1697 break;
1698 case 'I': /* Insert lines */
1699 gui_insert_lines(gui.row, arg1);
1700 break;
1701 case '$': /* Clear to end-of-line */
1702 gui_clear_block(gui.row, gui.col, gui.row,
1703 (int)Columns - 1);
1704 break;
1705 case 'h': /* Turn on highlighting */
1706 gui_start_highlight(arg1);
1707 break;
1708 case 'H': /* Turn off highlighting */
1709 gui_stop_highlight(arg1);
1710 break;
1711 case 'f': /* flash the window (visual bell) */
1712 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1713 break;
1714 default:
1715 p = s + 1; /* Skip the ESC */
1716 break;
1718 len -= (int)(++p - s);
1719 s = p;
1721 else if (
1722 #ifdef EBCDIC
1723 CtrlChar(s[0]) != 0 /* Ctrl character */
1724 #else
1725 s[0] < 0x20 /* Ctrl character */
1726 #endif
1727 #ifdef FEAT_SIGN_ICONS
1728 && s[0] != SIGN_BYTE
1729 # ifdef FEAT_NETBEANS_INTG
1730 && s[0] != MULTISIGN_BYTE
1731 # endif
1732 #endif
1735 if (s[0] == '\n') /* NL */
1737 gui.col = 0;
1738 if (gui.row < gui.scroll_region_bot)
1739 gui.row++;
1740 else
1741 gui_delete_lines(gui.scroll_region_top, 1);
1743 else if (s[0] == '\r') /* CR */
1745 gui.col = 0;
1747 else if (s[0] == '\b') /* Backspace */
1749 if (gui.col)
1750 --gui.col;
1752 else if (s[0] == Ctrl_L) /* cursor-right */
1754 ++gui.col;
1756 else if (s[0] == Ctrl_G) /* Beep */
1758 gui_mch_beep();
1760 /* Other Ctrl character: shouldn't happen! */
1762 --len; /* Skip this char */
1763 ++s;
1765 else
1767 p = s;
1768 while (len > 0 && (
1769 #ifdef EBCDIC
1770 CtrlChar(*p) == 0
1771 #else
1772 *p >= 0x20
1773 #endif
1774 #ifdef FEAT_SIGN_ICONS
1775 || *p == SIGN_BYTE
1776 # ifdef FEAT_NETBEANS_INTG
1777 || *p == MULTISIGN_BYTE
1778 # endif
1779 #endif
1782 len--;
1783 p++;
1785 gui_outstr(s, (int)(p - s));
1786 s = p;
1790 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1791 * set). */
1792 if (force_cursor)
1793 gui_update_cursor(TRUE, TRUE);
1795 /* When switching to another window the dragging must have stopped.
1796 * Required for GTK, dragged_sb isn't reset. */
1797 if (old_curwin != curwin)
1798 gui.dragged_sb = SBAR_NONE;
1800 /* Update the scrollbars after clearing the screen or when switched
1801 * to another window.
1802 * Update the horizontal scrollbar always, it's difficult to check all
1803 * situations where it might change. */
1804 if (force_scrollbar || old_curwin != curwin)
1805 gui_update_scrollbars(force_scrollbar);
1806 else
1807 gui_update_horiz_scrollbar(FALSE);
1808 old_curwin = curwin;
1811 * We need to make sure this is cleared since Athena doesn't tell us when
1812 * he is done dragging. Do the same for GTK.
1814 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1815 gui.dragged_sb = SBAR_NONE;
1816 #endif
1818 gui_mch_flush(); /* In case vim decides to take a nap */
1822 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1823 * produces wrong results. Call gui_dont_update_cursor() before that code and
1824 * gui_can_update_cursor() afterwards.
1826 void
1827 gui_dont_update_cursor()
1829 if (gui.in_use)
1831 /* Undraw the cursor now, we probably can't do it after the change. */
1832 gui_undraw_cursor();
1833 can_update_cursor = FALSE;
1837 void
1838 gui_can_update_cursor()
1840 can_update_cursor = TRUE;
1841 /* No need to update the cursor right now, there is always more output
1842 * after scrolling. */
1845 static void
1846 gui_outstr(s, len)
1847 char_u *s;
1848 int len;
1850 int this_len;
1851 #ifdef FEAT_MBYTE
1852 int cells;
1853 #endif
1855 if (len == 0)
1856 return;
1858 if (len < 0)
1859 len = (int)STRLEN(s);
1861 while (len > 0)
1863 #ifdef FEAT_MBYTE
1864 if (has_mbyte)
1866 /* Find out how many chars fit in the current line. */
1867 cells = 0;
1868 for (this_len = 0; this_len < len; )
1870 cells += (*mb_ptr2cells)(s + this_len);
1871 if (gui.col + cells > Columns)
1872 break;
1873 this_len += (*mb_ptr2len)(s + this_len);
1875 if (this_len > len)
1876 this_len = len; /* don't include following composing char */
1878 else
1879 #endif
1880 if (gui.col + len > Columns)
1881 this_len = Columns - gui.col;
1882 else
1883 this_len = len;
1885 (void)gui_outstr_nowrap(s, this_len,
1886 0, (guicolor_T)0, (guicolor_T)0, 0);
1887 s += this_len;
1888 len -= this_len;
1889 #ifdef FEAT_MBYTE
1890 /* fill up for a double-width char that doesn't fit. */
1891 if (len > 0 && gui.col < Columns)
1892 (void)gui_outstr_nowrap((char_u *)" ", 1,
1893 0, (guicolor_T)0, (guicolor_T)0, 0);
1894 #endif
1895 /* The cursor may wrap to the next line. */
1896 if (gui.col >= Columns)
1898 gui.col = 0;
1899 gui.row++;
1905 * Output one character (may be one or two display cells).
1906 * Caller must check for valid "off".
1907 * Returns FAIL or OK, just like gui_outstr_nowrap().
1909 static int
1910 gui_screenchar(off, flags, fg, bg, back)
1911 int off; /* Offset from start of screen */
1912 int flags;
1913 guicolor_T fg, bg; /* colors for cursor */
1914 int back; /* backup this many chars when using bold trick */
1916 #ifdef FEAT_MBYTE
1917 char_u buf[MB_MAXBYTES + 1];
1919 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1920 if (enc_utf8 && ScreenLines[off] == 0)
1921 return OK;
1923 if (enc_utf8 && ScreenLinesUC[off] != 0)
1924 /* Draw UTF-8 multi-byte character. */
1925 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1926 flags, fg, bg, back);
1928 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1930 buf[0] = ScreenLines[off];
1931 buf[1] = ScreenLines2[off];
1932 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1935 /* Draw non-multi-byte character or DBCS character. */
1936 return gui_outstr_nowrap(ScreenLines + off,
1937 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1938 flags, fg, bg, back);
1939 #else
1940 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1941 #endif
1944 #ifdef HAVE_GTK2
1946 * Output the string at the given screen position. This is used in place
1947 * of gui_screenchar() where possible because Pango needs as much context
1948 * as possible to work nicely. It's a lot faster as well.
1950 static int
1951 gui_screenstr(off, len, flags, fg, bg, back)
1952 int off; /* Offset from start of screen */
1953 int len; /* string length in screen cells */
1954 int flags;
1955 guicolor_T fg, bg; /* colors for cursor */
1956 int back; /* backup this many chars when using bold trick */
1958 char_u *buf;
1959 int outlen = 0;
1960 int i;
1961 int retval;
1963 if (len <= 0) /* "cannot happen"? */
1964 return OK;
1966 if (enc_utf8)
1968 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1969 if (buf == NULL)
1970 return OK; /* not much we could do here... */
1972 for (i = off; i < off + len; ++i)
1974 if (ScreenLines[i] == 0)
1975 continue; /* skip second half of double-width char */
1977 if (ScreenLinesUC[i] == 0)
1978 buf[outlen++] = ScreenLines[i];
1979 else
1980 outlen += utfc_char2bytes(i, buf + outlen);
1983 buf[outlen] = NUL; /* only to aid debugging */
1984 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1985 vim_free(buf);
1987 return retval;
1989 else if (enc_dbcs == DBCS_JPNU)
1991 buf = alloc((unsigned)(len * 2 + 1));
1992 if (buf == NULL)
1993 return OK; /* not much we could do here... */
1995 for (i = off; i < off + len; ++i)
1997 buf[outlen++] = ScreenLines[i];
1999 /* handle double-byte single-width char */
2000 if (ScreenLines[i] == 0x8e)
2001 buf[outlen++] = ScreenLines2[i];
2002 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2003 buf[outlen++] = ScreenLines[++i];
2006 buf[outlen] = NUL; /* only to aid debugging */
2007 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2008 vim_free(buf);
2010 return retval;
2012 else
2014 return gui_outstr_nowrap(&ScreenLines[off], len,
2015 flags, fg, bg, back);
2018 #endif /* HAVE_GTK2 */
2021 * Output the given string at the current cursor position. If the string is
2022 * too long to fit on the line, then it is truncated.
2023 * "flags":
2024 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2025 * actually draw (an inverted) cursor.
2026 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
2027 * background.
2028 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2029 * it.
2030 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2031 * FAIL (the caller should start drawing "back" chars back).
2034 gui_outstr_nowrap(s, len, flags, fg, bg, back)
2035 char_u *s;
2036 int len;
2037 int flags;
2038 guicolor_T fg, bg; /* colors for cursor */
2039 int back; /* backup this many chars when using bold trick */
2041 long_u highlight_mask;
2042 long_u hl_mask_todo;
2043 guicolor_T fg_color;
2044 guicolor_T bg_color;
2045 guicolor_T sp_color;
2046 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2047 GuiFont font = NOFONT;
2048 # ifdef FEAT_XFONTSET
2049 GuiFontset fontset = NOFONTSET;
2050 # endif
2051 #endif
2052 attrentry_T *aep = NULL;
2053 int draw_flags;
2054 int col = gui.col;
2055 #ifdef FEAT_SIGN_ICONS
2056 int draw_sign = FALSE;
2057 # ifdef FEAT_NETBEANS_INTG
2058 int multi_sign = FALSE;
2059 # endif
2060 #endif
2062 if (len < 0)
2063 len = (int)STRLEN(s);
2064 if (len == 0)
2065 return OK;
2067 #ifdef FEAT_SIGN_ICONS
2068 if (*s == SIGN_BYTE
2069 # ifdef FEAT_NETBEANS_INTG
2070 || *s == MULTISIGN_BYTE
2071 # endif
2074 # ifdef FEAT_NETBEANS_INTG
2075 if (*s == MULTISIGN_BYTE)
2076 multi_sign = TRUE;
2077 # endif
2078 /* draw spaces instead */
2079 s = (char_u *)" ";
2080 if (len == 1 && col > 0)
2081 --col;
2082 len = 2;
2083 draw_sign = TRUE;
2084 highlight_mask = 0;
2086 else
2087 #endif
2088 if (gui.highlight_mask > HL_ALL)
2090 aep = syn_gui_attr2entry(gui.highlight_mask);
2091 if (aep == NULL) /* highlighting not set */
2092 highlight_mask = 0;
2093 else
2094 highlight_mask = aep->ae_attr;
2096 else
2097 highlight_mask = gui.highlight_mask;
2098 hl_mask_todo = highlight_mask;
2100 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2101 /* Set the font */
2102 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2103 font = aep->ae_u.gui.font;
2104 # ifdef FEAT_XFONTSET
2105 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2106 fontset = aep->ae_u.gui.fontset;
2107 # endif
2108 else
2110 # ifdef FEAT_XFONTSET
2111 if (gui.fontset != NOFONTSET)
2112 fontset = gui.fontset;
2113 else
2114 # endif
2115 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2117 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2119 font = gui.boldital_font;
2120 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2122 else if (gui.bold_font != NOFONT)
2124 font = gui.bold_font;
2125 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2127 else
2128 font = gui.norm_font;
2130 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2132 font = gui.ital_font;
2133 hl_mask_todo &= ~HL_ITALIC;
2135 else
2136 font = gui.norm_font;
2138 # ifdef FEAT_XFONTSET
2139 if (fontset != NOFONTSET)
2140 gui_mch_set_fontset(fontset);
2141 else
2142 # endif
2143 gui_mch_set_font(font);
2144 #endif
2146 draw_flags = 0;
2148 /* Set the color */
2149 bg_color = gui.back_pixel;
2150 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2152 draw_flags |= DRAW_CURSOR;
2153 fg_color = fg;
2154 bg_color = bg;
2155 sp_color = fg;
2157 else if (aep != NULL)
2159 fg_color = aep->ae_u.gui.fg_color;
2160 if (fg_color == INVALCOLOR)
2161 fg_color = gui.norm_pixel;
2162 bg_color = aep->ae_u.gui.bg_color;
2163 if (bg_color == INVALCOLOR)
2164 bg_color = gui.back_pixel;
2165 sp_color = aep->ae_u.gui.sp_color;
2166 if (sp_color == INVALCOLOR)
2167 sp_color = fg_color;
2169 else
2171 fg_color = gui.norm_pixel;
2172 sp_color = fg_color;
2175 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2177 #if defined(AMIGA) || defined(RISCOS)
2178 gui_mch_set_colors(bg_color, fg_color);
2179 #else
2180 gui_mch_set_fg_color(bg_color);
2181 gui_mch_set_bg_color(fg_color);
2182 #endif
2184 else
2186 #if defined(AMIGA) || defined(RISCOS)
2187 gui_mch_set_colors(fg_color, bg_color);
2188 #else
2189 gui_mch_set_fg_color(fg_color);
2190 gui_mch_set_bg_color(bg_color);
2191 #endif
2193 gui_mch_set_sp_color(sp_color);
2195 /* Clear the selection if we are about to write over it */
2196 if (!(flags & GUI_MON_NOCLEAR))
2197 clip_may_clear_selection(gui.row, gui.row);
2200 #ifndef MSWIN16_FASTTEXT
2201 /* If there's no bold font, then fake it */
2202 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2203 draw_flags |= DRAW_BOLD;
2204 #endif
2207 * When drawing bold or italic characters the spill-over from the left
2208 * neighbor may be destroyed. Let the caller backup to start redrawing
2209 * just after a blank.
2211 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2212 return FAIL;
2214 #if defined(RISCOS) || defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
2215 /* If there's no italic font, then fake it.
2216 * For GTK2, we don't need a different font for italic style. */
2217 if (hl_mask_todo & HL_ITALIC)
2218 draw_flags |= DRAW_ITALIC;
2220 /* Do we underline the text? */
2221 if (hl_mask_todo & HL_UNDERLINE)
2222 draw_flags |= DRAW_UNDERL;
2224 # if defined(FEAT_GUI_MACVIM)
2225 /* Do we thick underline the text? */
2226 if (hl_mask_todo & HL_THICKUNDERLINE)
2227 draw_flags |= DRAW_TUNDERL;
2228 # endif
2229 #else
2230 /* Do we underline the text? */
2231 if ((hl_mask_todo & HL_UNDERLINE)
2232 # ifndef MSWIN16_FASTTEXT
2233 || (hl_mask_todo & HL_ITALIC)
2234 # endif
2236 draw_flags |= DRAW_UNDERL;
2237 #endif
2238 /* Do we undercurl the text? */
2239 if (hl_mask_todo & HL_UNDERCURL)
2240 draw_flags |= DRAW_UNDERC;
2242 /* Do we draw transparently? */
2243 if (flags & GUI_MON_TRS_CURSOR)
2244 draw_flags |= DRAW_TRANSP;
2247 * Draw the text.
2249 #ifdef HAVE_GTK2
2250 /* The value returned is the length in display cells */
2251 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2252 #else
2253 # ifdef FEAT_MBYTE
2254 # ifdef FEAT_GUI_MACVIM
2255 if (use_gui_macvim_draw_string)
2257 /* The value returned is the length in display cells */
2258 len = gui_macvim_draw_string(gui.row, col, s, len, draw_flags);
2260 else
2261 # endif
2262 if (enc_utf8)
2264 int start; /* index of bytes to be drawn */
2265 int cells; /* cellwidth of bytes to be drawn */
2266 int thislen; /* length of bytes to be drawin */
2267 int cn; /* cellwidth of current char */
2268 int i; /* index of current char */
2269 int c; /* current char value */
2270 int cl; /* byte length of current char */
2271 int comping; /* current char is composing */
2272 int scol = col; /* screen column */
2273 int dowide; /* use 'guifontwide' */
2275 /* Break the string at a composing character, it has to be drawn on
2276 * top of the previous character. */
2277 start = 0;
2278 cells = 0;
2279 for (i = 0; i < len; i += cl)
2281 c = utf_ptr2char(s + i);
2282 cn = utf_char2cells(c);
2283 comping = utf_iscomposing(c);
2284 if (!comping) /* count cells from non-composing chars */
2285 cells += cn;
2286 cl = utf_ptr2len(s + i);
2287 if (cl == 0) /* hit end of string */
2288 len = i + cl; /* len must be wrong "cannot happen" */
2290 # ifdef FEAT_GUI_MACVIM
2291 dowide = (cn > 1);
2292 # else
2293 if (cn > 1
2294 # ifdef FEAT_XFONTSET
2295 && fontset == NOFONTSET
2296 # endif
2297 && gui.wide_font != NOFONT)
2298 dowide = TRUE;
2299 else
2300 dowide = FALSE;
2301 # endif
2303 /* print the string so far if it's the last character or there is
2304 * a composing character. */
2305 if (i + cl >= len || (comping && i > start) || dowide
2306 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2307 || (cn > 1
2308 # ifdef FEAT_XFONTSET
2309 /* No fontset: At least draw char after wide char at
2310 * right position. */
2311 && fontset == NOFONTSET
2312 # endif
2314 # endif
2317 if (comping || dowide)
2318 thislen = i - start;
2319 else
2320 thislen = i - start + cl;
2321 if (thislen > 0)
2323 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2324 # ifdef FEAT_GUI_MACVIM
2325 cells,
2326 # endif
2327 draw_flags);
2328 start += thislen;
2330 scol += cells;
2331 cells = 0;
2332 if (dowide)
2334 gui_mch_set_font(gui.wide_font);
2335 gui_mch_draw_string(gui.row, scol - cn, s + start, cl,
2336 # ifdef FEAT_GUI_MACVIM
2338 # endif
2339 draw_flags | DRAW_WIDE);
2340 gui_mch_set_font(font);
2341 start += cl;
2344 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2345 /* No fontset: draw a space to fill the gap after a wide char
2346 * */
2347 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2348 # ifdef FEAT_XFONTSET
2349 && fontset == NOFONTSET
2350 # endif
2351 && !dowide)
2352 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2353 1, draw_flags);
2354 # endif
2356 /* Draw a composing char on top of the previous char. */
2357 if (comping)
2359 # if !defined(FEAT_GUI_MACVIM) && \
2360 (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2361 /* Carbon ATSUI autodraws composing char over previous char */
2362 gui_mch_draw_string(gui.row, scol, s + i, cl,
2363 draw_flags | DRAW_TRANSP);
2364 # else
2365 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2366 # ifdef FEAT_GUI_MACVIM
2368 # endif
2369 draw_flags | DRAW_TRANSP | DRAW_COMP);
2370 # endif
2371 start = i + cl;
2374 /* The stuff below assumes "len" is the length in screen columns. */
2375 len = scol - col;
2377 else
2378 # endif
2380 gui_mch_draw_string(gui.row, col, s, len,
2381 # ifdef FEAT_GUI_MACVIM
2382 len,
2383 # endif
2384 draw_flags);
2385 # ifdef FEAT_MBYTE
2386 if (enc_dbcs == DBCS_JPNU)
2388 int clen = 0;
2389 int i;
2391 /* Get the length in display cells, this can be different from the
2392 * number of bytes for "euc-jp". */
2393 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2394 clen += (*mb_ptr2cells)(s + i);
2395 len = clen;
2397 # endif
2399 #endif /* !HAVE_GTK2 */
2401 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2402 gui.col = col + len;
2404 /* May need to invert it when it's part of the selection. */
2405 if (flags & GUI_MON_NOCLEAR)
2406 clip_may_redraw_selection(gui.row, col, len);
2408 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2410 /* Invalidate the old physical cursor position if we wrote over it */
2411 if (gui.cursor_row == gui.row
2412 && gui.cursor_col >= col
2413 && gui.cursor_col < col + len)
2414 gui.cursor_is_valid = FALSE;
2417 #ifdef FEAT_SIGN_ICONS
2418 if (draw_sign)
2419 /* Draw the sign on top of the spaces. */
2420 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2421 # ifdef FEAT_NETBEANS_INTG
2422 if (multi_sign)
2423 netbeans_draw_multisign_indicator(gui.row);
2424 # endif
2425 #endif
2427 return OK;
2431 * Un-draw the cursor. Actually this just redraws the character at the given
2432 * position. The character just before it too, for when it was in bold.
2434 void
2435 gui_undraw_cursor()
2437 if (gui.cursor_is_valid)
2439 #ifdef FEAT_HANGULIN
2440 if (composing_hangul
2441 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2442 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2443 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2444 gui.norm_pixel, gui.back_pixel, 0);
2445 else
2447 #endif
2448 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2449 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2450 && gui.cursor_col > 0)
2451 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2452 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2453 #ifdef FEAT_HANGULIN
2454 if (composing_hangul)
2455 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2456 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2458 #endif
2459 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2460 * here in case it wasn't needed to undraw it. */
2461 gui.cursor_is_valid = FALSE;
2465 void
2466 gui_redraw(x, y, w, h)
2467 int x;
2468 int y;
2469 int w;
2470 int h;
2472 int row1, col1, row2, col2;
2474 row1 = Y_2_ROW(y);
2475 col1 = X_2_COL(x);
2476 row2 = Y_2_ROW(y + h - 1);
2477 col2 = X_2_COL(x + w - 1);
2479 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2482 * We may need to redraw the cursor, but don't take it upon us to change
2483 * its location after a scroll.
2484 * (maybe be more strict even and test col too?)
2485 * These things may be outside the update/clipping region and reality may
2486 * not reflect Vims internal ideas if these operations are clipped away.
2488 if (gui.row == gui.cursor_row)
2489 gui_update_cursor(TRUE, TRUE);
2493 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2494 * from col1 to col2 (inclusive).
2495 * Return TRUE when the character before the first drawn character has
2496 * different attributes (may have to be redrawn too).
2499 gui_redraw_block(row1, col1, row2, col2, flags)
2500 int row1;
2501 int col1;
2502 int row2;
2503 int col2;
2504 int flags; /* flags for gui_outstr_nowrap() */
2506 int old_row, old_col;
2507 long_u old_hl_mask;
2508 int off;
2509 sattr_T first_attr;
2510 int idx, len;
2511 int back, nback;
2512 int retval = FALSE;
2513 #ifdef FEAT_MBYTE
2514 int orig_col1, orig_col2;
2515 #endif
2517 /* Don't try to update when ScreenLines is not valid */
2518 if (!screen_cleared || ScreenLines == NULL)
2519 return retval;
2521 /* Don't try to draw outside the shell! */
2522 /* Check everything, strange values may be caused by a big border width */
2523 col1 = check_col(col1);
2524 col2 = check_col(col2);
2525 row1 = check_row(row1);
2526 row2 = check_row(row2);
2528 /* Remember where our cursor was */
2529 old_row = gui.row;
2530 old_col = gui.col;
2531 old_hl_mask = gui.highlight_mask;
2532 #ifdef FEAT_MBYTE
2533 orig_col1 = col1;
2534 orig_col2 = col2;
2535 #endif
2537 for (gui.row = row1; gui.row <= row2; gui.row++)
2539 #ifdef FEAT_MBYTE
2540 /* When only half of a double-wide character is in the block, include
2541 * the other half. */
2542 col1 = orig_col1;
2543 col2 = orig_col2;
2544 off = LineOffset[gui.row];
2545 if (enc_dbcs != 0)
2547 if (col1 > 0)
2548 col1 -= dbcs_screen_head_off(ScreenLines + off,
2549 ScreenLines + off + col1);
2550 col2 += dbcs_screen_tail_off(ScreenLines + off,
2551 ScreenLines + off + col2);
2553 else if (enc_utf8)
2555 if (ScreenLines[off + col1] == 0)
2556 --col1;
2557 # ifdef HAVE_GTK2
2558 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2559 ++col2;
2560 # endif
2562 #endif
2563 gui.col = col1;
2564 off = LineOffset[gui.row] + gui.col;
2565 len = col2 - col1 + 1;
2567 /* Find how many chars back this highlighting starts, or where a space
2568 * is. Needed for when the bold trick is used */
2569 for (back = 0; back < col1; ++back)
2570 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2571 || ScreenLines[off - 1 - back] == ' ')
2572 break;
2573 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2574 && ScreenLines[off - 1] != ' ');
2576 /* Break it up in strings of characters with the same attributes. */
2577 /* Print UTF-8 characters individually. */
2578 while (len > 0)
2580 first_attr = ScreenAttrs[off];
2581 gui.highlight_mask = first_attr;
2582 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2583 if (enc_utf8 && ScreenLinesUC[off] != 0)
2585 /* output multi-byte character separately */
2586 nback = gui_screenchar(off, flags,
2587 (guicolor_T)0, (guicolor_T)0, back);
2588 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2589 idx = 2;
2590 else
2591 idx = 1;
2593 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2595 /* output double-byte, single-width character separately */
2596 nback = gui_screenchar(off, flags,
2597 (guicolor_T)0, (guicolor_T)0, back);
2598 idx = 1;
2600 else
2601 #endif
2603 #ifdef HAVE_GTK2
2604 for (idx = 0; idx < len; ++idx)
2606 if (enc_utf8 && ScreenLines[off + idx] == 0)
2607 continue; /* skip second half of double-width char */
2608 if (ScreenAttrs[off + idx] != first_attr)
2609 break;
2611 /* gui_screenstr() takes care of multibyte chars */
2612 nback = gui_screenstr(off, idx, flags,
2613 (guicolor_T)0, (guicolor_T)0, back);
2614 #else
2615 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2616 idx++)
2618 # ifdef FEAT_MBYTE
2619 /* Stop at a multi-byte Unicode character. */
2620 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2621 break;
2622 if (enc_dbcs == DBCS_JPNU)
2624 /* Stop at a double-byte single-width char. */
2625 if (ScreenLines[off + idx] == 0x8e)
2626 break;
2627 if (len > 1 && (*mb_ptr2len)(ScreenLines
2628 + off + idx) == 2)
2629 ++idx; /* skip second byte of double-byte char */
2631 # endif
2633 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2634 (guicolor_T)0, (guicolor_T)0, back);
2635 #endif
2637 if (nback == FAIL)
2639 /* Must back up to start drawing where a bold or italic word
2640 * starts. */
2641 off -= back;
2642 len += back;
2643 gui.col -= back;
2645 else
2647 off += idx;
2648 len -= idx;
2650 back = 0;
2654 /* Put the cursor back where it was */
2655 gui.row = old_row;
2656 gui.col = old_col;
2657 gui.highlight_mask = (int)old_hl_mask;
2659 return retval;
2662 static void
2663 gui_delete_lines(row, count)
2664 int row;
2665 int count;
2667 if (count <= 0)
2668 return;
2670 if (row + count > gui.scroll_region_bot)
2671 /* Scrolled out of region, just blank the lines out */
2672 gui_clear_block(row, gui.scroll_region_left,
2673 gui.scroll_region_bot, gui.scroll_region_right);
2674 else
2676 gui_mch_delete_lines(row, count);
2678 /* If the cursor was in the deleted lines it's now gone. If the
2679 * cursor was in the scrolled lines adjust its position. */
2680 if (gui.cursor_row >= row
2681 && gui.cursor_col >= gui.scroll_region_left
2682 && gui.cursor_col <= gui.scroll_region_right)
2684 if (gui.cursor_row < row + count)
2685 gui.cursor_is_valid = FALSE;
2686 else if (gui.cursor_row <= gui.scroll_region_bot)
2687 gui.cursor_row -= count;
2692 static void
2693 gui_insert_lines(row, count)
2694 int row;
2695 int count;
2697 if (count <= 0)
2698 return;
2700 if (row + count > gui.scroll_region_bot)
2701 /* Scrolled out of region, just blank the lines out */
2702 gui_clear_block(row, gui.scroll_region_left,
2703 gui.scroll_region_bot, gui.scroll_region_right);
2704 else
2706 gui_mch_insert_lines(row, count);
2708 if (gui.cursor_row >= gui.row
2709 && gui.cursor_col >= gui.scroll_region_left
2710 && gui.cursor_col <= gui.scroll_region_right)
2712 if (gui.cursor_row <= gui.scroll_region_bot - count)
2713 gui.cursor_row += count;
2714 else if (gui.cursor_row <= gui.scroll_region_bot)
2715 gui.cursor_is_valid = FALSE;
2721 * The main GUI input routine. Waits for a character from the keyboard.
2722 * wtime == -1 Wait forever.
2723 * wtime == 0 Don't wait.
2724 * wtime > 0 Wait wtime milliseconds for a character.
2725 * Returns OK if a character was found to be available within the given time,
2726 * or FAIL otherwise.
2729 gui_wait_for_chars(wtime)
2730 long wtime;
2732 int retval;
2735 * If we're going to wait a bit, update the menus and mouse shape for the
2736 * current State.
2738 if (wtime != 0)
2740 #ifdef FEAT_MENU
2741 gui_update_menus(0);
2742 #endif
2745 gui_mch_update();
2746 if (input_available()) /* Got char, return immediately */
2747 return OK;
2748 if (wtime == 0) /* Don't wait for char */
2749 return FAIL;
2751 /* Before waiting, flush any output to the screen. */
2752 gui_mch_flush();
2754 if (wtime > 0)
2756 /* Blink when waiting for a character. Probably only does something
2757 * for showmatch() */
2758 gui_mch_start_blink();
2759 retval = gui_mch_wait_for_chars(wtime);
2760 gui_mch_stop_blink();
2761 return retval;
2765 * While we are waiting indefinitely for a character, blink the cursor.
2767 gui_mch_start_blink();
2769 retval = FAIL;
2771 * We may want to trigger the CursorHold event. First wait for
2772 * 'updatetime' and if nothing is typed within that time put the
2773 * K_CURSORHOLD key in the input buffer.
2775 if (gui_mch_wait_for_chars(p_ut) == OK)
2776 retval = OK;
2777 #ifdef FEAT_AUTOCMD
2778 else if (trigger_cursorhold())
2780 char_u buf[3];
2782 /* Put K_CURSORHOLD in the input buffer. */
2783 buf[0] = CSI;
2784 buf[1] = KS_EXTRA;
2785 buf[2] = (int)KE_CURSORHOLD;
2786 add_to_input_buf(buf, 3);
2788 retval = OK;
2790 #endif
2792 if (retval == FAIL)
2794 /* Blocking wait. */
2795 before_blocking();
2796 retval = gui_mch_wait_for_chars(-1L);
2799 gui_mch_stop_blink();
2800 return retval;
2804 * Fill p[4] with mouse coordinates encoded for check_termcode().
2806 static void
2807 fill_mouse_coord(p, col, row)
2808 char_u *p;
2809 int col;
2810 int row;
2812 p[0] = (char_u)(col / 128 + ' ' + 1);
2813 p[1] = (char_u)(col % 128 + ' ' + 1);
2814 p[2] = (char_u)(row / 128 + ' ' + 1);
2815 p[3] = (char_u)(row % 128 + ' ' + 1);
2819 * Generic mouse support function. Add a mouse event to the input buffer with
2820 * the given properties.
2821 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2822 * MOUSE_X1, MOUSE_X2
2823 * MOUSE_DRAG, or MOUSE_RELEASE.
2824 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2825 * x, y --- Coordinates of mouse in pixels.
2826 * repeated_click --- TRUE if this click comes only a short time after a
2827 * previous click.
2828 * modifiers --- Bit field which may be any of the following modifiers
2829 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2830 * This function will ignore drag events where the mouse has not moved to a new
2831 * character.
2833 void
2834 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2835 int button;
2836 int x;
2837 int y;
2838 int repeated_click;
2839 int_u modifiers;
2841 static int prev_row = 0, prev_col = 0;
2842 static int prev_button = -1;
2843 static int num_clicks = 1;
2844 char_u string[10];
2845 enum key_extra button_char;
2846 int row, col;
2847 #ifdef FEAT_CLIPBOARD
2848 int checkfor;
2849 int did_clip = FALSE;
2850 #endif
2853 * Scrolling may happen at any time, also while a selection is present.
2855 switch (button)
2857 case MOUSE_X1:
2858 button_char = KE_X1MOUSE;
2859 goto button_set;
2860 case MOUSE_X2:
2861 button_char = KE_X2MOUSE;
2862 goto button_set;
2863 case MOUSE_4:
2864 button_char = KE_MOUSEDOWN;
2865 goto button_set;
2866 case MOUSE_5:
2867 button_char = KE_MOUSEUP;
2868 button_set:
2870 /* Don't put events in the input queue now. */
2871 if (hold_gui_events)
2872 return;
2874 string[3] = CSI;
2875 string[4] = KS_EXTRA;
2876 string[5] = (int)button_char;
2878 /* Pass the pointer coordinates of the scroll event so that we
2879 * know which window to scroll. */
2880 row = gui_xy2colrow(x, y, &col);
2881 string[6] = (char_u)(col / 128 + ' ' + 1);
2882 string[7] = (char_u)(col % 128 + ' ' + 1);
2883 string[8] = (char_u)(row / 128 + ' ' + 1);
2884 string[9] = (char_u)(row % 128 + ' ' + 1);
2886 if (modifiers == 0)
2887 add_to_input_buf(string + 3, 7);
2888 else
2890 string[0] = CSI;
2891 string[1] = KS_MODIFIER;
2892 string[2] = 0;
2893 if (modifiers & MOUSE_SHIFT)
2894 string[2] |= MOD_MASK_SHIFT;
2895 if (modifiers & MOUSE_CTRL)
2896 string[2] |= MOD_MASK_CTRL;
2897 if (modifiers & MOUSE_ALT)
2898 string[2] |= MOD_MASK_ALT;
2899 add_to_input_buf(string, 10);
2901 return;
2905 #ifdef FEAT_CLIPBOARD
2906 /* If a clipboard selection is in progress, handle it */
2907 if (clip_star.state == SELECT_IN_PROGRESS)
2909 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2910 return;
2913 /* Determine which mouse settings to look for based on the current mode */
2914 switch (get_real_state())
2916 case NORMAL_BUSY:
2917 case OP_PENDING:
2918 case NORMAL: checkfor = MOUSE_NORMAL; break;
2919 case VISUAL: checkfor = MOUSE_VISUAL; break;
2920 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2921 case REPLACE:
2922 case REPLACE+LANGMAP:
2923 #ifdef FEAT_VREPLACE
2924 case VREPLACE:
2925 case VREPLACE+LANGMAP:
2926 #endif
2927 case INSERT:
2928 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2929 case ASKMORE:
2930 case HITRETURN: /* At the more- and hit-enter prompt pass the
2931 mouse event for a click on or below the
2932 message line. */
2933 if (Y_2_ROW(y) >= msg_row)
2934 checkfor = MOUSE_NORMAL;
2935 else
2936 checkfor = MOUSE_RETURN;
2937 break;
2940 * On the command line, use the clipboard selection on all lines
2941 * but the command line. But not when pasting.
2943 case CMDLINE:
2944 case CMDLINE+LANGMAP:
2945 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2946 checkfor = MOUSE_NONE;
2947 else
2948 checkfor = MOUSE_COMMAND;
2949 break;
2951 default:
2952 checkfor = MOUSE_NONE;
2953 break;
2957 * Allow clipboard selection of text on the command line in "normal"
2958 * modes. Don't do this when dragging the status line, or extending a
2959 * Visual selection.
2961 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2962 && Y_2_ROW(y) >= topframe->fr_height
2963 # ifdef FEAT_WINDOWS
2964 + firstwin->w_winrow
2965 # endif
2966 && button != MOUSE_DRAG
2967 # ifdef FEAT_MOUSESHAPE
2968 && !drag_status_line
2969 # ifdef FEAT_VERTSPLIT
2970 && !drag_sep_line
2971 # endif
2972 # endif
2974 checkfor = MOUSE_NONE;
2977 * Use modeless selection when holding CTRL and SHIFT pressed.
2979 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2980 checkfor = MOUSE_NONEF;
2983 * In Ex mode, always use modeless selection.
2985 if (exmode_active)
2986 checkfor = MOUSE_NONE;
2989 * If the mouse settings say to not use the mouse, use the modeless
2990 * selection. But if Visual is active, assume that only the Visual area
2991 * will be selected.
2992 * Exception: On the command line, both the selection is used and a mouse
2993 * key is send.
2995 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2997 #ifdef FEAT_VISUAL
2998 /* Don't do modeless selection in Visual mode. */
2999 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3000 return;
3001 #endif
3004 * When 'mousemodel' is "popup", shift-left is translated to right.
3005 * But not when also using Ctrl.
3007 if (mouse_model_popup() && button == MOUSE_LEFT
3008 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3010 button = MOUSE_RIGHT;
3011 modifiers &= ~ MOUSE_SHIFT;
3014 /* If the selection is done, allow the right button to extend it.
3015 * If the selection is cleared, allow the right button to start it
3016 * from the cursor position. */
3017 if (button == MOUSE_RIGHT)
3019 if (clip_star.state == SELECT_CLEARED)
3021 if (State & CMDLINE)
3023 col = msg_col;
3024 row = msg_row;
3026 else
3028 col = curwin->w_wcol;
3029 row = curwin->w_wrow + W_WINROW(curwin);
3031 clip_start_selection(col, row, FALSE);
3033 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3034 repeated_click);
3035 did_clip = TRUE;
3037 /* Allow the left button to start the selection */
3038 else if (button ==
3039 # ifdef RISCOS
3040 /* Only start a drag on a drag event. Otherwise
3041 * we don't get a release event. */
3042 MOUSE_DRAG
3043 # else
3044 MOUSE_LEFT
3045 # endif
3048 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3049 did_clip = TRUE;
3051 # ifdef RISCOS
3052 else if (button == MOUSE_LEFT)
3054 clip_clear_selection();
3055 did_clip = TRUE;
3057 # endif
3059 /* Always allow pasting */
3060 if (button != MOUSE_MIDDLE)
3062 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3063 return;
3064 if (checkfor != MOUSE_COMMAND)
3065 button = MOUSE_LEFT;
3067 repeated_click = FALSE;
3070 if (clip_star.state != SELECT_CLEARED && !did_clip)
3071 clip_clear_selection();
3072 #endif
3074 /* Don't put events in the input queue now. */
3075 if (hold_gui_events)
3076 return;
3078 row = gui_xy2colrow(x, y, &col);
3081 * If we are dragging and the mouse hasn't moved far enough to be on a
3082 * different character, then don't send an event to vim.
3084 if (button == MOUSE_DRAG)
3086 if (row == prev_row && col == prev_col)
3087 return;
3088 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3089 if (y < 0)
3090 row = -1;
3094 * If topline has changed (window scrolled) since the last click, reset
3095 * repeated_click, because we don't want starting Visual mode when
3096 * clicking on a different character in the text.
3098 if (curwin->w_topline != gui_prev_topline
3099 #ifdef FEAT_DIFF
3100 || curwin->w_topfill != gui_prev_topfill
3101 #endif
3103 repeated_click = FALSE;
3105 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3106 string[1] = KS_MOUSE;
3107 string[2] = KE_FILLER;
3108 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3110 if (repeated_click)
3113 * Handle multiple clicks. They only count if the mouse is still
3114 * pointing at the same character.
3116 if (button != prev_button || row != prev_row || col != prev_col)
3117 num_clicks = 1;
3118 else if (++num_clicks > 4)
3119 num_clicks = 1;
3121 else
3122 num_clicks = 1;
3123 prev_button = button;
3124 gui_prev_topline = curwin->w_topline;
3125 #ifdef FEAT_DIFF
3126 gui_prev_topfill = curwin->w_topfill;
3127 #endif
3129 string[3] = (char_u)(button | 0x20);
3130 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3132 else
3133 string[3] = (char_u)button;
3135 string[3] |= modifiers;
3136 fill_mouse_coord(string + 4, col, row);
3137 add_to_input_buf(string, 8);
3139 if (row < 0)
3140 prev_row = 0;
3141 else
3142 prev_row = row;
3143 prev_col = col;
3146 * We need to make sure this is cleared since Athena doesn't tell us when
3147 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3149 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3150 gui.dragged_sb = SBAR_NONE;
3151 #endif
3155 * Convert x and y coordinate to column and row in text window.
3156 * Corrects for multi-byte character.
3157 * returns column in "*colp" and row as return value;
3160 gui_xy2colrow(x, y, colp)
3161 int x;
3162 int y;
3163 int *colp;
3165 int col = check_col(X_2_COL(x));
3166 int row = check_row(Y_2_ROW(y));
3168 #ifdef FEAT_MBYTE
3169 *colp = mb_fix_col(col, row);
3170 #else
3171 *colp = col;
3172 #endif
3173 return row;
3176 #if defined(FEAT_MENU) || defined(PROTO)
3178 * Callback function for when a menu entry has been selected.
3180 void
3181 gui_menu_cb(menu)
3182 vimmenu_T *menu;
3184 char_u bytes[sizeof(long_u)];
3186 /* Don't put events in the input queue now. */
3187 if (hold_gui_events)
3188 return;
3190 bytes[0] = CSI;
3191 bytes[1] = KS_MENU;
3192 bytes[2] = KE_FILLER;
3193 add_to_input_buf(bytes, 3);
3194 add_long_to_buf((long_u)menu, bytes);
3195 add_to_input_buf_csi(bytes, sizeof(long_u));
3197 #endif
3199 static int prev_which_scrollbars[3];
3202 * Set which components are present.
3203 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3204 * in p_go.
3206 void
3207 gui_init_which_components(oldval)
3208 char_u *oldval UNUSED;
3210 #ifdef FEAT_MENU
3211 static int prev_menu_is_active = -1;
3212 #endif
3213 #ifdef FEAT_TOOLBAR
3214 static int prev_toolbar = -1;
3215 int using_toolbar = FALSE;
3216 #endif
3217 #ifdef FEAT_GUI_TABLINE
3218 int using_tabline;
3219 #endif
3220 #ifdef FEAT_FOOTER
3221 static int prev_footer = -1;
3222 int using_footer = FALSE;
3223 #endif
3224 #if defined(FEAT_MENU) && !defined(WIN16)
3225 static int prev_tearoff = -1;
3226 int using_tearoff = FALSE;
3227 #endif
3228 static int prev_nocaption = -1;
3229 int using_caption = TRUE;
3231 char_u *p;
3232 int i;
3233 #ifdef FEAT_MENU
3234 int grey_old, grey_new;
3235 char_u *temp;
3236 #endif
3237 win_T *wp;
3238 int need_set_size;
3239 int fix_size;
3241 #ifdef FEAT_MENU
3242 if (oldval != NULL && gui.in_use)
3245 * Check if the menu's go from grey to non-grey or vise versa.
3247 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3248 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3249 if (grey_old != grey_new)
3251 temp = p_go;
3252 p_go = oldval;
3253 gui_update_menus(MENU_ALL_MODES);
3254 p_go = temp;
3257 gui.menu_is_active = FALSE;
3258 #endif
3260 for (i = 0; i < 3; i++)
3261 gui.which_scrollbars[i] = FALSE;
3262 for (p = p_go; *p; p++)
3263 switch (*p)
3265 case GO_LEFT:
3266 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3267 break;
3268 case GO_RIGHT:
3269 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3270 break;
3271 #ifdef FEAT_VERTSPLIT
3272 case GO_VLEFT:
3273 if (win_hasvertsplit())
3274 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3275 break;
3276 case GO_VRIGHT:
3277 if (win_hasvertsplit())
3278 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3279 break;
3280 #endif
3281 case GO_BOT:
3282 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3283 break;
3284 #ifdef FEAT_MENU
3285 case GO_MENUS:
3286 gui.menu_is_active = TRUE;
3287 break;
3288 #endif
3289 case GO_GREY:
3290 /* make menu's have grey items, ignored here */
3291 break;
3292 #ifdef FEAT_TOOLBAR
3293 case GO_TOOLBAR:
3294 using_toolbar = TRUE;
3295 break;
3296 #endif
3297 #ifdef FEAT_FOOTER
3298 case GO_FOOTER:
3299 using_footer = TRUE;
3300 break;
3301 #endif
3302 case GO_TEAROFF:
3303 #if defined(FEAT_MENU) && !defined(WIN16)
3304 using_tearoff = TRUE;
3305 #endif
3306 break;
3307 case GO_NOCAPTION:
3308 using_caption = FALSE;
3309 break;
3310 default:
3311 /* Ignore options that are not supported */
3312 break;
3315 if (gui.in_use)
3317 need_set_size = 0;
3318 fix_size = FALSE;
3320 #ifdef FEAT_GUI_TABLINE
3321 /* Update the GUI tab line, it may appear or disappear. This may
3322 * cause the non-GUI tab line to disappear or appear. */
3323 using_tabline = gui_has_tabline();
3324 if (!gui_mch_showing_tabline() != !using_tabline)
3326 /* We don't want a resize event change "Rows" here, save and
3327 * restore it. Resizing is handled below. */
3328 i = Rows;
3329 gui_update_tabline();
3330 Rows = i;
3331 need_set_size |= RESIZE_VERT;
3332 if (using_tabline)
3333 fix_size = TRUE;
3334 if (!gui_use_tabline())
3335 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3337 #endif
3339 for (i = 0; i < 3; i++)
3341 /* The scrollbar needs to be updated when it is shown/unshown and
3342 * when switching tab pages. But the size only changes when it's
3343 * shown/unshown. Thus we need two places to remember whether a
3344 * scrollbar is there or not. */
3345 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3346 #ifdef FEAT_WINDOWS
3347 || gui.which_scrollbars[i]
3348 != curtab->tp_prev_which_scrollbars[i]
3349 #endif
3352 if (i == SBAR_BOTTOM)
3353 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3354 gui.which_scrollbars[i]);
3355 else
3357 FOR_ALL_WINDOWS(wp)
3359 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3362 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3364 if (i == SBAR_BOTTOM)
3365 need_set_size |= RESIZE_VERT;
3366 else
3367 need_set_size |= RESIZE_HOR;
3368 if (gui.which_scrollbars[i])
3369 fix_size = TRUE;
3372 #ifdef FEAT_WINDOWS
3373 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3374 #endif
3375 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3378 #ifdef FEAT_MENU
3379 if (gui.menu_is_active != prev_menu_is_active)
3381 /* We don't want a resize event change "Rows" here, save and
3382 * restore it. Resizing is handled below. */
3383 i = Rows;
3384 gui_mch_enable_menu(gui.menu_is_active);
3385 Rows = i;
3386 prev_menu_is_active = gui.menu_is_active;
3387 need_set_size |= RESIZE_VERT;
3388 if (gui.menu_is_active)
3389 fix_size = TRUE;
3391 #endif
3393 #ifdef FEAT_TOOLBAR
3394 if (using_toolbar != prev_toolbar)
3396 gui_mch_show_toolbar(using_toolbar);
3397 prev_toolbar = using_toolbar;
3398 need_set_size |= RESIZE_VERT;
3399 if (using_toolbar)
3400 fix_size = TRUE;
3402 #endif
3403 #ifdef FEAT_FOOTER
3404 if (using_footer != prev_footer)
3406 gui_mch_enable_footer(using_footer);
3407 prev_footer = using_footer;
3408 need_set_size |= RESIZE_VERT;
3409 if (using_footer)
3410 fix_size = TRUE;
3412 #endif
3413 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3414 if (using_tearoff != prev_tearoff)
3416 gui_mch_toggle_tearoffs(using_tearoff);
3417 prev_tearoff = using_tearoff;
3419 #endif
3420 if (using_caption != prev_nocaption)
3422 #if defined(WIN3264)
3423 gui_mch_show_caption(using_caption);
3424 #endif
3425 prev_nocaption = using_caption;
3426 need_set_size = TRUE;
3428 if (need_set_size != 0)
3430 #ifdef FEAT_GUI_GTK
3431 long prev_Columns = Columns;
3432 long prev_Rows = Rows;
3433 #endif
3434 /* Adjust the size of the window to make the text area keep the
3435 * same size and to avoid that part of our window is off-screen
3436 * and a scrollbar can't be used, for example. */
3437 gui_set_shellsize(FALSE, fix_size, need_set_size);
3439 #ifdef FEAT_GUI_GTK
3440 /* GTK has the annoying habit of sending us resize events when
3441 * changing the window size ourselves. This mostly happens when
3442 * waiting for a character to arrive, quite unpredictably, and may
3443 * change Columns and Rows when we don't want it. Wait for a
3444 * character here to avoid this effect.
3445 * If you remove this, please test this command for resizing
3446 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3447 * Don't do this while starting up though.
3448 * Don't change Rows when adding menu/toolbar/tabline.
3449 * Don't change Columns when adding vertical toolbar. */
3450 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3451 (void)char_avail();
3452 if ((need_set_size & RESIZE_VERT) == 0)
3453 Rows = prev_Rows;
3454 if ((need_set_size & RESIZE_HOR) == 0)
3455 Columns = prev_Columns;
3456 #endif
3458 #ifdef FEAT_WINDOWS
3459 /* When the console tabline appears or disappears the window positions
3460 * change. */
3461 if (firstwin->w_winrow != tabline_height())
3462 shell_new_rows(); /* recompute window positions and heights */
3463 #endif
3467 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3469 * Return TRUE if the GUI is taking care of the tabline.
3470 * It may still be hidden if 'showtabline' is zero.
3473 gui_use_tabline()
3475 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3479 * Return TRUE if the GUI is showing the tabline.
3480 * This uses 'showtabline'.
3482 static int
3483 gui_has_tabline()
3485 if (!gui_use_tabline()
3486 || p_stal == 0
3487 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3488 return FALSE;
3489 return TRUE;
3493 * Update the tabline.
3494 * This may display/undisplay the tabline and update the labels.
3496 void
3497 gui_update_tabline()
3499 int showit = gui_has_tabline();
3500 int shown = gui_mch_showing_tabline();
3502 if (!gui.starting && starting == 0)
3504 /* Updating the tabline uses direct GUI commands, flush
3505 * outstanding instructions first. (esp. clear screen) */
3506 out_flush();
3507 gui_mch_flush();
3509 if (!showit != !shown)
3510 gui_mch_show_tabline(showit);
3511 if (showit != 0)
3512 gui_mch_update_tabline();
3514 /* When the tabs change from hidden to shown or from shown to
3515 * hidden the size of the text area should remain the same. */
3516 if (!showit != !shown)
3517 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3522 * Get the label or tooltip for tab page "tp" into NameBuff[].
3524 void
3525 get_tabline_label(tp, tooltip)
3526 tabpage_T *tp;
3527 int tooltip; /* TRUE: get tooltip */
3529 int modified = FALSE;
3530 char_u buf[40];
3531 int wincount;
3532 win_T *wp;
3533 char_u **opt;
3535 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3536 opt = (tooltip ? &p_gtt : &p_gtl);
3537 if (**opt != NUL)
3539 int use_sandbox = FALSE;
3540 int save_called_emsg = called_emsg;
3541 char_u res[MAXPATHL];
3542 tabpage_T *save_curtab;
3543 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3544 : "guitablabel");
3546 called_emsg = FALSE;
3548 printer_page_num = tabpage_index(tp);
3549 # ifdef FEAT_EVAL
3550 set_vim_var_nr(VV_LNUM, printer_page_num);
3551 use_sandbox = was_set_insecurely(opt_name, 0);
3552 # endif
3553 /* It's almost as going to the tabpage, but without autocommands. */
3554 curtab->tp_firstwin = firstwin;
3555 curtab->tp_lastwin = lastwin;
3556 curtab->tp_curwin = curwin;
3557 save_curtab = curtab;
3558 curtab = tp;
3559 topframe = curtab->tp_topframe;
3560 firstwin = curtab->tp_firstwin;
3561 lastwin = curtab->tp_lastwin;
3562 curwin = curtab->tp_curwin;
3563 curbuf = curwin->w_buffer;
3565 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3566 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3567 0, (int)Columns, NULL, NULL);
3568 STRCPY(NameBuff, res);
3570 /* Back to the original curtab. */
3571 curtab = save_curtab;
3572 topframe = curtab->tp_topframe;
3573 firstwin = curtab->tp_firstwin;
3574 lastwin = curtab->tp_lastwin;
3575 curwin = curtab->tp_curwin;
3576 curbuf = curwin->w_buffer;
3578 if (called_emsg)
3579 set_string_option_direct(opt_name, -1,
3580 (char_u *)"", OPT_FREE, SID_ERROR);
3581 called_emsg |= save_called_emsg;
3584 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3585 * use a default label. */
3586 if (**opt == NUL || *NameBuff == NUL)
3588 /* Get the buffer name into NameBuff[] and shorten it. */
3589 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3590 if (!tooltip)
3591 shorten_dir(NameBuff);
3593 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3594 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3595 if (bufIsChanged(wp->w_buffer))
3596 modified = TRUE;
3597 if (modified || wincount > 1)
3599 if (wincount > 1)
3600 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3601 else
3602 buf[0] = NUL;
3603 if (modified)
3604 STRCAT(buf, "+");
3605 STRCAT(buf, " ");
3606 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
3607 mch_memmove(NameBuff, buf, STRLEN(buf));
3613 * Send the event for clicking to select tab page "nr".
3614 * Returns TRUE if it was done, FALSE when skipped because we are already at
3615 * that tab page or the cmdline window is open.
3618 send_tabline_event(nr)
3619 int nr;
3621 char_u string[3];
3623 if (nr == tabpage_index(curtab))
3624 return FALSE;
3626 /* Don't put events in the input queue now. */
3627 if (hold_gui_events
3628 # ifdef FEAT_CMDWIN
3629 || cmdwin_type != 0
3630 # endif
3633 /* Set it back to the current tab page. */
3634 gui_mch_set_curtab(tabpage_index(curtab));
3635 return FALSE;
3638 string[0] = CSI;
3639 string[1] = KS_TABLINE;
3640 string[2] = KE_FILLER;
3641 add_to_input_buf(string, 3);
3642 string[0] = nr;
3643 add_to_input_buf_csi(string, 1);
3644 return TRUE;
3648 * Send a tabline menu event
3650 void
3651 send_tabline_menu_event(tabidx, event)
3652 int tabidx;
3653 int event;
3655 char_u string[3];
3657 /* Don't put events in the input queue now. */
3658 if (hold_gui_events)
3659 return;
3661 string[0] = CSI;
3662 string[1] = KS_TABMENU;
3663 string[2] = KE_FILLER;
3664 add_to_input_buf(string, 3);
3665 string[0] = tabidx;
3666 string[1] = (char_u)(long)event;
3667 add_to_input_buf_csi(string, 2);
3670 #endif
3673 * Scrollbar stuff:
3676 #if defined(FEAT_WINDOWS) || defined(PROTO)
3678 * Remove all scrollbars. Used before switching to another tab page.
3680 void
3681 gui_remove_scrollbars()
3683 int i;
3684 win_T *wp;
3686 for (i = 0; i < 3; i++)
3688 if (i == SBAR_BOTTOM)
3689 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3690 else
3692 FOR_ALL_WINDOWS(wp)
3694 gui_do_scrollbar(wp, i, FALSE);
3697 curtab->tp_prev_which_scrollbars[i] = -1;
3700 #endif
3702 void
3703 gui_create_scrollbar(sb, type, wp)
3704 scrollbar_T *sb;
3705 int type;
3706 win_T *wp;
3708 #ifdef FEAT_GUI_MACVIM
3709 /* This is passed over to another process, make sure it fits in 32 bit */
3710 static int32_t sbar_ident = 0;
3711 #else
3712 static int sbar_ident = 0;
3713 #endif
3715 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3716 sb->wp = wp;
3717 sb->type = type;
3718 sb->value = 0;
3719 #ifdef FEAT_GUI_ATHENA
3720 sb->pixval = 0;
3721 #endif
3722 sb->size = 1;
3723 sb->max = 1;
3724 sb->top = 0;
3725 sb->height = 0;
3726 #ifdef FEAT_VERTSPLIT
3727 sb->width = 0;
3728 #endif
3729 sb->status_height = 0;
3730 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3734 * Find the scrollbar with the given index.
3736 scrollbar_T *
3737 gui_find_scrollbar(ident)
3738 long ident;
3740 win_T *wp;
3742 if (gui.bottom_sbar.ident == ident)
3743 return &gui.bottom_sbar;
3744 FOR_ALL_WINDOWS(wp)
3746 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3747 return &wp->w_scrollbars[SBAR_LEFT];
3748 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3749 return &wp->w_scrollbars[SBAR_RIGHT];
3751 return NULL;
3755 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3757 * For Win32, Macintosh and GTK+ 2:
3758 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3759 * you stop dragging the scrollbar. We get here each time the scrollbar is
3760 * dragged another pixel, but as far as the rest of vim goes, it thinks
3761 * we're just hanging in the call to DispatchMessage() in
3762 * process_message(). The DispatchMessage() call that hangs was passed a
3763 * mouse button click event in the scrollbar window. -- webb.
3765 * Solution: Do the scrolling right here. But only when allowed.
3766 * Ignore the scrollbars while executing an external command or when there
3767 * are still characters to be processed.
3769 void
3770 gui_drag_scrollbar(sb, value, still_dragging)
3771 scrollbar_T *sb;
3772 long value;
3773 int still_dragging;
3775 #ifdef FEAT_WINDOWS
3776 win_T *wp;
3777 #endif
3778 int sb_num;
3779 #ifdef USE_ON_FLY_SCROLL
3780 colnr_T old_leftcol = curwin->w_leftcol;
3781 # ifdef FEAT_SCROLLBIND
3782 linenr_T old_topline = curwin->w_topline;
3783 # endif
3784 # ifdef FEAT_DIFF
3785 int old_topfill = curwin->w_topfill;
3786 # endif
3787 #else
3788 char_u bytes[sizeof(long_u)];
3789 int byte_count;
3790 #endif
3792 if (sb == NULL)
3793 return;
3795 /* Don't put events in the input queue now. */
3796 if (hold_gui_events)
3797 return;
3799 #ifdef FEAT_CMDWIN
3800 if (cmdwin_type != 0 && sb->wp != curwin)
3801 return;
3802 #endif
3804 if (still_dragging)
3806 if (sb->wp == NULL)
3807 gui.dragged_sb = SBAR_BOTTOM;
3808 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3809 gui.dragged_sb = SBAR_LEFT;
3810 else
3811 gui.dragged_sb = SBAR_RIGHT;
3812 gui.dragged_wp = sb->wp;
3814 else
3816 gui.dragged_sb = SBAR_NONE;
3817 #ifdef HAVE_GTK2
3818 /* Keep the "dragged_wp" value until after the scrolling, for when the
3819 * moust button is released. GTK2 doesn't send the button-up event. */
3820 gui.dragged_wp = NULL;
3821 #endif
3824 /* Vertical sbar info is kept in the first sbar (the left one) */
3825 if (sb->wp != NULL)
3826 sb = &sb->wp->w_scrollbars[0];
3829 * Check validity of value
3831 if (value < 0)
3832 value = 0;
3833 #ifdef SCROLL_PAST_END
3834 else if (value > sb->max)
3835 value = sb->max;
3836 #else
3837 if (value > sb->max - sb->size + 1)
3838 value = sb->max - sb->size + 1;
3839 #endif
3841 sb->value = value;
3843 #ifdef USE_ON_FLY_SCROLL
3844 /* When not allowed to do the scrolling right now, return.
3845 * This also checked input_available(), but that causes the first click in
3846 * a scrollbar to be ignored when Vim doesn't have focus. */
3847 if (dont_scroll)
3848 return;
3849 #endif
3850 #ifdef FEAT_INS_EXPAND
3851 /* Disallow scrolling the current window when the completion popup menu is
3852 * visible. */
3853 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3854 return;
3855 #endif
3857 #ifdef FEAT_RIGHTLEFT
3858 if (sb->wp == NULL && curwin->w_p_rl)
3860 value = sb->max + 1 - sb->size - value;
3861 if (value < 0)
3862 value = 0;
3864 #endif
3866 if (sb->wp != NULL) /* vertical scrollbar */
3868 sb_num = 0;
3869 #ifdef FEAT_WINDOWS
3870 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3871 sb_num++;
3872 if (wp == NULL)
3873 return;
3874 #else
3875 if (sb->wp != curwin)
3876 return;
3877 #endif
3879 #ifdef USE_ON_FLY_SCROLL
3880 current_scrollbar = sb_num;
3881 scrollbar_value = value;
3882 if (State & NORMAL)
3884 gui_do_scroll();
3885 setcursor();
3887 else if (State & INSERT)
3889 ins_scroll();
3890 setcursor();
3892 else if (State & CMDLINE)
3894 if (msg_scrolled == 0)
3896 gui_do_scroll();
3897 redrawcmdline();
3900 # ifdef FEAT_FOLDING
3901 /* Value may have been changed for closed fold. */
3902 sb->value = sb->wp->w_topline - 1;
3903 # endif
3905 /* When dragging one scrollbar and there is another one at the other
3906 * side move the thumb of that one too. */
3907 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3908 gui_mch_set_scrollbar_thumb(
3909 &sb->wp->w_scrollbars[
3910 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3911 ? SBAR_LEFT : SBAR_RIGHT],
3912 sb->value, sb->size, sb->max);
3914 #else
3915 bytes[0] = CSI;
3916 bytes[1] = KS_VER_SCROLLBAR;
3917 bytes[2] = KE_FILLER;
3918 bytes[3] = (char_u)sb_num;
3919 byte_count = 4;
3920 #endif
3922 else
3924 #ifdef USE_ON_FLY_SCROLL
3925 scrollbar_value = value;
3927 if (State & NORMAL)
3928 gui_do_horiz_scroll();
3929 else if (State & INSERT)
3930 ins_horscroll();
3931 else if (State & CMDLINE)
3933 if (msg_scrolled == 0)
3935 gui_do_horiz_scroll();
3936 redrawcmdline();
3939 if (old_leftcol != curwin->w_leftcol)
3941 updateWindow(curwin); /* update window, status and cmdline */
3942 setcursor();
3944 #else
3945 bytes[0] = CSI;
3946 bytes[1] = KS_HOR_SCROLLBAR;
3947 bytes[2] = KE_FILLER;
3948 byte_count = 3;
3949 #endif
3952 #ifdef USE_ON_FLY_SCROLL
3953 # ifdef FEAT_SCROLLBIND
3955 * synchronize other windows, as necessary according to 'scrollbind'
3957 if (curwin->w_p_scb
3958 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3959 || (sb->wp == curwin && (curwin->w_topline != old_topline
3960 # ifdef FEAT_DIFF
3961 || curwin->w_topfill != old_topfill
3962 # endif
3963 ))))
3965 do_check_scrollbind(TRUE);
3966 /* need to update the window right here */
3967 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3968 if (wp->w_redr_type > 0)
3969 updateWindow(wp);
3970 setcursor();
3972 # endif
3973 out_flush();
3974 gui_update_cursor(FALSE, TRUE);
3975 #else
3976 add_to_input_buf(bytes, byte_count);
3977 add_long_to_buf((long_u)value, bytes);
3978 add_to_input_buf_csi(bytes, sizeof(long_u));
3979 #endif
3983 * Scrollbar stuff:
3987 * Called when something in the window layout has changed.
3989 void
3990 gui_may_update_scrollbars()
3992 if (gui.in_use && starting == 0)
3994 out_flush();
3995 gui_init_which_components(NULL);
3996 gui_update_scrollbars(TRUE);
3998 need_mouse_correct = TRUE;
4001 void
4002 gui_update_scrollbars(force)
4003 int force; /* Force all scrollbars to get updated */
4005 win_T *wp;
4006 scrollbar_T *sb;
4007 long val, size, max; /* need 32 bits here */
4008 int which_sb;
4009 int h, y;
4010 #ifdef FEAT_VERTSPLIT
4011 static win_T *prev_curwin = NULL;
4012 #endif
4014 /* Update the horizontal scrollbar */
4015 gui_update_horiz_scrollbar(force);
4017 #ifndef WIN3264
4018 /* Return straight away if there is neither a left nor right scrollbar.
4019 * On MS-Windows this is required anyway for scrollwheel messages. */
4020 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4021 return;
4022 #endif
4025 * Don't want to update a scrollbar while we're dragging it. But if we
4026 * have both a left and right scrollbar, and we drag one of them, we still
4027 * need to update the other one.
4029 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4030 && gui.which_scrollbars[SBAR_LEFT]
4031 && gui.which_scrollbars[SBAR_RIGHT])
4034 * If we have two scrollbars and one of them is being dragged, just
4035 * copy the scrollbar position from the dragged one to the other one.
4037 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4038 if (gui.dragged_wp != NULL)
4039 gui_mch_set_scrollbar_thumb(
4040 &gui.dragged_wp->w_scrollbars[which_sb],
4041 gui.dragged_wp->w_scrollbars[0].value,
4042 gui.dragged_wp->w_scrollbars[0].size,
4043 gui.dragged_wp->w_scrollbars[0].max);
4046 /* avoid that moving components around generates events */
4047 ++hold_gui_events;
4049 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4051 if (wp->w_buffer == NULL) /* just in case */
4052 continue;
4053 /* Skip a scrollbar that is being dragged. */
4054 if (!force && (gui.dragged_sb == SBAR_LEFT
4055 || gui.dragged_sb == SBAR_RIGHT)
4056 && gui.dragged_wp == wp)
4057 continue;
4059 #ifdef SCROLL_PAST_END
4060 max = wp->w_buffer->b_ml.ml_line_count - 1;
4061 #else
4062 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4063 #endif
4064 if (max < 0) /* empty buffer */
4065 max = 0;
4066 val = wp->w_topline - 1;
4067 size = wp->w_height;
4068 #ifdef SCROLL_PAST_END
4069 if (val > max) /* just in case */
4070 val = max;
4071 #else
4072 if (size > max + 1) /* just in case */
4073 size = max + 1;
4074 if (val > max - size + 1)
4075 val = max - size + 1;
4076 #endif
4077 if (val < 0) /* minimal value is 0 */
4078 val = 0;
4081 * Scrollbar at index 0 (the left one) contains all the information.
4082 * It would be the same info for left and right so we just store it for
4083 * one of them.
4085 sb = &wp->w_scrollbars[0];
4088 * Note: no check for valid w_botline. If it's not valid the
4089 * scrollbars will be updated later anyway.
4091 if (size < 1 || wp->w_botline - 2 > max)
4094 * This can happen during changing files. Just don't update the
4095 * scrollbar for now.
4097 sb->height = 0; /* Force update next time */
4098 if (gui.which_scrollbars[SBAR_LEFT])
4099 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4100 if (gui.which_scrollbars[SBAR_RIGHT])
4101 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4102 continue;
4104 if (force || sb->height != wp->w_height
4105 #ifdef FEAT_WINDOWS
4106 || sb->top != wp->w_winrow
4107 || sb->status_height != wp->w_status_height
4108 # ifdef FEAT_VERTSPLIT
4109 || sb->width != wp->w_width
4110 || prev_curwin != curwin
4111 # endif
4112 #endif
4115 /* Height, width or position of scrollbar has changed. For
4116 * vertical split: curwin changed. */
4117 sb->height = wp->w_height;
4118 #ifdef FEAT_WINDOWS
4119 sb->top = wp->w_winrow;
4120 sb->status_height = wp->w_status_height;
4121 # ifdef FEAT_VERTSPLIT
4122 sb->width = wp->w_width;
4123 # endif
4124 #endif
4126 /* Calculate height and position in pixels */
4127 h = (sb->height + sb->status_height) * gui.char_height;
4128 y = sb->top * gui.char_height + gui.border_offset;
4129 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4130 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MACVIM))
4131 if (gui.menu_is_active)
4132 y += gui.menu_height;
4133 #endif
4135 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4136 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4137 # ifdef FEAT_GUI_ATHENA
4138 y += gui.toolbar_height;
4139 # else
4140 # ifdef FEAT_GUI_MSWIN
4141 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4142 # endif
4143 # endif
4144 #endif
4146 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4147 if (gui_has_tabline())
4148 y += gui.tabline_height;
4149 #endif
4151 #ifdef FEAT_WINDOWS
4152 if (wp->w_winrow == 0)
4153 #endif
4155 /* Height of top scrollbar includes width of top border */
4156 h += gui.border_offset;
4157 y -= gui.border_offset;
4159 if (gui.which_scrollbars[SBAR_LEFT])
4161 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4162 gui.left_sbar_x, y,
4163 gui.scrollbar_width, h);
4164 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4166 if (gui.which_scrollbars[SBAR_RIGHT])
4168 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4169 gui.right_sbar_x, y,
4170 gui.scrollbar_width, h);
4171 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4175 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4176 * checking if the thumb moved at least a pixel. Only do this for
4177 * Athena, most other GUIs require the update anyway to make the
4178 * arrows work. */
4179 #ifdef FEAT_GUI_ATHENA
4180 if (max == 0)
4181 y = 0;
4182 else
4183 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4184 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4185 #else
4186 if (force || sb->value != val || sb->size != size || sb->max != max)
4187 #endif
4189 /* Thumb of scrollbar has moved */
4190 sb->value = val;
4191 #ifdef FEAT_GUI_ATHENA
4192 sb->pixval = y;
4193 #endif
4194 sb->size = size;
4195 sb->max = max;
4196 if (gui.which_scrollbars[SBAR_LEFT]
4197 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4198 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4199 val, size, max);
4200 if (gui.which_scrollbars[SBAR_RIGHT]
4201 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4202 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4203 val, size, max);
4206 #ifdef FEAT_VERTSPLIT
4207 prev_curwin = curwin;
4208 #endif
4209 --hold_gui_events;
4213 * Enable or disable a scrollbar.
4214 * Check for scrollbars for vertically split windows which are not enabled
4215 * sometimes.
4217 static void
4218 gui_do_scrollbar(wp, which, enable)
4219 win_T *wp;
4220 int which; /* SBAR_LEFT or SBAR_RIGHT */
4221 int enable; /* TRUE to enable scrollbar */
4223 #ifdef FEAT_VERTSPLIT
4224 int midcol = curwin->w_wincol + curwin->w_width / 2;
4225 int has_midcol = (wp->w_wincol <= midcol
4226 && wp->w_wincol + wp->w_width >= midcol);
4228 /* Only enable scrollbars that contain the middle column of the current
4229 * window. */
4230 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4232 /* Scrollbars only on one side. Don't enable scrollbars that don't
4233 * contain the middle column of the current window. */
4234 if (!has_midcol)
4235 enable = FALSE;
4237 else
4239 /* Scrollbars on both sides. Don't enable scrollbars that neither
4240 * contain the middle column of the current window nor are on the far
4241 * side. */
4242 if (midcol > Columns / 2)
4244 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4245 enable = FALSE;
4247 else
4249 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4250 : !has_midcol)
4251 enable = FALSE;
4254 #endif
4255 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4259 * Scroll a window according to the values set in the globals current_scrollbar
4260 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4261 * or FALSE otherwise.
4264 gui_do_scroll()
4266 win_T *wp, *save_wp;
4267 int i;
4268 long nlines;
4269 pos_T old_cursor;
4270 linenr_T old_topline;
4271 #ifdef FEAT_DIFF
4272 int old_topfill;
4273 #endif
4275 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4276 if (wp == NULL)
4277 break;
4278 if (wp == NULL)
4279 /* Couldn't find window */
4280 return FALSE;
4283 * Compute number of lines to scroll. If zero, nothing to do.
4285 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4286 if (nlines == 0)
4287 return FALSE;
4289 save_wp = curwin;
4290 old_topline = wp->w_topline;
4291 #ifdef FEAT_DIFF
4292 old_topfill = wp->w_topfill;
4293 #endif
4294 old_cursor = wp->w_cursor;
4295 curwin = wp;
4296 curbuf = wp->w_buffer;
4297 if (nlines < 0)
4298 scrolldown(-nlines, gui.dragged_wp == NULL);
4299 else
4300 scrollup(nlines, gui.dragged_wp == NULL);
4301 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4302 * the mouse-up event already, but we still want it to behave like when
4303 * dragging. But not the next click in an arrow. */
4304 if (gui.dragged_sb == SBAR_NONE)
4305 gui.dragged_wp = NULL;
4307 if (old_topline != wp->w_topline
4308 #ifdef FEAT_DIFF
4309 || old_topfill != wp->w_topfill
4310 #endif
4313 if (p_so != 0)
4315 cursor_correct(); /* fix window for 'so' */
4316 update_topline(); /* avoid up/down jump */
4318 if (old_cursor.lnum != wp->w_cursor.lnum)
4319 coladvance(wp->w_curswant);
4320 #ifdef FEAT_SCROLLBIND
4321 wp->w_scbind_pos = wp->w_topline;
4322 #endif
4325 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4326 validate_cursor();
4328 curwin = save_wp;
4329 curbuf = save_wp->w_buffer;
4332 * Don't call updateWindow() when nothing has changed (it will overwrite
4333 * the status line!).
4335 if (old_topline != wp->w_topline
4336 || wp->w_redr_type != 0
4337 #ifdef FEAT_DIFF
4338 || old_topfill != wp->w_topfill
4339 #endif
4342 int type = VALID;
4344 #ifdef FEAT_INS_EXPAND
4345 if (pum_visible())
4347 type = NOT_VALID;
4348 wp->w_lines_valid = 0;
4350 #endif
4351 /* Don't set must_redraw here, it may cause the popup menu to
4352 * disappear when losing focus after a scrollbar drag. */
4353 if (wp->w_redr_type < type)
4354 wp->w_redr_type = type;
4355 updateWindow(wp); /* update window, status line, and cmdline */
4358 #ifdef FEAT_INS_EXPAND
4359 /* May need to redraw the popup menu. */
4360 if (pum_visible())
4361 pum_redraw();
4362 #endif
4364 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4369 * Horizontal scrollbar stuff:
4373 * Return length of line "lnum" for horizontal scrolling.
4375 static colnr_T
4376 scroll_line_len(lnum)
4377 linenr_T lnum;
4379 char_u *p;
4380 colnr_T col;
4381 int w;
4383 p = ml_get(lnum);
4384 col = 0;
4385 if (*p != NUL)
4386 for (;;)
4388 w = chartabsize(p, col);
4389 mb_ptr_adv(p);
4390 if (*p == NUL) /* don't count the last character */
4391 break;
4392 col += w;
4394 return col;
4397 /* Remember which line is currently the longest, so that we don't have to
4398 * search for it when scrolling horizontally. */
4399 static linenr_T longest_lnum = 0;
4401 static void
4402 gui_update_horiz_scrollbar(force)
4403 int force;
4405 long value, size, max; /* need 32 bit ints here */
4407 if (!gui.which_scrollbars[SBAR_BOTTOM])
4408 return;
4410 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4411 return;
4413 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4414 return;
4417 * It is possible for the cursor to be invalid if we're in the middle of
4418 * something (like changing files). If so, don't do anything for now.
4420 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4422 gui.bottom_sbar.value = -1;
4423 return;
4426 size = W_WIDTH(curwin);
4427 if (curwin->w_p_wrap)
4429 value = 0;
4430 #ifdef SCROLL_PAST_END
4431 max = 0;
4432 #else
4433 max = W_WIDTH(curwin) - 1;
4434 #endif
4436 else
4438 value = curwin->w_leftcol;
4440 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4441 * line numbers, topline and botline can be invalid when displaying is
4442 * postponed. */
4443 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4444 && curwin->w_topline <= curwin->w_cursor.lnum
4445 && curwin->w_botline > curwin->w_cursor.lnum
4446 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4448 linenr_T lnum;
4449 colnr_T n;
4451 /* Use maximum of all visible lines. Remember the lnum of the
4452 * longest line, clostest to the cursor line. Used when scrolling
4453 * below. */
4454 max = 0;
4455 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4457 n = scroll_line_len(lnum);
4458 if (n > (colnr_T)max)
4460 max = n;
4461 longest_lnum = lnum;
4463 else if (n == (colnr_T)max
4464 && abs((int)(lnum - curwin->w_cursor.lnum))
4465 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4466 longest_lnum = lnum;
4469 else
4470 /* Use cursor line only. */
4471 max = scroll_line_len(curwin->w_cursor.lnum);
4472 #ifdef FEAT_VIRTUALEDIT
4473 if (virtual_active())
4475 /* May move the cursor even further to the right. */
4476 if (curwin->w_virtcol >= (colnr_T)max)
4477 max = curwin->w_virtcol;
4479 #endif
4481 #ifndef SCROLL_PAST_END
4482 max += W_WIDTH(curwin) - 1;
4483 #endif
4484 /* The line number isn't scrolled, thus there is less space when
4485 * 'number' is set (also for 'foldcolumn'). */
4486 size -= curwin_col_off();
4487 #ifndef SCROLL_PAST_END
4488 max -= curwin_col_off();
4489 #endif
4492 #ifndef SCROLL_PAST_END
4493 if (value > max - size + 1)
4494 value = max - size + 1; /* limit the value to allowable range */
4495 #endif
4497 #ifdef FEAT_RIGHTLEFT
4498 if (curwin->w_p_rl)
4500 value = max + 1 - size - value;
4501 if (value < 0)
4503 size += value;
4504 value = 0;
4507 #endif
4508 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4509 && max == gui.bottom_sbar.max)
4510 return;
4512 gui.bottom_sbar.value = value;
4513 gui.bottom_sbar.size = size;
4514 gui.bottom_sbar.max = max;
4515 gui.prev_wrap = curwin->w_p_wrap;
4517 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4521 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4524 gui_do_horiz_scroll()
4526 /* no wrapping, no scrolling */
4527 if (curwin->w_p_wrap)
4528 return FALSE;
4530 if ((long_u)curwin->w_leftcol == scrollbar_value)
4531 return FALSE;
4533 curwin->w_leftcol = (colnr_T)scrollbar_value;
4535 /* When the line of the cursor is too short, move the cursor to the
4536 * longest visible line. Do a sanity check on "longest_lnum", just in
4537 * case. */
4538 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4539 && longest_lnum >= curwin->w_topline
4540 && longest_lnum < curwin->w_botline
4541 && !virtual_active())
4543 if (scrollbar_value > (long_u)scroll_line_len(curwin->w_cursor.lnum))
4545 curwin->w_cursor.lnum = longest_lnum;
4546 curwin->w_cursor.col = 0;
4550 return leftcol_changed();
4554 * Check that none of the colors are the same as the background color
4556 void
4557 gui_check_colors()
4559 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4561 gui_set_bg_color((char_u *)"White");
4562 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4563 gui_set_fg_color((char_u *)"Black");
4567 static void
4568 gui_set_fg_color(name)
4569 char_u *name;
4571 gui.norm_pixel = gui_get_color(name);
4572 hl_set_fg_color_name(vim_strsave(name));
4575 static void
4576 gui_set_bg_color(name)
4577 char_u *name;
4579 gui.back_pixel = gui_get_color(name);
4580 hl_set_bg_color_name(vim_strsave(name));
4584 * Allocate a color by name.
4585 * Returns INVALCOLOR and gives an error message when failed.
4587 guicolor_T
4588 gui_get_color(name)
4589 char_u *name;
4591 guicolor_T t;
4593 if (*name == NUL)
4594 return INVALCOLOR;
4595 t = gui_mch_get_color(name);
4597 if (t == INVALCOLOR
4598 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4599 && gui.in_use
4600 #endif
4602 EMSG2(_("E254: Cannot allocate color %s"), name);
4603 return t;
4607 * Return the grey value of a color (range 0-255).
4610 gui_get_lightness(pixel)
4611 guicolor_T pixel;
4613 long_u rgb = gui_mch_get_rgb(pixel);
4615 return (int)( (((rgb >> 16) & 0xff) * 299)
4616 + (((rgb >> 8) & 0xff) * 587)
4617 + ((rgb & 0xff) * 114)) / 1000;
4620 #if defined(FEAT_GUI_X11) || defined(PROTO)
4621 void
4622 gui_new_scrollbar_colors()
4624 win_T *wp;
4626 /* Nothing to do if GUI hasn't started yet. */
4627 if (!gui.in_use)
4628 return;
4630 FOR_ALL_WINDOWS(wp)
4632 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4633 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4635 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4637 #endif
4640 * Call this when focus has changed.
4642 void
4643 gui_focus_change(in_focus)
4644 int in_focus;
4647 * Skip this code to avoid drawing the cursor when debugging and switching
4648 * between the debugger window and gvim.
4650 #if 1
4651 gui.in_focus = in_focus;
4652 out_flush(); /* make sure output has been written */
4653 gui_update_cursor(TRUE, FALSE);
4655 # ifdef FEAT_XIM
4656 xim_set_focus(in_focus);
4657 # endif
4659 /* Put events in the input queue only when allowed.
4660 * ui_focus_change() isn't called directly, because it invokes
4661 * autocommands and that must not happen asynchronously. */
4662 if (!hold_gui_events)
4664 char_u bytes[3];
4666 bytes[0] = CSI;
4667 bytes[1] = KS_EXTRA;
4668 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4669 add_to_input_buf(bytes, 3);
4671 #endif
4675 * Called when the mouse moved (but not when dragging).
4677 void
4678 gui_mouse_moved(x, y)
4679 int x;
4680 int y;
4682 win_T *wp;
4683 char_u st[8];
4685 /* Ignore this while still starting up. */
4686 if (!gui.in_use || gui.starting)
4687 return;
4689 #ifdef FEAT_MOUSESHAPE
4690 /* Get window pointer, and update mouse shape as well. */
4691 wp = xy2win(x, y);
4692 #endif
4694 /* Only handle this when 'mousefocus' set and ... */
4695 if (p_mousef
4696 && !hold_gui_events /* not holding events */
4697 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4698 && State != HITRETURN /* but not hit-return prompt */
4699 && msg_scrolled == 0 /* no scrolled message */
4700 && !need_mouse_correct /* not moving the pointer */
4701 && gui.in_focus) /* gvim in focus */
4703 /* Don't move the mouse when it's left or right of the Vim window */
4704 if (x < 0 || x > Columns * gui.char_width)
4705 return;
4706 #ifndef FEAT_MOUSESHAPE
4707 wp = xy2win(x, y);
4708 #endif
4709 if (wp == curwin || wp == NULL)
4710 return; /* still in the same old window, or none at all */
4712 #ifdef FEAT_WINDOWS
4713 /* Ignore position in the tab pages line. */
4714 if (Y_2_ROW(y) < tabline_height())
4715 return;
4716 #endif
4719 * format a mouse click on status line input
4720 * ala gui_send_mouse_event(0, x, y, 0, 0);
4721 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4722 * generate a K_LEFTMOUSE_NM key code.
4724 if (finish_op)
4726 /* abort the current operator first */
4727 st[0] = ESC;
4728 add_to_input_buf(st, 1);
4730 st[0] = CSI;
4731 st[1] = KS_MOUSE;
4732 st[2] = KE_FILLER;
4733 st[3] = (char_u)MOUSE_LEFT;
4734 fill_mouse_coord(st + 4,
4735 #ifdef FEAT_VERTSPLIT
4736 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4737 #else
4739 #endif
4740 wp->w_height + W_WINROW(wp));
4742 add_to_input_buf(st, 8);
4743 st[3] = (char_u)MOUSE_RELEASE;
4744 add_to_input_buf(st, 8);
4745 #ifdef FEAT_GUI_GTK
4746 /* Need to wake up the main loop */
4747 if (gtk_main_level() > 0)
4748 gtk_main_quit();
4749 #endif
4754 * Called when mouse should be moved to window with focus.
4756 void
4757 gui_mouse_correct()
4759 int x, y;
4760 win_T *wp = NULL;
4762 need_mouse_correct = FALSE;
4764 if (!(gui.in_use && p_mousef))
4765 return;
4767 gui_mch_getmouse(&x, &y);
4768 /* Don't move the mouse when it's left or right of the Vim window */
4769 if (x < 0 || x > Columns * gui.char_width)
4770 return;
4771 if (y >= 0
4772 # ifdef FEAT_WINDOWS
4773 && Y_2_ROW(y) >= tabline_height()
4774 # endif
4776 wp = xy2win(x, y);
4777 if (wp != curwin && wp != NULL) /* If in other than current window */
4779 validate_cline_row();
4780 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4781 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4782 + (gui.char_height) / 2);
4787 * Find window where the mouse pointer "y" coordinate is in.
4789 static win_T *
4790 xy2win(x, y)
4791 int x UNUSED;
4792 int y UNUSED;
4794 #ifdef FEAT_WINDOWS
4795 int row;
4796 int col;
4797 win_T *wp;
4799 row = Y_2_ROW(y);
4800 col = X_2_COL(x);
4801 if (row < 0 || col < 0) /* before first window */
4802 return NULL;
4803 wp = mouse_find_win(&row, &col);
4804 # ifdef FEAT_MOUSESHAPE
4805 if (State == HITRETURN || State == ASKMORE)
4807 if (Y_2_ROW(y) >= msg_row)
4808 update_mouseshape(SHAPE_IDX_MOREL);
4809 else
4810 update_mouseshape(SHAPE_IDX_MORE);
4812 else if (row > wp->w_height) /* below status line */
4813 update_mouseshape(SHAPE_IDX_CLINE);
4814 # ifdef FEAT_VERTSPLIT
4815 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4816 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4817 update_mouseshape(SHAPE_IDX_VSEP);
4818 # endif
4819 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4820 && row == wp->w_height && msg_scrolled == 0)
4821 update_mouseshape(SHAPE_IDX_STATUS);
4822 else
4823 update_mouseshape(-2);
4824 # endif
4825 return wp;
4826 #else
4827 return firstwin;
4828 #endif
4832 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4833 * File names may be given to redefine the args list.
4835 void
4836 ex_gui(eap)
4837 exarg_T *eap;
4839 char_u *arg = eap->arg;
4842 * Check for "-f" argument: foreground, don't fork.
4843 * Also don't fork when started with "gvim -f".
4844 * Do fork when using "gui -b".
4845 * Note that Mac OS X will never fork on :gui since it can only fork on
4846 * startup right after scanning the command line.
4848 if (arg[0] == '-'
4849 && (arg[1] == 'f' || arg[1] == 'b')
4850 && (arg[2] == NUL || vim_iswhite(arg[2])))
4852 gui.dofork = (arg[1] == 'b');
4853 eap->arg = skipwhite(eap->arg + 2);
4855 if (!gui.in_use)
4857 /* Clear the command. Needed for when forking+exiting, to avoid part
4858 * of the argument ending up after the shell prompt. */
4859 msg_clr_eos_force();
4860 gui_start();
4862 if (!ends_excmd(*eap->arg))
4863 ex_next(eap);
4866 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4867 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR) \
4868 || defined(FEAT_GUI_MACVIM)) || defined(PROTO)
4870 * This is shared between Athena, Motif and GTK.
4872 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4875 * Callback function for do_in_runtimepath().
4877 static void
4878 gfp_setname(fname, cookie)
4879 char_u *fname;
4880 void *cookie;
4882 char_u *gfp_buffer = cookie;
4884 if (STRLEN(fname) >= MAXPATHL)
4885 *gfp_buffer = NUL;
4886 else
4887 STRCPY(gfp_buffer, fname);
4891 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4892 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4895 gui_find_bitmap(name, buffer, ext)
4896 char_u *name;
4897 char_u *buffer;
4898 char *ext;
4900 if (STRLEN(name) > MAXPATHL - 14)
4901 return FAIL;
4902 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4903 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4904 || *buffer == NUL)
4905 return FAIL;
4906 return OK;
4909 # if !defined(HAVE_GTK2) || defined(PROTO)
4911 * Given the name of the "icon=" argument, try finding the bitmap file for the
4912 * icon. If it is an absolute path name, use it as it is. Otherwise append
4913 * "ext" and search for it in 'runtimepath'.
4914 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4915 * contains "name".
4917 void
4918 gui_find_iconfile(name, buffer, ext)
4919 char_u *name;
4920 char_u *buffer;
4921 char *ext;
4923 char_u buf[MAXPATHL + 1];
4925 expand_env(name, buffer, MAXPATHL);
4926 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4927 STRCPY(buffer, buf);
4929 # endif
4930 #endif
4932 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4933 void
4934 display_errors()
4936 char_u *p;
4938 if (isatty(2))
4939 fflush(stderr);
4940 else if (error_ga.ga_data != NULL)
4942 /* avoid putting up a message box with blanks only */
4943 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4944 if (!isspace(*p))
4946 /* Truncate a very long message, it will go off-screen. */
4947 if (STRLEN(p) > 2000)
4948 STRCPY(p + 2000 - 14, "...(truncated)");
4949 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4950 p, (char_u *)_("&Ok"), 1, NULL);
4951 break;
4953 ga_clear(&error_ga);
4956 #endif
4958 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4960 * Return TRUE if still starting up and there is no place to enter text.
4961 * For GTK and X11 we check if stderr is not a tty, which means we were
4962 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4963 * allow typing on stdin.
4966 no_console_input()
4968 return ((!gui.in_use || gui.starting)
4969 # ifndef NO_CONSOLE
4970 && !isatty(0) && !isatty(2)
4971 # endif
4974 #endif
4976 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4977 || defined(NEED_GUI_UPDATE_SCREEN) \
4978 || defined(PROTO)
4980 * Update the current window and the screen.
4982 void
4983 gui_update_screen()
4985 update_topline();
4986 validate_cursor();
4987 #ifdef FEAT_AUTOCMD
4988 /* Trigger CursorMoved if the cursor moved. */
4989 if (!finish_op && has_cursormoved()
4990 && !equalpos(last_cursormoved, curwin->w_cursor))
4992 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4993 last_cursormoved = curwin->w_cursor;
4995 #endif
4996 update_screen(0); /* may need to update the screen */
4997 setcursor();
4998 out_flush(); /* make sure output has been written */
4999 gui_update_cursor(TRUE, FALSE);
5000 gui_mch_flush();
5002 #endif
5004 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5005 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
5008 * Get the text to use in a find/replace dialog. Uses the last search pattern
5009 * if the argument is empty.
5010 * Returns an allocated string.
5012 char_u *
5013 get_find_dialog_text(arg, wwordp, mcasep)
5014 char_u *arg;
5015 int *wwordp; /* return: TRUE if \< \> found */
5016 int *mcasep; /* return: TRUE if \C found */
5018 char_u *text;
5020 if (*arg == NUL)
5021 text = last_search_pat();
5022 else
5023 text = arg;
5024 if (text != NULL)
5026 text = vim_strsave(text);
5027 if (text != NULL)
5029 int len = (int)STRLEN(text);
5030 int i;
5032 /* Remove "\V" */
5033 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5035 mch_memmove(text, text + 2, (size_t)(len - 1));
5036 len -= 2;
5039 /* Recognize "\c" and "\C" and remove. */
5040 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5042 *mcasep = (text[1] == 'C');
5043 mch_memmove(text, text + 2, (size_t)(len - 1));
5044 len -= 2;
5047 /* Recognize "\<text\>" and remove. */
5048 if (len >= 4
5049 && STRNCMP(text, "\\<", 2) == 0
5050 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5052 *wwordp = TRUE;
5053 mch_memmove(text, text + 2, (size_t)(len - 4));
5054 text[len - 4] = NUL;
5057 /* Recognize "\/" or "\?" and remove. */
5058 for (i = 0; i + 1 < len; ++i)
5059 if (text[i] == '\\' && (text[i + 1] == '/'
5060 || text[i + 1] == '?'))
5062 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5063 --len;
5067 return text;
5071 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5073 static void
5074 concat_esc(gap, text, what)
5075 garray_T *gap;
5076 char_u *text;
5077 int what;
5079 while (*text != NUL)
5081 #ifdef FEAT_MBYTE
5082 int l = (*mb_ptr2len)(text);
5084 if (l > 1)
5086 while (--l >= 0)
5087 ga_append(gap, *text++);
5088 continue;
5090 #endif
5091 if (*text == what)
5092 ga_append(gap, '\\');
5093 ga_append(gap, *text);
5094 ++text;
5099 * Handle the press of a button in the find-replace dialog.
5100 * Return TRUE when something was added to the input buffer.
5103 gui_do_findrepl(flags, find_text, repl_text, down)
5104 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5105 char_u *find_text;
5106 char_u *repl_text;
5107 int down; /* Search downwards. */
5109 garray_T ga;
5110 int i;
5111 int type = (flags & FRD_TYPE_MASK);
5112 char_u *p;
5113 regmatch_T regmatch;
5114 int save_did_emsg = did_emsg;
5115 static int busy = FALSE;
5117 /* When the screen is being updated we should not change buffers and
5118 * windows structures, it may cause freed memory to be used. Also don't
5119 * do this recursively (pressing "Find" quickly several times. */
5120 if (updating_screen || busy)
5121 return FALSE;
5123 /* refuse replace when text cannot be changed */
5124 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5125 return FALSE;
5127 busy = TRUE;
5129 ga_init2(&ga, 1, 100);
5130 if (type == FRD_REPLACEALL)
5131 ga_concat(&ga, (char_u *)"%s/");
5133 ga_concat(&ga, (char_u *)"\\V");
5134 if (flags & FRD_MATCH_CASE)
5135 ga_concat(&ga, (char_u *)"\\C");
5136 else
5137 ga_concat(&ga, (char_u *)"\\c");
5138 if (flags & FRD_WHOLE_WORD)
5139 ga_concat(&ga, (char_u *)"\\<");
5140 if (type == FRD_REPLACEALL || down)
5141 concat_esc(&ga, find_text, '/'); /* escape slashes */
5142 else
5143 concat_esc(&ga, find_text, '?'); /* escape '?' */
5144 if (flags & FRD_WHOLE_WORD)
5145 ga_concat(&ga, (char_u *)"\\>");
5147 if (type == FRD_REPLACEALL)
5149 ga_concat(&ga, (char_u *)"/");
5150 /* escape / and \ */
5151 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5152 if (p != NULL)
5153 ga_concat(&ga, p);
5154 vim_free(p);
5155 ga_concat(&ga, (char_u *)"/g");
5157 ga_append(&ga, NUL);
5159 if (type == FRD_REPLACE)
5161 /* Do the replacement when the text at the cursor matches. Thus no
5162 * replacement is done if the cursor was moved! */
5163 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5164 regmatch.rm_ic = 0;
5165 if (regmatch.regprog != NULL)
5167 p = ml_get_cursor();
5168 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5169 && regmatch.startp[0] == p)
5171 /* Clear the command line to remove any old "No match"
5172 * error. */
5173 msg_end_prompt();
5175 if (u_save_cursor() == OK)
5177 /* A button was pressed thus undo should be synced. */
5178 u_sync(FALSE);
5180 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5181 FALSE, FALSE);
5182 ins_str(repl_text);
5185 else
5186 MSG(_("No match at cursor, finding next"));
5187 vim_free(regmatch.regprog);
5191 if (type == FRD_REPLACEALL)
5193 /* A button was pressed, thus undo should be synced. */
5194 u_sync(FALSE);
5195 do_cmdline_cmd(ga.ga_data);
5197 else
5199 /* Search for the next match. */
5200 i = msg_scroll;
5201 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5202 SEARCH_MSG + SEARCH_MARK, NULL);
5203 msg_scroll = i; /* don't let an error message set msg_scroll */
5206 /* Don't want to pass did_emsg to other code, it may cause disabling
5207 * syntax HL if we were busy redrawing. */
5208 did_emsg = save_did_emsg;
5210 if (State & (NORMAL | INSERT))
5212 gui_update_screen(); /* update the screen */
5213 msg_didout = 0; /* overwrite any message */
5214 need_wait_return = FALSE; /* don't wait for return */
5217 vim_free(ga.ga_data);
5218 busy = FALSE;
5219 return (ga.ga_len > 0);
5222 #endif
5224 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5225 || defined(FEAT_GUI_MSWIN) \
5226 || defined(FEAT_GUI_MAC) \
5227 || defined(PROTO) \
5228 || defined(FEAT_GUI_MACVIM)
5230 #ifdef FEAT_WINDOWS
5231 static void gui_wingoto_xy __ARGS((int x, int y));
5234 * Jump to the window at specified point (x, y).
5236 static void
5237 gui_wingoto_xy(x, y)
5238 int x;
5239 int y;
5241 int row = Y_2_ROW(y);
5242 int col = X_2_COL(x);
5243 win_T *wp;
5245 if (row >= 0 && col >= 0)
5247 wp = mouse_find_win(&row, &col);
5248 if (wp != NULL && wp != curwin)
5249 win_goto(wp);
5252 #endif
5255 * Process file drop. Mouse cursor position, key modifiers, name of files
5256 * and count of files are given. Argument "fnames[count]" has full pathnames
5257 * of dropped files, they will be freed in this function, and caller can't use
5258 * fnames after call this function.
5260 void
5261 gui_handle_drop(x, y, modifiers, fnames, count)
5262 int x UNUSED;
5263 int y UNUSED;
5264 int_u modifiers;
5265 char_u **fnames;
5266 int count;
5268 int i;
5269 char_u *p;
5270 static int entered = FALSE;
5273 * This function is called by event handlers. Just in case we get a
5274 * second event before the first one is handled, ignore the second one.
5275 * Not sure if this can ever happen, just in case.
5277 if (entered)
5278 return;
5279 entered = TRUE;
5282 * When the cursor is at the command line, add the file names to the
5283 * command line, don't edit the files.
5285 if (State & CMDLINE)
5287 shorten_filenames(fnames, count);
5288 for (i = 0; i < count; ++i)
5290 if (fnames[i] != NULL)
5292 if (i > 0)
5293 add_to_input_buf((char_u*)" ", 1);
5295 /* We don't know what command is used thus we can't be sure
5296 * about which characters need to be escaped. Only escape the
5297 * most common ones. */
5298 # ifdef BACKSLASH_IN_FILENAME
5299 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5300 # else
5301 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5302 # endif
5303 if (p != NULL)
5304 add_to_input_buf_csi(p, (int)STRLEN(p));
5305 vim_free(p);
5306 vim_free(fnames[i]);
5309 vim_free(fnames);
5311 else
5313 /* Go to the window under mouse cursor, then shorten given "fnames" by
5314 * current window, because a window can have local current dir. */
5315 # ifdef FEAT_WINDOWS
5316 gui_wingoto_xy(x, y);
5317 # endif
5318 shorten_filenames(fnames, count);
5320 /* If Shift held down, remember the first item. */
5321 if ((modifiers & MOUSE_SHIFT) != 0)
5322 p = vim_strsave(fnames[0]);
5323 else
5324 p = NULL;
5326 /* Handle the drop, :edit or :split to get to the file. This also
5327 * frees fnames[]. Skip this if there is only one item it's a
5328 * directory and Shift is held down. */
5329 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5330 && mch_isdir(fnames[0]))
5332 vim_free(fnames[0]);
5333 vim_free(fnames);
5335 else
5336 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5338 /* If Shift held down, change to first file's directory. If the first
5339 * item is a directory, change to that directory (and let the explorer
5340 * plugin show the contents). */
5341 if (p != NULL)
5343 if (mch_isdir(p))
5345 if (mch_chdir((char *)p) == 0)
5346 shorten_fnames(TRUE);
5348 else if (vim_chdirfile(p) == OK)
5349 shorten_fnames(TRUE);
5350 vim_free(p);
5353 /* Update the screen display */
5354 update_screen(NOT_VALID);
5355 # ifdef FEAT_MENU
5356 gui_update_menus(0);
5357 # endif
5358 setcursor();
5359 out_flush();
5360 gui_update_cursor(FALSE, FALSE);
5361 gui_mch_flush();
5364 entered = FALSE;
5366 #endif