blob: 8113e56e9e04ffb189b455b1759892b22fe2dcd8 [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 Moolenaard35f9712005-12-18 22:02:33 +00002656 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002658 char_u buf[3];
2659
2660 /* Put K_CURSORHOLD in the input buffer. */
2661 buf[0] = CSI;
2662 buf[1] = KS_EXTRA;
2663 buf[2] = (int)KE_CURSORHOLD;
2664 add_to_input_buf(buf, 3);
2665
2666 retval = OK;
2667 }
2668#endif
2669
2670 if (retval == FAIL)
2671 {
2672 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002673 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677 gui_mch_stop_blink();
2678 return retval;
2679}
2680
2681/*
2682 * Fill buffer with mouse coordinates encoded for check_termcode().
2683 */
2684 static void
2685fill_mouse_coord(p, col, row)
2686 char_u *p;
2687 int col;
2688 int row;
2689{
2690 p[0] = (char_u)(col / 128 + ' ' + 1);
2691 p[1] = (char_u)(col % 128 + ' ' + 1);
2692 p[2] = (char_u)(row / 128 + ' ' + 1);
2693 p[3] = (char_u)(row % 128 + ' ' + 1);
2694}
2695
2696/*
2697 * Generic mouse support function. Add a mouse event to the input buffer with
2698 * the given properties.
2699 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2700 * MOUSE_X1, MOUSE_X2
2701 * MOUSE_DRAG, or MOUSE_RELEASE.
2702 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2703 * x, y --- Coordinates of mouse in pixels.
2704 * repeated_click --- TRUE if this click comes only a short time after a
2705 * previous click.
2706 * modifiers --- Bit field which may be any of the following modifiers
2707 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2708 * This function will ignore drag events where the mouse has not moved to a new
2709 * character.
2710 */
2711 void
2712gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2713 int button;
2714 int x;
2715 int y;
2716 int repeated_click;
2717 int_u modifiers;
2718{
2719 static int prev_row = 0, prev_col = 0;
2720 static int prev_button = -1;
2721 static int num_clicks = 1;
2722 char_u string[10];
2723 enum key_extra button_char;
2724 int row, col;
2725#ifdef FEAT_CLIPBOARD
2726 int checkfor;
2727 int did_clip = FALSE;
2728#endif
2729
2730 /*
2731 * Scrolling may happen at any time, also while a selection is present.
2732 */
2733 switch (button)
2734 {
2735 case MOUSE_X1:
2736 button_char = KE_X1MOUSE;
2737 goto button_set;
2738 case MOUSE_X2:
2739 button_char = KE_X2MOUSE;
2740 goto button_set;
2741 case MOUSE_4:
2742 button_char = KE_MOUSEDOWN;
2743 goto button_set;
2744 case MOUSE_5:
2745 button_char = KE_MOUSEUP;
2746button_set:
2747 {
2748 /* Don't put events in the input queue now. */
2749 if (hold_gui_events)
2750 return;
2751
2752 string[3] = CSI;
2753 string[4] = KS_EXTRA;
2754 string[5] = (int)button_char;
2755
2756 /* Pass the pointer coordinates of the scroll event so that we
2757 * know which window to scroll. */
2758 row = gui_xy2colrow(x, y, &col);
2759 string[6] = (char_u)(col / 128 + ' ' + 1);
2760 string[7] = (char_u)(col % 128 + ' ' + 1);
2761 string[8] = (char_u)(row / 128 + ' ' + 1);
2762 string[9] = (char_u)(row % 128 + ' ' + 1);
2763
2764 if (modifiers == 0)
2765 add_to_input_buf(string + 3, 7);
2766 else
2767 {
2768 string[0] = CSI;
2769 string[1] = KS_MODIFIER;
2770 string[2] = 0;
2771 if (modifiers & MOUSE_SHIFT)
2772 string[2] |= MOD_MASK_SHIFT;
2773 if (modifiers & MOUSE_CTRL)
2774 string[2] |= MOD_MASK_CTRL;
2775 if (modifiers & MOUSE_ALT)
2776 string[2] |= MOD_MASK_ALT;
2777 add_to_input_buf(string, 10);
2778 }
2779 return;
2780 }
2781 }
2782
2783#ifdef FEAT_CLIPBOARD
2784 /* If a clipboard selection is in progress, handle it */
2785 if (clip_star.state == SELECT_IN_PROGRESS)
2786 {
2787 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2788 return;
2789 }
2790
2791 /* Determine which mouse settings to look for based on the current mode */
2792 switch (get_real_state())
2793 {
2794 case NORMAL_BUSY:
2795 case OP_PENDING:
2796 case NORMAL: checkfor = MOUSE_NORMAL; break;
2797 case VISUAL: checkfor = MOUSE_VISUAL; break;
2798 case REPLACE:
2799 case REPLACE+LANGMAP:
2800#ifdef FEAT_VREPLACE
2801 case VREPLACE:
2802 case VREPLACE+LANGMAP:
2803#endif
2804 case INSERT:
2805 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2806 case ASKMORE:
2807 case HITRETURN: /* At the more- and hit-enter prompt pass the
2808 mouse event for a click on or below the
2809 message line. */
2810 if (Y_2_ROW(y) >= msg_row)
2811 checkfor = MOUSE_NORMAL;
2812 else
2813 checkfor = MOUSE_RETURN;
2814 break;
2815
2816 /*
2817 * On the command line, use the clipboard selection on all lines
2818 * but the command line. But not when pasting.
2819 */
2820 case CMDLINE:
2821 case CMDLINE+LANGMAP:
2822 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2823 checkfor = MOUSE_NONE;
2824 else
2825 checkfor = MOUSE_COMMAND;
2826 break;
2827
2828 default:
2829 checkfor = MOUSE_NONE;
2830 break;
2831 };
2832
2833 /*
2834 * Allow clipboard selection of text on the command line in "normal"
2835 * modes. Don't do this when dragging the status line, or extending a
2836 * Visual selection.
2837 */
2838 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2839 && Y_2_ROW(y) >= topframe->fr_height
2840 && button != MOUSE_DRAG
2841# ifdef FEAT_MOUSESHAPE
2842 && !drag_status_line
2843# ifdef FEAT_VERTSPLIT
2844 && !drag_sep_line
2845# endif
2846# endif
2847 )
2848 checkfor = MOUSE_NONE;
2849
2850 /*
2851 * Use modeless selection when holding CTRL and SHIFT pressed.
2852 */
2853 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2854 checkfor = MOUSE_NONEF;
2855
2856 /*
2857 * In Ex mode, always use modeless selection.
2858 */
2859 if (exmode_active)
2860 checkfor = MOUSE_NONE;
2861
2862 /*
2863 * If the mouse settings say to not use the mouse, use the modeless
2864 * selection. But if Visual is active, assume that only the Visual area
2865 * will be selected.
2866 * Exception: On the command line, both the selection is used and a mouse
2867 * key is send.
2868 */
2869 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2870 {
2871#ifdef FEAT_VISUAL
2872 /* Don't do modeless selection in Visual mode. */
2873 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2874 return;
2875#endif
2876
2877 /*
2878 * When 'mousemodel' is "popup", shift-left is translated to right.
2879 * But not when also using Ctrl.
2880 */
2881 if (mouse_model_popup() && button == MOUSE_LEFT
2882 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2883 {
2884 button = MOUSE_RIGHT;
2885 modifiers &= ~ MOUSE_SHIFT;
2886 }
2887
2888 /* If the selection is done, allow the right button to extend it.
2889 * If the selection is cleared, allow the right button to start it
2890 * from the cursor position. */
2891 if (button == MOUSE_RIGHT)
2892 {
2893 if (clip_star.state == SELECT_CLEARED)
2894 {
2895 if (State & CMDLINE)
2896 {
2897 col = msg_col;
2898 row = msg_row;
2899 }
2900 else
2901 {
2902 col = curwin->w_wcol;
2903 row = curwin->w_wrow + W_WINROW(curwin);
2904 }
2905 clip_start_selection(col, row, FALSE);
2906 }
2907 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2908 repeated_click);
2909 did_clip = TRUE;
2910 }
2911 /* Allow the left button to start the selection */
2912 else if (button ==
2913# ifdef RISCOS
2914 /* Only start a drag on a drag event. Otherwise
2915 * we don't get a release event. */
2916 MOUSE_DRAG
2917# else
2918 MOUSE_LEFT
2919# endif
2920 )
2921 {
2922 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2923 did_clip = TRUE;
2924 }
2925# ifdef RISCOS
2926 else if (button == MOUSE_LEFT)
2927 {
2928 clip_clear_selection();
2929 did_clip = TRUE;
2930 }
2931# endif
2932
2933 /* Always allow pasting */
2934 if (button != MOUSE_MIDDLE)
2935 {
2936 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2937 return;
2938 if (checkfor != MOUSE_COMMAND)
2939 button = MOUSE_LEFT;
2940 }
2941 repeated_click = FALSE;
2942 }
2943
2944 if (clip_star.state != SELECT_CLEARED && !did_clip)
2945 clip_clear_selection();
2946#endif
2947
2948 /* Don't put events in the input queue now. */
2949 if (hold_gui_events)
2950 return;
2951
2952 row = gui_xy2colrow(x, y, &col);
2953
2954 /*
2955 * If we are dragging and the mouse hasn't moved far enough to be on a
2956 * different character, then don't send an event to vim.
2957 */
2958 if (button == MOUSE_DRAG)
2959 {
2960 if (row == prev_row && col == prev_col)
2961 return;
2962 /* Dragging above the window, set "row" to -1 to cause a scroll. */
2963 if (y < 0)
2964 row = -1;
2965 }
2966
2967 /*
2968 * If topline has changed (window scrolled) since the last click, reset
2969 * repeated_click, because we don't want starting Visual mode when
2970 * clicking on a different character in the text.
2971 */
2972 if (curwin->w_topline != gui_prev_topline
2973#ifdef FEAT_DIFF
2974 || curwin->w_topfill != gui_prev_topfill
2975#endif
2976 )
2977 repeated_click = FALSE;
2978
2979 string[0] = CSI; /* this sequence is recognized by check_termcode() */
2980 string[1] = KS_MOUSE;
2981 string[2] = KE_FILLER;
2982 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
2983 {
2984 if (repeated_click)
2985 {
2986 /*
2987 * Handle multiple clicks. They only count if the mouse is still
2988 * pointing at the same character.
2989 */
2990 if (button != prev_button || row != prev_row || col != prev_col)
2991 num_clicks = 1;
2992 else if (++num_clicks > 4)
2993 num_clicks = 1;
2994 }
2995 else
2996 num_clicks = 1;
2997 prev_button = button;
2998 gui_prev_topline = curwin->w_topline;
2999#ifdef FEAT_DIFF
3000 gui_prev_topfill = curwin->w_topfill;
3001#endif
3002
3003 string[3] = (char_u)(button | 0x20);
3004 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3005 }
3006 else
3007 string[3] = (char_u)button;
3008
3009 string[3] |= modifiers;
3010 fill_mouse_coord(string + 4, col, row);
3011 add_to_input_buf(string, 8);
3012
3013 if (row < 0)
3014 prev_row = 0;
3015 else
3016 prev_row = row;
3017 prev_col = col;
3018
3019 /*
3020 * We need to make sure this is cleared since Athena doesn't tell us when
3021 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3022 */
3023#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3024 gui.dragged_sb = SBAR_NONE;
3025#endif
3026}
3027
3028/*
3029 * Convert x and y coordinate to column and row in text window.
3030 * Corrects for multi-byte character.
3031 * returns column in "*colp" and row as return value;
3032 */
3033 int
3034gui_xy2colrow(x, y, colp)
3035 int x;
3036 int y;
3037 int *colp;
3038{
3039 int col = check_col(X_2_COL(x));
3040 int row = check_row(Y_2_ROW(y));
3041
3042#ifdef FEAT_MBYTE
3043 *colp = mb_fix_col(col, row);
3044#else
3045 *colp = col;
3046#endif
3047 return row;
3048}
3049
3050#if defined(FEAT_MENU) || defined(PROTO)
3051/*
3052 * Callback function for when a menu entry has been selected.
3053 */
3054 void
3055gui_menu_cb(menu)
3056 vimmenu_T *menu;
3057{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003058 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060 /* Don't put events in the input queue now. */
3061 if (hold_gui_events)
3062 return;
3063
3064 bytes[0] = CSI;
3065 bytes[1] = KS_MENU;
3066 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003067 add_to_input_buf(bytes, 3);
3068 add_long_to_buf((long_u)menu, bytes);
3069 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070}
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
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003362 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 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
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003532 add_to_input_buf(bytes, byte_count);
3533 add_long_to_buf((long_u)value, bytes);
3534 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#endif
3536}
3537
3538/*
3539 * Scrollbar stuff:
3540 */
3541
3542 void
3543gui_update_scrollbars(force)
3544 int force; /* Force all scrollbars to get updated */
3545{
3546 win_T *wp;
3547 scrollbar_T *sb;
3548 long val, size, max; /* need 32 bits here */
3549 int which_sb;
3550 int h, y;
3551#ifdef FEAT_VERTSPLIT
3552 static win_T *prev_curwin = NULL;
3553#endif
3554
3555 /* Update the horizontal scrollbar */
3556 gui_update_horiz_scrollbar(force);
3557
3558#ifndef WIN3264
3559 /* Return straight away if there is neither a left nor right scrollbar.
3560 * On MS-Windows this is required anyway for scrollwheel messages. */
3561 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3562 return;
3563#endif
3564
3565 /*
3566 * Don't want to update a scrollbar while we're dragging it. But if we
3567 * have both a left and right scrollbar, and we drag one of them, we still
3568 * need to update the other one.
3569 */
3570 if ( (gui.dragged_sb == SBAR_LEFT
3571 || gui.dragged_sb == SBAR_RIGHT)
3572 && (!gui.which_scrollbars[SBAR_LEFT]
3573 || !gui.which_scrollbars[SBAR_RIGHT])
3574 && !force)
3575 return;
3576
3577 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT))
3578 {
3579 /*
3580 * If we have two scrollbars and one of them is being dragged, just
3581 * copy the scrollbar position from the dragged one to the other one.
3582 */
3583 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3584 if (gui.dragged_wp != NULL)
3585 gui_mch_set_scrollbar_thumb(
3586 &gui.dragged_wp->w_scrollbars[which_sb],
3587 gui.dragged_wp->w_scrollbars[0].value,
3588 gui.dragged_wp->w_scrollbars[0].size,
3589 gui.dragged_wp->w_scrollbars[0].max);
3590 return;
3591 }
3592
3593 /* avoid that moving components around generates events */
3594 ++hold_gui_events;
3595
3596 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3597 {
3598 if (wp->w_buffer == NULL) /* just in case */
3599 continue;
3600#ifdef SCROLL_PAST_END
3601 max = wp->w_buffer->b_ml.ml_line_count - 1;
3602#else
3603 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3604#endif
3605 if (max < 0) /* empty buffer */
3606 max = 0;
3607 val = wp->w_topline - 1;
3608 size = wp->w_height;
3609#ifdef SCROLL_PAST_END
3610 if (val > max) /* just in case */
3611 val = max;
3612#else
3613 if (size > max + 1) /* just in case */
3614 size = max + 1;
3615 if (val > max - size + 1)
3616 val = max - size + 1;
3617#endif
3618 if (val < 0) /* minimal value is 0 */
3619 val = 0;
3620
3621 /*
3622 * Scrollbar at index 0 (the left one) contains all the information.
3623 * It would be the same info for left and right so we just store it for
3624 * one of them.
3625 */
3626 sb = &wp->w_scrollbars[0];
3627
3628 /*
3629 * Note: no check for valid w_botline. If it's not valid the
3630 * scrollbars will be updated later anyway.
3631 */
3632 if (size < 1 || wp->w_botline - 2 > max)
3633 {
3634 /*
3635 * This can happen during changing files. Just don't update the
3636 * scrollbar for now.
3637 */
3638 sb->height = 0; /* Force update next time */
3639 if (gui.which_scrollbars[SBAR_LEFT])
3640 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3641 if (gui.which_scrollbars[SBAR_RIGHT])
3642 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3643 continue;
3644 }
3645 if (force || sb->height != wp->w_height
3646#ifdef FEAT_WINDOWS
3647 || sb->top != wp->w_winrow
3648 || sb->status_height != wp->w_status_height
3649# ifdef FEAT_VERTSPLIT
3650 || sb->width != wp->w_width
3651 || prev_curwin != curwin
3652# endif
3653#endif
3654 )
3655 {
3656 /* Height, width or position of scrollbar has changed. For
3657 * vertical split: curwin changed. */
3658 sb->height = wp->w_height;
3659#ifdef FEAT_WINDOWS
3660 sb->top = wp->w_winrow;
3661 sb->status_height = wp->w_status_height;
3662# ifdef FEAT_VERTSPLIT
3663 sb->width = wp->w_width;
3664# endif
3665#endif
3666
3667 /* Calculate height and position in pixels */
3668 h = (sb->height + sb->status_height) * gui.char_height;
3669 y = sb->top * gui.char_height + gui.border_offset;
3670#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
3671 if (gui.menu_is_active)
3672 y += gui.menu_height;
3673#endif
3674
3675#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
3676 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
3677# ifdef FEAT_GUI_ATHENA
3678 y += gui.toolbar_height;
3679# else
3680# ifdef FEAT_GUI_MSWIN
3681 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
3682# endif
3683# endif
3684#endif
3685
3686#ifdef FEAT_WINDOWS
3687 if (wp->w_winrow == 0)
3688#endif
3689 {
3690 /* Height of top scrollbar includes width of top border */
3691 h += gui.border_offset;
3692 y -= gui.border_offset;
3693 }
3694 if (gui.which_scrollbars[SBAR_LEFT])
3695 {
3696 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
3697 gui.left_sbar_x, y,
3698 gui.scrollbar_width, h);
3699 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
3700 }
3701 if (gui.which_scrollbars[SBAR_RIGHT])
3702 {
3703 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
3704 gui.right_sbar_x, y,
3705 gui.scrollbar_width, h);
3706 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
3707 }
3708 }
3709
3710 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
3711 * checking if the thumb moved at least a pixel. Only do this for
3712 * Athena, most other GUIs require the update anyway to make the
3713 * arrows work. */
3714#ifdef FEAT_GUI_ATHENA
3715 if (max == 0)
3716 y = 0;
3717 else
3718 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
3719 if (force || sb->pixval != y || sb->size != size || sb->max != max)
3720#else
3721 if (force || sb->value != val || sb->size != size || sb->max != max)
3722#endif
3723 {
3724 /* Thumb of scrollbar has moved */
3725 sb->value = val;
3726#ifdef FEAT_GUI_ATHENA
3727 sb->pixval = y;
3728#endif
3729 sb->size = size;
3730 sb->max = max;
3731 if (gui.which_scrollbars[SBAR_LEFT] && gui.dragged_sb != SBAR_LEFT)
3732 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
3733 val, size, max);
3734 if (gui.which_scrollbars[SBAR_RIGHT]
3735 && gui.dragged_sb != SBAR_RIGHT)
3736 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
3737 val, size, max);
3738 }
3739 }
3740#ifdef FEAT_VERTSPLIT
3741 prev_curwin = curwin;
3742#endif
3743 --hold_gui_events;
3744}
3745
3746/*
3747 * Enable or disable a scrollbar.
3748 * Check for scrollbars for vertically split windows which are not enabled
3749 * sometimes.
3750 */
3751 static void
3752gui_do_scrollbar(wp, which, enable)
3753 win_T *wp;
3754 int which; /* SBAR_LEFT or SBAR_RIGHT */
3755 int enable; /* TRUE to enable scrollbar */
3756{
3757#ifdef FEAT_VERTSPLIT
3758 int midcol = curwin->w_wincol + curwin->w_width / 2;
3759 int has_midcol = (wp->w_wincol <= midcol
3760 && wp->w_wincol + wp->w_width >= midcol);
3761
3762 /* Only enable scrollbars that contain the middle column of the current
3763 * window. */
3764 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
3765 {
3766 /* Scrollbars only on one side. Don't enable scrollbars that don't
3767 * contain the middle column of the current window. */
3768 if (!has_midcol)
3769 enable = FALSE;
3770 }
3771 else
3772 {
3773 /* Scrollbars on both sides. Don't enable scrollbars that neither
3774 * contain the middle column of the current window nor are on the far
3775 * side. */
3776 if (midcol > Columns / 2)
3777 {
3778 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
3779 enable = FALSE;
3780 }
3781 else
3782 {
3783 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
3784 : !has_midcol)
3785 enable = FALSE;
3786 }
3787 }
3788#endif
3789 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
3790}
3791
3792/*
3793 * Scroll a window according to the values set in the globals current_scrollbar
3794 * and scrollbar_value. Return TRUE if the cursor in the current window moved
3795 * or FALSE otherwise.
3796 */
3797 int
3798gui_do_scroll()
3799{
3800 win_T *wp, *save_wp;
3801 int i;
3802 long nlines;
3803 pos_T old_cursor;
3804 linenr_T old_topline;
3805#ifdef FEAT_DIFF
3806 int old_topfill;
3807#endif
3808
3809 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
3810 if (wp == NULL)
3811 break;
3812 if (wp == NULL)
3813 /* Couldn't find window */
3814 return FALSE;
3815
3816 /*
3817 * Compute number of lines to scroll. If zero, nothing to do.
3818 */
3819 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
3820 if (nlines == 0)
3821 return FALSE;
3822
3823 save_wp = curwin;
3824 old_topline = wp->w_topline;
3825#ifdef FEAT_DIFF
3826 old_topfill = wp->w_topfill;
3827#endif
3828 old_cursor = wp->w_cursor;
3829 curwin = wp;
3830 curbuf = wp->w_buffer;
3831 if (nlines < 0)
3832 scrolldown(-nlines, gui.dragged_wp == NULL);
3833 else
3834 scrollup(nlines, gui.dragged_wp == NULL);
3835 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
3836 * the mouse-up event already, but we still want it to behave like when
3837 * dragging. But not the next click in an arrow. */
3838 if (gui.dragged_sb == SBAR_NONE)
3839 gui.dragged_wp = NULL;
3840
3841 if (old_topline != wp->w_topline
3842#ifdef FEAT_DIFF
3843 || old_topfill != wp->w_topfill
3844#endif
3845 )
3846 {
3847 if (p_so != 0)
3848 {
3849 cursor_correct(); /* fix window for 'so' */
3850 update_topline(); /* avoid up/down jump */
3851 }
3852 if (old_cursor.lnum != wp->w_cursor.lnum)
3853 coladvance(wp->w_curswant);
3854#ifdef FEAT_SCROLLBIND
3855 wp->w_scbind_pos = wp->w_topline;
3856#endif
3857 }
3858
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003859 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
3860 validate_cursor();
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 curwin = save_wp;
3863 curbuf = save_wp->w_buffer;
3864
3865 /*
3866 * Don't call updateWindow() when nothing has changed (it will overwrite
3867 * the status line!).
3868 */
3869 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003870 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871#ifdef FEAT_DIFF
3872 || old_topfill != wp->w_topfill
3873#endif
3874 )
3875 {
3876 redraw_win_later(wp, VALID);
3877 updateWindow(wp); /* update window, status line, and cmdline */
3878 }
3879
3880 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
3881}
3882
3883
3884/*
3885 * Horizontal scrollbar stuff:
3886 */
3887
3888/*
3889 * Return length of line "lnum" for horizontal scrolling.
3890 */
3891 static colnr_T
3892scroll_line_len(lnum)
3893 linenr_T lnum;
3894{
3895 char_u *p;
3896 colnr_T col;
3897 int w;
3898
3899 p = ml_get(lnum);
3900 col = 0;
3901 if (*p != NUL)
3902 for (;;)
3903 {
3904 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003905 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 if (*p == NUL) /* don't count the last character */
3907 break;
3908 col += w;
3909 }
3910 return col;
3911}
3912
3913/* Remember which line is currently the longest, so that we don't have to
3914 * search for it when scrolling horizontally. */
3915static linenr_T longest_lnum = 0;
3916
3917 static void
3918gui_update_horiz_scrollbar(force)
3919 int force;
3920{
3921 long value, size, max; /* need 32 bit ints here */
3922
3923 if (!gui.which_scrollbars[SBAR_BOTTOM])
3924 return;
3925
3926 if (!force && gui.dragged_sb == SBAR_BOTTOM)
3927 return;
3928
3929 if (!force && curwin->w_p_wrap && gui.prev_wrap)
3930 return;
3931
3932 /*
3933 * It is possible for the cursor to be invalid if we're in the middle of
3934 * something (like changing files). If so, don't do anything for now.
3935 */
3936 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3937 {
3938 gui.bottom_sbar.value = -1;
3939 return;
3940 }
3941
3942 size = W_WIDTH(curwin);
3943 if (curwin->w_p_wrap)
3944 {
3945 value = 0;
3946#ifdef SCROLL_PAST_END
3947 max = 0;
3948#else
3949 max = W_WIDTH(curwin) - 1;
3950#endif
3951 }
3952 else
3953 {
3954 value = curwin->w_leftcol;
3955
3956 /* Calculate maximum for horizontal scrollbar. Check for reasonable
3957 * line numbers, topline and botline can be invalid when displaying is
3958 * postponed. */
3959 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
3960 && curwin->w_topline <= curwin->w_cursor.lnum
3961 && curwin->w_botline > curwin->w_cursor.lnum
3962 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
3963 {
3964 linenr_T lnum;
3965 colnr_T n;
3966
3967 /* Use maximum of all visible lines. Remember the lnum of the
3968 * longest line, clostest to the cursor line. Used when scrolling
3969 * below. */
3970 max = 0;
3971 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
3972 {
3973 n = scroll_line_len(lnum);
3974 if (n > (colnr_T)max)
3975 {
3976 max = n;
3977 longest_lnum = lnum;
3978 }
3979 else if (n == (colnr_T)max
3980 && abs((int)(lnum - curwin->w_cursor.lnum))
3981 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
3982 longest_lnum = lnum;
3983 }
3984 }
3985 else
3986 /* Use cursor line only. */
3987 max = scroll_line_len(curwin->w_cursor.lnum);
3988#ifdef FEAT_VIRTUALEDIT
3989 if (virtual_active())
3990 {
3991 /* May move the cursor even further to the right. */
3992 if (curwin->w_virtcol >= (colnr_T)max)
3993 max = curwin->w_virtcol;
3994 }
3995#endif
3996
3997#ifndef SCROLL_PAST_END
3998 max += W_WIDTH(curwin) - 1;
3999#endif
4000 /* The line number isn't scrolled, thus there is less space when
4001 * 'number' is set (also for 'foldcolumn'). */
4002 size -= curwin_col_off();
4003#ifndef SCROLL_PAST_END
4004 max -= curwin_col_off();
4005#endif
4006 }
4007
4008#ifndef SCROLL_PAST_END
4009 if (value > max - size + 1)
4010 value = max - size + 1; /* limit the value to allowable range */
4011#endif
4012
4013#ifdef FEAT_RIGHTLEFT
4014 if (curwin->w_p_rl)
4015 {
4016 value = max + 1 - size - value;
4017 if (value < 0)
4018 {
4019 size += value;
4020 value = 0;
4021 }
4022 }
4023#endif
4024 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4025 && max == gui.bottom_sbar.max)
4026 return;
4027
4028 gui.bottom_sbar.value = value;
4029 gui.bottom_sbar.size = size;
4030 gui.bottom_sbar.max = max;
4031 gui.prev_wrap = curwin->w_p_wrap;
4032
4033 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4034}
4035
4036/*
4037 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4038 */
4039 int
4040gui_do_horiz_scroll()
4041{
4042 /* no wrapping, no scrolling */
4043 if (curwin->w_p_wrap)
4044 return FALSE;
4045
4046 if (curwin->w_leftcol == scrollbar_value)
4047 return FALSE;
4048
4049 curwin->w_leftcol = scrollbar_value;
4050
4051 /* When the line of the cursor is too short, move the cursor to the
4052 * longest visible line. Do a sanity check on "longest_lnum", just in
4053 * case. */
4054 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4055 && longest_lnum >= curwin->w_topline
4056 && longest_lnum < curwin->w_botline
4057 && !virtual_active())
4058 {
4059 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4060 {
4061 curwin->w_cursor.lnum = longest_lnum;
4062 curwin->w_cursor.col = 0;
4063 }
4064 }
4065
4066 return leftcol_changed();
4067}
4068
4069/*
4070 * Check that none of the colors are the same as the background color
4071 */
4072 void
4073gui_check_colors()
4074{
4075 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4076 {
4077 gui_set_bg_color((char_u *)"White");
4078 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4079 gui_set_fg_color((char_u *)"Black");
4080 }
4081}
4082
Bram Moolenaar3918c952005-03-15 22:34:55 +00004083 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084gui_set_fg_color(name)
4085 char_u *name;
4086{
4087 gui.norm_pixel = gui_get_color(name);
4088 hl_set_fg_color_name(vim_strsave(name));
4089}
4090
Bram Moolenaar3918c952005-03-15 22:34:55 +00004091 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092gui_set_bg_color(name)
4093 char_u *name;
4094{
4095 gui.back_pixel = gui_get_color(name);
4096 hl_set_bg_color_name(vim_strsave(name));
4097}
4098
4099/*
4100 * Allocate a color by name.
4101 * Returns INVALCOLOR and gives an error message when failed.
4102 */
4103 guicolor_T
4104gui_get_color(name)
4105 char_u *name;
4106{
4107 guicolor_T t;
4108
4109 if (*name == NUL)
4110 return INVALCOLOR;
4111 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004114#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 && gui.in_use
4116#endif
4117 )
4118 EMSG2(_("E254: Cannot allocate color %s"), name);
4119 return t;
4120}
4121
4122/*
4123 * Return the grey value of a color (range 0-255).
4124 */
4125 int
4126gui_get_lightness(pixel)
4127 guicolor_T pixel;
4128{
4129 long_u rgb = gui_mch_get_rgb(pixel);
4130
4131 return ( (((rgb >> 16) & 0xff) * 299)
4132 + (((rgb >> 8) & 0xff) * 587)
4133 + ((rgb & 0xff) * 114)) / 1000;
4134}
4135
4136#if defined(FEAT_GUI_X11) || defined(PROTO)
4137 void
4138gui_new_scrollbar_colors()
4139{
4140 win_T *wp;
4141
4142 /* Nothing to do if GUI hasn't started yet. */
4143 if (!gui.in_use)
4144 return;
4145
4146 FOR_ALL_WINDOWS(wp)
4147 {
4148 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4149 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4150 }
4151 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4152}
4153#endif
4154
4155/*
4156 * Call this when focus has changed.
4157 */
4158 void
4159gui_focus_change(in_focus)
4160 int in_focus;
4161{
4162/*
4163 * Skip this code to avoid drawing the cursor when debugging and switching
4164 * between the debugger window and gvim.
4165 */
4166#if 1
4167 gui.in_focus = in_focus;
4168 out_flush(); /* make sure output has been written */
4169 gui_update_cursor(TRUE, FALSE);
4170
4171# ifdef FEAT_XIM
4172 xim_set_focus(in_focus);
4173# endif
4174
4175 ui_focus_change(in_focus);
4176#endif
4177}
4178
4179/*
4180 * Called when the mouse moved (but not when dragging).
4181 */
4182 void
4183gui_mouse_moved(x, y)
4184 int x;
4185 int y;
4186{
4187 win_T *wp;
4188 char_u st[6];
4189
4190#ifdef FEAT_MOUSESHAPE
4191 /* Get window pointer, and update mouse shape as well. */
4192 wp = xy2win(x, y);
4193#endif
4194
4195 /* Only handle this when 'mousefocus' set and ... */
4196 if (p_mousef
4197 && !hold_gui_events /* not holding events */
4198 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4199 && State != HITRETURN /* but not hit-return prompt */
4200 && msg_scrolled == 0 /* no scrolled message */
4201 && !need_mouse_correct /* not moving the pointer */
4202 && gui.in_focus) /* gvim in focus */
4203 {
4204 /* Don't move the mouse when it's left or right of the Vim window */
4205 if (x < 0 || x > Columns * gui.char_width)
4206 return;
4207#ifndef FEAT_MOUSESHAPE
4208 wp = xy2win(x, y);
4209#endif
4210 if (wp == curwin || wp == NULL)
4211 return; /* still in the same old window, or none at all */
4212
4213 /*
4214 * format a mouse click on status line input
4215 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004216 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4217 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 */
4219 if (finish_op)
4220 {
4221 /* abort the current operator first */
4222 st[0] = ESC;
4223 add_to_input_buf(st, 1);
4224 }
4225 st[0] = CSI;
4226 st[1] = KS_MOUSE;
4227 st[2] = KE_FILLER;
4228 st[3] = (char_u)MOUSE_LEFT;
4229 fill_mouse_coord(st + 4,
4230#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004231 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#else
4233 -1,
4234#endif
4235 wp->w_height + W_WINROW(wp));
4236
4237 add_to_input_buf(st, 8);
4238 st[3] = (char_u)MOUSE_RELEASE;
4239 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240#ifdef FEAT_GUI_GTK
4241 /* Need to wake up the main loop */
4242 if (gtk_main_level() > 0)
4243 gtk_main_quit();
4244#endif
4245 }
4246}
4247
4248/*
4249 * Called when mouse should be moved to window with focus.
4250 */
4251 void
4252gui_mouse_correct()
4253{
4254 int x, y;
4255 win_T *wp = NULL;
4256
4257 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004258
4259 if (!(gui.in_use && p_mousef))
4260 return;
4261
4262 gui_mch_getmouse(&x, &y);
4263 /* Don't move the mouse when it's left or right of the Vim window */
4264 if (x < 0 || x > Columns * gui.char_width)
4265 return;
4266 if (y >= 0)
4267 wp = xy2win(x, y);
4268 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004270 validate_cline_row();
4271 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4272 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275}
4276
4277/*
4278 * Find window where the mouse pointer "y" coordinate is in.
4279 */
4280/*ARGSUSED*/
4281 static win_T *
4282xy2win(x, y)
4283 int x;
4284 int y;
4285{
4286#ifdef FEAT_WINDOWS
4287 int row;
4288 int col;
4289 win_T *wp;
4290
4291 row = Y_2_ROW(y);
4292 col = X_2_COL(x);
4293 if (row < 0 || col < 0) /* before first window */
4294 return NULL;
4295 wp = mouse_find_win(&row, &col);
4296# ifdef FEAT_MOUSESHAPE
4297 if (State == HITRETURN || State == ASKMORE)
4298 {
4299 if (Y_2_ROW(y) >= msg_row)
4300 update_mouseshape(SHAPE_IDX_MOREL);
4301 else
4302 update_mouseshape(SHAPE_IDX_MORE);
4303 }
4304 else if (row > wp->w_height) /* below status line */
4305 update_mouseshape(SHAPE_IDX_CLINE);
4306# ifdef FEAT_VERTSPLIT
4307 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4308 && (row != wp->w_height || !stl_connected(wp)))
4309 update_mouseshape(SHAPE_IDX_VSEP);
4310# endif
4311 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4312 && row == wp->w_height)
4313 update_mouseshape(SHAPE_IDX_STATUS);
4314 else
4315 update_mouseshape(-2);
4316# endif
4317 return wp;
4318#else
4319 return firstwin;
4320#endif
4321}
4322
4323/*
4324 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4325 * File names may be given to redefine the args list.
4326 */
4327 void
4328ex_gui(eap)
4329 exarg_T *eap;
4330{
4331 char_u *arg = eap->arg;
4332
4333 /*
4334 * Check for "-f" argument: foreground, don't fork.
4335 * Also don't fork when started with "gvim -f".
4336 * Do fork when using "gui -b".
4337 */
4338 if (arg[0] == '-'
4339 && (arg[1] == 'f' || arg[1] == 'b')
4340 && (arg[2] == NUL || vim_iswhite(arg[2])))
4341 {
4342 gui.dofork = (arg[1] == 'b');
4343 eap->arg = skipwhite(eap->arg + 2);
4344 }
4345 if (!gui.in_use)
4346 {
4347 /* Clear the command. Needed for when forking+exiting, to avoid part
4348 * of the argument ending up after the shell prompt. */
4349 msg_clr_eos_force();
4350 gui_start();
4351 }
4352 if (!ends_excmd(*eap->arg))
4353 ex_next(eap);
4354}
4355
4356#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004357 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358/*
4359 * This is shared between Athena, Motif and GTK.
4360 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004361static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363/*
4364 * Callback function for do_in_runtimepath().
4365 */
4366 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004367gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004369 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004371 char_u *gfp_buffer = cookie;
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (STRLEN(fname) >= MAXPATHL)
4374 *gfp_buffer = NUL;
4375 else
4376 STRCPY(gfp_buffer, fname);
4377}
4378
4379/*
4380 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4381 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4382 */
4383 int
4384gui_find_bitmap(name, buffer, ext)
4385 char_u *name;
4386 char_u *buffer;
4387 char *ext;
4388{
4389 if (STRLEN(name) > MAXPATHL - 14)
4390 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004391 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4393 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
4395 return OK;
4396}
4397
4398# if !defined(HAVE_GTK2) || defined(PROTO)
4399/*
4400 * Given the name of the "icon=" argument, try finding the bitmap file for the
4401 * icon. If it is an absolute path name, use it as it is. Otherwise append
4402 * "ext" and search for it in 'runtimepath'.
4403 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4404 * contains "name".
4405 */
4406 void
4407gui_find_iconfile(name, buffer, ext)
4408 char_u *name;
4409 char_u *buffer;
4410 char *ext;
4411{
4412 char_u buf[MAXPATHL + 1];
4413
4414 expand_env(name, buffer, MAXPATHL);
4415 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4416 STRCPY(buffer, buf);
4417}
4418# endif
4419#endif
4420
Bram Moolenaar9372a112005-12-06 19:59:18 +00004421#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 void
4423display_errors()
4424{
4425 char_u *p;
4426
4427 if (isatty(2))
4428 fflush(stderr);
4429 else if (error_ga.ga_data != NULL)
4430 {
4431 /* avoid putting up a message box with blanks only */
4432 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4433 if (!isspace(*p))
4434 {
4435 /* Truncate a very long message, it will go off-screen. */
4436 if (STRLEN(p) > 2000)
4437 STRCPY(p + 2000 - 14, "...(truncated)");
4438 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4439 p, (char_u *)_("&Ok"), 1, NULL);
4440 break;
4441 }
4442 ga_clear(&error_ga);
4443 }
4444}
4445#endif
4446
4447#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4448/*
4449 * Return TRUE if still starting up and there is no place to enter text.
4450 * For GTK and X11 we check if stderr is not a tty, which means we were
4451 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4452 * allow typing on stdin.
4453 */
4454 int
4455no_console_input()
4456{
4457 return ((!gui.in_use || gui.starting)
4458# ifndef NO_CONSOLE
4459 && !isatty(0) && !isatty(2)
4460# endif
4461 );
4462}
4463#endif
4464
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004465#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4466 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467/*
4468 * Update the current window and the screen.
4469 */
4470 void
4471gui_update_screen()
4472{
4473 update_topline();
4474 validate_cursor();
4475 update_screen(0); /* may need to update the screen */
4476 setcursor();
4477 out_flush(); /* make sure output has been written */
4478 gui_update_cursor(TRUE, FALSE);
4479 gui_mch_flush();
4480}
4481#endif
4482
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004483#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4485
4486/*
4487 * Get the text to use in a find/replace dialog. Uses the last search pattern
4488 * if the argument is empty.
4489 * Returns an allocated string.
4490 */
4491 char_u *
4492get_find_dialog_text(arg, wwordp, mcasep)
4493 char_u *arg;
4494 int *wwordp; /* return: TRUE if \< \> found */
4495 int *mcasep; /* return: TRUE if \C found */
4496{
4497 char_u *text;
4498
4499 if (*arg == NUL)
4500 text = last_search_pat();
4501 else
4502 text = arg;
4503 if (text != NULL)
4504 {
4505 text = vim_strsave(text);
4506 if (text != NULL)
4507 {
4508 int len = STRLEN(text);
4509 int i;
4510
4511 /* Remove "\V" */
4512 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4513 {
4514 mch_memmove(text, text + 2, (size_t)(len - 1));
4515 len -= 2;
4516 }
4517
4518 /* Recognize "\c" and "\C" and remove. */
4519 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4520 {
4521 *mcasep = (text[1] == 'C');
4522 mch_memmove(text, text + 2, (size_t)(len - 1));
4523 len -= 2;
4524 }
4525
4526 /* Recognize "\<text\>" and remove. */
4527 if (len >= 4
4528 && STRNCMP(text, "\\<", 2) == 0
4529 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4530 {
4531 *wwordp = TRUE;
4532 mch_memmove(text, text + 2, (size_t)(len - 4));
4533 text[len - 4] = NUL;
4534 }
4535
4536 /* Recognize "\/" or "\?" and remove. */
4537 for (i = 0; i + 1 < len; ++i)
4538 if (text[i] == '\\' && (text[i + 1] == '/'
4539 || text[i + 1] == '?'))
4540 {
4541 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4542 --len;
4543 }
4544 }
4545 }
4546 return text;
4547}
4548
4549/*
4550 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4551 */
4552 static void
4553concat_esc(gap, text, what)
4554 garray_T *gap;
4555 char_u *text;
4556 int what;
4557{
4558 while (*text != NUL)
4559 {
4560#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004561 int l = (*mb_ptr2len)(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (l > 1)
4563 {
4564 while (--l >= 0)
4565 ga_append(gap, *text++);
4566 continue;
4567 }
4568#endif
4569 if (*text == what)
4570 ga_append(gap, '\\');
4571 ga_append(gap, *text);
4572 ++text;
4573 }
4574}
4575
4576/*
4577 * Handle the press of a button in the find-replace dialog.
4578 * Return TRUE when something was added to the input buffer.
4579 */
4580 int
4581gui_do_findrepl(flags, find_text, repl_text, down)
4582 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4583 char_u *find_text;
4584 char_u *repl_text;
4585 int down; /* Search downwards. */
4586{
4587 garray_T ga;
4588 int i;
4589 int type = (flags & FRD_TYPE_MASK);
4590 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004591 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004592 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004595 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 ga_concat(&ga, (char_u *)"%s/");
4597
4598 ga_concat(&ga, (char_u *)"\\V");
4599 if (flags & FRD_MATCH_CASE)
4600 ga_concat(&ga, (char_u *)"\\C");
4601 else
4602 ga_concat(&ga, (char_u *)"\\c");
4603 if (flags & FRD_WHOLE_WORD)
4604 ga_concat(&ga, (char_u *)"\\<");
4605 if (type == FRD_REPLACEALL || down)
4606 concat_esc(&ga, find_text, '/'); /* escape slashes */
4607 else
4608 concat_esc(&ga, find_text, '?'); /* escape '?' */
4609 if (flags & FRD_WHOLE_WORD)
4610 ga_concat(&ga, (char_u *)"\\>");
4611
4612 if (type == FRD_REPLACEALL)
4613 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 ga_concat(&ga, (char_u *)"/");
4615 concat_esc(&ga, repl_text, '/'); /* escape slashes */
4616 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004617 }
4618 ga_append(&ga, NUL);
4619
4620 if (type == FRD_REPLACE)
4621 {
4622 /* Do the replacement when the text at the cursor matches. Thus no
4623 * replacement is done if the cursor was moved! */
4624 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
4625 regmatch.rm_ic = 0;
4626 if (regmatch.regprog != NULL)
4627 {
4628 p = ml_get_cursor();
4629 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
4630 && regmatch.startp[0] == p)
4631 {
4632 /* Clear the command line to remove any old "No match"
4633 * error. */
4634 msg_end_prompt();
4635
4636 if (u_save_cursor() == OK)
4637 {
4638 /* A button was pressed thus undo should be synced. */
4639 if (no_u_sync == 0)
4640 u_sync();
4641
4642 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00004643 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004644 ins_str(repl_text);
4645 }
4646 }
4647 else
4648 MSG(_("No match at cursor, finding next"));
4649 vim_free(regmatch.regprog);
4650 }
4651 }
4652
4653 if (type == FRD_REPLACEALL)
4654 {
4655 /* A button was pressed, thus undo should be synced. */
4656 if (no_u_sync == 0)
4657 u_sync();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 do_cmdline_cmd(ga.ga_data);
4659 }
4660 else
4661 {
4662 /* Search for the next match. */
4663 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
4665 SEARCH_MSG + SEARCH_MARK);
4666 msg_scroll = i; /* don't let an error message set msg_scroll */
4667 }
4668
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004669 /* Don't want to pass did_emsg to other code, it may cause disabling
4670 * syntax HL if we were busy redrawing. */
4671 did_emsg = save_did_emsg;
4672
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (State & (NORMAL | INSERT))
4674 {
4675 gui_update_screen(); /* update the screen */
4676 msg_didout = 0; /* overwrite any message */
4677 need_wait_return = FALSE; /* don't wait for return */
4678 }
4679
4680 vim_free(ga.ga_data);
4681 return (ga.ga_len > 0);
4682}
4683
4684#endif
4685
4686#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
4687 || defined(FEAT_GUI_MSWIN) \
4688 || defined(FEAT_GUI_MAC) \
4689 || defined(PROTO)
4690
4691#ifdef FEAT_WINDOWS
4692static void gui_wingoto_xy __ARGS((int x, int y));
4693
4694/*
4695 * Jump to the window at specified point (x, y).
4696 */
4697 static void
4698gui_wingoto_xy(x, y)
4699 int x;
4700 int y;
4701{
4702 int row = Y_2_ROW(y);
4703 int col = X_2_COL(x);
4704 win_T *wp;
4705
4706 if (row >= 0 && col >= 0)
4707 {
4708 wp = mouse_find_win(&row, &col);
4709 if (wp != NULL && wp != curwin)
4710 win_goto(wp);
4711 }
4712}
4713#endif
4714
4715/*
4716 * Process file drop. Mouse cursor position, key modifiers, name of files
4717 * and count of files are given. Argument "fnames[count]" has full pathnames
4718 * of dropped files, they will be freed in this function, and caller can't use
4719 * fnames after call this function.
4720 */
4721/*ARGSUSED*/
4722 void
4723gui_handle_drop(x, y, modifiers, fnames, count)
4724 int x;
4725 int y;
4726 int_u modifiers;
4727 char_u **fnames;
4728 int count;
4729{
4730 int i;
4731 char_u *p;
4732
4733 /*
4734 * When the cursor is at the command line, add the file names to the
4735 * command line, don't edit the files.
4736 */
4737 if (State & CMDLINE)
4738 {
4739 shorten_filenames(fnames, count);
4740 for (i = 0; i < count; ++i)
4741 {
4742 if (fnames[i] != NULL)
4743 {
4744 if (i > 0)
4745 add_to_input_buf((char_u*)" ", 1);
4746
4747 /* We don't know what command is used thus we can't be sure
4748 * about which characters need to be escaped. Only escape the
4749 * most common ones. */
4750# ifdef BACKSLASH_IN_FILENAME
4751 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
4752# else
4753 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
4754# endif
4755 if (p != NULL)
4756 add_to_input_buf(p, (int)STRLEN(p));
4757 vim_free(p);
4758 vim_free(fnames[i]);
4759 }
4760 }
4761 vim_free(fnames);
4762 }
4763 else
4764 {
4765 /* Go to the window under mouse cursor, then shorten given "fnames" by
4766 * current window, because a window can have local current dir. */
4767# ifdef FEAT_WINDOWS
4768 gui_wingoto_xy(x, y);
4769# endif
4770 shorten_filenames(fnames, count);
4771
4772 /* If Shift held down, remember the first item. */
4773 if ((modifiers & MOUSE_SHIFT) != 0)
4774 p = vim_strsave(fnames[0]);
4775 else
4776 p = NULL;
4777
4778 /* Handle the drop, :edit or :split to get to the file. This also
4779 * frees fnames[]. Skip this if there is only one item it's a
4780 * directory and Shift is held down. */
4781 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
4782 && mch_isdir(fnames[0]))
4783 {
4784 vim_free(fnames[0]);
4785 vim_free(fnames);
4786 }
4787 else
4788 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
4789
4790 /* If Shift held down, change to first file's directory. If the first
4791 * item is a directory, change to that directory (and let the explorer
4792 * plugin show the contents). */
4793 if (p != NULL)
4794 {
4795 if (mch_isdir(p))
4796 {
4797 if (mch_chdir((char *)p) == 0)
4798 shorten_fnames(TRUE);
4799 }
4800 else if (vim_chdirfile(p) == OK)
4801 shorten_fnames(TRUE);
4802 vim_free(p);
4803 }
4804
4805 /* Update the screen display */
4806 update_screen(NOT_VALID);
4807# ifdef FEAT_MENU
4808 gui_update_menus(0);
4809# endif
4810 setcursor();
4811 out_flush();
4812 gui_update_cursor(FALSE, FALSE);
4813 gui_mch_flush();
4814 }
4815}
4816#endif