Validate screen before constraining window
[MacVim.git] / src / gui.c
blob9b5836eea4adc2b41f23212ea4fbdca4d3899509
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
11 #include "vim.h"
13 /* Structure containing all the GUI information */
14 gui_T gui;
16 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
17 static void set_guifontwide __ARGS((char_u *font_name));
18 #endif
19 static void gui_check_pos __ARGS((void));
20 static void gui_position_components __ARGS((int));
21 static void gui_outstr __ARGS((char_u *, int));
22 static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
23 #ifdef HAVE_GTK2
24 static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25 #endif
26 static void gui_delete_lines __ARGS((int row, int count));
27 static void gui_insert_lines __ARGS((int row, int count));
28 static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
29 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30 static int gui_has_tabline __ARGS((void));
31 #endif
32 static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33 static colnr_T scroll_line_len __ARGS((linenr_T lnum));
34 static void gui_update_horiz_scrollbar __ARGS((int));
35 static void gui_set_fg_color __ARGS((char_u *name));
36 static void gui_set_bg_color __ARGS((char_u *name));
37 static win_T *xy2win __ARGS((int x, int y));
39 static int can_update_cursor = TRUE; /* can display the cursor */
42 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
43 * this makes the thumb indicate the part of the text that is shown. Motif
44 * can't do this.
46 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
47 # define SCROLL_PAST_END
48 #endif
51 * gui_start -- Called when user wants to start the GUI.
53 * Careful: This function can be called recursively when there is a ":gui"
54 * command in the .gvimrc file. Only the first call should fork, not the
55 * recursive call.
57 void
58 gui_start()
60 char_u *old_term;
61 #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X)
62 /* By the time we get here Mac OS X will already have forked (it does so
63 * right after scanning the command line) so don't do anything here. This
64 * means that "f" in 'guioptions' cannot be supported.
66 # define MAY_FORK
67 int dofork = TRUE;
68 #endif
69 static int recursive = 0;
71 old_term = vim_strsave(T_NAME);
74 * Set_termname() will call gui_init() to start the GUI.
75 * Set the "starting" flag, to indicate that the GUI will start.
77 * We don't want to open the GUI shell until after we've read .gvimrc,
78 * otherwise we don't know what font we will use, and hence we don't know
79 * what size the shell should be. So if there are errors in the .gvimrc
80 * file, they will have to go to the terminal: Set full_screen to FALSE.
81 * full_screen will be set to TRUE again by a successful termcapinit().
83 settmode(TMODE_COOK); /* stop RAW mode */
84 if (full_screen)
85 cursor_on(); /* needed for ":gui" in .vimrc */
86 gui.starting = TRUE;
87 full_screen = FALSE;
89 #ifdef MAY_FORK
90 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
91 dofork = FALSE;
92 #endif
93 ++recursive;
95 termcapinit((char_u *)"builtin_gui");
96 gui.starting = recursive - 1;
98 if (!gui.in_use) /* failed to start GUI */
100 termcapinit(old_term); /* back to old term settings */
101 settmode(TMODE_RAW); /* restart RAW mode */
102 #ifdef FEAT_TITLE
103 set_title_defaults(); /* set 'title' and 'icon' again */
104 #endif
107 vim_free(old_term);
109 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
110 if (gui.in_use)
111 /* Display error messages in a dialog now. */
112 display_errors();
113 #endif
115 #if defined(MAY_FORK) && !defined(__QNXNTO__)
117 * Quit the current process and continue in the child.
118 * Makes "gvim file" disconnect from the shell it was started in.
119 * Don't do this when Vim was started with "-f" or the 'f' flag is present
120 * in 'guioptions'.
122 if (gui.in_use && dofork)
124 int pipefd[2]; /* pipe between parent and child */
125 int pipe_error;
126 char dummy;
127 pid_t pid = -1;
129 /* Setup a pipe between the child and the parent, so that the parent
130 * knows when the child has done the setsid() call and is allowed to
131 * exit. */
132 pipe_error = (pipe(pipefd) < 0);
133 pid = fork();
134 if (pid > 0) /* Parent */
136 /* Give the child some time to do the setsid(), otherwise the
137 * exit() may kill the child too (when starting gvim from inside a
138 * gvim). */
139 if (pipe_error)
140 ui_delay(300L, TRUE);
141 else
143 /* The read returns when the child closes the pipe (or when
144 * the child dies for some reason). */
145 close(pipefd[1]);
146 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
147 close(pipefd[0]);
150 /* When swapping screens we may need to go to the next line, e.g.,
151 * after a hit-enter prompt and using ":gui". */
152 if (newline_on_exit)
153 mch_errmsg("\r\n");
156 * The parent must skip the normal exit() processing, the child
157 * will do it. For example, GTK messes up signals when exiting.
159 _exit(0);
162 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
164 * Change our process group. On some systems/shells a CTRL-C in the
165 * shell where Vim was started would otherwise kill gvim!
167 if (pid == 0) /* child */
168 # if defined(HAVE_SETSID)
169 (void)setsid();
170 # else
171 (void)setpgid(0, 0);
172 # endif
173 # endif
174 if (!pipe_error)
176 close(pipefd[0]);
177 close(pipefd[1]);
180 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
181 /* Tell the session manager our new PID */
182 gui_mch_forked();
183 # endif
185 #else
186 # if defined(__QNXNTO__)
187 if (gui.in_use && dofork)
188 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
189 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
190 # endif
191 #endif
193 #ifdef FEAT_AUTOCMD
194 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
195 * the GUIFailed event. */
196 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
197 NULL, NULL, FALSE, curbuf);
198 #endif
200 --recursive;
204 * Call this when vim starts up, whether or not the GUI is started
206 void
207 gui_prepare(argc, argv)
208 int *argc;
209 char **argv;
211 gui.in_use = FALSE; /* No GUI yet (maybe later) */
212 gui.starting = FALSE; /* No GUI yet (maybe later) */
213 gui_mch_prepare(argc, argv);
217 * Try initializing the GUI and check if it can be started.
218 * Used from main() to check early if "vim -g" can start the GUI.
219 * Used from gui_init() to prepare for starting the GUI.
220 * Returns FAIL or OK.
223 gui_init_check()
225 static int result = MAYBE;
227 if (result != MAYBE)
229 if (result == FAIL)
230 EMSG(_("E229: Cannot start the GUI"));
231 return result;
234 gui.shell_created = FALSE;
235 gui.dying = FALSE;
236 gui.in_focus = TRUE; /* so the guicursor setting works */
237 gui.dragged_sb = SBAR_NONE;
238 gui.dragged_wp = NULL;
239 gui.pointer_hidden = FALSE;
240 gui.col = 0;
241 gui.row = 0;
242 gui.num_cols = Columns;
243 gui.num_rows = Rows;
245 gui.cursor_is_valid = FALSE;
246 gui.scroll_region_top = 0;
247 gui.scroll_region_bot = Rows - 1;
248 gui.scroll_region_left = 0;
249 gui.scroll_region_right = Columns - 1;
250 gui.highlight_mask = HL_NORMAL;
251 gui.char_width = 1;
252 gui.char_height = 1;
253 gui.char_ascent = 0;
254 gui.border_width = 0;
256 gui.norm_font = NOFONT;
257 #ifndef HAVE_GTK2
258 gui.bold_font = NOFONT;
259 gui.ital_font = NOFONT;
260 gui.boldital_font = NOFONT;
261 # ifdef FEAT_XFONTSET
262 gui.fontset = NOFONTSET;
263 # endif
264 #endif
266 #ifdef FEAT_MENU
267 # ifndef HAVE_GTK2
268 # ifdef FONTSET_ALWAYS
269 gui.menu_fontset = NOFONTSET;
270 # else
271 gui.menu_font = NOFONT;
272 # endif
273 # endif
274 gui.menu_is_active = TRUE; /* default: include menu */
275 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
276 gui.menu_height = MENU_DEFAULT_HEIGHT;
277 gui.menu_width = 0;
278 # endif
279 #endif
280 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
281 gui.toolbar_height = 0;
282 #endif
283 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
284 gui.footer_height = 0;
285 #endif
286 #ifdef FEAT_BEVAL_TIP
287 gui.tooltip_fontset = NOFONTSET;
288 #endif
290 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
291 gui.prev_wrap = -1;
293 #ifdef ALWAYS_USE_GUI
294 result = OK;
295 #else
296 result = gui_mch_init_check();
297 #endif
298 return result;
302 * This is the call which starts the GUI.
304 void
305 gui_init()
307 win_T *wp;
308 static int recursive = 0;
311 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
312 * function will then be executed at the first call, the rest by the
313 * recursive call. This allow the shell to be opened halfway reading a
314 * gvimrc file.
316 if (!recursive)
318 ++recursive;
320 clip_init(TRUE);
322 /* If can't initialize, don't try doing the rest */
323 if (gui_init_check() == FAIL)
325 --recursive;
326 clip_init(FALSE);
327 return;
331 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
332 * breaks the Paste toolbar button.
334 set_option_value((char_u *)"paste", 0L, NULL, 0);
337 * Set up system-wide default menus.
339 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
340 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
342 sys_menu = TRUE;
343 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
344 sys_menu = FALSE;
346 #endif
349 * Switch on the mouse by default, unless the user changed it already.
350 * This can then be changed in the .gvimrc.
352 if (!option_was_set((char_u *)"mouse"))
353 set_string_option_direct((char_u *)"mouse", -1,
354 (char_u *)"a", OPT_FREE, SID_NONE);
357 * If -U option given, use only the initializations from that file and
358 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
360 if (use_gvimrc != NULL)
362 if (STRCMP(use_gvimrc, "NONE") != 0
363 && STRCMP(use_gvimrc, "NORC") != 0
364 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
365 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
367 else
370 * Get system wide defaults for gvim, only when file name defined.
372 #ifdef SYS_GVIMRC_FILE
373 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
374 #endif
377 * Try to read GUI initialization commands from the following
378 * places:
379 * - environment variable GVIMINIT
380 * - the user gvimrc file (~/.gvimrc)
381 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
382 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
383 * The first that exists is used, the rest is ignored.
385 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
386 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
387 DOSO_GVIMRC) == FAIL
388 #ifdef USR_GVIMRC_FILE2
389 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
390 DOSO_GVIMRC) == FAIL
391 #endif
394 #ifdef USR_GVIMRC_FILE3
395 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
396 #endif
400 * Read initialization commands from ".gvimrc" in current
401 * directory. This is only done if the 'exrc' option is set.
402 * Because of security reasons we disallow shell and write
403 * commands now, except for unix if the file is owned by the user
404 * or 'secure' option has been reset in environment of global
405 * ".gvimrc".
406 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
407 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
409 if (p_exrc)
411 #ifdef UNIX
413 struct stat s;
415 /* if ".gvimrc" file is not owned by user, set 'secure'
416 * mode */
417 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
418 secure = p_secure;
420 #else
421 secure = p_secure;
422 #endif
424 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
425 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
426 #ifdef SYS_GVIMRC_FILE
427 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
428 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
429 #endif
430 #ifdef USR_GVIMRC_FILE2
431 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
432 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
433 #endif
434 #ifdef USR_GVIMRC_FILE3
435 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
436 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
437 #endif
439 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
441 if (secure == 2)
442 need_wait_return = TRUE;
443 secure = 0;
447 if (need_wait_return || msg_didany)
448 wait_return(TRUE);
450 --recursive;
453 /* If recursive call opened the shell, return here from the first call */
454 if (gui.in_use)
455 return;
458 * Create the GUI shell.
460 gui.in_use = TRUE; /* Must be set after menus have been set up */
461 if (gui_mch_init() == FAIL)
462 goto error;
464 /* Avoid a delay for an error message that was printed in the terminal
465 * where Vim was started. */
466 emsg_on_display = FALSE;
467 msg_scrolled = 0;
468 clear_sb_text();
469 need_wait_return = FALSE;
470 msg_didany = FALSE;
473 * Check validity of any generic resources that may have been loaded.
475 if (gui.border_width < 0)
476 gui.border_width = 0;
479 * Set up the fonts. First use a font specified with "-fn" or "-font".
481 if (font_argument != NULL)
482 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
483 if (
484 #ifdef FEAT_XFONTSET
485 (*p_guifontset == NUL
486 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
487 #endif
488 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
489 : p_guifont, FALSE) == FAIL)
491 EMSG(_("E665: Cannot start GUI, no valid font found"));
492 goto error2;
494 #ifdef FEAT_MBYTE
495 if (gui_get_wide_font() == FAIL)
496 EMSG(_("E231: 'guifontwide' invalid"));
497 #endif
499 gui.num_cols = Columns;
500 gui.num_rows = Rows;
501 gui_reset_scroll_region();
503 /* Create initial scrollbars */
504 FOR_ALL_WINDOWS(wp)
506 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
507 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
509 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
511 #ifdef FEAT_MENU
512 gui_create_initial_menus(root_menu);
513 #endif
514 #ifdef FEAT_SUN_WORKSHOP
515 if (usingSunWorkShop)
516 workshop_init();
517 #endif
518 #ifdef FEAT_SIGN_ICONS
519 sign_gui_started();
520 #endif
522 /* Configure the desired menu and scrollbars */
523 gui_init_which_components(NULL);
525 /* All components of the GUI have been created now */
526 gui.shell_created = TRUE;
528 #ifndef FEAT_GUI_GTK
529 /* Set the shell size, adjusted for the screen size. For GTK this only
530 * works after the shell has been opened, thus it is further down. */
531 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
532 #endif
533 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
534 /* Need to set the size of the menubar after all the menus have been
535 * created. */
536 gui_mch_compute_menu_height((Widget)0);
537 #endif
540 * Actually open the GUI shell.
542 if (gui_mch_open() != FAIL)
544 #ifdef FEAT_TITLE
545 maketitle();
546 resettitle();
547 #endif
548 init_gui_options();
549 #ifdef FEAT_ARABIC
550 /* Our GUI can't do bidi. */
551 p_tbidi = FALSE;
552 #endif
553 #if defined(FEAT_GUI_GTK)
554 /* Give GTK+ a chance to put all widget's into place. */
555 gui_mch_update();
557 # ifdef FEAT_MENU
558 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
559 * It was still there to make F10 work. */
560 if (vim_strchr(p_go, GO_MENUS) == NULL)
562 --gui.starting;
563 gui_mch_enable_menu(FALSE);
564 ++gui.starting;
565 gui_mch_update();
567 # endif
569 /* Now make sure the shell fits on the screen. */
570 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
571 #endif
572 /* When 'lines' was set while starting up the topframe may have to be
573 * resized. */
574 win_new_shellsize();
576 #ifdef FEAT_BEVAL
577 /* Always create the Balloon Evaluation area, but disable it when
578 * 'ballooneval' is off */
579 # ifdef FEAT_GUI_GTK
580 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
581 &general_beval_cb, NULL);
582 # else
583 # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
585 extern Widget textArea;
586 balloonEval = gui_mch_create_beval_area(textArea, NULL,
587 &general_beval_cb, NULL);
589 # else
590 # ifdef FEAT_GUI_W32
591 balloonEval = gui_mch_create_beval_area(NULL, NULL,
592 &general_beval_cb, NULL);
593 # endif
594 # endif
595 # endif
596 if (!p_beval)
597 gui_mch_disable_beval_area(balloonEval);
598 #endif
600 #ifdef FEAT_NETBEANS_INTG
601 if (starting == 0 && usingNetbeans)
602 /* Tell the client that it can start sending commands. */
603 netbeans_startup_done();
604 #endif
605 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
606 if (!im_xim_isvalid_imactivate())
607 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
608 #endif
609 /* When 'cmdheight' was set during startup it may not have taken
610 * effect yet. */
611 if (p_ch != 1L)
612 command_height();
614 return;
617 error2:
618 #ifdef FEAT_GUI_X11
619 /* undo gui_mch_init() */
620 gui_mch_uninit();
621 #endif
623 error:
624 gui.in_use = FALSE;
625 clip_init(FALSE);
629 void
630 gui_exit(rc)
631 int rc;
633 #ifndef __BEOS__
634 /* don't free the fonts, it leads to a BUS error
635 * richard@whitequeen.com Jul 99 */
636 free_highlight_fonts();
637 #endif
638 gui.in_use = FALSE;
639 gui_mch_exit(rc);
642 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
643 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
644 || defined(PROTO) || defined(FEAT_GUI_MACVIM)
645 # define NEED_GUI_UPDATE_SCREEN 1
647 * Called when the GUI shell is closed by the user. If there are no changed
648 * files Vim exits, otherwise there will be a dialog to ask the user what to
649 * do.
650 * When this function returns, Vim should NOT exit!
652 void
653 gui_shell_closed()
655 cmdmod_T save_cmdmod;
657 save_cmdmod = cmdmod;
659 /* Only exit when there are no changed files */
660 exiting = TRUE;
661 # ifdef FEAT_BROWSE
662 cmdmod.browse = TRUE;
663 # endif
664 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
665 cmdmod.confirm = TRUE;
666 # endif
667 /* If there are changed buffers, present the user with a dialog if
668 * possible, otherwise give an error message. */
669 if (!check_changed_any(FALSE))
670 getout(0);
672 exiting = FALSE;
673 cmdmod = save_cmdmod;
674 gui_update_screen(); /* redraw, window may show changed buffer */
676 #endif
679 * Set the font. "font_list" is a a comma separated list of font names. The
680 * first font name that works is used. If none is found, use the default
681 * font.
682 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
683 * Return OK when able to set the font. When it failed FAIL is returned and
684 * the fonts are unchanged.
686 /*ARGSUSED*/
688 gui_init_font(font_list, fontset)
689 char_u *font_list;
690 int fontset;
692 #define FONTLEN 320
693 char_u font_name[FONTLEN];
694 int font_list_empty = FALSE;
695 int ret = FAIL;
697 if (!gui.in_use)
698 return FAIL;
700 font_name[0] = NUL;
701 if (*font_list == NUL)
702 font_list_empty = TRUE;
703 else
705 #ifdef FEAT_XFONTSET
706 /* When using a fontset, the whole list of fonts is one name. */
707 if (fontset)
708 ret = gui_mch_init_font(font_list, TRUE);
709 else
710 #endif
711 while (*font_list != NUL)
713 /* Isolate one comma separated font name. */
714 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
716 #if defined(FEAT_GUI_MACVIM)
717 /* The font dialog is modeless in Mac OS X, so when
718 * gui_mch_init_font() is called with "*" it brings up the
719 * dialog and returns immediately. In this case we don't want
720 * it to be called again with NULL, so return here. */
721 if (STRCMP(font_name, "*") == 0) {
722 gui_mch_init_font(font_name, FALSE);
723 return FALSE;
725 #endif
727 /* Careful!!! The Win32 version of gui_mch_init_font(), when
728 * called with "*" will change p_guifont to the selected font
729 * name, which frees the old value. This makes font_list
730 * invalid. Thus when OK is returned here, font_list must no
731 * longer be used! */
732 if (gui_mch_init_font(font_name, FALSE) == OK)
734 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
735 /* If it's a Unicode font, try setting 'guifontwide' to a
736 * similar double-width font. */
737 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
738 && strstr((char *)font_name, "10646") != NULL)
739 set_guifontwide(font_name);
740 #endif
741 ret = OK;
742 break;
747 if (ret != OK
748 && STRCMP(font_list, "*") != 0
749 && (font_list_empty || gui.norm_font == NOFONT))
752 * Couldn't load any font in 'font_list', keep the current font if
753 * there is one. If 'font_list' is empty, or if there is no current
754 * font, tell gui_mch_init_font() to try to find a font we can load.
756 ret = gui_mch_init_font(NULL, FALSE);
759 if (ret == OK)
761 #ifndef HAVE_GTK2
762 /* Set normal font as current font */
763 # ifdef FEAT_XFONTSET
764 if (gui.fontset != NOFONTSET)
765 gui_mch_set_fontset(gui.fontset);
766 else
767 # endif
768 gui_mch_set_font(gui.norm_font);
769 #endif
770 gui_set_shellsize(FALSE,
771 #ifdef MSWIN
772 TRUE
773 #else
774 FALSE
775 #endif
776 , RESIZE_BOTH);
779 return ret;
782 #if defined(FEAT_MBYTE) || defined(PROTO)
783 # ifndef HAVE_GTK2
785 * Try setting 'guifontwide' to a font twice as wide as "name".
787 static void
788 set_guifontwide(name)
789 char_u *name;
791 int i = 0;
792 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
793 char_u *wp = NULL;
794 char_u *p;
795 GuiFont font;
797 wp = wide_name;
798 for (p = name; *p != NUL; ++p)
800 *wp++ = *p;
801 if (*p == '-')
803 ++i;
804 if (i == 6) /* font type: change "--" to "-*-" */
806 if (p[1] == '-')
807 *wp++ = '*';
809 else if (i == 12) /* found the width */
811 ++p;
812 i = getdigits(&p);
813 if (i != 0)
815 /* Double the width specification. */
816 sprintf((char *)wp, "%d%s", i * 2, p);
817 font = gui_mch_get_font(wide_name, FALSE);
818 if (font != NOFONT)
820 gui_mch_free_font(gui.wide_font);
821 gui.wide_font = font;
822 set_string_option_direct((char_u *)"gfw", -1,
823 wide_name, OPT_FREE, 0);
826 break;
831 # endif /* !HAVE_GTK2 */
834 * Get the font for 'guifontwide'.
835 * Return FAIL for an invalid font name.
838 gui_get_wide_font()
840 GuiFont font = NOFONT;
841 char_u font_name[FONTLEN];
842 char_u *p;
844 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
845 return OK; /* Will give an error message later. */
847 if (p_guifontwide != NULL && *p_guifontwide != NUL)
849 for (p = p_guifontwide; *p != NUL; )
851 /* Isolate one comma separated font name. */
852 (void)copy_option_part(&p, font_name, FONTLEN, ",");
853 font = gui_mch_get_font(font_name, FALSE);
854 if (font != NOFONT)
855 break;
857 if (font == NOFONT)
858 return FAIL;
861 gui_mch_free_font(gui.wide_font);
862 #ifdef HAVE_GTK2
863 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
864 if (font != NOFONT && gui.norm_font != NOFONT
865 && pango_font_description_equal(font, gui.norm_font))
867 gui.wide_font = NOFONT;
868 gui_mch_free_font(font);
870 else
871 #endif
872 gui.wide_font = font;
873 return OK;
875 #endif
877 void
878 gui_set_cursor(row, col)
879 int row;
880 int col;
882 gui.row = row;
883 gui.col = col;
887 * gui_check_pos - check if the cursor is on the screen.
889 static void
890 gui_check_pos()
892 if (gui.row >= screen_Rows)
893 gui.row = screen_Rows - 1;
894 if (gui.col >= screen_Columns)
895 gui.col = screen_Columns - 1;
896 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
897 gui.cursor_is_valid = FALSE;
901 * Redraw the cursor if necessary or when forced.
902 * Careful: The contents of ScreenLines[] must match what is on the screen,
903 * otherwise this goes wrong. May need to call out_flush() first.
905 void
906 gui_update_cursor(force, clear_selection)
907 int force; /* when TRUE, update even when not moved */
908 int clear_selection;/* clear selection under cursor */
910 int cur_width = 0;
911 int cur_height = 0;
912 int old_hl_mask;
913 int idx;
914 int id;
915 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
916 int cattr; /* cursor attributes */
917 int attr;
918 attrentry_T *aep = NULL;
920 /* Don't update the cursor when halfway busy scrolling or the screen size
921 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
922 if (!can_update_cursor || screen_Columns != gui.num_cols
923 || screen_Rows != gui.num_rows)
924 return;
926 gui_check_pos();
927 if (!gui.cursor_is_valid || force
928 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
930 gui_undraw_cursor();
931 if (gui.row < 0)
932 return;
933 #ifdef USE_IM_CONTROL
934 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
935 im_set_position(gui.row, gui.col);
936 #endif
937 gui.cursor_row = gui.row;
938 gui.cursor_col = gui.col;
940 /* Only write to the screen after ScreenLines[] has been initialized */
941 if (!screen_cleared || ScreenLines == NULL)
942 return;
944 /* Clear the selection if we are about to write over it */
945 if (clear_selection)
946 clip_may_clear_selection(gui.row, gui.row);
947 /* Check that the cursor is inside the shell (resizing may have made
948 * it invalid) */
949 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
950 return;
952 gui.cursor_is_valid = TRUE;
955 * How the cursor is drawn depends on the current mode.
957 idx = get_shape_idx(FALSE);
958 if (State & LANGMAP)
959 id = shape_table[idx].id_lm;
960 else
961 id = shape_table[idx].id;
963 /* get the colors and attributes for the cursor. Default is inverted */
964 cfg = INVALCOLOR;
965 cbg = INVALCOLOR;
966 cattr = HL_INVERSE;
967 gui_mch_set_blinking(shape_table[idx].blinkwait,
968 shape_table[idx].blinkon,
969 shape_table[idx].blinkoff);
970 if (id > 0)
972 cattr = syn_id2colors(id, &cfg, &cbg);
973 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
975 static int iid;
976 guicolor_T fg, bg;
978 if (
979 # ifdef HAVE_GTK2
980 preedit_get_status()
981 # else
982 im_get_status()
983 # endif
986 iid = syn_name2id((char_u *)"CursorIM");
987 if (iid > 0)
989 syn_id2colors(iid, &fg, &bg);
990 if (bg != INVALCOLOR)
991 cbg = bg;
992 if (fg != INVALCOLOR)
993 cfg = fg;
997 #endif
1001 * Get the attributes for the character under the cursor.
1002 * When no cursor color was given, use the character color.
1004 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1005 if (attr > HL_ALL)
1006 aep = syn_gui_attr2entry(attr);
1007 if (aep != NULL)
1009 attr = aep->ae_attr;
1010 if (cfg == INVALCOLOR)
1011 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1012 : aep->ae_u.gui.fg_color);
1013 if (cbg == INVALCOLOR)
1014 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1015 : aep->ae_u.gui.bg_color);
1017 if (cfg == INVALCOLOR)
1018 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1019 if (cbg == INVALCOLOR)
1020 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1022 #ifdef FEAT_XIM
1023 if (aep != NULL)
1025 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1026 : aep->ae_u.gui.bg_color);
1027 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1028 : aep->ae_u.gui.fg_color);
1029 if (xim_bg_color == INVALCOLOR)
1030 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1031 : gui.back_pixel;
1032 if (xim_fg_color == INVALCOLOR)
1033 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1034 : gui.norm_pixel;
1036 else
1038 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1039 : gui.back_pixel;
1040 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1041 : gui.norm_pixel;
1043 #endif
1045 attr &= ~HL_INVERSE;
1046 if (cattr & HL_INVERSE)
1048 cc = cbg;
1049 cbg = cfg;
1050 cfg = cc;
1052 cattr &= ~HL_INVERSE;
1055 * When we don't have window focus, draw a hollow cursor.
1057 if (!gui.in_focus)
1059 gui_mch_draw_hollow_cursor(cbg);
1060 return;
1063 old_hl_mask = gui.highlight_mask;
1064 if (shape_table[idx].shape == SHAPE_BLOCK
1065 #ifdef FEAT_HANGULIN
1066 || composing_hangul
1067 #endif
1071 * Draw the text character with the cursor colors. Use the
1072 * character attributes plus the cursor attributes.
1074 gui.highlight_mask = (cattr | attr);
1075 #ifdef FEAT_HANGULIN
1076 if (composing_hangul)
1077 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1078 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1079 else
1080 #endif
1081 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1082 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1084 else
1086 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1087 int col_off = FALSE;
1088 #endif
1090 * First draw the partial cursor, then overwrite with the text
1091 * character, using a transparent background.
1093 if (shape_table[idx].shape == SHAPE_VER)
1095 cur_height = gui.char_height;
1096 cur_width = (gui.char_width * shape_table[idx].percentage
1097 + 99) / 100;
1099 else
1101 cur_height = (gui.char_height * shape_table[idx].percentage
1102 + 99) / 100;
1103 cur_width = gui.char_width;
1105 #ifdef FEAT_MBYTE
1106 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1107 LineOffset[gui.row] + screen_Columns) > 1)
1109 /* Double wide character. */
1110 if (shape_table[idx].shape != SHAPE_VER)
1111 cur_width += gui.char_width;
1112 # ifdef FEAT_RIGHTLEFT
1113 if (CURSOR_BAR_RIGHT)
1115 /* gui.col points to the left halve of the character but
1116 * the vertical line needs to be on the right halve.
1117 * A double-wide horizontal line is also drawn from the
1118 * right halve in gui_mch_draw_part_cursor(). */
1119 col_off = TRUE;
1120 ++gui.col;
1122 # endif
1124 #endif
1125 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1126 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1127 if (col_off)
1128 --gui.col;
1129 #endif
1131 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1132 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1133 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1134 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1135 (guicolor_T)0, (guicolor_T)0, 0);
1136 #endif
1138 gui.highlight_mask = old_hl_mask;
1142 #if defined(FEAT_MENU) || defined(PROTO)
1143 void
1144 gui_position_menu()
1146 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
1147 || defined(FEAT_GUI_MACVIM))
1148 if (gui.menu_is_active && gui.in_use)
1149 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1150 # endif
1152 #endif
1155 * Position the various GUI components (text area, menu). The vertical
1156 * scrollbars are NOT handled here. See gui_update_scrollbars().
1158 /*ARGSUSED*/
1159 static void
1160 gui_position_components(total_width)
1161 int total_width;
1163 int text_area_x;
1164 int text_area_y;
1165 int text_area_width;
1166 int text_area_height;
1168 /* avoid that moving components around generates events */
1169 ++hold_gui_events;
1171 text_area_x = 0;
1172 if (gui.which_scrollbars[SBAR_LEFT])
1173 text_area_x += gui.scrollbar_width;
1175 text_area_y = 0;
1176 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
1177 || defined(FEAT_GUI_MACVIM))
1178 gui.menu_width = total_width;
1179 if (gui.menu_is_active)
1180 text_area_y += gui.menu_height;
1181 #endif
1182 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1183 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1184 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1185 #endif
1187 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1188 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1189 if (gui_has_tabline())
1190 text_area_y += gui.tabline_height;
1191 #endif
1193 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1194 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1196 # ifdef FEAT_GUI_ATHENA
1197 gui_mch_set_toolbar_pos(0, text_area_y,
1198 gui.menu_width, gui.toolbar_height);
1199 # endif
1200 text_area_y += gui.toolbar_height;
1202 #endif
1204 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1205 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1207 gui_mch_set_text_area_pos(text_area_x,
1208 text_area_y,
1209 text_area_width,
1210 text_area_height
1211 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1212 + xim_get_status_area_height()
1213 #endif
1215 #ifdef FEAT_MENU
1216 gui_position_menu();
1217 #endif
1218 if (gui.which_scrollbars[SBAR_BOTTOM])
1219 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1220 text_area_x,
1221 text_area_y + text_area_height,
1222 text_area_width,
1223 gui.scrollbar_height);
1224 gui.left_sbar_x = 0;
1225 gui.right_sbar_x = text_area_x + text_area_width;
1227 --hold_gui_events;
1231 * Get the width of the widgets and decorations to the side of the text area.
1234 gui_get_base_width()
1236 int base_width;
1238 base_width = 2 * gui.border_offset;
1239 if (gui.which_scrollbars[SBAR_LEFT])
1240 base_width += gui.scrollbar_width;
1241 if (gui.which_scrollbars[SBAR_RIGHT])
1242 base_width += gui.scrollbar_width;
1243 return base_width;
1247 * Get the height of the widgets and decorations above and below the text area.
1250 gui_get_base_height()
1252 int base_height;
1254 base_height = 2 * gui.border_offset;
1255 if (gui.which_scrollbars[SBAR_BOTTOM])
1256 base_height += gui.scrollbar_height;
1257 #ifdef FEAT_GUI_GTK
1258 /* We can't take the sizes properly into account until anything is
1259 * realized. Therefore we recalculate all the values here just before
1260 * setting the size. (--mdcki) */
1261 #elif !defined(FEAT_GUI_MACVIM)
1262 # ifdef FEAT_MENU
1263 if (gui.menu_is_active)
1264 base_height += gui.menu_height;
1265 # endif
1266 # ifdef FEAT_TOOLBAR
1267 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1268 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1269 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1270 # else
1271 base_height += gui.toolbar_height;
1272 # endif
1273 # endif
1274 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1275 || defined(FEAT_GUI_MOTIF))
1276 if (gui_has_tabline())
1277 base_height += gui.tabline_height;
1278 # endif
1279 # ifdef FEAT_FOOTER
1280 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1281 base_height += gui.footer_height;
1282 # endif
1283 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1284 base_height += gui_mch_text_area_extra_height();
1285 # endif
1286 #endif
1287 return base_height;
1291 * Should be called after the GUI shell has been resized. Its arguments are
1292 * the new width and height of the shell in pixels.
1294 void
1295 gui_resize_shell(pixel_width, pixel_height)
1296 int pixel_width;
1297 int pixel_height;
1299 static int busy = FALSE;
1301 if (!gui.shell_created) /* ignore when still initializing */
1302 return;
1305 * Can't resize the screen while it is being redrawn. Remember the new
1306 * size and handle it later.
1308 if (updating_screen || busy)
1310 new_pixel_width = pixel_width;
1311 new_pixel_height = pixel_height;
1312 return;
1315 again:
1316 busy = TRUE;
1318 /* Flush pending output before redrawing */
1319 out_flush();
1321 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1322 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
1324 gui_position_components(pixel_width);
1326 gui_reset_scroll_region();
1328 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1329 * at the last line here (why does it have to be one row too low?).
1331 if (State == ASKMORE || State == CONFIRM)
1332 gui.row = gui.num_rows;
1334 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1335 * the safe side. */
1336 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1337 || gui.num_rows != Rows || gui.num_cols != Columns)
1338 shell_resized();
1340 gui_update_scrollbars(TRUE);
1341 gui_update_cursor(FALSE, TRUE);
1342 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1343 xim_set_status_area();
1344 #endif
1346 busy = FALSE;
1349 * We could have been called again while redrawing the screen.
1350 * Need to do it all again with the latest size then.
1352 if (new_pixel_height)
1354 pixel_width = new_pixel_width;
1355 pixel_height = new_pixel_height;
1356 new_pixel_width = 0;
1357 new_pixel_height = 0;
1358 goto again;
1363 * Check if gui_resize_shell() must be called.
1365 void
1366 gui_may_resize_shell()
1368 int h, w;
1370 if (new_pixel_height)
1372 /* careful: gui_resize_shell() may postpone the resize again if we
1373 * were called indirectly by it */
1374 w = new_pixel_width;
1375 h = new_pixel_height;
1376 new_pixel_width = 0;
1377 new_pixel_height = 0;
1378 gui_resize_shell(w, h);
1383 gui_get_shellsize()
1385 Rows = gui.num_rows;
1386 Columns = gui.num_cols;
1387 return OK;
1391 * Set the size of the Vim shell according to Rows and Columns.
1392 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1393 * on the screen.
1395 /*ARGSUSED*/
1396 void
1397 gui_set_shellsize(mustset, fit_to_display, direction)
1398 int mustset; /* set by the user */
1399 int fit_to_display;
1400 int direction; /* RESIZE_HOR, RESIZE_VER */
1402 int base_width;
1403 int base_height;
1404 int width;
1405 int height;
1406 int min_width;
1407 int min_height;
1408 int screen_w;
1409 int screen_h;
1411 if (!gui.shell_created)
1412 return;
1414 #ifdef MSWIN
1415 /* If not setting to a user specified size and maximized, calculate the
1416 * number of characters that fit in the maximized window. */
1417 if (!mustset && gui_mch_maximized())
1419 gui_mch_newfont();
1420 return;
1422 #endif
1424 base_width = gui_get_base_width();
1425 base_height = gui_get_base_height();
1426 #ifdef USE_SUN_WORKSHOP
1427 if (!mustset && usingSunWorkShop
1428 && workshop_get_width_height(&width, &height))
1430 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1431 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1433 else
1434 #endif
1436 width = Columns * gui.char_width + base_width;
1437 height = Rows * gui.char_height + base_height;
1440 if (fit_to_display)
1442 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1443 if ((direction & RESIZE_HOR) && width > screen_w)
1445 Columns = (screen_w - base_width) / gui.char_width;
1446 if (Columns < MIN_COLUMNS)
1447 Columns = MIN_COLUMNS;
1448 width = Columns * gui.char_width + base_width;
1450 if ((direction & RESIZE_VERT) && height > screen_h)
1452 Rows = (screen_h - base_height) / gui.char_height;
1453 check_shellsize();
1454 height = Rows * gui.char_height + base_height;
1457 gui.num_cols = Columns;
1458 gui.num_rows = Rows;
1460 min_width = base_width + MIN_COLUMNS * gui.char_width;
1461 min_height = base_height + MIN_LINES * gui.char_height;
1462 # ifdef FEAT_WINDOWS
1463 min_height += tabline_height() * gui.char_height;
1464 # endif
1466 gui_mch_set_shellsize(width, height, min_width, min_height,
1467 base_width, base_height, direction);
1468 if (fit_to_display)
1470 int x, y;
1472 /* Some window managers put the Vim window left of/above the screen. */
1473 gui_mch_update();
1474 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1475 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1478 gui_position_components(width);
1479 gui_update_scrollbars(TRUE);
1480 gui_reset_scroll_region();
1484 * Called when Rows and/or Columns has changed.
1486 void
1487 gui_new_shellsize()
1489 gui_reset_scroll_region();
1493 * Make scroll region cover whole screen.
1495 void
1496 gui_reset_scroll_region()
1498 gui.scroll_region_top = 0;
1499 gui.scroll_region_bot = gui.num_rows - 1;
1500 gui.scroll_region_left = 0;
1501 gui.scroll_region_right = gui.num_cols - 1;
1504 void
1505 gui_start_highlight(mask)
1506 int mask;
1508 if (mask > HL_ALL) /* highlight code */
1509 gui.highlight_mask = mask;
1510 else /* mask */
1511 gui.highlight_mask |= mask;
1514 void
1515 gui_stop_highlight(mask)
1516 int mask;
1518 if (mask > HL_ALL) /* highlight code */
1519 gui.highlight_mask = HL_NORMAL;
1520 else /* mask */
1521 gui.highlight_mask &= ~mask;
1525 * Clear a rectangular region of the screen from text pos (row1, col1) to
1526 * (row2, col2) inclusive.
1528 void
1529 gui_clear_block(row1, col1, row2, col2)
1530 int row1;
1531 int col1;
1532 int row2;
1533 int col2;
1535 /* Clear the selection if we are about to write over it */
1536 clip_may_clear_selection(row1, row2);
1538 gui_mch_clear_block(row1, col1, row2, col2);
1540 /* Invalidate cursor if it was in this block */
1541 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1542 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1543 gui.cursor_is_valid = FALSE;
1547 * Write code to update the cursor later. This avoids the need to flush the
1548 * output buffer before calling gui_update_cursor().
1550 void
1551 gui_update_cursor_later()
1553 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1556 void
1557 gui_write(s, len)
1558 char_u *s;
1559 int len;
1561 char_u *p;
1562 int arg1 = 0, arg2 = 0;
1563 /* this doesn't make sense, disabled until someone can explain why it
1564 * would be needed */
1565 #if 0 && (defined(RISCOS) || defined(WIN16))
1566 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1567 our redraw speed will suffer */
1568 #else
1569 int force_cursor = FALSE; /* force cursor update */
1570 #endif
1571 int force_scrollbar = FALSE;
1572 static win_T *old_curwin = NULL;
1574 /* #define DEBUG_GUI_WRITE */
1575 #ifdef DEBUG_GUI_WRITE
1577 int i;
1578 char_u *str;
1580 printf("gui_write(%d):\n ", len);
1581 for (i = 0; i < len; i++)
1582 if (s[i] == ESC)
1584 if (i != 0)
1585 printf("\n ");
1586 printf("<ESC>");
1588 else
1590 str = transchar_byte(s[i]);
1591 if (str[0] && str[1])
1592 printf("<%s>", (char *)str);
1593 else
1594 printf("%s", (char *)str);
1596 printf("\n");
1598 #endif
1599 while (len)
1601 if (s[0] == ESC && s[1] == '|')
1603 p = s + 2;
1604 if (VIM_ISDIGIT(*p))
1606 arg1 = getdigits(&p);
1607 if (p > s + len)
1608 break;
1609 if (*p == ';')
1611 ++p;
1612 arg2 = getdigits(&p);
1613 if (p > s + len)
1614 break;
1617 switch (*p)
1619 case 'C': /* Clear screen */
1620 clip_scroll_selection(9999);
1621 gui_mch_clear_all();
1622 gui.cursor_is_valid = FALSE;
1623 force_scrollbar = TRUE;
1624 break;
1625 case 'M': /* Move cursor */
1626 gui_set_cursor(arg1, arg2);
1627 break;
1628 case 's': /* force cursor (shape) update */
1629 force_cursor = TRUE;
1630 break;
1631 case 'R': /* Set scroll region */
1632 if (arg1 < arg2)
1634 gui.scroll_region_top = arg1;
1635 gui.scroll_region_bot = arg2;
1637 else
1639 gui.scroll_region_top = arg2;
1640 gui.scroll_region_bot = arg1;
1642 break;
1643 #ifdef FEAT_VERTSPLIT
1644 case 'V': /* Set vertical scroll region */
1645 if (arg1 < arg2)
1647 gui.scroll_region_left = arg1;
1648 gui.scroll_region_right = arg2;
1650 else
1652 gui.scroll_region_left = arg2;
1653 gui.scroll_region_right = arg1;
1655 break;
1656 #endif
1657 case 'd': /* Delete line */
1658 gui_delete_lines(gui.row, 1);
1659 break;
1660 case 'D': /* Delete lines */
1661 gui_delete_lines(gui.row, arg1);
1662 break;
1663 case 'i': /* Insert line */
1664 gui_insert_lines(gui.row, 1);
1665 break;
1666 case 'I': /* Insert lines */
1667 gui_insert_lines(gui.row, arg1);
1668 break;
1669 case '$': /* Clear to end-of-line */
1670 gui_clear_block(gui.row, gui.col, gui.row,
1671 (int)Columns - 1);
1672 break;
1673 case 'h': /* Turn on highlighting */
1674 gui_start_highlight(arg1);
1675 break;
1676 case 'H': /* Turn off highlighting */
1677 gui_stop_highlight(arg1);
1678 break;
1679 case 'f': /* flash the window (visual bell) */
1680 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1681 break;
1682 default:
1683 p = s + 1; /* Skip the ESC */
1684 break;
1686 len -= (int)(++p - s);
1687 s = p;
1689 else if (
1690 #ifdef EBCDIC
1691 CtrlChar(s[0]) != 0 /* Ctrl character */
1692 #else
1693 s[0] < 0x20 /* Ctrl character */
1694 #endif
1695 #ifdef FEAT_SIGN_ICONS
1696 && s[0] != SIGN_BYTE
1697 # ifdef FEAT_NETBEANS_INTG
1698 && s[0] != MULTISIGN_BYTE
1699 # endif
1700 #endif
1703 if (s[0] == '\n') /* NL */
1705 gui.col = 0;
1706 if (gui.row < gui.scroll_region_bot)
1707 gui.row++;
1708 else
1709 gui_delete_lines(gui.scroll_region_top, 1);
1711 else if (s[0] == '\r') /* CR */
1713 gui.col = 0;
1715 else if (s[0] == '\b') /* Backspace */
1717 if (gui.col)
1718 --gui.col;
1720 else if (s[0] == Ctrl_L) /* cursor-right */
1722 ++gui.col;
1724 else if (s[0] == Ctrl_G) /* Beep */
1726 gui_mch_beep();
1728 /* Other Ctrl character: shouldn't happen! */
1730 --len; /* Skip this char */
1731 ++s;
1733 else
1735 p = s;
1736 while (len > 0 && (
1737 #ifdef EBCDIC
1738 CtrlChar(*p) == 0
1739 #else
1740 *p >= 0x20
1741 #endif
1742 #ifdef FEAT_SIGN_ICONS
1743 || *p == SIGN_BYTE
1744 # ifdef FEAT_NETBEANS_INTG
1745 || *p == MULTISIGN_BYTE
1746 # endif
1747 #endif
1750 len--;
1751 p++;
1753 gui_outstr(s, (int)(p - s));
1754 s = p;
1758 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1759 * set). */
1760 if (force_cursor)
1761 gui_update_cursor(TRUE, TRUE);
1763 /* When switching to another window the dragging must have stopped.
1764 * Required for GTK, dragged_sb isn't reset. */
1765 if (old_curwin != curwin)
1766 gui.dragged_sb = SBAR_NONE;
1768 /* Update the scrollbars after clearing the screen or when switched
1769 * to another window.
1770 * Update the horizontal scrollbar always, it's difficult to check all
1771 * situations where it might change. */
1772 if (force_scrollbar || old_curwin != curwin)
1773 gui_update_scrollbars(force_scrollbar);
1774 else
1775 gui_update_horiz_scrollbar(FALSE);
1776 old_curwin = curwin;
1779 * We need to make sure this is cleared since Athena doesn't tell us when
1780 * he is done dragging. Do the same for GTK.
1782 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1783 gui.dragged_sb = SBAR_NONE;
1784 #endif
1786 gui_mch_flush(); /* In case vim decides to take a nap */
1790 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1791 * produces wrong results. Call gui_dont_update_cursor() before that code and
1792 * gui_can_update_cursor() afterwards.
1794 void
1795 gui_dont_update_cursor()
1797 if (gui.in_use)
1799 /* Undraw the cursor now, we probably can't do it after the change. */
1800 gui_undraw_cursor();
1801 can_update_cursor = FALSE;
1805 void
1806 gui_can_update_cursor()
1808 can_update_cursor = TRUE;
1809 /* No need to update the cursor right now, there is always more output
1810 * after scrolling. */
1813 static void
1814 gui_outstr(s, len)
1815 char_u *s;
1816 int len;
1818 int this_len;
1819 #ifdef FEAT_MBYTE
1820 int cells;
1821 #endif
1823 if (len == 0)
1824 return;
1826 if (len < 0)
1827 len = (int)STRLEN(s);
1829 while (len > 0)
1831 #ifdef FEAT_MBYTE
1832 if (has_mbyte)
1834 /* Find out how many chars fit in the current line. */
1835 cells = 0;
1836 for (this_len = 0; this_len < len; )
1838 cells += (*mb_ptr2cells)(s + this_len);
1839 if (gui.col + cells > Columns)
1840 break;
1841 this_len += (*mb_ptr2len)(s + this_len);
1843 if (this_len > len)
1844 this_len = len; /* don't include following composing char */
1846 else
1847 #endif
1848 if (gui.col + len > Columns)
1849 this_len = Columns - gui.col;
1850 else
1851 this_len = len;
1853 (void)gui_outstr_nowrap(s, this_len,
1854 0, (guicolor_T)0, (guicolor_T)0, 0);
1855 s += this_len;
1856 len -= this_len;
1857 #ifdef FEAT_MBYTE
1858 /* fill up for a double-width char that doesn't fit. */
1859 if (len > 0 && gui.col < Columns)
1860 (void)gui_outstr_nowrap((char_u *)" ", 1,
1861 0, (guicolor_T)0, (guicolor_T)0, 0);
1862 #endif
1863 /* The cursor may wrap to the next line. */
1864 if (gui.col >= Columns)
1866 gui.col = 0;
1867 gui.row++;
1873 * Output one character (may be one or two display cells).
1874 * Caller must check for valid "off".
1875 * Returns FAIL or OK, just like gui_outstr_nowrap().
1877 static int
1878 gui_screenchar(off, flags, fg, bg, back)
1879 int off; /* Offset from start of screen */
1880 int flags;
1881 guicolor_T fg, bg; /* colors for cursor */
1882 int back; /* backup this many chars when using bold trick */
1884 #ifdef FEAT_MBYTE
1885 char_u buf[MB_MAXBYTES + 1];
1887 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1888 if (enc_utf8 && ScreenLines[off] == 0)
1889 return OK;
1891 if (enc_utf8 && ScreenLinesUC[off] != 0)
1892 /* Draw UTF-8 multi-byte character. */
1893 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1894 flags, fg, bg, back);
1896 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1898 buf[0] = ScreenLines[off];
1899 buf[1] = ScreenLines2[off];
1900 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1903 /* Draw non-multi-byte character or DBCS character. */
1904 return gui_outstr_nowrap(ScreenLines + off,
1905 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1906 flags, fg, bg, back);
1907 #else
1908 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1909 #endif
1912 #ifdef HAVE_GTK2
1914 * Output the string at the given screen position. This is used in place
1915 * of gui_screenchar() where possible because Pango needs as much context
1916 * as possible to work nicely. It's a lot faster as well.
1918 static int
1919 gui_screenstr(off, len, flags, fg, bg, back)
1920 int off; /* Offset from start of screen */
1921 int len; /* string length in screen cells */
1922 int flags;
1923 guicolor_T fg, bg; /* colors for cursor */
1924 int back; /* backup this many chars when using bold trick */
1926 char_u *buf;
1927 int outlen = 0;
1928 int i;
1929 int retval;
1931 if (len <= 0) /* "cannot happen"? */
1932 return OK;
1934 if (enc_utf8)
1936 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1937 if (buf == NULL)
1938 return OK; /* not much we could do here... */
1940 for (i = off; i < off + len; ++i)
1942 if (ScreenLines[i] == 0)
1943 continue; /* skip second half of double-width char */
1945 if (ScreenLinesUC[i] == 0)
1946 buf[outlen++] = ScreenLines[i];
1947 else
1948 outlen += utfc_char2bytes(i, buf + outlen);
1951 buf[outlen] = NUL; /* only to aid debugging */
1952 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1953 vim_free(buf);
1955 return retval;
1957 else if (enc_dbcs == DBCS_JPNU)
1959 buf = alloc((unsigned)(len * 2 + 1));
1960 if (buf == NULL)
1961 return OK; /* not much we could do here... */
1963 for (i = off; i < off + len; ++i)
1965 buf[outlen++] = ScreenLines[i];
1967 /* handle double-byte single-width char */
1968 if (ScreenLines[i] == 0x8e)
1969 buf[outlen++] = ScreenLines2[i];
1970 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1971 buf[outlen++] = ScreenLines[++i];
1974 buf[outlen] = NUL; /* only to aid debugging */
1975 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1976 vim_free(buf);
1978 return retval;
1980 else
1982 return gui_outstr_nowrap(&ScreenLines[off], len,
1983 flags, fg, bg, back);
1986 #endif /* HAVE_GTK2 */
1989 * Output the given string at the current cursor position. If the string is
1990 * too long to fit on the line, then it is truncated.
1991 * "flags":
1992 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1993 * actually draw (an inverted) cursor.
1994 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
1995 * background.
1996 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1997 * it.
1998 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1999 * FAIL (the caller should start drawing "back" chars back).
2002 gui_outstr_nowrap(s, len, flags, fg, bg, back)
2003 char_u *s;
2004 int len;
2005 int flags;
2006 guicolor_T fg, bg; /* colors for cursor */
2007 int back; /* backup this many chars when using bold trick */
2009 long_u highlight_mask;
2010 long_u hl_mask_todo;
2011 guicolor_T fg_color;
2012 guicolor_T bg_color;
2013 guicolor_T sp_color;
2014 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2015 GuiFont font = NOFONT;
2016 # ifdef FEAT_XFONTSET
2017 GuiFontset fontset = NOFONTSET;
2018 # endif
2019 #endif
2020 attrentry_T *aep = NULL;
2021 int draw_flags;
2022 int col = gui.col;
2023 #ifdef FEAT_SIGN_ICONS
2024 int draw_sign = FALSE;
2025 # ifdef FEAT_NETBEANS_INTG
2026 int multi_sign = FALSE;
2027 # endif
2028 #endif
2030 if (len < 0)
2031 len = (int)STRLEN(s);
2032 if (len == 0)
2033 return OK;
2035 #ifdef FEAT_SIGN_ICONS
2036 if (*s == SIGN_BYTE
2037 # ifdef FEAT_NETBEANS_INTG
2038 || *s == MULTISIGN_BYTE
2039 # endif
2042 # ifdef FEAT_NETBEANS_INTG
2043 if (*s == MULTISIGN_BYTE)
2044 multi_sign = TRUE;
2045 # endif
2046 /* draw spaces instead */
2047 s = (char_u *)" ";
2048 if (len == 1 && col > 0)
2049 --col;
2050 len = 2;
2051 draw_sign = TRUE;
2052 highlight_mask = 0;
2054 else
2055 #endif
2056 if (gui.highlight_mask > HL_ALL)
2058 aep = syn_gui_attr2entry(gui.highlight_mask);
2059 if (aep == NULL) /* highlighting not set */
2060 highlight_mask = 0;
2061 else
2062 highlight_mask = aep->ae_attr;
2064 else
2065 highlight_mask = gui.highlight_mask;
2066 hl_mask_todo = highlight_mask;
2068 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2069 /* Set the font */
2070 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2071 font = aep->ae_u.gui.font;
2072 # ifdef FEAT_XFONTSET
2073 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2074 fontset = aep->ae_u.gui.fontset;
2075 # endif
2076 else
2078 # ifdef FEAT_XFONTSET
2079 if (gui.fontset != NOFONTSET)
2080 fontset = gui.fontset;
2081 else
2082 # endif
2083 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2085 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2087 font = gui.boldital_font;
2088 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2090 else if (gui.bold_font != NOFONT)
2092 font = gui.bold_font;
2093 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2095 else
2096 font = gui.norm_font;
2098 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2100 font = gui.ital_font;
2101 hl_mask_todo &= ~HL_ITALIC;
2103 else
2104 font = gui.norm_font;
2106 # ifdef FEAT_XFONTSET
2107 if (fontset != NOFONTSET)
2108 gui_mch_set_fontset(fontset);
2109 else
2110 # endif
2111 gui_mch_set_font(font);
2112 #endif
2114 draw_flags = 0;
2116 /* Set the color */
2117 bg_color = gui.back_pixel;
2118 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2120 draw_flags |= DRAW_CURSOR;
2121 fg_color = fg;
2122 bg_color = bg;
2123 sp_color = fg;
2125 else if (aep != NULL)
2127 fg_color = aep->ae_u.gui.fg_color;
2128 if (fg_color == INVALCOLOR)
2129 fg_color = gui.norm_pixel;
2130 bg_color = aep->ae_u.gui.bg_color;
2131 if (bg_color == INVALCOLOR)
2132 bg_color = gui.back_pixel;
2133 sp_color = aep->ae_u.gui.sp_color;
2134 if (sp_color == INVALCOLOR)
2135 sp_color = fg_color;
2137 else
2139 fg_color = gui.norm_pixel;
2140 sp_color = fg_color;
2143 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2145 #if defined(AMIGA) || defined(RISCOS)
2146 gui_mch_set_colors(bg_color, fg_color);
2147 #else
2148 gui_mch_set_fg_color(bg_color);
2149 gui_mch_set_bg_color(fg_color);
2150 #endif
2152 else
2154 #if defined(AMIGA) || defined(RISCOS)
2155 gui_mch_set_colors(fg_color, bg_color);
2156 #else
2157 gui_mch_set_fg_color(fg_color);
2158 gui_mch_set_bg_color(bg_color);
2159 #endif
2161 gui_mch_set_sp_color(sp_color);
2163 /* Clear the selection if we are about to write over it */
2164 if (!(flags & GUI_MON_NOCLEAR))
2165 clip_may_clear_selection(gui.row, gui.row);
2168 #ifndef MSWIN16_FASTTEXT
2169 /* If there's no bold font, then fake it */
2170 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2171 draw_flags |= DRAW_BOLD;
2172 #endif
2175 * When drawing bold or italic characters the spill-over from the left
2176 * neighbor may be destroyed. Let the caller backup to start redrawing
2177 * just after a blank.
2179 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2180 return FAIL;
2182 #if defined(RISCOS) || defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
2183 /* If there's no italic font, then fake it.
2184 * For GTK2, we don't need a different font for italic style. */
2185 if (hl_mask_todo & HL_ITALIC)
2186 draw_flags |= DRAW_ITALIC;
2188 /* Do we underline the text? */
2189 if (hl_mask_todo & HL_UNDERLINE)
2190 draw_flags |= DRAW_UNDERL;
2191 #else
2192 /* Do we underline the text? */
2193 if ((hl_mask_todo & HL_UNDERLINE)
2194 # ifndef MSWIN16_FASTTEXT
2195 || (hl_mask_todo & HL_ITALIC)
2196 # endif
2198 draw_flags |= DRAW_UNDERL;
2199 #endif
2200 /* Do we undercurl the text? */
2201 if (hl_mask_todo & HL_UNDERCURL)
2202 draw_flags |= DRAW_UNDERC;
2204 /* Do we draw transparently? */
2205 if (flags & GUI_MON_TRS_CURSOR)
2206 draw_flags |= DRAW_TRANSP;
2209 * Draw the text.
2211 #ifdef HAVE_GTK2
2212 /* The value returned is the length in display cells */
2213 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2214 #elif defined(FEAT_GUI_MACVIM) && defined(FEAT_MBYTE)
2215 /* The value returned is the length in display cells */
2216 len = gui_macvim_draw_string(gui.row, col, s, len, draw_flags);
2217 #else
2218 # ifdef FEAT_MBYTE
2219 if (enc_utf8)
2221 int start; /* index of bytes to be drawn */
2222 int cells; /* cellwidth of bytes to be drawn */
2223 int thislen; /* length of bytes to be drawin */
2224 int cn; /* cellwidth of current char */
2225 int i; /* index of current char */
2226 int c; /* current char value */
2227 int cl; /* byte length of current char */
2228 int comping; /* current char is composing */
2229 int scol = col; /* screen column */
2230 int dowide; /* use 'guifontwide' */
2232 /* Break the string at a composing character, it has to be drawn on
2233 * top of the previous character. */
2234 start = 0;
2235 cells = 0;
2236 for (i = 0; i < len; i += cl)
2238 c = utf_ptr2char(s + i);
2239 cn = utf_char2cells(c);
2240 if (cn > 1
2241 # ifdef FEAT_XFONTSET
2242 && fontset == NOFONTSET
2243 # endif
2244 && gui.wide_font != NOFONT)
2245 dowide = TRUE;
2246 else
2247 dowide = FALSE;
2248 comping = utf_iscomposing(c);
2249 if (!comping) /* count cells from non-composing chars */
2250 cells += cn;
2251 cl = utf_ptr2len(s + i);
2252 if (cl == 0) /* hit end of string */
2253 len = i + cl; /* len must be wrong "cannot happen" */
2255 /* print the string so far if it's the last character or there is
2256 * a composing character. */
2257 if (i + cl >= len || (comping && i > start) || dowide
2258 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2259 || (cn > 1
2260 # ifdef FEAT_XFONTSET
2261 /* No fontset: At least draw char after wide char at
2262 * right position. */
2263 && fontset == NOFONTSET
2264 # endif
2266 # endif
2269 if (comping || dowide)
2270 thislen = i - start;
2271 else
2272 thislen = i - start + cl;
2273 if (thislen > 0)
2275 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2276 draw_flags);
2277 start += thislen;
2279 scol += cells;
2280 cells = 0;
2281 if (dowide)
2283 gui_mch_set_font(gui.wide_font);
2284 gui_mch_draw_string(gui.row, scol - cn,
2285 s + start, cl, draw_flags);
2286 gui_mch_set_font(font);
2287 start += cl;
2290 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2291 /* No fontset: draw a space to fill the gap after a wide char
2292 * */
2293 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2294 # ifdef FEAT_XFONTSET
2295 && fontset == NOFONTSET
2296 # endif
2297 && !dowide)
2298 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2299 1, draw_flags);
2300 # endif
2302 /* Draw a composing char on top of the previous char. */
2303 if (comping)
2305 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2306 /* Carbon ATSUI autodraws composing char over previous char */
2307 gui_mch_draw_string(gui.row, scol, s + i, cl,
2308 draw_flags | DRAW_TRANSP);
2309 # else
2310 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2311 draw_flags | DRAW_TRANSP);
2312 # endif
2313 start = i + cl;
2316 /* The stuff below assumes "len" is the length in screen columns. */
2317 len = scol - col;
2319 else
2320 # endif
2322 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2323 # ifdef FEAT_MBYTE
2324 if (enc_dbcs == DBCS_JPNU)
2326 int clen = 0;
2327 int i;
2329 /* Get the length in display cells, this can be different from the
2330 * number of bytes for "euc-jp". */
2331 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2332 clen += (*mb_ptr2cells)(s + i);
2333 len = clen;
2335 # endif
2337 #endif /* !HAVE_GTK2 */
2339 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2340 gui.col = col + len;
2342 /* May need to invert it when it's part of the selection. */
2343 if (flags & GUI_MON_NOCLEAR)
2344 clip_may_redraw_selection(gui.row, col, len);
2346 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2348 /* Invalidate the old physical cursor position if we wrote over it */
2349 if (gui.cursor_row == gui.row
2350 && gui.cursor_col >= col
2351 && gui.cursor_col < col + len)
2352 gui.cursor_is_valid = FALSE;
2355 #ifdef FEAT_SIGN_ICONS
2356 if (draw_sign)
2357 /* Draw the sign on top of the spaces. */
2358 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2359 # ifdef FEAT_NETBEANS_INTG
2360 if (multi_sign)
2361 netbeans_draw_multisign_indicator(gui.row);
2362 # endif
2363 #endif
2365 return OK;
2369 * Un-draw the cursor. Actually this just redraws the character at the given
2370 * position. The character just before it too, for when it was in bold.
2372 void
2373 gui_undraw_cursor()
2375 if (gui.cursor_is_valid)
2377 #ifdef FEAT_HANGULIN
2378 if (composing_hangul
2379 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2380 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2381 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2382 gui.norm_pixel, gui.back_pixel, 0);
2383 else
2385 #endif
2386 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2387 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2388 && gui.cursor_col > 0)
2389 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2390 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2391 #ifdef FEAT_HANGULIN
2392 if (composing_hangul)
2393 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2394 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2396 #endif
2397 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2398 * here in case it wasn't needed to undraw it. */
2399 gui.cursor_is_valid = FALSE;
2403 void
2404 gui_redraw(x, y, w, h)
2405 int x;
2406 int y;
2407 int w;
2408 int h;
2410 int row1, col1, row2, col2;
2412 row1 = Y_2_ROW(y);
2413 col1 = X_2_COL(x);
2414 row2 = Y_2_ROW(y + h - 1);
2415 col2 = X_2_COL(x + w - 1);
2417 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2420 * We may need to redraw the cursor, but don't take it upon us to change
2421 * its location after a scroll.
2422 * (maybe be more strict even and test col too?)
2423 * These things may be outside the update/clipping region and reality may
2424 * not reflect Vims internal ideas if these operations are clipped away.
2426 if (gui.row == gui.cursor_row)
2427 gui_update_cursor(TRUE, TRUE);
2431 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2432 * from col1 to col2 (inclusive).
2433 * Return TRUE when the character before the first drawn character has
2434 * different attributes (may have to be redrawn too).
2437 gui_redraw_block(row1, col1, row2, col2, flags)
2438 int row1;
2439 int col1;
2440 int row2;
2441 int col2;
2442 int flags; /* flags for gui_outstr_nowrap() */
2444 int old_row, old_col;
2445 long_u old_hl_mask;
2446 int off;
2447 sattr_T first_attr;
2448 int idx, len;
2449 int back, nback;
2450 int retval = FALSE;
2451 #ifdef FEAT_MBYTE
2452 int orig_col1, orig_col2;
2453 #endif
2455 /* Don't try to update when ScreenLines is not valid */
2456 if (!screen_cleared || ScreenLines == NULL)
2457 return retval;
2459 /* Don't try to draw outside the shell! */
2460 /* Check everything, strange values may be caused by a big border width */
2461 col1 = check_col(col1);
2462 col2 = check_col(col2);
2463 row1 = check_row(row1);
2464 row2 = check_row(row2);
2466 /* Remember where our cursor was */
2467 old_row = gui.row;
2468 old_col = gui.col;
2469 old_hl_mask = gui.highlight_mask;
2470 #ifdef FEAT_MBYTE
2471 orig_col1 = col1;
2472 orig_col2 = col2;
2473 #endif
2475 for (gui.row = row1; gui.row <= row2; gui.row++)
2477 #ifdef FEAT_MBYTE
2478 /* When only half of a double-wide character is in the block, include
2479 * the other half. */
2480 col1 = orig_col1;
2481 col2 = orig_col2;
2482 off = LineOffset[gui.row];
2483 if (enc_dbcs != 0)
2485 if (col1 > 0)
2486 col1 -= dbcs_screen_head_off(ScreenLines + off,
2487 ScreenLines + off + col1);
2488 col2 += dbcs_screen_tail_off(ScreenLines + off,
2489 ScreenLines + off + col2);
2491 else if (enc_utf8)
2493 if (ScreenLines[off + col1] == 0)
2494 --col1;
2495 # ifdef HAVE_GTK2
2496 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2497 ++col2;
2498 # endif
2500 #endif
2501 gui.col = col1;
2502 off = LineOffset[gui.row] + gui.col;
2503 len = col2 - col1 + 1;
2505 /* Find how many chars back this highlighting starts, or where a space
2506 * is. Needed for when the bold trick is used */
2507 for (back = 0; back < col1; ++back)
2508 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2509 || ScreenLines[off - 1 - back] == ' ')
2510 break;
2511 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2512 && ScreenLines[off - 1] != ' ');
2514 /* Break it up in strings of characters with the same attributes. */
2515 /* Print UTF-8 characters individually. */
2516 while (len > 0)
2518 first_attr = ScreenAttrs[off];
2519 gui.highlight_mask = first_attr;
2520 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2521 if (enc_utf8 && ScreenLinesUC[off] != 0)
2523 /* output multi-byte character separately */
2524 nback = gui_screenchar(off, flags,
2525 (guicolor_T)0, (guicolor_T)0, back);
2526 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2527 idx = 2;
2528 else
2529 idx = 1;
2531 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2533 /* output double-byte, single-width character separately */
2534 nback = gui_screenchar(off, flags,
2535 (guicolor_T)0, (guicolor_T)0, back);
2536 idx = 1;
2538 else
2539 #endif
2541 #ifdef HAVE_GTK2
2542 for (idx = 0; idx < len; ++idx)
2544 if (enc_utf8 && ScreenLines[off + idx] == 0)
2545 continue; /* skip second half of double-width char */
2546 if (ScreenAttrs[off + idx] != first_attr)
2547 break;
2549 /* gui_screenstr() takes care of multibyte chars */
2550 nback = gui_screenstr(off, idx, flags,
2551 (guicolor_T)0, (guicolor_T)0, back);
2552 #else
2553 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2554 idx++)
2556 # ifdef FEAT_MBYTE
2557 /* Stop at a multi-byte Unicode character. */
2558 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2559 break;
2560 if (enc_dbcs == DBCS_JPNU)
2562 /* Stop at a double-byte single-width char. */
2563 if (ScreenLines[off + idx] == 0x8e)
2564 break;
2565 if (len > 1 && (*mb_ptr2len)(ScreenLines
2566 + off + idx) == 2)
2567 ++idx; /* skip second byte of double-byte char */
2569 # endif
2571 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2572 (guicolor_T)0, (guicolor_T)0, back);
2573 #endif
2575 if (nback == FAIL)
2577 /* Must back up to start drawing where a bold or italic word
2578 * starts. */
2579 off -= back;
2580 len += back;
2581 gui.col -= back;
2583 else
2585 off += idx;
2586 len -= idx;
2588 back = 0;
2592 /* Put the cursor back where it was */
2593 gui.row = old_row;
2594 gui.col = old_col;
2595 gui.highlight_mask = (int)old_hl_mask;
2597 return retval;
2600 static void
2601 gui_delete_lines(row, count)
2602 int row;
2603 int count;
2605 if (count <= 0)
2606 return;
2608 if (row + count > gui.scroll_region_bot)
2609 /* Scrolled out of region, just blank the lines out */
2610 gui_clear_block(row, gui.scroll_region_left,
2611 gui.scroll_region_bot, gui.scroll_region_right);
2612 else
2614 gui_mch_delete_lines(row, count);
2616 /* If the cursor was in the deleted lines it's now gone. If the
2617 * cursor was in the scrolled lines adjust its position. */
2618 if (gui.cursor_row >= row
2619 && gui.cursor_col >= gui.scroll_region_left
2620 && gui.cursor_col <= gui.scroll_region_right)
2622 if (gui.cursor_row < row + count)
2623 gui.cursor_is_valid = FALSE;
2624 else if (gui.cursor_row <= gui.scroll_region_bot)
2625 gui.cursor_row -= count;
2630 static void
2631 gui_insert_lines(row, count)
2632 int row;
2633 int count;
2635 if (count <= 0)
2636 return;
2638 if (row + count > gui.scroll_region_bot)
2639 /* Scrolled out of region, just blank the lines out */
2640 gui_clear_block(row, gui.scroll_region_left,
2641 gui.scroll_region_bot, gui.scroll_region_right);
2642 else
2644 gui_mch_insert_lines(row, count);
2646 if (gui.cursor_row >= gui.row
2647 && gui.cursor_col >= gui.scroll_region_left
2648 && gui.cursor_col <= gui.scroll_region_right)
2650 if (gui.cursor_row <= gui.scroll_region_bot - count)
2651 gui.cursor_row += count;
2652 else if (gui.cursor_row <= gui.scroll_region_bot)
2653 gui.cursor_is_valid = FALSE;
2659 * The main GUI input routine. Waits for a character from the keyboard.
2660 * wtime == -1 Wait forever.
2661 * wtime == 0 Don't wait.
2662 * wtime > 0 Wait wtime milliseconds for a character.
2663 * Returns OK if a character was found to be available within the given time,
2664 * or FAIL otherwise.
2667 gui_wait_for_chars(wtime)
2668 long wtime;
2670 int retval;
2673 * If we're going to wait a bit, update the menus and mouse shape for the
2674 * current State.
2676 if (wtime != 0)
2678 #ifdef FEAT_MENU
2679 gui_update_menus(0);
2680 #endif
2683 gui_mch_update();
2684 if (input_available()) /* Got char, return immediately */
2685 return OK;
2686 if (wtime == 0) /* Don't wait for char */
2687 return FAIL;
2689 /* Before waiting, flush any output to the screen. */
2690 gui_mch_flush();
2692 if (wtime > 0)
2694 /* Blink when waiting for a character. Probably only does something
2695 * for showmatch() */
2696 gui_mch_start_blink();
2697 retval = gui_mch_wait_for_chars(wtime);
2698 gui_mch_stop_blink();
2699 return retval;
2703 * While we are waiting indefinitely for a character, blink the cursor.
2705 gui_mch_start_blink();
2707 retval = FAIL;
2709 * We may want to trigger the CursorHold event. First wait for
2710 * 'updatetime' and if nothing is typed within that time put the
2711 * K_CURSORHOLD key in the input buffer.
2713 if (gui_mch_wait_for_chars(p_ut) == OK)
2714 retval = OK;
2715 #ifdef FEAT_AUTOCMD
2716 else if (trigger_cursorhold())
2718 char_u buf[3];
2720 /* Put K_CURSORHOLD in the input buffer. */
2721 buf[0] = CSI;
2722 buf[1] = KS_EXTRA;
2723 buf[2] = (int)KE_CURSORHOLD;
2724 add_to_input_buf(buf, 3);
2726 retval = OK;
2728 #endif
2730 if (retval == FAIL)
2732 /* Blocking wait. */
2733 before_blocking();
2734 retval = gui_mch_wait_for_chars(-1L);
2737 gui_mch_stop_blink();
2738 return retval;
2742 * Fill p[4] with mouse coordinates encoded for check_termcode().
2744 static void
2745 fill_mouse_coord(p, col, row)
2746 char_u *p;
2747 int col;
2748 int row;
2750 p[0] = (char_u)(col / 128 + ' ' + 1);
2751 p[1] = (char_u)(col % 128 + ' ' + 1);
2752 p[2] = (char_u)(row / 128 + ' ' + 1);
2753 p[3] = (char_u)(row % 128 + ' ' + 1);
2757 * Generic mouse support function. Add a mouse event to the input buffer with
2758 * the given properties.
2759 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2760 * MOUSE_X1, MOUSE_X2
2761 * MOUSE_DRAG, or MOUSE_RELEASE.
2762 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2763 * x, y --- Coordinates of mouse in pixels.
2764 * repeated_click --- TRUE if this click comes only a short time after a
2765 * previous click.
2766 * modifiers --- Bit field which may be any of the following modifiers
2767 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2768 * This function will ignore drag events where the mouse has not moved to a new
2769 * character.
2771 void
2772 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2773 int button;
2774 int x;
2775 int y;
2776 int repeated_click;
2777 int_u modifiers;
2779 static int prev_row = 0, prev_col = 0;
2780 static int prev_button = -1;
2781 static int num_clicks = 1;
2782 char_u string[10];
2783 enum key_extra button_char;
2784 int row, col;
2785 #ifdef FEAT_CLIPBOARD
2786 int checkfor;
2787 int did_clip = FALSE;
2788 #endif
2791 * Scrolling may happen at any time, also while a selection is present.
2793 switch (button)
2795 case MOUSE_X1:
2796 button_char = KE_X1MOUSE;
2797 goto button_set;
2798 case MOUSE_X2:
2799 button_char = KE_X2MOUSE;
2800 goto button_set;
2801 case MOUSE_4:
2802 button_char = KE_MOUSEDOWN;
2803 goto button_set;
2804 case MOUSE_5:
2805 button_char = KE_MOUSEUP;
2806 button_set:
2808 /* Don't put events in the input queue now. */
2809 if (hold_gui_events)
2810 return;
2812 string[3] = CSI;
2813 string[4] = KS_EXTRA;
2814 string[5] = (int)button_char;
2816 /* Pass the pointer coordinates of the scroll event so that we
2817 * know which window to scroll. */
2818 row = gui_xy2colrow(x, y, &col);
2819 string[6] = (char_u)(col / 128 + ' ' + 1);
2820 string[7] = (char_u)(col % 128 + ' ' + 1);
2821 string[8] = (char_u)(row / 128 + ' ' + 1);
2822 string[9] = (char_u)(row % 128 + ' ' + 1);
2824 if (modifiers == 0)
2825 add_to_input_buf(string + 3, 7);
2826 else
2828 string[0] = CSI;
2829 string[1] = KS_MODIFIER;
2830 string[2] = 0;
2831 if (modifiers & MOUSE_SHIFT)
2832 string[2] |= MOD_MASK_SHIFT;
2833 if (modifiers & MOUSE_CTRL)
2834 string[2] |= MOD_MASK_CTRL;
2835 if (modifiers & MOUSE_ALT)
2836 string[2] |= MOD_MASK_ALT;
2837 add_to_input_buf(string, 10);
2839 return;
2843 #ifdef FEAT_CLIPBOARD
2844 /* If a clipboard selection is in progress, handle it */
2845 if (clip_star.state == SELECT_IN_PROGRESS)
2847 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2848 return;
2851 /* Determine which mouse settings to look for based on the current mode */
2852 switch (get_real_state())
2854 case NORMAL_BUSY:
2855 case OP_PENDING:
2856 case NORMAL: checkfor = MOUSE_NORMAL; break;
2857 case VISUAL: checkfor = MOUSE_VISUAL; break;
2858 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2859 case REPLACE:
2860 case REPLACE+LANGMAP:
2861 #ifdef FEAT_VREPLACE
2862 case VREPLACE:
2863 case VREPLACE+LANGMAP:
2864 #endif
2865 case INSERT:
2866 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2867 case ASKMORE:
2868 case HITRETURN: /* At the more- and hit-enter prompt pass the
2869 mouse event for a click on or below the
2870 message line. */
2871 if (Y_2_ROW(y) >= msg_row)
2872 checkfor = MOUSE_NORMAL;
2873 else
2874 checkfor = MOUSE_RETURN;
2875 break;
2878 * On the command line, use the clipboard selection on all lines
2879 * but the command line. But not when pasting.
2881 case CMDLINE:
2882 case CMDLINE+LANGMAP:
2883 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2884 checkfor = MOUSE_NONE;
2885 else
2886 checkfor = MOUSE_COMMAND;
2887 break;
2889 default:
2890 checkfor = MOUSE_NONE;
2891 break;
2895 * Allow clipboard selection of text on the command line in "normal"
2896 * modes. Don't do this when dragging the status line, or extending a
2897 * Visual selection.
2899 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2900 && Y_2_ROW(y) >= topframe->fr_height
2901 # ifdef FEAT_WINDOWS
2902 + firstwin->w_winrow
2903 # endif
2904 && button != MOUSE_DRAG
2905 # ifdef FEAT_MOUSESHAPE
2906 && !drag_status_line
2907 # ifdef FEAT_VERTSPLIT
2908 && !drag_sep_line
2909 # endif
2910 # endif
2912 checkfor = MOUSE_NONE;
2915 * Use modeless selection when holding CTRL and SHIFT pressed.
2917 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2918 checkfor = MOUSE_NONEF;
2921 * In Ex mode, always use modeless selection.
2923 if (exmode_active)
2924 checkfor = MOUSE_NONE;
2927 * If the mouse settings say to not use the mouse, use the modeless
2928 * selection. But if Visual is active, assume that only the Visual area
2929 * will be selected.
2930 * Exception: On the command line, both the selection is used and a mouse
2931 * key is send.
2933 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2935 #ifdef FEAT_VISUAL
2936 /* Don't do modeless selection in Visual mode. */
2937 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2938 return;
2939 #endif
2942 * When 'mousemodel' is "popup", shift-left is translated to right.
2943 * But not when also using Ctrl.
2945 if (mouse_model_popup() && button == MOUSE_LEFT
2946 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2948 button = MOUSE_RIGHT;
2949 modifiers &= ~ MOUSE_SHIFT;
2952 /* If the selection is done, allow the right button to extend it.
2953 * If the selection is cleared, allow the right button to start it
2954 * from the cursor position. */
2955 if (button == MOUSE_RIGHT)
2957 if (clip_star.state == SELECT_CLEARED)
2959 if (State & CMDLINE)
2961 col = msg_col;
2962 row = msg_row;
2964 else
2966 col = curwin->w_wcol;
2967 row = curwin->w_wrow + W_WINROW(curwin);
2969 clip_start_selection(col, row, FALSE);
2971 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2972 repeated_click);
2973 did_clip = TRUE;
2975 /* Allow the left button to start the selection */
2976 else if (button ==
2977 # ifdef RISCOS
2978 /* Only start a drag on a drag event. Otherwise
2979 * we don't get a release event. */
2980 MOUSE_DRAG
2981 # else
2982 MOUSE_LEFT
2983 # endif
2986 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2987 did_clip = TRUE;
2989 # ifdef RISCOS
2990 else if (button == MOUSE_LEFT)
2992 clip_clear_selection();
2993 did_clip = TRUE;
2995 # endif
2997 /* Always allow pasting */
2998 if (button != MOUSE_MIDDLE)
3000 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3001 return;
3002 if (checkfor != MOUSE_COMMAND)
3003 button = MOUSE_LEFT;
3005 repeated_click = FALSE;
3008 if (clip_star.state != SELECT_CLEARED && !did_clip)
3009 clip_clear_selection();
3010 #endif
3012 /* Don't put events in the input queue now. */
3013 if (hold_gui_events)
3014 return;
3016 row = gui_xy2colrow(x, y, &col);
3019 * If we are dragging and the mouse hasn't moved far enough to be on a
3020 * different character, then don't send an event to vim.
3022 if (button == MOUSE_DRAG)
3024 if (row == prev_row && col == prev_col)
3025 return;
3026 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3027 if (y < 0)
3028 row = -1;
3032 * If topline has changed (window scrolled) since the last click, reset
3033 * repeated_click, because we don't want starting Visual mode when
3034 * clicking on a different character in the text.
3036 if (curwin->w_topline != gui_prev_topline
3037 #ifdef FEAT_DIFF
3038 || curwin->w_topfill != gui_prev_topfill
3039 #endif
3041 repeated_click = FALSE;
3043 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3044 string[1] = KS_MOUSE;
3045 string[2] = KE_FILLER;
3046 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3048 if (repeated_click)
3051 * Handle multiple clicks. They only count if the mouse is still
3052 * pointing at the same character.
3054 if (button != prev_button || row != prev_row || col != prev_col)
3055 num_clicks = 1;
3056 else if (++num_clicks > 4)
3057 num_clicks = 1;
3059 else
3060 num_clicks = 1;
3061 prev_button = button;
3062 gui_prev_topline = curwin->w_topline;
3063 #ifdef FEAT_DIFF
3064 gui_prev_topfill = curwin->w_topfill;
3065 #endif
3067 string[3] = (char_u)(button | 0x20);
3068 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3070 else
3071 string[3] = (char_u)button;
3073 string[3] |= modifiers;
3074 fill_mouse_coord(string + 4, col, row);
3075 add_to_input_buf(string, 8);
3077 if (row < 0)
3078 prev_row = 0;
3079 else
3080 prev_row = row;
3081 prev_col = col;
3084 * We need to make sure this is cleared since Athena doesn't tell us when
3085 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3087 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3088 gui.dragged_sb = SBAR_NONE;
3089 #endif
3093 * Convert x and y coordinate to column and row in text window.
3094 * Corrects for multi-byte character.
3095 * returns column in "*colp" and row as return value;
3098 gui_xy2colrow(x, y, colp)
3099 int x;
3100 int y;
3101 int *colp;
3103 int col = check_col(X_2_COL(x));
3104 int row = check_row(Y_2_ROW(y));
3106 #ifdef FEAT_MBYTE
3107 *colp = mb_fix_col(col, row);
3108 #else
3109 *colp = col;
3110 #endif
3111 return row;
3114 #if defined(FEAT_MENU) || defined(PROTO)
3116 * Callback function for when a menu entry has been selected.
3118 void
3119 gui_menu_cb(menu)
3120 vimmenu_T *menu;
3122 char_u bytes[sizeof(long_u)];
3124 /* Don't put events in the input queue now. */
3125 if (hold_gui_events)
3126 return;
3128 bytes[0] = CSI;
3129 bytes[1] = KS_MENU;
3130 bytes[2] = KE_FILLER;
3131 add_to_input_buf(bytes, 3);
3132 add_long_to_buf((long_u)menu, bytes);
3133 add_to_input_buf_csi(bytes, sizeof(long_u));
3135 #endif
3137 static int prev_which_scrollbars[3];
3140 * Set which components are present.
3141 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3142 * in p_go.
3144 /*ARGSUSED*/
3145 void
3146 gui_init_which_components(oldval)
3147 char_u *oldval;
3149 #ifdef FEAT_MENU
3150 static int prev_menu_is_active = -1;
3151 #endif
3152 #ifdef FEAT_TOOLBAR
3153 static int prev_toolbar = -1;
3154 int using_toolbar = FALSE;
3155 #endif
3156 #ifdef FEAT_GUI_TABLINE
3157 int using_tabline;
3158 #endif
3159 #ifdef FEAT_FOOTER
3160 static int prev_footer = -1;
3161 int using_footer = FALSE;
3162 #endif
3163 #if defined(FEAT_MENU) && !defined(WIN16)
3164 static int prev_tearoff = -1;
3165 int using_tearoff = FALSE;
3166 #endif
3168 char_u *p;
3169 int i;
3170 #ifdef FEAT_MENU
3171 int grey_old, grey_new;
3172 char_u *temp;
3173 #endif
3174 win_T *wp;
3175 int need_set_size;
3176 int fix_size;
3178 #ifdef FEAT_MENU
3179 if (oldval != NULL && gui.in_use)
3182 * Check if the menu's go from grey to non-grey or vise versa.
3184 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3185 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3186 if (grey_old != grey_new)
3188 temp = p_go;
3189 p_go = oldval;
3190 gui_update_menus(MENU_ALL_MODES);
3191 p_go = temp;
3194 gui.menu_is_active = FALSE;
3195 #endif
3197 for (i = 0; i < 3; i++)
3198 gui.which_scrollbars[i] = FALSE;
3199 for (p = p_go; *p; p++)
3200 switch (*p)
3202 case GO_LEFT:
3203 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3204 break;
3205 case GO_RIGHT:
3206 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3207 break;
3208 #ifdef FEAT_VERTSPLIT
3209 case GO_VLEFT:
3210 if (win_hasvertsplit())
3211 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3212 break;
3213 case GO_VRIGHT:
3214 if (win_hasvertsplit())
3215 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3216 break;
3217 #endif
3218 case GO_BOT:
3219 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3220 break;
3221 #ifdef FEAT_MENU
3222 case GO_MENUS:
3223 gui.menu_is_active = TRUE;
3224 break;
3225 #endif
3226 case GO_GREY:
3227 /* make menu's have grey items, ignored here */
3228 break;
3229 #ifdef FEAT_TOOLBAR
3230 case GO_TOOLBAR:
3231 using_toolbar = TRUE;
3232 break;
3233 #endif
3234 #ifdef FEAT_FOOTER
3235 case GO_FOOTER:
3236 using_footer = TRUE;
3237 break;
3238 #endif
3239 case GO_TEAROFF:
3240 #if defined(FEAT_MENU) && !defined(WIN16)
3241 using_tearoff = TRUE;
3242 #endif
3243 break;
3244 default:
3245 /* Ignore options that are not supported */
3246 break;
3249 if (gui.in_use)
3251 need_set_size = 0;
3252 fix_size = FALSE;
3254 #ifdef FEAT_GUI_TABLINE
3255 /* Update the GUI tab line, it may appear or disappear. This may
3256 * cause the non-GUI tab line to disappear or appear. */
3257 using_tabline = gui_has_tabline();
3258 if (!gui_mch_showing_tabline() != !using_tabline)
3260 /* We don't want a resize event change "Rows" here, save and
3261 * restore it. Resizing is handled below. */
3262 i = Rows;
3263 gui_update_tabline();
3264 Rows = i;
3265 need_set_size |= RESIZE_VERT;
3266 if (using_tabline)
3267 fix_size = TRUE;
3268 if (!gui_use_tabline())
3269 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3271 #endif
3273 for (i = 0; i < 3; i++)
3275 /* The scrollbar needs to be updated when it is shown/unshown and
3276 * when switching tab pages. But the size only changes when it's
3277 * shown/unshown. Thus we need two places to remember whether a
3278 * scrollbar is there or not. */
3279 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3280 #ifdef FEAT_WINDOWS
3281 || gui.which_scrollbars[i]
3282 != curtab->tp_prev_which_scrollbars[i]
3283 #endif
3286 if (i == SBAR_BOTTOM)
3287 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3288 gui.which_scrollbars[i]);
3289 else
3291 FOR_ALL_WINDOWS(wp)
3293 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3296 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3298 if (i == SBAR_BOTTOM)
3299 need_set_size |= RESIZE_VERT;
3300 else
3301 need_set_size |= RESIZE_HOR;
3302 if (gui.which_scrollbars[i])
3303 fix_size = TRUE;
3306 #ifdef FEAT_WINDOWS
3307 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3308 #endif
3309 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3312 #ifdef FEAT_MENU
3313 if (gui.menu_is_active != prev_menu_is_active)
3315 /* We don't want a resize event change "Rows" here, save and
3316 * restore it. Resizing is handled below. */
3317 i = Rows;
3318 gui_mch_enable_menu(gui.menu_is_active);
3319 Rows = i;
3320 prev_menu_is_active = gui.menu_is_active;
3321 need_set_size |= RESIZE_VERT;
3322 if (gui.menu_is_active)
3323 fix_size = TRUE;
3325 #endif
3327 #ifdef FEAT_TOOLBAR
3328 if (using_toolbar != prev_toolbar)
3330 gui_mch_show_toolbar(using_toolbar);
3331 prev_toolbar = using_toolbar;
3332 need_set_size |= RESIZE_VERT;
3333 if (using_toolbar)
3334 fix_size = TRUE;
3336 #endif
3337 #ifdef FEAT_FOOTER
3338 if (using_footer != prev_footer)
3340 gui_mch_enable_footer(using_footer);
3341 prev_footer = using_footer;
3342 need_set_size |= RESIZE_VERT;
3343 if (using_footer)
3344 fix_size = TRUE;
3346 #endif
3347 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3348 if (using_tearoff != prev_tearoff)
3350 gui_mch_toggle_tearoffs(using_tearoff);
3351 prev_tearoff = using_tearoff;
3353 #endif
3354 if (need_set_size != 0)
3356 #ifdef FEAT_GUI_GTK
3357 long prev_Columns = Columns;
3358 long prev_Rows = Rows;
3359 #endif
3360 /* Adjust the size of the window to make the text area keep the
3361 * same size and to avoid that part of our window is off-screen
3362 * and a scrollbar can't be used, for example. */
3363 gui_set_shellsize(FALSE, fix_size, need_set_size);
3365 #ifdef FEAT_GUI_GTK
3366 /* GTK has the annoying habit of sending us resize events when
3367 * changing the window size ourselves. This mostly happens when
3368 * waiting for a character to arrive, quite unpredictably, and may
3369 * change Columns and Rows when we don't want it. Wait for a
3370 * character here to avoid this effect.
3371 * If you remove this, please test this command for resizing
3372 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3373 * Don't do this while starting up though.
3374 * Don't change Rows when adding menu/toolbar/tabline.
3375 * Don't change Columns when adding vertical toolbar. */
3376 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3377 (void)char_avail();
3378 if ((need_set_size & RESIZE_VERT) == 0)
3379 Rows = prev_Rows;
3380 if ((need_set_size & RESIZE_HOR) == 0)
3381 Columns = prev_Columns;
3382 #endif
3384 #ifdef FEAT_WINDOWS
3385 /* When the console tabline appears or disappears the window positions
3386 * change. */
3387 if (firstwin->w_winrow != tabline_height())
3388 shell_new_rows(); /* recompute window positions and heights */
3389 #endif
3393 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3395 * Return TRUE if the GUI is taking care of the tabline.
3396 * It may still be hidden if 'showtabline' is zero.
3399 gui_use_tabline()
3401 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3405 * Return TRUE if the GUI is showing the tabline.
3406 * This uses 'showtabline'.
3408 static int
3409 gui_has_tabline()
3411 if (!gui_use_tabline()
3412 || p_stal == 0
3413 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3414 return FALSE;
3415 return TRUE;
3419 * Update the tabline.
3420 * This may display/undisplay the tabline and update the labels.
3422 void
3423 gui_update_tabline()
3425 int showit = gui_has_tabline();
3426 int shown = gui_mch_showing_tabline();
3428 if (!gui.starting && starting == 0)
3430 /* Updating the tabline uses direct GUI commands, flush
3431 * outstanding instructions first. (esp. clear screen) */
3432 out_flush();
3433 gui_mch_flush();
3435 if (!showit != !shown)
3436 gui_mch_show_tabline(showit);
3437 if (showit != 0)
3438 gui_mch_update_tabline();
3440 /* When the tabs change from hidden to shown or from shown to
3441 * hidden the size of the text area should remain the same. */
3442 if (!showit != !shown)
3443 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3448 * Get the label or tooltip for tab page "tp" into NameBuff[].
3450 void
3451 get_tabline_label(tp, tooltip)
3452 tabpage_T *tp;
3453 int tooltip; /* TRUE: get tooltip */
3455 int modified = FALSE;
3456 char_u buf[40];
3457 int wincount;
3458 win_T *wp;
3459 char_u **opt;
3461 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3462 opt = (tooltip ? &p_gtt : &p_gtl);
3463 if (**opt != NUL)
3465 int use_sandbox = FALSE;
3466 int save_called_emsg = called_emsg;
3467 char_u res[MAXPATHL];
3468 tabpage_T *save_curtab;
3469 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3470 : "guitablabel");
3472 called_emsg = FALSE;
3474 printer_page_num = tabpage_index(tp);
3475 # ifdef FEAT_EVAL
3476 set_vim_var_nr(VV_LNUM, printer_page_num);
3477 use_sandbox = was_set_insecurely(opt_name, 0);
3478 # endif
3479 /* It's almost as going to the tabpage, but without autocommands. */
3480 curtab->tp_firstwin = firstwin;
3481 curtab->tp_lastwin = lastwin;
3482 curtab->tp_curwin = curwin;
3483 save_curtab = curtab;
3484 curtab = tp;
3485 topframe = curtab->tp_topframe;
3486 firstwin = curtab->tp_firstwin;
3487 lastwin = curtab->tp_lastwin;
3488 curwin = curtab->tp_curwin;
3489 curbuf = curwin->w_buffer;
3491 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3492 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3493 0, (int)Columns, NULL, NULL);
3494 STRCPY(NameBuff, res);
3496 /* Back to the original curtab. */
3497 curtab = save_curtab;
3498 topframe = curtab->tp_topframe;
3499 firstwin = curtab->tp_firstwin;
3500 lastwin = curtab->tp_lastwin;
3501 curwin = curtab->tp_curwin;
3502 curbuf = curwin->w_buffer;
3504 if (called_emsg)
3505 set_string_option_direct(opt_name, -1,
3506 (char_u *)"", OPT_FREE, SID_ERROR);
3507 called_emsg |= save_called_emsg;
3510 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3511 * use a default label. */
3512 if (**opt == NUL || *NameBuff == NUL)
3514 /* Get the buffer name into NameBuff[] and shorten it. */
3515 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3516 if (!tooltip)
3517 shorten_dir(NameBuff);
3519 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3520 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3521 if (bufIsChanged(wp->w_buffer))
3522 modified = TRUE;
3523 if (modified || wincount > 1)
3525 if (wincount > 1)
3526 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3527 else
3528 buf[0] = NUL;
3529 if (modified)
3530 STRCAT(buf, "+");
3531 STRCAT(buf, " ");
3532 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
3533 mch_memmove(NameBuff, buf, STRLEN(buf));
3539 * Send the event for clicking to select tab page "nr".
3540 * Returns TRUE if it was done, FALSE when skipped because we are already at
3541 * that tab page or the cmdline window is open.
3544 send_tabline_event(nr)
3545 int nr;
3547 char_u string[3];
3549 if (nr == tabpage_index(curtab))
3550 return FALSE;
3552 /* Don't put events in the input queue now. */
3553 if (hold_gui_events
3554 # ifdef FEAT_CMDWIN
3555 || cmdwin_type != 0
3556 # endif
3559 /* Set it back to the current tab page. */
3560 gui_mch_set_curtab(tabpage_index(curtab));
3561 return FALSE;
3564 string[0] = CSI;
3565 string[1] = KS_TABLINE;
3566 string[2] = KE_FILLER;
3567 add_to_input_buf(string, 3);
3568 string[0] = nr;
3569 add_to_input_buf_csi(string, 1);
3570 return TRUE;
3574 * Send a tabline menu event
3576 void
3577 send_tabline_menu_event(tabidx, event)
3578 int tabidx;
3579 int event;
3581 char_u string[3];
3583 /* Don't put events in the input queue now. */
3584 if (hold_gui_events)
3585 return;
3587 string[0] = CSI;
3588 string[1] = KS_TABMENU;
3589 string[2] = KE_FILLER;
3590 add_to_input_buf(string, 3);
3591 string[0] = tabidx;
3592 string[1] = (char_u)(long)event;
3593 add_to_input_buf_csi(string, 2);
3596 #endif
3599 * Scrollbar stuff:
3602 #if defined(FEAT_WINDOWS) || defined(PROTO)
3604 * Remove all scrollbars. Used before switching to another tab page.
3606 void
3607 gui_remove_scrollbars()
3609 int i;
3610 win_T *wp;
3612 for (i = 0; i < 3; i++)
3614 if (i == SBAR_BOTTOM)
3615 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3616 else
3618 FOR_ALL_WINDOWS(wp)
3620 gui_do_scrollbar(wp, i, FALSE);
3623 curtab->tp_prev_which_scrollbars[i] = -1;
3626 #endif
3628 void
3629 gui_create_scrollbar(sb, type, wp)
3630 scrollbar_T *sb;
3631 int type;
3632 win_T *wp;
3634 static int sbar_ident = 0;
3636 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3637 sb->wp = wp;
3638 sb->type = type;
3639 sb->value = 0;
3640 #ifdef FEAT_GUI_ATHENA
3641 sb->pixval = 0;
3642 #endif
3643 sb->size = 1;
3644 sb->max = 1;
3645 sb->top = 0;
3646 sb->height = 0;
3647 #ifdef FEAT_VERTSPLIT
3648 sb->width = 0;
3649 #endif
3650 sb->status_height = 0;
3651 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3655 * Find the scrollbar with the given index.
3657 scrollbar_T *
3658 gui_find_scrollbar(ident)
3659 long ident;
3661 win_T *wp;
3663 if (gui.bottom_sbar.ident == ident)
3664 return &gui.bottom_sbar;
3665 FOR_ALL_WINDOWS(wp)
3667 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3668 return &wp->w_scrollbars[SBAR_LEFT];
3669 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3670 return &wp->w_scrollbars[SBAR_RIGHT];
3672 return NULL;
3676 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3678 * For Win32, Macintosh and GTK+ 2:
3679 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3680 * you stop dragging the scrollbar. We get here each time the scrollbar is
3681 * dragged another pixel, but as far as the rest of vim goes, it thinks
3682 * we're just hanging in the call to DispatchMessage() in
3683 * process_message(). The DispatchMessage() call that hangs was passed a
3684 * mouse button click event in the scrollbar window. -- webb.
3686 * Solution: Do the scrolling right here. But only when allowed.
3687 * Ignore the scrollbars while executing an external command or when there
3688 * are still characters to be processed.
3690 void
3691 gui_drag_scrollbar(sb, value, still_dragging)
3692 scrollbar_T *sb;
3693 long value;
3694 int still_dragging;
3696 #ifdef FEAT_WINDOWS
3697 win_T *wp;
3698 #endif
3699 int sb_num;
3700 #ifdef USE_ON_FLY_SCROLL
3701 colnr_T old_leftcol = curwin->w_leftcol;
3702 # ifdef FEAT_SCROLLBIND
3703 linenr_T old_topline = curwin->w_topline;
3704 # endif
3705 # ifdef FEAT_DIFF
3706 int old_topfill = curwin->w_topfill;
3707 # endif
3708 #else
3709 char_u bytes[sizeof(long_u)];
3710 int byte_count;
3711 #endif
3713 if (sb == NULL)
3714 return;
3716 /* Don't put events in the input queue now. */
3717 if (hold_gui_events)
3718 return;
3720 #ifdef FEAT_CMDWIN
3721 if (cmdwin_type != 0 && sb->wp != curwin)
3722 return;
3723 #endif
3725 if (still_dragging)
3727 if (sb->wp == NULL)
3728 gui.dragged_sb = SBAR_BOTTOM;
3729 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3730 gui.dragged_sb = SBAR_LEFT;
3731 else
3732 gui.dragged_sb = SBAR_RIGHT;
3733 gui.dragged_wp = sb->wp;
3735 else
3737 gui.dragged_sb = SBAR_NONE;
3738 #ifdef HAVE_GTK2
3739 /* Keep the "dragged_wp" value until after the scrolling, for when the
3740 * moust button is released. GTK2 doesn't send the button-up event. */
3741 gui.dragged_wp = NULL;
3742 #endif
3745 /* Vertical sbar info is kept in the first sbar (the left one) */
3746 if (sb->wp != NULL)
3747 sb = &sb->wp->w_scrollbars[0];
3750 * Check validity of value
3752 if (value < 0)
3753 value = 0;
3754 #ifdef SCROLL_PAST_END
3755 else if (value > sb->max)
3756 value = sb->max;
3757 #else
3758 if (value > sb->max - sb->size + 1)
3759 value = sb->max - sb->size + 1;
3760 #endif
3762 sb->value = value;
3764 #ifdef USE_ON_FLY_SCROLL
3765 /* When not allowed to do the scrolling right now, return.
3766 * This also checked input_available(), but that causes the first click in
3767 * a scrollbar to be ignored when Vim doesn't have focus. */
3768 if (dont_scroll)
3769 return;
3770 #endif
3771 #ifdef FEAT_INS_EXPAND
3772 /* Disallow scrolling the current window when the completion popup menu is
3773 * visible. */
3774 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3775 return;
3776 #endif
3778 #ifdef FEAT_RIGHTLEFT
3779 if (sb->wp == NULL && curwin->w_p_rl)
3781 value = sb->max + 1 - sb->size - value;
3782 if (value < 0)
3783 value = 0;
3785 #endif
3787 if (sb->wp != NULL) /* vertical scrollbar */
3789 sb_num = 0;
3790 #ifdef FEAT_WINDOWS
3791 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3792 sb_num++;
3793 if (wp == NULL)
3794 return;
3795 #else
3796 if (sb->wp != curwin)
3797 return;
3798 #endif
3800 #ifdef USE_ON_FLY_SCROLL
3801 current_scrollbar = sb_num;
3802 scrollbar_value = value;
3803 if (State & NORMAL)
3805 gui_do_scroll();
3806 setcursor();
3808 else if (State & INSERT)
3810 ins_scroll();
3811 setcursor();
3813 else if (State & CMDLINE)
3815 if (msg_scrolled == 0)
3817 gui_do_scroll();
3818 redrawcmdline();
3821 # ifdef FEAT_FOLDING
3822 /* Value may have been changed for closed fold. */
3823 sb->value = sb->wp->w_topline - 1;
3824 # endif
3826 /* When dragging one scrollbar and there is another one at the other
3827 * side move the thumb of that one too. */
3828 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3829 gui_mch_set_scrollbar_thumb(
3830 &sb->wp->w_scrollbars[
3831 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3832 ? SBAR_LEFT : SBAR_RIGHT],
3833 sb->value, sb->size, sb->max);
3835 #else
3836 bytes[0] = CSI;
3837 bytes[1] = KS_VER_SCROLLBAR;
3838 bytes[2] = KE_FILLER;
3839 bytes[3] = (char_u)sb_num;
3840 byte_count = 4;
3841 #endif
3843 else
3845 #ifdef USE_ON_FLY_SCROLL
3846 scrollbar_value = value;
3848 if (State & NORMAL)
3849 gui_do_horiz_scroll();
3850 else if (State & INSERT)
3851 ins_horscroll();
3852 else if (State & CMDLINE)
3854 if (msg_scrolled == 0)
3856 gui_do_horiz_scroll();
3857 redrawcmdline();
3860 if (old_leftcol != curwin->w_leftcol)
3862 updateWindow(curwin); /* update window, status and cmdline */
3863 setcursor();
3865 #else
3866 bytes[0] = CSI;
3867 bytes[1] = KS_HOR_SCROLLBAR;
3868 bytes[2] = KE_FILLER;
3869 byte_count = 3;
3870 #endif
3873 #ifdef USE_ON_FLY_SCROLL
3874 # ifdef FEAT_SCROLLBIND
3876 * synchronize other windows, as necessary according to 'scrollbind'
3878 if (curwin->w_p_scb
3879 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3880 || (sb->wp == curwin && (curwin->w_topline != old_topline
3881 # ifdef FEAT_DIFF
3882 || curwin->w_topfill != old_topfill
3883 # endif
3884 ))))
3886 do_check_scrollbind(TRUE);
3887 /* need to update the window right here */
3888 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3889 if (wp->w_redr_type > 0)
3890 updateWindow(wp);
3891 setcursor();
3893 # endif
3894 out_flush();
3895 gui_update_cursor(FALSE, TRUE);
3896 #else
3897 add_to_input_buf(bytes, byte_count);
3898 add_long_to_buf((long_u)value, bytes);
3899 add_to_input_buf_csi(bytes, sizeof(long_u));
3900 #endif
3904 * Scrollbar stuff:
3907 void
3908 gui_update_scrollbars(force)
3909 int force; /* Force all scrollbars to get updated */
3911 win_T *wp;
3912 scrollbar_T *sb;
3913 long val, size, max; /* need 32 bits here */
3914 int which_sb;
3915 int h, y;
3916 #ifdef FEAT_VERTSPLIT
3917 static win_T *prev_curwin = NULL;
3918 #endif
3920 /* Update the horizontal scrollbar */
3921 gui_update_horiz_scrollbar(force);
3923 #ifndef WIN3264
3924 /* Return straight away if there is neither a left nor right scrollbar.
3925 * On MS-Windows this is required anyway for scrollwheel messages. */
3926 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3927 return;
3928 #endif
3931 * Don't want to update a scrollbar while we're dragging it. But if we
3932 * have both a left and right scrollbar, and we drag one of them, we still
3933 * need to update the other one.
3935 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3936 && gui.which_scrollbars[SBAR_LEFT]
3937 && gui.which_scrollbars[SBAR_RIGHT])
3940 * If we have two scrollbars and one of them is being dragged, just
3941 * copy the scrollbar position from the dragged one to the other one.
3943 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3944 if (gui.dragged_wp != NULL)
3945 gui_mch_set_scrollbar_thumb(
3946 &gui.dragged_wp->w_scrollbars[which_sb],
3947 gui.dragged_wp->w_scrollbars[0].value,
3948 gui.dragged_wp->w_scrollbars[0].size,
3949 gui.dragged_wp->w_scrollbars[0].max);
3952 /* avoid that moving components around generates events */
3953 ++hold_gui_events;
3955 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3957 if (wp->w_buffer == NULL) /* just in case */
3958 continue;
3959 /* Skip a scrollbar that is being dragged. */
3960 if (!force && (gui.dragged_sb == SBAR_LEFT
3961 || gui.dragged_sb == SBAR_RIGHT)
3962 && gui.dragged_wp == wp)
3963 continue;
3965 #ifdef SCROLL_PAST_END
3966 max = wp->w_buffer->b_ml.ml_line_count - 1;
3967 #else
3968 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3969 #endif
3970 if (max < 0) /* empty buffer */
3971 max = 0;
3972 val = wp->w_topline - 1;
3973 size = wp->w_height;
3974 #ifdef SCROLL_PAST_END
3975 if (val > max) /* just in case */
3976 val = max;
3977 #else
3978 if (size > max + 1) /* just in case */
3979 size = max + 1;
3980 if (val > max - size + 1)
3981 val = max - size + 1;
3982 #endif
3983 if (val < 0) /* minimal value is 0 */
3984 val = 0;
3987 * Scrollbar at index 0 (the left one) contains all the information.
3988 * It would be the same info for left and right so we just store it for
3989 * one of them.
3991 sb = &wp->w_scrollbars[0];
3994 * Note: no check for valid w_botline. If it's not valid the
3995 * scrollbars will be updated later anyway.
3997 if (size < 1 || wp->w_botline - 2 > max)
4000 * This can happen during changing files. Just don't update the
4001 * scrollbar for now.
4003 sb->height = 0; /* Force update next time */
4004 if (gui.which_scrollbars[SBAR_LEFT])
4005 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4006 if (gui.which_scrollbars[SBAR_RIGHT])
4007 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4008 continue;
4010 if (force || sb->height != wp->w_height
4011 #ifdef FEAT_WINDOWS
4012 || sb->top != wp->w_winrow
4013 || sb->status_height != wp->w_status_height
4014 # ifdef FEAT_VERTSPLIT
4015 || sb->width != wp->w_width
4016 || prev_curwin != curwin
4017 # endif
4018 #endif
4021 /* Height, width or position of scrollbar has changed. For
4022 * vertical split: curwin changed. */
4023 sb->height = wp->w_height;
4024 #ifdef FEAT_WINDOWS
4025 sb->top = wp->w_winrow;
4026 sb->status_height = wp->w_status_height;
4027 # ifdef FEAT_VERTSPLIT
4028 sb->width = wp->w_width;
4029 # endif
4030 #endif
4032 /* Calculate height and position in pixels */
4033 h = (sb->height + sb->status_height) * gui.char_height;
4034 y = sb->top * gui.char_height + gui.border_offset;
4035 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4036 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MACVIM))
4037 if (gui.menu_is_active)
4038 y += gui.menu_height;
4039 #endif
4041 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4042 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4043 # ifdef FEAT_GUI_ATHENA
4044 y += gui.toolbar_height;
4045 # else
4046 # ifdef FEAT_GUI_MSWIN
4047 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4048 # endif
4049 # endif
4050 #endif
4052 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4053 if (gui_has_tabline())
4054 y += gui.tabline_height;
4055 #endif
4057 #ifdef FEAT_WINDOWS
4058 if (wp->w_winrow == 0)
4059 #endif
4061 /* Height of top scrollbar includes width of top border */
4062 h += gui.border_offset;
4063 y -= gui.border_offset;
4065 if (gui.which_scrollbars[SBAR_LEFT])
4067 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4068 gui.left_sbar_x, y,
4069 gui.scrollbar_width, h);
4070 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4072 if (gui.which_scrollbars[SBAR_RIGHT])
4074 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4075 gui.right_sbar_x, y,
4076 gui.scrollbar_width, h);
4077 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4081 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4082 * checking if the thumb moved at least a pixel. Only do this for
4083 * Athena, most other GUIs require the update anyway to make the
4084 * arrows work. */
4085 #ifdef FEAT_GUI_ATHENA
4086 if (max == 0)
4087 y = 0;
4088 else
4089 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4090 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4091 #else
4092 if (force || sb->value != val || sb->size != size || sb->max != max)
4093 #endif
4095 /* Thumb of scrollbar has moved */
4096 sb->value = val;
4097 #ifdef FEAT_GUI_ATHENA
4098 sb->pixval = y;
4099 #endif
4100 sb->size = size;
4101 sb->max = max;
4102 if (gui.which_scrollbars[SBAR_LEFT]
4103 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4104 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4105 val, size, max);
4106 if (gui.which_scrollbars[SBAR_RIGHT]
4107 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4108 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4109 val, size, max);
4112 #ifdef FEAT_VERTSPLIT
4113 prev_curwin = curwin;
4114 #endif
4115 --hold_gui_events;
4119 * Enable or disable a scrollbar.
4120 * Check for scrollbars for vertically split windows which are not enabled
4121 * sometimes.
4123 static void
4124 gui_do_scrollbar(wp, which, enable)
4125 win_T *wp;
4126 int which; /* SBAR_LEFT or SBAR_RIGHT */
4127 int enable; /* TRUE to enable scrollbar */
4129 #ifdef FEAT_VERTSPLIT
4130 int midcol = curwin->w_wincol + curwin->w_width / 2;
4131 int has_midcol = (wp->w_wincol <= midcol
4132 && wp->w_wincol + wp->w_width >= midcol);
4134 /* Only enable scrollbars that contain the middle column of the current
4135 * window. */
4136 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4138 /* Scrollbars only on one side. Don't enable scrollbars that don't
4139 * contain the middle column of the current window. */
4140 if (!has_midcol)
4141 enable = FALSE;
4143 else
4145 /* Scrollbars on both sides. Don't enable scrollbars that neither
4146 * contain the middle column of the current window nor are on the far
4147 * side. */
4148 if (midcol > Columns / 2)
4150 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4151 enable = FALSE;
4153 else
4155 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4156 : !has_midcol)
4157 enable = FALSE;
4160 #endif
4161 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4165 * Scroll a window according to the values set in the globals current_scrollbar
4166 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4167 * or FALSE otherwise.
4170 gui_do_scroll()
4172 win_T *wp, *save_wp;
4173 int i;
4174 long nlines;
4175 pos_T old_cursor;
4176 linenr_T old_topline;
4177 #ifdef FEAT_DIFF
4178 int old_topfill;
4179 #endif
4181 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4182 if (wp == NULL)
4183 break;
4184 if (wp == NULL)
4185 /* Couldn't find window */
4186 return FALSE;
4189 * Compute number of lines to scroll. If zero, nothing to do.
4191 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4192 if (nlines == 0)
4193 return FALSE;
4195 save_wp = curwin;
4196 old_topline = wp->w_topline;
4197 #ifdef FEAT_DIFF
4198 old_topfill = wp->w_topfill;
4199 #endif
4200 old_cursor = wp->w_cursor;
4201 curwin = wp;
4202 curbuf = wp->w_buffer;
4203 if (nlines < 0)
4204 scrolldown(-nlines, gui.dragged_wp == NULL);
4205 else
4206 scrollup(nlines, gui.dragged_wp == NULL);
4207 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4208 * the mouse-up event already, but we still want it to behave like when
4209 * dragging. But not the next click in an arrow. */
4210 if (gui.dragged_sb == SBAR_NONE)
4211 gui.dragged_wp = NULL;
4213 if (old_topline != wp->w_topline
4214 #ifdef FEAT_DIFF
4215 || old_topfill != wp->w_topfill
4216 #endif
4219 if (p_so != 0)
4221 cursor_correct(); /* fix window for 'so' */
4222 update_topline(); /* avoid up/down jump */
4224 if (old_cursor.lnum != wp->w_cursor.lnum)
4225 coladvance(wp->w_curswant);
4226 #ifdef FEAT_SCROLLBIND
4227 wp->w_scbind_pos = wp->w_topline;
4228 #endif
4231 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4232 validate_cursor();
4234 curwin = save_wp;
4235 curbuf = save_wp->w_buffer;
4238 * Don't call updateWindow() when nothing has changed (it will overwrite
4239 * the status line!).
4241 if (old_topline != wp->w_topline
4242 || wp->w_redr_type != 0
4243 #ifdef FEAT_DIFF
4244 || old_topfill != wp->w_topfill
4245 #endif
4248 int type = VALID;
4250 #ifdef FEAT_INS_EXPAND
4251 if (pum_visible())
4253 type = NOT_VALID;
4254 wp->w_lines_valid = 0;
4256 #endif
4257 /* Don't set must_redraw here, it may cause the popup menu to
4258 * disappear when losing focus after a scrollbar drag. */
4259 if (wp->w_redr_type < type)
4260 wp->w_redr_type = type;
4261 updateWindow(wp); /* update window, status line, and cmdline */
4264 #ifdef FEAT_INS_EXPAND
4265 /* May need to redraw the popup menu. */
4266 if (pum_visible())
4267 pum_redraw();
4268 #endif
4270 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4275 * Horizontal scrollbar stuff:
4279 * Return length of line "lnum" for horizontal scrolling.
4281 static colnr_T
4282 scroll_line_len(lnum)
4283 linenr_T lnum;
4285 char_u *p;
4286 colnr_T col;
4287 int w;
4289 p = ml_get(lnum);
4290 col = 0;
4291 if (*p != NUL)
4292 for (;;)
4294 w = chartabsize(p, col);
4295 mb_ptr_adv(p);
4296 if (*p == NUL) /* don't count the last character */
4297 break;
4298 col += w;
4300 return col;
4303 /* Remember which line is currently the longest, so that we don't have to
4304 * search for it when scrolling horizontally. */
4305 static linenr_T longest_lnum = 0;
4307 static void
4308 gui_update_horiz_scrollbar(force)
4309 int force;
4311 long value, size, max; /* need 32 bit ints here */
4313 if (!gui.which_scrollbars[SBAR_BOTTOM])
4314 return;
4316 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4317 return;
4319 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4320 return;
4323 * It is possible for the cursor to be invalid if we're in the middle of
4324 * something (like changing files). If so, don't do anything for now.
4326 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4328 gui.bottom_sbar.value = -1;
4329 return;
4332 size = W_WIDTH(curwin);
4333 if (curwin->w_p_wrap)
4335 value = 0;
4336 #ifdef SCROLL_PAST_END
4337 max = 0;
4338 #else
4339 max = W_WIDTH(curwin) - 1;
4340 #endif
4342 else
4344 value = curwin->w_leftcol;
4346 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4347 * line numbers, topline and botline can be invalid when displaying is
4348 * postponed. */
4349 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4350 && curwin->w_topline <= curwin->w_cursor.lnum
4351 && curwin->w_botline > curwin->w_cursor.lnum
4352 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4354 linenr_T lnum;
4355 colnr_T n;
4357 /* Use maximum of all visible lines. Remember the lnum of the
4358 * longest line, clostest to the cursor line. Used when scrolling
4359 * below. */
4360 max = 0;
4361 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4363 n = scroll_line_len(lnum);
4364 if (n > (colnr_T)max)
4366 max = n;
4367 longest_lnum = lnum;
4369 else if (n == (colnr_T)max
4370 && abs((int)(lnum - curwin->w_cursor.lnum))
4371 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4372 longest_lnum = lnum;
4375 else
4376 /* Use cursor line only. */
4377 max = scroll_line_len(curwin->w_cursor.lnum);
4378 #ifdef FEAT_VIRTUALEDIT
4379 if (virtual_active())
4381 /* May move the cursor even further to the right. */
4382 if (curwin->w_virtcol >= (colnr_T)max)
4383 max = curwin->w_virtcol;
4385 #endif
4387 #ifndef SCROLL_PAST_END
4388 max += W_WIDTH(curwin) - 1;
4389 #endif
4390 /* The line number isn't scrolled, thus there is less space when
4391 * 'number' is set (also for 'foldcolumn'). */
4392 size -= curwin_col_off();
4393 #ifndef SCROLL_PAST_END
4394 max -= curwin_col_off();
4395 #endif
4398 #ifndef SCROLL_PAST_END
4399 if (value > max - size + 1)
4400 value = max - size + 1; /* limit the value to allowable range */
4401 #endif
4403 #ifdef FEAT_RIGHTLEFT
4404 if (curwin->w_p_rl)
4406 value = max + 1 - size - value;
4407 if (value < 0)
4409 size += value;
4410 value = 0;
4413 #endif
4414 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4415 && max == gui.bottom_sbar.max)
4416 return;
4418 gui.bottom_sbar.value = value;
4419 gui.bottom_sbar.size = size;
4420 gui.bottom_sbar.max = max;
4421 gui.prev_wrap = curwin->w_p_wrap;
4423 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4427 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4430 gui_do_horiz_scroll()
4432 /* no wrapping, no scrolling */
4433 if (curwin->w_p_wrap)
4434 return FALSE;
4436 if (curwin->w_leftcol == scrollbar_value)
4437 return FALSE;
4439 curwin->w_leftcol = (colnr_T)scrollbar_value;
4441 /* When the line of the cursor is too short, move the cursor to the
4442 * longest visible line. Do a sanity check on "longest_lnum", just in
4443 * case. */
4444 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4445 && longest_lnum >= curwin->w_topline
4446 && longest_lnum < curwin->w_botline
4447 && !virtual_active())
4449 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4451 curwin->w_cursor.lnum = longest_lnum;
4452 curwin->w_cursor.col = 0;
4456 return leftcol_changed();
4460 * Check that none of the colors are the same as the background color
4462 void
4463 gui_check_colors()
4465 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4467 gui_set_bg_color((char_u *)"White");
4468 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4469 gui_set_fg_color((char_u *)"Black");
4473 static void
4474 gui_set_fg_color(name)
4475 char_u *name;
4477 gui.norm_pixel = gui_get_color(name);
4478 hl_set_fg_color_name(vim_strsave(name));
4481 static void
4482 gui_set_bg_color(name)
4483 char_u *name;
4485 gui.back_pixel = gui_get_color(name);
4486 hl_set_bg_color_name(vim_strsave(name));
4490 * Allocate a color by name.
4491 * Returns INVALCOLOR and gives an error message when failed.
4493 guicolor_T
4494 gui_get_color(name)
4495 char_u *name;
4497 guicolor_T t;
4499 if (*name == NUL)
4500 return INVALCOLOR;
4501 t = gui_mch_get_color(name);
4503 if (t == INVALCOLOR
4504 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4505 && gui.in_use
4506 #endif
4508 EMSG2(_("E254: Cannot allocate color %s"), name);
4509 return t;
4513 * Return the grey value of a color (range 0-255).
4516 gui_get_lightness(pixel)
4517 guicolor_T pixel;
4519 long_u rgb = gui_mch_get_rgb(pixel);
4521 return (int)( (((rgb >> 16) & 0xff) * 299)
4522 + (((rgb >> 8) & 0xff) * 587)
4523 + ((rgb & 0xff) * 114)) / 1000;
4526 #if defined(FEAT_GUI_X11) || defined(PROTO)
4527 void
4528 gui_new_scrollbar_colors()
4530 win_T *wp;
4532 /* Nothing to do if GUI hasn't started yet. */
4533 if (!gui.in_use)
4534 return;
4536 FOR_ALL_WINDOWS(wp)
4538 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4539 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4541 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4543 #endif
4546 * Call this when focus has changed.
4548 void
4549 gui_focus_change(in_focus)
4550 int in_focus;
4553 * Skip this code to avoid drawing the cursor when debugging and switching
4554 * between the debugger window and gvim.
4556 #if 1
4557 gui.in_focus = in_focus;
4558 out_flush(); /* make sure output has been written */
4559 gui_update_cursor(TRUE, FALSE);
4561 # ifdef FEAT_XIM
4562 xim_set_focus(in_focus);
4563 # endif
4565 /* Put events in the input queue only when allowed.
4566 * ui_focus_change() isn't called directly, because it invokes
4567 * autocommands and that must not happen asynchronously. */
4568 if (!hold_gui_events)
4570 char_u bytes[3];
4572 bytes[0] = CSI;
4573 bytes[1] = KS_EXTRA;
4574 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4575 add_to_input_buf(bytes, 3);
4577 #endif
4581 * Called when the mouse moved (but not when dragging).
4583 void
4584 gui_mouse_moved(x, y)
4585 int x;
4586 int y;
4588 win_T *wp;
4589 char_u st[8];
4591 /* Ignore this while still starting up. */
4592 if (!gui.in_use || gui.starting)
4593 return;
4595 #ifdef FEAT_MOUSESHAPE
4596 /* Get window pointer, and update mouse shape as well. */
4597 wp = xy2win(x, y);
4598 #endif
4600 /* Only handle this when 'mousefocus' set and ... */
4601 if (p_mousef
4602 && !hold_gui_events /* not holding events */
4603 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4604 && State != HITRETURN /* but not hit-return prompt */
4605 && msg_scrolled == 0 /* no scrolled message */
4606 && !need_mouse_correct /* not moving the pointer */
4607 && gui.in_focus) /* gvim in focus */
4609 /* Don't move the mouse when it's left or right of the Vim window */
4610 if (x < 0 || x > Columns * gui.char_width)
4611 return;
4612 #ifndef FEAT_MOUSESHAPE
4613 wp = xy2win(x, y);
4614 #endif
4615 if (wp == curwin || wp == NULL)
4616 return; /* still in the same old window, or none at all */
4618 #ifdef FEAT_WINDOWS
4619 /* Ignore position in the tab pages line. */
4620 if (Y_2_ROW(y) < tabline_height())
4621 return;
4622 #endif
4625 * format a mouse click on status line input
4626 * ala gui_send_mouse_event(0, x, y, 0, 0);
4627 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4628 * generate a K_LEFTMOUSE_NM key code.
4630 if (finish_op)
4632 /* abort the current operator first */
4633 st[0] = ESC;
4634 add_to_input_buf(st, 1);
4636 st[0] = CSI;
4637 st[1] = KS_MOUSE;
4638 st[2] = KE_FILLER;
4639 st[3] = (char_u)MOUSE_LEFT;
4640 fill_mouse_coord(st + 4,
4641 #ifdef FEAT_VERTSPLIT
4642 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4643 #else
4645 #endif
4646 wp->w_height + W_WINROW(wp));
4648 add_to_input_buf(st, 8);
4649 st[3] = (char_u)MOUSE_RELEASE;
4650 add_to_input_buf(st, 8);
4651 #ifdef FEAT_GUI_GTK
4652 /* Need to wake up the main loop */
4653 if (gtk_main_level() > 0)
4654 gtk_main_quit();
4655 #endif
4660 * Called when mouse should be moved to window with focus.
4662 void
4663 gui_mouse_correct()
4665 int x, y;
4666 win_T *wp = NULL;
4668 need_mouse_correct = FALSE;
4670 if (!(gui.in_use && p_mousef))
4671 return;
4673 gui_mch_getmouse(&x, &y);
4674 /* Don't move the mouse when it's left or right of the Vim window */
4675 if (x < 0 || x > Columns * gui.char_width)
4676 return;
4677 if (y >= 0
4678 # ifdef FEAT_WINDOWS
4679 && Y_2_ROW(y) >= tabline_height()
4680 # endif
4682 wp = xy2win(x, y);
4683 if (wp != curwin && wp != NULL) /* If in other than current window */
4685 validate_cline_row();
4686 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4687 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4688 + (gui.char_height) / 2);
4693 * Find window where the mouse pointer "y" coordinate is in.
4695 /*ARGSUSED*/
4696 static win_T *
4697 xy2win(x, y)
4698 int x;
4699 int y;
4701 #ifdef FEAT_WINDOWS
4702 int row;
4703 int col;
4704 win_T *wp;
4706 row = Y_2_ROW(y);
4707 col = X_2_COL(x);
4708 if (row < 0 || col < 0) /* before first window */
4709 return NULL;
4710 wp = mouse_find_win(&row, &col);
4711 # ifdef FEAT_MOUSESHAPE
4712 if (State == HITRETURN || State == ASKMORE)
4714 if (Y_2_ROW(y) >= msg_row)
4715 update_mouseshape(SHAPE_IDX_MOREL);
4716 else
4717 update_mouseshape(SHAPE_IDX_MORE);
4719 else if (row > wp->w_height) /* below status line */
4720 update_mouseshape(SHAPE_IDX_CLINE);
4721 # ifdef FEAT_VERTSPLIT
4722 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4723 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4724 update_mouseshape(SHAPE_IDX_VSEP);
4725 # endif
4726 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4727 && row == wp->w_height && msg_scrolled == 0)
4728 update_mouseshape(SHAPE_IDX_STATUS);
4729 else
4730 update_mouseshape(-2);
4731 # endif
4732 return wp;
4733 #else
4734 return firstwin;
4735 #endif
4739 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4740 * File names may be given to redefine the args list.
4742 void
4743 ex_gui(eap)
4744 exarg_T *eap;
4746 char_u *arg = eap->arg;
4749 * Check for "-f" argument: foreground, don't fork.
4750 * Also don't fork when started with "gvim -f".
4751 * Do fork when using "gui -b".
4752 * Note that Mac OS X will never fork on :gui since it can only fork on
4753 * startup right after scanning the command line.
4755 if (arg[0] == '-'
4756 && (arg[1] == 'f' || arg[1] == 'b')
4757 && (arg[2] == NUL || vim_iswhite(arg[2])))
4759 gui.dofork = (arg[1] == 'b');
4760 eap->arg = skipwhite(eap->arg + 2);
4762 if (!gui.in_use)
4764 /* Clear the command. Needed for when forking+exiting, to avoid part
4765 * of the argument ending up after the shell prompt. */
4766 msg_clr_eos_force();
4767 gui_start();
4769 if (!ends_excmd(*eap->arg))
4770 ex_next(eap);
4773 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4774 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR) \
4775 || defined(FEAT_GUI_MACVIM)) || defined(PROTO)
4777 * This is shared between Athena, Motif and GTK.
4779 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4782 * Callback function for do_in_runtimepath().
4784 static void
4785 gfp_setname(fname, cookie)
4786 char_u *fname;
4787 void *cookie;
4789 char_u *gfp_buffer = cookie;
4791 if (STRLEN(fname) >= MAXPATHL)
4792 *gfp_buffer = NUL;
4793 else
4794 STRCPY(gfp_buffer, fname);
4798 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4799 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4802 gui_find_bitmap(name, buffer, ext)
4803 char_u *name;
4804 char_u *buffer;
4805 char *ext;
4807 if (STRLEN(name) > MAXPATHL - 14)
4808 return FAIL;
4809 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4810 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4811 || *buffer == NUL)
4812 return FAIL;
4813 return OK;
4816 # if !defined(HAVE_GTK2) || defined(PROTO)
4818 * Given the name of the "icon=" argument, try finding the bitmap file for the
4819 * icon. If it is an absolute path name, use it as it is. Otherwise append
4820 * "ext" and search for it in 'runtimepath'.
4821 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4822 * contains "name".
4824 void
4825 gui_find_iconfile(name, buffer, ext)
4826 char_u *name;
4827 char_u *buffer;
4828 char *ext;
4830 char_u buf[MAXPATHL + 1];
4832 expand_env(name, buffer, MAXPATHL);
4833 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4834 STRCPY(buffer, buf);
4836 # endif
4837 #endif
4839 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4840 void
4841 display_errors()
4843 char_u *p;
4845 if (isatty(2))
4846 fflush(stderr);
4847 else if (error_ga.ga_data != NULL)
4849 /* avoid putting up a message box with blanks only */
4850 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4851 if (!isspace(*p))
4853 /* Truncate a very long message, it will go off-screen. */
4854 if (STRLEN(p) > 2000)
4855 STRCPY(p + 2000 - 14, "...(truncated)");
4856 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4857 p, (char_u *)_("&Ok"), 1, NULL);
4858 break;
4860 ga_clear(&error_ga);
4863 #endif
4865 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4867 * Return TRUE if still starting up and there is no place to enter text.
4868 * For GTK and X11 we check if stderr is not a tty, which means we were
4869 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4870 * allow typing on stdin.
4873 no_console_input()
4875 return ((!gui.in_use || gui.starting)
4876 # ifndef NO_CONSOLE
4877 && !isatty(0) && !isatty(2)
4878 # endif
4881 #endif
4883 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4884 || defined(NEED_GUI_UPDATE_SCREEN) \
4885 || defined(PROTO)
4887 * Update the current window and the screen.
4889 void
4890 gui_update_screen()
4892 update_topline();
4893 validate_cursor();
4894 #ifdef FEAT_AUTOCMD
4895 /* Trigger CursorMoved if the cursor moved. */
4896 if (!finish_op && has_cursormoved()
4897 && !equalpos(last_cursormoved, curwin->w_cursor))
4899 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4900 last_cursormoved = curwin->w_cursor;
4902 #endif
4903 update_screen(0); /* may need to update the screen */
4904 setcursor();
4905 out_flush(); /* make sure output has been written */
4906 gui_update_cursor(TRUE, FALSE);
4907 gui_mch_flush();
4909 #endif
4911 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4912 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4915 * Get the text to use in a find/replace dialog. Uses the last search pattern
4916 * if the argument is empty.
4917 * Returns an allocated string.
4919 char_u *
4920 get_find_dialog_text(arg, wwordp, mcasep)
4921 char_u *arg;
4922 int *wwordp; /* return: TRUE if \< \> found */
4923 int *mcasep; /* return: TRUE if \C found */
4925 char_u *text;
4927 if (*arg == NUL)
4928 text = last_search_pat();
4929 else
4930 text = arg;
4931 if (text != NULL)
4933 text = vim_strsave(text);
4934 if (text != NULL)
4936 int len = (int)STRLEN(text);
4937 int i;
4939 /* Remove "\V" */
4940 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4942 mch_memmove(text, text + 2, (size_t)(len - 1));
4943 len -= 2;
4946 /* Recognize "\c" and "\C" and remove. */
4947 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4949 *mcasep = (text[1] == 'C');
4950 mch_memmove(text, text + 2, (size_t)(len - 1));
4951 len -= 2;
4954 /* Recognize "\<text\>" and remove. */
4955 if (len >= 4
4956 && STRNCMP(text, "\\<", 2) == 0
4957 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4959 *wwordp = TRUE;
4960 mch_memmove(text, text + 2, (size_t)(len - 4));
4961 text[len - 4] = NUL;
4964 /* Recognize "\/" or "\?" and remove. */
4965 for (i = 0; i + 1 < len; ++i)
4966 if (text[i] == '\\' && (text[i + 1] == '/'
4967 || text[i + 1] == '?'))
4969 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4970 --len;
4974 return text;
4978 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4980 static void
4981 concat_esc(gap, text, what)
4982 garray_T *gap;
4983 char_u *text;
4984 int what;
4986 while (*text != NUL)
4988 #ifdef FEAT_MBYTE
4989 int l = (*mb_ptr2len)(text);
4991 if (l > 1)
4993 while (--l >= 0)
4994 ga_append(gap, *text++);
4995 continue;
4997 #endif
4998 if (*text == what)
4999 ga_append(gap, '\\');
5000 ga_append(gap, *text);
5001 ++text;
5006 * Handle the press of a button in the find-replace dialog.
5007 * Return TRUE when something was added to the input buffer.
5010 gui_do_findrepl(flags, find_text, repl_text, down)
5011 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5012 char_u *find_text;
5013 char_u *repl_text;
5014 int down; /* Search downwards. */
5016 garray_T ga;
5017 int i;
5018 int type = (flags & FRD_TYPE_MASK);
5019 char_u *p;
5020 regmatch_T regmatch;
5021 int save_did_emsg = did_emsg;
5023 ga_init2(&ga, 1, 100);
5024 if (type == FRD_REPLACEALL)
5025 ga_concat(&ga, (char_u *)"%s/");
5027 ga_concat(&ga, (char_u *)"\\V");
5028 if (flags & FRD_MATCH_CASE)
5029 ga_concat(&ga, (char_u *)"\\C");
5030 else
5031 ga_concat(&ga, (char_u *)"\\c");
5032 if (flags & FRD_WHOLE_WORD)
5033 ga_concat(&ga, (char_u *)"\\<");
5034 if (type == FRD_REPLACEALL || down)
5035 concat_esc(&ga, find_text, '/'); /* escape slashes */
5036 else
5037 concat_esc(&ga, find_text, '?'); /* escape '?' */
5038 if (flags & FRD_WHOLE_WORD)
5039 ga_concat(&ga, (char_u *)"\\>");
5041 if (type == FRD_REPLACEALL)
5043 ga_concat(&ga, (char_u *)"/");
5044 /* escape / and \ */
5045 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5046 if (p != NULL)
5047 ga_concat(&ga, p);
5048 vim_free(p);
5049 ga_concat(&ga, (char_u *)"/g");
5051 ga_append(&ga, NUL);
5053 if (type == FRD_REPLACE)
5055 /* Do the replacement when the text at the cursor matches. Thus no
5056 * replacement is done if the cursor was moved! */
5057 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5058 regmatch.rm_ic = 0;
5059 if (regmatch.regprog != NULL)
5061 p = ml_get_cursor();
5062 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5063 && regmatch.startp[0] == p)
5065 /* Clear the command line to remove any old "No match"
5066 * error. */
5067 msg_end_prompt();
5069 if (u_save_cursor() == OK)
5071 /* A button was pressed thus undo should be synced. */
5072 u_sync(FALSE);
5074 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5075 FALSE, FALSE);
5076 ins_str(repl_text);
5079 else
5080 MSG(_("No match at cursor, finding next"));
5081 vim_free(regmatch.regprog);
5085 if (type == FRD_REPLACEALL)
5087 /* A button was pressed, thus undo should be synced. */
5088 u_sync(FALSE);
5089 do_cmdline_cmd(ga.ga_data);
5091 else
5093 /* Search for the next match. */
5094 i = msg_scroll;
5095 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5096 SEARCH_MSG + SEARCH_MARK, NULL);
5097 msg_scroll = i; /* don't let an error message set msg_scroll */
5100 /* Don't want to pass did_emsg to other code, it may cause disabling
5101 * syntax HL if we were busy redrawing. */
5102 did_emsg = save_did_emsg;
5104 if (State & (NORMAL | INSERT))
5106 gui_update_screen(); /* update the screen */
5107 msg_didout = 0; /* overwrite any message */
5108 need_wait_return = FALSE; /* don't wait for return */
5111 vim_free(ga.ga_data);
5112 return (ga.ga_len > 0);
5115 #endif
5117 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5118 || defined(FEAT_GUI_MSWIN) \
5119 || defined(FEAT_GUI_MAC) \
5120 || defined(PROTO) \
5121 || defined(FEAT_GUI_MACVIM)
5123 #ifdef FEAT_WINDOWS
5124 static void gui_wingoto_xy __ARGS((int x, int y));
5127 * Jump to the window at specified point (x, y).
5129 static void
5130 gui_wingoto_xy(x, y)
5131 int x;
5132 int y;
5134 int row = Y_2_ROW(y);
5135 int col = X_2_COL(x);
5136 win_T *wp;
5138 if (row >= 0 && col >= 0)
5140 wp = mouse_find_win(&row, &col);
5141 if (wp != NULL && wp != curwin)
5142 win_goto(wp);
5145 #endif
5148 * Process file drop. Mouse cursor position, key modifiers, name of files
5149 * and count of files are given. Argument "fnames[count]" has full pathnames
5150 * of dropped files, they will be freed in this function, and caller can't use
5151 * fnames after call this function.
5153 /*ARGSUSED*/
5154 void
5155 gui_handle_drop(x, y, modifiers, fnames, count)
5156 int x;
5157 int y;
5158 int_u modifiers;
5159 char_u **fnames;
5160 int count;
5162 int i;
5163 char_u *p;
5164 static int entered = FALSE;
5167 * This function is called by event handlers. Just in case we get a
5168 * second event before the first one is handled, ignore the second one.
5169 * Not sure if this can ever happen, just in case.
5171 if (entered)
5172 return;
5173 entered = TRUE;
5176 * When the cursor is at the command line, add the file names to the
5177 * command line, don't edit the files.
5179 if (State & CMDLINE)
5181 shorten_filenames(fnames, count);
5182 for (i = 0; i < count; ++i)
5184 if (fnames[i] != NULL)
5186 if (i > 0)
5187 add_to_input_buf((char_u*)" ", 1);
5189 /* We don't know what command is used thus we can't be sure
5190 * about which characters need to be escaped. Only escape the
5191 * most common ones. */
5192 # ifdef BACKSLASH_IN_FILENAME
5193 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5194 # else
5195 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5196 # endif
5197 if (p != NULL)
5198 add_to_input_buf_csi(p, (int)STRLEN(p));
5199 vim_free(p);
5200 vim_free(fnames[i]);
5203 vim_free(fnames);
5205 else
5207 /* Go to the window under mouse cursor, then shorten given "fnames" by
5208 * current window, because a window can have local current dir. */
5209 # ifdef FEAT_WINDOWS
5210 gui_wingoto_xy(x, y);
5211 # endif
5212 shorten_filenames(fnames, count);
5214 /* If Shift held down, remember the first item. */
5215 if ((modifiers & MOUSE_SHIFT) != 0)
5216 p = vim_strsave(fnames[0]);
5217 else
5218 p = NULL;
5220 /* Handle the drop, :edit or :split to get to the file. This also
5221 * frees fnames[]. Skip this if there is only one item it's a
5222 * directory and Shift is held down. */
5223 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5224 && mch_isdir(fnames[0]))
5226 vim_free(fnames[0]);
5227 vim_free(fnames);
5229 else
5230 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5232 /* If Shift held down, change to first file's directory. If the first
5233 * item is a directory, change to that directory (and let the explorer
5234 * plugin show the contents). */
5235 if (p != NULL)
5237 if (mch_isdir(p))
5239 if (mch_chdir((char *)p) == 0)
5240 shorten_fnames(TRUE);
5242 else if (vim_chdirfile(p) == OK)
5243 shorten_fnames(TRUE);
5244 vim_free(p);
5247 /* Update the screen display */
5248 update_screen(NOT_VALID);
5249 # ifdef FEAT_MENU
5250 gui_update_menus(0);
5251 # endif
5252 setcursor();
5253 out_flush();
5254 gui_update_cursor(FALSE, FALSE);
5255 gui_mch_flush();
5258 entered = FALSE;
5260 #endif