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