Disable useless scrollbars
[MacVim.git] / src / gui.c
blob7e4f890d723a235361e2c1db65388ac158a0349b
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 (void)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 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
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) \
640 || defined(PROTO) || defined(FEAT_GUI_MACVIM)
641 # define NEED_GUI_UPDATE_SCREEN 1
643 * Called when the GUI shell is closed by the user. If there are no changed
644 * files Vim exits, otherwise there will be a dialog to ask the user what to
645 * do.
646 * When this function returns, Vim should NOT exit!
648 void
649 gui_shell_closed()
651 cmdmod_T save_cmdmod;
653 save_cmdmod = cmdmod;
655 /* Only exit when there are no changed files */
656 exiting = TRUE;
657 # ifdef FEAT_BROWSE
658 cmdmod.browse = TRUE;
659 # endif
660 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
661 cmdmod.confirm = TRUE;
662 # endif
663 /* If there are changed buffers, present the user with a dialog if
664 * possible, otherwise give an error message. */
665 if (!check_changed_any(FALSE))
666 getout(0);
668 exiting = FALSE;
669 cmdmod = save_cmdmod;
670 gui_update_screen(); /* redraw, window may show changed buffer */
672 #endif
675 * Set the font. "font_list" is a a comma separated list of font names. The
676 * first font name that works is used. If none is found, use the default
677 * font.
678 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
679 * Return OK when able to set the font. When it failed FAIL is returned and
680 * the fonts are unchanged.
682 /*ARGSUSED*/
684 gui_init_font(font_list, fontset)
685 char_u *font_list;
686 int fontset;
688 #define FONTLEN 320
689 char_u font_name[FONTLEN];
690 int font_list_empty = FALSE;
691 int ret = FAIL;
693 if (!gui.in_use)
694 return FAIL;
696 font_name[0] = NUL;
697 if (*font_list == NUL)
698 font_list_empty = TRUE;
699 else
701 #ifdef FEAT_XFONTSET
702 /* When using a fontset, the whole list of fonts is one name. */
703 if (fontset)
704 ret = gui_mch_init_font(font_list, TRUE);
705 else
706 #endif
707 while (*font_list != NUL)
709 /* Isolate one comma separated font name. */
710 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
712 #if defined(FEAT_GUI_MACVIM)
713 /* The font dialog is modeless in Mac OS X, so when
714 * gui_mch_init_font() is called with "*" it brings up the
715 * dialog and returns immediately. In this case we don't want
716 * it to be called again with NULL, so return here. */
717 if (STRCMP(font_name, "*") == 0) {
718 gui_mch_init_font(font_name, FALSE);
719 return FALSE;
721 #endif
723 /* Careful!!! The Win32 version of gui_mch_init_font(), when
724 * called with "*" will change p_guifont to the selected font
725 * name, which frees the old value. This makes font_list
726 * invalid. Thus when OK is returned here, font_list must no
727 * longer be used! */
728 if (gui_mch_init_font(font_name, FALSE) == OK)
730 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
731 /* If it's a Unicode font, try setting 'guifontwide' to a
732 * similar double-width font. */
733 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
734 && strstr((char *)font_name, "10646") != NULL)
735 set_guifontwide(font_name);
736 #endif
737 ret = OK;
738 break;
743 if (ret != OK
744 && STRCMP(font_list, "*") != 0
745 && (font_list_empty || gui.norm_font == NOFONT))
748 * Couldn't load any font in 'font_list', keep the current font if
749 * there is one. If 'font_list' is empty, or if there is no current
750 * font, tell gui_mch_init_font() to try to find a font we can load.
752 ret = gui_mch_init_font(NULL, FALSE);
755 if (ret == OK)
757 #ifndef HAVE_GTK2
758 /* Set normal font as current font */
759 # ifdef FEAT_XFONTSET
760 if (gui.fontset != NOFONTSET)
761 gui_mch_set_fontset(gui.fontset);
762 else
763 # endif
764 gui_mch_set_font(gui.norm_font);
765 #endif
766 gui_set_shellsize(FALSE,
767 #ifdef MSWIN
768 TRUE
769 #else
770 FALSE
771 #endif
772 , RESIZE_BOTH);
775 return ret;
778 #if defined(FEAT_MBYTE) || defined(PROTO)
779 # ifndef HAVE_GTK2
781 * Try setting 'guifontwide' to a font twice as wide as "name".
783 static void
784 set_guifontwide(name)
785 char_u *name;
787 int i = 0;
788 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
789 char_u *wp = NULL;
790 char_u *p;
791 GuiFont font;
793 wp = wide_name;
794 for (p = name; *p != NUL; ++p)
796 *wp++ = *p;
797 if (*p == '-')
799 ++i;
800 if (i == 6) /* font type: change "--" to "-*-" */
802 if (p[1] == '-')
803 *wp++ = '*';
805 else if (i == 12) /* found the width */
807 ++p;
808 i = getdigits(&p);
809 if (i != 0)
811 /* Double the width specification. */
812 sprintf((char *)wp, "%d%s", i * 2, p);
813 font = gui_mch_get_font(wide_name, FALSE);
814 if (font != NOFONT)
816 gui_mch_free_font(gui.wide_font);
817 gui.wide_font = font;
818 set_string_option_direct((char_u *)"gfw", -1,
819 wide_name, OPT_FREE, 0);
822 break;
827 # endif /* !HAVE_GTK2 */
830 * Get the font for 'guifontwide'.
831 * Return FAIL for an invalid font name.
834 gui_get_wide_font()
836 GuiFont font = NOFONT;
837 char_u font_name[FONTLEN];
838 char_u *p;
840 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
841 return OK; /* Will give an error message later. */
843 if (p_guifontwide != NULL && *p_guifontwide != NUL)
845 for (p = p_guifontwide; *p != NUL; )
847 /* Isolate one comma separated font name. */
848 (void)copy_option_part(&p, font_name, FONTLEN, ",");
849 font = gui_mch_get_font(font_name, FALSE);
850 if (font != NOFONT)
851 break;
853 if (font == NOFONT)
854 return FAIL;
857 gui_mch_free_font(gui.wide_font);
858 #ifdef HAVE_GTK2
859 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
860 if (font != NOFONT && gui.norm_font != NOFONT
861 && pango_font_description_equal(font, gui.norm_font))
863 gui.wide_font = NOFONT;
864 gui_mch_free_font(font);
866 else
867 #endif
868 gui.wide_font = font;
869 return OK;
871 #endif
873 void
874 gui_set_cursor(row, col)
875 int row;
876 int col;
878 gui.row = row;
879 gui.col = col;
883 * gui_check_pos - check if the cursor is on the screen.
885 static void
886 gui_check_pos()
888 if (gui.row >= screen_Rows)
889 gui.row = screen_Rows - 1;
890 if (gui.col >= screen_Columns)
891 gui.col = screen_Columns - 1;
892 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
893 gui.cursor_is_valid = FALSE;
897 * Redraw the cursor if necessary or when forced.
898 * Careful: The contents of ScreenLines[] must match what is on the screen,
899 * otherwise this goes wrong. May need to call out_flush() first.
901 void
902 gui_update_cursor(force, clear_selection)
903 int force; /* when TRUE, update even when not moved */
904 int clear_selection;/* clear selection under cursor */
906 int cur_width = 0;
907 int cur_height = 0;
908 int old_hl_mask;
909 int idx;
910 int id;
911 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
912 int cattr; /* cursor attributes */
913 int attr;
914 attrentry_T *aep = NULL;
916 /* Don't update the cursor when halfway busy scrolling.
917 * ScreenLines[] isn't valid then. */
918 if (!can_update_cursor)
919 return;
921 gui_check_pos();
922 if (!gui.cursor_is_valid || force
923 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
925 gui_undraw_cursor();
926 if (gui.row < 0)
927 return;
928 #ifdef USE_IM_CONTROL
929 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
930 im_set_position(gui.row, gui.col);
931 #endif
932 gui.cursor_row = gui.row;
933 gui.cursor_col = gui.col;
935 /* Only write to the screen after ScreenLines[] has been initialized */
936 if (!screen_cleared || ScreenLines == NULL)
937 return;
939 /* Clear the selection if we are about to write over it */
940 if (clear_selection)
941 clip_may_clear_selection(gui.row, gui.row);
942 /* Check that the cursor is inside the shell (resizing may have made
943 * it invalid) */
944 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
945 return;
947 gui.cursor_is_valid = TRUE;
950 * How the cursor is drawn depends on the current mode.
952 idx = get_shape_idx(FALSE);
953 if (State & LANGMAP)
954 id = shape_table[idx].id_lm;
955 else
956 id = shape_table[idx].id;
958 /* get the colors and attributes for the cursor. Default is inverted */
959 cfg = INVALCOLOR;
960 cbg = INVALCOLOR;
961 cattr = HL_INVERSE;
962 gui_mch_set_blinking(shape_table[idx].blinkwait,
963 shape_table[idx].blinkon,
964 shape_table[idx].blinkoff);
965 if (id > 0)
967 cattr = syn_id2colors(id, &cfg, &cbg);
968 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
970 static int iid;
971 guicolor_T fg, bg;
973 if (im_get_status())
975 iid = syn_name2id((char_u *)"CursorIM");
976 if (iid > 0)
978 syn_id2colors(iid, &fg, &bg);
979 if (bg != INVALCOLOR)
980 cbg = bg;
981 if (fg != INVALCOLOR)
982 cfg = fg;
986 #endif
990 * Get the attributes for the character under the cursor.
991 * When no cursor color was given, use the character color.
993 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
994 if (attr > HL_ALL)
995 aep = syn_gui_attr2entry(attr);
996 if (aep != NULL)
998 attr = aep->ae_attr;
999 if (cfg == INVALCOLOR)
1000 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1001 : aep->ae_u.gui.fg_color);
1002 if (cbg == INVALCOLOR)
1003 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1004 : aep->ae_u.gui.bg_color);
1006 if (cfg == INVALCOLOR)
1007 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1008 if (cbg == INVALCOLOR)
1009 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1011 #ifdef FEAT_XIM
1012 if (aep != NULL)
1014 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1015 : aep->ae_u.gui.bg_color);
1016 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1017 : aep->ae_u.gui.fg_color);
1018 if (xim_bg_color == INVALCOLOR)
1019 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1020 : gui.back_pixel;
1021 if (xim_fg_color == INVALCOLOR)
1022 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1023 : gui.norm_pixel;
1025 else
1027 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1028 : gui.back_pixel;
1029 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1030 : gui.norm_pixel;
1032 #endif
1034 attr &= ~HL_INVERSE;
1035 if (cattr & HL_INVERSE)
1037 cc = cbg;
1038 cbg = cfg;
1039 cfg = cc;
1041 cattr &= ~HL_INVERSE;
1044 * When we don't have window focus, draw a hollow cursor.
1046 if (!gui.in_focus)
1048 gui_mch_draw_hollow_cursor(cbg);
1049 return;
1052 old_hl_mask = gui.highlight_mask;
1053 if (shape_table[idx].shape == SHAPE_BLOCK
1054 #ifdef FEAT_HANGULIN
1055 || composing_hangul
1056 #endif
1060 * Draw the text character with the cursor colors. Use the
1061 * character attributes plus the cursor attributes.
1063 gui.highlight_mask = (cattr | attr);
1064 #ifdef FEAT_HANGULIN
1065 if (composing_hangul)
1066 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1067 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1068 else
1069 #endif
1070 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1071 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1073 else
1075 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1076 int col_off = FALSE;
1077 #endif
1079 * First draw the partial cursor, then overwrite with the text
1080 * character, using a transparent background.
1082 if (shape_table[idx].shape == SHAPE_VER)
1084 cur_height = gui.char_height;
1085 cur_width = (gui.char_width * shape_table[idx].percentage
1086 + 99) / 100;
1088 else
1090 cur_height = (gui.char_height * shape_table[idx].percentage
1091 + 99) / 100;
1092 cur_width = gui.char_width;
1094 #ifdef FEAT_MBYTE
1095 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1096 LineOffset[gui.row] + screen_Columns) > 1)
1098 /* Double wide character. */
1099 if (shape_table[idx].shape != SHAPE_VER)
1100 cur_width += gui.char_width;
1101 # ifdef FEAT_RIGHTLEFT
1102 if (CURSOR_BAR_RIGHT)
1104 /* gui.col points to the left halve of the character but
1105 * the vertical line needs to be on the right halve.
1106 * A double-wide horizontal line is also drawn from the
1107 * right halve in gui_mch_draw_part_cursor(). */
1108 col_off = TRUE;
1109 ++gui.col;
1111 # endif
1113 #endif
1114 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1115 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1116 if (col_off)
1117 --gui.col;
1118 #endif
1120 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1121 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1122 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1123 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1124 (guicolor_T)0, (guicolor_T)0, 0);
1125 #endif
1127 gui.highlight_mask = old_hl_mask;
1131 #if defined(FEAT_MENU) || defined(PROTO)
1132 void
1133 gui_position_menu()
1135 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
1136 || defined(FEAT_GUI_MACVIM))
1137 if (gui.menu_is_active && gui.in_use)
1138 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1139 # endif
1141 #endif
1144 * Position the various GUI components (text area, menu). The vertical
1145 * scrollbars are NOT handled here. See gui_update_scrollbars().
1147 /*ARGSUSED*/
1148 static void
1149 gui_position_components(total_width)
1150 int total_width;
1152 int text_area_x;
1153 int text_area_y;
1154 int text_area_width;
1155 int text_area_height;
1157 /* avoid that moving components around generates events */
1158 ++hold_gui_events;
1160 text_area_x = 0;
1161 if (gui.which_scrollbars[SBAR_LEFT])
1162 text_area_x += gui.scrollbar_width;
1164 text_area_y = 0;
1165 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
1166 || defined(FEAT_GUI_MACVIM))
1167 gui.menu_width = total_width;
1168 if (gui.menu_is_active)
1169 text_area_y += gui.menu_height;
1170 #endif
1171 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1172 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1173 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1174 #endif
1176 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1177 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1178 if (gui_has_tabline())
1179 text_area_y += gui.tabline_height;
1180 #endif
1182 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1183 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1185 # ifdef FEAT_GUI_ATHENA
1186 gui_mch_set_toolbar_pos(0, text_area_y,
1187 gui.menu_width, gui.toolbar_height);
1188 # endif
1189 text_area_y += gui.toolbar_height;
1191 #endif
1193 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1194 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1196 gui_mch_set_text_area_pos(text_area_x,
1197 text_area_y,
1198 text_area_width,
1199 text_area_height
1200 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1201 + xim_get_status_area_height()
1202 #endif
1204 #ifdef FEAT_MENU
1205 gui_position_menu();
1206 #endif
1207 if (gui.which_scrollbars[SBAR_BOTTOM])
1208 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1209 text_area_x,
1210 text_area_y + text_area_height,
1211 text_area_width,
1212 gui.scrollbar_height);
1213 gui.left_sbar_x = 0;
1214 gui.right_sbar_x = text_area_x + text_area_width;
1216 --hold_gui_events;
1220 * Get the width of the widgets and decorations to the side of the text area.
1223 gui_get_base_width()
1225 int base_width;
1227 base_width = 2 * gui.border_offset;
1228 if (gui.which_scrollbars[SBAR_LEFT])
1229 base_width += gui.scrollbar_width;
1230 if (gui.which_scrollbars[SBAR_RIGHT])
1231 base_width += gui.scrollbar_width;
1232 return base_width;
1236 * Get the height of the widgets and decorations above and below the text area.
1239 gui_get_base_height()
1241 int base_height;
1243 base_height = 2 * gui.border_offset;
1244 if (gui.which_scrollbars[SBAR_BOTTOM])
1245 base_height += gui.scrollbar_height;
1246 #ifdef FEAT_GUI_GTK
1247 /* We can't take the sizes properly into account until anything is
1248 * realized. Therefore we recalculate all the values here just before
1249 * setting the size. (--mdcki) */
1250 #elif !defined(FEAT_GUI_MACVIM)
1251 # ifdef FEAT_MENU
1252 if (gui.menu_is_active)
1253 base_height += gui.menu_height;
1254 # endif
1255 # ifdef FEAT_TOOLBAR
1256 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1257 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1258 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1259 # else
1260 base_height += gui.toolbar_height;
1261 # endif
1262 # endif
1263 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1264 || defined(FEAT_GUI_MOTIF))
1265 if (gui_has_tabline())
1266 base_height += gui.tabline_height;
1267 # endif
1268 # ifdef FEAT_FOOTER
1269 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1270 base_height += gui.footer_height;
1271 # endif
1272 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1273 base_height += gui_mch_text_area_extra_height();
1274 # endif
1275 #endif
1276 return base_height;
1280 * Should be called after the GUI shell has been resized. Its arguments are
1281 * the new width and height of the shell in pixels.
1283 void
1284 gui_resize_shell(pixel_width, pixel_height)
1285 int pixel_width;
1286 int pixel_height;
1288 static int busy = FALSE;
1290 if (!gui.shell_created) /* ignore when still initializing */
1291 return;
1294 * Can't resize the screen while it is being redrawn. Remember the new
1295 * size and handle it later.
1297 if (updating_screen || busy)
1299 new_pixel_width = pixel_width;
1300 new_pixel_height = pixel_height;
1301 return;
1304 again:
1305 busy = TRUE;
1307 /* Flush pending output before redrawing */
1308 out_flush();
1310 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1311 gui.num_rows = (pixel_height - gui_get_base_height()
1312 #if !defined(FEAT_GUI_PHOTON) && !defined(FEAT_GUI_MSWIN)
1313 + (gui.char_height / 2)
1314 #endif
1315 ) / gui.char_height;
1317 gui_position_components(pixel_width);
1319 gui_reset_scroll_region();
1321 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1322 * at the last line here (why does it have to be one row too low?).
1324 if (State == ASKMORE || State == CONFIRM)
1325 gui.row = gui.num_rows;
1327 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1328 * the safe side. */
1329 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1330 || gui.num_rows != Rows || gui.num_cols != Columns)
1331 shell_resized();
1333 gui_update_scrollbars(TRUE);
1334 gui_update_cursor(FALSE, TRUE);
1335 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1336 xim_set_status_area();
1337 #endif
1339 busy = FALSE;
1342 * We could have been called again while redrawing the screen.
1343 * Need to do it all again with the latest size then.
1345 if (new_pixel_height)
1347 pixel_width = new_pixel_width;
1348 pixel_height = new_pixel_height;
1349 new_pixel_width = 0;
1350 new_pixel_height = 0;
1351 goto again;
1356 * Check if gui_resize_shell() must be called.
1358 void
1359 gui_may_resize_shell()
1361 int h, w;
1363 if (new_pixel_height)
1365 /* careful: gui_resize_shell() may postpone the resize again if we
1366 * were called indirectly by it */
1367 w = new_pixel_width;
1368 h = new_pixel_height;
1369 new_pixel_width = 0;
1370 new_pixel_height = 0;
1371 gui_resize_shell(w, h);
1376 gui_get_shellsize()
1378 Rows = gui.num_rows;
1379 Columns = gui.num_cols;
1380 return OK;
1384 * Set the size of the Vim shell according to Rows and Columns.
1385 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1386 * on the screen.
1388 /*ARGSUSED*/
1389 void
1390 gui_set_shellsize(mustset, fit_to_display, direction)
1391 int mustset; /* set by the user */
1392 int fit_to_display;
1393 int direction; /* RESIZE_HOR, RESIZE_VER */
1395 int base_width;
1396 int base_height;
1397 int width;
1398 int height;
1399 int min_width;
1400 int min_height;
1401 int screen_w;
1402 int screen_h;
1404 if (!gui.shell_created)
1405 return;
1407 #ifdef MSWIN
1408 /* If not setting to a user specified size and maximized, calculate the
1409 * number of characters that fit in the maximized window. */
1410 if (!mustset && gui_mch_maximized())
1412 gui_mch_newfont();
1413 return;
1415 #endif
1417 base_width = gui_get_base_width();
1418 base_height = gui_get_base_height();
1419 #ifdef USE_SUN_WORKSHOP
1420 if (!mustset && usingSunWorkShop
1421 && workshop_get_width_height(&width, &height))
1423 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1424 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1426 else
1427 #endif
1429 width = Columns * gui.char_width + base_width;
1430 height = Rows * gui.char_height + base_height;
1433 if (fit_to_display)
1435 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1436 if ((direction & RESIZE_HOR) && width > screen_w)
1438 Columns = (screen_w - base_width) / gui.char_width;
1439 if (Columns < MIN_COLUMNS)
1440 Columns = MIN_COLUMNS;
1441 width = Columns * gui.char_width + base_width;
1443 if ((direction & RESIZE_VERT) && height > screen_h)
1445 Rows = (screen_h - base_height) / gui.char_height;
1446 check_shellsize();
1447 height = Rows * gui.char_height + base_height;
1450 gui.num_cols = Columns;
1451 gui.num_rows = Rows;
1453 min_width = base_width + MIN_COLUMNS * gui.char_width;
1454 min_height = base_height + MIN_LINES * gui.char_height;
1455 # ifdef FEAT_WINDOWS
1456 min_height += tabline_height() * gui.char_height;
1457 # endif
1459 gui_mch_set_shellsize(width, height, min_width, min_height,
1460 base_width, base_height, direction);
1461 if (fit_to_display)
1463 int x, y;
1465 /* Some window managers put the Vim window left of/above the screen. */
1466 gui_mch_update();
1467 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1468 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1471 gui_position_components(width);
1472 gui_update_scrollbars(TRUE);
1473 gui_reset_scroll_region();
1477 * Called when Rows and/or Columns has changed.
1479 void
1480 gui_new_shellsize()
1482 gui_reset_scroll_region();
1486 * Make scroll region cover whole screen.
1488 void
1489 gui_reset_scroll_region()
1491 gui.scroll_region_top = 0;
1492 gui.scroll_region_bot = gui.num_rows - 1;
1493 gui.scroll_region_left = 0;
1494 gui.scroll_region_right = gui.num_cols - 1;
1497 void
1498 gui_start_highlight(mask)
1499 int mask;
1501 if (mask > HL_ALL) /* highlight code */
1502 gui.highlight_mask = mask;
1503 else /* mask */
1504 gui.highlight_mask |= mask;
1507 void
1508 gui_stop_highlight(mask)
1509 int mask;
1511 if (mask > HL_ALL) /* highlight code */
1512 gui.highlight_mask = HL_NORMAL;
1513 else /* mask */
1514 gui.highlight_mask &= ~mask;
1518 * Clear a rectangular region of the screen from text pos (row1, col1) to
1519 * (row2, col2) inclusive.
1521 void
1522 gui_clear_block(row1, col1, row2, col2)
1523 int row1;
1524 int col1;
1525 int row2;
1526 int col2;
1528 /* Clear the selection if we are about to write over it */
1529 clip_may_clear_selection(row1, row2);
1531 gui_mch_clear_block(row1, col1, row2, col2);
1533 /* Invalidate cursor if it was in this block */
1534 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1535 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1536 gui.cursor_is_valid = FALSE;
1540 * Write code to update the cursor later. This avoids the need to flush the
1541 * output buffer before calling gui_update_cursor().
1543 void
1544 gui_update_cursor_later()
1546 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1549 void
1550 gui_write(s, len)
1551 char_u *s;
1552 int len;
1554 char_u *p;
1555 int arg1 = 0, arg2 = 0;
1556 /* this doesn't make sense, disabled until someone can explain why it
1557 * would be needed */
1558 #if 0 && (defined(RISCOS) || defined(WIN16))
1559 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1560 our redraw speed will suffer */
1561 #else
1562 int force_cursor = FALSE; /* force cursor update */
1563 #endif
1564 int force_scrollbar = FALSE;
1565 static win_T *old_curwin = NULL;
1567 /* #define DEBUG_GUI_WRITE */
1568 #ifdef DEBUG_GUI_WRITE
1570 int i;
1571 char_u *str;
1573 printf("gui_write(%d):\n ", len);
1574 for (i = 0; i < len; i++)
1575 if (s[i] == ESC)
1577 if (i != 0)
1578 printf("\n ");
1579 printf("<ESC>");
1581 else
1583 str = transchar_byte(s[i]);
1584 if (str[0] && str[1])
1585 printf("<%s>", (char *)str);
1586 else
1587 printf("%s", (char *)str);
1589 printf("\n");
1591 #endif
1592 while (len)
1594 if (s[0] == ESC && s[1] == '|')
1596 p = s + 2;
1597 if (VIM_ISDIGIT(*p))
1599 arg1 = getdigits(&p);
1600 if (p > s + len)
1601 break;
1602 if (*p == ';')
1604 ++p;
1605 arg2 = getdigits(&p);
1606 if (p > s + len)
1607 break;
1610 switch (*p)
1612 case 'C': /* Clear screen */
1613 clip_scroll_selection(9999);
1614 gui_mch_clear_all();
1615 gui.cursor_is_valid = FALSE;
1616 force_scrollbar = TRUE;
1617 break;
1618 case 'M': /* Move cursor */
1619 gui_set_cursor(arg1, arg2);
1620 break;
1621 case 's': /* force cursor (shape) update */
1622 force_cursor = TRUE;
1623 break;
1624 case 'R': /* Set scroll region */
1625 if (arg1 < arg2)
1627 gui.scroll_region_top = arg1;
1628 gui.scroll_region_bot = arg2;
1630 else
1632 gui.scroll_region_top = arg2;
1633 gui.scroll_region_bot = arg1;
1635 break;
1636 #ifdef FEAT_VERTSPLIT
1637 case 'V': /* Set vertical scroll region */
1638 if (arg1 < arg2)
1640 gui.scroll_region_left = arg1;
1641 gui.scroll_region_right = arg2;
1643 else
1645 gui.scroll_region_left = arg2;
1646 gui.scroll_region_right = arg1;
1648 break;
1649 #endif
1650 case 'd': /* Delete line */
1651 gui_delete_lines(gui.row, 1);
1652 break;
1653 case 'D': /* Delete lines */
1654 gui_delete_lines(gui.row, arg1);
1655 break;
1656 case 'i': /* Insert line */
1657 gui_insert_lines(gui.row, 1);
1658 break;
1659 case 'I': /* Insert lines */
1660 gui_insert_lines(gui.row, arg1);
1661 break;
1662 case '$': /* Clear to end-of-line */
1663 gui_clear_block(gui.row, gui.col, gui.row,
1664 (int)Columns - 1);
1665 break;
1666 case 'h': /* Turn on highlighting */
1667 gui_start_highlight(arg1);
1668 break;
1669 case 'H': /* Turn off highlighting */
1670 gui_stop_highlight(arg1);
1671 break;
1672 case 'f': /* flash the window (visual bell) */
1673 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1674 break;
1675 default:
1676 p = s + 1; /* Skip the ESC */
1677 break;
1679 len -= (int)(++p - s);
1680 s = p;
1682 else if (
1683 #ifdef EBCDIC
1684 CtrlChar(s[0]) != 0 /* Ctrl character */
1685 #else
1686 s[0] < 0x20 /* Ctrl character */
1687 #endif
1688 #ifdef FEAT_SIGN_ICONS
1689 && s[0] != SIGN_BYTE
1690 # ifdef FEAT_NETBEANS_INTG
1691 && s[0] != MULTISIGN_BYTE
1692 # endif
1693 #endif
1696 if (s[0] == '\n') /* NL */
1698 gui.col = 0;
1699 if (gui.row < gui.scroll_region_bot)
1700 gui.row++;
1701 else
1702 gui_delete_lines(gui.scroll_region_top, 1);
1704 else if (s[0] == '\r') /* CR */
1706 gui.col = 0;
1708 else if (s[0] == '\b') /* Backspace */
1710 if (gui.col)
1711 --gui.col;
1713 else if (s[0] == Ctrl_L) /* cursor-right */
1715 ++gui.col;
1717 else if (s[0] == Ctrl_G) /* Beep */
1719 gui_mch_beep();
1721 /* Other Ctrl character: shouldn't happen! */
1723 --len; /* Skip this char */
1724 ++s;
1726 else
1728 p = s;
1729 while (len > 0 && (
1730 #ifdef EBCDIC
1731 CtrlChar(*p) == 0
1732 #else
1733 *p >= 0x20
1734 #endif
1735 #ifdef FEAT_SIGN_ICONS
1736 || *p == SIGN_BYTE
1737 # ifdef FEAT_NETBEANS_INTG
1738 || *p == MULTISIGN_BYTE
1739 # endif
1740 #endif
1743 len--;
1744 p++;
1746 gui_outstr(s, (int)(p - s));
1747 s = p;
1751 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1752 * set). */
1753 if (force_cursor)
1754 gui_update_cursor(TRUE, TRUE);
1756 /* When switching to another window the dragging must have stopped.
1757 * Required for GTK, dragged_sb isn't reset. */
1758 if (old_curwin != curwin)
1759 gui.dragged_sb = SBAR_NONE;
1761 /* Update the scrollbars after clearing the screen or when switched
1762 * to another window.
1763 * Update the horizontal scrollbar always, it's difficult to check all
1764 * situations where it might change. */
1765 if (force_scrollbar || old_curwin != curwin)
1766 gui_update_scrollbars(force_scrollbar);
1767 else
1768 gui_update_horiz_scrollbar(FALSE);
1769 old_curwin = curwin;
1772 * We need to make sure this is cleared since Athena doesn't tell us when
1773 * he is done dragging. Do the same for GTK.
1775 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1776 gui.dragged_sb = SBAR_NONE;
1777 #endif
1779 gui_mch_flush(); /* In case vim decides to take a nap */
1783 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1784 * produces wrong results. Call gui_dont_update_cursor() before that code and
1785 * gui_can_update_cursor() afterwards.
1787 void
1788 gui_dont_update_cursor()
1790 if (gui.in_use)
1792 /* Undraw the cursor now, we probably can't do it after the change. */
1793 gui_undraw_cursor();
1794 can_update_cursor = FALSE;
1798 void
1799 gui_can_update_cursor()
1801 can_update_cursor = TRUE;
1802 /* No need to update the cursor right now, there is always more output
1803 * after scrolling. */
1806 static void
1807 gui_outstr(s, len)
1808 char_u *s;
1809 int len;
1811 int this_len;
1812 #ifdef FEAT_MBYTE
1813 int cells;
1814 #endif
1816 if (len == 0)
1817 return;
1819 if (len < 0)
1820 len = (int)STRLEN(s);
1822 while (len > 0)
1824 #ifdef FEAT_MBYTE
1825 if (has_mbyte)
1827 /* Find out how many chars fit in the current line. */
1828 cells = 0;
1829 for (this_len = 0; this_len < len; )
1831 cells += (*mb_ptr2cells)(s + this_len);
1832 if (gui.col + cells > Columns)
1833 break;
1834 this_len += (*mb_ptr2len)(s + this_len);
1836 if (this_len > len)
1837 this_len = len; /* don't include following composing char */
1839 else
1840 #endif
1841 if (gui.col + len > Columns)
1842 this_len = Columns - gui.col;
1843 else
1844 this_len = len;
1846 (void)gui_outstr_nowrap(s, this_len,
1847 0, (guicolor_T)0, (guicolor_T)0, 0);
1848 s += this_len;
1849 len -= this_len;
1850 #ifdef FEAT_MBYTE
1851 /* fill up for a double-width char that doesn't fit. */
1852 if (len > 0 && gui.col < Columns)
1853 (void)gui_outstr_nowrap((char_u *)" ", 1,
1854 0, (guicolor_T)0, (guicolor_T)0, 0);
1855 #endif
1856 /* The cursor may wrap to the next line. */
1857 if (gui.col >= Columns)
1859 gui.col = 0;
1860 gui.row++;
1866 * Output one character (may be one or two display cells).
1867 * Caller must check for valid "off".
1868 * Returns FAIL or OK, just like gui_outstr_nowrap().
1870 static int
1871 gui_screenchar(off, flags, fg, bg, back)
1872 int off; /* Offset from start of screen */
1873 int flags;
1874 guicolor_T fg, bg; /* colors for cursor */
1875 int back; /* backup this many chars when using bold trick */
1877 #ifdef FEAT_MBYTE
1878 char_u buf[MB_MAXBYTES + 1];
1880 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1881 if (enc_utf8 && ScreenLines[off] == 0)
1882 return OK;
1884 if (enc_utf8 && ScreenLinesUC[off] != 0)
1885 /* Draw UTF-8 multi-byte character. */
1886 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1887 flags, fg, bg, back);
1889 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1891 buf[0] = ScreenLines[off];
1892 buf[1] = ScreenLines2[off];
1893 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1896 /* Draw non-multi-byte character or DBCS character. */
1897 return gui_outstr_nowrap(ScreenLines + off,
1898 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1899 flags, fg, bg, back);
1900 #else
1901 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1902 #endif
1905 #ifdef HAVE_GTK2
1907 * Output the string at the given screen position. This is used in place
1908 * of gui_screenchar() where possible because Pango needs as much context
1909 * as possible to work nicely. It's a lot faster as well.
1911 static int
1912 gui_screenstr(off, len, flags, fg, bg, back)
1913 int off; /* Offset from start of screen */
1914 int len; /* string length in screen cells */
1915 int flags;
1916 guicolor_T fg, bg; /* colors for cursor */
1917 int back; /* backup this many chars when using bold trick */
1919 char_u *buf;
1920 int outlen = 0;
1921 int i;
1922 int retval;
1924 if (len <= 0) /* "cannot happen"? */
1925 return OK;
1927 if (enc_utf8)
1929 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1930 if (buf == NULL)
1931 return OK; /* not much we could do here... */
1933 for (i = off; i < off + len; ++i)
1935 if (ScreenLines[i] == 0)
1936 continue; /* skip second half of double-width char */
1938 if (ScreenLinesUC[i] == 0)
1939 buf[outlen++] = ScreenLines[i];
1940 else
1941 outlen += utfc_char2bytes(i, buf + outlen);
1944 buf[outlen] = NUL; /* only to aid debugging */
1945 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1946 vim_free(buf);
1948 return retval;
1950 else if (enc_dbcs == DBCS_JPNU)
1952 buf = alloc((unsigned)(len * 2 + 1));
1953 if (buf == NULL)
1954 return OK; /* not much we could do here... */
1956 for (i = off; i < off + len; ++i)
1958 buf[outlen++] = ScreenLines[i];
1960 /* handle double-byte single-width char */
1961 if (ScreenLines[i] == 0x8e)
1962 buf[outlen++] = ScreenLines2[i];
1963 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1964 buf[outlen++] = ScreenLines[++i];
1967 buf[outlen] = NUL; /* only to aid debugging */
1968 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1969 vim_free(buf);
1971 return retval;
1973 else
1975 return gui_outstr_nowrap(&ScreenLines[off], len,
1976 flags, fg, bg, back);
1979 #endif /* HAVE_GTK2 */
1982 * Output the given string at the current cursor position. If the string is
1983 * too long to fit on the line, then it is truncated.
1984 * "flags":
1985 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1986 * actually draw (an inverted) cursor.
1987 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
1988 * background.
1989 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1990 * it.
1991 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1992 * FAIL (the caller should start drawing "back" chars back).
1995 gui_outstr_nowrap(s, len, flags, fg, bg, back)
1996 char_u *s;
1997 int len;
1998 int flags;
1999 guicolor_T fg, bg; /* colors for cursor */
2000 int back; /* backup this many chars when using bold trick */
2002 long_u highlight_mask;
2003 long_u hl_mask_todo;
2004 guicolor_T fg_color;
2005 guicolor_T bg_color;
2006 guicolor_T sp_color;
2007 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2008 GuiFont font = NOFONT;
2009 # ifdef FEAT_XFONTSET
2010 GuiFontset fontset = NOFONTSET;
2011 # endif
2012 #endif
2013 attrentry_T *aep = NULL;
2014 int draw_flags;
2015 int col = gui.col;
2016 #ifdef FEAT_SIGN_ICONS
2017 int draw_sign = FALSE;
2018 # ifdef FEAT_NETBEANS_INTG
2019 int multi_sign = FALSE;
2020 # endif
2021 #endif
2023 if (len < 0)
2024 len = (int)STRLEN(s);
2025 if (len == 0)
2026 return OK;
2028 #ifdef FEAT_SIGN_ICONS
2029 if (*s == SIGN_BYTE
2030 # ifdef FEAT_NETBEANS_INTG
2031 || *s == MULTISIGN_BYTE
2032 # endif
2035 # ifdef FEAT_NETBEANS_INTG
2036 if (*s == MULTISIGN_BYTE)
2037 multi_sign = TRUE;
2038 # endif
2039 /* draw spaces instead */
2040 s = (char_u *)" ";
2041 if (len == 1 && col > 0)
2042 --col;
2043 len = 2;
2044 draw_sign = TRUE;
2045 highlight_mask = 0;
2047 else
2048 #endif
2049 if (gui.highlight_mask > HL_ALL)
2051 aep = syn_gui_attr2entry(gui.highlight_mask);
2052 if (aep == NULL) /* highlighting not set */
2053 highlight_mask = 0;
2054 else
2055 highlight_mask = aep->ae_attr;
2057 else
2058 highlight_mask = gui.highlight_mask;
2059 hl_mask_todo = highlight_mask;
2061 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2062 /* Set the font */
2063 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2064 font = aep->ae_u.gui.font;
2065 # ifdef FEAT_XFONTSET
2066 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2067 fontset = aep->ae_u.gui.fontset;
2068 # endif
2069 else
2071 # ifdef FEAT_XFONTSET
2072 if (gui.fontset != NOFONTSET)
2073 fontset = gui.fontset;
2074 else
2075 # endif
2076 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2078 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2080 font = gui.boldital_font;
2081 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2083 else if (gui.bold_font != NOFONT)
2085 font = gui.bold_font;
2086 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2088 else
2089 font = gui.norm_font;
2091 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2093 font = gui.ital_font;
2094 hl_mask_todo &= ~HL_ITALIC;
2096 else
2097 font = gui.norm_font;
2099 # ifdef FEAT_XFONTSET
2100 if (fontset != NOFONTSET)
2101 gui_mch_set_fontset(fontset);
2102 else
2103 # endif
2104 gui_mch_set_font(font);
2105 #endif
2107 draw_flags = 0;
2109 /* Set the color */
2110 bg_color = gui.back_pixel;
2111 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2113 draw_flags |= DRAW_CURSOR;
2114 fg_color = fg;
2115 bg_color = bg;
2116 sp_color = fg;
2118 else if (aep != NULL)
2120 fg_color = aep->ae_u.gui.fg_color;
2121 if (fg_color == INVALCOLOR)
2122 fg_color = gui.norm_pixel;
2123 bg_color = aep->ae_u.gui.bg_color;
2124 if (bg_color == INVALCOLOR)
2125 bg_color = gui.back_pixel;
2126 sp_color = aep->ae_u.gui.sp_color;
2127 if (sp_color == INVALCOLOR)
2128 sp_color = fg_color;
2130 else
2132 fg_color = gui.norm_pixel;
2133 sp_color = fg_color;
2136 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2138 #if defined(AMIGA) || defined(RISCOS)
2139 gui_mch_set_colors(bg_color, fg_color);
2140 #else
2141 gui_mch_set_fg_color(bg_color);
2142 gui_mch_set_bg_color(fg_color);
2143 #endif
2145 else
2147 #if defined(AMIGA) || defined(RISCOS)
2148 gui_mch_set_colors(fg_color, bg_color);
2149 #else
2150 gui_mch_set_fg_color(fg_color);
2151 gui_mch_set_bg_color(bg_color);
2152 #endif
2154 gui_mch_set_sp_color(sp_color);
2156 /* Clear the selection if we are about to write over it */
2157 if (!(flags & GUI_MON_NOCLEAR))
2158 clip_may_clear_selection(gui.row, gui.row);
2161 #ifndef MSWIN16_FASTTEXT
2162 /* If there's no bold font, then fake it */
2163 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2164 draw_flags |= DRAW_BOLD;
2165 #endif
2168 * When drawing bold or italic characters the spill-over from the left
2169 * neighbor may be destroyed. Let the caller backup to start redrawing
2170 * just after a blank.
2172 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2173 return FAIL;
2175 #if defined(RISCOS) || defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
2176 /* If there's no italic font, then fake it.
2177 * For GTK2, we don't need a different font for italic style. */
2178 if (hl_mask_todo & HL_ITALIC)
2179 draw_flags |= DRAW_ITALIC;
2181 /* Do we underline the text? */
2182 if (hl_mask_todo & HL_UNDERLINE)
2183 draw_flags |= DRAW_UNDERL;
2184 #else
2185 /* Do we underline the text? */
2186 if ((hl_mask_todo & HL_UNDERLINE)
2187 # ifndef MSWIN16_FASTTEXT
2188 || (hl_mask_todo & HL_ITALIC)
2189 # endif
2191 draw_flags |= DRAW_UNDERL;
2192 #endif
2193 /* Do we undercurl the text? */
2194 if (hl_mask_todo & HL_UNDERCURL)
2195 draw_flags |= DRAW_UNDERC;
2197 /* Do we draw transparently? */
2198 if (flags & GUI_MON_TRS_CURSOR)
2199 draw_flags |= DRAW_TRANSP;
2202 * Draw the text.
2204 #ifdef HAVE_GTK2
2205 /* The value returned is the length in display cells */
2206 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2207 #elif defined(FEAT_GUI_MACVIM)
2208 /* The value returned is the length in display cells */
2209 len = gui_macvim_draw_string(gui.row, col, s, len, draw_flags);
2210 #else
2211 # ifdef FEAT_MBYTE
2212 if (enc_utf8)
2214 int start; /* index of bytes to be drawn */
2215 int cells; /* cellwidth of bytes to be drawn */
2216 int thislen; /* length of bytes to be drawin */
2217 int cn; /* cellwidth of current char */
2218 int i; /* index of current char */
2219 int c; /* current char value */
2220 int cl; /* byte length of current char */
2221 int comping; /* current char is composing */
2222 int scol = col; /* screen column */
2223 int dowide; /* use 'guifontwide' */
2225 /* Break the string at a composing character, it has to be drawn on
2226 * top of the previous character. */
2227 start = 0;
2228 cells = 0;
2229 for (i = 0; i < len; i += cl)
2231 c = utf_ptr2char(s + i);
2232 cn = utf_char2cells(c);
2233 if (cn > 1
2234 # ifdef FEAT_XFONTSET
2235 && fontset == NOFONTSET
2236 # endif
2237 && gui.wide_font != NOFONT)
2238 dowide = TRUE;
2239 else
2240 dowide = FALSE;
2241 comping = utf_iscomposing(c);
2242 if (!comping) /* count cells from non-composing chars */
2243 cells += cn;
2244 cl = utf_ptr2len(s + i);
2245 if (cl == 0) /* hit end of string */
2246 len = i + cl; /* len must be wrong "cannot happen" */
2248 /* print the string so far if it's the last character or there is
2249 * a composing character. */
2250 if (i + cl >= len || (comping && i > start) || dowide
2251 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2252 || (cn > 1
2253 # ifdef FEAT_XFONTSET
2254 /* No fontset: At least draw char after wide char at
2255 * right position. */
2256 && fontset == NOFONTSET
2257 # endif
2259 # endif
2262 if (comping || dowide)
2263 thislen = i - start;
2264 else
2265 thislen = i - start + cl;
2266 if (thislen > 0)
2268 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2269 draw_flags);
2270 start += thislen;
2272 scol += cells;
2273 cells = 0;
2274 if (dowide)
2276 gui_mch_set_font(gui.wide_font);
2277 gui_mch_draw_string(gui.row, scol - cn,
2278 s + start, cl, draw_flags);
2279 gui_mch_set_font(font);
2280 start += cl;
2283 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2284 /* No fontset: draw a space to fill the gap after a wide char
2285 * */
2286 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2287 # ifdef FEAT_XFONTSET
2288 && fontset == NOFONTSET
2289 # endif
2290 && !dowide)
2291 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2292 1, draw_flags);
2293 # endif
2295 /* Draw a composing char on top of the previous char. */
2296 if (comping)
2298 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2299 /* Carbon ATSUI autodraws composing char over previous char */
2300 gui_mch_draw_string(gui.row, scol, s + i, cl,
2301 draw_flags | DRAW_TRANSP);
2302 # else
2303 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2304 draw_flags | DRAW_TRANSP);
2305 # endif
2306 start = i + cl;
2309 /* The stuff below assumes "len" is the length in screen columns. */
2310 len = scol - col;
2312 else
2313 # endif
2315 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2316 # ifdef FEAT_MBYTE
2317 if (enc_dbcs == DBCS_JPNU)
2319 int clen = 0;
2320 int i;
2322 /* Get the length in display cells, this can be different from the
2323 * number of bytes for "euc-jp". */
2324 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2325 clen += (*mb_ptr2cells)(s + i);
2326 len = clen;
2328 # endif
2330 #endif /* !HAVE_GTK2 */
2332 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2333 gui.col = col + len;
2335 /* May need to invert it when it's part of the selection. */
2336 if (flags & GUI_MON_NOCLEAR)
2337 clip_may_redraw_selection(gui.row, col, len);
2339 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2341 /* Invalidate the old physical cursor position if we wrote over it */
2342 if (gui.cursor_row == gui.row
2343 && gui.cursor_col >= col
2344 && gui.cursor_col < col + len)
2345 gui.cursor_is_valid = FALSE;
2348 #ifdef FEAT_SIGN_ICONS
2349 if (draw_sign)
2350 /* Draw the sign on top of the spaces. */
2351 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2352 # ifdef FEAT_NETBEANS_INTG
2353 if (multi_sign)
2354 netbeans_draw_multisign_indicator(gui.row);
2355 # endif
2356 #endif
2358 return OK;
2362 * Un-draw the cursor. Actually this just redraws the character at the given
2363 * position. The character just before it too, for when it was in bold.
2365 void
2366 gui_undraw_cursor()
2368 if (gui.cursor_is_valid)
2370 #ifdef FEAT_HANGULIN
2371 if (composing_hangul
2372 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2373 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2374 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2375 gui.norm_pixel, gui.back_pixel, 0);
2376 else
2378 #endif
2379 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2380 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2381 && gui.cursor_col > 0)
2382 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2383 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2384 #ifdef FEAT_HANGULIN
2385 if (composing_hangul)
2386 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2387 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2389 #endif
2390 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2391 * here in case it wasn't needed to undraw it. */
2392 gui.cursor_is_valid = FALSE;
2396 void
2397 gui_redraw(x, y, w, h)
2398 int x;
2399 int y;
2400 int w;
2401 int h;
2403 int row1, col1, row2, col2;
2405 row1 = Y_2_ROW(y);
2406 col1 = X_2_COL(x);
2407 row2 = Y_2_ROW(y + h - 1);
2408 col2 = X_2_COL(x + w - 1);
2410 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2413 * We may need to redraw the cursor, but don't take it upon us to change
2414 * its location after a scroll.
2415 * (maybe be more strict even and test col too?)
2416 * These things may be outside the update/clipping region and reality may
2417 * not reflect Vims internal ideas if these operations are clipped away.
2419 if (gui.row == gui.cursor_row)
2420 gui_update_cursor(TRUE, TRUE);
2424 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2425 * from col1 to col2 (inclusive).
2426 * Return TRUE when the character before the first drawn character has
2427 * different attributes (may have to be redrawn too).
2430 gui_redraw_block(row1, col1, row2, col2, flags)
2431 int row1;
2432 int col1;
2433 int row2;
2434 int col2;
2435 int flags; /* flags for gui_outstr_nowrap() */
2437 int old_row, old_col;
2438 long_u old_hl_mask;
2439 int off;
2440 sattr_T first_attr;
2441 int idx, len;
2442 int back, nback;
2443 int retval = FALSE;
2444 #ifdef FEAT_MBYTE
2445 int orig_col1, orig_col2;
2446 #endif
2448 /* Don't try to update when ScreenLines is not valid */
2449 if (!screen_cleared || ScreenLines == NULL)
2450 return retval;
2452 /* Don't try to draw outside the shell! */
2453 /* Check everything, strange values may be caused by a big border width */
2454 col1 = check_col(col1);
2455 col2 = check_col(col2);
2456 row1 = check_row(row1);
2457 row2 = check_row(row2);
2459 /* Remember where our cursor was */
2460 old_row = gui.row;
2461 old_col = gui.col;
2462 old_hl_mask = gui.highlight_mask;
2463 #ifdef FEAT_MBYTE
2464 orig_col1 = col1;
2465 orig_col2 = col2;
2466 #endif
2468 for (gui.row = row1; gui.row <= row2; gui.row++)
2470 #ifdef FEAT_MBYTE
2471 /* When only half of a double-wide character is in the block, include
2472 * the other half. */
2473 col1 = orig_col1;
2474 col2 = orig_col2;
2475 off = LineOffset[gui.row];
2476 if (enc_dbcs != 0)
2478 if (col1 > 0)
2479 col1 -= dbcs_screen_head_off(ScreenLines + off,
2480 ScreenLines + off + col1);
2481 col2 += dbcs_screen_tail_off(ScreenLines + off,
2482 ScreenLines + off + col2);
2484 else if (enc_utf8)
2486 if (ScreenLines[off + col1] == 0)
2487 --col1;
2488 # ifdef HAVE_GTK2
2489 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2490 ++col2;
2491 # endif
2493 #endif
2494 gui.col = col1;
2495 off = LineOffset[gui.row] + gui.col;
2496 len = col2 - col1 + 1;
2498 /* Find how many chars back this highlighting starts, or where a space
2499 * is. Needed for when the bold trick is used */
2500 for (back = 0; back < col1; ++back)
2501 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2502 || ScreenLines[off - 1 - back] == ' ')
2503 break;
2504 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2505 && ScreenLines[off - 1] != ' ');
2507 /* Break it up in strings of characters with the same attributes. */
2508 /* Print UTF-8 characters individually. */
2509 while (len > 0)
2511 first_attr = ScreenAttrs[off];
2512 gui.highlight_mask = first_attr;
2513 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2514 if (enc_utf8 && ScreenLinesUC[off] != 0)
2516 /* output multi-byte character separately */
2517 nback = gui_screenchar(off, flags,
2518 (guicolor_T)0, (guicolor_T)0, back);
2519 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2520 idx = 2;
2521 else
2522 idx = 1;
2524 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2526 /* output double-byte, single-width character separately */
2527 nback = gui_screenchar(off, flags,
2528 (guicolor_T)0, (guicolor_T)0, back);
2529 idx = 1;
2531 else
2532 #endif
2534 #ifdef HAVE_GTK2
2535 for (idx = 0; idx < len; ++idx)
2537 if (enc_utf8 && ScreenLines[off + idx] == 0)
2538 continue; /* skip second half of double-width char */
2539 if (ScreenAttrs[off + idx] != first_attr)
2540 break;
2542 /* gui_screenstr() takes care of multibyte chars */
2543 nback = gui_screenstr(off, idx, flags,
2544 (guicolor_T)0, (guicolor_T)0, back);
2545 #else
2546 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2547 idx++)
2549 # ifdef FEAT_MBYTE
2550 /* Stop at a multi-byte Unicode character. */
2551 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2552 break;
2553 if (enc_dbcs == DBCS_JPNU)
2555 /* Stop at a double-byte single-width char. */
2556 if (ScreenLines[off + idx] == 0x8e)
2557 break;
2558 if (len > 1 && (*mb_ptr2len)(ScreenLines
2559 + off + idx) == 2)
2560 ++idx; /* skip second byte of double-byte char */
2562 # endif
2564 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2565 (guicolor_T)0, (guicolor_T)0, back);
2566 #endif
2568 if (nback == FAIL)
2570 /* Must back up to start drawing where a bold or italic word
2571 * starts. */
2572 off -= back;
2573 len += back;
2574 gui.col -= back;
2576 else
2578 off += idx;
2579 len -= idx;
2581 back = 0;
2585 /* Put the cursor back where it was */
2586 gui.row = old_row;
2587 gui.col = old_col;
2588 gui.highlight_mask = (int)old_hl_mask;
2590 return retval;
2593 static void
2594 gui_delete_lines(row, count)
2595 int row;
2596 int count;
2598 if (count <= 0)
2599 return;
2601 if (row + count > gui.scroll_region_bot)
2602 /* Scrolled out of region, just blank the lines out */
2603 gui_clear_block(row, gui.scroll_region_left,
2604 gui.scroll_region_bot, gui.scroll_region_right);
2605 else
2607 gui_mch_delete_lines(row, count);
2609 /* If the cursor was in the deleted lines it's now gone. If the
2610 * cursor was in the scrolled lines adjust its position. */
2611 if (gui.cursor_row >= row
2612 && gui.cursor_col >= gui.scroll_region_left
2613 && gui.cursor_col <= gui.scroll_region_right)
2615 if (gui.cursor_row < row + count)
2616 gui.cursor_is_valid = FALSE;
2617 else if (gui.cursor_row <= gui.scroll_region_bot)
2618 gui.cursor_row -= count;
2623 static void
2624 gui_insert_lines(row, count)
2625 int row;
2626 int count;
2628 if (count <= 0)
2629 return;
2631 if (row + count > gui.scroll_region_bot)
2632 /* Scrolled out of region, just blank the lines out */
2633 gui_clear_block(row, gui.scroll_region_left,
2634 gui.scroll_region_bot, gui.scroll_region_right);
2635 else
2637 gui_mch_insert_lines(row, count);
2639 if (gui.cursor_row >= gui.row
2640 && gui.cursor_col >= gui.scroll_region_left
2641 && gui.cursor_col <= gui.scroll_region_right)
2643 if (gui.cursor_row <= gui.scroll_region_bot - count)
2644 gui.cursor_row += count;
2645 else if (gui.cursor_row <= gui.scroll_region_bot)
2646 gui.cursor_is_valid = FALSE;
2652 * The main GUI input routine. Waits for a character from the keyboard.
2653 * wtime == -1 Wait forever.
2654 * wtime == 0 Don't wait.
2655 * wtime > 0 Wait wtime milliseconds for a character.
2656 * Returns OK if a character was found to be available within the given time,
2657 * or FAIL otherwise.
2660 gui_wait_for_chars(wtime)
2661 long wtime;
2663 int retval;
2666 * If we're going to wait a bit, update the menus and mouse shape for the
2667 * current State.
2669 if (wtime != 0)
2671 #ifdef FEAT_MENU
2672 gui_update_menus(0);
2673 #endif
2676 gui_mch_update();
2677 if (input_available()) /* Got char, return immediately */
2678 return OK;
2679 if (wtime == 0) /* Don't wait for char */
2680 return FAIL;
2682 /* Before waiting, flush any output to the screen. */
2683 gui_mch_flush();
2685 if (wtime > 0)
2687 /* Blink when waiting for a character. Probably only does something
2688 * for showmatch() */
2689 gui_mch_start_blink();
2690 retval = gui_mch_wait_for_chars(wtime);
2691 gui_mch_stop_blink();
2692 return retval;
2696 * While we are waiting indefinitely for a character, blink the cursor.
2698 gui_mch_start_blink();
2700 retval = FAIL;
2702 * We may want to trigger the CursorHold event. First wait for
2703 * 'updatetime' and if nothing is typed within that time put the
2704 * K_CURSORHOLD key in the input buffer.
2706 if (gui_mch_wait_for_chars(p_ut) == OK)
2707 retval = OK;
2708 #ifdef FEAT_AUTOCMD
2709 else if (trigger_cursorhold())
2711 char_u buf[3];
2713 /* Put K_CURSORHOLD in the input buffer. */
2714 buf[0] = CSI;
2715 buf[1] = KS_EXTRA;
2716 buf[2] = (int)KE_CURSORHOLD;
2717 add_to_input_buf(buf, 3);
2719 retval = OK;
2721 #endif
2723 if (retval == FAIL)
2725 /* Blocking wait. */
2726 before_blocking();
2727 retval = gui_mch_wait_for_chars(-1L);
2730 gui_mch_stop_blink();
2731 return retval;
2735 * Fill p[4] with mouse coordinates encoded for check_termcode().
2737 static void
2738 fill_mouse_coord(p, col, row)
2739 char_u *p;
2740 int col;
2741 int row;
2743 p[0] = (char_u)(col / 128 + ' ' + 1);
2744 p[1] = (char_u)(col % 128 + ' ' + 1);
2745 p[2] = (char_u)(row / 128 + ' ' + 1);
2746 p[3] = (char_u)(row % 128 + ' ' + 1);
2750 * Generic mouse support function. Add a mouse event to the input buffer with
2751 * the given properties.
2752 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2753 * MOUSE_X1, MOUSE_X2
2754 * MOUSE_DRAG, or MOUSE_RELEASE.
2755 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2756 * x, y --- Coordinates of mouse in pixels.
2757 * repeated_click --- TRUE if this click comes only a short time after a
2758 * previous click.
2759 * modifiers --- Bit field which may be any of the following modifiers
2760 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2761 * This function will ignore drag events where the mouse has not moved to a new
2762 * character.
2764 void
2765 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2766 int button;
2767 int x;
2768 int y;
2769 int repeated_click;
2770 int_u modifiers;
2772 static int prev_row = 0, prev_col = 0;
2773 static int prev_button = -1;
2774 static int num_clicks = 1;
2775 char_u string[10];
2776 enum key_extra button_char;
2777 int row, col;
2778 #ifdef FEAT_CLIPBOARD
2779 int checkfor;
2780 int did_clip = FALSE;
2781 #endif
2784 * Scrolling may happen at any time, also while a selection is present.
2786 switch (button)
2788 case MOUSE_X1:
2789 button_char = KE_X1MOUSE;
2790 goto button_set;
2791 case MOUSE_X2:
2792 button_char = KE_X2MOUSE;
2793 goto button_set;
2794 case MOUSE_4:
2795 button_char = KE_MOUSEDOWN;
2796 goto button_set;
2797 case MOUSE_5:
2798 button_char = KE_MOUSEUP;
2799 button_set:
2801 /* Don't put events in the input queue now. */
2802 if (hold_gui_events)
2803 return;
2805 string[3] = CSI;
2806 string[4] = KS_EXTRA;
2807 string[5] = (int)button_char;
2809 /* Pass the pointer coordinates of the scroll event so that we
2810 * know which window to scroll. */
2811 row = gui_xy2colrow(x, y, &col);
2812 string[6] = (char_u)(col / 128 + ' ' + 1);
2813 string[7] = (char_u)(col % 128 + ' ' + 1);
2814 string[8] = (char_u)(row / 128 + ' ' + 1);
2815 string[9] = (char_u)(row % 128 + ' ' + 1);
2817 if (modifiers == 0)
2818 add_to_input_buf(string + 3, 7);
2819 else
2821 string[0] = CSI;
2822 string[1] = KS_MODIFIER;
2823 string[2] = 0;
2824 if (modifiers & MOUSE_SHIFT)
2825 string[2] |= MOD_MASK_SHIFT;
2826 if (modifiers & MOUSE_CTRL)
2827 string[2] |= MOD_MASK_CTRL;
2828 if (modifiers & MOUSE_ALT)
2829 string[2] |= MOD_MASK_ALT;
2830 add_to_input_buf(string, 10);
2832 return;
2836 #ifdef FEAT_CLIPBOARD
2837 /* If a clipboard selection is in progress, handle it */
2838 if (clip_star.state == SELECT_IN_PROGRESS)
2840 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2841 return;
2844 /* Determine which mouse settings to look for based on the current mode */
2845 switch (get_real_state())
2847 case NORMAL_BUSY:
2848 case OP_PENDING:
2849 case NORMAL: checkfor = MOUSE_NORMAL; break;
2850 case VISUAL: checkfor = MOUSE_VISUAL; break;
2851 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2852 case REPLACE:
2853 case REPLACE+LANGMAP:
2854 #ifdef FEAT_VREPLACE
2855 case VREPLACE:
2856 case VREPLACE+LANGMAP:
2857 #endif
2858 case INSERT:
2859 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2860 case ASKMORE:
2861 case HITRETURN: /* At the more- and hit-enter prompt pass the
2862 mouse event for a click on or below the
2863 message line. */
2864 if (Y_2_ROW(y) >= msg_row)
2865 checkfor = MOUSE_NORMAL;
2866 else
2867 checkfor = MOUSE_RETURN;
2868 break;
2871 * On the command line, use the clipboard selection on all lines
2872 * but the command line. But not when pasting.
2874 case CMDLINE:
2875 case CMDLINE+LANGMAP:
2876 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2877 checkfor = MOUSE_NONE;
2878 else
2879 checkfor = MOUSE_COMMAND;
2880 break;
2882 default:
2883 checkfor = MOUSE_NONE;
2884 break;
2888 * Allow clipboard selection of text on the command line in "normal"
2889 * modes. Don't do this when dragging the status line, or extending a
2890 * Visual selection.
2892 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2893 && Y_2_ROW(y) >= topframe->fr_height
2894 # ifdef FEAT_WINDOWS
2895 + firstwin->w_winrow
2896 # endif
2897 && button != MOUSE_DRAG
2898 # ifdef FEAT_MOUSESHAPE
2899 && !drag_status_line
2900 # ifdef FEAT_VERTSPLIT
2901 && !drag_sep_line
2902 # endif
2903 # endif
2905 checkfor = MOUSE_NONE;
2908 * Use modeless selection when holding CTRL and SHIFT pressed.
2910 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2911 checkfor = MOUSE_NONEF;
2914 * In Ex mode, always use modeless selection.
2916 if (exmode_active)
2917 checkfor = MOUSE_NONE;
2920 * If the mouse settings say to not use the mouse, use the modeless
2921 * selection. But if Visual is active, assume that only the Visual area
2922 * will be selected.
2923 * Exception: On the command line, both the selection is used and a mouse
2924 * key is send.
2926 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2928 #ifdef FEAT_VISUAL
2929 /* Don't do modeless selection in Visual mode. */
2930 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2931 return;
2932 #endif
2935 * When 'mousemodel' is "popup", shift-left is translated to right.
2936 * But not when also using Ctrl.
2938 if (mouse_model_popup() && button == MOUSE_LEFT
2939 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2941 button = MOUSE_RIGHT;
2942 modifiers &= ~ MOUSE_SHIFT;
2945 /* If the selection is done, allow the right button to extend it.
2946 * If the selection is cleared, allow the right button to start it
2947 * from the cursor position. */
2948 if (button == MOUSE_RIGHT)
2950 if (clip_star.state == SELECT_CLEARED)
2952 if (State & CMDLINE)
2954 col = msg_col;
2955 row = msg_row;
2957 else
2959 col = curwin->w_wcol;
2960 row = curwin->w_wrow + W_WINROW(curwin);
2962 clip_start_selection(col, row, FALSE);
2964 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2965 repeated_click);
2966 did_clip = TRUE;
2968 /* Allow the left button to start the selection */
2969 else if (button ==
2970 # ifdef RISCOS
2971 /* Only start a drag on a drag event. Otherwise
2972 * we don't get a release event. */
2973 MOUSE_DRAG
2974 # else
2975 MOUSE_LEFT
2976 # endif
2979 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2980 did_clip = TRUE;
2982 # ifdef RISCOS
2983 else if (button == MOUSE_LEFT)
2985 clip_clear_selection();
2986 did_clip = TRUE;
2988 # endif
2990 /* Always allow pasting */
2991 if (button != MOUSE_MIDDLE)
2993 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2994 return;
2995 if (checkfor != MOUSE_COMMAND)
2996 button = MOUSE_LEFT;
2998 repeated_click = FALSE;
3001 if (clip_star.state != SELECT_CLEARED && !did_clip)
3002 clip_clear_selection();
3003 #endif
3005 /* Don't put events in the input queue now. */
3006 if (hold_gui_events)
3007 return;
3009 row = gui_xy2colrow(x, y, &col);
3012 * If we are dragging and the mouse hasn't moved far enough to be on a
3013 * different character, then don't send an event to vim.
3015 if (button == MOUSE_DRAG)
3017 if (row == prev_row && col == prev_col)
3018 return;
3019 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3020 if (y < 0)
3021 row = -1;
3025 * If topline has changed (window scrolled) since the last click, reset
3026 * repeated_click, because we don't want starting Visual mode when
3027 * clicking on a different character in the text.
3029 if (curwin->w_topline != gui_prev_topline
3030 #ifdef FEAT_DIFF
3031 || curwin->w_topfill != gui_prev_topfill
3032 #endif
3034 repeated_click = FALSE;
3036 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3037 string[1] = KS_MOUSE;
3038 string[2] = KE_FILLER;
3039 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3041 if (repeated_click)
3044 * Handle multiple clicks. They only count if the mouse is still
3045 * pointing at the same character.
3047 if (button != prev_button || row != prev_row || col != prev_col)
3048 num_clicks = 1;
3049 else if (++num_clicks > 4)
3050 num_clicks = 1;
3052 else
3053 num_clicks = 1;
3054 prev_button = button;
3055 gui_prev_topline = curwin->w_topline;
3056 #ifdef FEAT_DIFF
3057 gui_prev_topfill = curwin->w_topfill;
3058 #endif
3060 string[3] = (char_u)(button | 0x20);
3061 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3063 else
3064 string[3] = (char_u)button;
3066 string[3] |= modifiers;
3067 fill_mouse_coord(string + 4, col, row);
3068 add_to_input_buf(string, 8);
3070 if (row < 0)
3071 prev_row = 0;
3072 else
3073 prev_row = row;
3074 prev_col = col;
3077 * We need to make sure this is cleared since Athena doesn't tell us when
3078 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3080 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3081 gui.dragged_sb = SBAR_NONE;
3082 #endif
3086 * Convert x and y coordinate to column and row in text window.
3087 * Corrects for multi-byte character.
3088 * returns column in "*colp" and row as return value;
3091 gui_xy2colrow(x, y, colp)
3092 int x;
3093 int y;
3094 int *colp;
3096 int col = check_col(X_2_COL(x));
3097 int row = check_row(Y_2_ROW(y));
3099 #ifdef FEAT_MBYTE
3100 *colp = mb_fix_col(col, row);
3101 #else
3102 *colp = col;
3103 #endif
3104 return row;
3107 #if defined(FEAT_MENU) || defined(PROTO)
3109 * Callback function for when a menu entry has been selected.
3111 void
3112 gui_menu_cb(menu)
3113 vimmenu_T *menu;
3115 char_u bytes[sizeof(long_u)];
3117 /* Don't put events in the input queue now. */
3118 if (hold_gui_events)
3119 return;
3121 bytes[0] = CSI;
3122 bytes[1] = KS_MENU;
3123 bytes[2] = KE_FILLER;
3124 add_to_input_buf(bytes, 3);
3125 add_long_to_buf((long_u)menu, bytes);
3126 add_to_input_buf_csi(bytes, sizeof(long_u));
3128 #endif
3130 static int prev_which_scrollbars[3];
3133 * Set which components are present.
3134 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3135 * in p_go.
3137 /*ARGSUSED*/
3138 void
3139 gui_init_which_components(oldval)
3140 char_u *oldval;
3142 #ifdef FEAT_MENU
3143 static int prev_menu_is_active = -1;
3144 #endif
3145 #ifdef FEAT_TOOLBAR
3146 static int prev_toolbar = -1;
3147 int using_toolbar = FALSE;
3148 #endif
3149 #ifdef FEAT_GUI_TABLINE
3150 int using_tabline;
3151 #endif
3152 #ifdef FEAT_FOOTER
3153 static int prev_footer = -1;
3154 int using_footer = FALSE;
3155 #endif
3156 #if defined(FEAT_MENU) && !defined(WIN16)
3157 static int prev_tearoff = -1;
3158 int using_tearoff = FALSE;
3159 #endif
3161 char_u *p;
3162 int i;
3163 #ifdef FEAT_MENU
3164 int grey_old, grey_new;
3165 char_u *temp;
3166 #endif
3167 win_T *wp;
3168 int need_set_size;
3169 int fix_size;
3171 #ifdef FEAT_MENU
3172 if (oldval != NULL && gui.in_use)
3175 * Check if the menu's go from grey to non-grey or vise versa.
3177 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3178 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3179 if (grey_old != grey_new)
3181 temp = p_go;
3182 p_go = oldval;
3183 gui_update_menus(MENU_ALL_MODES);
3184 p_go = temp;
3187 gui.menu_is_active = FALSE;
3188 #endif
3190 for (i = 0; i < 3; i++)
3191 gui.which_scrollbars[i] = FALSE;
3192 for (p = p_go; *p; p++)
3193 switch (*p)
3195 case GO_LEFT:
3196 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3197 break;
3198 case GO_RIGHT:
3199 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3200 break;
3201 #ifdef FEAT_VERTSPLIT
3202 case GO_VLEFT:
3203 if (win_hasvertsplit())
3204 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3205 break;
3206 case GO_VRIGHT:
3207 if (win_hasvertsplit())
3208 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3209 break;
3210 #endif
3211 case GO_BOT:
3212 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3213 break;
3214 #ifdef FEAT_MENU
3215 case GO_MENUS:
3216 gui.menu_is_active = TRUE;
3217 break;
3218 #endif
3219 case GO_GREY:
3220 /* make menu's have grey items, ignored here */
3221 break;
3222 #ifdef FEAT_TOOLBAR
3223 case GO_TOOLBAR:
3224 using_toolbar = TRUE;
3225 break;
3226 #endif
3227 #ifdef FEAT_FOOTER
3228 case GO_FOOTER:
3229 using_footer = TRUE;
3230 break;
3231 #endif
3232 case GO_TEAROFF:
3233 #if defined(FEAT_MENU) && !defined(WIN16)
3234 using_tearoff = TRUE;
3235 #endif
3236 break;
3237 default:
3238 /* Ignore options that are not supported */
3239 break;
3242 if (gui.in_use)
3244 need_set_size = 0;
3245 fix_size = FALSE;
3247 #ifdef FEAT_GUI_TABLINE
3248 /* Update the GUI tab line, it may appear or disappear. This may
3249 * cause the non-GUI tab line to disappear or appear. */
3250 using_tabline = gui_has_tabline();
3251 if (!gui_mch_showing_tabline() != !using_tabline)
3253 /* We don't want a resize event change "Rows" here, save and
3254 * restore it. Resizing is handled below. */
3255 i = Rows;
3256 gui_update_tabline();
3257 Rows = i;
3258 need_set_size = RESIZE_VERT;
3259 if (using_tabline)
3260 fix_size = TRUE;
3261 if (!gui_use_tabline())
3262 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3264 #endif
3266 for (i = 0; i < 3; i++)
3268 /* The scrollbar needs to be updated when it is shown/unshown and
3269 * when switching tab pages. But the size only changes when it's
3270 * shown/unshown. Thus we need two places to remember whether a
3271 * scrollbar is there or not. */
3272 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3273 #ifdef FEAT_WINDOWS
3274 || gui.which_scrollbars[i]
3275 != curtab->tp_prev_which_scrollbars[i]
3276 #endif
3279 if (i == SBAR_BOTTOM)
3280 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3281 gui.which_scrollbars[i]);
3282 else
3284 FOR_ALL_WINDOWS(wp)
3286 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3289 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3291 if (i == SBAR_BOTTOM)
3292 need_set_size = RESIZE_VERT;
3293 else
3294 need_set_size = RESIZE_HOR;
3295 if (gui.which_scrollbars[i])
3296 fix_size = TRUE;
3299 #ifdef FEAT_WINDOWS
3300 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3301 #endif
3302 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3305 #ifdef FEAT_MENU
3306 if (gui.menu_is_active != prev_menu_is_active)
3308 /* We don't want a resize event change "Rows" here, save and
3309 * restore it. Resizing is handled below. */
3310 i = Rows;
3311 gui_mch_enable_menu(gui.menu_is_active);
3312 Rows = i;
3313 prev_menu_is_active = gui.menu_is_active;
3314 need_set_size = RESIZE_VERT;
3315 if (gui.menu_is_active)
3316 fix_size = TRUE;
3318 #endif
3320 #ifdef FEAT_TOOLBAR
3321 if (using_toolbar != prev_toolbar)
3323 gui_mch_show_toolbar(using_toolbar);
3324 prev_toolbar = using_toolbar;
3325 need_set_size = RESIZE_VERT;
3326 if (using_toolbar)
3327 fix_size = TRUE;
3329 #endif
3330 #ifdef FEAT_FOOTER
3331 if (using_footer != prev_footer)
3333 gui_mch_enable_footer(using_footer);
3334 prev_footer = using_footer;
3335 need_set_size = RESIZE_VERT;
3336 if (using_footer)
3337 fix_size = TRUE;
3339 #endif
3340 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3341 if (using_tearoff != prev_tearoff)
3343 gui_mch_toggle_tearoffs(using_tearoff);
3344 prev_tearoff = using_tearoff;
3346 #endif
3347 if (need_set_size)
3349 #ifdef FEAT_GUI_GTK
3350 long c = Columns;
3351 #endif
3352 /* Adjust the size of the window to make the text area keep the
3353 * same size and to avoid that part of our window is off-screen
3354 * and a scrollbar can't be used, for example. */
3355 gui_set_shellsize(FALSE, fix_size, need_set_size);
3357 #ifdef FEAT_GUI_GTK
3358 /* GTK has the annoying habit of sending us resize events when
3359 * changing the window size ourselves. This mostly happens when
3360 * waiting for a character to arrive, quite unpredictably, and may
3361 * change Columns and Rows when we don't want it. Wait for a
3362 * character here to avoid this effect.
3363 * If you remove this, please test this command for resizing
3364 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3365 * Don't do this while starting up though.
3366 * And don't change Rows, it may have be reduced intentionally
3367 * when adding menu/toolbar/tabline. */
3368 if (!gui.starting)
3369 (void)char_avail();
3370 Columns = c;
3371 #endif
3373 #ifdef FEAT_WINDOWS
3374 /* When the console tabline appears or disappears the window positions
3375 * change. */
3376 if (firstwin->w_winrow != tabline_height())
3377 shell_new_rows(); /* recompute window positions and heights */
3378 #endif
3382 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3384 * Return TRUE if the GUI is taking care of the tabline.
3385 * It may still be hidden if 'showtabline' is zero.
3388 gui_use_tabline()
3390 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3394 * Return TRUE if the GUI is showing the tabline.
3395 * This uses 'showtabline'.
3397 static int
3398 gui_has_tabline()
3400 if (!gui_use_tabline()
3401 || p_stal == 0
3402 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3403 return FALSE;
3404 return TRUE;
3408 * Update the tabline.
3409 * This may display/undisplay the tabline and update the labels.
3411 void
3412 gui_update_tabline()
3414 int showit = gui_has_tabline();
3415 int shown = gui_mch_showing_tabline();
3417 if (!gui.starting && starting == 0)
3419 /* Updating the tabline uses direct GUI commands, flush
3420 * outstanding instructions first. (esp. clear screen) */
3421 out_flush();
3422 gui_mch_flush();
3424 if (!showit != !shown)
3425 gui_mch_show_tabline(showit);
3426 if (showit != 0)
3427 gui_mch_update_tabline();
3429 /* When the tabs change from hidden to shown or from shown to
3430 * hidden the size of the text area should remain the same. */
3431 if (!showit != !shown)
3432 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3437 * Get the label or tooltip for tab page "tp" into NameBuff[].
3439 void
3440 get_tabline_label(tp, tooltip)
3441 tabpage_T *tp;
3442 int tooltip; /* TRUE: get tooltip */
3444 int modified = FALSE;
3445 char_u buf[40];
3446 int wincount;
3447 win_T *wp;
3448 char_u **opt;
3450 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3451 opt = (tooltip ? &p_gtt : &p_gtl);
3452 if (**opt != NUL)
3454 int use_sandbox = FALSE;
3455 int save_called_emsg = called_emsg;
3456 char_u res[MAXPATHL];
3457 tabpage_T *save_curtab;
3458 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3459 : "guitablabel");
3461 called_emsg = FALSE;
3463 printer_page_num = tabpage_index(tp);
3464 # ifdef FEAT_EVAL
3465 set_vim_var_nr(VV_LNUM, printer_page_num);
3466 use_sandbox = was_set_insecurely(opt_name, 0);
3467 # endif
3468 /* It's almost as going to the tabpage, but without autocommands. */
3469 curtab->tp_firstwin = firstwin;
3470 curtab->tp_lastwin = lastwin;
3471 curtab->tp_curwin = curwin;
3472 save_curtab = curtab;
3473 curtab = tp;
3474 topframe = curtab->tp_topframe;
3475 firstwin = curtab->tp_firstwin;
3476 lastwin = curtab->tp_lastwin;
3477 curwin = curtab->tp_curwin;
3478 curbuf = curwin->w_buffer;
3480 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3481 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3482 0, (int)Columns, NULL, NULL);
3483 STRCPY(NameBuff, res);
3485 /* Back to the original curtab. */
3486 curtab = save_curtab;
3487 topframe = curtab->tp_topframe;
3488 firstwin = curtab->tp_firstwin;
3489 lastwin = curtab->tp_lastwin;
3490 curwin = curtab->tp_curwin;
3491 curbuf = curwin->w_buffer;
3493 if (called_emsg)
3494 set_string_option_direct(opt_name, -1,
3495 (char_u *)"", OPT_FREE, SID_ERROR);
3496 called_emsg |= save_called_emsg;
3499 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3500 * use a default label. */
3501 if (**opt == NUL || *NameBuff == NUL)
3503 /* Get the buffer name into NameBuff[] and shorten it. */
3504 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3505 if (!tooltip)
3506 shorten_dir(NameBuff);
3508 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3509 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3510 if (bufIsChanged(wp->w_buffer))
3511 modified = TRUE;
3512 if (modified || wincount > 1)
3514 if (wincount > 1)
3515 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3516 else
3517 buf[0] = NUL;
3518 if (modified)
3519 STRCAT(buf, "+");
3520 STRCAT(buf, " ");
3521 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3522 mch_memmove(NameBuff, buf, STRLEN(buf));
3528 * Send the event for clicking to select tab page "nr".
3529 * Returns TRUE if it was done, FALSE when skipped because we are already at
3530 * that tab page or the cmdline window is open.
3533 send_tabline_event(nr)
3534 int nr;
3536 char_u string[3];
3538 if (nr == tabpage_index(curtab))
3539 return FALSE;
3541 /* Don't put events in the input queue now. */
3542 if (hold_gui_events
3543 # ifdef FEAT_CMDWIN
3544 || cmdwin_type != 0
3545 # endif
3548 /* Set it back to the current tab page. */
3549 gui_mch_set_curtab(tabpage_index(curtab));
3550 return FALSE;
3553 string[0] = CSI;
3554 string[1] = KS_TABLINE;
3555 string[2] = KE_FILLER;
3556 add_to_input_buf(string, 3);
3557 string[0] = nr;
3558 add_to_input_buf_csi(string, 1);
3559 return TRUE;
3563 * Send a tabline menu event
3565 void
3566 send_tabline_menu_event(tabidx, event)
3567 int tabidx;
3568 int event;
3570 char_u string[3];
3572 /* Don't put events in the input queue now. */
3573 if (hold_gui_events)
3574 return;
3576 string[0] = CSI;
3577 string[1] = KS_TABMENU;
3578 string[2] = KE_FILLER;
3579 add_to_input_buf(string, 3);
3580 string[0] = tabidx;
3581 string[1] = (char_u)(long)event;
3582 add_to_input_buf_csi(string, 2);
3585 #endif
3588 * Scrollbar stuff:
3591 #if defined(FEAT_WINDOWS) || defined(PROTO)
3593 * Remove all scrollbars. Used before switching to another tab page.
3595 void
3596 gui_remove_scrollbars()
3598 int i;
3599 win_T *wp;
3601 for (i = 0; i < 3; i++)
3603 if (i == SBAR_BOTTOM)
3604 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3605 else
3607 FOR_ALL_WINDOWS(wp)
3609 gui_do_scrollbar(wp, i, FALSE);
3612 curtab->tp_prev_which_scrollbars[i] = -1;
3615 #endif
3617 void
3618 gui_create_scrollbar(sb, type, wp)
3619 scrollbar_T *sb;
3620 int type;
3621 win_T *wp;
3623 static int sbar_ident = 0;
3625 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3626 sb->wp = wp;
3627 sb->type = type;
3628 sb->value = 0;
3629 #ifdef FEAT_GUI_ATHENA
3630 sb->pixval = 0;
3631 #endif
3632 sb->size = 1;
3633 sb->max = 1;
3634 sb->top = 0;
3635 sb->height = 0;
3636 #ifdef FEAT_VERTSPLIT
3637 sb->width = 0;
3638 #endif
3639 sb->status_height = 0;
3640 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3644 * Find the scrollbar with the given index.
3646 scrollbar_T *
3647 gui_find_scrollbar(ident)
3648 long ident;
3650 win_T *wp;
3652 if (gui.bottom_sbar.ident == ident)
3653 return &gui.bottom_sbar;
3654 FOR_ALL_WINDOWS(wp)
3656 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3657 return &wp->w_scrollbars[SBAR_LEFT];
3658 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3659 return &wp->w_scrollbars[SBAR_RIGHT];
3661 return NULL;
3665 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3667 * For Win32, Macintosh and GTK+ 2:
3668 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3669 * you stop dragging the scrollbar. We get here each time the scrollbar is
3670 * dragged another pixel, but as far as the rest of vim goes, it thinks
3671 * we're just hanging in the call to DispatchMessage() in
3672 * process_message(). The DispatchMessage() call that hangs was passed a
3673 * mouse button click event in the scrollbar window. -- webb.
3675 * Solution: Do the scrolling right here. But only when allowed.
3676 * Ignore the scrollbars while executing an external command or when there
3677 * are still characters to be processed.
3679 void
3680 gui_drag_scrollbar(sb, value, still_dragging)
3681 scrollbar_T *sb;
3682 long value;
3683 int still_dragging;
3685 #ifdef FEAT_WINDOWS
3686 win_T *wp;
3687 #endif
3688 int sb_num;
3689 #ifdef USE_ON_FLY_SCROLL
3690 colnr_T old_leftcol = curwin->w_leftcol;
3691 # ifdef FEAT_SCROLLBIND
3692 linenr_T old_topline = curwin->w_topline;
3693 # endif
3694 # ifdef FEAT_DIFF
3695 int old_topfill = curwin->w_topfill;
3696 # endif
3697 #else
3698 char_u bytes[sizeof(long_u)];
3699 int byte_count;
3700 #endif
3702 if (sb == NULL)
3703 return;
3705 /* Don't put events in the input queue now. */
3706 if (hold_gui_events)
3707 return;
3709 #ifdef FEAT_CMDWIN
3710 if (cmdwin_type != 0 && sb->wp != curwin)
3711 return;
3712 #endif
3714 if (still_dragging)
3716 if (sb->wp == NULL)
3717 gui.dragged_sb = SBAR_BOTTOM;
3718 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3719 gui.dragged_sb = SBAR_LEFT;
3720 else
3721 gui.dragged_sb = SBAR_RIGHT;
3722 gui.dragged_wp = sb->wp;
3724 else
3726 gui.dragged_sb = SBAR_NONE;
3727 #ifdef HAVE_GTK2
3728 /* Keep the "dragged_wp" value until after the scrolling, for when the
3729 * moust button is released. GTK2 doesn't send the button-up event. */
3730 gui.dragged_wp = NULL;
3731 #endif
3734 /* Vertical sbar info is kept in the first sbar (the left one) */
3735 if (sb->wp != NULL)
3736 sb = &sb->wp->w_scrollbars[0];
3739 * Check validity of value
3741 if (value < 0)
3742 value = 0;
3743 #ifdef SCROLL_PAST_END
3744 else if (value > sb->max)
3745 value = sb->max;
3746 #else
3747 if (value > sb->max - sb->size + 1)
3748 value = sb->max - sb->size + 1;
3749 #endif
3751 sb->value = value;
3753 #ifdef USE_ON_FLY_SCROLL
3754 /* When not allowed to do the scrolling right now, return. */
3755 if (dont_scroll || input_available())
3756 return;
3757 #endif
3758 #ifdef FEAT_INS_EXPAND
3759 /* Disallow scrolling the current window when the completion popup menu is
3760 * visible. */
3761 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3762 return;
3763 #endif
3765 #ifdef FEAT_RIGHTLEFT
3766 if (sb->wp == NULL && curwin->w_p_rl)
3768 value = sb->max + 1 - sb->size - value;
3769 if (value < 0)
3770 value = 0;
3772 #endif
3774 if (sb->wp != NULL) /* vertical scrollbar */
3776 sb_num = 0;
3777 #ifdef FEAT_WINDOWS
3778 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3779 sb_num++;
3780 if (wp == NULL)
3781 return;
3782 #else
3783 if (sb->wp != curwin)
3784 return;
3785 #endif
3787 #ifdef USE_ON_FLY_SCROLL
3788 current_scrollbar = sb_num;
3789 scrollbar_value = value;
3790 if (State & NORMAL)
3792 gui_do_scroll();
3793 setcursor();
3795 else if (State & INSERT)
3797 ins_scroll();
3798 setcursor();
3800 else if (State & CMDLINE)
3802 if (msg_scrolled == 0)
3804 gui_do_scroll();
3805 redrawcmdline();
3808 # ifdef FEAT_FOLDING
3809 /* Value may have been changed for closed fold. */
3810 sb->value = sb->wp->w_topline - 1;
3811 # endif
3813 /* When dragging one scrollbar and there is another one at the other
3814 * side move the thumb of that one too. */
3815 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3816 gui_mch_set_scrollbar_thumb(
3817 &sb->wp->w_scrollbars[
3818 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3819 ? SBAR_LEFT : SBAR_RIGHT],
3820 sb->value, sb->size, sb->max);
3822 #else
3823 bytes[0] = CSI;
3824 bytes[1] = KS_VER_SCROLLBAR;
3825 bytes[2] = KE_FILLER;
3826 bytes[3] = (char_u)sb_num;
3827 byte_count = 4;
3828 #endif
3830 else
3832 #ifdef USE_ON_FLY_SCROLL
3833 scrollbar_value = value;
3835 if (State & NORMAL)
3836 gui_do_horiz_scroll();
3837 else if (State & INSERT)
3838 ins_horscroll();
3839 else if (State & CMDLINE)
3841 if (msg_scrolled == 0)
3843 gui_do_horiz_scroll();
3844 redrawcmdline();
3847 if (old_leftcol != curwin->w_leftcol)
3849 updateWindow(curwin); /* update window, status and cmdline */
3850 setcursor();
3852 #else
3853 bytes[0] = CSI;
3854 bytes[1] = KS_HOR_SCROLLBAR;
3855 bytes[2] = KE_FILLER;
3856 byte_count = 3;
3857 #endif
3860 #ifdef USE_ON_FLY_SCROLL
3861 # ifdef FEAT_SCROLLBIND
3863 * synchronize other windows, as necessary according to 'scrollbind'
3865 if (curwin->w_p_scb
3866 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3867 || (sb->wp == curwin && (curwin->w_topline != old_topline
3868 # ifdef FEAT_DIFF
3869 || curwin->w_topfill != old_topfill
3870 # endif
3871 ))))
3873 do_check_scrollbind(TRUE);
3874 /* need to update the window right here */
3875 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3876 if (wp->w_redr_type > 0)
3877 updateWindow(wp);
3878 setcursor();
3880 # endif
3881 out_flush();
3882 gui_update_cursor(FALSE, TRUE);
3883 #else
3884 add_to_input_buf(bytes, byte_count);
3885 add_long_to_buf((long_u)value, bytes);
3886 add_to_input_buf_csi(bytes, sizeof(long_u));
3887 #endif
3891 * Scrollbar stuff:
3894 void
3895 gui_update_scrollbars(force)
3896 int force; /* Force all scrollbars to get updated */
3898 win_T *wp;
3899 scrollbar_T *sb;
3900 long val, size, max; /* need 32 bits here */
3901 int which_sb;
3902 int h, y;
3903 #ifdef FEAT_VERTSPLIT
3904 static win_T *prev_curwin = NULL;
3905 #endif
3907 /* Update the horizontal scrollbar */
3908 gui_update_horiz_scrollbar(force);
3910 #ifndef WIN3264
3911 /* Return straight away if there is neither a left nor right scrollbar.
3912 * On MS-Windows this is required anyway for scrollwheel messages. */
3913 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3914 return;
3915 #endif
3918 * Don't want to update a scrollbar while we're dragging it. But if we
3919 * have both a left and right scrollbar, and we drag one of them, we still
3920 * need to update the other one.
3922 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3923 && gui.which_scrollbars[SBAR_LEFT]
3924 && gui.which_scrollbars[SBAR_RIGHT])
3927 * If we have two scrollbars and one of them is being dragged, just
3928 * copy the scrollbar position from the dragged one to the other one.
3930 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3931 if (gui.dragged_wp != NULL)
3932 gui_mch_set_scrollbar_thumb(
3933 &gui.dragged_wp->w_scrollbars[which_sb],
3934 gui.dragged_wp->w_scrollbars[0].value,
3935 gui.dragged_wp->w_scrollbars[0].size,
3936 gui.dragged_wp->w_scrollbars[0].max);
3939 /* avoid that moving components around generates events */
3940 ++hold_gui_events;
3942 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3944 if (wp->w_buffer == NULL) /* just in case */
3945 continue;
3946 /* Skip a scrollbar that is being dragged. */
3947 if (!force && (gui.dragged_sb == SBAR_LEFT
3948 || gui.dragged_sb == SBAR_RIGHT)
3949 && gui.dragged_wp == wp)
3950 continue;
3952 #ifdef SCROLL_PAST_END
3953 max = wp->w_buffer->b_ml.ml_line_count - 1;
3954 #else
3955 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3956 #endif
3957 if (max < 0) /* empty buffer */
3958 max = 0;
3959 val = wp->w_topline - 1;
3960 size = wp->w_height;
3961 #ifdef SCROLL_PAST_END
3962 if (val > max) /* just in case */
3963 val = max;
3964 #else
3965 if (size > max + 1) /* just in case */
3966 size = max + 1;
3967 if (val > max - size + 1)
3968 val = max - size + 1;
3969 #endif
3970 if (val < 0) /* minimal value is 0 */
3971 val = 0;
3974 * Scrollbar at index 0 (the left one) contains all the information.
3975 * It would be the same info for left and right so we just store it for
3976 * one of them.
3978 sb = &wp->w_scrollbars[0];
3981 * Note: no check for valid w_botline. If it's not valid the
3982 * scrollbars will be updated later anyway.
3984 if (size < 1 || wp->w_botline - 2 > max)
3987 * This can happen during changing files. Just don't update the
3988 * scrollbar for now.
3990 sb->height = 0; /* Force update next time */
3991 if (gui.which_scrollbars[SBAR_LEFT])
3992 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3993 if (gui.which_scrollbars[SBAR_RIGHT])
3994 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3995 continue;
3997 if (force || sb->height != wp->w_height
3998 #ifdef FEAT_WINDOWS
3999 || sb->top != wp->w_winrow
4000 || sb->status_height != wp->w_status_height
4001 # ifdef FEAT_VERTSPLIT
4002 || sb->width != wp->w_width
4003 || prev_curwin != curwin
4004 # endif
4005 #endif
4008 /* Height, width or position of scrollbar has changed. For
4009 * vertical split: curwin changed. */
4010 sb->height = wp->w_height;
4011 #ifdef FEAT_WINDOWS
4012 sb->top = wp->w_winrow;
4013 sb->status_height = wp->w_status_height;
4014 # ifdef FEAT_VERTSPLIT
4015 sb->width = wp->w_width;
4016 # endif
4017 #endif
4019 /* Calculate height and position in pixels */
4020 h = (sb->height + sb->status_height) * gui.char_height;
4021 y = sb->top * gui.char_height + gui.border_offset;
4022 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4023 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MACVIM))
4024 if (gui.menu_is_active)
4025 y += gui.menu_height;
4026 #endif
4028 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4029 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4030 # ifdef FEAT_GUI_ATHENA
4031 y += gui.toolbar_height;
4032 # else
4033 # ifdef FEAT_GUI_MSWIN
4034 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4035 # endif
4036 # endif
4037 #endif
4039 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4040 if (gui_has_tabline())
4041 y += gui.tabline_height;
4042 #endif
4044 #ifdef FEAT_WINDOWS
4045 if (wp->w_winrow == 0)
4046 #endif
4048 /* Height of top scrollbar includes width of top border */
4049 h += gui.border_offset;
4050 y -= gui.border_offset;
4052 if (gui.which_scrollbars[SBAR_LEFT])
4054 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4055 gui.left_sbar_x, y,
4056 gui.scrollbar_width, h);
4057 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4059 if (gui.which_scrollbars[SBAR_RIGHT])
4061 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4062 gui.right_sbar_x, y,
4063 gui.scrollbar_width, h);
4064 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4068 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4069 * checking if the thumb moved at least a pixel. Only do this for
4070 * Athena, most other GUIs require the update anyway to make the
4071 * arrows work. */
4072 #ifdef FEAT_GUI_ATHENA
4073 if (max == 0)
4074 y = 0;
4075 else
4076 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4077 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4078 #else
4079 if (force || sb->value != val || sb->size != size || sb->max != max)
4080 #endif
4082 /* Thumb of scrollbar has moved */
4083 sb->value = val;
4084 #ifdef FEAT_GUI_ATHENA
4085 sb->pixval = y;
4086 #endif
4087 sb->size = size;
4088 sb->max = max;
4089 if (gui.which_scrollbars[SBAR_LEFT]
4090 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4091 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4092 val, size, max);
4093 if (gui.which_scrollbars[SBAR_RIGHT]
4094 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4095 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4096 val, size, max);
4099 #ifdef FEAT_VERTSPLIT
4100 prev_curwin = curwin;
4101 #endif
4102 --hold_gui_events;
4106 * Enable or disable a scrollbar.
4107 * Check for scrollbars for vertically split windows which are not enabled
4108 * sometimes.
4110 static void
4111 gui_do_scrollbar(wp, which, enable)
4112 win_T *wp;
4113 int which; /* SBAR_LEFT or SBAR_RIGHT */
4114 int enable; /* TRUE to enable scrollbar */
4116 #ifdef FEAT_VERTSPLIT
4117 int midcol = curwin->w_wincol + curwin->w_width / 2;
4118 int has_midcol = (wp->w_wincol <= midcol
4119 && wp->w_wincol + wp->w_width >= midcol);
4121 /* Only enable scrollbars that contain the middle column of the current
4122 * window. */
4123 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4125 /* Scrollbars only on one side. Don't enable scrollbars that don't
4126 * contain the middle column of the current window. */
4127 if (!has_midcol)
4128 enable = FALSE;
4130 else
4132 /* Scrollbars on both sides. Don't enable scrollbars that neither
4133 * contain the middle column of the current window nor are on the far
4134 * side. */
4135 if (midcol > Columns / 2)
4137 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4138 enable = FALSE;
4140 else
4142 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4143 : !has_midcol)
4144 enable = FALSE;
4147 #endif
4148 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4152 * Scroll a window according to the values set in the globals current_scrollbar
4153 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4154 * or FALSE otherwise.
4157 gui_do_scroll()
4159 win_T *wp, *save_wp;
4160 int i;
4161 long nlines;
4162 pos_T old_cursor;
4163 linenr_T old_topline;
4164 #ifdef FEAT_DIFF
4165 int old_topfill;
4166 #endif
4168 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4169 if (wp == NULL)
4170 break;
4171 if (wp == NULL)
4172 /* Couldn't find window */
4173 return FALSE;
4176 * Compute number of lines to scroll. If zero, nothing to do.
4178 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4179 if (nlines == 0)
4180 return FALSE;
4182 save_wp = curwin;
4183 old_topline = wp->w_topline;
4184 #ifdef FEAT_DIFF
4185 old_topfill = wp->w_topfill;
4186 #endif
4187 old_cursor = wp->w_cursor;
4188 curwin = wp;
4189 curbuf = wp->w_buffer;
4190 if (nlines < 0)
4191 scrolldown(-nlines, gui.dragged_wp == NULL);
4192 else
4193 scrollup(nlines, gui.dragged_wp == NULL);
4194 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4195 * the mouse-up event already, but we still want it to behave like when
4196 * dragging. But not the next click in an arrow. */
4197 if (gui.dragged_sb == SBAR_NONE)
4198 gui.dragged_wp = NULL;
4200 if (old_topline != wp->w_topline
4201 #ifdef FEAT_DIFF
4202 || old_topfill != wp->w_topfill
4203 #endif
4206 if (p_so != 0)
4208 cursor_correct(); /* fix window for 'so' */
4209 update_topline(); /* avoid up/down jump */
4211 if (old_cursor.lnum != wp->w_cursor.lnum)
4212 coladvance(wp->w_curswant);
4213 #ifdef FEAT_SCROLLBIND
4214 wp->w_scbind_pos = wp->w_topline;
4215 #endif
4218 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4219 validate_cursor();
4221 curwin = save_wp;
4222 curbuf = save_wp->w_buffer;
4225 * Don't call updateWindow() when nothing has changed (it will overwrite
4226 * the status line!).
4228 if (old_topline != wp->w_topline
4229 || wp->w_redr_type != 0
4230 #ifdef FEAT_DIFF
4231 || old_topfill != wp->w_topfill
4232 #endif
4235 redraw_win_later(wp, VALID);
4236 updateWindow(wp); /* update window, status line, and cmdline */
4239 #ifdef FEAT_INS_EXPAND
4240 /* May need to redraw the popup menu. */
4241 if (pum_visible())
4242 pum_redraw();
4243 #endif
4245 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4250 * Horizontal scrollbar stuff:
4254 * Return length of line "lnum" for horizontal scrolling.
4256 static colnr_T
4257 scroll_line_len(lnum)
4258 linenr_T lnum;
4260 char_u *p;
4261 colnr_T col;
4262 int w;
4264 p = ml_get(lnum);
4265 col = 0;
4266 if (*p != NUL)
4267 for (;;)
4269 w = chartabsize(p, col);
4270 mb_ptr_adv(p);
4271 if (*p == NUL) /* don't count the last character */
4272 break;
4273 col += w;
4275 return col;
4278 /* Remember which line is currently the longest, so that we don't have to
4279 * search for it when scrolling horizontally. */
4280 static linenr_T longest_lnum = 0;
4282 static void
4283 gui_update_horiz_scrollbar(force)
4284 int force;
4286 long value, size, max; /* need 32 bit ints here */
4288 if (!gui.which_scrollbars[SBAR_BOTTOM])
4289 return;
4291 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4292 return;
4294 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4295 return;
4298 * It is possible for the cursor to be invalid if we're in the middle of
4299 * something (like changing files). If so, don't do anything for now.
4301 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4303 gui.bottom_sbar.value = -1;
4304 return;
4307 size = W_WIDTH(curwin);
4308 if (curwin->w_p_wrap)
4310 value = 0;
4311 #ifdef SCROLL_PAST_END
4312 max = 0;
4313 #else
4314 max = W_WIDTH(curwin) - 1;
4315 #endif
4317 else
4319 value = curwin->w_leftcol;
4321 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4322 * line numbers, topline and botline can be invalid when displaying is
4323 * postponed. */
4324 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4325 && curwin->w_topline <= curwin->w_cursor.lnum
4326 && curwin->w_botline > curwin->w_cursor.lnum
4327 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4329 linenr_T lnum;
4330 colnr_T n;
4332 /* Use maximum of all visible lines. Remember the lnum of the
4333 * longest line, clostest to the cursor line. Used when scrolling
4334 * below. */
4335 max = 0;
4336 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4338 n = scroll_line_len(lnum);
4339 if (n > (colnr_T)max)
4341 max = n;
4342 longest_lnum = lnum;
4344 else if (n == (colnr_T)max
4345 && abs((int)(lnum - curwin->w_cursor.lnum))
4346 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4347 longest_lnum = lnum;
4350 else
4351 /* Use cursor line only. */
4352 max = scroll_line_len(curwin->w_cursor.lnum);
4353 #ifdef FEAT_VIRTUALEDIT
4354 if (virtual_active())
4356 /* May move the cursor even further to the right. */
4357 if (curwin->w_virtcol >= (colnr_T)max)
4358 max = curwin->w_virtcol;
4360 #endif
4362 #ifndef SCROLL_PAST_END
4363 max += W_WIDTH(curwin) - 1;
4364 #endif
4365 /* The line number isn't scrolled, thus there is less space when
4366 * 'number' is set (also for 'foldcolumn'). */
4367 size -= curwin_col_off();
4368 #ifndef SCROLL_PAST_END
4369 max -= curwin_col_off();
4370 #endif
4373 #ifndef SCROLL_PAST_END
4374 if (value > max - size + 1)
4375 value = max - size + 1; /* limit the value to allowable range */
4376 #endif
4378 #ifdef FEAT_RIGHTLEFT
4379 if (curwin->w_p_rl)
4381 value = max + 1 - size - value;
4382 if (value < 0)
4384 size += value;
4385 value = 0;
4388 #endif
4389 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4390 && max == gui.bottom_sbar.max)
4391 return;
4393 gui.bottom_sbar.value = value;
4394 gui.bottom_sbar.size = size;
4395 gui.bottom_sbar.max = max;
4396 gui.prev_wrap = curwin->w_p_wrap;
4398 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4402 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4405 gui_do_horiz_scroll()
4407 /* no wrapping, no scrolling */
4408 if (curwin->w_p_wrap)
4409 return FALSE;
4411 if (curwin->w_leftcol == scrollbar_value)
4412 return FALSE;
4414 curwin->w_leftcol = (colnr_T)scrollbar_value;
4416 /* When the line of the cursor is too short, move the cursor to the
4417 * longest visible line. Do a sanity check on "longest_lnum", just in
4418 * case. */
4419 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4420 && longest_lnum >= curwin->w_topline
4421 && longest_lnum < curwin->w_botline
4422 && !virtual_active())
4424 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4426 curwin->w_cursor.lnum = longest_lnum;
4427 curwin->w_cursor.col = 0;
4431 return leftcol_changed();
4435 * Check that none of the colors are the same as the background color
4437 void
4438 gui_check_colors()
4440 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4442 gui_set_bg_color((char_u *)"White");
4443 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4444 gui_set_fg_color((char_u *)"Black");
4448 static void
4449 gui_set_fg_color(name)
4450 char_u *name;
4452 gui.norm_pixel = gui_get_color(name);
4453 hl_set_fg_color_name(vim_strsave(name));
4456 static void
4457 gui_set_bg_color(name)
4458 char_u *name;
4460 gui.back_pixel = gui_get_color(name);
4461 hl_set_bg_color_name(vim_strsave(name));
4465 * Allocate a color by name.
4466 * Returns INVALCOLOR and gives an error message when failed.
4468 guicolor_T
4469 gui_get_color(name)
4470 char_u *name;
4472 guicolor_T t;
4474 if (*name == NUL)
4475 return INVALCOLOR;
4476 t = gui_mch_get_color(name);
4478 if (t == INVALCOLOR
4479 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4480 && gui.in_use
4481 #endif
4483 EMSG2(_("E254: Cannot allocate color %s"), name);
4484 return t;
4488 * Return the grey value of a color (range 0-255).
4491 gui_get_lightness(pixel)
4492 guicolor_T pixel;
4494 long_u rgb = gui_mch_get_rgb(pixel);
4496 return (int)( (((rgb >> 16) & 0xff) * 299)
4497 + (((rgb >> 8) & 0xff) * 587)
4498 + ((rgb & 0xff) * 114)) / 1000;
4501 #if defined(FEAT_GUI_X11) || defined(PROTO)
4502 void
4503 gui_new_scrollbar_colors()
4505 win_T *wp;
4507 /* Nothing to do if GUI hasn't started yet. */
4508 if (!gui.in_use)
4509 return;
4511 FOR_ALL_WINDOWS(wp)
4513 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4514 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4516 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4518 #endif
4521 * Call this when focus has changed.
4523 void
4524 gui_focus_change(in_focus)
4525 int in_focus;
4528 * Skip this code to avoid drawing the cursor when debugging and switching
4529 * between the debugger window and gvim.
4531 #if 1
4532 gui.in_focus = in_focus;
4533 out_flush(); /* make sure output has been written */
4534 gui_update_cursor(TRUE, FALSE);
4536 # ifdef FEAT_XIM
4537 xim_set_focus(in_focus);
4538 # endif
4540 /* Put events in the input queue only when allowed.
4541 * ui_focus_change() isn't called directly, because it invokes
4542 * autocommands and that must not happen asynchronously. */
4543 if (!hold_gui_events)
4545 char_u bytes[3];
4547 bytes[0] = CSI;
4548 bytes[1] = KS_EXTRA;
4549 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4550 add_to_input_buf(bytes, 3);
4552 #endif
4556 * Called when the mouse moved (but not when dragging).
4558 void
4559 gui_mouse_moved(x, y)
4560 int x;
4561 int y;
4563 win_T *wp;
4564 char_u st[8];
4566 /* Ignore this while still starting up. */
4567 if (!gui.in_use || gui.starting)
4568 return;
4570 #ifdef FEAT_MOUSESHAPE
4571 /* Get window pointer, and update mouse shape as well. */
4572 wp = xy2win(x, y);
4573 #endif
4575 /* Only handle this when 'mousefocus' set and ... */
4576 if (p_mousef
4577 && !hold_gui_events /* not holding events */
4578 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4579 && State != HITRETURN /* but not hit-return prompt */
4580 && msg_scrolled == 0 /* no scrolled message */
4581 && !need_mouse_correct /* not moving the pointer */
4582 && gui.in_focus) /* gvim in focus */
4584 /* Don't move the mouse when it's left or right of the Vim window */
4585 if (x < 0 || x > Columns * gui.char_width)
4586 return;
4587 #ifndef FEAT_MOUSESHAPE
4588 wp = xy2win(x, y);
4589 #endif
4590 if (wp == curwin || wp == NULL)
4591 return; /* still in the same old window, or none at all */
4593 #ifdef FEAT_WINDOWS
4594 /* Ignore position in the tab pages line. */
4595 if (Y_2_ROW(y) < tabline_height())
4596 return;
4597 #endif
4600 * format a mouse click on status line input
4601 * ala gui_send_mouse_event(0, x, y, 0, 0);
4602 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4603 * generate a K_LEFTMOUSE_NM key code.
4605 if (finish_op)
4607 /* abort the current operator first */
4608 st[0] = ESC;
4609 add_to_input_buf(st, 1);
4611 st[0] = CSI;
4612 st[1] = KS_MOUSE;
4613 st[2] = KE_FILLER;
4614 st[3] = (char_u)MOUSE_LEFT;
4615 fill_mouse_coord(st + 4,
4616 #ifdef FEAT_VERTSPLIT
4617 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4618 #else
4620 #endif
4621 wp->w_height + W_WINROW(wp));
4623 add_to_input_buf(st, 8);
4624 st[3] = (char_u)MOUSE_RELEASE;
4625 add_to_input_buf(st, 8);
4626 #ifdef FEAT_GUI_GTK
4627 /* Need to wake up the main loop */
4628 if (gtk_main_level() > 0)
4629 gtk_main_quit();
4630 #endif
4635 * Called when mouse should be moved to window with focus.
4637 void
4638 gui_mouse_correct()
4640 int x, y;
4641 win_T *wp = NULL;
4643 need_mouse_correct = FALSE;
4645 if (!(gui.in_use && p_mousef))
4646 return;
4648 gui_mch_getmouse(&x, &y);
4649 /* Don't move the mouse when it's left or right of the Vim window */
4650 if (x < 0 || x > Columns * gui.char_width)
4651 return;
4652 if (y >= 0
4653 # ifdef FEAT_WINDOWS
4654 && Y_2_ROW(y) >= tabline_height()
4655 # endif
4657 wp = xy2win(x, y);
4658 if (wp != curwin && wp != NULL) /* If in other than current window */
4660 validate_cline_row();
4661 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4662 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4663 + (gui.char_height) / 2);
4668 * Find window where the mouse pointer "y" coordinate is in.
4670 /*ARGSUSED*/
4671 static win_T *
4672 xy2win(x, y)
4673 int x;
4674 int y;
4676 #ifdef FEAT_WINDOWS
4677 int row;
4678 int col;
4679 win_T *wp;
4681 row = Y_2_ROW(y);
4682 col = X_2_COL(x);
4683 if (row < 0 || col < 0) /* before first window */
4684 return NULL;
4685 wp = mouse_find_win(&row, &col);
4686 # ifdef FEAT_MOUSESHAPE
4687 if (State == HITRETURN || State == ASKMORE)
4689 if (Y_2_ROW(y) >= msg_row)
4690 update_mouseshape(SHAPE_IDX_MOREL);
4691 else
4692 update_mouseshape(SHAPE_IDX_MORE);
4694 else if (row > wp->w_height) /* below status line */
4695 update_mouseshape(SHAPE_IDX_CLINE);
4696 # ifdef FEAT_VERTSPLIT
4697 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4698 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4699 update_mouseshape(SHAPE_IDX_VSEP);
4700 # endif
4701 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4702 && row == wp->w_height && msg_scrolled == 0)
4703 update_mouseshape(SHAPE_IDX_STATUS);
4704 else
4705 update_mouseshape(-2);
4706 # endif
4707 return wp;
4708 #else
4709 return firstwin;
4710 #endif
4714 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4715 * File names may be given to redefine the args list.
4717 void
4718 ex_gui(eap)
4719 exarg_T *eap;
4721 char_u *arg = eap->arg;
4724 * Check for "-f" argument: foreground, don't fork.
4725 * Also don't fork when started with "gvim -f".
4726 * Do fork when using "gui -b".
4728 if (arg[0] == '-'
4729 && (arg[1] == 'f' || arg[1] == 'b')
4730 && (arg[2] == NUL || vim_iswhite(arg[2])))
4732 gui.dofork = (arg[1] == 'b');
4733 eap->arg = skipwhite(eap->arg + 2);
4735 if (!gui.in_use)
4737 /* Clear the command. Needed for when forking+exiting, to avoid part
4738 * of the argument ending up after the shell prompt. */
4739 msg_clr_eos_force();
4740 gui_start();
4742 if (!ends_excmd(*eap->arg))
4743 ex_next(eap);
4746 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4747 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4749 * This is shared between Athena, Motif and GTK.
4751 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4754 * Callback function for do_in_runtimepath().
4756 static void
4757 gfp_setname(fname, cookie)
4758 char_u *fname;
4759 void *cookie;
4761 char_u *gfp_buffer = cookie;
4763 if (STRLEN(fname) >= MAXPATHL)
4764 *gfp_buffer = NUL;
4765 else
4766 STRCPY(gfp_buffer, fname);
4770 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4771 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4774 gui_find_bitmap(name, buffer, ext)
4775 char_u *name;
4776 char_u *buffer;
4777 char *ext;
4779 if (STRLEN(name) > MAXPATHL - 14)
4780 return FAIL;
4781 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4782 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4783 || *buffer == NUL)
4784 return FAIL;
4785 return OK;
4788 # if !defined(HAVE_GTK2) || defined(PROTO)
4790 * Given the name of the "icon=" argument, try finding the bitmap file for the
4791 * icon. If it is an absolute path name, use it as it is. Otherwise append
4792 * "ext" and search for it in 'runtimepath'.
4793 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4794 * contains "name".
4796 void
4797 gui_find_iconfile(name, buffer, ext)
4798 char_u *name;
4799 char_u *buffer;
4800 char *ext;
4802 char_u buf[MAXPATHL + 1];
4804 expand_env(name, buffer, MAXPATHL);
4805 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4806 STRCPY(buffer, buf);
4808 # endif
4809 #endif
4811 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4812 void
4813 display_errors()
4815 char_u *p;
4817 if (isatty(2))
4818 fflush(stderr);
4819 else if (error_ga.ga_data != NULL)
4821 /* avoid putting up a message box with blanks only */
4822 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4823 if (!isspace(*p))
4825 /* Truncate a very long message, it will go off-screen. */
4826 if (STRLEN(p) > 2000)
4827 STRCPY(p + 2000 - 14, "...(truncated)");
4828 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4829 p, (char_u *)_("&Ok"), 1, NULL);
4830 break;
4832 ga_clear(&error_ga);
4835 #endif
4837 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4839 * Return TRUE if still starting up and there is no place to enter text.
4840 * For GTK and X11 we check if stderr is not a tty, which means we were
4841 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4842 * allow typing on stdin.
4845 no_console_input()
4847 return ((!gui.in_use || gui.starting)
4848 # ifndef NO_CONSOLE
4849 && !isatty(0) && !isatty(2)
4850 # endif
4853 #endif
4855 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4856 || defined(NEED_GUI_UPDATE_SCREEN) \
4857 || defined(PROTO)
4859 * Update the current window and the screen.
4861 void
4862 gui_update_screen()
4864 update_topline();
4865 validate_cursor();
4866 update_screen(0); /* may need to update the screen */
4867 setcursor();
4868 out_flush(); /* make sure output has been written */
4869 gui_update_cursor(TRUE, FALSE);
4870 gui_mch_flush();
4872 #endif
4874 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4875 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4878 * Get the text to use in a find/replace dialog. Uses the last search pattern
4879 * if the argument is empty.
4880 * Returns an allocated string.
4882 char_u *
4883 get_find_dialog_text(arg, wwordp, mcasep)
4884 char_u *arg;
4885 int *wwordp; /* return: TRUE if \< \> found */
4886 int *mcasep; /* return: TRUE if \C found */
4888 char_u *text;
4890 if (*arg == NUL)
4891 text = last_search_pat();
4892 else
4893 text = arg;
4894 if (text != NULL)
4896 text = vim_strsave(text);
4897 if (text != NULL)
4899 int len = (int)STRLEN(text);
4900 int i;
4902 /* Remove "\V" */
4903 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4905 mch_memmove(text, text + 2, (size_t)(len - 1));
4906 len -= 2;
4909 /* Recognize "\c" and "\C" and remove. */
4910 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4912 *mcasep = (text[1] == 'C');
4913 mch_memmove(text, text + 2, (size_t)(len - 1));
4914 len -= 2;
4917 /* Recognize "\<text\>" and remove. */
4918 if (len >= 4
4919 && STRNCMP(text, "\\<", 2) == 0
4920 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4922 *wwordp = TRUE;
4923 mch_memmove(text, text + 2, (size_t)(len - 4));
4924 text[len - 4] = NUL;
4927 /* Recognize "\/" or "\?" and remove. */
4928 for (i = 0; i + 1 < len; ++i)
4929 if (text[i] == '\\' && (text[i + 1] == '/'
4930 || text[i + 1] == '?'))
4932 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4933 --len;
4937 return text;
4941 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4943 static void
4944 concat_esc(gap, text, what)
4945 garray_T *gap;
4946 char_u *text;
4947 int what;
4949 while (*text != NUL)
4951 #ifdef FEAT_MBYTE
4952 int l = (*mb_ptr2len)(text);
4954 if (l > 1)
4956 while (--l >= 0)
4957 ga_append(gap, *text++);
4958 continue;
4960 #endif
4961 if (*text == what)
4962 ga_append(gap, '\\');
4963 ga_append(gap, *text);
4964 ++text;
4969 * Handle the press of a button in the find-replace dialog.
4970 * Return TRUE when something was added to the input buffer.
4973 gui_do_findrepl(flags, find_text, repl_text, down)
4974 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4975 char_u *find_text;
4976 char_u *repl_text;
4977 int down; /* Search downwards. */
4979 garray_T ga;
4980 int i;
4981 int type = (flags & FRD_TYPE_MASK);
4982 char_u *p;
4983 regmatch_T regmatch;
4984 int save_did_emsg = did_emsg;
4986 ga_init2(&ga, 1, 100);
4987 if (type == FRD_REPLACEALL)
4988 ga_concat(&ga, (char_u *)"%s/");
4990 ga_concat(&ga, (char_u *)"\\V");
4991 if (flags & FRD_MATCH_CASE)
4992 ga_concat(&ga, (char_u *)"\\C");
4993 else
4994 ga_concat(&ga, (char_u *)"\\c");
4995 if (flags & FRD_WHOLE_WORD)
4996 ga_concat(&ga, (char_u *)"\\<");
4997 if (type == FRD_REPLACEALL || down)
4998 concat_esc(&ga, find_text, '/'); /* escape slashes */
4999 else
5000 concat_esc(&ga, find_text, '?'); /* escape '?' */
5001 if (flags & FRD_WHOLE_WORD)
5002 ga_concat(&ga, (char_u *)"\\>");
5004 if (type == FRD_REPLACEALL)
5006 ga_concat(&ga, (char_u *)"/");
5007 /* escape / and \ */
5008 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5009 if (p != NULL)
5010 ga_concat(&ga, p);
5011 vim_free(p);
5012 ga_concat(&ga, (char_u *)"/g");
5014 ga_append(&ga, NUL);
5016 if (type == FRD_REPLACE)
5018 /* Do the replacement when the text at the cursor matches. Thus no
5019 * replacement is done if the cursor was moved! */
5020 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5021 regmatch.rm_ic = 0;
5022 if (regmatch.regprog != NULL)
5024 p = ml_get_cursor();
5025 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5026 && regmatch.startp[0] == p)
5028 /* Clear the command line to remove any old "No match"
5029 * error. */
5030 msg_end_prompt();
5032 if (u_save_cursor() == OK)
5034 /* A button was pressed thus undo should be synced. */
5035 u_sync(FALSE);
5037 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5038 FALSE, FALSE);
5039 ins_str(repl_text);
5042 else
5043 MSG(_("No match at cursor, finding next"));
5044 vim_free(regmatch.regprog);
5048 if (type == FRD_REPLACEALL)
5050 /* A button was pressed, thus undo should be synced. */
5051 u_sync(FALSE);
5052 do_cmdline_cmd(ga.ga_data);
5054 else
5056 /* Search for the next match. */
5057 i = msg_scroll;
5058 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5059 SEARCH_MSG + SEARCH_MARK);
5060 msg_scroll = i; /* don't let an error message set msg_scroll */
5063 /* Don't want to pass did_emsg to other code, it may cause disabling
5064 * syntax HL if we were busy redrawing. */
5065 did_emsg = save_did_emsg;
5067 if (State & (NORMAL | INSERT))
5069 gui_update_screen(); /* update the screen */
5070 msg_didout = 0; /* overwrite any message */
5071 need_wait_return = FALSE; /* don't wait for return */
5074 vim_free(ga.ga_data);
5075 return (ga.ga_len > 0);
5078 #endif
5080 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5081 || defined(FEAT_GUI_MSWIN) \
5082 || defined(FEAT_GUI_MAC) \
5083 || defined(PROTO) \
5084 || defined(FEAT_GUI_MACVIM)
5086 #ifdef FEAT_WINDOWS
5087 static void gui_wingoto_xy __ARGS((int x, int y));
5090 * Jump to the window at specified point (x, y).
5092 static void
5093 gui_wingoto_xy(x, y)
5094 int x;
5095 int y;
5097 int row = Y_2_ROW(y);
5098 int col = X_2_COL(x);
5099 win_T *wp;
5101 if (row >= 0 && col >= 0)
5103 wp = mouse_find_win(&row, &col);
5104 if (wp != NULL && wp != curwin)
5105 win_goto(wp);
5108 #endif
5111 * Process file drop. Mouse cursor position, key modifiers, name of files
5112 * and count of files are given. Argument "fnames[count]" has full pathnames
5113 * of dropped files, they will be freed in this function, and caller can't use
5114 * fnames after call this function.
5116 /*ARGSUSED*/
5117 void
5118 gui_handle_drop(x, y, modifiers, fnames, count)
5119 int x;
5120 int y;
5121 int_u modifiers;
5122 char_u **fnames;
5123 int count;
5125 int i;
5126 char_u *p;
5129 * When the cursor is at the command line, add the file names to the
5130 * command line, don't edit the files.
5132 if (State & CMDLINE)
5134 shorten_filenames(fnames, count);
5135 for (i = 0; i < count; ++i)
5137 if (fnames[i] != NULL)
5139 if (i > 0)
5140 add_to_input_buf((char_u*)" ", 1);
5142 /* We don't know what command is used thus we can't be sure
5143 * about which characters need to be escaped. Only escape the
5144 * most common ones. */
5145 # ifdef BACKSLASH_IN_FILENAME
5146 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5147 # else
5148 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5149 # endif
5150 if (p != NULL)
5151 add_to_input_buf_csi(p, (int)STRLEN(p));
5152 vim_free(p);
5153 vim_free(fnames[i]);
5156 vim_free(fnames);
5158 else
5160 /* Go to the window under mouse cursor, then shorten given "fnames" by
5161 * current window, because a window can have local current dir. */
5162 # ifdef FEAT_WINDOWS
5163 gui_wingoto_xy(x, y);
5164 # endif
5165 shorten_filenames(fnames, count);
5167 /* If Shift held down, remember the first item. */
5168 if ((modifiers & MOUSE_SHIFT) != 0)
5169 p = vim_strsave(fnames[0]);
5170 else
5171 p = NULL;
5173 /* Handle the drop, :edit or :split to get to the file. This also
5174 * frees fnames[]. Skip this if there is only one item it's a
5175 * directory and Shift is held down. */
5176 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5177 && mch_isdir(fnames[0]))
5179 vim_free(fnames[0]);
5180 vim_free(fnames);
5182 else
5183 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5185 /* If Shift held down, change to first file's directory. If the first
5186 * item is a directory, change to that directory (and let the explorer
5187 * plugin show the contents). */
5188 if (p != NULL)
5190 if (mch_isdir(p))
5192 if (mch_chdir((char *)p) == 0)
5193 shorten_fnames(TRUE);
5195 else if (vim_chdirfile(p) == OK)
5196 shorten_fnames(TRUE);
5197 vim_free(p);
5200 /* Update the screen display */
5201 update_screen(NOT_VALID);
5202 # ifdef FEAT_MENU
5203 gui_update_menus(0);
5204 # endif
5205 setcursor();
5206 out_flush();
5207 gui_update_cursor(FALSE, FALSE);
5208 gui_mch_flush();
5211 #endif