Merged from the latest developing branch.
[MacVim/jjgod.git] / src / gui.c
blobb2b8e8499d3e772217a0dc379961da8dcb0054a6
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 # ifndef FEAT_GUI_GTK
272 gui.menu_height = MENU_DEFAULT_HEIGHT;
273 gui.menu_width = 0;
274 # endif
275 #endif
276 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
277 gui.toolbar_height = 0;
278 #endif
279 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
280 gui.footer_height = 0;
281 #endif
282 #ifdef FEAT_BEVAL_TIP
283 gui.tooltip_fontset = NOFONTSET;
284 #endif
286 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
287 gui.prev_wrap = -1;
289 #ifdef ALWAYS_USE_GUI
290 result = OK;
291 #else
292 result = gui_mch_init_check();
293 #endif
294 return result;
298 * This is the call which starts the GUI.
300 void
301 gui_init()
303 win_T *wp;
304 static int recursive = 0;
307 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
308 * function will then be executed at the first call, the rest by the
309 * recursive call. This allow the shell to be opened halfway reading a
310 * gvimrc file.
312 if (!recursive)
314 ++recursive;
316 clip_init(TRUE);
318 /* If can't initialize, don't try doing the rest */
319 if (gui_init_check() == FAIL)
321 --recursive;
322 clip_init(FALSE);
323 return;
327 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
328 * breaks the Paste toolbar button.
330 set_option_value((char_u *)"paste", 0L, NULL, 0);
333 * Set up system-wide default menus.
335 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
336 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
338 sys_menu = TRUE;
339 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
340 sys_menu = FALSE;
342 #endif
345 * Switch on the mouse by default, unless the user changed it already.
346 * This can then be changed in the .gvimrc.
348 if (!option_was_set((char_u *)"mouse"))
349 set_string_option_direct((char_u *)"mouse", -1,
350 (char_u *)"a", OPT_FREE, SID_NONE);
353 * If -U option given, use only the initializations from that file and
354 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
356 if (use_gvimrc != NULL)
358 if (STRCMP(use_gvimrc, "NONE") != 0
359 && STRCMP(use_gvimrc, "NORC") != 0
360 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
361 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
363 else
366 * Get system wide defaults for gvim, only when file name defined.
368 #ifdef SYS_GVIMRC_FILE
369 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
370 #endif
373 * Try to read GUI initialization commands from the following
374 * places:
375 * - environment variable GVIMINIT
376 * - the user gvimrc file (~/.gvimrc)
377 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
378 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
379 * The first that exists is used, the rest is ignored.
381 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
382 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
383 DOSO_GVIMRC) == FAIL
384 #ifdef USR_GVIMRC_FILE2
385 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
386 DOSO_GVIMRC) == FAIL
387 #endif
390 #ifdef USR_GVIMRC_FILE3
391 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
392 #endif
396 * Read initialization commands from ".gvimrc" in current
397 * directory. This is only done if the 'exrc' option is set.
398 * Because of security reasons we disallow shell and write
399 * commands now, except for unix if the file is owned by the user
400 * or 'secure' option has been reset in environment of global
401 * ".gvimrc".
402 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
403 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
405 if (p_exrc)
407 #ifdef UNIX
409 struct stat s;
411 /* if ".gvimrc" file is not owned by user, set 'secure'
412 * mode */
413 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
414 secure = p_secure;
416 #else
417 secure = p_secure;
418 #endif
420 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
421 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
422 #ifdef SYS_GVIMRC_FILE
423 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
424 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
425 #endif
426 #ifdef USR_GVIMRC_FILE2
427 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
428 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
429 #endif
430 #ifdef USR_GVIMRC_FILE3
431 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
432 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
433 #endif
435 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
437 if (secure == 2)
438 need_wait_return = TRUE;
439 secure = 0;
443 if (need_wait_return || msg_didany)
444 wait_return(TRUE);
446 --recursive;
449 /* If recursive call opened the shell, return here from the first call */
450 if (gui.in_use)
451 return;
454 * Create the GUI shell.
456 gui.in_use = TRUE; /* Must be set after menus have been set up */
457 if (gui_mch_init() == FAIL)
458 goto error;
460 /* Avoid a delay for an error message that was printed in the terminal
461 * where Vim was started. */
462 emsg_on_display = FALSE;
463 msg_scrolled = 0;
464 clear_sb_text();
465 need_wait_return = FALSE;
466 msg_didany = FALSE;
469 * Check validity of any generic resources that may have been loaded.
471 if (gui.border_width < 0)
472 gui.border_width = 0;
475 * Set up the fonts. First use a font specified with "-fn" or "-font".
477 if (font_argument != NULL)
478 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
479 if (
480 #ifdef FEAT_XFONTSET
481 (*p_guifontset == NUL
482 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
483 #endif
484 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
485 : p_guifont, FALSE) == FAIL)
487 EMSG(_("E665: Cannot start GUI, no valid font found"));
488 goto error2;
490 #ifdef FEAT_MBYTE
491 if (gui_get_wide_font() == FAIL)
492 EMSG(_("E231: 'guifontwide' invalid"));
493 #endif
495 gui.num_cols = Columns;
496 gui.num_rows = Rows;
497 gui_reset_scroll_region();
499 /* Create initial scrollbars */
500 FOR_ALL_WINDOWS(wp)
502 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
503 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
505 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
507 #ifdef FEAT_MENU
508 gui_create_initial_menus(root_menu);
509 #endif
510 #ifdef FEAT_SUN_WORKSHOP
511 if (usingSunWorkShop)
512 workshop_init();
513 #endif
514 #ifdef FEAT_SIGN_ICONS
515 sign_gui_started();
516 #endif
518 /* Configure the desired menu and scrollbars */
519 gui_init_which_components(NULL);
521 /* All components of the GUI have been created now */
522 gui.shell_created = TRUE;
524 #ifndef FEAT_GUI_GTK
525 /* Set the shell size, adjusted for the screen size. For GTK this only
526 * works after the shell has been opened, thus it is further down. */
527 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
528 #endif
529 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
530 /* Need to set the size of the menubar after all the menus have been
531 * created. */
532 gui_mch_compute_menu_height((Widget)0);
533 #endif
536 * Actually open the GUI shell.
538 if (gui_mch_open() != FAIL)
540 #ifdef FEAT_TITLE
541 maketitle();
542 resettitle();
543 #endif
544 init_gui_options();
545 #ifdef FEAT_ARABIC
546 /* Our GUI can't do bidi. */
547 p_tbidi = FALSE;
548 #endif
549 #if defined(FEAT_GUI_GTK)
550 /* Give GTK+ a chance to put all widget's into place. */
551 gui_mch_update();
553 # ifdef FEAT_MENU
554 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
555 * It was still there to make F10 work. */
556 if (vim_strchr(p_go, GO_MENUS) == NULL)
558 --gui.starting;
559 gui_mch_enable_menu(FALSE);
560 ++gui.starting;
561 gui_mch_update();
563 # endif
565 /* Now make sure the shell fits on the screen. */
566 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
567 #endif
568 /* When 'lines' was set while starting up the topframe may have to be
569 * resized. */
570 win_new_shellsize();
572 #ifdef FEAT_BEVAL
573 /* Always create the Balloon Evaluation area, but disable it when
574 * 'ballooneval' is off */
575 # ifdef FEAT_GUI_GTK
576 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
577 &general_beval_cb, NULL);
578 # else
579 # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
581 extern Widget textArea;
582 balloonEval = gui_mch_create_beval_area(textArea, NULL,
583 &general_beval_cb, NULL);
585 # else
586 # ifdef FEAT_GUI_W32
587 balloonEval = gui_mch_create_beval_area(NULL, NULL,
588 &general_beval_cb, NULL);
589 # endif
590 # endif
591 # endif
592 if (!p_beval)
593 gui_mch_disable_beval_area(balloonEval);
594 #endif
596 #ifdef FEAT_NETBEANS_INTG
597 if (starting == 0 && usingNetbeans)
598 /* Tell the client that it can start sending commands. */
599 netbeans_startup_done();
600 #endif
601 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
602 if (!im_xim_isvalid_imactivate())
603 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
604 #endif
605 /* When 'cmdheight' was set during startup it may not have taken
606 * effect yet. */
607 if (p_ch != 1L)
608 command_height();
610 return;
613 error2:
614 #ifdef FEAT_GUI_X11
615 /* undo gui_mch_init() */
616 gui_mch_uninit();
617 #endif
619 error:
620 gui.in_use = FALSE;
621 clip_init(FALSE);
625 void
626 gui_exit(rc)
627 int rc;
629 #ifndef __BEOS__
630 /* don't free the fonts, it leads to a BUS error
631 * richard@whitequeen.com Jul 99 */
632 free_highlight_fonts();
633 #endif
634 gui.in_use = FALSE;
635 gui_mch_exit(rc);
638 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
639 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
640 # define NEED_GUI_UPDATE_SCREEN 1
642 * Called when the GUI shell is closed by the user. If there are no changed
643 * files Vim exits, otherwise there will be a dialog to ask the user what to
644 * do.
645 * When this function returns, Vim should NOT exit!
647 void
648 gui_shell_closed()
650 cmdmod_T save_cmdmod;
652 save_cmdmod = cmdmod;
654 /* Only exit when there are no changed files */
655 exiting = TRUE;
656 # ifdef FEAT_BROWSE
657 cmdmod.browse = TRUE;
658 # endif
659 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
660 cmdmod.confirm = TRUE;
661 # endif
662 /* If there are changed buffers, present the user with a dialog if
663 * possible, otherwise give an error message. */
664 if (!check_changed_any(FALSE))
665 getout(0);
667 exiting = FALSE;
668 cmdmod = save_cmdmod;
669 gui_update_screen(); /* redraw, window may show changed buffer */
671 #endif
674 * Set the font. "font_list" is a a comma separated list of font names. The
675 * first font name that works is used. If none is found, use the default
676 * font.
677 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
678 * Return OK when able to set the font. When it failed FAIL is returned and
679 * the fonts are unchanged.
681 /*ARGSUSED*/
683 gui_init_font(font_list, fontset)
684 char_u *font_list;
685 int fontset;
687 #define FONTLEN 320
688 char_u font_name[FONTLEN];
689 int font_list_empty = FALSE;
690 int ret = FAIL;
692 if (!gui.in_use)
693 return FAIL;
695 font_name[0] = NUL;
696 if (*font_list == NUL)
697 font_list_empty = TRUE;
698 else
700 #ifdef FEAT_XFONTSET
701 /* When using a fontset, the whole list of fonts is one name. */
702 if (fontset)
703 ret = gui_mch_init_font(font_list, TRUE);
704 else
705 #endif
706 while (*font_list != NUL)
708 /* Isolate one comma separated font name. */
709 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
711 /* Careful!!! The Win32 version of gui_mch_init_font(), when
712 * called with "*" will change p_guifont to the selected font
713 * name, which frees the old value. This makes font_list
714 * invalid. Thus when OK is returned here, font_list must no
715 * longer be used! */
716 if (gui_mch_init_font(font_name, FALSE) == OK)
718 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
719 /* If it's a Unicode font, try setting 'guifontwide' to a
720 * similar double-width font. */
721 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
722 && strstr((char *)font_name, "10646") != NULL)
723 set_guifontwide(font_name);
724 #endif
725 ret = OK;
726 break;
731 if (ret != OK
732 && STRCMP(font_list, "*") != 0
733 && (font_list_empty || gui.norm_font == NOFONT))
736 * Couldn't load any font in 'font_list', keep the current font if
737 * there is one. If 'font_list' is empty, or if there is no current
738 * font, tell gui_mch_init_font() to try to find a font we can load.
740 ret = gui_mch_init_font(NULL, FALSE);
743 if (ret == OK)
745 #ifndef HAVE_GTK2
746 /* Set normal font as current font */
747 # ifdef FEAT_XFONTSET
748 if (gui.fontset != NOFONTSET)
749 gui_mch_set_fontset(gui.fontset);
750 else
751 # endif
752 gui_mch_set_font(gui.norm_font);
753 #endif
754 gui_set_shellsize(FALSE,
755 #ifdef MSWIN
756 TRUE
757 #else
758 FALSE
759 #endif
760 , RESIZE_BOTH);
763 return ret;
766 #if defined(FEAT_MBYTE) || defined(PROTO)
767 # ifndef HAVE_GTK2
769 * Try setting 'guifontwide' to a font twice as wide as "name".
771 static void
772 set_guifontwide(name)
773 char_u *name;
775 int i = 0;
776 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
777 char_u *wp = NULL;
778 char_u *p;
779 GuiFont font;
781 wp = wide_name;
782 for (p = name; *p != NUL; ++p)
784 *wp++ = *p;
785 if (*p == '-')
787 ++i;
788 if (i == 6) /* font type: change "--" to "-*-" */
790 if (p[1] == '-')
791 *wp++ = '*';
793 else if (i == 12) /* found the width */
795 ++p;
796 i = getdigits(&p);
797 if (i != 0)
799 /* Double the width specification. */
800 sprintf((char *)wp, "%d%s", i * 2, p);
801 font = gui_mch_get_font(wide_name, FALSE);
802 if (font != NOFONT)
804 gui_mch_free_font(gui.wide_font);
805 gui.wide_font = font;
806 set_string_option_direct((char_u *)"gfw", -1,
807 wide_name, OPT_FREE, 0);
810 break;
815 # endif /* !HAVE_GTK2 */
818 * Get the font for 'guifontwide'.
819 * Return FAIL for an invalid font name.
822 gui_get_wide_font()
824 GuiFont font = NOFONT;
825 char_u font_name[FONTLEN];
826 char_u *p;
828 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
829 return OK; /* Will give an error message later. */
831 if (p_guifontwide != NULL && *p_guifontwide != NUL)
833 for (p = p_guifontwide; *p != NUL; )
835 /* Isolate one comma separated font name. */
836 (void)copy_option_part(&p, font_name, FONTLEN, ",");
837 font = gui_mch_get_font(font_name, FALSE);
838 if (font != NOFONT)
839 break;
841 if (font == NOFONT)
842 return FAIL;
845 gui_mch_free_font(gui.wide_font);
846 #ifdef HAVE_GTK2
847 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
848 if (font != NOFONT && gui.norm_font != NOFONT
849 && pango_font_description_equal(font, gui.norm_font))
851 gui.wide_font = NOFONT;
852 gui_mch_free_font(font);
854 else
855 #endif
856 gui.wide_font = font;
857 return OK;
859 #endif
861 void
862 gui_set_cursor(row, col)
863 int row;
864 int col;
866 gui.row = row;
867 gui.col = col;
871 * gui_check_pos - check if the cursor is on the screen.
873 static void
874 gui_check_pos()
876 if (gui.row >= screen_Rows)
877 gui.row = screen_Rows - 1;
878 if (gui.col >= screen_Columns)
879 gui.col = screen_Columns - 1;
880 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
881 gui.cursor_is_valid = FALSE;
885 * Redraw the cursor if necessary or when forced.
886 * Careful: The contents of ScreenLines[] must match what is on the screen,
887 * otherwise this goes wrong. May need to call out_flush() first.
889 void
890 gui_update_cursor(force, clear_selection)
891 int force; /* when TRUE, update even when not moved */
892 int clear_selection;/* clear selection under cursor */
894 int cur_width = 0;
895 int cur_height = 0;
896 int old_hl_mask;
897 int idx;
898 int id;
899 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
900 int cattr; /* cursor attributes */
901 int attr;
902 attrentry_T *aep = NULL;
904 /* Don't update the cursor when halfway busy scrolling.
905 * ScreenLines[] isn't valid then. */
906 if (!can_update_cursor)
907 return;
909 gui_check_pos();
910 if (!gui.cursor_is_valid || force
911 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
913 gui_undraw_cursor();
914 if (gui.row < 0)
915 return;
916 #ifdef USE_IM_CONTROL
917 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
918 im_set_position(gui.row, gui.col);
919 #endif
920 gui.cursor_row = gui.row;
921 gui.cursor_col = gui.col;
923 /* Only write to the screen after ScreenLines[] has been initialized */
924 if (!screen_cleared || ScreenLines == NULL)
925 return;
927 /* Clear the selection if we are about to write over it */
928 if (clear_selection)
929 clip_may_clear_selection(gui.row, gui.row);
930 /* Check that the cursor is inside the shell (resizing may have made
931 * it invalid) */
932 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
933 return;
935 gui.cursor_is_valid = TRUE;
938 * How the cursor is drawn depends on the current mode.
940 idx = get_shape_idx(FALSE);
941 if (State & LANGMAP)
942 id = shape_table[idx].id_lm;
943 else
944 id = shape_table[idx].id;
946 /* get the colors and attributes for the cursor. Default is inverted */
947 cfg = INVALCOLOR;
948 cbg = INVALCOLOR;
949 cattr = HL_INVERSE;
950 gui_mch_set_blinking(shape_table[idx].blinkwait,
951 shape_table[idx].blinkon,
952 shape_table[idx].blinkoff);
953 if (id > 0)
955 cattr = syn_id2colors(id, &cfg, &cbg);
956 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
958 static int iid;
959 guicolor_T fg, bg;
961 if (im_get_status())
963 iid = syn_name2id((char_u *)"CursorIM");
964 if (iid > 0)
966 syn_id2colors(iid, &fg, &bg);
967 if (bg != INVALCOLOR)
968 cbg = bg;
969 if (fg != INVALCOLOR)
970 cfg = fg;
974 #endif
978 * Get the attributes for the character under the cursor.
979 * When no cursor color was given, use the character color.
981 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
982 if (attr > HL_ALL)
983 aep = syn_gui_attr2entry(attr);
984 if (aep != NULL)
986 attr = aep->ae_attr;
987 if (cfg == INVALCOLOR)
988 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
989 : aep->ae_u.gui.fg_color);
990 if (cbg == INVALCOLOR)
991 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
992 : aep->ae_u.gui.bg_color);
994 if (cfg == INVALCOLOR)
995 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
996 if (cbg == INVALCOLOR)
997 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
999 #ifdef FEAT_XIM
1000 if (aep != NULL)
1002 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1003 : aep->ae_u.gui.bg_color);
1004 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1005 : aep->ae_u.gui.fg_color);
1006 if (xim_bg_color == INVALCOLOR)
1007 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1008 : gui.back_pixel;
1009 if (xim_fg_color == INVALCOLOR)
1010 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1011 : gui.norm_pixel;
1013 else
1015 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1016 : gui.back_pixel;
1017 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1018 : gui.norm_pixel;
1020 #endif
1022 attr &= ~HL_INVERSE;
1023 if (cattr & HL_INVERSE)
1025 cc = cbg;
1026 cbg = cfg;
1027 cfg = cc;
1029 cattr &= ~HL_INVERSE;
1032 * When we don't have window focus, draw a hollow cursor.
1034 if (!gui.in_focus)
1036 gui_mch_draw_hollow_cursor(cbg);
1037 return;
1040 old_hl_mask = gui.highlight_mask;
1041 if (shape_table[idx].shape == SHAPE_BLOCK
1042 #ifdef FEAT_HANGULIN
1043 || composing_hangul
1044 #endif
1048 * Draw the text character with the cursor colors. Use the
1049 * character attributes plus the cursor attributes.
1051 gui.highlight_mask = (cattr | attr);
1052 #ifdef FEAT_HANGULIN
1053 if (composing_hangul)
1054 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1055 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1056 else
1057 #endif
1058 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1059 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1061 else
1063 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1064 int col_off = FALSE;
1065 #endif
1067 * First draw the partial cursor, then overwrite with the text
1068 * character, using a transparent background.
1070 if (shape_table[idx].shape == SHAPE_VER)
1072 cur_height = gui.char_height;
1073 cur_width = (gui.char_width * shape_table[idx].percentage
1074 + 99) / 100;
1076 else
1078 cur_height = (gui.char_height * shape_table[idx].percentage
1079 + 99) / 100;
1080 cur_width = gui.char_width;
1082 #ifdef FEAT_MBYTE
1083 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1084 LineOffset[gui.row] + screen_Columns) > 1)
1086 /* Double wide character. */
1087 if (shape_table[idx].shape != SHAPE_VER)
1088 cur_width += gui.char_width;
1089 # ifdef FEAT_RIGHTLEFT
1090 if (CURSOR_BAR_RIGHT)
1092 /* gui.col points to the left halve of the character but
1093 * the vertical line needs to be on the right halve.
1094 * A double-wide horizontal line is also drawn from the
1095 * right halve in gui_mch_draw_part_cursor(). */
1096 col_off = TRUE;
1097 ++gui.col;
1099 # endif
1101 #endif
1102 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1103 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1104 if (col_off)
1105 --gui.col;
1106 #endif
1108 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1109 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1110 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1111 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1112 (guicolor_T)0, (guicolor_T)0, 0);
1113 #endif
1115 gui.highlight_mask = old_hl_mask;
1119 #if defined(FEAT_MENU) || defined(PROTO)
1120 void
1121 gui_position_menu()
1123 # if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
1124 if (gui.menu_is_active && gui.in_use)
1125 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1126 # endif
1128 #endif
1131 * Position the various GUI components (text area, menu). The vertical
1132 * scrollbars are NOT handled here. See gui_update_scrollbars().
1134 /*ARGSUSED*/
1135 static void
1136 gui_position_components(total_width)
1137 int total_width;
1139 int text_area_x;
1140 int text_area_y;
1141 int text_area_width;
1142 int text_area_height;
1144 /* avoid that moving components around generates events */
1145 ++hold_gui_events;
1147 text_area_x = 0;
1148 if (gui.which_scrollbars[SBAR_LEFT])
1149 text_area_x += gui.scrollbar_width;
1151 text_area_y = 0;
1152 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1153 gui.menu_width = total_width;
1154 if (gui.menu_is_active)
1155 text_area_y += gui.menu_height;
1156 #endif
1157 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1158 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1159 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1160 #endif
1162 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1163 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1164 if (gui_has_tabline())
1165 text_area_y += gui.tabline_height;
1166 #endif
1168 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1169 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1171 # ifdef FEAT_GUI_ATHENA
1172 gui_mch_set_toolbar_pos(0, text_area_y,
1173 gui.menu_width, gui.toolbar_height);
1174 # endif
1175 text_area_y += gui.toolbar_height;
1177 #endif
1179 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1180 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1182 gui_mch_set_text_area_pos(text_area_x,
1183 text_area_y,
1184 text_area_width,
1185 text_area_height
1186 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1187 + xim_get_status_area_height()
1188 #endif
1190 #ifdef FEAT_MENU
1191 gui_position_menu();
1192 #endif
1193 if (gui.which_scrollbars[SBAR_BOTTOM])
1194 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1195 text_area_x,
1196 text_area_y + text_area_height,
1197 text_area_width,
1198 gui.scrollbar_height);
1199 gui.left_sbar_x = 0;
1200 gui.right_sbar_x = text_area_x + text_area_width;
1202 --hold_gui_events;
1206 * Get the width of the widgets and decorations to the side of the text area.
1209 gui_get_base_width()
1211 int base_width;
1213 base_width = 2 * gui.border_offset;
1214 if (gui.which_scrollbars[SBAR_LEFT])
1215 base_width += gui.scrollbar_width;
1216 if (gui.which_scrollbars[SBAR_RIGHT])
1217 base_width += gui.scrollbar_width;
1218 return base_width;
1222 * Get the height of the widgets and decorations above and below the text area.
1225 gui_get_base_height()
1227 int base_height;
1229 base_height = 2 * gui.border_offset;
1230 if (gui.which_scrollbars[SBAR_BOTTOM])
1231 base_height += gui.scrollbar_height;
1232 #ifdef FEAT_GUI_GTK
1233 /* We can't take the sizes properly into account until anything is
1234 * realized. Therefore we recalculate all the values here just before
1235 * setting the size. (--mdcki) */
1236 #else
1237 # ifdef FEAT_MENU
1238 if (gui.menu_is_active)
1239 base_height += gui.menu_height;
1240 # endif
1241 # ifdef FEAT_TOOLBAR
1242 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1243 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1244 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1245 # else
1246 base_height += gui.toolbar_height;
1247 # endif
1248 # endif
1249 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1250 || defined(FEAT_GUI_MOTIF))
1251 if (gui_has_tabline())
1252 base_height += gui.tabline_height;
1253 # endif
1254 # ifdef FEAT_FOOTER
1255 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1256 base_height += gui.footer_height;
1257 # endif
1258 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1259 base_height += gui_mch_text_area_extra_height();
1260 # endif
1261 #endif
1262 return base_height;
1266 * Should be called after the GUI shell has been resized. Its arguments are
1267 * the new width and height of the shell in pixels.
1269 void
1270 gui_resize_shell(pixel_width, pixel_height)
1271 int pixel_width;
1272 int pixel_height;
1274 static int busy = FALSE;
1276 if (!gui.shell_created) /* ignore when still initializing */
1277 return;
1280 * Can't resize the screen while it is being redrawn. Remember the new
1281 * size and handle it later.
1283 if (updating_screen || busy)
1285 new_pixel_width = pixel_width;
1286 new_pixel_height = pixel_height;
1287 return;
1290 again:
1291 busy = TRUE;
1293 /* Flush pending output before redrawing */
1294 out_flush();
1296 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1297 gui.num_rows = (pixel_height - gui_get_base_height()
1298 #if !defined(FEAT_GUI_PHOTON) && !defined(FEAT_GUI_MSWIN)
1299 + (gui.char_height / 2)
1300 #endif
1301 ) / gui.char_height;
1303 gui_position_components(pixel_width);
1305 gui_reset_scroll_region();
1307 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1308 * at the last line here (why does it have to be one row too low?).
1310 if (State == ASKMORE || State == CONFIRM)
1311 gui.row = gui.num_rows;
1313 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1314 * the safe side. */
1315 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1316 || gui.num_rows != Rows || gui.num_cols != Columns)
1317 shell_resized();
1319 gui_update_scrollbars(TRUE);
1320 gui_update_cursor(FALSE, TRUE);
1321 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1322 xim_set_status_area();
1323 #endif
1325 busy = FALSE;
1328 * We could have been called again while redrawing the screen.
1329 * Need to do it all again with the latest size then.
1331 if (new_pixel_height)
1333 pixel_width = new_pixel_width;
1334 pixel_height = new_pixel_height;
1335 new_pixel_width = 0;
1336 new_pixel_height = 0;
1337 goto again;
1342 * Check if gui_resize_shell() must be called.
1344 void
1345 gui_may_resize_shell()
1347 int h, w;
1349 if (new_pixel_height)
1351 /* careful: gui_resize_shell() may postpone the resize again if we
1352 * were called indirectly by it */
1353 w = new_pixel_width;
1354 h = new_pixel_height;
1355 new_pixel_width = 0;
1356 new_pixel_height = 0;
1357 gui_resize_shell(w, h);
1362 gui_get_shellsize()
1364 Rows = gui.num_rows;
1365 Columns = gui.num_cols;
1366 return OK;
1370 * Set the size of the Vim shell according to Rows and Columns.
1371 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1372 * on the screen.
1374 /*ARGSUSED*/
1375 void
1376 gui_set_shellsize(mustset, fit_to_display, direction)
1377 int mustset; /* set by the user */
1378 int fit_to_display;
1379 int direction; /* RESIZE_HOR, RESIZE_VER */
1381 int base_width;
1382 int base_height;
1383 int width;
1384 int height;
1385 int min_width;
1386 int min_height;
1387 int screen_w;
1388 int screen_h;
1390 if (!gui.shell_created)
1391 return;
1393 #ifdef MSWIN
1394 /* If not setting to a user specified size and maximized, calculate the
1395 * number of characters that fit in the maximized window. */
1396 if (!mustset && gui_mch_maximized())
1398 gui_mch_newfont();
1399 return;
1401 #endif
1403 base_width = gui_get_base_width();
1404 base_height = gui_get_base_height();
1405 #ifdef USE_SUN_WORKSHOP
1406 if (!mustset && usingSunWorkShop
1407 && workshop_get_width_height(&width, &height))
1409 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1410 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1412 else
1413 #endif
1415 width = Columns * gui.char_width + base_width;
1416 height = Rows * gui.char_height + base_height;
1419 if (fit_to_display)
1421 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1422 if ((direction & RESIZE_HOR) && width > screen_w)
1424 Columns = (screen_w - base_width) / gui.char_width;
1425 if (Columns < MIN_COLUMNS)
1426 Columns = MIN_COLUMNS;
1427 width = Columns * gui.char_width + base_width;
1429 if ((direction & RESIZE_VERT) && height > screen_h)
1431 Rows = (screen_h - base_height) / gui.char_height;
1432 check_shellsize();
1433 height = Rows * gui.char_height + base_height;
1436 gui.num_cols = Columns;
1437 gui.num_rows = Rows;
1439 min_width = base_width + MIN_COLUMNS * gui.char_width;
1440 min_height = base_height + MIN_LINES * gui.char_height;
1441 # ifdef FEAT_WINDOWS
1442 min_height += tabline_height() * gui.char_height;
1443 # endif
1445 gui_mch_set_shellsize(width, height, min_width, min_height,
1446 base_width, base_height, direction);
1447 if (fit_to_display)
1449 int x, y;
1451 /* Some window managers put the Vim window left of/above the screen. */
1452 gui_mch_update();
1453 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1454 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1457 gui_position_components(width);
1458 gui_update_scrollbars(TRUE);
1459 gui_reset_scroll_region();
1463 * Called when Rows and/or Columns has changed.
1465 void
1466 gui_new_shellsize()
1468 gui_reset_scroll_region();
1472 * Make scroll region cover whole screen.
1474 void
1475 gui_reset_scroll_region()
1477 gui.scroll_region_top = 0;
1478 gui.scroll_region_bot = gui.num_rows - 1;
1479 gui.scroll_region_left = 0;
1480 gui.scroll_region_right = gui.num_cols - 1;
1483 void
1484 gui_start_highlight(mask)
1485 int mask;
1487 if (mask > HL_ALL) /* highlight code */
1488 gui.highlight_mask = mask;
1489 else /* mask */
1490 gui.highlight_mask |= mask;
1493 void
1494 gui_stop_highlight(mask)
1495 int mask;
1497 if (mask > HL_ALL) /* highlight code */
1498 gui.highlight_mask = HL_NORMAL;
1499 else /* mask */
1500 gui.highlight_mask &= ~mask;
1504 * Clear a rectangular region of the screen from text pos (row1, col1) to
1505 * (row2, col2) inclusive.
1507 void
1508 gui_clear_block(row1, col1, row2, col2)
1509 int row1;
1510 int col1;
1511 int row2;
1512 int col2;
1514 /* Clear the selection if we are about to write over it */
1515 clip_may_clear_selection(row1, row2);
1517 gui_mch_clear_block(row1, col1, row2, col2);
1519 /* Invalidate cursor if it was in this block */
1520 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1521 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1522 gui.cursor_is_valid = FALSE;
1526 * Write code to update the cursor later. This avoids the need to flush the
1527 * output buffer before calling gui_update_cursor().
1529 void
1530 gui_update_cursor_later()
1532 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1535 void
1536 gui_write(s, len)
1537 char_u *s;
1538 int len;
1540 char_u *p;
1541 int arg1 = 0, arg2 = 0;
1542 /* this doesn't make sense, disabled until someone can explain why it
1543 * would be needed */
1544 #if 0 && (defined(RISCOS) || defined(WIN16))
1545 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1546 our redraw speed will suffer */
1547 #else
1548 int force_cursor = FALSE; /* force cursor update */
1549 #endif
1550 int force_scrollbar = FALSE;
1551 static win_T *old_curwin = NULL;
1553 /* #define DEBUG_GUI_WRITE */
1554 #ifdef DEBUG_GUI_WRITE
1556 int i;
1557 char_u *str;
1559 printf("gui_write(%d):\n ", len);
1560 for (i = 0; i < len; i++)
1561 if (s[i] == ESC)
1563 if (i != 0)
1564 printf("\n ");
1565 printf("<ESC>");
1567 else
1569 str = transchar_byte(s[i]);
1570 if (str[0] && str[1])
1571 printf("<%s>", (char *)str);
1572 else
1573 printf("%s", (char *)str);
1575 printf("\n");
1577 #endif
1578 while (len)
1580 if (s[0] == ESC && s[1] == '|')
1582 p = s + 2;
1583 if (VIM_ISDIGIT(*p))
1585 arg1 = getdigits(&p);
1586 if (p > s + len)
1587 break;
1588 if (*p == ';')
1590 ++p;
1591 arg2 = getdigits(&p);
1592 if (p > s + len)
1593 break;
1596 switch (*p)
1598 case 'C': /* Clear screen */
1599 clip_scroll_selection(9999);
1600 gui_mch_clear_all();
1601 gui.cursor_is_valid = FALSE;
1602 force_scrollbar = TRUE;
1603 break;
1604 case 'M': /* Move cursor */
1605 gui_set_cursor(arg1, arg2);
1606 break;
1607 case 's': /* force cursor (shape) update */
1608 force_cursor = TRUE;
1609 break;
1610 case 'R': /* Set scroll region */
1611 if (arg1 < arg2)
1613 gui.scroll_region_top = arg1;
1614 gui.scroll_region_bot = arg2;
1616 else
1618 gui.scroll_region_top = arg2;
1619 gui.scroll_region_bot = arg1;
1621 break;
1622 #ifdef FEAT_VERTSPLIT
1623 case 'V': /* Set vertical scroll region */
1624 if (arg1 < arg2)
1626 gui.scroll_region_left = arg1;
1627 gui.scroll_region_right = arg2;
1629 else
1631 gui.scroll_region_left = arg2;
1632 gui.scroll_region_right = arg1;
1634 break;
1635 #endif
1636 case 'd': /* Delete line */
1637 gui_delete_lines(gui.row, 1);
1638 break;
1639 case 'D': /* Delete lines */
1640 gui_delete_lines(gui.row, arg1);
1641 break;
1642 case 'i': /* Insert line */
1643 gui_insert_lines(gui.row, 1);
1644 break;
1645 case 'I': /* Insert lines */
1646 gui_insert_lines(gui.row, arg1);
1647 break;
1648 case '$': /* Clear to end-of-line */
1649 gui_clear_block(gui.row, gui.col, gui.row,
1650 (int)Columns - 1);
1651 break;
1652 case 'h': /* Turn on highlighting */
1653 gui_start_highlight(arg1);
1654 break;
1655 case 'H': /* Turn off highlighting */
1656 gui_stop_highlight(arg1);
1657 break;
1658 case 'f': /* flash the window (visual bell) */
1659 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1660 break;
1661 default:
1662 p = s + 1; /* Skip the ESC */
1663 break;
1665 len -= (int)(++p - s);
1666 s = p;
1668 else if (
1669 #ifdef EBCDIC
1670 CtrlChar(s[0]) != 0 /* Ctrl character */
1671 #else
1672 s[0] < 0x20 /* Ctrl character */
1673 #endif
1674 #ifdef FEAT_SIGN_ICONS
1675 && s[0] != SIGN_BYTE
1676 # ifdef FEAT_NETBEANS_INTG
1677 && s[0] != MULTISIGN_BYTE
1678 # endif
1679 #endif
1682 if (s[0] == '\n') /* NL */
1684 gui.col = 0;
1685 if (gui.row < gui.scroll_region_bot)
1686 gui.row++;
1687 else
1688 gui_delete_lines(gui.scroll_region_top, 1);
1690 else if (s[0] == '\r') /* CR */
1692 gui.col = 0;
1694 else if (s[0] == '\b') /* Backspace */
1696 if (gui.col)
1697 --gui.col;
1699 else if (s[0] == Ctrl_L) /* cursor-right */
1701 ++gui.col;
1703 else if (s[0] == Ctrl_G) /* Beep */
1705 gui_mch_beep();
1707 /* Other Ctrl character: shouldn't happen! */
1709 --len; /* Skip this char */
1710 ++s;
1712 else
1714 p = s;
1715 while (len > 0 && (
1716 #ifdef EBCDIC
1717 CtrlChar(*p) == 0
1718 #else
1719 *p >= 0x20
1720 #endif
1721 #ifdef FEAT_SIGN_ICONS
1722 || *p == SIGN_BYTE
1723 # ifdef FEAT_NETBEANS_INTG
1724 || *p == MULTISIGN_BYTE
1725 # endif
1726 #endif
1729 len--;
1730 p++;
1732 gui_outstr(s, (int)(p - s));
1733 s = p;
1737 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1738 * set). */
1739 if (force_cursor)
1740 gui_update_cursor(TRUE, TRUE);
1742 /* When switching to another window the dragging must have stopped.
1743 * Required for GTK, dragged_sb isn't reset. */
1744 if (old_curwin != curwin)
1745 gui.dragged_sb = SBAR_NONE;
1747 /* Update the scrollbars after clearing the screen or when switched
1748 * to another window.
1749 * Update the horizontal scrollbar always, it's difficult to check all
1750 * situations where it might change. */
1751 if (force_scrollbar || old_curwin != curwin)
1752 gui_update_scrollbars(force_scrollbar);
1753 else
1754 gui_update_horiz_scrollbar(FALSE);
1755 old_curwin = curwin;
1758 * We need to make sure this is cleared since Athena doesn't tell us when
1759 * he is done dragging. Do the same for GTK.
1761 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1762 gui.dragged_sb = SBAR_NONE;
1763 #endif
1765 gui_mch_flush(); /* In case vim decides to take a nap */
1769 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1770 * produces wrong results. Call gui_dont_update_cursor() before that code and
1771 * gui_can_update_cursor() afterwards.
1773 void
1774 gui_dont_update_cursor()
1776 if (gui.in_use)
1778 /* Undraw the cursor now, we probably can't do it after the change. */
1779 gui_undraw_cursor();
1780 can_update_cursor = FALSE;
1784 void
1785 gui_can_update_cursor()
1787 can_update_cursor = TRUE;
1788 /* No need to update the cursor right now, there is always more output
1789 * after scrolling. */
1792 static void
1793 gui_outstr(s, len)
1794 char_u *s;
1795 int len;
1797 int this_len;
1798 #ifdef FEAT_MBYTE
1799 int cells;
1800 #endif
1802 if (len == 0)
1803 return;
1805 if (len < 0)
1806 len = (int)STRLEN(s);
1808 while (len > 0)
1810 #ifdef FEAT_MBYTE
1811 if (has_mbyte)
1813 /* Find out how many chars fit in the current line. */
1814 cells = 0;
1815 for (this_len = 0; this_len < len; )
1817 cells += (*mb_ptr2cells)(s + this_len);
1818 if (gui.col + cells > Columns)
1819 break;
1820 this_len += (*mb_ptr2len)(s + this_len);
1822 if (this_len > len)
1823 this_len = len; /* don't include following composing char */
1825 else
1826 #endif
1827 if (gui.col + len > Columns)
1828 this_len = Columns - gui.col;
1829 else
1830 this_len = len;
1832 (void)gui_outstr_nowrap(s, this_len,
1833 0, (guicolor_T)0, (guicolor_T)0, 0);
1834 s += this_len;
1835 len -= this_len;
1836 #ifdef FEAT_MBYTE
1837 /* fill up for a double-width char that doesn't fit. */
1838 if (len > 0 && gui.col < Columns)
1839 (void)gui_outstr_nowrap((char_u *)" ", 1,
1840 0, (guicolor_T)0, (guicolor_T)0, 0);
1841 #endif
1842 /* The cursor may wrap to the next line. */
1843 if (gui.col >= Columns)
1845 gui.col = 0;
1846 gui.row++;
1852 * Output one character (may be one or two display cells).
1853 * Caller must check for valid "off".
1854 * Returns FAIL or OK, just like gui_outstr_nowrap().
1856 static int
1857 gui_screenchar(off, flags, fg, bg, back)
1858 int off; /* Offset from start of screen */
1859 int flags;
1860 guicolor_T fg, bg; /* colors for cursor */
1861 int back; /* backup this many chars when using bold trick */
1863 #ifdef FEAT_MBYTE
1864 char_u buf[MB_MAXBYTES + 1];
1866 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1867 if (enc_utf8 && ScreenLines[off] == 0)
1868 return OK;
1870 if (enc_utf8 && ScreenLinesUC[off] != 0)
1871 /* Draw UTF-8 multi-byte character. */
1872 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1873 flags, fg, bg, back);
1875 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1877 buf[0] = ScreenLines[off];
1878 buf[1] = ScreenLines2[off];
1879 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1882 /* Draw non-multi-byte character or DBCS character. */
1883 return gui_outstr_nowrap(ScreenLines + off,
1884 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1885 flags, fg, bg, back);
1886 #else
1887 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1888 #endif
1891 #ifdef HAVE_GTK2
1893 * Output the string at the given screen position. This is used in place
1894 * of gui_screenchar() where possible because Pango needs as much context
1895 * as possible to work nicely. It's a lot faster as well.
1897 static int
1898 gui_screenstr(off, len, flags, fg, bg, back)
1899 int off; /* Offset from start of screen */
1900 int len; /* string length in screen cells */
1901 int flags;
1902 guicolor_T fg, bg; /* colors for cursor */
1903 int back; /* backup this many chars when using bold trick */
1905 char_u *buf;
1906 int outlen = 0;
1907 int i;
1908 int retval;
1910 if (len <= 0) /* "cannot happen"? */
1911 return OK;
1913 if (enc_utf8)
1915 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1916 if (buf == NULL)
1917 return OK; /* not much we could do here... */
1919 for (i = off; i < off + len; ++i)
1921 if (ScreenLines[i] == 0)
1922 continue; /* skip second half of double-width char */
1924 if (ScreenLinesUC[i] == 0)
1925 buf[outlen++] = ScreenLines[i];
1926 else
1927 outlen += utfc_char2bytes(i, buf + outlen);
1930 buf[outlen] = NUL; /* only to aid debugging */
1931 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1932 vim_free(buf);
1934 return retval;
1936 else if (enc_dbcs == DBCS_JPNU)
1938 buf = alloc((unsigned)(len * 2 + 1));
1939 if (buf == NULL)
1940 return OK; /* not much we could do here... */
1942 for (i = off; i < off + len; ++i)
1944 buf[outlen++] = ScreenLines[i];
1946 /* handle double-byte single-width char */
1947 if (ScreenLines[i] == 0x8e)
1948 buf[outlen++] = ScreenLines2[i];
1949 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1950 buf[outlen++] = ScreenLines[++i];
1953 buf[outlen] = NUL; /* only to aid debugging */
1954 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1955 vim_free(buf);
1957 return retval;
1959 else
1961 return gui_outstr_nowrap(&ScreenLines[off], len,
1962 flags, fg, bg, back);
1965 #endif /* HAVE_GTK2 */
1968 * Output the given string at the current cursor position. If the string is
1969 * too long to fit on the line, then it is truncated.
1970 * "flags":
1971 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1972 * actually draw (an inverted) cursor.
1973 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
1974 * background.
1975 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1976 * it.
1977 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1978 * FAIL (the caller should start drawing "back" chars back).
1981 gui_outstr_nowrap(s, len, flags, fg, bg, back)
1982 char_u *s;
1983 int len;
1984 int flags;
1985 guicolor_T fg, bg; /* colors for cursor */
1986 int back; /* backup this many chars when using bold trick */
1988 long_u highlight_mask;
1989 long_u hl_mask_todo;
1990 guicolor_T fg_color;
1991 guicolor_T bg_color;
1992 guicolor_T sp_color;
1993 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
1994 GuiFont font = NOFONT;
1995 # ifdef FEAT_XFONTSET
1996 GuiFontset fontset = NOFONTSET;
1997 # endif
1998 #endif
1999 attrentry_T *aep = NULL;
2000 int draw_flags;
2001 int col = gui.col;
2002 #ifdef FEAT_SIGN_ICONS
2003 int draw_sign = FALSE;
2004 # ifdef FEAT_NETBEANS_INTG
2005 int multi_sign = FALSE;
2006 # endif
2007 #endif
2009 if (len < 0)
2010 len = (int)STRLEN(s);
2011 if (len == 0)
2012 return OK;
2014 #ifdef FEAT_SIGN_ICONS
2015 if (*s == SIGN_BYTE
2016 # ifdef FEAT_NETBEANS_INTG
2017 || *s == MULTISIGN_BYTE
2018 # endif
2021 # ifdef FEAT_NETBEANS_INTG
2022 if (*s == MULTISIGN_BYTE)
2023 multi_sign = TRUE;
2024 # endif
2025 /* draw spaces instead */
2026 s = (char_u *)" ";
2027 if (len == 1 && col > 0)
2028 --col;
2029 len = 2;
2030 draw_sign = TRUE;
2031 highlight_mask = 0;
2033 else
2034 #endif
2035 if (gui.highlight_mask > HL_ALL)
2037 aep = syn_gui_attr2entry(gui.highlight_mask);
2038 if (aep == NULL) /* highlighting not set */
2039 highlight_mask = 0;
2040 else
2041 highlight_mask = aep->ae_attr;
2043 else
2044 highlight_mask = gui.highlight_mask;
2045 hl_mask_todo = highlight_mask;
2047 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2048 /* Set the font */
2049 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2050 font = aep->ae_u.gui.font;
2051 # ifdef FEAT_XFONTSET
2052 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2053 fontset = aep->ae_u.gui.fontset;
2054 # endif
2055 else
2057 # ifdef FEAT_XFONTSET
2058 if (gui.fontset != NOFONTSET)
2059 fontset = gui.fontset;
2060 else
2061 # endif
2062 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2064 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2066 font = gui.boldital_font;
2067 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2069 else if (gui.bold_font != NOFONT)
2071 font = gui.bold_font;
2072 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2074 else
2075 font = gui.norm_font;
2077 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2079 font = gui.ital_font;
2080 hl_mask_todo &= ~HL_ITALIC;
2082 else
2083 font = gui.norm_font;
2085 # ifdef FEAT_XFONTSET
2086 if (fontset != NOFONTSET)
2087 gui_mch_set_fontset(fontset);
2088 else
2089 # endif
2090 gui_mch_set_font(font);
2091 #endif
2093 draw_flags = 0;
2095 /* Set the color */
2096 bg_color = gui.back_pixel;
2097 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2099 draw_flags |= DRAW_CURSOR;
2100 fg_color = fg;
2101 bg_color = bg;
2102 sp_color = fg;
2104 else if (aep != NULL)
2106 fg_color = aep->ae_u.gui.fg_color;
2107 if (fg_color == INVALCOLOR)
2108 fg_color = gui.norm_pixel;
2109 bg_color = aep->ae_u.gui.bg_color;
2110 if (bg_color == INVALCOLOR)
2111 bg_color = gui.back_pixel;
2112 sp_color = aep->ae_u.gui.sp_color;
2113 if (sp_color == INVALCOLOR)
2114 sp_color = fg_color;
2116 else
2118 fg_color = gui.norm_pixel;
2119 sp_color = fg_color;
2122 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2124 #if defined(AMIGA) || defined(RISCOS)
2125 gui_mch_set_colors(bg_color, fg_color);
2126 #else
2127 gui_mch_set_fg_color(bg_color);
2128 gui_mch_set_bg_color(fg_color);
2129 #endif
2131 else
2133 #if defined(AMIGA) || defined(RISCOS)
2134 gui_mch_set_colors(fg_color, bg_color);
2135 #else
2136 gui_mch_set_fg_color(fg_color);
2137 gui_mch_set_bg_color(bg_color);
2138 #endif
2140 gui_mch_set_sp_color(sp_color);
2142 /* Clear the selection if we are about to write over it */
2143 if (!(flags & GUI_MON_NOCLEAR))
2144 clip_may_clear_selection(gui.row, gui.row);
2147 #ifndef MSWIN16_FASTTEXT
2148 /* If there's no bold font, then fake it */
2149 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2150 draw_flags |= DRAW_BOLD;
2151 #endif
2154 * When drawing bold or italic characters the spill-over from the left
2155 * neighbor may be destroyed. Let the caller backup to start redrawing
2156 * just after a blank.
2158 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2159 return FAIL;
2161 #if defined(RISCOS) || defined(HAVE_GTK2)
2162 /* If there's no italic font, then fake it.
2163 * For GTK2, we don't need a different font for italic style. */
2164 if (hl_mask_todo & HL_ITALIC)
2165 draw_flags |= DRAW_ITALIC;
2167 /* Do we underline the text? */
2168 if (hl_mask_todo & HL_UNDERLINE)
2169 draw_flags |= DRAW_UNDERL;
2170 #else
2171 /* Do we underline the text? */
2172 if ((hl_mask_todo & HL_UNDERLINE)
2173 # ifndef MSWIN16_FASTTEXT
2174 || (hl_mask_todo & HL_ITALIC)
2175 # endif
2177 draw_flags |= DRAW_UNDERL;
2178 #endif
2179 /* Do we undercurl the text? */
2180 if (hl_mask_todo & HL_UNDERCURL)
2181 draw_flags |= DRAW_UNDERC;
2183 /* Do we draw transparently? */
2184 if (flags & GUI_MON_TRS_CURSOR)
2185 draw_flags |= DRAW_TRANSP;
2188 * Draw the text.
2190 #ifdef HAVE_GTK2
2191 /* The value returned is the length in display cells */
2192 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2193 #else
2194 # ifdef FEAT_MBYTE
2195 if (enc_utf8)
2197 int start; /* index of bytes to be drawn */
2198 int cells; /* cellwidth of bytes to be drawn */
2199 int thislen; /* length of bytes to be drawin */
2200 int cn; /* cellwidth of current char */
2201 int i; /* index of current char */
2202 int c; /* current char value */
2203 int cl; /* byte length of current char */
2204 int comping; /* current char is composing */
2205 int scol = col; /* screen column */
2206 int dowide; /* use 'guifontwide' */
2208 /* Break the string at a composing character, it has to be drawn on
2209 * top of the previous character. */
2210 start = 0;
2211 cells = 0;
2212 for (i = 0; i < len; i += cl)
2214 c = utf_ptr2char(s + i);
2215 cn = utf_char2cells(c);
2216 if (cn > 1
2217 # ifdef FEAT_XFONTSET
2218 && fontset == NOFONTSET
2219 # endif
2220 && gui.wide_font != NOFONT)
2221 dowide = TRUE;
2222 else
2223 dowide = FALSE;
2224 comping = utf_iscomposing(c);
2225 if (!comping) /* count cells from non-composing chars */
2226 cells += cn;
2227 cl = utf_ptr2len(s + i);
2228 if (cl == 0) /* hit end of string */
2229 len = i + cl; /* len must be wrong "cannot happen" */
2231 /* print the string so far if it's the last character or there is
2232 * a composing character. */
2233 if (i + cl >= len || (comping && i > start) || dowide
2234 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2235 || (cn > 1
2236 # ifdef FEAT_XFONTSET
2237 /* No fontset: At least draw char after wide char at
2238 * right position. */
2239 && fontset == NOFONTSET
2240 # endif
2242 # endif
2245 if (comping || dowide)
2246 thislen = i - start;
2247 else
2248 thislen = i - start + cl;
2249 if (thislen > 0)
2251 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2252 draw_flags);
2253 start += thislen;
2255 scol += cells;
2256 cells = 0;
2257 if (dowide)
2259 gui_mch_set_font(gui.wide_font);
2260 gui_mch_draw_string(gui.row, scol - cn,
2261 s + start, cl, draw_flags);
2262 gui_mch_set_font(font);
2263 start += cl;
2266 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2267 /* No fontset: draw a space to fill the gap after a wide char
2268 * */
2269 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2270 # ifdef FEAT_XFONTSET
2271 && fontset == NOFONTSET
2272 # endif
2273 && !dowide)
2274 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2275 1, draw_flags);
2276 # endif
2278 /* Draw a composing char on top of the previous char. */
2279 if (comping)
2281 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2282 /* Carbon ATSUI autodraws composing char over previous char */
2283 gui_mch_draw_string(gui.row, scol, s + i, cl,
2284 draw_flags | DRAW_TRANSP);
2285 # else
2286 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2287 draw_flags | DRAW_TRANSP);
2288 # endif
2289 start = i + cl;
2292 /* The stuff below assumes "len" is the length in screen columns. */
2293 len = scol - col;
2295 else
2296 # endif
2298 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2299 # ifdef FEAT_MBYTE
2300 if (enc_dbcs == DBCS_JPNU)
2302 int clen = 0;
2303 int i;
2305 /* Get the length in display cells, this can be different from the
2306 * number of bytes for "euc-jp". */
2307 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2308 clen += (*mb_ptr2cells)(s + i);
2309 len = clen;
2311 # endif
2313 #endif /* !HAVE_GTK2 */
2315 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2316 gui.col = col + len;
2318 /* May need to invert it when it's part of the selection. */
2319 if (flags & GUI_MON_NOCLEAR)
2320 clip_may_redraw_selection(gui.row, col, len);
2322 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2324 /* Invalidate the old physical cursor position if we wrote over it */
2325 if (gui.cursor_row == gui.row
2326 && gui.cursor_col >= col
2327 && gui.cursor_col < col + len)
2328 gui.cursor_is_valid = FALSE;
2331 #ifdef FEAT_SIGN_ICONS
2332 if (draw_sign)
2333 /* Draw the sign on top of the spaces. */
2334 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2335 # ifdef FEAT_NETBEANS_INTG
2336 if (multi_sign)
2337 netbeans_draw_multisign_indicator(gui.row);
2338 # endif
2339 #endif
2341 return OK;
2345 * Un-draw the cursor. Actually this just redraws the character at the given
2346 * position. The character just before it too, for when it was in bold.
2348 void
2349 gui_undraw_cursor()
2351 if (gui.cursor_is_valid)
2353 #ifdef FEAT_HANGULIN
2354 if (composing_hangul
2355 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2356 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2357 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2358 gui.norm_pixel, gui.back_pixel, 0);
2359 else
2361 #endif
2362 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2363 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2364 && gui.cursor_col > 0)
2365 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2366 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2367 #ifdef FEAT_HANGULIN
2368 if (composing_hangul)
2369 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2370 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2372 #endif
2373 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2374 * here in case it wasn't needed to undraw it. */
2375 gui.cursor_is_valid = FALSE;
2379 void
2380 gui_redraw(x, y, w, h)
2381 int x;
2382 int y;
2383 int w;
2384 int h;
2386 int row1, col1, row2, col2;
2388 row1 = Y_2_ROW(y);
2389 col1 = X_2_COL(x);
2390 row2 = Y_2_ROW(y + h - 1);
2391 col2 = X_2_COL(x + w - 1);
2393 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2396 * We may need to redraw the cursor, but don't take it upon us to change
2397 * its location after a scroll.
2398 * (maybe be more strict even and test col too?)
2399 * These things may be outside the update/clipping region and reality may
2400 * not reflect Vims internal ideas if these operations are clipped away.
2402 if (gui.row == gui.cursor_row)
2403 gui_update_cursor(TRUE, TRUE);
2407 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2408 * from col1 to col2 (inclusive).
2409 * Return TRUE when the character before the first drawn character has
2410 * different attributes (may have to be redrawn too).
2413 gui_redraw_block(row1, col1, row2, col2, flags)
2414 int row1;
2415 int col1;
2416 int row2;
2417 int col2;
2418 int flags; /* flags for gui_outstr_nowrap() */
2420 int old_row, old_col;
2421 long_u old_hl_mask;
2422 int off;
2423 sattr_T first_attr;
2424 int idx, len;
2425 int back, nback;
2426 int retval = FALSE;
2427 #ifdef FEAT_MBYTE
2428 int orig_col1, orig_col2;
2429 #endif
2431 /* Don't try to update when ScreenLines is not valid */
2432 if (!screen_cleared || ScreenLines == NULL)
2433 return retval;
2435 /* Don't try to draw outside the shell! */
2436 /* Check everything, strange values may be caused by a big border width */
2437 col1 = check_col(col1);
2438 col2 = check_col(col2);
2439 row1 = check_row(row1);
2440 row2 = check_row(row2);
2442 /* Remember where our cursor was */
2443 old_row = gui.row;
2444 old_col = gui.col;
2445 old_hl_mask = gui.highlight_mask;
2446 #ifdef FEAT_MBYTE
2447 orig_col1 = col1;
2448 orig_col2 = col2;
2449 #endif
2451 for (gui.row = row1; gui.row <= row2; gui.row++)
2453 #ifdef FEAT_MBYTE
2454 /* When only half of a double-wide character is in the block, include
2455 * the other half. */
2456 col1 = orig_col1;
2457 col2 = orig_col2;
2458 off = LineOffset[gui.row];
2459 if (enc_dbcs != 0)
2461 if (col1 > 0)
2462 col1 -= dbcs_screen_head_off(ScreenLines + off,
2463 ScreenLines + off + col1);
2464 col2 += dbcs_screen_tail_off(ScreenLines + off,
2465 ScreenLines + off + col2);
2467 else if (enc_utf8)
2469 if (ScreenLines[off + col1] == 0)
2470 --col1;
2471 # ifdef HAVE_GTK2
2472 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2473 ++col2;
2474 # endif
2476 #endif
2477 gui.col = col1;
2478 off = LineOffset[gui.row] + gui.col;
2479 len = col2 - col1 + 1;
2481 /* Find how many chars back this highlighting starts, or where a space
2482 * is. Needed for when the bold trick is used */
2483 for (back = 0; back < col1; ++back)
2484 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2485 || ScreenLines[off - 1 - back] == ' ')
2486 break;
2487 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2488 && ScreenLines[off - 1] != ' ');
2490 /* Break it up in strings of characters with the same attributes. */
2491 /* Print UTF-8 characters individually. */
2492 while (len > 0)
2494 first_attr = ScreenAttrs[off];
2495 gui.highlight_mask = first_attr;
2496 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2497 if (enc_utf8 && ScreenLinesUC[off] != 0)
2499 /* output multi-byte character separately */
2500 nback = gui_screenchar(off, flags,
2501 (guicolor_T)0, (guicolor_T)0, back);
2502 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2503 idx = 2;
2504 else
2505 idx = 1;
2507 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2509 /* output double-byte, single-width character separately */
2510 nback = gui_screenchar(off, flags,
2511 (guicolor_T)0, (guicolor_T)0, back);
2512 idx = 1;
2514 else
2515 #endif
2517 #ifdef HAVE_GTK2
2518 for (idx = 0; idx < len; ++idx)
2520 if (enc_utf8 && ScreenLines[off + idx] == 0)
2521 continue; /* skip second half of double-width char */
2522 if (ScreenAttrs[off + idx] != first_attr)
2523 break;
2525 /* gui_screenstr() takes care of multibyte chars */
2526 nback = gui_screenstr(off, idx, flags,
2527 (guicolor_T)0, (guicolor_T)0, back);
2528 #else
2529 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2530 idx++)
2532 # ifdef FEAT_MBYTE
2533 /* Stop at a multi-byte Unicode character. */
2534 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2535 break;
2536 if (enc_dbcs == DBCS_JPNU)
2538 /* Stop at a double-byte single-width char. */
2539 if (ScreenLines[off + idx] == 0x8e)
2540 break;
2541 if (len > 1 && (*mb_ptr2len)(ScreenLines
2542 + off + idx) == 2)
2543 ++idx; /* skip second byte of double-byte char */
2545 # endif
2547 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2548 (guicolor_T)0, (guicolor_T)0, back);
2549 #endif
2551 if (nback == FAIL)
2553 /* Must back up to start drawing where a bold or italic word
2554 * starts. */
2555 off -= back;
2556 len += back;
2557 gui.col -= back;
2559 else
2561 off += idx;
2562 len -= idx;
2564 back = 0;
2568 /* Put the cursor back where it was */
2569 gui.row = old_row;
2570 gui.col = old_col;
2571 gui.highlight_mask = (int)old_hl_mask;
2573 return retval;
2576 static void
2577 gui_delete_lines(row, count)
2578 int row;
2579 int count;
2581 if (count <= 0)
2582 return;
2584 if (row + count > gui.scroll_region_bot)
2585 /* Scrolled out of region, just blank the lines out */
2586 gui_clear_block(row, gui.scroll_region_left,
2587 gui.scroll_region_bot, gui.scroll_region_right);
2588 else
2590 gui_mch_delete_lines(row, count);
2592 /* If the cursor was in the deleted lines it's now gone. If the
2593 * cursor was in the scrolled lines adjust its position. */
2594 if (gui.cursor_row >= row
2595 && gui.cursor_col >= gui.scroll_region_left
2596 && gui.cursor_col <= gui.scroll_region_right)
2598 if (gui.cursor_row < row + count)
2599 gui.cursor_is_valid = FALSE;
2600 else if (gui.cursor_row <= gui.scroll_region_bot)
2601 gui.cursor_row -= count;
2606 static void
2607 gui_insert_lines(row, count)
2608 int row;
2609 int count;
2611 if (count <= 0)
2612 return;
2614 if (row + count > gui.scroll_region_bot)
2615 /* Scrolled out of region, just blank the lines out */
2616 gui_clear_block(row, gui.scroll_region_left,
2617 gui.scroll_region_bot, gui.scroll_region_right);
2618 else
2620 gui_mch_insert_lines(row, count);
2622 if (gui.cursor_row >= gui.row
2623 && gui.cursor_col >= gui.scroll_region_left
2624 && gui.cursor_col <= gui.scroll_region_right)
2626 if (gui.cursor_row <= gui.scroll_region_bot - count)
2627 gui.cursor_row += count;
2628 else if (gui.cursor_row <= gui.scroll_region_bot)
2629 gui.cursor_is_valid = FALSE;
2635 * The main GUI input routine. Waits for a character from the keyboard.
2636 * wtime == -1 Wait forever.
2637 * wtime == 0 Don't wait.
2638 * wtime > 0 Wait wtime milliseconds for a character.
2639 * Returns OK if a character was found to be available within the given time,
2640 * or FAIL otherwise.
2643 gui_wait_for_chars(wtime)
2644 long wtime;
2646 int retval;
2649 * If we're going to wait a bit, update the menus and mouse shape for the
2650 * current State.
2652 if (wtime != 0)
2654 #ifdef FEAT_MENU
2655 gui_update_menus(0);
2656 #endif
2659 gui_mch_update();
2660 if (input_available()) /* Got char, return immediately */
2661 return OK;
2662 if (wtime == 0) /* Don't wait for char */
2663 return FAIL;
2665 /* Before waiting, flush any output to the screen. */
2666 gui_mch_flush();
2668 if (wtime > 0)
2670 /* Blink when waiting for a character. Probably only does something
2671 * for showmatch() */
2672 gui_mch_start_blink();
2673 retval = gui_mch_wait_for_chars(wtime);
2674 gui_mch_stop_blink();
2675 return retval;
2679 * While we are waiting indefinitely for a character, blink the cursor.
2681 gui_mch_start_blink();
2683 retval = FAIL;
2685 * We may want to trigger the CursorHold event. First wait for
2686 * 'updatetime' and if nothing is typed within that time put the
2687 * K_CURSORHOLD key in the input buffer.
2689 if (gui_mch_wait_for_chars(p_ut) == OK)
2690 retval = OK;
2691 #ifdef FEAT_AUTOCMD
2692 else if (trigger_cursorhold())
2694 char_u buf[3];
2696 /* Put K_CURSORHOLD in the input buffer. */
2697 buf[0] = CSI;
2698 buf[1] = KS_EXTRA;
2699 buf[2] = (int)KE_CURSORHOLD;
2700 add_to_input_buf(buf, 3);
2702 retval = OK;
2704 #endif
2706 if (retval == FAIL)
2708 /* Blocking wait. */
2709 before_blocking();
2710 retval = gui_mch_wait_for_chars(-1L);
2713 gui_mch_stop_blink();
2714 return retval;
2718 * Fill p[4] with mouse coordinates encoded for check_termcode().
2720 static void
2721 fill_mouse_coord(p, col, row)
2722 char_u *p;
2723 int col;
2724 int row;
2726 p[0] = (char_u)(col / 128 + ' ' + 1);
2727 p[1] = (char_u)(col % 128 + ' ' + 1);
2728 p[2] = (char_u)(row / 128 + ' ' + 1);
2729 p[3] = (char_u)(row % 128 + ' ' + 1);
2733 * Generic mouse support function. Add a mouse event to the input buffer with
2734 * the given properties.
2735 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2736 * MOUSE_X1, MOUSE_X2
2737 * MOUSE_DRAG, or MOUSE_RELEASE.
2738 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2739 * x, y --- Coordinates of mouse in pixels.
2740 * repeated_click --- TRUE if this click comes only a short time after a
2741 * previous click.
2742 * modifiers --- Bit field which may be any of the following modifiers
2743 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2744 * This function will ignore drag events where the mouse has not moved to a new
2745 * character.
2747 void
2748 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2749 int button;
2750 int x;
2751 int y;
2752 int repeated_click;
2753 int_u modifiers;
2755 static int prev_row = 0, prev_col = 0;
2756 static int prev_button = -1;
2757 static int num_clicks = 1;
2758 char_u string[10];
2759 enum key_extra button_char;
2760 int row, col;
2761 #ifdef FEAT_CLIPBOARD
2762 int checkfor;
2763 int did_clip = FALSE;
2764 #endif
2767 * Scrolling may happen at any time, also while a selection is present.
2769 switch (button)
2771 case MOUSE_X1:
2772 button_char = KE_X1MOUSE;
2773 goto button_set;
2774 case MOUSE_X2:
2775 button_char = KE_X2MOUSE;
2776 goto button_set;
2777 case MOUSE_4:
2778 button_char = KE_MOUSEDOWN;
2779 goto button_set;
2780 case MOUSE_5:
2781 button_char = KE_MOUSEUP;
2782 button_set:
2784 /* Don't put events in the input queue now. */
2785 if (hold_gui_events)
2786 return;
2788 string[3] = CSI;
2789 string[4] = KS_EXTRA;
2790 string[5] = (int)button_char;
2792 /* Pass the pointer coordinates of the scroll event so that we
2793 * know which window to scroll. */
2794 row = gui_xy2colrow(x, y, &col);
2795 string[6] = (char_u)(col / 128 + ' ' + 1);
2796 string[7] = (char_u)(col % 128 + ' ' + 1);
2797 string[8] = (char_u)(row / 128 + ' ' + 1);
2798 string[9] = (char_u)(row % 128 + ' ' + 1);
2800 if (modifiers == 0)
2801 add_to_input_buf(string + 3, 7);
2802 else
2804 string[0] = CSI;
2805 string[1] = KS_MODIFIER;
2806 string[2] = 0;
2807 if (modifiers & MOUSE_SHIFT)
2808 string[2] |= MOD_MASK_SHIFT;
2809 if (modifiers & MOUSE_CTRL)
2810 string[2] |= MOD_MASK_CTRL;
2811 if (modifiers & MOUSE_ALT)
2812 string[2] |= MOD_MASK_ALT;
2813 add_to_input_buf(string, 10);
2815 return;
2819 #ifdef FEAT_CLIPBOARD
2820 /* If a clipboard selection is in progress, handle it */
2821 if (clip_star.state == SELECT_IN_PROGRESS)
2823 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2824 return;
2827 /* Determine which mouse settings to look for based on the current mode */
2828 switch (get_real_state())
2830 case NORMAL_BUSY:
2831 case OP_PENDING:
2832 case NORMAL: checkfor = MOUSE_NORMAL; break;
2833 case VISUAL: checkfor = MOUSE_VISUAL; break;
2834 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2835 case REPLACE:
2836 case REPLACE+LANGMAP:
2837 #ifdef FEAT_VREPLACE
2838 case VREPLACE:
2839 case VREPLACE+LANGMAP:
2840 #endif
2841 case INSERT:
2842 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2843 case ASKMORE:
2844 case HITRETURN: /* At the more- and hit-enter prompt pass the
2845 mouse event for a click on or below the
2846 message line. */
2847 if (Y_2_ROW(y) >= msg_row)
2848 checkfor = MOUSE_NORMAL;
2849 else
2850 checkfor = MOUSE_RETURN;
2851 break;
2854 * On the command line, use the clipboard selection on all lines
2855 * but the command line. But not when pasting.
2857 case CMDLINE:
2858 case CMDLINE+LANGMAP:
2859 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2860 checkfor = MOUSE_NONE;
2861 else
2862 checkfor = MOUSE_COMMAND;
2863 break;
2865 default:
2866 checkfor = MOUSE_NONE;
2867 break;
2871 * Allow clipboard selection of text on the command line in "normal"
2872 * modes. Don't do this when dragging the status line, or extending a
2873 * Visual selection.
2875 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2876 && Y_2_ROW(y) >= topframe->fr_height
2877 # ifdef FEAT_WINDOWS
2878 + firstwin->w_winrow
2879 # endif
2880 && button != MOUSE_DRAG
2881 # ifdef FEAT_MOUSESHAPE
2882 && !drag_status_line
2883 # ifdef FEAT_VERTSPLIT
2884 && !drag_sep_line
2885 # endif
2886 # endif
2888 checkfor = MOUSE_NONE;
2891 * Use modeless selection when holding CTRL and SHIFT pressed.
2893 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2894 checkfor = MOUSE_NONEF;
2897 * In Ex mode, always use modeless selection.
2899 if (exmode_active)
2900 checkfor = MOUSE_NONE;
2903 * If the mouse settings say to not use the mouse, use the modeless
2904 * selection. But if Visual is active, assume that only the Visual area
2905 * will be selected.
2906 * Exception: On the command line, both the selection is used and a mouse
2907 * key is send.
2909 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2911 #ifdef FEAT_VISUAL
2912 /* Don't do modeless selection in Visual mode. */
2913 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2914 return;
2915 #endif
2918 * When 'mousemodel' is "popup", shift-left is translated to right.
2919 * But not when also using Ctrl.
2921 if (mouse_model_popup() && button == MOUSE_LEFT
2922 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2924 button = MOUSE_RIGHT;
2925 modifiers &= ~ MOUSE_SHIFT;
2928 /* If the selection is done, allow the right button to extend it.
2929 * If the selection is cleared, allow the right button to start it
2930 * from the cursor position. */
2931 if (button == MOUSE_RIGHT)
2933 if (clip_star.state == SELECT_CLEARED)
2935 if (State & CMDLINE)
2937 col = msg_col;
2938 row = msg_row;
2940 else
2942 col = curwin->w_wcol;
2943 row = curwin->w_wrow + W_WINROW(curwin);
2945 clip_start_selection(col, row, FALSE);
2947 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2948 repeated_click);
2949 did_clip = TRUE;
2951 /* Allow the left button to start the selection */
2952 else if (button ==
2953 # ifdef RISCOS
2954 /* Only start a drag on a drag event. Otherwise
2955 * we don't get a release event. */
2956 MOUSE_DRAG
2957 # else
2958 MOUSE_LEFT
2959 # endif
2962 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2963 did_clip = TRUE;
2965 # ifdef RISCOS
2966 else if (button == MOUSE_LEFT)
2968 clip_clear_selection();
2969 did_clip = TRUE;
2971 # endif
2973 /* Always allow pasting */
2974 if (button != MOUSE_MIDDLE)
2976 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2977 return;
2978 if (checkfor != MOUSE_COMMAND)
2979 button = MOUSE_LEFT;
2981 repeated_click = FALSE;
2984 if (clip_star.state != SELECT_CLEARED && !did_clip)
2985 clip_clear_selection();
2986 #endif
2988 /* Don't put events in the input queue now. */
2989 if (hold_gui_events)
2990 return;
2992 row = gui_xy2colrow(x, y, &col);
2995 * If we are dragging and the mouse hasn't moved far enough to be on a
2996 * different character, then don't send an event to vim.
2998 if (button == MOUSE_DRAG)
3000 if (row == prev_row && col == prev_col)
3001 return;
3002 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3003 if (y < 0)
3004 row = -1;
3008 * If topline has changed (window scrolled) since the last click, reset
3009 * repeated_click, because we don't want starting Visual mode when
3010 * clicking on a different character in the text.
3012 if (curwin->w_topline != gui_prev_topline
3013 #ifdef FEAT_DIFF
3014 || curwin->w_topfill != gui_prev_topfill
3015 #endif
3017 repeated_click = FALSE;
3019 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3020 string[1] = KS_MOUSE;
3021 string[2] = KE_FILLER;
3022 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3024 if (repeated_click)
3027 * Handle multiple clicks. They only count if the mouse is still
3028 * pointing at the same character.
3030 if (button != prev_button || row != prev_row || col != prev_col)
3031 num_clicks = 1;
3032 else if (++num_clicks > 4)
3033 num_clicks = 1;
3035 else
3036 num_clicks = 1;
3037 prev_button = button;
3038 gui_prev_topline = curwin->w_topline;
3039 #ifdef FEAT_DIFF
3040 gui_prev_topfill = curwin->w_topfill;
3041 #endif
3043 string[3] = (char_u)(button | 0x20);
3044 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3046 else
3047 string[3] = (char_u)button;
3049 string[3] |= modifiers;
3050 fill_mouse_coord(string + 4, col, row);
3051 add_to_input_buf(string, 8);
3053 if (row < 0)
3054 prev_row = 0;
3055 else
3056 prev_row = row;
3057 prev_col = col;
3060 * We need to make sure this is cleared since Athena doesn't tell us when
3061 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3063 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3064 gui.dragged_sb = SBAR_NONE;
3065 #endif
3069 * Convert x and y coordinate to column and row in text window.
3070 * Corrects for multi-byte character.
3071 * returns column in "*colp" and row as return value;
3074 gui_xy2colrow(x, y, colp)
3075 int x;
3076 int y;
3077 int *colp;
3079 int col = check_col(X_2_COL(x));
3080 int row = check_row(Y_2_ROW(y));
3082 #ifdef FEAT_MBYTE
3083 *colp = mb_fix_col(col, row);
3084 #else
3085 *colp = col;
3086 #endif
3087 return row;
3090 #if defined(FEAT_MENU) || defined(PROTO)
3092 * Callback function for when a menu entry has been selected.
3094 void
3095 gui_menu_cb(menu)
3096 vimmenu_T *menu;
3098 char_u bytes[sizeof(long_u)];
3100 /* Don't put events in the input queue now. */
3101 if (hold_gui_events)
3102 return;
3104 bytes[0] = CSI;
3105 bytes[1] = KS_MENU;
3106 bytes[2] = KE_FILLER;
3107 add_to_input_buf(bytes, 3);
3108 add_long_to_buf((long_u)menu, bytes);
3109 add_to_input_buf_csi(bytes, sizeof(long_u));
3111 #endif
3113 static int prev_which_scrollbars[3];
3116 * Set which components are present.
3117 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3118 * in p_go.
3120 /*ARGSUSED*/
3121 void
3122 gui_init_which_components(oldval)
3123 char_u *oldval;
3125 #ifdef FEAT_MENU
3126 static int prev_menu_is_active = -1;
3127 #endif
3128 #ifdef FEAT_TOOLBAR
3129 static int prev_toolbar = -1;
3130 int using_toolbar = FALSE;
3131 #endif
3132 #ifdef FEAT_GUI_TABLINE
3133 int using_tabline;
3134 #endif
3135 #ifdef FEAT_FOOTER
3136 static int prev_footer = -1;
3137 int using_footer = FALSE;
3138 #endif
3139 #if defined(FEAT_MENU) && !defined(WIN16)
3140 static int prev_tearoff = -1;
3141 int using_tearoff = FALSE;
3142 #endif
3144 char_u *p;
3145 int i;
3146 #ifdef FEAT_MENU
3147 int grey_old, grey_new;
3148 char_u *temp;
3149 #endif
3150 win_T *wp;
3151 int need_set_size;
3152 int fix_size;
3154 #ifdef FEAT_MENU
3155 if (oldval != NULL && gui.in_use)
3158 * Check if the menu's go from grey to non-grey or vise versa.
3160 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3161 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3162 if (grey_old != grey_new)
3164 temp = p_go;
3165 p_go = oldval;
3166 gui_update_menus(MENU_ALL_MODES);
3167 p_go = temp;
3170 gui.menu_is_active = FALSE;
3171 #endif
3173 for (i = 0; i < 3; i++)
3174 gui.which_scrollbars[i] = FALSE;
3175 for (p = p_go; *p; p++)
3176 switch (*p)
3178 case GO_LEFT:
3179 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3180 break;
3181 case GO_RIGHT:
3182 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3183 break;
3184 #ifdef FEAT_VERTSPLIT
3185 case GO_VLEFT:
3186 if (win_hasvertsplit())
3187 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3188 break;
3189 case GO_VRIGHT:
3190 if (win_hasvertsplit())
3191 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3192 break;
3193 #endif
3194 case GO_BOT:
3195 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3196 break;
3197 #ifdef FEAT_MENU
3198 case GO_MENUS:
3199 gui.menu_is_active = TRUE;
3200 break;
3201 #endif
3202 case GO_GREY:
3203 /* make menu's have grey items, ignored here */
3204 break;
3205 #ifdef FEAT_TOOLBAR
3206 case GO_TOOLBAR:
3207 using_toolbar = TRUE;
3208 break;
3209 #endif
3210 #ifdef FEAT_FOOTER
3211 case GO_FOOTER:
3212 using_footer = TRUE;
3213 break;
3214 #endif
3215 case GO_TEAROFF:
3216 #if defined(FEAT_MENU) && !defined(WIN16)
3217 using_tearoff = TRUE;
3218 #endif
3219 break;
3220 default:
3221 /* Ignore options that are not supported */
3222 break;
3225 if (gui.in_use)
3227 need_set_size = 0;
3228 fix_size = FALSE;
3230 #ifdef FEAT_GUI_TABLINE
3231 /* Update the GUI tab line, it may appear or disappear. This may
3232 * cause the non-GUI tab line to disappear or appear. */
3233 using_tabline = gui_has_tabline();
3234 if (!gui_mch_showing_tabline() != !using_tabline)
3236 /* We don't want a resize event change "Rows" here, save and
3237 * restore it. Resizing is handled below. */
3238 i = Rows;
3239 gui_update_tabline();
3240 Rows = i;
3241 need_set_size = RESIZE_VERT;
3242 if (using_tabline)
3243 fix_size = TRUE;
3244 if (!gui_use_tabline())
3245 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3247 #endif
3249 for (i = 0; i < 3; i++)
3251 /* The scrollbar needs to be updated when it is shown/unshown and
3252 * when switching tab pages. But the size only changes when it's
3253 * shown/unshown. Thus we need two places to remember whether a
3254 * scrollbar is there or not. */
3255 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3256 #ifdef FEAT_WINDOWS
3257 || gui.which_scrollbars[i]
3258 != curtab->tp_prev_which_scrollbars[i]
3259 #endif
3262 if (i == SBAR_BOTTOM)
3263 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3264 gui.which_scrollbars[i]);
3265 else
3267 FOR_ALL_WINDOWS(wp)
3269 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3272 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3274 if (i == SBAR_BOTTOM)
3275 need_set_size = RESIZE_VERT;
3276 else
3277 need_set_size = RESIZE_HOR;
3278 if (gui.which_scrollbars[i])
3279 fix_size = TRUE;
3282 #ifdef FEAT_WINDOWS
3283 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3284 #endif
3285 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3288 #ifdef FEAT_MENU
3289 if (gui.menu_is_active != prev_menu_is_active)
3291 /* We don't want a resize event change "Rows" here, save and
3292 * restore it. Resizing is handled below. */
3293 i = Rows;
3294 gui_mch_enable_menu(gui.menu_is_active);
3295 Rows = i;
3296 prev_menu_is_active = gui.menu_is_active;
3297 need_set_size = RESIZE_VERT;
3298 if (gui.menu_is_active)
3299 fix_size = TRUE;
3301 #endif
3303 #ifdef FEAT_TOOLBAR
3304 if (using_toolbar != prev_toolbar)
3306 gui_mch_show_toolbar(using_toolbar);
3307 prev_toolbar = using_toolbar;
3308 need_set_size = RESIZE_VERT;
3309 if (using_toolbar)
3310 fix_size = TRUE;
3312 #endif
3313 #ifdef FEAT_FOOTER
3314 if (using_footer != prev_footer)
3316 gui_mch_enable_footer(using_footer);
3317 prev_footer = using_footer;
3318 need_set_size = RESIZE_VERT;
3319 if (using_footer)
3320 fix_size = TRUE;
3322 #endif
3323 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3324 if (using_tearoff != prev_tearoff)
3326 gui_mch_toggle_tearoffs(using_tearoff);
3327 prev_tearoff = using_tearoff;
3329 #endif
3330 if (need_set_size)
3332 #ifdef FEAT_GUI_GTK
3333 long c = Columns;
3334 #endif
3335 /* Adjust the size of the window to make the text area keep the
3336 * same size and to avoid that part of our window is off-screen
3337 * and a scrollbar can't be used, for example. */
3338 gui_set_shellsize(FALSE, fix_size, need_set_size);
3340 #ifdef FEAT_GUI_GTK
3341 /* GTK has the annoying habit of sending us resize events when
3342 * changing the window size ourselves. This mostly happens when
3343 * waiting for a character to arrive, quite unpredictably, and may
3344 * change Columns and Rows when we don't want it. Wait for a
3345 * character here to avoid this effect.
3346 * If you remove this, please test this command for resizing
3347 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3348 * Don't do this while starting up though.
3349 * And don't change Rows, it may have be reduced intentionally
3350 * when adding menu/toolbar/tabline. */
3351 if (!gui.starting)
3352 (void)char_avail();
3353 Columns = c;
3354 #endif
3356 #ifdef FEAT_WINDOWS
3357 /* When the console tabline appears or disappears the window positions
3358 * change. */
3359 if (firstwin->w_winrow != tabline_height())
3360 shell_new_rows(); /* recompute window positions and heights */
3361 #endif
3365 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3367 * Return TRUE if the GUI is taking care of the tabline.
3368 * It may still be hidden if 'showtabline' is zero.
3371 gui_use_tabline()
3373 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3377 * Return TRUE if the GUI is showing the tabline.
3378 * This uses 'showtabline'.
3380 static int
3381 gui_has_tabline()
3383 if (!gui_use_tabline()
3384 || p_stal == 0
3385 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3386 return FALSE;
3387 return TRUE;
3391 * Update the tabline.
3392 * This may display/undisplay the tabline and update the labels.
3394 void
3395 gui_update_tabline()
3397 int showit = gui_has_tabline();
3398 int shown = gui_mch_showing_tabline();
3400 if (!gui.starting && starting == 0)
3402 /* Updating the tabline uses direct GUI commands, flush
3403 * outstanding instructions first. (esp. clear screen) */
3404 out_flush();
3405 gui_mch_flush();
3407 if (!showit != !shown)
3408 gui_mch_show_tabline(showit);
3409 if (showit != 0)
3410 gui_mch_update_tabline();
3412 /* When the tabs change from hidden to shown or from shown to
3413 * hidden the size of the text area should remain the same. */
3414 if (!showit != !shown)
3415 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3420 * Get the label or tooltip for tab page "tp" into NameBuff[].
3422 void
3423 get_tabline_label(tp, tooltip)
3424 tabpage_T *tp;
3425 int tooltip; /* TRUE: get tooltip */
3427 int modified = FALSE;
3428 char_u buf[40];
3429 int wincount;
3430 win_T *wp;
3431 char_u **opt;
3433 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3434 opt = (tooltip ? &p_gtt : &p_gtl);
3435 if (**opt != NUL)
3437 int use_sandbox = FALSE;
3438 int save_called_emsg = called_emsg;
3439 char_u res[MAXPATHL];
3440 tabpage_T *save_curtab;
3441 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3442 : "guitablabel");
3444 called_emsg = FALSE;
3446 printer_page_num = tabpage_index(tp);
3447 # ifdef FEAT_EVAL
3448 set_vim_var_nr(VV_LNUM, printer_page_num);
3449 use_sandbox = was_set_insecurely(opt_name, 0);
3450 # endif
3451 /* It's almost as going to the tabpage, but without autocommands. */
3452 curtab->tp_firstwin = firstwin;
3453 curtab->tp_lastwin = lastwin;
3454 curtab->tp_curwin = curwin;
3455 save_curtab = curtab;
3456 curtab = tp;
3457 topframe = curtab->tp_topframe;
3458 firstwin = curtab->tp_firstwin;
3459 lastwin = curtab->tp_lastwin;
3460 curwin = curtab->tp_curwin;
3461 curbuf = curwin->w_buffer;
3463 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3464 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3465 0, (int)Columns, NULL, NULL);
3466 STRCPY(NameBuff, res);
3468 /* Back to the original curtab. */
3469 curtab = save_curtab;
3470 topframe = curtab->tp_topframe;
3471 firstwin = curtab->tp_firstwin;
3472 lastwin = curtab->tp_lastwin;
3473 curwin = curtab->tp_curwin;
3474 curbuf = curwin->w_buffer;
3476 if (called_emsg)
3477 set_string_option_direct(opt_name, -1,
3478 (char_u *)"", OPT_FREE, SID_ERROR);
3479 called_emsg |= save_called_emsg;
3482 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3483 * use a default label. */
3484 if (**opt == NUL || *NameBuff == NUL)
3486 /* Get the buffer name into NameBuff[] and shorten it. */
3487 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3488 if (!tooltip)
3489 shorten_dir(NameBuff);
3491 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3492 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3493 if (bufIsChanged(wp->w_buffer))
3494 modified = TRUE;
3495 if (modified || wincount > 1)
3497 if (wincount > 1)
3498 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3499 else
3500 buf[0] = NUL;
3501 if (modified)
3502 STRCAT(buf, "+");
3503 STRCAT(buf, " ");
3504 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3505 mch_memmove(NameBuff, buf, STRLEN(buf));
3511 * Send the event for clicking to select tab page "nr".
3512 * Returns TRUE if it was done, FALSE when skipped because we are already at
3513 * that tab page or the cmdline window is open.
3516 send_tabline_event(nr)
3517 int nr;
3519 char_u string[3];
3521 if (nr == tabpage_index(curtab))
3522 return FALSE;
3524 /* Don't put events in the input queue now. */
3525 if (hold_gui_events
3526 # ifdef FEAT_CMDWIN
3527 || cmdwin_type != 0
3528 # endif
3531 /* Set it back to the current tab page. */
3532 gui_mch_set_curtab(tabpage_index(curtab));
3533 return FALSE;
3536 string[0] = CSI;
3537 string[1] = KS_TABLINE;
3538 string[2] = KE_FILLER;
3539 add_to_input_buf(string, 3);
3540 string[0] = nr;
3541 add_to_input_buf_csi(string, 1);
3542 return TRUE;
3546 * Send a tabline menu event
3548 void
3549 send_tabline_menu_event(tabidx, event)
3550 int tabidx;
3551 int event;
3553 char_u string[3];
3555 /* Don't put events in the input queue now. */
3556 if (hold_gui_events)
3557 return;
3559 string[0] = CSI;
3560 string[1] = KS_TABMENU;
3561 string[2] = KE_FILLER;
3562 add_to_input_buf(string, 3);
3563 string[0] = tabidx;
3564 string[1] = (char_u)(long)event;
3565 add_to_input_buf_csi(string, 2);
3568 #endif
3571 * Scrollbar stuff:
3574 #if defined(FEAT_WINDOWS) || defined(PROTO)
3576 * Remove all scrollbars. Used before switching to another tab page.
3578 void
3579 gui_remove_scrollbars()
3581 int i;
3582 win_T *wp;
3584 for (i = 0; i < 3; i++)
3586 if (i == SBAR_BOTTOM)
3587 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3588 else
3590 FOR_ALL_WINDOWS(wp)
3592 gui_do_scrollbar(wp, i, FALSE);
3595 curtab->tp_prev_which_scrollbars[i] = -1;
3598 #endif
3600 void
3601 gui_create_scrollbar(sb, type, wp)
3602 scrollbar_T *sb;
3603 int type;
3604 win_T *wp;
3606 static int sbar_ident = 0;
3608 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3609 sb->wp = wp;
3610 sb->type = type;
3611 sb->value = 0;
3612 #ifdef FEAT_GUI_ATHENA
3613 sb->pixval = 0;
3614 #endif
3615 sb->size = 1;
3616 sb->max = 1;
3617 sb->top = 0;
3618 sb->height = 0;
3619 #ifdef FEAT_VERTSPLIT
3620 sb->width = 0;
3621 #endif
3622 sb->status_height = 0;
3623 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3627 * Find the scrollbar with the given index.
3629 scrollbar_T *
3630 gui_find_scrollbar(ident)
3631 long ident;
3633 win_T *wp;
3635 if (gui.bottom_sbar.ident == ident)
3636 return &gui.bottom_sbar;
3637 FOR_ALL_WINDOWS(wp)
3639 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3640 return &wp->w_scrollbars[SBAR_LEFT];
3641 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3642 return &wp->w_scrollbars[SBAR_RIGHT];
3644 return NULL;
3648 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3650 * For Win32, Macintosh and GTK+ 2:
3651 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3652 * you stop dragging the scrollbar. We get here each time the scrollbar is
3653 * dragged another pixel, but as far as the rest of vim goes, it thinks
3654 * we're just hanging in the call to DispatchMessage() in
3655 * process_message(). The DispatchMessage() call that hangs was passed a
3656 * mouse button click event in the scrollbar window. -- webb.
3658 * Solution: Do the scrolling right here. But only when allowed.
3659 * Ignore the scrollbars while executing an external command or when there
3660 * are still characters to be processed.
3662 void
3663 gui_drag_scrollbar(sb, value, still_dragging)
3664 scrollbar_T *sb;
3665 long value;
3666 int still_dragging;
3668 #ifdef FEAT_WINDOWS
3669 win_T *wp;
3670 #endif
3671 int sb_num;
3672 #ifdef USE_ON_FLY_SCROLL
3673 colnr_T old_leftcol = curwin->w_leftcol;
3674 # ifdef FEAT_SCROLLBIND
3675 linenr_T old_topline = curwin->w_topline;
3676 # endif
3677 # ifdef FEAT_DIFF
3678 int old_topfill = curwin->w_topfill;
3679 # endif
3680 #else
3681 char_u bytes[sizeof(long_u)];
3682 int byte_count;
3683 #endif
3685 if (sb == NULL)
3686 return;
3688 /* Don't put events in the input queue now. */
3689 if (hold_gui_events)
3690 return;
3692 #ifdef FEAT_CMDWIN
3693 if (cmdwin_type != 0 && sb->wp != curwin)
3694 return;
3695 #endif
3697 if (still_dragging)
3699 if (sb->wp == NULL)
3700 gui.dragged_sb = SBAR_BOTTOM;
3701 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3702 gui.dragged_sb = SBAR_LEFT;
3703 else
3704 gui.dragged_sb = SBAR_RIGHT;
3705 gui.dragged_wp = sb->wp;
3707 else
3709 gui.dragged_sb = SBAR_NONE;
3710 #ifdef HAVE_GTK2
3711 /* Keep the "dragged_wp" value until after the scrolling, for when the
3712 * moust button is released. GTK2 doesn't send the button-up event. */
3713 gui.dragged_wp = NULL;
3714 #endif
3717 /* Vertical sbar info is kept in the first sbar (the left one) */
3718 if (sb->wp != NULL)
3719 sb = &sb->wp->w_scrollbars[0];
3722 * Check validity of value
3724 if (value < 0)
3725 value = 0;
3726 #ifdef SCROLL_PAST_END
3727 else if (value > sb->max)
3728 value = sb->max;
3729 #else
3730 if (value > sb->max - sb->size + 1)
3731 value = sb->max - sb->size + 1;
3732 #endif
3734 sb->value = value;
3736 #ifdef USE_ON_FLY_SCROLL
3737 /* When not allowed to do the scrolling right now, return. */
3738 if (dont_scroll || input_available())
3739 return;
3740 #endif
3741 #ifdef FEAT_INS_EXPAND
3742 /* Disallow scrolling the current window when the completion popup menu is
3743 * visible. */
3744 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3745 return;
3746 #endif
3748 #ifdef FEAT_RIGHTLEFT
3749 if (sb->wp == NULL && curwin->w_p_rl)
3751 value = sb->max + 1 - sb->size - value;
3752 if (value < 0)
3753 value = 0;
3755 #endif
3757 if (sb->wp != NULL) /* vertical scrollbar */
3759 sb_num = 0;
3760 #ifdef FEAT_WINDOWS
3761 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3762 sb_num++;
3763 if (wp == NULL)
3764 return;
3765 #else
3766 if (sb->wp != curwin)
3767 return;
3768 #endif
3770 #ifdef USE_ON_FLY_SCROLL
3771 current_scrollbar = sb_num;
3772 scrollbar_value = value;
3773 if (State & NORMAL)
3775 gui_do_scroll();
3776 setcursor();
3778 else if (State & INSERT)
3780 ins_scroll();
3781 setcursor();
3783 else if (State & CMDLINE)
3785 if (msg_scrolled == 0)
3787 gui_do_scroll();
3788 redrawcmdline();
3791 # ifdef FEAT_FOLDING
3792 /* Value may have been changed for closed fold. */
3793 sb->value = sb->wp->w_topline - 1;
3794 # endif
3796 /* When dragging one scrollbar and there is another one at the other
3797 * side move the thumb of that one too. */
3798 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3799 gui_mch_set_scrollbar_thumb(
3800 &sb->wp->w_scrollbars[
3801 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3802 ? SBAR_LEFT : SBAR_RIGHT],
3803 sb->value, sb->size, sb->max);
3805 #else
3806 bytes[0] = CSI;
3807 bytes[1] = KS_VER_SCROLLBAR;
3808 bytes[2] = KE_FILLER;
3809 bytes[3] = (char_u)sb_num;
3810 byte_count = 4;
3811 #endif
3813 else
3815 #ifdef USE_ON_FLY_SCROLL
3816 scrollbar_value = value;
3818 if (State & NORMAL)
3819 gui_do_horiz_scroll();
3820 else if (State & INSERT)
3821 ins_horscroll();
3822 else if (State & CMDLINE)
3824 if (msg_scrolled == 0)
3826 gui_do_horiz_scroll();
3827 redrawcmdline();
3830 if (old_leftcol != curwin->w_leftcol)
3832 updateWindow(curwin); /* update window, status and cmdline */
3833 setcursor();
3835 #else
3836 bytes[0] = CSI;
3837 bytes[1] = KS_HOR_SCROLLBAR;
3838 bytes[2] = KE_FILLER;
3839 byte_count = 3;
3840 #endif
3843 #ifdef USE_ON_FLY_SCROLL
3844 # ifdef FEAT_SCROLLBIND
3846 * synchronize other windows, as necessary according to 'scrollbind'
3848 if (curwin->w_p_scb
3849 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3850 || (sb->wp == curwin && (curwin->w_topline != old_topline
3851 # ifdef FEAT_DIFF
3852 || curwin->w_topfill != old_topfill
3853 # endif
3854 ))))
3856 do_check_scrollbind(TRUE);
3857 /* need to update the window right here */
3858 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3859 if (wp->w_redr_type > 0)
3860 updateWindow(wp);
3861 setcursor();
3863 # endif
3864 out_flush();
3865 gui_update_cursor(FALSE, TRUE);
3866 #else
3867 add_to_input_buf(bytes, byte_count);
3868 add_long_to_buf((long_u)value, bytes);
3869 add_to_input_buf_csi(bytes, sizeof(long_u));
3870 #endif
3874 * Scrollbar stuff:
3877 void
3878 gui_update_scrollbars(force)
3879 int force; /* Force all scrollbars to get updated */
3881 win_T *wp;
3882 scrollbar_T *sb;
3883 long val, size, max; /* need 32 bits here */
3884 int which_sb;
3885 int h, y;
3886 #ifdef FEAT_VERTSPLIT
3887 static win_T *prev_curwin = NULL;
3888 #endif
3890 /* Update the horizontal scrollbar */
3891 gui_update_horiz_scrollbar(force);
3893 #ifndef WIN3264
3894 /* Return straight away if there is neither a left nor right scrollbar.
3895 * On MS-Windows this is required anyway for scrollwheel messages. */
3896 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3897 return;
3898 #endif
3901 * Don't want to update a scrollbar while we're dragging it. But if we
3902 * have both a left and right scrollbar, and we drag one of them, we still
3903 * need to update the other one.
3905 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3906 && gui.which_scrollbars[SBAR_LEFT]
3907 && gui.which_scrollbars[SBAR_RIGHT])
3910 * If we have two scrollbars and one of them is being dragged, just
3911 * copy the scrollbar position from the dragged one to the other one.
3913 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3914 if (gui.dragged_wp != NULL)
3915 gui_mch_set_scrollbar_thumb(
3916 &gui.dragged_wp->w_scrollbars[which_sb],
3917 gui.dragged_wp->w_scrollbars[0].value,
3918 gui.dragged_wp->w_scrollbars[0].size,
3919 gui.dragged_wp->w_scrollbars[0].max);
3922 /* avoid that moving components around generates events */
3923 ++hold_gui_events;
3925 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3927 if (wp->w_buffer == NULL) /* just in case */
3928 continue;
3929 /* Skip a scrollbar that is being dragged. */
3930 if (!force && (gui.dragged_sb == SBAR_LEFT
3931 || gui.dragged_sb == SBAR_RIGHT)
3932 && gui.dragged_wp == wp)
3933 continue;
3935 #ifdef SCROLL_PAST_END
3936 max = wp->w_buffer->b_ml.ml_line_count - 1;
3937 #else
3938 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3939 #endif
3940 if (max < 0) /* empty buffer */
3941 max = 0;
3942 val = wp->w_topline - 1;
3943 size = wp->w_height;
3944 #ifdef SCROLL_PAST_END
3945 if (val > max) /* just in case */
3946 val = max;
3947 #else
3948 if (size > max + 1) /* just in case */
3949 size = max + 1;
3950 if (val > max - size + 1)
3951 val = max - size + 1;
3952 #endif
3953 if (val < 0) /* minimal value is 0 */
3954 val = 0;
3957 * Scrollbar at index 0 (the left one) contains all the information.
3958 * It would be the same info for left and right so we just store it for
3959 * one of them.
3961 sb = &wp->w_scrollbars[0];
3964 * Note: no check for valid w_botline. If it's not valid the
3965 * scrollbars will be updated later anyway.
3967 if (size < 1 || wp->w_botline - 2 > max)
3970 * This can happen during changing files. Just don't update the
3971 * scrollbar for now.
3973 sb->height = 0; /* Force update next time */
3974 if (gui.which_scrollbars[SBAR_LEFT])
3975 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3976 if (gui.which_scrollbars[SBAR_RIGHT])
3977 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3978 continue;
3980 if (force || sb->height != wp->w_height
3981 #ifdef FEAT_WINDOWS
3982 || sb->top != wp->w_winrow
3983 || sb->status_height != wp->w_status_height
3984 # ifdef FEAT_VERTSPLIT
3985 || sb->width != wp->w_width
3986 || prev_curwin != curwin
3987 # endif
3988 #endif
3991 /* Height, width or position of scrollbar has changed. For
3992 * vertical split: curwin changed. */
3993 sb->height = wp->w_height;
3994 #ifdef FEAT_WINDOWS
3995 sb->top = wp->w_winrow;
3996 sb->status_height = wp->w_status_height;
3997 # ifdef FEAT_VERTSPLIT
3998 sb->width = wp->w_width;
3999 # endif
4000 #endif
4002 /* Calculate height and position in pixels */
4003 h = (sb->height + sb->status_height) * gui.char_height;
4004 y = sb->top * gui.char_height + gui.border_offset;
4005 #if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4006 if (gui.menu_is_active)
4007 y += gui.menu_height;
4008 #endif
4010 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4011 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4012 # ifdef FEAT_GUI_ATHENA
4013 y += gui.toolbar_height;
4014 # else
4015 # ifdef FEAT_GUI_MSWIN
4016 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4017 # endif
4018 # endif
4019 #endif
4021 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4022 if (gui_has_tabline())
4023 y += gui.tabline_height;
4024 #endif
4026 #ifdef FEAT_WINDOWS
4027 if (wp->w_winrow == 0)
4028 #endif
4030 /* Height of top scrollbar includes width of top border */
4031 h += gui.border_offset;
4032 y -= gui.border_offset;
4034 if (gui.which_scrollbars[SBAR_LEFT])
4036 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4037 gui.left_sbar_x, y,
4038 gui.scrollbar_width, h);
4039 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4041 if (gui.which_scrollbars[SBAR_RIGHT])
4043 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4044 gui.right_sbar_x, y,
4045 gui.scrollbar_width, h);
4046 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4050 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4051 * checking if the thumb moved at least a pixel. Only do this for
4052 * Athena, most other GUIs require the update anyway to make the
4053 * arrows work. */
4054 #ifdef FEAT_GUI_ATHENA
4055 if (max == 0)
4056 y = 0;
4057 else
4058 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4059 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4060 #else
4061 if (force || sb->value != val || sb->size != size || sb->max != max)
4062 #endif
4064 /* Thumb of scrollbar has moved */
4065 sb->value = val;
4066 #ifdef FEAT_GUI_ATHENA
4067 sb->pixval = y;
4068 #endif
4069 sb->size = size;
4070 sb->max = max;
4071 if (gui.which_scrollbars[SBAR_LEFT]
4072 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4073 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4074 val, size, max);
4075 if (gui.which_scrollbars[SBAR_RIGHT]
4076 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4077 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4078 val, size, max);
4081 #ifdef FEAT_VERTSPLIT
4082 prev_curwin = curwin;
4083 #endif
4084 --hold_gui_events;
4088 * Enable or disable a scrollbar.
4089 * Check for scrollbars for vertically split windows which are not enabled
4090 * sometimes.
4092 static void
4093 gui_do_scrollbar(wp, which, enable)
4094 win_T *wp;
4095 int which; /* SBAR_LEFT or SBAR_RIGHT */
4096 int enable; /* TRUE to enable scrollbar */
4098 #ifdef FEAT_VERTSPLIT
4099 int midcol = curwin->w_wincol + curwin->w_width / 2;
4100 int has_midcol = (wp->w_wincol <= midcol
4101 && wp->w_wincol + wp->w_width >= midcol);
4103 /* Only enable scrollbars that contain the middle column of the current
4104 * window. */
4105 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4107 /* Scrollbars only on one side. Don't enable scrollbars that don't
4108 * contain the middle column of the current window. */
4109 if (!has_midcol)
4110 enable = FALSE;
4112 else
4114 /* Scrollbars on both sides. Don't enable scrollbars that neither
4115 * contain the middle column of the current window nor are on the far
4116 * side. */
4117 if (midcol > Columns / 2)
4119 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4120 enable = FALSE;
4122 else
4124 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4125 : !has_midcol)
4126 enable = FALSE;
4129 #endif
4130 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4134 * Scroll a window according to the values set in the globals current_scrollbar
4135 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4136 * or FALSE otherwise.
4139 gui_do_scroll()
4141 win_T *wp, *save_wp;
4142 int i;
4143 long nlines;
4144 pos_T old_cursor;
4145 linenr_T old_topline;
4146 #ifdef FEAT_DIFF
4147 int old_topfill;
4148 #endif
4150 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4151 if (wp == NULL)
4152 break;
4153 if (wp == NULL)
4154 /* Couldn't find window */
4155 return FALSE;
4158 * Compute number of lines to scroll. If zero, nothing to do.
4160 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4161 if (nlines == 0)
4162 return FALSE;
4164 save_wp = curwin;
4165 old_topline = wp->w_topline;
4166 #ifdef FEAT_DIFF
4167 old_topfill = wp->w_topfill;
4168 #endif
4169 old_cursor = wp->w_cursor;
4170 curwin = wp;
4171 curbuf = wp->w_buffer;
4172 if (nlines < 0)
4173 scrolldown(-nlines, gui.dragged_wp == NULL);
4174 else
4175 scrollup(nlines, gui.dragged_wp == NULL);
4176 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4177 * the mouse-up event already, but we still want it to behave like when
4178 * dragging. But not the next click in an arrow. */
4179 if (gui.dragged_sb == SBAR_NONE)
4180 gui.dragged_wp = NULL;
4182 if (old_topline != wp->w_topline
4183 #ifdef FEAT_DIFF
4184 || old_topfill != wp->w_topfill
4185 #endif
4188 if (p_so != 0)
4190 cursor_correct(); /* fix window for 'so' */
4191 update_topline(); /* avoid up/down jump */
4193 if (old_cursor.lnum != wp->w_cursor.lnum)
4194 coladvance(wp->w_curswant);
4195 #ifdef FEAT_SCROLLBIND
4196 wp->w_scbind_pos = wp->w_topline;
4197 #endif
4200 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4201 validate_cursor();
4203 curwin = save_wp;
4204 curbuf = save_wp->w_buffer;
4207 * Don't call updateWindow() when nothing has changed (it will overwrite
4208 * the status line!).
4210 if (old_topline != wp->w_topline
4211 || wp->w_redr_type != 0
4212 #ifdef FEAT_DIFF
4213 || old_topfill != wp->w_topfill
4214 #endif
4217 redraw_win_later(wp, VALID);
4218 updateWindow(wp); /* update window, status line, and cmdline */
4221 #ifdef FEAT_INS_EXPAND
4222 /* May need to redraw the popup menu. */
4223 if (pum_visible())
4224 pum_redraw();
4225 #endif
4227 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4232 * Horizontal scrollbar stuff:
4236 * Return length of line "lnum" for horizontal scrolling.
4238 static colnr_T
4239 scroll_line_len(lnum)
4240 linenr_T lnum;
4242 char_u *p;
4243 colnr_T col;
4244 int w;
4246 p = ml_get(lnum);
4247 col = 0;
4248 if (*p != NUL)
4249 for (;;)
4251 w = chartabsize(p, col);
4252 mb_ptr_adv(p);
4253 if (*p == NUL) /* don't count the last character */
4254 break;
4255 col += w;
4257 return col;
4260 /* Remember which line is currently the longest, so that we don't have to
4261 * search for it when scrolling horizontally. */
4262 static linenr_T longest_lnum = 0;
4264 static void
4265 gui_update_horiz_scrollbar(force)
4266 int force;
4268 long value, size, max; /* need 32 bit ints here */
4270 if (!gui.which_scrollbars[SBAR_BOTTOM])
4271 return;
4273 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4274 return;
4276 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4277 return;
4280 * It is possible for the cursor to be invalid if we're in the middle of
4281 * something (like changing files). If so, don't do anything for now.
4283 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4285 gui.bottom_sbar.value = -1;
4286 return;
4289 size = W_WIDTH(curwin);
4290 if (curwin->w_p_wrap)
4292 value = 0;
4293 #ifdef SCROLL_PAST_END
4294 max = 0;
4295 #else
4296 max = W_WIDTH(curwin) - 1;
4297 #endif
4299 else
4301 value = curwin->w_leftcol;
4303 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4304 * line numbers, topline and botline can be invalid when displaying is
4305 * postponed. */
4306 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4307 && curwin->w_topline <= curwin->w_cursor.lnum
4308 && curwin->w_botline > curwin->w_cursor.lnum
4309 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4311 linenr_T lnum;
4312 colnr_T n;
4314 /* Use maximum of all visible lines. Remember the lnum of the
4315 * longest line, clostest to the cursor line. Used when scrolling
4316 * below. */
4317 max = 0;
4318 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4320 n = scroll_line_len(lnum);
4321 if (n > (colnr_T)max)
4323 max = n;
4324 longest_lnum = lnum;
4326 else if (n == (colnr_T)max
4327 && abs((int)(lnum - curwin->w_cursor.lnum))
4328 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4329 longest_lnum = lnum;
4332 else
4333 /* Use cursor line only. */
4334 max = scroll_line_len(curwin->w_cursor.lnum);
4335 #ifdef FEAT_VIRTUALEDIT
4336 if (virtual_active())
4338 /* May move the cursor even further to the right. */
4339 if (curwin->w_virtcol >= (colnr_T)max)
4340 max = curwin->w_virtcol;
4342 #endif
4344 #ifndef SCROLL_PAST_END
4345 max += W_WIDTH(curwin) - 1;
4346 #endif
4347 /* The line number isn't scrolled, thus there is less space when
4348 * 'number' is set (also for 'foldcolumn'). */
4349 size -= curwin_col_off();
4350 #ifndef SCROLL_PAST_END
4351 max -= curwin_col_off();
4352 #endif
4355 #ifndef SCROLL_PAST_END
4356 if (value > max - size + 1)
4357 value = max - size + 1; /* limit the value to allowable range */
4358 #endif
4360 #ifdef FEAT_RIGHTLEFT
4361 if (curwin->w_p_rl)
4363 value = max + 1 - size - value;
4364 if (value < 0)
4366 size += value;
4367 value = 0;
4370 #endif
4371 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4372 && max == gui.bottom_sbar.max)
4373 return;
4375 gui.bottom_sbar.value = value;
4376 gui.bottom_sbar.size = size;
4377 gui.bottom_sbar.max = max;
4378 gui.prev_wrap = curwin->w_p_wrap;
4380 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4384 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4387 gui_do_horiz_scroll()
4389 /* no wrapping, no scrolling */
4390 if (curwin->w_p_wrap)
4391 return FALSE;
4393 if (curwin->w_leftcol == scrollbar_value)
4394 return FALSE;
4396 curwin->w_leftcol = (colnr_T)scrollbar_value;
4398 /* When the line of the cursor is too short, move the cursor to the
4399 * longest visible line. Do a sanity check on "longest_lnum", just in
4400 * case. */
4401 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4402 && longest_lnum >= curwin->w_topline
4403 && longest_lnum < curwin->w_botline
4404 && !virtual_active())
4406 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4408 curwin->w_cursor.lnum = longest_lnum;
4409 curwin->w_cursor.col = 0;
4413 return leftcol_changed();
4417 * Check that none of the colors are the same as the background color
4419 void
4420 gui_check_colors()
4422 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4424 gui_set_bg_color((char_u *)"White");
4425 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4426 gui_set_fg_color((char_u *)"Black");
4430 static void
4431 gui_set_fg_color(name)
4432 char_u *name;
4434 gui.norm_pixel = gui_get_color(name);
4435 hl_set_fg_color_name(vim_strsave(name));
4438 static void
4439 gui_set_bg_color(name)
4440 char_u *name;
4442 gui.back_pixel = gui_get_color(name);
4443 hl_set_bg_color_name(vim_strsave(name));
4447 * Allocate a color by name.
4448 * Returns INVALCOLOR and gives an error message when failed.
4450 guicolor_T
4451 gui_get_color(name)
4452 char_u *name;
4454 guicolor_T t;
4456 if (*name == NUL)
4457 return INVALCOLOR;
4458 t = gui_mch_get_color(name);
4460 if (t == INVALCOLOR
4461 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4462 && gui.in_use
4463 #endif
4465 EMSG2(_("E254: Cannot allocate color %s"), name);
4466 return t;
4470 * Return the grey value of a color (range 0-255).
4473 gui_get_lightness(pixel)
4474 guicolor_T pixel;
4476 long_u rgb = gui_mch_get_rgb(pixel);
4478 return (int)( (((rgb >> 16) & 0xff) * 299)
4479 + (((rgb >> 8) & 0xff) * 587)
4480 + ((rgb & 0xff) * 114)) / 1000;
4483 #if defined(FEAT_GUI_X11) || defined(PROTO)
4484 void
4485 gui_new_scrollbar_colors()
4487 win_T *wp;
4489 /* Nothing to do if GUI hasn't started yet. */
4490 if (!gui.in_use)
4491 return;
4493 FOR_ALL_WINDOWS(wp)
4495 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4496 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4498 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4500 #endif
4503 * Call this when focus has changed.
4505 void
4506 gui_focus_change(in_focus)
4507 int in_focus;
4510 * Skip this code to avoid drawing the cursor when debugging and switching
4511 * between the debugger window and gvim.
4513 #if 1
4514 gui.in_focus = in_focus;
4515 out_flush(); /* make sure output has been written */
4516 gui_update_cursor(TRUE, FALSE);
4518 # ifdef FEAT_XIM
4519 xim_set_focus(in_focus);
4520 # endif
4522 /* Put events in the input queue only when allowed.
4523 * ui_focus_change() isn't called directly, because it invokes
4524 * autocommands and that must not happen asynchronously. */
4525 if (!hold_gui_events)
4527 char_u bytes[3];
4529 bytes[0] = CSI;
4530 bytes[1] = KS_EXTRA;
4531 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4532 add_to_input_buf(bytes, 3);
4534 #endif
4538 * Called when the mouse moved (but not when dragging).
4540 void
4541 gui_mouse_moved(x, y)
4542 int x;
4543 int y;
4545 win_T *wp;
4546 char_u st[8];
4548 /* Ignore this while still starting up. */
4549 if (!gui.in_use || gui.starting)
4550 return;
4552 #ifdef FEAT_MOUSESHAPE
4553 /* Get window pointer, and update mouse shape as well. */
4554 wp = xy2win(x, y);
4555 #endif
4557 /* Only handle this when 'mousefocus' set and ... */
4558 if (p_mousef
4559 && !hold_gui_events /* not holding events */
4560 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4561 && State != HITRETURN /* but not hit-return prompt */
4562 && msg_scrolled == 0 /* no scrolled message */
4563 && !need_mouse_correct /* not moving the pointer */
4564 && gui.in_focus) /* gvim in focus */
4566 /* Don't move the mouse when it's left or right of the Vim window */
4567 if (x < 0 || x > Columns * gui.char_width)
4568 return;
4569 #ifndef FEAT_MOUSESHAPE
4570 wp = xy2win(x, y);
4571 #endif
4572 if (wp == curwin || wp == NULL)
4573 return; /* still in the same old window, or none at all */
4575 #ifdef FEAT_WINDOWS
4576 /* Ignore position in the tab pages line. */
4577 if (Y_2_ROW(y) < tabline_height())
4578 return;
4579 #endif
4582 * format a mouse click on status line input
4583 * ala gui_send_mouse_event(0, x, y, 0, 0);
4584 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4585 * generate a K_LEFTMOUSE_NM key code.
4587 if (finish_op)
4589 /* abort the current operator first */
4590 st[0] = ESC;
4591 add_to_input_buf(st, 1);
4593 st[0] = CSI;
4594 st[1] = KS_MOUSE;
4595 st[2] = KE_FILLER;
4596 st[3] = (char_u)MOUSE_LEFT;
4597 fill_mouse_coord(st + 4,
4598 #ifdef FEAT_VERTSPLIT
4599 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4600 #else
4602 #endif
4603 wp->w_height + W_WINROW(wp));
4605 add_to_input_buf(st, 8);
4606 st[3] = (char_u)MOUSE_RELEASE;
4607 add_to_input_buf(st, 8);
4608 #ifdef FEAT_GUI_GTK
4609 /* Need to wake up the main loop */
4610 if (gtk_main_level() > 0)
4611 gtk_main_quit();
4612 #endif
4617 * Called when mouse should be moved to window with focus.
4619 void
4620 gui_mouse_correct()
4622 int x, y;
4623 win_T *wp = NULL;
4625 need_mouse_correct = FALSE;
4627 if (!(gui.in_use && p_mousef))
4628 return;
4630 gui_mch_getmouse(&x, &y);
4631 /* Don't move the mouse when it's left or right of the Vim window */
4632 if (x < 0 || x > Columns * gui.char_width)
4633 return;
4634 if (y >= 0
4635 # ifdef FEAT_WINDOWS
4636 && Y_2_ROW(y) >= tabline_height()
4637 # endif
4639 wp = xy2win(x, y);
4640 if (wp != curwin && wp != NULL) /* If in other than current window */
4642 validate_cline_row();
4643 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4644 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4645 + (gui.char_height) / 2);
4650 * Find window where the mouse pointer "y" coordinate is in.
4652 /*ARGSUSED*/
4653 static win_T *
4654 xy2win(x, y)
4655 int x;
4656 int y;
4658 #ifdef FEAT_WINDOWS
4659 int row;
4660 int col;
4661 win_T *wp;
4663 row = Y_2_ROW(y);
4664 col = X_2_COL(x);
4665 if (row < 0 || col < 0) /* before first window */
4666 return NULL;
4667 wp = mouse_find_win(&row, &col);
4668 # ifdef FEAT_MOUSESHAPE
4669 if (State == HITRETURN || State == ASKMORE)
4671 if (Y_2_ROW(y) >= msg_row)
4672 update_mouseshape(SHAPE_IDX_MOREL);
4673 else
4674 update_mouseshape(SHAPE_IDX_MORE);
4676 else if (row > wp->w_height) /* below status line */
4677 update_mouseshape(SHAPE_IDX_CLINE);
4678 # ifdef FEAT_VERTSPLIT
4679 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4680 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4681 update_mouseshape(SHAPE_IDX_VSEP);
4682 # endif
4683 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4684 && row == wp->w_height && msg_scrolled == 0)
4685 update_mouseshape(SHAPE_IDX_STATUS);
4686 else
4687 update_mouseshape(-2);
4688 # endif
4689 return wp;
4690 #else
4691 return firstwin;
4692 #endif
4696 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4697 * File names may be given to redefine the args list.
4699 void
4700 ex_gui(eap)
4701 exarg_T *eap;
4703 char_u *arg = eap->arg;
4706 * Check for "-f" argument: foreground, don't fork.
4707 * Also don't fork when started with "gvim -f".
4708 * Do fork when using "gui -b".
4710 if (arg[0] == '-'
4711 && (arg[1] == 'f' || arg[1] == 'b')
4712 && (arg[2] == NUL || vim_iswhite(arg[2])))
4714 gui.dofork = (arg[1] == 'b');
4715 eap->arg = skipwhite(eap->arg + 2);
4717 if (!gui.in_use)
4719 /* Clear the command. Needed for when forking+exiting, to avoid part
4720 * of the argument ending up after the shell prompt. */
4721 msg_clr_eos_force();
4722 gui_start();
4724 if (!ends_excmd(*eap->arg))
4725 ex_next(eap);
4728 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4729 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4731 * This is shared between Athena, Motif and GTK.
4733 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4736 * Callback function for do_in_runtimepath().
4738 static void
4739 gfp_setname(fname, cookie)
4740 char_u *fname;
4741 void *cookie;
4743 char_u *gfp_buffer = cookie;
4745 if (STRLEN(fname) >= MAXPATHL)
4746 *gfp_buffer = NUL;
4747 else
4748 STRCPY(gfp_buffer, fname);
4752 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4753 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4756 gui_find_bitmap(name, buffer, ext)
4757 char_u *name;
4758 char_u *buffer;
4759 char *ext;
4761 if (STRLEN(name) > MAXPATHL - 14)
4762 return FAIL;
4763 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4764 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4765 || *buffer == NUL)
4766 return FAIL;
4767 return OK;
4770 # if !defined(HAVE_GTK2) || defined(PROTO)
4772 * Given the name of the "icon=" argument, try finding the bitmap file for the
4773 * icon. If it is an absolute path name, use it as it is. Otherwise append
4774 * "ext" and search for it in 'runtimepath'.
4775 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4776 * contains "name".
4778 void
4779 gui_find_iconfile(name, buffer, ext)
4780 char_u *name;
4781 char_u *buffer;
4782 char *ext;
4784 char_u buf[MAXPATHL + 1];
4786 expand_env(name, buffer, MAXPATHL);
4787 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4788 STRCPY(buffer, buf);
4790 # endif
4791 #endif
4793 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4794 void
4795 display_errors()
4797 char_u *p;
4799 if (isatty(2))
4800 fflush(stderr);
4801 else if (error_ga.ga_data != NULL)
4803 /* avoid putting up a message box with blanks only */
4804 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4805 if (!isspace(*p))
4807 /* Truncate a very long message, it will go off-screen. */
4808 if (STRLEN(p) > 2000)
4809 STRCPY(p + 2000 - 14, "...(truncated)");
4810 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4811 p, (char_u *)_("&Ok"), 1, NULL);
4812 break;
4814 ga_clear(&error_ga);
4817 #endif
4819 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4821 * Return TRUE if still starting up and there is no place to enter text.
4822 * For GTK and X11 we check if stderr is not a tty, which means we were
4823 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4824 * allow typing on stdin.
4827 no_console_input()
4829 return ((!gui.in_use || gui.starting)
4830 # ifndef NO_CONSOLE
4831 && !isatty(0) && !isatty(2)
4832 # endif
4835 #endif
4837 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4838 || defined(NEED_GUI_UPDATE_SCREEN) \
4839 || defined(PROTO)
4841 * Update the current window and the screen.
4843 void
4844 gui_update_screen()
4846 update_topline();
4847 validate_cursor();
4848 update_screen(0); /* may need to update the screen */
4849 setcursor();
4850 out_flush(); /* make sure output has been written */
4851 gui_update_cursor(TRUE, FALSE);
4852 gui_mch_flush();
4854 #endif
4856 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4857 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4860 * Get the text to use in a find/replace dialog. Uses the last search pattern
4861 * if the argument is empty.
4862 * Returns an allocated string.
4864 char_u *
4865 get_find_dialog_text(arg, wwordp, mcasep)
4866 char_u *arg;
4867 int *wwordp; /* return: TRUE if \< \> found */
4868 int *mcasep; /* return: TRUE if \C found */
4870 char_u *text;
4872 if (*arg == NUL)
4873 text = last_search_pat();
4874 else
4875 text = arg;
4876 if (text != NULL)
4878 text = vim_strsave(text);
4879 if (text != NULL)
4881 int len = (int)STRLEN(text);
4882 int i;
4884 /* Remove "\V" */
4885 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4887 mch_memmove(text, text + 2, (size_t)(len - 1));
4888 len -= 2;
4891 /* Recognize "\c" and "\C" and remove. */
4892 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4894 *mcasep = (text[1] == 'C');
4895 mch_memmove(text, text + 2, (size_t)(len - 1));
4896 len -= 2;
4899 /* Recognize "\<text\>" and remove. */
4900 if (len >= 4
4901 && STRNCMP(text, "\\<", 2) == 0
4902 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4904 *wwordp = TRUE;
4905 mch_memmove(text, text + 2, (size_t)(len - 4));
4906 text[len - 4] = NUL;
4909 /* Recognize "\/" or "\?" and remove. */
4910 for (i = 0; i + 1 < len; ++i)
4911 if (text[i] == '\\' && (text[i + 1] == '/'
4912 || text[i + 1] == '?'))
4914 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4915 --len;
4919 return text;
4923 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4925 static void
4926 concat_esc(gap, text, what)
4927 garray_T *gap;
4928 char_u *text;
4929 int what;
4931 while (*text != NUL)
4933 #ifdef FEAT_MBYTE
4934 int l = (*mb_ptr2len)(text);
4936 if (l > 1)
4938 while (--l >= 0)
4939 ga_append(gap, *text++);
4940 continue;
4942 #endif
4943 if (*text == what)
4944 ga_append(gap, '\\');
4945 ga_append(gap, *text);
4946 ++text;
4951 * Handle the press of a button in the find-replace dialog.
4952 * Return TRUE when something was added to the input buffer.
4955 gui_do_findrepl(flags, find_text, repl_text, down)
4956 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4957 char_u *find_text;
4958 char_u *repl_text;
4959 int down; /* Search downwards. */
4961 garray_T ga;
4962 int i;
4963 int type = (flags & FRD_TYPE_MASK);
4964 char_u *p;
4965 regmatch_T regmatch;
4966 int save_did_emsg = did_emsg;
4968 ga_init2(&ga, 1, 100);
4969 if (type == FRD_REPLACEALL)
4970 ga_concat(&ga, (char_u *)"%s/");
4972 ga_concat(&ga, (char_u *)"\\V");
4973 if (flags & FRD_MATCH_CASE)
4974 ga_concat(&ga, (char_u *)"\\C");
4975 else
4976 ga_concat(&ga, (char_u *)"\\c");
4977 if (flags & FRD_WHOLE_WORD)
4978 ga_concat(&ga, (char_u *)"\\<");
4979 if (type == FRD_REPLACEALL || down)
4980 concat_esc(&ga, find_text, '/'); /* escape slashes */
4981 else
4982 concat_esc(&ga, find_text, '?'); /* escape '?' */
4983 if (flags & FRD_WHOLE_WORD)
4984 ga_concat(&ga, (char_u *)"\\>");
4986 if (type == FRD_REPLACEALL)
4988 ga_concat(&ga, (char_u *)"/");
4989 /* escape / and \ */
4990 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
4991 if (p != NULL)
4992 ga_concat(&ga, p);
4993 vim_free(p);
4994 ga_concat(&ga, (char_u *)"/g");
4996 ga_append(&ga, NUL);
4998 if (type == FRD_REPLACE)
5000 /* Do the replacement when the text at the cursor matches. Thus no
5001 * replacement is done if the cursor was moved! */
5002 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5003 regmatch.rm_ic = 0;
5004 if (regmatch.regprog != NULL)
5006 p = ml_get_cursor();
5007 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5008 && regmatch.startp[0] == p)
5010 /* Clear the command line to remove any old "No match"
5011 * error. */
5012 msg_end_prompt();
5014 if (u_save_cursor() == OK)
5016 /* A button was pressed thus undo should be synced. */
5017 u_sync(FALSE);
5019 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5020 FALSE, FALSE);
5021 ins_str(repl_text);
5024 else
5025 MSG(_("No match at cursor, finding next"));
5026 vim_free(regmatch.regprog);
5030 if (type == FRD_REPLACEALL)
5032 /* A button was pressed, thus undo should be synced. */
5033 u_sync(FALSE);
5034 do_cmdline_cmd(ga.ga_data);
5036 else
5038 /* Search for the next match. */
5039 i = msg_scroll;
5040 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5041 SEARCH_MSG + SEARCH_MARK);
5042 msg_scroll = i; /* don't let an error message set msg_scroll */
5045 /* Don't want to pass did_emsg to other code, it may cause disabling
5046 * syntax HL if we were busy redrawing. */
5047 did_emsg = save_did_emsg;
5049 if (State & (NORMAL | INSERT))
5051 gui_update_screen(); /* update the screen */
5052 msg_didout = 0; /* overwrite any message */
5053 need_wait_return = FALSE; /* don't wait for return */
5056 vim_free(ga.ga_data);
5057 return (ga.ga_len > 0);
5060 #endif
5062 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5063 || defined(FEAT_GUI_MSWIN) \
5064 || defined(FEAT_GUI_MAC) \
5065 || defined(PROTO)
5067 #ifdef FEAT_WINDOWS
5068 static void gui_wingoto_xy __ARGS((int x, int y));
5071 * Jump to the window at specified point (x, y).
5073 static void
5074 gui_wingoto_xy(x, y)
5075 int x;
5076 int y;
5078 int row = Y_2_ROW(y);
5079 int col = X_2_COL(x);
5080 win_T *wp;
5082 if (row >= 0 && col >= 0)
5084 wp = mouse_find_win(&row, &col);
5085 if (wp != NULL && wp != curwin)
5086 win_goto(wp);
5089 #endif
5092 * Process file drop. Mouse cursor position, key modifiers, name of files
5093 * and count of files are given. Argument "fnames[count]" has full pathnames
5094 * of dropped files, they will be freed in this function, and caller can't use
5095 * fnames after call this function.
5097 /*ARGSUSED*/
5098 void
5099 gui_handle_drop(x, y, modifiers, fnames, count)
5100 int x;
5101 int y;
5102 int_u modifiers;
5103 char_u **fnames;
5104 int count;
5106 int i;
5107 char_u *p;
5110 * When the cursor is at the command line, add the file names to the
5111 * command line, don't edit the files.
5113 if (State & CMDLINE)
5115 shorten_filenames(fnames, count);
5116 for (i = 0; i < count; ++i)
5118 if (fnames[i] != NULL)
5120 if (i > 0)
5121 add_to_input_buf((char_u*)" ", 1);
5123 /* We don't know what command is used thus we can't be sure
5124 * about which characters need to be escaped. Only escape the
5125 * most common ones. */
5126 # ifdef BACKSLASH_IN_FILENAME
5127 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5128 # else
5129 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5130 # endif
5131 if (p != NULL)
5132 add_to_input_buf_csi(p, (int)STRLEN(p));
5133 vim_free(p);
5134 vim_free(fnames[i]);
5137 vim_free(fnames);
5139 else
5141 /* Go to the window under mouse cursor, then shorten given "fnames" by
5142 * current window, because a window can have local current dir. */
5143 # ifdef FEAT_WINDOWS
5144 gui_wingoto_xy(x, y);
5145 # endif
5146 shorten_filenames(fnames, count);
5148 /* If Shift held down, remember the first item. */
5149 if ((modifiers & MOUSE_SHIFT) != 0)
5150 p = vim_strsave(fnames[0]);
5151 else
5152 p = NULL;
5154 /* Handle the drop, :edit or :split to get to the file. This also
5155 * frees fnames[]. Skip this if there is only one item it's a
5156 * directory and Shift is held down. */
5157 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5158 && mch_isdir(fnames[0]))
5160 vim_free(fnames[0]);
5161 vim_free(fnames);
5163 else
5164 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5166 /* If Shift held down, change to first file's directory. If the first
5167 * item is a directory, change to that directory (and let the explorer
5168 * plugin show the contents). */
5169 if (p != NULL)
5171 if (mch_isdir(p))
5173 if (mch_chdir((char *)p) == 0)
5174 shorten_fnames(TRUE);
5176 else if (vim_chdirfile(p) == OK)
5177 shorten_fnames(TRUE);
5178 vim_free(p);
5181 /* Update the screen display */
5182 update_screen(NOT_VALID);
5183 # ifdef FEAT_MENU
5184 gui_update_menus(0);
5185 # endif
5186 setcursor();
5187 out_flush();
5188 gui_update_cursor(FALSE, FALSE);
5189 gui_mch_flush();
5192 #endif