comparison src/gui.c @ 7:3fc0f57ecb91 v7.0001

updated for version 7.0001
author vimboss
date Sun, 13 Jun 2004 20:20:40 +0000
parents
children 4424b47a0797
comparison
equal deleted inserted replaced
6:c2daee826b8f 7:3fc0f57ecb91
1 /* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
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 */
10
11 #include "vim.h"
12
13 /* Structure containing all the GUI information */
14 gui_T gui;
15
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 static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
30 static colnr_T scroll_line_len __ARGS((linenr_T lnum));
31 static void gui_update_horiz_scrollbar __ARGS((int));
32 static win_T *xy2win __ARGS((int x, int y));
33
34 static int can_update_cursor = TRUE; /* can display the cursor */
35
36 /*
37 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
38 * this makes the thumb indicate the part of the text that is shown. Motif
39 * can't do this.
40 */
41 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
42 # define SCROLL_PAST_END
43 #endif
44
45 /*
46 * gui_start -- Called when user wants to start the GUI.
47 *
48 * Careful: This function can be called recursively when there is a ":gui"
49 * command in the .gvimrc file. Only the first call should fork, not the
50 * recursive call.
51 */
52 void
53 gui_start()
54 {
55 char_u *old_term;
56 #if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X)
57 # define MAY_FORK
58 int dofork = TRUE;
59 #endif
60 static int recursive = 0;
61
62 old_term = vim_strsave(T_NAME);
63
64 /*
65 * Set_termname() will call gui_init() to start the GUI.
66 * Set the "starting" flag, to indicate that the GUI will start.
67 *
68 * We don't want to open the GUI shell until after we've read .gvimrc,
69 * otherwise we don't know what font we will use, and hence we don't know
70 * what size the shell should be. So if there are errors in the .gvimrc
71 * file, they will have to go to the terminal: Set full_screen to FALSE.
72 * full_screen will be set to TRUE again by a successful termcapinit().
73 */
74 settmode(TMODE_COOK); /* stop RAW mode */
75 if (full_screen)
76 cursor_on(); /* needed for ":gui" in .vimrc */
77 gui.starting = TRUE;
78 full_screen = FALSE;
79
80 #ifdef MAY_FORK
81 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
82 dofork = FALSE;
83 #endif
84 ++recursive;
85
86 termcapinit((char_u *)"builtin_gui");
87 gui.starting = recursive - 1;
88
89 if (!gui.in_use) /* failed to start GUI */
90 {
91 termcapinit(old_term); /* back to old term settings */
92 settmode(TMODE_RAW); /* restart RAW mode */
93 #ifdef FEAT_TITLE
94 set_title_defaults(); /* set 'title' and 'icon' again */
95 #endif
96 }
97
98 vim_free(old_term);
99
100 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
101 if (gui.in_use)
102 /* Display error messages in a dialog now. */
103 display_errors();
104 #endif
105
106 #if defined(MAY_FORK) && !defined(__QNXNTO__)
107 /*
108 * Quit the current process and continue in the child.
109 * Makes "gvim file" disconnect from the shell it was started in.
110 * Don't do this when Vim was started with "-f" or the 'f' flag is present
111 * in 'guioptions'.
112 */
113 if (gui.in_use && dofork)
114 {
115 int pipefd[2]; /* pipe between parent and child */
116 int pipe_error;
117 char dummy;
118 pid_t pid = -1;
119
120 /* Setup a pipe between the child and the parent, so that the parent
121 * knows when the child has done the setsid() call and is allowed to
122 * exit. */
123 pipe_error = (pipe(pipefd) < 0);
124 pid = fork();
125 if (pid > 0) /* Parent */
126 {
127 /* Give the child some time to do the setsid(), otherwise the
128 * exit() may kill the child too (when starting gvim from inside a
129 * gvim). */
130 if (pipe_error)
131 ui_delay(300L, TRUE);
132 else
133 {
134 /* The read returns when the child closes the pipe (or when
135 * the child dies for some reason). */
136 close(pipefd[1]);
137 (void)read(pipefd[0], &dummy, (size_t)1);
138 close(pipefd[0]);
139 }
140
141 /* When swapping screens we may need to go to the next line, e.g.,
142 * after a hit-enter prompt and using ":gui". */
143 if (newline_on_exit)
144 mch_errmsg("\r\n");
145
146 /*
147 * The parent must skip the normal exit() processing, the child
148 * will do it. For example, GTK messes up signals when exiting.
149 */
150 _exit(0);
151 }
152
153 # if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
154 /*
155 * Change our process group. On some systems/shells a CTRL-C in the
156 * shell where Vim was started would otherwise kill gvim!
157 */
158 if (pid == 0) /* child */
159 # if defined(HAVE_SETSID)
160 (void)setsid();
161 # else
162 (void)setpgid(0, 0);
163 # endif
164 # endif
165 if (!pipe_error)
166 {
167 close(pipefd[0]);
168 close(pipefd[1]);
169 }
170
171 # if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
172 /* Tell the session manager our new PID */
173 gui_mch_forked();
174 # endif
175 }
176 #else
177 # if defined(__QNXNTO__)
178 if (gui.in_use && dofork)
179 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
180 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
181 # endif
182 #endif
183
184 #ifdef FEAT_AUTOCMD
185 /* If the GUI started successfully, trigger the GUIEnter event */
186 if (gui.in_use)
187 apply_autocmds(EVENT_GUIENTER, NULL, NULL, FALSE, curbuf);
188 #endif
189
190 --recursive;
191 }
192
193 /*
194 * Call this when vim starts up, whether or not the GUI is started
195 */
196 void
197 gui_prepare(argc, argv)
198 int *argc;
199 char **argv;
200 {
201 gui.in_use = FALSE; /* No GUI yet (maybe later) */
202 gui.starting = FALSE; /* No GUI yet (maybe later) */
203 gui_mch_prepare(argc, argv);
204 }
205
206 /*
207 * Try initializing the GUI and check if it can be started.
208 * Used from main() to check early if "vim -g" can start the GUI.
209 * Used from gui_init() to prepare for starting the GUI.
210 * Returns FAIL or OK.
211 */
212 int
213 gui_init_check()
214 {
215 static int result = MAYBE;
216
217 if (result != MAYBE)
218 {
219 if (result == FAIL)
220 EMSG(_("E229: Cannot start the GUI"));
221 return result;
222 }
223
224 gui.shell_created = FALSE;
225 gui.dying = FALSE;
226 gui.in_focus = TRUE; /* so the guicursor setting works */
227 gui.dragged_sb = SBAR_NONE;
228 gui.dragged_wp = NULL;
229 gui.pointer_hidden = FALSE;
230 gui.col = 0;
231 gui.row = 0;
232 gui.num_cols = Columns;
233 gui.num_rows = Rows;
234
235 gui.cursor_is_valid = FALSE;
236 gui.scroll_region_top = 0;
237 gui.scroll_region_bot = Rows - 1;
238 gui.scroll_region_left = 0;
239 gui.scroll_region_right = Columns - 1;
240 gui.highlight_mask = HL_NORMAL;
241 gui.char_width = 1;
242 gui.char_height = 1;
243 gui.char_ascent = 0;
244 gui.border_width = 0;
245
246 gui.norm_font = NOFONT;
247 #ifndef HAVE_GTK2
248 gui.bold_font = NOFONT;
249 gui.ital_font = NOFONT;
250 gui.boldital_font = NOFONT;
251 # ifdef FEAT_XFONTSET
252 gui.fontset = NOFONTSET;
253 # endif
254 #endif
255
256 #ifdef FEAT_MENU
257 # ifndef HAVE_GTK2
258 # ifdef FONTSET_ALWAYS
259 gui.menu_fontset = NOFONTSET;
260 # else
261 gui.menu_font = NOFONT;
262 # endif
263 # endif
264 gui.menu_is_active = TRUE; /* default: include menu */
265 # ifndef FEAT_GUI_GTK
266 gui.menu_height = MENU_DEFAULT_HEIGHT;
267 gui.menu_width = 0;
268 # endif
269 #endif
270 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
271 gui.toolbar_height = 0;
272 #endif
273 #if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
274 gui.footer_height = 0;
275 #endif
276 #ifdef FEAT_BEVAL_TIP
277 gui.tooltip_fontset = NOFONTSET;
278 #endif
279
280 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
281 gui.prev_wrap = -1;
282
283 #ifdef ALWAYS_USE_GUI
284 result = OK;
285 #else
286 result = gui_mch_init_check();
287 #endif
288 return result;
289 }
290
291 /*
292 * This is the call which starts the GUI.
293 */
294 void
295 gui_init()
296 {
297 win_T *wp;
298 static int recursive = 0;
299
300 /*
301 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
302 * function will then be executed at the first call, the rest by the
303 * recursive call. This allow the shell to be opened halfway reading a
304 * gvimrc file.
305 */
306 if (!recursive)
307 {
308 ++recursive;
309
310 clip_init(TRUE);
311
312 /* If can't initialize, don't try doing the rest */
313 if (gui_init_check() == FAIL)
314 {
315 --recursive;
316 clip_init(FALSE);
317 return;
318 }
319
320 /*
321 * Set up system-wide default menus.
322 */
323 #if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
324 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
325 {
326 sys_menu = TRUE;
327 do_source((char_u *)SYS_MENU_FILE, FALSE, FALSE);
328 sys_menu = FALSE;
329 }
330 #endif
331
332 /*
333 * Switch on the mouse by default, unless the user changed it already.
334 * This can then be changed in the .gvimrc.
335 */
336 if (!option_was_set((char_u *)"mouse"))
337 set_string_option_direct((char_u *)"mouse", -1,
338 (char_u *)"a", OPT_FREE);
339
340 /*
341 * If -U option given, use only the initializations from that file and
342 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
343 */
344 if (use_gvimrc != NULL)
345 {
346 if (STRCMP(use_gvimrc, "NONE") != 0
347 && STRCMP(use_gvimrc, "NORC") != 0
348 && do_source(use_gvimrc, FALSE, FALSE) != OK)
349 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
350 }
351 else
352 {
353 /*
354 * Get system wide defaults for gvim, only when file name defined.
355 */
356 #ifdef SYS_GVIMRC_FILE
357 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, FALSE);
358 #endif
359
360 /*
361 * Try to read GUI initialization commands from the following
362 * places:
363 * - environment variable GVIMINIT
364 * - the user gvimrc file (~/.gvimrc)
365 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
366 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
367 * The first that exists is used, the rest is ignored.
368 */
369 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
370 && do_source((char_u *)USR_GVIMRC_FILE, TRUE, FALSE) == FAIL
371 #ifdef USR_GVIMRC_FILE2
372 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE, FALSE) == FAIL
373 #endif
374 )
375 {
376 #ifdef USR_GVIMRC_FILE3
377 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, FALSE);
378 #endif
379 }
380
381 /*
382 * Read initialization commands from ".gvimrc" in current
383 * directory. This is only done if the 'exrc' option is set.
384 * Because of security reasons we disallow shell and write
385 * commands now, except for unix if the file is owned by the user
386 * or 'secure' option has been reset in environment of global
387 * ".gvimrc".
388 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
389 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
390 */
391 if (p_exrc)
392 {
393 #ifdef UNIX
394 {
395 struct stat s;
396
397 /* if ".gvimrc" file is not owned by user, set 'secure'
398 * mode */
399 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
400 secure = p_secure;
401 }
402 #else
403 secure = p_secure;
404 #endif
405
406 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
407 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
408 #ifdef SYS_GVIMRC_FILE
409 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
410 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
411 #endif
412 #ifdef USR_GVIMRC_FILE2
413 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
414 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
415 #endif
416 #ifdef USR_GVIMRC_FILE3
417 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
418 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
419 #endif
420 )
421 do_source((char_u *)GVIMRC_FILE, TRUE, FALSE);
422
423 if (secure == 2)
424 need_wait_return = TRUE;
425 secure = 0;
426 }
427 }
428
429 if (need_wait_return || msg_didany)
430 wait_return(TRUE);
431
432 --recursive;
433 }
434
435 /* If recursive call opened the shell, return here from the first call */
436 if (gui.in_use)
437 return;
438
439 /*
440 * Create the GUI shell.
441 */
442 gui.in_use = TRUE; /* Must be set after menus have been set up */
443 if (gui_mch_init() == FAIL)
444 goto error;
445
446 /* Avoid a delay for an error message that was printed in the terminal
447 * where Vim was started. */
448 emsg_on_display = FALSE;
449 msg_scrolled = 0;
450 need_wait_return = FALSE;
451 msg_didany = FALSE;
452
453 /*
454 * Check validity of any generic resources that may have been loaded.
455 */
456 if (gui.border_width < 0)
457 gui.border_width = 0;
458
459 /*
460 * Set up the fonts. First use a font specified with "-fn" or "-font".
461 */
462 if (font_argument != NULL)
463 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
464 if (
465 #ifdef FEAT_XFONTSET
466 (*p_guifontset == NUL
467 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
468 #endif
469 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
470 : p_guifont, FALSE) == FAIL)
471 {
472 EMSG(_("E665: Cannot start GUI, no valid font found"));
473 goto error2;
474 }
475 #ifdef FEAT_MBYTE
476 if (gui_get_wide_font() == FAIL)
477 EMSG(_("E231: 'guifontwide' invalid"));
478 #endif
479
480 gui.num_cols = Columns;
481 gui.num_rows = Rows;
482 gui_reset_scroll_region();
483
484 /* Create initial scrollbars */
485 FOR_ALL_WINDOWS(wp)
486 {
487 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
488 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
489 }
490 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
491
492 #ifdef FEAT_MENU
493 gui_create_initial_menus(root_menu);
494 #endif
495 #ifdef FEAT_SUN_WORKSHOP
496 if (usingSunWorkShop)
497 workshop_init();
498 #endif
499 #ifdef FEAT_SIGN_ICONS
500 sign_gui_started();
501 #endif
502
503 /* Configure the desired menu and scrollbars */
504 gui_init_which_components(NULL);
505
506 /* All components of the GUI have been created now */
507 gui.shell_created = TRUE;
508
509 #ifndef FEAT_GUI_GTK
510 /* Set the shell size, adjusted for the screen size. For GTK this only
511 * works after the shell has been opened, thus it is further down. */
512 gui_set_shellsize(FALSE, TRUE);
513 #endif
514 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
515 /* Need to set the size of the menubar after all the menus have been
516 * created. */
517 gui_mch_compute_menu_height((Widget)0);
518 #endif
519
520 /*
521 * Actually open the GUI shell.
522 */
523 if (gui_mch_open() != FAIL)
524 {
525 #ifdef FEAT_TITLE
526 maketitle();
527 resettitle();
528 #endif
529 init_gui_options();
530 #ifdef FEAT_ARABIC
531 /* Our GUI can't do bidi. */
532 p_tbidi = FALSE;
533 #endif
534 #ifdef FEAT_GUI_GTK
535 /* Give GTK+ a chance to put all widget's into place. */
536 gui_mch_update();
537 /* Now make sure the shell fits on the screen. */
538 gui_set_shellsize(FALSE, TRUE);
539 #endif
540 #ifdef FEAT_NETBEANS_INTG
541 if (starting == 0 && usingNetbeans)
542 /* Tell the client that it can start sending commands. */
543 netbeans_startup_done();
544 #endif
545 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
546 if (!im_xim_isvalid_imactivate())
547 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
548 #endif
549
550 return;
551 }
552
553 error2:
554 #ifdef FEAT_GUI_X11
555 /* undo gui_mch_init() */
556 gui_mch_uninit();
557 #endif
558
559 error:
560 gui.in_use = FALSE;
561 clip_init(FALSE);
562 }
563
564
565 void
566 gui_exit(rc)
567 int rc;
568 {
569 #ifndef __BEOS__
570 /* don't free the fonts, it leads to a BUS error
571 * richard@whitequeen.com Jul 99 */
572 free_highlight_fonts();
573 #endif
574 gui.in_use = FALSE;
575 gui_mch_exit(rc);
576 }
577
578 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
579 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
580 /*
581 * Called when the GUI shell is closed by the user. If there are no changed
582 * files Vim exits, otherwise there will be a dialog to ask the user what to
583 * do.
584 * When this function returns, Vim should NOT exit!
585 */
586 void
587 gui_shell_closed()
588 {
589 cmdmod_T save_cmdmod;
590
591 save_cmdmod = cmdmod;
592
593 /* Only exit when there are no changed files */
594 exiting = TRUE;
595 # ifdef FEAT_BROWSE
596 cmdmod.browse = TRUE;
597 # endif
598 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
599 cmdmod.confirm = TRUE;
600 # endif
601 /* If there are changed buffers, present the user with a dialog if
602 * possible, otherwise give an error message. */
603 if (!check_changed_any(FALSE))
604 getout(0);
605
606 exiting = FALSE;
607 cmdmod = save_cmdmod;
608 setcursor(); /* position cursor */
609 out_flush();
610 }
611 #endif
612
613 /*
614 * Set the font. "font_list" is a a comma separated list of font names. The
615 * first font name that works is used. If none is found, use the default
616 * font.
617 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
618 * Return OK when able to set the font. When it failed FAIL is returned and
619 * the fonts are unchanged.
620 */
621 /*ARGSUSED*/
622 int
623 gui_init_font(font_list, fontset)
624 char_u *font_list;
625 int fontset;
626 {
627 #define FONTLEN 320
628 char_u font_name[FONTLEN];
629 int font_list_empty = FALSE;
630 int ret = FAIL;
631
632 if (!gui.in_use)
633 return FAIL;
634
635 font_name[0] = NUL;
636 if (*font_list == NUL)
637 font_list_empty = TRUE;
638 else
639 {
640 #ifdef FEAT_XFONTSET
641 /* When using a fontset, the whole list of fonts is one name. */
642 if (fontset)
643 ret = gui_mch_init_font(font_list, TRUE);
644 else
645 #endif
646 while (*font_list != NUL)
647 {
648 /* Isolate one comma separated font name. */
649 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
650
651 /* Careful!!! The Win32 version of gui_mch_init_font(), when
652 * called with "*" will change p_guifont to the selected font
653 * name, which frees the old value. This makes font_list
654 * invalid. Thus when OK is returned here, font_list must no
655 * longer be used! */
656 if (gui_mch_init_font(font_name, FALSE) == OK)
657 {
658 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
659 /* If it's a Unicode font, try setting 'guifontwide' to a
660 * similar double-width font. */
661 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
662 && strstr((char *)font_name, "10646") != NULL)
663 set_guifontwide(font_name);
664 #endif
665 ret = OK;
666 break;
667 }
668 }
669 }
670
671 if (ret != OK
672 && STRCMP(font_list, "*") != 0
673 && (font_list_empty || gui.norm_font == NOFONT))
674 {
675 /*
676 * Couldn't load any font in 'font_list', keep the current font if
677 * there is one. If 'font_list' is empty, or if there is no current
678 * font, tell gui_mch_init_font() to try to find a font we can load.
679 */
680 ret = gui_mch_init_font(NULL, FALSE);
681 }
682
683 if (ret == OK)
684 {
685 #ifndef HAVE_GTK2
686 /* Set normal font as current font */
687 # ifdef FEAT_XFONTSET
688 if (gui.fontset != NOFONTSET)
689 gui_mch_set_fontset(gui.fontset);
690 else
691 # endif
692 gui_mch_set_font(gui.norm_font);
693 #endif
694 gui_set_shellsize(FALSE,
695 #ifdef MSWIN
696 TRUE
697 #else
698 FALSE
699 #endif
700 );
701 }
702
703 return ret;
704 }
705
706 #if defined(FEAT_MBYTE) || defined(PROTO)
707 # ifndef HAVE_GTK2
708 /*
709 * Try setting 'guifontwide' to a font twice as wide as "name".
710 */
711 static void
712 set_guifontwide(name)
713 char_u *name;
714 {
715 int i = 0;
716 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
717 char_u *wp = NULL;
718 char_u *p;
719 GuiFont font;
720
721 wp = wide_name;
722 for (p = name; *p != NUL; ++p)
723 {
724 *wp++ = *p;
725 if (*p == '-')
726 {
727 ++i;
728 if (i == 6) /* font type: change "--" to "-*-" */
729 {
730 if (p[1] == '-')
731 *wp++ = '*';
732 }
733 else if (i == 12) /* found the width */
734 {
735 ++p;
736 i = getdigits(&p);
737 if (i != 0)
738 {
739 /* Double the width specification. */
740 sprintf((char *)wp, "%d%s", i * 2, p);
741 font = gui_mch_get_font(wide_name, FALSE);
742 if (font != NOFONT)
743 {
744 gui.wide_font = font;
745 set_string_option_direct((char_u *)"gfw", -1,
746 wide_name, OPT_FREE);
747 }
748 }
749 break;
750 }
751 }
752 }
753 }
754 # endif /* !HAVE_GTK2 */
755
756 /*
757 * Get the font for 'guifontwide'.
758 * Return FAIL for an invalid font name.
759 */
760 int
761 gui_get_wide_font()
762 {
763 GuiFont font = NOFONT;
764 char_u font_name[FONTLEN];
765 char_u *p;
766
767 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
768 return OK; /* Will give an error message later. */
769
770 if (p_guifontwide != NULL && *p_guifontwide != NUL)
771 {
772 for (p = p_guifontwide; *p != NUL; )
773 {
774 /* Isolate one comma separated font name. */
775 (void)copy_option_part(&p, font_name, FONTLEN, ",");
776 font = gui_mch_get_font(font_name, FALSE);
777 if (font != NOFONT)
778 break;
779 }
780 if (font == NOFONT)
781 return FAIL;
782 }
783
784 gui_mch_free_font(gui.wide_font);
785 #ifdef HAVE_GTK2
786 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
787 if (font != NOFONT && gui.norm_font != NOFONT
788 && pango_font_description_equal(font, gui.norm_font))
789 {
790 gui.wide_font = NOFONT;
791 gui_mch_free_font(font);
792 }
793 else
794 #endif
795 gui.wide_font = font;
796 return OK;
797 }
798 #endif
799
800 void
801 gui_set_cursor(row, col)
802 int row;
803 int col;
804 {
805 gui.row = row;
806 gui.col = col;
807 }
808
809 /*
810 * gui_check_pos - check if the cursor is on the screen.
811 */
812 static void
813 gui_check_pos()
814 {
815 if (gui.row >= screen_Rows)
816 gui.row = screen_Rows - 1;
817 if (gui.col >= screen_Columns)
818 gui.col = screen_Columns - 1;
819 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
820 gui.cursor_is_valid = FALSE;
821 }
822
823 /*
824 * Redraw the cursor if necessary or when forced.
825 * Careful: The contents of ScreenLines[] must match what is on the screen,
826 * otherwise this goes wrong. May need to call out_flush() first.
827 */
828 void
829 gui_update_cursor(force, clear_selection)
830 int force; /* when TRUE, update even when not moved */
831 int clear_selection;/* clear selection under cursor */
832 {
833 int cur_width = 0;
834 int cur_height = 0;
835 int old_hl_mask;
836 int idx;
837 int id;
838 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
839 int cattr; /* cursor attributes */
840 int attr;
841 attrentry_T *aep = NULL;
842
843 /* Don't update the cursor when halfway busy scrolling.
844 * ScreenLines[] isn't valid then. */
845 if (!can_update_cursor)
846 return;
847
848 gui_check_pos();
849 if (!gui.cursor_is_valid || force
850 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
851 {
852 gui_undraw_cursor();
853 if (gui.row < 0)
854 return;
855 #ifdef USE_IM_CONTROL
856 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
857 im_set_position(gui.row, gui.col);
858 #endif
859 gui.cursor_row = gui.row;
860 gui.cursor_col = gui.col;
861
862 /* Only write to the screen after ScreenLines[] has been initialized */
863 if (!screen_cleared || ScreenLines == NULL)
864 return;
865
866 /* Clear the selection if we are about to write over it */
867 if (clear_selection)
868 clip_may_clear_selection(gui.row, gui.row);
869 /* Check that the cursor is inside the shell (resizing may have made
870 * it invalid) */
871 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
872 return;
873
874 gui.cursor_is_valid = TRUE;
875
876 /*
877 * How the cursor is drawn depends on the current mode.
878 */
879 idx = get_shape_idx(FALSE);
880 if (State & LANGMAP)
881 id = shape_table[idx].id_lm;
882 else
883 id = shape_table[idx].id;
884
885 /* get the colors and attributes for the cursor. Default is inverted */
886 cfg = INVALCOLOR;
887 cbg = INVALCOLOR;
888 cattr = HL_INVERSE;
889 gui_mch_set_blinking(shape_table[idx].blinkwait,
890 shape_table[idx].blinkon,
891 shape_table[idx].blinkoff);
892 if (id > 0)
893 {
894 cattr = syn_id2colors(id, &cfg, &cbg);
895 #if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
896 {
897 static int iid;
898 guicolor_T fg, bg;
899
900 if (im_get_status())
901 {
902 iid = syn_name2id((char_u *)"CursorIM");
903 if (iid > 0)
904 {
905 syn_id2colors(iid, &fg, &bg);
906 if (bg != INVALCOLOR)
907 cbg = bg;
908 if (fg != INVALCOLOR)
909 cfg = fg;
910 }
911 }
912 }
913 #endif
914 }
915
916 /*
917 * Get the attributes for the character under the cursor.
918 * When no cursor color was given, use the character color.
919 */
920 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
921 if (attr > HL_ALL)
922 aep = syn_gui_attr2entry(attr);
923 if (aep != NULL)
924 {
925 attr = aep->ae_attr;
926 if (cfg == INVALCOLOR)
927 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
928 : aep->ae_u.gui.fg_color);
929 if (cbg == INVALCOLOR)
930 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
931 : aep->ae_u.gui.bg_color);
932 }
933 if (cfg == INVALCOLOR)
934 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
935 if (cbg == INVALCOLOR)
936 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
937
938 #ifdef FEAT_XIM
939 if (aep != NULL)
940 {
941 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
942 : aep->ae_u.gui.bg_color);
943 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
944 : aep->ae_u.gui.fg_color);
945 if (xim_bg_color == INVALCOLOR)
946 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
947 : gui.back_pixel;
948 if (xim_fg_color == INVALCOLOR)
949 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
950 : gui.norm_pixel;
951 }
952 else
953 {
954 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
955 : gui.back_pixel;
956 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
957 : gui.norm_pixel;
958 }
959 #endif
960
961 attr &= ~HL_INVERSE;
962 if (cattr & HL_INVERSE)
963 {
964 cc = cbg;
965 cbg = cfg;
966 cfg = cc;
967 }
968 cattr &= ~HL_INVERSE;
969
970 /*
971 * When we don't have window focus, draw a hollow cursor.
972 */
973 if (!gui.in_focus)
974 {
975 gui_mch_draw_hollow_cursor(cbg);
976 return;
977 }
978
979 old_hl_mask = gui.highlight_mask;
980 if (shape_table[idx].shape == SHAPE_BLOCK
981 #ifdef FEAT_HANGULIN
982 || composing_hangul
983 #endif
984 )
985 {
986 /*
987 * Draw the text character with the cursor colors. Use the
988 * character attributes plus the cursor attributes.
989 */
990 gui.highlight_mask = (cattr | attr);
991 #ifdef FEAT_HANGULIN
992 if (composing_hangul)
993 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
994 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
995 else
996 #endif
997 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
998 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
999 }
1000 else
1001 {
1002 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1003 int col_off = FALSE;
1004 #endif
1005 /*
1006 * First draw the partial cursor, then overwrite with the text
1007 * character, using a transparent background.
1008 */
1009 if (shape_table[idx].shape == SHAPE_VER)
1010 {
1011 cur_height = gui.char_height;
1012 cur_width = (gui.char_width * shape_table[idx].percentage
1013 + 99) / 100;
1014 }
1015 else
1016 {
1017 cur_height = (gui.char_height * shape_table[idx].percentage
1018 + 99) / 100;
1019 cur_width = gui.char_width;
1020 }
1021 #ifdef FEAT_MBYTE
1022 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col) > 1)
1023 {
1024 /* Double wide character. */
1025 if (shape_table[idx].shape != SHAPE_VER)
1026 cur_width += gui.char_width;
1027 # ifdef FEAT_RIGHTLEFT
1028 if (CURSOR_BAR_RIGHT)
1029 {
1030 /* gui.col points to the left halve of the character but
1031 * the vertical line needs to be on the right halve.
1032 * A double-wide horizontal line is also drawn from the
1033 * right halve in gui_mch_draw_part_cursor(). */
1034 col_off = TRUE;
1035 ++gui.col;
1036 }
1037 # endif
1038 }
1039 #endif
1040 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1041 #if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1042 if (col_off)
1043 --gui.col;
1044 #endif
1045
1046 #ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1047 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1048 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1049 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1050 (guicolor_T)0, (guicolor_T)0, 0);
1051 #endif
1052 }
1053 gui.highlight_mask = old_hl_mask;
1054 }
1055 }
1056
1057 #if defined(FEAT_MENU) || defined(PROTO)
1058 void
1059 gui_position_menu()
1060 {
1061 # if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
1062 if (gui.menu_is_active && gui.in_use)
1063 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1064 # endif
1065 }
1066 #endif
1067
1068 /*
1069 * Position the various GUI components (text area, menu). The vertical
1070 * scrollbars are NOT handled here. See gui_update_scrollbars().
1071 */
1072 /*ARGSUSED*/
1073 static void
1074 gui_position_components(total_width)
1075 int total_width;
1076 {
1077 int text_area_x;
1078 int text_area_y;
1079 int text_area_width;
1080 int text_area_height;
1081
1082 /* avoid that moving components around generates events */
1083 ++hold_gui_events;
1084
1085 text_area_x = 0;
1086 if (gui.which_scrollbars[SBAR_LEFT])
1087 text_area_x += gui.scrollbar_width;
1088
1089 text_area_y = 0;
1090 #if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1091 gui.menu_width = total_width;
1092 if (gui.menu_is_active)
1093 text_area_y += gui.menu_height;
1094 #endif
1095 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1096 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1097 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1098 #endif
1099
1100 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1101 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1102 {
1103 # ifdef FEAT_GUI_ATHENA
1104 gui_mch_set_toolbar_pos(0, text_area_y,
1105 gui.menu_width, gui.toolbar_height);
1106 # endif
1107 text_area_y += gui.toolbar_height;
1108 }
1109 #endif
1110
1111 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1112 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1113
1114 gui_mch_set_text_area_pos(text_area_x,
1115 text_area_y,
1116 text_area_width,
1117 text_area_height
1118 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1119 + xim_get_status_area_height()
1120 #endif
1121 );
1122 #ifdef FEAT_MENU
1123 gui_position_menu();
1124 #endif
1125 if (gui.which_scrollbars[SBAR_BOTTOM])
1126 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1127 text_area_x,
1128 text_area_y + text_area_height,
1129 text_area_width,
1130 gui.scrollbar_height);
1131 gui.left_sbar_x = 0;
1132 gui.right_sbar_x = text_area_x + text_area_width;
1133
1134 --hold_gui_events;
1135 }
1136
1137 int
1138 gui_get_base_width()
1139 {
1140 int base_width;
1141
1142 base_width = 2 * gui.border_offset;
1143 if (gui.which_scrollbars[SBAR_LEFT])
1144 base_width += gui.scrollbar_width;
1145 if (gui.which_scrollbars[SBAR_RIGHT])
1146 base_width += gui.scrollbar_width;
1147 return base_width;
1148 }
1149
1150 int
1151 gui_get_base_height()
1152 {
1153 int base_height;
1154
1155 base_height = 2 * gui.border_offset;
1156 if (gui.which_scrollbars[SBAR_BOTTOM])
1157 base_height += gui.scrollbar_height;
1158 #ifdef FEAT_GUI_GTK
1159 /* We can't take the sizes properly into account until anything is
1160 * realized. Therefore we recalculate all the values here just before
1161 * setting the size. (--mdcki) */
1162 #else
1163 # ifdef FEAT_MENU
1164 if (gui.menu_is_active)
1165 base_height += gui.menu_height;
1166 # endif
1167 # ifdef FEAT_TOOLBAR
1168 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1169 # if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1170 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1171 # else
1172 base_height += gui.toolbar_height;
1173 # endif
1174 # endif
1175 # ifdef FEAT_FOOTER
1176 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1177 base_height += gui.footer_height;
1178 # endif
1179 # if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1180 base_height += gui_mch_text_area_extra_height();
1181 # endif
1182 #endif
1183 return base_height;
1184 }
1185
1186 /*
1187 * Should be called after the GUI shell has been resized. Its arguments are
1188 * the new width and height of the shell in pixels.
1189 */
1190 void
1191 gui_resize_shell(pixel_width, pixel_height)
1192 int pixel_width;
1193 int pixel_height;
1194 {
1195 static int busy = FALSE;
1196
1197 if (!gui.shell_created) /* ignore when still initializing */
1198 return;
1199
1200 /*
1201 * Can't resize the screen while it is being redrawn. Remember the new
1202 * size and handle it later.
1203 */
1204 if (updating_screen || busy)
1205 {
1206 new_pixel_width = pixel_width;
1207 new_pixel_height = pixel_height;
1208 return;
1209 }
1210
1211 again:
1212 busy = TRUE;
1213
1214 #ifdef FEAT_GUI_BEOS
1215 vim_lock_screen();
1216 #endif
1217
1218 /* Flush pending output before redrawing */
1219 out_flush();
1220
1221 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1222 gui.num_rows = (pixel_height - gui_get_base_height()
1223 #if !defined(FEAT_GUI_PHOTON) && !defined(FEAT_GUI_MSWIN)
1224 + (gui.char_height / 2)
1225 #endif
1226 ) / gui.char_height;
1227
1228 gui_position_components(pixel_width);
1229
1230 gui_reset_scroll_region();
1231 /*
1232 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1233 * at the last line here (why does it have to be one row too low?).
1234 */
1235 if (State == ASKMORE || State == CONFIRM)
1236 gui.row = gui.num_rows;
1237
1238 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1239 * the safe side. */
1240 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1241 || gui.num_rows != Rows || gui.num_cols != Columns)
1242 shell_resized();
1243
1244 #ifdef FEAT_GUI_BEOS
1245 vim_unlock_screen();
1246 #endif
1247
1248 gui_update_scrollbars(TRUE);
1249 gui_update_cursor(FALSE, TRUE);
1250 #if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1251 xim_set_status_area();
1252 #endif
1253
1254 busy = FALSE;
1255 /*
1256 * We could have been called again while redrawing the screen.
1257 * Need to do it all again with the latest size then.
1258 */
1259 if (new_pixel_height)
1260 {
1261 pixel_width = new_pixel_width;
1262 pixel_height = new_pixel_height;
1263 new_pixel_width = 0;
1264 new_pixel_height = 0;
1265 goto again;
1266 }
1267 }
1268
1269 /*
1270 * Check if gui_resize_shell() must be called.
1271 */
1272 void
1273 gui_may_resize_shell()
1274 {
1275 int h, w;
1276
1277 if (new_pixel_height)
1278 {
1279 /* careful: gui_resize_shell() may postpone the resize again if we
1280 * were called indirectly by it */
1281 w = new_pixel_width;
1282 h = new_pixel_height;
1283 new_pixel_width = 0;
1284 new_pixel_height = 0;
1285 gui_resize_shell(w, h);
1286 }
1287 }
1288
1289 int
1290 gui_get_shellsize()
1291 {
1292 Rows = gui.num_rows;
1293 Columns = gui.num_cols;
1294 return OK;
1295 }
1296
1297 /*
1298 * Set the size of the Vim shell according to Rows and Columns.
1299 */
1300 /*ARGSUSED*/
1301 void
1302 gui_set_shellsize(mustset, fit_to_display)
1303 int mustset; /* set by the user */
1304 int fit_to_display;
1305 {
1306 int base_width;
1307 int base_height;
1308 int width;
1309 int height;
1310 int min_width;
1311 int min_height;
1312 int screen_w;
1313 int screen_h;
1314
1315 if (!gui.shell_created)
1316 return;
1317
1318 #ifdef MSWIN
1319 /* If not setting to a user specified size and maximized, calculate the
1320 * number of characters that fit in the maximized window. */
1321 if (!mustset && gui_mch_maximized())
1322 {
1323 gui_mch_newfont();
1324 return;
1325 }
1326 #endif
1327
1328 base_width = gui_get_base_width();
1329 base_height = gui_get_base_height();
1330 #ifdef USE_SUN_WORKSHOP
1331 if (!mustset && usingSunWorkShop
1332 && workshop_get_width_height(&width, &height))
1333 {
1334 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1335 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1336 }
1337 else
1338 #endif
1339 {
1340 width = Columns * gui.char_width + base_width;
1341 height = Rows * gui.char_height + base_height;
1342 }
1343
1344 if (fit_to_display)
1345 {
1346 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1347 if (width > screen_w)
1348 {
1349 Columns = (screen_w - base_width) / gui.char_width;
1350 if (Columns < MIN_COLUMNS)
1351 Columns = MIN_COLUMNS;
1352 width = Columns * gui.char_width + base_width;
1353 }
1354 if (height > screen_h)
1355 {
1356 Rows = (screen_h - base_height) / gui.char_height;
1357 check_shellsize();
1358 height = Rows * gui.char_height + base_height;
1359 }
1360 }
1361 gui.num_cols = Columns;
1362 gui.num_rows = Rows;
1363
1364 min_width = base_width + MIN_COLUMNS * gui.char_width;
1365 min_height = base_height + MIN_LINES * gui.char_height;
1366
1367 gui_mch_set_shellsize(width, height, min_width, min_height,
1368 base_width, base_height);
1369 if (fit_to_display)
1370 {
1371 int x, y;
1372
1373 /* Some window managers put the Vim window left of/above the screen. */
1374 gui_mch_update();
1375 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1376 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1377 }
1378
1379 gui_position_components(width);
1380 gui_update_scrollbars(TRUE);
1381 gui_reset_scroll_region();
1382 }
1383
1384 /*
1385 * Called when Rows and/or Columns has changed.
1386 */
1387 void
1388 gui_new_shellsize()
1389 {
1390 gui_reset_scroll_region();
1391 }
1392
1393 /*
1394 * Make scroll region cover whole screen.
1395 */
1396 void
1397 gui_reset_scroll_region()
1398 {
1399 gui.scroll_region_top = 0;
1400 gui.scroll_region_bot = gui.num_rows - 1;
1401 gui.scroll_region_left = 0;
1402 gui.scroll_region_right = gui.num_cols - 1;
1403 }
1404
1405 void
1406 gui_start_highlight(mask)
1407 int mask;
1408 {
1409 if (mask > HL_ALL) /* highlight code */
1410 gui.highlight_mask = mask;
1411 else /* mask */
1412 gui.highlight_mask |= mask;
1413 }
1414
1415 void
1416 gui_stop_highlight(mask)
1417 int mask;
1418 {
1419 if (mask > HL_ALL) /* highlight code */
1420 gui.highlight_mask = HL_NORMAL;
1421 else /* mask */
1422 gui.highlight_mask &= ~mask;
1423 }
1424
1425 /*
1426 * Clear a rectangular region of the screen from text pos (row1, col1) to
1427 * (row2, col2) inclusive.
1428 */
1429 void
1430 gui_clear_block(row1, col1, row2, col2)
1431 int row1;
1432 int col1;
1433 int row2;
1434 int col2;
1435 {
1436 /* Clear the selection if we are about to write over it */
1437 clip_may_clear_selection(row1, row2);
1438
1439 gui_mch_clear_block(row1, col1, row2, col2);
1440
1441 /* Invalidate cursor if it was in this block */
1442 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1443 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1444 gui.cursor_is_valid = FALSE;
1445 }
1446
1447 /*
1448 * Write code to update the cursor later. This avoids the need to flush the
1449 * output buffer before calling gui_update_cursor().
1450 */
1451 void
1452 gui_update_cursor_later()
1453 {
1454 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1455 }
1456
1457 void
1458 gui_write(s, len)
1459 char_u *s;
1460 int len;
1461 {
1462 char_u *p;
1463 int arg1 = 0, arg2 = 0;
1464 /* this doesn't make sense, disabled until someone can explain why it
1465 * would be needed */
1466 #if 0 && (defined(RISCOS) || defined(WIN16))
1467 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1468 our redraw speed will suffer */
1469 #else
1470 int force_cursor = FALSE; /* force cursor update */
1471 #endif
1472 int force_scrollbar = FALSE;
1473 static win_T *old_curwin = NULL;
1474
1475 /* #define DEBUG_GUI_WRITE */
1476 #ifdef DEBUG_GUI_WRITE
1477 {
1478 int i;
1479 char_u *str;
1480
1481 printf("gui_write(%d):\n ", len);
1482 for (i = 0; i < len; i++)
1483 if (s[i] == ESC)
1484 {
1485 if (i != 0)
1486 printf("\n ");
1487 printf("<ESC>");
1488 }
1489 else
1490 {
1491 str = transchar_byte(s[i]);
1492 if (str[0] && str[1])
1493 printf("<%s>", (char *)str);
1494 else
1495 printf("%s", (char *)str);
1496 }
1497 printf("\n");
1498 }
1499 #endif
1500 while (len)
1501 {
1502 if (s[0] == ESC && s[1] == '|')
1503 {
1504 p = s + 2;
1505 if (VIM_ISDIGIT(*p))
1506 {
1507 arg1 = getdigits(&p);
1508 if (p > s + len)
1509 break;
1510 if (*p == ';')
1511 {
1512 ++p;
1513 arg2 = getdigits(&p);
1514 if (p > s + len)
1515 break;
1516 }
1517 }
1518 switch (*p)
1519 {
1520 case 'C': /* Clear screen */
1521 clip_scroll_selection(9999);
1522 gui_mch_clear_all();
1523 gui.cursor_is_valid = FALSE;
1524 force_scrollbar = TRUE;
1525 break;
1526 case 'M': /* Move cursor */
1527 gui_set_cursor(arg1, arg2);
1528 break;
1529 case 's': /* force cursor (shape) update */
1530 force_cursor = TRUE;
1531 break;
1532 case 'R': /* Set scroll region */
1533 if (arg1 < arg2)
1534 {
1535 gui.scroll_region_top = arg1;
1536 gui.scroll_region_bot = arg2;
1537 }
1538 else
1539 {
1540 gui.scroll_region_top = arg2;
1541 gui.scroll_region_bot = arg1;
1542 }
1543 break;
1544 #ifdef FEAT_VERTSPLIT
1545 case 'V': /* Set vertical scroll region */
1546 if (arg1 < arg2)
1547 {
1548 gui.scroll_region_left = arg1;
1549 gui.scroll_region_right = arg2;
1550 }
1551 else
1552 {
1553 gui.scroll_region_left = arg2;
1554 gui.scroll_region_right = arg1;
1555 }
1556 break;
1557 #endif
1558 case 'd': /* Delete line */
1559 gui_delete_lines(gui.row, 1);
1560 break;
1561 case 'D': /* Delete lines */
1562 gui_delete_lines(gui.row, arg1);
1563 break;
1564 case 'i': /* Insert line */
1565 gui_insert_lines(gui.row, 1);
1566 break;
1567 case 'I': /* Insert lines */
1568 gui_insert_lines(gui.row, arg1);
1569 break;
1570 case '$': /* Clear to end-of-line */
1571 gui_clear_block(gui.row, gui.col, gui.row,
1572 (int)Columns - 1);
1573 break;
1574 case 'h': /* Turn on highlighting */
1575 gui_start_highlight(arg1);
1576 break;
1577 case 'H': /* Turn off highlighting */
1578 gui_stop_highlight(arg1);
1579 break;
1580 case 'f': /* flash the window (visual bell) */
1581 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1582 break;
1583 default:
1584 p = s + 1; /* Skip the ESC */
1585 break;
1586 }
1587 len -= (int)(++p - s);
1588 s = p;
1589 }
1590 else if (
1591 #ifdef EBCDIC
1592 CtrlChar(s[0]) != 0 /* Ctrl character */
1593 #else
1594 s[0] < 0x20 /* Ctrl character */
1595 #endif
1596 #ifdef FEAT_SIGN_ICONS
1597 && s[0] != SIGN_BYTE
1598 # ifdef FEAT_NETBEANS_INTG
1599 && s[0] != MULTISIGN_BYTE
1600 # endif
1601 #endif
1602 )
1603 {
1604 if (s[0] == '\n') /* NL */
1605 {
1606 gui.col = 0;
1607 if (gui.row < gui.scroll_region_bot)
1608 gui.row++;
1609 else
1610 gui_delete_lines(gui.scroll_region_top, 1);
1611 }
1612 else if (s[0] == '\r') /* CR */
1613 {
1614 gui.col = 0;
1615 }
1616 else if (s[0] == '\b') /* Backspace */
1617 {
1618 if (gui.col)
1619 --gui.col;
1620 }
1621 else if (s[0] == Ctrl_L) /* cursor-right */
1622 {
1623 ++gui.col;
1624 }
1625 else if (s[0] == Ctrl_G) /* Beep */
1626 {
1627 gui_mch_beep();
1628 }
1629 /* Other Ctrl character: shouldn't happen! */
1630
1631 --len; /* Skip this char */
1632 ++s;
1633 }
1634 else
1635 {
1636 p = s;
1637 while (len > 0 && (
1638 #ifdef EBCDIC
1639 CtrlChar(*p) == 0
1640 #else
1641 *p >= 0x20
1642 #endif
1643 #ifdef FEAT_SIGN_ICONS
1644 || *p == SIGN_BYTE
1645 # ifdef FEAT_NETBEANS_INTG
1646 || *p == MULTISIGN_BYTE
1647 # endif
1648 #endif
1649 ))
1650 {
1651 len--;
1652 p++;
1653 }
1654 gui_outstr(s, (int)(p - s));
1655 s = p;
1656 }
1657 }
1658
1659 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1660 * set). */
1661 if (force_cursor)
1662 gui_update_cursor(TRUE, TRUE);
1663
1664 /* When switching to another window the dragging must have stopped.
1665 * Required for GTK, dragged_sb isn't reset. */
1666 if (old_curwin != curwin)
1667 gui.dragged_sb = SBAR_NONE;
1668
1669 /* Update the scrollbars after clearing the screen or when switched
1670 * to another window.
1671 * Update the horizontal scrollbar always, it's difficult to check all
1672 * situations where it might change. */
1673 if (force_scrollbar || old_curwin != curwin)
1674 gui_update_scrollbars(force_scrollbar);
1675 else
1676 gui_update_horiz_scrollbar(FALSE);
1677 old_curwin = curwin;
1678
1679 /*
1680 * We need to make sure this is cleared since Athena doesn't tell us when
1681 * he is done dragging. Do the same for GTK.
1682 */
1683 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
1684 gui.dragged_sb = SBAR_NONE;
1685 #endif
1686
1687 gui_mch_flush(); /* In case vim decides to take a nap */
1688 }
1689
1690 /*
1691 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1692 * produces wrong results. Call gui_dont_update_cursor() before that code and
1693 * gui_can_update_cursor() afterwards.
1694 */
1695 void
1696 gui_dont_update_cursor()
1697 {
1698 if (gui.in_use)
1699 {
1700 /* Undraw the cursor now, we probably can't do it after the change. */
1701 gui_undraw_cursor();
1702 can_update_cursor = FALSE;
1703 }
1704 }
1705
1706 void
1707 gui_can_update_cursor()
1708 {
1709 can_update_cursor = TRUE;
1710 /* No need to update the cursor right now, there is always more output
1711 * after scrolling. */
1712 }
1713
1714 static void
1715 gui_outstr(s, len)
1716 char_u *s;
1717 int len;
1718 {
1719 int this_len;
1720 #ifdef FEAT_MBYTE
1721 int cells;
1722 #endif
1723
1724 if (len == 0)
1725 return;
1726
1727 if (len < 0)
1728 len = (int)STRLEN(s);
1729
1730 while (len > 0)
1731 {
1732 #ifdef FEAT_MBYTE
1733 if (has_mbyte)
1734 {
1735 /* Find out how many chars fit in the current line. */
1736 cells = 0;
1737 for (this_len = 0; this_len < len; )
1738 {
1739 cells += (*mb_ptr2cells)(s + this_len);
1740 if (gui.col + cells > Columns)
1741 break;
1742 this_len += (*mb_ptr2len_check)(s + this_len);
1743 }
1744 if (this_len > len)
1745 this_len = len; /* don't include following composing char */
1746 }
1747 else
1748 #endif
1749 if (gui.col + len > Columns)
1750 this_len = Columns - gui.col;
1751 else
1752 this_len = len;
1753
1754 (void)gui_outstr_nowrap(s, this_len,
1755 0, (guicolor_T)0, (guicolor_T)0, 0);
1756 s += this_len;
1757 len -= this_len;
1758 #ifdef FEAT_MBYTE
1759 /* fill up for a double-width char that doesn't fit. */
1760 if (len > 0 && gui.col < Columns)
1761 (void)gui_outstr_nowrap((char_u *)" ", 1,
1762 0, (guicolor_T)0, (guicolor_T)0, 0);
1763 #endif
1764 /* The cursor may wrap to the next line. */
1765 if (gui.col >= Columns)
1766 {
1767 gui.col = 0;
1768 gui.row++;
1769 }
1770 }
1771 }
1772
1773 /*
1774 * Output one character (may be one or two display cells).
1775 * Caller must check for valid "off".
1776 * Returns FAIL or OK, just like gui_outstr_nowrap().
1777 */
1778 static int
1779 gui_screenchar(off, flags, fg, bg, back)
1780 int off; /* Offset from start of screen */
1781 int flags;
1782 guicolor_T fg, bg; /* colors for cursor */
1783 int back; /* backup this many chars when using bold trick */
1784 {
1785 #ifdef FEAT_MBYTE
1786 char_u buf[MB_MAXBYTES + 1];
1787
1788 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1789 if (enc_utf8 && ScreenLines[off] == 0)
1790 return OK;
1791
1792 if (enc_utf8 && ScreenLinesUC[off] != 0)
1793 /* Draw UTF-8 multi-byte character. */
1794 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1795 flags, fg, bg, back);
1796
1797 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1798 {
1799 buf[0] = ScreenLines[off];
1800 buf[1] = ScreenLines2[off];
1801 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1802 }
1803
1804 /* Draw non-multi-byte character or DBCS character. */
1805 return gui_outstr_nowrap(ScreenLines + off,
1806 enc_dbcs ? (*mb_ptr2len_check)(ScreenLines + off) : 1,
1807 flags, fg, bg, back);
1808 #else
1809 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1810 #endif
1811 }
1812
1813 #ifdef HAVE_GTK2
1814 /*
1815 * Output the string at the given screen position. This is used in place
1816 * of gui_screenchar() where possible because Pango needs as much context
1817 * as possible to work nicely. It's a lot faster as well.
1818 */
1819 static int
1820 gui_screenstr(off, len, flags, fg, bg, back)
1821 int off; /* Offset from start of screen */
1822 int len; /* string length in screen cells */
1823 int flags;
1824 guicolor_T fg, bg; /* colors for cursor */
1825 int back; /* backup this many chars when using bold trick */
1826 {
1827 char_u *buf;
1828 int outlen = 0;
1829 int i;
1830 int retval;
1831
1832 if (len <= 0) /* "cannot happen"? */
1833 return OK;
1834
1835 if (enc_utf8)
1836 {
1837 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1838 if (buf == NULL)
1839 return OK; /* not much we could do here... */
1840
1841 for (i = off; i < off + len; ++i)
1842 {
1843 if (ScreenLines[i] == 0)
1844 continue; /* skip second half of double-width char */
1845
1846 if (ScreenLinesUC[i] == 0)
1847 buf[outlen++] = ScreenLines[i];
1848 else
1849 outlen += utfc_char2bytes(i, buf + outlen);
1850 }
1851
1852 buf[outlen] = NUL; /* only to aid debugging */
1853 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1854 vim_free(buf);
1855
1856 return retval;
1857 }
1858 else if (enc_dbcs == DBCS_JPNU)
1859 {
1860 buf = alloc((unsigned)(len * 2 + 1));
1861 if (buf == NULL)
1862 return OK; /* not much we could do here... */
1863
1864 for (i = off; i < off + len; ++i)
1865 {
1866 buf[outlen++] = ScreenLines[i];
1867
1868 /* handle double-byte single-width char */
1869 if (ScreenLines[i] == 0x8e)
1870 buf[outlen++] = ScreenLines2[i];
1871 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1872 buf[outlen++] = ScreenLines[++i];
1873 }
1874
1875 buf[outlen] = NUL; /* only to aid debugging */
1876 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1877 vim_free(buf);
1878
1879 return retval;
1880 }
1881 else
1882 {
1883 return gui_outstr_nowrap(&ScreenLines[off], len,
1884 flags, fg, bg, back);
1885 }
1886 }
1887 #endif /* HAVE_GTK2 */
1888
1889 /*
1890 * Output the given string at the current cursor position. If the string is
1891 * too long to fit on the line, then it is truncated.
1892 * "flags":
1893 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1894 * actually draw (an inverted) cursor.
1895 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparant
1896 * background.
1897 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1898 * it.
1899 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1900 * FAIL (the caller should start drawing "back" chars back).
1901 */
1902 int
1903 gui_outstr_nowrap(s, len, flags, fg, bg, back)
1904 char_u *s;
1905 int len;
1906 int flags;
1907 guicolor_T fg, bg; /* colors for cursor */
1908 int back; /* backup this many chars when using bold trick */
1909 {
1910 long_u highlight_mask;
1911 long_u hl_mask_todo;
1912 guicolor_T fg_color;
1913 guicolor_T bg_color;
1914 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
1915 GuiFont font = NOFONT;
1916 # ifdef FEAT_XFONTSET
1917 GuiFontset fontset = NOFONTSET;
1918 # endif
1919 #endif
1920 attrentry_T *aep = NULL;
1921 int draw_flags;
1922 int col = gui.col;
1923 #ifdef FEAT_SIGN_ICONS
1924 int draw_sign = FALSE;
1925 # ifdef FEAT_NETBEANS_INTG
1926 int multi_sign = FALSE;
1927 # endif
1928 #endif
1929
1930 if (len < 0)
1931 len = (int)STRLEN(s);
1932 if (len == 0)
1933 return OK;
1934
1935 #ifdef FEAT_SIGN_ICONS
1936 if (*s == SIGN_BYTE
1937 # ifdef FEAT_NETBEANS_INTG
1938 || *s == MULTISIGN_BYTE
1939 # endif
1940 )
1941 {
1942 # ifdef FEAT_NETBEANS_INTG
1943 if (*s == MULTISIGN_BYTE)
1944 multi_sign = TRUE;
1945 # endif
1946 /* draw spaces instead */
1947 s = (char_u *)" ";
1948 if (len == 1 && col > 0)
1949 --col;
1950 len = 2;
1951 draw_sign = TRUE;
1952 highlight_mask = 0;
1953 }
1954 else
1955 #endif
1956 if (gui.highlight_mask > HL_ALL)
1957 {
1958 aep = syn_gui_attr2entry(gui.highlight_mask);
1959 if (aep == NULL) /* highlighting not set */
1960 highlight_mask = 0;
1961 else
1962 highlight_mask = aep->ae_attr;
1963 }
1964 else
1965 highlight_mask = gui.highlight_mask;
1966 hl_mask_todo = highlight_mask;
1967
1968 #if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
1969 /* Set the font */
1970 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
1971 font = aep->ae_u.gui.font;
1972 # ifdef FEAT_XFONTSET
1973 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
1974 fontset = aep->ae_u.gui.fontset;
1975 # endif
1976 else
1977 {
1978 # ifdef FEAT_XFONTSET
1979 if (gui.fontset != NOFONTSET)
1980 fontset = gui.fontset;
1981 else
1982 # endif
1983 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
1984 {
1985 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
1986 {
1987 font = gui.boldital_font;
1988 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
1989 }
1990 else if (gui.bold_font != NOFONT)
1991 {
1992 font = gui.bold_font;
1993 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
1994 }
1995 else
1996 font = gui.norm_font;
1997 }
1998 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
1999 {
2000 font = gui.ital_font;
2001 hl_mask_todo &= ~HL_ITALIC;
2002 }
2003 else
2004 font = gui.norm_font;
2005 }
2006 # ifdef FEAT_XFONTSET
2007 if (fontset != NOFONTSET)
2008 gui_mch_set_fontset(fontset);
2009 else
2010 # endif
2011 gui_mch_set_font(font);
2012 #endif
2013
2014 draw_flags = 0;
2015
2016 /* Set the color */
2017 bg_color = gui.back_pixel;
2018 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2019 {
2020 draw_flags |= DRAW_CURSOR;
2021 fg_color = fg;
2022 bg_color = bg;
2023 }
2024 else if (aep != NULL)
2025 {
2026 fg_color = aep->ae_u.gui.fg_color;
2027 if (fg_color == INVALCOLOR)
2028 fg_color = gui.norm_pixel;
2029 bg_color = aep->ae_u.gui.bg_color;
2030 if (bg_color == INVALCOLOR)
2031 bg_color = gui.back_pixel;
2032 }
2033 else
2034 fg_color = gui.norm_pixel;
2035
2036 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2037 {
2038 #if defined(AMIGA) || defined(RISCOS)
2039 gui_mch_set_colors(bg_color, fg_color);
2040 #else
2041 gui_mch_set_fg_color(bg_color);
2042 gui_mch_set_bg_color(fg_color);
2043 #endif
2044 }
2045 else
2046 {
2047 #if defined(AMIGA) || defined(RISCOS)
2048 gui_mch_set_colors(fg_color, bg_color);
2049 #else
2050 gui_mch_set_fg_color(fg_color);
2051 gui_mch_set_bg_color(bg_color);
2052 #endif
2053 }
2054
2055 /* Clear the selection if we are about to write over it */
2056 if (!(flags & GUI_MON_NOCLEAR))
2057 clip_may_clear_selection(gui.row, gui.row);
2058
2059
2060 #ifndef MSWIN16_FASTTEXT
2061 /* If there's no bold font, then fake it */
2062 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2063 draw_flags |= DRAW_BOLD;
2064 #endif
2065
2066 /*
2067 * When drawing bold or italic characters the spill-over from the left
2068 * neighbor may be destroyed. Let the caller backup to start redrawing
2069 * just after a blank.
2070 */
2071 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2072 return FAIL;
2073
2074 #if defined(RISCOS) || defined(HAVE_GTK2)
2075 /* If there's no italic font, then fake it.
2076 * For GTK2, we don't need a different font for italic style. */
2077 if (hl_mask_todo & HL_ITALIC)
2078 draw_flags |= DRAW_ITALIC;
2079
2080 /* Do we underline the text? */
2081 if (hl_mask_todo & HL_UNDERLINE)
2082 draw_flags |= DRAW_UNDERL;
2083 #else
2084 /* Do we underline the text? */
2085 if ((hl_mask_todo & HL_UNDERLINE)
2086 # ifndef MSWIN16_FASTTEXT
2087 || (hl_mask_todo & HL_ITALIC)
2088 # endif
2089 )
2090 draw_flags |= DRAW_UNDERL;
2091 #endif
2092
2093 /* Do we draw transparantly? */
2094 if (flags & GUI_MON_TRS_CURSOR)
2095 draw_flags |= DRAW_TRANSP;
2096
2097 /*
2098 * Draw the text.
2099 */
2100 #ifdef HAVE_GTK2
2101 /* The value returned is the length in display cells */
2102 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2103 #else
2104 # ifdef FEAT_MBYTE
2105 if (enc_utf8)
2106 {
2107 int start; /* index of bytes to be drawn */
2108 int cells; /* cellwidth of bytes to be drawn */
2109 int thislen; /* length of bytes to be drawin */
2110 int cn; /* cellwidth of current char */
2111 int i; /* index of current char */
2112 int c; /* current char value */
2113 int cl; /* byte length of current char */
2114 int comping; /* current char is composing */
2115 int scol = col; /* screen column */
2116 int dowide; /* use 'guifontwide' */
2117
2118 /* Break the string at a composing character, it has to be drawn on
2119 * top of the previous character. */
2120 start = 0;
2121 cells = 0;
2122 for (i = 0; i < len; i += cl)
2123 {
2124 c = utf_ptr2char(s + i);
2125 cn = utf_char2cells(c);
2126 if (cn > 1
2127 # ifdef FEAT_XFONTSET
2128 && fontset == NOFONTSET
2129 # endif
2130 && gui.wide_font != NOFONT)
2131 dowide = TRUE;
2132 else
2133 dowide = FALSE;
2134 comping = utf_iscomposing(c);
2135 if (!comping) /* count cells from non-composing chars */
2136 cells += cn;
2137 cl = utf_ptr2len_check(s + i);
2138 if (cl == 0) /* hit end of string */
2139 len = i + cl; /* len must be wrong "cannot happen" */
2140
2141 /* print the string so far if it's the last character or there is
2142 * a composing character. */
2143 if (i + cl >= len || (comping && i > start) || dowide
2144 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2145 || (cn > 1
2146 # ifdef FEAT_XFONTSET
2147 /* No fontset: At least draw char after wide char at
2148 * right position. */
2149 && fontset == NOFONTSET
2150 # endif
2151 )
2152 # endif
2153 )
2154 {
2155 if (comping || dowide)
2156 thislen = i - start;
2157 else
2158 thislen = i - start + cl;
2159 if (thislen > 0)
2160 {
2161 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2162 draw_flags);
2163 start += thislen;
2164 }
2165 scol += cells;
2166 cells = 0;
2167 if (dowide)
2168 {
2169 gui_mch_set_font(gui.wide_font);
2170 gui_mch_draw_string(gui.row, scol - cn,
2171 s + start, cl, draw_flags);
2172 gui_mch_set_font(font);
2173 start += cl;
2174 }
2175
2176 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
2177 /* No fontset: draw a space to fill the gap after a wide char */
2178 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2179 # ifdef FEAT_XFONTSET
2180 && fontset == NOFONTSET
2181 # endif
2182 && !dowide)
2183 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2184 1, draw_flags);
2185 # endif
2186 }
2187 /* Draw a composing char on top of the previous char. */
2188 if (comping)
2189 {
2190 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2191 draw_flags | DRAW_TRANSP);
2192 start = i + cl;
2193 }
2194 }
2195 /* The stuff below assumes "len" is the length in screen columns. */
2196 len = scol - col;
2197 }
2198 else
2199 # endif
2200 {
2201 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2202 # ifdef FEAT_MBYTE
2203 if (enc_dbcs == DBCS_JPNU)
2204 {
2205 int clen = 0;
2206 int i;
2207
2208 /* Get the length in display cells, this can be different from the
2209 * number of bytes for "euc-jp". */
2210 for (i = 0; i < len; i += (*mb_ptr2len_check)(s + i))
2211 clen += (*mb_ptr2cells)(s + i);
2212 len = clen;
2213 }
2214 # endif
2215 }
2216 #endif /* !HAVE_GTK2 */
2217
2218 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2219 gui.col = col + len;
2220
2221 /* May need to invert it when it's part of the selection. */
2222 if (flags & GUI_MON_NOCLEAR)
2223 clip_may_redraw_selection(gui.row, col, len);
2224
2225 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2226 {
2227 /* Invalidate the old physical cursor position if we wrote over it */
2228 if (gui.cursor_row == gui.row
2229 && gui.cursor_col >= col
2230 && gui.cursor_col < col + len)
2231 gui.cursor_is_valid = FALSE;
2232 }
2233
2234 #ifdef FEAT_SIGN_ICONS
2235 if (draw_sign)
2236 /* Draw the sign on top of the spaces. */
2237 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2238 # ifdef FEAT_NETBEANS_INTG
2239 if (multi_sign)
2240 netbeans_draw_multisign_indicator(gui.row);
2241 # endif
2242 #endif
2243
2244 return OK;
2245 }
2246
2247 /*
2248 * Un-draw the cursor. Actually this just redraws the character at the given
2249 * position. The character just before it too, for when it was in bold.
2250 */
2251 void
2252 gui_undraw_cursor()
2253 {
2254 if (gui.cursor_is_valid)
2255 {
2256 #ifdef FEAT_HANGULIN
2257 if (composing_hangul
2258 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2259 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2260 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2261 gui.norm_pixel, gui.back_pixel, 0);
2262 else
2263 {
2264 #endif
2265 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2266 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2267 && gui.cursor_col > 0)
2268 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2269 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2270 #ifdef FEAT_HANGULIN
2271 if (composing_hangul)
2272 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2273 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2274 }
2275 #endif
2276 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2277 * here in case it wasn't needed to undraw it. */
2278 gui.cursor_is_valid = FALSE;
2279 }
2280 }
2281
2282 void
2283 gui_redraw(x, y, w, h)
2284 int x;
2285 int y;
2286 int w;
2287 int h;
2288 {
2289 int row1, col1, row2, col2;
2290
2291 row1 = Y_2_ROW(y);
2292 col1 = X_2_COL(x);
2293 row2 = Y_2_ROW(y + h - 1);
2294 col2 = X_2_COL(x + w - 1);
2295
2296 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2297
2298 /*
2299 * We may need to redraw the cursor, but don't take it upon us to change
2300 * its location after a scroll.
2301 * (maybe be more strict even and test col too?)
2302 * These things may be outside the update/clipping region and reality may
2303 * not reflect Vims internal ideas if these operations are clipped away.
2304 */
2305 if (gui.row == gui.cursor_row)
2306 gui_update_cursor(TRUE, TRUE);
2307 }
2308
2309 /*
2310 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2311 * from col1 to col2 (inclusive).
2312 * Return TRUE when the character before the first drawn character has
2313 * different attributes (may have to be redrawn too).
2314 */
2315 int
2316 gui_redraw_block(row1, col1, row2, col2, flags)
2317 int row1;
2318 int col1;
2319 int row2;
2320 int col2;
2321 int flags; /* flags for gui_outstr_nowrap() */
2322 {
2323 int old_row, old_col;
2324 long_u old_hl_mask;
2325 int off;
2326 char_u first_attr;
2327 int idx, len;
2328 int back, nback;
2329 int retval = FALSE;
2330 #ifdef FEAT_MBYTE
2331 int orig_col1, orig_col2;
2332 #endif
2333
2334 /* Don't try to update when ScreenLines is not valid */
2335 if (!screen_cleared || ScreenLines == NULL)
2336 return retval;
2337
2338 /* Don't try to draw outside the shell! */
2339 /* Check everything, strange values may be caused by a big border width */
2340 col1 = check_col(col1);
2341 col2 = check_col(col2);
2342 row1 = check_row(row1);
2343 row2 = check_row(row2);
2344
2345 /* Remember where our cursor was */
2346 old_row = gui.row;
2347 old_col = gui.col;
2348 old_hl_mask = gui.highlight_mask;
2349 #ifdef FEAT_MBYTE
2350 orig_col1 = col1;
2351 orig_col2 = col2;
2352 #endif
2353
2354 for (gui.row = row1; gui.row <= row2; gui.row++)
2355 {
2356 #ifdef FEAT_MBYTE
2357 /* When only half of a double-wide character is in the block, include
2358 * the other half. */
2359 col1 = orig_col1;
2360 col2 = orig_col2;
2361 off = LineOffset[gui.row];
2362 if (enc_dbcs != 0)
2363 {
2364 if (col1 > 0)
2365 col1 -= dbcs_screen_head_off(ScreenLines + off,
2366 ScreenLines + off + col1);
2367 col2 += dbcs_screen_tail_off(ScreenLines + off,
2368 ScreenLines + off + col2);
2369 }
2370 else if (enc_utf8)
2371 {
2372 if (ScreenLines[off + col1] == 0)
2373 --col1;
2374 # ifdef HAVE_GTK2
2375 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2376 ++col2;
2377 # endif
2378 }
2379 #endif
2380 gui.col = col1;
2381 off = LineOffset[gui.row] + gui.col;
2382 len = col2 - col1 + 1;
2383
2384 /* Find how many chars back this highlighting starts, or where a space
2385 * is. Needed for when the bold trick is used */
2386 for (back = 0; back < col1; ++back)
2387 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2388 || ScreenLines[off - 1 - back] == ' ')
2389 break;
2390 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2391 && ScreenLines[off - 1] != ' ');
2392
2393 /* Break it up in strings of characters with the same attributes. */
2394 /* Print UTF-8 characters individually. */
2395 while (len > 0)
2396 {
2397 first_attr = ScreenAttrs[off];
2398 gui.highlight_mask = first_attr;
2399 #if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2400 if (enc_utf8 && ScreenLinesUC[off] != 0)
2401 {
2402 /* output multi-byte character separately */
2403 nback = gui_screenchar(off, flags,
2404 (guicolor_T)0, (guicolor_T)0, back);
2405 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2406 idx = 2;
2407 else
2408 idx = 1;
2409 }
2410 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2411 {
2412 /* output double-byte, single-width character separately */
2413 nback = gui_screenchar(off, flags,
2414 (guicolor_T)0, (guicolor_T)0, back);
2415 idx = 1;
2416 }
2417 else
2418 #endif
2419 {
2420 #ifdef HAVE_GTK2
2421 for (idx = 0; idx < len; ++idx)
2422 {
2423 if (enc_utf8 && ScreenLines[off + idx] == 0)
2424 continue; /* skip second half of double-width char */
2425 if (ScreenAttrs[off + idx] != first_attr)
2426 break;
2427 }
2428 /* gui_screenstr() takes care of multibyte chars */
2429 nback = gui_screenstr(off, idx, flags,
2430 (guicolor_T)0, (guicolor_T)0, back);
2431 #else
2432 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2433 idx++)
2434 {
2435 # ifdef FEAT_MBYTE
2436 /* Stop at a multi-byte Unicode character. */
2437 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2438 break;
2439 if (enc_dbcs == DBCS_JPNU)
2440 {
2441 /* Stop at a double-byte single-width char. */
2442 if (ScreenLines[off + idx] == 0x8e)
2443 break;
2444 if (len > 1 && (*mb_ptr2len_check)(ScreenLines
2445 + off + idx) == 2)
2446 ++idx; /* skip second byte of double-byte char */
2447 }
2448 # endif
2449 }
2450 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2451 (guicolor_T)0, (guicolor_T)0, back);
2452 #endif
2453 }
2454 if (nback == FAIL)
2455 {
2456 /* Must back up to start drawing where a bold or italic word
2457 * starts. */
2458 off -= back;
2459 len += back;
2460 gui.col -= back;
2461 }
2462 else
2463 {
2464 off += idx;
2465 len -= idx;
2466 }
2467 back = 0;
2468 }
2469 }
2470
2471 /* Put the cursor back where it was */
2472 gui.row = old_row;
2473 gui.col = old_col;
2474 gui.highlight_mask = old_hl_mask;
2475
2476 return retval;
2477 }
2478
2479 static void
2480 gui_delete_lines(row, count)
2481 int row;
2482 int count;
2483 {
2484 if (count <= 0)
2485 return;
2486
2487 if (row + count > gui.scroll_region_bot)
2488 /* Scrolled out of region, just blank the lines out */
2489 gui_clear_block(row, gui.scroll_region_left,
2490 gui.scroll_region_bot, gui.scroll_region_right);
2491 else
2492 {
2493 gui_mch_delete_lines(row, count);
2494
2495 /* If the cursor was in the deleted lines it's now gone. If the
2496 * cursor was in the scrolled lines adjust its position. */
2497 if (gui.cursor_row >= row
2498 && gui.cursor_col >= gui.scroll_region_left
2499 && gui.cursor_col <= gui.scroll_region_right)
2500 {
2501 if (gui.cursor_row < row + count)
2502 gui.cursor_is_valid = FALSE;
2503 else if (gui.cursor_row <= gui.scroll_region_bot)
2504 gui.cursor_row -= count;
2505 }
2506 }
2507 }
2508
2509 static void
2510 gui_insert_lines(row, count)
2511 int row;
2512 int count;
2513 {
2514 if (count <= 0)
2515 return;
2516
2517 if (row + count > gui.scroll_region_bot)
2518 /* Scrolled out of region, just blank the lines out */
2519 gui_clear_block(row, gui.scroll_region_left,
2520 gui.scroll_region_bot, gui.scroll_region_right);
2521 else
2522 {
2523 gui_mch_insert_lines(row, count);
2524
2525 if (gui.cursor_row >= gui.row
2526 && gui.cursor_col >= gui.scroll_region_left
2527 && gui.cursor_col <= gui.scroll_region_right)
2528 {
2529 if (gui.cursor_row <= gui.scroll_region_bot - count)
2530 gui.cursor_row += count;
2531 else if (gui.cursor_row <= gui.scroll_region_bot)
2532 gui.cursor_is_valid = FALSE;
2533 }
2534 }
2535 }
2536
2537 /*
2538 * The main GUI input routine. Waits for a character from the keyboard.
2539 * wtime == -1 Wait forever.
2540 * wtime == 0 Don't wait.
2541 * wtime > 0 Wait wtime milliseconds for a character.
2542 * Returns OK if a character was found to be available within the given time,
2543 * or FAIL otherwise.
2544 */
2545 int
2546 gui_wait_for_chars(wtime)
2547 long wtime;
2548 {
2549 int retval;
2550 #ifdef FEAT_AUTOCMD
2551 static int once_already = 0;
2552 #endif
2553
2554 /*
2555 * If we're going to wait a bit, update the menus and mouse shape for the
2556 * current State.
2557 */
2558 if (wtime != 0)
2559 {
2560 #ifdef FEAT_MENU
2561 gui_update_menus(0);
2562 #endif
2563 }
2564
2565 gui_mch_update();
2566 if (input_available()) /* Got char, return immediately */
2567 {
2568 #ifdef FEAT_AUTOCMD
2569 once_already = 0;
2570 #endif
2571 return OK;
2572 }
2573 if (wtime == 0) /* Don't wait for char */
2574 {
2575 #ifdef FEAT_AUTOCMD
2576 once_already = 0;
2577 #endif
2578 return FAIL;
2579 }
2580
2581 /* Before waiting, flush any output to the screen. */
2582 gui_mch_flush();
2583
2584 if (wtime > 0)
2585 {
2586 /* Blink when waiting for a character. Probably only does something
2587 * for showmatch() */
2588 gui_mch_start_blink();
2589 retval = gui_mch_wait_for_chars(wtime);
2590 gui_mch_stop_blink();
2591 #ifdef FEAT_AUTOCMD
2592 once_already = 0;
2593 #endif
2594 return retval;
2595 }
2596
2597 /*
2598 * While we are waiting indefenitely for a character, blink the cursor.
2599 */
2600 gui_mch_start_blink();
2601
2602
2603 #ifdef FEAT_AUTOCMD
2604 /* If there is no character available within 2 seconds (default),
2605 * write the autoscript file to disk */
2606 if (once_already == 2)
2607 {
2608 updatescript(0);
2609 retval = gui_mch_wait_for_chars(-1L);
2610 once_already = 0;
2611 }
2612 else if (once_already == 1)
2613 {
2614 setcursor();
2615 once_already = 2;
2616 retval = 0;
2617 }
2618 else
2619 #endif
2620 if (gui_mch_wait_for_chars(p_ut) != OK)
2621 {
2622 #ifdef FEAT_AUTOCMD
2623 if (has_cursorhold() && get_real_state() == NORMAL_BUSY)
2624 {
2625 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
2626 update_screen(VALID);
2627 showruler(FALSE);
2628 setcursor();
2629
2630 once_already = 1;
2631 retval = 0;
2632 }
2633 else
2634 #endif
2635 {
2636 updatescript(0);
2637 retval = gui_mch_wait_for_chars(-1L);
2638 #ifdef FEAT_AUTOCMD
2639 once_already = 0;
2640 #endif
2641 }
2642 }
2643 else
2644 retval = OK;
2645
2646 gui_mch_stop_blink();
2647 return retval;
2648 }
2649
2650 /*
2651 * Fill buffer with mouse coordinates encoded for check_termcode().
2652 */
2653 static void
2654 fill_mouse_coord(p, col, row)
2655 char_u *p;
2656 int col;
2657 int row;
2658 {
2659 p[0] = (char_u)(col / 128 + ' ' + 1);
2660 p[1] = (char_u)(col % 128 + ' ' + 1);
2661 p[2] = (char_u)(row / 128 + ' ' + 1);
2662 p[3] = (char_u)(row % 128 + ' ' + 1);
2663 }
2664
2665 /*
2666 * Generic mouse support function. Add a mouse event to the input buffer with
2667 * the given properties.
2668 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2669 * MOUSE_X1, MOUSE_X2
2670 * MOUSE_DRAG, or MOUSE_RELEASE.
2671 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2672 * x, y --- Coordinates of mouse in pixels.
2673 * repeated_click --- TRUE if this click comes only a short time after a
2674 * previous click.
2675 * modifiers --- Bit field which may be any of the following modifiers
2676 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2677 * This function will ignore drag events where the mouse has not moved to a new
2678 * character.
2679 */
2680 void
2681 gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2682 int button;
2683 int x;
2684 int y;
2685 int repeated_click;
2686 int_u modifiers;
2687 {
2688 static int prev_row = 0, prev_col = 0;
2689 static int prev_button = -1;
2690 static int num_clicks = 1;
2691 char_u string[10];
2692 enum key_extra button_char;
2693 int row, col;
2694 #ifdef FEAT_CLIPBOARD
2695 int checkfor;
2696 int did_clip = FALSE;
2697 #endif
2698
2699 /*
2700 * Scrolling may happen at any time, also while a selection is present.
2701 */
2702 switch (button)
2703 {
2704 case MOUSE_X1:
2705 button_char = KE_X1MOUSE;
2706 goto button_set;
2707 case MOUSE_X2:
2708 button_char = KE_X2MOUSE;
2709 goto button_set;
2710 case MOUSE_4:
2711 button_char = KE_MOUSEDOWN;
2712 goto button_set;
2713 case MOUSE_5:
2714 button_char = KE_MOUSEUP;
2715 button_set:
2716 {
2717 /* Don't put events in the input queue now. */
2718 if (hold_gui_events)
2719 return;
2720
2721 string[3] = CSI;
2722 string[4] = KS_EXTRA;
2723 string[5] = (int)button_char;
2724
2725 /* Pass the pointer coordinates of the scroll event so that we
2726 * know which window to scroll. */
2727 row = gui_xy2colrow(x, y, &col);
2728 string[6] = (char_u)(col / 128 + ' ' + 1);
2729 string[7] = (char_u)(col % 128 + ' ' + 1);
2730 string[8] = (char_u)(row / 128 + ' ' + 1);
2731 string[9] = (char_u)(row % 128 + ' ' + 1);
2732
2733 if (modifiers == 0)
2734 add_to_input_buf(string + 3, 7);
2735 else
2736 {
2737 string[0] = CSI;
2738 string[1] = KS_MODIFIER;
2739 string[2] = 0;
2740 if (modifiers & MOUSE_SHIFT)
2741 string[2] |= MOD_MASK_SHIFT;
2742 if (modifiers & MOUSE_CTRL)
2743 string[2] |= MOD_MASK_CTRL;
2744 if (modifiers & MOUSE_ALT)
2745 string[2] |= MOD_MASK_ALT;
2746 add_to_input_buf(string, 10);
2747 }
2748 return;
2749 }
2750 }
2751
2752 #ifdef FEAT_CLIPBOARD
2753 /* If a clipboard selection is in progress, handle it */
2754 if (clip_star.state == SELECT_IN_PROGRESS)
2755 {
2756 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2757 return;
2758 }
2759
2760 /* Determine which mouse settings to look for based on the current mode */
2761 switch (get_real_state())
2762 {
2763 case NORMAL_BUSY:
2764 case OP_PENDING:
2765 case NORMAL: checkfor = MOUSE_NORMAL; break;
2766 case VISUAL: checkfor = MOUSE_VISUAL; break;
2767 case REPLACE:
2768 case REPLACE+LANGMAP:
2769 #ifdef FEAT_VREPLACE
2770 case VREPLACE:
2771 case VREPLACE+LANGMAP:
2772 #endif
2773 case INSERT:
2774 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2775 case ASKMORE:
2776 case HITRETURN: /* At the more- and hit-enter prompt pass the
2777 mouse event for a click on or below the
2778 message line. */
2779 if (Y_2_ROW(y) >= msg_row)
2780 checkfor = MOUSE_NORMAL;
2781 else
2782 checkfor = MOUSE_RETURN;
2783 break;
2784
2785 /*
2786 * On the command line, use the clipboard selection on all lines
2787 * but the command line. But not when pasting.
2788 */
2789 case CMDLINE:
2790 case CMDLINE+LANGMAP:
2791 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2792 checkfor = MOUSE_NONE;
2793 else
2794 checkfor = MOUSE_COMMAND;
2795 break;
2796
2797 default:
2798 checkfor = MOUSE_NONE;
2799 break;
2800 };
2801
2802 /*
2803 * Allow clipboard selection of text on the command line in "normal"
2804 * modes. Don't do this when dragging the status line, or extending a
2805 * Visual selection.
2806 */
2807 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2808 && Y_2_ROW(y) >= topframe->fr_height
2809 && button != MOUSE_DRAG
2810 # ifdef FEAT_MOUSESHAPE
2811 && !drag_status_line
2812 # ifdef FEAT_VERTSPLIT
2813 && !drag_sep_line
2814 # endif
2815 # endif
2816 )
2817 checkfor = MOUSE_NONE;
2818
2819 /*
2820 * Use modeless selection when holding CTRL and SHIFT pressed.
2821 */
2822 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2823 checkfor = MOUSE_NONEF;
2824
2825 /*
2826 * In Ex mode, always use modeless selection.
2827 */
2828 if (exmode_active)
2829 checkfor = MOUSE_NONE;
2830
2831 /*
2832 * If the mouse settings say to not use the mouse, use the modeless
2833 * selection. But if Visual is active, assume that only the Visual area
2834 * will be selected.
2835 * Exception: On the command line, both the selection is used and a mouse
2836 * key is send.
2837 */
2838 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2839 {
2840 #ifdef FEAT_VISUAL
2841 /* Don't do modeless selection in Visual mode. */
2842 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2843 return;
2844 #endif
2845
2846 /*
2847 * When 'mousemodel' is "popup", shift-left is translated to right.
2848 * But not when also using Ctrl.
2849 */
2850 if (mouse_model_popup() && button == MOUSE_LEFT
2851 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2852 {
2853 button = MOUSE_RIGHT;
2854 modifiers &= ~ MOUSE_SHIFT;
2855 }
2856
2857 /* If the selection is done, allow the right button to extend it.
2858 * If the selection is cleared, allow the right button to start it
2859 * from the cursor position. */
2860 if (button == MOUSE_RIGHT)
2861 {
2862 if (clip_star.state == SELECT_CLEARED)
2863 {
2864 if (State & CMDLINE)
2865 {
2866 col = msg_col;
2867 row = msg_row;
2868 }
2869 else
2870 {
2871 col = curwin->w_wcol;
2872 row = curwin->w_wrow + W_WINROW(curwin);
2873 }
2874 clip_start_selection(col, row, FALSE);
2875 }
2876 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2877 repeated_click);
2878 did_clip = TRUE;
2879 }
2880 /* Allow the left button to start the selection */
2881 else if (button ==
2882 # ifdef RISCOS
2883 /* Only start a drag on a drag event. Otherwise
2884 * we don't get a release event. */
2885 MOUSE_DRAG
2886 # else
2887 MOUSE_LEFT
2888 # endif
2889 )
2890 {
2891 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2892 did_clip = TRUE;
2893 }
2894 # ifdef RISCOS
2895 else if (button == MOUSE_LEFT)
2896 {
2897 clip_clear_selection();
2898 did_clip = TRUE;
2899 }
2900 # endif
2901
2902 /* Always allow pasting */
2903 if (button != MOUSE_MIDDLE)
2904 {
2905 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2906 return;
2907 if (checkfor != MOUSE_COMMAND)
2908 button = MOUSE_LEFT;
2909 }
2910 repeated_click = FALSE;
2911 }
2912
2913 if (clip_star.state != SELECT_CLEARED && !did_clip)
2914 clip_clear_selection();
2915 #endif
2916
2917 /* Don't put events in the input queue now. */
2918 if (hold_gui_events)
2919 return;
2920
2921 row = gui_xy2colrow(x, y, &col);
2922
2923 /*
2924 * If we are dragging and the mouse hasn't moved far enough to be on a
2925 * different character, then don't send an event to vim.
2926 */
2927 if (button == MOUSE_DRAG)
2928 {
2929 if (row == prev_row && col == prev_col)
2930 return;
2931 /* Dragging above the window, set "row" to -1 to cause a scroll. */
2932 if (y < 0)
2933 row = -1;
2934 }
2935
2936 /*
2937 * If topline has changed (window scrolled) since the last click, reset
2938 * repeated_click, because we don't want starting Visual mode when
2939 * clicking on a different character in the text.
2940 */
2941 if (curwin->w_topline != gui_prev_topline
2942 #ifdef FEAT_DIFF
2943 || curwin->w_topfill != gui_prev_topfill
2944 #endif
2945 )
2946 repeated_click = FALSE;
2947
2948 string[0] = CSI; /* this sequence is recognized by check_termcode() */
2949 string[1] = KS_MOUSE;
2950 string[2] = KE_FILLER;
2951 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
2952 {
2953 if (repeated_click)
2954 {
2955 /*
2956 * Handle multiple clicks. They only count if the mouse is still
2957 * pointing at the same character.
2958 */
2959 if (button != prev_button || row != prev_row || col != prev_col)
2960 num_clicks = 1;
2961 else if (++num_clicks > 4)
2962 num_clicks = 1;
2963 }
2964 else
2965 num_clicks = 1;
2966 prev_button = button;
2967 gui_prev_topline = curwin->w_topline;
2968 #ifdef FEAT_DIFF
2969 gui_prev_topfill = curwin->w_topfill;
2970 #endif
2971
2972 string[3] = (char_u)(button | 0x20);
2973 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
2974 }
2975 else
2976 string[3] = (char_u)button;
2977
2978 string[3] |= modifiers;
2979 fill_mouse_coord(string + 4, col, row);
2980 add_to_input_buf(string, 8);
2981
2982 if (row < 0)
2983 prev_row = 0;
2984 else
2985 prev_row = row;
2986 prev_col = col;
2987
2988 /*
2989 * We need to make sure this is cleared since Athena doesn't tell us when
2990 * he is done dragging. Neither does GTK+ 2 -- at least for now.
2991 */
2992 #if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
2993 gui.dragged_sb = SBAR_NONE;
2994 #endif
2995 }
2996
2997 /*
2998 * Convert x and y coordinate to column and row in text window.
2999 * Corrects for multi-byte character.
3000 * returns column in "*colp" and row as return value;
3001 */
3002 int
3003 gui_xy2colrow(x, y, colp)
3004 int x;
3005 int y;
3006 int *colp;
3007 {
3008 int col = check_col(X_2_COL(x));
3009 int row = check_row(Y_2_ROW(y));
3010
3011 #ifdef FEAT_MBYTE
3012 *colp = mb_fix_col(col, row);
3013 #else
3014 *colp = col;
3015 #endif
3016 return row;
3017 }
3018
3019 #if defined(FEAT_MENU) || defined(PROTO)
3020 /*
3021 * Callback function for when a menu entry has been selected.
3022 */
3023 void
3024 gui_menu_cb(menu)
3025 vimmenu_T *menu;
3026 {
3027 char_u bytes[3 + sizeof(long_u)];
3028
3029 /* Don't put events in the input queue now. */
3030 if (hold_gui_events)
3031 return;
3032
3033 bytes[0] = CSI;
3034 bytes[1] = KS_MENU;
3035 bytes[2] = KE_FILLER;
3036 add_long_to_buf((long_u)menu, bytes + 3);
3037 add_to_input_buf(bytes, 3 + sizeof(long_u));
3038 }
3039 #endif
3040
3041 /*
3042 * Set which components are present.
3043 * If "oldval" is not NULL, "oldval" is the previous value, the new * value is
3044 * in p_go.
3045 */
3046 /*ARGSUSED*/
3047 void
3048 gui_init_which_components(oldval)
3049 char_u *oldval;
3050 {
3051 static int prev_which_scrollbars[3] = {-1, -1, -1};
3052 #ifdef FEAT_MENU
3053 static int prev_menu_is_active = -1;
3054 #endif
3055 #ifdef FEAT_TOOLBAR
3056 static int prev_toolbar = -1;
3057 int using_toolbar = FALSE;
3058 #endif
3059 #ifdef FEAT_FOOTER
3060 static int prev_footer = -1;
3061 int using_footer = FALSE;
3062 #endif
3063 #if defined(FEAT_MENU) && !defined(WIN16)
3064 static int prev_tearoff = -1;
3065 int using_tearoff = FALSE;
3066 #endif
3067
3068 char_u *p;
3069 int i;
3070 #ifdef FEAT_MENU
3071 int grey_old, grey_new;
3072 char_u *temp;
3073 #endif
3074 win_T *wp;
3075 int need_set_size;
3076 int fix_size;
3077
3078 #ifdef FEAT_MENU
3079 if (oldval != NULL && gui.in_use)
3080 {
3081 /*
3082 * Check if the menu's go from grey to non-grey or vise versa.
3083 */
3084 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3085 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3086 if (grey_old != grey_new)
3087 {
3088 temp = p_go;
3089 p_go = oldval;
3090 gui_update_menus(MENU_ALL_MODES);
3091 p_go = temp;
3092 }
3093 }
3094 gui.menu_is_active = FALSE;
3095 #endif
3096
3097 for (i = 0; i < 3; i++)
3098 gui.which_scrollbars[i] = FALSE;
3099 for (p = p_go; *p; p++)
3100 switch (*p)
3101 {
3102 case GO_LEFT:
3103 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3104 break;
3105 case GO_RIGHT:
3106 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3107 break;
3108 #ifdef FEAT_VERTSPLIT
3109 case GO_VLEFT:
3110 if (win_hasvertsplit())
3111 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3112 break;
3113 case GO_VRIGHT:
3114 if (win_hasvertsplit())
3115 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3116 break;
3117 #endif
3118 case GO_BOT:
3119 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3120 break;
3121 #ifdef FEAT_MENU
3122 case GO_MENUS:
3123 gui.menu_is_active = TRUE;
3124 break;
3125 #endif
3126 case GO_GREY:
3127 /* make menu's have grey items, ignored here */
3128 break;
3129 #ifdef FEAT_TOOLBAR
3130 case GO_TOOLBAR:
3131 using_toolbar = TRUE;
3132 break;
3133 #endif
3134 #ifdef FEAT_FOOTER
3135 case GO_FOOTER:
3136 using_footer = TRUE;
3137 break;
3138 #endif
3139 case GO_TEAROFF:
3140 #if defined(FEAT_MENU) && !defined(WIN16)
3141 using_tearoff = TRUE;
3142 #endif
3143 break;
3144 default:
3145 /* Ignore options that are not supported */
3146 break;
3147 }
3148 if (gui.in_use)
3149 {
3150 need_set_size = FALSE;
3151 fix_size = FALSE;
3152 for (i = 0; i < 3; i++)
3153 {
3154 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3155 {
3156 if (i == SBAR_BOTTOM)
3157 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3158 gui.which_scrollbars[i]);
3159 else
3160 {
3161 FOR_ALL_WINDOWS(wp)
3162 {
3163 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3164 }
3165 }
3166 need_set_size = TRUE;
3167 if (gui.which_scrollbars[i])
3168 fix_size = TRUE;
3169 }
3170 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3171 }
3172
3173 #ifdef FEAT_MENU
3174 if (gui.menu_is_active != prev_menu_is_active)
3175 {
3176 /* We don't want a resize event change "Rows" here, save and
3177 * restore it. Resizing is handled below. */
3178 i = Rows;
3179 gui_mch_enable_menu(gui.menu_is_active);
3180 Rows = i;
3181 prev_menu_is_active = gui.menu_is_active;
3182 need_set_size = TRUE;
3183 if (gui.menu_is_active)
3184 fix_size = TRUE;
3185 }
3186 #endif
3187
3188 #ifdef FEAT_TOOLBAR
3189 if (using_toolbar != prev_toolbar)
3190 {
3191 gui_mch_show_toolbar(using_toolbar);
3192 prev_toolbar = using_toolbar;
3193 need_set_size = TRUE;
3194 if (using_toolbar)
3195 fix_size = TRUE;
3196 }
3197 #endif
3198 #ifdef FEAT_FOOTER
3199 if (using_footer != prev_footer)
3200 {
3201 gui_mch_enable_footer(using_footer);
3202 prev_footer = using_footer;
3203 need_set_size = TRUE;
3204 if (using_footer)
3205 fix_size = TRUE;
3206 }
3207 #endif
3208 #if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3209 if (using_tearoff != prev_tearoff)
3210 {
3211 gui_mch_toggle_tearoffs(using_tearoff);
3212 prev_tearoff = using_tearoff;
3213 }
3214 #endif
3215 if (need_set_size)
3216 /* Adjust the size of the window to make the text area keep the
3217 * same size and to avoid that part of our window is off-screen
3218 * and a scrollbar can't be used, for example. */
3219 gui_set_shellsize(FALSE, fix_size);
3220 }
3221 }
3222
3223
3224 /*
3225 * Scrollbar stuff:
3226 */
3227
3228 void
3229 gui_create_scrollbar(sb, type, wp)
3230 scrollbar_T *sb;
3231 int type;
3232 win_T *wp;
3233 {
3234 static int sbar_ident = 0;
3235
3236 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3237 sb->wp = wp;
3238 sb->type = type;
3239 sb->value = 0;
3240 #ifdef FEAT_GUI_ATHENA
3241 sb->pixval = 0;
3242 #endif
3243 sb->size = 1;
3244 sb->max = 1;
3245 sb->top = 0;
3246 sb->height = 0;
3247 #ifdef FEAT_VERTSPLIT
3248 sb->width = 0;
3249 #endif
3250 sb->status_height = 0;
3251 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3252 }
3253
3254 /*
3255 * Find the scrollbar with the given index.
3256 */
3257 scrollbar_T *
3258 gui_find_scrollbar(ident)
3259 long ident;
3260 {
3261 win_T *wp;
3262
3263 if (gui.bottom_sbar.ident == ident)
3264 return &gui.bottom_sbar;
3265 FOR_ALL_WINDOWS(wp)
3266 {
3267 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3268 return &wp->w_scrollbars[SBAR_LEFT];
3269 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3270 return &wp->w_scrollbars[SBAR_RIGHT];
3271 }
3272 return NULL;
3273 }
3274
3275 /*
3276 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3277 *
3278 * For Win32, Macintosh and GTK+ 2:
3279 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3280 * you stop dragging the scrollbar. We get here each time the scrollbar is
3281 * dragged another pixel, but as far as the rest of vim goes, it thinks
3282 * we're just hanging in the call to DispatchMessage() in
3283 * process_message(). The DispatchMessage() call that hangs was passed a
3284 * mouse button click event in the scrollbar window. -- webb.
3285 *
3286 * Solution: Do the scrolling right here. But only when allowed.
3287 * Ignore the scrollbars while executing an external command or when there
3288 * are still characters to be processed.
3289 */
3290 void
3291 gui_drag_scrollbar(sb, value, still_dragging)
3292 scrollbar_T *sb;
3293 long value;
3294 int still_dragging;
3295 {
3296 #ifdef FEAT_WINDOWS
3297 win_T *wp;
3298 #endif
3299 int sb_num;
3300 #ifdef USE_ON_FLY_SCROLL
3301 colnr_T old_leftcol = curwin->w_leftcol;
3302 # ifdef FEAT_SCROLLBIND
3303 linenr_T old_topline = curwin->w_topline;
3304 # endif
3305 # ifdef FEAT_DIFF
3306 int old_topfill = curwin->w_topfill;
3307 # endif
3308 #else
3309 char_u bytes[4 + sizeof(long_u)];
3310 int byte_count;
3311 #endif
3312
3313 if (sb == NULL)
3314 return;
3315
3316 /* Don't put events in the input queue now. */
3317 if (hold_gui_events)
3318 return;
3319
3320 #ifdef FEAT_CMDWIN
3321 if (cmdwin_type != 0 && sb->wp != curwin)
3322 return;
3323 #endif
3324
3325 if (still_dragging)
3326 {
3327 if (sb->wp == NULL)
3328 gui.dragged_sb = SBAR_BOTTOM;
3329 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3330 gui.dragged_sb = SBAR_LEFT;
3331 else
3332 gui.dragged_sb = SBAR_RIGHT;
3333 gui.dragged_wp = sb->wp;
3334 }
3335 else
3336 {
3337 gui.dragged_sb = SBAR_NONE;
3338 #ifdef HAVE_GTK2
3339 /* Keep the "dragged_wp" value until after the scrolling, for when the
3340 * moust button is released. GTK2 doesn't send the button-up event. */
3341 gui.dragged_wp = NULL;
3342 #endif
3343 }
3344
3345 /* Vertical sbar info is kept in the first sbar (the left one) */
3346 if (sb->wp != NULL)
3347 sb = &sb->wp->w_scrollbars[0];
3348
3349 /*
3350 * Check validity of value
3351 */
3352 if (value < 0)
3353 value = 0;
3354 #ifdef SCROLL_PAST_END
3355 else if (value > sb->max)
3356 value = sb->max;
3357 #else
3358 if (value > sb->max - sb->size + 1)
3359 value = sb->max - sb->size + 1;
3360 #endif
3361
3362 sb->value = value;
3363
3364 #ifdef USE_ON_FLY_SCROLL
3365 /* When not allowed to do the scrolling right now, return. */
3366 if (dont_scroll || input_available())
3367 return;
3368 #endif
3369
3370 #ifdef FEAT_RIGHTLEFT
3371 if (sb->wp == NULL && curwin->w_p_rl)
3372 {
3373 value = sb->max + 1 - sb->size - value;
3374 if (value < 0)
3375 value = 0;
3376 }
3377 #endif
3378
3379 if (sb->wp != NULL) /* vertical scrollbar */
3380 {
3381 sb_num = 0;
3382 #ifdef FEAT_WINDOWS
3383 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3384 sb_num++;
3385 if (wp == NULL)
3386 return;
3387 #else
3388 if (sb->wp != curwin)
3389 return;
3390 #endif
3391
3392 #ifdef USE_ON_FLY_SCROLL
3393 current_scrollbar = sb_num;
3394 scrollbar_value = value;
3395 if (State & NORMAL)
3396 {
3397 gui_do_scroll();
3398 setcursor();
3399 }
3400 else if (State & INSERT)
3401 {
3402 ins_scroll();
3403 setcursor();
3404 }
3405 else if (State & CMDLINE)
3406 {
3407 if (msg_scrolled == 0)
3408 {
3409 gui_do_scroll();
3410 redrawcmdline();
3411 }
3412 }
3413 # ifdef FEAT_FOLDING
3414 /* Value may have been changed for closed fold. */
3415 sb->value = sb->wp->w_topline - 1;
3416 # endif
3417 #else
3418 bytes[0] = CSI;
3419 bytes[1] = KS_VER_SCROLLBAR;
3420 bytes[2] = KE_FILLER;
3421 bytes[3] = (char_u)sb_num;
3422 byte_count = 4;
3423 #endif
3424 }
3425 else
3426 {
3427 #ifdef USE_ON_FLY_SCROLL
3428 scrollbar_value = value;
3429
3430 if (State & NORMAL)
3431 gui_do_horiz_scroll();
3432 else if (State & INSERT)
3433 ins_horscroll();
3434 else if (State & CMDLINE)
3435 {
3436 if (!msg_scrolled)
3437 {
3438 gui_do_horiz_scroll();
3439 redrawcmdline();
3440 }
3441 }
3442 if (old_leftcol != curwin->w_leftcol)
3443 {
3444 updateWindow(curwin); /* update window, status and cmdline */
3445 setcursor();
3446 }
3447 #else
3448 bytes[0] = CSI;
3449 bytes[1] = KS_HOR_SCROLLBAR;
3450 bytes[2] = KE_FILLER;
3451 byte_count = 3;
3452 #endif
3453 }
3454
3455 #ifdef USE_ON_FLY_SCROLL
3456 # ifdef FEAT_SCROLLBIND
3457 /*
3458 * synchronize other windows, as necessary according to 'scrollbind'
3459 */
3460 if (curwin->w_p_scb
3461 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3462 || (sb->wp == curwin && (curwin->w_topline != old_topline
3463 # ifdef FEAT_DIFF
3464 || curwin->w_topfill != old_topfill
3465 # endif
3466 ))))
3467 {
3468 do_check_scrollbind(TRUE);
3469 /* need to update the window right here */
3470 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3471 if (wp->w_redr_type > 0)
3472 updateWindow(wp);
3473 setcursor();
3474 }
3475 # endif
3476 out_flush();
3477 gui_update_cursor(FALSE, TRUE);
3478 #else
3479 add_long_to_buf((long)value, bytes + byte_count);
3480 add_to_input_buf(bytes, byte_count + sizeof(long_u));
3481 #endif
3482 }
3483
3484 /*
3485 * Scrollbar stuff:
3486 */
3487
3488 void
3489 gui_update_scrollbars(force)
3490 int force; /* Force all scrollbars to get updated */
3491 {
3492 win_T *wp;
3493 scrollbar_T *sb;
3494 long val, size, max; /* need 32 bits here */
3495 int which_sb;
3496 int h, y;
3497 #ifdef FEAT_VERTSPLIT
3498 static win_T *prev_curwin = NULL;
3499 #endif
3500
3501 /* Update the horizontal scrollbar */
3502 gui_update_horiz_scrollbar(force);
3503
3504 #ifndef WIN3264
3505 /* Return straight away if there is neither a left nor right scrollbar.
3506 * On MS-Windows this is required anyway for scrollwheel messages. */
3507 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3508 return;
3509 #endif
3510
3511 /*
3512 * Don't want to update a scrollbar while we're dragging it. But if we
3513 * have both a left and right scrollbar, and we drag one of them, we still
3514 * need to update the other one.
3515 */
3516 if ( (gui.dragged_sb == SBAR_LEFT
3517 || gui.dragged_sb == SBAR_RIGHT)
3518 && (!gui.which_scrollbars[SBAR_LEFT]
3519 || !gui.which_scrollbars[SBAR_RIGHT])
3520 && !force)
3521 return;
3522
3523 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT))
3524 {
3525 /*
3526 * If we have two scrollbars and one of them is being dragged, just
3527 * copy the scrollbar position from the dragged one to the other one.
3528 */
3529 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3530 if (gui.dragged_wp != NULL)
3531 gui_mch_set_scrollbar_thumb(
3532 &gui.dragged_wp->w_scrollbars[which_sb],
3533 gui.dragged_wp->w_scrollbars[0].value,
3534 gui.dragged_wp->w_scrollbars[0].size,
3535 gui.dragged_wp->w_scrollbars[0].max);
3536 return;
3537 }
3538
3539 /* avoid that moving components around generates events */
3540 ++hold_gui_events;
3541
3542 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3543 {
3544 if (wp->w_buffer == NULL) /* just in case */
3545 continue;
3546 #ifdef SCROLL_PAST_END
3547 max = wp->w_buffer->b_ml.ml_line_count - 1;
3548 #else
3549 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3550 #endif
3551 if (max < 0) /* empty buffer */
3552 max = 0;
3553 val = wp->w_topline - 1;
3554 size = wp->w_height;
3555 #ifdef SCROLL_PAST_END
3556 if (val > max) /* just in case */
3557 val = max;
3558 #else
3559 if (size > max + 1) /* just in case */
3560 size = max + 1;
3561 if (val > max - size + 1)
3562 val = max - size + 1;
3563 #endif
3564 if (val < 0) /* minimal value is 0 */
3565 val = 0;
3566
3567 /*
3568 * Scrollbar at index 0 (the left one) contains all the information.
3569 * It would be the same info for left and right so we just store it for
3570 * one of them.
3571 */
3572 sb = &wp->w_scrollbars[0];
3573
3574 /*
3575 * Note: no check for valid w_botline. If it's not valid the
3576 * scrollbars will be updated later anyway.
3577 */
3578 if (size < 1 || wp->w_botline - 2 > max)
3579 {
3580 /*
3581 * This can happen during changing files. Just don't update the
3582 * scrollbar for now.
3583 */
3584 sb->height = 0; /* Force update next time */
3585 if (gui.which_scrollbars[SBAR_LEFT])
3586 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3587 if (gui.which_scrollbars[SBAR_RIGHT])
3588 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3589 continue;
3590 }
3591 if (force || sb->height != wp->w_height
3592 #ifdef FEAT_WINDOWS
3593 || sb->top != wp->w_winrow
3594 || sb->status_height != wp->w_status_height
3595 # ifdef FEAT_VERTSPLIT
3596 || sb->width != wp->w_width
3597 || prev_curwin != curwin
3598 # endif
3599 #endif
3600 )
3601 {
3602 /* Height, width or position of scrollbar has changed. For
3603 * vertical split: curwin changed. */
3604 sb->height = wp->w_height;
3605 #ifdef FEAT_WINDOWS
3606 sb->top = wp->w_winrow;
3607 sb->status_height = wp->w_status_height;
3608 # ifdef FEAT_VERTSPLIT
3609 sb->width = wp->w_width;
3610 # endif
3611 #endif
3612
3613 /* Calculate height and position in pixels */
3614 h = (sb->height + sb->status_height) * gui.char_height;
3615 y = sb->top * gui.char_height + gui.border_offset;
3616 #if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
3617 if (gui.menu_is_active)
3618 y += gui.menu_height;
3619 #endif
3620
3621 #if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
3622 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
3623 # ifdef FEAT_GUI_ATHENA
3624 y += gui.toolbar_height;
3625 # else
3626 # ifdef FEAT_GUI_MSWIN
3627 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
3628 # endif
3629 # endif
3630 #endif
3631
3632 #ifdef FEAT_WINDOWS
3633 if (wp->w_winrow == 0)
3634 #endif
3635 {
3636 /* Height of top scrollbar includes width of top border */
3637 h += gui.border_offset;
3638 y -= gui.border_offset;
3639 }
3640 if (gui.which_scrollbars[SBAR_LEFT])
3641 {
3642 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
3643 gui.left_sbar_x, y,
3644 gui.scrollbar_width, h);
3645 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
3646 }
3647 if (gui.which_scrollbars[SBAR_RIGHT])
3648 {
3649 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
3650 gui.right_sbar_x, y,
3651 gui.scrollbar_width, h);
3652 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
3653 }
3654 }
3655
3656 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
3657 * checking if the thumb moved at least a pixel. Only do this for
3658 * Athena, most other GUIs require the update anyway to make the
3659 * arrows work. */
3660 #ifdef FEAT_GUI_ATHENA
3661 if (max == 0)
3662 y = 0;
3663 else
3664 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
3665 if (force || sb->pixval != y || sb->size != size || sb->max != max)
3666 #else
3667 if (force || sb->value != val || sb->size != size || sb->max != max)
3668 #endif
3669 {
3670 /* Thumb of scrollbar has moved */
3671 sb->value = val;
3672 #ifdef FEAT_GUI_ATHENA
3673 sb->pixval = y;
3674 #endif
3675 sb->size = size;
3676 sb->max = max;
3677 if (gui.which_scrollbars[SBAR_LEFT] && gui.dragged_sb != SBAR_LEFT)
3678 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
3679 val, size, max);
3680 if (gui.which_scrollbars[SBAR_RIGHT]
3681 && gui.dragged_sb != SBAR_RIGHT)
3682 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
3683 val, size, max);
3684 }
3685 }
3686 #ifdef FEAT_VERTSPLIT
3687 prev_curwin = curwin;
3688 #endif
3689 --hold_gui_events;
3690 }
3691
3692 /*
3693 * Enable or disable a scrollbar.
3694 * Check for scrollbars for vertically split windows which are not enabled
3695 * sometimes.
3696 */
3697 static void
3698 gui_do_scrollbar(wp, which, enable)
3699 win_T *wp;
3700 int which; /* SBAR_LEFT or SBAR_RIGHT */
3701 int enable; /* TRUE to enable scrollbar */
3702 {
3703 #ifdef FEAT_VERTSPLIT
3704 int midcol = curwin->w_wincol + curwin->w_width / 2;
3705 int has_midcol = (wp->w_wincol <= midcol
3706 && wp->w_wincol + wp->w_width >= midcol);
3707
3708 /* Only enable scrollbars that contain the middle column of the current
3709 * window. */
3710 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
3711 {
3712 /* Scrollbars only on one side. Don't enable scrollbars that don't
3713 * contain the middle column of the current window. */
3714 if (!has_midcol)
3715 enable = FALSE;
3716 }
3717 else
3718 {
3719 /* Scrollbars on both sides. Don't enable scrollbars that neither
3720 * contain the middle column of the current window nor are on the far
3721 * side. */
3722 if (midcol > Columns / 2)
3723 {
3724 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
3725 enable = FALSE;
3726 }
3727 else
3728 {
3729 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
3730 : !has_midcol)
3731 enable = FALSE;
3732 }
3733 }
3734 #endif
3735 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
3736 }
3737
3738 /*
3739 * Scroll a window according to the values set in the globals current_scrollbar
3740 * and scrollbar_value. Return TRUE if the cursor in the current window moved
3741 * or FALSE otherwise.
3742 */
3743 int
3744 gui_do_scroll()
3745 {
3746 win_T *wp, *save_wp;
3747 int i;
3748 long nlines;
3749 pos_T old_cursor;
3750 linenr_T old_topline;
3751 #ifdef FEAT_DIFF
3752 int old_topfill;
3753 #endif
3754
3755 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
3756 if (wp == NULL)
3757 break;
3758 if (wp == NULL)
3759 /* Couldn't find window */
3760 return FALSE;
3761
3762 /*
3763 * Compute number of lines to scroll. If zero, nothing to do.
3764 */
3765 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
3766 if (nlines == 0)
3767 return FALSE;
3768
3769 save_wp = curwin;
3770 old_topline = wp->w_topline;
3771 #ifdef FEAT_DIFF
3772 old_topfill = wp->w_topfill;
3773 #endif
3774 old_cursor = wp->w_cursor;
3775 curwin = wp;
3776 curbuf = wp->w_buffer;
3777 if (nlines < 0)
3778 scrolldown(-nlines, gui.dragged_wp == NULL);
3779 else
3780 scrollup(nlines, gui.dragged_wp == NULL);
3781 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
3782 * the mouse-up event already, but we still want it to behave like when
3783 * dragging. But not the next click in an arrow. */
3784 if (gui.dragged_sb == SBAR_NONE)
3785 gui.dragged_wp = NULL;
3786
3787 if (old_topline != wp->w_topline
3788 #ifdef FEAT_DIFF
3789 || old_topfill != wp->w_topfill
3790 #endif
3791 )
3792 {
3793 if (p_so != 0)
3794 {
3795 cursor_correct(); /* fix window for 'so' */
3796 update_topline(); /* avoid up/down jump */
3797 }
3798 if (old_cursor.lnum != wp->w_cursor.lnum)
3799 coladvance(wp->w_curswant);
3800 #ifdef FEAT_SCROLLBIND
3801 wp->w_scbind_pos = wp->w_topline;
3802 #endif
3803 }
3804
3805 curwin = save_wp;
3806 curbuf = save_wp->w_buffer;
3807
3808 /*
3809 * Don't call updateWindow() when nothing has changed (it will overwrite
3810 * the status line!).
3811 */
3812 if (old_topline != wp->w_topline
3813 #ifdef FEAT_DIFF
3814 || old_topfill != wp->w_topfill
3815 #endif
3816 )
3817 {
3818 redraw_win_later(wp, VALID);
3819 updateWindow(wp); /* update window, status line, and cmdline */
3820 }
3821
3822 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
3823 }
3824
3825
3826 /*
3827 * Horizontal scrollbar stuff:
3828 */
3829
3830 /*
3831 * Return length of line "lnum" for horizontal scrolling.
3832 */
3833 static colnr_T
3834 scroll_line_len(lnum)
3835 linenr_T lnum;
3836 {
3837 char_u *p;
3838 colnr_T col;
3839 int w;
3840
3841 p = ml_get(lnum);
3842 col = 0;
3843 if (*p != NUL)
3844 for (;;)
3845 {
3846 w = chartabsize(p, col);
3847 #ifdef FEAT_MBYTE
3848 if (has_mbyte)
3849 p += (*mb_ptr2len_check)(p);
3850 else
3851 #endif
3852 ++p;
3853 if (*p == NUL) /* don't count the last character */
3854 break;
3855 col += w;
3856 }
3857 return col;
3858 }
3859
3860 /* Remember which line is currently the longest, so that we don't have to
3861 * search for it when scrolling horizontally. */
3862 static linenr_T longest_lnum = 0;
3863
3864 static void
3865 gui_update_horiz_scrollbar(force)
3866 int force;
3867 {
3868 long value, size, max; /* need 32 bit ints here */
3869
3870 if (!gui.which_scrollbars[SBAR_BOTTOM])
3871 return;
3872
3873 if (!force && gui.dragged_sb == SBAR_BOTTOM)
3874 return;
3875
3876 if (!force && curwin->w_p_wrap && gui.prev_wrap)
3877 return;
3878
3879 /*
3880 * It is possible for the cursor to be invalid if we're in the middle of
3881 * something (like changing files). If so, don't do anything for now.
3882 */
3883 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3884 {
3885 gui.bottom_sbar.value = -1;
3886 return;
3887 }
3888
3889 size = W_WIDTH(curwin);
3890 if (curwin->w_p_wrap)
3891 {
3892 value = 0;
3893 #ifdef SCROLL_PAST_END
3894 max = 0;
3895 #else
3896 max = W_WIDTH(curwin) - 1;
3897 #endif
3898 }
3899 else
3900 {
3901 value = curwin->w_leftcol;
3902
3903 /* Calculate maximum for horizontal scrollbar. Check for reasonable
3904 * line numbers, topline and botline can be invalid when displaying is
3905 * postponed. */
3906 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
3907 && curwin->w_topline <= curwin->w_cursor.lnum
3908 && curwin->w_botline > curwin->w_cursor.lnum
3909 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
3910 {
3911 linenr_T lnum;
3912 colnr_T n;
3913
3914 /* Use maximum of all visible lines. Remember the lnum of the
3915 * longest line, clostest to the cursor line. Used when scrolling
3916 * below. */
3917 max = 0;
3918 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
3919 {
3920 n = scroll_line_len(lnum);
3921 if (n > (colnr_T)max)
3922 {
3923 max = n;
3924 longest_lnum = lnum;
3925 }
3926 else if (n == (colnr_T)max
3927 && abs((int)(lnum - curwin->w_cursor.lnum))
3928 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
3929 longest_lnum = lnum;
3930 }
3931 }
3932 else
3933 /* Use cursor line only. */
3934 max = scroll_line_len(curwin->w_cursor.lnum);
3935 #ifdef FEAT_VIRTUALEDIT
3936 if (virtual_active())
3937 {
3938 /* May move the cursor even further to the right. */
3939 if (curwin->w_virtcol >= (colnr_T)max)
3940 max = curwin->w_virtcol;
3941 }
3942 #endif
3943
3944 #ifndef SCROLL_PAST_END
3945 max += W_WIDTH(curwin) - 1;
3946 #endif
3947 /* The line number isn't scrolled, thus there is less space when
3948 * 'number' is set (also for 'foldcolumn'). */
3949 size -= curwin_col_off();
3950 #ifndef SCROLL_PAST_END
3951 max -= curwin_col_off();
3952 #endif
3953 }
3954
3955 #ifndef SCROLL_PAST_END
3956 if (value > max - size + 1)
3957 value = max - size + 1; /* limit the value to allowable range */
3958 #endif
3959
3960 #ifdef FEAT_RIGHTLEFT
3961 if (curwin->w_p_rl)
3962 {
3963 value = max + 1 - size - value;
3964 if (value < 0)
3965 {
3966 size += value;
3967 value = 0;
3968 }
3969 }
3970 #endif
3971 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
3972 && max == gui.bottom_sbar.max)
3973 return;
3974
3975 gui.bottom_sbar.value = value;
3976 gui.bottom_sbar.size = size;
3977 gui.bottom_sbar.max = max;
3978 gui.prev_wrap = curwin->w_p_wrap;
3979
3980 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
3981 }
3982
3983 /*
3984 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
3985 */
3986 int
3987 gui_do_horiz_scroll()
3988 {
3989 /* no wrapping, no scrolling */
3990 if (curwin->w_p_wrap)
3991 return FALSE;
3992
3993 if (curwin->w_leftcol == scrollbar_value)
3994 return FALSE;
3995
3996 curwin->w_leftcol = scrollbar_value;
3997
3998 /* When the line of the cursor is too short, move the cursor to the
3999 * longest visible line. Do a sanity check on "longest_lnum", just in
4000 * case. */
4001 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4002 && longest_lnum >= curwin->w_topline
4003 && longest_lnum < curwin->w_botline
4004 && !virtual_active())
4005 {
4006 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4007 {
4008 curwin->w_cursor.lnum = longest_lnum;
4009 curwin->w_cursor.col = 0;
4010 }
4011 }
4012
4013 return leftcol_changed();
4014 }
4015
4016 /*
4017 * Check that none of the colors are the same as the background color
4018 */
4019 void
4020 gui_check_colors()
4021 {
4022 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4023 {
4024 gui_set_bg_color((char_u *)"White");
4025 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4026 gui_set_fg_color((char_u *)"Black");
4027 }
4028 }
4029
4030 void
4031 gui_set_fg_color(name)
4032 char_u *name;
4033 {
4034 gui.norm_pixel = gui_get_color(name);
4035 hl_set_fg_color_name(vim_strsave(name));
4036 }
4037
4038 void
4039 gui_set_bg_color(name)
4040 char_u *name;
4041 {
4042 gui.back_pixel = gui_get_color(name);
4043 hl_set_bg_color_name(vim_strsave(name));
4044 }
4045
4046 /*
4047 * Allocate a color by name.
4048 * Returns INVALCOLOR and gives an error message when failed.
4049 */
4050 guicolor_T
4051 gui_get_color(name)
4052 char_u *name;
4053 {
4054 guicolor_T t;
4055
4056 if (*name == NUL)
4057 return INVALCOLOR;
4058 t = gui_mch_get_color(name);
4059 if (t == INVALCOLOR
4060 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
4061 && gui.in_use
4062 #endif
4063 )
4064 EMSG2(_("E254: Cannot allocate color %s"), name);
4065 return t;
4066 }
4067
4068 /*
4069 * Return the grey value of a color (range 0-255).
4070 */
4071 int
4072 gui_get_lightness(pixel)
4073 guicolor_T pixel;
4074 {
4075 long_u rgb = gui_mch_get_rgb(pixel);
4076
4077 return ( (((rgb >> 16) & 0xff) * 299)
4078 + (((rgb >> 8) & 0xff) * 587)
4079 + ((rgb & 0xff) * 114)) / 1000;
4080 }
4081
4082 #if defined(FEAT_GUI_X11) || defined(PROTO)
4083 void
4084 gui_new_scrollbar_colors()
4085 {
4086 win_T *wp;
4087
4088 /* Nothing to do if GUI hasn't started yet. */
4089 if (!gui.in_use)
4090 return;
4091
4092 FOR_ALL_WINDOWS(wp)
4093 {
4094 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4095 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4096 }
4097 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4098 }
4099 #endif
4100
4101 /*
4102 * Call this when focus has changed.
4103 */
4104 void
4105 gui_focus_change(in_focus)
4106 int in_focus;
4107 {
4108 /*
4109 * Skip this code to avoid drawing the cursor when debugging and switching
4110 * between the debugger window and gvim.
4111 */
4112 #if 1
4113 gui.in_focus = in_focus;
4114 out_flush(); /* make sure output has been written */
4115 gui_update_cursor(TRUE, FALSE);
4116
4117 # ifdef FEAT_XIM
4118 xim_set_focus(in_focus);
4119 # endif
4120
4121 ui_focus_change(in_focus);
4122 #endif
4123 }
4124
4125 /*
4126 * Called when the mouse moved (but not when dragging).
4127 */
4128 void
4129 gui_mouse_moved(x, y)
4130 int x;
4131 int y;
4132 {
4133 win_T *wp;
4134 char_u st[6];
4135
4136 #ifdef FEAT_MOUSESHAPE
4137 /* Get window pointer, and update mouse shape as well. */
4138 wp = xy2win(x, y);
4139 #endif
4140
4141 /* Only handle this when 'mousefocus' set and ... */
4142 if (p_mousef
4143 && !hold_gui_events /* not holding events */
4144 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4145 && State != HITRETURN /* but not hit-return prompt */
4146 && msg_scrolled == 0 /* no scrolled message */
4147 && !need_mouse_correct /* not moving the pointer */
4148 && gui.in_focus) /* gvim in focus */
4149 {
4150 /* Don't move the mouse when it's left or right of the Vim window */
4151 if (x < 0 || x > Columns * gui.char_width)
4152 return;
4153 #ifndef FEAT_MOUSESHAPE
4154 wp = xy2win(x, y);
4155 #endif
4156 if (wp == curwin || wp == NULL)
4157 return; /* still in the same old window, or none at all */
4158
4159 /*
4160 * format a mouse click on status line input
4161 * ala gui_send_mouse_event(0, x, y, 0, 0);
4162 * Trick: Use a column of -1, so that check_termcode will generate a
4163 * K_LEFTMOUSE_NM key code.
4164 */
4165 if (finish_op)
4166 {
4167 /* abort the current operator first */
4168 st[0] = ESC;
4169 add_to_input_buf(st, 1);
4170 }
4171 st[0] = CSI;
4172 st[1] = KS_MOUSE;
4173 st[2] = KE_FILLER;
4174 st[3] = (char_u)MOUSE_LEFT;
4175 fill_mouse_coord(st + 4,
4176 #ifdef FEAT_VERTSPLIT
4177 W_WINCOL(wp),
4178 #else
4179 -1,
4180 #endif
4181 wp->w_height + W_WINROW(wp));
4182
4183 add_to_input_buf(st, 8);
4184 st[3] = (char_u)MOUSE_RELEASE;
4185 add_to_input_buf(st, 8);
4186
4187 #ifdef FEAT_GUI_GTK
4188 /* Need to wake up the main loop */
4189 if (gtk_main_level() > 0)
4190 gtk_main_quit();
4191 #endif
4192 }
4193 }
4194
4195 /*
4196 * Called when mouse should be moved to window with focus.
4197 */
4198 void
4199 gui_mouse_correct()
4200 {
4201 int x, y;
4202 win_T *wp = NULL;
4203
4204 need_mouse_correct = FALSE;
4205 if (gui.in_use && p_mousef)
4206 {
4207 x = gui_mch_get_mouse_x();
4208 /* Don't move the mouse when it's left or right of the Vim window */
4209 if (x < 0 || x > Columns * gui.char_width)
4210 return;
4211 y = gui_mch_get_mouse_y();
4212 if (y >= 0)
4213 wp = xy2win(x, y);
4214 if (wp != curwin && wp != NULL) /* If in other than current window */
4215 {
4216 validate_cline_row();
4217 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4218 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4219 + (gui.char_height) / 2);
4220 }
4221 }
4222 }
4223
4224 /*
4225 * Find window where the mouse pointer "y" coordinate is in.
4226 */
4227 /*ARGSUSED*/
4228 static win_T *
4229 xy2win(x, y)
4230 int x;
4231 int y;
4232 {
4233 #ifdef FEAT_WINDOWS
4234 int row;
4235 int col;
4236 win_T *wp;
4237
4238 row = Y_2_ROW(y);
4239 col = X_2_COL(x);
4240 if (row < 0 || col < 0) /* before first window */
4241 return NULL;
4242 wp = mouse_find_win(&row, &col);
4243 # ifdef FEAT_MOUSESHAPE
4244 if (State == HITRETURN || State == ASKMORE)
4245 {
4246 if (Y_2_ROW(y) >= msg_row)
4247 update_mouseshape(SHAPE_IDX_MOREL);
4248 else
4249 update_mouseshape(SHAPE_IDX_MORE);
4250 }
4251 else if (row > wp->w_height) /* below status line */
4252 update_mouseshape(SHAPE_IDX_CLINE);
4253 # ifdef FEAT_VERTSPLIT
4254 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4255 && (row != wp->w_height || !stl_connected(wp)))
4256 update_mouseshape(SHAPE_IDX_VSEP);
4257 # endif
4258 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4259 && row == wp->w_height)
4260 update_mouseshape(SHAPE_IDX_STATUS);
4261 else
4262 update_mouseshape(-2);
4263 # endif
4264 return wp;
4265 #else
4266 return firstwin;
4267 #endif
4268 }
4269
4270 /*
4271 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4272 * File names may be given to redefine the args list.
4273 */
4274 void
4275 ex_gui(eap)
4276 exarg_T *eap;
4277 {
4278 char_u *arg = eap->arg;
4279
4280 /*
4281 * Check for "-f" argument: foreground, don't fork.
4282 * Also don't fork when started with "gvim -f".
4283 * Do fork when using "gui -b".
4284 */
4285 if (arg[0] == '-'
4286 && (arg[1] == 'f' || arg[1] == 'b')
4287 && (arg[2] == NUL || vim_iswhite(arg[2])))
4288 {
4289 gui.dofork = (arg[1] == 'b');
4290 eap->arg = skipwhite(eap->arg + 2);
4291 }
4292 if (!gui.in_use)
4293 {
4294 /* Clear the command. Needed for when forking+exiting, to avoid part
4295 * of the argument ending up after the shell prompt. */
4296 msg_clr_eos_force();
4297 gui_start();
4298 }
4299 if (!ends_excmd(*eap->arg))
4300 ex_next(eap);
4301 }
4302
4303 #if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
4304 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
4305 /*
4306 * This is shared between Athena, Motif and GTK.
4307 */
4308 static char_u *gfp_buffer;
4309
4310 static void gfp_setname __ARGS((char_u *fname));
4311
4312 /*
4313 * Callback function for do_in_runtimepath().
4314 */
4315 static void
4316 gfp_setname(fname)
4317 char_u *fname;
4318 {
4319 if (STRLEN(fname) >= MAXPATHL)
4320 *gfp_buffer = NUL;
4321 else
4322 STRCPY(gfp_buffer, fname);
4323 }
4324
4325 /*
4326 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4327 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4328 */
4329 int
4330 gui_find_bitmap(name, buffer, ext)
4331 char_u *name;
4332 char_u *buffer;
4333 char *ext;
4334 {
4335 if (STRLEN(name) > MAXPATHL - 14)
4336 return FAIL;
4337 sprintf((char *)buffer, "bitmaps/%s.%s", name, ext);
4338 gfp_buffer = buffer;
4339 if (do_in_runtimepath(buffer, FALSE, gfp_setname) == FAIL || *buffer == NUL)
4340 return FAIL;
4341 return OK;
4342 }
4343
4344 # if !defined(HAVE_GTK2) || defined(PROTO)
4345 /*
4346 * Given the name of the "icon=" argument, try finding the bitmap file for the
4347 * icon. If it is an absolute path name, use it as it is. Otherwise append
4348 * "ext" and search for it in 'runtimepath'.
4349 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4350 * contains "name".
4351 */
4352 void
4353 gui_find_iconfile(name, buffer, ext)
4354 char_u *name;
4355 char_u *buffer;
4356 char *ext;
4357 {
4358 char_u buf[MAXPATHL + 1];
4359
4360 expand_env(name, buffer, MAXPATHL);
4361 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4362 STRCPY(buffer, buf);
4363 }
4364 # endif
4365 #endif
4366
4367 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
4368 void
4369 display_errors()
4370 {
4371 char_u *p;
4372
4373 if (isatty(2))
4374 fflush(stderr);
4375 else if (error_ga.ga_data != NULL)
4376 {
4377 /* avoid putting up a message box with blanks only */
4378 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4379 if (!isspace(*p))
4380 {
4381 /* Truncate a very long message, it will go off-screen. */
4382 if (STRLEN(p) > 2000)
4383 STRCPY(p + 2000 - 14, "...(truncated)");
4384 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4385 p, (char_u *)_("&Ok"), 1, NULL);
4386 break;
4387 }
4388 ga_clear(&error_ga);
4389 }
4390 }
4391 #endif
4392
4393 #if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4394 /*
4395 * Return TRUE if still starting up and there is no place to enter text.
4396 * For GTK and X11 we check if stderr is not a tty, which means we were
4397 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4398 * allow typing on stdin.
4399 */
4400 int
4401 no_console_input()
4402 {
4403 return ((!gui.in_use || gui.starting)
4404 # ifndef NO_CONSOLE
4405 && !isatty(0) && !isatty(2)
4406 # endif
4407 );
4408 }
4409 #endif
4410
4411 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4412 || defined(MSWIN_FIND_REPLACE) || defined(FEAT_SUN_WORKSHOP) \
4413 || defined(PROTO)
4414 /*
4415 * Update the current window and the screen.
4416 */
4417 void
4418 gui_update_screen()
4419 {
4420 update_topline();
4421 validate_cursor();
4422 update_screen(0); /* may need to update the screen */
4423 setcursor();
4424 out_flush(); /* make sure output has been written */
4425 gui_update_cursor(TRUE, FALSE);
4426 gui_mch_flush();
4427 }
4428 #endif
4429
4430 #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MOTIF) \
4431 || defined(MSWIN_FIND_REPLACE) || defined(PROTO)
4432 static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4433
4434 /*
4435 * Get the text to use in a find/replace dialog. Uses the last search pattern
4436 * if the argument is empty.
4437 * Returns an allocated string.
4438 */
4439 char_u *
4440 get_find_dialog_text(arg, wwordp, mcasep)
4441 char_u *arg;
4442 int *wwordp; /* return: TRUE if \< \> found */
4443 int *mcasep; /* return: TRUE if \C found */
4444 {
4445 char_u *text;
4446
4447 if (*arg == NUL)
4448 text = last_search_pat();
4449 else
4450 text = arg;
4451 if (text != NULL)
4452 {
4453 text = vim_strsave(text);
4454 if (text != NULL)
4455 {
4456 int len = STRLEN(text);
4457 int i;
4458
4459 /* Remove "\V" */
4460 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4461 {
4462 mch_memmove(text, text + 2, (size_t)(len - 1));
4463 len -= 2;
4464 }
4465
4466 /* Recognize "\c" and "\C" and remove. */
4467 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4468 {
4469 *mcasep = (text[1] == 'C');
4470 mch_memmove(text, text + 2, (size_t)(len - 1));
4471 len -= 2;
4472 }
4473
4474 /* Recognize "\<text\>" and remove. */
4475 if (len >= 4
4476 && STRNCMP(text, "\\<", 2) == 0
4477 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4478 {
4479 *wwordp = TRUE;
4480 mch_memmove(text, text + 2, (size_t)(len - 4));
4481 text[len - 4] = NUL;
4482 }
4483
4484 /* Recognize "\/" or "\?" and remove. */
4485 for (i = 0; i + 1 < len; ++i)
4486 if (text[i] == '\\' && (text[i + 1] == '/'
4487 || text[i + 1] == '?'))
4488 {
4489 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4490 --len;
4491 }
4492 }
4493 }
4494 return text;
4495 }
4496
4497 /*
4498 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4499 */
4500 static void
4501 concat_esc(gap, text, what)
4502 garray_T *gap;
4503 char_u *text;
4504 int what;
4505 {
4506 while (*text != NUL)
4507 {
4508 #ifdef FEAT_MBYTE
4509 int l = (*mb_ptr2len_check)(text);
4510 if (l > 1)
4511 {
4512 while (--l >= 0)
4513 ga_append(gap, *text++);
4514 continue;
4515 }
4516 #endif
4517 if (*text == what)
4518 ga_append(gap, '\\');
4519 ga_append(gap, *text);
4520 ++text;
4521 }
4522 }
4523
4524 /*
4525 * Handle the press of a button in the find-replace dialog.
4526 * Return TRUE when something was added to the input buffer.
4527 */
4528 int
4529 gui_do_findrepl(flags, find_text, repl_text, down)
4530 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4531 char_u *find_text;
4532 char_u *repl_text;
4533 int down; /* Search downwards. */
4534 {
4535 garray_T ga;
4536 int i;
4537 int type = (flags & FRD_TYPE_MASK);
4538 char_u *p;
4539
4540 ga_init2(&ga, 1, 100);
4541
4542 if (type == FRD_REPLACE)
4543 {
4544 /* Do the replacement when the text under the cursor matches. */
4545 i = STRLEN(find_text);
4546 p = ml_get_cursor();
4547 if (((flags & FRD_MATCH_CASE)
4548 ? STRNCMP(p, find_text, i) == 0
4549 : STRNICMP(p, find_text, i) == 0)
4550 && u_save_cursor() == OK)
4551 {
4552 /* A button was pressed thus undo should be synced. */
4553 if (no_u_sync == 0)
4554 u_sync();
4555
4556 del_bytes((long)i, FALSE);
4557 ins_str(repl_text);
4558 }
4559 }
4560 else if (type == FRD_REPLACEALL)
4561 ga_concat(&ga, (char_u *)"%s/");
4562
4563 ga_concat(&ga, (char_u *)"\\V");
4564 if (flags & FRD_MATCH_CASE)
4565 ga_concat(&ga, (char_u *)"\\C");
4566 else
4567 ga_concat(&ga, (char_u *)"\\c");
4568 if (flags & FRD_WHOLE_WORD)
4569 ga_concat(&ga, (char_u *)"\\<");
4570 if (type == FRD_REPLACEALL || down)
4571 concat_esc(&ga, find_text, '/'); /* escape slashes */
4572 else
4573 concat_esc(&ga, find_text, '?'); /* escape '?' */
4574 if (flags & FRD_WHOLE_WORD)
4575 ga_concat(&ga, (char_u *)"\\>");
4576
4577 if (type == FRD_REPLACEALL)
4578 {
4579 /* A button was pressed, thus undo should be synced. */
4580 if (no_u_sync == 0)
4581 u_sync();
4582
4583 ga_concat(&ga, (char_u *)"/");
4584 concat_esc(&ga, repl_text, '/'); /* escape slashes */
4585 ga_concat(&ga, (char_u *)"/g");
4586 do_cmdline_cmd(ga.ga_data);
4587 }
4588 else
4589 {
4590 /* Search for the next match. */
4591 i = msg_scroll;
4592 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
4593 SEARCH_MSG + SEARCH_MARK);
4594 msg_scroll = i; /* don't let an error message set msg_scroll */
4595 }
4596
4597 if (State & (NORMAL | INSERT))
4598 {
4599 gui_update_screen(); /* update the screen */
4600 msg_didout = 0; /* overwrite any message */
4601 need_wait_return = FALSE; /* don't wait for return */
4602 }
4603
4604 vim_free(ga.ga_data);
4605 return (ga.ga_len > 0);
4606 }
4607
4608 #endif
4609
4610 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
4611 || defined(FEAT_GUI_MSWIN) \
4612 || defined(FEAT_GUI_MAC) \
4613 || defined(PROTO)
4614
4615 #ifdef FEAT_WINDOWS
4616 static void gui_wingoto_xy __ARGS((int x, int y));
4617
4618 /*
4619 * Jump to the window at specified point (x, y).
4620 */
4621 static void
4622 gui_wingoto_xy(x, y)
4623 int x;
4624 int y;
4625 {
4626 int row = Y_2_ROW(y);
4627 int col = X_2_COL(x);
4628 win_T *wp;
4629
4630 if (row >= 0 && col >= 0)
4631 {
4632 wp = mouse_find_win(&row, &col);
4633 if (wp != NULL && wp != curwin)
4634 win_goto(wp);
4635 }
4636 }
4637 #endif
4638
4639 /*
4640 * Process file drop. Mouse cursor position, key modifiers, name of files
4641 * and count of files are given. Argument "fnames[count]" has full pathnames
4642 * of dropped files, they will be freed in this function, and caller can't use
4643 * fnames after call this function.
4644 */
4645 /*ARGSUSED*/
4646 void
4647 gui_handle_drop(x, y, modifiers, fnames, count)
4648 int x;
4649 int y;
4650 int_u modifiers;
4651 char_u **fnames;
4652 int count;
4653 {
4654 int i;
4655 char_u *p;
4656
4657 /*
4658 * When the cursor is at the command line, add the file names to the
4659 * command line, don't edit the files.
4660 */
4661 if (State & CMDLINE)
4662 {
4663 shorten_filenames(fnames, count);
4664 for (i = 0; i < count; ++i)
4665 {
4666 if (fnames[i] != NULL)
4667 {
4668 if (i > 0)
4669 add_to_input_buf((char_u*)" ", 1);
4670
4671 /* We don't know what command is used thus we can't be sure
4672 * about which characters need to be escaped. Only escape the
4673 * most common ones. */
4674 # ifdef BACKSLASH_IN_FILENAME
4675 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
4676 # else
4677 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
4678 # endif
4679 if (p != NULL)
4680 add_to_input_buf(p, (int)STRLEN(p));
4681 vim_free(p);
4682 vim_free(fnames[i]);
4683 }
4684 }
4685 vim_free(fnames);
4686 }
4687 else
4688 {
4689 /* Go to the window under mouse cursor, then shorten given "fnames" by
4690 * current window, because a window can have local current dir. */
4691 # ifdef FEAT_WINDOWS
4692 gui_wingoto_xy(x, y);
4693 # endif
4694 shorten_filenames(fnames, count);
4695
4696 /* If Shift held down, remember the first item. */
4697 if ((modifiers & MOUSE_SHIFT) != 0)
4698 p = vim_strsave(fnames[0]);
4699 else
4700 p = NULL;
4701
4702 /* Handle the drop, :edit or :split to get to the file. This also
4703 * frees fnames[]. Skip this if there is only one item it's a
4704 * directory and Shift is held down. */
4705 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
4706 && mch_isdir(fnames[0]))
4707 {
4708 vim_free(fnames[0]);
4709 vim_free(fnames);
4710 }
4711 else
4712 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
4713
4714 /* If Shift held down, change to first file's directory. If the first
4715 * item is a directory, change to that directory (and let the explorer
4716 * plugin show the contents). */
4717 if (p != NULL)
4718 {
4719 if (mch_isdir(p))
4720 {
4721 if (mch_chdir((char *)p) == 0)
4722 shorten_fnames(TRUE);
4723 }
4724 else if (vim_chdirfile(p) == OK)
4725 shorten_fnames(TRUE);
4726 vim_free(p);
4727 }
4728
4729 /* Update the screen display */
4730 update_screen(NOT_VALID);
4731 # ifdef FEAT_MENU
4732 gui_update_menus(0);
4733 # endif
4734 setcursor();
4735 out_flush();
4736 gui_update_cursor(FALSE, FALSE);
4737 gui_mch_flush();
4738 }
4739 }
4740 #endif