Vim 7.2b ready for testing.
[MacVim/KaoriYa.git] / src / gui.c
blob8568d33ab402b661a3862cabbf3bdb5599b5a63b
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 (
962 # ifdef HAVE_GTK2
963 preedit_get_status()
964 # else
965 im_get_status()
966 # endif
969 iid = syn_name2id((char_u *)"CursorIM");
970 if (iid > 0)
972 syn_id2colors(iid, &fg, &bg);
973 if (bg != INVALCOLOR)
974 cbg = bg;
975 if (fg != INVALCOLOR)
976 cfg = fg;
980 #endif
984 * Get the attributes for the character under the cursor.
985 * When no cursor color was given, use the character color.
987 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
988 if (attr > HL_ALL)
989 aep = syn_gui_attr2entry(attr);
990 if (aep != NULL)
992 attr = aep->ae_attr;
993 if (cfg == INVALCOLOR)
994 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
995 : aep->ae_u.gui.fg_color);
996 if (cbg == INVALCOLOR)
997 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
998 : aep->ae_u.gui.bg_color);
1000 if (cfg == INVALCOLOR)
1001 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1002 if (cbg == INVALCOLOR)
1003 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1005 #ifdef FEAT_XIM
1006 if (aep != NULL)
1008 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1009 : aep->ae_u.gui.bg_color);
1010 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1011 : aep->ae_u.gui.fg_color);
1012 if (xim_bg_color == INVALCOLOR)
1013 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1014 : gui.back_pixel;
1015 if (xim_fg_color == INVALCOLOR)
1016 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1017 : gui.norm_pixel;
1019 else
1021 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1022 : gui.back_pixel;
1023 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1024 : gui.norm_pixel;
1026 #endif
1028 attr &= ~HL_INVERSE;
1029 if (cattr & HL_INVERSE)
1031 cc = cbg;
1032 cbg = cfg;
1033 cfg = cc;
1035 cattr &= ~HL_INVERSE;
1038 * When we don't have window focus, draw a hollow cursor.
1040 if (!gui.in_focus)
1042 gui_mch_draw_hollow_cursor(cbg);
1043 return;
1046 old_hl_mask = gui.highlight_mask;
1047 if (shape_table[idx].shape == SHAPE_BLOCK
1048 #ifdef FEAT_HANGULIN
1049 || composing_hangul
1050 #endif
1054 * Draw the text character with the cursor colors. Use the
1055 * character attributes plus the cursor attributes.
1057 gui.highlight_mask = (cattr | attr);
1058 #ifdef FEAT_HANGULIN
1059 if (composing_hangul)
1060 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1061 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1062 else
1063 #endif
1064 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1065 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1067 else
1069 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1070 int col_off = FALSE;
1071 #endif
1073 * First draw the partial cursor, then overwrite with the text
1074 * character, using a transparent background.
1076 if (shape_table[idx].shape == SHAPE_VER)
1078 cur_height = gui.char_height;
1079 cur_width = (gui.char_width * shape_table[idx].percentage
1080 + 99) / 100;
1082 else
1084 cur_height = (gui.char_height * shape_table[idx].percentage
1085 + 99) / 100;
1086 cur_width = gui.char_width;
1088 #ifdef FEAT_MBYTE
1089 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1090 LineOffset[gui.row] + screen_Columns) > 1)
1092 /* Double wide character. */
1093 if (shape_table[idx].shape != SHAPE_VER)
1094 cur_width += gui.char_width;
1095 # ifdef FEAT_RIGHTLEFT
1096 if (CURSOR_BAR_RIGHT)
1098 /* gui.col points to the left halve of the character but
1099 * the vertical line needs to be on the right halve.
1100 * A double-wide horizontal line is also drawn from the
1101 * right halve in gui_mch_draw_part_cursor(). */
1102 col_off = TRUE;
1103 ++gui.col;
1105 # endif
1107 #endif
1108 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1109 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1110 if (col_off)
1111 --gui.col;
1112 #endif
1114 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1115 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1116 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1117 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1118 (guicolor_T)0, (guicolor_T)0, 0);
1119 #endif
1121 gui.highlight_mask = old_hl_mask;
1125 #if defined(FEAT_MENU) || defined(PROTO)
1126 void
1127 gui_position_menu()
1129 # if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
1130 if (gui.menu_is_active && gui.in_use)
1131 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1132 # endif
1134 #endif
1137 * Position the various GUI components (text area, menu). The vertical
1138 * scrollbars are NOT handled here. See gui_update_scrollbars().
1140 /*ARGSUSED*/
1141 static void
1142 gui_position_components(total_width)
1143 int total_width;
1145 int text_area_x;
1146 int text_area_y;
1147 int text_area_width;
1148 int text_area_height;
1150 /* avoid that moving components around generates events */
1151 ++hold_gui_events;
1153 text_area_x = 0;
1154 if (gui.which_scrollbars[SBAR_LEFT])
1155 text_area_x += gui.scrollbar_width;
1157 text_area_y = 0;
1158 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1159 gui.menu_width = total_width;
1160 if (gui.menu_is_active)
1161 text_area_y += gui.menu_height;
1162 #endif
1163 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1164 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1165 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1166 #endif
1168 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1169 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1170 if (gui_has_tabline())
1171 text_area_y += gui.tabline_height;
1172 #endif
1174 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1175 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1177 # ifdef FEAT_GUI_ATHENA
1178 gui_mch_set_toolbar_pos(0, text_area_y,
1179 gui.menu_width, gui.toolbar_height);
1180 # endif
1181 text_area_y += gui.toolbar_height;
1183 #endif
1185 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1186 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1188 gui_mch_set_text_area_pos(text_area_x,
1189 text_area_y,
1190 text_area_width,
1191 text_area_height
1192 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1193 + xim_get_status_area_height()
1194 #endif
1196 #ifdef FEAT_MENU
1197 gui_position_menu();
1198 #endif
1199 if (gui.which_scrollbars[SBAR_BOTTOM])
1200 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1201 text_area_x,
1202 text_area_y + text_area_height,
1203 text_area_width,
1204 gui.scrollbar_height);
1205 gui.left_sbar_x = 0;
1206 gui.right_sbar_x = text_area_x + text_area_width;
1208 --hold_gui_events;
1212 * Get the width of the widgets and decorations to the side of the text area.
1215 gui_get_base_width()
1217 int base_width;
1219 base_width = 2 * gui.border_offset;
1220 if (gui.which_scrollbars[SBAR_LEFT])
1221 base_width += gui.scrollbar_width;
1222 if (gui.which_scrollbars[SBAR_RIGHT])
1223 base_width += gui.scrollbar_width;
1224 return base_width;
1228 * Get the height of the widgets and decorations above and below the text area.
1231 gui_get_base_height()
1233 int base_height;
1235 base_height = 2 * gui.border_offset;
1236 if (gui.which_scrollbars[SBAR_BOTTOM])
1237 base_height += gui.scrollbar_height;
1238 #ifdef FEAT_GUI_GTK
1239 /* We can't take the sizes properly into account until anything is
1240 * realized. Therefore we recalculate all the values here just before
1241 * setting the size. (--mdcki) */
1242 #else
1243 # ifdef FEAT_MENU
1244 if (gui.menu_is_active)
1245 base_height += gui.menu_height;
1246 # endif
1247 # ifdef FEAT_TOOLBAR
1248 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1249 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1250 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1251 # else
1252 base_height += gui.toolbar_height;
1253 # endif
1254 # endif
1255 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1256 || defined(FEAT_GUI_MOTIF))
1257 if (gui_has_tabline())
1258 base_height += gui.tabline_height;
1259 # endif
1260 # ifdef FEAT_FOOTER
1261 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1262 base_height += gui.footer_height;
1263 # endif
1264 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1265 base_height += gui_mch_text_area_extra_height();
1266 # endif
1267 #endif
1268 return base_height;
1272 * Should be called after the GUI shell has been resized. Its arguments are
1273 * the new width and height of the shell in pixels.
1275 void
1276 gui_resize_shell(pixel_width, pixel_height)
1277 int pixel_width;
1278 int pixel_height;
1280 static int busy = FALSE;
1282 if (!gui.shell_created) /* ignore when still initializing */
1283 return;
1286 * Can't resize the screen while it is being redrawn. Remember the new
1287 * size and handle it later.
1289 if (updating_screen || busy)
1291 new_pixel_width = pixel_width;
1292 new_pixel_height = pixel_height;
1293 return;
1296 again:
1297 busy = TRUE;
1299 /* Flush pending output before redrawing */
1300 out_flush();
1302 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1303 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
1305 gui_position_components(pixel_width);
1307 gui_reset_scroll_region();
1309 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1310 * at the last line here (why does it have to be one row too low?).
1312 if (State == ASKMORE || State == CONFIRM)
1313 gui.row = gui.num_rows;
1315 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1316 * the safe side. */
1317 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1318 || gui.num_rows != Rows || gui.num_cols != Columns)
1319 shell_resized();
1321 gui_update_scrollbars(TRUE);
1322 gui_update_cursor(FALSE, TRUE);
1323 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1324 xim_set_status_area();
1325 #endif
1327 busy = FALSE;
1330 * We could have been called again while redrawing the screen.
1331 * Need to do it all again with the latest size then.
1333 if (new_pixel_height)
1335 pixel_width = new_pixel_width;
1336 pixel_height = new_pixel_height;
1337 new_pixel_width = 0;
1338 new_pixel_height = 0;
1339 goto again;
1344 * Check if gui_resize_shell() must be called.
1346 void
1347 gui_may_resize_shell()
1349 int h, w;
1351 if (new_pixel_height)
1353 /* careful: gui_resize_shell() may postpone the resize again if we
1354 * were called indirectly by it */
1355 w = new_pixel_width;
1356 h = new_pixel_height;
1357 new_pixel_width = 0;
1358 new_pixel_height = 0;
1359 gui_resize_shell(w, h);
1364 gui_get_shellsize()
1366 Rows = gui.num_rows;
1367 Columns = gui.num_cols;
1368 return OK;
1372 * Set the size of the Vim shell according to Rows and Columns.
1373 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1374 * on the screen.
1376 /*ARGSUSED*/
1377 void
1378 gui_set_shellsize(mustset, fit_to_display, direction)
1379 int mustset; /* set by the user */
1380 int fit_to_display;
1381 int direction; /* RESIZE_HOR, RESIZE_VER */
1383 int base_width;
1384 int base_height;
1385 int width;
1386 int height;
1387 int min_width;
1388 int min_height;
1389 int screen_w;
1390 int screen_h;
1392 if (!gui.shell_created)
1393 return;
1395 #ifdef MSWIN
1396 /* If not setting to a user specified size and maximized, calculate the
1397 * number of characters that fit in the maximized window. */
1398 if (!mustset && gui_mch_maximized())
1400 gui_mch_newfont();
1401 return;
1403 #endif
1405 base_width = gui_get_base_width();
1406 base_height = gui_get_base_height();
1407 #ifdef USE_SUN_WORKSHOP
1408 if (!mustset && usingSunWorkShop
1409 && workshop_get_width_height(&width, &height))
1411 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1412 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1414 else
1415 #endif
1417 width = Columns * gui.char_width + base_width;
1418 height = Rows * gui.char_height + base_height;
1421 if (fit_to_display)
1423 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1424 if ((direction & RESIZE_HOR) && width > screen_w)
1426 Columns = (screen_w - base_width) / gui.char_width;
1427 if (Columns < MIN_COLUMNS)
1428 Columns = MIN_COLUMNS;
1429 width = Columns * gui.char_width + base_width;
1431 if ((direction & RESIZE_VERT) && height > screen_h)
1433 Rows = (screen_h - base_height) / gui.char_height;
1434 check_shellsize();
1435 height = Rows * gui.char_height + base_height;
1438 gui.num_cols = Columns;
1439 gui.num_rows = Rows;
1441 min_width = base_width + MIN_COLUMNS * gui.char_width;
1442 min_height = base_height + MIN_LINES * gui.char_height;
1443 # ifdef FEAT_WINDOWS
1444 min_height += tabline_height() * gui.char_height;
1445 # endif
1447 gui_mch_set_shellsize(width, height, min_width, min_height,
1448 base_width, base_height, direction);
1449 if (fit_to_display)
1451 int x, y;
1453 /* Some window managers put the Vim window left of/above the screen. */
1454 gui_mch_update();
1455 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1456 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1459 gui_position_components(width);
1460 gui_update_scrollbars(TRUE);
1461 gui_reset_scroll_region();
1465 * Called when Rows and/or Columns has changed.
1467 void
1468 gui_new_shellsize()
1470 gui_reset_scroll_region();
1474 * Make scroll region cover whole screen.
1476 void
1477 gui_reset_scroll_region()
1479 gui.scroll_region_top = 0;
1480 gui.scroll_region_bot = gui.num_rows - 1;
1481 gui.scroll_region_left = 0;
1482 gui.scroll_region_right = gui.num_cols - 1;
1485 void
1486 gui_start_highlight(mask)
1487 int mask;
1489 if (mask > HL_ALL) /* highlight code */
1490 gui.highlight_mask = mask;
1491 else /* mask */
1492 gui.highlight_mask |= mask;
1495 void
1496 gui_stop_highlight(mask)
1497 int mask;
1499 if (mask > HL_ALL) /* highlight code */
1500 gui.highlight_mask = HL_NORMAL;
1501 else /* mask */
1502 gui.highlight_mask &= ~mask;
1506 * Clear a rectangular region of the screen from text pos (row1, col1) to
1507 * (row2, col2) inclusive.
1509 void
1510 gui_clear_block(row1, col1, row2, col2)
1511 int row1;
1512 int col1;
1513 int row2;
1514 int col2;
1516 /* Clear the selection if we are about to write over it */
1517 clip_may_clear_selection(row1, row2);
1519 gui_mch_clear_block(row1, col1, row2, col2);
1521 /* Invalidate cursor if it was in this block */
1522 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1523 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1524 gui.cursor_is_valid = FALSE;
1528 * Write code to update the cursor later. This avoids the need to flush the
1529 * output buffer before calling gui_update_cursor().
1531 void
1532 gui_update_cursor_later()
1534 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1537 void
1538 gui_write(s, len)
1539 char_u *s;
1540 int len;
1542 char_u *p;
1543 int arg1 = 0, arg2 = 0;
1544 /* this doesn't make sense, disabled until someone can explain why it
1545 * would be needed */
1546 #if 0 && (defined(RISCOS) || defined(WIN16))
1547 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1548 our redraw speed will suffer */
1549 #else
1550 int force_cursor = FALSE; /* force cursor update */
1551 #endif
1552 int force_scrollbar = FALSE;
1553 static win_T *old_curwin = NULL;
1555 /* #define DEBUG_GUI_WRITE */
1556 #ifdef DEBUG_GUI_WRITE
1558 int i;
1559 char_u *str;
1561 printf("gui_write(%d):\n ", len);
1562 for (i = 0; i < len; i++)
1563 if (s[i] == ESC)
1565 if (i != 0)
1566 printf("\n ");
1567 printf("<ESC>");
1569 else
1571 str = transchar_byte(s[i]);
1572 if (str[0] && str[1])
1573 printf("<%s>", (char *)str);
1574 else
1575 printf("%s", (char *)str);
1577 printf("\n");
1579 #endif
1580 while (len)
1582 if (s[0] == ESC && s[1] == '|')
1584 p = s + 2;
1585 if (VIM_ISDIGIT(*p))
1587 arg1 = getdigits(&p);
1588 if (p > s + len)
1589 break;
1590 if (*p == ';')
1592 ++p;
1593 arg2 = getdigits(&p);
1594 if (p > s + len)
1595 break;
1598 switch (*p)
1600 case 'C': /* Clear screen */
1601 clip_scroll_selection(9999);
1602 gui_mch_clear_all();
1603 gui.cursor_is_valid = FALSE;
1604 force_scrollbar = TRUE;
1605 break;
1606 case 'M': /* Move cursor */
1607 gui_set_cursor(arg1, arg2);
1608 break;
1609 case 's': /* force cursor (shape) update */
1610 force_cursor = TRUE;
1611 break;
1612 case 'R': /* Set scroll region */
1613 if (arg1 < arg2)
1615 gui.scroll_region_top = arg1;
1616 gui.scroll_region_bot = arg2;
1618 else
1620 gui.scroll_region_top = arg2;
1621 gui.scroll_region_bot = arg1;
1623 break;
1624 #ifdef FEAT_VERTSPLIT
1625 case 'V': /* Set vertical scroll region */
1626 if (arg1 < arg2)
1628 gui.scroll_region_left = arg1;
1629 gui.scroll_region_right = arg2;
1631 else
1633 gui.scroll_region_left = arg2;
1634 gui.scroll_region_right = arg1;
1636 break;
1637 #endif
1638 case 'd': /* Delete line */
1639 gui_delete_lines(gui.row, 1);
1640 break;
1641 case 'D': /* Delete lines */
1642 gui_delete_lines(gui.row, arg1);
1643 break;
1644 case 'i': /* Insert line */
1645 gui_insert_lines(gui.row, 1);
1646 break;
1647 case 'I': /* Insert lines */
1648 gui_insert_lines(gui.row, arg1);
1649 break;
1650 case '$': /* Clear to end-of-line */
1651 gui_clear_block(gui.row, gui.col, gui.row,
1652 (int)Columns - 1);
1653 break;
1654 case 'h': /* Turn on highlighting */
1655 gui_start_highlight(arg1);
1656 break;
1657 case 'H': /* Turn off highlighting */
1658 gui_stop_highlight(arg1);
1659 break;
1660 case 'f': /* flash the window (visual bell) */
1661 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1662 break;
1663 default:
1664 p = s + 1; /* Skip the ESC */
1665 break;
1667 len -= (int)(++p - s);
1668 s = p;
1670 else if (
1671 #ifdef EBCDIC
1672 CtrlChar(s[0]) != 0 /* Ctrl character */
1673 #else
1674 s[0] < 0x20 /* Ctrl character */
1675 #endif
1676 #ifdef FEAT_SIGN_ICONS
1677 && s[0] != SIGN_BYTE
1678 # ifdef FEAT_NETBEANS_INTG
1679 && s[0] != MULTISIGN_BYTE
1680 # endif
1681 #endif
1684 if (s[0] == '\n') /* NL */
1686 gui.col = 0;
1687 if (gui.row < gui.scroll_region_bot)
1688 gui.row++;
1689 else
1690 gui_delete_lines(gui.scroll_region_top, 1);
1692 else if (s[0] == '\r') /* CR */
1694 gui.col = 0;
1696 else if (s[0] == '\b') /* Backspace */
1698 if (gui.col)
1699 --gui.col;
1701 else if (s[0] == Ctrl_L) /* cursor-right */
1703 ++gui.col;
1705 else if (s[0] == Ctrl_G) /* Beep */
1707 gui_mch_beep();
1709 /* Other Ctrl character: shouldn't happen! */
1711 --len; /* Skip this char */
1712 ++s;
1714 else
1716 p = s;
1717 while (len > 0 && (
1718 #ifdef EBCDIC
1719 CtrlChar(*p) == 0
1720 #else
1721 *p >= 0x20
1722 #endif
1723 #ifdef FEAT_SIGN_ICONS
1724 || *p == SIGN_BYTE
1725 # ifdef FEAT_NETBEANS_INTG
1726 || *p == MULTISIGN_BYTE
1727 # endif
1728 #endif
1731 len--;
1732 p++;
1734 gui_outstr(s, (int)(p - s));
1735 s = p;
1739 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1740 * set). */
1741 if (force_cursor)
1742 gui_update_cursor(TRUE, TRUE);
1744 /* When switching to another window the dragging must have stopped.
1745 * Required for GTK, dragged_sb isn't reset. */
1746 if (old_curwin != curwin)
1747 gui.dragged_sb = SBAR_NONE;
1749 /* Update the scrollbars after clearing the screen or when switched
1750 * to another window.
1751 * Update the horizontal scrollbar always, it's difficult to check all
1752 * situations where it might change. */
1753 if (force_scrollbar || old_curwin != curwin)
1754 gui_update_scrollbars(force_scrollbar);
1755 else
1756 gui_update_horiz_scrollbar(FALSE);
1757 old_curwin = curwin;
1760 * We need to make sure this is cleared since Athena doesn't tell us when
1761 * he is done dragging. Do the same for GTK.
1763 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1764 gui.dragged_sb = SBAR_NONE;
1765 #endif
1767 gui_mch_flush(); /* In case vim decides to take a nap */
1771 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1772 * produces wrong results. Call gui_dont_update_cursor() before that code and
1773 * gui_can_update_cursor() afterwards.
1775 void
1776 gui_dont_update_cursor()
1778 if (gui.in_use)
1780 /* Undraw the cursor now, we probably can't do it after the change. */
1781 gui_undraw_cursor();
1782 can_update_cursor = FALSE;
1786 void
1787 gui_can_update_cursor()
1789 can_update_cursor = TRUE;
1790 /* No need to update the cursor right now, there is always more output
1791 * after scrolling. */
1794 static void
1795 gui_outstr(s, len)
1796 char_u *s;
1797 int len;
1799 int this_len;
1800 #ifdef FEAT_MBYTE
1801 int cells;
1802 #endif
1804 if (len == 0)
1805 return;
1807 if (len < 0)
1808 len = (int)STRLEN(s);
1810 while (len > 0)
1812 #ifdef FEAT_MBYTE
1813 if (has_mbyte)
1815 /* Find out how many chars fit in the current line. */
1816 cells = 0;
1817 for (this_len = 0; this_len < len; )
1819 cells += (*mb_ptr2cells)(s + this_len);
1820 if (gui.col + cells > Columns)
1821 break;
1822 this_len += (*mb_ptr2len)(s + this_len);
1824 if (this_len > len)
1825 this_len = len; /* don't include following composing char */
1827 else
1828 #endif
1829 if (gui.col + len > Columns)
1830 this_len = Columns - gui.col;
1831 else
1832 this_len = len;
1834 (void)gui_outstr_nowrap(s, this_len,
1835 0, (guicolor_T)0, (guicolor_T)0, 0);
1836 s += this_len;
1837 len -= this_len;
1838 #ifdef FEAT_MBYTE
1839 /* fill up for a double-width char that doesn't fit. */
1840 if (len > 0 && gui.col < Columns)
1841 (void)gui_outstr_nowrap((char_u *)" ", 1,
1842 0, (guicolor_T)0, (guicolor_T)0, 0);
1843 #endif
1844 /* The cursor may wrap to the next line. */
1845 if (gui.col >= Columns)
1847 gui.col = 0;
1848 gui.row++;
1854 * Output one character (may be one or two display cells).
1855 * Caller must check for valid "off".
1856 * Returns FAIL or OK, just like gui_outstr_nowrap().
1858 static int
1859 gui_screenchar(off, flags, fg, bg, back)
1860 int off; /* Offset from start of screen */
1861 int flags;
1862 guicolor_T fg, bg; /* colors for cursor */
1863 int back; /* backup this many chars when using bold trick */
1865 #ifdef FEAT_MBYTE
1866 char_u buf[MB_MAXBYTES + 1];
1868 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1869 if (enc_utf8 && ScreenLines[off] == 0)
1870 return OK;
1872 if (enc_utf8 && ScreenLinesUC[off] != 0)
1873 /* Draw UTF-8 multi-byte character. */
1874 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1875 flags, fg, bg, back);
1877 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1879 buf[0] = ScreenLines[off];
1880 buf[1] = ScreenLines2[off];
1881 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1884 /* Draw non-multi-byte character or DBCS character. */
1885 return gui_outstr_nowrap(ScreenLines + off,
1886 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1887 flags, fg, bg, back);
1888 #else
1889 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1890 #endif
1893 #ifdef HAVE_GTK2
1895 * Output the string at the given screen position. This is used in place
1896 * of gui_screenchar() where possible because Pango needs as much context
1897 * as possible to work nicely. It's a lot faster as well.
1899 static int
1900 gui_screenstr(off, len, flags, fg, bg, back)
1901 int off; /* Offset from start of screen */
1902 int len; /* string length in screen cells */
1903 int flags;
1904 guicolor_T fg, bg; /* colors for cursor */
1905 int back; /* backup this many chars when using bold trick */
1907 char_u *buf;
1908 int outlen = 0;
1909 int i;
1910 int retval;
1912 if (len <= 0) /* "cannot happen"? */
1913 return OK;
1915 if (enc_utf8)
1917 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1918 if (buf == NULL)
1919 return OK; /* not much we could do here... */
1921 for (i = off; i < off + len; ++i)
1923 if (ScreenLines[i] == 0)
1924 continue; /* skip second half of double-width char */
1926 if (ScreenLinesUC[i] == 0)
1927 buf[outlen++] = ScreenLines[i];
1928 else
1929 outlen += utfc_char2bytes(i, buf + outlen);
1932 buf[outlen] = NUL; /* only to aid debugging */
1933 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1934 vim_free(buf);
1936 return retval;
1938 else if (enc_dbcs == DBCS_JPNU)
1940 buf = alloc((unsigned)(len * 2 + 1));
1941 if (buf == NULL)
1942 return OK; /* not much we could do here... */
1944 for (i = off; i < off + len; ++i)
1946 buf[outlen++] = ScreenLines[i];
1948 /* handle double-byte single-width char */
1949 if (ScreenLines[i] == 0x8e)
1950 buf[outlen++] = ScreenLines2[i];
1951 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1952 buf[outlen++] = ScreenLines[++i];
1955 buf[outlen] = NUL; /* only to aid debugging */
1956 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1957 vim_free(buf);
1959 return retval;
1961 else
1963 return gui_outstr_nowrap(&ScreenLines[off], len,
1964 flags, fg, bg, back);
1967 #endif /* HAVE_GTK2 */
1970 * Output the given string at the current cursor position. If the string is
1971 * too long to fit on the line, then it is truncated.
1972 * "flags":
1973 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1974 * actually draw (an inverted) cursor.
1975 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
1976 * background.
1977 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1978 * it.
1979 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1980 * FAIL (the caller should start drawing "back" chars back).
1983 gui_outstr_nowrap(s, len, flags, fg, bg, back)
1984 char_u *s;
1985 int len;
1986 int flags;
1987 guicolor_T fg, bg; /* colors for cursor */
1988 int back; /* backup this many chars when using bold trick */
1990 long_u highlight_mask;
1991 long_u hl_mask_todo;
1992 guicolor_T fg_color;
1993 guicolor_T bg_color;
1994 guicolor_T sp_color;
1995 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
1996 GuiFont font = NOFONT;
1997 # ifdef FEAT_XFONTSET
1998 GuiFontset fontset = NOFONTSET;
1999 # endif
2000 #endif
2001 attrentry_T *aep = NULL;
2002 int draw_flags;
2003 int col = gui.col;
2004 #ifdef FEAT_SIGN_ICONS
2005 int draw_sign = FALSE;
2006 # ifdef FEAT_NETBEANS_INTG
2007 int multi_sign = FALSE;
2008 # endif
2009 #endif
2011 if (len < 0)
2012 len = (int)STRLEN(s);
2013 if (len == 0)
2014 return OK;
2016 #ifdef FEAT_SIGN_ICONS
2017 if (*s == SIGN_BYTE
2018 # ifdef FEAT_NETBEANS_INTG
2019 || *s == MULTISIGN_BYTE
2020 # endif
2023 # ifdef FEAT_NETBEANS_INTG
2024 if (*s == MULTISIGN_BYTE)
2025 multi_sign = TRUE;
2026 # endif
2027 /* draw spaces instead */
2028 s = (char_u *)" ";
2029 if (len == 1 && col > 0)
2030 --col;
2031 len = 2;
2032 draw_sign = TRUE;
2033 highlight_mask = 0;
2035 else
2036 #endif
2037 if (gui.highlight_mask > HL_ALL)
2039 aep = syn_gui_attr2entry(gui.highlight_mask);
2040 if (aep == NULL) /* highlighting not set */
2041 highlight_mask = 0;
2042 else
2043 highlight_mask = aep->ae_attr;
2045 else
2046 highlight_mask = gui.highlight_mask;
2047 hl_mask_todo = highlight_mask;
2049 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2050 /* Set the font */
2051 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2052 font = aep->ae_u.gui.font;
2053 # ifdef FEAT_XFONTSET
2054 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2055 fontset = aep->ae_u.gui.fontset;
2056 # endif
2057 else
2059 # ifdef FEAT_XFONTSET
2060 if (gui.fontset != NOFONTSET)
2061 fontset = gui.fontset;
2062 else
2063 # endif
2064 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2066 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2068 font = gui.boldital_font;
2069 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2071 else if (gui.bold_font != NOFONT)
2073 font = gui.bold_font;
2074 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2076 else
2077 font = gui.norm_font;
2079 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2081 font = gui.ital_font;
2082 hl_mask_todo &= ~HL_ITALIC;
2084 else
2085 font = gui.norm_font;
2087 # ifdef FEAT_XFONTSET
2088 if (fontset != NOFONTSET)
2089 gui_mch_set_fontset(fontset);
2090 else
2091 # endif
2092 gui_mch_set_font(font);
2093 #endif
2095 draw_flags = 0;
2097 /* Set the color */
2098 bg_color = gui.back_pixel;
2099 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2101 draw_flags |= DRAW_CURSOR;
2102 fg_color = fg;
2103 bg_color = bg;
2104 sp_color = fg;
2106 else if (aep != NULL)
2108 fg_color = aep->ae_u.gui.fg_color;
2109 if (fg_color == INVALCOLOR)
2110 fg_color = gui.norm_pixel;
2111 bg_color = aep->ae_u.gui.bg_color;
2112 if (bg_color == INVALCOLOR)
2113 bg_color = gui.back_pixel;
2114 sp_color = aep->ae_u.gui.sp_color;
2115 if (sp_color == INVALCOLOR)
2116 sp_color = fg_color;
2118 else
2120 fg_color = gui.norm_pixel;
2121 sp_color = fg_color;
2124 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2126 #if defined(AMIGA) || defined(RISCOS)
2127 gui_mch_set_colors(bg_color, fg_color);
2128 #else
2129 gui_mch_set_fg_color(bg_color);
2130 gui_mch_set_bg_color(fg_color);
2131 #endif
2133 else
2135 #if defined(AMIGA) || defined(RISCOS)
2136 gui_mch_set_colors(fg_color, bg_color);
2137 #else
2138 gui_mch_set_fg_color(fg_color);
2139 gui_mch_set_bg_color(bg_color);
2140 #endif
2142 gui_mch_set_sp_color(sp_color);
2144 /* Clear the selection if we are about to write over it */
2145 if (!(flags & GUI_MON_NOCLEAR))
2146 clip_may_clear_selection(gui.row, gui.row);
2149 #ifndef MSWIN16_FASTTEXT
2150 /* If there's no bold font, then fake it */
2151 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2152 draw_flags |= DRAW_BOLD;
2153 #endif
2156 * When drawing bold or italic characters the spill-over from the left
2157 * neighbor may be destroyed. Let the caller backup to start redrawing
2158 * just after a blank.
2160 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2161 return FAIL;
2163 #if defined(RISCOS) || defined(HAVE_GTK2)
2164 /* If there's no italic font, then fake it.
2165 * For GTK2, we don't need a different font for italic style. */
2166 if (hl_mask_todo & HL_ITALIC)
2167 draw_flags |= DRAW_ITALIC;
2169 /* Do we underline the text? */
2170 if (hl_mask_todo & HL_UNDERLINE)
2171 draw_flags |= DRAW_UNDERL;
2172 #else
2173 /* Do we underline the text? */
2174 if ((hl_mask_todo & HL_UNDERLINE)
2175 # ifndef MSWIN16_FASTTEXT
2176 || (hl_mask_todo & HL_ITALIC)
2177 # endif
2179 draw_flags |= DRAW_UNDERL;
2180 #endif
2181 /* Do we undercurl the text? */
2182 if (hl_mask_todo & HL_UNDERCURL)
2183 draw_flags |= DRAW_UNDERC;
2185 /* Do we draw transparently? */
2186 if (flags & GUI_MON_TRS_CURSOR)
2187 draw_flags |= DRAW_TRANSP;
2190 * Draw the text.
2192 #ifdef HAVE_GTK2
2193 /* The value returned is the length in display cells */
2194 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2195 #else
2196 # ifdef FEAT_MBYTE
2197 if (enc_utf8)
2199 int start; /* index of bytes to be drawn */
2200 int cells; /* cellwidth of bytes to be drawn */
2201 int thislen; /* length of bytes to be drawin */
2202 int cn; /* cellwidth of current char */
2203 int i; /* index of current char */
2204 int c; /* current char value */
2205 int cl; /* byte length of current char */
2206 int comping; /* current char is composing */
2207 int scol = col; /* screen column */
2208 int dowide; /* use 'guifontwide' */
2210 /* Break the string at a composing character, it has to be drawn on
2211 * top of the previous character. */
2212 start = 0;
2213 cells = 0;
2214 for (i = 0; i < len; i += cl)
2216 c = utf_ptr2char(s + i);
2217 cn = utf_char2cells(c);
2218 if (cn > 1
2219 # ifdef FEAT_XFONTSET
2220 && fontset == NOFONTSET
2221 # endif
2222 && gui.wide_font != NOFONT)
2223 dowide = TRUE;
2224 else
2225 dowide = FALSE;
2226 comping = utf_iscomposing(c);
2227 if (!comping) /* count cells from non-composing chars */
2228 cells += cn;
2229 cl = utf_ptr2len(s + i);
2230 if (cl == 0) /* hit end of string */
2231 len = i + cl; /* len must be wrong "cannot happen" */
2233 /* print the string so far if it's the last character or there is
2234 * a composing character. */
2235 if (i + cl >= len || (comping && i > start) || dowide
2236 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2237 || (cn > 1
2238 # ifdef FEAT_XFONTSET
2239 /* No fontset: At least draw char after wide char at
2240 * right position. */
2241 && fontset == NOFONTSET
2242 # endif
2244 # endif
2247 if (comping || dowide)
2248 thislen = i - start;
2249 else
2250 thislen = i - start + cl;
2251 if (thislen > 0)
2253 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2254 draw_flags);
2255 start += thislen;
2257 scol += cells;
2258 cells = 0;
2259 if (dowide)
2261 gui_mch_set_font(gui.wide_font);
2262 gui_mch_draw_string(gui.row, scol - cn,
2263 s + start, cl, draw_flags);
2264 gui_mch_set_font(font);
2265 start += cl;
2268 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2269 /* No fontset: draw a space to fill the gap after a wide char
2270 * */
2271 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2272 # ifdef FEAT_XFONTSET
2273 && fontset == NOFONTSET
2274 # endif
2275 && !dowide)
2276 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2277 1, draw_flags);
2278 # endif
2280 /* Draw a composing char on top of the previous char. */
2281 if (comping)
2283 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2284 /* Carbon ATSUI autodraws composing char over previous char */
2285 gui_mch_draw_string(gui.row, scol, s + i, cl,
2286 draw_flags | DRAW_TRANSP);
2287 # else
2288 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2289 draw_flags | DRAW_TRANSP);
2290 # endif
2291 start = i + cl;
2294 /* The stuff below assumes "len" is the length in screen columns. */
2295 len = scol - col;
2297 else
2298 # endif
2300 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2301 # ifdef FEAT_MBYTE
2302 if (enc_dbcs == DBCS_JPNU)
2304 int clen = 0;
2305 int i;
2307 /* Get the length in display cells, this can be different from the
2308 * number of bytes for "euc-jp". */
2309 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2310 clen += (*mb_ptr2cells)(s + i);
2311 len = clen;
2313 # endif
2315 #endif /* !HAVE_GTK2 */
2317 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2318 gui.col = col + len;
2320 /* May need to invert it when it's part of the selection. */
2321 if (flags & GUI_MON_NOCLEAR)
2322 clip_may_redraw_selection(gui.row, col, len);
2324 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2326 /* Invalidate the old physical cursor position if we wrote over it */
2327 if (gui.cursor_row == gui.row
2328 && gui.cursor_col >= col
2329 && gui.cursor_col < col + len)
2330 gui.cursor_is_valid = FALSE;
2333 #ifdef FEAT_SIGN_ICONS
2334 if (draw_sign)
2335 /* Draw the sign on top of the spaces. */
2336 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2337 # ifdef FEAT_NETBEANS_INTG
2338 if (multi_sign)
2339 netbeans_draw_multisign_indicator(gui.row);
2340 # endif
2341 #endif
2343 return OK;
2347 * Un-draw the cursor. Actually this just redraws the character at the given
2348 * position. The character just before it too, for when it was in bold.
2350 void
2351 gui_undraw_cursor()
2353 if (gui.cursor_is_valid)
2355 #ifdef FEAT_HANGULIN
2356 if (composing_hangul
2357 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2358 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2359 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2360 gui.norm_pixel, gui.back_pixel, 0);
2361 else
2363 #endif
2364 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2365 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2366 && gui.cursor_col > 0)
2367 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2368 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2369 #ifdef FEAT_HANGULIN
2370 if (composing_hangul)
2371 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2372 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2374 #endif
2375 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2376 * here in case it wasn't needed to undraw it. */
2377 gui.cursor_is_valid = FALSE;
2381 void
2382 gui_redraw(x, y, w, h)
2383 int x;
2384 int y;
2385 int w;
2386 int h;
2388 int row1, col1, row2, col2;
2390 row1 = Y_2_ROW(y);
2391 col1 = X_2_COL(x);
2392 row2 = Y_2_ROW(y + h - 1);
2393 col2 = X_2_COL(x + w - 1);
2395 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2398 * We may need to redraw the cursor, but don't take it upon us to change
2399 * its location after a scroll.
2400 * (maybe be more strict even and test col too?)
2401 * These things may be outside the update/clipping region and reality may
2402 * not reflect Vims internal ideas if these operations are clipped away.
2404 if (gui.row == gui.cursor_row)
2405 gui_update_cursor(TRUE, TRUE);
2409 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2410 * from col1 to col2 (inclusive).
2411 * Return TRUE when the character before the first drawn character has
2412 * different attributes (may have to be redrawn too).
2415 gui_redraw_block(row1, col1, row2, col2, flags)
2416 int row1;
2417 int col1;
2418 int row2;
2419 int col2;
2420 int flags; /* flags for gui_outstr_nowrap() */
2422 int old_row, old_col;
2423 long_u old_hl_mask;
2424 int off;
2425 sattr_T first_attr;
2426 int idx, len;
2427 int back, nback;
2428 int retval = FALSE;
2429 #ifdef FEAT_MBYTE
2430 int orig_col1, orig_col2;
2431 #endif
2433 /* Don't try to update when ScreenLines is not valid */
2434 if (!screen_cleared || ScreenLines == NULL)
2435 return retval;
2437 /* Don't try to draw outside the shell! */
2438 /* Check everything, strange values may be caused by a big border width */
2439 col1 = check_col(col1);
2440 col2 = check_col(col2);
2441 row1 = check_row(row1);
2442 row2 = check_row(row2);
2444 /* Remember where our cursor was */
2445 old_row = gui.row;
2446 old_col = gui.col;
2447 old_hl_mask = gui.highlight_mask;
2448 #ifdef FEAT_MBYTE
2449 orig_col1 = col1;
2450 orig_col2 = col2;
2451 #endif
2453 for (gui.row = row1; gui.row <= row2; gui.row++)
2455 #ifdef FEAT_MBYTE
2456 /* When only half of a double-wide character is in the block, include
2457 * the other half. */
2458 col1 = orig_col1;
2459 col2 = orig_col2;
2460 off = LineOffset[gui.row];
2461 if (enc_dbcs != 0)
2463 if (col1 > 0)
2464 col1 -= dbcs_screen_head_off(ScreenLines + off,
2465 ScreenLines + off + col1);
2466 col2 += dbcs_screen_tail_off(ScreenLines + off,
2467 ScreenLines + off + col2);
2469 else if (enc_utf8)
2471 if (ScreenLines[off + col1] == 0)
2472 --col1;
2473 # ifdef HAVE_GTK2
2474 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2475 ++col2;
2476 # endif
2478 #endif
2479 gui.col = col1;
2480 off = LineOffset[gui.row] + gui.col;
2481 len = col2 - col1 + 1;
2483 /* Find how many chars back this highlighting starts, or where a space
2484 * is. Needed for when the bold trick is used */
2485 for (back = 0; back < col1; ++back)
2486 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2487 || ScreenLines[off - 1 - back] == ' ')
2488 break;
2489 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2490 && ScreenLines[off - 1] != ' ');
2492 /* Break it up in strings of characters with the same attributes. */
2493 /* Print UTF-8 characters individually. */
2494 while (len > 0)
2496 first_attr = ScreenAttrs[off];
2497 gui.highlight_mask = first_attr;
2498 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2499 if (enc_utf8 && ScreenLinesUC[off] != 0)
2501 /* output multi-byte character separately */
2502 nback = gui_screenchar(off, flags,
2503 (guicolor_T)0, (guicolor_T)0, back);
2504 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2505 idx = 2;
2506 else
2507 idx = 1;
2509 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2511 /* output double-byte, single-width character separately */
2512 nback = gui_screenchar(off, flags,
2513 (guicolor_T)0, (guicolor_T)0, back);
2514 idx = 1;
2516 else
2517 #endif
2519 #ifdef HAVE_GTK2
2520 for (idx = 0; idx < len; ++idx)
2522 if (enc_utf8 && ScreenLines[off + idx] == 0)
2523 continue; /* skip second half of double-width char */
2524 if (ScreenAttrs[off + idx] != first_attr)
2525 break;
2527 /* gui_screenstr() takes care of multibyte chars */
2528 nback = gui_screenstr(off, idx, flags,
2529 (guicolor_T)0, (guicolor_T)0, back);
2530 #else
2531 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2532 idx++)
2534 # ifdef FEAT_MBYTE
2535 /* Stop at a multi-byte Unicode character. */
2536 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2537 break;
2538 if (enc_dbcs == DBCS_JPNU)
2540 /* Stop at a double-byte single-width char. */
2541 if (ScreenLines[off + idx] == 0x8e)
2542 break;
2543 if (len > 1 && (*mb_ptr2len)(ScreenLines
2544 + off + idx) == 2)
2545 ++idx; /* skip second byte of double-byte char */
2547 # endif
2549 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2550 (guicolor_T)0, (guicolor_T)0, back);
2551 #endif
2553 if (nback == FAIL)
2555 /* Must back up to start drawing where a bold or italic word
2556 * starts. */
2557 off -= back;
2558 len += back;
2559 gui.col -= back;
2561 else
2563 off += idx;
2564 len -= idx;
2566 back = 0;
2570 /* Put the cursor back where it was */
2571 gui.row = old_row;
2572 gui.col = old_col;
2573 gui.highlight_mask = (int)old_hl_mask;
2575 return retval;
2578 static void
2579 gui_delete_lines(row, count)
2580 int row;
2581 int count;
2583 if (count <= 0)
2584 return;
2586 if (row + count > gui.scroll_region_bot)
2587 /* Scrolled out of region, just blank the lines out */
2588 gui_clear_block(row, gui.scroll_region_left,
2589 gui.scroll_region_bot, gui.scroll_region_right);
2590 else
2592 gui_mch_delete_lines(row, count);
2594 /* If the cursor was in the deleted lines it's now gone. If the
2595 * cursor was in the scrolled lines adjust its position. */
2596 if (gui.cursor_row >= row
2597 && gui.cursor_col >= gui.scroll_region_left
2598 && gui.cursor_col <= gui.scroll_region_right)
2600 if (gui.cursor_row < row + count)
2601 gui.cursor_is_valid = FALSE;
2602 else if (gui.cursor_row <= gui.scroll_region_bot)
2603 gui.cursor_row -= count;
2608 static void
2609 gui_insert_lines(row, count)
2610 int row;
2611 int count;
2613 if (count <= 0)
2614 return;
2616 if (row + count > gui.scroll_region_bot)
2617 /* Scrolled out of region, just blank the lines out */
2618 gui_clear_block(row, gui.scroll_region_left,
2619 gui.scroll_region_bot, gui.scroll_region_right);
2620 else
2622 gui_mch_insert_lines(row, count);
2624 if (gui.cursor_row >= gui.row
2625 && gui.cursor_col >= gui.scroll_region_left
2626 && gui.cursor_col <= gui.scroll_region_right)
2628 if (gui.cursor_row <= gui.scroll_region_bot - count)
2629 gui.cursor_row += count;
2630 else if (gui.cursor_row <= gui.scroll_region_bot)
2631 gui.cursor_is_valid = FALSE;
2637 * The main GUI input routine. Waits for a character from the keyboard.
2638 * wtime == -1 Wait forever.
2639 * wtime == 0 Don't wait.
2640 * wtime > 0 Wait wtime milliseconds for a character.
2641 * Returns OK if a character was found to be available within the given time,
2642 * or FAIL otherwise.
2645 gui_wait_for_chars(wtime)
2646 long wtime;
2648 int retval;
2651 * If we're going to wait a bit, update the menus and mouse shape for the
2652 * current State.
2654 if (wtime != 0)
2656 #ifdef FEAT_MENU
2657 gui_update_menus(0);
2658 #endif
2661 gui_mch_update();
2662 if (input_available()) /* Got char, return immediately */
2663 return OK;
2664 if (wtime == 0) /* Don't wait for char */
2665 return FAIL;
2667 /* Before waiting, flush any output to the screen. */
2668 gui_mch_flush();
2670 if (wtime > 0)
2672 /* Blink when waiting for a character. Probably only does something
2673 * for showmatch() */
2674 gui_mch_start_blink();
2675 retval = gui_mch_wait_for_chars(wtime);
2676 gui_mch_stop_blink();
2677 return retval;
2681 * While we are waiting indefinitely for a character, blink the cursor.
2683 gui_mch_start_blink();
2685 retval = FAIL;
2687 * We may want to trigger the CursorHold event. First wait for
2688 * 'updatetime' and if nothing is typed within that time put the
2689 * K_CURSORHOLD key in the input buffer.
2691 if (gui_mch_wait_for_chars(p_ut) == OK)
2692 retval = OK;
2693 #ifdef FEAT_AUTOCMD
2694 else if (trigger_cursorhold())
2696 char_u buf[3];
2698 /* Put K_CURSORHOLD in the input buffer. */
2699 buf[0] = CSI;
2700 buf[1] = KS_EXTRA;
2701 buf[2] = (int)KE_CURSORHOLD;
2702 add_to_input_buf(buf, 3);
2704 retval = OK;
2706 #endif
2708 if (retval == FAIL)
2710 /* Blocking wait. */
2711 before_blocking();
2712 retval = gui_mch_wait_for_chars(-1L);
2715 gui_mch_stop_blink();
2716 return retval;
2720 * Fill p[4] with mouse coordinates encoded for check_termcode().
2722 static void
2723 fill_mouse_coord(p, col, row)
2724 char_u *p;
2725 int col;
2726 int row;
2728 p[0] = (char_u)(col / 128 + ' ' + 1);
2729 p[1] = (char_u)(col % 128 + ' ' + 1);
2730 p[2] = (char_u)(row / 128 + ' ' + 1);
2731 p[3] = (char_u)(row % 128 + ' ' + 1);
2735 * Generic mouse support function. Add a mouse event to the input buffer with
2736 * the given properties.
2737 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2738 * MOUSE_X1, MOUSE_X2
2739 * MOUSE_DRAG, or MOUSE_RELEASE.
2740 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2741 * x, y --- Coordinates of mouse in pixels.
2742 * repeated_click --- TRUE if this click comes only a short time after a
2743 * previous click.
2744 * modifiers --- Bit field which may be any of the following modifiers
2745 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2746 * This function will ignore drag events where the mouse has not moved to a new
2747 * character.
2749 void
2750 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2751 int button;
2752 int x;
2753 int y;
2754 int repeated_click;
2755 int_u modifiers;
2757 static int prev_row = 0, prev_col = 0;
2758 static int prev_button = -1;
2759 static int num_clicks = 1;
2760 char_u string[10];
2761 enum key_extra button_char;
2762 int row, col;
2763 #ifdef FEAT_CLIPBOARD
2764 int checkfor;
2765 int did_clip = FALSE;
2766 #endif
2769 * Scrolling may happen at any time, also while a selection is present.
2771 switch (button)
2773 case MOUSE_X1:
2774 button_char = KE_X1MOUSE;
2775 goto button_set;
2776 case MOUSE_X2:
2777 button_char = KE_X2MOUSE;
2778 goto button_set;
2779 case MOUSE_4:
2780 button_char = KE_MOUSEDOWN;
2781 goto button_set;
2782 case MOUSE_5:
2783 button_char = KE_MOUSEUP;
2784 button_set:
2786 /* Don't put events in the input queue now. */
2787 if (hold_gui_events)
2788 return;
2790 string[3] = CSI;
2791 string[4] = KS_EXTRA;
2792 string[5] = (int)button_char;
2794 /* Pass the pointer coordinates of the scroll event so that we
2795 * know which window to scroll. */
2796 row = gui_xy2colrow(x, y, &col);
2797 string[6] = (char_u)(col / 128 + ' ' + 1);
2798 string[7] = (char_u)(col % 128 + ' ' + 1);
2799 string[8] = (char_u)(row / 128 + ' ' + 1);
2800 string[9] = (char_u)(row % 128 + ' ' + 1);
2802 if (modifiers == 0)
2803 add_to_input_buf(string + 3, 7);
2804 else
2806 string[0] = CSI;
2807 string[1] = KS_MODIFIER;
2808 string[2] = 0;
2809 if (modifiers & MOUSE_SHIFT)
2810 string[2] |= MOD_MASK_SHIFT;
2811 if (modifiers & MOUSE_CTRL)
2812 string[2] |= MOD_MASK_CTRL;
2813 if (modifiers & MOUSE_ALT)
2814 string[2] |= MOD_MASK_ALT;
2815 add_to_input_buf(string, 10);
2817 return;
2821 #ifdef FEAT_CLIPBOARD
2822 /* If a clipboard selection is in progress, handle it */
2823 if (clip_star.state == SELECT_IN_PROGRESS)
2825 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2826 return;
2829 /* Determine which mouse settings to look for based on the current mode */
2830 switch (get_real_state())
2832 case NORMAL_BUSY:
2833 case OP_PENDING:
2834 case NORMAL: checkfor = MOUSE_NORMAL; break;
2835 case VISUAL: checkfor = MOUSE_VISUAL; break;
2836 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2837 case REPLACE:
2838 case REPLACE+LANGMAP:
2839 #ifdef FEAT_VREPLACE
2840 case VREPLACE:
2841 case VREPLACE+LANGMAP:
2842 #endif
2843 case INSERT:
2844 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2845 case ASKMORE:
2846 case HITRETURN: /* At the more- and hit-enter prompt pass the
2847 mouse event for a click on or below the
2848 message line. */
2849 if (Y_2_ROW(y) >= msg_row)
2850 checkfor = MOUSE_NORMAL;
2851 else
2852 checkfor = MOUSE_RETURN;
2853 break;
2856 * On the command line, use the clipboard selection on all lines
2857 * but the command line. But not when pasting.
2859 case CMDLINE:
2860 case CMDLINE+LANGMAP:
2861 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2862 checkfor = MOUSE_NONE;
2863 else
2864 checkfor = MOUSE_COMMAND;
2865 break;
2867 default:
2868 checkfor = MOUSE_NONE;
2869 break;
2873 * Allow clipboard selection of text on the command line in "normal"
2874 * modes. Don't do this when dragging the status line, or extending a
2875 * Visual selection.
2877 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2878 && Y_2_ROW(y) >= topframe->fr_height
2879 # ifdef FEAT_WINDOWS
2880 + firstwin->w_winrow
2881 # endif
2882 && button != MOUSE_DRAG
2883 # ifdef FEAT_MOUSESHAPE
2884 && !drag_status_line
2885 # ifdef FEAT_VERTSPLIT
2886 && !drag_sep_line
2887 # endif
2888 # endif
2890 checkfor = MOUSE_NONE;
2893 * Use modeless selection when holding CTRL and SHIFT pressed.
2895 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2896 checkfor = MOUSE_NONEF;
2899 * In Ex mode, always use modeless selection.
2901 if (exmode_active)
2902 checkfor = MOUSE_NONE;
2905 * If the mouse settings say to not use the mouse, use the modeless
2906 * selection. But if Visual is active, assume that only the Visual area
2907 * will be selected.
2908 * Exception: On the command line, both the selection is used and a mouse
2909 * key is send.
2911 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2913 #ifdef FEAT_VISUAL
2914 /* Don't do modeless selection in Visual mode. */
2915 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2916 return;
2917 #endif
2920 * When 'mousemodel' is "popup", shift-left is translated to right.
2921 * But not when also using Ctrl.
2923 if (mouse_model_popup() && button == MOUSE_LEFT
2924 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2926 button = MOUSE_RIGHT;
2927 modifiers &= ~ MOUSE_SHIFT;
2930 /* If the selection is done, allow the right button to extend it.
2931 * If the selection is cleared, allow the right button to start it
2932 * from the cursor position. */
2933 if (button == MOUSE_RIGHT)
2935 if (clip_star.state == SELECT_CLEARED)
2937 if (State & CMDLINE)
2939 col = msg_col;
2940 row = msg_row;
2942 else
2944 col = curwin->w_wcol;
2945 row = curwin->w_wrow + W_WINROW(curwin);
2947 clip_start_selection(col, row, FALSE);
2949 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2950 repeated_click);
2951 did_clip = TRUE;
2953 /* Allow the left button to start the selection */
2954 else if (button ==
2955 # ifdef RISCOS
2956 /* Only start a drag on a drag event. Otherwise
2957 * we don't get a release event. */
2958 MOUSE_DRAG
2959 # else
2960 MOUSE_LEFT
2961 # endif
2964 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2965 did_clip = TRUE;
2967 # ifdef RISCOS
2968 else if (button == MOUSE_LEFT)
2970 clip_clear_selection();
2971 did_clip = TRUE;
2973 # endif
2975 /* Always allow pasting */
2976 if (button != MOUSE_MIDDLE)
2978 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2979 return;
2980 if (checkfor != MOUSE_COMMAND)
2981 button = MOUSE_LEFT;
2983 repeated_click = FALSE;
2986 if (clip_star.state != SELECT_CLEARED && !did_clip)
2987 clip_clear_selection();
2988 #endif
2990 /* Don't put events in the input queue now. */
2991 if (hold_gui_events)
2992 return;
2994 row = gui_xy2colrow(x, y, &col);
2997 * If we are dragging and the mouse hasn't moved far enough to be on a
2998 * different character, then don't send an event to vim.
3000 if (button == MOUSE_DRAG)
3002 if (row == prev_row && col == prev_col)
3003 return;
3004 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3005 if (y < 0)
3006 row = -1;
3010 * If topline has changed (window scrolled) since the last click, reset
3011 * repeated_click, because we don't want starting Visual mode when
3012 * clicking on a different character in the text.
3014 if (curwin->w_topline != gui_prev_topline
3015 #ifdef FEAT_DIFF
3016 || curwin->w_topfill != gui_prev_topfill
3017 #endif
3019 repeated_click = FALSE;
3021 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3022 string[1] = KS_MOUSE;
3023 string[2] = KE_FILLER;
3024 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3026 if (repeated_click)
3029 * Handle multiple clicks. They only count if the mouse is still
3030 * pointing at the same character.
3032 if (button != prev_button || row != prev_row || col != prev_col)
3033 num_clicks = 1;
3034 else if (++num_clicks > 4)
3035 num_clicks = 1;
3037 else
3038 num_clicks = 1;
3039 prev_button = button;
3040 gui_prev_topline = curwin->w_topline;
3041 #ifdef FEAT_DIFF
3042 gui_prev_topfill = curwin->w_topfill;
3043 #endif
3045 string[3] = (char_u)(button | 0x20);
3046 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3048 else
3049 string[3] = (char_u)button;
3051 string[3] |= modifiers;
3052 fill_mouse_coord(string + 4, col, row);
3053 add_to_input_buf(string, 8);
3055 if (row < 0)
3056 prev_row = 0;
3057 else
3058 prev_row = row;
3059 prev_col = col;
3062 * We need to make sure this is cleared since Athena doesn't tell us when
3063 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3065 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3066 gui.dragged_sb = SBAR_NONE;
3067 #endif
3071 * Convert x and y coordinate to column and row in text window.
3072 * Corrects for multi-byte character.
3073 * returns column in "*colp" and row as return value;
3076 gui_xy2colrow(x, y, colp)
3077 int x;
3078 int y;
3079 int *colp;
3081 int col = check_col(X_2_COL(x));
3082 int row = check_row(Y_2_ROW(y));
3084 #ifdef FEAT_MBYTE
3085 *colp = mb_fix_col(col, row);
3086 #else
3087 *colp = col;
3088 #endif
3089 return row;
3092 #if defined(FEAT_MENU) || defined(PROTO)
3094 * Callback function for when a menu entry has been selected.
3096 void
3097 gui_menu_cb(menu)
3098 vimmenu_T *menu;
3100 char_u bytes[sizeof(long_u)];
3102 /* Don't put events in the input queue now. */
3103 if (hold_gui_events)
3104 return;
3106 bytes[0] = CSI;
3107 bytes[1] = KS_MENU;
3108 bytes[2] = KE_FILLER;
3109 add_to_input_buf(bytes, 3);
3110 add_long_to_buf((long_u)menu, bytes);
3111 add_to_input_buf_csi(bytes, sizeof(long_u));
3113 #endif
3115 static int prev_which_scrollbars[3];
3118 * Set which components are present.
3119 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3120 * in p_go.
3122 /*ARGSUSED*/
3123 void
3124 gui_init_which_components(oldval)
3125 char_u *oldval;
3127 #ifdef FEAT_MENU
3128 static int prev_menu_is_active = -1;
3129 #endif
3130 #ifdef FEAT_TOOLBAR
3131 static int prev_toolbar = -1;
3132 int using_toolbar = FALSE;
3133 #endif
3134 #ifdef FEAT_GUI_TABLINE
3135 int using_tabline;
3136 #endif
3137 #ifdef FEAT_FOOTER
3138 static int prev_footer = -1;
3139 int using_footer = FALSE;
3140 #endif
3141 #if defined(FEAT_MENU) && !defined(WIN16)
3142 static int prev_tearoff = -1;
3143 int using_tearoff = FALSE;
3144 #endif
3146 char_u *p;
3147 int i;
3148 #ifdef FEAT_MENU
3149 int grey_old, grey_new;
3150 char_u *temp;
3151 #endif
3152 win_T *wp;
3153 int need_set_size;
3154 int fix_size;
3156 #ifdef FEAT_MENU
3157 if (oldval != NULL && gui.in_use)
3160 * Check if the menu's go from grey to non-grey or vise versa.
3162 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3163 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3164 if (grey_old != grey_new)
3166 temp = p_go;
3167 p_go = oldval;
3168 gui_update_menus(MENU_ALL_MODES);
3169 p_go = temp;
3172 gui.menu_is_active = FALSE;
3173 #endif
3175 for (i = 0; i < 3; i++)
3176 gui.which_scrollbars[i] = FALSE;
3177 for (p = p_go; *p; p++)
3178 switch (*p)
3180 case GO_LEFT:
3181 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3182 break;
3183 case GO_RIGHT:
3184 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3185 break;
3186 #ifdef FEAT_VERTSPLIT
3187 case GO_VLEFT:
3188 if (win_hasvertsplit())
3189 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3190 break;
3191 case GO_VRIGHT:
3192 if (win_hasvertsplit())
3193 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3194 break;
3195 #endif
3196 case GO_BOT:
3197 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3198 break;
3199 #ifdef FEAT_MENU
3200 case GO_MENUS:
3201 gui.menu_is_active = TRUE;
3202 break;
3203 #endif
3204 case GO_GREY:
3205 /* make menu's have grey items, ignored here */
3206 break;
3207 #ifdef FEAT_TOOLBAR
3208 case GO_TOOLBAR:
3209 using_toolbar = TRUE;
3210 break;
3211 #endif
3212 #ifdef FEAT_FOOTER
3213 case GO_FOOTER:
3214 using_footer = TRUE;
3215 break;
3216 #endif
3217 case GO_TEAROFF:
3218 #if defined(FEAT_MENU) && !defined(WIN16)
3219 using_tearoff = TRUE;
3220 #endif
3221 break;
3222 default:
3223 /* Ignore options that are not supported */
3224 break;
3227 if (gui.in_use)
3229 need_set_size = 0;
3230 fix_size = FALSE;
3232 #ifdef FEAT_GUI_TABLINE
3233 /* Update the GUI tab line, it may appear or disappear. This may
3234 * cause the non-GUI tab line to disappear or appear. */
3235 using_tabline = gui_has_tabline();
3236 if (!gui_mch_showing_tabline() != !using_tabline)
3238 /* We don't want a resize event change "Rows" here, save and
3239 * restore it. Resizing is handled below. */
3240 i = Rows;
3241 gui_update_tabline();
3242 Rows = i;
3243 need_set_size = RESIZE_VERT;
3244 if (using_tabline)
3245 fix_size = TRUE;
3246 if (!gui_use_tabline())
3247 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3249 #endif
3251 for (i = 0; i < 3; i++)
3253 /* The scrollbar needs to be updated when it is shown/unshown and
3254 * when switching tab pages. But the size only changes when it's
3255 * shown/unshown. Thus we need two places to remember whether a
3256 * scrollbar is there or not. */
3257 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3258 #ifdef FEAT_WINDOWS
3259 || gui.which_scrollbars[i]
3260 != curtab->tp_prev_which_scrollbars[i]
3261 #endif
3264 if (i == SBAR_BOTTOM)
3265 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3266 gui.which_scrollbars[i]);
3267 else
3269 FOR_ALL_WINDOWS(wp)
3271 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3274 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3276 if (i == SBAR_BOTTOM)
3277 need_set_size = RESIZE_VERT;
3278 else
3279 need_set_size = RESIZE_HOR;
3280 if (gui.which_scrollbars[i])
3281 fix_size = TRUE;
3284 #ifdef FEAT_WINDOWS
3285 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3286 #endif
3287 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3290 #ifdef FEAT_MENU
3291 if (gui.menu_is_active != prev_menu_is_active)
3293 /* We don't want a resize event change "Rows" here, save and
3294 * restore it. Resizing is handled below. */
3295 i = Rows;
3296 gui_mch_enable_menu(gui.menu_is_active);
3297 Rows = i;
3298 prev_menu_is_active = gui.menu_is_active;
3299 need_set_size = RESIZE_VERT;
3300 if (gui.menu_is_active)
3301 fix_size = TRUE;
3303 #endif
3305 #ifdef FEAT_TOOLBAR
3306 if (using_toolbar != prev_toolbar)
3308 gui_mch_show_toolbar(using_toolbar);
3309 prev_toolbar = using_toolbar;
3310 need_set_size = RESIZE_VERT;
3311 if (using_toolbar)
3312 fix_size = TRUE;
3314 #endif
3315 #ifdef FEAT_FOOTER
3316 if (using_footer != prev_footer)
3318 gui_mch_enable_footer(using_footer);
3319 prev_footer = using_footer;
3320 need_set_size = RESIZE_VERT;
3321 if (using_footer)
3322 fix_size = TRUE;
3324 #endif
3325 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3326 if (using_tearoff != prev_tearoff)
3328 gui_mch_toggle_tearoffs(using_tearoff);
3329 prev_tearoff = using_tearoff;
3331 #endif
3332 if (need_set_size)
3334 #ifdef FEAT_GUI_GTK
3335 long c = Columns;
3336 #endif
3337 /* Adjust the size of the window to make the text area keep the
3338 * same size and to avoid that part of our window is off-screen
3339 * and a scrollbar can't be used, for example. */
3340 gui_set_shellsize(FALSE, fix_size, need_set_size);
3342 #ifdef FEAT_GUI_GTK
3343 /* GTK has the annoying habit of sending us resize events when
3344 * changing the window size ourselves. This mostly happens when
3345 * waiting for a character to arrive, quite unpredictably, and may
3346 * change Columns and Rows when we don't want it. Wait for a
3347 * character here to avoid this effect.
3348 * If you remove this, please test this command for resizing
3349 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3350 * Don't do this while starting up though.
3351 * And don't change Rows, it may have be reduced intentionally
3352 * when adding menu/toolbar/tabline. */
3353 if (!gui.starting)
3354 (void)char_avail();
3355 Columns = c;
3356 #endif
3358 #ifdef FEAT_WINDOWS
3359 /* When the console tabline appears or disappears the window positions
3360 * change. */
3361 if (firstwin->w_winrow != tabline_height())
3362 shell_new_rows(); /* recompute window positions and heights */
3363 #endif
3367 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3369 * Return TRUE if the GUI is taking care of the tabline.
3370 * It may still be hidden if 'showtabline' is zero.
3373 gui_use_tabline()
3375 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3379 * Return TRUE if the GUI is showing the tabline.
3380 * This uses 'showtabline'.
3382 static int
3383 gui_has_tabline()
3385 if (!gui_use_tabline()
3386 || p_stal == 0
3387 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3388 return FALSE;
3389 return TRUE;
3393 * Update the tabline.
3394 * This may display/undisplay the tabline and update the labels.
3396 void
3397 gui_update_tabline()
3399 int showit = gui_has_tabline();
3400 int shown = gui_mch_showing_tabline();
3402 if (!gui.starting && starting == 0)
3404 /* Updating the tabline uses direct GUI commands, flush
3405 * outstanding instructions first. (esp. clear screen) */
3406 out_flush();
3407 gui_mch_flush();
3409 if (!showit != !shown)
3410 gui_mch_show_tabline(showit);
3411 if (showit != 0)
3412 gui_mch_update_tabline();
3414 /* When the tabs change from hidden to shown or from shown to
3415 * hidden the size of the text area should remain the same. */
3416 if (!showit != !shown)
3417 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3422 * Get the label or tooltip for tab page "tp" into NameBuff[].
3424 void
3425 get_tabline_label(tp, tooltip)
3426 tabpage_T *tp;
3427 int tooltip; /* TRUE: get tooltip */
3429 int modified = FALSE;
3430 char_u buf[40];
3431 int wincount;
3432 win_T *wp;
3433 char_u **opt;
3435 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3436 opt = (tooltip ? &p_gtt : &p_gtl);
3437 if (**opt != NUL)
3439 int use_sandbox = FALSE;
3440 int save_called_emsg = called_emsg;
3441 char_u res[MAXPATHL];
3442 tabpage_T *save_curtab;
3443 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3444 : "guitablabel");
3446 called_emsg = FALSE;
3448 printer_page_num = tabpage_index(tp);
3449 # ifdef FEAT_EVAL
3450 set_vim_var_nr(VV_LNUM, printer_page_num);
3451 use_sandbox = was_set_insecurely(opt_name, 0);
3452 # endif
3453 /* It's almost as going to the tabpage, but without autocommands. */
3454 curtab->tp_firstwin = firstwin;
3455 curtab->tp_lastwin = lastwin;
3456 curtab->tp_curwin = curwin;
3457 save_curtab = curtab;
3458 curtab = tp;
3459 topframe = curtab->tp_topframe;
3460 firstwin = curtab->tp_firstwin;
3461 lastwin = curtab->tp_lastwin;
3462 curwin = curtab->tp_curwin;
3463 curbuf = curwin->w_buffer;
3465 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3466 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3467 0, (int)Columns, NULL, NULL);
3468 STRCPY(NameBuff, res);
3470 /* Back to the original curtab. */
3471 curtab = save_curtab;
3472 topframe = curtab->tp_topframe;
3473 firstwin = curtab->tp_firstwin;
3474 lastwin = curtab->tp_lastwin;
3475 curwin = curtab->tp_curwin;
3476 curbuf = curwin->w_buffer;
3478 if (called_emsg)
3479 set_string_option_direct(opt_name, -1,
3480 (char_u *)"", OPT_FREE, SID_ERROR);
3481 called_emsg |= save_called_emsg;
3484 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3485 * use a default label. */
3486 if (**opt == NUL || *NameBuff == NUL)
3488 /* Get the buffer name into NameBuff[] and shorten it. */
3489 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3490 if (!tooltip)
3491 shorten_dir(NameBuff);
3493 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3494 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3495 if (bufIsChanged(wp->w_buffer))
3496 modified = TRUE;
3497 if (modified || wincount > 1)
3499 if (wincount > 1)
3500 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3501 else
3502 buf[0] = NUL;
3503 if (modified)
3504 STRCAT(buf, "+");
3505 STRCAT(buf, " ");
3506 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
3507 mch_memmove(NameBuff, buf, STRLEN(buf));
3513 * Send the event for clicking to select tab page "nr".
3514 * Returns TRUE if it was done, FALSE when skipped because we are already at
3515 * that tab page or the cmdline window is open.
3518 send_tabline_event(nr)
3519 int nr;
3521 char_u string[3];
3523 if (nr == tabpage_index(curtab))
3524 return FALSE;
3526 /* Don't put events in the input queue now. */
3527 if (hold_gui_events
3528 # ifdef FEAT_CMDWIN
3529 || cmdwin_type != 0
3530 # endif
3533 /* Set it back to the current tab page. */
3534 gui_mch_set_curtab(tabpage_index(curtab));
3535 return FALSE;
3538 string[0] = CSI;
3539 string[1] = KS_TABLINE;
3540 string[2] = KE_FILLER;
3541 add_to_input_buf(string, 3);
3542 string[0] = nr;
3543 add_to_input_buf_csi(string, 1);
3544 return TRUE;
3548 * Send a tabline menu event
3550 void
3551 send_tabline_menu_event(tabidx, event)
3552 int tabidx;
3553 int event;
3555 char_u string[3];
3557 /* Don't put events in the input queue now. */
3558 if (hold_gui_events)
3559 return;
3561 string[0] = CSI;
3562 string[1] = KS_TABMENU;
3563 string[2] = KE_FILLER;
3564 add_to_input_buf(string, 3);
3565 string[0] = tabidx;
3566 string[1] = (char_u)(long)event;
3567 add_to_input_buf_csi(string, 2);
3570 #endif
3573 * Scrollbar stuff:
3576 #if defined(FEAT_WINDOWS) || defined(PROTO)
3578 * Remove all scrollbars. Used before switching to another tab page.
3580 void
3581 gui_remove_scrollbars()
3583 int i;
3584 win_T *wp;
3586 for (i = 0; i < 3; i++)
3588 if (i == SBAR_BOTTOM)
3589 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3590 else
3592 FOR_ALL_WINDOWS(wp)
3594 gui_do_scrollbar(wp, i, FALSE);
3597 curtab->tp_prev_which_scrollbars[i] = -1;
3600 #endif
3602 void
3603 gui_create_scrollbar(sb, type, wp)
3604 scrollbar_T *sb;
3605 int type;
3606 win_T *wp;
3608 static int sbar_ident = 0;
3610 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3611 sb->wp = wp;
3612 sb->type = type;
3613 sb->value = 0;
3614 #ifdef FEAT_GUI_ATHENA
3615 sb->pixval = 0;
3616 #endif
3617 sb->size = 1;
3618 sb->max = 1;
3619 sb->top = 0;
3620 sb->height = 0;
3621 #ifdef FEAT_VERTSPLIT
3622 sb->width = 0;
3623 #endif
3624 sb->status_height = 0;
3625 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3629 * Find the scrollbar with the given index.
3631 scrollbar_T *
3632 gui_find_scrollbar(ident)
3633 long ident;
3635 win_T *wp;
3637 if (gui.bottom_sbar.ident == ident)
3638 return &gui.bottom_sbar;
3639 FOR_ALL_WINDOWS(wp)
3641 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3642 return &wp->w_scrollbars[SBAR_LEFT];
3643 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3644 return &wp->w_scrollbars[SBAR_RIGHT];
3646 return NULL;
3650 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3652 * For Win32, Macintosh and GTK+ 2:
3653 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3654 * you stop dragging the scrollbar. We get here each time the scrollbar is
3655 * dragged another pixel, but as far as the rest of vim goes, it thinks
3656 * we're just hanging in the call to DispatchMessage() in
3657 * process_message(). The DispatchMessage() call that hangs was passed a
3658 * mouse button click event in the scrollbar window. -- webb.
3660 * Solution: Do the scrolling right here. But only when allowed.
3661 * Ignore the scrollbars while executing an external command or when there
3662 * are still characters to be processed.
3664 void
3665 gui_drag_scrollbar(sb, value, still_dragging)
3666 scrollbar_T *sb;
3667 long value;
3668 int still_dragging;
3670 #ifdef FEAT_WINDOWS
3671 win_T *wp;
3672 #endif
3673 int sb_num;
3674 #ifdef USE_ON_FLY_SCROLL
3675 colnr_T old_leftcol = curwin->w_leftcol;
3676 # ifdef FEAT_SCROLLBIND
3677 linenr_T old_topline = curwin->w_topline;
3678 # endif
3679 # ifdef FEAT_DIFF
3680 int old_topfill = curwin->w_topfill;
3681 # endif
3682 #else
3683 char_u bytes[sizeof(long_u)];
3684 int byte_count;
3685 #endif
3687 if (sb == NULL)
3688 return;
3690 /* Don't put events in the input queue now. */
3691 if (hold_gui_events)
3692 return;
3694 #ifdef FEAT_CMDWIN
3695 if (cmdwin_type != 0 && sb->wp != curwin)
3696 return;
3697 #endif
3699 if (still_dragging)
3701 if (sb->wp == NULL)
3702 gui.dragged_sb = SBAR_BOTTOM;
3703 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3704 gui.dragged_sb = SBAR_LEFT;
3705 else
3706 gui.dragged_sb = SBAR_RIGHT;
3707 gui.dragged_wp = sb->wp;
3709 else
3711 gui.dragged_sb = SBAR_NONE;
3712 #ifdef HAVE_GTK2
3713 /* Keep the "dragged_wp" value until after the scrolling, for when the
3714 * moust button is released. GTK2 doesn't send the button-up event. */
3715 gui.dragged_wp = NULL;
3716 #endif
3719 /* Vertical sbar info is kept in the first sbar (the left one) */
3720 if (sb->wp != NULL)
3721 sb = &sb->wp->w_scrollbars[0];
3724 * Check validity of value
3726 if (value < 0)
3727 value = 0;
3728 #ifdef SCROLL_PAST_END
3729 else if (value > sb->max)
3730 value = sb->max;
3731 #else
3732 if (value > sb->max - sb->size + 1)
3733 value = sb->max - sb->size + 1;
3734 #endif
3736 sb->value = value;
3738 #ifdef USE_ON_FLY_SCROLL
3739 /* When not allowed to do the scrolling right now, return.
3740 * This also checked input_available(), but that causes the first click in
3741 * a scrollbar to be ignored when Vim doesn't have focus. */
3742 if (dont_scroll)
3743 return;
3744 #endif
3745 #ifdef FEAT_INS_EXPAND
3746 /* Disallow scrolling the current window when the completion popup menu is
3747 * visible. */
3748 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3749 return;
3750 #endif
3752 #ifdef FEAT_RIGHTLEFT
3753 if (sb->wp == NULL && curwin->w_p_rl)
3755 value = sb->max + 1 - sb->size - value;
3756 if (value < 0)
3757 value = 0;
3759 #endif
3761 if (sb->wp != NULL) /* vertical scrollbar */
3763 sb_num = 0;
3764 #ifdef FEAT_WINDOWS
3765 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3766 sb_num++;
3767 if (wp == NULL)
3768 return;
3769 #else
3770 if (sb->wp != curwin)
3771 return;
3772 #endif
3774 #ifdef USE_ON_FLY_SCROLL
3775 current_scrollbar = sb_num;
3776 scrollbar_value = value;
3777 if (State & NORMAL)
3779 gui_do_scroll();
3780 setcursor();
3782 else if (State & INSERT)
3784 ins_scroll();
3785 setcursor();
3787 else if (State & CMDLINE)
3789 if (msg_scrolled == 0)
3791 gui_do_scroll();
3792 redrawcmdline();
3795 # ifdef FEAT_FOLDING
3796 /* Value may have been changed for closed fold. */
3797 sb->value = sb->wp->w_topline - 1;
3798 # endif
3800 /* When dragging one scrollbar and there is another one at the other
3801 * side move the thumb of that one too. */
3802 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3803 gui_mch_set_scrollbar_thumb(
3804 &sb->wp->w_scrollbars[
3805 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3806 ? SBAR_LEFT : SBAR_RIGHT],
3807 sb->value, sb->size, sb->max);
3809 #else
3810 bytes[0] = CSI;
3811 bytes[1] = KS_VER_SCROLLBAR;
3812 bytes[2] = KE_FILLER;
3813 bytes[3] = (char_u)sb_num;
3814 byte_count = 4;
3815 #endif
3817 else
3819 #ifdef USE_ON_FLY_SCROLL
3820 scrollbar_value = value;
3822 if (State & NORMAL)
3823 gui_do_horiz_scroll();
3824 else if (State & INSERT)
3825 ins_horscroll();
3826 else if (State & CMDLINE)
3828 if (msg_scrolled == 0)
3830 gui_do_horiz_scroll();
3831 redrawcmdline();
3834 if (old_leftcol != curwin->w_leftcol)
3836 updateWindow(curwin); /* update window, status and cmdline */
3837 setcursor();
3839 #else
3840 bytes[0] = CSI;
3841 bytes[1] = KS_HOR_SCROLLBAR;
3842 bytes[2] = KE_FILLER;
3843 byte_count = 3;
3844 #endif
3847 #ifdef USE_ON_FLY_SCROLL
3848 # ifdef FEAT_SCROLLBIND
3850 * synchronize other windows, as necessary according to 'scrollbind'
3852 if (curwin->w_p_scb
3853 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3854 || (sb->wp == curwin && (curwin->w_topline != old_topline
3855 # ifdef FEAT_DIFF
3856 || curwin->w_topfill != old_topfill
3857 # endif
3858 ))))
3860 do_check_scrollbind(TRUE);
3861 /* need to update the window right here */
3862 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3863 if (wp->w_redr_type > 0)
3864 updateWindow(wp);
3865 setcursor();
3867 # endif
3868 out_flush();
3869 gui_update_cursor(FALSE, TRUE);
3870 #else
3871 add_to_input_buf(bytes, byte_count);
3872 add_long_to_buf((long_u)value, bytes);
3873 add_to_input_buf_csi(bytes, sizeof(long_u));
3874 #endif
3878 * Scrollbar stuff:
3881 void
3882 gui_update_scrollbars(force)
3883 int force; /* Force all scrollbars to get updated */
3885 win_T *wp;
3886 scrollbar_T *sb;
3887 long val, size, max; /* need 32 bits here */
3888 int which_sb;
3889 int h, y;
3890 #ifdef FEAT_VERTSPLIT
3891 static win_T *prev_curwin = NULL;
3892 #endif
3894 /* Update the horizontal scrollbar */
3895 gui_update_horiz_scrollbar(force);
3897 #ifndef WIN3264
3898 /* Return straight away if there is neither a left nor right scrollbar.
3899 * On MS-Windows this is required anyway for scrollwheel messages. */
3900 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3901 return;
3902 #endif
3905 * Don't want to update a scrollbar while we're dragging it. But if we
3906 * have both a left and right scrollbar, and we drag one of them, we still
3907 * need to update the other one.
3909 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3910 && gui.which_scrollbars[SBAR_LEFT]
3911 && gui.which_scrollbars[SBAR_RIGHT])
3914 * If we have two scrollbars and one of them is being dragged, just
3915 * copy the scrollbar position from the dragged one to the other one.
3917 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3918 if (gui.dragged_wp != NULL)
3919 gui_mch_set_scrollbar_thumb(
3920 &gui.dragged_wp->w_scrollbars[which_sb],
3921 gui.dragged_wp->w_scrollbars[0].value,
3922 gui.dragged_wp->w_scrollbars[0].size,
3923 gui.dragged_wp->w_scrollbars[0].max);
3926 /* avoid that moving components around generates events */
3927 ++hold_gui_events;
3929 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3931 if (wp->w_buffer == NULL) /* just in case */
3932 continue;
3933 /* Skip a scrollbar that is being dragged. */
3934 if (!force && (gui.dragged_sb == SBAR_LEFT
3935 || gui.dragged_sb == SBAR_RIGHT)
3936 && gui.dragged_wp == wp)
3937 continue;
3939 #ifdef SCROLL_PAST_END
3940 max = wp->w_buffer->b_ml.ml_line_count - 1;
3941 #else
3942 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3943 #endif
3944 if (max < 0) /* empty buffer */
3945 max = 0;
3946 val = wp->w_topline - 1;
3947 size = wp->w_height;
3948 #ifdef SCROLL_PAST_END
3949 if (val > max) /* just in case */
3950 val = max;
3951 #else
3952 if (size > max + 1) /* just in case */
3953 size = max + 1;
3954 if (val > max - size + 1)
3955 val = max - size + 1;
3956 #endif
3957 if (val < 0) /* minimal value is 0 */
3958 val = 0;
3961 * Scrollbar at index 0 (the left one) contains all the information.
3962 * It would be the same info for left and right so we just store it for
3963 * one of them.
3965 sb = &wp->w_scrollbars[0];
3968 * Note: no check for valid w_botline. If it's not valid the
3969 * scrollbars will be updated later anyway.
3971 if (size < 1 || wp->w_botline - 2 > max)
3974 * This can happen during changing files. Just don't update the
3975 * scrollbar for now.
3977 sb->height = 0; /* Force update next time */
3978 if (gui.which_scrollbars[SBAR_LEFT])
3979 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3980 if (gui.which_scrollbars[SBAR_RIGHT])
3981 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3982 continue;
3984 if (force || sb->height != wp->w_height
3985 #ifdef FEAT_WINDOWS
3986 || sb->top != wp->w_winrow
3987 || sb->status_height != wp->w_status_height
3988 # ifdef FEAT_VERTSPLIT
3989 || sb->width != wp->w_width
3990 || prev_curwin != curwin
3991 # endif
3992 #endif
3995 /* Height, width or position of scrollbar has changed. For
3996 * vertical split: curwin changed. */
3997 sb->height = wp->w_height;
3998 #ifdef FEAT_WINDOWS
3999 sb->top = wp->w_winrow;
4000 sb->status_height = wp->w_status_height;
4001 # ifdef FEAT_VERTSPLIT
4002 sb->width = wp->w_width;
4003 # endif
4004 #endif
4006 /* Calculate height and position in pixels */
4007 h = (sb->height + sb->status_height) * gui.char_height;
4008 y = sb->top * gui.char_height + gui.border_offset;
4009 #if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4010 if (gui.menu_is_active)
4011 y += gui.menu_height;
4012 #endif
4014 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4015 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4016 # ifdef FEAT_GUI_ATHENA
4017 y += gui.toolbar_height;
4018 # else
4019 # ifdef FEAT_GUI_MSWIN
4020 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4021 # endif
4022 # endif
4023 #endif
4025 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4026 if (gui_has_tabline())
4027 y += gui.tabline_height;
4028 #endif
4030 #ifdef FEAT_WINDOWS
4031 if (wp->w_winrow == 0)
4032 #endif
4034 /* Height of top scrollbar includes width of top border */
4035 h += gui.border_offset;
4036 y -= gui.border_offset;
4038 if (gui.which_scrollbars[SBAR_LEFT])
4040 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4041 gui.left_sbar_x, y,
4042 gui.scrollbar_width, h);
4043 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4045 if (gui.which_scrollbars[SBAR_RIGHT])
4047 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4048 gui.right_sbar_x, y,
4049 gui.scrollbar_width, h);
4050 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4054 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4055 * checking if the thumb moved at least a pixel. Only do this for
4056 * Athena, most other GUIs require the update anyway to make the
4057 * arrows work. */
4058 #ifdef FEAT_GUI_ATHENA
4059 if (max == 0)
4060 y = 0;
4061 else
4062 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4063 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4064 #else
4065 if (force || sb->value != val || sb->size != size || sb->max != max)
4066 #endif
4068 /* Thumb of scrollbar has moved */
4069 sb->value = val;
4070 #ifdef FEAT_GUI_ATHENA
4071 sb->pixval = y;
4072 #endif
4073 sb->size = size;
4074 sb->max = max;
4075 if (gui.which_scrollbars[SBAR_LEFT]
4076 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4077 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4078 val, size, max);
4079 if (gui.which_scrollbars[SBAR_RIGHT]
4080 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4081 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4082 val, size, max);
4085 #ifdef FEAT_VERTSPLIT
4086 prev_curwin = curwin;
4087 #endif
4088 --hold_gui_events;
4092 * Enable or disable a scrollbar.
4093 * Check for scrollbars for vertically split windows which are not enabled
4094 * sometimes.
4096 static void
4097 gui_do_scrollbar(wp, which, enable)
4098 win_T *wp;
4099 int which; /* SBAR_LEFT or SBAR_RIGHT */
4100 int enable; /* TRUE to enable scrollbar */
4102 #ifdef FEAT_VERTSPLIT
4103 int midcol = curwin->w_wincol + curwin->w_width / 2;
4104 int has_midcol = (wp->w_wincol <= midcol
4105 && wp->w_wincol + wp->w_width >= midcol);
4107 /* Only enable scrollbars that contain the middle column of the current
4108 * window. */
4109 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4111 /* Scrollbars only on one side. Don't enable scrollbars that don't
4112 * contain the middle column of the current window. */
4113 if (!has_midcol)
4114 enable = FALSE;
4116 else
4118 /* Scrollbars on both sides. Don't enable scrollbars that neither
4119 * contain the middle column of the current window nor are on the far
4120 * side. */
4121 if (midcol > Columns / 2)
4123 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4124 enable = FALSE;
4126 else
4128 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4129 : !has_midcol)
4130 enable = FALSE;
4133 #endif
4134 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4138 * Scroll a window according to the values set in the globals current_scrollbar
4139 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4140 * or FALSE otherwise.
4143 gui_do_scroll()
4145 win_T *wp, *save_wp;
4146 int i;
4147 long nlines;
4148 pos_T old_cursor;
4149 linenr_T old_topline;
4150 #ifdef FEAT_DIFF
4151 int old_topfill;
4152 #endif
4154 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4155 if (wp == NULL)
4156 break;
4157 if (wp == NULL)
4158 /* Couldn't find window */
4159 return FALSE;
4162 * Compute number of lines to scroll. If zero, nothing to do.
4164 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4165 if (nlines == 0)
4166 return FALSE;
4168 save_wp = curwin;
4169 old_topline = wp->w_topline;
4170 #ifdef FEAT_DIFF
4171 old_topfill = wp->w_topfill;
4172 #endif
4173 old_cursor = wp->w_cursor;
4174 curwin = wp;
4175 curbuf = wp->w_buffer;
4176 if (nlines < 0)
4177 scrolldown(-nlines, gui.dragged_wp == NULL);
4178 else
4179 scrollup(nlines, gui.dragged_wp == NULL);
4180 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4181 * the mouse-up event already, but we still want it to behave like when
4182 * dragging. But not the next click in an arrow. */
4183 if (gui.dragged_sb == SBAR_NONE)
4184 gui.dragged_wp = NULL;
4186 if (old_topline != wp->w_topline
4187 #ifdef FEAT_DIFF
4188 || old_topfill != wp->w_topfill
4189 #endif
4192 if (p_so != 0)
4194 cursor_correct(); /* fix window for 'so' */
4195 update_topline(); /* avoid up/down jump */
4197 if (old_cursor.lnum != wp->w_cursor.lnum)
4198 coladvance(wp->w_curswant);
4199 #ifdef FEAT_SCROLLBIND
4200 wp->w_scbind_pos = wp->w_topline;
4201 #endif
4204 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4205 validate_cursor();
4207 curwin = save_wp;
4208 curbuf = save_wp->w_buffer;
4211 * Don't call updateWindow() when nothing has changed (it will overwrite
4212 * the status line!).
4214 if (old_topline != wp->w_topline
4215 || wp->w_redr_type != 0
4216 #ifdef FEAT_DIFF
4217 || old_topfill != wp->w_topfill
4218 #endif
4221 int type = VALID;
4223 #ifdef FEAT_INS_EXPAND
4224 if (pum_visible())
4226 type = NOT_VALID;
4227 wp->w_lines_valid = 0;
4229 #endif
4230 /* Don't set must_redraw here, it may cause the popup menu to
4231 * disappear when losing focus after a scrollbar drag. */
4232 if (wp->w_redr_type < type)
4233 wp->w_redr_type = type;
4234 updateWindow(wp); /* update window, status line, and cmdline */
4237 #ifdef FEAT_INS_EXPAND
4238 /* May need to redraw the popup menu. */
4239 if (pum_visible())
4240 pum_redraw();
4241 #endif
4243 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4248 * Horizontal scrollbar stuff:
4252 * Return length of line "lnum" for horizontal scrolling.
4254 static colnr_T
4255 scroll_line_len(lnum)
4256 linenr_T lnum;
4258 char_u *p;
4259 colnr_T col;
4260 int w;
4262 p = ml_get(lnum);
4263 col = 0;
4264 if (*p != NUL)
4265 for (;;)
4267 w = chartabsize(p, col);
4268 mb_ptr_adv(p);
4269 if (*p == NUL) /* don't count the last character */
4270 break;
4271 col += w;
4273 return col;
4276 /* Remember which line is currently the longest, so that we don't have to
4277 * search for it when scrolling horizontally. */
4278 static linenr_T longest_lnum = 0;
4280 static void
4281 gui_update_horiz_scrollbar(force)
4282 int force;
4284 long value, size, max; /* need 32 bit ints here */
4286 if (!gui.which_scrollbars[SBAR_BOTTOM])
4287 return;
4289 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4290 return;
4292 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4293 return;
4296 * It is possible for the cursor to be invalid if we're in the middle of
4297 * something (like changing files). If so, don't do anything for now.
4299 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4301 gui.bottom_sbar.value = -1;
4302 return;
4305 size = W_WIDTH(curwin);
4306 if (curwin->w_p_wrap)
4308 value = 0;
4309 #ifdef SCROLL_PAST_END
4310 max = 0;
4311 #else
4312 max = W_WIDTH(curwin) - 1;
4313 #endif
4315 else
4317 value = curwin->w_leftcol;
4319 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4320 * line numbers, topline and botline can be invalid when displaying is
4321 * postponed. */
4322 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4323 && curwin->w_topline <= curwin->w_cursor.lnum
4324 && curwin->w_botline > curwin->w_cursor.lnum
4325 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4327 linenr_T lnum;
4328 colnr_T n;
4330 /* Use maximum of all visible lines. Remember the lnum of the
4331 * longest line, clostest to the cursor line. Used when scrolling
4332 * below. */
4333 max = 0;
4334 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4336 n = scroll_line_len(lnum);
4337 if (n > (colnr_T)max)
4339 max = n;
4340 longest_lnum = lnum;
4342 else if (n == (colnr_T)max
4343 && abs((int)(lnum - curwin->w_cursor.lnum))
4344 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4345 longest_lnum = lnum;
4348 else
4349 /* Use cursor line only. */
4350 max = scroll_line_len(curwin->w_cursor.lnum);
4351 #ifdef FEAT_VIRTUALEDIT
4352 if (virtual_active())
4354 /* May move the cursor even further to the right. */
4355 if (curwin->w_virtcol >= (colnr_T)max)
4356 max = curwin->w_virtcol;
4358 #endif
4360 #ifndef SCROLL_PAST_END
4361 max += W_WIDTH(curwin) - 1;
4362 #endif
4363 /* The line number isn't scrolled, thus there is less space when
4364 * 'number' is set (also for 'foldcolumn'). */
4365 size -= curwin_col_off();
4366 #ifndef SCROLL_PAST_END
4367 max -= curwin_col_off();
4368 #endif
4371 #ifndef SCROLL_PAST_END
4372 if (value > max - size + 1)
4373 value = max - size + 1; /* limit the value to allowable range */
4374 #endif
4376 #ifdef FEAT_RIGHTLEFT
4377 if (curwin->w_p_rl)
4379 value = max + 1 - size - value;
4380 if (value < 0)
4382 size += value;
4383 value = 0;
4386 #endif
4387 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4388 && max == gui.bottom_sbar.max)
4389 return;
4391 gui.bottom_sbar.value = value;
4392 gui.bottom_sbar.size = size;
4393 gui.bottom_sbar.max = max;
4394 gui.prev_wrap = curwin->w_p_wrap;
4396 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4400 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4403 gui_do_horiz_scroll()
4405 /* no wrapping, no scrolling */
4406 if (curwin->w_p_wrap)
4407 return FALSE;
4409 if (curwin->w_leftcol == scrollbar_value)
4410 return FALSE;
4412 curwin->w_leftcol = (colnr_T)scrollbar_value;
4414 /* When the line of the cursor is too short, move the cursor to the
4415 * longest visible line. Do a sanity check on "longest_lnum", just in
4416 * case. */
4417 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4418 && longest_lnum >= curwin->w_topline
4419 && longest_lnum < curwin->w_botline
4420 && !virtual_active())
4422 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4424 curwin->w_cursor.lnum = longest_lnum;
4425 curwin->w_cursor.col = 0;
4429 return leftcol_changed();
4433 * Check that none of the colors are the same as the background color
4435 void
4436 gui_check_colors()
4438 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4440 gui_set_bg_color((char_u *)"White");
4441 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4442 gui_set_fg_color((char_u *)"Black");
4446 static void
4447 gui_set_fg_color(name)
4448 char_u *name;
4450 gui.norm_pixel = gui_get_color(name);
4451 hl_set_fg_color_name(vim_strsave(name));
4454 static void
4455 gui_set_bg_color(name)
4456 char_u *name;
4458 gui.back_pixel = gui_get_color(name);
4459 hl_set_bg_color_name(vim_strsave(name));
4463 * Allocate a color by name.
4464 * Returns INVALCOLOR and gives an error message when failed.
4466 guicolor_T
4467 gui_get_color(name)
4468 char_u *name;
4470 guicolor_T t;
4472 if (*name == NUL)
4473 return INVALCOLOR;
4474 t = gui_mch_get_color(name);
4476 if (t == INVALCOLOR
4477 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4478 && gui.in_use
4479 #endif
4481 EMSG2(_("E254: Cannot allocate color %s"), name);
4482 return t;
4486 * Return the grey value of a color (range 0-255).
4489 gui_get_lightness(pixel)
4490 guicolor_T pixel;
4492 long_u rgb = gui_mch_get_rgb(pixel);
4494 return (int)( (((rgb >> 16) & 0xff) * 299)
4495 + (((rgb >> 8) & 0xff) * 587)
4496 + ((rgb & 0xff) * 114)) / 1000;
4499 #if defined(FEAT_GUI_X11) || defined(PROTO)
4500 void
4501 gui_new_scrollbar_colors()
4503 win_T *wp;
4505 /* Nothing to do if GUI hasn't started yet. */
4506 if (!gui.in_use)
4507 return;
4509 FOR_ALL_WINDOWS(wp)
4511 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4512 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4514 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4516 #endif
4519 * Call this when focus has changed.
4521 void
4522 gui_focus_change(in_focus)
4523 int in_focus;
4526 * Skip this code to avoid drawing the cursor when debugging and switching
4527 * between the debugger window and gvim.
4529 #if 1
4530 gui.in_focus = in_focus;
4531 out_flush(); /* make sure output has been written */
4532 gui_update_cursor(TRUE, FALSE);
4534 # ifdef FEAT_XIM
4535 xim_set_focus(in_focus);
4536 # endif
4538 /* Put events in the input queue only when allowed.
4539 * ui_focus_change() isn't called directly, because it invokes
4540 * autocommands and that must not happen asynchronously. */
4541 if (!hold_gui_events)
4543 char_u bytes[3];
4545 bytes[0] = CSI;
4546 bytes[1] = KS_EXTRA;
4547 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4548 add_to_input_buf(bytes, 3);
4550 #endif
4554 * Called when the mouse moved (but not when dragging).
4556 void
4557 gui_mouse_moved(x, y)
4558 int x;
4559 int y;
4561 win_T *wp;
4562 char_u st[8];
4564 /* Ignore this while still starting up. */
4565 if (!gui.in_use || gui.starting)
4566 return;
4568 #ifdef FEAT_MOUSESHAPE
4569 /* Get window pointer, and update mouse shape as well. */
4570 wp = xy2win(x, y);
4571 #endif
4573 /* Only handle this when 'mousefocus' set and ... */
4574 if (p_mousef
4575 && !hold_gui_events /* not holding events */
4576 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4577 && State != HITRETURN /* but not hit-return prompt */
4578 && msg_scrolled == 0 /* no scrolled message */
4579 && !need_mouse_correct /* not moving the pointer */
4580 && gui.in_focus) /* gvim in focus */
4582 /* Don't move the mouse when it's left or right of the Vim window */
4583 if (x < 0 || x > Columns * gui.char_width)
4584 return;
4585 #ifndef FEAT_MOUSESHAPE
4586 wp = xy2win(x, y);
4587 #endif
4588 if (wp == curwin || wp == NULL)
4589 return; /* still in the same old window, or none at all */
4591 #ifdef FEAT_WINDOWS
4592 /* Ignore position in the tab pages line. */
4593 if (Y_2_ROW(y) < tabline_height())
4594 return;
4595 #endif
4598 * format a mouse click on status line input
4599 * ala gui_send_mouse_event(0, x, y, 0, 0);
4600 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4601 * generate a K_LEFTMOUSE_NM key code.
4603 if (finish_op)
4605 /* abort the current operator first */
4606 st[0] = ESC;
4607 add_to_input_buf(st, 1);
4609 st[0] = CSI;
4610 st[1] = KS_MOUSE;
4611 st[2] = KE_FILLER;
4612 st[3] = (char_u)MOUSE_LEFT;
4613 fill_mouse_coord(st + 4,
4614 #ifdef FEAT_VERTSPLIT
4615 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4616 #else
4618 #endif
4619 wp->w_height + W_WINROW(wp));
4621 add_to_input_buf(st, 8);
4622 st[3] = (char_u)MOUSE_RELEASE;
4623 add_to_input_buf(st, 8);
4624 #ifdef FEAT_GUI_GTK
4625 /* Need to wake up the main loop */
4626 if (gtk_main_level() > 0)
4627 gtk_main_quit();
4628 #endif
4633 * Called when mouse should be moved to window with focus.
4635 void
4636 gui_mouse_correct()
4638 int x, y;
4639 win_T *wp = NULL;
4641 need_mouse_correct = FALSE;
4643 if (!(gui.in_use && p_mousef))
4644 return;
4646 gui_mch_getmouse(&x, &y);
4647 /* Don't move the mouse when it's left or right of the Vim window */
4648 if (x < 0 || x > Columns * gui.char_width)
4649 return;
4650 if (y >= 0
4651 # ifdef FEAT_WINDOWS
4652 && Y_2_ROW(y) >= tabline_height()
4653 # endif
4655 wp = xy2win(x, y);
4656 if (wp != curwin && wp != NULL) /* If in other than current window */
4658 validate_cline_row();
4659 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4660 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4661 + (gui.char_height) / 2);
4666 * Find window where the mouse pointer "y" coordinate is in.
4668 /*ARGSUSED*/
4669 static win_T *
4670 xy2win(x, y)
4671 int x;
4672 int y;
4674 #ifdef FEAT_WINDOWS
4675 int row;
4676 int col;
4677 win_T *wp;
4679 row = Y_2_ROW(y);
4680 col = X_2_COL(x);
4681 if (row < 0 || col < 0) /* before first window */
4682 return NULL;
4683 wp = mouse_find_win(&row, &col);
4684 # ifdef FEAT_MOUSESHAPE
4685 if (State == HITRETURN || State == ASKMORE)
4687 if (Y_2_ROW(y) >= msg_row)
4688 update_mouseshape(SHAPE_IDX_MOREL);
4689 else
4690 update_mouseshape(SHAPE_IDX_MORE);
4692 else if (row > wp->w_height) /* below status line */
4693 update_mouseshape(SHAPE_IDX_CLINE);
4694 # ifdef FEAT_VERTSPLIT
4695 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4696 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4697 update_mouseshape(SHAPE_IDX_VSEP);
4698 # endif
4699 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4700 && row == wp->w_height && msg_scrolled == 0)
4701 update_mouseshape(SHAPE_IDX_STATUS);
4702 else
4703 update_mouseshape(-2);
4704 # endif
4705 return wp;
4706 #else
4707 return firstwin;
4708 #endif
4712 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4713 * File names may be given to redefine the args list.
4715 void
4716 ex_gui(eap)
4717 exarg_T *eap;
4719 char_u *arg = eap->arg;
4722 * Check for "-f" argument: foreground, don't fork.
4723 * Also don't fork when started with "gvim -f".
4724 * Do fork when using "gui -b".
4726 if (arg[0] == '-'
4727 && (arg[1] == 'f' || arg[1] == 'b')
4728 && (arg[2] == NUL || vim_iswhite(arg[2])))
4730 gui.dofork = (arg[1] == 'b');
4731 eap->arg = skipwhite(eap->arg + 2);
4733 if (!gui.in_use)
4735 /* Clear the command. Needed for when forking+exiting, to avoid part
4736 * of the argument ending up after the shell prompt. */
4737 msg_clr_eos_force();
4738 gui_start();
4740 if (!ends_excmd(*eap->arg))
4741 ex_next(eap);
4744 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4745 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4747 * This is shared between Athena, Motif and GTK.
4749 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4752 * Callback function for do_in_runtimepath().
4754 static void
4755 gfp_setname(fname, cookie)
4756 char_u *fname;
4757 void *cookie;
4759 char_u *gfp_buffer = cookie;
4761 if (STRLEN(fname) >= MAXPATHL)
4762 *gfp_buffer = NUL;
4763 else
4764 STRCPY(gfp_buffer, fname);
4768 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4769 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4772 gui_find_bitmap(name, buffer, ext)
4773 char_u *name;
4774 char_u *buffer;
4775 char *ext;
4777 if (STRLEN(name) > MAXPATHL - 14)
4778 return FAIL;
4779 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4780 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4781 || *buffer == NUL)
4782 return FAIL;
4783 return OK;
4786 # if !defined(HAVE_GTK2) || defined(PROTO)
4788 * Given the name of the "icon=" argument, try finding the bitmap file for the
4789 * icon. If it is an absolute path name, use it as it is. Otherwise append
4790 * "ext" and search for it in 'runtimepath'.
4791 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4792 * contains "name".
4794 void
4795 gui_find_iconfile(name, buffer, ext)
4796 char_u *name;
4797 char_u *buffer;
4798 char *ext;
4800 char_u buf[MAXPATHL + 1];
4802 expand_env(name, buffer, MAXPATHL);
4803 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4804 STRCPY(buffer, buf);
4806 # endif
4807 #endif
4809 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4810 void
4811 display_errors()
4813 char_u *p;
4815 if (isatty(2))
4816 fflush(stderr);
4817 else if (error_ga.ga_data != NULL)
4819 /* avoid putting up a message box with blanks only */
4820 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4821 if (!isspace(*p))
4823 /* Truncate a very long message, it will go off-screen. */
4824 if (STRLEN(p) > 2000)
4825 STRCPY(p + 2000 - 14, "...(truncated)");
4826 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4827 p, (char_u *)_("&Ok"), 1, NULL);
4828 break;
4830 ga_clear(&error_ga);
4833 #endif
4835 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4837 * Return TRUE if still starting up and there is no place to enter text.
4838 * For GTK and X11 we check if stderr is not a tty, which means we were
4839 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4840 * allow typing on stdin.
4843 no_console_input()
4845 return ((!gui.in_use || gui.starting)
4846 # ifndef NO_CONSOLE
4847 && !isatty(0) && !isatty(2)
4848 # endif
4851 #endif
4853 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4854 || defined(NEED_GUI_UPDATE_SCREEN) \
4855 || defined(PROTO)
4857 * Update the current window and the screen.
4859 void
4860 gui_update_screen()
4862 update_topline();
4863 validate_cursor();
4864 #ifdef FEAT_AUTOCMD
4865 /* Trigger CursorMoved if the cursor moved. */
4866 if (!finish_op && has_cursormoved()
4867 && !equalpos(last_cursormoved, curwin->w_cursor))
4869 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4870 last_cursormoved = curwin->w_cursor;
4872 #endif
4873 update_screen(0); /* may need to update the screen */
4874 setcursor();
4875 out_flush(); /* make sure output has been written */
4876 gui_update_cursor(TRUE, FALSE);
4877 gui_mch_flush();
4879 #endif
4881 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4882 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4885 * Get the text to use in a find/replace dialog. Uses the last search pattern
4886 * if the argument is empty.
4887 * Returns an allocated string.
4889 char_u *
4890 get_find_dialog_text(arg, wwordp, mcasep)
4891 char_u *arg;
4892 int *wwordp; /* return: TRUE if \< \> found */
4893 int *mcasep; /* return: TRUE if \C found */
4895 char_u *text;
4897 if (*arg == NUL)
4898 text = last_search_pat();
4899 else
4900 text = arg;
4901 if (text != NULL)
4903 text = vim_strsave(text);
4904 if (text != NULL)
4906 int len = (int)STRLEN(text);
4907 int i;
4909 /* Remove "\V" */
4910 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4912 mch_memmove(text, text + 2, (size_t)(len - 1));
4913 len -= 2;
4916 /* Recognize "\c" and "\C" and remove. */
4917 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4919 *mcasep = (text[1] == 'C');
4920 mch_memmove(text, text + 2, (size_t)(len - 1));
4921 len -= 2;
4924 /* Recognize "\<text\>" and remove. */
4925 if (len >= 4
4926 && STRNCMP(text, "\\<", 2) == 0
4927 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4929 *wwordp = TRUE;
4930 mch_memmove(text, text + 2, (size_t)(len - 4));
4931 text[len - 4] = NUL;
4934 /* Recognize "\/" or "\?" and remove. */
4935 for (i = 0; i + 1 < len; ++i)
4936 if (text[i] == '\\' && (text[i + 1] == '/'
4937 || text[i + 1] == '?'))
4939 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4940 --len;
4944 return text;
4948 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4950 static void
4951 concat_esc(gap, text, what)
4952 garray_T *gap;
4953 char_u *text;
4954 int what;
4956 while (*text != NUL)
4958 #ifdef FEAT_MBYTE
4959 int l = (*mb_ptr2len)(text);
4961 if (l > 1)
4963 while (--l >= 0)
4964 ga_append(gap, *text++);
4965 continue;
4967 #endif
4968 if (*text == what)
4969 ga_append(gap, '\\');
4970 ga_append(gap, *text);
4971 ++text;
4976 * Handle the press of a button in the find-replace dialog.
4977 * Return TRUE when something was added to the input buffer.
4980 gui_do_findrepl(flags, find_text, repl_text, down)
4981 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4982 char_u *find_text;
4983 char_u *repl_text;
4984 int down; /* Search downwards. */
4986 garray_T ga;
4987 int i;
4988 int type = (flags & FRD_TYPE_MASK);
4989 char_u *p;
4990 regmatch_T regmatch;
4991 int save_did_emsg = did_emsg;
4993 ga_init2(&ga, 1, 100);
4994 if (type == FRD_REPLACEALL)
4995 ga_concat(&ga, (char_u *)"%s/");
4997 ga_concat(&ga, (char_u *)"\\V");
4998 if (flags & FRD_MATCH_CASE)
4999 ga_concat(&ga, (char_u *)"\\C");
5000 else
5001 ga_concat(&ga, (char_u *)"\\c");
5002 if (flags & FRD_WHOLE_WORD)
5003 ga_concat(&ga, (char_u *)"\\<");
5004 if (type == FRD_REPLACEALL || down)
5005 concat_esc(&ga, find_text, '/'); /* escape slashes */
5006 else
5007 concat_esc(&ga, find_text, '?'); /* escape '?' */
5008 if (flags & FRD_WHOLE_WORD)
5009 ga_concat(&ga, (char_u *)"\\>");
5011 if (type == FRD_REPLACEALL)
5013 ga_concat(&ga, (char_u *)"/");
5014 /* escape / and \ */
5015 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5016 if (p != NULL)
5017 ga_concat(&ga, p);
5018 vim_free(p);
5019 ga_concat(&ga, (char_u *)"/g");
5021 ga_append(&ga, NUL);
5023 if (type == FRD_REPLACE)
5025 /* Do the replacement when the text at the cursor matches. Thus no
5026 * replacement is done if the cursor was moved! */
5027 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5028 regmatch.rm_ic = 0;
5029 if (regmatch.regprog != NULL)
5031 p = ml_get_cursor();
5032 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5033 && regmatch.startp[0] == p)
5035 /* Clear the command line to remove any old "No match"
5036 * error. */
5037 msg_end_prompt();
5039 if (u_save_cursor() == OK)
5041 /* A button was pressed thus undo should be synced. */
5042 u_sync(FALSE);
5044 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5045 FALSE, FALSE);
5046 ins_str(repl_text);
5049 else
5050 MSG(_("No match at cursor, finding next"));
5051 vim_free(regmatch.regprog);
5055 if (type == FRD_REPLACEALL)
5057 /* A button was pressed, thus undo should be synced. */
5058 u_sync(FALSE);
5059 do_cmdline_cmd(ga.ga_data);
5061 else
5063 /* Search for the next match. */
5064 i = msg_scroll;
5065 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5066 SEARCH_MSG + SEARCH_MARK, NULL);
5067 msg_scroll = i; /* don't let an error message set msg_scroll */
5070 /* Don't want to pass did_emsg to other code, it may cause disabling
5071 * syntax HL if we were busy redrawing. */
5072 did_emsg = save_did_emsg;
5074 if (State & (NORMAL | INSERT))
5076 gui_update_screen(); /* update the screen */
5077 msg_didout = 0; /* overwrite any message */
5078 need_wait_return = FALSE; /* don't wait for return */
5081 vim_free(ga.ga_data);
5082 return (ga.ga_len > 0);
5085 #endif
5087 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5088 || defined(FEAT_GUI_MSWIN) \
5089 || defined(FEAT_GUI_MAC) \
5090 || defined(PROTO)
5092 #ifdef FEAT_WINDOWS
5093 static void gui_wingoto_xy __ARGS((int x, int y));
5096 * Jump to the window at specified point (x, y).
5098 static void
5099 gui_wingoto_xy(x, y)
5100 int x;
5101 int y;
5103 int row = Y_2_ROW(y);
5104 int col = X_2_COL(x);
5105 win_T *wp;
5107 if (row >= 0 && col >= 0)
5109 wp = mouse_find_win(&row, &col);
5110 if (wp != NULL && wp != curwin)
5111 win_goto(wp);
5114 #endif
5117 * Process file drop. Mouse cursor position, key modifiers, name of files
5118 * and count of files are given. Argument "fnames[count]" has full pathnames
5119 * of dropped files, they will be freed in this function, and caller can't use
5120 * fnames after call this function.
5122 /*ARGSUSED*/
5123 void
5124 gui_handle_drop(x, y, modifiers, fnames, count)
5125 int x;
5126 int y;
5127 int_u modifiers;
5128 char_u **fnames;
5129 int count;
5131 int i;
5132 char_u *p;
5133 static int entered = FALSE;
5136 * This function is called by event handlers. Just in case we get a
5137 * second event before the first one is handled, ignore the second one.
5138 * Not sure if this can ever happen, just in case.
5140 if (entered)
5141 return;
5142 entered = TRUE;
5145 * When the cursor is at the command line, add the file names to the
5146 * command line, don't edit the files.
5148 if (State & CMDLINE)
5150 shorten_filenames(fnames, count);
5151 for (i = 0; i < count; ++i)
5153 if (fnames[i] != NULL)
5155 if (i > 0)
5156 add_to_input_buf((char_u*)" ", 1);
5158 /* We don't know what command is used thus we can't be sure
5159 * about which characters need to be escaped. Only escape the
5160 * most common ones. */
5161 # ifdef BACKSLASH_IN_FILENAME
5162 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5163 # else
5164 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5165 # endif
5166 if (p != NULL)
5167 add_to_input_buf_csi(p, (int)STRLEN(p));
5168 vim_free(p);
5169 vim_free(fnames[i]);
5172 vim_free(fnames);
5174 else
5176 /* Go to the window under mouse cursor, then shorten given "fnames" by
5177 * current window, because a window can have local current dir. */
5178 # ifdef FEAT_WINDOWS
5179 gui_wingoto_xy(x, y);
5180 # endif
5181 shorten_filenames(fnames, count);
5183 /* If Shift held down, remember the first item. */
5184 if ((modifiers & MOUSE_SHIFT) != 0)
5185 p = vim_strsave(fnames[0]);
5186 else
5187 p = NULL;
5189 /* Handle the drop, :edit or :split to get to the file. This also
5190 * frees fnames[]. Skip this if there is only one item it's a
5191 * directory and Shift is held down. */
5192 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5193 && mch_isdir(fnames[0]))
5195 vim_free(fnames[0]);
5196 vim_free(fnames);
5198 else
5199 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5201 /* If Shift held down, change to first file's directory. If the first
5202 * item is a directory, change to that directory (and let the explorer
5203 * plugin show the contents). */
5204 if (p != NULL)
5206 if (mch_isdir(p))
5208 if (mch_chdir((char *)p) == 0)
5209 shorten_fnames(TRUE);
5211 else if (vim_chdirfile(p) == OK)
5212 shorten_fnames(TRUE);
5213 vim_free(p);
5216 /* Update the screen display */
5217 update_screen(NOT_VALID);
5218 # ifdef FEAT_MENU
5219 gui_update_menus(0);
5220 # endif
5221 setcursor();
5222 out_flush();
5223 gui_update_cursor(FALSE, FALSE);
5224 gui_mch_flush();
5227 entered = FALSE;
5229 #endif