Add fork support
[MacVim.git] / src / gui.c
blob581af3d29680bc6fe3e520aede5c140d9938aa80
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__)
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 pid_t pid = -1;
122 # if defined MACOS_X
123 int i;
125 /* on os x, you have to exec after a fork, otherwise calls to
126 * frameworks will assert (and without corefoundation, you can't start
127 * the gui. what fun.). See CAVEATS at
128 http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/fork.2.html
130 * Since we have to go through this anyways, we might as well use vfork.
131 * But: then we can't detach from our starting shell, so stick with
132 * fork.
134 * Kinda sucks to restart vim when doing :gui, so don't fork in that
135 * case (make sure gui.dofork is only set when interpreting argv, not
136 * when doing :gui. Currently, gui.dofork is set to false in ex_gui().
138 * Also doesn't work well if vim starts cscope (or some other
139 * subprocess I guess), because it's not transferred to the newly
140 * exec'd process, leaving an orphaned process (not a zombie process)
141 * behind. The Right Thing is to kill all our child processes before
142 * calling exec.
145 /* stolen from http://paste.lisp.org/display/50906 */
146 extern int *_NSGetArgc(void);
147 extern char ***_NSGetArgv(void);
149 int argc = *_NSGetArgc();
150 char ** argv = *_NSGetArgv();
151 char * newargv[argc+2];
153 for (i = 0; i < argc; i++) {
154 newargv[i] = argv[i];
156 newargv[argc] = "-f";
157 newargv[argc+1] = NULL;
159 pid = fork();
160 switch(pid) {
161 case -1:
162 # ifndef NDEBUG
163 fprintf(stderr, "vim: Mac OS X workaround fork() failed!");
164 # endif
165 _exit(255);
166 case 0:
167 /* Child. */
169 /* shut down all the stuff we just started, just to start
170 * it again from the exec :-\ */
171 prepare_getout();
173 /* make sure we survive our shell */
174 setsid();
176 /* Restarts the vim process, will not return. */
177 execvp(argv[0], newargv);
179 /* if we come here, exec has failed. bail. */
180 _exit(255);
181 default:
182 /* Parent */
183 _exit(0);
185 # else
186 int pipefd[2]; /* pipe between parent and child */
187 int pipe_error;
188 char dummy;
190 /* Setup a pipe between the child and the parent, so that the parent
191 * knows when the child has done the setsid() call and is allowed to
192 * exit. */
193 pipe_error = (pipe(pipefd) < 0);
194 pid = fork();
195 if (pid > 0) /* Parent */
197 /* Give the child some time to do the setsid(), otherwise the
198 * exit() may kill the child too (when starting gvim from inside a
199 * gvim). */
200 if (pipe_error)
201 ui_delay(300L, TRUE);
202 else
204 /* The read returns when the child closes the pipe (or when
205 * the child dies for some reason). */
206 close(pipefd[1]);
207 (void)read(pipefd[0], &dummy, (size_t)1);
208 close(pipefd[0]);
211 /* When swapping screens we may need to go to the next line, e.g.,
212 * after a hit-enter prompt and using ":gui". */
213 if (newline_on_exit)
214 mch_errmsg("\r\n");
217 * The parent must skip the normal exit() processing, the child
218 * will do it. For example, GTK messes up signals when exiting.
220 _exit(0);
223 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
225 * Change our process group. On some systems/shells a CTRL-C in the
226 * shell where Vim was started would otherwise kill gvim!
228 if (pid == 0) /* child */
229 # if defined(HAVE_SETSID)
230 (void)setsid();
231 # else
232 (void)setpgid(0, 0);
233 # endif
234 # endif
235 if (!pipe_error)
237 close(pipefd[0]);
238 close(pipefd[1]);
242 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
243 /* Tell the session manager our new PID */
244 gui_mch_forked();
245 # endif
247 # endif
249 #else
250 # if defined(__QNXNTO__)
251 if (gui.in_use && dofork)
252 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
253 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
254 # endif
255 #endif
257 #ifdef FEAT_AUTOCMD
258 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
259 * the GUIFailed event. */
260 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
261 NULL, NULL, FALSE, curbuf);
262 #endif
264 --recursive;
268 * Call this when vim starts up, whether or not the GUI is started
270 void
271 gui_prepare(argc, argv)
272 int *argc;
273 char **argv;
275 gui.in_use = FALSE; /* No GUI yet (maybe later) */
276 gui.starting = FALSE; /* No GUI yet (maybe later) */
277 gui_mch_prepare(argc, argv);
281 * Try initializing the GUI and check if it can be started.
282 * Used from main() to check early if "vim -g" can start the GUI.
283 * Used from gui_init() to prepare for starting the GUI.
284 * Returns FAIL or OK.
287 gui_init_check()
289 static int result = MAYBE;
291 if (result != MAYBE)
293 if (result == FAIL)
294 EMSG(_("E229: Cannot start the GUI"));
295 return result;
298 gui.shell_created = FALSE;
299 gui.dying = FALSE;
300 gui.in_focus = TRUE; /* so the guicursor setting works */
301 gui.dragged_sb = SBAR_NONE;
302 gui.dragged_wp = NULL;
303 gui.pointer_hidden = FALSE;
304 gui.col = 0;
305 gui.row = 0;
306 gui.num_cols = Columns;
307 gui.num_rows = Rows;
309 gui.cursor_is_valid = FALSE;
310 gui.scroll_region_top = 0;
311 gui.scroll_region_bot = Rows - 1;
312 gui.scroll_region_left = 0;
313 gui.scroll_region_right = Columns - 1;
314 gui.highlight_mask = HL_NORMAL;
315 gui.char_width = 1;
316 gui.char_height = 1;
317 gui.char_ascent = 0;
318 gui.border_width = 0;
320 gui.norm_font = NOFONT;
321 #ifndef HAVE_GTK2
322 gui.bold_font = NOFONT;
323 gui.ital_font = NOFONT;
324 gui.boldital_font = NOFONT;
325 # ifdef FEAT_XFONTSET
326 gui.fontset = NOFONTSET;
327 # endif
328 #endif
330 #ifdef FEAT_MENU
331 # ifndef HAVE_GTK2
332 # ifdef FONTSET_ALWAYS
333 gui.menu_fontset = NOFONTSET;
334 # else
335 gui.menu_font = NOFONT;
336 # endif
337 # endif
338 gui.menu_is_active = TRUE; /* default: include menu */
339 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
340 gui.menu_height = MENU_DEFAULT_HEIGHT;
341 gui.menu_width = 0;
342 # endif
343 #endif
344 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
345 gui.toolbar_height = 0;
346 #endif
347 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
348 gui.footer_height = 0;
349 #endif
350 #ifdef FEAT_BEVAL_TIP
351 gui.tooltip_fontset = NOFONTSET;
352 #endif
354 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
355 gui.prev_wrap = -1;
357 #ifdef ALWAYS_USE_GUI
358 result = OK;
359 #else
360 result = gui_mch_init_check();
361 #endif
362 return result;
366 * This is the call which starts the GUI.
368 void
369 gui_init()
371 win_T *wp;
372 static int recursive = 0;
375 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
376 * function will then be executed at the first call, the rest by the
377 * recursive call. This allow the shell to be opened halfway reading a
378 * gvimrc file.
380 if (!recursive)
382 ++recursive;
384 clip_init(TRUE);
386 /* If can't initialize, don't try doing the rest */
387 if (gui_init_check() == FAIL)
389 --recursive;
390 clip_init(FALSE);
391 return;
395 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
396 * breaks the Paste toolbar button.
398 set_option_value((char_u *)"paste", 0L, NULL, 0);
401 * Set up system-wide default menus.
403 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
404 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
406 sys_menu = TRUE;
407 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
408 sys_menu = FALSE;
410 #endif
413 * Switch on the mouse by default, unless the user changed it already.
414 * This can then be changed in the .gvimrc.
416 if (!option_was_set((char_u *)"mouse"))
417 set_string_option_direct((char_u *)"mouse", -1,
418 (char_u *)"a", OPT_FREE, SID_NONE);
421 * If -U option given, use only the initializations from that file and
422 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
424 if (use_gvimrc != NULL)
426 if (STRCMP(use_gvimrc, "NONE") != 0
427 && STRCMP(use_gvimrc, "NORC") != 0
428 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
429 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
431 else
434 * Get system wide defaults for gvim, only when file name defined.
436 #ifdef SYS_GVIMRC_FILE
437 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
438 #endif
441 * Try to read GUI initialization commands from the following
442 * places:
443 * - environment variable GVIMINIT
444 * - the user gvimrc file (~/.gvimrc)
445 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
446 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
447 * The first that exists is used, the rest is ignored.
449 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
450 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
451 DOSO_GVIMRC) == FAIL
452 #ifdef USR_GVIMRC_FILE2
453 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
454 DOSO_GVIMRC) == FAIL
455 #endif
458 #ifdef USR_GVIMRC_FILE3
459 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
460 #endif
464 * Read initialization commands from ".gvimrc" in current
465 * directory. This is only done if the 'exrc' option is set.
466 * Because of security reasons we disallow shell and write
467 * commands now, except for unix if the file is owned by the user
468 * or 'secure' option has been reset in environment of global
469 * ".gvimrc".
470 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
471 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
473 if (p_exrc)
475 #ifdef UNIX
477 struct stat s;
479 /* if ".gvimrc" file is not owned by user, set 'secure'
480 * mode */
481 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
482 secure = p_secure;
484 #else
485 secure = p_secure;
486 #endif
488 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
489 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
490 #ifdef SYS_GVIMRC_FILE
491 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
492 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
493 #endif
494 #ifdef USR_GVIMRC_FILE2
495 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
496 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
497 #endif
498 #ifdef USR_GVIMRC_FILE3
499 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
500 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
501 #endif
503 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
505 if (secure == 2)
506 need_wait_return = TRUE;
507 secure = 0;
511 if (need_wait_return || msg_didany)
512 wait_return(TRUE);
514 --recursive;
517 /* If recursive call opened the shell, return here from the first call */
518 if (gui.in_use)
519 return;
522 * Create the GUI shell.
524 gui.in_use = TRUE; /* Must be set after menus have been set up */
525 if (gui_mch_init() == FAIL)
526 goto error;
528 /* Avoid a delay for an error message that was printed in the terminal
529 * where Vim was started. */
530 emsg_on_display = FALSE;
531 msg_scrolled = 0;
532 clear_sb_text();
533 need_wait_return = FALSE;
534 msg_didany = FALSE;
537 * Check validity of any generic resources that may have been loaded.
539 if (gui.border_width < 0)
540 gui.border_width = 0;
543 * Set up the fonts. First use a font specified with "-fn" or "-font".
545 if (font_argument != NULL)
546 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
547 if (
548 #ifdef FEAT_XFONTSET
549 (*p_guifontset == NUL
550 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
551 #endif
552 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
553 : p_guifont, FALSE) == FAIL)
555 EMSG(_("E665: Cannot start GUI, no valid font found"));
556 goto error2;
558 #ifdef FEAT_MBYTE
559 if (gui_get_wide_font() == FAIL)
560 EMSG(_("E231: 'guifontwide' invalid"));
561 #endif
563 gui.num_cols = Columns;
564 gui.num_rows = Rows;
565 gui_reset_scroll_region();
567 /* Create initial scrollbars */
568 FOR_ALL_WINDOWS(wp)
570 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
571 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
573 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
575 #ifdef FEAT_MENU
576 gui_create_initial_menus(root_menu);
577 #endif
578 #ifdef FEAT_SUN_WORKSHOP
579 if (usingSunWorkShop)
580 workshop_init();
581 #endif
582 #ifdef FEAT_SIGN_ICONS
583 sign_gui_started();
584 #endif
586 /* Configure the desired menu and scrollbars */
587 gui_init_which_components(NULL);
589 /* All components of the GUI have been created now */
590 gui.shell_created = TRUE;
592 #ifndef FEAT_GUI_GTK
593 /* Set the shell size, adjusted for the screen size. For GTK this only
594 * works after the shell has been opened, thus it is further down. */
595 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
596 #endif
597 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
598 /* Need to set the size of the menubar after all the menus have been
599 * created. */
600 gui_mch_compute_menu_height((Widget)0);
601 #endif
604 * Actually open the GUI shell.
606 if (gui_mch_open() != FAIL)
608 #ifdef FEAT_TITLE
609 maketitle();
610 resettitle();
611 #endif
612 init_gui_options();
613 #ifdef FEAT_ARABIC
614 /* Our GUI can't do bidi. */
615 p_tbidi = FALSE;
616 #endif
617 #if defined(FEAT_GUI_GTK)
618 /* Give GTK+ a chance to put all widget's into place. */
619 gui_mch_update();
621 # ifdef FEAT_MENU
622 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
623 * It was still there to make F10 work. */
624 if (vim_strchr(p_go, GO_MENUS) == NULL)
626 --gui.starting;
627 gui_mch_enable_menu(FALSE);
628 ++gui.starting;
629 gui_mch_update();
631 # endif
633 /* Now make sure the shell fits on the screen. */
634 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
635 #endif
636 /* When 'lines' was set while starting up the topframe may have to be
637 * resized. */
638 win_new_shellsize();
640 #ifdef FEAT_BEVAL
641 /* Always create the Balloon Evaluation area, but disable it when
642 * 'ballooneval' is off */
643 # ifdef FEAT_GUI_GTK
644 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
645 &general_beval_cb, NULL);
646 # else
647 # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
649 extern Widget textArea;
650 balloonEval = gui_mch_create_beval_area(textArea, NULL,
651 &general_beval_cb, NULL);
653 # else
654 # ifdef FEAT_GUI_W32
655 balloonEval = gui_mch_create_beval_area(NULL, NULL,
656 &general_beval_cb, NULL);
657 # endif
658 # endif
659 # endif
660 if (!p_beval)
661 gui_mch_disable_beval_area(balloonEval);
662 #endif
664 #ifdef FEAT_NETBEANS_INTG
665 if (starting == 0 && usingNetbeans)
666 /* Tell the client that it can start sending commands. */
667 netbeans_startup_done();
668 #endif
669 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
670 if (!im_xim_isvalid_imactivate())
671 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
672 #endif
673 /* When 'cmdheight' was set during startup it may not have taken
674 * effect yet. */
675 if (p_ch != 1L)
676 command_height();
678 return;
681 error2:
682 #ifdef FEAT_GUI_X11
683 /* undo gui_mch_init() */
684 gui_mch_uninit();
685 #endif
687 error:
688 gui.in_use = FALSE;
689 clip_init(FALSE);
693 void
694 gui_exit(rc)
695 int rc;
697 #ifndef __BEOS__
698 /* don't free the fonts, it leads to a BUS error
699 * richard@whitequeen.com Jul 99 */
700 free_highlight_fonts();
701 #endif
702 gui.in_use = FALSE;
703 gui_mch_exit(rc);
706 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
707 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
708 || defined(PROTO) || defined(FEAT_GUI_MACVIM)
709 # define NEED_GUI_UPDATE_SCREEN 1
711 * Called when the GUI shell is closed by the user. If there are no changed
712 * files Vim exits, otherwise there will be a dialog to ask the user what to
713 * do.
714 * When this function returns, Vim should NOT exit!
716 void
717 gui_shell_closed()
719 cmdmod_T save_cmdmod;
721 save_cmdmod = cmdmod;
723 /* Only exit when there are no changed files */
724 exiting = TRUE;
725 # ifdef FEAT_BROWSE
726 cmdmod.browse = TRUE;
727 # endif
728 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
729 cmdmod.confirm = TRUE;
730 # endif
731 /* If there are changed buffers, present the user with a dialog if
732 * possible, otherwise give an error message. */
733 if (!check_changed_any(FALSE))
734 getout(0);
736 exiting = FALSE;
737 cmdmod = save_cmdmod;
738 gui_update_screen(); /* redraw, window may show changed buffer */
740 #endif
743 * Set the font. "font_list" is a a comma separated list of font names. The
744 * first font name that works is used. If none is found, use the default
745 * font.
746 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
747 * Return OK when able to set the font. When it failed FAIL is returned and
748 * the fonts are unchanged.
750 /*ARGSUSED*/
752 gui_init_font(font_list, fontset)
753 char_u *font_list;
754 int fontset;
756 #define FONTLEN 320
757 char_u font_name[FONTLEN];
758 int font_list_empty = FALSE;
759 int ret = FAIL;
761 if (!gui.in_use)
762 return FAIL;
764 font_name[0] = NUL;
765 if (*font_list == NUL)
766 font_list_empty = TRUE;
767 else
769 #ifdef FEAT_XFONTSET
770 /* When using a fontset, the whole list of fonts is one name. */
771 if (fontset)
772 ret = gui_mch_init_font(font_list, TRUE);
773 else
774 #endif
775 while (*font_list != NUL)
777 /* Isolate one comma separated font name. */
778 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
780 #if defined(FEAT_GUI_MACVIM)
781 /* The font dialog is modeless in Mac OS X, so when
782 * gui_mch_init_font() is called with "*" it brings up the
783 * dialog and returns immediately. In this case we don't want
784 * it to be called again with NULL, so return here. */
785 if (STRCMP(font_name, "*") == 0) {
786 gui_mch_init_font(font_name, FALSE);
787 return FALSE;
789 #endif
791 /* Careful!!! The Win32 version of gui_mch_init_font(), when
792 * called with "*" will change p_guifont to the selected font
793 * name, which frees the old value. This makes font_list
794 * invalid. Thus when OK is returned here, font_list must no
795 * longer be used! */
796 if (gui_mch_init_font(font_name, FALSE) == OK)
798 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
799 /* If it's a Unicode font, try setting 'guifontwide' to a
800 * similar double-width font. */
801 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
802 && strstr((char *)font_name, "10646") != NULL)
803 set_guifontwide(font_name);
804 #endif
805 ret = OK;
806 break;
811 if (ret != OK
812 && STRCMP(font_list, "*") != 0
813 && (font_list_empty || gui.norm_font == NOFONT))
816 * Couldn't load any font in 'font_list', keep the current font if
817 * there is one. If 'font_list' is empty, or if there is no current
818 * font, tell gui_mch_init_font() to try to find a font we can load.
820 ret = gui_mch_init_font(NULL, FALSE);
823 if (ret == OK)
825 #ifndef HAVE_GTK2
826 /* Set normal font as current font */
827 # ifdef FEAT_XFONTSET
828 if (gui.fontset != NOFONTSET)
829 gui_mch_set_fontset(gui.fontset);
830 else
831 # endif
832 gui_mch_set_font(gui.norm_font);
833 #endif
834 gui_set_shellsize(FALSE,
835 #ifdef MSWIN
836 TRUE
837 #else
838 FALSE
839 #endif
840 , RESIZE_BOTH);
843 return ret;
846 #if defined(FEAT_MBYTE) || defined(PROTO)
847 # ifndef HAVE_GTK2
849 * Try setting 'guifontwide' to a font twice as wide as "name".
851 static void
852 set_guifontwide(name)
853 char_u *name;
855 int i = 0;
856 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
857 char_u *wp = NULL;
858 char_u *p;
859 GuiFont font;
861 wp = wide_name;
862 for (p = name; *p != NUL; ++p)
864 *wp++ = *p;
865 if (*p == '-')
867 ++i;
868 if (i == 6) /* font type: change "--" to "-*-" */
870 if (p[1] == '-')
871 *wp++ = '*';
873 else if (i == 12) /* found the width */
875 ++p;
876 i = getdigits(&p);
877 if (i != 0)
879 /* Double the width specification. */
880 sprintf((char *)wp, "%d%s", i * 2, p);
881 font = gui_mch_get_font(wide_name, FALSE);
882 if (font != NOFONT)
884 gui_mch_free_font(gui.wide_font);
885 gui.wide_font = font;
886 set_string_option_direct((char_u *)"gfw", -1,
887 wide_name, OPT_FREE, 0);
890 break;
895 # endif /* !HAVE_GTK2 */
898 * Get the font for 'guifontwide'.
899 * Return FAIL for an invalid font name.
902 gui_get_wide_font()
904 GuiFont font = NOFONT;
905 char_u font_name[FONTLEN];
906 char_u *p;
908 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
909 return OK; /* Will give an error message later. */
911 if (p_guifontwide != NULL && *p_guifontwide != NUL)
913 for (p = p_guifontwide; *p != NUL; )
915 /* Isolate one comma separated font name. */
916 (void)copy_option_part(&p, font_name, FONTLEN, ",");
917 font = gui_mch_get_font(font_name, FALSE);
918 if (font != NOFONT)
919 break;
921 if (font == NOFONT)
922 return FAIL;
925 gui_mch_free_font(gui.wide_font);
926 #ifdef HAVE_GTK2
927 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
928 if (font != NOFONT && gui.norm_font != NOFONT
929 && pango_font_description_equal(font, gui.norm_font))
931 gui.wide_font = NOFONT;
932 gui_mch_free_font(font);
934 else
935 #endif
936 gui.wide_font = font;
937 return OK;
939 #endif
941 void
942 gui_set_cursor(row, col)
943 int row;
944 int col;
946 gui.row = row;
947 gui.col = col;
951 * gui_check_pos - check if the cursor is on the screen.
953 static void
954 gui_check_pos()
956 if (gui.row >= screen_Rows)
957 gui.row = screen_Rows - 1;
958 if (gui.col >= screen_Columns)
959 gui.col = screen_Columns - 1;
960 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
961 gui.cursor_is_valid = FALSE;
965 * Redraw the cursor if necessary or when forced.
966 * Careful: The contents of ScreenLines[] must match what is on the screen,
967 * otherwise this goes wrong. May need to call out_flush() first.
969 void
970 gui_update_cursor(force, clear_selection)
971 int force; /* when TRUE, update even when not moved */
972 int clear_selection;/* clear selection under cursor */
974 int cur_width = 0;
975 int cur_height = 0;
976 int old_hl_mask;
977 int idx;
978 int id;
979 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
980 int cattr; /* cursor attributes */
981 int attr;
982 attrentry_T *aep = NULL;
984 /* Don't update the cursor when halfway busy scrolling.
985 * ScreenLines[] isn't valid then. */
986 if (!can_update_cursor)
987 return;
989 gui_check_pos();
990 if (!gui.cursor_is_valid || force
991 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
993 gui_undraw_cursor();
994 if (gui.row < 0)
995 return;
996 #ifdef USE_IM_CONTROL
997 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
998 im_set_position(gui.row, gui.col);
999 #endif
1000 gui.cursor_row = gui.row;
1001 gui.cursor_col = gui.col;
1003 /* Only write to the screen after ScreenLines[] has been initialized */
1004 if (!screen_cleared || ScreenLines == NULL)
1005 return;
1007 /* Clear the selection if we are about to write over it */
1008 if (clear_selection)
1009 clip_may_clear_selection(gui.row, gui.row);
1010 /* Check that the cursor is inside the shell (resizing may have made
1011 * it invalid) */
1012 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1013 return;
1015 gui.cursor_is_valid = TRUE;
1018 * How the cursor is drawn depends on the current mode.
1020 idx = get_shape_idx(FALSE);
1021 if (State & LANGMAP)
1022 id = shape_table[idx].id_lm;
1023 else
1024 id = shape_table[idx].id;
1026 /* get the colors and attributes for the cursor. Default is inverted */
1027 cfg = INVALCOLOR;
1028 cbg = INVALCOLOR;
1029 cattr = HL_INVERSE;
1030 gui_mch_set_blinking(shape_table[idx].blinkwait,
1031 shape_table[idx].blinkon,
1032 shape_table[idx].blinkoff);
1033 if (id > 0)
1035 cattr = syn_id2colors(id, &cfg, &cbg);
1036 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1038 static int iid;
1039 guicolor_T fg, bg;
1041 if (im_get_status())
1043 iid = syn_name2id((char_u *)"CursorIM");
1044 if (iid > 0)
1046 syn_id2colors(iid, &fg, &bg);
1047 if (bg != INVALCOLOR)
1048 cbg = bg;
1049 if (fg != INVALCOLOR)
1050 cfg = fg;
1054 #endif
1058 * Get the attributes for the character under the cursor.
1059 * When no cursor color was given, use the character color.
1061 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1062 if (attr > HL_ALL)
1063 aep = syn_gui_attr2entry(attr);
1064 if (aep != NULL)
1066 attr = aep->ae_attr;
1067 if (cfg == INVALCOLOR)
1068 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1069 : aep->ae_u.gui.fg_color);
1070 if (cbg == INVALCOLOR)
1071 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1072 : aep->ae_u.gui.bg_color);
1074 if (cfg == INVALCOLOR)
1075 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1076 if (cbg == INVALCOLOR)
1077 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1079 #ifdef FEAT_XIM
1080 if (aep != NULL)
1082 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1083 : aep->ae_u.gui.bg_color);
1084 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1085 : aep->ae_u.gui.fg_color);
1086 if (xim_bg_color == INVALCOLOR)
1087 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1088 : gui.back_pixel;
1089 if (xim_fg_color == INVALCOLOR)
1090 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1091 : gui.norm_pixel;
1093 else
1095 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1096 : gui.back_pixel;
1097 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1098 : gui.norm_pixel;
1100 #endif
1102 attr &= ~HL_INVERSE;
1103 if (cattr & HL_INVERSE)
1105 cc = cbg;
1106 cbg = cfg;
1107 cfg = cc;
1109 cattr &= ~HL_INVERSE;
1112 * When we don't have window focus, draw a hollow cursor.
1114 if (!gui.in_focus)
1116 gui_mch_draw_hollow_cursor(cbg);
1117 return;
1120 old_hl_mask = gui.highlight_mask;
1121 if (shape_table[idx].shape == SHAPE_BLOCK
1122 #ifdef FEAT_HANGULIN
1123 || composing_hangul
1124 #endif
1128 * Draw the text character with the cursor colors. Use the
1129 * character attributes plus the cursor attributes.
1131 gui.highlight_mask = (cattr | attr);
1132 #ifdef FEAT_HANGULIN
1133 if (composing_hangul)
1134 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1135 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1136 else
1137 #endif
1138 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1139 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1141 else
1143 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1144 int col_off = FALSE;
1145 #endif
1147 * First draw the partial cursor, then overwrite with the text
1148 * character, using a transparent background.
1150 if (shape_table[idx].shape == SHAPE_VER)
1152 cur_height = gui.char_height;
1153 cur_width = (gui.char_width * shape_table[idx].percentage
1154 + 99) / 100;
1156 else
1158 cur_height = (gui.char_height * shape_table[idx].percentage
1159 + 99) / 100;
1160 cur_width = gui.char_width;
1162 #ifdef FEAT_MBYTE
1163 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1164 LineOffset[gui.row] + screen_Columns) > 1)
1166 /* Double wide character. */
1167 if (shape_table[idx].shape != SHAPE_VER)
1168 cur_width += gui.char_width;
1169 # ifdef FEAT_RIGHTLEFT
1170 if (CURSOR_BAR_RIGHT)
1172 /* gui.col points to the left halve of the character but
1173 * the vertical line needs to be on the right halve.
1174 * A double-wide horizontal line is also drawn from the
1175 * right halve in gui_mch_draw_part_cursor(). */
1176 col_off = TRUE;
1177 ++gui.col;
1179 # endif
1181 #endif
1182 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1183 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1184 if (col_off)
1185 --gui.col;
1186 #endif
1188 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1189 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1190 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1191 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1192 (guicolor_T)0, (guicolor_T)0, 0);
1193 #endif
1195 gui.highlight_mask = old_hl_mask;
1199 #if defined(FEAT_MENU) || defined(PROTO)
1200 void
1201 gui_position_menu()
1203 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
1204 || defined(FEAT_GUI_MACVIM))
1205 if (gui.menu_is_active && gui.in_use)
1206 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1207 # endif
1209 #endif
1212 * Position the various GUI components (text area, menu). The vertical
1213 * scrollbars are NOT handled here. See gui_update_scrollbars().
1215 /*ARGSUSED*/
1216 static void
1217 gui_position_components(total_width)
1218 int total_width;
1220 int text_area_x;
1221 int text_area_y;
1222 int text_area_width;
1223 int text_area_height;
1225 /* avoid that moving components around generates events */
1226 ++hold_gui_events;
1228 text_area_x = 0;
1229 if (gui.which_scrollbars[SBAR_LEFT])
1230 text_area_x += gui.scrollbar_width;
1232 text_area_y = 0;
1233 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
1234 || defined(FEAT_GUI_MACVIM))
1235 gui.menu_width = total_width;
1236 if (gui.menu_is_active)
1237 text_area_y += gui.menu_height;
1238 #endif
1239 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1240 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1241 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1242 #endif
1244 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1245 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1246 if (gui_has_tabline())
1247 text_area_y += gui.tabline_height;
1248 #endif
1250 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1251 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1253 # ifdef FEAT_GUI_ATHENA
1254 gui_mch_set_toolbar_pos(0, text_area_y,
1255 gui.menu_width, gui.toolbar_height);
1256 # endif
1257 text_area_y += gui.toolbar_height;
1259 #endif
1261 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1262 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1264 gui_mch_set_text_area_pos(text_area_x,
1265 text_area_y,
1266 text_area_width,
1267 text_area_height
1268 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1269 + xim_get_status_area_height()
1270 #endif
1272 #ifdef FEAT_MENU
1273 gui_position_menu();
1274 #endif
1275 if (gui.which_scrollbars[SBAR_BOTTOM])
1276 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1277 text_area_x,
1278 text_area_y + text_area_height,
1279 text_area_width,
1280 gui.scrollbar_height);
1281 gui.left_sbar_x = 0;
1282 gui.right_sbar_x = text_area_x + text_area_width;
1284 --hold_gui_events;
1288 * Get the width of the widgets and decorations to the side of the text area.
1291 gui_get_base_width()
1293 int base_width;
1295 base_width = 2 * gui.border_offset;
1296 if (gui.which_scrollbars[SBAR_LEFT])
1297 base_width += gui.scrollbar_width;
1298 if (gui.which_scrollbars[SBAR_RIGHT])
1299 base_width += gui.scrollbar_width;
1300 return base_width;
1304 * Get the height of the widgets and decorations above and below the text area.
1307 gui_get_base_height()
1309 int base_height;
1311 base_height = 2 * gui.border_offset;
1312 if (gui.which_scrollbars[SBAR_BOTTOM])
1313 base_height += gui.scrollbar_height;
1314 #ifdef FEAT_GUI_GTK
1315 /* We can't take the sizes properly into account until anything is
1316 * realized. Therefore we recalculate all the values here just before
1317 * setting the size. (--mdcki) */
1318 #elif !defined(FEAT_GUI_MACVIM)
1319 # ifdef FEAT_MENU
1320 if (gui.menu_is_active)
1321 base_height += gui.menu_height;
1322 # endif
1323 # ifdef FEAT_TOOLBAR
1324 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1325 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1326 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1327 # else
1328 base_height += gui.toolbar_height;
1329 # endif
1330 # endif
1331 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1332 || defined(FEAT_GUI_MOTIF))
1333 if (gui_has_tabline())
1334 base_height += gui.tabline_height;
1335 # endif
1336 # ifdef FEAT_FOOTER
1337 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1338 base_height += gui.footer_height;
1339 # endif
1340 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1341 base_height += gui_mch_text_area_extra_height();
1342 # endif
1343 #endif
1344 return base_height;
1348 * Should be called after the GUI shell has been resized. Its arguments are
1349 * the new width and height of the shell in pixels.
1351 void
1352 gui_resize_shell(pixel_width, pixel_height)
1353 int pixel_width;
1354 int pixel_height;
1356 static int busy = FALSE;
1358 if (!gui.shell_created) /* ignore when still initializing */
1359 return;
1362 * Can't resize the screen while it is being redrawn. Remember the new
1363 * size and handle it later.
1365 if (updating_screen || busy)
1367 new_pixel_width = pixel_width;
1368 new_pixel_height = pixel_height;
1369 return;
1372 again:
1373 busy = TRUE;
1375 /* Flush pending output before redrawing */
1376 out_flush();
1378 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1379 gui.num_rows = (pixel_height - gui_get_base_height()
1380 #if !defined(FEAT_GUI_PHOTON) && !defined(FEAT_GUI_MSWIN)
1381 + (gui.char_height / 2)
1382 #endif
1383 ) / gui.char_height;
1385 gui_position_components(pixel_width);
1387 gui_reset_scroll_region();
1389 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1390 * at the last line here (why does it have to be one row too low?).
1392 if (State == ASKMORE || State == CONFIRM)
1393 gui.row = gui.num_rows;
1395 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1396 * the safe side. */
1397 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1398 || gui.num_rows != Rows || gui.num_cols != Columns)
1399 shell_resized();
1401 gui_update_scrollbars(TRUE);
1402 gui_update_cursor(FALSE, TRUE);
1403 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1404 xim_set_status_area();
1405 #endif
1407 busy = FALSE;
1410 * We could have been called again while redrawing the screen.
1411 * Need to do it all again with the latest size then.
1413 if (new_pixel_height)
1415 pixel_width = new_pixel_width;
1416 pixel_height = new_pixel_height;
1417 new_pixel_width = 0;
1418 new_pixel_height = 0;
1419 goto again;
1424 * Check if gui_resize_shell() must be called.
1426 void
1427 gui_may_resize_shell()
1429 int h, w;
1431 if (new_pixel_height)
1433 /* careful: gui_resize_shell() may postpone the resize again if we
1434 * were called indirectly by it */
1435 w = new_pixel_width;
1436 h = new_pixel_height;
1437 new_pixel_width = 0;
1438 new_pixel_height = 0;
1439 gui_resize_shell(w, h);
1444 gui_get_shellsize()
1446 Rows = gui.num_rows;
1447 Columns = gui.num_cols;
1448 return OK;
1452 * Set the size of the Vim shell according to Rows and Columns.
1453 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1454 * on the screen.
1456 /*ARGSUSED*/
1457 void
1458 gui_set_shellsize(mustset, fit_to_display, direction)
1459 int mustset; /* set by the user */
1460 int fit_to_display;
1461 int direction; /* RESIZE_HOR, RESIZE_VER */
1463 int base_width;
1464 int base_height;
1465 int width;
1466 int height;
1467 int min_width;
1468 int min_height;
1469 int screen_w;
1470 int screen_h;
1472 if (!gui.shell_created)
1473 return;
1475 #ifdef MSWIN
1476 /* If not setting to a user specified size and maximized, calculate the
1477 * number of characters that fit in the maximized window. */
1478 if (!mustset && gui_mch_maximized())
1480 gui_mch_newfont();
1481 return;
1483 #endif
1485 base_width = gui_get_base_width();
1486 base_height = gui_get_base_height();
1487 #ifdef USE_SUN_WORKSHOP
1488 if (!mustset && usingSunWorkShop
1489 && workshop_get_width_height(&width, &height))
1491 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1492 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1494 else
1495 #endif
1497 width = Columns * gui.char_width + base_width;
1498 height = Rows * gui.char_height + base_height;
1501 if (fit_to_display)
1503 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1504 if ((direction & RESIZE_HOR) && width > screen_w)
1506 Columns = (screen_w - base_width) / gui.char_width;
1507 if (Columns < MIN_COLUMNS)
1508 Columns = MIN_COLUMNS;
1509 width = Columns * gui.char_width + base_width;
1511 if ((direction & RESIZE_VERT) && height > screen_h)
1513 Rows = (screen_h - base_height) / gui.char_height;
1514 check_shellsize();
1515 height = Rows * gui.char_height + base_height;
1518 gui.num_cols = Columns;
1519 gui.num_rows = Rows;
1521 min_width = base_width + MIN_COLUMNS * gui.char_width;
1522 min_height = base_height + MIN_LINES * gui.char_height;
1523 # ifdef FEAT_WINDOWS
1524 min_height += tabline_height() * gui.char_height;
1525 # endif
1527 gui_mch_set_shellsize(width, height, min_width, min_height,
1528 base_width, base_height, direction);
1529 if (fit_to_display)
1531 int x, y;
1533 /* Some window managers put the Vim window left of/above the screen. */
1534 gui_mch_update();
1535 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1536 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1539 gui_position_components(width);
1540 gui_update_scrollbars(TRUE);
1541 gui_reset_scroll_region();
1545 * Called when Rows and/or Columns has changed.
1547 void
1548 gui_new_shellsize()
1550 gui_reset_scroll_region();
1554 * Make scroll region cover whole screen.
1556 void
1557 gui_reset_scroll_region()
1559 gui.scroll_region_top = 0;
1560 gui.scroll_region_bot = gui.num_rows - 1;
1561 gui.scroll_region_left = 0;
1562 gui.scroll_region_right = gui.num_cols - 1;
1565 void
1566 gui_start_highlight(mask)
1567 int mask;
1569 if (mask > HL_ALL) /* highlight code */
1570 gui.highlight_mask = mask;
1571 else /* mask */
1572 gui.highlight_mask |= mask;
1575 void
1576 gui_stop_highlight(mask)
1577 int mask;
1579 if (mask > HL_ALL) /* highlight code */
1580 gui.highlight_mask = HL_NORMAL;
1581 else /* mask */
1582 gui.highlight_mask &= ~mask;
1586 * Clear a rectangular region of the screen from text pos (row1, col1) to
1587 * (row2, col2) inclusive.
1589 void
1590 gui_clear_block(row1, col1, row2, col2)
1591 int row1;
1592 int col1;
1593 int row2;
1594 int col2;
1596 /* Clear the selection if we are about to write over it */
1597 clip_may_clear_selection(row1, row2);
1599 gui_mch_clear_block(row1, col1, row2, col2);
1601 /* Invalidate cursor if it was in this block */
1602 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1603 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1604 gui.cursor_is_valid = FALSE;
1608 * Write code to update the cursor later. This avoids the need to flush the
1609 * output buffer before calling gui_update_cursor().
1611 void
1612 gui_update_cursor_later()
1614 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1617 void
1618 gui_write(s, len)
1619 char_u *s;
1620 int len;
1622 char_u *p;
1623 int arg1 = 0, arg2 = 0;
1624 /* this doesn't make sense, disabled until someone can explain why it
1625 * would be needed */
1626 #if 0 && (defined(RISCOS) || defined(WIN16))
1627 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1628 our redraw speed will suffer */
1629 #else
1630 int force_cursor = FALSE; /* force cursor update */
1631 #endif
1632 int force_scrollbar = FALSE;
1633 static win_T *old_curwin = NULL;
1635 /* #define DEBUG_GUI_WRITE */
1636 #ifdef DEBUG_GUI_WRITE
1638 int i;
1639 char_u *str;
1641 printf("gui_write(%d):\n ", len);
1642 for (i = 0; i < len; i++)
1643 if (s[i] == ESC)
1645 if (i != 0)
1646 printf("\n ");
1647 printf("<ESC>");
1649 else
1651 str = transchar_byte(s[i]);
1652 if (str[0] && str[1])
1653 printf("<%s>", (char *)str);
1654 else
1655 printf("%s", (char *)str);
1657 printf("\n");
1659 #endif
1660 while (len)
1662 if (s[0] == ESC && s[1] == '|')
1664 p = s + 2;
1665 if (VIM_ISDIGIT(*p))
1667 arg1 = getdigits(&p);
1668 if (p > s + len)
1669 break;
1670 if (*p == ';')
1672 ++p;
1673 arg2 = getdigits(&p);
1674 if (p > s + len)
1675 break;
1678 switch (*p)
1680 case 'C': /* Clear screen */
1681 clip_scroll_selection(9999);
1682 gui_mch_clear_all();
1683 gui.cursor_is_valid = FALSE;
1684 force_scrollbar = TRUE;
1685 break;
1686 case 'M': /* Move cursor */
1687 gui_set_cursor(arg1, arg2);
1688 break;
1689 case 's': /* force cursor (shape) update */
1690 force_cursor = TRUE;
1691 break;
1692 case 'R': /* Set scroll region */
1693 if (arg1 < arg2)
1695 gui.scroll_region_top = arg1;
1696 gui.scroll_region_bot = arg2;
1698 else
1700 gui.scroll_region_top = arg2;
1701 gui.scroll_region_bot = arg1;
1703 break;
1704 #ifdef FEAT_VERTSPLIT
1705 case 'V': /* Set vertical scroll region */
1706 if (arg1 < arg2)
1708 gui.scroll_region_left = arg1;
1709 gui.scroll_region_right = arg2;
1711 else
1713 gui.scroll_region_left = arg2;
1714 gui.scroll_region_right = arg1;
1716 break;
1717 #endif
1718 case 'd': /* Delete line */
1719 gui_delete_lines(gui.row, 1);
1720 break;
1721 case 'D': /* Delete lines */
1722 gui_delete_lines(gui.row, arg1);
1723 break;
1724 case 'i': /* Insert line */
1725 gui_insert_lines(gui.row, 1);
1726 break;
1727 case 'I': /* Insert lines */
1728 gui_insert_lines(gui.row, arg1);
1729 break;
1730 case '$': /* Clear to end-of-line */
1731 gui_clear_block(gui.row, gui.col, gui.row,
1732 (int)Columns - 1);
1733 break;
1734 case 'h': /* Turn on highlighting */
1735 gui_start_highlight(arg1);
1736 break;
1737 case 'H': /* Turn off highlighting */
1738 gui_stop_highlight(arg1);
1739 break;
1740 case 'f': /* flash the window (visual bell) */
1741 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1742 break;
1743 default:
1744 p = s + 1; /* Skip the ESC */
1745 break;
1747 len -= (int)(++p - s);
1748 s = p;
1750 else if (
1751 #ifdef EBCDIC
1752 CtrlChar(s[0]) != 0 /* Ctrl character */
1753 #else
1754 s[0] < 0x20 /* Ctrl character */
1755 #endif
1756 #ifdef FEAT_SIGN_ICONS
1757 && s[0] != SIGN_BYTE
1758 # ifdef FEAT_NETBEANS_INTG
1759 && s[0] != MULTISIGN_BYTE
1760 # endif
1761 #endif
1764 if (s[0] == '\n') /* NL */
1766 gui.col = 0;
1767 if (gui.row < gui.scroll_region_bot)
1768 gui.row++;
1769 else
1770 gui_delete_lines(gui.scroll_region_top, 1);
1772 else if (s[0] == '\r') /* CR */
1774 gui.col = 0;
1776 else if (s[0] == '\b') /* Backspace */
1778 if (gui.col)
1779 --gui.col;
1781 else if (s[0] == Ctrl_L) /* cursor-right */
1783 ++gui.col;
1785 else if (s[0] == Ctrl_G) /* Beep */
1787 gui_mch_beep();
1789 /* Other Ctrl character: shouldn't happen! */
1791 --len; /* Skip this char */
1792 ++s;
1794 else
1796 p = s;
1797 while (len > 0 && (
1798 #ifdef EBCDIC
1799 CtrlChar(*p) == 0
1800 #else
1801 *p >= 0x20
1802 #endif
1803 #ifdef FEAT_SIGN_ICONS
1804 || *p == SIGN_BYTE
1805 # ifdef FEAT_NETBEANS_INTG
1806 || *p == MULTISIGN_BYTE
1807 # endif
1808 #endif
1811 len--;
1812 p++;
1814 gui_outstr(s, (int)(p - s));
1815 s = p;
1819 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1820 * set). */
1821 if (force_cursor)
1822 gui_update_cursor(TRUE, TRUE);
1824 /* When switching to another window the dragging must have stopped.
1825 * Required for GTK, dragged_sb isn't reset. */
1826 if (old_curwin != curwin)
1827 gui.dragged_sb = SBAR_NONE;
1829 /* Update the scrollbars after clearing the screen or when switched
1830 * to another window.
1831 * Update the horizontal scrollbar always, it's difficult to check all
1832 * situations where it might change. */
1833 if (force_scrollbar || old_curwin != curwin)
1834 gui_update_scrollbars(force_scrollbar);
1835 else
1836 gui_update_horiz_scrollbar(FALSE);
1837 old_curwin = curwin;
1840 * We need to make sure this is cleared since Athena doesn't tell us when
1841 * he is done dragging. Do the same for GTK.
1843 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1844 gui.dragged_sb = SBAR_NONE;
1845 #endif
1847 gui_mch_flush(); /* In case vim decides to take a nap */
1851 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1852 * produces wrong results. Call gui_dont_update_cursor() before that code and
1853 * gui_can_update_cursor() afterwards.
1855 void
1856 gui_dont_update_cursor()
1858 if (gui.in_use)
1860 /* Undraw the cursor now, we probably can't do it after the change. */
1861 gui_undraw_cursor();
1862 can_update_cursor = FALSE;
1866 void
1867 gui_can_update_cursor()
1869 can_update_cursor = TRUE;
1870 /* No need to update the cursor right now, there is always more output
1871 * after scrolling. */
1874 static void
1875 gui_outstr(s, len)
1876 char_u *s;
1877 int len;
1879 int this_len;
1880 #ifdef FEAT_MBYTE
1881 int cells;
1882 #endif
1884 if (len == 0)
1885 return;
1887 if (len < 0)
1888 len = (int)STRLEN(s);
1890 while (len > 0)
1892 #ifdef FEAT_MBYTE
1893 if (has_mbyte)
1895 /* Find out how many chars fit in the current line. */
1896 cells = 0;
1897 for (this_len = 0; this_len < len; )
1899 cells += (*mb_ptr2cells)(s + this_len);
1900 if (gui.col + cells > Columns)
1901 break;
1902 this_len += (*mb_ptr2len)(s + this_len);
1904 if (this_len > len)
1905 this_len = len; /* don't include following composing char */
1907 else
1908 #endif
1909 if (gui.col + len > Columns)
1910 this_len = Columns - gui.col;
1911 else
1912 this_len = len;
1914 (void)gui_outstr_nowrap(s, this_len,
1915 0, (guicolor_T)0, (guicolor_T)0, 0);
1916 s += this_len;
1917 len -= this_len;
1918 #ifdef FEAT_MBYTE
1919 /* fill up for a double-width char that doesn't fit. */
1920 if (len > 0 && gui.col < Columns)
1921 (void)gui_outstr_nowrap((char_u *)" ", 1,
1922 0, (guicolor_T)0, (guicolor_T)0, 0);
1923 #endif
1924 /* The cursor may wrap to the next line. */
1925 if (gui.col >= Columns)
1927 gui.col = 0;
1928 gui.row++;
1934 * Output one character (may be one or two display cells).
1935 * Caller must check for valid "off".
1936 * Returns FAIL or OK, just like gui_outstr_nowrap().
1938 static int
1939 gui_screenchar(off, flags, fg, bg, back)
1940 int off; /* Offset from start of screen */
1941 int flags;
1942 guicolor_T fg, bg; /* colors for cursor */
1943 int back; /* backup this many chars when using bold trick */
1945 #ifdef FEAT_MBYTE
1946 char_u buf[MB_MAXBYTES + 1];
1948 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1949 if (enc_utf8 && ScreenLines[off] == 0)
1950 return OK;
1952 if (enc_utf8 && ScreenLinesUC[off] != 0)
1953 /* Draw UTF-8 multi-byte character. */
1954 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1955 flags, fg, bg, back);
1957 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1959 buf[0] = ScreenLines[off];
1960 buf[1] = ScreenLines2[off];
1961 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1964 /* Draw non-multi-byte character or DBCS character. */
1965 return gui_outstr_nowrap(ScreenLines + off,
1966 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1967 flags, fg, bg, back);
1968 #else
1969 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1970 #endif
1973 #ifdef HAVE_GTK2
1975 * Output the string at the given screen position. This is used in place
1976 * of gui_screenchar() where possible because Pango needs as much context
1977 * as possible to work nicely. It's a lot faster as well.
1979 static int
1980 gui_screenstr(off, len, flags, fg, bg, back)
1981 int off; /* Offset from start of screen */
1982 int len; /* string length in screen cells */
1983 int flags;
1984 guicolor_T fg, bg; /* colors for cursor */
1985 int back; /* backup this many chars when using bold trick */
1987 char_u *buf;
1988 int outlen = 0;
1989 int i;
1990 int retval;
1992 if (len <= 0) /* "cannot happen"? */
1993 return OK;
1995 if (enc_utf8)
1997 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1998 if (buf == NULL)
1999 return OK; /* not much we could do here... */
2001 for (i = off; i < off + len; ++i)
2003 if (ScreenLines[i] == 0)
2004 continue; /* skip second half of double-width char */
2006 if (ScreenLinesUC[i] == 0)
2007 buf[outlen++] = ScreenLines[i];
2008 else
2009 outlen += utfc_char2bytes(i, buf + outlen);
2012 buf[outlen] = NUL; /* only to aid debugging */
2013 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2014 vim_free(buf);
2016 return retval;
2018 else if (enc_dbcs == DBCS_JPNU)
2020 buf = alloc((unsigned)(len * 2 + 1));
2021 if (buf == NULL)
2022 return OK; /* not much we could do here... */
2024 for (i = off; i < off + len; ++i)
2026 buf[outlen++] = ScreenLines[i];
2028 /* handle double-byte single-width char */
2029 if (ScreenLines[i] == 0x8e)
2030 buf[outlen++] = ScreenLines2[i];
2031 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2032 buf[outlen++] = ScreenLines[++i];
2035 buf[outlen] = NUL; /* only to aid debugging */
2036 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2037 vim_free(buf);
2039 return retval;
2041 else
2043 return gui_outstr_nowrap(&ScreenLines[off], len,
2044 flags, fg, bg, back);
2047 #endif /* HAVE_GTK2 */
2050 * Output the given string at the current cursor position. If the string is
2051 * too long to fit on the line, then it is truncated.
2052 * "flags":
2053 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2054 * actually draw (an inverted) cursor.
2055 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
2056 * background.
2057 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2058 * it.
2059 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2060 * FAIL (the caller should start drawing "back" chars back).
2063 gui_outstr_nowrap(s, len, flags, fg, bg, back)
2064 char_u *s;
2065 int len;
2066 int flags;
2067 guicolor_T fg, bg; /* colors for cursor */
2068 int back; /* backup this many chars when using bold trick */
2070 long_u highlight_mask;
2071 long_u hl_mask_todo;
2072 guicolor_T fg_color;
2073 guicolor_T bg_color;
2074 guicolor_T sp_color;
2075 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2076 GuiFont font = NOFONT;
2077 # ifdef FEAT_XFONTSET
2078 GuiFontset fontset = NOFONTSET;
2079 # endif
2080 #endif
2081 attrentry_T *aep = NULL;
2082 int draw_flags;
2083 int col = gui.col;
2084 #ifdef FEAT_SIGN_ICONS
2085 int draw_sign = FALSE;
2086 # ifdef FEAT_NETBEANS_INTG
2087 int multi_sign = FALSE;
2088 # endif
2089 #endif
2091 if (len < 0)
2092 len = (int)STRLEN(s);
2093 if (len == 0)
2094 return OK;
2096 #ifdef FEAT_SIGN_ICONS
2097 if (*s == SIGN_BYTE
2098 # ifdef FEAT_NETBEANS_INTG
2099 || *s == MULTISIGN_BYTE
2100 # endif
2103 # ifdef FEAT_NETBEANS_INTG
2104 if (*s == MULTISIGN_BYTE)
2105 multi_sign = TRUE;
2106 # endif
2107 /* draw spaces instead */
2108 s = (char_u *)" ";
2109 if (len == 1 && col > 0)
2110 --col;
2111 len = 2;
2112 draw_sign = TRUE;
2113 highlight_mask = 0;
2115 else
2116 #endif
2117 if (gui.highlight_mask > HL_ALL)
2119 aep = syn_gui_attr2entry(gui.highlight_mask);
2120 if (aep == NULL) /* highlighting not set */
2121 highlight_mask = 0;
2122 else
2123 highlight_mask = aep->ae_attr;
2125 else
2126 highlight_mask = gui.highlight_mask;
2127 hl_mask_todo = highlight_mask;
2129 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2130 /* Set the font */
2131 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2132 font = aep->ae_u.gui.font;
2133 # ifdef FEAT_XFONTSET
2134 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2135 fontset = aep->ae_u.gui.fontset;
2136 # endif
2137 else
2139 # ifdef FEAT_XFONTSET
2140 if (gui.fontset != NOFONTSET)
2141 fontset = gui.fontset;
2142 else
2143 # endif
2144 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2146 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2148 font = gui.boldital_font;
2149 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2151 else if (gui.bold_font != NOFONT)
2153 font = gui.bold_font;
2154 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2156 else
2157 font = gui.norm_font;
2159 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2161 font = gui.ital_font;
2162 hl_mask_todo &= ~HL_ITALIC;
2164 else
2165 font = gui.norm_font;
2167 # ifdef FEAT_XFONTSET
2168 if (fontset != NOFONTSET)
2169 gui_mch_set_fontset(fontset);
2170 else
2171 # endif
2172 gui_mch_set_font(font);
2173 #endif
2175 draw_flags = 0;
2177 /* Set the color */
2178 bg_color = gui.back_pixel;
2179 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2181 draw_flags |= DRAW_CURSOR;
2182 fg_color = fg;
2183 bg_color = bg;
2184 sp_color = fg;
2186 else if (aep != NULL)
2188 fg_color = aep->ae_u.gui.fg_color;
2189 if (fg_color == INVALCOLOR)
2190 fg_color = gui.norm_pixel;
2191 bg_color = aep->ae_u.gui.bg_color;
2192 if (bg_color == INVALCOLOR)
2193 bg_color = gui.back_pixel;
2194 sp_color = aep->ae_u.gui.sp_color;
2195 if (sp_color == INVALCOLOR)
2196 sp_color = fg_color;
2198 else
2200 fg_color = gui.norm_pixel;
2201 sp_color = fg_color;
2204 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2206 #if defined(AMIGA) || defined(RISCOS)
2207 gui_mch_set_colors(bg_color, fg_color);
2208 #else
2209 gui_mch_set_fg_color(bg_color);
2210 gui_mch_set_bg_color(fg_color);
2211 #endif
2213 else
2215 #if defined(AMIGA) || defined(RISCOS)
2216 gui_mch_set_colors(fg_color, bg_color);
2217 #else
2218 gui_mch_set_fg_color(fg_color);
2219 gui_mch_set_bg_color(bg_color);
2220 #endif
2222 gui_mch_set_sp_color(sp_color);
2224 /* Clear the selection if we are about to write over it */
2225 if (!(flags & GUI_MON_NOCLEAR))
2226 clip_may_clear_selection(gui.row, gui.row);
2229 #ifndef MSWIN16_FASTTEXT
2230 /* If there's no bold font, then fake it */
2231 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2232 draw_flags |= DRAW_BOLD;
2233 #endif
2236 * When drawing bold or italic characters the spill-over from the left
2237 * neighbor may be destroyed. Let the caller backup to start redrawing
2238 * just after a blank.
2240 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2241 return FAIL;
2243 #if defined(RISCOS) || defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
2244 /* If there's no italic font, then fake it.
2245 * For GTK2, we don't need a different font for italic style. */
2246 if (hl_mask_todo & HL_ITALIC)
2247 draw_flags |= DRAW_ITALIC;
2249 /* Do we underline the text? */
2250 if (hl_mask_todo & HL_UNDERLINE)
2251 draw_flags |= DRAW_UNDERL;
2252 #else
2253 /* Do we underline the text? */
2254 if ((hl_mask_todo & HL_UNDERLINE)
2255 # ifndef MSWIN16_FASTTEXT
2256 || (hl_mask_todo & HL_ITALIC)
2257 # endif
2259 draw_flags |= DRAW_UNDERL;
2260 #endif
2261 /* Do we undercurl the text? */
2262 if (hl_mask_todo & HL_UNDERCURL)
2263 draw_flags |= DRAW_UNDERC;
2265 /* Do we draw transparently? */
2266 if (flags & GUI_MON_TRS_CURSOR)
2267 draw_flags |= DRAW_TRANSP;
2270 * Draw the text.
2272 #ifdef HAVE_GTK2
2273 /* The value returned is the length in display cells */
2274 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2275 #elif defined(FEAT_GUI_MACVIM) && defined(FEAT_MBYTE)
2276 /* The value returned is the length in display cells */
2277 len = gui_macvim_draw_string(gui.row, col, s, len, draw_flags);
2278 #else
2279 # ifdef FEAT_MBYTE
2280 if (enc_utf8)
2282 int start; /* index of bytes to be drawn */
2283 int cells; /* cellwidth of bytes to be drawn */
2284 int thislen; /* length of bytes to be drawin */
2285 int cn; /* cellwidth of current char */
2286 int i; /* index of current char */
2287 int c; /* current char value */
2288 int cl; /* byte length of current char */
2289 int comping; /* current char is composing */
2290 int scol = col; /* screen column */
2291 int dowide; /* use 'guifontwide' */
2293 /* Break the string at a composing character, it has to be drawn on
2294 * top of the previous character. */
2295 start = 0;
2296 cells = 0;
2297 for (i = 0; i < len; i += cl)
2299 c = utf_ptr2char(s + i);
2300 cn = utf_char2cells(c);
2301 if (cn > 1
2302 # ifdef FEAT_XFONTSET
2303 && fontset == NOFONTSET
2304 # endif
2305 && gui.wide_font != NOFONT)
2306 dowide = TRUE;
2307 else
2308 dowide = FALSE;
2309 comping = utf_iscomposing(c);
2310 if (!comping) /* count cells from non-composing chars */
2311 cells += cn;
2312 cl = utf_ptr2len(s + i);
2313 if (cl == 0) /* hit end of string */
2314 len = i + cl; /* len must be wrong "cannot happen" */
2316 /* print the string so far if it's the last character or there is
2317 * a composing character. */
2318 if (i + cl >= len || (comping && i > start) || dowide
2319 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2320 || (cn > 1
2321 # ifdef FEAT_XFONTSET
2322 /* No fontset: At least draw char after wide char at
2323 * right position. */
2324 && fontset == NOFONTSET
2325 # endif
2327 # endif
2330 if (comping || dowide)
2331 thislen = i - start;
2332 else
2333 thislen = i - start + cl;
2334 if (thislen > 0)
2336 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2337 draw_flags);
2338 start += thislen;
2340 scol += cells;
2341 cells = 0;
2342 if (dowide)
2344 gui_mch_set_font(gui.wide_font);
2345 gui_mch_draw_string(gui.row, scol - cn,
2346 s + start, cl, draw_flags);
2347 gui_mch_set_font(font);
2348 start += cl;
2351 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2352 /* No fontset: draw a space to fill the gap after a wide char
2353 * */
2354 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2355 # ifdef FEAT_XFONTSET
2356 && fontset == NOFONTSET
2357 # endif
2358 && !dowide)
2359 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2360 1, draw_flags);
2361 # endif
2363 /* Draw a composing char on top of the previous char. */
2364 if (comping)
2366 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2367 /* Carbon ATSUI autodraws composing char over previous char */
2368 gui_mch_draw_string(gui.row, scol, s + i, cl,
2369 draw_flags | DRAW_TRANSP);
2370 # else
2371 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2372 draw_flags | DRAW_TRANSP);
2373 # endif
2374 start = i + cl;
2377 /* The stuff below assumes "len" is the length in screen columns. */
2378 len = scol - col;
2380 else
2381 # endif
2383 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2384 # ifdef FEAT_MBYTE
2385 if (enc_dbcs == DBCS_JPNU)
2387 int clen = 0;
2388 int i;
2390 /* Get the length in display cells, this can be different from the
2391 * number of bytes for "euc-jp". */
2392 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2393 clen += (*mb_ptr2cells)(s + i);
2394 len = clen;
2396 # endif
2398 #endif /* !HAVE_GTK2 */
2400 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2401 gui.col = col + len;
2403 /* May need to invert it when it's part of the selection. */
2404 if (flags & GUI_MON_NOCLEAR)
2405 clip_may_redraw_selection(gui.row, col, len);
2407 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2409 /* Invalidate the old physical cursor position if we wrote over it */
2410 if (gui.cursor_row == gui.row
2411 && gui.cursor_col >= col
2412 && gui.cursor_col < col + len)
2413 gui.cursor_is_valid = FALSE;
2416 #ifdef FEAT_SIGN_ICONS
2417 if (draw_sign)
2418 /* Draw the sign on top of the spaces. */
2419 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2420 # ifdef FEAT_NETBEANS_INTG
2421 if (multi_sign)
2422 netbeans_draw_multisign_indicator(gui.row);
2423 # endif
2424 #endif
2426 return OK;
2430 * Un-draw the cursor. Actually this just redraws the character at the given
2431 * position. The character just before it too, for when it was in bold.
2433 void
2434 gui_undraw_cursor()
2436 if (gui.cursor_is_valid)
2438 #ifdef FEAT_HANGULIN
2439 if (composing_hangul
2440 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2441 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2442 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2443 gui.norm_pixel, gui.back_pixel, 0);
2444 else
2446 #endif
2447 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2448 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2449 && gui.cursor_col > 0)
2450 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2451 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2452 #ifdef FEAT_HANGULIN
2453 if (composing_hangul)
2454 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2455 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2457 #endif
2458 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2459 * here in case it wasn't needed to undraw it. */
2460 gui.cursor_is_valid = FALSE;
2464 void
2465 gui_redraw(x, y, w, h)
2466 int x;
2467 int y;
2468 int w;
2469 int h;
2471 int row1, col1, row2, col2;
2473 row1 = Y_2_ROW(y);
2474 col1 = X_2_COL(x);
2475 row2 = Y_2_ROW(y + h - 1);
2476 col2 = X_2_COL(x + w - 1);
2478 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2481 * We may need to redraw the cursor, but don't take it upon us to change
2482 * its location after a scroll.
2483 * (maybe be more strict even and test col too?)
2484 * These things may be outside the update/clipping region and reality may
2485 * not reflect Vims internal ideas if these operations are clipped away.
2487 if (gui.row == gui.cursor_row)
2488 gui_update_cursor(TRUE, TRUE);
2492 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2493 * from col1 to col2 (inclusive).
2494 * Return TRUE when the character before the first drawn character has
2495 * different attributes (may have to be redrawn too).
2498 gui_redraw_block(row1, col1, row2, col2, flags)
2499 int row1;
2500 int col1;
2501 int row2;
2502 int col2;
2503 int flags; /* flags for gui_outstr_nowrap() */
2505 int old_row, old_col;
2506 long_u old_hl_mask;
2507 int off;
2508 sattr_T first_attr;
2509 int idx, len;
2510 int back, nback;
2511 int retval = FALSE;
2512 #ifdef FEAT_MBYTE
2513 int orig_col1, orig_col2;
2514 #endif
2516 /* Don't try to update when ScreenLines is not valid */
2517 if (!screen_cleared || ScreenLines == NULL)
2518 return retval;
2520 /* Don't try to draw outside the shell! */
2521 /* Check everything, strange values may be caused by a big border width */
2522 col1 = check_col(col1);
2523 col2 = check_col(col2);
2524 row1 = check_row(row1);
2525 row2 = check_row(row2);
2527 /* Remember where our cursor was */
2528 old_row = gui.row;
2529 old_col = gui.col;
2530 old_hl_mask = gui.highlight_mask;
2531 #ifdef FEAT_MBYTE
2532 orig_col1 = col1;
2533 orig_col2 = col2;
2534 #endif
2536 for (gui.row = row1; gui.row <= row2; gui.row++)
2538 #ifdef FEAT_MBYTE
2539 /* When only half of a double-wide character is in the block, include
2540 * the other half. */
2541 col1 = orig_col1;
2542 col2 = orig_col2;
2543 off = LineOffset[gui.row];
2544 if (enc_dbcs != 0)
2546 if (col1 > 0)
2547 col1 -= dbcs_screen_head_off(ScreenLines + off,
2548 ScreenLines + off + col1);
2549 col2 += dbcs_screen_tail_off(ScreenLines + off,
2550 ScreenLines + off + col2);
2552 else if (enc_utf8)
2554 if (ScreenLines[off + col1] == 0)
2555 --col1;
2556 # ifdef HAVE_GTK2
2557 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2558 ++col2;
2559 # endif
2561 #endif
2562 gui.col = col1;
2563 off = LineOffset[gui.row] + gui.col;
2564 len = col2 - col1 + 1;
2566 /* Find how many chars back this highlighting starts, or where a space
2567 * is. Needed for when the bold trick is used */
2568 for (back = 0; back < col1; ++back)
2569 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2570 || ScreenLines[off - 1 - back] == ' ')
2571 break;
2572 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2573 && ScreenLines[off - 1] != ' ');
2575 /* Break it up in strings of characters with the same attributes. */
2576 /* Print UTF-8 characters individually. */
2577 while (len > 0)
2579 first_attr = ScreenAttrs[off];
2580 gui.highlight_mask = first_attr;
2581 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2582 if (enc_utf8 && ScreenLinesUC[off] != 0)
2584 /* output multi-byte character separately */
2585 nback = gui_screenchar(off, flags,
2586 (guicolor_T)0, (guicolor_T)0, back);
2587 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2588 idx = 2;
2589 else
2590 idx = 1;
2592 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2594 /* output double-byte, single-width character separately */
2595 nback = gui_screenchar(off, flags,
2596 (guicolor_T)0, (guicolor_T)0, back);
2597 idx = 1;
2599 else
2600 #endif
2602 #ifdef HAVE_GTK2
2603 for (idx = 0; idx < len; ++idx)
2605 if (enc_utf8 && ScreenLines[off + idx] == 0)
2606 continue; /* skip second half of double-width char */
2607 if (ScreenAttrs[off + idx] != first_attr)
2608 break;
2610 /* gui_screenstr() takes care of multibyte chars */
2611 nback = gui_screenstr(off, idx, flags,
2612 (guicolor_T)0, (guicolor_T)0, back);
2613 #else
2614 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2615 idx++)
2617 # ifdef FEAT_MBYTE
2618 /* Stop at a multi-byte Unicode character. */
2619 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2620 break;
2621 if (enc_dbcs == DBCS_JPNU)
2623 /* Stop at a double-byte single-width char. */
2624 if (ScreenLines[off + idx] == 0x8e)
2625 break;
2626 if (len > 1 && (*mb_ptr2len)(ScreenLines
2627 + off + idx) == 2)
2628 ++idx; /* skip second byte of double-byte char */
2630 # endif
2632 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2633 (guicolor_T)0, (guicolor_T)0, back);
2634 #endif
2636 if (nback == FAIL)
2638 /* Must back up to start drawing where a bold or italic word
2639 * starts. */
2640 off -= back;
2641 len += back;
2642 gui.col -= back;
2644 else
2646 off += idx;
2647 len -= idx;
2649 back = 0;
2653 /* Put the cursor back where it was */
2654 gui.row = old_row;
2655 gui.col = old_col;
2656 gui.highlight_mask = (int)old_hl_mask;
2658 return retval;
2661 static void
2662 gui_delete_lines(row, count)
2663 int row;
2664 int count;
2666 if (count <= 0)
2667 return;
2669 if (row + count > gui.scroll_region_bot)
2670 /* Scrolled out of region, just blank the lines out */
2671 gui_clear_block(row, gui.scroll_region_left,
2672 gui.scroll_region_bot, gui.scroll_region_right);
2673 else
2675 gui_mch_delete_lines(row, count);
2677 /* If the cursor was in the deleted lines it's now gone. If the
2678 * cursor was in the scrolled lines adjust its position. */
2679 if (gui.cursor_row >= row
2680 && gui.cursor_col >= gui.scroll_region_left
2681 && gui.cursor_col <= gui.scroll_region_right)
2683 if (gui.cursor_row < row + count)
2684 gui.cursor_is_valid = FALSE;
2685 else if (gui.cursor_row <= gui.scroll_region_bot)
2686 gui.cursor_row -= count;
2691 static void
2692 gui_insert_lines(row, count)
2693 int row;
2694 int count;
2696 if (count <= 0)
2697 return;
2699 if (row + count > gui.scroll_region_bot)
2700 /* Scrolled out of region, just blank the lines out */
2701 gui_clear_block(row, gui.scroll_region_left,
2702 gui.scroll_region_bot, gui.scroll_region_right);
2703 else
2705 gui_mch_insert_lines(row, count);
2707 if (gui.cursor_row >= gui.row
2708 && gui.cursor_col >= gui.scroll_region_left
2709 && gui.cursor_col <= gui.scroll_region_right)
2711 if (gui.cursor_row <= gui.scroll_region_bot - count)
2712 gui.cursor_row += count;
2713 else if (gui.cursor_row <= gui.scroll_region_bot)
2714 gui.cursor_is_valid = FALSE;
2720 * The main GUI input routine. Waits for a character from the keyboard.
2721 * wtime == -1 Wait forever.
2722 * wtime == 0 Don't wait.
2723 * wtime > 0 Wait wtime milliseconds for a character.
2724 * Returns OK if a character was found to be available within the given time,
2725 * or FAIL otherwise.
2728 gui_wait_for_chars(wtime)
2729 long wtime;
2731 int retval;
2734 * If we're going to wait a bit, update the menus and mouse shape for the
2735 * current State.
2737 if (wtime != 0)
2739 #ifdef FEAT_MENU
2740 gui_update_menus(0);
2741 #endif
2744 gui_mch_update();
2745 if (input_available()) /* Got char, return immediately */
2746 return OK;
2747 if (wtime == 0) /* Don't wait for char */
2748 return FAIL;
2750 /* Before waiting, flush any output to the screen. */
2751 gui_mch_flush();
2753 if (wtime > 0)
2755 /* Blink when waiting for a character. Probably only does something
2756 * for showmatch() */
2757 gui_mch_start_blink();
2758 retval = gui_mch_wait_for_chars(wtime);
2759 gui_mch_stop_blink();
2760 return retval;
2764 * While we are waiting indefinitely for a character, blink the cursor.
2766 gui_mch_start_blink();
2768 retval = FAIL;
2770 * We may want to trigger the CursorHold event. First wait for
2771 * 'updatetime' and if nothing is typed within that time put the
2772 * K_CURSORHOLD key in the input buffer.
2774 if (gui_mch_wait_for_chars(p_ut) == OK)
2775 retval = OK;
2776 #ifdef FEAT_AUTOCMD
2777 else if (trigger_cursorhold())
2779 char_u buf[3];
2781 /* Put K_CURSORHOLD in the input buffer. */
2782 buf[0] = CSI;
2783 buf[1] = KS_EXTRA;
2784 buf[2] = (int)KE_CURSORHOLD;
2785 add_to_input_buf(buf, 3);
2787 retval = OK;
2789 #endif
2791 if (retval == FAIL)
2793 /* Blocking wait. */
2794 before_blocking();
2795 retval = gui_mch_wait_for_chars(-1L);
2798 gui_mch_stop_blink();
2799 return retval;
2803 * Fill p[4] with mouse coordinates encoded for check_termcode().
2805 static void
2806 fill_mouse_coord(p, col, row)
2807 char_u *p;
2808 int col;
2809 int row;
2811 p[0] = (char_u)(col / 128 + ' ' + 1);
2812 p[1] = (char_u)(col % 128 + ' ' + 1);
2813 p[2] = (char_u)(row / 128 + ' ' + 1);
2814 p[3] = (char_u)(row % 128 + ' ' + 1);
2818 * Generic mouse support function. Add a mouse event to the input buffer with
2819 * the given properties.
2820 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2821 * MOUSE_X1, MOUSE_X2
2822 * MOUSE_DRAG, or MOUSE_RELEASE.
2823 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2824 * x, y --- Coordinates of mouse in pixels.
2825 * repeated_click --- TRUE if this click comes only a short time after a
2826 * previous click.
2827 * modifiers --- Bit field which may be any of the following modifiers
2828 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2829 * This function will ignore drag events where the mouse has not moved to a new
2830 * character.
2832 void
2833 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2834 int button;
2835 int x;
2836 int y;
2837 int repeated_click;
2838 int_u modifiers;
2840 static int prev_row = 0, prev_col = 0;
2841 static int prev_button = -1;
2842 static int num_clicks = 1;
2843 char_u string[10];
2844 enum key_extra button_char;
2845 int row, col;
2846 #ifdef FEAT_CLIPBOARD
2847 int checkfor;
2848 int did_clip = FALSE;
2849 #endif
2852 * Scrolling may happen at any time, also while a selection is present.
2854 switch (button)
2856 case MOUSE_X1:
2857 button_char = KE_X1MOUSE;
2858 goto button_set;
2859 case MOUSE_X2:
2860 button_char = KE_X2MOUSE;
2861 goto button_set;
2862 case MOUSE_4:
2863 button_char = KE_MOUSEDOWN;
2864 goto button_set;
2865 case MOUSE_5:
2866 button_char = KE_MOUSEUP;
2867 button_set:
2869 /* Don't put events in the input queue now. */
2870 if (hold_gui_events)
2871 return;
2873 string[3] = CSI;
2874 string[4] = KS_EXTRA;
2875 string[5] = (int)button_char;
2877 /* Pass the pointer coordinates of the scroll event so that we
2878 * know which window to scroll. */
2879 row = gui_xy2colrow(x, y, &col);
2880 string[6] = (char_u)(col / 128 + ' ' + 1);
2881 string[7] = (char_u)(col % 128 + ' ' + 1);
2882 string[8] = (char_u)(row / 128 + ' ' + 1);
2883 string[9] = (char_u)(row % 128 + ' ' + 1);
2885 if (modifiers == 0)
2886 add_to_input_buf(string + 3, 7);
2887 else
2889 string[0] = CSI;
2890 string[1] = KS_MODIFIER;
2891 string[2] = 0;
2892 if (modifiers & MOUSE_SHIFT)
2893 string[2] |= MOD_MASK_SHIFT;
2894 if (modifiers & MOUSE_CTRL)
2895 string[2] |= MOD_MASK_CTRL;
2896 if (modifiers & MOUSE_ALT)
2897 string[2] |= MOD_MASK_ALT;
2898 add_to_input_buf(string, 10);
2900 return;
2904 #ifdef FEAT_CLIPBOARD
2905 /* If a clipboard selection is in progress, handle it */
2906 if (clip_star.state == SELECT_IN_PROGRESS)
2908 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2909 return;
2912 /* Determine which mouse settings to look for based on the current mode */
2913 switch (get_real_state())
2915 case NORMAL_BUSY:
2916 case OP_PENDING:
2917 case NORMAL: checkfor = MOUSE_NORMAL; break;
2918 case VISUAL: checkfor = MOUSE_VISUAL; break;
2919 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2920 case REPLACE:
2921 case REPLACE+LANGMAP:
2922 #ifdef FEAT_VREPLACE
2923 case VREPLACE:
2924 case VREPLACE+LANGMAP:
2925 #endif
2926 case INSERT:
2927 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2928 case ASKMORE:
2929 case HITRETURN: /* At the more- and hit-enter prompt pass the
2930 mouse event for a click on or below the
2931 message line. */
2932 if (Y_2_ROW(y) >= msg_row)
2933 checkfor = MOUSE_NORMAL;
2934 else
2935 checkfor = MOUSE_RETURN;
2936 break;
2939 * On the command line, use the clipboard selection on all lines
2940 * but the command line. But not when pasting.
2942 case CMDLINE:
2943 case CMDLINE+LANGMAP:
2944 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2945 checkfor = MOUSE_NONE;
2946 else
2947 checkfor = MOUSE_COMMAND;
2948 break;
2950 default:
2951 checkfor = MOUSE_NONE;
2952 break;
2956 * Allow clipboard selection of text on the command line in "normal"
2957 * modes. Don't do this when dragging the status line, or extending a
2958 * Visual selection.
2960 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2961 && Y_2_ROW(y) >= topframe->fr_height
2962 # ifdef FEAT_WINDOWS
2963 + firstwin->w_winrow
2964 # endif
2965 && button != MOUSE_DRAG
2966 # ifdef FEAT_MOUSESHAPE
2967 && !drag_status_line
2968 # ifdef FEAT_VERTSPLIT
2969 && !drag_sep_line
2970 # endif
2971 # endif
2973 checkfor = MOUSE_NONE;
2976 * Use modeless selection when holding CTRL and SHIFT pressed.
2978 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2979 checkfor = MOUSE_NONEF;
2982 * In Ex mode, always use modeless selection.
2984 if (exmode_active)
2985 checkfor = MOUSE_NONE;
2988 * If the mouse settings say to not use the mouse, use the modeless
2989 * selection. But if Visual is active, assume that only the Visual area
2990 * will be selected.
2991 * Exception: On the command line, both the selection is used and a mouse
2992 * key is send.
2994 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2996 #ifdef FEAT_VISUAL
2997 /* Don't do modeless selection in Visual mode. */
2998 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2999 return;
3000 #endif
3003 * When 'mousemodel' is "popup", shift-left is translated to right.
3004 * But not when also using Ctrl.
3006 if (mouse_model_popup() && button == MOUSE_LEFT
3007 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3009 button = MOUSE_RIGHT;
3010 modifiers &= ~ MOUSE_SHIFT;
3013 /* If the selection is done, allow the right button to extend it.
3014 * If the selection is cleared, allow the right button to start it
3015 * from the cursor position. */
3016 if (button == MOUSE_RIGHT)
3018 if (clip_star.state == SELECT_CLEARED)
3020 if (State & CMDLINE)
3022 col = msg_col;
3023 row = msg_row;
3025 else
3027 col = curwin->w_wcol;
3028 row = curwin->w_wrow + W_WINROW(curwin);
3030 clip_start_selection(col, row, FALSE);
3032 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3033 repeated_click);
3034 did_clip = TRUE;
3036 /* Allow the left button to start the selection */
3037 else if (button ==
3038 # ifdef RISCOS
3039 /* Only start a drag on a drag event. Otherwise
3040 * we don't get a release event. */
3041 MOUSE_DRAG
3042 # else
3043 MOUSE_LEFT
3044 # endif
3047 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3048 did_clip = TRUE;
3050 # ifdef RISCOS
3051 else if (button == MOUSE_LEFT)
3053 clip_clear_selection();
3054 did_clip = TRUE;
3056 # endif
3058 /* Always allow pasting */
3059 if (button != MOUSE_MIDDLE)
3061 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3062 return;
3063 if (checkfor != MOUSE_COMMAND)
3064 button = MOUSE_LEFT;
3066 repeated_click = FALSE;
3069 if (clip_star.state != SELECT_CLEARED && !did_clip)
3070 clip_clear_selection();
3071 #endif
3073 /* Don't put events in the input queue now. */
3074 if (hold_gui_events)
3075 return;
3077 row = gui_xy2colrow(x, y, &col);
3080 * If we are dragging and the mouse hasn't moved far enough to be on a
3081 * different character, then don't send an event to vim.
3083 if (button == MOUSE_DRAG)
3085 if (row == prev_row && col == prev_col)
3086 return;
3087 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3088 if (y < 0)
3089 row = -1;
3093 * If topline has changed (window scrolled) since the last click, reset
3094 * repeated_click, because we don't want starting Visual mode when
3095 * clicking on a different character in the text.
3097 if (curwin->w_topline != gui_prev_topline
3098 #ifdef FEAT_DIFF
3099 || curwin->w_topfill != gui_prev_topfill
3100 #endif
3102 repeated_click = FALSE;
3104 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3105 string[1] = KS_MOUSE;
3106 string[2] = KE_FILLER;
3107 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3109 if (repeated_click)
3112 * Handle multiple clicks. They only count if the mouse is still
3113 * pointing at the same character.
3115 if (button != prev_button || row != prev_row || col != prev_col)
3116 num_clicks = 1;
3117 else if (++num_clicks > 4)
3118 num_clicks = 1;
3120 else
3121 num_clicks = 1;
3122 prev_button = button;
3123 gui_prev_topline = curwin->w_topline;
3124 #ifdef FEAT_DIFF
3125 gui_prev_topfill = curwin->w_topfill;
3126 #endif
3128 string[3] = (char_u)(button | 0x20);
3129 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3131 else
3132 string[3] = (char_u)button;
3134 string[3] |= modifiers;
3135 fill_mouse_coord(string + 4, col, row);
3136 add_to_input_buf(string, 8);
3138 if (row < 0)
3139 prev_row = 0;
3140 else
3141 prev_row = row;
3142 prev_col = col;
3145 * We need to make sure this is cleared since Athena doesn't tell us when
3146 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3148 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3149 gui.dragged_sb = SBAR_NONE;
3150 #endif
3154 * Convert x and y coordinate to column and row in text window.
3155 * Corrects for multi-byte character.
3156 * returns column in "*colp" and row as return value;
3159 gui_xy2colrow(x, y, colp)
3160 int x;
3161 int y;
3162 int *colp;
3164 int col = check_col(X_2_COL(x));
3165 int row = check_row(Y_2_ROW(y));
3167 #ifdef FEAT_MBYTE
3168 *colp = mb_fix_col(col, row);
3169 #else
3170 *colp = col;
3171 #endif
3172 return row;
3175 #if defined(FEAT_MENU) || defined(PROTO)
3177 * Callback function for when a menu entry has been selected.
3179 void
3180 gui_menu_cb(menu)
3181 vimmenu_T *menu;
3183 char_u bytes[sizeof(long_u)];
3185 /* Don't put events in the input queue now. */
3186 if (hold_gui_events)
3187 return;
3189 bytes[0] = CSI;
3190 bytes[1] = KS_MENU;
3191 bytes[2] = KE_FILLER;
3192 add_to_input_buf(bytes, 3);
3193 add_long_to_buf((long_u)menu, bytes);
3194 add_to_input_buf_csi(bytes, sizeof(long_u));
3196 #endif
3198 static int prev_which_scrollbars[3];
3201 * Set which components are present.
3202 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3203 * in p_go.
3205 /*ARGSUSED*/
3206 void
3207 gui_init_which_components(oldval)
3208 char_u *oldval;
3210 #ifdef FEAT_MENU
3211 static int prev_menu_is_active = -1;
3212 #endif
3213 #ifdef FEAT_TOOLBAR
3214 static int prev_toolbar = -1;
3215 int using_toolbar = FALSE;
3216 #endif
3217 #ifdef FEAT_GUI_TABLINE
3218 int using_tabline;
3219 #endif
3220 #ifdef FEAT_FOOTER
3221 static int prev_footer = -1;
3222 int using_footer = FALSE;
3223 #endif
3224 #if defined(FEAT_MENU) && !defined(WIN16)
3225 static int prev_tearoff = -1;
3226 int using_tearoff = FALSE;
3227 #endif
3229 char_u *p;
3230 int i;
3231 #ifdef FEAT_MENU
3232 int grey_old, grey_new;
3233 char_u *temp;
3234 #endif
3235 win_T *wp;
3236 int need_set_size;
3237 int fix_size;
3239 #ifdef FEAT_MENU
3240 if (oldval != NULL && gui.in_use)
3243 * Check if the menu's go from grey to non-grey or vise versa.
3245 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3246 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3247 if (grey_old != grey_new)
3249 temp = p_go;
3250 p_go = oldval;
3251 gui_update_menus(MENU_ALL_MODES);
3252 p_go = temp;
3255 gui.menu_is_active = FALSE;
3256 #endif
3258 for (i = 0; i < 3; i++)
3259 gui.which_scrollbars[i] = FALSE;
3260 for (p = p_go; *p; p++)
3261 switch (*p)
3263 case GO_LEFT:
3264 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3265 break;
3266 case GO_RIGHT:
3267 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3268 break;
3269 #ifdef FEAT_VERTSPLIT
3270 case GO_VLEFT:
3271 if (win_hasvertsplit())
3272 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3273 break;
3274 case GO_VRIGHT:
3275 if (win_hasvertsplit())
3276 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3277 break;
3278 #endif
3279 case GO_BOT:
3280 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3281 break;
3282 #ifdef FEAT_MENU
3283 case GO_MENUS:
3284 gui.menu_is_active = TRUE;
3285 break;
3286 #endif
3287 case GO_GREY:
3288 /* make menu's have grey items, ignored here */
3289 break;
3290 #ifdef FEAT_TOOLBAR
3291 case GO_TOOLBAR:
3292 using_toolbar = TRUE;
3293 break;
3294 #endif
3295 #ifdef FEAT_FOOTER
3296 case GO_FOOTER:
3297 using_footer = TRUE;
3298 break;
3299 #endif
3300 case GO_TEAROFF:
3301 #if defined(FEAT_MENU) && !defined(WIN16)
3302 using_tearoff = TRUE;
3303 #endif
3304 break;
3305 default:
3306 /* Ignore options that are not supported */
3307 break;
3310 if (gui.in_use)
3312 need_set_size = 0;
3313 fix_size = FALSE;
3315 #ifdef FEAT_GUI_TABLINE
3316 /* Update the GUI tab line, it may appear or disappear. This may
3317 * cause the non-GUI tab line to disappear or appear. */
3318 using_tabline = gui_has_tabline();
3319 if (!gui_mch_showing_tabline() != !using_tabline)
3321 /* We don't want a resize event change "Rows" here, save and
3322 * restore it. Resizing is handled below. */
3323 i = Rows;
3324 gui_update_tabline();
3325 Rows = i;
3326 need_set_size = RESIZE_VERT;
3327 if (using_tabline)
3328 fix_size = TRUE;
3329 if (!gui_use_tabline())
3330 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3332 #endif
3334 for (i = 0; i < 3; i++)
3336 /* The scrollbar needs to be updated when it is shown/unshown and
3337 * when switching tab pages. But the size only changes when it's
3338 * shown/unshown. Thus we need two places to remember whether a
3339 * scrollbar is there or not. */
3340 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3341 #ifdef FEAT_WINDOWS
3342 || gui.which_scrollbars[i]
3343 != curtab->tp_prev_which_scrollbars[i]
3344 #endif
3347 if (i == SBAR_BOTTOM)
3348 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3349 gui.which_scrollbars[i]);
3350 else
3352 FOR_ALL_WINDOWS(wp)
3354 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3357 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3359 if (i == SBAR_BOTTOM)
3360 need_set_size = RESIZE_VERT;
3361 else
3362 need_set_size = RESIZE_HOR;
3363 if (gui.which_scrollbars[i])
3364 fix_size = TRUE;
3367 #ifdef FEAT_WINDOWS
3368 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3369 #endif
3370 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3373 #ifdef FEAT_MENU
3374 if (gui.menu_is_active != prev_menu_is_active)
3376 /* We don't want a resize event change "Rows" here, save and
3377 * restore it. Resizing is handled below. */
3378 i = Rows;
3379 gui_mch_enable_menu(gui.menu_is_active);
3380 Rows = i;
3381 prev_menu_is_active = gui.menu_is_active;
3382 need_set_size = RESIZE_VERT;
3383 if (gui.menu_is_active)
3384 fix_size = TRUE;
3386 #endif
3388 #ifdef FEAT_TOOLBAR
3389 if (using_toolbar != prev_toolbar)
3391 gui_mch_show_toolbar(using_toolbar);
3392 prev_toolbar = using_toolbar;
3393 need_set_size = RESIZE_VERT;
3394 if (using_toolbar)
3395 fix_size = TRUE;
3397 #endif
3398 #ifdef FEAT_FOOTER
3399 if (using_footer != prev_footer)
3401 gui_mch_enable_footer(using_footer);
3402 prev_footer = using_footer;
3403 need_set_size = RESIZE_VERT;
3404 if (using_footer)
3405 fix_size = TRUE;
3407 #endif
3408 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3409 if (using_tearoff != prev_tearoff)
3411 gui_mch_toggle_tearoffs(using_tearoff);
3412 prev_tearoff = using_tearoff;
3414 #endif
3415 if (need_set_size)
3417 #ifdef FEAT_GUI_GTK
3418 long c = Columns;
3419 #endif
3420 /* Adjust the size of the window to make the text area keep the
3421 * same size and to avoid that part of our window is off-screen
3422 * and a scrollbar can't be used, for example. */
3423 gui_set_shellsize(FALSE, fix_size, need_set_size);
3425 #ifdef FEAT_GUI_GTK
3426 /* GTK has the annoying habit of sending us resize events when
3427 * changing the window size ourselves. This mostly happens when
3428 * waiting for a character to arrive, quite unpredictably, and may
3429 * change Columns and Rows when we don't want it. Wait for a
3430 * character here to avoid this effect.
3431 * If you remove this, please test this command for resizing
3432 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3433 * Don't do this while starting up though.
3434 * And don't change Rows, it may have be reduced intentionally
3435 * when adding menu/toolbar/tabline. */
3436 if (!gui.starting)
3437 (void)char_avail();
3438 Columns = c;
3439 #endif
3441 #ifdef FEAT_WINDOWS
3442 /* When the console tabline appears or disappears the window positions
3443 * change. */
3444 if (firstwin->w_winrow != tabline_height())
3445 shell_new_rows(); /* recompute window positions and heights */
3446 #endif
3450 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3452 * Return TRUE if the GUI is taking care of the tabline.
3453 * It may still be hidden if 'showtabline' is zero.
3456 gui_use_tabline()
3458 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3462 * Return TRUE if the GUI is showing the tabline.
3463 * This uses 'showtabline'.
3465 static int
3466 gui_has_tabline()
3468 if (!gui_use_tabline()
3469 || p_stal == 0
3470 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3471 return FALSE;
3472 return TRUE;
3476 * Update the tabline.
3477 * This may display/undisplay the tabline and update the labels.
3479 void
3480 gui_update_tabline()
3482 int showit = gui_has_tabline();
3483 int shown = gui_mch_showing_tabline();
3485 if (!gui.starting && starting == 0)
3487 /* Updating the tabline uses direct GUI commands, flush
3488 * outstanding instructions first. (esp. clear screen) */
3489 out_flush();
3490 gui_mch_flush();
3492 if (!showit != !shown)
3493 gui_mch_show_tabline(showit);
3494 if (showit != 0)
3495 gui_mch_update_tabline();
3497 /* When the tabs change from hidden to shown or from shown to
3498 * hidden the size of the text area should remain the same. */
3499 if (!showit != !shown)
3500 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3505 * Get the label or tooltip for tab page "tp" into NameBuff[].
3507 void
3508 get_tabline_label(tp, tooltip)
3509 tabpage_T *tp;
3510 int tooltip; /* TRUE: get tooltip */
3512 int modified = FALSE;
3513 char_u buf[40];
3514 int wincount;
3515 win_T *wp;
3516 char_u **opt;
3518 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3519 opt = (tooltip ? &p_gtt : &p_gtl);
3520 if (**opt != NUL)
3522 int use_sandbox = FALSE;
3523 int save_called_emsg = called_emsg;
3524 char_u res[MAXPATHL];
3525 tabpage_T *save_curtab;
3526 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3527 : "guitablabel");
3529 called_emsg = FALSE;
3531 printer_page_num = tabpage_index(tp);
3532 # ifdef FEAT_EVAL
3533 set_vim_var_nr(VV_LNUM, printer_page_num);
3534 use_sandbox = was_set_insecurely(opt_name, 0);
3535 # endif
3536 /* It's almost as going to the tabpage, but without autocommands. */
3537 curtab->tp_firstwin = firstwin;
3538 curtab->tp_lastwin = lastwin;
3539 curtab->tp_curwin = curwin;
3540 save_curtab = curtab;
3541 curtab = tp;
3542 topframe = curtab->tp_topframe;
3543 firstwin = curtab->tp_firstwin;
3544 lastwin = curtab->tp_lastwin;
3545 curwin = curtab->tp_curwin;
3546 curbuf = curwin->w_buffer;
3548 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3549 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3550 0, (int)Columns, NULL, NULL);
3551 STRCPY(NameBuff, res);
3553 /* Back to the original curtab. */
3554 curtab = save_curtab;
3555 topframe = curtab->tp_topframe;
3556 firstwin = curtab->tp_firstwin;
3557 lastwin = curtab->tp_lastwin;
3558 curwin = curtab->tp_curwin;
3559 curbuf = curwin->w_buffer;
3561 if (called_emsg)
3562 set_string_option_direct(opt_name, -1,
3563 (char_u *)"", OPT_FREE, SID_ERROR);
3564 called_emsg |= save_called_emsg;
3567 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3568 * use a default label. */
3569 if (**opt == NUL || *NameBuff == NUL)
3571 /* Get the buffer name into NameBuff[] and shorten it. */
3572 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3573 if (!tooltip)
3574 shorten_dir(NameBuff);
3576 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3577 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3578 if (bufIsChanged(wp->w_buffer))
3579 modified = TRUE;
3580 if (modified || wincount > 1)
3582 if (wincount > 1)
3583 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3584 else
3585 buf[0] = NUL;
3586 if (modified)
3587 STRCAT(buf, "+");
3588 STRCAT(buf, " ");
3589 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3590 mch_memmove(NameBuff, buf, STRLEN(buf));
3596 * Send the event for clicking to select tab page "nr".
3597 * Returns TRUE if it was done, FALSE when skipped because we are already at
3598 * that tab page or the cmdline window is open.
3601 send_tabline_event(nr)
3602 int nr;
3604 char_u string[3];
3606 if (nr == tabpage_index(curtab))
3607 return FALSE;
3609 /* Don't put events in the input queue now. */
3610 if (hold_gui_events
3611 # ifdef FEAT_CMDWIN
3612 || cmdwin_type != 0
3613 # endif
3616 /* Set it back to the current tab page. */
3617 gui_mch_set_curtab(tabpage_index(curtab));
3618 return FALSE;
3621 string[0] = CSI;
3622 string[1] = KS_TABLINE;
3623 string[2] = KE_FILLER;
3624 add_to_input_buf(string, 3);
3625 string[0] = nr;
3626 add_to_input_buf_csi(string, 1);
3627 return TRUE;
3631 * Send a tabline menu event
3633 void
3634 send_tabline_menu_event(tabidx, event)
3635 int tabidx;
3636 int event;
3638 char_u string[3];
3640 /* Don't put events in the input queue now. */
3641 if (hold_gui_events)
3642 return;
3644 string[0] = CSI;
3645 string[1] = KS_TABMENU;
3646 string[2] = KE_FILLER;
3647 add_to_input_buf(string, 3);
3648 string[0] = tabidx;
3649 string[1] = (char_u)(long)event;
3650 add_to_input_buf_csi(string, 2);
3653 #endif
3656 * Scrollbar stuff:
3659 #if defined(FEAT_WINDOWS) || defined(PROTO)
3661 * Remove all scrollbars. Used before switching to another tab page.
3663 void
3664 gui_remove_scrollbars()
3666 int i;
3667 win_T *wp;
3669 for (i = 0; i < 3; i++)
3671 if (i == SBAR_BOTTOM)
3672 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3673 else
3675 FOR_ALL_WINDOWS(wp)
3677 gui_do_scrollbar(wp, i, FALSE);
3680 curtab->tp_prev_which_scrollbars[i] = -1;
3683 #endif
3685 void
3686 gui_create_scrollbar(sb, type, wp)
3687 scrollbar_T *sb;
3688 int type;
3689 win_T *wp;
3691 static int sbar_ident = 0;
3693 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3694 sb->wp = wp;
3695 sb->type = type;
3696 sb->value = 0;
3697 #ifdef FEAT_GUI_ATHENA
3698 sb->pixval = 0;
3699 #endif
3700 sb->size = 1;
3701 sb->max = 1;
3702 sb->top = 0;
3703 sb->height = 0;
3704 #ifdef FEAT_VERTSPLIT
3705 sb->width = 0;
3706 #endif
3707 sb->status_height = 0;
3708 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3712 * Find the scrollbar with the given index.
3714 scrollbar_T *
3715 gui_find_scrollbar(ident)
3716 long ident;
3718 win_T *wp;
3720 if (gui.bottom_sbar.ident == ident)
3721 return &gui.bottom_sbar;
3722 FOR_ALL_WINDOWS(wp)
3724 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3725 return &wp->w_scrollbars[SBAR_LEFT];
3726 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3727 return &wp->w_scrollbars[SBAR_RIGHT];
3729 return NULL;
3733 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3735 * For Win32, Macintosh and GTK+ 2:
3736 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3737 * you stop dragging the scrollbar. We get here each time the scrollbar is
3738 * dragged another pixel, but as far as the rest of vim goes, it thinks
3739 * we're just hanging in the call to DispatchMessage() in
3740 * process_message(). The DispatchMessage() call that hangs was passed a
3741 * mouse button click event in the scrollbar window. -- webb.
3743 * Solution: Do the scrolling right here. But only when allowed.
3744 * Ignore the scrollbars while executing an external command or when there
3745 * are still characters to be processed.
3747 void
3748 gui_drag_scrollbar(sb, value, still_dragging)
3749 scrollbar_T *sb;
3750 long value;
3751 int still_dragging;
3753 #ifdef FEAT_WINDOWS
3754 win_T *wp;
3755 #endif
3756 int sb_num;
3757 #ifdef USE_ON_FLY_SCROLL
3758 colnr_T old_leftcol = curwin->w_leftcol;
3759 # ifdef FEAT_SCROLLBIND
3760 linenr_T old_topline = curwin->w_topline;
3761 # endif
3762 # ifdef FEAT_DIFF
3763 int old_topfill = curwin->w_topfill;
3764 # endif
3765 #else
3766 char_u bytes[sizeof(long_u)];
3767 int byte_count;
3768 #endif
3770 if (sb == NULL)
3771 return;
3773 /* Don't put events in the input queue now. */
3774 if (hold_gui_events)
3775 return;
3777 #ifdef FEAT_CMDWIN
3778 if (cmdwin_type != 0 && sb->wp != curwin)
3779 return;
3780 #endif
3782 if (still_dragging)
3784 if (sb->wp == NULL)
3785 gui.dragged_sb = SBAR_BOTTOM;
3786 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3787 gui.dragged_sb = SBAR_LEFT;
3788 else
3789 gui.dragged_sb = SBAR_RIGHT;
3790 gui.dragged_wp = sb->wp;
3792 else
3794 gui.dragged_sb = SBAR_NONE;
3795 #ifdef HAVE_GTK2
3796 /* Keep the "dragged_wp" value until after the scrolling, for when the
3797 * moust button is released. GTK2 doesn't send the button-up event. */
3798 gui.dragged_wp = NULL;
3799 #endif
3802 /* Vertical sbar info is kept in the first sbar (the left one) */
3803 if (sb->wp != NULL)
3804 sb = &sb->wp->w_scrollbars[0];
3807 * Check validity of value
3809 if (value < 0)
3810 value = 0;
3811 #ifdef SCROLL_PAST_END
3812 else if (value > sb->max)
3813 value = sb->max;
3814 #else
3815 if (value > sb->max - sb->size + 1)
3816 value = sb->max - sb->size + 1;
3817 #endif
3819 sb->value = value;
3821 #ifdef USE_ON_FLY_SCROLL
3822 /* When not allowed to do the scrolling right now, return. */
3823 if (dont_scroll || input_available())
3824 return;
3825 #endif
3826 #ifdef FEAT_INS_EXPAND
3827 /* Disallow scrolling the current window when the completion popup menu is
3828 * visible. */
3829 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3830 return;
3831 #endif
3833 #ifdef FEAT_RIGHTLEFT
3834 if (sb->wp == NULL && curwin->w_p_rl)
3836 value = sb->max + 1 - sb->size - value;
3837 if (value < 0)
3838 value = 0;
3840 #endif
3842 if (sb->wp != NULL) /* vertical scrollbar */
3844 sb_num = 0;
3845 #ifdef FEAT_WINDOWS
3846 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3847 sb_num++;
3848 if (wp == NULL)
3849 return;
3850 #else
3851 if (sb->wp != curwin)
3852 return;
3853 #endif
3855 #ifdef USE_ON_FLY_SCROLL
3856 current_scrollbar = sb_num;
3857 scrollbar_value = value;
3858 if (State & NORMAL)
3860 gui_do_scroll();
3861 setcursor();
3863 else if (State & INSERT)
3865 ins_scroll();
3866 setcursor();
3868 else if (State & CMDLINE)
3870 if (msg_scrolled == 0)
3872 gui_do_scroll();
3873 redrawcmdline();
3876 # ifdef FEAT_FOLDING
3877 /* Value may have been changed for closed fold. */
3878 sb->value = sb->wp->w_topline - 1;
3879 # endif
3881 /* When dragging one scrollbar and there is another one at the other
3882 * side move the thumb of that one too. */
3883 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3884 gui_mch_set_scrollbar_thumb(
3885 &sb->wp->w_scrollbars[
3886 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3887 ? SBAR_LEFT : SBAR_RIGHT],
3888 sb->value, sb->size, sb->max);
3890 #else
3891 bytes[0] = CSI;
3892 bytes[1] = KS_VER_SCROLLBAR;
3893 bytes[2] = KE_FILLER;
3894 bytes[3] = (char_u)sb_num;
3895 byte_count = 4;
3896 #endif
3898 else
3900 #ifdef USE_ON_FLY_SCROLL
3901 scrollbar_value = value;
3903 if (State & NORMAL)
3904 gui_do_horiz_scroll();
3905 else if (State & INSERT)
3906 ins_horscroll();
3907 else if (State & CMDLINE)
3909 if (msg_scrolled == 0)
3911 gui_do_horiz_scroll();
3912 redrawcmdline();
3915 if (old_leftcol != curwin->w_leftcol)
3917 updateWindow(curwin); /* update window, status and cmdline */
3918 setcursor();
3920 #else
3921 bytes[0] = CSI;
3922 bytes[1] = KS_HOR_SCROLLBAR;
3923 bytes[2] = KE_FILLER;
3924 byte_count = 3;
3925 #endif
3928 #ifdef USE_ON_FLY_SCROLL
3929 # ifdef FEAT_SCROLLBIND
3931 * synchronize other windows, as necessary according to 'scrollbind'
3933 if (curwin->w_p_scb
3934 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3935 || (sb->wp == curwin && (curwin->w_topline != old_topline
3936 # ifdef FEAT_DIFF
3937 || curwin->w_topfill != old_topfill
3938 # endif
3939 ))))
3941 do_check_scrollbind(TRUE);
3942 /* need to update the window right here */
3943 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3944 if (wp->w_redr_type > 0)
3945 updateWindow(wp);
3946 setcursor();
3948 # endif
3949 out_flush();
3950 gui_update_cursor(FALSE, TRUE);
3951 #else
3952 add_to_input_buf(bytes, byte_count);
3953 add_long_to_buf((long_u)value, bytes);
3954 add_to_input_buf_csi(bytes, sizeof(long_u));
3955 #endif
3959 * Scrollbar stuff:
3962 void
3963 gui_update_scrollbars(force)
3964 int force; /* Force all scrollbars to get updated */
3966 win_T *wp;
3967 scrollbar_T *sb;
3968 long val, size, max; /* need 32 bits here */
3969 int which_sb;
3970 int h, y;
3971 #ifdef FEAT_VERTSPLIT
3972 static win_T *prev_curwin = NULL;
3973 #endif
3975 /* Update the horizontal scrollbar */
3976 gui_update_horiz_scrollbar(force);
3978 #ifndef WIN3264
3979 /* Return straight away if there is neither a left nor right scrollbar.
3980 * On MS-Windows this is required anyway for scrollwheel messages. */
3981 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3982 return;
3983 #endif
3986 * Don't want to update a scrollbar while we're dragging it. But if we
3987 * have both a left and right scrollbar, and we drag one of them, we still
3988 * need to update the other one.
3990 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3991 && gui.which_scrollbars[SBAR_LEFT]
3992 && gui.which_scrollbars[SBAR_RIGHT])
3995 * If we have two scrollbars and one of them is being dragged, just
3996 * copy the scrollbar position from the dragged one to the other one.
3998 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3999 if (gui.dragged_wp != NULL)
4000 gui_mch_set_scrollbar_thumb(
4001 &gui.dragged_wp->w_scrollbars[which_sb],
4002 gui.dragged_wp->w_scrollbars[0].value,
4003 gui.dragged_wp->w_scrollbars[0].size,
4004 gui.dragged_wp->w_scrollbars[0].max);
4007 /* avoid that moving components around generates events */
4008 ++hold_gui_events;
4010 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4012 if (wp->w_buffer == NULL) /* just in case */
4013 continue;
4014 /* Skip a scrollbar that is being dragged. */
4015 if (!force && (gui.dragged_sb == SBAR_LEFT
4016 || gui.dragged_sb == SBAR_RIGHT)
4017 && gui.dragged_wp == wp)
4018 continue;
4020 #ifdef SCROLL_PAST_END
4021 max = wp->w_buffer->b_ml.ml_line_count - 1;
4022 #else
4023 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4024 #endif
4025 if (max < 0) /* empty buffer */
4026 max = 0;
4027 val = wp->w_topline - 1;
4028 size = wp->w_height;
4029 #ifdef SCROLL_PAST_END
4030 if (val > max) /* just in case */
4031 val = max;
4032 #else
4033 if (size > max + 1) /* just in case */
4034 size = max + 1;
4035 if (val > max - size + 1)
4036 val = max - size + 1;
4037 #endif
4038 if (val < 0) /* minimal value is 0 */
4039 val = 0;
4042 * Scrollbar at index 0 (the left one) contains all the information.
4043 * It would be the same info for left and right so we just store it for
4044 * one of them.
4046 sb = &wp->w_scrollbars[0];
4049 * Note: no check for valid w_botline. If it's not valid the
4050 * scrollbars will be updated later anyway.
4052 if (size < 1 || wp->w_botline - 2 > max)
4055 * This can happen during changing files. Just don't update the
4056 * scrollbar for now.
4058 sb->height = 0; /* Force update next time */
4059 if (gui.which_scrollbars[SBAR_LEFT])
4060 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4061 if (gui.which_scrollbars[SBAR_RIGHT])
4062 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4063 continue;
4065 if (force || sb->height != wp->w_height
4066 #ifdef FEAT_WINDOWS
4067 || sb->top != wp->w_winrow
4068 || sb->status_height != wp->w_status_height
4069 # ifdef FEAT_VERTSPLIT
4070 || sb->width != wp->w_width
4071 || prev_curwin != curwin
4072 # endif
4073 #endif
4076 /* Height, width or position of scrollbar has changed. For
4077 * vertical split: curwin changed. */
4078 sb->height = wp->w_height;
4079 #ifdef FEAT_WINDOWS
4080 sb->top = wp->w_winrow;
4081 sb->status_height = wp->w_status_height;
4082 # ifdef FEAT_VERTSPLIT
4083 sb->width = wp->w_width;
4084 # endif
4085 #endif
4087 /* Calculate height and position in pixels */
4088 h = (sb->height + sb->status_height) * gui.char_height;
4089 y = sb->top * gui.char_height + gui.border_offset;
4090 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4091 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MACVIM))
4092 if (gui.menu_is_active)
4093 y += gui.menu_height;
4094 #endif
4096 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4097 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4098 # ifdef FEAT_GUI_ATHENA
4099 y += gui.toolbar_height;
4100 # else
4101 # ifdef FEAT_GUI_MSWIN
4102 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4103 # endif
4104 # endif
4105 #endif
4107 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4108 if (gui_has_tabline())
4109 y += gui.tabline_height;
4110 #endif
4112 #ifdef FEAT_WINDOWS
4113 if (wp->w_winrow == 0)
4114 #endif
4116 /* Height of top scrollbar includes width of top border */
4117 h += gui.border_offset;
4118 y -= gui.border_offset;
4120 if (gui.which_scrollbars[SBAR_LEFT])
4122 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4123 gui.left_sbar_x, y,
4124 gui.scrollbar_width, h);
4125 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4127 if (gui.which_scrollbars[SBAR_RIGHT])
4129 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4130 gui.right_sbar_x, y,
4131 gui.scrollbar_width, h);
4132 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4136 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4137 * checking if the thumb moved at least a pixel. Only do this for
4138 * Athena, most other GUIs require the update anyway to make the
4139 * arrows work. */
4140 #ifdef FEAT_GUI_ATHENA
4141 if (max == 0)
4142 y = 0;
4143 else
4144 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4145 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4146 #else
4147 if (force || sb->value != val || sb->size != size || sb->max != max)
4148 #endif
4150 /* Thumb of scrollbar has moved */
4151 sb->value = val;
4152 #ifdef FEAT_GUI_ATHENA
4153 sb->pixval = y;
4154 #endif
4155 sb->size = size;
4156 sb->max = max;
4157 if (gui.which_scrollbars[SBAR_LEFT]
4158 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4159 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4160 val, size, max);
4161 if (gui.which_scrollbars[SBAR_RIGHT]
4162 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4163 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4164 val, size, max);
4167 #ifdef FEAT_VERTSPLIT
4168 prev_curwin = curwin;
4169 #endif
4170 --hold_gui_events;
4174 * Enable or disable a scrollbar.
4175 * Check for scrollbars for vertically split windows which are not enabled
4176 * sometimes.
4178 static void
4179 gui_do_scrollbar(wp, which, enable)
4180 win_T *wp;
4181 int which; /* SBAR_LEFT or SBAR_RIGHT */
4182 int enable; /* TRUE to enable scrollbar */
4184 #ifdef FEAT_VERTSPLIT
4185 int midcol = curwin->w_wincol + curwin->w_width / 2;
4186 int has_midcol = (wp->w_wincol <= midcol
4187 && wp->w_wincol + wp->w_width >= midcol);
4189 /* Only enable scrollbars that contain the middle column of the current
4190 * window. */
4191 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4193 /* Scrollbars only on one side. Don't enable scrollbars that don't
4194 * contain the middle column of the current window. */
4195 if (!has_midcol)
4196 enable = FALSE;
4198 else
4200 /* Scrollbars on both sides. Don't enable scrollbars that neither
4201 * contain the middle column of the current window nor are on the far
4202 * side. */
4203 if (midcol > Columns / 2)
4205 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4206 enable = FALSE;
4208 else
4210 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4211 : !has_midcol)
4212 enable = FALSE;
4215 #endif
4216 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4220 * Scroll a window according to the values set in the globals current_scrollbar
4221 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4222 * or FALSE otherwise.
4225 gui_do_scroll()
4227 win_T *wp, *save_wp;
4228 int i;
4229 long nlines;
4230 pos_T old_cursor;
4231 linenr_T old_topline;
4232 #ifdef FEAT_DIFF
4233 int old_topfill;
4234 #endif
4236 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4237 if (wp == NULL)
4238 break;
4239 if (wp == NULL)
4240 /* Couldn't find window */
4241 return FALSE;
4244 * Compute number of lines to scroll. If zero, nothing to do.
4246 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4247 if (nlines == 0)
4248 return FALSE;
4250 save_wp = curwin;
4251 old_topline = wp->w_topline;
4252 #ifdef FEAT_DIFF
4253 old_topfill = wp->w_topfill;
4254 #endif
4255 old_cursor = wp->w_cursor;
4256 curwin = wp;
4257 curbuf = wp->w_buffer;
4258 if (nlines < 0)
4259 scrolldown(-nlines, gui.dragged_wp == NULL);
4260 else
4261 scrollup(nlines, gui.dragged_wp == NULL);
4262 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4263 * the mouse-up event already, but we still want it to behave like when
4264 * dragging. But not the next click in an arrow. */
4265 if (gui.dragged_sb == SBAR_NONE)
4266 gui.dragged_wp = NULL;
4268 if (old_topline != wp->w_topline
4269 #ifdef FEAT_DIFF
4270 || old_topfill != wp->w_topfill
4271 #endif
4274 if (p_so != 0)
4276 cursor_correct(); /* fix window for 'so' */
4277 update_topline(); /* avoid up/down jump */
4279 if (old_cursor.lnum != wp->w_cursor.lnum)
4280 coladvance(wp->w_curswant);
4281 #ifdef FEAT_SCROLLBIND
4282 wp->w_scbind_pos = wp->w_topline;
4283 #endif
4286 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4287 validate_cursor();
4289 curwin = save_wp;
4290 curbuf = save_wp->w_buffer;
4293 * Don't call updateWindow() when nothing has changed (it will overwrite
4294 * the status line!).
4296 if (old_topline != wp->w_topline
4297 || wp->w_redr_type != 0
4298 #ifdef FEAT_DIFF
4299 || old_topfill != wp->w_topfill
4300 #endif
4303 int type = VALID;
4305 #ifdef FEAT_INS_EXPAND
4306 if (pum_visible())
4308 type = NOT_VALID;
4309 wp->w_lines_valid = 0;
4311 #endif
4312 /* Don't set must_redraw here, it may cause the popup menu to
4313 * disappear when losing focus after a scrollbar drag. */
4314 if (wp->w_redr_type < type)
4315 wp->w_redr_type = type;
4316 updateWindow(wp); /* update window, status line, and cmdline */
4319 #ifdef FEAT_INS_EXPAND
4320 /* May need to redraw the popup menu. */
4321 if (pum_visible())
4322 pum_redraw();
4323 #endif
4325 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4330 * Horizontal scrollbar stuff:
4334 * Return length of line "lnum" for horizontal scrolling.
4336 static colnr_T
4337 scroll_line_len(lnum)
4338 linenr_T lnum;
4340 char_u *p;
4341 colnr_T col;
4342 int w;
4344 p = ml_get(lnum);
4345 col = 0;
4346 if (*p != NUL)
4347 for (;;)
4349 w = chartabsize(p, col);
4350 mb_ptr_adv(p);
4351 if (*p == NUL) /* don't count the last character */
4352 break;
4353 col += w;
4355 return col;
4358 /* Remember which line is currently the longest, so that we don't have to
4359 * search for it when scrolling horizontally. */
4360 static linenr_T longest_lnum = 0;
4362 static void
4363 gui_update_horiz_scrollbar(force)
4364 int force;
4366 long value, size, max; /* need 32 bit ints here */
4368 if (!gui.which_scrollbars[SBAR_BOTTOM])
4369 return;
4371 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4372 return;
4374 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4375 return;
4378 * It is possible for the cursor to be invalid if we're in the middle of
4379 * something (like changing files). If so, don't do anything for now.
4381 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4383 gui.bottom_sbar.value = -1;
4384 return;
4387 size = W_WIDTH(curwin);
4388 if (curwin->w_p_wrap)
4390 value = 0;
4391 #ifdef SCROLL_PAST_END
4392 max = 0;
4393 #else
4394 max = W_WIDTH(curwin) - 1;
4395 #endif
4397 else
4399 value = curwin->w_leftcol;
4401 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4402 * line numbers, topline and botline can be invalid when displaying is
4403 * postponed. */
4404 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4405 && curwin->w_topline <= curwin->w_cursor.lnum
4406 && curwin->w_botline > curwin->w_cursor.lnum
4407 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4409 linenr_T lnum;
4410 colnr_T n;
4412 /* Use maximum of all visible lines. Remember the lnum of the
4413 * longest line, clostest to the cursor line. Used when scrolling
4414 * below. */
4415 max = 0;
4416 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4418 n = scroll_line_len(lnum);
4419 if (n > (colnr_T)max)
4421 max = n;
4422 longest_lnum = lnum;
4424 else if (n == (colnr_T)max
4425 && abs((int)(lnum - curwin->w_cursor.lnum))
4426 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4427 longest_lnum = lnum;
4430 else
4431 /* Use cursor line only. */
4432 max = scroll_line_len(curwin->w_cursor.lnum);
4433 #ifdef FEAT_VIRTUALEDIT
4434 if (virtual_active())
4436 /* May move the cursor even further to the right. */
4437 if (curwin->w_virtcol >= (colnr_T)max)
4438 max = curwin->w_virtcol;
4440 #endif
4442 #ifndef SCROLL_PAST_END
4443 max += W_WIDTH(curwin) - 1;
4444 #endif
4445 /* The line number isn't scrolled, thus there is less space when
4446 * 'number' is set (also for 'foldcolumn'). */
4447 size -= curwin_col_off();
4448 #ifndef SCROLL_PAST_END
4449 max -= curwin_col_off();
4450 #endif
4453 #ifndef SCROLL_PAST_END
4454 if (value > max - size + 1)
4455 value = max - size + 1; /* limit the value to allowable range */
4456 #endif
4458 #ifdef FEAT_RIGHTLEFT
4459 if (curwin->w_p_rl)
4461 value = max + 1 - size - value;
4462 if (value < 0)
4464 size += value;
4465 value = 0;
4468 #endif
4469 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4470 && max == gui.bottom_sbar.max)
4471 return;
4473 gui.bottom_sbar.value = value;
4474 gui.bottom_sbar.size = size;
4475 gui.bottom_sbar.max = max;
4476 gui.prev_wrap = curwin->w_p_wrap;
4478 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4482 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4485 gui_do_horiz_scroll()
4487 /* no wrapping, no scrolling */
4488 if (curwin->w_p_wrap)
4489 return FALSE;
4491 if (curwin->w_leftcol == scrollbar_value)
4492 return FALSE;
4494 curwin->w_leftcol = (colnr_T)scrollbar_value;
4496 /* When the line of the cursor is too short, move the cursor to the
4497 * longest visible line. Do a sanity check on "longest_lnum", just in
4498 * case. */
4499 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4500 && longest_lnum >= curwin->w_topline
4501 && longest_lnum < curwin->w_botline
4502 && !virtual_active())
4504 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4506 curwin->w_cursor.lnum = longest_lnum;
4507 curwin->w_cursor.col = 0;
4511 return leftcol_changed();
4515 * Check that none of the colors are the same as the background color
4517 void
4518 gui_check_colors()
4520 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4522 gui_set_bg_color((char_u *)"White");
4523 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4524 gui_set_fg_color((char_u *)"Black");
4528 static void
4529 gui_set_fg_color(name)
4530 char_u *name;
4532 gui.norm_pixel = gui_get_color(name);
4533 hl_set_fg_color_name(vim_strsave(name));
4536 static void
4537 gui_set_bg_color(name)
4538 char_u *name;
4540 gui.back_pixel = gui_get_color(name);
4541 hl_set_bg_color_name(vim_strsave(name));
4545 * Allocate a color by name.
4546 * Returns INVALCOLOR and gives an error message when failed.
4548 guicolor_T
4549 gui_get_color(name)
4550 char_u *name;
4552 guicolor_T t;
4554 if (*name == NUL)
4555 return INVALCOLOR;
4556 t = gui_mch_get_color(name);
4558 if (t == INVALCOLOR
4559 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4560 && gui.in_use
4561 #endif
4563 EMSG2(_("E254: Cannot allocate color %s"), name);
4564 return t;
4568 * Return the grey value of a color (range 0-255).
4571 gui_get_lightness(pixel)
4572 guicolor_T pixel;
4574 long_u rgb = gui_mch_get_rgb(pixel);
4576 return (int)( (((rgb >> 16) & 0xff) * 299)
4577 + (((rgb >> 8) & 0xff) * 587)
4578 + ((rgb & 0xff) * 114)) / 1000;
4581 #if defined(FEAT_GUI_X11) || defined(PROTO)
4582 void
4583 gui_new_scrollbar_colors()
4585 win_T *wp;
4587 /* Nothing to do if GUI hasn't started yet. */
4588 if (!gui.in_use)
4589 return;
4591 FOR_ALL_WINDOWS(wp)
4593 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4594 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4596 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4598 #endif
4601 * Call this when focus has changed.
4603 void
4604 gui_focus_change(in_focus)
4605 int in_focus;
4608 * Skip this code to avoid drawing the cursor when debugging and switching
4609 * between the debugger window and gvim.
4611 #if 1
4612 gui.in_focus = in_focus;
4613 out_flush(); /* make sure output has been written */
4614 gui_update_cursor(TRUE, FALSE);
4616 # ifdef FEAT_XIM
4617 xim_set_focus(in_focus);
4618 # endif
4620 /* Put events in the input queue only when allowed.
4621 * ui_focus_change() isn't called directly, because it invokes
4622 * autocommands and that must not happen asynchronously. */
4623 if (!hold_gui_events)
4625 char_u bytes[3];
4627 bytes[0] = CSI;
4628 bytes[1] = KS_EXTRA;
4629 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4630 add_to_input_buf(bytes, 3);
4632 #endif
4636 * Called when the mouse moved (but not when dragging).
4638 void
4639 gui_mouse_moved(x, y)
4640 int x;
4641 int y;
4643 win_T *wp;
4644 char_u st[8];
4646 /* Ignore this while still starting up. */
4647 if (!gui.in_use || gui.starting)
4648 return;
4650 #ifdef FEAT_MOUSESHAPE
4651 /* Get window pointer, and update mouse shape as well. */
4652 wp = xy2win(x, y);
4653 #endif
4655 /* Only handle this when 'mousefocus' set and ... */
4656 if (p_mousef
4657 && !hold_gui_events /* not holding events */
4658 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4659 && State != HITRETURN /* but not hit-return prompt */
4660 && msg_scrolled == 0 /* no scrolled message */
4661 && !need_mouse_correct /* not moving the pointer */
4662 && gui.in_focus) /* gvim in focus */
4664 /* Don't move the mouse when it's left or right of the Vim window */
4665 if (x < 0 || x > Columns * gui.char_width)
4666 return;
4667 #ifndef FEAT_MOUSESHAPE
4668 wp = xy2win(x, y);
4669 #endif
4670 if (wp == curwin || wp == NULL)
4671 return; /* still in the same old window, or none at all */
4673 #ifdef FEAT_WINDOWS
4674 /* Ignore position in the tab pages line. */
4675 if (Y_2_ROW(y) < tabline_height())
4676 return;
4677 #endif
4680 * format a mouse click on status line input
4681 * ala gui_send_mouse_event(0, x, y, 0, 0);
4682 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4683 * generate a K_LEFTMOUSE_NM key code.
4685 if (finish_op)
4687 /* abort the current operator first */
4688 st[0] = ESC;
4689 add_to_input_buf(st, 1);
4691 st[0] = CSI;
4692 st[1] = KS_MOUSE;
4693 st[2] = KE_FILLER;
4694 st[3] = (char_u)MOUSE_LEFT;
4695 fill_mouse_coord(st + 4,
4696 #ifdef FEAT_VERTSPLIT
4697 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4698 #else
4700 #endif
4701 wp->w_height + W_WINROW(wp));
4703 add_to_input_buf(st, 8);
4704 st[3] = (char_u)MOUSE_RELEASE;
4705 add_to_input_buf(st, 8);
4706 #ifdef FEAT_GUI_GTK
4707 /* Need to wake up the main loop */
4708 if (gtk_main_level() > 0)
4709 gtk_main_quit();
4710 #endif
4715 * Called when mouse should be moved to window with focus.
4717 void
4718 gui_mouse_correct()
4720 int x, y;
4721 win_T *wp = NULL;
4723 need_mouse_correct = FALSE;
4725 if (!(gui.in_use && p_mousef))
4726 return;
4728 gui_mch_getmouse(&x, &y);
4729 /* Don't move the mouse when it's left or right of the Vim window */
4730 if (x < 0 || x > Columns * gui.char_width)
4731 return;
4732 if (y >= 0
4733 # ifdef FEAT_WINDOWS
4734 && Y_2_ROW(y) >= tabline_height()
4735 # endif
4737 wp = xy2win(x, y);
4738 if (wp != curwin && wp != NULL) /* If in other than current window */
4740 validate_cline_row();
4741 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4742 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4743 + (gui.char_height) / 2);
4748 * Find window where the mouse pointer "y" coordinate is in.
4750 /*ARGSUSED*/
4751 static win_T *
4752 xy2win(x, y)
4753 int x;
4754 int y;
4756 #ifdef FEAT_WINDOWS
4757 int row;
4758 int col;
4759 win_T *wp;
4761 row = Y_2_ROW(y);
4762 col = X_2_COL(x);
4763 if (row < 0 || col < 0) /* before first window */
4764 return NULL;
4765 wp = mouse_find_win(&row, &col);
4766 # ifdef FEAT_MOUSESHAPE
4767 if (State == HITRETURN || State == ASKMORE)
4769 if (Y_2_ROW(y) >= msg_row)
4770 update_mouseshape(SHAPE_IDX_MOREL);
4771 else
4772 update_mouseshape(SHAPE_IDX_MORE);
4774 else if (row > wp->w_height) /* below status line */
4775 update_mouseshape(SHAPE_IDX_CLINE);
4776 # ifdef FEAT_VERTSPLIT
4777 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4778 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4779 update_mouseshape(SHAPE_IDX_VSEP);
4780 # endif
4781 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4782 && row == wp->w_height && msg_scrolled == 0)
4783 update_mouseshape(SHAPE_IDX_STATUS);
4784 else
4785 update_mouseshape(-2);
4786 # endif
4787 return wp;
4788 #else
4789 return firstwin;
4790 #endif
4794 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4795 * File names may be given to redefine the args list.
4797 void
4798 ex_gui(eap)
4799 exarg_T *eap;
4801 char_u *arg = eap->arg;
4804 * Check for "-f" argument: foreground, don't fork.
4805 * Also don't fork when started with "gvim -f".
4806 * Do fork when using "gui -b".
4808 if (arg[0] == '-'
4809 && (arg[1] == 'f' || arg[1] == 'b')
4810 && (arg[2] == NUL || vim_iswhite(arg[2])))
4812 gui.dofork = (arg[1] == 'b');
4813 eap->arg = skipwhite(eap->arg + 2);
4815 if (!gui.in_use)
4817 /* Clear the command. Needed for when forking+exiting, to avoid part
4818 * of the argument ending up after the shell prompt. */
4820 #ifdef MACOS_X
4821 /* os x doesn't really support fork(), so we can't fork of a gui
4822 * in an already running vim. see gui_start() for more details.
4824 gui.dofork = FALSE;
4825 #endif
4826 msg_clr_eos_force();
4827 gui_start();
4829 if (!ends_excmd(*eap->arg))
4830 ex_next(eap);
4833 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4834 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4836 * This is shared between Athena, Motif and GTK.
4838 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4841 * Callback function for do_in_runtimepath().
4843 static void
4844 gfp_setname(fname, cookie)
4845 char_u *fname;
4846 void *cookie;
4848 char_u *gfp_buffer = cookie;
4850 if (STRLEN(fname) >= MAXPATHL)
4851 *gfp_buffer = NUL;
4852 else
4853 STRCPY(gfp_buffer, fname);
4857 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4858 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4861 gui_find_bitmap(name, buffer, ext)
4862 char_u *name;
4863 char_u *buffer;
4864 char *ext;
4866 if (STRLEN(name) > MAXPATHL - 14)
4867 return FAIL;
4868 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4869 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4870 || *buffer == NUL)
4871 return FAIL;
4872 return OK;
4875 # if !defined(HAVE_GTK2) || defined(PROTO)
4877 * Given the name of the "icon=" argument, try finding the bitmap file for the
4878 * icon. If it is an absolute path name, use it as it is. Otherwise append
4879 * "ext" and search for it in 'runtimepath'.
4880 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4881 * contains "name".
4883 void
4884 gui_find_iconfile(name, buffer, ext)
4885 char_u *name;
4886 char_u *buffer;
4887 char *ext;
4889 char_u buf[MAXPATHL + 1];
4891 expand_env(name, buffer, MAXPATHL);
4892 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4893 STRCPY(buffer, buf);
4895 # endif
4896 #endif
4898 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4899 void
4900 display_errors()
4902 char_u *p;
4904 if (isatty(2))
4905 fflush(stderr);
4906 else if (error_ga.ga_data != NULL)
4908 /* avoid putting up a message box with blanks only */
4909 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4910 if (!isspace(*p))
4912 /* Truncate a very long message, it will go off-screen. */
4913 if (STRLEN(p) > 2000)
4914 STRCPY(p + 2000 - 14, "...(truncated)");
4915 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4916 p, (char_u *)_("&Ok"), 1, NULL);
4917 break;
4919 ga_clear(&error_ga);
4922 #endif
4924 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4926 * Return TRUE if still starting up and there is no place to enter text.
4927 * For GTK and X11 we check if stderr is not a tty, which means we were
4928 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4929 * allow typing on stdin.
4932 no_console_input()
4934 return ((!gui.in_use || gui.starting)
4935 # ifndef NO_CONSOLE
4936 && !isatty(0) && !isatty(2)
4937 # endif
4940 #endif
4942 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4943 || defined(NEED_GUI_UPDATE_SCREEN) \
4944 || defined(PROTO)
4946 * Update the current window and the screen.
4948 void
4949 gui_update_screen()
4951 update_topline();
4952 validate_cursor();
4953 update_screen(0); /* may need to update the screen */
4954 setcursor();
4955 out_flush(); /* make sure output has been written */
4956 gui_update_cursor(TRUE, FALSE);
4957 gui_mch_flush();
4959 #endif
4961 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4962 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4965 * Get the text to use in a find/replace dialog. Uses the last search pattern
4966 * if the argument is empty.
4967 * Returns an allocated string.
4969 char_u *
4970 get_find_dialog_text(arg, wwordp, mcasep)
4971 char_u *arg;
4972 int *wwordp; /* return: TRUE if \< \> found */
4973 int *mcasep; /* return: TRUE if \C found */
4975 char_u *text;
4977 if (*arg == NUL)
4978 text = last_search_pat();
4979 else
4980 text = arg;
4981 if (text != NULL)
4983 text = vim_strsave(text);
4984 if (text != NULL)
4986 int len = (int)STRLEN(text);
4987 int i;
4989 /* Remove "\V" */
4990 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4992 mch_memmove(text, text + 2, (size_t)(len - 1));
4993 len -= 2;
4996 /* Recognize "\c" and "\C" and remove. */
4997 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4999 *mcasep = (text[1] == 'C');
5000 mch_memmove(text, text + 2, (size_t)(len - 1));
5001 len -= 2;
5004 /* Recognize "\<text\>" and remove. */
5005 if (len >= 4
5006 && STRNCMP(text, "\\<", 2) == 0
5007 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5009 *wwordp = TRUE;
5010 mch_memmove(text, text + 2, (size_t)(len - 4));
5011 text[len - 4] = NUL;
5014 /* Recognize "\/" or "\?" and remove. */
5015 for (i = 0; i + 1 < len; ++i)
5016 if (text[i] == '\\' && (text[i + 1] == '/'
5017 || text[i + 1] == '?'))
5019 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5020 --len;
5024 return text;
5028 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5030 static void
5031 concat_esc(gap, text, what)
5032 garray_T *gap;
5033 char_u *text;
5034 int what;
5036 while (*text != NUL)
5038 #ifdef FEAT_MBYTE
5039 int l = (*mb_ptr2len)(text);
5041 if (l > 1)
5043 while (--l >= 0)
5044 ga_append(gap, *text++);
5045 continue;
5047 #endif
5048 if (*text == what)
5049 ga_append(gap, '\\');
5050 ga_append(gap, *text);
5051 ++text;
5056 * Handle the press of a button in the find-replace dialog.
5057 * Return TRUE when something was added to the input buffer.
5060 gui_do_findrepl(flags, find_text, repl_text, down)
5061 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5062 char_u *find_text;
5063 char_u *repl_text;
5064 int down; /* Search downwards. */
5066 garray_T ga;
5067 int i;
5068 int type = (flags & FRD_TYPE_MASK);
5069 char_u *p;
5070 regmatch_T regmatch;
5071 int save_did_emsg = did_emsg;
5073 ga_init2(&ga, 1, 100);
5074 if (type == FRD_REPLACEALL)
5075 ga_concat(&ga, (char_u *)"%s/");
5077 ga_concat(&ga, (char_u *)"\\V");
5078 if (flags & FRD_MATCH_CASE)
5079 ga_concat(&ga, (char_u *)"\\C");
5080 else
5081 ga_concat(&ga, (char_u *)"\\c");
5082 if (flags & FRD_WHOLE_WORD)
5083 ga_concat(&ga, (char_u *)"\\<");
5084 if (type == FRD_REPLACEALL || down)
5085 concat_esc(&ga, find_text, '/'); /* escape slashes */
5086 else
5087 concat_esc(&ga, find_text, '?'); /* escape '?' */
5088 if (flags & FRD_WHOLE_WORD)
5089 ga_concat(&ga, (char_u *)"\\>");
5091 if (type == FRD_REPLACEALL)
5093 ga_concat(&ga, (char_u *)"/");
5094 /* escape / and \ */
5095 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5096 if (p != NULL)
5097 ga_concat(&ga, p);
5098 vim_free(p);
5099 ga_concat(&ga, (char_u *)"/g");
5101 ga_append(&ga, NUL);
5103 if (type == FRD_REPLACE)
5105 /* Do the replacement when the text at the cursor matches. Thus no
5106 * replacement is done if the cursor was moved! */
5107 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5108 regmatch.rm_ic = 0;
5109 if (regmatch.regprog != NULL)
5111 p = ml_get_cursor();
5112 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5113 && regmatch.startp[0] == p)
5115 /* Clear the command line to remove any old "No match"
5116 * error. */
5117 msg_end_prompt();
5119 if (u_save_cursor() == OK)
5121 /* A button was pressed thus undo should be synced. */
5122 u_sync(FALSE);
5124 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5125 FALSE, FALSE);
5126 ins_str(repl_text);
5129 else
5130 MSG(_("No match at cursor, finding next"));
5131 vim_free(regmatch.regprog);
5135 if (type == FRD_REPLACEALL)
5137 /* A button was pressed, thus undo should be synced. */
5138 u_sync(FALSE);
5139 do_cmdline_cmd(ga.ga_data);
5141 else
5143 /* Search for the next match. */
5144 i = msg_scroll;
5145 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5146 SEARCH_MSG + SEARCH_MARK);
5147 msg_scroll = i; /* don't let an error message set msg_scroll */
5150 /* Don't want to pass did_emsg to other code, it may cause disabling
5151 * syntax HL if we were busy redrawing. */
5152 did_emsg = save_did_emsg;
5154 if (State & (NORMAL | INSERT))
5156 gui_update_screen(); /* update the screen */
5157 msg_didout = 0; /* overwrite any message */
5158 need_wait_return = FALSE; /* don't wait for return */
5161 vim_free(ga.ga_data);
5162 return (ga.ga_len > 0);
5165 #endif
5167 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5168 || defined(FEAT_GUI_MSWIN) \
5169 || defined(FEAT_GUI_MAC) \
5170 || defined(PROTO) \
5171 || defined(FEAT_GUI_MACVIM)
5173 #ifdef FEAT_WINDOWS
5174 static void gui_wingoto_xy __ARGS((int x, int y));
5177 * Jump to the window at specified point (x, y).
5179 static void
5180 gui_wingoto_xy(x, y)
5181 int x;
5182 int y;
5184 int row = Y_2_ROW(y);
5185 int col = X_2_COL(x);
5186 win_T *wp;
5188 if (row >= 0 && col >= 0)
5190 wp = mouse_find_win(&row, &col);
5191 if (wp != NULL && wp != curwin)
5192 win_goto(wp);
5195 #endif
5198 * Process file drop. Mouse cursor position, key modifiers, name of files
5199 * and count of files are given. Argument "fnames[count]" has full pathnames
5200 * of dropped files, they will be freed in this function, and caller can't use
5201 * fnames after call this function.
5203 /*ARGSUSED*/
5204 void
5205 gui_handle_drop(x, y, modifiers, fnames, count)
5206 int x;
5207 int y;
5208 int_u modifiers;
5209 char_u **fnames;
5210 int count;
5212 int i;
5213 char_u *p;
5216 * When the cursor is at the command line, add the file names to the
5217 * command line, don't edit the files.
5219 if (State & CMDLINE)
5221 shorten_filenames(fnames, count);
5222 for (i = 0; i < count; ++i)
5224 if (fnames[i] != NULL)
5226 if (i > 0)
5227 add_to_input_buf((char_u*)" ", 1);
5229 /* We don't know what command is used thus we can't be sure
5230 * about which characters need to be escaped. Only escape the
5231 * most common ones. */
5232 # ifdef BACKSLASH_IN_FILENAME
5233 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5234 # else
5235 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5236 # endif
5237 if (p != NULL)
5238 add_to_input_buf_csi(p, (int)STRLEN(p));
5239 vim_free(p);
5240 vim_free(fnames[i]);
5243 vim_free(fnames);
5245 else
5247 /* Go to the window under mouse cursor, then shorten given "fnames" by
5248 * current window, because a window can have local current dir. */
5249 # ifdef FEAT_WINDOWS
5250 gui_wingoto_xy(x, y);
5251 # endif
5252 shorten_filenames(fnames, count);
5254 /* If Shift held down, remember the first item. */
5255 if ((modifiers & MOUSE_SHIFT) != 0)
5256 p = vim_strsave(fnames[0]);
5257 else
5258 p = NULL;
5260 /* Handle the drop, :edit or :split to get to the file. This also
5261 * frees fnames[]. Skip this if there is only one item it's a
5262 * directory and Shift is held down. */
5263 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5264 && mch_isdir(fnames[0]))
5266 vim_free(fnames[0]);
5267 vim_free(fnames);
5269 else
5270 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5272 /* If Shift held down, change to first file's directory. If the first
5273 * item is a directory, change to that directory (and let the explorer
5274 * plugin show the contents). */
5275 if (p != NULL)
5277 if (mch_isdir(p))
5279 if (mch_chdir((char *)p) == 0)
5280 shorten_fnames(TRUE);
5282 else if (vim_chdirfile(p) == OK)
5283 shorten_fnames(TRUE);
5284 vim_free(p);
5287 /* Update the screen display */
5288 update_screen(NOT_VALID);
5289 # ifdef FEAT_MENU
5290 gui_update_menus(0);
5291 # endif
5292 setcursor();
5293 out_flush();
5294 gui_update_cursor(FALSE, FALSE);
5295 gui_mch_flush();
5298 #endif