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