Merge branch 'vim' into feat/var-tabstops
[vim_extended.git] / src / gui.c
blob48e7b0fb08615e73b0b28e01100b2357dcec8de5
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 # define MAY_FORK
63 int dofork = TRUE;
64 #endif
65 static int recursive = 0;
67 old_term = vim_strsave(T_NAME);
70 * Set_termname() will call gui_init() to start the GUI.
71 * Set the "starting" flag, to indicate that the GUI will start.
73 * We don't want to open the GUI shell until after we've read .gvimrc,
74 * otherwise we don't know what font we will use, and hence we don't know
75 * what size the shell should be. So if there are errors in the .gvimrc
76 * file, they will have to go to the terminal: Set full_screen to FALSE.
77 * full_screen will be set to TRUE again by a successful termcapinit().
79 settmode(TMODE_COOK); /* stop RAW mode */
80 if (full_screen)
81 cursor_on(); /* needed for ":gui" in .vimrc */
82 gui.starting = TRUE;
83 full_screen = FALSE;
85 #ifdef MAY_FORK
86 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
87 dofork = FALSE;
88 #endif
89 ++recursive;
91 termcapinit((char_u *)"builtin_gui");
92 gui.starting = recursive - 1;
94 if (!gui.in_use) /* failed to start GUI */
96 termcapinit(old_term); /* back to old term settings */
97 settmode(TMODE_RAW); /* restart RAW mode */
98 #ifdef FEAT_TITLE
99 set_title_defaults(); /* set 'title' and 'icon' again */
100 #endif
103 vim_free(old_term);
105 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
106 if (gui.in_use)
107 /* Display error messages in a dialog now. */
108 display_errors();
109 #endif
111 #if defined(MAY_FORK) && !defined(__QNXNTO__)
113 * Quit the current process and continue in the child.
114 * Makes "gvim file" disconnect from the shell it was started in.
115 * Don't do this when Vim was started with "-f" or the 'f' flag is present
116 * in 'guioptions'.
118 if (gui.in_use && dofork)
120 int pipefd[2]; /* pipe between parent and child */
121 int pipe_error;
122 char dummy;
123 pid_t pid = -1;
125 /* Setup a pipe between the child and the parent, so that the parent
126 * knows when the child has done the setsid() call and is allowed to
127 * exit. */
128 pipe_error = (pipe(pipefd) < 0);
129 pid = fork();
130 if (pid > 0) /* Parent */
132 /* Give the child some time to do the setsid(), otherwise the
133 * exit() may kill the child too (when starting gvim from inside a
134 * gvim). */
135 if (pipe_error)
136 ui_delay(300L, TRUE);
137 else
139 /* The read returns when the child closes the pipe (or when
140 * the child dies for some reason). */
141 close(pipefd[1]);
142 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
143 close(pipefd[0]);
146 /* When swapping screens we may need to go to the next line, e.g.,
147 * after a hit-enter prompt and using ":gui". */
148 if (newline_on_exit)
149 mch_errmsg("\r\n");
152 * The parent must skip the normal exit() processing, the child
153 * will do it. For example, GTK messes up signals when exiting.
155 _exit(0);
158 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
160 * Change our process group. On some systems/shells a CTRL-C in the
161 * shell where Vim was started would otherwise kill gvim!
163 if (pid == 0) /* child */
164 # if defined(HAVE_SETSID)
165 (void)setsid();
166 # else
167 (void)setpgid(0, 0);
168 # endif
169 # endif
170 if (!pipe_error)
172 close(pipefd[0]);
173 close(pipefd[1]);
176 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
177 /* Tell the session manager our new PID */
178 gui_mch_forked();
179 # endif
181 #else
182 # if defined(__QNXNTO__)
183 if (gui.in_use && dofork)
184 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
185 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
186 # endif
187 #endif
189 #ifdef FEAT_AUTOCMD
190 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
191 * the GUIFailed event. */
192 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
193 NULL, NULL, FALSE, curbuf);
194 #endif
196 --recursive;
200 * Call this when vim starts up, whether or not the GUI is started
202 void
203 gui_prepare(argc, argv)
204 int *argc;
205 char **argv;
207 gui.in_use = FALSE; /* No GUI yet (maybe later) */
208 gui.starting = FALSE; /* No GUI yet (maybe later) */
209 gui_mch_prepare(argc, argv);
213 * Try initializing the GUI and check if it can be started.
214 * Used from main() to check early if "vim -g" can start the GUI.
215 * Used from gui_init() to prepare for starting the GUI.
216 * Returns FAIL or OK.
219 gui_init_check()
221 static int result = MAYBE;
223 if (result != MAYBE)
225 if (result == FAIL)
226 EMSG(_("E229: Cannot start the GUI"));
227 return result;
230 gui.shell_created = FALSE;
231 gui.dying = FALSE;
232 gui.in_focus = TRUE; /* so the guicursor setting works */
233 gui.dragged_sb = SBAR_NONE;
234 gui.dragged_wp = NULL;
235 gui.pointer_hidden = FALSE;
236 gui.col = 0;
237 gui.row = 0;
238 gui.num_cols = Columns;
239 gui.num_rows = Rows;
241 gui.cursor_is_valid = FALSE;
242 gui.scroll_region_top = 0;
243 gui.scroll_region_bot = Rows - 1;
244 gui.scroll_region_left = 0;
245 gui.scroll_region_right = Columns - 1;
246 gui.highlight_mask = HL_NORMAL;
247 gui.char_width = 1;
248 gui.char_height = 1;
249 gui.char_ascent = 0;
250 gui.border_width = 0;
252 gui.norm_font = NOFONT;
253 #ifndef HAVE_GTK2
254 gui.bold_font = NOFONT;
255 gui.ital_font = NOFONT;
256 gui.boldital_font = NOFONT;
257 # ifdef FEAT_XFONTSET
258 gui.fontset = NOFONTSET;
259 # endif
260 #endif
262 #ifdef FEAT_MENU
263 # ifndef HAVE_GTK2
264 # ifdef FONTSET_ALWAYS
265 gui.menu_fontset = NOFONTSET;
266 # else
267 gui.menu_font = NOFONT;
268 # endif
269 # endif
270 gui.menu_is_active = TRUE; /* default: include menu */
271 # ifndef FEAT_GUI_GTK
272 gui.menu_height = MENU_DEFAULT_HEIGHT;
273 gui.menu_width = 0;
274 # endif
275 #endif
276 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
277 gui.toolbar_height = 0;
278 #endif
279 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
280 gui.footer_height = 0;
281 #endif
282 #ifdef FEAT_BEVAL_TIP
283 gui.tooltip_fontset = NOFONTSET;
284 #endif
286 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
287 gui.prev_wrap = -1;
289 #ifdef ALWAYS_USE_GUI
290 result = OK;
291 #else
292 result = gui_mch_init_check();
293 #endif
294 return result;
298 * This is the call which starts the GUI.
300 void
301 gui_init()
303 win_T *wp;
304 static int recursive = 0;
307 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
308 * function will then be executed at the first call, the rest by the
309 * recursive call. This allow the shell to be opened halfway reading a
310 * gvimrc file.
312 if (!recursive)
314 ++recursive;
316 clip_init(TRUE);
318 /* If can't initialize, don't try doing the rest */
319 if (gui_init_check() == FAIL)
321 --recursive;
322 clip_init(FALSE);
323 return;
327 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
328 * breaks the Paste toolbar button.
330 set_option_value((char_u *)"paste", 0L, NULL, 0);
333 * Set up system-wide default menus.
335 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
336 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
338 sys_menu = TRUE;
339 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
340 sys_menu = FALSE;
342 #endif
345 * Switch on the mouse by default, unless the user changed it already.
346 * This can then be changed in the .gvimrc.
348 if (!option_was_set((char_u *)"mouse"))
349 set_string_option_direct((char_u *)"mouse", -1,
350 (char_u *)"a", OPT_FREE, SID_NONE);
353 * If -U option given, use only the initializations from that file and
354 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
356 if (use_gvimrc != NULL)
358 if (STRCMP(use_gvimrc, "NONE") != 0
359 && STRCMP(use_gvimrc, "NORC") != 0
360 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
361 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
363 else
366 * Get system wide defaults for gvim, only when file name defined.
368 #ifdef SYS_GVIMRC_FILE
369 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
370 #endif
373 * Try to read GUI initialization commands from the following
374 * places:
375 * - environment variable GVIMINIT
376 * - the user gvimrc file (~/.gvimrc)
377 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
378 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
379 * The first that exists is used, the rest is ignored.
381 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
382 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
383 DOSO_GVIMRC) == FAIL
384 #ifdef USR_GVIMRC_FILE2
385 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
386 DOSO_GVIMRC) == FAIL
387 #endif
390 #ifdef USR_GVIMRC_FILE3
391 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
392 #endif
396 * Read initialization commands from ".gvimrc" in current
397 * directory. This is only done if the 'exrc' option is set.
398 * Because of security reasons we disallow shell and write
399 * commands now, except for unix if the file is owned by the user
400 * or 'secure' option has been reset in environment of global
401 * ".gvimrc".
402 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
403 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
405 if (p_exrc)
407 #ifdef UNIX
409 struct stat s;
411 /* if ".gvimrc" file is not owned by user, set 'secure'
412 * mode */
413 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
414 secure = p_secure;
416 #else
417 secure = p_secure;
418 #endif
420 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
421 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
422 #ifdef SYS_GVIMRC_FILE
423 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
424 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
425 #endif
426 #ifdef USR_GVIMRC_FILE2
427 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
428 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
429 #endif
430 #ifdef USR_GVIMRC_FILE3
431 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
432 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
433 #endif
435 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
437 if (secure == 2)
438 need_wait_return = TRUE;
439 secure = 0;
443 if (need_wait_return || msg_didany)
444 wait_return(TRUE);
446 --recursive;
449 /* If recursive call opened the shell, return here from the first call */
450 if (gui.in_use)
451 return;
454 * Create the GUI shell.
456 gui.in_use = TRUE; /* Must be set after menus have been set up */
457 if (gui_mch_init() == FAIL)
458 goto error;
460 /* Avoid a delay for an error message that was printed in the terminal
461 * where Vim was started. */
462 emsg_on_display = FALSE;
463 msg_scrolled = 0;
464 clear_sb_text();
465 need_wait_return = FALSE;
466 msg_didany = FALSE;
469 * Check validity of any generic resources that may have been loaded.
471 if (gui.border_width < 0)
472 gui.border_width = 0;
475 * Set up the fonts. First use a font specified with "-fn" or "-font".
477 if (font_argument != NULL)
478 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
479 if (
480 #ifdef FEAT_XFONTSET
481 (*p_guifontset == NUL
482 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
483 #endif
484 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
485 : p_guifont, FALSE) == FAIL)
487 EMSG(_("E665: Cannot start GUI, no valid font found"));
488 goto error2;
490 #ifdef FEAT_MBYTE
491 if (gui_get_wide_font() == FAIL)
492 EMSG(_("E231: 'guifontwide' invalid"));
493 #endif
495 gui.num_cols = Columns;
496 gui.num_rows = Rows;
497 gui_reset_scroll_region();
499 /* Create initial scrollbars */
500 FOR_ALL_WINDOWS(wp)
502 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
503 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
505 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
507 #ifdef FEAT_MENU
508 gui_create_initial_menus(root_menu);
509 #endif
510 #ifdef FEAT_SUN_WORKSHOP
511 if (usingSunWorkShop)
512 workshop_init();
513 #endif
514 #ifdef FEAT_SIGN_ICONS
515 sign_gui_started();
516 #endif
518 /* Configure the desired menu and scrollbars */
519 gui_init_which_components(NULL);
521 /* All components of the GUI have been created now */
522 gui.shell_created = TRUE;
524 #ifndef FEAT_GUI_GTK
525 /* Set the shell size, adjusted for the screen size. For GTK this only
526 * works after the shell has been opened, thus it is further down. */
527 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
528 #endif
529 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
530 /* Need to set the size of the menubar after all the menus have been
531 * created. */
532 gui_mch_compute_menu_height((Widget)0);
533 #endif
536 * Actually open the GUI shell.
538 if (gui_mch_open() != FAIL)
540 #ifdef FEAT_TITLE
541 maketitle();
542 resettitle();
543 #endif
544 init_gui_options();
545 #ifdef FEAT_ARABIC
546 /* Our GUI can't do bidi. */
547 p_tbidi = FALSE;
548 #endif
549 #if defined(FEAT_GUI_GTK)
550 /* Give GTK+ a chance to put all widget's into place. */
551 gui_mch_update();
553 # ifdef FEAT_MENU
554 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
555 * It was still there to make F10 work. */
556 if (vim_strchr(p_go, GO_MENUS) == NULL)
558 --gui.starting;
559 gui_mch_enable_menu(FALSE);
560 ++gui.starting;
561 gui_mch_update();
563 # endif
565 /* Now make sure the shell fits on the screen. */
566 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
567 #endif
568 /* When 'lines' was set while starting up the topframe may have to be
569 * resized. */
570 win_new_shellsize();
572 #ifdef FEAT_BEVAL
573 /* Always create the Balloon Evaluation area, but disable it when
574 * 'ballooneval' is off */
575 # ifdef FEAT_GUI_GTK
576 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
577 &general_beval_cb, NULL);
578 # else
579 # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
581 extern Widget textArea;
582 balloonEval = gui_mch_create_beval_area(textArea, NULL,
583 &general_beval_cb, NULL);
585 # else
586 # ifdef FEAT_GUI_W32
587 balloonEval = gui_mch_create_beval_area(NULL, NULL,
588 &general_beval_cb, NULL);
589 # endif
590 # endif
591 # endif
592 if (!p_beval)
593 gui_mch_disable_beval_area(balloonEval);
594 #endif
596 #ifdef FEAT_NETBEANS_INTG
597 if (starting == 0 && usingNetbeans)
598 /* Tell the client that it can start sending commands. */
599 netbeans_startup_done();
600 #endif
601 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
602 if (!im_xim_isvalid_imactivate())
603 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
604 #endif
605 /* When 'cmdheight' was set during startup it may not have taken
606 * effect yet. */
607 if (p_ch != 1L)
608 command_height();
610 return;
613 error2:
614 #ifdef FEAT_GUI_X11
615 /* undo gui_mch_init() */
616 gui_mch_uninit();
617 #endif
619 error:
620 gui.in_use = FALSE;
621 clip_init(FALSE);
625 void
626 gui_exit(rc)
627 int rc;
629 #ifndef __BEOS__
630 /* don't free the fonts, it leads to a BUS error
631 * richard@whitequeen.com Jul 99 */
632 free_highlight_fonts();
633 #endif
634 gui.in_use = FALSE;
635 gui_mch_exit(rc);
638 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
639 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
640 # define NEED_GUI_UPDATE_SCREEN 1
642 * Called when the GUI shell is closed by the user. If there are no changed
643 * files Vim exits, otherwise there will be a dialog to ask the user what to
644 * do.
645 * When this function returns, Vim should NOT exit!
647 void
648 gui_shell_closed()
650 cmdmod_T save_cmdmod;
652 save_cmdmod = cmdmod;
654 /* Only exit when there are no changed files */
655 exiting = TRUE;
656 # ifdef FEAT_BROWSE
657 cmdmod.browse = TRUE;
658 # endif
659 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
660 cmdmod.confirm = TRUE;
661 # endif
662 /* If there are changed buffers, present the user with a dialog if
663 * possible, otherwise give an error message. */
664 if (!check_changed_any(FALSE))
665 getout(0);
667 exiting = FALSE;
668 cmdmod = save_cmdmod;
669 gui_update_screen(); /* redraw, window may show changed buffer */
671 #endif
674 * Set the font. "font_list" is a a comma separated list of font names. The
675 * first font name that works is used. If none is found, use the default
676 * font.
677 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
678 * Return OK when able to set the font. When it failed FAIL is returned and
679 * the fonts are unchanged.
682 gui_init_font(font_list, fontset)
683 char_u *font_list;
684 int fontset UNUSED;
686 #define FONTLEN 320
687 char_u font_name[FONTLEN];
688 int font_list_empty = FALSE;
689 int ret = FAIL;
691 if (!gui.in_use)
692 return FAIL;
694 font_name[0] = NUL;
695 if (*font_list == NUL)
696 font_list_empty = TRUE;
697 else
699 #ifdef FEAT_XFONTSET
700 /* When using a fontset, the whole list of fonts is one name. */
701 if (fontset)
702 ret = gui_mch_init_font(font_list, TRUE);
703 else
704 #endif
705 while (*font_list != NUL)
707 /* Isolate one comma separated font name. */
708 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
710 /* Careful!!! The Win32 version of gui_mch_init_font(), when
711 * called with "*" will change p_guifont to the selected font
712 * name, which frees the old value. This makes font_list
713 * invalid. Thus when OK is returned here, font_list must no
714 * longer be used! */
715 if (gui_mch_init_font(font_name, FALSE) == OK)
717 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
718 /* If it's a Unicode font, try setting 'guifontwide' to a
719 * similar double-width font. */
720 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
721 && strstr((char *)font_name, "10646") != NULL)
722 set_guifontwide(font_name);
723 #endif
724 ret = OK;
725 break;
730 if (ret != OK
731 && STRCMP(font_list, "*") != 0
732 && (font_list_empty || gui.norm_font == NOFONT))
735 * Couldn't load any font in 'font_list', keep the current font if
736 * there is one. If 'font_list' is empty, or if there is no current
737 * font, tell gui_mch_init_font() to try to find a font we can load.
739 ret = gui_mch_init_font(NULL, FALSE);
742 if (ret == OK)
744 #ifndef HAVE_GTK2
745 /* Set normal font as current font */
746 # ifdef FEAT_XFONTSET
747 if (gui.fontset != NOFONTSET)
748 gui_mch_set_fontset(gui.fontset);
749 else
750 # endif
751 gui_mch_set_font(gui.norm_font);
752 #endif
753 gui_set_shellsize(FALSE,
754 #ifdef MSWIN
755 TRUE
756 #else
757 FALSE
758 #endif
759 , RESIZE_BOTH);
762 return ret;
765 #if defined(FEAT_MBYTE) || defined(PROTO)
766 # ifndef HAVE_GTK2
768 * Try setting 'guifontwide' to a font twice as wide as "name".
770 static void
771 set_guifontwide(name)
772 char_u *name;
774 int i = 0;
775 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
776 char_u *wp = NULL;
777 char_u *p;
778 GuiFont font;
780 wp = wide_name;
781 for (p = name; *p != NUL; ++p)
783 *wp++ = *p;
784 if (*p == '-')
786 ++i;
787 if (i == 6) /* font type: change "--" to "-*-" */
789 if (p[1] == '-')
790 *wp++ = '*';
792 else if (i == 12) /* found the width */
794 ++p;
795 i = getdigits(&p);
796 if (i != 0)
798 /* Double the width specification. */
799 sprintf((char *)wp, "%d%s", i * 2, p);
800 font = gui_mch_get_font(wide_name, FALSE);
801 if (font != NOFONT)
803 gui_mch_free_font(gui.wide_font);
804 gui.wide_font = font;
805 set_string_option_direct((char_u *)"gfw", -1,
806 wide_name, OPT_FREE, 0);
809 break;
814 # endif /* !HAVE_GTK2 */
817 * Get the font for 'guifontwide'.
818 * Return FAIL for an invalid font name.
821 gui_get_wide_font()
823 GuiFont font = NOFONT;
824 char_u font_name[FONTLEN];
825 char_u *p;
827 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
828 return OK; /* Will give an error message later. */
830 if (p_guifontwide != NULL && *p_guifontwide != NUL)
832 for (p = p_guifontwide; *p != NUL; )
834 /* Isolate one comma separated font name. */
835 (void)copy_option_part(&p, font_name, FONTLEN, ",");
836 font = gui_mch_get_font(font_name, FALSE);
837 if (font != NOFONT)
838 break;
840 if (font == NOFONT)
841 return FAIL;
844 gui_mch_free_font(gui.wide_font);
845 #ifdef HAVE_GTK2
846 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
847 if (font != NOFONT && gui.norm_font != NOFONT
848 && pango_font_description_equal(font, gui.norm_font))
850 gui.wide_font = NOFONT;
851 gui_mch_free_font(font);
853 else
854 #endif
855 gui.wide_font = font;
856 return OK;
858 #endif
860 void
861 gui_set_cursor(row, col)
862 int row;
863 int col;
865 gui.row = row;
866 gui.col = col;
870 * gui_check_pos - check if the cursor is on the screen.
872 static void
873 gui_check_pos()
875 if (gui.row >= screen_Rows)
876 gui.row = screen_Rows - 1;
877 if (gui.col >= screen_Columns)
878 gui.col = screen_Columns - 1;
879 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
880 gui.cursor_is_valid = FALSE;
884 * Redraw the cursor if necessary or when forced.
885 * Careful: The contents of ScreenLines[] must match what is on the screen,
886 * otherwise this goes wrong. May need to call out_flush() first.
888 void
889 gui_update_cursor(force, clear_selection)
890 int force; /* when TRUE, update even when not moved */
891 int clear_selection;/* clear selection under cursor */
893 int cur_width = 0;
894 int cur_height = 0;
895 int old_hl_mask;
896 int idx;
897 int id;
898 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
899 int cattr; /* cursor attributes */
900 int attr;
901 attrentry_T *aep = NULL;
903 /* Don't update the cursor when halfway busy scrolling or the screen size
904 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
905 if (!can_update_cursor || screen_Columns != gui.num_cols
906 || screen_Rows != gui.num_rows)
907 return;
909 gui_check_pos();
910 if (!gui.cursor_is_valid || force
911 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
913 gui_undraw_cursor();
914 if (gui.row < 0)
915 return;
916 #ifdef USE_IM_CONTROL
917 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
918 im_set_position(gui.row, gui.col);
919 #endif
920 gui.cursor_row = gui.row;
921 gui.cursor_col = gui.col;
923 /* Only write to the screen after ScreenLines[] has been initialized */
924 if (!screen_cleared || ScreenLines == NULL)
925 return;
927 /* Clear the selection if we are about to write over it */
928 if (clear_selection)
929 clip_may_clear_selection(gui.row, gui.row);
930 /* Check that the cursor is inside the shell (resizing may have made
931 * it invalid) */
932 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
933 return;
935 gui.cursor_is_valid = TRUE;
938 * How the cursor is drawn depends on the current mode.
940 idx = get_shape_idx(FALSE);
941 if (State & LANGMAP)
942 id = shape_table[idx].id_lm;
943 else
944 id = shape_table[idx].id;
946 /* get the colors and attributes for the cursor. Default is inverted */
947 cfg = INVALCOLOR;
948 cbg = INVALCOLOR;
949 cattr = HL_INVERSE;
950 gui_mch_set_blinking(shape_table[idx].blinkwait,
951 shape_table[idx].blinkon,
952 shape_table[idx].blinkoff);
953 if (id > 0)
955 cattr = syn_id2colors(id, &cfg, &cbg);
956 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
958 static int iid;
959 guicolor_T fg, bg;
961 if (
962 # if defined(HAVE_GTK2) && !defined(FEAT_HANGULIN)
963 preedit_get_status()
964 # else
965 im_get_status()
966 # endif
969 iid = syn_name2id((char_u *)"CursorIM");
970 if (iid > 0)
972 syn_id2colors(iid, &fg, &bg);
973 if (bg != INVALCOLOR)
974 cbg = bg;
975 if (fg != INVALCOLOR)
976 cfg = fg;
980 #endif
984 * Get the attributes for the character under the cursor.
985 * When no cursor color was given, use the character color.
987 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
988 if (attr > HL_ALL)
989 aep = syn_gui_attr2entry(attr);
990 if (aep != NULL)
992 attr = aep->ae_attr;
993 if (cfg == INVALCOLOR)
994 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
995 : aep->ae_u.gui.fg_color);
996 if (cbg == INVALCOLOR)
997 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
998 : aep->ae_u.gui.bg_color);
1000 if (cfg == INVALCOLOR)
1001 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1002 if (cbg == INVALCOLOR)
1003 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1005 #ifdef FEAT_XIM
1006 if (aep != NULL)
1008 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1009 : aep->ae_u.gui.bg_color);
1010 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1011 : aep->ae_u.gui.fg_color);
1012 if (xim_bg_color == INVALCOLOR)
1013 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1014 : gui.back_pixel;
1015 if (xim_fg_color == INVALCOLOR)
1016 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1017 : gui.norm_pixel;
1019 else
1021 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1022 : gui.back_pixel;
1023 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1024 : gui.norm_pixel;
1026 #endif
1028 attr &= ~HL_INVERSE;
1029 if (cattr & HL_INVERSE)
1031 cc = cbg;
1032 cbg = cfg;
1033 cfg = cc;
1035 cattr &= ~HL_INVERSE;
1038 * When we don't have window focus, draw a hollow cursor.
1040 if (!gui.in_focus)
1042 gui_mch_draw_hollow_cursor(cbg);
1043 return;
1046 old_hl_mask = gui.highlight_mask;
1047 if (shape_table[idx].shape == SHAPE_BLOCK
1048 #ifdef FEAT_HANGULIN
1049 || composing_hangul
1050 #endif
1054 * Draw the text character with the cursor colors. Use the
1055 * character attributes plus the cursor attributes.
1057 gui.highlight_mask = (cattr | attr);
1058 #ifdef FEAT_HANGULIN
1059 if (composing_hangul)
1060 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1061 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1062 else
1063 #endif
1064 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1065 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1067 else
1069 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1070 int col_off = FALSE;
1071 #endif
1073 * First draw the partial cursor, then overwrite with the text
1074 * character, using a transparent background.
1076 if (shape_table[idx].shape == SHAPE_VER)
1078 cur_height = gui.char_height;
1079 cur_width = (gui.char_width * shape_table[idx].percentage
1080 + 99) / 100;
1082 else
1084 cur_height = (gui.char_height * shape_table[idx].percentage
1085 + 99) / 100;
1086 cur_width = gui.char_width;
1088 #ifdef FEAT_MBYTE
1089 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1090 LineOffset[gui.row] + screen_Columns) > 1)
1092 /* Double wide character. */
1093 if (shape_table[idx].shape != SHAPE_VER)
1094 cur_width += gui.char_width;
1095 # ifdef FEAT_RIGHTLEFT
1096 if (CURSOR_BAR_RIGHT)
1098 /* gui.col points to the left halve of the character but
1099 * the vertical line needs to be on the right halve.
1100 * A double-wide horizontal line is also drawn from the
1101 * right halve in gui_mch_draw_part_cursor(). */
1102 col_off = TRUE;
1103 ++gui.col;
1105 # endif
1107 #endif
1108 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1109 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1110 if (col_off)
1111 --gui.col;
1112 #endif
1114 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1115 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1116 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1117 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1118 (guicolor_T)0, (guicolor_T)0, 0);
1119 #endif
1121 gui.highlight_mask = old_hl_mask;
1125 #if defined(FEAT_MENU) || defined(PROTO)
1126 void
1127 gui_position_menu()
1129 # if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
1130 if (gui.menu_is_active && gui.in_use)
1131 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1132 # endif
1134 #endif
1137 * Position the various GUI components (text area, menu). The vertical
1138 * scrollbars are NOT handled here. See gui_update_scrollbars().
1140 static void
1141 gui_position_components(total_width)
1142 int total_width UNUSED;
1144 int text_area_x;
1145 int text_area_y;
1146 int text_area_width;
1147 int text_area_height;
1149 /* avoid that moving components around generates events */
1150 ++hold_gui_events;
1152 text_area_x = 0;
1153 if (gui.which_scrollbars[SBAR_LEFT])
1154 text_area_x += gui.scrollbar_width;
1156 text_area_y = 0;
1157 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1158 gui.menu_width = total_width;
1159 if (gui.menu_is_active)
1160 text_area_y += gui.menu_height;
1161 #endif
1162 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1163 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1164 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1165 #endif
1167 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1168 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1169 if (gui_has_tabline())
1170 text_area_y += gui.tabline_height;
1171 #endif
1173 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1174 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1176 # ifdef FEAT_GUI_ATHENA
1177 gui_mch_set_toolbar_pos(0, text_area_y,
1178 gui.menu_width, gui.toolbar_height);
1179 # endif
1180 text_area_y += gui.toolbar_height;
1182 #endif
1184 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1185 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1187 gui_mch_set_text_area_pos(text_area_x,
1188 text_area_y,
1189 text_area_width,
1190 text_area_height
1191 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1192 + xim_get_status_area_height()
1193 #endif
1195 #ifdef FEAT_MENU
1196 gui_position_menu();
1197 #endif
1198 if (gui.which_scrollbars[SBAR_BOTTOM])
1199 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1200 text_area_x,
1201 text_area_y + text_area_height,
1202 text_area_width,
1203 gui.scrollbar_height);
1204 gui.left_sbar_x = 0;
1205 gui.right_sbar_x = text_area_x + text_area_width;
1207 --hold_gui_events;
1211 * Get the width of the widgets and decorations to the side of the text area.
1214 gui_get_base_width()
1216 int base_width;
1218 base_width = 2 * gui.border_offset;
1219 if (gui.which_scrollbars[SBAR_LEFT])
1220 base_width += gui.scrollbar_width;
1221 if (gui.which_scrollbars[SBAR_RIGHT])
1222 base_width += gui.scrollbar_width;
1223 return base_width;
1227 * Get the height of the widgets and decorations above and below the text area.
1230 gui_get_base_height()
1232 int base_height;
1234 base_height = 2 * gui.border_offset;
1235 if (gui.which_scrollbars[SBAR_BOTTOM])
1236 base_height += gui.scrollbar_height;
1237 #ifdef FEAT_GUI_GTK
1238 /* We can't take the sizes properly into account until anything is
1239 * realized. Therefore we recalculate all the values here just before
1240 * setting the size. (--mdcki) */
1241 #else
1242 # ifdef FEAT_MENU
1243 if (gui.menu_is_active)
1244 base_height += gui.menu_height;
1245 # endif
1246 # ifdef FEAT_TOOLBAR
1247 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1248 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1249 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1250 # else
1251 base_height += gui.toolbar_height;
1252 # endif
1253 # endif
1254 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1255 || defined(FEAT_GUI_MOTIF))
1256 if (gui_has_tabline())
1257 base_height += gui.tabline_height;
1258 # endif
1259 # ifdef FEAT_FOOTER
1260 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1261 base_height += gui.footer_height;
1262 # endif
1263 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1264 base_height += gui_mch_text_area_extra_height();
1265 # endif
1266 #endif
1267 return base_height;
1271 * Should be called after the GUI shell has been resized. Its arguments are
1272 * the new width and height of the shell in pixels.
1274 void
1275 gui_resize_shell(pixel_width, pixel_height)
1276 int pixel_width;
1277 int pixel_height;
1279 static int busy = FALSE;
1281 if (!gui.shell_created) /* ignore when still initializing */
1282 return;
1285 * Can't resize the screen while it is being redrawn. Remember the new
1286 * size and handle it later.
1288 if (updating_screen || busy)
1290 new_pixel_width = pixel_width;
1291 new_pixel_height = pixel_height;
1292 return;
1295 again:
1296 busy = TRUE;
1298 /* Flush pending output before redrawing */
1299 out_flush();
1301 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1302 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
1304 gui_position_components(pixel_width);
1306 gui_reset_scroll_region();
1308 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1309 * at the last line here (why does it have to be one row too low?).
1311 if (State == ASKMORE || State == CONFIRM)
1312 gui.row = gui.num_rows;
1314 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1315 * the safe side. */
1316 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1317 || gui.num_rows != Rows || gui.num_cols != Columns)
1318 shell_resized();
1320 gui_update_scrollbars(TRUE);
1321 gui_update_cursor(FALSE, TRUE);
1322 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1323 xim_set_status_area();
1324 #endif
1326 busy = FALSE;
1329 * We could have been called again while redrawing the screen.
1330 * Need to do it all again with the latest size then.
1332 if (new_pixel_height)
1334 pixel_width = new_pixel_width;
1335 pixel_height = new_pixel_height;
1336 new_pixel_width = 0;
1337 new_pixel_height = 0;
1338 goto again;
1343 * Check if gui_resize_shell() must be called.
1345 void
1346 gui_may_resize_shell()
1348 int h, w;
1350 if (new_pixel_height)
1352 /* careful: gui_resize_shell() may postpone the resize again if we
1353 * were called indirectly by it */
1354 w = new_pixel_width;
1355 h = new_pixel_height;
1356 new_pixel_width = 0;
1357 new_pixel_height = 0;
1358 gui_resize_shell(w, h);
1363 gui_get_shellsize()
1365 Rows = gui.num_rows;
1366 Columns = gui.num_cols;
1367 return OK;
1371 * Set the size of the Vim shell according to Rows and Columns.
1372 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1373 * on the screen.
1375 void
1376 gui_set_shellsize(mustset, fit_to_display, direction)
1377 int mustset UNUSED; /* set by the user */
1378 int fit_to_display;
1379 int direction; /* RESIZE_HOR, RESIZE_VER */
1381 int base_width;
1382 int base_height;
1383 int width;
1384 int height;
1385 int min_width;
1386 int min_height;
1387 int screen_w;
1388 int screen_h;
1389 #ifdef HAVE_GTK2
1390 int un_maximize = mustset;
1391 int did_adjust = 0;
1392 #endif
1394 if (!gui.shell_created)
1395 return;
1397 #ifdef MSWIN
1398 /* If not setting to a user specified size and maximized, calculate the
1399 * number of characters that fit in the maximized window. */
1400 if (!mustset && gui_mch_maximized())
1402 gui_mch_newfont();
1403 return;
1405 #endif
1407 base_width = gui_get_base_width();
1408 base_height = gui_get_base_height();
1409 #ifdef USE_SUN_WORKSHOP
1410 if (!mustset && usingSunWorkShop
1411 && workshop_get_width_height(&width, &height))
1413 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1414 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1416 else
1417 #endif
1419 width = Columns * gui.char_width + base_width;
1420 height = Rows * gui.char_height + base_height;
1423 if (fit_to_display)
1425 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1426 if ((direction & RESIZE_HOR) && width > screen_w)
1428 Columns = (screen_w - base_width) / gui.char_width;
1429 if (Columns < MIN_COLUMNS)
1430 Columns = MIN_COLUMNS;
1431 width = Columns * gui.char_width + base_width;
1432 #ifdef HAVE_GTK2
1433 ++did_adjust;
1434 #endif
1436 if ((direction & RESIZE_VERT) && height > screen_h)
1438 Rows = (screen_h - base_height) / gui.char_height;
1439 check_shellsize();
1440 height = Rows * gui.char_height + base_height;
1441 #ifdef HAVE_GTK2
1442 ++did_adjust;
1443 #endif
1445 #ifdef HAVE_GTK2
1446 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1447 && height + gui.char_height >= screen_h))
1448 /* don't unmaximize if at maximum size */
1449 un_maximize = FALSE;
1450 #endif
1452 gui.num_cols = Columns;
1453 gui.num_rows = Rows;
1455 min_width = base_width + MIN_COLUMNS * gui.char_width;
1456 min_height = base_height + MIN_LINES * gui.char_height;
1457 #ifdef FEAT_WINDOWS
1458 min_height += tabline_height() * gui.char_height;
1459 #endif
1461 #ifdef HAVE_GTK2
1462 if (un_maximize)
1464 /* If the window size is smaller than the screen unmaximize the
1465 * window, otherwise resizing won't work. */
1466 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1467 if ((width + gui.char_width < screen_w
1468 || height + gui.char_height * 2 < screen_h)
1469 && gui_mch_maximized())
1470 gui_mch_unmaximize();
1472 #endif
1474 gui_mch_set_shellsize(width, height, min_width, min_height,
1475 base_width, base_height, direction);
1476 if (fit_to_display)
1478 int x, y;
1480 /* Some window managers put the Vim window left of/above the screen. */
1481 gui_mch_update();
1482 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1483 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1486 gui_position_components(width);
1487 gui_update_scrollbars(TRUE);
1488 gui_reset_scroll_region();
1492 * Called when Rows and/or Columns has changed.
1494 void
1495 gui_new_shellsize()
1497 gui_reset_scroll_region();
1501 * Make scroll region cover whole screen.
1503 void
1504 gui_reset_scroll_region()
1506 gui.scroll_region_top = 0;
1507 gui.scroll_region_bot = gui.num_rows - 1;
1508 gui.scroll_region_left = 0;
1509 gui.scroll_region_right = gui.num_cols - 1;
1512 void
1513 gui_start_highlight(mask)
1514 int mask;
1516 if (mask > HL_ALL) /* highlight code */
1517 gui.highlight_mask = mask;
1518 else /* mask */
1519 gui.highlight_mask |= mask;
1522 void
1523 gui_stop_highlight(mask)
1524 int mask;
1526 if (mask > HL_ALL) /* highlight code */
1527 gui.highlight_mask = HL_NORMAL;
1528 else /* mask */
1529 gui.highlight_mask &= ~mask;
1533 * Clear a rectangular region of the screen from text pos (row1, col1) to
1534 * (row2, col2) inclusive.
1536 void
1537 gui_clear_block(row1, col1, row2, col2)
1538 int row1;
1539 int col1;
1540 int row2;
1541 int col2;
1543 /* Clear the selection if we are about to write over it */
1544 clip_may_clear_selection(row1, row2);
1546 gui_mch_clear_block(row1, col1, row2, col2);
1548 /* Invalidate cursor if it was in this block */
1549 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1550 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1551 gui.cursor_is_valid = FALSE;
1555 * Write code to update the cursor later. This avoids the need to flush the
1556 * output buffer before calling gui_update_cursor().
1558 void
1559 gui_update_cursor_later()
1561 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1564 void
1565 gui_write(s, len)
1566 char_u *s;
1567 int len;
1569 char_u *p;
1570 int arg1 = 0, arg2 = 0;
1571 /* this doesn't make sense, disabled until someone can explain why it
1572 * would be needed */
1573 #if 0 && (defined(RISCOS) || defined(WIN16))
1574 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1575 our redraw speed will suffer */
1576 #else
1577 int force_cursor = FALSE; /* force cursor update */
1578 #endif
1579 int force_scrollbar = FALSE;
1580 static win_T *old_curwin = NULL;
1582 /* #define DEBUG_GUI_WRITE */
1583 #ifdef DEBUG_GUI_WRITE
1585 int i;
1586 char_u *str;
1588 printf("gui_write(%d):\n ", len);
1589 for (i = 0; i < len; i++)
1590 if (s[i] == ESC)
1592 if (i != 0)
1593 printf("\n ");
1594 printf("<ESC>");
1596 else
1598 str = transchar_byte(s[i]);
1599 if (str[0] && str[1])
1600 printf("<%s>", (char *)str);
1601 else
1602 printf("%s", (char *)str);
1604 printf("\n");
1606 #endif
1607 while (len)
1609 if (s[0] == ESC && s[1] == '|')
1611 p = s + 2;
1612 if (VIM_ISDIGIT(*p))
1614 arg1 = getdigits(&p);
1615 if (p > s + len)
1616 break;
1617 if (*p == ';')
1619 ++p;
1620 arg2 = getdigits(&p);
1621 if (p > s + len)
1622 break;
1625 switch (*p)
1627 case 'C': /* Clear screen */
1628 clip_scroll_selection(9999);
1629 gui_mch_clear_all();
1630 gui.cursor_is_valid = FALSE;
1631 force_scrollbar = TRUE;
1632 break;
1633 case 'M': /* Move cursor */
1634 gui_set_cursor(arg1, arg2);
1635 break;
1636 case 's': /* force cursor (shape) update */
1637 force_cursor = TRUE;
1638 break;
1639 case 'R': /* Set scroll region */
1640 if (arg1 < arg2)
1642 gui.scroll_region_top = arg1;
1643 gui.scroll_region_bot = arg2;
1645 else
1647 gui.scroll_region_top = arg2;
1648 gui.scroll_region_bot = arg1;
1650 break;
1651 #ifdef FEAT_VERTSPLIT
1652 case 'V': /* Set vertical scroll region */
1653 if (arg1 < arg2)
1655 gui.scroll_region_left = arg1;
1656 gui.scroll_region_right = arg2;
1658 else
1660 gui.scroll_region_left = arg2;
1661 gui.scroll_region_right = arg1;
1663 break;
1664 #endif
1665 case 'd': /* Delete line */
1666 gui_delete_lines(gui.row, 1);
1667 break;
1668 case 'D': /* Delete lines */
1669 gui_delete_lines(gui.row, arg1);
1670 break;
1671 case 'i': /* Insert line */
1672 gui_insert_lines(gui.row, 1);
1673 break;
1674 case 'I': /* Insert lines */
1675 gui_insert_lines(gui.row, arg1);
1676 break;
1677 case '$': /* Clear to end-of-line */
1678 gui_clear_block(gui.row, gui.col, gui.row,
1679 (int)Columns - 1);
1680 break;
1681 case 'h': /* Turn on highlighting */
1682 gui_start_highlight(arg1);
1683 break;
1684 case 'H': /* Turn off highlighting */
1685 gui_stop_highlight(arg1);
1686 break;
1687 case 'f': /* flash the window (visual bell) */
1688 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1689 break;
1690 default:
1691 p = s + 1; /* Skip the ESC */
1692 break;
1694 len -= (int)(++p - s);
1695 s = p;
1697 else if (
1698 #ifdef EBCDIC
1699 CtrlChar(s[0]) != 0 /* Ctrl character */
1700 #else
1701 s[0] < 0x20 /* Ctrl character */
1702 #endif
1703 #ifdef FEAT_SIGN_ICONS
1704 && s[0] != SIGN_BYTE
1705 # ifdef FEAT_NETBEANS_INTG
1706 && s[0] != MULTISIGN_BYTE
1707 # endif
1708 #endif
1711 if (s[0] == '\n') /* NL */
1713 gui.col = 0;
1714 if (gui.row < gui.scroll_region_bot)
1715 gui.row++;
1716 else
1717 gui_delete_lines(gui.scroll_region_top, 1);
1719 else if (s[0] == '\r') /* CR */
1721 gui.col = 0;
1723 else if (s[0] == '\b') /* Backspace */
1725 if (gui.col)
1726 --gui.col;
1728 else if (s[0] == Ctrl_L) /* cursor-right */
1730 ++gui.col;
1732 else if (s[0] == Ctrl_G) /* Beep */
1734 gui_mch_beep();
1736 /* Other Ctrl character: shouldn't happen! */
1738 --len; /* Skip this char */
1739 ++s;
1741 else
1743 p = s;
1744 while (len > 0 && (
1745 #ifdef EBCDIC
1746 CtrlChar(*p) == 0
1747 #else
1748 *p >= 0x20
1749 #endif
1750 #ifdef FEAT_SIGN_ICONS
1751 || *p == SIGN_BYTE
1752 # ifdef FEAT_NETBEANS_INTG
1753 || *p == MULTISIGN_BYTE
1754 # endif
1755 #endif
1758 len--;
1759 p++;
1761 gui_outstr(s, (int)(p - s));
1762 s = p;
1766 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1767 * set). */
1768 if (force_cursor)
1769 gui_update_cursor(TRUE, TRUE);
1771 /* When switching to another window the dragging must have stopped.
1772 * Required for GTK, dragged_sb isn't reset. */
1773 if (old_curwin != curwin)
1774 gui.dragged_sb = SBAR_NONE;
1776 /* Update the scrollbars after clearing the screen or when switched
1777 * to another window.
1778 * Update the horizontal scrollbar always, it's difficult to check all
1779 * situations where it might change. */
1780 if (force_scrollbar || old_curwin != curwin)
1781 gui_update_scrollbars(force_scrollbar);
1782 else
1783 gui_update_horiz_scrollbar(FALSE);
1784 old_curwin = curwin;
1787 * We need to make sure this is cleared since Athena doesn't tell us when
1788 * he is done dragging. Do the same for GTK.
1790 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1791 gui.dragged_sb = SBAR_NONE;
1792 #endif
1794 gui_mch_flush(); /* In case vim decides to take a nap */
1798 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1799 * produces wrong results. Call gui_dont_update_cursor() before that code and
1800 * gui_can_update_cursor() afterwards.
1802 void
1803 gui_dont_update_cursor()
1805 if (gui.in_use)
1807 /* Undraw the cursor now, we probably can't do it after the change. */
1808 gui_undraw_cursor();
1809 can_update_cursor = FALSE;
1813 void
1814 gui_can_update_cursor()
1816 can_update_cursor = TRUE;
1817 /* No need to update the cursor right now, there is always more output
1818 * after scrolling. */
1821 static void
1822 gui_outstr(s, len)
1823 char_u *s;
1824 int len;
1826 int this_len;
1827 #ifdef FEAT_MBYTE
1828 int cells;
1829 #endif
1831 if (len == 0)
1832 return;
1834 if (len < 0)
1835 len = (int)STRLEN(s);
1837 while (len > 0)
1839 #ifdef FEAT_MBYTE
1840 if (has_mbyte)
1842 /* Find out how many chars fit in the current line. */
1843 cells = 0;
1844 for (this_len = 0; this_len < len; )
1846 cells += (*mb_ptr2cells)(s + this_len);
1847 if (gui.col + cells > Columns)
1848 break;
1849 this_len += (*mb_ptr2len)(s + this_len);
1851 if (this_len > len)
1852 this_len = len; /* don't include following composing char */
1854 else
1855 #endif
1856 if (gui.col + len > Columns)
1857 this_len = Columns - gui.col;
1858 else
1859 this_len = len;
1861 (void)gui_outstr_nowrap(s, this_len,
1862 0, (guicolor_T)0, (guicolor_T)0, 0);
1863 s += this_len;
1864 len -= this_len;
1865 #ifdef FEAT_MBYTE
1866 /* fill up for a double-width char that doesn't fit. */
1867 if (len > 0 && gui.col < Columns)
1868 (void)gui_outstr_nowrap((char_u *)" ", 1,
1869 0, (guicolor_T)0, (guicolor_T)0, 0);
1870 #endif
1871 /* The cursor may wrap to the next line. */
1872 if (gui.col >= Columns)
1874 gui.col = 0;
1875 gui.row++;
1881 * Output one character (may be one or two display cells).
1882 * Caller must check for valid "off".
1883 * Returns FAIL or OK, just like gui_outstr_nowrap().
1885 static int
1886 gui_screenchar(off, flags, fg, bg, back)
1887 int off; /* Offset from start of screen */
1888 int flags;
1889 guicolor_T fg, bg; /* colors for cursor */
1890 int back; /* backup this many chars when using bold trick */
1892 #ifdef FEAT_MBYTE
1893 char_u buf[MB_MAXBYTES + 1];
1895 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1896 if (enc_utf8 && ScreenLines[off] == 0)
1897 return OK;
1899 if (enc_utf8 && ScreenLinesUC[off] != 0)
1900 /* Draw UTF-8 multi-byte character. */
1901 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1902 flags, fg, bg, back);
1904 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1906 buf[0] = ScreenLines[off];
1907 buf[1] = ScreenLines2[off];
1908 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1911 /* Draw non-multi-byte character or DBCS character. */
1912 return gui_outstr_nowrap(ScreenLines + off,
1913 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1914 flags, fg, bg, back);
1915 #else
1916 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1917 #endif
1920 #ifdef HAVE_GTK2
1922 * Output the string at the given screen position. This is used in place
1923 * of gui_screenchar() where possible because Pango needs as much context
1924 * as possible to work nicely. It's a lot faster as well.
1926 static int
1927 gui_screenstr(off, len, flags, fg, bg, back)
1928 int off; /* Offset from start of screen */
1929 int len; /* string length in screen cells */
1930 int flags;
1931 guicolor_T fg, bg; /* colors for cursor */
1932 int back; /* backup this many chars when using bold trick */
1934 char_u *buf;
1935 int outlen = 0;
1936 int i;
1937 int retval;
1939 if (len <= 0) /* "cannot happen"? */
1940 return OK;
1942 if (enc_utf8)
1944 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1945 if (buf == NULL)
1946 return OK; /* not much we could do here... */
1948 for (i = off; i < off + len; ++i)
1950 if (ScreenLines[i] == 0)
1951 continue; /* skip second half of double-width char */
1953 if (ScreenLinesUC[i] == 0)
1954 buf[outlen++] = ScreenLines[i];
1955 else
1956 outlen += utfc_char2bytes(i, buf + outlen);
1959 buf[outlen] = NUL; /* only to aid debugging */
1960 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1961 vim_free(buf);
1963 return retval;
1965 else if (enc_dbcs == DBCS_JPNU)
1967 buf = alloc((unsigned)(len * 2 + 1));
1968 if (buf == NULL)
1969 return OK; /* not much we could do here... */
1971 for (i = off; i < off + len; ++i)
1973 buf[outlen++] = ScreenLines[i];
1975 /* handle double-byte single-width char */
1976 if (ScreenLines[i] == 0x8e)
1977 buf[outlen++] = ScreenLines2[i];
1978 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1979 buf[outlen++] = ScreenLines[++i];
1982 buf[outlen] = NUL; /* only to aid debugging */
1983 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1984 vim_free(buf);
1986 return retval;
1988 else
1990 return gui_outstr_nowrap(&ScreenLines[off], len,
1991 flags, fg, bg, back);
1994 #endif /* HAVE_GTK2 */
1997 * Output the given string at the current cursor position. If the string is
1998 * too long to fit on the line, then it is truncated.
1999 * "flags":
2000 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2001 * actually draw (an inverted) cursor.
2002 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
2003 * background.
2004 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2005 * it.
2006 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2007 * FAIL (the caller should start drawing "back" chars back).
2010 gui_outstr_nowrap(s, len, flags, fg, bg, back)
2011 char_u *s;
2012 int len;
2013 int flags;
2014 guicolor_T fg, bg; /* colors for cursor */
2015 int back; /* backup this many chars when using bold trick */
2017 long_u highlight_mask;
2018 long_u hl_mask_todo;
2019 guicolor_T fg_color;
2020 guicolor_T bg_color;
2021 guicolor_T sp_color;
2022 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2023 GuiFont font = NOFONT;
2024 # ifdef FEAT_XFONTSET
2025 GuiFontset fontset = NOFONTSET;
2026 # endif
2027 #endif
2028 attrentry_T *aep = NULL;
2029 int draw_flags;
2030 int col = gui.col;
2031 #ifdef FEAT_SIGN_ICONS
2032 int draw_sign = FALSE;
2033 # ifdef FEAT_NETBEANS_INTG
2034 int multi_sign = FALSE;
2035 # endif
2036 #endif
2038 if (len < 0)
2039 len = (int)STRLEN(s);
2040 if (len == 0)
2041 return OK;
2043 #ifdef FEAT_SIGN_ICONS
2044 if (*s == SIGN_BYTE
2045 # ifdef FEAT_NETBEANS_INTG
2046 || *s == MULTISIGN_BYTE
2047 # endif
2050 # ifdef FEAT_NETBEANS_INTG
2051 if (*s == MULTISIGN_BYTE)
2052 multi_sign = TRUE;
2053 # endif
2054 /* draw spaces instead */
2055 s = (char_u *)" ";
2056 if (len == 1 && col > 0)
2057 --col;
2058 len = 2;
2059 draw_sign = TRUE;
2060 highlight_mask = 0;
2062 else
2063 #endif
2064 if (gui.highlight_mask > HL_ALL)
2066 aep = syn_gui_attr2entry(gui.highlight_mask);
2067 if (aep == NULL) /* highlighting not set */
2068 highlight_mask = 0;
2069 else
2070 highlight_mask = aep->ae_attr;
2072 else
2073 highlight_mask = gui.highlight_mask;
2074 hl_mask_todo = highlight_mask;
2076 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2077 /* Set the font */
2078 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2079 font = aep->ae_u.gui.font;
2080 # ifdef FEAT_XFONTSET
2081 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2082 fontset = aep->ae_u.gui.fontset;
2083 # endif
2084 else
2086 # ifdef FEAT_XFONTSET
2087 if (gui.fontset != NOFONTSET)
2088 fontset = gui.fontset;
2089 else
2090 # endif
2091 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2093 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2095 font = gui.boldital_font;
2096 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2098 else if (gui.bold_font != NOFONT)
2100 font = gui.bold_font;
2101 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2103 else
2104 font = gui.norm_font;
2106 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2108 font = gui.ital_font;
2109 hl_mask_todo &= ~HL_ITALIC;
2111 else
2112 font = gui.norm_font;
2114 # ifdef FEAT_XFONTSET
2115 if (fontset != NOFONTSET)
2116 gui_mch_set_fontset(fontset);
2117 else
2118 # endif
2119 gui_mch_set_font(font);
2120 #endif
2122 draw_flags = 0;
2124 /* Set the color */
2125 bg_color = gui.back_pixel;
2126 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2128 draw_flags |= DRAW_CURSOR;
2129 fg_color = fg;
2130 bg_color = bg;
2131 sp_color = fg;
2133 else if (aep != NULL)
2135 fg_color = aep->ae_u.gui.fg_color;
2136 if (fg_color == INVALCOLOR)
2137 fg_color = gui.norm_pixel;
2138 bg_color = aep->ae_u.gui.bg_color;
2139 if (bg_color == INVALCOLOR)
2140 bg_color = gui.back_pixel;
2141 sp_color = aep->ae_u.gui.sp_color;
2142 if (sp_color == INVALCOLOR)
2143 sp_color = fg_color;
2145 else
2147 fg_color = gui.norm_pixel;
2148 sp_color = fg_color;
2151 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2153 #if defined(AMIGA) || defined(RISCOS)
2154 gui_mch_set_colors(bg_color, fg_color);
2155 #else
2156 gui_mch_set_fg_color(bg_color);
2157 gui_mch_set_bg_color(fg_color);
2158 #endif
2160 else
2162 #if defined(AMIGA) || defined(RISCOS)
2163 gui_mch_set_colors(fg_color, bg_color);
2164 #else
2165 gui_mch_set_fg_color(fg_color);
2166 gui_mch_set_bg_color(bg_color);
2167 #endif
2169 gui_mch_set_sp_color(sp_color);
2171 /* Clear the selection if we are about to write over it */
2172 if (!(flags & GUI_MON_NOCLEAR))
2173 clip_may_clear_selection(gui.row, gui.row);
2176 #ifndef MSWIN16_FASTTEXT
2177 /* If there's no bold font, then fake it */
2178 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2179 draw_flags |= DRAW_BOLD;
2180 #endif
2183 * When drawing bold or italic characters the spill-over from the left
2184 * neighbor may be destroyed. Let the caller backup to start redrawing
2185 * just after a blank.
2187 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2188 return FAIL;
2190 #if defined(RISCOS) || defined(HAVE_GTK2)
2191 /* If there's no italic font, then fake it.
2192 * For GTK2, we don't need a different font for italic style. */
2193 if (hl_mask_todo & HL_ITALIC)
2194 draw_flags |= DRAW_ITALIC;
2196 /* Do we underline the text? */
2197 if (hl_mask_todo & HL_UNDERLINE)
2198 draw_flags |= DRAW_UNDERL;
2199 #else
2200 /* Do we underline the text? */
2201 if ((hl_mask_todo & HL_UNDERLINE)
2202 # ifndef MSWIN16_FASTTEXT
2203 || (hl_mask_todo & HL_ITALIC)
2204 # endif
2206 draw_flags |= DRAW_UNDERL;
2207 #endif
2208 /* Do we undercurl the text? */
2209 if (hl_mask_todo & HL_UNDERCURL)
2210 draw_flags |= DRAW_UNDERC;
2212 /* Do we draw transparently? */
2213 if (flags & GUI_MON_TRS_CURSOR)
2214 draw_flags |= DRAW_TRANSP;
2217 * Draw the text.
2219 #ifdef HAVE_GTK2
2220 /* The value returned is the length in display cells */
2221 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2222 #else
2223 # ifdef FEAT_MBYTE
2224 if (enc_utf8)
2226 int start; /* index of bytes to be drawn */
2227 int cells; /* cellwidth of bytes to be drawn */
2228 int thislen; /* length of bytes to be drawin */
2229 int cn; /* cellwidth of current char */
2230 int i; /* index of current char */
2231 int c; /* current char value */
2232 int cl; /* byte length of current char */
2233 int comping; /* current char is composing */
2234 int scol = col; /* screen column */
2235 int dowide; /* use 'guifontwide' */
2237 /* Break the string at a composing character, it has to be drawn on
2238 * top of the previous character. */
2239 start = 0;
2240 cells = 0;
2241 for (i = 0; i < len; i += cl)
2243 c = utf_ptr2char(s + i);
2244 cn = utf_char2cells(c);
2245 if (cn > 1
2246 # ifdef FEAT_XFONTSET
2247 && fontset == NOFONTSET
2248 # endif
2249 && gui.wide_font != NOFONT)
2250 dowide = TRUE;
2251 else
2252 dowide = FALSE;
2253 comping = utf_iscomposing(c);
2254 if (!comping) /* count cells from non-composing chars */
2255 cells += cn;
2256 cl = utf_ptr2len(s + i);
2257 if (cl == 0) /* hit end of string */
2258 len = i + cl; /* len must be wrong "cannot happen" */
2260 /* print the string so far if it's the last character or there is
2261 * a composing character. */
2262 if (i + cl >= len || (comping && i > start) || dowide
2263 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2264 || (cn > 1
2265 # ifdef FEAT_XFONTSET
2266 /* No fontset: At least draw char after wide char at
2267 * right position. */
2268 && fontset == NOFONTSET
2269 # endif
2271 # endif
2274 if (comping || dowide)
2275 thislen = i - start;
2276 else
2277 thislen = i - start + cl;
2278 if (thislen > 0)
2280 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2281 draw_flags);
2282 start += thislen;
2284 scol += cells;
2285 cells = 0;
2286 if (dowide)
2288 gui_mch_set_font(gui.wide_font);
2289 gui_mch_draw_string(gui.row, scol - cn,
2290 s + start, cl, draw_flags);
2291 gui_mch_set_font(font);
2292 start += cl;
2295 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2296 /* No fontset: draw a space to fill the gap after a wide char
2297 * */
2298 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2299 # ifdef FEAT_XFONTSET
2300 && fontset == NOFONTSET
2301 # endif
2302 && !dowide)
2303 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2304 1, draw_flags);
2305 # endif
2307 /* Draw a composing char on top of the previous char. */
2308 if (comping)
2310 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2311 /* Carbon ATSUI autodraws composing char over previous char */
2312 gui_mch_draw_string(gui.row, scol, s + i, cl,
2313 draw_flags | DRAW_TRANSP);
2314 # else
2315 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2316 draw_flags | DRAW_TRANSP);
2317 # endif
2318 start = i + cl;
2321 /* The stuff below assumes "len" is the length in screen columns. */
2322 len = scol - col;
2324 else
2325 # endif
2327 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2328 # ifdef FEAT_MBYTE
2329 if (enc_dbcs == DBCS_JPNU)
2331 int clen = 0;
2332 int i;
2334 /* Get the length in display cells, this can be different from the
2335 * number of bytes for "euc-jp". */
2336 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2337 clen += (*mb_ptr2cells)(s + i);
2338 len = clen;
2340 # endif
2342 #endif /* !HAVE_GTK2 */
2344 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2345 gui.col = col + len;
2347 /* May need to invert it when it's part of the selection. */
2348 if (flags & GUI_MON_NOCLEAR)
2349 clip_may_redraw_selection(gui.row, col, len);
2351 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2353 /* Invalidate the old physical cursor position if we wrote over it */
2354 if (gui.cursor_row == gui.row
2355 && gui.cursor_col >= col
2356 && gui.cursor_col < col + len)
2357 gui.cursor_is_valid = FALSE;
2360 #ifdef FEAT_SIGN_ICONS
2361 if (draw_sign)
2362 /* Draw the sign on top of the spaces. */
2363 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2364 # ifdef FEAT_NETBEANS_INTG
2365 if (multi_sign)
2366 netbeans_draw_multisign_indicator(gui.row);
2367 # endif
2368 #endif
2370 return OK;
2374 * Un-draw the cursor. Actually this just redraws the character at the given
2375 * position. The character just before it too, for when it was in bold.
2377 void
2378 gui_undraw_cursor()
2380 if (gui.cursor_is_valid)
2382 #ifdef FEAT_HANGULIN
2383 if (composing_hangul
2384 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2385 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2386 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2387 gui.norm_pixel, gui.back_pixel, 0);
2388 else
2390 #endif
2391 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2392 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2393 && gui.cursor_col > 0)
2394 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2395 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2396 #ifdef FEAT_HANGULIN
2397 if (composing_hangul)
2398 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2399 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2401 #endif
2402 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2403 * here in case it wasn't needed to undraw it. */
2404 gui.cursor_is_valid = FALSE;
2408 void
2409 gui_redraw(x, y, w, h)
2410 int x;
2411 int y;
2412 int w;
2413 int h;
2415 int row1, col1, row2, col2;
2417 row1 = Y_2_ROW(y);
2418 col1 = X_2_COL(x);
2419 row2 = Y_2_ROW(y + h - 1);
2420 col2 = X_2_COL(x + w - 1);
2422 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2425 * We may need to redraw the cursor, but don't take it upon us to change
2426 * its location after a scroll.
2427 * (maybe be more strict even and test col too?)
2428 * These things may be outside the update/clipping region and reality may
2429 * not reflect Vims internal ideas if these operations are clipped away.
2431 if (gui.row == gui.cursor_row)
2432 gui_update_cursor(TRUE, TRUE);
2436 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2437 * from col1 to col2 (inclusive).
2438 * Return TRUE when the character before the first drawn character has
2439 * different attributes (may have to be redrawn too).
2442 gui_redraw_block(row1, col1, row2, col2, flags)
2443 int row1;
2444 int col1;
2445 int row2;
2446 int col2;
2447 int flags; /* flags for gui_outstr_nowrap() */
2449 int old_row, old_col;
2450 long_u old_hl_mask;
2451 int off;
2452 sattr_T first_attr;
2453 int idx, len;
2454 int back, nback;
2455 int retval = FALSE;
2456 #ifdef FEAT_MBYTE
2457 int orig_col1, orig_col2;
2458 #endif
2460 /* Don't try to update when ScreenLines is not valid */
2461 if (!screen_cleared || ScreenLines == NULL)
2462 return retval;
2464 /* Don't try to draw outside the shell! */
2465 /* Check everything, strange values may be caused by a big border width */
2466 col1 = check_col(col1);
2467 col2 = check_col(col2);
2468 row1 = check_row(row1);
2469 row2 = check_row(row2);
2471 /* Remember where our cursor was */
2472 old_row = gui.row;
2473 old_col = gui.col;
2474 old_hl_mask = gui.highlight_mask;
2475 #ifdef FEAT_MBYTE
2476 orig_col1 = col1;
2477 orig_col2 = col2;
2478 #endif
2480 for (gui.row = row1; gui.row <= row2; gui.row++)
2482 #ifdef FEAT_MBYTE
2483 /* When only half of a double-wide character is in the block, include
2484 * the other half. */
2485 col1 = orig_col1;
2486 col2 = orig_col2;
2487 off = LineOffset[gui.row];
2488 if (enc_dbcs != 0)
2490 if (col1 > 0)
2491 col1 -= dbcs_screen_head_off(ScreenLines + off,
2492 ScreenLines + off + col1);
2493 col2 += dbcs_screen_tail_off(ScreenLines + off,
2494 ScreenLines + off + col2);
2496 else if (enc_utf8)
2498 if (ScreenLines[off + col1] == 0)
2499 --col1;
2500 # ifdef HAVE_GTK2
2501 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2502 ++col2;
2503 # endif
2505 #endif
2506 gui.col = col1;
2507 off = LineOffset[gui.row] + gui.col;
2508 len = col2 - col1 + 1;
2510 /* Find how many chars back this highlighting starts, or where a space
2511 * is. Needed for when the bold trick is used */
2512 for (back = 0; back < col1; ++back)
2513 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2514 || ScreenLines[off - 1 - back] == ' ')
2515 break;
2516 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2517 && ScreenLines[off - 1] != ' ');
2519 /* Break it up in strings of characters with the same attributes. */
2520 /* Print UTF-8 characters individually. */
2521 while (len > 0)
2523 first_attr = ScreenAttrs[off];
2524 gui.highlight_mask = first_attr;
2525 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2526 if (enc_utf8 && ScreenLinesUC[off] != 0)
2528 /* output multi-byte character separately */
2529 nback = gui_screenchar(off, flags,
2530 (guicolor_T)0, (guicolor_T)0, back);
2531 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2532 idx = 2;
2533 else
2534 idx = 1;
2536 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2538 /* output double-byte, single-width character separately */
2539 nback = gui_screenchar(off, flags,
2540 (guicolor_T)0, (guicolor_T)0, back);
2541 idx = 1;
2543 else
2544 #endif
2546 #ifdef HAVE_GTK2
2547 for (idx = 0; idx < len; ++idx)
2549 if (enc_utf8 && ScreenLines[off + idx] == 0)
2550 continue; /* skip second half of double-width char */
2551 if (ScreenAttrs[off + idx] != first_attr)
2552 break;
2554 /* gui_screenstr() takes care of multibyte chars */
2555 nback = gui_screenstr(off, idx, flags,
2556 (guicolor_T)0, (guicolor_T)0, back);
2557 #else
2558 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2559 idx++)
2561 # ifdef FEAT_MBYTE
2562 /* Stop at a multi-byte Unicode character. */
2563 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2564 break;
2565 if (enc_dbcs == DBCS_JPNU)
2567 /* Stop at a double-byte single-width char. */
2568 if (ScreenLines[off + idx] == 0x8e)
2569 break;
2570 if (len > 1 && (*mb_ptr2len)(ScreenLines
2571 + off + idx) == 2)
2572 ++idx; /* skip second byte of double-byte char */
2574 # endif
2576 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2577 (guicolor_T)0, (guicolor_T)0, back);
2578 #endif
2580 if (nback == FAIL)
2582 /* Must back up to start drawing where a bold or italic word
2583 * starts. */
2584 off -= back;
2585 len += back;
2586 gui.col -= back;
2588 else
2590 off += idx;
2591 len -= idx;
2593 back = 0;
2597 /* Put the cursor back where it was */
2598 gui.row = old_row;
2599 gui.col = old_col;
2600 gui.highlight_mask = (int)old_hl_mask;
2602 return retval;
2605 static void
2606 gui_delete_lines(row, count)
2607 int row;
2608 int count;
2610 if (count <= 0)
2611 return;
2613 if (row + count > gui.scroll_region_bot)
2614 /* Scrolled out of region, just blank the lines out */
2615 gui_clear_block(row, gui.scroll_region_left,
2616 gui.scroll_region_bot, gui.scroll_region_right);
2617 else
2619 gui_mch_delete_lines(row, count);
2621 /* If the cursor was in the deleted lines it's now gone. If the
2622 * cursor was in the scrolled lines adjust its position. */
2623 if (gui.cursor_row >= row
2624 && gui.cursor_col >= gui.scroll_region_left
2625 && gui.cursor_col <= gui.scroll_region_right)
2627 if (gui.cursor_row < row + count)
2628 gui.cursor_is_valid = FALSE;
2629 else if (gui.cursor_row <= gui.scroll_region_bot)
2630 gui.cursor_row -= count;
2635 static void
2636 gui_insert_lines(row, count)
2637 int row;
2638 int count;
2640 if (count <= 0)
2641 return;
2643 if (row + count > gui.scroll_region_bot)
2644 /* Scrolled out of region, just blank the lines out */
2645 gui_clear_block(row, gui.scroll_region_left,
2646 gui.scroll_region_bot, gui.scroll_region_right);
2647 else
2649 gui_mch_insert_lines(row, count);
2651 if (gui.cursor_row >= gui.row
2652 && gui.cursor_col >= gui.scroll_region_left
2653 && gui.cursor_col <= gui.scroll_region_right)
2655 if (gui.cursor_row <= gui.scroll_region_bot - count)
2656 gui.cursor_row += count;
2657 else if (gui.cursor_row <= gui.scroll_region_bot)
2658 gui.cursor_is_valid = FALSE;
2664 * The main GUI input routine. Waits for a character from the keyboard.
2665 * wtime == -1 Wait forever.
2666 * wtime == 0 Don't wait.
2667 * wtime > 0 Wait wtime milliseconds for a character.
2668 * Returns OK if a character was found to be available within the given time,
2669 * or FAIL otherwise.
2672 gui_wait_for_chars(wtime)
2673 long wtime;
2675 int retval;
2678 * If we're going to wait a bit, update the menus and mouse shape for the
2679 * current State.
2681 if (wtime != 0)
2683 #ifdef FEAT_MENU
2684 gui_update_menus(0);
2685 #endif
2688 gui_mch_update();
2689 if (input_available()) /* Got char, return immediately */
2690 return OK;
2691 if (wtime == 0) /* Don't wait for char */
2692 return FAIL;
2694 /* Before waiting, flush any output to the screen. */
2695 gui_mch_flush();
2697 if (wtime > 0)
2699 /* Blink when waiting for a character. Probably only does something
2700 * for showmatch() */
2701 gui_mch_start_blink();
2702 retval = gui_mch_wait_for_chars(wtime);
2703 gui_mch_stop_blink();
2704 return retval;
2708 * While we are waiting indefinitely for a character, blink the cursor.
2710 gui_mch_start_blink();
2712 retval = FAIL;
2714 * We may want to trigger the CursorHold event. First wait for
2715 * 'updatetime' and if nothing is typed within that time put the
2716 * K_CURSORHOLD key in the input buffer.
2718 if (gui_mch_wait_for_chars(p_ut) == OK)
2719 retval = OK;
2720 #ifdef FEAT_AUTOCMD
2721 else if (trigger_cursorhold())
2723 char_u buf[3];
2725 /* Put K_CURSORHOLD in the input buffer. */
2726 buf[0] = CSI;
2727 buf[1] = KS_EXTRA;
2728 buf[2] = (int)KE_CURSORHOLD;
2729 add_to_input_buf(buf, 3);
2731 retval = OK;
2733 #endif
2735 if (retval == FAIL)
2737 /* Blocking wait. */
2738 before_blocking();
2739 retval = gui_mch_wait_for_chars(-1L);
2742 gui_mch_stop_blink();
2743 return retval;
2747 * Fill p[4] with mouse coordinates encoded for check_termcode().
2749 static void
2750 fill_mouse_coord(p, col, row)
2751 char_u *p;
2752 int col;
2753 int row;
2755 p[0] = (char_u)(col / 128 + ' ' + 1);
2756 p[1] = (char_u)(col % 128 + ' ' + 1);
2757 p[2] = (char_u)(row / 128 + ' ' + 1);
2758 p[3] = (char_u)(row % 128 + ' ' + 1);
2762 * Generic mouse support function. Add a mouse event to the input buffer with
2763 * the given properties.
2764 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2765 * MOUSE_X1, MOUSE_X2
2766 * MOUSE_DRAG, or MOUSE_RELEASE.
2767 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2768 * x, y --- Coordinates of mouse in pixels.
2769 * repeated_click --- TRUE if this click comes only a short time after a
2770 * previous click.
2771 * modifiers --- Bit field which may be any of the following modifiers
2772 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2773 * This function will ignore drag events where the mouse has not moved to a new
2774 * character.
2776 void
2777 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2778 int button;
2779 int x;
2780 int y;
2781 int repeated_click;
2782 int_u modifiers;
2784 static int prev_row = 0, prev_col = 0;
2785 static int prev_button = -1;
2786 static int num_clicks = 1;
2787 char_u string[10];
2788 enum key_extra button_char;
2789 int row, col;
2790 #ifdef FEAT_CLIPBOARD
2791 int checkfor;
2792 int did_clip = FALSE;
2793 #endif
2796 * Scrolling may happen at any time, also while a selection is present.
2798 switch (button)
2800 case MOUSE_X1:
2801 button_char = KE_X1MOUSE;
2802 goto button_set;
2803 case MOUSE_X2:
2804 button_char = KE_X2MOUSE;
2805 goto button_set;
2806 case MOUSE_4:
2807 button_char = KE_MOUSEDOWN;
2808 goto button_set;
2809 case MOUSE_5:
2810 button_char = KE_MOUSEUP;
2811 button_set:
2813 /* Don't put events in the input queue now. */
2814 if (hold_gui_events)
2815 return;
2817 string[3] = CSI;
2818 string[4] = KS_EXTRA;
2819 string[5] = (int)button_char;
2821 /* Pass the pointer coordinates of the scroll event so that we
2822 * know which window to scroll. */
2823 row = gui_xy2colrow(x, y, &col);
2824 string[6] = (char_u)(col / 128 + ' ' + 1);
2825 string[7] = (char_u)(col % 128 + ' ' + 1);
2826 string[8] = (char_u)(row / 128 + ' ' + 1);
2827 string[9] = (char_u)(row % 128 + ' ' + 1);
2829 if (modifiers == 0)
2830 add_to_input_buf(string + 3, 7);
2831 else
2833 string[0] = CSI;
2834 string[1] = KS_MODIFIER;
2835 string[2] = 0;
2836 if (modifiers & MOUSE_SHIFT)
2837 string[2] |= MOD_MASK_SHIFT;
2838 if (modifiers & MOUSE_CTRL)
2839 string[2] |= MOD_MASK_CTRL;
2840 if (modifiers & MOUSE_ALT)
2841 string[2] |= MOD_MASK_ALT;
2842 add_to_input_buf(string, 10);
2844 return;
2848 #ifdef FEAT_CLIPBOARD
2849 /* If a clipboard selection is in progress, handle it */
2850 if (clip_star.state == SELECT_IN_PROGRESS)
2852 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2853 return;
2856 /* Determine which mouse settings to look for based on the current mode */
2857 switch (get_real_state())
2859 case NORMAL_BUSY:
2860 case OP_PENDING:
2861 case NORMAL: checkfor = MOUSE_NORMAL; break;
2862 case VISUAL: checkfor = MOUSE_VISUAL; break;
2863 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2864 case REPLACE:
2865 case REPLACE+LANGMAP:
2866 #ifdef FEAT_VREPLACE
2867 case VREPLACE:
2868 case VREPLACE+LANGMAP:
2869 #endif
2870 case INSERT:
2871 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2872 case ASKMORE:
2873 case HITRETURN: /* At the more- and hit-enter prompt pass the
2874 mouse event for a click on or below the
2875 message line. */
2876 if (Y_2_ROW(y) >= msg_row)
2877 checkfor = MOUSE_NORMAL;
2878 else
2879 checkfor = MOUSE_RETURN;
2880 break;
2883 * On the command line, use the clipboard selection on all lines
2884 * but the command line. But not when pasting.
2886 case CMDLINE:
2887 case CMDLINE+LANGMAP:
2888 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2889 checkfor = MOUSE_NONE;
2890 else
2891 checkfor = MOUSE_COMMAND;
2892 break;
2894 default:
2895 checkfor = MOUSE_NONE;
2896 break;
2900 * Allow clipboard selection of text on the command line in "normal"
2901 * modes. Don't do this when dragging the status line, or extending a
2902 * Visual selection.
2904 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2905 && Y_2_ROW(y) >= topframe->fr_height
2906 # ifdef FEAT_WINDOWS
2907 + firstwin->w_winrow
2908 # endif
2909 && button != MOUSE_DRAG
2910 # ifdef FEAT_MOUSESHAPE
2911 && !drag_status_line
2912 # ifdef FEAT_VERTSPLIT
2913 && !drag_sep_line
2914 # endif
2915 # endif
2917 checkfor = MOUSE_NONE;
2920 * Use modeless selection when holding CTRL and SHIFT pressed.
2922 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2923 checkfor = MOUSE_NONEF;
2926 * In Ex mode, always use modeless selection.
2928 if (exmode_active)
2929 checkfor = MOUSE_NONE;
2932 * If the mouse settings say to not use the mouse, use the modeless
2933 * selection. But if Visual is active, assume that only the Visual area
2934 * will be selected.
2935 * Exception: On the command line, both the selection is used and a mouse
2936 * key is send.
2938 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2940 #ifdef FEAT_VISUAL
2941 /* Don't do modeless selection in Visual mode. */
2942 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2943 return;
2944 #endif
2947 * When 'mousemodel' is "popup", shift-left is translated to right.
2948 * But not when also using Ctrl.
2950 if (mouse_model_popup() && button == MOUSE_LEFT
2951 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2953 button = MOUSE_RIGHT;
2954 modifiers &= ~ MOUSE_SHIFT;
2957 /* If the selection is done, allow the right button to extend it.
2958 * If the selection is cleared, allow the right button to start it
2959 * from the cursor position. */
2960 if (button == MOUSE_RIGHT)
2962 if (clip_star.state == SELECT_CLEARED)
2964 if (State & CMDLINE)
2966 col = msg_col;
2967 row = msg_row;
2969 else
2971 col = curwin->w_wcol;
2972 row = curwin->w_wrow + W_WINROW(curwin);
2974 clip_start_selection(col, row, FALSE);
2976 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2977 repeated_click);
2978 did_clip = TRUE;
2980 /* Allow the left button to start the selection */
2981 else if (button ==
2982 # ifdef RISCOS
2983 /* Only start a drag on a drag event. Otherwise
2984 * we don't get a release event. */
2985 MOUSE_DRAG
2986 # else
2987 MOUSE_LEFT
2988 # endif
2991 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2992 did_clip = TRUE;
2994 # ifdef RISCOS
2995 else if (button == MOUSE_LEFT)
2997 clip_clear_selection();
2998 did_clip = TRUE;
3000 # endif
3002 /* Always allow pasting */
3003 if (button != MOUSE_MIDDLE)
3005 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3006 return;
3007 if (checkfor != MOUSE_COMMAND)
3008 button = MOUSE_LEFT;
3010 repeated_click = FALSE;
3013 if (clip_star.state != SELECT_CLEARED && !did_clip)
3014 clip_clear_selection();
3015 #endif
3017 /* Don't put events in the input queue now. */
3018 if (hold_gui_events)
3019 return;
3021 row = gui_xy2colrow(x, y, &col);
3024 * If we are dragging and the mouse hasn't moved far enough to be on a
3025 * different character, then don't send an event to vim.
3027 if (button == MOUSE_DRAG)
3029 if (row == prev_row && col == prev_col)
3030 return;
3031 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3032 if (y < 0)
3033 row = -1;
3037 * If topline has changed (window scrolled) since the last click, reset
3038 * repeated_click, because we don't want starting Visual mode when
3039 * clicking on a different character in the text.
3041 if (curwin->w_topline != gui_prev_topline
3042 #ifdef FEAT_DIFF
3043 || curwin->w_topfill != gui_prev_topfill
3044 #endif
3046 repeated_click = FALSE;
3048 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3049 string[1] = KS_MOUSE;
3050 string[2] = KE_FILLER;
3051 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3053 if (repeated_click)
3056 * Handle multiple clicks. They only count if the mouse is still
3057 * pointing at the same character.
3059 if (button != prev_button || row != prev_row || col != prev_col)
3060 num_clicks = 1;
3061 else if (++num_clicks > 4)
3062 num_clicks = 1;
3064 else
3065 num_clicks = 1;
3066 prev_button = button;
3067 gui_prev_topline = curwin->w_topline;
3068 #ifdef FEAT_DIFF
3069 gui_prev_topfill = curwin->w_topfill;
3070 #endif
3072 string[3] = (char_u)(button | 0x20);
3073 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3075 else
3076 string[3] = (char_u)button;
3078 string[3] |= modifiers;
3079 fill_mouse_coord(string + 4, col, row);
3080 add_to_input_buf(string, 8);
3082 if (row < 0)
3083 prev_row = 0;
3084 else
3085 prev_row = row;
3086 prev_col = col;
3089 * We need to make sure this is cleared since Athena doesn't tell us when
3090 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3092 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3093 gui.dragged_sb = SBAR_NONE;
3094 #endif
3098 * Convert x and y coordinate to column and row in text window.
3099 * Corrects for multi-byte character.
3100 * returns column in "*colp" and row as return value;
3103 gui_xy2colrow(x, y, colp)
3104 int x;
3105 int y;
3106 int *colp;
3108 int col = check_col(X_2_COL(x));
3109 int row = check_row(Y_2_ROW(y));
3111 #ifdef FEAT_MBYTE
3112 *colp = mb_fix_col(col, row);
3113 #else
3114 *colp = col;
3115 #endif
3116 return row;
3119 #if defined(FEAT_MENU) || defined(PROTO)
3121 * Callback function for when a menu entry has been selected.
3123 void
3124 gui_menu_cb(menu)
3125 vimmenu_T *menu;
3127 char_u bytes[sizeof(long_u)];
3129 /* Don't put events in the input queue now. */
3130 if (hold_gui_events)
3131 return;
3133 bytes[0] = CSI;
3134 bytes[1] = KS_MENU;
3135 bytes[2] = KE_FILLER;
3136 add_to_input_buf(bytes, 3);
3137 add_long_to_buf((long_u)menu, bytes);
3138 add_to_input_buf_csi(bytes, sizeof(long_u));
3140 #endif
3142 static int prev_which_scrollbars[3];
3145 * Set which components are present.
3146 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3147 * in p_go.
3149 void
3150 gui_init_which_components(oldval)
3151 char_u *oldval UNUSED;
3153 #ifdef FEAT_MENU
3154 static int prev_menu_is_active = -1;
3155 #endif
3156 #ifdef FEAT_TOOLBAR
3157 static int prev_toolbar = -1;
3158 int using_toolbar = FALSE;
3159 #endif
3160 #ifdef FEAT_GUI_TABLINE
3161 int using_tabline;
3162 #endif
3163 #ifdef FEAT_FOOTER
3164 static int prev_footer = -1;
3165 int using_footer = FALSE;
3166 #endif
3167 #if defined(FEAT_MENU) && !defined(WIN16)
3168 static int prev_tearoff = -1;
3169 int using_tearoff = FALSE;
3170 #endif
3172 char_u *p;
3173 int i;
3174 #ifdef FEAT_MENU
3175 int grey_old, grey_new;
3176 char_u *temp;
3177 #endif
3178 win_T *wp;
3179 int need_set_size;
3180 int fix_size;
3182 #ifdef FEAT_MENU
3183 if (oldval != NULL && gui.in_use)
3186 * Check if the menu's go from grey to non-grey or vise versa.
3188 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3189 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3190 if (grey_old != grey_new)
3192 temp = p_go;
3193 p_go = oldval;
3194 gui_update_menus(MENU_ALL_MODES);
3195 p_go = temp;
3198 gui.menu_is_active = FALSE;
3199 #endif
3201 for (i = 0; i < 3; i++)
3202 gui.which_scrollbars[i] = FALSE;
3203 for (p = p_go; *p; p++)
3204 switch (*p)
3206 case GO_LEFT:
3207 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3208 break;
3209 case GO_RIGHT:
3210 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3211 break;
3212 #ifdef FEAT_VERTSPLIT
3213 case GO_VLEFT:
3214 if (win_hasvertsplit())
3215 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3216 break;
3217 case GO_VRIGHT:
3218 if (win_hasvertsplit())
3219 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3220 break;
3221 #endif
3222 case GO_BOT:
3223 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3224 break;
3225 #ifdef FEAT_MENU
3226 case GO_MENUS:
3227 gui.menu_is_active = TRUE;
3228 break;
3229 #endif
3230 case GO_GREY:
3231 /* make menu's have grey items, ignored here */
3232 break;
3233 #ifdef FEAT_TOOLBAR
3234 case GO_TOOLBAR:
3235 using_toolbar = TRUE;
3236 break;
3237 #endif
3238 #ifdef FEAT_FOOTER
3239 case GO_FOOTER:
3240 using_footer = TRUE;
3241 break;
3242 #endif
3243 case GO_TEAROFF:
3244 #if defined(FEAT_MENU) && !defined(WIN16)
3245 using_tearoff = TRUE;
3246 #endif
3247 break;
3248 default:
3249 /* Ignore options that are not supported */
3250 break;
3253 if (gui.in_use)
3255 need_set_size = 0;
3256 fix_size = FALSE;
3258 #ifdef FEAT_GUI_TABLINE
3259 /* Update the GUI tab line, it may appear or disappear. This may
3260 * cause the non-GUI tab line to disappear or appear. */
3261 using_tabline = gui_has_tabline();
3262 if (!gui_mch_showing_tabline() != !using_tabline)
3264 /* We don't want a resize event change "Rows" here, save and
3265 * restore it. Resizing is handled below. */
3266 i = Rows;
3267 gui_update_tabline();
3268 Rows = i;
3269 need_set_size |= RESIZE_VERT;
3270 if (using_tabline)
3271 fix_size = TRUE;
3272 if (!gui_use_tabline())
3273 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3275 #endif
3277 for (i = 0; i < 3; i++)
3279 /* The scrollbar needs to be updated when it is shown/unshown and
3280 * when switching tab pages. But the size only changes when it's
3281 * shown/unshown. Thus we need two places to remember whether a
3282 * scrollbar is there or not. */
3283 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3284 #ifdef FEAT_WINDOWS
3285 || gui.which_scrollbars[i]
3286 != curtab->tp_prev_which_scrollbars[i]
3287 #endif
3290 if (i == SBAR_BOTTOM)
3291 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3292 gui.which_scrollbars[i]);
3293 else
3295 FOR_ALL_WINDOWS(wp)
3297 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3300 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3302 if (i == SBAR_BOTTOM)
3303 need_set_size |= RESIZE_VERT;
3304 else
3305 need_set_size |= RESIZE_HOR;
3306 if (gui.which_scrollbars[i])
3307 fix_size = TRUE;
3310 #ifdef FEAT_WINDOWS
3311 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3312 #endif
3313 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3316 #ifdef FEAT_MENU
3317 if (gui.menu_is_active != prev_menu_is_active)
3319 /* We don't want a resize event change "Rows" here, save and
3320 * restore it. Resizing is handled below. */
3321 i = Rows;
3322 gui_mch_enable_menu(gui.menu_is_active);
3323 Rows = i;
3324 prev_menu_is_active = gui.menu_is_active;
3325 need_set_size |= RESIZE_VERT;
3326 if (gui.menu_is_active)
3327 fix_size = TRUE;
3329 #endif
3331 #ifdef FEAT_TOOLBAR
3332 if (using_toolbar != prev_toolbar)
3334 gui_mch_show_toolbar(using_toolbar);
3335 prev_toolbar = using_toolbar;
3336 need_set_size |= RESIZE_VERT;
3337 if (using_toolbar)
3338 fix_size = TRUE;
3340 #endif
3341 #ifdef FEAT_FOOTER
3342 if (using_footer != prev_footer)
3344 gui_mch_enable_footer(using_footer);
3345 prev_footer = using_footer;
3346 need_set_size |= RESIZE_VERT;
3347 if (using_footer)
3348 fix_size = TRUE;
3350 #endif
3351 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3352 if (using_tearoff != prev_tearoff)
3354 gui_mch_toggle_tearoffs(using_tearoff);
3355 prev_tearoff = using_tearoff;
3357 #endif
3358 if (need_set_size != 0)
3360 #ifdef FEAT_GUI_GTK
3361 long prev_Columns = Columns;
3362 long prev_Rows = Rows;
3363 #endif
3364 /* Adjust the size of the window to make the text area keep the
3365 * same size and to avoid that part of our window is off-screen
3366 * and a scrollbar can't be used, for example. */
3367 gui_set_shellsize(FALSE, fix_size, need_set_size);
3369 #ifdef FEAT_GUI_GTK
3370 /* GTK has the annoying habit of sending us resize events when
3371 * changing the window size ourselves. This mostly happens when
3372 * waiting for a character to arrive, quite unpredictably, and may
3373 * change Columns and Rows when we don't want it. Wait for a
3374 * character here to avoid this effect.
3375 * If you remove this, please test this command for resizing
3376 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3377 * Don't do this while starting up though.
3378 * Don't change Rows when adding menu/toolbar/tabline.
3379 * Don't change Columns when adding vertical toolbar. */
3380 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3381 (void)char_avail();
3382 if ((need_set_size & RESIZE_VERT) == 0)
3383 Rows = prev_Rows;
3384 if ((need_set_size & RESIZE_HOR) == 0)
3385 Columns = prev_Columns;
3386 #endif
3388 #ifdef FEAT_WINDOWS
3389 /* When the console tabline appears or disappears the window positions
3390 * change. */
3391 if (firstwin->w_winrow != tabline_height())
3392 shell_new_rows(); /* recompute window positions and heights */
3393 #endif
3397 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3399 * Return TRUE if the GUI is taking care of the tabline.
3400 * It may still be hidden if 'showtabline' is zero.
3403 gui_use_tabline()
3405 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3409 * Return TRUE if the GUI is showing the tabline.
3410 * This uses 'showtabline'.
3412 static int
3413 gui_has_tabline()
3415 if (!gui_use_tabline()
3416 || p_stal == 0
3417 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3418 return FALSE;
3419 return TRUE;
3423 * Update the tabline.
3424 * This may display/undisplay the tabline and update the labels.
3426 void
3427 gui_update_tabline()
3429 int showit = gui_has_tabline();
3430 int shown = gui_mch_showing_tabline();
3432 if (!gui.starting && starting == 0)
3434 /* Updating the tabline uses direct GUI commands, flush
3435 * outstanding instructions first. (esp. clear screen) */
3436 out_flush();
3437 gui_mch_flush();
3439 if (!showit != !shown)
3440 gui_mch_show_tabline(showit);
3441 if (showit != 0)
3442 gui_mch_update_tabline();
3444 /* When the tabs change from hidden to shown or from shown to
3445 * hidden the size of the text area should remain the same. */
3446 if (!showit != !shown)
3447 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3452 * Get the label or tooltip for tab page "tp" into NameBuff[].
3454 void
3455 get_tabline_label(tp, tooltip)
3456 tabpage_T *tp;
3457 int tooltip; /* TRUE: get tooltip */
3459 int modified = FALSE;
3460 char_u buf[40];
3461 int wincount;
3462 win_T *wp;
3463 char_u **opt;
3465 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3466 opt = (tooltip ? &p_gtt : &p_gtl);
3467 if (**opt != NUL)
3469 int use_sandbox = FALSE;
3470 int save_called_emsg = called_emsg;
3471 char_u res[MAXPATHL];
3472 tabpage_T *save_curtab;
3473 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3474 : "guitablabel");
3476 called_emsg = FALSE;
3478 printer_page_num = tabpage_index(tp);
3479 # ifdef FEAT_EVAL
3480 set_vim_var_nr(VV_LNUM, printer_page_num);
3481 use_sandbox = was_set_insecurely(opt_name, 0);
3482 # endif
3483 /* It's almost as going to the tabpage, but without autocommands. */
3484 curtab->tp_firstwin = firstwin;
3485 curtab->tp_lastwin = lastwin;
3486 curtab->tp_curwin = curwin;
3487 save_curtab = curtab;
3488 curtab = tp;
3489 topframe = curtab->tp_topframe;
3490 firstwin = curtab->tp_firstwin;
3491 lastwin = curtab->tp_lastwin;
3492 curwin = curtab->tp_curwin;
3493 curbuf = curwin->w_buffer;
3495 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3496 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3497 0, (int)Columns, NULL, NULL);
3498 STRCPY(NameBuff, res);
3500 /* Back to the original curtab. */
3501 curtab = save_curtab;
3502 topframe = curtab->tp_topframe;
3503 firstwin = curtab->tp_firstwin;
3504 lastwin = curtab->tp_lastwin;
3505 curwin = curtab->tp_curwin;
3506 curbuf = curwin->w_buffer;
3508 if (called_emsg)
3509 set_string_option_direct(opt_name, -1,
3510 (char_u *)"", OPT_FREE, SID_ERROR);
3511 called_emsg |= save_called_emsg;
3514 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3515 * use a default label. */
3516 if (**opt == NUL || *NameBuff == NUL)
3518 /* Get the buffer name into NameBuff[] and shorten it. */
3519 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3520 if (!tooltip)
3521 shorten_dir(NameBuff);
3523 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3524 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3525 if (bufIsChanged(wp->w_buffer))
3526 modified = TRUE;
3527 if (modified || wincount > 1)
3529 if (wincount > 1)
3530 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3531 else
3532 buf[0] = NUL;
3533 if (modified)
3534 STRCAT(buf, "+");
3535 STRCAT(buf, " ");
3536 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
3537 mch_memmove(NameBuff, buf, STRLEN(buf));
3543 * Send the event for clicking to select tab page "nr".
3544 * Returns TRUE if it was done, FALSE when skipped because we are already at
3545 * that tab page or the cmdline window is open.
3548 send_tabline_event(nr)
3549 int nr;
3551 char_u string[3];
3553 if (nr == tabpage_index(curtab))
3554 return FALSE;
3556 /* Don't put events in the input queue now. */
3557 if (hold_gui_events
3558 # ifdef FEAT_CMDWIN
3559 || cmdwin_type != 0
3560 # endif
3563 /* Set it back to the current tab page. */
3564 gui_mch_set_curtab(tabpage_index(curtab));
3565 return FALSE;
3568 string[0] = CSI;
3569 string[1] = KS_TABLINE;
3570 string[2] = KE_FILLER;
3571 add_to_input_buf(string, 3);
3572 string[0] = nr;
3573 add_to_input_buf_csi(string, 1);
3574 return TRUE;
3578 * Send a tabline menu event
3580 void
3581 send_tabline_menu_event(tabidx, event)
3582 int tabidx;
3583 int event;
3585 char_u string[3];
3587 /* Don't put events in the input queue now. */
3588 if (hold_gui_events)
3589 return;
3591 string[0] = CSI;
3592 string[1] = KS_TABMENU;
3593 string[2] = KE_FILLER;
3594 add_to_input_buf(string, 3);
3595 string[0] = tabidx;
3596 string[1] = (char_u)(long)event;
3597 add_to_input_buf_csi(string, 2);
3600 #endif
3603 * Scrollbar stuff:
3606 #if defined(FEAT_WINDOWS) || defined(PROTO)
3608 * Remove all scrollbars. Used before switching to another tab page.
3610 void
3611 gui_remove_scrollbars()
3613 int i;
3614 win_T *wp;
3616 for (i = 0; i < 3; i++)
3618 if (i == SBAR_BOTTOM)
3619 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3620 else
3622 FOR_ALL_WINDOWS(wp)
3624 gui_do_scrollbar(wp, i, FALSE);
3627 curtab->tp_prev_which_scrollbars[i] = -1;
3630 #endif
3632 void
3633 gui_create_scrollbar(sb, type, wp)
3634 scrollbar_T *sb;
3635 int type;
3636 win_T *wp;
3638 static int sbar_ident = 0;
3640 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3641 sb->wp = wp;
3642 sb->type = type;
3643 sb->value = 0;
3644 #ifdef FEAT_GUI_ATHENA
3645 sb->pixval = 0;
3646 #endif
3647 sb->size = 1;
3648 sb->max = 1;
3649 sb->top = 0;
3650 sb->height = 0;
3651 #ifdef FEAT_VERTSPLIT
3652 sb->width = 0;
3653 #endif
3654 sb->status_height = 0;
3655 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3659 * Find the scrollbar with the given index.
3661 scrollbar_T *
3662 gui_find_scrollbar(ident)
3663 long ident;
3665 win_T *wp;
3667 if (gui.bottom_sbar.ident == ident)
3668 return &gui.bottom_sbar;
3669 FOR_ALL_WINDOWS(wp)
3671 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3672 return &wp->w_scrollbars[SBAR_LEFT];
3673 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3674 return &wp->w_scrollbars[SBAR_RIGHT];
3676 return NULL;
3680 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3682 * For Win32, Macintosh and GTK+ 2:
3683 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3684 * you stop dragging the scrollbar. We get here each time the scrollbar is
3685 * dragged another pixel, but as far as the rest of vim goes, it thinks
3686 * we're just hanging in the call to DispatchMessage() in
3687 * process_message(). The DispatchMessage() call that hangs was passed a
3688 * mouse button click event in the scrollbar window. -- webb.
3690 * Solution: Do the scrolling right here. But only when allowed.
3691 * Ignore the scrollbars while executing an external command or when there
3692 * are still characters to be processed.
3694 void
3695 gui_drag_scrollbar(sb, value, still_dragging)
3696 scrollbar_T *sb;
3697 long value;
3698 int still_dragging;
3700 #ifdef FEAT_WINDOWS
3701 win_T *wp;
3702 #endif
3703 int sb_num;
3704 #ifdef USE_ON_FLY_SCROLL
3705 colnr_T old_leftcol = curwin->w_leftcol;
3706 # ifdef FEAT_SCROLLBIND
3707 linenr_T old_topline = curwin->w_topline;
3708 # endif
3709 # ifdef FEAT_DIFF
3710 int old_topfill = curwin->w_topfill;
3711 # endif
3712 #else
3713 char_u bytes[sizeof(long_u)];
3714 int byte_count;
3715 #endif
3717 if (sb == NULL)
3718 return;
3720 /* Don't put events in the input queue now. */
3721 if (hold_gui_events)
3722 return;
3724 #ifdef FEAT_CMDWIN
3725 if (cmdwin_type != 0 && sb->wp != curwin)
3726 return;
3727 #endif
3729 if (still_dragging)
3731 if (sb->wp == NULL)
3732 gui.dragged_sb = SBAR_BOTTOM;
3733 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3734 gui.dragged_sb = SBAR_LEFT;
3735 else
3736 gui.dragged_sb = SBAR_RIGHT;
3737 gui.dragged_wp = sb->wp;
3739 else
3741 gui.dragged_sb = SBAR_NONE;
3742 #ifdef HAVE_GTK2
3743 /* Keep the "dragged_wp" value until after the scrolling, for when the
3744 * moust button is released. GTK2 doesn't send the button-up event. */
3745 gui.dragged_wp = NULL;
3746 #endif
3749 /* Vertical sbar info is kept in the first sbar (the left one) */
3750 if (sb->wp != NULL)
3751 sb = &sb->wp->w_scrollbars[0];
3754 * Check validity of value
3756 if (value < 0)
3757 value = 0;
3758 #ifdef SCROLL_PAST_END
3759 else if (value > sb->max)
3760 value = sb->max;
3761 #else
3762 if (value > sb->max - sb->size + 1)
3763 value = sb->max - sb->size + 1;
3764 #endif
3766 sb->value = value;
3768 #ifdef USE_ON_FLY_SCROLL
3769 /* When not allowed to do the scrolling right now, return.
3770 * This also checked input_available(), but that causes the first click in
3771 * a scrollbar to be ignored when Vim doesn't have focus. */
3772 if (dont_scroll)
3773 return;
3774 #endif
3775 #ifdef FEAT_INS_EXPAND
3776 /* Disallow scrolling the current window when the completion popup menu is
3777 * visible. */
3778 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3779 return;
3780 #endif
3782 #ifdef FEAT_RIGHTLEFT
3783 if (sb->wp == NULL && curwin->w_p_rl)
3785 value = sb->max + 1 - sb->size - value;
3786 if (value < 0)
3787 value = 0;
3789 #endif
3791 if (sb->wp != NULL) /* vertical scrollbar */
3793 sb_num = 0;
3794 #ifdef FEAT_WINDOWS
3795 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3796 sb_num++;
3797 if (wp == NULL)
3798 return;
3799 #else
3800 if (sb->wp != curwin)
3801 return;
3802 #endif
3804 #ifdef USE_ON_FLY_SCROLL
3805 current_scrollbar = sb_num;
3806 scrollbar_value = value;
3807 if (State & NORMAL)
3809 gui_do_scroll();
3810 setcursor();
3812 else if (State & INSERT)
3814 ins_scroll();
3815 setcursor();
3817 else if (State & CMDLINE)
3819 if (msg_scrolled == 0)
3821 gui_do_scroll();
3822 redrawcmdline();
3825 # ifdef FEAT_FOLDING
3826 /* Value may have been changed for closed fold. */
3827 sb->value = sb->wp->w_topline - 1;
3828 # endif
3830 /* When dragging one scrollbar and there is another one at the other
3831 * side move the thumb of that one too. */
3832 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3833 gui_mch_set_scrollbar_thumb(
3834 &sb->wp->w_scrollbars[
3835 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3836 ? SBAR_LEFT : SBAR_RIGHT],
3837 sb->value, sb->size, sb->max);
3839 #else
3840 bytes[0] = CSI;
3841 bytes[1] = KS_VER_SCROLLBAR;
3842 bytes[2] = KE_FILLER;
3843 bytes[3] = (char_u)sb_num;
3844 byte_count = 4;
3845 #endif
3847 else
3849 #ifdef USE_ON_FLY_SCROLL
3850 scrollbar_value = value;
3852 if (State & NORMAL)
3853 gui_do_horiz_scroll();
3854 else if (State & INSERT)
3855 ins_horscroll();
3856 else if (State & CMDLINE)
3858 if (msg_scrolled == 0)
3860 gui_do_horiz_scroll();
3861 redrawcmdline();
3864 if (old_leftcol != curwin->w_leftcol)
3866 updateWindow(curwin); /* update window, status and cmdline */
3867 setcursor();
3869 #else
3870 bytes[0] = CSI;
3871 bytes[1] = KS_HOR_SCROLLBAR;
3872 bytes[2] = KE_FILLER;
3873 byte_count = 3;
3874 #endif
3877 #ifdef USE_ON_FLY_SCROLL
3878 # ifdef FEAT_SCROLLBIND
3880 * synchronize other windows, as necessary according to 'scrollbind'
3882 if (curwin->w_p_scb
3883 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3884 || (sb->wp == curwin && (curwin->w_topline != old_topline
3885 # ifdef FEAT_DIFF
3886 || curwin->w_topfill != old_topfill
3887 # endif
3888 ))))
3890 do_check_scrollbind(TRUE);
3891 /* need to update the window right here */
3892 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3893 if (wp->w_redr_type > 0)
3894 updateWindow(wp);
3895 setcursor();
3897 # endif
3898 out_flush();
3899 gui_update_cursor(FALSE, TRUE);
3900 #else
3901 add_to_input_buf(bytes, byte_count);
3902 add_long_to_buf((long_u)value, bytes);
3903 add_to_input_buf_csi(bytes, sizeof(long_u));
3904 #endif
3908 * Scrollbar stuff:
3912 * Called when something in the window layout has changed.
3914 void
3915 gui_may_update_scrollbars()
3917 if (gui.in_use && starting == 0)
3919 out_flush();
3920 gui_init_which_components(NULL);
3921 gui_update_scrollbars(TRUE);
3923 need_mouse_correct = TRUE;
3926 void
3927 gui_update_scrollbars(force)
3928 int force; /* Force all scrollbars to get updated */
3930 win_T *wp;
3931 scrollbar_T *sb;
3932 long val, size, max; /* need 32 bits here */
3933 int which_sb;
3934 int h, y;
3935 #ifdef FEAT_VERTSPLIT
3936 static win_T *prev_curwin = NULL;
3937 #endif
3939 /* Update the horizontal scrollbar */
3940 gui_update_horiz_scrollbar(force);
3942 #ifndef WIN3264
3943 /* Return straight away if there is neither a left nor right scrollbar.
3944 * On MS-Windows this is required anyway for scrollwheel messages. */
3945 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3946 return;
3947 #endif
3950 * Don't want to update a scrollbar while we're dragging it. But if we
3951 * have both a left and right scrollbar, and we drag one of them, we still
3952 * need to update the other one.
3954 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3955 && gui.which_scrollbars[SBAR_LEFT]
3956 && gui.which_scrollbars[SBAR_RIGHT])
3959 * If we have two scrollbars and one of them is being dragged, just
3960 * copy the scrollbar position from the dragged one to the other one.
3962 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3963 if (gui.dragged_wp != NULL)
3964 gui_mch_set_scrollbar_thumb(
3965 &gui.dragged_wp->w_scrollbars[which_sb],
3966 gui.dragged_wp->w_scrollbars[0].value,
3967 gui.dragged_wp->w_scrollbars[0].size,
3968 gui.dragged_wp->w_scrollbars[0].max);
3971 /* avoid that moving components around generates events */
3972 ++hold_gui_events;
3974 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3976 if (wp->w_buffer == NULL) /* just in case */
3977 continue;
3978 /* Skip a scrollbar that is being dragged. */
3979 if (!force && (gui.dragged_sb == SBAR_LEFT
3980 || gui.dragged_sb == SBAR_RIGHT)
3981 && gui.dragged_wp == wp)
3982 continue;
3984 #ifdef SCROLL_PAST_END
3985 max = wp->w_buffer->b_ml.ml_line_count - 1;
3986 #else
3987 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3988 #endif
3989 if (max < 0) /* empty buffer */
3990 max = 0;
3991 val = wp->w_topline - 1;
3992 size = wp->w_height;
3993 #ifdef SCROLL_PAST_END
3994 if (val > max) /* just in case */
3995 val = max;
3996 #else
3997 if (size > max + 1) /* just in case */
3998 size = max + 1;
3999 if (val > max - size + 1)
4000 val = max - size + 1;
4001 #endif
4002 if (val < 0) /* minimal value is 0 */
4003 val = 0;
4006 * Scrollbar at index 0 (the left one) contains all the information.
4007 * It would be the same info for left and right so we just store it for
4008 * one of them.
4010 sb = &wp->w_scrollbars[0];
4013 * Note: no check for valid w_botline. If it's not valid the
4014 * scrollbars will be updated later anyway.
4016 if (size < 1 || wp->w_botline - 2 > max)
4019 * This can happen during changing files. Just don't update the
4020 * scrollbar for now.
4022 sb->height = 0; /* Force update next time */
4023 if (gui.which_scrollbars[SBAR_LEFT])
4024 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4025 if (gui.which_scrollbars[SBAR_RIGHT])
4026 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4027 continue;
4029 if (force || sb->height != wp->w_height
4030 #ifdef FEAT_WINDOWS
4031 || sb->top != wp->w_winrow
4032 || sb->status_height != wp->w_status_height
4033 # ifdef FEAT_VERTSPLIT
4034 || sb->width != wp->w_width
4035 || prev_curwin != curwin
4036 # endif
4037 #endif
4040 /* Height, width or position of scrollbar has changed. For
4041 * vertical split: curwin changed. */
4042 sb->height = wp->w_height;
4043 #ifdef FEAT_WINDOWS
4044 sb->top = wp->w_winrow;
4045 sb->status_height = wp->w_status_height;
4046 # ifdef FEAT_VERTSPLIT
4047 sb->width = wp->w_width;
4048 # endif
4049 #endif
4051 /* Calculate height and position in pixels */
4052 h = (sb->height + sb->status_height) * gui.char_height;
4053 y = sb->top * gui.char_height + gui.border_offset;
4054 #if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4055 if (gui.menu_is_active)
4056 y += gui.menu_height;
4057 #endif
4059 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4060 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4061 # ifdef FEAT_GUI_ATHENA
4062 y += gui.toolbar_height;
4063 # else
4064 # ifdef FEAT_GUI_MSWIN
4065 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4066 # endif
4067 # endif
4068 #endif
4070 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4071 if (gui_has_tabline())
4072 y += gui.tabline_height;
4073 #endif
4075 #ifdef FEAT_WINDOWS
4076 if (wp->w_winrow == 0)
4077 #endif
4079 /* Height of top scrollbar includes width of top border */
4080 h += gui.border_offset;
4081 y -= gui.border_offset;
4083 if (gui.which_scrollbars[SBAR_LEFT])
4085 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4086 gui.left_sbar_x, y,
4087 gui.scrollbar_width, h);
4088 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4090 if (gui.which_scrollbars[SBAR_RIGHT])
4092 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4093 gui.right_sbar_x, y,
4094 gui.scrollbar_width, h);
4095 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4099 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4100 * checking if the thumb moved at least a pixel. Only do this for
4101 * Athena, most other GUIs require the update anyway to make the
4102 * arrows work. */
4103 #ifdef FEAT_GUI_ATHENA
4104 if (max == 0)
4105 y = 0;
4106 else
4107 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4108 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4109 #else
4110 if (force || sb->value != val || sb->size != size || sb->max != max)
4111 #endif
4113 /* Thumb of scrollbar has moved */
4114 sb->value = val;
4115 #ifdef FEAT_GUI_ATHENA
4116 sb->pixval = y;
4117 #endif
4118 sb->size = size;
4119 sb->max = max;
4120 if (gui.which_scrollbars[SBAR_LEFT]
4121 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4122 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4123 val, size, max);
4124 if (gui.which_scrollbars[SBAR_RIGHT]
4125 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4126 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4127 val, size, max);
4130 #ifdef FEAT_VERTSPLIT
4131 prev_curwin = curwin;
4132 #endif
4133 --hold_gui_events;
4137 * Enable or disable a scrollbar.
4138 * Check for scrollbars for vertically split windows which are not enabled
4139 * sometimes.
4141 static void
4142 gui_do_scrollbar(wp, which, enable)
4143 win_T *wp;
4144 int which; /* SBAR_LEFT or SBAR_RIGHT */
4145 int enable; /* TRUE to enable scrollbar */
4147 #ifdef FEAT_VERTSPLIT
4148 int midcol = curwin->w_wincol + curwin->w_width / 2;
4149 int has_midcol = (wp->w_wincol <= midcol
4150 && wp->w_wincol + wp->w_width >= midcol);
4152 /* Only enable scrollbars that contain the middle column of the current
4153 * window. */
4154 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4156 /* Scrollbars only on one side. Don't enable scrollbars that don't
4157 * contain the middle column of the current window. */
4158 if (!has_midcol)
4159 enable = FALSE;
4161 else
4163 /* Scrollbars on both sides. Don't enable scrollbars that neither
4164 * contain the middle column of the current window nor are on the far
4165 * side. */
4166 if (midcol > Columns / 2)
4168 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4169 enable = FALSE;
4171 else
4173 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4174 : !has_midcol)
4175 enable = FALSE;
4178 #endif
4179 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4183 * Scroll a window according to the values set in the globals current_scrollbar
4184 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4185 * or FALSE otherwise.
4188 gui_do_scroll()
4190 win_T *wp, *save_wp;
4191 int i;
4192 long nlines;
4193 pos_T old_cursor;
4194 linenr_T old_topline;
4195 #ifdef FEAT_DIFF
4196 int old_topfill;
4197 #endif
4199 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4200 if (wp == NULL)
4201 break;
4202 if (wp == NULL)
4203 /* Couldn't find window */
4204 return FALSE;
4207 * Compute number of lines to scroll. If zero, nothing to do.
4209 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4210 if (nlines == 0)
4211 return FALSE;
4213 save_wp = curwin;
4214 old_topline = wp->w_topline;
4215 #ifdef FEAT_DIFF
4216 old_topfill = wp->w_topfill;
4217 #endif
4218 old_cursor = wp->w_cursor;
4219 curwin = wp;
4220 curbuf = wp->w_buffer;
4221 if (nlines < 0)
4222 scrolldown(-nlines, gui.dragged_wp == NULL);
4223 else
4224 scrollup(nlines, gui.dragged_wp == NULL);
4225 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4226 * the mouse-up event already, but we still want it to behave like when
4227 * dragging. But not the next click in an arrow. */
4228 if (gui.dragged_sb == SBAR_NONE)
4229 gui.dragged_wp = NULL;
4231 if (old_topline != wp->w_topline
4232 #ifdef FEAT_DIFF
4233 || old_topfill != wp->w_topfill
4234 #endif
4237 if (p_so != 0)
4239 cursor_correct(); /* fix window for 'so' */
4240 update_topline(); /* avoid up/down jump */
4242 if (old_cursor.lnum != wp->w_cursor.lnum)
4243 coladvance(wp->w_curswant);
4244 #ifdef FEAT_SCROLLBIND
4245 wp->w_scbind_pos = wp->w_topline;
4246 #endif
4249 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4250 validate_cursor();
4252 curwin = save_wp;
4253 curbuf = save_wp->w_buffer;
4256 * Don't call updateWindow() when nothing has changed (it will overwrite
4257 * the status line!).
4259 if (old_topline != wp->w_topline
4260 || wp->w_redr_type != 0
4261 #ifdef FEAT_DIFF
4262 || old_topfill != wp->w_topfill
4263 #endif
4266 int type = VALID;
4268 #ifdef FEAT_INS_EXPAND
4269 if (pum_visible())
4271 type = NOT_VALID;
4272 wp->w_lines_valid = 0;
4274 #endif
4275 /* Don't set must_redraw here, it may cause the popup menu to
4276 * disappear when losing focus after a scrollbar drag. */
4277 if (wp->w_redr_type < type)
4278 wp->w_redr_type = type;
4279 updateWindow(wp); /* update window, status line, and cmdline */
4282 #ifdef FEAT_INS_EXPAND
4283 /* May need to redraw the popup menu. */
4284 if (pum_visible())
4285 pum_redraw();
4286 #endif
4288 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4293 * Horizontal scrollbar stuff:
4297 * Return length of line "lnum" for horizontal scrolling.
4299 static colnr_T
4300 scroll_line_len(lnum)
4301 linenr_T lnum;
4303 char_u *p;
4304 colnr_T col;
4305 int w;
4307 p = ml_get(lnum);
4308 col = 0;
4309 if (*p != NUL)
4310 for (;;)
4312 w = chartabsize(p, col);
4313 mb_ptr_adv(p);
4314 if (*p == NUL) /* don't count the last character */
4315 break;
4316 col += w;
4318 return col;
4321 /* Remember which line is currently the longest, so that we don't have to
4322 * search for it when scrolling horizontally. */
4323 static linenr_T longest_lnum = 0;
4325 static void
4326 gui_update_horiz_scrollbar(force)
4327 int force;
4329 long value, size, max; /* need 32 bit ints here */
4331 if (!gui.which_scrollbars[SBAR_BOTTOM])
4332 return;
4334 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4335 return;
4337 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4338 return;
4341 * It is possible for the cursor to be invalid if we're in the middle of
4342 * something (like changing files). If so, don't do anything for now.
4344 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4346 gui.bottom_sbar.value = -1;
4347 return;
4350 size = W_WIDTH(curwin);
4351 if (curwin->w_p_wrap)
4353 value = 0;
4354 #ifdef SCROLL_PAST_END
4355 max = 0;
4356 #else
4357 max = W_WIDTH(curwin) - 1;
4358 #endif
4360 else
4362 value = curwin->w_leftcol;
4364 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4365 * line numbers, topline and botline can be invalid when displaying is
4366 * postponed. */
4367 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4368 && curwin->w_topline <= curwin->w_cursor.lnum
4369 && curwin->w_botline > curwin->w_cursor.lnum
4370 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4372 linenr_T lnum;
4373 colnr_T n;
4375 /* Use maximum of all visible lines. Remember the lnum of the
4376 * longest line, clostest to the cursor line. Used when scrolling
4377 * below. */
4378 max = 0;
4379 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4381 n = scroll_line_len(lnum);
4382 if (n > (colnr_T)max)
4384 max = n;
4385 longest_lnum = lnum;
4387 else if (n == (colnr_T)max
4388 && abs((int)(lnum - curwin->w_cursor.lnum))
4389 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4390 longest_lnum = lnum;
4393 else
4394 /* Use cursor line only. */
4395 max = scroll_line_len(curwin->w_cursor.lnum);
4396 #ifdef FEAT_VIRTUALEDIT
4397 if (virtual_active())
4399 /* May move the cursor even further to the right. */
4400 if (curwin->w_virtcol >= (colnr_T)max)
4401 max = curwin->w_virtcol;
4403 #endif
4405 #ifndef SCROLL_PAST_END
4406 max += W_WIDTH(curwin) - 1;
4407 #endif
4408 /* The line number isn't scrolled, thus there is less space when
4409 * 'number' is set (also for 'foldcolumn'). */
4410 size -= curwin_col_off();
4411 #ifndef SCROLL_PAST_END
4412 max -= curwin_col_off();
4413 #endif
4416 #ifndef SCROLL_PAST_END
4417 if (value > max - size + 1)
4418 value = max - size + 1; /* limit the value to allowable range */
4419 #endif
4421 #ifdef FEAT_RIGHTLEFT
4422 if (curwin->w_p_rl)
4424 value = max + 1 - size - value;
4425 if (value < 0)
4427 size += value;
4428 value = 0;
4431 #endif
4432 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4433 && max == gui.bottom_sbar.max)
4434 return;
4436 gui.bottom_sbar.value = value;
4437 gui.bottom_sbar.size = size;
4438 gui.bottom_sbar.max = max;
4439 gui.prev_wrap = curwin->w_p_wrap;
4441 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4445 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4448 gui_do_horiz_scroll()
4450 /* no wrapping, no scrolling */
4451 if (curwin->w_p_wrap)
4452 return FALSE;
4454 if ((long_u)curwin->w_leftcol == scrollbar_value)
4455 return FALSE;
4457 curwin->w_leftcol = (colnr_T)scrollbar_value;
4459 /* When the line of the cursor is too short, move the cursor to the
4460 * longest visible line. Do a sanity check on "longest_lnum", just in
4461 * case. */
4462 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4463 && longest_lnum >= curwin->w_topline
4464 && longest_lnum < curwin->w_botline
4465 && !virtual_active())
4467 if (scrollbar_value > (long_u)scroll_line_len(curwin->w_cursor.lnum))
4469 curwin->w_cursor.lnum = longest_lnum;
4470 curwin->w_cursor.col = 0;
4474 return leftcol_changed();
4478 * Check that none of the colors are the same as the background color
4480 void
4481 gui_check_colors()
4483 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4485 gui_set_bg_color((char_u *)"White");
4486 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4487 gui_set_fg_color((char_u *)"Black");
4491 static void
4492 gui_set_fg_color(name)
4493 char_u *name;
4495 gui.norm_pixel = gui_get_color(name);
4496 hl_set_fg_color_name(vim_strsave(name));
4499 static void
4500 gui_set_bg_color(name)
4501 char_u *name;
4503 gui.back_pixel = gui_get_color(name);
4504 hl_set_bg_color_name(vim_strsave(name));
4508 * Allocate a color by name.
4509 * Returns INVALCOLOR and gives an error message when failed.
4511 guicolor_T
4512 gui_get_color(name)
4513 char_u *name;
4515 guicolor_T t;
4517 if (*name == NUL)
4518 return INVALCOLOR;
4519 t = gui_mch_get_color(name);
4521 if (t == INVALCOLOR
4522 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4523 && gui.in_use
4524 #endif
4526 EMSG2(_("E254: Cannot allocate color %s"), name);
4527 return t;
4531 * Return the grey value of a color (range 0-255).
4534 gui_get_lightness(pixel)
4535 guicolor_T pixel;
4537 long_u rgb = gui_mch_get_rgb(pixel);
4539 return (int)( (((rgb >> 16) & 0xff) * 299)
4540 + (((rgb >> 8) & 0xff) * 587)
4541 + ((rgb & 0xff) * 114)) / 1000;
4544 #if defined(FEAT_GUI_X11) || defined(PROTO)
4545 void
4546 gui_new_scrollbar_colors()
4548 win_T *wp;
4550 /* Nothing to do if GUI hasn't started yet. */
4551 if (!gui.in_use)
4552 return;
4554 FOR_ALL_WINDOWS(wp)
4556 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4557 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4559 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4561 #endif
4564 * Call this when focus has changed.
4566 void
4567 gui_focus_change(in_focus)
4568 int in_focus;
4571 * Skip this code to avoid drawing the cursor when debugging and switching
4572 * between the debugger window and gvim.
4574 #if 1
4575 gui.in_focus = in_focus;
4576 out_flush(); /* make sure output has been written */
4577 gui_update_cursor(TRUE, FALSE);
4579 # ifdef FEAT_XIM
4580 xim_set_focus(in_focus);
4581 # endif
4583 /* Put events in the input queue only when allowed.
4584 * ui_focus_change() isn't called directly, because it invokes
4585 * autocommands and that must not happen asynchronously. */
4586 if (!hold_gui_events)
4588 char_u bytes[3];
4590 bytes[0] = CSI;
4591 bytes[1] = KS_EXTRA;
4592 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4593 add_to_input_buf(bytes, 3);
4595 #endif
4599 * Called when the mouse moved (but not when dragging).
4601 void
4602 gui_mouse_moved(x, y)
4603 int x;
4604 int y;
4606 win_T *wp;
4607 char_u st[8];
4609 /* Ignore this while still starting up. */
4610 if (!gui.in_use || gui.starting)
4611 return;
4613 #ifdef FEAT_MOUSESHAPE
4614 /* Get window pointer, and update mouse shape as well. */
4615 wp = xy2win(x, y);
4616 #endif
4618 /* Only handle this when 'mousefocus' set and ... */
4619 if (p_mousef
4620 && !hold_gui_events /* not holding events */
4621 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4622 && State != HITRETURN /* but not hit-return prompt */
4623 && msg_scrolled == 0 /* no scrolled message */
4624 && !need_mouse_correct /* not moving the pointer */
4625 && gui.in_focus) /* gvim in focus */
4627 /* Don't move the mouse when it's left or right of the Vim window */
4628 if (x < 0 || x > Columns * gui.char_width)
4629 return;
4630 #ifndef FEAT_MOUSESHAPE
4631 wp = xy2win(x, y);
4632 #endif
4633 if (wp == curwin || wp == NULL)
4634 return; /* still in the same old window, or none at all */
4636 #ifdef FEAT_WINDOWS
4637 /* Ignore position in the tab pages line. */
4638 if (Y_2_ROW(y) < tabline_height())
4639 return;
4640 #endif
4643 * format a mouse click on status line input
4644 * ala gui_send_mouse_event(0, x, y, 0, 0);
4645 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4646 * generate a K_LEFTMOUSE_NM key code.
4648 if (finish_op)
4650 /* abort the current operator first */
4651 st[0] = ESC;
4652 add_to_input_buf(st, 1);
4654 st[0] = CSI;
4655 st[1] = KS_MOUSE;
4656 st[2] = KE_FILLER;
4657 st[3] = (char_u)MOUSE_LEFT;
4658 fill_mouse_coord(st + 4,
4659 #ifdef FEAT_VERTSPLIT
4660 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4661 #else
4663 #endif
4664 wp->w_height + W_WINROW(wp));
4666 add_to_input_buf(st, 8);
4667 st[3] = (char_u)MOUSE_RELEASE;
4668 add_to_input_buf(st, 8);
4669 #ifdef FEAT_GUI_GTK
4670 /* Need to wake up the main loop */
4671 if (gtk_main_level() > 0)
4672 gtk_main_quit();
4673 #endif
4678 * Called when mouse should be moved to window with focus.
4680 void
4681 gui_mouse_correct()
4683 int x, y;
4684 win_T *wp = NULL;
4686 need_mouse_correct = FALSE;
4688 if (!(gui.in_use && p_mousef))
4689 return;
4691 gui_mch_getmouse(&x, &y);
4692 /* Don't move the mouse when it's left or right of the Vim window */
4693 if (x < 0 || x > Columns * gui.char_width)
4694 return;
4695 if (y >= 0
4696 # ifdef FEAT_WINDOWS
4697 && Y_2_ROW(y) >= tabline_height()
4698 # endif
4700 wp = xy2win(x, y);
4701 if (wp != curwin && wp != NULL) /* If in other than current window */
4703 validate_cline_row();
4704 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4705 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4706 + (gui.char_height) / 2);
4711 * Find window where the mouse pointer "y" coordinate is in.
4713 static win_T *
4714 xy2win(x, y)
4715 int x UNUSED;
4716 int y UNUSED;
4718 #ifdef FEAT_WINDOWS
4719 int row;
4720 int col;
4721 win_T *wp;
4723 row = Y_2_ROW(y);
4724 col = X_2_COL(x);
4725 if (row < 0 || col < 0) /* before first window */
4726 return NULL;
4727 wp = mouse_find_win(&row, &col);
4728 # ifdef FEAT_MOUSESHAPE
4729 if (State == HITRETURN || State == ASKMORE)
4731 if (Y_2_ROW(y) >= msg_row)
4732 update_mouseshape(SHAPE_IDX_MOREL);
4733 else
4734 update_mouseshape(SHAPE_IDX_MORE);
4736 else if (row > wp->w_height) /* below status line */
4737 update_mouseshape(SHAPE_IDX_CLINE);
4738 # ifdef FEAT_VERTSPLIT
4739 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4740 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4741 update_mouseshape(SHAPE_IDX_VSEP);
4742 # endif
4743 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4744 && row == wp->w_height && msg_scrolled == 0)
4745 update_mouseshape(SHAPE_IDX_STATUS);
4746 else
4747 update_mouseshape(-2);
4748 # endif
4749 return wp;
4750 #else
4751 return firstwin;
4752 #endif
4756 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4757 * File names may be given to redefine the args list.
4759 void
4760 ex_gui(eap)
4761 exarg_T *eap;
4763 char_u *arg = eap->arg;
4766 * Check for "-f" argument: foreground, don't fork.
4767 * Also don't fork when started with "gvim -f".
4768 * Do fork when using "gui -b".
4770 if (arg[0] == '-'
4771 && (arg[1] == 'f' || arg[1] == 'b')
4772 && (arg[2] == NUL || vim_iswhite(arg[2])))
4774 gui.dofork = (arg[1] == 'b');
4775 eap->arg = skipwhite(eap->arg + 2);
4777 if (!gui.in_use)
4779 /* Clear the command. Needed for when forking+exiting, to avoid part
4780 * of the argument ending up after the shell prompt. */
4781 msg_clr_eos_force();
4782 gui_start();
4784 if (!ends_excmd(*eap->arg))
4785 ex_next(eap);
4788 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4789 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4791 * This is shared between Athena, Motif and GTK.
4793 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4796 * Callback function for do_in_runtimepath().
4798 static void
4799 gfp_setname(fname, cookie)
4800 char_u *fname;
4801 void *cookie;
4803 char_u *gfp_buffer = cookie;
4805 if (STRLEN(fname) >= MAXPATHL)
4806 *gfp_buffer = NUL;
4807 else
4808 STRCPY(gfp_buffer, fname);
4812 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4813 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4816 gui_find_bitmap(name, buffer, ext)
4817 char_u *name;
4818 char_u *buffer;
4819 char *ext;
4821 if (STRLEN(name) > MAXPATHL - 14)
4822 return FAIL;
4823 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4824 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4825 || *buffer == NUL)
4826 return FAIL;
4827 return OK;
4830 # if !defined(HAVE_GTK2) || defined(PROTO)
4832 * Given the name of the "icon=" argument, try finding the bitmap file for the
4833 * icon. If it is an absolute path name, use it as it is. Otherwise append
4834 * "ext" and search for it in 'runtimepath'.
4835 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4836 * contains "name".
4838 void
4839 gui_find_iconfile(name, buffer, ext)
4840 char_u *name;
4841 char_u *buffer;
4842 char *ext;
4844 char_u buf[MAXPATHL + 1];
4846 expand_env(name, buffer, MAXPATHL);
4847 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4848 STRCPY(buffer, buf);
4850 # endif
4851 #endif
4853 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4854 void
4855 display_errors()
4857 char_u *p;
4859 if (isatty(2))
4860 fflush(stderr);
4861 else if (error_ga.ga_data != NULL)
4863 /* avoid putting up a message box with blanks only */
4864 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4865 if (!isspace(*p))
4867 /* Truncate a very long message, it will go off-screen. */
4868 if (STRLEN(p) > 2000)
4869 STRCPY(p + 2000 - 14, "...(truncated)");
4870 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4871 p, (char_u *)_("&Ok"), 1, NULL);
4872 break;
4874 ga_clear(&error_ga);
4877 #endif
4879 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4881 * Return TRUE if still starting up and there is no place to enter text.
4882 * For GTK and X11 we check if stderr is not a tty, which means we were
4883 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4884 * allow typing on stdin.
4887 no_console_input()
4889 return ((!gui.in_use || gui.starting)
4890 # ifndef NO_CONSOLE
4891 && !isatty(0) && !isatty(2)
4892 # endif
4895 #endif
4897 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4898 || defined(NEED_GUI_UPDATE_SCREEN) \
4899 || defined(PROTO)
4901 * Update the current window and the screen.
4903 void
4904 gui_update_screen()
4906 update_topline();
4907 validate_cursor();
4908 #ifdef FEAT_AUTOCMD
4909 /* Trigger CursorMoved if the cursor moved. */
4910 if (!finish_op && has_cursormoved()
4911 && !equalpos(last_cursormoved, curwin->w_cursor))
4913 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4914 last_cursormoved = curwin->w_cursor;
4916 #endif
4917 update_screen(0); /* may need to update the screen */
4918 setcursor();
4919 out_flush(); /* make sure output has been written */
4920 gui_update_cursor(TRUE, FALSE);
4921 gui_mch_flush();
4923 #endif
4925 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4926 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4929 * Get the text to use in a find/replace dialog. Uses the last search pattern
4930 * if the argument is empty.
4931 * Returns an allocated string.
4933 char_u *
4934 get_find_dialog_text(arg, wwordp, mcasep)
4935 char_u *arg;
4936 int *wwordp; /* return: TRUE if \< \> found */
4937 int *mcasep; /* return: TRUE if \C found */
4939 char_u *text;
4941 if (*arg == NUL)
4942 text = last_search_pat();
4943 else
4944 text = arg;
4945 if (text != NULL)
4947 text = vim_strsave(text);
4948 if (text != NULL)
4950 int len = (int)STRLEN(text);
4951 int i;
4953 /* Remove "\V" */
4954 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4956 mch_memmove(text, text + 2, (size_t)(len - 1));
4957 len -= 2;
4960 /* Recognize "\c" and "\C" and remove. */
4961 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4963 *mcasep = (text[1] == 'C');
4964 mch_memmove(text, text + 2, (size_t)(len - 1));
4965 len -= 2;
4968 /* Recognize "\<text\>" and remove. */
4969 if (len >= 4
4970 && STRNCMP(text, "\\<", 2) == 0
4971 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4973 *wwordp = TRUE;
4974 mch_memmove(text, text + 2, (size_t)(len - 4));
4975 text[len - 4] = NUL;
4978 /* Recognize "\/" or "\?" and remove. */
4979 for (i = 0; i + 1 < len; ++i)
4980 if (text[i] == '\\' && (text[i + 1] == '/'
4981 || text[i + 1] == '?'))
4983 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4984 --len;
4988 return text;
4992 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4994 static void
4995 concat_esc(gap, text, what)
4996 garray_T *gap;
4997 char_u *text;
4998 int what;
5000 while (*text != NUL)
5002 #ifdef FEAT_MBYTE
5003 int l = (*mb_ptr2len)(text);
5005 if (l > 1)
5007 while (--l >= 0)
5008 ga_append(gap, *text++);
5009 continue;
5011 #endif
5012 if (*text == what)
5013 ga_append(gap, '\\');
5014 ga_append(gap, *text);
5015 ++text;
5020 * Handle the press of a button in the find-replace dialog.
5021 * Return TRUE when something was added to the input buffer.
5024 gui_do_findrepl(flags, find_text, repl_text, down)
5025 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5026 char_u *find_text;
5027 char_u *repl_text;
5028 int down; /* Search downwards. */
5030 garray_T ga;
5031 int i;
5032 int type = (flags & FRD_TYPE_MASK);
5033 char_u *p;
5034 regmatch_T regmatch;
5035 int save_did_emsg = did_emsg;
5036 static int busy = FALSE;
5038 /* When the screen is being updated we should not change buffers and
5039 * windows structures, it may cause freed memory to be used. Also don't
5040 * do this recursively (pressing "Find" quickly several times. */
5041 if (updating_screen || busy)
5042 return FALSE;
5044 /* refuse replace when text cannot be changed */
5045 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5046 return FALSE;
5048 busy = TRUE;
5050 ga_init2(&ga, 1, 100);
5051 if (type == FRD_REPLACEALL)
5052 ga_concat(&ga, (char_u *)"%s/");
5054 ga_concat(&ga, (char_u *)"\\V");
5055 if (flags & FRD_MATCH_CASE)
5056 ga_concat(&ga, (char_u *)"\\C");
5057 else
5058 ga_concat(&ga, (char_u *)"\\c");
5059 if (flags & FRD_WHOLE_WORD)
5060 ga_concat(&ga, (char_u *)"\\<");
5061 if (type == FRD_REPLACEALL || down)
5062 concat_esc(&ga, find_text, '/'); /* escape slashes */
5063 else
5064 concat_esc(&ga, find_text, '?'); /* escape '?' */
5065 if (flags & FRD_WHOLE_WORD)
5066 ga_concat(&ga, (char_u *)"\\>");
5068 if (type == FRD_REPLACEALL)
5070 ga_concat(&ga, (char_u *)"/");
5071 /* escape / and \ */
5072 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5073 if (p != NULL)
5074 ga_concat(&ga, p);
5075 vim_free(p);
5076 ga_concat(&ga, (char_u *)"/g");
5078 ga_append(&ga, NUL);
5080 if (type == FRD_REPLACE)
5082 /* Do the replacement when the text at the cursor matches. Thus no
5083 * replacement is done if the cursor was moved! */
5084 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5085 regmatch.rm_ic = 0;
5086 if (regmatch.regprog != NULL)
5088 p = ml_get_cursor();
5089 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5090 && regmatch.startp[0] == p)
5092 /* Clear the command line to remove any old "No match"
5093 * error. */
5094 msg_end_prompt();
5096 if (u_save_cursor() == OK)
5098 /* A button was pressed thus undo should be synced. */
5099 u_sync(FALSE);
5101 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5102 FALSE, FALSE);
5103 ins_str(repl_text);
5106 else
5107 MSG(_("No match at cursor, finding next"));
5108 vim_free(regmatch.regprog);
5112 if (type == FRD_REPLACEALL)
5114 /* A button was pressed, thus undo should be synced. */
5115 u_sync(FALSE);
5116 do_cmdline_cmd(ga.ga_data);
5118 else
5120 /* Search for the next match. */
5121 i = msg_scroll;
5122 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5123 SEARCH_MSG + SEARCH_MARK, NULL);
5124 msg_scroll = i; /* don't let an error message set msg_scroll */
5127 /* Don't want to pass did_emsg to other code, it may cause disabling
5128 * syntax HL if we were busy redrawing. */
5129 did_emsg = save_did_emsg;
5131 if (State & (NORMAL | INSERT))
5133 gui_update_screen(); /* update the screen */
5134 msg_didout = 0; /* overwrite any message */
5135 need_wait_return = FALSE; /* don't wait for return */
5138 vim_free(ga.ga_data);
5139 busy = FALSE;
5140 return (ga.ga_len > 0);
5143 #endif
5145 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5146 || defined(FEAT_GUI_MSWIN) \
5147 || defined(FEAT_GUI_MAC) \
5148 || defined(PROTO)
5150 #ifdef FEAT_WINDOWS
5151 static void gui_wingoto_xy __ARGS((int x, int y));
5154 * Jump to the window at specified point (x, y).
5156 static void
5157 gui_wingoto_xy(x, y)
5158 int x;
5159 int y;
5161 int row = Y_2_ROW(y);
5162 int col = X_2_COL(x);
5163 win_T *wp;
5165 if (row >= 0 && col >= 0)
5167 wp = mouse_find_win(&row, &col);
5168 if (wp != NULL && wp != curwin)
5169 win_goto(wp);
5172 #endif
5175 * Process file drop. Mouse cursor position, key modifiers, name of files
5176 * and count of files are given. Argument "fnames[count]" has full pathnames
5177 * of dropped files, they will be freed in this function, and caller can't use
5178 * fnames after call this function.
5180 void
5181 gui_handle_drop(x, y, modifiers, fnames, count)
5182 int x UNUSED;
5183 int y UNUSED;
5184 int_u modifiers;
5185 char_u **fnames;
5186 int count;
5188 int i;
5189 char_u *p;
5190 static int entered = FALSE;
5193 * This function is called by event handlers. Just in case we get a
5194 * second event before the first one is handled, ignore the second one.
5195 * Not sure if this can ever happen, just in case.
5197 if (entered)
5198 return;
5199 entered = TRUE;
5202 * When the cursor is at the command line, add the file names to the
5203 * command line, don't edit the files.
5205 if (State & CMDLINE)
5207 shorten_filenames(fnames, count);
5208 for (i = 0; i < count; ++i)
5210 if (fnames[i] != NULL)
5212 if (i > 0)
5213 add_to_input_buf((char_u*)" ", 1);
5215 /* We don't know what command is used thus we can't be sure
5216 * about which characters need to be escaped. Only escape the
5217 * most common ones. */
5218 # ifdef BACKSLASH_IN_FILENAME
5219 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5220 # else
5221 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5222 # endif
5223 if (p != NULL)
5224 add_to_input_buf_csi(p, (int)STRLEN(p));
5225 vim_free(p);
5226 vim_free(fnames[i]);
5229 vim_free(fnames);
5231 else
5233 /* Go to the window under mouse cursor, then shorten given "fnames" by
5234 * current window, because a window can have local current dir. */
5235 # ifdef FEAT_WINDOWS
5236 gui_wingoto_xy(x, y);
5237 # endif
5238 shorten_filenames(fnames, count);
5240 /* If Shift held down, remember the first item. */
5241 if ((modifiers & MOUSE_SHIFT) != 0)
5242 p = vim_strsave(fnames[0]);
5243 else
5244 p = NULL;
5246 /* Handle the drop, :edit or :split to get to the file. This also
5247 * frees fnames[]. Skip this if there is only one item it's a
5248 * directory and Shift is held down. */
5249 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5250 && mch_isdir(fnames[0]))
5252 vim_free(fnames[0]);
5253 vim_free(fnames);
5255 else
5256 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5258 /* If Shift held down, change to first file's directory. If the first
5259 * item is a directory, change to that directory (and let the explorer
5260 * plugin show the contents). */
5261 if (p != NULL)
5263 if (mch_isdir(p))
5265 if (mch_chdir((char *)p) == 0)
5266 shorten_fnames(TRUE);
5268 else if (vim_chdirfile(p) == OK)
5269 shorten_fnames(TRUE);
5270 vim_free(p);
5273 /* Update the screen display */
5274 update_screen(NOT_VALID);
5275 # ifdef FEAT_MENU
5276 gui_update_menus(0);
5277 # endif
5278 setcursor();
5279 out_flush();
5280 gui_update_cursor(FALSE, FALSE);
5281 gui_mch_flush();
5284 entered = FALSE;
5286 #endif