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