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