Update credits
[MacVim.git] / src / gui.c
blob323f12c3f77ef52b87478642e5621c3862cabb5c
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
11 #include "vim.h"
13 /* Structure containing all the GUI information */
14 gui_T gui;
16 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
17 static void set_guifontwide __ARGS((char_u *font_name));
18 #endif
19 static void gui_check_pos __ARGS((void));
20 static void gui_position_components __ARGS((int));
21 static void gui_outstr __ARGS((char_u *, int));
22 static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
23 #ifdef HAVE_GTK2
24 static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25 #endif
26 static void gui_delete_lines __ARGS((int row, int count));
27 static void gui_insert_lines __ARGS((int row, int count));
28 static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
29 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30 static int gui_has_tabline __ARGS((void));
31 #endif
32 static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33 static colnr_T scroll_line_len __ARGS((linenr_T lnum));
34 static void gui_update_horiz_scrollbar __ARGS((int));
35 static void gui_set_fg_color __ARGS((char_u *name));
36 static void gui_set_bg_color __ARGS((char_u *name));
37 static win_T *xy2win __ARGS((int x, int y));
39 static int can_update_cursor = TRUE; /* can display the cursor */
42 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
43 * this makes the thumb indicate the part of the text that is shown. Motif
44 * can't do this.
46 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
47 # define SCROLL_PAST_END
48 #endif
51 * gui_start -- Called when user wants to start the GUI.
53 * Careful: This function can be called recursively when there is a ":gui"
54 * command in the .gvimrc file. Only the first call should fork, not the
55 * recursive call.
57 void
58 gui_start()
60 char_u *old_term;
61 #if defined(UNIX) && !defined(__BEOS__)
62 # define MAY_FORK
63 int dofork = TRUE;
64 #endif
65 static int recursive = 0;
67 old_term = vim_strsave(T_NAME);
70 * Set_termname() will call gui_init() to start the GUI.
71 * Set the "starting" flag, to indicate that the GUI will start.
73 * We don't want to open the GUI shell until after we've read .gvimrc,
74 * otherwise we don't know what font we will use, and hence we don't know
75 * what size the shell should be. So if there are errors in the .gvimrc
76 * file, they will have to go to the terminal: Set full_screen to FALSE.
77 * full_screen will be set to TRUE again by a successful termcapinit().
79 settmode(TMODE_COOK); /* stop RAW mode */
80 if (full_screen)
81 cursor_on(); /* needed for ":gui" in .vimrc */
82 gui.starting = TRUE;
83 full_screen = FALSE;
85 #ifdef MAY_FORK
86 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
87 dofork = FALSE;
88 #endif
89 ++recursive;
91 termcapinit((char_u *)"builtin_gui");
92 gui.starting = recursive - 1;
94 if (!gui.in_use) /* failed to start GUI */
96 termcapinit(old_term); /* back to old term settings */
97 settmode(TMODE_RAW); /* restart RAW mode */
98 #ifdef FEAT_TITLE
99 set_title_defaults(); /* set 'title' and 'icon' again */
100 #endif
103 vim_free(old_term);
105 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
106 if (gui.in_use)
107 /* Display error messages in a dialog now. */
108 display_errors();
109 #endif
111 #if defined(MAY_FORK) && !defined(__QNXNTO__)
113 * Quit the current process and continue in the child.
114 * Makes "gvim file" disconnect from the shell it was started in.
115 * Don't do this when Vim was started with "-f" or the 'f' flag is present
116 * in 'guioptions'.
118 if (gui.in_use && dofork)
120 pid_t pid = -1;
122 # if defined MACOS_X
123 int i;
125 /* on os x, you have to exec after a fork, otherwise calls to
126 * frameworks will assert (and without corefoundation, you can't start
127 * the gui. what fun.). See CAVEATS at
128 http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/fork.2.html
130 * Since we have to go through this anyways, we might as well use vfork.
131 * But: then we can't detach from our starting shell, so stick with
132 * fork.
134 * Kinda sucks to restart vim when doing :gui, so don't fork in that
135 * case (make sure gui.dofork is only set when interpreting argv, not
136 * when doing :gui. Currently, gui.dofork is set to false in ex_gui().
138 * Also doesn't work well if vim starts cscope (or some other
139 * subprocess I guess), because it's not transferred to the newly
140 * exec'd process, leaving an orphaned process (not a zombie process)
141 * behind. The Right Thing is to kill all our child processes before
142 * calling exec.
145 /* stolen from http://paste.lisp.org/display/50906 */
146 extern int *_NSGetArgc(void);
147 extern char ***_NSGetArgv(void);
149 int argc = *_NSGetArgc();
150 char ** argv = *_NSGetArgv();
151 char * newargv[argc+2];
153 newargv[0] = argv[0];
156 * make sure "-f" is in front of potential "--remote" flags, else
157 * they would consume it.
159 newargv[1] = "-f";
161 for (i = 1; i < argc; i++) {
162 newargv[i + 1] = argv[i];
164 newargv[argc+1] = NULL;
166 /* shut down all the stuff we just started, just to start
167 * it again from the exec :-\ */
168 prepare_getout();
170 pid = fork();
171 switch(pid) {
172 case -1:
173 # ifndef NDEBUG
174 fprintf(stderr, "vim: Mac OS X workaround fork() failed!");
175 # endif
176 _exit(255);
177 case 0:
178 /* Child. */
180 /* make sure we survive our shell */
181 setsid();
183 /* Restarts the vim process, will not return. */
184 execvp(argv[0], newargv);
186 /* if we come here, exec has failed. bail. */
187 _exit(255);
188 default:
189 /* Parent */
190 _exit(0);
192 # else
193 int pipefd[2]; /* pipe between parent and child */
194 int pipe_error;
195 char dummy;
197 /* Setup a pipe between the child and the parent, so that the parent
198 * knows when the child has done the setsid() call and is allowed to
199 * exit. */
200 pipe_error = (pipe(pipefd) < 0);
201 pid = fork();
202 if (pid > 0) /* Parent */
204 /* Give the child some time to do the setsid(), otherwise the
205 * exit() may kill the child too (when starting gvim from inside a
206 * gvim). */
207 if (pipe_error)
208 ui_delay(300L, TRUE);
209 else
211 /* The read returns when the child closes the pipe (or when
212 * the child dies for some reason). */
213 close(pipefd[1]);
214 (void)read(pipefd[0], &dummy, (size_t)1);
215 close(pipefd[0]);
218 /* When swapping screens we may need to go to the next line, e.g.,
219 * after a hit-enter prompt and using ":gui". */
220 if (newline_on_exit)
221 mch_errmsg("\r\n");
224 * The parent must skip the normal exit() processing, the child
225 * will do it. For example, GTK messes up signals when exiting.
227 _exit(0);
230 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
232 * Change our process group. On some systems/shells a CTRL-C in the
233 * shell where Vim was started would otherwise kill gvim!
235 if (pid == 0) /* child */
236 # if defined(HAVE_SETSID)
237 (void)setsid();
238 # else
239 (void)setpgid(0, 0);
240 # endif
241 # endif
242 if (!pipe_error)
244 close(pipefd[0]);
245 close(pipefd[1]);
249 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
250 /* Tell the session manager our new PID */
251 gui_mch_forked();
252 # endif
254 # endif
256 #else
257 # if defined(__QNXNTO__)
258 if (gui.in_use && dofork)
259 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
260 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
261 # endif
262 #endif
264 #ifdef FEAT_AUTOCMD
265 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
266 * the GUIFailed event. */
267 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
268 NULL, NULL, FALSE, curbuf);
269 #endif
271 --recursive;
275 * Call this when vim starts up, whether or not the GUI is started
277 void
278 gui_prepare(argc, argv)
279 int *argc;
280 char **argv;
282 gui.in_use = FALSE; /* No GUI yet (maybe later) */
283 gui.starting = FALSE; /* No GUI yet (maybe later) */
284 gui_mch_prepare(argc, argv);
288 * Try initializing the GUI and check if it can be started.
289 * Used from main() to check early if "vim -g" can start the GUI.
290 * Used from gui_init() to prepare for starting the GUI.
291 * Returns FAIL or OK.
294 gui_init_check()
296 static int result = MAYBE;
298 if (result != MAYBE)
300 if (result == FAIL)
301 EMSG(_("E229: Cannot start the GUI"));
302 return result;
305 gui.shell_created = FALSE;
306 gui.dying = FALSE;
307 gui.in_focus = TRUE; /* so the guicursor setting works */
308 gui.dragged_sb = SBAR_NONE;
309 gui.dragged_wp = NULL;
310 gui.pointer_hidden = FALSE;
311 gui.col = 0;
312 gui.row = 0;
313 gui.num_cols = Columns;
314 gui.num_rows = Rows;
316 gui.cursor_is_valid = FALSE;
317 gui.scroll_region_top = 0;
318 gui.scroll_region_bot = Rows - 1;
319 gui.scroll_region_left = 0;
320 gui.scroll_region_right = Columns - 1;
321 gui.highlight_mask = HL_NORMAL;
322 gui.char_width = 1;
323 gui.char_height = 1;
324 gui.char_ascent = 0;
325 gui.border_width = 0;
327 gui.norm_font = NOFONT;
328 #ifndef HAVE_GTK2
329 gui.bold_font = NOFONT;
330 gui.ital_font = NOFONT;
331 gui.boldital_font = NOFONT;
332 # ifdef FEAT_XFONTSET
333 gui.fontset = NOFONTSET;
334 # endif
335 #endif
337 #ifdef FEAT_MENU
338 # ifndef HAVE_GTK2
339 # ifdef FONTSET_ALWAYS
340 gui.menu_fontset = NOFONTSET;
341 # else
342 gui.menu_font = NOFONT;
343 # endif
344 # endif
345 gui.menu_is_active = TRUE; /* default: include menu */
346 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
347 gui.menu_height = MENU_DEFAULT_HEIGHT;
348 gui.menu_width = 0;
349 # endif
350 #endif
351 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
352 gui.toolbar_height = 0;
353 #endif
354 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
355 gui.footer_height = 0;
356 #endif
357 #ifdef FEAT_BEVAL_TIP
358 gui.tooltip_fontset = NOFONTSET;
359 #endif
361 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
362 gui.prev_wrap = -1;
364 #ifdef ALWAYS_USE_GUI
365 result = OK;
366 #else
367 result = gui_mch_init_check();
368 #endif
369 return result;
373 * This is the call which starts the GUI.
375 void
376 gui_init()
378 win_T *wp;
379 static int recursive = 0;
382 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
383 * function will then be executed at the first call, the rest by the
384 * recursive call. This allow the shell to be opened halfway reading a
385 * gvimrc file.
387 if (!recursive)
389 ++recursive;
391 clip_init(TRUE);
393 /* If can't initialize, don't try doing the rest */
394 if (gui_init_check() == FAIL)
396 --recursive;
397 clip_init(FALSE);
398 return;
402 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
403 * breaks the Paste toolbar button.
405 set_option_value((char_u *)"paste", 0L, NULL, 0);
408 * Set up system-wide default menus.
410 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
411 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
413 sys_menu = TRUE;
414 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
415 sys_menu = FALSE;
417 #endif
420 * Switch on the mouse by default, unless the user changed it already.
421 * This can then be changed in the .gvimrc.
423 if (!option_was_set((char_u *)"mouse"))
424 set_string_option_direct((char_u *)"mouse", -1,
425 (char_u *)"a", OPT_FREE, SID_NONE);
428 * If -U option given, use only the initializations from that file and
429 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
431 if (use_gvimrc != NULL)
433 if (STRCMP(use_gvimrc, "NONE") != 0
434 && STRCMP(use_gvimrc, "NORC") != 0
435 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
436 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
438 else
441 * Get system wide defaults for gvim, only when file name defined.
443 #ifdef SYS_GVIMRC_FILE
444 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
445 #endif
448 * Try to read GUI initialization commands from the following
449 * places:
450 * - environment variable GVIMINIT
451 * - the user gvimrc file (~/.gvimrc)
452 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
453 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
454 * The first that exists is used, the rest is ignored.
456 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
457 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
458 DOSO_GVIMRC) == FAIL
459 #ifdef USR_GVIMRC_FILE2
460 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
461 DOSO_GVIMRC) == FAIL
462 #endif
465 #ifdef USR_GVIMRC_FILE3
466 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
467 #endif
471 * Read initialization commands from ".gvimrc" in current
472 * directory. This is only done if the 'exrc' option is set.
473 * Because of security reasons we disallow shell and write
474 * commands now, except for unix if the file is owned by the user
475 * or 'secure' option has been reset in environment of global
476 * ".gvimrc".
477 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
478 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
480 if (p_exrc)
482 #ifdef UNIX
484 struct stat s;
486 /* if ".gvimrc" file is not owned by user, set 'secure'
487 * mode */
488 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
489 secure = p_secure;
491 #else
492 secure = p_secure;
493 #endif
495 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
496 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
497 #ifdef SYS_GVIMRC_FILE
498 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
499 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
500 #endif
501 #ifdef USR_GVIMRC_FILE2
502 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
503 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
504 #endif
505 #ifdef USR_GVIMRC_FILE3
506 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
507 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
508 #endif
510 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
512 if (secure == 2)
513 need_wait_return = TRUE;
514 secure = 0;
518 if (need_wait_return || msg_didany)
519 wait_return(TRUE);
521 --recursive;
524 /* If recursive call opened the shell, return here from the first call */
525 if (gui.in_use)
526 return;
529 * Create the GUI shell.
531 gui.in_use = TRUE; /* Must be set after menus have been set up */
532 if (gui_mch_init() == FAIL)
533 goto error;
535 /* Avoid a delay for an error message that was printed in the terminal
536 * where Vim was started. */
537 emsg_on_display = FALSE;
538 msg_scrolled = 0;
539 clear_sb_text();
540 need_wait_return = FALSE;
541 msg_didany = FALSE;
544 * Check validity of any generic resources that may have been loaded.
546 if (gui.border_width < 0)
547 gui.border_width = 0;
550 * Set up the fonts. First use a font specified with "-fn" or "-font".
552 if (font_argument != NULL)
553 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
554 if (
555 #ifdef FEAT_XFONTSET
556 (*p_guifontset == NUL
557 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
558 #endif
559 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
560 : p_guifont, FALSE) == FAIL)
562 EMSG(_("E665: Cannot start GUI, no valid font found"));
563 goto error2;
565 #ifdef FEAT_MBYTE
566 if (gui_get_wide_font() == FAIL)
567 EMSG(_("E231: 'guifontwide' invalid"));
568 #endif
570 gui.num_cols = Columns;
571 gui.num_rows = Rows;
572 gui_reset_scroll_region();
574 /* Create initial scrollbars */
575 FOR_ALL_WINDOWS(wp)
577 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
578 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
580 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
582 #ifdef FEAT_MENU
583 gui_create_initial_menus(root_menu);
584 #endif
585 #ifdef FEAT_SUN_WORKSHOP
586 if (usingSunWorkShop)
587 workshop_init();
588 #endif
589 #ifdef FEAT_SIGN_ICONS
590 sign_gui_started();
591 #endif
593 /* Configure the desired menu and scrollbars */
594 gui_init_which_components(NULL);
596 /* All components of the GUI have been created now */
597 gui.shell_created = TRUE;
599 #ifndef FEAT_GUI_GTK
600 /* Set the shell size, adjusted for the screen size. For GTK this only
601 * works after the shell has been opened, thus it is further down. */
602 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
603 #endif
604 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
605 /* Need to set the size of the menubar after all the menus have been
606 * created. */
607 gui_mch_compute_menu_height((Widget)0);
608 #endif
611 * Actually open the GUI shell.
613 if (gui_mch_open() != FAIL)
615 #ifdef FEAT_TITLE
616 maketitle();
617 resettitle();
618 #endif
619 init_gui_options();
620 #ifdef FEAT_ARABIC
621 /* Our GUI can't do bidi. */
622 p_tbidi = FALSE;
623 #endif
624 #if defined(FEAT_GUI_GTK)
625 /* Give GTK+ a chance to put all widget's into place. */
626 gui_mch_update();
628 # ifdef FEAT_MENU
629 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
630 * It was still there to make F10 work. */
631 if (vim_strchr(p_go, GO_MENUS) == NULL)
633 --gui.starting;
634 gui_mch_enable_menu(FALSE);
635 ++gui.starting;
636 gui_mch_update();
638 # endif
640 /* Now make sure the shell fits on the screen. */
641 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
642 #endif
643 /* When 'lines' was set while starting up the topframe may have to be
644 * resized. */
645 win_new_shellsize();
647 #ifdef FEAT_BEVAL
648 /* Always create the Balloon Evaluation area, but disable it when
649 * 'ballooneval' is off */
650 # ifdef FEAT_GUI_GTK
651 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
652 &general_beval_cb, NULL);
653 # else
654 # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
656 extern Widget textArea;
657 balloonEval = gui_mch_create_beval_area(textArea, NULL,
658 &general_beval_cb, NULL);
660 # else
661 # ifdef FEAT_GUI_W32
662 balloonEval = gui_mch_create_beval_area(NULL, NULL,
663 &general_beval_cb, NULL);
664 # endif
665 # endif
666 # endif
667 if (!p_beval)
668 gui_mch_disable_beval_area(balloonEval);
669 #endif
671 #ifdef FEAT_NETBEANS_INTG
672 if (starting == 0 && usingNetbeans)
673 /* Tell the client that it can start sending commands. */
674 netbeans_startup_done();
675 #endif
676 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
677 if (!im_xim_isvalid_imactivate())
678 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
679 #endif
680 /* When 'cmdheight' was set during startup it may not have taken
681 * effect yet. */
682 if (p_ch != 1L)
683 command_height();
685 return;
688 error2:
689 #ifdef FEAT_GUI_X11
690 /* undo gui_mch_init() */
691 gui_mch_uninit();
692 #endif
694 error:
695 gui.in_use = FALSE;
696 clip_init(FALSE);
700 void
701 gui_exit(rc)
702 int rc;
704 #ifndef __BEOS__
705 /* don't free the fonts, it leads to a BUS error
706 * richard@whitequeen.com Jul 99 */
707 free_highlight_fonts();
708 #endif
709 gui.in_use = FALSE;
710 gui_mch_exit(rc);
713 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
714 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
715 || defined(PROTO) || defined(FEAT_GUI_MACVIM)
716 # define NEED_GUI_UPDATE_SCREEN 1
718 * Called when the GUI shell is closed by the user. If there are no changed
719 * files Vim exits, otherwise there will be a dialog to ask the user what to
720 * do.
721 * When this function returns, Vim should NOT exit!
723 void
724 gui_shell_closed()
726 cmdmod_T save_cmdmod;
728 save_cmdmod = cmdmod;
730 /* Only exit when there are no changed files */
731 exiting = TRUE;
732 # ifdef FEAT_BROWSE
733 cmdmod.browse = TRUE;
734 # endif
735 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
736 cmdmod.confirm = TRUE;
737 # endif
738 /* If there are changed buffers, present the user with a dialog if
739 * possible, otherwise give an error message. */
740 if (!check_changed_any(FALSE))
741 getout(0);
743 exiting = FALSE;
744 cmdmod = save_cmdmod;
745 gui_update_screen(); /* redraw, window may show changed buffer */
747 #endif
750 * Set the font. "font_list" is a a comma separated list of font names. The
751 * first font name that works is used. If none is found, use the default
752 * font.
753 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
754 * Return OK when able to set the font. When it failed FAIL is returned and
755 * the fonts are unchanged.
757 /*ARGSUSED*/
759 gui_init_font(font_list, fontset)
760 char_u *font_list;
761 int fontset;
763 #define FONTLEN 320
764 char_u font_name[FONTLEN];
765 int font_list_empty = FALSE;
766 int ret = FAIL;
768 if (!gui.in_use)
769 return FAIL;
771 font_name[0] = NUL;
772 if (*font_list == NUL)
773 font_list_empty = TRUE;
774 else
776 #ifdef FEAT_XFONTSET
777 /* When using a fontset, the whole list of fonts is one name. */
778 if (fontset)
779 ret = gui_mch_init_font(font_list, TRUE);
780 else
781 #endif
782 while (*font_list != NUL)
784 /* Isolate one comma separated font name. */
785 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
787 #if defined(FEAT_GUI_MACVIM)
788 /* The font dialog is modeless in Mac OS X, so when
789 * gui_mch_init_font() is called with "*" it brings up the
790 * dialog and returns immediately. In this case we don't want
791 * it to be called again with NULL, so return here. */
792 if (STRCMP(font_name, "*") == 0) {
793 gui_mch_init_font(font_name, FALSE);
794 return FALSE;
796 #endif
798 /* Careful!!! The Win32 version of gui_mch_init_font(), when
799 * called with "*" will change p_guifont to the selected font
800 * name, which frees the old value. This makes font_list
801 * invalid. Thus when OK is returned here, font_list must no
802 * longer be used! */
803 if (gui_mch_init_font(font_name, FALSE) == OK)
805 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
806 /* If it's a Unicode font, try setting 'guifontwide' to a
807 * similar double-width font. */
808 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
809 && strstr((char *)font_name, "10646") != NULL)
810 set_guifontwide(font_name);
811 #endif
812 ret = OK;
813 break;
818 if (ret != OK
819 && STRCMP(font_list, "*") != 0
820 && (font_list_empty || gui.norm_font == NOFONT))
823 * Couldn't load any font in 'font_list', keep the current font if
824 * there is one. If 'font_list' is empty, or if there is no current
825 * font, tell gui_mch_init_font() to try to find a font we can load.
827 ret = gui_mch_init_font(NULL, FALSE);
830 if (ret == OK)
832 #ifndef HAVE_GTK2
833 /* Set normal font as current font */
834 # ifdef FEAT_XFONTSET
835 if (gui.fontset != NOFONTSET)
836 gui_mch_set_fontset(gui.fontset);
837 else
838 # endif
839 gui_mch_set_font(gui.norm_font);
840 #endif
841 gui_set_shellsize(FALSE,
842 #ifdef MSWIN
843 TRUE
844 #else
845 FALSE
846 #endif
847 , RESIZE_BOTH);
850 return ret;
853 #if defined(FEAT_MBYTE) || defined(PROTO)
854 # ifndef HAVE_GTK2
856 * Try setting 'guifontwide' to a font twice as wide as "name".
858 static void
859 set_guifontwide(name)
860 char_u *name;
862 int i = 0;
863 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
864 char_u *wp = NULL;
865 char_u *p;
866 GuiFont font;
868 wp = wide_name;
869 for (p = name; *p != NUL; ++p)
871 *wp++ = *p;
872 if (*p == '-')
874 ++i;
875 if (i == 6) /* font type: change "--" to "-*-" */
877 if (p[1] == '-')
878 *wp++ = '*';
880 else if (i == 12) /* found the width */
882 ++p;
883 i = getdigits(&p);
884 if (i != 0)
886 /* Double the width specification. */
887 sprintf((char *)wp, "%d%s", i * 2, p);
888 font = gui_mch_get_font(wide_name, FALSE);
889 if (font != NOFONT)
891 gui_mch_free_font(gui.wide_font);
892 gui.wide_font = font;
893 set_string_option_direct((char_u *)"gfw", -1,
894 wide_name, OPT_FREE, 0);
897 break;
902 # endif /* !HAVE_GTK2 */
905 * Get the font for 'guifontwide'.
906 * Return FAIL for an invalid font name.
909 gui_get_wide_font()
911 GuiFont font = NOFONT;
912 char_u font_name[FONTLEN];
913 char_u *p;
915 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
916 return OK; /* Will give an error message later. */
918 if (p_guifontwide != NULL && *p_guifontwide != NUL)
920 for (p = p_guifontwide; *p != NUL; )
922 /* Isolate one comma separated font name. */
923 (void)copy_option_part(&p, font_name, FONTLEN, ",");
924 font = gui_mch_get_font(font_name, FALSE);
925 if (font != NOFONT)
926 break;
928 if (font == NOFONT)
929 return FAIL;
932 gui_mch_free_font(gui.wide_font);
933 #ifdef HAVE_GTK2
934 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
935 if (font != NOFONT && gui.norm_font != NOFONT
936 && pango_font_description_equal(font, gui.norm_font))
938 gui.wide_font = NOFONT;
939 gui_mch_free_font(font);
941 else
942 #endif
943 gui.wide_font = font;
944 return OK;
946 #endif
948 void
949 gui_set_cursor(row, col)
950 int row;
951 int col;
953 gui.row = row;
954 gui.col = col;
958 * gui_check_pos - check if the cursor is on the screen.
960 static void
961 gui_check_pos()
963 if (gui.row >= screen_Rows)
964 gui.row = screen_Rows - 1;
965 if (gui.col >= screen_Columns)
966 gui.col = screen_Columns - 1;
967 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
968 gui.cursor_is_valid = FALSE;
972 * Redraw the cursor if necessary or when forced.
973 * Careful: The contents of ScreenLines[] must match what is on the screen,
974 * otherwise this goes wrong. May need to call out_flush() first.
976 void
977 gui_update_cursor(force, clear_selection)
978 int force; /* when TRUE, update even when not moved */
979 int clear_selection;/* clear selection under cursor */
981 int cur_width = 0;
982 int cur_height = 0;
983 int old_hl_mask;
984 int idx;
985 int id;
986 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
987 int cattr; /* cursor attributes */
988 int attr;
989 attrentry_T *aep = NULL;
991 /* Don't update the cursor when halfway busy scrolling.
992 * ScreenLines[] isn't valid then. */
993 if (!can_update_cursor)
994 return;
996 gui_check_pos();
997 if (!gui.cursor_is_valid || force
998 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1000 gui_undraw_cursor();
1001 if (gui.row < 0)
1002 return;
1003 #ifdef USE_IM_CONTROL
1004 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1005 im_set_position(gui.row, gui.col);
1006 #endif
1007 gui.cursor_row = gui.row;
1008 gui.cursor_col = gui.col;
1010 /* Only write to the screen after ScreenLines[] has been initialized */
1011 if (!screen_cleared || ScreenLines == NULL)
1012 return;
1014 /* Clear the selection if we are about to write over it */
1015 if (clear_selection)
1016 clip_may_clear_selection(gui.row, gui.row);
1017 /* Check that the cursor is inside the shell (resizing may have made
1018 * it invalid) */
1019 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1020 return;
1022 gui.cursor_is_valid = TRUE;
1025 * How the cursor is drawn depends on the current mode.
1027 idx = get_shape_idx(FALSE);
1028 if (State & LANGMAP)
1029 id = shape_table[idx].id_lm;
1030 else
1031 id = shape_table[idx].id;
1033 /* get the colors and attributes for the cursor. Default is inverted */
1034 cfg = INVALCOLOR;
1035 cbg = INVALCOLOR;
1036 cattr = HL_INVERSE;
1037 gui_mch_set_blinking(shape_table[idx].blinkwait,
1038 shape_table[idx].blinkon,
1039 shape_table[idx].blinkoff);
1040 if (id > 0)
1042 cattr = syn_id2colors(id, &cfg, &cbg);
1043 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
1045 static int iid;
1046 guicolor_T fg, bg;
1048 if (im_get_status())
1050 iid = syn_name2id((char_u *)"CursorIM");
1051 if (iid > 0)
1053 syn_id2colors(iid, &fg, &bg);
1054 if (bg != INVALCOLOR)
1055 cbg = bg;
1056 if (fg != INVALCOLOR)
1057 cfg = fg;
1061 #endif
1065 * Get the attributes for the character under the cursor.
1066 * When no cursor color was given, use the character color.
1068 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1069 if (attr > HL_ALL)
1070 aep = syn_gui_attr2entry(attr);
1071 if (aep != NULL)
1073 attr = aep->ae_attr;
1074 if (cfg == INVALCOLOR)
1075 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1076 : aep->ae_u.gui.fg_color);
1077 if (cbg == INVALCOLOR)
1078 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1079 : aep->ae_u.gui.bg_color);
1081 if (cfg == INVALCOLOR)
1082 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1083 if (cbg == INVALCOLOR)
1084 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1086 #ifdef FEAT_XIM
1087 if (aep != NULL)
1089 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1090 : aep->ae_u.gui.bg_color);
1091 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1092 : aep->ae_u.gui.fg_color);
1093 if (xim_bg_color == INVALCOLOR)
1094 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1095 : gui.back_pixel;
1096 if (xim_fg_color == INVALCOLOR)
1097 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1098 : gui.norm_pixel;
1100 else
1102 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1103 : gui.back_pixel;
1104 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1105 : gui.norm_pixel;
1107 #endif
1109 attr &= ~HL_INVERSE;
1110 if (cattr & HL_INVERSE)
1112 cc = cbg;
1113 cbg = cfg;
1114 cfg = cc;
1116 cattr &= ~HL_INVERSE;
1119 * When we don't have window focus, draw a hollow cursor.
1121 if (!gui.in_focus)
1123 gui_mch_draw_hollow_cursor(cbg);
1124 return;
1127 old_hl_mask = gui.highlight_mask;
1128 if (shape_table[idx].shape == SHAPE_BLOCK
1129 #ifdef FEAT_HANGULIN
1130 || composing_hangul
1131 #endif
1135 * Draw the text character with the cursor colors. Use the
1136 * character attributes plus the cursor attributes.
1138 gui.highlight_mask = (cattr | attr);
1139 #ifdef FEAT_HANGULIN
1140 if (composing_hangul)
1141 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1142 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1143 else
1144 #endif
1145 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1146 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1148 else
1150 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1151 int col_off = FALSE;
1152 #endif
1154 * First draw the partial cursor, then overwrite with the text
1155 * character, using a transparent background.
1157 if (shape_table[idx].shape == SHAPE_VER)
1159 cur_height = gui.char_height;
1160 cur_width = (gui.char_width * shape_table[idx].percentage
1161 + 99) / 100;
1163 else
1165 cur_height = (gui.char_height * shape_table[idx].percentage
1166 + 99) / 100;
1167 cur_width = gui.char_width;
1169 #ifdef FEAT_MBYTE
1170 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1171 LineOffset[gui.row] + screen_Columns) > 1)
1173 /* Double wide character. */
1174 if (shape_table[idx].shape != SHAPE_VER)
1175 cur_width += gui.char_width;
1176 # ifdef FEAT_RIGHTLEFT
1177 if (CURSOR_BAR_RIGHT)
1179 /* gui.col points to the left halve of the character but
1180 * the vertical line needs to be on the right halve.
1181 * A double-wide horizontal line is also drawn from the
1182 * right halve in gui_mch_draw_part_cursor(). */
1183 col_off = TRUE;
1184 ++gui.col;
1186 # endif
1188 #endif
1189 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1190 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1191 if (col_off)
1192 --gui.col;
1193 #endif
1195 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1196 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1197 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1198 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1199 (guicolor_T)0, (guicolor_T)0, 0);
1200 #endif
1202 gui.highlight_mask = old_hl_mask;
1206 #if defined(FEAT_MENU) || defined(PROTO)
1207 void
1208 gui_position_menu()
1210 # if !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
1211 || defined(FEAT_GUI_MACVIM))
1212 if (gui.menu_is_active && gui.in_use)
1213 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1214 # endif
1216 #endif
1219 * Position the various GUI components (text area, menu). The vertical
1220 * scrollbars are NOT handled here. See gui_update_scrollbars().
1222 /*ARGSUSED*/
1223 static void
1224 gui_position_components(total_width)
1225 int total_width;
1227 int text_area_x;
1228 int text_area_y;
1229 int text_area_width;
1230 int text_area_height;
1232 /* avoid that moving components around generates events */
1233 ++hold_gui_events;
1235 text_area_x = 0;
1236 if (gui.which_scrollbars[SBAR_LEFT])
1237 text_area_x += gui.scrollbar_width;
1239 text_area_y = 0;
1240 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
1241 || defined(FEAT_GUI_MACVIM))
1242 gui.menu_width = total_width;
1243 if (gui.menu_is_active)
1244 text_area_y += gui.menu_height;
1245 #endif
1246 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1247 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1248 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1249 #endif
1251 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1252 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
1253 if (gui_has_tabline())
1254 text_area_y += gui.tabline_height;
1255 #endif
1257 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1258 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1260 # ifdef FEAT_GUI_ATHENA
1261 gui_mch_set_toolbar_pos(0, text_area_y,
1262 gui.menu_width, gui.toolbar_height);
1263 # endif
1264 text_area_y += gui.toolbar_height;
1266 #endif
1268 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1269 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1271 gui_mch_set_text_area_pos(text_area_x,
1272 text_area_y,
1273 text_area_width,
1274 text_area_height
1275 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1276 + xim_get_status_area_height()
1277 #endif
1279 #ifdef FEAT_MENU
1280 gui_position_menu();
1281 #endif
1282 if (gui.which_scrollbars[SBAR_BOTTOM])
1283 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1284 text_area_x,
1285 text_area_y + text_area_height,
1286 text_area_width,
1287 gui.scrollbar_height);
1288 gui.left_sbar_x = 0;
1289 gui.right_sbar_x = text_area_x + text_area_width;
1291 --hold_gui_events;
1295 * Get the width of the widgets and decorations to the side of the text area.
1298 gui_get_base_width()
1300 int base_width;
1302 base_width = 2 * gui.border_offset;
1303 if (gui.which_scrollbars[SBAR_LEFT])
1304 base_width += gui.scrollbar_width;
1305 if (gui.which_scrollbars[SBAR_RIGHT])
1306 base_width += gui.scrollbar_width;
1307 return base_width;
1311 * Get the height of the widgets and decorations above and below the text area.
1314 gui_get_base_height()
1316 int base_height;
1318 base_height = 2 * gui.border_offset;
1319 if (gui.which_scrollbars[SBAR_BOTTOM])
1320 base_height += gui.scrollbar_height;
1321 #ifdef FEAT_GUI_GTK
1322 /* We can't take the sizes properly into account until anything is
1323 * realized. Therefore we recalculate all the values here just before
1324 * setting the size. (--mdcki) */
1325 #elif !defined(FEAT_GUI_MACVIM)
1326 # ifdef FEAT_MENU
1327 if (gui.menu_is_active)
1328 base_height += gui.menu_height;
1329 # endif
1330 # ifdef FEAT_TOOLBAR
1331 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1332 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1333 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1334 # else
1335 base_height += gui.toolbar_height;
1336 # endif
1337 # endif
1338 # if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1339 || defined(FEAT_GUI_MOTIF))
1340 if (gui_has_tabline())
1341 base_height += gui.tabline_height;
1342 # endif
1343 # ifdef FEAT_FOOTER
1344 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1345 base_height += gui.footer_height;
1346 # endif
1347 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1348 base_height += gui_mch_text_area_extra_height();
1349 # endif
1350 #endif
1351 return base_height;
1355 * Should be called after the GUI shell has been resized. Its arguments are
1356 * the new width and height of the shell in pixels.
1358 void
1359 gui_resize_shell(pixel_width, pixel_height)
1360 int pixel_width;
1361 int pixel_height;
1363 static int busy = FALSE;
1365 if (!gui.shell_created) /* ignore when still initializing */
1366 return;
1369 * Can't resize the screen while it is being redrawn. Remember the new
1370 * size and handle it later.
1372 if (updating_screen || busy)
1374 new_pixel_width = pixel_width;
1375 new_pixel_height = pixel_height;
1376 return;
1379 again:
1380 busy = TRUE;
1382 /* Flush pending output before redrawing */
1383 out_flush();
1385 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1386 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
1388 gui_position_components(pixel_width);
1390 gui_reset_scroll_region();
1392 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1393 * at the last line here (why does it have to be one row too low?).
1395 if (State == ASKMORE || State == CONFIRM)
1396 gui.row = gui.num_rows;
1398 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1399 * the safe side. */
1400 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1401 || gui.num_rows != Rows || gui.num_cols != Columns)
1402 shell_resized();
1404 gui_update_scrollbars(TRUE);
1405 gui_update_cursor(FALSE, TRUE);
1406 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1407 xim_set_status_area();
1408 #endif
1410 busy = FALSE;
1413 * We could have been called again while redrawing the screen.
1414 * Need to do it all again with the latest size then.
1416 if (new_pixel_height)
1418 pixel_width = new_pixel_width;
1419 pixel_height = new_pixel_height;
1420 new_pixel_width = 0;
1421 new_pixel_height = 0;
1422 goto again;
1427 * Check if gui_resize_shell() must be called.
1429 void
1430 gui_may_resize_shell()
1432 int h, w;
1434 if (new_pixel_height)
1436 /* careful: gui_resize_shell() may postpone the resize again if we
1437 * were called indirectly by it */
1438 w = new_pixel_width;
1439 h = new_pixel_height;
1440 new_pixel_width = 0;
1441 new_pixel_height = 0;
1442 gui_resize_shell(w, h);
1447 gui_get_shellsize()
1449 Rows = gui.num_rows;
1450 Columns = gui.num_cols;
1451 return OK;
1455 * Set the size of the Vim shell according to Rows and Columns.
1456 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1457 * on the screen.
1459 /*ARGSUSED*/
1460 void
1461 gui_set_shellsize(mustset, fit_to_display, direction)
1462 int mustset; /* set by the user */
1463 int fit_to_display;
1464 int direction; /* RESIZE_HOR, RESIZE_VER */
1466 int base_width;
1467 int base_height;
1468 int width;
1469 int height;
1470 int min_width;
1471 int min_height;
1472 int screen_w;
1473 int screen_h;
1475 if (!gui.shell_created)
1476 return;
1478 #ifdef MSWIN
1479 /* If not setting to a user specified size and maximized, calculate the
1480 * number of characters that fit in the maximized window. */
1481 if (!mustset && gui_mch_maximized())
1483 gui_mch_newfont();
1484 return;
1486 #endif
1488 base_width = gui_get_base_width();
1489 base_height = gui_get_base_height();
1490 #ifdef USE_SUN_WORKSHOP
1491 if (!mustset && usingSunWorkShop
1492 && workshop_get_width_height(&width, &height))
1494 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1495 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1497 else
1498 #endif
1500 width = Columns * gui.char_width + base_width;
1501 height = Rows * gui.char_height + base_height;
1504 if (fit_to_display)
1506 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1507 if ((direction & RESIZE_HOR) && width > screen_w)
1509 Columns = (screen_w - base_width) / gui.char_width;
1510 if (Columns < MIN_COLUMNS)
1511 Columns = MIN_COLUMNS;
1512 width = Columns * gui.char_width + base_width;
1514 if ((direction & RESIZE_VERT) && height > screen_h)
1516 Rows = (screen_h - base_height) / gui.char_height;
1517 check_shellsize();
1518 height = Rows * gui.char_height + base_height;
1521 gui.num_cols = Columns;
1522 gui.num_rows = Rows;
1524 min_width = base_width + MIN_COLUMNS * gui.char_width;
1525 min_height = base_height + MIN_LINES * gui.char_height;
1526 # ifdef FEAT_WINDOWS
1527 min_height += tabline_height() * gui.char_height;
1528 # endif
1530 gui_mch_set_shellsize(width, height, min_width, min_height,
1531 base_width, base_height, direction);
1532 if (fit_to_display)
1534 int x, y;
1536 /* Some window managers put the Vim window left of/above the screen. */
1537 gui_mch_update();
1538 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1539 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1542 gui_position_components(width);
1543 gui_update_scrollbars(TRUE);
1544 gui_reset_scroll_region();
1548 * Called when Rows and/or Columns has changed.
1550 void
1551 gui_new_shellsize()
1553 gui_reset_scroll_region();
1557 * Make scroll region cover whole screen.
1559 void
1560 gui_reset_scroll_region()
1562 gui.scroll_region_top = 0;
1563 gui.scroll_region_bot = gui.num_rows - 1;
1564 gui.scroll_region_left = 0;
1565 gui.scroll_region_right = gui.num_cols - 1;
1568 void
1569 gui_start_highlight(mask)
1570 int mask;
1572 if (mask > HL_ALL) /* highlight code */
1573 gui.highlight_mask = mask;
1574 else /* mask */
1575 gui.highlight_mask |= mask;
1578 void
1579 gui_stop_highlight(mask)
1580 int mask;
1582 if (mask > HL_ALL) /* highlight code */
1583 gui.highlight_mask = HL_NORMAL;
1584 else /* mask */
1585 gui.highlight_mask &= ~mask;
1589 * Clear a rectangular region of the screen from text pos (row1, col1) to
1590 * (row2, col2) inclusive.
1592 void
1593 gui_clear_block(row1, col1, row2, col2)
1594 int row1;
1595 int col1;
1596 int row2;
1597 int col2;
1599 /* Clear the selection if we are about to write over it */
1600 clip_may_clear_selection(row1, row2);
1602 gui_mch_clear_block(row1, col1, row2, col2);
1604 /* Invalidate cursor if it was in this block */
1605 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1606 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1607 gui.cursor_is_valid = FALSE;
1611 * Write code to update the cursor later. This avoids the need to flush the
1612 * output buffer before calling gui_update_cursor().
1614 void
1615 gui_update_cursor_later()
1617 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1620 void
1621 gui_write(s, len)
1622 char_u *s;
1623 int len;
1625 char_u *p;
1626 int arg1 = 0, arg2 = 0;
1627 /* this doesn't make sense, disabled until someone can explain why it
1628 * would be needed */
1629 #if 0 && (defined(RISCOS) || defined(WIN16))
1630 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1631 our redraw speed will suffer */
1632 #else
1633 int force_cursor = FALSE; /* force cursor update */
1634 #endif
1635 int force_scrollbar = FALSE;
1636 static win_T *old_curwin = NULL;
1638 /* #define DEBUG_GUI_WRITE */
1639 #ifdef DEBUG_GUI_WRITE
1641 int i;
1642 char_u *str;
1644 printf("gui_write(%d):\n ", len);
1645 for (i = 0; i < len; i++)
1646 if (s[i] == ESC)
1648 if (i != 0)
1649 printf("\n ");
1650 printf("<ESC>");
1652 else
1654 str = transchar_byte(s[i]);
1655 if (str[0] && str[1])
1656 printf("<%s>", (char *)str);
1657 else
1658 printf("%s", (char *)str);
1660 printf("\n");
1662 #endif
1663 while (len)
1665 if (s[0] == ESC && s[1] == '|')
1667 p = s + 2;
1668 if (VIM_ISDIGIT(*p))
1670 arg1 = getdigits(&p);
1671 if (p > s + len)
1672 break;
1673 if (*p == ';')
1675 ++p;
1676 arg2 = getdigits(&p);
1677 if (p > s + len)
1678 break;
1681 switch (*p)
1683 case 'C': /* Clear screen */
1684 clip_scroll_selection(9999);
1685 gui_mch_clear_all();
1686 gui.cursor_is_valid = FALSE;
1687 force_scrollbar = TRUE;
1688 break;
1689 case 'M': /* Move cursor */
1690 gui_set_cursor(arg1, arg2);
1691 break;
1692 case 's': /* force cursor (shape) update */
1693 force_cursor = TRUE;
1694 break;
1695 case 'R': /* Set scroll region */
1696 if (arg1 < arg2)
1698 gui.scroll_region_top = arg1;
1699 gui.scroll_region_bot = arg2;
1701 else
1703 gui.scroll_region_top = arg2;
1704 gui.scroll_region_bot = arg1;
1706 break;
1707 #ifdef FEAT_VERTSPLIT
1708 case 'V': /* Set vertical scroll region */
1709 if (arg1 < arg2)
1711 gui.scroll_region_left = arg1;
1712 gui.scroll_region_right = arg2;
1714 else
1716 gui.scroll_region_left = arg2;
1717 gui.scroll_region_right = arg1;
1719 break;
1720 #endif
1721 case 'd': /* Delete line */
1722 gui_delete_lines(gui.row, 1);
1723 break;
1724 case 'D': /* Delete lines */
1725 gui_delete_lines(gui.row, arg1);
1726 break;
1727 case 'i': /* Insert line */
1728 gui_insert_lines(gui.row, 1);
1729 break;
1730 case 'I': /* Insert lines */
1731 gui_insert_lines(gui.row, arg1);
1732 break;
1733 case '$': /* Clear to end-of-line */
1734 gui_clear_block(gui.row, gui.col, gui.row,
1735 (int)Columns - 1);
1736 break;
1737 case 'h': /* Turn on highlighting */
1738 gui_start_highlight(arg1);
1739 break;
1740 case 'H': /* Turn off highlighting */
1741 gui_stop_highlight(arg1);
1742 break;
1743 case 'f': /* flash the window (visual bell) */
1744 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1745 break;
1746 default:
1747 p = s + 1; /* Skip the ESC */
1748 break;
1750 len -= (int)(++p - s);
1751 s = p;
1753 else if (
1754 #ifdef EBCDIC
1755 CtrlChar(s[0]) != 0 /* Ctrl character */
1756 #else
1757 s[0] < 0x20 /* Ctrl character */
1758 #endif
1759 #ifdef FEAT_SIGN_ICONS
1760 && s[0] != SIGN_BYTE
1761 # ifdef FEAT_NETBEANS_INTG
1762 && s[0] != MULTISIGN_BYTE
1763 # endif
1764 #endif
1767 if (s[0] == '\n') /* NL */
1769 gui.col = 0;
1770 if (gui.row < gui.scroll_region_bot)
1771 gui.row++;
1772 else
1773 gui_delete_lines(gui.scroll_region_top, 1);
1775 else if (s[0] == '\r') /* CR */
1777 gui.col = 0;
1779 else if (s[0] == '\b') /* Backspace */
1781 if (gui.col)
1782 --gui.col;
1784 else if (s[0] == Ctrl_L) /* cursor-right */
1786 ++gui.col;
1788 else if (s[0] == Ctrl_G) /* Beep */
1790 gui_mch_beep();
1792 /* Other Ctrl character: shouldn't happen! */
1794 --len; /* Skip this char */
1795 ++s;
1797 else
1799 p = s;
1800 while (len > 0 && (
1801 #ifdef EBCDIC
1802 CtrlChar(*p) == 0
1803 #else
1804 *p >= 0x20
1805 #endif
1806 #ifdef FEAT_SIGN_ICONS
1807 || *p == SIGN_BYTE
1808 # ifdef FEAT_NETBEANS_INTG
1809 || *p == MULTISIGN_BYTE
1810 # endif
1811 #endif
1814 len--;
1815 p++;
1817 gui_outstr(s, (int)(p - s));
1818 s = p;
1822 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1823 * set). */
1824 if (force_cursor)
1825 gui_update_cursor(TRUE, TRUE);
1827 /* When switching to another window the dragging must have stopped.
1828 * Required for GTK, dragged_sb isn't reset. */
1829 if (old_curwin != curwin)
1830 gui.dragged_sb = SBAR_NONE;
1832 /* Update the scrollbars after clearing the screen or when switched
1833 * to another window.
1834 * Update the horizontal scrollbar always, it's difficult to check all
1835 * situations where it might change. */
1836 if (force_scrollbar || old_curwin != curwin)
1837 gui_update_scrollbars(force_scrollbar);
1838 else
1839 gui_update_horiz_scrollbar(FALSE);
1840 old_curwin = curwin;
1843 * We need to make sure this is cleared since Athena doesn't tell us when
1844 * he is done dragging. Do the same for GTK.
1846 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1847 gui.dragged_sb = SBAR_NONE;
1848 #endif
1850 gui_mch_flush(); /* In case vim decides to take a nap */
1854 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1855 * produces wrong results. Call gui_dont_update_cursor() before that code and
1856 * gui_can_update_cursor() afterwards.
1858 void
1859 gui_dont_update_cursor()
1861 if (gui.in_use)
1863 /* Undraw the cursor now, we probably can't do it after the change. */
1864 gui_undraw_cursor();
1865 can_update_cursor = FALSE;
1869 void
1870 gui_can_update_cursor()
1872 can_update_cursor = TRUE;
1873 /* No need to update the cursor right now, there is always more output
1874 * after scrolling. */
1877 static void
1878 gui_outstr(s, len)
1879 char_u *s;
1880 int len;
1882 int this_len;
1883 #ifdef FEAT_MBYTE
1884 int cells;
1885 #endif
1887 if (len == 0)
1888 return;
1890 if (len < 0)
1891 len = (int)STRLEN(s);
1893 while (len > 0)
1895 #ifdef FEAT_MBYTE
1896 if (has_mbyte)
1898 /* Find out how many chars fit in the current line. */
1899 cells = 0;
1900 for (this_len = 0; this_len < len; )
1902 cells += (*mb_ptr2cells)(s + this_len);
1903 if (gui.col + cells > Columns)
1904 break;
1905 this_len += (*mb_ptr2len)(s + this_len);
1907 if (this_len > len)
1908 this_len = len; /* don't include following composing char */
1910 else
1911 #endif
1912 if (gui.col + len > Columns)
1913 this_len = Columns - gui.col;
1914 else
1915 this_len = len;
1917 (void)gui_outstr_nowrap(s, this_len,
1918 0, (guicolor_T)0, (guicolor_T)0, 0);
1919 s += this_len;
1920 len -= this_len;
1921 #ifdef FEAT_MBYTE
1922 /* fill up for a double-width char that doesn't fit. */
1923 if (len > 0 && gui.col < Columns)
1924 (void)gui_outstr_nowrap((char_u *)" ", 1,
1925 0, (guicolor_T)0, (guicolor_T)0, 0);
1926 #endif
1927 /* The cursor may wrap to the next line. */
1928 if (gui.col >= Columns)
1930 gui.col = 0;
1931 gui.row++;
1937 * Output one character (may be one or two display cells).
1938 * Caller must check for valid "off".
1939 * Returns FAIL or OK, just like gui_outstr_nowrap().
1941 static int
1942 gui_screenchar(off, flags, fg, bg, back)
1943 int off; /* Offset from start of screen */
1944 int flags;
1945 guicolor_T fg, bg; /* colors for cursor */
1946 int back; /* backup this many chars when using bold trick */
1948 #ifdef FEAT_MBYTE
1949 char_u buf[MB_MAXBYTES + 1];
1951 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1952 if (enc_utf8 && ScreenLines[off] == 0)
1953 return OK;
1955 if (enc_utf8 && ScreenLinesUC[off] != 0)
1956 /* Draw UTF-8 multi-byte character. */
1957 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1958 flags, fg, bg, back);
1960 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1962 buf[0] = ScreenLines[off];
1963 buf[1] = ScreenLines2[off];
1964 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1967 /* Draw non-multi-byte character or DBCS character. */
1968 return gui_outstr_nowrap(ScreenLines + off,
1969 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
1970 flags, fg, bg, back);
1971 #else
1972 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1973 #endif
1976 #ifdef HAVE_GTK2
1978 * Output the string at the given screen position. This is used in place
1979 * of gui_screenchar() where possible because Pango needs as much context
1980 * as possible to work nicely. It's a lot faster as well.
1982 static int
1983 gui_screenstr(off, len, flags, fg, bg, back)
1984 int off; /* Offset from start of screen */
1985 int len; /* string length in screen cells */
1986 int flags;
1987 guicolor_T fg, bg; /* colors for cursor */
1988 int back; /* backup this many chars when using bold trick */
1990 char_u *buf;
1991 int outlen = 0;
1992 int i;
1993 int retval;
1995 if (len <= 0) /* "cannot happen"? */
1996 return OK;
1998 if (enc_utf8)
2000 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2001 if (buf == NULL)
2002 return OK; /* not much we could do here... */
2004 for (i = off; i < off + len; ++i)
2006 if (ScreenLines[i] == 0)
2007 continue; /* skip second half of double-width char */
2009 if (ScreenLinesUC[i] == 0)
2010 buf[outlen++] = ScreenLines[i];
2011 else
2012 outlen += utfc_char2bytes(i, buf + outlen);
2015 buf[outlen] = NUL; /* only to aid debugging */
2016 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2017 vim_free(buf);
2019 return retval;
2021 else if (enc_dbcs == DBCS_JPNU)
2023 buf = alloc((unsigned)(len * 2 + 1));
2024 if (buf == NULL)
2025 return OK; /* not much we could do here... */
2027 for (i = off; i < off + len; ++i)
2029 buf[outlen++] = ScreenLines[i];
2031 /* handle double-byte single-width char */
2032 if (ScreenLines[i] == 0x8e)
2033 buf[outlen++] = ScreenLines2[i];
2034 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2035 buf[outlen++] = ScreenLines[++i];
2038 buf[outlen] = NUL; /* only to aid debugging */
2039 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2040 vim_free(buf);
2042 return retval;
2044 else
2046 return gui_outstr_nowrap(&ScreenLines[off], len,
2047 flags, fg, bg, back);
2050 #endif /* HAVE_GTK2 */
2053 * Output the given string at the current cursor position. If the string is
2054 * too long to fit on the line, then it is truncated.
2055 * "flags":
2056 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2057 * actually draw (an inverted) cursor.
2058 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
2059 * background.
2060 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2061 * it.
2062 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2063 * FAIL (the caller should start drawing "back" chars back).
2066 gui_outstr_nowrap(s, len, flags, fg, bg, back)
2067 char_u *s;
2068 int len;
2069 int flags;
2070 guicolor_T fg, bg; /* colors for cursor */
2071 int back; /* backup this many chars when using bold trick */
2073 long_u highlight_mask;
2074 long_u hl_mask_todo;
2075 guicolor_T fg_color;
2076 guicolor_T bg_color;
2077 guicolor_T sp_color;
2078 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2079 GuiFont font = NOFONT;
2080 # ifdef FEAT_XFONTSET
2081 GuiFontset fontset = NOFONTSET;
2082 # endif
2083 #endif
2084 attrentry_T *aep = NULL;
2085 int draw_flags;
2086 int col = gui.col;
2087 #ifdef FEAT_SIGN_ICONS
2088 int draw_sign = FALSE;
2089 # ifdef FEAT_NETBEANS_INTG
2090 int multi_sign = FALSE;
2091 # endif
2092 #endif
2094 if (len < 0)
2095 len = (int)STRLEN(s);
2096 if (len == 0)
2097 return OK;
2099 #ifdef FEAT_SIGN_ICONS
2100 if (*s == SIGN_BYTE
2101 # ifdef FEAT_NETBEANS_INTG
2102 || *s == MULTISIGN_BYTE
2103 # endif
2106 # ifdef FEAT_NETBEANS_INTG
2107 if (*s == MULTISIGN_BYTE)
2108 multi_sign = TRUE;
2109 # endif
2110 /* draw spaces instead */
2111 s = (char_u *)" ";
2112 if (len == 1 && col > 0)
2113 --col;
2114 len = 2;
2115 draw_sign = TRUE;
2116 highlight_mask = 0;
2118 else
2119 #endif
2120 if (gui.highlight_mask > HL_ALL)
2122 aep = syn_gui_attr2entry(gui.highlight_mask);
2123 if (aep == NULL) /* highlighting not set */
2124 highlight_mask = 0;
2125 else
2126 highlight_mask = aep->ae_attr;
2128 else
2129 highlight_mask = gui.highlight_mask;
2130 hl_mask_todo = highlight_mask;
2132 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
2133 /* Set the font */
2134 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2135 font = aep->ae_u.gui.font;
2136 # ifdef FEAT_XFONTSET
2137 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2138 fontset = aep->ae_u.gui.fontset;
2139 # endif
2140 else
2142 # ifdef FEAT_XFONTSET
2143 if (gui.fontset != NOFONTSET)
2144 fontset = gui.fontset;
2145 else
2146 # endif
2147 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2149 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2151 font = gui.boldital_font;
2152 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2154 else if (gui.bold_font != NOFONT)
2156 font = gui.bold_font;
2157 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2159 else
2160 font = gui.norm_font;
2162 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2164 font = gui.ital_font;
2165 hl_mask_todo &= ~HL_ITALIC;
2167 else
2168 font = gui.norm_font;
2170 # ifdef FEAT_XFONTSET
2171 if (fontset != NOFONTSET)
2172 gui_mch_set_fontset(fontset);
2173 else
2174 # endif
2175 gui_mch_set_font(font);
2176 #endif
2178 draw_flags = 0;
2180 /* Set the color */
2181 bg_color = gui.back_pixel;
2182 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2184 draw_flags |= DRAW_CURSOR;
2185 fg_color = fg;
2186 bg_color = bg;
2187 sp_color = fg;
2189 else if (aep != NULL)
2191 fg_color = aep->ae_u.gui.fg_color;
2192 if (fg_color == INVALCOLOR)
2193 fg_color = gui.norm_pixel;
2194 bg_color = aep->ae_u.gui.bg_color;
2195 if (bg_color == INVALCOLOR)
2196 bg_color = gui.back_pixel;
2197 sp_color = aep->ae_u.gui.sp_color;
2198 if (sp_color == INVALCOLOR)
2199 sp_color = fg_color;
2201 else
2203 fg_color = gui.norm_pixel;
2204 sp_color = fg_color;
2207 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2209 #if defined(AMIGA) || defined(RISCOS)
2210 gui_mch_set_colors(bg_color, fg_color);
2211 #else
2212 gui_mch_set_fg_color(bg_color);
2213 gui_mch_set_bg_color(fg_color);
2214 #endif
2216 else
2218 #if defined(AMIGA) || defined(RISCOS)
2219 gui_mch_set_colors(fg_color, bg_color);
2220 #else
2221 gui_mch_set_fg_color(fg_color);
2222 gui_mch_set_bg_color(bg_color);
2223 #endif
2225 gui_mch_set_sp_color(sp_color);
2227 /* Clear the selection if we are about to write over it */
2228 if (!(flags & GUI_MON_NOCLEAR))
2229 clip_may_clear_selection(gui.row, gui.row);
2232 #ifndef MSWIN16_FASTTEXT
2233 /* If there's no bold font, then fake it */
2234 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2235 draw_flags |= DRAW_BOLD;
2236 #endif
2239 * When drawing bold or italic characters the spill-over from the left
2240 * neighbor may be destroyed. Let the caller backup to start redrawing
2241 * just after a blank.
2243 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2244 return FAIL;
2246 #if defined(RISCOS) || defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
2247 /* If there's no italic font, then fake it.
2248 * For GTK2, we don't need a different font for italic style. */
2249 if (hl_mask_todo & HL_ITALIC)
2250 draw_flags |= DRAW_ITALIC;
2252 /* Do we underline the text? */
2253 if (hl_mask_todo & HL_UNDERLINE)
2254 draw_flags |= DRAW_UNDERL;
2255 #else
2256 /* Do we underline the text? */
2257 if ((hl_mask_todo & HL_UNDERLINE)
2258 # ifndef MSWIN16_FASTTEXT
2259 || (hl_mask_todo & HL_ITALIC)
2260 # endif
2262 draw_flags |= DRAW_UNDERL;
2263 #endif
2264 /* Do we undercurl the text? */
2265 if (hl_mask_todo & HL_UNDERCURL)
2266 draw_flags |= DRAW_UNDERC;
2268 /* Do we draw transparently? */
2269 if (flags & GUI_MON_TRS_CURSOR)
2270 draw_flags |= DRAW_TRANSP;
2273 * Draw the text.
2275 #ifdef HAVE_GTK2
2276 /* The value returned is the length in display cells */
2277 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2278 #elif defined(FEAT_GUI_MACVIM) && defined(FEAT_MBYTE)
2279 /* The value returned is the length in display cells */
2280 len = gui_macvim_draw_string(gui.row, col, s, len, draw_flags);
2281 #else
2282 # ifdef FEAT_MBYTE
2283 if (enc_utf8)
2285 int start; /* index of bytes to be drawn */
2286 int cells; /* cellwidth of bytes to be drawn */
2287 int thislen; /* length of bytes to be drawin */
2288 int cn; /* cellwidth of current char */
2289 int i; /* index of current char */
2290 int c; /* current char value */
2291 int cl; /* byte length of current char */
2292 int comping; /* current char is composing */
2293 int scol = col; /* screen column */
2294 int dowide; /* use 'guifontwide' */
2296 /* Break the string at a composing character, it has to be drawn on
2297 * top of the previous character. */
2298 start = 0;
2299 cells = 0;
2300 for (i = 0; i < len; i += cl)
2302 c = utf_ptr2char(s + i);
2303 cn = utf_char2cells(c);
2304 if (cn > 1
2305 # ifdef FEAT_XFONTSET
2306 && fontset == NOFONTSET
2307 # endif
2308 && gui.wide_font != NOFONT)
2309 dowide = TRUE;
2310 else
2311 dowide = FALSE;
2312 comping = utf_iscomposing(c);
2313 if (!comping) /* count cells from non-composing chars */
2314 cells += cn;
2315 cl = utf_ptr2len(s + i);
2316 if (cl == 0) /* hit end of string */
2317 len = i + cl; /* len must be wrong "cannot happen" */
2319 /* print the string so far if it's the last character or there is
2320 * a composing character. */
2321 if (i + cl >= len || (comping && i > start) || dowide
2322 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2323 || (cn > 1
2324 # ifdef FEAT_XFONTSET
2325 /* No fontset: At least draw char after wide char at
2326 * right position. */
2327 && fontset == NOFONTSET
2328 # endif
2330 # endif
2333 if (comping || dowide)
2334 thislen = i - start;
2335 else
2336 thislen = i - start + cl;
2337 if (thislen > 0)
2339 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2340 draw_flags);
2341 start += thislen;
2343 scol += cells;
2344 cells = 0;
2345 if (dowide)
2347 gui_mch_set_font(gui.wide_font);
2348 gui_mch_draw_string(gui.row, scol - cn,
2349 s + start, cl, draw_flags);
2350 gui_mch_set_font(font);
2351 start += cl;
2354 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2355 /* No fontset: draw a space to fill the gap after a wide char
2356 * */
2357 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2358 # ifdef FEAT_XFONTSET
2359 && fontset == NOFONTSET
2360 # endif
2361 && !dowide)
2362 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2363 1, draw_flags);
2364 # endif
2366 /* Draw a composing char on top of the previous char. */
2367 if (comping)
2369 # if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
2370 /* Carbon ATSUI autodraws composing char over previous char */
2371 gui_mch_draw_string(gui.row, scol, s + i, cl,
2372 draw_flags | DRAW_TRANSP);
2373 # else
2374 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2375 draw_flags | DRAW_TRANSP);
2376 # endif
2377 start = i + cl;
2380 /* The stuff below assumes "len" is the length in screen columns. */
2381 len = scol - col;
2383 else
2384 # endif
2386 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2387 # ifdef FEAT_MBYTE
2388 if (enc_dbcs == DBCS_JPNU)
2390 int clen = 0;
2391 int i;
2393 /* Get the length in display cells, this can be different from the
2394 * number of bytes for "euc-jp". */
2395 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
2396 clen += (*mb_ptr2cells)(s + i);
2397 len = clen;
2399 # endif
2401 #endif /* !HAVE_GTK2 */
2403 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2404 gui.col = col + len;
2406 /* May need to invert it when it's part of the selection. */
2407 if (flags & GUI_MON_NOCLEAR)
2408 clip_may_redraw_selection(gui.row, col, len);
2410 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2412 /* Invalidate the old physical cursor position if we wrote over it */
2413 if (gui.cursor_row == gui.row
2414 && gui.cursor_col >= col
2415 && gui.cursor_col < col + len)
2416 gui.cursor_is_valid = FALSE;
2419 #ifdef FEAT_SIGN_ICONS
2420 if (draw_sign)
2421 /* Draw the sign on top of the spaces. */
2422 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2423 # ifdef FEAT_NETBEANS_INTG
2424 if (multi_sign)
2425 netbeans_draw_multisign_indicator(gui.row);
2426 # endif
2427 #endif
2429 return OK;
2433 * Un-draw the cursor. Actually this just redraws the character at the given
2434 * position. The character just before it too, for when it was in bold.
2436 void
2437 gui_undraw_cursor()
2439 if (gui.cursor_is_valid)
2441 #ifdef FEAT_HANGULIN
2442 if (composing_hangul
2443 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2444 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2445 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2446 gui.norm_pixel, gui.back_pixel, 0);
2447 else
2449 #endif
2450 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2451 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2452 && gui.cursor_col > 0)
2453 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2454 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2455 #ifdef FEAT_HANGULIN
2456 if (composing_hangul)
2457 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2458 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2460 #endif
2461 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2462 * here in case it wasn't needed to undraw it. */
2463 gui.cursor_is_valid = FALSE;
2467 void
2468 gui_redraw(x, y, w, h)
2469 int x;
2470 int y;
2471 int w;
2472 int h;
2474 int row1, col1, row2, col2;
2476 row1 = Y_2_ROW(y);
2477 col1 = X_2_COL(x);
2478 row2 = Y_2_ROW(y + h - 1);
2479 col2 = X_2_COL(x + w - 1);
2481 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2484 * We may need to redraw the cursor, but don't take it upon us to change
2485 * its location after a scroll.
2486 * (maybe be more strict even and test col too?)
2487 * These things may be outside the update/clipping region and reality may
2488 * not reflect Vims internal ideas if these operations are clipped away.
2490 if (gui.row == gui.cursor_row)
2491 gui_update_cursor(TRUE, TRUE);
2495 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2496 * from col1 to col2 (inclusive).
2497 * Return TRUE when the character before the first drawn character has
2498 * different attributes (may have to be redrawn too).
2501 gui_redraw_block(row1, col1, row2, col2, flags)
2502 int row1;
2503 int col1;
2504 int row2;
2505 int col2;
2506 int flags; /* flags for gui_outstr_nowrap() */
2508 int old_row, old_col;
2509 long_u old_hl_mask;
2510 int off;
2511 sattr_T first_attr;
2512 int idx, len;
2513 int back, nback;
2514 int retval = FALSE;
2515 #ifdef FEAT_MBYTE
2516 int orig_col1, orig_col2;
2517 #endif
2519 /* Don't try to update when ScreenLines is not valid */
2520 if (!screen_cleared || ScreenLines == NULL)
2521 return retval;
2523 /* Don't try to draw outside the shell! */
2524 /* Check everything, strange values may be caused by a big border width */
2525 col1 = check_col(col1);
2526 col2 = check_col(col2);
2527 row1 = check_row(row1);
2528 row2 = check_row(row2);
2530 /* Remember where our cursor was */
2531 old_row = gui.row;
2532 old_col = gui.col;
2533 old_hl_mask = gui.highlight_mask;
2534 #ifdef FEAT_MBYTE
2535 orig_col1 = col1;
2536 orig_col2 = col2;
2537 #endif
2539 for (gui.row = row1; gui.row <= row2; gui.row++)
2541 #ifdef FEAT_MBYTE
2542 /* When only half of a double-wide character is in the block, include
2543 * the other half. */
2544 col1 = orig_col1;
2545 col2 = orig_col2;
2546 off = LineOffset[gui.row];
2547 if (enc_dbcs != 0)
2549 if (col1 > 0)
2550 col1 -= dbcs_screen_head_off(ScreenLines + off,
2551 ScreenLines + off + col1);
2552 col2 += dbcs_screen_tail_off(ScreenLines + off,
2553 ScreenLines + off + col2);
2555 else if (enc_utf8)
2557 if (ScreenLines[off + col1] == 0)
2558 --col1;
2559 # ifdef HAVE_GTK2
2560 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2561 ++col2;
2562 # endif
2564 #endif
2565 gui.col = col1;
2566 off = LineOffset[gui.row] + gui.col;
2567 len = col2 - col1 + 1;
2569 /* Find how many chars back this highlighting starts, or where a space
2570 * is. Needed for when the bold trick is used */
2571 for (back = 0; back < col1; ++back)
2572 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2573 || ScreenLines[off - 1 - back] == ' ')
2574 break;
2575 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2576 && ScreenLines[off - 1] != ' ');
2578 /* Break it up in strings of characters with the same attributes. */
2579 /* Print UTF-8 characters individually. */
2580 while (len > 0)
2582 first_attr = ScreenAttrs[off];
2583 gui.highlight_mask = first_attr;
2584 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2585 if (enc_utf8 && ScreenLinesUC[off] != 0)
2587 /* output multi-byte character separately */
2588 nback = gui_screenchar(off, flags,
2589 (guicolor_T)0, (guicolor_T)0, back);
2590 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2591 idx = 2;
2592 else
2593 idx = 1;
2595 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2597 /* output double-byte, single-width character separately */
2598 nback = gui_screenchar(off, flags,
2599 (guicolor_T)0, (guicolor_T)0, back);
2600 idx = 1;
2602 else
2603 #endif
2605 #ifdef HAVE_GTK2
2606 for (idx = 0; idx < len; ++idx)
2608 if (enc_utf8 && ScreenLines[off + idx] == 0)
2609 continue; /* skip second half of double-width char */
2610 if (ScreenAttrs[off + idx] != first_attr)
2611 break;
2613 /* gui_screenstr() takes care of multibyte chars */
2614 nback = gui_screenstr(off, idx, flags,
2615 (guicolor_T)0, (guicolor_T)0, back);
2616 #else
2617 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2618 idx++)
2620 # ifdef FEAT_MBYTE
2621 /* Stop at a multi-byte Unicode character. */
2622 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2623 break;
2624 if (enc_dbcs == DBCS_JPNU)
2626 /* Stop at a double-byte single-width char. */
2627 if (ScreenLines[off + idx] == 0x8e)
2628 break;
2629 if (len > 1 && (*mb_ptr2len)(ScreenLines
2630 + off + idx) == 2)
2631 ++idx; /* skip second byte of double-byte char */
2633 # endif
2635 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2636 (guicolor_T)0, (guicolor_T)0, back);
2637 #endif
2639 if (nback == FAIL)
2641 /* Must back up to start drawing where a bold or italic word
2642 * starts. */
2643 off -= back;
2644 len += back;
2645 gui.col -= back;
2647 else
2649 off += idx;
2650 len -= idx;
2652 back = 0;
2656 /* Put the cursor back where it was */
2657 gui.row = old_row;
2658 gui.col = old_col;
2659 gui.highlight_mask = (int)old_hl_mask;
2661 return retval;
2664 static void
2665 gui_delete_lines(row, count)
2666 int row;
2667 int count;
2669 if (count <= 0)
2670 return;
2672 if (row + count > gui.scroll_region_bot)
2673 /* Scrolled out of region, just blank the lines out */
2674 gui_clear_block(row, gui.scroll_region_left,
2675 gui.scroll_region_bot, gui.scroll_region_right);
2676 else
2678 gui_mch_delete_lines(row, count);
2680 /* If the cursor was in the deleted lines it's now gone. If the
2681 * cursor was in the scrolled lines adjust its position. */
2682 if (gui.cursor_row >= row
2683 && gui.cursor_col >= gui.scroll_region_left
2684 && gui.cursor_col <= gui.scroll_region_right)
2686 if (gui.cursor_row < row + count)
2687 gui.cursor_is_valid = FALSE;
2688 else if (gui.cursor_row <= gui.scroll_region_bot)
2689 gui.cursor_row -= count;
2694 static void
2695 gui_insert_lines(row, count)
2696 int row;
2697 int count;
2699 if (count <= 0)
2700 return;
2702 if (row + count > gui.scroll_region_bot)
2703 /* Scrolled out of region, just blank the lines out */
2704 gui_clear_block(row, gui.scroll_region_left,
2705 gui.scroll_region_bot, gui.scroll_region_right);
2706 else
2708 gui_mch_insert_lines(row, count);
2710 if (gui.cursor_row >= gui.row
2711 && gui.cursor_col >= gui.scroll_region_left
2712 && gui.cursor_col <= gui.scroll_region_right)
2714 if (gui.cursor_row <= gui.scroll_region_bot - count)
2715 gui.cursor_row += count;
2716 else if (gui.cursor_row <= gui.scroll_region_bot)
2717 gui.cursor_is_valid = FALSE;
2723 * The main GUI input routine. Waits for a character from the keyboard.
2724 * wtime == -1 Wait forever.
2725 * wtime == 0 Don't wait.
2726 * wtime > 0 Wait wtime milliseconds for a character.
2727 * Returns OK if a character was found to be available within the given time,
2728 * or FAIL otherwise.
2731 gui_wait_for_chars(wtime)
2732 long wtime;
2734 int retval;
2737 * If we're going to wait a bit, update the menus and mouse shape for the
2738 * current State.
2740 if (wtime != 0)
2742 #ifdef FEAT_MENU
2743 gui_update_menus(0);
2744 #endif
2747 gui_mch_update();
2748 if (input_available()) /* Got char, return immediately */
2749 return OK;
2750 if (wtime == 0) /* Don't wait for char */
2751 return FAIL;
2753 /* Before waiting, flush any output to the screen. */
2754 gui_mch_flush();
2756 if (wtime > 0)
2758 /* Blink when waiting for a character. Probably only does something
2759 * for showmatch() */
2760 gui_mch_start_blink();
2761 retval = gui_mch_wait_for_chars(wtime);
2762 gui_mch_stop_blink();
2763 return retval;
2767 * While we are waiting indefinitely for a character, blink the cursor.
2769 gui_mch_start_blink();
2771 retval = FAIL;
2773 * We may want to trigger the CursorHold event. First wait for
2774 * 'updatetime' and if nothing is typed within that time put the
2775 * K_CURSORHOLD key in the input buffer.
2777 if (gui_mch_wait_for_chars(p_ut) == OK)
2778 retval = OK;
2779 #ifdef FEAT_AUTOCMD
2780 else if (trigger_cursorhold())
2782 char_u buf[3];
2784 /* Put K_CURSORHOLD in the input buffer. */
2785 buf[0] = CSI;
2786 buf[1] = KS_EXTRA;
2787 buf[2] = (int)KE_CURSORHOLD;
2788 add_to_input_buf(buf, 3);
2790 retval = OK;
2792 #endif
2794 if (retval == FAIL)
2796 /* Blocking wait. */
2797 before_blocking();
2798 retval = gui_mch_wait_for_chars(-1L);
2801 gui_mch_stop_blink();
2802 return retval;
2806 * Fill p[4] with mouse coordinates encoded for check_termcode().
2808 static void
2809 fill_mouse_coord(p, col, row)
2810 char_u *p;
2811 int col;
2812 int row;
2814 p[0] = (char_u)(col / 128 + ' ' + 1);
2815 p[1] = (char_u)(col % 128 + ' ' + 1);
2816 p[2] = (char_u)(row / 128 + ' ' + 1);
2817 p[3] = (char_u)(row % 128 + ' ' + 1);
2821 * Generic mouse support function. Add a mouse event to the input buffer with
2822 * the given properties.
2823 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2824 * MOUSE_X1, MOUSE_X2
2825 * MOUSE_DRAG, or MOUSE_RELEASE.
2826 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2827 * x, y --- Coordinates of mouse in pixels.
2828 * repeated_click --- TRUE if this click comes only a short time after a
2829 * previous click.
2830 * modifiers --- Bit field which may be any of the following modifiers
2831 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2832 * This function will ignore drag events where the mouse has not moved to a new
2833 * character.
2835 void
2836 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2837 int button;
2838 int x;
2839 int y;
2840 int repeated_click;
2841 int_u modifiers;
2843 static int prev_row = 0, prev_col = 0;
2844 static int prev_button = -1;
2845 static int num_clicks = 1;
2846 char_u string[10];
2847 enum key_extra button_char;
2848 int row, col;
2849 #ifdef FEAT_CLIPBOARD
2850 int checkfor;
2851 int did_clip = FALSE;
2852 #endif
2855 * Scrolling may happen at any time, also while a selection is present.
2857 switch (button)
2859 case MOUSE_X1:
2860 button_char = KE_X1MOUSE;
2861 goto button_set;
2862 case MOUSE_X2:
2863 button_char = KE_X2MOUSE;
2864 goto button_set;
2865 case MOUSE_4:
2866 button_char = KE_MOUSEDOWN;
2867 goto button_set;
2868 case MOUSE_5:
2869 button_char = KE_MOUSEUP;
2870 button_set:
2872 /* Don't put events in the input queue now. */
2873 if (hold_gui_events)
2874 return;
2876 string[3] = CSI;
2877 string[4] = KS_EXTRA;
2878 string[5] = (int)button_char;
2880 /* Pass the pointer coordinates of the scroll event so that we
2881 * know which window to scroll. */
2882 row = gui_xy2colrow(x, y, &col);
2883 string[6] = (char_u)(col / 128 + ' ' + 1);
2884 string[7] = (char_u)(col % 128 + ' ' + 1);
2885 string[8] = (char_u)(row / 128 + ' ' + 1);
2886 string[9] = (char_u)(row % 128 + ' ' + 1);
2888 if (modifiers == 0)
2889 add_to_input_buf(string + 3, 7);
2890 else
2892 string[0] = CSI;
2893 string[1] = KS_MODIFIER;
2894 string[2] = 0;
2895 if (modifiers & MOUSE_SHIFT)
2896 string[2] |= MOD_MASK_SHIFT;
2897 if (modifiers & MOUSE_CTRL)
2898 string[2] |= MOD_MASK_CTRL;
2899 if (modifiers & MOUSE_ALT)
2900 string[2] |= MOD_MASK_ALT;
2901 add_to_input_buf(string, 10);
2903 return;
2907 #ifdef FEAT_CLIPBOARD
2908 /* If a clipboard selection is in progress, handle it */
2909 if (clip_star.state == SELECT_IN_PROGRESS)
2911 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2912 return;
2915 /* Determine which mouse settings to look for based on the current mode */
2916 switch (get_real_state())
2918 case NORMAL_BUSY:
2919 case OP_PENDING:
2920 case NORMAL: checkfor = MOUSE_NORMAL; break;
2921 case VISUAL: checkfor = MOUSE_VISUAL; break;
2922 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
2923 case REPLACE:
2924 case REPLACE+LANGMAP:
2925 #ifdef FEAT_VREPLACE
2926 case VREPLACE:
2927 case VREPLACE+LANGMAP:
2928 #endif
2929 case INSERT:
2930 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2931 case ASKMORE:
2932 case HITRETURN: /* At the more- and hit-enter prompt pass the
2933 mouse event for a click on or below the
2934 message line. */
2935 if (Y_2_ROW(y) >= msg_row)
2936 checkfor = MOUSE_NORMAL;
2937 else
2938 checkfor = MOUSE_RETURN;
2939 break;
2942 * On the command line, use the clipboard selection on all lines
2943 * but the command line. But not when pasting.
2945 case CMDLINE:
2946 case CMDLINE+LANGMAP:
2947 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2948 checkfor = MOUSE_NONE;
2949 else
2950 checkfor = MOUSE_COMMAND;
2951 break;
2953 default:
2954 checkfor = MOUSE_NONE;
2955 break;
2959 * Allow clipboard selection of text on the command line in "normal"
2960 * modes. Don't do this when dragging the status line, or extending a
2961 * Visual selection.
2963 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2964 && Y_2_ROW(y) >= topframe->fr_height
2965 # ifdef FEAT_WINDOWS
2966 + firstwin->w_winrow
2967 # endif
2968 && button != MOUSE_DRAG
2969 # ifdef FEAT_MOUSESHAPE
2970 && !drag_status_line
2971 # ifdef FEAT_VERTSPLIT
2972 && !drag_sep_line
2973 # endif
2974 # endif
2976 checkfor = MOUSE_NONE;
2979 * Use modeless selection when holding CTRL and SHIFT pressed.
2981 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2982 checkfor = MOUSE_NONEF;
2985 * In Ex mode, always use modeless selection.
2987 if (exmode_active)
2988 checkfor = MOUSE_NONE;
2991 * If the mouse settings say to not use the mouse, use the modeless
2992 * selection. But if Visual is active, assume that only the Visual area
2993 * will be selected.
2994 * Exception: On the command line, both the selection is used and a mouse
2995 * key is send.
2997 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2999 #ifdef FEAT_VISUAL
3000 /* Don't do modeless selection in Visual mode. */
3001 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3002 return;
3003 #endif
3006 * When 'mousemodel' is "popup", shift-left is translated to right.
3007 * But not when also using Ctrl.
3009 if (mouse_model_popup() && button == MOUSE_LEFT
3010 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3012 button = MOUSE_RIGHT;
3013 modifiers &= ~ MOUSE_SHIFT;
3016 /* If the selection is done, allow the right button to extend it.
3017 * If the selection is cleared, allow the right button to start it
3018 * from the cursor position. */
3019 if (button == MOUSE_RIGHT)
3021 if (clip_star.state == SELECT_CLEARED)
3023 if (State & CMDLINE)
3025 col = msg_col;
3026 row = msg_row;
3028 else
3030 col = curwin->w_wcol;
3031 row = curwin->w_wrow + W_WINROW(curwin);
3033 clip_start_selection(col, row, FALSE);
3035 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3036 repeated_click);
3037 did_clip = TRUE;
3039 /* Allow the left button to start the selection */
3040 else if (button ==
3041 # ifdef RISCOS
3042 /* Only start a drag on a drag event. Otherwise
3043 * we don't get a release event. */
3044 MOUSE_DRAG
3045 # else
3046 MOUSE_LEFT
3047 # endif
3050 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3051 did_clip = TRUE;
3053 # ifdef RISCOS
3054 else if (button == MOUSE_LEFT)
3056 clip_clear_selection();
3057 did_clip = TRUE;
3059 # endif
3061 /* Always allow pasting */
3062 if (button != MOUSE_MIDDLE)
3064 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3065 return;
3066 if (checkfor != MOUSE_COMMAND)
3067 button = MOUSE_LEFT;
3069 repeated_click = FALSE;
3072 if (clip_star.state != SELECT_CLEARED && !did_clip)
3073 clip_clear_selection();
3074 #endif
3076 /* Don't put events in the input queue now. */
3077 if (hold_gui_events)
3078 return;
3080 row = gui_xy2colrow(x, y, &col);
3083 * If we are dragging and the mouse hasn't moved far enough to be on a
3084 * different character, then don't send an event to vim.
3086 if (button == MOUSE_DRAG)
3088 if (row == prev_row && col == prev_col)
3089 return;
3090 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3091 if (y < 0)
3092 row = -1;
3096 * If topline has changed (window scrolled) since the last click, reset
3097 * repeated_click, because we don't want starting Visual mode when
3098 * clicking on a different character in the text.
3100 if (curwin->w_topline != gui_prev_topline
3101 #ifdef FEAT_DIFF
3102 || curwin->w_topfill != gui_prev_topfill
3103 #endif
3105 repeated_click = FALSE;
3107 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3108 string[1] = KS_MOUSE;
3109 string[2] = KE_FILLER;
3110 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3112 if (repeated_click)
3115 * Handle multiple clicks. They only count if the mouse is still
3116 * pointing at the same character.
3118 if (button != prev_button || row != prev_row || col != prev_col)
3119 num_clicks = 1;
3120 else if (++num_clicks > 4)
3121 num_clicks = 1;
3123 else
3124 num_clicks = 1;
3125 prev_button = button;
3126 gui_prev_topline = curwin->w_topline;
3127 #ifdef FEAT_DIFF
3128 gui_prev_topfill = curwin->w_topfill;
3129 #endif
3131 string[3] = (char_u)(button | 0x20);
3132 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3134 else
3135 string[3] = (char_u)button;
3137 string[3] |= modifiers;
3138 fill_mouse_coord(string + 4, col, row);
3139 add_to_input_buf(string, 8);
3141 if (row < 0)
3142 prev_row = 0;
3143 else
3144 prev_row = row;
3145 prev_col = col;
3148 * We need to make sure this is cleared since Athena doesn't tell us when
3149 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3151 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3152 gui.dragged_sb = SBAR_NONE;
3153 #endif
3157 * Convert x and y coordinate to column and row in text window.
3158 * Corrects for multi-byte character.
3159 * returns column in "*colp" and row as return value;
3162 gui_xy2colrow(x, y, colp)
3163 int x;
3164 int y;
3165 int *colp;
3167 int col = check_col(X_2_COL(x));
3168 int row = check_row(Y_2_ROW(y));
3170 #ifdef FEAT_MBYTE
3171 *colp = mb_fix_col(col, row);
3172 #else
3173 *colp = col;
3174 #endif
3175 return row;
3178 #if defined(FEAT_MENU) || defined(PROTO)
3180 * Callback function for when a menu entry has been selected.
3182 void
3183 gui_menu_cb(menu)
3184 vimmenu_T *menu;
3186 char_u bytes[sizeof(long_u)];
3188 /* Don't put events in the input queue now. */
3189 if (hold_gui_events)
3190 return;
3192 bytes[0] = CSI;
3193 bytes[1] = KS_MENU;
3194 bytes[2] = KE_FILLER;
3195 add_to_input_buf(bytes, 3);
3196 add_long_to_buf((long_u)menu, bytes);
3197 add_to_input_buf_csi(bytes, sizeof(long_u));
3199 #endif
3201 static int prev_which_scrollbars[3];
3204 * Set which components are present.
3205 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
3206 * in p_go.
3208 /*ARGSUSED*/
3209 void
3210 gui_init_which_components(oldval)
3211 char_u *oldval;
3213 #ifdef FEAT_MENU
3214 static int prev_menu_is_active = -1;
3215 #endif
3216 #ifdef FEAT_TOOLBAR
3217 static int prev_toolbar = -1;
3218 int using_toolbar = FALSE;
3219 #endif
3220 #ifdef FEAT_GUI_TABLINE
3221 int using_tabline;
3222 #endif
3223 #ifdef FEAT_FOOTER
3224 static int prev_footer = -1;
3225 int using_footer = FALSE;
3226 #endif
3227 #if defined(FEAT_MENU) && !defined(WIN16)
3228 static int prev_tearoff = -1;
3229 int using_tearoff = FALSE;
3230 #endif
3232 char_u *p;
3233 int i;
3234 #ifdef FEAT_MENU
3235 int grey_old, grey_new;
3236 char_u *temp;
3237 #endif
3238 win_T *wp;
3239 int need_set_size;
3240 int fix_size;
3242 #ifdef FEAT_MENU
3243 if (oldval != NULL && gui.in_use)
3246 * Check if the menu's go from grey to non-grey or vise versa.
3248 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3249 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3250 if (grey_old != grey_new)
3252 temp = p_go;
3253 p_go = oldval;
3254 gui_update_menus(MENU_ALL_MODES);
3255 p_go = temp;
3258 gui.menu_is_active = FALSE;
3259 #endif
3261 for (i = 0; i < 3; i++)
3262 gui.which_scrollbars[i] = FALSE;
3263 for (p = p_go; *p; p++)
3264 switch (*p)
3266 case GO_LEFT:
3267 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3268 break;
3269 case GO_RIGHT:
3270 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3271 break;
3272 #ifdef FEAT_VERTSPLIT
3273 case GO_VLEFT:
3274 if (win_hasvertsplit())
3275 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3276 break;
3277 case GO_VRIGHT:
3278 if (win_hasvertsplit())
3279 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3280 break;
3281 #endif
3282 case GO_BOT:
3283 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3284 break;
3285 #ifdef FEAT_MENU
3286 case GO_MENUS:
3287 gui.menu_is_active = TRUE;
3288 break;
3289 #endif
3290 case GO_GREY:
3291 /* make menu's have grey items, ignored here */
3292 break;
3293 #ifdef FEAT_TOOLBAR
3294 case GO_TOOLBAR:
3295 using_toolbar = TRUE;
3296 break;
3297 #endif
3298 #ifdef FEAT_FOOTER
3299 case GO_FOOTER:
3300 using_footer = TRUE;
3301 break;
3302 #endif
3303 case GO_TEAROFF:
3304 #if defined(FEAT_MENU) && !defined(WIN16)
3305 using_tearoff = TRUE;
3306 #endif
3307 break;
3308 default:
3309 /* Ignore options that are not supported */
3310 break;
3313 if (gui.in_use)
3315 need_set_size = 0;
3316 fix_size = FALSE;
3318 #ifdef FEAT_GUI_TABLINE
3319 /* Update the GUI tab line, it may appear or disappear. This may
3320 * cause the non-GUI tab line to disappear or appear. */
3321 using_tabline = gui_has_tabline();
3322 if (!gui_mch_showing_tabline() != !using_tabline)
3324 /* We don't want a resize event change "Rows" here, save and
3325 * restore it. Resizing is handled below. */
3326 i = Rows;
3327 gui_update_tabline();
3328 Rows = i;
3329 need_set_size = RESIZE_VERT;
3330 if (using_tabline)
3331 fix_size = TRUE;
3332 if (!gui_use_tabline())
3333 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3335 #endif
3337 for (i = 0; i < 3; i++)
3339 /* The scrollbar needs to be updated when it is shown/unshown and
3340 * when switching tab pages. But the size only changes when it's
3341 * shown/unshown. Thus we need two places to remember whether a
3342 * scrollbar is there or not. */
3343 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3344 #ifdef FEAT_WINDOWS
3345 || gui.which_scrollbars[i]
3346 != curtab->tp_prev_which_scrollbars[i]
3347 #endif
3350 if (i == SBAR_BOTTOM)
3351 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3352 gui.which_scrollbars[i]);
3353 else
3355 FOR_ALL_WINDOWS(wp)
3357 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3360 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3362 if (i == SBAR_BOTTOM)
3363 need_set_size = RESIZE_VERT;
3364 else
3365 need_set_size = RESIZE_HOR;
3366 if (gui.which_scrollbars[i])
3367 fix_size = TRUE;
3370 #ifdef FEAT_WINDOWS
3371 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3372 #endif
3373 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3376 #ifdef FEAT_MENU
3377 if (gui.menu_is_active != prev_menu_is_active)
3379 /* We don't want a resize event change "Rows" here, save and
3380 * restore it. Resizing is handled below. */
3381 i = Rows;
3382 gui_mch_enable_menu(gui.menu_is_active);
3383 Rows = i;
3384 prev_menu_is_active = gui.menu_is_active;
3385 need_set_size = RESIZE_VERT;
3386 if (gui.menu_is_active)
3387 fix_size = TRUE;
3389 #endif
3391 #ifdef FEAT_TOOLBAR
3392 if (using_toolbar != prev_toolbar)
3394 gui_mch_show_toolbar(using_toolbar);
3395 prev_toolbar = using_toolbar;
3396 need_set_size = RESIZE_VERT;
3397 if (using_toolbar)
3398 fix_size = TRUE;
3400 #endif
3401 #ifdef FEAT_FOOTER
3402 if (using_footer != prev_footer)
3404 gui_mch_enable_footer(using_footer);
3405 prev_footer = using_footer;
3406 need_set_size = RESIZE_VERT;
3407 if (using_footer)
3408 fix_size = TRUE;
3410 #endif
3411 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3412 if (using_tearoff != prev_tearoff)
3414 gui_mch_toggle_tearoffs(using_tearoff);
3415 prev_tearoff = using_tearoff;
3417 #endif
3418 if (need_set_size)
3420 #ifdef FEAT_GUI_GTK
3421 long c = Columns;
3422 #endif
3423 /* Adjust the size of the window to make the text area keep the
3424 * same size and to avoid that part of our window is off-screen
3425 * and a scrollbar can't be used, for example. */
3426 gui_set_shellsize(FALSE, fix_size, need_set_size);
3428 #ifdef FEAT_GUI_GTK
3429 /* GTK has the annoying habit of sending us resize events when
3430 * changing the window size ourselves. This mostly happens when
3431 * waiting for a character to arrive, quite unpredictably, and may
3432 * change Columns and Rows when we don't want it. Wait for a
3433 * character here to avoid this effect.
3434 * If you remove this, please test this command for resizing
3435 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3436 * Don't do this while starting up though.
3437 * And don't change Rows, it may have be reduced intentionally
3438 * when adding menu/toolbar/tabline. */
3439 if (!gui.starting)
3440 (void)char_avail();
3441 Columns = c;
3442 #endif
3444 #ifdef FEAT_WINDOWS
3445 /* When the console tabline appears or disappears the window positions
3446 * change. */
3447 if (firstwin->w_winrow != tabline_height())
3448 shell_new_rows(); /* recompute window positions and heights */
3449 #endif
3453 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3455 * Return TRUE if the GUI is taking care of the tabline.
3456 * It may still be hidden if 'showtabline' is zero.
3459 gui_use_tabline()
3461 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3465 * Return TRUE if the GUI is showing the tabline.
3466 * This uses 'showtabline'.
3468 static int
3469 gui_has_tabline()
3471 if (!gui_use_tabline()
3472 || p_stal == 0
3473 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3474 return FALSE;
3475 return TRUE;
3479 * Update the tabline.
3480 * This may display/undisplay the tabline and update the labels.
3482 void
3483 gui_update_tabline()
3485 int showit = gui_has_tabline();
3486 int shown = gui_mch_showing_tabline();
3488 if (!gui.starting && starting == 0)
3490 /* Updating the tabline uses direct GUI commands, flush
3491 * outstanding instructions first. (esp. clear screen) */
3492 out_flush();
3493 gui_mch_flush();
3495 if (!showit != !shown)
3496 gui_mch_show_tabline(showit);
3497 if (showit != 0)
3498 gui_mch_update_tabline();
3500 /* When the tabs change from hidden to shown or from shown to
3501 * hidden the size of the text area should remain the same. */
3502 if (!showit != !shown)
3503 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
3508 * Get the label or tooltip for tab page "tp" into NameBuff[].
3510 void
3511 get_tabline_label(tp, tooltip)
3512 tabpage_T *tp;
3513 int tooltip; /* TRUE: get tooltip */
3515 int modified = FALSE;
3516 char_u buf[40];
3517 int wincount;
3518 win_T *wp;
3519 char_u **opt;
3521 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
3522 opt = (tooltip ? &p_gtt : &p_gtl);
3523 if (**opt != NUL)
3525 int use_sandbox = FALSE;
3526 int save_called_emsg = called_emsg;
3527 char_u res[MAXPATHL];
3528 tabpage_T *save_curtab;
3529 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3530 : "guitablabel");
3532 called_emsg = FALSE;
3534 printer_page_num = tabpage_index(tp);
3535 # ifdef FEAT_EVAL
3536 set_vim_var_nr(VV_LNUM, printer_page_num);
3537 use_sandbox = was_set_insecurely(opt_name, 0);
3538 # endif
3539 /* It's almost as going to the tabpage, but without autocommands. */
3540 curtab->tp_firstwin = firstwin;
3541 curtab->tp_lastwin = lastwin;
3542 curtab->tp_curwin = curwin;
3543 save_curtab = curtab;
3544 curtab = tp;
3545 topframe = curtab->tp_topframe;
3546 firstwin = curtab->tp_firstwin;
3547 lastwin = curtab->tp_lastwin;
3548 curwin = curtab->tp_curwin;
3549 curbuf = curwin->w_buffer;
3551 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3552 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
3553 0, (int)Columns, NULL, NULL);
3554 STRCPY(NameBuff, res);
3556 /* Back to the original curtab. */
3557 curtab = save_curtab;
3558 topframe = curtab->tp_topframe;
3559 firstwin = curtab->tp_firstwin;
3560 lastwin = curtab->tp_lastwin;
3561 curwin = curtab->tp_curwin;
3562 curbuf = curwin->w_buffer;
3564 if (called_emsg)
3565 set_string_option_direct(opt_name, -1,
3566 (char_u *)"", OPT_FREE, SID_ERROR);
3567 called_emsg |= save_called_emsg;
3570 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3571 * use a default label. */
3572 if (**opt == NUL || *NameBuff == NUL)
3574 /* Get the buffer name into NameBuff[] and shorten it. */
3575 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3576 if (!tooltip)
3577 shorten_dir(NameBuff);
3579 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3580 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3581 if (bufIsChanged(wp->w_buffer))
3582 modified = TRUE;
3583 if (modified || wincount > 1)
3585 if (wincount > 1)
3586 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3587 else
3588 buf[0] = NUL;
3589 if (modified)
3590 STRCAT(buf, "+");
3591 STRCAT(buf, " ");
3592 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3593 mch_memmove(NameBuff, buf, STRLEN(buf));
3599 * Send the event for clicking to select tab page "nr".
3600 * Returns TRUE if it was done, FALSE when skipped because we are already at
3601 * that tab page or the cmdline window is open.
3604 send_tabline_event(nr)
3605 int nr;
3607 char_u string[3];
3609 if (nr == tabpage_index(curtab))
3610 return FALSE;
3612 /* Don't put events in the input queue now. */
3613 if (hold_gui_events
3614 # ifdef FEAT_CMDWIN
3615 || cmdwin_type != 0
3616 # endif
3619 /* Set it back to the current tab page. */
3620 gui_mch_set_curtab(tabpage_index(curtab));
3621 return FALSE;
3624 string[0] = CSI;
3625 string[1] = KS_TABLINE;
3626 string[2] = KE_FILLER;
3627 add_to_input_buf(string, 3);
3628 string[0] = nr;
3629 add_to_input_buf_csi(string, 1);
3630 return TRUE;
3634 * Send a tabline menu event
3636 void
3637 send_tabline_menu_event(tabidx, event)
3638 int tabidx;
3639 int event;
3641 char_u string[3];
3643 /* Don't put events in the input queue now. */
3644 if (hold_gui_events)
3645 return;
3647 string[0] = CSI;
3648 string[1] = KS_TABMENU;
3649 string[2] = KE_FILLER;
3650 add_to_input_buf(string, 3);
3651 string[0] = tabidx;
3652 string[1] = (char_u)(long)event;
3653 add_to_input_buf_csi(string, 2);
3656 #endif
3659 * Scrollbar stuff:
3662 #if defined(FEAT_WINDOWS) || defined(PROTO)
3664 * Remove all scrollbars. Used before switching to another tab page.
3666 void
3667 gui_remove_scrollbars()
3669 int i;
3670 win_T *wp;
3672 for (i = 0; i < 3; i++)
3674 if (i == SBAR_BOTTOM)
3675 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3676 else
3678 FOR_ALL_WINDOWS(wp)
3680 gui_do_scrollbar(wp, i, FALSE);
3683 curtab->tp_prev_which_scrollbars[i] = -1;
3686 #endif
3688 void
3689 gui_create_scrollbar(sb, type, wp)
3690 scrollbar_T *sb;
3691 int type;
3692 win_T *wp;
3694 static int sbar_ident = 0;
3696 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3697 sb->wp = wp;
3698 sb->type = type;
3699 sb->value = 0;
3700 #ifdef FEAT_GUI_ATHENA
3701 sb->pixval = 0;
3702 #endif
3703 sb->size = 1;
3704 sb->max = 1;
3705 sb->top = 0;
3706 sb->height = 0;
3707 #ifdef FEAT_VERTSPLIT
3708 sb->width = 0;
3709 #endif
3710 sb->status_height = 0;
3711 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3715 * Find the scrollbar with the given index.
3717 scrollbar_T *
3718 gui_find_scrollbar(ident)
3719 long ident;
3721 win_T *wp;
3723 if (gui.bottom_sbar.ident == ident)
3724 return &gui.bottom_sbar;
3725 FOR_ALL_WINDOWS(wp)
3727 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3728 return &wp->w_scrollbars[SBAR_LEFT];
3729 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3730 return &wp->w_scrollbars[SBAR_RIGHT];
3732 return NULL;
3736 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3738 * For Win32, Macintosh and GTK+ 2:
3739 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3740 * you stop dragging the scrollbar. We get here each time the scrollbar is
3741 * dragged another pixel, but as far as the rest of vim goes, it thinks
3742 * we're just hanging in the call to DispatchMessage() in
3743 * process_message(). The DispatchMessage() call that hangs was passed a
3744 * mouse button click event in the scrollbar window. -- webb.
3746 * Solution: Do the scrolling right here. But only when allowed.
3747 * Ignore the scrollbars while executing an external command or when there
3748 * are still characters to be processed.
3750 void
3751 gui_drag_scrollbar(sb, value, still_dragging)
3752 scrollbar_T *sb;
3753 long value;
3754 int still_dragging;
3756 #ifdef FEAT_WINDOWS
3757 win_T *wp;
3758 #endif
3759 int sb_num;
3760 #ifdef USE_ON_FLY_SCROLL
3761 colnr_T old_leftcol = curwin->w_leftcol;
3762 # ifdef FEAT_SCROLLBIND
3763 linenr_T old_topline = curwin->w_topline;
3764 # endif
3765 # ifdef FEAT_DIFF
3766 int old_topfill = curwin->w_topfill;
3767 # endif
3768 #else
3769 char_u bytes[sizeof(long_u)];
3770 int byte_count;
3771 #endif
3773 if (sb == NULL)
3774 return;
3776 /* Don't put events in the input queue now. */
3777 if (hold_gui_events)
3778 return;
3780 #ifdef FEAT_CMDWIN
3781 if (cmdwin_type != 0 && sb->wp != curwin)
3782 return;
3783 #endif
3785 if (still_dragging)
3787 if (sb->wp == NULL)
3788 gui.dragged_sb = SBAR_BOTTOM;
3789 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3790 gui.dragged_sb = SBAR_LEFT;
3791 else
3792 gui.dragged_sb = SBAR_RIGHT;
3793 gui.dragged_wp = sb->wp;
3795 else
3797 gui.dragged_sb = SBAR_NONE;
3798 #ifdef HAVE_GTK2
3799 /* Keep the "dragged_wp" value until after the scrolling, for when the
3800 * moust button is released. GTK2 doesn't send the button-up event. */
3801 gui.dragged_wp = NULL;
3802 #endif
3805 /* Vertical sbar info is kept in the first sbar (the left one) */
3806 if (sb->wp != NULL)
3807 sb = &sb->wp->w_scrollbars[0];
3810 * Check validity of value
3812 if (value < 0)
3813 value = 0;
3814 #ifdef SCROLL_PAST_END
3815 else if (value > sb->max)
3816 value = sb->max;
3817 #else
3818 if (value > sb->max - sb->size + 1)
3819 value = sb->max - sb->size + 1;
3820 #endif
3822 sb->value = value;
3824 #ifdef USE_ON_FLY_SCROLL
3825 /* When not allowed to do the scrolling right now, return.
3826 * This also checked input_available(), but that causes the first click in
3827 * a scrollbar to be ignored when Vim doesn't have focus. */
3828 if (dont_scroll)
3829 return;
3830 #endif
3831 #ifdef FEAT_INS_EXPAND
3832 /* Disallow scrolling the current window when the completion popup menu is
3833 * visible. */
3834 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3835 return;
3836 #endif
3838 #ifdef FEAT_RIGHTLEFT
3839 if (sb->wp == NULL && curwin->w_p_rl)
3841 value = sb->max + 1 - sb->size - value;
3842 if (value < 0)
3843 value = 0;
3845 #endif
3847 if (sb->wp != NULL) /* vertical scrollbar */
3849 sb_num = 0;
3850 #ifdef FEAT_WINDOWS
3851 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3852 sb_num++;
3853 if (wp == NULL)
3854 return;
3855 #else
3856 if (sb->wp != curwin)
3857 return;
3858 #endif
3860 #ifdef USE_ON_FLY_SCROLL
3861 current_scrollbar = sb_num;
3862 scrollbar_value = value;
3863 if (State & NORMAL)
3865 gui_do_scroll();
3866 setcursor();
3868 else if (State & INSERT)
3870 ins_scroll();
3871 setcursor();
3873 else if (State & CMDLINE)
3875 if (msg_scrolled == 0)
3877 gui_do_scroll();
3878 redrawcmdline();
3881 # ifdef FEAT_FOLDING
3882 /* Value may have been changed for closed fold. */
3883 sb->value = sb->wp->w_topline - 1;
3884 # endif
3886 /* When dragging one scrollbar and there is another one at the other
3887 * side move the thumb of that one too. */
3888 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3889 gui_mch_set_scrollbar_thumb(
3890 &sb->wp->w_scrollbars[
3891 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3892 ? SBAR_LEFT : SBAR_RIGHT],
3893 sb->value, sb->size, sb->max);
3895 #else
3896 bytes[0] = CSI;
3897 bytes[1] = KS_VER_SCROLLBAR;
3898 bytes[2] = KE_FILLER;
3899 bytes[3] = (char_u)sb_num;
3900 byte_count = 4;
3901 #endif
3903 else
3905 #ifdef USE_ON_FLY_SCROLL
3906 scrollbar_value = value;
3908 if (State & NORMAL)
3909 gui_do_horiz_scroll();
3910 else if (State & INSERT)
3911 ins_horscroll();
3912 else if (State & CMDLINE)
3914 if (msg_scrolled == 0)
3916 gui_do_horiz_scroll();
3917 redrawcmdline();
3920 if (old_leftcol != curwin->w_leftcol)
3922 updateWindow(curwin); /* update window, status and cmdline */
3923 setcursor();
3925 #else
3926 bytes[0] = CSI;
3927 bytes[1] = KS_HOR_SCROLLBAR;
3928 bytes[2] = KE_FILLER;
3929 byte_count = 3;
3930 #endif
3933 #ifdef USE_ON_FLY_SCROLL
3934 # ifdef FEAT_SCROLLBIND
3936 * synchronize other windows, as necessary according to 'scrollbind'
3938 if (curwin->w_p_scb
3939 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3940 || (sb->wp == curwin && (curwin->w_topline != old_topline
3941 # ifdef FEAT_DIFF
3942 || curwin->w_topfill != old_topfill
3943 # endif
3944 ))))
3946 do_check_scrollbind(TRUE);
3947 /* need to update the window right here */
3948 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3949 if (wp->w_redr_type > 0)
3950 updateWindow(wp);
3951 setcursor();
3953 # endif
3954 out_flush();
3955 gui_update_cursor(FALSE, TRUE);
3956 #else
3957 add_to_input_buf(bytes, byte_count);
3958 add_long_to_buf((long_u)value, bytes);
3959 add_to_input_buf_csi(bytes, sizeof(long_u));
3960 #endif
3964 * Scrollbar stuff:
3967 void
3968 gui_update_scrollbars(force)
3969 int force; /* Force all scrollbars to get updated */
3971 win_T *wp;
3972 scrollbar_T *sb;
3973 long val, size, max; /* need 32 bits here */
3974 int which_sb;
3975 int h, y;
3976 #ifdef FEAT_VERTSPLIT
3977 static win_T *prev_curwin = NULL;
3978 #endif
3980 /* Update the horizontal scrollbar */
3981 gui_update_horiz_scrollbar(force);
3983 #ifndef WIN3264
3984 /* Return straight away if there is neither a left nor right scrollbar.
3985 * On MS-Windows this is required anyway for scrollwheel messages. */
3986 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3987 return;
3988 #endif
3991 * Don't want to update a scrollbar while we're dragging it. But if we
3992 * have both a left and right scrollbar, and we drag one of them, we still
3993 * need to update the other one.
3995 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3996 && gui.which_scrollbars[SBAR_LEFT]
3997 && gui.which_scrollbars[SBAR_RIGHT])
4000 * If we have two scrollbars and one of them is being dragged, just
4001 * copy the scrollbar position from the dragged one to the other one.
4003 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4004 if (gui.dragged_wp != NULL)
4005 gui_mch_set_scrollbar_thumb(
4006 &gui.dragged_wp->w_scrollbars[which_sb],
4007 gui.dragged_wp->w_scrollbars[0].value,
4008 gui.dragged_wp->w_scrollbars[0].size,
4009 gui.dragged_wp->w_scrollbars[0].max);
4012 /* avoid that moving components around generates events */
4013 ++hold_gui_events;
4015 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
4017 if (wp->w_buffer == NULL) /* just in case */
4018 continue;
4019 /* Skip a scrollbar that is being dragged. */
4020 if (!force && (gui.dragged_sb == SBAR_LEFT
4021 || gui.dragged_sb == SBAR_RIGHT)
4022 && gui.dragged_wp == wp)
4023 continue;
4025 #ifdef SCROLL_PAST_END
4026 max = wp->w_buffer->b_ml.ml_line_count - 1;
4027 #else
4028 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4029 #endif
4030 if (max < 0) /* empty buffer */
4031 max = 0;
4032 val = wp->w_topline - 1;
4033 size = wp->w_height;
4034 #ifdef SCROLL_PAST_END
4035 if (val > max) /* just in case */
4036 val = max;
4037 #else
4038 if (size > max + 1) /* just in case */
4039 size = max + 1;
4040 if (val > max - size + 1)
4041 val = max - size + 1;
4042 #endif
4043 if (val < 0) /* minimal value is 0 */
4044 val = 0;
4047 * Scrollbar at index 0 (the left one) contains all the information.
4048 * It would be the same info for left and right so we just store it for
4049 * one of them.
4051 sb = &wp->w_scrollbars[0];
4054 * Note: no check for valid w_botline. If it's not valid the
4055 * scrollbars will be updated later anyway.
4057 if (size < 1 || wp->w_botline - 2 > max)
4060 * This can happen during changing files. Just don't update the
4061 * scrollbar for now.
4063 sb->height = 0; /* Force update next time */
4064 if (gui.which_scrollbars[SBAR_LEFT])
4065 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4066 if (gui.which_scrollbars[SBAR_RIGHT])
4067 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4068 continue;
4070 if (force || sb->height != wp->w_height
4071 #ifdef FEAT_WINDOWS
4072 || sb->top != wp->w_winrow
4073 || sb->status_height != wp->w_status_height
4074 # ifdef FEAT_VERTSPLIT
4075 || sb->width != wp->w_width
4076 || prev_curwin != curwin
4077 # endif
4078 #endif
4081 /* Height, width or position of scrollbar has changed. For
4082 * vertical split: curwin changed. */
4083 sb->height = wp->w_height;
4084 #ifdef FEAT_WINDOWS
4085 sb->top = wp->w_winrow;
4086 sb->status_height = wp->w_status_height;
4087 # ifdef FEAT_VERTSPLIT
4088 sb->width = wp->w_width;
4089 # endif
4090 #endif
4092 /* Calculate height and position in pixels */
4093 h = (sb->height + sb->status_height) * gui.char_height;
4094 y = sb->top * gui.char_height + gui.border_offset;
4095 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4096 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MACVIM))
4097 if (gui.menu_is_active)
4098 y += gui.menu_height;
4099 #endif
4101 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4102 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4103 # ifdef FEAT_GUI_ATHENA
4104 y += gui.toolbar_height;
4105 # else
4106 # ifdef FEAT_GUI_MSWIN
4107 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4108 # endif
4109 # endif
4110 #endif
4112 #if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4113 if (gui_has_tabline())
4114 y += gui.tabline_height;
4115 #endif
4117 #ifdef FEAT_WINDOWS
4118 if (wp->w_winrow == 0)
4119 #endif
4121 /* Height of top scrollbar includes width of top border */
4122 h += gui.border_offset;
4123 y -= gui.border_offset;
4125 if (gui.which_scrollbars[SBAR_LEFT])
4127 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4128 gui.left_sbar_x, y,
4129 gui.scrollbar_width, h);
4130 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4132 if (gui.which_scrollbars[SBAR_RIGHT])
4134 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4135 gui.right_sbar_x, y,
4136 gui.scrollbar_width, h);
4137 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4141 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4142 * checking if the thumb moved at least a pixel. Only do this for
4143 * Athena, most other GUIs require the update anyway to make the
4144 * arrows work. */
4145 #ifdef FEAT_GUI_ATHENA
4146 if (max == 0)
4147 y = 0;
4148 else
4149 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4150 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4151 #else
4152 if (force || sb->value != val || sb->size != size || sb->max != max)
4153 #endif
4155 /* Thumb of scrollbar has moved */
4156 sb->value = val;
4157 #ifdef FEAT_GUI_ATHENA
4158 sb->pixval = y;
4159 #endif
4160 sb->size = size;
4161 sb->max = max;
4162 if (gui.which_scrollbars[SBAR_LEFT]
4163 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
4164 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4165 val, size, max);
4166 if (gui.which_scrollbars[SBAR_RIGHT]
4167 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
4168 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4169 val, size, max);
4172 #ifdef FEAT_VERTSPLIT
4173 prev_curwin = curwin;
4174 #endif
4175 --hold_gui_events;
4179 * Enable or disable a scrollbar.
4180 * Check for scrollbars for vertically split windows which are not enabled
4181 * sometimes.
4183 static void
4184 gui_do_scrollbar(wp, which, enable)
4185 win_T *wp;
4186 int which; /* SBAR_LEFT or SBAR_RIGHT */
4187 int enable; /* TRUE to enable scrollbar */
4189 #ifdef FEAT_VERTSPLIT
4190 int midcol = curwin->w_wincol + curwin->w_width / 2;
4191 int has_midcol = (wp->w_wincol <= midcol
4192 && wp->w_wincol + wp->w_width >= midcol);
4194 /* Only enable scrollbars that contain the middle column of the current
4195 * window. */
4196 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4198 /* Scrollbars only on one side. Don't enable scrollbars that don't
4199 * contain the middle column of the current window. */
4200 if (!has_midcol)
4201 enable = FALSE;
4203 else
4205 /* Scrollbars on both sides. Don't enable scrollbars that neither
4206 * contain the middle column of the current window nor are on the far
4207 * side. */
4208 if (midcol > Columns / 2)
4210 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4211 enable = FALSE;
4213 else
4215 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4216 : !has_midcol)
4217 enable = FALSE;
4220 #endif
4221 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4225 * Scroll a window according to the values set in the globals current_scrollbar
4226 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4227 * or FALSE otherwise.
4230 gui_do_scroll()
4232 win_T *wp, *save_wp;
4233 int i;
4234 long nlines;
4235 pos_T old_cursor;
4236 linenr_T old_topline;
4237 #ifdef FEAT_DIFF
4238 int old_topfill;
4239 #endif
4241 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4242 if (wp == NULL)
4243 break;
4244 if (wp == NULL)
4245 /* Couldn't find window */
4246 return FALSE;
4249 * Compute number of lines to scroll. If zero, nothing to do.
4251 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4252 if (nlines == 0)
4253 return FALSE;
4255 save_wp = curwin;
4256 old_topline = wp->w_topline;
4257 #ifdef FEAT_DIFF
4258 old_topfill = wp->w_topfill;
4259 #endif
4260 old_cursor = wp->w_cursor;
4261 curwin = wp;
4262 curbuf = wp->w_buffer;
4263 if (nlines < 0)
4264 scrolldown(-nlines, gui.dragged_wp == NULL);
4265 else
4266 scrollup(nlines, gui.dragged_wp == NULL);
4267 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4268 * the mouse-up event already, but we still want it to behave like when
4269 * dragging. But not the next click in an arrow. */
4270 if (gui.dragged_sb == SBAR_NONE)
4271 gui.dragged_wp = NULL;
4273 if (old_topline != wp->w_topline
4274 #ifdef FEAT_DIFF
4275 || old_topfill != wp->w_topfill
4276 #endif
4279 if (p_so != 0)
4281 cursor_correct(); /* fix window for 'so' */
4282 update_topline(); /* avoid up/down jump */
4284 if (old_cursor.lnum != wp->w_cursor.lnum)
4285 coladvance(wp->w_curswant);
4286 #ifdef FEAT_SCROLLBIND
4287 wp->w_scbind_pos = wp->w_topline;
4288 #endif
4291 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4292 validate_cursor();
4294 curwin = save_wp;
4295 curbuf = save_wp->w_buffer;
4298 * Don't call updateWindow() when nothing has changed (it will overwrite
4299 * the status line!).
4301 if (old_topline != wp->w_topline
4302 || wp->w_redr_type != 0
4303 #ifdef FEAT_DIFF
4304 || old_topfill != wp->w_topfill
4305 #endif
4308 int type = VALID;
4310 #ifdef FEAT_INS_EXPAND
4311 if (pum_visible())
4313 type = NOT_VALID;
4314 wp->w_lines_valid = 0;
4316 #endif
4317 /* Don't set must_redraw here, it may cause the popup menu to
4318 * disappear when losing focus after a scrollbar drag. */
4319 if (wp->w_redr_type < type)
4320 wp->w_redr_type = type;
4321 updateWindow(wp); /* update window, status line, and cmdline */
4324 #ifdef FEAT_INS_EXPAND
4325 /* May need to redraw the popup menu. */
4326 if (pum_visible())
4327 pum_redraw();
4328 #endif
4330 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4335 * Horizontal scrollbar stuff:
4339 * Return length of line "lnum" for horizontal scrolling.
4341 static colnr_T
4342 scroll_line_len(lnum)
4343 linenr_T lnum;
4345 char_u *p;
4346 colnr_T col;
4347 int w;
4349 p = ml_get(lnum);
4350 col = 0;
4351 if (*p != NUL)
4352 for (;;)
4354 w = chartabsize(p, col);
4355 mb_ptr_adv(p);
4356 if (*p == NUL) /* don't count the last character */
4357 break;
4358 col += w;
4360 return col;
4363 /* Remember which line is currently the longest, so that we don't have to
4364 * search for it when scrolling horizontally. */
4365 static linenr_T longest_lnum = 0;
4367 static void
4368 gui_update_horiz_scrollbar(force)
4369 int force;
4371 long value, size, max; /* need 32 bit ints here */
4373 if (!gui.which_scrollbars[SBAR_BOTTOM])
4374 return;
4376 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4377 return;
4379 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4380 return;
4383 * It is possible for the cursor to be invalid if we're in the middle of
4384 * something (like changing files). If so, don't do anything for now.
4386 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4388 gui.bottom_sbar.value = -1;
4389 return;
4392 size = W_WIDTH(curwin);
4393 if (curwin->w_p_wrap)
4395 value = 0;
4396 #ifdef SCROLL_PAST_END
4397 max = 0;
4398 #else
4399 max = W_WIDTH(curwin) - 1;
4400 #endif
4402 else
4404 value = curwin->w_leftcol;
4406 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4407 * line numbers, topline and botline can be invalid when displaying is
4408 * postponed. */
4409 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4410 && curwin->w_topline <= curwin->w_cursor.lnum
4411 && curwin->w_botline > curwin->w_cursor.lnum
4412 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4414 linenr_T lnum;
4415 colnr_T n;
4417 /* Use maximum of all visible lines. Remember the lnum of the
4418 * longest line, clostest to the cursor line. Used when scrolling
4419 * below. */
4420 max = 0;
4421 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4423 n = scroll_line_len(lnum);
4424 if (n > (colnr_T)max)
4426 max = n;
4427 longest_lnum = lnum;
4429 else if (n == (colnr_T)max
4430 && abs((int)(lnum - curwin->w_cursor.lnum))
4431 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4432 longest_lnum = lnum;
4435 else
4436 /* Use cursor line only. */
4437 max = scroll_line_len(curwin->w_cursor.lnum);
4438 #ifdef FEAT_VIRTUALEDIT
4439 if (virtual_active())
4441 /* May move the cursor even further to the right. */
4442 if (curwin->w_virtcol >= (colnr_T)max)
4443 max = curwin->w_virtcol;
4445 #endif
4447 #ifndef SCROLL_PAST_END
4448 max += W_WIDTH(curwin) - 1;
4449 #endif
4450 /* The line number isn't scrolled, thus there is less space when
4451 * 'number' is set (also for 'foldcolumn'). */
4452 size -= curwin_col_off();
4453 #ifndef SCROLL_PAST_END
4454 max -= curwin_col_off();
4455 #endif
4458 #ifndef SCROLL_PAST_END
4459 if (value > max - size + 1)
4460 value = max - size + 1; /* limit the value to allowable range */
4461 #endif
4463 #ifdef FEAT_RIGHTLEFT
4464 if (curwin->w_p_rl)
4466 value = max + 1 - size - value;
4467 if (value < 0)
4469 size += value;
4470 value = 0;
4473 #endif
4474 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4475 && max == gui.bottom_sbar.max)
4476 return;
4478 gui.bottom_sbar.value = value;
4479 gui.bottom_sbar.size = size;
4480 gui.bottom_sbar.max = max;
4481 gui.prev_wrap = curwin->w_p_wrap;
4483 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4487 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4490 gui_do_horiz_scroll()
4492 /* no wrapping, no scrolling */
4493 if (curwin->w_p_wrap)
4494 return FALSE;
4496 if (curwin->w_leftcol == scrollbar_value)
4497 return FALSE;
4499 curwin->w_leftcol = (colnr_T)scrollbar_value;
4501 /* When the line of the cursor is too short, move the cursor to the
4502 * longest visible line. Do a sanity check on "longest_lnum", just in
4503 * case. */
4504 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4505 && longest_lnum >= curwin->w_topline
4506 && longest_lnum < curwin->w_botline
4507 && !virtual_active())
4509 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4511 curwin->w_cursor.lnum = longest_lnum;
4512 curwin->w_cursor.col = 0;
4516 return leftcol_changed();
4520 * Check that none of the colors are the same as the background color
4522 void
4523 gui_check_colors()
4525 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4527 gui_set_bg_color((char_u *)"White");
4528 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4529 gui_set_fg_color((char_u *)"Black");
4533 static void
4534 gui_set_fg_color(name)
4535 char_u *name;
4537 gui.norm_pixel = gui_get_color(name);
4538 hl_set_fg_color_name(vim_strsave(name));
4541 static void
4542 gui_set_bg_color(name)
4543 char_u *name;
4545 gui.back_pixel = gui_get_color(name);
4546 hl_set_bg_color_name(vim_strsave(name));
4550 * Allocate a color by name.
4551 * Returns INVALCOLOR and gives an error message when failed.
4553 guicolor_T
4554 gui_get_color(name)
4555 char_u *name;
4557 guicolor_T t;
4559 if (*name == NUL)
4560 return INVALCOLOR;
4561 t = gui_mch_get_color(name);
4563 if (t == INVALCOLOR
4564 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4565 && gui.in_use
4566 #endif
4568 EMSG2(_("E254: Cannot allocate color %s"), name);
4569 return t;
4573 * Return the grey value of a color (range 0-255).
4576 gui_get_lightness(pixel)
4577 guicolor_T pixel;
4579 long_u rgb = gui_mch_get_rgb(pixel);
4581 return (int)( (((rgb >> 16) & 0xff) * 299)
4582 + (((rgb >> 8) & 0xff) * 587)
4583 + ((rgb & 0xff) * 114)) / 1000;
4586 #if defined(FEAT_GUI_X11) || defined(PROTO)
4587 void
4588 gui_new_scrollbar_colors()
4590 win_T *wp;
4592 /* Nothing to do if GUI hasn't started yet. */
4593 if (!gui.in_use)
4594 return;
4596 FOR_ALL_WINDOWS(wp)
4598 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4599 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4601 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4603 #endif
4606 * Call this when focus has changed.
4608 void
4609 gui_focus_change(in_focus)
4610 int in_focus;
4613 * Skip this code to avoid drawing the cursor when debugging and switching
4614 * between the debugger window and gvim.
4616 #if 1
4617 gui.in_focus = in_focus;
4618 out_flush(); /* make sure output has been written */
4619 gui_update_cursor(TRUE, FALSE);
4621 # ifdef FEAT_XIM
4622 xim_set_focus(in_focus);
4623 # endif
4625 /* Put events in the input queue only when allowed.
4626 * ui_focus_change() isn't called directly, because it invokes
4627 * autocommands and that must not happen asynchronously. */
4628 if (!hold_gui_events)
4630 char_u bytes[3];
4632 bytes[0] = CSI;
4633 bytes[1] = KS_EXTRA;
4634 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4635 add_to_input_buf(bytes, 3);
4637 #endif
4641 * Called when the mouse moved (but not when dragging).
4643 void
4644 gui_mouse_moved(x, y)
4645 int x;
4646 int y;
4648 win_T *wp;
4649 char_u st[8];
4651 /* Ignore this while still starting up. */
4652 if (!gui.in_use || gui.starting)
4653 return;
4655 #ifdef FEAT_MOUSESHAPE
4656 /* Get window pointer, and update mouse shape as well. */
4657 wp = xy2win(x, y);
4658 #endif
4660 /* Only handle this when 'mousefocus' set and ... */
4661 if (p_mousef
4662 && !hold_gui_events /* not holding events */
4663 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4664 && State != HITRETURN /* but not hit-return prompt */
4665 && msg_scrolled == 0 /* no scrolled message */
4666 && !need_mouse_correct /* not moving the pointer */
4667 && gui.in_focus) /* gvim in focus */
4669 /* Don't move the mouse when it's left or right of the Vim window */
4670 if (x < 0 || x > Columns * gui.char_width)
4671 return;
4672 #ifndef FEAT_MOUSESHAPE
4673 wp = xy2win(x, y);
4674 #endif
4675 if (wp == curwin || wp == NULL)
4676 return; /* still in the same old window, or none at all */
4678 #ifdef FEAT_WINDOWS
4679 /* Ignore position in the tab pages line. */
4680 if (Y_2_ROW(y) < tabline_height())
4681 return;
4682 #endif
4685 * format a mouse click on status line input
4686 * ala gui_send_mouse_event(0, x, y, 0, 0);
4687 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4688 * generate a K_LEFTMOUSE_NM key code.
4690 if (finish_op)
4692 /* abort the current operator first */
4693 st[0] = ESC;
4694 add_to_input_buf(st, 1);
4696 st[0] = CSI;
4697 st[1] = KS_MOUSE;
4698 st[2] = KE_FILLER;
4699 st[3] = (char_u)MOUSE_LEFT;
4700 fill_mouse_coord(st + 4,
4701 #ifdef FEAT_VERTSPLIT
4702 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
4703 #else
4705 #endif
4706 wp->w_height + W_WINROW(wp));
4708 add_to_input_buf(st, 8);
4709 st[3] = (char_u)MOUSE_RELEASE;
4710 add_to_input_buf(st, 8);
4711 #ifdef FEAT_GUI_GTK
4712 /* Need to wake up the main loop */
4713 if (gtk_main_level() > 0)
4714 gtk_main_quit();
4715 #endif
4720 * Called when mouse should be moved to window with focus.
4722 void
4723 gui_mouse_correct()
4725 int x, y;
4726 win_T *wp = NULL;
4728 need_mouse_correct = FALSE;
4730 if (!(gui.in_use && p_mousef))
4731 return;
4733 gui_mch_getmouse(&x, &y);
4734 /* Don't move the mouse when it's left or right of the Vim window */
4735 if (x < 0 || x > Columns * gui.char_width)
4736 return;
4737 if (y >= 0
4738 # ifdef FEAT_WINDOWS
4739 && Y_2_ROW(y) >= tabline_height()
4740 # endif
4742 wp = xy2win(x, y);
4743 if (wp != curwin && wp != NULL) /* If in other than current window */
4745 validate_cline_row();
4746 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4747 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4748 + (gui.char_height) / 2);
4753 * Find window where the mouse pointer "y" coordinate is in.
4755 /*ARGSUSED*/
4756 static win_T *
4757 xy2win(x, y)
4758 int x;
4759 int y;
4761 #ifdef FEAT_WINDOWS
4762 int row;
4763 int col;
4764 win_T *wp;
4766 row = Y_2_ROW(y);
4767 col = X_2_COL(x);
4768 if (row < 0 || col < 0) /* before first window */
4769 return NULL;
4770 wp = mouse_find_win(&row, &col);
4771 # ifdef FEAT_MOUSESHAPE
4772 if (State == HITRETURN || State == ASKMORE)
4774 if (Y_2_ROW(y) >= msg_row)
4775 update_mouseshape(SHAPE_IDX_MOREL);
4776 else
4777 update_mouseshape(SHAPE_IDX_MORE);
4779 else if (row > wp->w_height) /* below status line */
4780 update_mouseshape(SHAPE_IDX_CLINE);
4781 # ifdef FEAT_VERTSPLIT
4782 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4783 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
4784 update_mouseshape(SHAPE_IDX_VSEP);
4785 # endif
4786 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4787 && row == wp->w_height && msg_scrolled == 0)
4788 update_mouseshape(SHAPE_IDX_STATUS);
4789 else
4790 update_mouseshape(-2);
4791 # endif
4792 return wp;
4793 #else
4794 return firstwin;
4795 #endif
4799 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4800 * File names may be given to redefine the args list.
4802 void
4803 ex_gui(eap)
4804 exarg_T *eap;
4806 char_u *arg = eap->arg;
4809 * Check for "-f" argument: foreground, don't fork.
4810 * Also don't fork when started with "gvim -f".
4811 * Do fork when using "gui -b".
4813 if (arg[0] == '-'
4814 && (arg[1] == 'f' || arg[1] == 'b')
4815 && (arg[2] == NUL || vim_iswhite(arg[2])))
4817 gui.dofork = (arg[1] == 'b');
4818 eap->arg = skipwhite(eap->arg + 2);
4820 if (!gui.in_use)
4822 /* Clear the command. Needed for when forking+exiting, to avoid part
4823 * of the argument ending up after the shell prompt. */
4825 #ifdef MACOS_X
4826 /* os x doesn't really support fork(), so we can't fork of a gui
4827 * in an already running vim. see gui_start() for more details.
4829 gui.dofork = FALSE;
4830 #endif
4831 msg_clr_eos_force();
4832 gui_start();
4834 if (!ends_excmd(*eap->arg))
4835 ex_next(eap);
4838 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4839 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4841 * This is shared between Athena, Motif and GTK.
4843 static void gfp_setname __ARGS((char_u *fname, void *cookie));
4846 * Callback function for do_in_runtimepath().
4848 static void
4849 gfp_setname(fname, cookie)
4850 char_u *fname;
4851 void *cookie;
4853 char_u *gfp_buffer = cookie;
4855 if (STRLEN(fname) >= MAXPATHL)
4856 *gfp_buffer = NUL;
4857 else
4858 STRCPY(gfp_buffer, fname);
4862 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4863 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4866 gui_find_bitmap(name, buffer, ext)
4867 char_u *name;
4868 char_u *buffer;
4869 char *ext;
4871 if (STRLEN(name) > MAXPATHL - 14)
4872 return FAIL;
4873 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
4874 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4875 || *buffer == NUL)
4876 return FAIL;
4877 return OK;
4880 # if !defined(HAVE_GTK2) || defined(PROTO)
4882 * Given the name of the "icon=" argument, try finding the bitmap file for the
4883 * icon. If it is an absolute path name, use it as it is. Otherwise append
4884 * "ext" and search for it in 'runtimepath'.
4885 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4886 * contains "name".
4888 void
4889 gui_find_iconfile(name, buffer, ext)
4890 char_u *name;
4891 char_u *buffer;
4892 char *ext;
4894 char_u buf[MAXPATHL + 1];
4896 expand_env(name, buffer, MAXPATHL);
4897 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4898 STRCPY(buffer, buf);
4900 # endif
4901 #endif
4903 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4904 void
4905 display_errors()
4907 char_u *p;
4909 if (isatty(2))
4910 fflush(stderr);
4911 else if (error_ga.ga_data != NULL)
4913 /* avoid putting up a message box with blanks only */
4914 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4915 if (!isspace(*p))
4917 /* Truncate a very long message, it will go off-screen. */
4918 if (STRLEN(p) > 2000)
4919 STRCPY(p + 2000 - 14, "...(truncated)");
4920 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4921 p, (char_u *)_("&Ok"), 1, NULL);
4922 break;
4924 ga_clear(&error_ga);
4927 #endif
4929 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4931 * Return TRUE if still starting up and there is no place to enter text.
4932 * For GTK and X11 we check if stderr is not a tty, which means we were
4933 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4934 * allow typing on stdin.
4937 no_console_input()
4939 return ((!gui.in_use || gui.starting)
4940 # ifndef NO_CONSOLE
4941 && !isatty(0) && !isatty(2)
4942 # endif
4945 #endif
4947 #if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4948 || defined(NEED_GUI_UPDATE_SCREEN) \
4949 || defined(PROTO)
4951 * Update the current window and the screen.
4953 void
4954 gui_update_screen()
4956 update_topline();
4957 validate_cursor();
4958 update_screen(0); /* may need to update the screen */
4959 setcursor();
4960 out_flush(); /* make sure output has been written */
4961 gui_update_cursor(TRUE, FALSE);
4962 gui_mch_flush();
4964 #endif
4966 #if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4967 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4970 * Get the text to use in a find/replace dialog. Uses the last search pattern
4971 * if the argument is empty.
4972 * Returns an allocated string.
4974 char_u *
4975 get_find_dialog_text(arg, wwordp, mcasep)
4976 char_u *arg;
4977 int *wwordp; /* return: TRUE if \< \> found */
4978 int *mcasep; /* return: TRUE if \C found */
4980 char_u *text;
4982 if (*arg == NUL)
4983 text = last_search_pat();
4984 else
4985 text = arg;
4986 if (text != NULL)
4988 text = vim_strsave(text);
4989 if (text != NULL)
4991 int len = (int)STRLEN(text);
4992 int i;
4994 /* Remove "\V" */
4995 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4997 mch_memmove(text, text + 2, (size_t)(len - 1));
4998 len -= 2;
5001 /* Recognize "\c" and "\C" and remove. */
5002 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5004 *mcasep = (text[1] == 'C');
5005 mch_memmove(text, text + 2, (size_t)(len - 1));
5006 len -= 2;
5009 /* Recognize "\<text\>" and remove. */
5010 if (len >= 4
5011 && STRNCMP(text, "\\<", 2) == 0
5012 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5014 *wwordp = TRUE;
5015 mch_memmove(text, text + 2, (size_t)(len - 4));
5016 text[len - 4] = NUL;
5019 /* Recognize "\/" or "\?" and remove. */
5020 for (i = 0; i + 1 < len; ++i)
5021 if (text[i] == '\\' && (text[i + 1] == '/'
5022 || text[i + 1] == '?'))
5024 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5025 --len;
5029 return text;
5033 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5035 static void
5036 concat_esc(gap, text, what)
5037 garray_T *gap;
5038 char_u *text;
5039 int what;
5041 while (*text != NUL)
5043 #ifdef FEAT_MBYTE
5044 int l = (*mb_ptr2len)(text);
5046 if (l > 1)
5048 while (--l >= 0)
5049 ga_append(gap, *text++);
5050 continue;
5052 #endif
5053 if (*text == what)
5054 ga_append(gap, '\\');
5055 ga_append(gap, *text);
5056 ++text;
5061 * Handle the press of a button in the find-replace dialog.
5062 * Return TRUE when something was added to the input buffer.
5065 gui_do_findrepl(flags, find_text, repl_text, down)
5066 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5067 char_u *find_text;
5068 char_u *repl_text;
5069 int down; /* Search downwards. */
5071 garray_T ga;
5072 int i;
5073 int type = (flags & FRD_TYPE_MASK);
5074 char_u *p;
5075 regmatch_T regmatch;
5076 int save_did_emsg = did_emsg;
5078 ga_init2(&ga, 1, 100);
5079 if (type == FRD_REPLACEALL)
5080 ga_concat(&ga, (char_u *)"%s/");
5082 ga_concat(&ga, (char_u *)"\\V");
5083 if (flags & FRD_MATCH_CASE)
5084 ga_concat(&ga, (char_u *)"\\C");
5085 else
5086 ga_concat(&ga, (char_u *)"\\c");
5087 if (flags & FRD_WHOLE_WORD)
5088 ga_concat(&ga, (char_u *)"\\<");
5089 if (type == FRD_REPLACEALL || down)
5090 concat_esc(&ga, find_text, '/'); /* escape slashes */
5091 else
5092 concat_esc(&ga, find_text, '?'); /* escape '?' */
5093 if (flags & FRD_WHOLE_WORD)
5094 ga_concat(&ga, (char_u *)"\\>");
5096 if (type == FRD_REPLACEALL)
5098 ga_concat(&ga, (char_u *)"/");
5099 /* escape / and \ */
5100 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5101 if (p != NULL)
5102 ga_concat(&ga, p);
5103 vim_free(p);
5104 ga_concat(&ga, (char_u *)"/g");
5106 ga_append(&ga, NUL);
5108 if (type == FRD_REPLACE)
5110 /* Do the replacement when the text at the cursor matches. Thus no
5111 * replacement is done if the cursor was moved! */
5112 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5113 regmatch.rm_ic = 0;
5114 if (regmatch.regprog != NULL)
5116 p = ml_get_cursor();
5117 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5118 && regmatch.startp[0] == p)
5120 /* Clear the command line to remove any old "No match"
5121 * error. */
5122 msg_end_prompt();
5124 if (u_save_cursor() == OK)
5126 /* A button was pressed thus undo should be synced. */
5127 u_sync(FALSE);
5129 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
5130 FALSE, FALSE);
5131 ins_str(repl_text);
5134 else
5135 MSG(_("No match at cursor, finding next"));
5136 vim_free(regmatch.regprog);
5140 if (type == FRD_REPLACEALL)
5142 /* A button was pressed, thus undo should be synced. */
5143 u_sync(FALSE);
5144 do_cmdline_cmd(ga.ga_data);
5146 else
5148 /* Search for the next match. */
5149 i = msg_scroll;
5150 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
5151 SEARCH_MSG + SEARCH_MARK, NULL);
5152 msg_scroll = i; /* don't let an error message set msg_scroll */
5155 /* Don't want to pass did_emsg to other code, it may cause disabling
5156 * syntax HL if we were busy redrawing. */
5157 did_emsg = save_did_emsg;
5159 if (State & (NORMAL | INSERT))
5161 gui_update_screen(); /* update the screen */
5162 msg_didout = 0; /* overwrite any message */
5163 need_wait_return = FALSE; /* don't wait for return */
5166 vim_free(ga.ga_data);
5167 return (ga.ga_len > 0);
5170 #endif
5172 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5173 || defined(FEAT_GUI_MSWIN) \
5174 || defined(FEAT_GUI_MAC) \
5175 || defined(PROTO) \
5176 || defined(FEAT_GUI_MACVIM)
5178 #ifdef FEAT_WINDOWS
5179 static void gui_wingoto_xy __ARGS((int x, int y));
5182 * Jump to the window at specified point (x, y).
5184 static void
5185 gui_wingoto_xy(x, y)
5186 int x;
5187 int y;
5189 int row = Y_2_ROW(y);
5190 int col = X_2_COL(x);
5191 win_T *wp;
5193 if (row >= 0 && col >= 0)
5195 wp = mouse_find_win(&row, &col);
5196 if (wp != NULL && wp != curwin)
5197 win_goto(wp);
5200 #endif
5203 * Process file drop. Mouse cursor position, key modifiers, name of files
5204 * and count of files are given. Argument "fnames[count]" has full pathnames
5205 * of dropped files, they will be freed in this function, and caller can't use
5206 * fnames after call this function.
5208 /*ARGSUSED*/
5209 void
5210 gui_handle_drop(x, y, modifiers, fnames, count)
5211 int x;
5212 int y;
5213 int_u modifiers;
5214 char_u **fnames;
5215 int count;
5217 int i;
5218 char_u *p;
5221 * When the cursor is at the command line, add the file names to the
5222 * command line, don't edit the files.
5224 if (State & CMDLINE)
5226 shorten_filenames(fnames, count);
5227 for (i = 0; i < count; ++i)
5229 if (fnames[i] != NULL)
5231 if (i > 0)
5232 add_to_input_buf((char_u*)" ", 1);
5234 /* We don't know what command is used thus we can't be sure
5235 * about which characters need to be escaped. Only escape the
5236 * most common ones. */
5237 # ifdef BACKSLASH_IN_FILENAME
5238 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5239 # else
5240 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5241 # endif
5242 if (p != NULL)
5243 add_to_input_buf_csi(p, (int)STRLEN(p));
5244 vim_free(p);
5245 vim_free(fnames[i]);
5248 vim_free(fnames);
5250 else
5252 /* Go to the window under mouse cursor, then shorten given "fnames" by
5253 * current window, because a window can have local current dir. */
5254 # ifdef FEAT_WINDOWS
5255 gui_wingoto_xy(x, y);
5256 # endif
5257 shorten_filenames(fnames, count);
5259 /* If Shift held down, remember the first item. */
5260 if ((modifiers & MOUSE_SHIFT) != 0)
5261 p = vim_strsave(fnames[0]);
5262 else
5263 p = NULL;
5265 /* Handle the drop, :edit or :split to get to the file. This also
5266 * frees fnames[]. Skip this if there is only one item it's a
5267 * directory and Shift is held down. */
5268 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5269 && mch_isdir(fnames[0]))
5271 vim_free(fnames[0]);
5272 vim_free(fnames);
5274 else
5275 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5277 /* If Shift held down, change to first file's directory. If the first
5278 * item is a directory, change to that directory (and let the explorer
5279 * plugin show the contents). */
5280 if (p != NULL)
5282 if (mch_isdir(p))
5284 if (mch_chdir((char *)p) == 0)
5285 shorten_fnames(TRUE);
5287 else if (vim_chdirfile(p) == OK)
5288 shorten_fnames(TRUE);
5289 vim_free(p);
5292 /* Update the screen display */
5293 update_screen(NOT_VALID);
5294 # ifdef FEAT_MENU
5295 gui_update_menus(0);
5296 # endif
5297 setcursor();
5298 out_flush();
5299 gui_update_cursor(FALSE, FALSE);
5300 gui_mch_flush();
5303 #endif