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