blob: 4255607a72262d3758bbea599e64ca095503924f [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
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static 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));
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020023#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +000024static 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));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30static int gui_has_tabline __ARGS((void));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33static colnr_T scroll_line_len __ARGS((linenr_T lnum));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020034static linenr_T gui_find_longest_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static void gui_update_horiz_scrollbar __ARGS((int));
Bram Moolenaar3918c952005-03-15 22:34:55 +000036static void gui_set_fg_color __ARGS((char_u *name));
37static void gui_set_bg_color __ARGS((char_u *name));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static win_T *xy2win __ARGS((int x, int y));
39
40static int can_update_cursor = TRUE; /* can display the cursor */
41
42/*
43 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
44 * this makes the thumb indicate the part of the text that is shown. Motif
45 * can't do this.
46 */
47#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
48# define SCROLL_PAST_END
49#endif
50
51/*
52 * gui_start -- Called when user wants to start the GUI.
53 *
54 * Careful: This function can be called recursively when there is a ":gui"
55 * command in the .gvimrc file. Only the first call should fork, not the
56 * recursive call.
57 */
58 void
59gui_start()
60{
61 char_u *old_term;
62#if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X)
63# define MAY_FORK
64 int dofork = TRUE;
65#endif
66 static int recursive = 0;
67
68 old_term = vim_strsave(T_NAME);
69
70 /*
71 * Set_termname() will call gui_init() to start the GUI.
72 * Set the "starting" flag, to indicate that the GUI will start.
73 *
74 * We don't want to open the GUI shell until after we've read .gvimrc,
75 * otherwise we don't know what font we will use, and hence we don't know
76 * what size the shell should be. So if there are errors in the .gvimrc
77 * file, they will have to go to the terminal: Set full_screen to FALSE.
78 * full_screen will be set to TRUE again by a successful termcapinit().
79 */
80 settmode(TMODE_COOK); /* stop RAW mode */
81 if (full_screen)
82 cursor_on(); /* needed for ":gui" in .vimrc */
83 gui.starting = TRUE;
84 full_screen = FALSE;
85
86#ifdef MAY_FORK
87 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
88 dofork = FALSE;
89#endif
90 ++recursive;
91
92 termcapinit((char_u *)"builtin_gui");
93 gui.starting = recursive - 1;
94
95 if (!gui.in_use) /* failed to start GUI */
96 {
97 termcapinit(old_term); /* back to old term settings */
98 settmode(TMODE_RAW); /* restart RAW mode */
99#ifdef FEAT_TITLE
100 set_title_defaults(); /* set 'title' and 'icon' again */
101#endif
102 }
103
104 vim_free(old_term);
105
Bram Moolenaar9372a112005-12-06 19:59:18 +0000106#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200108 {
109# ifdef FEAT_EVAL
110 Window x11_window;
111 Display *x11_display;
112
113 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
114 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
115# endif
116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 /* Display error messages in a dialog now. */
118 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121
122#if defined(MAY_FORK) && !defined(__QNXNTO__)
123 /*
124 * Quit the current process and continue in the child.
125 * Makes "gvim file" disconnect from the shell it was started in.
126 * Don't do this when Vim was started with "-f" or the 'f' flag is present
127 * in 'guioptions'.
128 */
129 if (gui.in_use && dofork)
130 {
131 int pipefd[2]; /* pipe between parent and child */
132 int pipe_error;
133 char dummy;
134 pid_t pid = -1;
135
136 /* Setup a pipe between the child and the parent, so that the parent
137 * knows when the child has done the setsid() call and is allowed to
138 * exit. */
139 pipe_error = (pipe(pipefd) < 0);
140 pid = fork();
141 if (pid > 0) /* Parent */
142 {
143 /* Give the child some time to do the setsid(), otherwise the
144 * exit() may kill the child too (when starting gvim from inside a
145 * gvim). */
146 if (pipe_error)
147 ui_delay(300L, TRUE);
148 else
149 {
150 /* The read returns when the child closes the pipe (or when
151 * the child dies for some reason). */
152 close(pipefd[1]);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000153 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 close(pipefd[0]);
155 }
156
157 /* When swapping screens we may need to go to the next line, e.g.,
158 * after a hit-enter prompt and using ":gui". */
159 if (newline_on_exit)
160 mch_errmsg("\r\n");
161
162 /*
163 * The parent must skip the normal exit() processing, the child
164 * will do it. For example, GTK messes up signals when exiting.
165 */
166 _exit(0);
167 }
168
169# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
170 /*
171 * Change our process group. On some systems/shells a CTRL-C in the
172 * shell where Vim was started would otherwise kill gvim!
173 */
174 if (pid == 0) /* child */
175# if defined(HAVE_SETSID)
176 (void)setsid();
177# else
178 (void)setpgid(0, 0);
179# endif
180# endif
181 if (!pipe_error)
182 {
183 close(pipefd[0]);
184 close(pipefd[1]);
185 }
186
187# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
188 /* Tell the session manager our new PID */
189 gui_mch_forked();
190# endif
191 }
192#else
193# if defined(__QNXNTO__)
194 if (gui.in_use && dofork)
195 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
196 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
197# endif
198#endif
199
200#ifdef FEAT_AUTOCMD
Bram Moolenaar265e5072006-08-29 16:13:22 +0000201 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
202 * the GUIFailed event. */
Bram Moolenaar97cfe902010-08-06 20:42:30 +0200203 gui_mch_update();
Bram Moolenaar265e5072006-08-29 16:13:22 +0000204 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
205 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#endif
207
208 --recursive;
209}
210
211/*
212 * Call this when vim starts up, whether or not the GUI is started
213 */
214 void
215gui_prepare(argc, argv)
216 int *argc;
217 char **argv;
218{
219 gui.in_use = FALSE; /* No GUI yet (maybe later) */
220 gui.starting = FALSE; /* No GUI yet (maybe later) */
221 gui_mch_prepare(argc, argv);
222}
223
224/*
225 * Try initializing the GUI and check if it can be started.
226 * Used from main() to check early if "vim -g" can start the GUI.
227 * Used from gui_init() to prepare for starting the GUI.
228 * Returns FAIL or OK.
229 */
230 int
231gui_init_check()
232{
233 static int result = MAYBE;
234
235 if (result != MAYBE)
236 {
237 if (result == FAIL)
238 EMSG(_("E229: Cannot start the GUI"));
239 return result;
240 }
241
242 gui.shell_created = FALSE;
243 gui.dying = FALSE;
244 gui.in_focus = TRUE; /* so the guicursor setting works */
245 gui.dragged_sb = SBAR_NONE;
246 gui.dragged_wp = NULL;
247 gui.pointer_hidden = FALSE;
248 gui.col = 0;
249 gui.row = 0;
250 gui.num_cols = Columns;
251 gui.num_rows = Rows;
252
253 gui.cursor_is_valid = FALSE;
254 gui.scroll_region_top = 0;
255 gui.scroll_region_bot = Rows - 1;
256 gui.scroll_region_left = 0;
257 gui.scroll_region_right = Columns - 1;
258 gui.highlight_mask = HL_NORMAL;
259 gui.char_width = 1;
260 gui.char_height = 1;
261 gui.char_ascent = 0;
262 gui.border_width = 0;
263
264 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200265#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 gui.bold_font = NOFONT;
267 gui.ital_font = NOFONT;
268 gui.boldital_font = NOFONT;
269# ifdef FEAT_XFONTSET
270 gui.fontset = NOFONTSET;
271# endif
272#endif
273
274#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200275# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276# ifdef FONTSET_ALWAYS
277 gui.menu_fontset = NOFONTSET;
278# else
279 gui.menu_font = NOFONT;
280# endif
281# endif
282 gui.menu_is_active = TRUE; /* default: include menu */
283# ifndef FEAT_GUI_GTK
284 gui.menu_height = MENU_DEFAULT_HEIGHT;
285 gui.menu_width = 0;
286# endif
287#endif
288#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
289 gui.toolbar_height = 0;
290#endif
291#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
292 gui.footer_height = 0;
293#endif
294#ifdef FEAT_BEVAL_TIP
295 gui.tooltip_fontset = NOFONTSET;
296#endif
297
298 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
299 gui.prev_wrap = -1;
300
301#ifdef ALWAYS_USE_GUI
302 result = OK;
303#else
304 result = gui_mch_init_check();
305#endif
306 return result;
307}
308
309/*
310 * This is the call which starts the GUI.
311 */
312 void
313gui_init()
314{
315 win_T *wp;
316 static int recursive = 0;
317
318 /*
319 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
320 * function will then be executed at the first call, the rest by the
321 * recursive call. This allow the shell to be opened halfway reading a
322 * gvimrc file.
323 */
324 if (!recursive)
325 {
326 ++recursive;
327
328 clip_init(TRUE);
329
330 /* If can't initialize, don't try doing the rest */
331 if (gui_init_check() == FAIL)
332 {
333 --recursive;
334 clip_init(FALSE);
335 return;
336 }
337
338 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000339 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
340 * breaks the Paste toolbar button.
341 */
342 set_option_value((char_u *)"paste", 0L, NULL, 0);
343
344 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * Set up system-wide default menus.
346 */
347#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
348 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
349 {
350 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000351 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 sys_menu = FALSE;
353 }
354#endif
355
356 /*
357 * Switch on the mouse by default, unless the user changed it already.
358 * This can then be changed in the .gvimrc.
359 */
360 if (!option_was_set((char_u *)"mouse"))
361 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000362 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364 /*
365 * If -U option given, use only the initializations from that file and
366 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
367 */
368 if (use_gvimrc != NULL)
369 {
370 if (STRCMP(use_gvimrc, "NONE") != 0
371 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000372 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
374 }
375 else
376 {
377 /*
378 * Get system wide defaults for gvim, only when file name defined.
379 */
380#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000381 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#endif
383
384 /*
385 * Try to read GUI initialization commands from the following
386 * places:
387 * - environment variable GVIMINIT
388 * - the user gvimrc file (~/.gvimrc)
389 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
390 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
391 * The first that exists is used, the rest is ignored.
392 */
393 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
395 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
398 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#endif
400 )
401 {
402#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#endif
405 }
406
407 /*
408 * Read initialization commands from ".gvimrc" in current
409 * directory. This is only done if the 'exrc' option is set.
410 * Because of security reasons we disallow shell and write
411 * commands now, except for unix if the file is owned by the user
412 * or 'secure' option has been reset in environment of global
413 * ".gvimrc".
414 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
415 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
416 */
417 if (p_exrc)
418 {
419#ifdef UNIX
420 {
421 struct stat s;
422
423 /* if ".gvimrc" file is not owned by user, set 'secure'
424 * mode */
425 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
426 secure = p_secure;
427 }
428#else
429 secure = p_secure;
430#endif
431
432 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
433 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
434#ifdef SYS_GVIMRC_FILE
435 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
436 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
437#endif
438#ifdef USR_GVIMRC_FILE2
439 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
440 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
441#endif
442#ifdef USR_GVIMRC_FILE3
443 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
444 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
445#endif
446 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000447 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449 if (secure == 2)
450 need_wait_return = TRUE;
451 secure = 0;
452 }
453 }
454
455 if (need_wait_return || msg_didany)
456 wait_return(TRUE);
457
458 --recursive;
459 }
460
461 /* If recursive call opened the shell, return here from the first call */
462 if (gui.in_use)
463 return;
464
465 /*
466 * Create the GUI shell.
467 */
468 gui.in_use = TRUE; /* Must be set after menus have been set up */
469 if (gui_mch_init() == FAIL)
470 goto error;
471
472 /* Avoid a delay for an error message that was printed in the terminal
473 * where Vim was started. */
474 emsg_on_display = FALSE;
475 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000476 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 need_wait_return = FALSE;
478 msg_didany = FALSE;
479
480 /*
481 * Check validity of any generic resources that may have been loaded.
482 */
483 if (gui.border_width < 0)
484 gui.border_width = 0;
485
486 /*
487 * Set up the fonts. First use a font specified with "-fn" or "-font".
488 */
489 if (font_argument != NULL)
490 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
491 if (
492#ifdef FEAT_XFONTSET
493 (*p_guifontset == NUL
494 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
495#endif
496 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
497 : p_guifont, FALSE) == FAIL)
498 {
499 EMSG(_("E665: Cannot start GUI, no valid font found"));
500 goto error2;
501 }
502#ifdef FEAT_MBYTE
503 if (gui_get_wide_font() == FAIL)
504 EMSG(_("E231: 'guifontwide' invalid"));
505#endif
506
507 gui.num_cols = Columns;
508 gui.num_rows = Rows;
509 gui_reset_scroll_region();
510
511 /* Create initial scrollbars */
512 FOR_ALL_WINDOWS(wp)
513 {
514 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
515 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
516 }
517 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
518
519#ifdef FEAT_MENU
520 gui_create_initial_menus(root_menu);
521#endif
522#ifdef FEAT_SUN_WORKSHOP
523 if (usingSunWorkShop)
524 workshop_init();
525#endif
526#ifdef FEAT_SIGN_ICONS
527 sign_gui_started();
528#endif
529
530 /* Configure the desired menu and scrollbars */
531 gui_init_which_components(NULL);
532
533 /* All components of the GUI have been created now */
534 gui.shell_created = TRUE;
535
536#ifndef FEAT_GUI_GTK
537 /* Set the shell size, adjusted for the screen size. For GTK this only
538 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000539 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540#endif
541#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
542 /* Need to set the size of the menubar after all the menus have been
543 * created. */
544 gui_mch_compute_menu_height((Widget)0);
545#endif
546
547 /*
548 * Actually open the GUI shell.
549 */
550 if (gui_mch_open() != FAIL)
551 {
552#ifdef FEAT_TITLE
553 maketitle();
554 resettitle();
555#endif
556 init_gui_options();
557#ifdef FEAT_ARABIC
558 /* Our GUI can't do bidi. */
559 p_tbidi = FALSE;
560#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000561#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 /* Give GTK+ a chance to put all widget's into place. */
563 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000564
565# ifdef FEAT_MENU
566 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
567 * It was still there to make F10 work. */
568 if (vim_strchr(p_go, GO_MENUS) == NULL)
569 {
570 --gui.starting;
571 gui_mch_enable_menu(FALSE);
572 ++gui.starting;
573 gui_mch_update();
574 }
575# endif
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000578 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000580 /* When 'lines' was set while starting up the topframe may have to be
581 * resized. */
582 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000583
584#ifdef FEAT_BEVAL
585 /* Always create the Balloon Evaluation area, but disable it when
586 * 'ballooneval' is off */
587# ifdef FEAT_GUI_GTK
588 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
589 &general_beval_cb, NULL);
590# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000591# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000592 {
593 extern Widget textArea;
594 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000595 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000596 }
597# else
598# ifdef FEAT_GUI_W32
599 balloonEval = gui_mch_create_beval_area(NULL, NULL,
600 &general_beval_cb, NULL);
601# endif
602# endif
603# endif
604 if (!p_beval)
605 gui_mch_disable_beval_area(balloonEval);
606#endif
607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
609 if (!im_xim_isvalid_imactivate())
610 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
611#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000612 /* When 'cmdheight' was set during startup it may not have taken
613 * effect yet. */
614 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000615 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 return;
618 }
619
620error2:
621#ifdef FEAT_GUI_X11
622 /* undo gui_mch_init() */
623 gui_mch_uninit();
624#endif
625
626error:
627 gui.in_use = FALSE;
628 clip_init(FALSE);
629}
630
631
632 void
633gui_exit(rc)
634 int rc;
635{
636#ifndef __BEOS__
637 /* don't free the fonts, it leads to a BUS error
638 * richard@whitequeen.com Jul 99 */
639 free_highlight_fonts();
640#endif
641 gui.in_use = FALSE;
642 gui_mch_exit(rc);
643}
644
Bram Moolenaar9372a112005-12-06 19:59:18 +0000645#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000647# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648/*
649 * Called when the GUI shell is closed by the user. If there are no changed
650 * files Vim exits, otherwise there will be a dialog to ask the user what to
651 * do.
652 * When this function returns, Vim should NOT exit!
653 */
654 void
655gui_shell_closed()
656{
657 cmdmod_T save_cmdmod;
658
659 save_cmdmod = cmdmod;
660
661 /* Only exit when there are no changed files */
662 exiting = TRUE;
663# ifdef FEAT_BROWSE
664 cmdmod.browse = TRUE;
665# endif
666# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
667 cmdmod.confirm = TRUE;
668# endif
669 /* If there are changed buffers, present the user with a dialog if
670 * possible, otherwise give an error message. */
671 if (!check_changed_any(FALSE))
672 getout(0);
673
674 exiting = FALSE;
675 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000676 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678#endif
679
680/*
681 * Set the font. "font_list" is a a comma separated list of font names. The
682 * first font name that works is used. If none is found, use the default
683 * font.
684 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
685 * Return OK when able to set the font. When it failed FAIL is returned and
686 * the fonts are unchanged.
687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int
689gui_init_font(font_list, fontset)
690 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000691 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693#define FONTLEN 320
694 char_u font_name[FONTLEN];
695 int font_list_empty = FALSE;
696 int ret = FAIL;
697
698 if (!gui.in_use)
699 return FAIL;
700
701 font_name[0] = NUL;
702 if (*font_list == NUL)
703 font_list_empty = TRUE;
704 else
705 {
706#ifdef FEAT_XFONTSET
707 /* When using a fontset, the whole list of fonts is one name. */
708 if (fontset)
709 ret = gui_mch_init_font(font_list, TRUE);
710 else
711#endif
712 while (*font_list != NUL)
713 {
714 /* Isolate one comma separated font name. */
715 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
716
717 /* Careful!!! The Win32 version of gui_mch_init_font(), when
718 * called with "*" will change p_guifont to the selected font
719 * name, which frees the old value. This makes font_list
720 * invalid. Thus when OK is returned here, font_list must no
721 * longer be used! */
722 if (gui_mch_init_font(font_name, FALSE) == OK)
723 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200724#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 /* If it's a Unicode font, try setting 'guifontwide' to a
726 * similar double-width font. */
727 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
728 && strstr((char *)font_name, "10646") != NULL)
729 set_guifontwide(font_name);
730#endif
731 ret = OK;
732 break;
733 }
734 }
735 }
736
737 if (ret != OK
738 && STRCMP(font_list, "*") != 0
739 && (font_list_empty || gui.norm_font == NOFONT))
740 {
741 /*
742 * Couldn't load any font in 'font_list', keep the current font if
743 * there is one. If 'font_list' is empty, or if there is no current
744 * font, tell gui_mch_init_font() to try to find a font we can load.
745 */
746 ret = gui_mch_init_font(NULL, FALSE);
747 }
748
749 if (ret == OK)
750 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200751#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /* Set normal font as current font */
753# ifdef FEAT_XFONTSET
754 if (gui.fontset != NOFONTSET)
755 gui_mch_set_fontset(gui.fontset);
756 else
757# endif
758 gui_mch_set_font(gui.norm_font);
759#endif
760 gui_set_shellsize(FALSE,
761#ifdef MSWIN
762 TRUE
763#else
764 FALSE
765#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000766 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768
769 return ret;
770}
771
772#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200773# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774/*
775 * Try setting 'guifontwide' to a font twice as wide as "name".
776 */
777 static void
778set_guifontwide(name)
779 char_u *name;
780{
781 int i = 0;
782 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
783 char_u *wp = NULL;
784 char_u *p;
785 GuiFont font;
786
787 wp = wide_name;
788 for (p = name; *p != NUL; ++p)
789 {
790 *wp++ = *p;
791 if (*p == '-')
792 {
793 ++i;
794 if (i == 6) /* font type: change "--" to "-*-" */
795 {
796 if (p[1] == '-')
797 *wp++ = '*';
798 }
799 else if (i == 12) /* found the width */
800 {
801 ++p;
802 i = getdigits(&p);
803 if (i != 0)
804 {
805 /* Double the width specification. */
806 sprintf((char *)wp, "%d%s", i * 2, p);
807 font = gui_mch_get_font(wide_name, FALSE);
808 if (font != NOFONT)
809 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000810 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 gui.wide_font = font;
812 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000813 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
815 }
816 break;
817 }
818 }
819 }
820}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200821# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823/*
824 * Get the font for 'guifontwide'.
825 * Return FAIL for an invalid font name.
826 */
827 int
828gui_get_wide_font()
829{
830 GuiFont font = NOFONT;
831 char_u font_name[FONTLEN];
832 char_u *p;
833
834 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
835 return OK; /* Will give an error message later. */
836
837 if (p_guifontwide != NULL && *p_guifontwide != NUL)
838 {
839 for (p = p_guifontwide; *p != NUL; )
840 {
841 /* Isolate one comma separated font name. */
842 (void)copy_option_part(&p, font_name, FONTLEN, ",");
843 font = gui_mch_get_font(font_name, FALSE);
844 if (font != NOFONT)
845 break;
846 }
847 if (font == NOFONT)
848 return FAIL;
849 }
850
851 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200852#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
854 if (font != NOFONT && gui.norm_font != NOFONT
855 && pango_font_description_equal(font, gui.norm_font))
856 {
857 gui.wide_font = NOFONT;
858 gui_mch_free_font(font);
859 }
860 else
861#endif
862 gui.wide_font = font;
863 return OK;
864}
865#endif
866
867 void
868gui_set_cursor(row, col)
869 int row;
870 int col;
871{
872 gui.row = row;
873 gui.col = col;
874}
875
876/*
877 * gui_check_pos - check if the cursor is on the screen.
878 */
879 static void
880gui_check_pos()
881{
882 if (gui.row >= screen_Rows)
883 gui.row = screen_Rows - 1;
884 if (gui.col >= screen_Columns)
885 gui.col = screen_Columns - 1;
886 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
887 gui.cursor_is_valid = FALSE;
888}
889
890/*
891 * Redraw the cursor if necessary or when forced.
892 * Careful: The contents of ScreenLines[] must match what is on the screen,
893 * otherwise this goes wrong. May need to call out_flush() first.
894 */
895 void
896gui_update_cursor(force, clear_selection)
897 int force; /* when TRUE, update even when not moved */
898 int clear_selection;/* clear selection under cursor */
899{
900 int cur_width = 0;
901 int cur_height = 0;
902 int old_hl_mask;
903 int idx;
904 int id;
905 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
906 int cattr; /* cursor attributes */
907 int attr;
908 attrentry_T *aep = NULL;
909
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +0000910 /* Don't update the cursor when halfway busy scrolling or the screen size
911 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
912 if (!can_update_cursor || screen_Columns != gui.num_cols
913 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 return;
915
916 gui_check_pos();
917 if (!gui.cursor_is_valid || force
918 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
919 {
920 gui_undraw_cursor();
921 if (gui.row < 0)
922 return;
923#ifdef USE_IM_CONTROL
924 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
925 im_set_position(gui.row, gui.col);
926#endif
927 gui.cursor_row = gui.row;
928 gui.cursor_col = gui.col;
929
930 /* Only write to the screen after ScreenLines[] has been initialized */
931 if (!screen_cleared || ScreenLines == NULL)
932 return;
933
934 /* Clear the selection if we are about to write over it */
935 if (clear_selection)
936 clip_may_clear_selection(gui.row, gui.row);
937 /* Check that the cursor is inside the shell (resizing may have made
938 * it invalid) */
939 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
940 return;
941
942 gui.cursor_is_valid = TRUE;
943
944 /*
945 * How the cursor is drawn depends on the current mode.
946 */
947 idx = get_shape_idx(FALSE);
948 if (State & LANGMAP)
949 id = shape_table[idx].id_lm;
950 else
951 id = shape_table[idx].id;
952
953 /* get the colors and attributes for the cursor. Default is inverted */
954 cfg = INVALCOLOR;
955 cbg = INVALCOLOR;
956 cattr = HL_INVERSE;
957 gui_mch_set_blinking(shape_table[idx].blinkwait,
958 shape_table[idx].blinkon,
959 shape_table[idx].blinkoff);
960 if (id > 0)
961 {
962 cattr = syn_id2colors(id, &cfg, &cbg);
963#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
964 {
965 static int iid;
966 guicolor_T fg, bg;
967
Bram Moolenaarc236c162008-07-13 17:41:49 +0000968 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200969# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000970 preedit_get_status()
971# else
972 im_get_status()
973# endif
974 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
976 iid = syn_name2id((char_u *)"CursorIM");
977 if (iid > 0)
978 {
979 syn_id2colors(iid, &fg, &bg);
980 if (bg != INVALCOLOR)
981 cbg = bg;
982 if (fg != INVALCOLOR)
983 cfg = fg;
984 }
985 }
986 }
987#endif
988 }
989
990 /*
991 * Get the attributes for the character under the cursor.
992 * When no cursor color was given, use the character color.
993 */
994 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
995 if (attr > HL_ALL)
996 aep = syn_gui_attr2entry(attr);
997 if (aep != NULL)
998 {
999 attr = aep->ae_attr;
1000 if (cfg == INVALCOLOR)
1001 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1002 : aep->ae_u.gui.fg_color);
1003 if (cbg == INVALCOLOR)
1004 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1005 : aep->ae_u.gui.bg_color);
1006 }
1007 if (cfg == INVALCOLOR)
1008 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1009 if (cbg == INVALCOLOR)
1010 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1011
1012#ifdef FEAT_XIM
1013 if (aep != NULL)
1014 {
1015 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1016 : aep->ae_u.gui.bg_color);
1017 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1018 : aep->ae_u.gui.fg_color);
1019 if (xim_bg_color == INVALCOLOR)
1020 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1021 : gui.back_pixel;
1022 if (xim_fg_color == INVALCOLOR)
1023 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1024 : gui.norm_pixel;
1025 }
1026 else
1027 {
1028 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1029 : gui.back_pixel;
1030 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1031 : gui.norm_pixel;
1032 }
1033#endif
1034
1035 attr &= ~HL_INVERSE;
1036 if (cattr & HL_INVERSE)
1037 {
1038 cc = cbg;
1039 cbg = cfg;
1040 cfg = cc;
1041 }
1042 cattr &= ~HL_INVERSE;
1043
1044 /*
1045 * When we don't have window focus, draw a hollow cursor.
1046 */
1047 if (!gui.in_focus)
1048 {
1049 gui_mch_draw_hollow_cursor(cbg);
1050 return;
1051 }
1052
1053 old_hl_mask = gui.highlight_mask;
1054 if (shape_table[idx].shape == SHAPE_BLOCK
1055#ifdef FEAT_HANGULIN
1056 || composing_hangul
1057#endif
1058 )
1059 {
1060 /*
1061 * Draw the text character with the cursor colors. Use the
1062 * character attributes plus the cursor attributes.
1063 */
1064 gui.highlight_mask = (cattr | attr);
1065#ifdef FEAT_HANGULIN
1066 if (composing_hangul)
1067 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1068 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1069 else
1070#endif
1071 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1072 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1073 }
1074 else
1075 {
1076#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1077 int col_off = FALSE;
1078#endif
1079 /*
1080 * First draw the partial cursor, then overwrite with the text
1081 * character, using a transparent background.
1082 */
1083 if (shape_table[idx].shape == SHAPE_VER)
1084 {
1085 cur_height = gui.char_height;
1086 cur_width = (gui.char_width * shape_table[idx].percentage
1087 + 99) / 100;
1088 }
1089 else
1090 {
1091 cur_height = (gui.char_height * shape_table[idx].percentage
1092 + 99) / 100;
1093 cur_width = gui.char_width;
1094 }
1095#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001096 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1097 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
1099 /* Double wide character. */
1100 if (shape_table[idx].shape != SHAPE_VER)
1101 cur_width += gui.char_width;
1102# ifdef FEAT_RIGHTLEFT
1103 if (CURSOR_BAR_RIGHT)
1104 {
1105 /* gui.col points to the left halve of the character but
1106 * the vertical line needs to be on the right halve.
1107 * A double-wide horizontal line is also drawn from the
1108 * right halve in gui_mch_draw_part_cursor(). */
1109 col_off = TRUE;
1110 ++gui.col;
1111 }
1112# endif
1113 }
1114#endif
1115 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1116#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1117 if (col_off)
1118 --gui.col;
1119#endif
1120
1121#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1122 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1123 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1124 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1125 (guicolor_T)0, (guicolor_T)0, 0);
1126#endif
1127 }
1128 gui.highlight_mask = old_hl_mask;
1129 }
1130}
1131
1132#if defined(FEAT_MENU) || defined(PROTO)
1133 void
1134gui_position_menu()
1135{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001136# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (gui.menu_is_active && gui.in_use)
1138 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1139# endif
1140}
1141#endif
1142
1143/*
1144 * Position the various GUI components (text area, menu). The vertical
1145 * scrollbars are NOT handled here. See gui_update_scrollbars().
1146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 static void
1148gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001149 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150{
1151 int text_area_x;
1152 int text_area_y;
1153 int text_area_width;
1154 int text_area_height;
1155
1156 /* avoid that moving components around generates events */
1157 ++hold_gui_events;
1158
1159 text_area_x = 0;
1160 if (gui.which_scrollbars[SBAR_LEFT])
1161 text_area_x += gui.scrollbar_width;
1162
1163 text_area_y = 0;
1164#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1165 gui.menu_width = total_width;
1166 if (gui.menu_is_active)
1167 text_area_y += gui.menu_height;
1168#endif
1169#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1170 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1171 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1172#endif
1173
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001174# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001175 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001176 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001177 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001178#endif
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1181 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1182 {
1183# ifdef FEAT_GUI_ATHENA
1184 gui_mch_set_toolbar_pos(0, text_area_y,
1185 gui.menu_width, gui.toolbar_height);
1186# endif
1187 text_area_y += gui.toolbar_height;
1188 }
1189#endif
1190
1191 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1192 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1193
1194 gui_mch_set_text_area_pos(text_area_x,
1195 text_area_y,
1196 text_area_width,
1197 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001198#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 + xim_get_status_area_height()
1200#endif
1201 );
1202#ifdef FEAT_MENU
1203 gui_position_menu();
1204#endif
1205 if (gui.which_scrollbars[SBAR_BOTTOM])
1206 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1207 text_area_x,
1208 text_area_y + text_area_height,
1209 text_area_width,
1210 gui.scrollbar_height);
1211 gui.left_sbar_x = 0;
1212 gui.right_sbar_x = text_area_x + text_area_width;
1213
1214 --hold_gui_events;
1215}
1216
Bram Moolenaar02743632005-07-25 20:42:36 +00001217/*
1218 * Get the width of the widgets and decorations to the side of the text area.
1219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int
1221gui_get_base_width()
1222{
1223 int base_width;
1224
1225 base_width = 2 * gui.border_offset;
1226 if (gui.which_scrollbars[SBAR_LEFT])
1227 base_width += gui.scrollbar_width;
1228 if (gui.which_scrollbars[SBAR_RIGHT])
1229 base_width += gui.scrollbar_width;
1230 return base_width;
1231}
1232
Bram Moolenaar02743632005-07-25 20:42:36 +00001233/*
1234 * Get the height of the widgets and decorations above and below the text area.
1235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 int
1237gui_get_base_height()
1238{
1239 int base_height;
1240
1241 base_height = 2 * gui.border_offset;
1242 if (gui.which_scrollbars[SBAR_BOTTOM])
1243 base_height += gui.scrollbar_height;
1244#ifdef FEAT_GUI_GTK
1245 /* We can't take the sizes properly into account until anything is
1246 * realized. Therefore we recalculate all the values here just before
1247 * setting the size. (--mdcki) */
1248#else
1249# ifdef FEAT_MENU
1250 if (gui.menu_is_active)
1251 base_height += gui.menu_height;
1252# endif
1253# ifdef FEAT_TOOLBAR
1254 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1255# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1256 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1257# else
1258 base_height += gui.toolbar_height;
1259# endif
1260# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001261# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1262 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001263 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001264 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266# ifdef FEAT_FOOTER
1267 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1268 base_height += gui.footer_height;
1269# endif
1270# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1271 base_height += gui_mch_text_area_extra_height();
1272# endif
1273#endif
1274 return base_height;
1275}
1276
1277/*
1278 * Should be called after the GUI shell has been resized. Its arguments are
1279 * the new width and height of the shell in pixels.
1280 */
1281 void
1282gui_resize_shell(pixel_width, pixel_height)
1283 int pixel_width;
1284 int pixel_height;
1285{
1286 static int busy = FALSE;
1287
1288 if (!gui.shell_created) /* ignore when still initializing */
1289 return;
1290
1291 /*
1292 * Can't resize the screen while it is being redrawn. Remember the new
1293 * size and handle it later.
1294 */
1295 if (updating_screen || busy)
1296 {
1297 new_pixel_width = pixel_width;
1298 new_pixel_height = pixel_height;
1299 return;
1300 }
1301
1302again:
1303 busy = TRUE;
1304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 /* Flush pending output before redrawing */
1306 out_flush();
1307
1308 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001309 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
1311 gui_position_components(pixel_width);
1312
1313 gui_reset_scroll_region();
1314 /*
1315 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1316 * at the last line here (why does it have to be one row too low?).
1317 */
1318 if (State == ASKMORE || State == CONFIRM)
1319 gui.row = gui.num_rows;
1320
1321 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1322 * the safe side. */
1323 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1324 || gui.num_rows != Rows || gui.num_cols != Columns)
1325 shell_resized();
1326
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 gui_update_scrollbars(TRUE);
1328 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001329#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 xim_set_status_area();
1331#endif
1332
1333 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001334
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 /*
1336 * We could have been called again while redrawing the screen.
1337 * Need to do it all again with the latest size then.
1338 */
1339 if (new_pixel_height)
1340 {
1341 pixel_width = new_pixel_width;
1342 pixel_height = new_pixel_height;
1343 new_pixel_width = 0;
1344 new_pixel_height = 0;
1345 goto again;
1346 }
1347}
1348
1349/*
1350 * Check if gui_resize_shell() must be called.
1351 */
1352 void
1353gui_may_resize_shell()
1354{
1355 int h, w;
1356
1357 if (new_pixel_height)
1358 {
1359 /* careful: gui_resize_shell() may postpone the resize again if we
1360 * were called indirectly by it */
1361 w = new_pixel_width;
1362 h = new_pixel_height;
1363 new_pixel_width = 0;
1364 new_pixel_height = 0;
1365 gui_resize_shell(w, h);
1366 }
1367}
1368
1369 int
1370gui_get_shellsize()
1371{
1372 Rows = gui.num_rows;
1373 Columns = gui.num_cols;
1374 return OK;
1375}
1376
1377/*
1378 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001379 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1380 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001383gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001384 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001386 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 int base_width;
1389 int base_height;
1390 int width;
1391 int height;
1392 int min_width;
1393 int min_height;
1394 int screen_w;
1395 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001396#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001397 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001398 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001399#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001400 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
1402 if (!gui.shell_created)
1403 return;
1404
1405#ifdef MSWIN
1406 /* If not setting to a user specified size and maximized, calculate the
1407 * number of characters that fit in the maximized window. */
1408 if (!mustset && gui_mch_maximized())
1409 {
1410 gui_mch_newfont();
1411 return;
1412 }
1413#endif
1414
1415 base_width = gui_get_base_width();
1416 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001417 if (fit_to_display)
1418 /* Remember the original window position. */
1419 gui_mch_get_winpos(&x, &y);
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421#ifdef USE_SUN_WORKSHOP
1422 if (!mustset && usingSunWorkShop
1423 && workshop_get_width_height(&width, &height))
1424 {
1425 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1426 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1427 }
1428 else
1429#endif
1430 {
1431 width = Columns * gui.char_width + base_width;
1432 height = Rows * gui.char_height + base_height;
1433 }
1434
1435 if (fit_to_display)
1436 {
1437 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001438 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 {
1440 Columns = (screen_w - base_width) / gui.char_width;
1441 if (Columns < MIN_COLUMNS)
1442 Columns = MIN_COLUMNS;
1443 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001444#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001445 ++did_adjust;
1446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001448 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 {
1450 Rows = (screen_h - base_height) / gui.char_height;
1451 check_shellsize();
1452 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001453#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001454 ++did_adjust;
1455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001457#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001458 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1459 && height + gui.char_height >= screen_h))
1460 /* don't unmaximize if at maximum size */
1461 un_maximize = FALSE;
1462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464 gui.num_cols = Columns;
1465 gui.num_rows = Rows;
1466
1467 min_width = base_width + MIN_COLUMNS * gui.char_width;
1468 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001469#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001470 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001471#endif
1472
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001473#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001474 if (un_maximize)
1475 {
1476 /* If the window size is smaller than the screen unmaximize the
1477 * window, otherwise resizing won't work. */
1478 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1479 if ((width + gui.char_width < screen_w
1480 || height + gui.char_height * 2 < screen_h)
1481 && gui_mch_maximized())
1482 gui_mch_unmaximize();
1483 }
1484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001487 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001489 if (fit_to_display && x >= 0 && y >= 0)
1490 {
1491 /* Some window managers put the Vim window left of/above the screen.
1492 * Only change the position if it wasn't already negative before
1493 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 gui_mch_update();
1495 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1496 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1497 }
1498
1499 gui_position_components(width);
1500 gui_update_scrollbars(TRUE);
1501 gui_reset_scroll_region();
1502}
1503
1504/*
1505 * Called when Rows and/or Columns has changed.
1506 */
1507 void
1508gui_new_shellsize()
1509{
1510 gui_reset_scroll_region();
1511}
1512
1513/*
1514 * Make scroll region cover whole screen.
1515 */
1516 void
1517gui_reset_scroll_region()
1518{
1519 gui.scroll_region_top = 0;
1520 gui.scroll_region_bot = gui.num_rows - 1;
1521 gui.scroll_region_left = 0;
1522 gui.scroll_region_right = gui.num_cols - 1;
1523}
1524
1525 void
1526gui_start_highlight(mask)
1527 int mask;
1528{
1529 if (mask > HL_ALL) /* highlight code */
1530 gui.highlight_mask = mask;
1531 else /* mask */
1532 gui.highlight_mask |= mask;
1533}
1534
1535 void
1536gui_stop_highlight(mask)
1537 int mask;
1538{
1539 if (mask > HL_ALL) /* highlight code */
1540 gui.highlight_mask = HL_NORMAL;
1541 else /* mask */
1542 gui.highlight_mask &= ~mask;
1543}
1544
1545/*
1546 * Clear a rectangular region of the screen from text pos (row1, col1) to
1547 * (row2, col2) inclusive.
1548 */
1549 void
1550gui_clear_block(row1, col1, row2, col2)
1551 int row1;
1552 int col1;
1553 int row2;
1554 int col2;
1555{
1556 /* Clear the selection if we are about to write over it */
1557 clip_may_clear_selection(row1, row2);
1558
1559 gui_mch_clear_block(row1, col1, row2, col2);
1560
1561 /* Invalidate cursor if it was in this block */
1562 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1563 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1564 gui.cursor_is_valid = FALSE;
1565}
1566
1567/*
1568 * Write code to update the cursor later. This avoids the need to flush the
1569 * output buffer before calling gui_update_cursor().
1570 */
1571 void
1572gui_update_cursor_later()
1573{
1574 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1575}
1576
1577 void
1578gui_write(s, len)
1579 char_u *s;
1580 int len;
1581{
1582 char_u *p;
1583 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 int force_scrollbar = FALSE;
1586 static win_T *old_curwin = NULL;
1587
1588/* #define DEBUG_GUI_WRITE */
1589#ifdef DEBUG_GUI_WRITE
1590 {
1591 int i;
1592 char_u *str;
1593
1594 printf("gui_write(%d):\n ", len);
1595 for (i = 0; i < len; i++)
1596 if (s[i] == ESC)
1597 {
1598 if (i != 0)
1599 printf("\n ");
1600 printf("<ESC>");
1601 }
1602 else
1603 {
1604 str = transchar_byte(s[i]);
1605 if (str[0] && str[1])
1606 printf("<%s>", (char *)str);
1607 else
1608 printf("%s", (char *)str);
1609 }
1610 printf("\n");
1611 }
1612#endif
1613 while (len)
1614 {
1615 if (s[0] == ESC && s[1] == '|')
1616 {
1617 p = s + 2;
1618 if (VIM_ISDIGIT(*p))
1619 {
1620 arg1 = getdigits(&p);
1621 if (p > s + len)
1622 break;
1623 if (*p == ';')
1624 {
1625 ++p;
1626 arg2 = getdigits(&p);
1627 if (p > s + len)
1628 break;
1629 }
1630 }
1631 switch (*p)
1632 {
1633 case 'C': /* Clear screen */
1634 clip_scroll_selection(9999);
1635 gui_mch_clear_all();
1636 gui.cursor_is_valid = FALSE;
1637 force_scrollbar = TRUE;
1638 break;
1639 case 'M': /* Move cursor */
1640 gui_set_cursor(arg1, arg2);
1641 break;
1642 case 's': /* force cursor (shape) update */
1643 force_cursor = TRUE;
1644 break;
1645 case 'R': /* Set scroll region */
1646 if (arg1 < arg2)
1647 {
1648 gui.scroll_region_top = arg1;
1649 gui.scroll_region_bot = arg2;
1650 }
1651 else
1652 {
1653 gui.scroll_region_top = arg2;
1654 gui.scroll_region_bot = arg1;
1655 }
1656 break;
1657#ifdef FEAT_VERTSPLIT
1658 case 'V': /* Set vertical scroll region */
1659 if (arg1 < arg2)
1660 {
1661 gui.scroll_region_left = arg1;
1662 gui.scroll_region_right = arg2;
1663 }
1664 else
1665 {
1666 gui.scroll_region_left = arg2;
1667 gui.scroll_region_right = arg1;
1668 }
1669 break;
1670#endif
1671 case 'd': /* Delete line */
1672 gui_delete_lines(gui.row, 1);
1673 break;
1674 case 'D': /* Delete lines */
1675 gui_delete_lines(gui.row, arg1);
1676 break;
1677 case 'i': /* Insert line */
1678 gui_insert_lines(gui.row, 1);
1679 break;
1680 case 'I': /* Insert lines */
1681 gui_insert_lines(gui.row, arg1);
1682 break;
1683 case '$': /* Clear to end-of-line */
1684 gui_clear_block(gui.row, gui.col, gui.row,
1685 (int)Columns - 1);
1686 break;
1687 case 'h': /* Turn on highlighting */
1688 gui_start_highlight(arg1);
1689 break;
1690 case 'H': /* Turn off highlighting */
1691 gui_stop_highlight(arg1);
1692 break;
1693 case 'f': /* flash the window (visual bell) */
1694 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1695 break;
1696 default:
1697 p = s + 1; /* Skip the ESC */
1698 break;
1699 }
1700 len -= (int)(++p - s);
1701 s = p;
1702 }
1703 else if (
1704#ifdef EBCDIC
1705 CtrlChar(s[0]) != 0 /* Ctrl character */
1706#else
1707 s[0] < 0x20 /* Ctrl character */
1708#endif
1709#ifdef FEAT_SIGN_ICONS
1710 && s[0] != SIGN_BYTE
1711# ifdef FEAT_NETBEANS_INTG
1712 && s[0] != MULTISIGN_BYTE
1713# endif
1714#endif
1715 )
1716 {
1717 if (s[0] == '\n') /* NL */
1718 {
1719 gui.col = 0;
1720 if (gui.row < gui.scroll_region_bot)
1721 gui.row++;
1722 else
1723 gui_delete_lines(gui.scroll_region_top, 1);
1724 }
1725 else if (s[0] == '\r') /* CR */
1726 {
1727 gui.col = 0;
1728 }
1729 else if (s[0] == '\b') /* Backspace */
1730 {
1731 if (gui.col)
1732 --gui.col;
1733 }
1734 else if (s[0] == Ctrl_L) /* cursor-right */
1735 {
1736 ++gui.col;
1737 }
1738 else if (s[0] == Ctrl_G) /* Beep */
1739 {
1740 gui_mch_beep();
1741 }
1742 /* Other Ctrl character: shouldn't happen! */
1743
1744 --len; /* Skip this char */
1745 ++s;
1746 }
1747 else
1748 {
1749 p = s;
1750 while (len > 0 && (
1751#ifdef EBCDIC
1752 CtrlChar(*p) == 0
1753#else
1754 *p >= 0x20
1755#endif
1756#ifdef FEAT_SIGN_ICONS
1757 || *p == SIGN_BYTE
1758# ifdef FEAT_NETBEANS_INTG
1759 || *p == MULTISIGN_BYTE
1760# endif
1761#endif
1762 ))
1763 {
1764 len--;
1765 p++;
1766 }
1767 gui_outstr(s, (int)(p - s));
1768 s = p;
1769 }
1770 }
1771
1772 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1773 * set). */
1774 if (force_cursor)
1775 gui_update_cursor(TRUE, TRUE);
1776
1777 /* When switching to another window the dragging must have stopped.
1778 * Required for GTK, dragged_sb isn't reset. */
1779 if (old_curwin != curwin)
1780 gui.dragged_sb = SBAR_NONE;
1781
1782 /* Update the scrollbars after clearing the screen or when switched
1783 * to another window.
1784 * Update the horizontal scrollbar always, it's difficult to check all
1785 * situations where it might change. */
1786 if (force_scrollbar || old_curwin != curwin)
1787 gui_update_scrollbars(force_scrollbar);
1788 else
1789 gui_update_horiz_scrollbar(FALSE);
1790 old_curwin = curwin;
1791
1792 /*
1793 * We need to make sure this is cleared since Athena doesn't tell us when
1794 * he is done dragging. Do the same for GTK.
1795 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001796#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 gui.dragged_sb = SBAR_NONE;
1798#endif
1799
1800 gui_mch_flush(); /* In case vim decides to take a nap */
1801}
1802
1803/*
1804 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1805 * produces wrong results. Call gui_dont_update_cursor() before that code and
1806 * gui_can_update_cursor() afterwards.
1807 */
1808 void
1809gui_dont_update_cursor()
1810{
1811 if (gui.in_use)
1812 {
1813 /* Undraw the cursor now, we probably can't do it after the change. */
1814 gui_undraw_cursor();
1815 can_update_cursor = FALSE;
1816 }
1817}
1818
1819 void
1820gui_can_update_cursor()
1821{
1822 can_update_cursor = TRUE;
1823 /* No need to update the cursor right now, there is always more output
1824 * after scrolling. */
1825}
1826
1827 static void
1828gui_outstr(s, len)
1829 char_u *s;
1830 int len;
1831{
1832 int this_len;
1833#ifdef FEAT_MBYTE
1834 int cells;
1835#endif
1836
1837 if (len == 0)
1838 return;
1839
1840 if (len < 0)
1841 len = (int)STRLEN(s);
1842
1843 while (len > 0)
1844 {
1845#ifdef FEAT_MBYTE
1846 if (has_mbyte)
1847 {
1848 /* Find out how many chars fit in the current line. */
1849 cells = 0;
1850 for (this_len = 0; this_len < len; )
1851 {
1852 cells += (*mb_ptr2cells)(s + this_len);
1853 if (gui.col + cells > Columns)
1854 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001855 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
1857 if (this_len > len)
1858 this_len = len; /* don't include following composing char */
1859 }
1860 else
1861#endif
1862 if (gui.col + len > Columns)
1863 this_len = Columns - gui.col;
1864 else
1865 this_len = len;
1866
1867 (void)gui_outstr_nowrap(s, this_len,
1868 0, (guicolor_T)0, (guicolor_T)0, 0);
1869 s += this_len;
1870 len -= this_len;
1871#ifdef FEAT_MBYTE
1872 /* fill up for a double-width char that doesn't fit. */
1873 if (len > 0 && gui.col < Columns)
1874 (void)gui_outstr_nowrap((char_u *)" ", 1,
1875 0, (guicolor_T)0, (guicolor_T)0, 0);
1876#endif
1877 /* The cursor may wrap to the next line. */
1878 if (gui.col >= Columns)
1879 {
1880 gui.col = 0;
1881 gui.row++;
1882 }
1883 }
1884}
1885
1886/*
1887 * Output one character (may be one or two display cells).
1888 * Caller must check for valid "off".
1889 * Returns FAIL or OK, just like gui_outstr_nowrap().
1890 */
1891 static int
1892gui_screenchar(off, flags, fg, bg, back)
1893 int off; /* Offset from start of screen */
1894 int flags;
1895 guicolor_T fg, bg; /* colors for cursor */
1896 int back; /* backup this many chars when using bold trick */
1897{
1898#ifdef FEAT_MBYTE
1899 char_u buf[MB_MAXBYTES + 1];
1900
1901 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1902 if (enc_utf8 && ScreenLines[off] == 0)
1903 return OK;
1904
1905 if (enc_utf8 && ScreenLinesUC[off] != 0)
1906 /* Draw UTF-8 multi-byte character. */
1907 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1908 flags, fg, bg, back);
1909
1910 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1911 {
1912 buf[0] = ScreenLines[off];
1913 buf[1] = ScreenLines2[off];
1914 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1915 }
1916
1917 /* Draw non-multi-byte character or DBCS character. */
1918 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001919 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 flags, fg, bg, back);
1921#else
1922 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1923#endif
1924}
1925
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001926#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927/*
1928 * Output the string at the given screen position. This is used in place
1929 * of gui_screenchar() where possible because Pango needs as much context
1930 * as possible to work nicely. It's a lot faster as well.
1931 */
1932 static int
1933gui_screenstr(off, len, flags, fg, bg, back)
1934 int off; /* Offset from start of screen */
1935 int len; /* string length in screen cells */
1936 int flags;
1937 guicolor_T fg, bg; /* colors for cursor */
1938 int back; /* backup this many chars when using bold trick */
1939{
1940 char_u *buf;
1941 int outlen = 0;
1942 int i;
1943 int retval;
1944
1945 if (len <= 0) /* "cannot happen"? */
1946 return OK;
1947
1948 if (enc_utf8)
1949 {
1950 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1951 if (buf == NULL)
1952 return OK; /* not much we could do here... */
1953
1954 for (i = off; i < off + len; ++i)
1955 {
1956 if (ScreenLines[i] == 0)
1957 continue; /* skip second half of double-width char */
1958
1959 if (ScreenLinesUC[i] == 0)
1960 buf[outlen++] = ScreenLines[i];
1961 else
1962 outlen += utfc_char2bytes(i, buf + outlen);
1963 }
1964
1965 buf[outlen] = NUL; /* only to aid debugging */
1966 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1967 vim_free(buf);
1968
1969 return retval;
1970 }
1971 else if (enc_dbcs == DBCS_JPNU)
1972 {
1973 buf = alloc((unsigned)(len * 2 + 1));
1974 if (buf == NULL)
1975 return OK; /* not much we could do here... */
1976
1977 for (i = off; i < off + len; ++i)
1978 {
1979 buf[outlen++] = ScreenLines[i];
1980
1981 /* handle double-byte single-width char */
1982 if (ScreenLines[i] == 0x8e)
1983 buf[outlen++] = ScreenLines2[i];
1984 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1985 buf[outlen++] = ScreenLines[++i];
1986 }
1987
1988 buf[outlen] = NUL; /* only to aid debugging */
1989 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1990 vim_free(buf);
1991
1992 return retval;
1993 }
1994 else
1995 {
1996 return gui_outstr_nowrap(&ScreenLines[off], len,
1997 flags, fg, bg, back);
1998 }
1999}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002000#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002/*
2003 * Output the given string at the current cursor position. If the string is
2004 * too long to fit on the line, then it is truncated.
2005 * "flags":
2006 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2007 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002008 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 * background.
2010 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2011 * it.
2012 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2013 * FAIL (the caller should start drawing "back" chars back).
2014 */
2015 int
2016gui_outstr_nowrap(s, len, flags, fg, bg, back)
2017 char_u *s;
2018 int len;
2019 int flags;
2020 guicolor_T fg, bg; /* colors for cursor */
2021 int back; /* backup this many chars when using bold trick */
2022{
2023 long_u highlight_mask;
2024 long_u hl_mask_todo;
2025 guicolor_T fg_color;
2026 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002027 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002028#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 GuiFont font = NOFONT;
2030# ifdef FEAT_XFONTSET
2031 GuiFontset fontset = NOFONTSET;
2032# endif
2033#endif
2034 attrentry_T *aep = NULL;
2035 int draw_flags;
2036 int col = gui.col;
2037#ifdef FEAT_SIGN_ICONS
2038 int draw_sign = FALSE;
2039# ifdef FEAT_NETBEANS_INTG
2040 int multi_sign = FALSE;
2041# endif
2042#endif
2043
2044 if (len < 0)
2045 len = (int)STRLEN(s);
2046 if (len == 0)
2047 return OK;
2048
2049#ifdef FEAT_SIGN_ICONS
2050 if (*s == SIGN_BYTE
2051# ifdef FEAT_NETBEANS_INTG
2052 || *s == MULTISIGN_BYTE
2053# endif
2054 )
2055 {
2056# ifdef FEAT_NETBEANS_INTG
2057 if (*s == MULTISIGN_BYTE)
2058 multi_sign = TRUE;
2059# endif
2060 /* draw spaces instead */
2061 s = (char_u *)" ";
2062 if (len == 1 && col > 0)
2063 --col;
2064 len = 2;
2065 draw_sign = TRUE;
2066 highlight_mask = 0;
2067 }
2068 else
2069#endif
2070 if (gui.highlight_mask > HL_ALL)
2071 {
2072 aep = syn_gui_attr2entry(gui.highlight_mask);
2073 if (aep == NULL) /* highlighting not set */
2074 highlight_mask = 0;
2075 else
2076 highlight_mask = aep->ae_attr;
2077 }
2078 else
2079 highlight_mask = gui.highlight_mask;
2080 hl_mask_todo = highlight_mask;
2081
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002082#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 /* Set the font */
2084 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2085 font = aep->ae_u.gui.font;
2086# ifdef FEAT_XFONTSET
2087 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2088 fontset = aep->ae_u.gui.fontset;
2089# endif
2090 else
2091 {
2092# ifdef FEAT_XFONTSET
2093 if (gui.fontset != NOFONTSET)
2094 fontset = gui.fontset;
2095 else
2096# endif
2097 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2098 {
2099 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2100 {
2101 font = gui.boldital_font;
2102 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2103 }
2104 else if (gui.bold_font != NOFONT)
2105 {
2106 font = gui.bold_font;
2107 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2108 }
2109 else
2110 font = gui.norm_font;
2111 }
2112 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2113 {
2114 font = gui.ital_font;
2115 hl_mask_todo &= ~HL_ITALIC;
2116 }
2117 else
2118 font = gui.norm_font;
2119 }
2120# ifdef FEAT_XFONTSET
2121 if (fontset != NOFONTSET)
2122 gui_mch_set_fontset(fontset);
2123 else
2124# endif
2125 gui_mch_set_font(font);
2126#endif
2127
2128 draw_flags = 0;
2129
2130 /* Set the color */
2131 bg_color = gui.back_pixel;
2132 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2133 {
2134 draw_flags |= DRAW_CURSOR;
2135 fg_color = fg;
2136 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002137 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
2139 else if (aep != NULL)
2140 {
2141 fg_color = aep->ae_u.gui.fg_color;
2142 if (fg_color == INVALCOLOR)
2143 fg_color = gui.norm_pixel;
2144 bg_color = aep->ae_u.gui.bg_color;
2145 if (bg_color == INVALCOLOR)
2146 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002147 sp_color = aep->ae_u.gui.sp_color;
2148 if (sp_color == INVALCOLOR)
2149 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002154 sp_color = fg_color;
2155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2158 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002159#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 gui_mch_set_colors(bg_color, fg_color);
2161#else
2162 gui_mch_set_fg_color(bg_color);
2163 gui_mch_set_bg_color(fg_color);
2164#endif
2165 }
2166 else
2167 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002168#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 gui_mch_set_colors(fg_color, bg_color);
2170#else
2171 gui_mch_set_fg_color(fg_color);
2172 gui_mch_set_bg_color(bg_color);
2173#endif
2174 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002175 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 /* Clear the selection if we are about to write over it */
2178 if (!(flags & GUI_MON_NOCLEAR))
2179 clip_may_clear_selection(gui.row, gui.row);
2180
2181
2182#ifndef MSWIN16_FASTTEXT
2183 /* If there's no bold font, then fake it */
2184 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2185 draw_flags |= DRAW_BOLD;
2186#endif
2187
2188 /*
2189 * When drawing bold or italic characters the spill-over from the left
2190 * neighbor may be destroyed. Let the caller backup to start redrawing
2191 * just after a blank.
2192 */
2193 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2194 return FAIL;
2195
Bram Moolenaare60acc12011-05-10 16:41:25 +02002196#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 /* If there's no italic font, then fake it.
2198 * For GTK2, we don't need a different font for italic style. */
2199 if (hl_mask_todo & HL_ITALIC)
2200 draw_flags |= DRAW_ITALIC;
2201
2202 /* Do we underline the text? */
2203 if (hl_mask_todo & HL_UNDERLINE)
2204 draw_flags |= DRAW_UNDERL;
2205#else
2206 /* Do we underline the text? */
2207 if ((hl_mask_todo & HL_UNDERLINE)
2208# ifndef MSWIN16_FASTTEXT
2209 || (hl_mask_todo & HL_ITALIC)
2210# endif
2211 )
2212 draw_flags |= DRAW_UNDERL;
2213#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002214 /* Do we undercurl the text? */
2215 if (hl_mask_todo & HL_UNDERCURL)
2216 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002218 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (flags & GUI_MON_TRS_CURSOR)
2220 draw_flags |= DRAW_TRANSP;
2221
2222 /*
2223 * Draw the text.
2224 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002225#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 /* The value returned is the length in display cells */
2227 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2228#else
2229# ifdef FEAT_MBYTE
2230 if (enc_utf8)
2231 {
2232 int start; /* index of bytes to be drawn */
2233 int cells; /* cellwidth of bytes to be drawn */
2234 int thislen; /* length of bytes to be drawin */
2235 int cn; /* cellwidth of current char */
2236 int i; /* index of current char */
2237 int c; /* current char value */
2238 int cl; /* byte length of current char */
2239 int comping; /* current char is composing */
2240 int scol = col; /* screen column */
2241 int dowide; /* use 'guifontwide' */
2242
2243 /* Break the string at a composing character, it has to be drawn on
2244 * top of the previous character. */
2245 start = 0;
2246 cells = 0;
2247 for (i = 0; i < len; i += cl)
2248 {
2249 c = utf_ptr2char(s + i);
2250 cn = utf_char2cells(c);
2251 if (cn > 1
2252# ifdef FEAT_XFONTSET
2253 && fontset == NOFONTSET
2254# endif
2255 && gui.wide_font != NOFONT)
2256 dowide = TRUE;
2257 else
2258 dowide = FALSE;
2259 comping = utf_iscomposing(c);
2260 if (!comping) /* count cells from non-composing chars */
2261 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002262 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (cl == 0) /* hit end of string */
2264 len = i + cl; /* len must be wrong "cannot happen" */
2265
2266 /* print the string so far if it's the last character or there is
2267 * a composing character. */
2268 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002269# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 || (cn > 1
2271# ifdef FEAT_XFONTSET
2272 /* No fontset: At least draw char after wide char at
2273 * right position. */
2274 && fontset == NOFONTSET
2275# endif
2276 )
2277# endif
2278 )
2279 {
2280 if (comping || dowide)
2281 thislen = i - start;
2282 else
2283 thislen = i - start + cl;
2284 if (thislen > 0)
2285 {
2286 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2287 draw_flags);
2288 start += thislen;
2289 }
2290 scol += cells;
2291 cells = 0;
2292 if (dowide)
2293 {
2294 gui_mch_set_font(gui.wide_font);
2295 gui_mch_draw_string(gui.row, scol - cn,
2296 s + start, cl, draw_flags);
2297 gui_mch_set_font(font);
2298 start += cl;
2299 }
2300
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002301# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002302 /* No fontset: draw a space to fill the gap after a wide char
2303 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2305# ifdef FEAT_XFONTSET
2306 && fontset == NOFONTSET
2307# endif
2308 && !dowide)
2309 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2310 1, draw_flags);
2311# endif
2312 }
2313 /* Draw a composing char on top of the previous char. */
2314 if (comping)
2315 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002316# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002317 /* Carbon ATSUI autodraws composing char over previous char */
2318 gui_mch_draw_string(gui.row, scol, s + i, cl,
2319 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002320# else
2321 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2322 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 start = i + cl;
2325 }
2326 }
2327 /* The stuff below assumes "len" is the length in screen columns. */
2328 len = scol - col;
2329 }
2330 else
2331# endif
2332 {
2333 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2334# ifdef FEAT_MBYTE
2335 if (enc_dbcs == DBCS_JPNU)
2336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 /* Get the length in display cells, this can be different from the
2338 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002339 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341# endif
2342 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002343#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2346 gui.col = col + len;
2347
2348 /* May need to invert it when it's part of the selection. */
2349 if (flags & GUI_MON_NOCLEAR)
2350 clip_may_redraw_selection(gui.row, col, len);
2351
2352 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2353 {
2354 /* Invalidate the old physical cursor position if we wrote over it */
2355 if (gui.cursor_row == gui.row
2356 && gui.cursor_col >= col
2357 && gui.cursor_col < col + len)
2358 gui.cursor_is_valid = FALSE;
2359 }
2360
2361#ifdef FEAT_SIGN_ICONS
2362 if (draw_sign)
2363 /* Draw the sign on top of the spaces. */
2364 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002365# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02002366 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (multi_sign)
2368 netbeans_draw_multisign_indicator(gui.row);
2369# endif
2370#endif
2371
2372 return OK;
2373}
2374
2375/*
2376 * Un-draw the cursor. Actually this just redraws the character at the given
2377 * position. The character just before it too, for when it was in bold.
2378 */
2379 void
2380gui_undraw_cursor()
2381{
2382 if (gui.cursor_is_valid)
2383 {
2384#ifdef FEAT_HANGULIN
2385 if (composing_hangul
2386 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2387 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2388 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2389 gui.norm_pixel, gui.back_pixel, 0);
2390 else
2391 {
2392#endif
2393 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2394 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2395 && gui.cursor_col > 0)
2396 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2397 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2398#ifdef FEAT_HANGULIN
2399 if (composing_hangul)
2400 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2401 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2402 }
2403#endif
2404 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2405 * here in case it wasn't needed to undraw it. */
2406 gui.cursor_is_valid = FALSE;
2407 }
2408}
2409
2410 void
2411gui_redraw(x, y, w, h)
2412 int x;
2413 int y;
2414 int w;
2415 int h;
2416{
2417 int row1, col1, row2, col2;
2418
2419 row1 = Y_2_ROW(y);
2420 col1 = X_2_COL(x);
2421 row2 = Y_2_ROW(y + h - 1);
2422 col2 = X_2_COL(x + w - 1);
2423
2424 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2425
2426 /*
2427 * We may need to redraw the cursor, but don't take it upon us to change
2428 * its location after a scroll.
2429 * (maybe be more strict even and test col too?)
2430 * These things may be outside the update/clipping region and reality may
2431 * not reflect Vims internal ideas if these operations are clipped away.
2432 */
2433 if (gui.row == gui.cursor_row)
2434 gui_update_cursor(TRUE, TRUE);
2435}
2436
2437/*
2438 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2439 * from col1 to col2 (inclusive).
2440 * Return TRUE when the character before the first drawn character has
2441 * different attributes (may have to be redrawn too).
2442 */
2443 int
2444gui_redraw_block(row1, col1, row2, col2, flags)
2445 int row1;
2446 int col1;
2447 int row2;
2448 int col2;
2449 int flags; /* flags for gui_outstr_nowrap() */
2450{
2451 int old_row, old_col;
2452 long_u old_hl_mask;
2453 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002454 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 int idx, len;
2456 int back, nback;
2457 int retval = FALSE;
2458#ifdef FEAT_MBYTE
2459 int orig_col1, orig_col2;
2460#endif
2461
2462 /* Don't try to update when ScreenLines is not valid */
2463 if (!screen_cleared || ScreenLines == NULL)
2464 return retval;
2465
2466 /* Don't try to draw outside the shell! */
2467 /* Check everything, strange values may be caused by a big border width */
2468 col1 = check_col(col1);
2469 col2 = check_col(col2);
2470 row1 = check_row(row1);
2471 row2 = check_row(row2);
2472
2473 /* Remember where our cursor was */
2474 old_row = gui.row;
2475 old_col = gui.col;
2476 old_hl_mask = gui.highlight_mask;
2477#ifdef FEAT_MBYTE
2478 orig_col1 = col1;
2479 orig_col2 = col2;
2480#endif
2481
2482 for (gui.row = row1; gui.row <= row2; gui.row++)
2483 {
2484#ifdef FEAT_MBYTE
2485 /* When only half of a double-wide character is in the block, include
2486 * the other half. */
2487 col1 = orig_col1;
2488 col2 = orig_col2;
2489 off = LineOffset[gui.row];
2490 if (enc_dbcs != 0)
2491 {
2492 if (col1 > 0)
2493 col1 -= dbcs_screen_head_off(ScreenLines + off,
2494 ScreenLines + off + col1);
2495 col2 += dbcs_screen_tail_off(ScreenLines + off,
2496 ScreenLines + off + col2);
2497 }
2498 else if (enc_utf8)
2499 {
2500 if (ScreenLines[off + col1] == 0)
2501 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002502# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2504 ++col2;
2505# endif
2506 }
2507#endif
2508 gui.col = col1;
2509 off = LineOffset[gui.row] + gui.col;
2510 len = col2 - col1 + 1;
2511
2512 /* Find how many chars back this highlighting starts, or where a space
2513 * is. Needed for when the bold trick is used */
2514 for (back = 0; back < col1; ++back)
2515 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2516 || ScreenLines[off - 1 - back] == ' ')
2517 break;
2518 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2519 && ScreenLines[off - 1] != ' ');
2520
2521 /* Break it up in strings of characters with the same attributes. */
2522 /* Print UTF-8 characters individually. */
2523 while (len > 0)
2524 {
2525 first_attr = ScreenAttrs[off];
2526 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002527#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (enc_utf8 && ScreenLinesUC[off] != 0)
2529 {
2530 /* output multi-byte character separately */
2531 nback = gui_screenchar(off, flags,
2532 (guicolor_T)0, (guicolor_T)0, back);
2533 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2534 idx = 2;
2535 else
2536 idx = 1;
2537 }
2538 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2539 {
2540 /* output double-byte, single-width character separately */
2541 nback = gui_screenchar(off, flags,
2542 (guicolor_T)0, (guicolor_T)0, back);
2543 idx = 1;
2544 }
2545 else
2546#endif
2547 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002548#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 for (idx = 0; idx < len; ++idx)
2550 {
2551 if (enc_utf8 && ScreenLines[off + idx] == 0)
2552 continue; /* skip second half of double-width char */
2553 if (ScreenAttrs[off + idx] != first_attr)
2554 break;
2555 }
2556 /* gui_screenstr() takes care of multibyte chars */
2557 nback = gui_screenstr(off, idx, flags,
2558 (guicolor_T)0, (guicolor_T)0, back);
2559#else
2560 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2561 idx++)
2562 {
2563# ifdef FEAT_MBYTE
2564 /* Stop at a multi-byte Unicode character. */
2565 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2566 break;
2567 if (enc_dbcs == DBCS_JPNU)
2568 {
2569 /* Stop at a double-byte single-width char. */
2570 if (ScreenLines[off + idx] == 0x8e)
2571 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002572 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 + off + idx) == 2)
2574 ++idx; /* skip second byte of double-byte char */
2575 }
2576# endif
2577 }
2578 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2579 (guicolor_T)0, (guicolor_T)0, back);
2580#endif
2581 }
2582 if (nback == FAIL)
2583 {
2584 /* Must back up to start drawing where a bold or italic word
2585 * starts. */
2586 off -= back;
2587 len += back;
2588 gui.col -= back;
2589 }
2590 else
2591 {
2592 off += idx;
2593 len -= idx;
2594 }
2595 back = 0;
2596 }
2597 }
2598
2599 /* Put the cursor back where it was */
2600 gui.row = old_row;
2601 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002602 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604 return retval;
2605}
2606
2607 static void
2608gui_delete_lines(row, count)
2609 int row;
2610 int count;
2611{
2612 if (count <= 0)
2613 return;
2614
2615 if (row + count > gui.scroll_region_bot)
2616 /* Scrolled out of region, just blank the lines out */
2617 gui_clear_block(row, gui.scroll_region_left,
2618 gui.scroll_region_bot, gui.scroll_region_right);
2619 else
2620 {
2621 gui_mch_delete_lines(row, count);
2622
2623 /* If the cursor was in the deleted lines it's now gone. If the
2624 * cursor was in the scrolled lines adjust its position. */
2625 if (gui.cursor_row >= row
2626 && gui.cursor_col >= gui.scroll_region_left
2627 && gui.cursor_col <= gui.scroll_region_right)
2628 {
2629 if (gui.cursor_row < row + count)
2630 gui.cursor_is_valid = FALSE;
2631 else if (gui.cursor_row <= gui.scroll_region_bot)
2632 gui.cursor_row -= count;
2633 }
2634 }
2635}
2636
2637 static void
2638gui_insert_lines(row, count)
2639 int row;
2640 int count;
2641{
2642 if (count <= 0)
2643 return;
2644
2645 if (row + count > gui.scroll_region_bot)
2646 /* Scrolled out of region, just blank the lines out */
2647 gui_clear_block(row, gui.scroll_region_left,
2648 gui.scroll_region_bot, gui.scroll_region_right);
2649 else
2650 {
2651 gui_mch_insert_lines(row, count);
2652
2653 if (gui.cursor_row >= gui.row
2654 && gui.cursor_col >= gui.scroll_region_left
2655 && gui.cursor_col <= gui.scroll_region_right)
2656 {
2657 if (gui.cursor_row <= gui.scroll_region_bot - count)
2658 gui.cursor_row += count;
2659 else if (gui.cursor_row <= gui.scroll_region_bot)
2660 gui.cursor_is_valid = FALSE;
2661 }
2662 }
2663}
2664
2665/*
2666 * The main GUI input routine. Waits for a character from the keyboard.
2667 * wtime == -1 Wait forever.
2668 * wtime == 0 Don't wait.
2669 * wtime > 0 Wait wtime milliseconds for a character.
2670 * Returns OK if a character was found to be available within the given time,
2671 * or FAIL otherwise.
2672 */
2673 int
2674gui_wait_for_chars(wtime)
2675 long wtime;
2676{
2677 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002679#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /*
2681 * If we're going to wait a bit, update the menus and mouse shape for the
2682 * current State.
2683 */
2684 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 gui_update_menus(0);
2686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 gui_mch_update();
2689 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 /* Before waiting, flush any output to the screen. */
2695 gui_mch_flush();
2696
2697 if (wtime > 0)
2698 {
2699 /* Blink when waiting for a character. Probably only does something
2700 * for showmatch() */
2701 gui_mch_start_blink();
2702 retval = gui_mch_wait_for_chars(wtime);
2703 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return retval;
2705 }
2706
2707 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002708 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
2710 gui_mch_start_blink();
2711
Bram Moolenaar3918c952005-03-15 22:34:55 +00002712 retval = FAIL;
2713 /*
2714 * We may want to trigger the CursorHold event. First wait for
2715 * 'updatetime' and if nothing is typed within that time put the
2716 * K_CURSORHOLD key in the input buffer.
2717 */
2718 if (gui_mch_wait_for_chars(p_ut) == OK)
2719 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002721 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002723 char_u buf[3];
2724
2725 /* Put K_CURSORHOLD in the input buffer. */
2726 buf[0] = CSI;
2727 buf[1] = KS_EXTRA;
2728 buf[2] = (int)KE_CURSORHOLD;
2729 add_to_input_buf(buf, 3);
2730
2731 retval = OK;
2732 }
2733#endif
2734
2735 if (retval == FAIL)
2736 {
2737 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002738 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 gui_mch_stop_blink();
2743 return retval;
2744}
2745
2746/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002747 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 */
2749 static void
2750fill_mouse_coord(p, col, row)
2751 char_u *p;
2752 int col;
2753 int row;
2754{
2755 p[0] = (char_u)(col / 128 + ' ' + 1);
2756 p[1] = (char_u)(col % 128 + ' ' + 1);
2757 p[2] = (char_u)(row / 128 + ' ' + 1);
2758 p[3] = (char_u)(row % 128 + ' ' + 1);
2759}
2760
2761/*
2762 * Generic mouse support function. Add a mouse event to the input buffer with
2763 * the given properties.
2764 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2765 * MOUSE_X1, MOUSE_X2
2766 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002767 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2768 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * x, y --- Coordinates of mouse in pixels.
2770 * repeated_click --- TRUE if this click comes only a short time after a
2771 * previous click.
2772 * modifiers --- Bit field which may be any of the following modifiers
2773 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2774 * This function will ignore drag events where the mouse has not moved to a new
2775 * character.
2776 */
2777 void
2778gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2779 int button;
2780 int x;
2781 int y;
2782 int repeated_click;
2783 int_u modifiers;
2784{
2785 static int prev_row = 0, prev_col = 0;
2786 static int prev_button = -1;
2787 static int num_clicks = 1;
2788 char_u string[10];
2789 enum key_extra button_char;
2790 int row, col;
2791#ifdef FEAT_CLIPBOARD
2792 int checkfor;
2793 int did_clip = FALSE;
2794#endif
2795
2796 /*
2797 * Scrolling may happen at any time, also while a selection is present.
2798 */
2799 switch (button)
2800 {
2801 case MOUSE_X1:
2802 button_char = KE_X1MOUSE;
2803 goto button_set;
2804 case MOUSE_X2:
2805 button_char = KE_X2MOUSE;
2806 goto button_set;
2807 case MOUSE_4:
2808 button_char = KE_MOUSEDOWN;
2809 goto button_set;
2810 case MOUSE_5:
2811 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002812 goto button_set;
2813 case MOUSE_6:
2814 button_char = KE_MOUSELEFT;
2815 goto button_set;
2816 case MOUSE_7:
2817 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818button_set:
2819 {
2820 /* Don't put events in the input queue now. */
2821 if (hold_gui_events)
2822 return;
2823
2824 string[3] = CSI;
2825 string[4] = KS_EXTRA;
2826 string[5] = (int)button_char;
2827
2828 /* Pass the pointer coordinates of the scroll event so that we
2829 * know which window to scroll. */
2830 row = gui_xy2colrow(x, y, &col);
2831 string[6] = (char_u)(col / 128 + ' ' + 1);
2832 string[7] = (char_u)(col % 128 + ' ' + 1);
2833 string[8] = (char_u)(row / 128 + ' ' + 1);
2834 string[9] = (char_u)(row % 128 + ' ' + 1);
2835
2836 if (modifiers == 0)
2837 add_to_input_buf(string + 3, 7);
2838 else
2839 {
2840 string[0] = CSI;
2841 string[1] = KS_MODIFIER;
2842 string[2] = 0;
2843 if (modifiers & MOUSE_SHIFT)
2844 string[2] |= MOD_MASK_SHIFT;
2845 if (modifiers & MOUSE_CTRL)
2846 string[2] |= MOD_MASK_CTRL;
2847 if (modifiers & MOUSE_ALT)
2848 string[2] |= MOD_MASK_ALT;
2849 add_to_input_buf(string, 10);
2850 }
2851 return;
2852 }
2853 }
2854
2855#ifdef FEAT_CLIPBOARD
2856 /* If a clipboard selection is in progress, handle it */
2857 if (clip_star.state == SELECT_IN_PROGRESS)
2858 {
2859 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2860 return;
2861 }
2862
2863 /* Determine which mouse settings to look for based on the current mode */
2864 switch (get_real_state())
2865 {
2866 case NORMAL_BUSY:
2867 case OP_PENDING:
2868 case NORMAL: checkfor = MOUSE_NORMAL; break;
2869 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002870 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case REPLACE:
2872 case REPLACE+LANGMAP:
2873#ifdef FEAT_VREPLACE
2874 case VREPLACE:
2875 case VREPLACE+LANGMAP:
2876#endif
2877 case INSERT:
2878 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2879 case ASKMORE:
2880 case HITRETURN: /* At the more- and hit-enter prompt pass the
2881 mouse event for a click on or below the
2882 message line. */
2883 if (Y_2_ROW(y) >= msg_row)
2884 checkfor = MOUSE_NORMAL;
2885 else
2886 checkfor = MOUSE_RETURN;
2887 break;
2888
2889 /*
2890 * On the command line, use the clipboard selection on all lines
2891 * but the command line. But not when pasting.
2892 */
2893 case CMDLINE:
2894 case CMDLINE+LANGMAP:
2895 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2896 checkfor = MOUSE_NONE;
2897 else
2898 checkfor = MOUSE_COMMAND;
2899 break;
2900
2901 default:
2902 checkfor = MOUSE_NONE;
2903 break;
2904 };
2905
2906 /*
2907 * Allow clipboard selection of text on the command line in "normal"
2908 * modes. Don't do this when dragging the status line, or extending a
2909 * Visual selection.
2910 */
2911 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2912 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00002913# ifdef FEAT_WINDOWS
2914 + firstwin->w_winrow
2915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 && button != MOUSE_DRAG
2917# ifdef FEAT_MOUSESHAPE
2918 && !drag_status_line
2919# ifdef FEAT_VERTSPLIT
2920 && !drag_sep_line
2921# endif
2922# endif
2923 )
2924 checkfor = MOUSE_NONE;
2925
2926 /*
2927 * Use modeless selection when holding CTRL and SHIFT pressed.
2928 */
2929 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2930 checkfor = MOUSE_NONEF;
2931
2932 /*
2933 * In Ex mode, always use modeless selection.
2934 */
2935 if (exmode_active)
2936 checkfor = MOUSE_NONE;
2937
2938 /*
2939 * If the mouse settings say to not use the mouse, use the modeless
2940 * selection. But if Visual is active, assume that only the Visual area
2941 * will be selected.
2942 * Exception: On the command line, both the selection is used and a mouse
2943 * key is send.
2944 */
2945 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2946 {
2947#ifdef FEAT_VISUAL
2948 /* Don't do modeless selection in Visual mode. */
2949 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2950 return;
2951#endif
2952
2953 /*
2954 * When 'mousemodel' is "popup", shift-left is translated to right.
2955 * But not when also using Ctrl.
2956 */
2957 if (mouse_model_popup() && button == MOUSE_LEFT
2958 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2959 {
2960 button = MOUSE_RIGHT;
2961 modifiers &= ~ MOUSE_SHIFT;
2962 }
2963
2964 /* If the selection is done, allow the right button to extend it.
2965 * If the selection is cleared, allow the right button to start it
2966 * from the cursor position. */
2967 if (button == MOUSE_RIGHT)
2968 {
2969 if (clip_star.state == SELECT_CLEARED)
2970 {
2971 if (State & CMDLINE)
2972 {
2973 col = msg_col;
2974 row = msg_row;
2975 }
2976 else
2977 {
2978 col = curwin->w_wcol;
2979 row = curwin->w_wrow + W_WINROW(curwin);
2980 }
2981 clip_start_selection(col, row, FALSE);
2982 }
2983 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2984 repeated_click);
2985 did_clip = TRUE;
2986 }
2987 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02002988 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
2990 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2991 did_clip = TRUE;
2992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 /* Always allow pasting */
2995 if (button != MOUSE_MIDDLE)
2996 {
2997 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2998 return;
2999 if (checkfor != MOUSE_COMMAND)
3000 button = MOUSE_LEFT;
3001 }
3002 repeated_click = FALSE;
3003 }
3004
3005 if (clip_star.state != SELECT_CLEARED && !did_clip)
3006 clip_clear_selection();
3007#endif
3008
3009 /* Don't put events in the input queue now. */
3010 if (hold_gui_events)
3011 return;
3012
3013 row = gui_xy2colrow(x, y, &col);
3014
3015 /*
3016 * If we are dragging and the mouse hasn't moved far enough to be on a
3017 * different character, then don't send an event to vim.
3018 */
3019 if (button == MOUSE_DRAG)
3020 {
3021 if (row == prev_row && col == prev_col)
3022 return;
3023 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3024 if (y < 0)
3025 row = -1;
3026 }
3027
3028 /*
3029 * If topline has changed (window scrolled) since the last click, reset
3030 * repeated_click, because we don't want starting Visual mode when
3031 * clicking on a different character in the text.
3032 */
3033 if (curwin->w_topline != gui_prev_topline
3034#ifdef FEAT_DIFF
3035 || curwin->w_topfill != gui_prev_topfill
3036#endif
3037 )
3038 repeated_click = FALSE;
3039
3040 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3041 string[1] = KS_MOUSE;
3042 string[2] = KE_FILLER;
3043 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3044 {
3045 if (repeated_click)
3046 {
3047 /*
3048 * Handle multiple clicks. They only count if the mouse is still
3049 * pointing at the same character.
3050 */
3051 if (button != prev_button || row != prev_row || col != prev_col)
3052 num_clicks = 1;
3053 else if (++num_clicks > 4)
3054 num_clicks = 1;
3055 }
3056 else
3057 num_clicks = 1;
3058 prev_button = button;
3059 gui_prev_topline = curwin->w_topline;
3060#ifdef FEAT_DIFF
3061 gui_prev_topfill = curwin->w_topfill;
3062#endif
3063
3064 string[3] = (char_u)(button | 0x20);
3065 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3066 }
3067 else
3068 string[3] = (char_u)button;
3069
3070 string[3] |= modifiers;
3071 fill_mouse_coord(string + 4, col, row);
3072 add_to_input_buf(string, 8);
3073
3074 if (row < 0)
3075 prev_row = 0;
3076 else
3077 prev_row = row;
3078 prev_col = col;
3079
3080 /*
3081 * We need to make sure this is cleared since Athena doesn't tell us when
3082 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3083 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003084#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 gui.dragged_sb = SBAR_NONE;
3086#endif
3087}
3088
3089/*
3090 * Convert x and y coordinate to column and row in text window.
3091 * Corrects for multi-byte character.
3092 * returns column in "*colp" and row as return value;
3093 */
3094 int
3095gui_xy2colrow(x, y, colp)
3096 int x;
3097 int y;
3098 int *colp;
3099{
3100 int col = check_col(X_2_COL(x));
3101 int row = check_row(Y_2_ROW(y));
3102
3103#ifdef FEAT_MBYTE
3104 *colp = mb_fix_col(col, row);
3105#else
3106 *colp = col;
3107#endif
3108 return row;
3109}
3110
3111#if defined(FEAT_MENU) || defined(PROTO)
3112/*
3113 * Callback function for when a menu entry has been selected.
3114 */
3115 void
3116gui_menu_cb(menu)
3117 vimmenu_T *menu;
3118{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003119 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /* Don't put events in the input queue now. */
3122 if (hold_gui_events)
3123 return;
3124
3125 bytes[0] = CSI;
3126 bytes[1] = KS_MENU;
3127 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003128 add_to_input_buf(bytes, 3);
3129 add_long_to_buf((long_u)menu, bytes);
3130 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132#endif
3133
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003134static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136/*
3137 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003138 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 * in p_go.
3140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 void
3142gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003143 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#ifdef FEAT_MENU
3146 static int prev_menu_is_active = -1;
3147#endif
3148#ifdef FEAT_TOOLBAR
3149 static int prev_toolbar = -1;
3150 int using_toolbar = FALSE;
3151#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003152#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003153 int using_tabline;
3154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#ifdef FEAT_FOOTER
3156 static int prev_footer = -1;
3157 int using_footer = FALSE;
3158#endif
3159#if defined(FEAT_MENU) && !defined(WIN16)
3160 static int prev_tearoff = -1;
3161 int using_tearoff = FALSE;
3162#endif
3163
3164 char_u *p;
3165 int i;
3166#ifdef FEAT_MENU
3167 int grey_old, grey_new;
3168 char_u *temp;
3169#endif
3170 win_T *wp;
3171 int need_set_size;
3172 int fix_size;
3173
3174#ifdef FEAT_MENU
3175 if (oldval != NULL && gui.in_use)
3176 {
3177 /*
3178 * Check if the menu's go from grey to non-grey or vise versa.
3179 */
3180 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3181 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3182 if (grey_old != grey_new)
3183 {
3184 temp = p_go;
3185 p_go = oldval;
3186 gui_update_menus(MENU_ALL_MODES);
3187 p_go = temp;
3188 }
3189 }
3190 gui.menu_is_active = FALSE;
3191#endif
3192
3193 for (i = 0; i < 3; i++)
3194 gui.which_scrollbars[i] = FALSE;
3195 for (p = p_go; *p; p++)
3196 switch (*p)
3197 {
3198 case GO_LEFT:
3199 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3200 break;
3201 case GO_RIGHT:
3202 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3203 break;
3204#ifdef FEAT_VERTSPLIT
3205 case GO_VLEFT:
3206 if (win_hasvertsplit())
3207 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3208 break;
3209 case GO_VRIGHT:
3210 if (win_hasvertsplit())
3211 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3212 break;
3213#endif
3214 case GO_BOT:
3215 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3216 break;
3217#ifdef FEAT_MENU
3218 case GO_MENUS:
3219 gui.menu_is_active = TRUE;
3220 break;
3221#endif
3222 case GO_GREY:
3223 /* make menu's have grey items, ignored here */
3224 break;
3225#ifdef FEAT_TOOLBAR
3226 case GO_TOOLBAR:
3227 using_toolbar = TRUE;
3228 break;
3229#endif
3230#ifdef FEAT_FOOTER
3231 case GO_FOOTER:
3232 using_footer = TRUE;
3233 break;
3234#endif
3235 case GO_TEAROFF:
3236#if defined(FEAT_MENU) && !defined(WIN16)
3237 using_tearoff = TRUE;
3238#endif
3239 break;
3240 default:
3241 /* Ignore options that are not supported */
3242 break;
3243 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (gui.in_use)
3246 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003247 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003249
3250#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003251 /* Update the GUI tab line, it may appear or disappear. This may
3252 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003253 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003254 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003255 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003256 /* We don't want a resize event change "Rows" here, save and
3257 * restore it. Resizing is handled below. */
3258 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003259 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003260 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003261 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003262 if (using_tabline)
3263 fix_size = TRUE;
3264 if (!gui_use_tabline())
3265 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3266 }
3267#endif
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 for (i = 0; i < 3; i++)
3270 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003271 /* The scrollbar needs to be updated when it is shown/unshown and
3272 * when switching tab pages. But the size only changes when it's
3273 * shown/unshown. Thus we need two places to remember whether a
3274 * scrollbar is there or not. */
3275 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003276#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003277 || gui.which_scrollbars[i]
3278 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003279#endif
3280 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
3282 if (i == SBAR_BOTTOM)
3283 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3284 gui.which_scrollbars[i]);
3285 else
3286 {
3287 FOR_ALL_WINDOWS(wp)
3288 {
3289 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3290 }
3291 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003292 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3293 {
3294 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003295 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003296 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003297 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003298 if (gui.which_scrollbars[i])
3299 fix_size = TRUE;
3300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003302#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003303 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003304#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003305 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307
3308#ifdef FEAT_MENU
3309 if (gui.menu_is_active != prev_menu_is_active)
3310 {
3311 /* We don't want a resize event change "Rows" here, save and
3312 * restore it. Resizing is handled below. */
3313 i = Rows;
3314 gui_mch_enable_menu(gui.menu_is_active);
3315 Rows = i;
3316 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003317 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (gui.menu_is_active)
3319 fix_size = TRUE;
3320 }
3321#endif
3322
3323#ifdef FEAT_TOOLBAR
3324 if (using_toolbar != prev_toolbar)
3325 {
3326 gui_mch_show_toolbar(using_toolbar);
3327 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003328 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (using_toolbar)
3330 fix_size = TRUE;
3331 }
3332#endif
3333#ifdef FEAT_FOOTER
3334 if (using_footer != prev_footer)
3335 {
3336 gui_mch_enable_footer(using_footer);
3337 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003338 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (using_footer)
3340 fix_size = TRUE;
3341 }
3342#endif
3343#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3344 if (using_tearoff != prev_tearoff)
3345 {
3346 gui_mch_toggle_tearoffs(using_tearoff);
3347 prev_tearoff = using_tearoff;
3348 }
3349#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003350 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003351 {
3352#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003353 long prev_Columns = Columns;
3354 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 /* Adjust the size of the window to make the text area keep the
3357 * same size and to avoid that part of our window is off-screen
3358 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003359 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003360
3361#ifdef FEAT_GUI_GTK
3362 /* GTK has the annoying habit of sending us resize events when
3363 * changing the window size ourselves. This mostly happens when
3364 * waiting for a character to arrive, quite unpredictably, and may
3365 * change Columns and Rows when we don't want it. Wait for a
3366 * character here to avoid this effect.
3367 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003368 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003369 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003370 * Don't change Rows when adding menu/toolbar/tabline.
3371 * Don't change Columns when adding vertical toolbar. */
3372 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003373 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003374 if ((need_set_size & RESIZE_VERT) == 0)
3375 Rows = prev_Rows;
3376 if ((need_set_size & RESIZE_HOR) == 0)
3377 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003378#endif
3379 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003380#ifdef FEAT_WINDOWS
3381 /* When the console tabline appears or disappears the window positions
3382 * change. */
3383 if (firstwin->w_winrow != tabline_height())
3384 shell_new_rows(); /* recompute window positions and heights */
3385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387}
3388
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003389#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3390/*
3391 * Return TRUE if the GUI is taking care of the tabline.
3392 * It may still be hidden if 'showtabline' is zero.
3393 */
3394 int
3395gui_use_tabline()
3396{
3397 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3398}
3399
3400/*
3401 * Return TRUE if the GUI is showing the tabline.
3402 * This uses 'showtabline'.
3403 */
3404 static int
3405gui_has_tabline()
3406{
3407 if (!gui_use_tabline()
3408 || p_stal == 0
3409 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3410 return FALSE;
3411 return TRUE;
3412}
3413
3414/*
3415 * Update the tabline.
3416 * This may display/undisplay the tabline and update the labels.
3417 */
3418 void
3419gui_update_tabline()
3420{
3421 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003422 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003423
3424 if (!gui.starting && starting == 0)
3425 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003426 /* Updating the tabline uses direct GUI commands, flush
3427 * outstanding instructions first. (esp. clear screen) */
3428 out_flush();
3429 gui_mch_flush();
3430
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003431 if (!showit != !shown)
3432 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003433 if (showit != 0)
3434 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003435
3436 /* When the tabs change from hidden to shown or from shown to
3437 * hidden the size of the text area should remain the same. */
3438 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003439 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003440 }
3441}
3442
3443/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003444 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003445 */
3446 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003447get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003448 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003449 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003450{
3451 int modified = FALSE;
3452 char_u buf[40];
3453 int wincount;
3454 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003455 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003456
Bram Moolenaar57657d82006-04-21 22:12:41 +00003457 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003458 opt = (tooltip ? &p_gtt : &p_gtl);
3459 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003460 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003461 int use_sandbox = FALSE;
3462 int save_called_emsg = called_emsg;
3463 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003464 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003465 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3466 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003467
3468 called_emsg = FALSE;
3469
3470 printer_page_num = tabpage_index(tp);
3471# ifdef FEAT_EVAL
3472 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003473 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003474# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003475 /* It's almost as going to the tabpage, but without autocommands. */
3476 curtab->tp_firstwin = firstwin;
3477 curtab->tp_lastwin = lastwin;
3478 curtab->tp_curwin = curwin;
3479 save_curtab = curtab;
3480 curtab = tp;
3481 topframe = curtab->tp_topframe;
3482 firstwin = curtab->tp_firstwin;
3483 lastwin = curtab->tp_lastwin;
3484 curwin = curtab->tp_curwin;
3485 curbuf = curwin->w_buffer;
3486
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003487 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003488 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003489 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003490 STRCPY(NameBuff, res);
3491
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003492 /* Back to the original curtab. */
3493 curtab = save_curtab;
3494 topframe = curtab->tp_topframe;
3495 firstwin = curtab->tp_firstwin;
3496 lastwin = curtab->tp_lastwin;
3497 curwin = curtab->tp_curwin;
3498 curbuf = curwin->w_buffer;
3499
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003500 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003501 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003502 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003503 called_emsg |= save_called_emsg;
3504 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003505
3506 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3507 * use a default label. */
3508 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003509 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003510 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003511 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003512 if (!tooltip)
3513 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003514
3515 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3516 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3517 if (bufIsChanged(wp->w_buffer))
3518 modified = TRUE;
3519 if (modified || wincount > 1)
3520 {
3521 if (wincount > 1)
3522 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3523 else
3524 buf[0] = NUL;
3525 if (modified)
3526 STRCAT(buf, "+");
3527 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003528 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003529 mch_memmove(NameBuff, buf, STRLEN(buf));
3530 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003531 }
3532}
3533
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003534/*
3535 * Send the event for clicking to select tab page "nr".
3536 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003537 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003538 */
3539 int
3540send_tabline_event(nr)
3541 int nr;
3542{
3543 char_u string[3];
3544
3545 if (nr == tabpage_index(curtab))
3546 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003547
3548 /* Don't put events in the input queue now. */
3549 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003550# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003551 || cmdwin_type != 0
3552# endif
3553 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003554 {
3555 /* Set it back to the current tab page. */
3556 gui_mch_set_curtab(tabpage_index(curtab));
3557 return FALSE;
3558 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003559
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003560 string[0] = CSI;
3561 string[1] = KS_TABLINE;
3562 string[2] = KE_FILLER;
3563 add_to_input_buf(string, 3);
3564 string[0] = nr;
3565 add_to_input_buf_csi(string, 1);
3566 return TRUE;
3567}
3568
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003569/*
3570 * Send a tabline menu event
3571 */
3572 void
3573send_tabline_menu_event(tabidx, event)
3574 int tabidx;
3575 int event;
3576{
3577 char_u string[3];
3578
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003579 /* Don't put events in the input queue now. */
3580 if (hold_gui_events)
3581 return;
3582
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003583 string[0] = CSI;
3584 string[1] = KS_TABMENU;
3585 string[2] = KE_FILLER;
3586 add_to_input_buf(string, 3);
3587 string[0] = tabidx;
3588 string[1] = (char_u)(long)event;
3589 add_to_input_buf_csi(string, 2);
3590}
3591
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594/*
3595 * Scrollbar stuff:
3596 */
3597
Bram Moolenaare45828b2006-02-15 22:12:56 +00003598#if defined(FEAT_WINDOWS) || defined(PROTO)
3599/*
3600 * Remove all scrollbars. Used before switching to another tab page.
3601 */
3602 void
3603gui_remove_scrollbars()
3604{
3605 int i;
3606 win_T *wp;
3607
3608 for (i = 0; i < 3; i++)
3609 {
3610 if (i == SBAR_BOTTOM)
3611 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3612 else
3613 {
3614 FOR_ALL_WINDOWS(wp)
3615 {
3616 gui_do_scrollbar(wp, i, FALSE);
3617 }
3618 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003619 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003620 }
3621}
3622#endif
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 void
3625gui_create_scrollbar(sb, type, wp)
3626 scrollbar_T *sb;
3627 int type;
3628 win_T *wp;
3629{
3630 static int sbar_ident = 0;
3631
3632 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3633 sb->wp = wp;
3634 sb->type = type;
3635 sb->value = 0;
3636#ifdef FEAT_GUI_ATHENA
3637 sb->pixval = 0;
3638#endif
3639 sb->size = 1;
3640 sb->max = 1;
3641 sb->top = 0;
3642 sb->height = 0;
3643#ifdef FEAT_VERTSPLIT
3644 sb->width = 0;
3645#endif
3646 sb->status_height = 0;
3647 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3648}
3649
3650/*
3651 * Find the scrollbar with the given index.
3652 */
3653 scrollbar_T *
3654gui_find_scrollbar(ident)
3655 long ident;
3656{
3657 win_T *wp;
3658
3659 if (gui.bottom_sbar.ident == ident)
3660 return &gui.bottom_sbar;
3661 FOR_ALL_WINDOWS(wp)
3662 {
3663 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3664 return &wp->w_scrollbars[SBAR_LEFT];
3665 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3666 return &wp->w_scrollbars[SBAR_RIGHT];
3667 }
3668 return NULL;
3669}
3670
3671/*
3672 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3673 *
3674 * For Win32, Macintosh and GTK+ 2:
3675 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3676 * you stop dragging the scrollbar. We get here each time the scrollbar is
3677 * dragged another pixel, but as far as the rest of vim goes, it thinks
3678 * we're just hanging in the call to DispatchMessage() in
3679 * process_message(). The DispatchMessage() call that hangs was passed a
3680 * mouse button click event in the scrollbar window. -- webb.
3681 *
3682 * Solution: Do the scrolling right here. But only when allowed.
3683 * Ignore the scrollbars while executing an external command or when there
3684 * are still characters to be processed.
3685 */
3686 void
3687gui_drag_scrollbar(sb, value, still_dragging)
3688 scrollbar_T *sb;
3689 long value;
3690 int still_dragging;
3691{
3692#ifdef FEAT_WINDOWS
3693 win_T *wp;
3694#endif
3695 int sb_num;
3696#ifdef USE_ON_FLY_SCROLL
3697 colnr_T old_leftcol = curwin->w_leftcol;
3698# ifdef FEAT_SCROLLBIND
3699 linenr_T old_topline = curwin->w_topline;
3700# endif
3701# ifdef FEAT_DIFF
3702 int old_topfill = curwin->w_topfill;
3703# endif
3704#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003705 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 int byte_count;
3707#endif
3708
3709 if (sb == NULL)
3710 return;
3711
3712 /* Don't put events in the input queue now. */
3713 if (hold_gui_events)
3714 return;
3715
3716#ifdef FEAT_CMDWIN
3717 if (cmdwin_type != 0 && sb->wp != curwin)
3718 return;
3719#endif
3720
3721 if (still_dragging)
3722 {
3723 if (sb->wp == NULL)
3724 gui.dragged_sb = SBAR_BOTTOM;
3725 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3726 gui.dragged_sb = SBAR_LEFT;
3727 else
3728 gui.dragged_sb = SBAR_RIGHT;
3729 gui.dragged_wp = sb->wp;
3730 }
3731 else
3732 {
3733 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003734#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 /* Keep the "dragged_wp" value until after the scrolling, for when the
3736 * moust button is released. GTK2 doesn't send the button-up event. */
3737 gui.dragged_wp = NULL;
3738#endif
3739 }
3740
3741 /* Vertical sbar info is kept in the first sbar (the left one) */
3742 if (sb->wp != NULL)
3743 sb = &sb->wp->w_scrollbars[0];
3744
3745 /*
3746 * Check validity of value
3747 */
3748 if (value < 0)
3749 value = 0;
3750#ifdef SCROLL_PAST_END
3751 else if (value > sb->max)
3752 value = sb->max;
3753#else
3754 if (value > sb->max - sb->size + 1)
3755 value = sb->max - sb->size + 1;
3756#endif
3757
3758 sb->value = value;
3759
3760#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003761 /* When not allowed to do the scrolling right now, return.
3762 * This also checked input_available(), but that causes the first click in
3763 * a scrollbar to be ignored when Vim doesn't have focus. */
3764 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return;
3766#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003767#ifdef FEAT_INS_EXPAND
3768 /* Disallow scrolling the current window when the completion popup menu is
3769 * visible. */
3770 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3771 return;
3772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
3774#ifdef FEAT_RIGHTLEFT
3775 if (sb->wp == NULL && curwin->w_p_rl)
3776 {
3777 value = sb->max + 1 - sb->size - value;
3778 if (value < 0)
3779 value = 0;
3780 }
3781#endif
3782
3783 if (sb->wp != NULL) /* vertical scrollbar */
3784 {
3785 sb_num = 0;
3786#ifdef FEAT_WINDOWS
3787 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3788 sb_num++;
3789 if (wp == NULL)
3790 return;
3791#else
3792 if (sb->wp != curwin)
3793 return;
3794#endif
3795
3796#ifdef USE_ON_FLY_SCROLL
3797 current_scrollbar = sb_num;
3798 scrollbar_value = value;
3799 if (State & NORMAL)
3800 {
3801 gui_do_scroll();
3802 setcursor();
3803 }
3804 else if (State & INSERT)
3805 {
3806 ins_scroll();
3807 setcursor();
3808 }
3809 else if (State & CMDLINE)
3810 {
3811 if (msg_scrolled == 0)
3812 {
3813 gui_do_scroll();
3814 redrawcmdline();
3815 }
3816 }
3817# ifdef FEAT_FOLDING
3818 /* Value may have been changed for closed fold. */
3819 sb->value = sb->wp->w_topline - 1;
3820# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003821
3822 /* When dragging one scrollbar and there is another one at the other
3823 * side move the thumb of that one too. */
3824 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3825 gui_mch_set_scrollbar_thumb(
3826 &sb->wp->w_scrollbars[
3827 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3828 ? SBAR_LEFT : SBAR_RIGHT],
3829 sb->value, sb->size, sb->max);
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#else
3832 bytes[0] = CSI;
3833 bytes[1] = KS_VER_SCROLLBAR;
3834 bytes[2] = KE_FILLER;
3835 bytes[3] = (char_u)sb_num;
3836 byte_count = 4;
3837#endif
3838 }
3839 else
3840 {
3841#ifdef USE_ON_FLY_SCROLL
3842 scrollbar_value = value;
3843
3844 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003845 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 else if (State & INSERT)
3847 ins_horscroll();
3848 else if (State & CMDLINE)
3849 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003850 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003852 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 redrawcmdline();
3854 }
3855 }
3856 if (old_leftcol != curwin->w_leftcol)
3857 {
3858 updateWindow(curwin); /* update window, status and cmdline */
3859 setcursor();
3860 }
3861#else
3862 bytes[0] = CSI;
3863 bytes[1] = KS_HOR_SCROLLBAR;
3864 bytes[2] = KE_FILLER;
3865 byte_count = 3;
3866#endif
3867 }
3868
3869#ifdef USE_ON_FLY_SCROLL
3870# ifdef FEAT_SCROLLBIND
3871 /*
3872 * synchronize other windows, as necessary according to 'scrollbind'
3873 */
3874 if (curwin->w_p_scb
3875 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3876 || (sb->wp == curwin && (curwin->w_topline != old_topline
3877# ifdef FEAT_DIFF
3878 || curwin->w_topfill != old_topfill
3879# endif
3880 ))))
3881 {
3882 do_check_scrollbind(TRUE);
3883 /* need to update the window right here */
3884 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3885 if (wp->w_redr_type > 0)
3886 updateWindow(wp);
3887 setcursor();
3888 }
3889# endif
3890 out_flush();
3891 gui_update_cursor(FALSE, TRUE);
3892#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003893 add_to_input_buf(bytes, byte_count);
3894 add_long_to_buf((long_u)value, bytes);
3895 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896#endif
3897}
3898
3899/*
3900 * Scrollbar stuff:
3901 */
3902
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02003903#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003904/*
3905 * Called when something in the window layout has changed.
3906 */
3907 void
3908gui_may_update_scrollbars()
3909{
3910 if (gui.in_use && starting == 0)
3911 {
3912 out_flush();
3913 gui_init_which_components(NULL);
3914 gui_update_scrollbars(TRUE);
3915 }
3916 need_mouse_correct = TRUE;
3917}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02003918#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 void
3921gui_update_scrollbars(force)
3922 int force; /* Force all scrollbars to get updated */
3923{
3924 win_T *wp;
3925 scrollbar_T *sb;
3926 long val, size, max; /* need 32 bits here */
3927 int which_sb;
3928 int h, y;
3929#ifdef FEAT_VERTSPLIT
3930 static win_T *prev_curwin = NULL;
3931#endif
3932
3933 /* Update the horizontal scrollbar */
3934 gui_update_horiz_scrollbar(force);
3935
3936#ifndef WIN3264
3937 /* Return straight away if there is neither a left nor right scrollbar.
3938 * On MS-Windows this is required anyway for scrollwheel messages. */
3939 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3940 return;
3941#endif
3942
3943 /*
3944 * Don't want to update a scrollbar while we're dragging it. But if we
3945 * have both a left and right scrollbar, and we drag one of them, we still
3946 * need to update the other one.
3947 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003948 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3949 && gui.which_scrollbars[SBAR_LEFT]
3950 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 /*
3953 * If we have two scrollbars and one of them is being dragged, just
3954 * copy the scrollbar position from the dragged one to the other one.
3955 */
3956 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3957 if (gui.dragged_wp != NULL)
3958 gui_mch_set_scrollbar_thumb(
3959 &gui.dragged_wp->w_scrollbars[which_sb],
3960 gui.dragged_wp->w_scrollbars[0].value,
3961 gui.dragged_wp->w_scrollbars[0].size,
3962 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964
3965 /* avoid that moving components around generates events */
3966 ++hold_gui_events;
3967
3968 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3969 {
3970 if (wp->w_buffer == NULL) /* just in case */
3971 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003972 /* Skip a scrollbar that is being dragged. */
3973 if (!force && (gui.dragged_sb == SBAR_LEFT
3974 || gui.dragged_sb == SBAR_RIGHT)
3975 && gui.dragged_wp == wp)
3976 continue;
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978#ifdef SCROLL_PAST_END
3979 max = wp->w_buffer->b_ml.ml_line_count - 1;
3980#else
3981 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3982#endif
3983 if (max < 0) /* empty buffer */
3984 max = 0;
3985 val = wp->w_topline - 1;
3986 size = wp->w_height;
3987#ifdef SCROLL_PAST_END
3988 if (val > max) /* just in case */
3989 val = max;
3990#else
3991 if (size > max + 1) /* just in case */
3992 size = max + 1;
3993 if (val > max - size + 1)
3994 val = max - size + 1;
3995#endif
3996 if (val < 0) /* minimal value is 0 */
3997 val = 0;
3998
3999 /*
4000 * Scrollbar at index 0 (the left one) contains all the information.
4001 * It would be the same info for left and right so we just store it for
4002 * one of them.
4003 */
4004 sb = &wp->w_scrollbars[0];
4005
4006 /*
4007 * Note: no check for valid w_botline. If it's not valid the
4008 * scrollbars will be updated later anyway.
4009 */
4010 if (size < 1 || wp->w_botline - 2 > max)
4011 {
4012 /*
4013 * This can happen during changing files. Just don't update the
4014 * scrollbar for now.
4015 */
4016 sb->height = 0; /* Force update next time */
4017 if (gui.which_scrollbars[SBAR_LEFT])
4018 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4019 if (gui.which_scrollbars[SBAR_RIGHT])
4020 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4021 continue;
4022 }
4023 if (force || sb->height != wp->w_height
4024#ifdef FEAT_WINDOWS
4025 || sb->top != wp->w_winrow
4026 || sb->status_height != wp->w_status_height
4027# ifdef FEAT_VERTSPLIT
4028 || sb->width != wp->w_width
4029 || prev_curwin != curwin
4030# endif
4031#endif
4032 )
4033 {
4034 /* Height, width or position of scrollbar has changed. For
4035 * vertical split: curwin changed. */
4036 sb->height = wp->w_height;
4037#ifdef FEAT_WINDOWS
4038 sb->top = wp->w_winrow;
4039 sb->status_height = wp->w_status_height;
4040# ifdef FEAT_VERTSPLIT
4041 sb->width = wp->w_width;
4042# endif
4043#endif
4044
4045 /* Calculate height and position in pixels */
4046 h = (sb->height + sb->status_height) * gui.char_height;
4047 y = sb->top * gui.char_height + gui.border_offset;
4048#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4049 if (gui.menu_is_active)
4050 y += gui.menu_height;
4051#endif
4052
4053#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4054 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4055# ifdef FEAT_GUI_ATHENA
4056 y += gui.toolbar_height;
4057# else
4058# ifdef FEAT_GUI_MSWIN
4059 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4060# endif
4061# endif
4062#endif
4063
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004064#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4065 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004066 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004067#endif
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069#ifdef FEAT_WINDOWS
4070 if (wp->w_winrow == 0)
4071#endif
4072 {
4073 /* Height of top scrollbar includes width of top border */
4074 h += gui.border_offset;
4075 y -= gui.border_offset;
4076 }
4077 if (gui.which_scrollbars[SBAR_LEFT])
4078 {
4079 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4080 gui.left_sbar_x, y,
4081 gui.scrollbar_width, h);
4082 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4083 }
4084 if (gui.which_scrollbars[SBAR_RIGHT])
4085 {
4086 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4087 gui.right_sbar_x, y,
4088 gui.scrollbar_width, h);
4089 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4090 }
4091 }
4092
4093 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4094 * checking if the thumb moved at least a pixel. Only do this for
4095 * Athena, most other GUIs require the update anyway to make the
4096 * arrows work. */
4097#ifdef FEAT_GUI_ATHENA
4098 if (max == 0)
4099 y = 0;
4100 else
4101 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4102 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4103#else
4104 if (force || sb->value != val || sb->size != size || sb->max != max)
4105#endif
4106 {
4107 /* Thumb of scrollbar has moved */
4108 sb->value = val;
4109#ifdef FEAT_GUI_ATHENA
4110 sb->pixval = y;
4111#endif
4112 sb->size = size;
4113 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004114 if (gui.which_scrollbars[SBAR_LEFT]
4115 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4117 val, size, max);
4118 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004119 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4121 val, size, max);
4122 }
4123 }
4124#ifdef FEAT_VERTSPLIT
4125 prev_curwin = curwin;
4126#endif
4127 --hold_gui_events;
4128}
4129
4130/*
4131 * Enable or disable a scrollbar.
4132 * Check for scrollbars for vertically split windows which are not enabled
4133 * sometimes.
4134 */
4135 static void
4136gui_do_scrollbar(wp, which, enable)
4137 win_T *wp;
4138 int which; /* SBAR_LEFT or SBAR_RIGHT */
4139 int enable; /* TRUE to enable scrollbar */
4140{
4141#ifdef FEAT_VERTSPLIT
4142 int midcol = curwin->w_wincol + curwin->w_width / 2;
4143 int has_midcol = (wp->w_wincol <= midcol
4144 && wp->w_wincol + wp->w_width >= midcol);
4145
4146 /* Only enable scrollbars that contain the middle column of the current
4147 * window. */
4148 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4149 {
4150 /* Scrollbars only on one side. Don't enable scrollbars that don't
4151 * contain the middle column of the current window. */
4152 if (!has_midcol)
4153 enable = FALSE;
4154 }
4155 else
4156 {
4157 /* Scrollbars on both sides. Don't enable scrollbars that neither
4158 * contain the middle column of the current window nor are on the far
4159 * side. */
4160 if (midcol > Columns / 2)
4161 {
4162 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4163 enable = FALSE;
4164 }
4165 else
4166 {
4167 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4168 : !has_midcol)
4169 enable = FALSE;
4170 }
4171 }
4172#endif
4173 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4174}
4175
4176/*
4177 * Scroll a window according to the values set in the globals current_scrollbar
4178 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4179 * or FALSE otherwise.
4180 */
4181 int
4182gui_do_scroll()
4183{
4184 win_T *wp, *save_wp;
4185 int i;
4186 long nlines;
4187 pos_T old_cursor;
4188 linenr_T old_topline;
4189#ifdef FEAT_DIFF
4190 int old_topfill;
4191#endif
4192
4193 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4194 if (wp == NULL)
4195 break;
4196 if (wp == NULL)
4197 /* Couldn't find window */
4198 return FALSE;
4199
4200 /*
4201 * Compute number of lines to scroll. If zero, nothing to do.
4202 */
4203 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4204 if (nlines == 0)
4205 return FALSE;
4206
4207 save_wp = curwin;
4208 old_topline = wp->w_topline;
4209#ifdef FEAT_DIFF
4210 old_topfill = wp->w_topfill;
4211#endif
4212 old_cursor = wp->w_cursor;
4213 curwin = wp;
4214 curbuf = wp->w_buffer;
4215 if (nlines < 0)
4216 scrolldown(-nlines, gui.dragged_wp == NULL);
4217 else
4218 scrollup(nlines, gui.dragged_wp == NULL);
4219 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4220 * the mouse-up event already, but we still want it to behave like when
4221 * dragging. But not the next click in an arrow. */
4222 if (gui.dragged_sb == SBAR_NONE)
4223 gui.dragged_wp = NULL;
4224
4225 if (old_topline != wp->w_topline
4226#ifdef FEAT_DIFF
4227 || old_topfill != wp->w_topfill
4228#endif
4229 )
4230 {
4231 if (p_so != 0)
4232 {
4233 cursor_correct(); /* fix window for 'so' */
4234 update_topline(); /* avoid up/down jump */
4235 }
4236 if (old_cursor.lnum != wp->w_cursor.lnum)
4237 coladvance(wp->w_curswant);
4238#ifdef FEAT_SCROLLBIND
4239 wp->w_scbind_pos = wp->w_topline;
4240#endif
4241 }
4242
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004243 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4244 validate_cursor();
4245
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 curwin = save_wp;
4247 curbuf = save_wp->w_buffer;
4248
4249 /*
4250 * Don't call updateWindow() when nothing has changed (it will overwrite
4251 * the status line!).
4252 */
4253 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004254 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#ifdef FEAT_DIFF
4256 || old_topfill != wp->w_topfill
4257#endif
4258 )
4259 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004260 int type = VALID;
4261
4262#ifdef FEAT_INS_EXPAND
4263 if (pum_visible())
4264 {
4265 type = NOT_VALID;
4266 wp->w_lines_valid = 0;
4267 }
4268#endif
4269 /* Don't set must_redraw here, it may cause the popup menu to
4270 * disappear when losing focus after a scrollbar drag. */
4271 if (wp->w_redr_type < type)
4272 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 updateWindow(wp); /* update window, status line, and cmdline */
4274 }
4275
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004276#ifdef FEAT_INS_EXPAND
4277 /* May need to redraw the popup menu. */
4278 if (pum_visible())
4279 pum_redraw();
4280#endif
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4283}
4284
4285
4286/*
4287 * Horizontal scrollbar stuff:
4288 */
4289
4290/*
4291 * Return length of line "lnum" for horizontal scrolling.
4292 */
4293 static colnr_T
4294scroll_line_len(lnum)
4295 linenr_T lnum;
4296{
4297 char_u *p;
4298 colnr_T col;
4299 int w;
4300
4301 p = ml_get(lnum);
4302 col = 0;
4303 if (*p != NUL)
4304 for (;;)
4305 {
4306 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004307 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 if (*p == NUL) /* don't count the last character */
4309 break;
4310 col += w;
4311 }
4312 return col;
4313}
4314
4315/* Remember which line is currently the longest, so that we don't have to
4316 * search for it when scrolling horizontally. */
4317static linenr_T longest_lnum = 0;
4318
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004319/*
4320 * Find longest visible line number. If this is not possible (or not desired,
4321 * by setting 'h' in "guioptions") then the current line number is returned.
4322 */
4323 static linenr_T
4324gui_find_longest_lnum()
4325{
4326 linenr_T ret = 0;
4327
4328 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4329 * line numbers, topline and botline can be invalid when displaying is
4330 * postponed. */
4331 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4332 && curwin->w_topline <= curwin->w_cursor.lnum
4333 && curwin->w_botline > curwin->w_cursor.lnum
4334 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4335 {
4336 linenr_T lnum;
4337 colnr_T n;
4338 long max = 0;
4339
4340 /* Use maximum of all visible lines. Remember the lnum of the
4341 * longest line, closest to the cursor line. Used when scrolling
4342 * below. */
4343 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4344 {
4345 n = scroll_line_len(lnum);
4346 if (n > (colnr_T)max)
4347 {
4348 max = n;
4349 ret = lnum;
4350 }
4351 else if (n == (colnr_T)max
4352 && abs((int)(lnum - curwin->w_cursor.lnum))
4353 < abs((int)(ret - curwin->w_cursor.lnum)))
4354 ret = lnum;
4355 }
4356 }
4357 else
4358 /* Use cursor line only. */
4359 ret = curwin->w_cursor.lnum;
4360
4361 return ret;
4362}
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 static void
4365gui_update_horiz_scrollbar(force)
4366 int force;
4367{
4368 long value, size, max; /* need 32 bit ints here */
4369
4370 if (!gui.which_scrollbars[SBAR_BOTTOM])
4371 return;
4372
4373 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4374 return;
4375
4376 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4377 return;
4378
4379 /*
4380 * It is possible for the cursor to be invalid if we're in the middle of
4381 * something (like changing files). If so, don't do anything for now.
4382 */
4383 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4384 {
4385 gui.bottom_sbar.value = -1;
4386 return;
4387 }
4388
4389 size = W_WIDTH(curwin);
4390 if (curwin->w_p_wrap)
4391 {
4392 value = 0;
4393#ifdef SCROLL_PAST_END
4394 max = 0;
4395#else
4396 max = W_WIDTH(curwin) - 1;
4397#endif
4398 }
4399 else
4400 {
4401 value = curwin->w_leftcol;
4402
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004403 longest_lnum = gui_find_longest_lnum();
4404 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406#ifdef FEAT_VIRTUALEDIT
4407 if (virtual_active())
4408 {
4409 /* May move the cursor even further to the right. */
4410 if (curwin->w_virtcol >= (colnr_T)max)
4411 max = curwin->w_virtcol;
4412 }
4413#endif
4414
4415#ifndef SCROLL_PAST_END
4416 max += W_WIDTH(curwin) - 1;
4417#endif
4418 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004419 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 size -= curwin_col_off();
4421#ifndef SCROLL_PAST_END
4422 max -= curwin_col_off();
4423#endif
4424 }
4425
4426#ifndef SCROLL_PAST_END
4427 if (value > max - size + 1)
4428 value = max - size + 1; /* limit the value to allowable range */
4429#endif
4430
4431#ifdef FEAT_RIGHTLEFT
4432 if (curwin->w_p_rl)
4433 {
4434 value = max + 1 - size - value;
4435 if (value < 0)
4436 {
4437 size += value;
4438 value = 0;
4439 }
4440 }
4441#endif
4442 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4443 && max == gui.bottom_sbar.max)
4444 return;
4445
4446 gui.bottom_sbar.value = value;
4447 gui.bottom_sbar.size = size;
4448 gui.bottom_sbar.max = max;
4449 gui.prev_wrap = curwin->w_p_wrap;
4450
4451 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4452}
4453
4454/*
4455 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4456 */
4457 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004458gui_do_horiz_scroll(leftcol, compute_longest_lnum)
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004459 long_u leftcol;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004460 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462 /* no wrapping, no scrolling */
4463 if (curwin->w_p_wrap)
4464 return FALSE;
4465
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004466 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 return FALSE;
4468
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004469 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004472 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004474 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004475 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004477 if (compute_longest_lnum)
4478 {
4479 curwin->w_cursor.lnum = gui_find_longest_lnum();
4480 curwin->w_cursor.col = 0;
4481 }
4482 /* Do a sanity check on "longest_lnum", just in case. */
4483 else if (longest_lnum >= curwin->w_topline
4484 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 curwin->w_cursor.lnum = longest_lnum;
4487 curwin->w_cursor.col = 0;
4488 }
4489 }
4490
4491 return leftcol_changed();
4492}
4493
4494/*
4495 * Check that none of the colors are the same as the background color
4496 */
4497 void
4498gui_check_colors()
4499{
4500 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4501 {
4502 gui_set_bg_color((char_u *)"White");
4503 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4504 gui_set_fg_color((char_u *)"Black");
4505 }
4506}
4507
Bram Moolenaar3918c952005-03-15 22:34:55 +00004508 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509gui_set_fg_color(name)
4510 char_u *name;
4511{
4512 gui.norm_pixel = gui_get_color(name);
4513 hl_set_fg_color_name(vim_strsave(name));
4514}
4515
Bram Moolenaar3918c952005-03-15 22:34:55 +00004516 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517gui_set_bg_color(name)
4518 char_u *name;
4519{
4520 gui.back_pixel = gui_get_color(name);
4521 hl_set_bg_color_name(vim_strsave(name));
4522}
4523
4524/*
4525 * Allocate a color by name.
4526 * Returns INVALCOLOR and gives an error message when failed.
4527 */
4528 guicolor_T
4529gui_get_color(name)
4530 char_u *name;
4531{
4532 guicolor_T t;
4533
4534 if (*name == NUL)
4535 return INVALCOLOR;
4536 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004539#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 && gui.in_use
4541#endif
4542 )
4543 EMSG2(_("E254: Cannot allocate color %s"), name);
4544 return t;
4545}
4546
4547/*
4548 * Return the grey value of a color (range 0-255).
4549 */
4550 int
4551gui_get_lightness(pixel)
4552 guicolor_T pixel;
4553{
4554 long_u rgb = gui_mch_get_rgb(pixel);
4555
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004556 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004557 + (((rgb >> 8) & 0xff) * 587)
4558 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559}
4560
4561#if defined(FEAT_GUI_X11) || defined(PROTO)
4562 void
4563gui_new_scrollbar_colors()
4564{
4565 win_T *wp;
4566
4567 /* Nothing to do if GUI hasn't started yet. */
4568 if (!gui.in_use)
4569 return;
4570
4571 FOR_ALL_WINDOWS(wp)
4572 {
4573 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4574 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4575 }
4576 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4577}
4578#endif
4579
4580/*
4581 * Call this when focus has changed.
4582 */
4583 void
4584gui_focus_change(in_focus)
4585 int in_focus;
4586{
4587/*
4588 * Skip this code to avoid drawing the cursor when debugging and switching
4589 * between the debugger window and gvim.
4590 */
4591#if 1
4592 gui.in_focus = in_focus;
4593 out_flush(); /* make sure output has been written */
4594 gui_update_cursor(TRUE, FALSE);
4595
4596# ifdef FEAT_XIM
4597 xim_set_focus(in_focus);
4598# endif
4599
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004600 /* Put events in the input queue only when allowed.
4601 * ui_focus_change() isn't called directly, because it invokes
4602 * autocommands and that must not happen asynchronously. */
4603 if (!hold_gui_events)
4604 {
4605 char_u bytes[3];
4606
4607 bytes[0] = CSI;
4608 bytes[1] = KS_EXTRA;
4609 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4610 add_to_input_buf(bytes, 3);
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#endif
4613}
4614
4615/*
4616 * Called when the mouse moved (but not when dragging).
4617 */
4618 void
4619gui_mouse_moved(x, y)
4620 int x;
4621 int y;
4622{
4623 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004624 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaard3667a22006-03-16 21:35:52 +00004626 /* Ignore this while still starting up. */
4627 if (!gui.in_use || gui.starting)
4628 return;
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630#ifdef FEAT_MOUSESHAPE
4631 /* Get window pointer, and update mouse shape as well. */
4632 wp = xy2win(x, y);
4633#endif
4634
4635 /* Only handle this when 'mousefocus' set and ... */
4636 if (p_mousef
4637 && !hold_gui_events /* not holding events */
4638 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4639 && State != HITRETURN /* but not hit-return prompt */
4640 && msg_scrolled == 0 /* no scrolled message */
4641 && !need_mouse_correct /* not moving the pointer */
4642 && gui.in_focus) /* gvim in focus */
4643 {
4644 /* Don't move the mouse when it's left or right of the Vim window */
4645 if (x < 0 || x > Columns * gui.char_width)
4646 return;
4647#ifndef FEAT_MOUSESHAPE
4648 wp = xy2win(x, y);
4649#endif
4650 if (wp == curwin || wp == NULL)
4651 return; /* still in the same old window, or none at all */
4652
Bram Moolenaar9c102382006-05-03 21:26:49 +00004653#ifdef FEAT_WINDOWS
4654 /* Ignore position in the tab pages line. */
4655 if (Y_2_ROW(y) < tabline_height())
4656 return;
4657#endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /*
4660 * format a mouse click on status line input
4661 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004662 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4663 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 */
4665 if (finish_op)
4666 {
4667 /* abort the current operator first */
4668 st[0] = ESC;
4669 add_to_input_buf(st, 1);
4670 }
4671 st[0] = CSI;
4672 st[1] = KS_MOUSE;
4673 st[2] = KE_FILLER;
4674 st[3] = (char_u)MOUSE_LEFT;
4675 fill_mouse_coord(st + 4,
4676#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004677 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#else
4679 -1,
4680#endif
4681 wp->w_height + W_WINROW(wp));
4682
4683 add_to_input_buf(st, 8);
4684 st[3] = (char_u)MOUSE_RELEASE;
4685 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#ifdef FEAT_GUI_GTK
4687 /* Need to wake up the main loop */
4688 if (gtk_main_level() > 0)
4689 gtk_main_quit();
4690#endif
4691 }
4692}
4693
4694/*
4695 * Called when mouse should be moved to window with focus.
4696 */
4697 void
4698gui_mouse_correct()
4699{
4700 int x, y;
4701 win_T *wp = NULL;
4702
4703 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004704
4705 if (!(gui.in_use && p_mousef))
4706 return;
4707
4708 gui_mch_getmouse(&x, &y);
4709 /* Don't move the mouse when it's left or right of the Vim window */
4710 if (x < 0 || x > Columns * gui.char_width)
4711 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004712 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004713# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004714 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004715# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004716 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004717 wp = xy2win(x, y);
4718 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004720 validate_cline_row();
4721 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4722 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725}
4726
4727/*
4728 * Find window where the mouse pointer "y" coordinate is in.
4729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 static win_T *
4731xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004732 int x UNUSED;
4733 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735#ifdef FEAT_WINDOWS
4736 int row;
4737 int col;
4738 win_T *wp;
4739
4740 row = Y_2_ROW(y);
4741 col = X_2_COL(x);
4742 if (row < 0 || col < 0) /* before first window */
4743 return NULL;
4744 wp = mouse_find_win(&row, &col);
4745# ifdef FEAT_MOUSESHAPE
4746 if (State == HITRETURN || State == ASKMORE)
4747 {
4748 if (Y_2_ROW(y) >= msg_row)
4749 update_mouseshape(SHAPE_IDX_MOREL);
4750 else
4751 update_mouseshape(SHAPE_IDX_MORE);
4752 }
4753 else if (row > wp->w_height) /* below status line */
4754 update_mouseshape(SHAPE_IDX_CLINE);
4755# ifdef FEAT_VERTSPLIT
4756 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004757 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 update_mouseshape(SHAPE_IDX_VSEP);
4759# endif
4760 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004761 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 update_mouseshape(SHAPE_IDX_STATUS);
4763 else
4764 update_mouseshape(-2);
4765# endif
4766 return wp;
4767#else
4768 return firstwin;
4769#endif
4770}
4771
4772/*
4773 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4774 * File names may be given to redefine the args list.
4775 */
4776 void
4777ex_gui(eap)
4778 exarg_T *eap;
4779{
4780 char_u *arg = eap->arg;
4781
4782 /*
4783 * Check for "-f" argument: foreground, don't fork.
4784 * Also don't fork when started with "gvim -f".
4785 * Do fork when using "gui -b".
4786 */
4787 if (arg[0] == '-'
4788 && (arg[1] == 'f' || arg[1] == 'b')
4789 && (arg[2] == NUL || vim_iswhite(arg[2])))
4790 {
4791 gui.dofork = (arg[1] == 'b');
4792 eap->arg = skipwhite(eap->arg + 2);
4793 }
4794 if (!gui.in_use)
4795 {
4796 /* Clear the command. Needed for when forking+exiting, to avoid part
4797 * of the argument ending up after the shell prompt. */
4798 msg_clr_eos_force();
4799 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004800#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004801 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804 if (!ends_excmd(*eap->arg))
4805 ex_next(eap);
4806}
4807
4808#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004809 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810/*
4811 * This is shared between Athena, Motif and GTK.
4812 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004813static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
4815/*
4816 * Callback function for do_in_runtimepath().
4817 */
4818 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004819gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004823 char_u *gfp_buffer = cookie;
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if (STRLEN(fname) >= MAXPATHL)
4826 *gfp_buffer = NUL;
4827 else
4828 STRCPY(gfp_buffer, fname);
4829}
4830
4831/*
4832 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4833 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4834 */
4835 int
4836gui_find_bitmap(name, buffer, ext)
4837 char_u *name;
4838 char_u *buffer;
4839 char *ext;
4840{
4841 if (STRLEN(name) > MAXPATHL - 14)
4842 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004843 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004844 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4845 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 return FAIL;
4847 return OK;
4848}
4849
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004850# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851/*
4852 * Given the name of the "icon=" argument, try finding the bitmap file for the
4853 * icon. If it is an absolute path name, use it as it is. Otherwise append
4854 * "ext" and search for it in 'runtimepath'.
4855 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4856 * contains "name".
4857 */
4858 void
4859gui_find_iconfile(name, buffer, ext)
4860 char_u *name;
4861 char_u *buffer;
4862 char *ext;
4863{
4864 char_u buf[MAXPATHL + 1];
4865
4866 expand_env(name, buffer, MAXPATHL);
4867 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4868 STRCPY(buffer, buf);
4869}
4870# endif
4871#endif
4872
Bram Moolenaar9372a112005-12-06 19:59:18 +00004873#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 void
4875display_errors()
4876{
4877 char_u *p;
4878
4879 if (isatty(2))
4880 fflush(stderr);
4881 else if (error_ga.ga_data != NULL)
4882 {
4883 /* avoid putting up a message box with blanks only */
4884 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4885 if (!isspace(*p))
4886 {
4887 /* Truncate a very long message, it will go off-screen. */
4888 if (STRLEN(p) > 2000)
4889 STRCPY(p + 2000 - 14, "...(truncated)");
4890 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004891 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 break;
4893 }
4894 ga_clear(&error_ga);
4895 }
4896}
4897#endif
4898
4899#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4900/*
4901 * Return TRUE if still starting up and there is no place to enter text.
4902 * For GTK and X11 we check if stderr is not a tty, which means we were
4903 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4904 * allow typing on stdin.
4905 */
4906 int
4907no_console_input()
4908{
4909 return ((!gui.in_use || gui.starting)
4910# ifndef NO_CONSOLE
4911 && !isatty(0) && !isatty(2)
4912# endif
4913 );
4914}
4915#endif
4916
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004917#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00004918 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004919 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920/*
4921 * Update the current window and the screen.
4922 */
4923 void
4924gui_update_screen()
4925{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004926#ifdef FEAT_CONCEAL
4927 linenr_T conceal_old_cursor_line = 0;
4928 linenr_T conceal_new_cursor_line = 0;
4929 int conceal_update_lines = FALSE;
4930#endif
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 update_topline();
4933 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004934
4935#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00004936 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004937 if (!finish_op && (
4938# ifdef FEAT_AUTOCMD
4939 has_cursormoved()
4940# endif
4941# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
4942 ||
4943# endif
4944# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004945 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004946# endif
4947 )
4948 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00004949 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004950# ifdef FEAT_AUTOCMD
4951 if (has_cursormoved())
4952 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4953# endif
4954# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004955 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004956 {
4957 conceal_old_cursor_line = last_cursormoved.lnum;
4958 conceal_new_cursor_line = curwin->w_cursor.lnum;
4959 conceal_update_lines = TRUE;
4960 }
4961# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00004962 last_cursormoved = curwin->w_cursor;
4963 }
4964#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 update_screen(0); /* may need to update the screen */
4967 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004968# if defined(FEAT_CONCEAL)
4969 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004970 && (conceal_old_cursor_line != conceal_new_cursor_line
4971 || conceal_cursor_line(curwin)
4972 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004973 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004974 if (conceal_old_cursor_line != conceal_new_cursor_line)
4975 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004976 update_single_line(curwin, conceal_new_cursor_line);
4977 curwin->w_valid &= ~VALID_CROW;
4978 }
4979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 out_flush(); /* make sure output has been written */
4981 gui_update_cursor(TRUE, FALSE);
4982 gui_mch_flush();
4983}
4984#endif
4985
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004986#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4988
4989/*
4990 * Get the text to use in a find/replace dialog. Uses the last search pattern
4991 * if the argument is empty.
4992 * Returns an allocated string.
4993 */
4994 char_u *
4995get_find_dialog_text(arg, wwordp, mcasep)
4996 char_u *arg;
4997 int *wwordp; /* return: TRUE if \< \> found */
4998 int *mcasep; /* return: TRUE if \C found */
4999{
5000 char_u *text;
5001
5002 if (*arg == NUL)
5003 text = last_search_pat();
5004 else
5005 text = arg;
5006 if (text != NULL)
5007 {
5008 text = vim_strsave(text);
5009 if (text != NULL)
5010 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005011 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 int i;
5013
5014 /* Remove "\V" */
5015 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5016 {
5017 mch_memmove(text, text + 2, (size_t)(len - 1));
5018 len -= 2;
5019 }
5020
5021 /* Recognize "\c" and "\C" and remove. */
5022 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5023 {
5024 *mcasep = (text[1] == 'C');
5025 mch_memmove(text, text + 2, (size_t)(len - 1));
5026 len -= 2;
5027 }
5028
5029 /* Recognize "\<text\>" and remove. */
5030 if (len >= 4
5031 && STRNCMP(text, "\\<", 2) == 0
5032 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5033 {
5034 *wwordp = TRUE;
5035 mch_memmove(text, text + 2, (size_t)(len - 4));
5036 text[len - 4] = NUL;
5037 }
5038
5039 /* Recognize "\/" or "\?" and remove. */
5040 for (i = 0; i + 1 < len; ++i)
5041 if (text[i] == '\\' && (text[i + 1] == '/'
5042 || text[i + 1] == '?'))
5043 {
5044 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5045 --len;
5046 }
5047 }
5048 }
5049 return text;
5050}
5051
5052/*
5053 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5054 */
5055 static void
5056concat_esc(gap, text, what)
5057 garray_T *gap;
5058 char_u *text;
5059 int what;
5060{
5061 while (*text != NUL)
5062 {
5063#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005064 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (l > 1)
5067 {
5068 while (--l >= 0)
5069 ga_append(gap, *text++);
5070 continue;
5071 }
5072#endif
5073 if (*text == what)
5074 ga_append(gap, '\\');
5075 ga_append(gap, *text);
5076 ++text;
5077 }
5078}
5079
5080/*
5081 * Handle the press of a button in the find-replace dialog.
5082 * Return TRUE when something was added to the input buffer.
5083 */
5084 int
5085gui_do_findrepl(flags, find_text, repl_text, down)
5086 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5087 char_u *find_text;
5088 char_u *repl_text;
5089 int down; /* Search downwards. */
5090{
5091 garray_T ga;
5092 int i;
5093 int type = (flags & FRD_TYPE_MASK);
5094 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005095 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005096 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005097 static int busy = FALSE;
5098
5099 /* When the screen is being updated we should not change buffers and
5100 * windows structures, it may cause freed memory to be used. Also don't
5101 * do this recursively (pressing "Find" quickly several times. */
5102 if (updating_screen || busy)
5103 return FALSE;
5104
5105 /* refuse replace when text cannot be changed */
5106 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5107 return FALSE;
5108
5109 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005112 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 ga_concat(&ga, (char_u *)"%s/");
5114
5115 ga_concat(&ga, (char_u *)"\\V");
5116 if (flags & FRD_MATCH_CASE)
5117 ga_concat(&ga, (char_u *)"\\C");
5118 else
5119 ga_concat(&ga, (char_u *)"\\c");
5120 if (flags & FRD_WHOLE_WORD)
5121 ga_concat(&ga, (char_u *)"\\<");
5122 if (type == FRD_REPLACEALL || down)
5123 concat_esc(&ga, find_text, '/'); /* escape slashes */
5124 else
5125 concat_esc(&ga, find_text, '?'); /* escape '?' */
5126 if (flags & FRD_WHOLE_WORD)
5127 ga_concat(&ga, (char_u *)"\\>");
5128
5129 if (type == FRD_REPLACEALL)
5130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005132 /* escape / and \ */
5133 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5134 if (p != NULL)
5135 ga_concat(&ga, p);
5136 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005138 }
5139 ga_append(&ga, NUL);
5140
5141 if (type == FRD_REPLACE)
5142 {
5143 /* Do the replacement when the text at the cursor matches. Thus no
5144 * replacement is done if the cursor was moved! */
5145 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5146 regmatch.rm_ic = 0;
5147 if (regmatch.regprog != NULL)
5148 {
5149 p = ml_get_cursor();
5150 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5151 && regmatch.startp[0] == p)
5152 {
5153 /* Clear the command line to remove any old "No match"
5154 * error. */
5155 msg_end_prompt();
5156
5157 if (u_save_cursor() == OK)
5158 {
5159 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005160 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005161
5162 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005163 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005164 ins_str(repl_text);
5165 }
5166 }
5167 else
5168 MSG(_("No match at cursor, finding next"));
5169 vim_free(regmatch.regprog);
5170 }
5171 }
5172
5173 if (type == FRD_REPLACEALL)
5174 {
5175 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005176 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 do_cmdline_cmd(ga.ga_data);
5178 }
5179 else
5180 {
5181 /* Search for the next match. */
5182 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005184 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 msg_scroll = i; /* don't let an error message set msg_scroll */
5186 }
5187
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005188 /* Don't want to pass did_emsg to other code, it may cause disabling
5189 * syntax HL if we were busy redrawing. */
5190 did_emsg = save_did_emsg;
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (State & (NORMAL | INSERT))
5193 {
5194 gui_update_screen(); /* update the screen */
5195 msg_didout = 0; /* overwrite any message */
5196 need_wait_return = FALSE; /* don't wait for return */
5197 }
5198
5199 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005200 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 return (ga.ga_len > 0);
5202}
5203
5204#endif
5205
5206#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5207 || defined(FEAT_GUI_MSWIN) \
5208 || defined(FEAT_GUI_MAC) \
5209 || defined(PROTO)
5210
5211#ifdef FEAT_WINDOWS
5212static void gui_wingoto_xy __ARGS((int x, int y));
5213
5214/*
5215 * Jump to the window at specified point (x, y).
5216 */
5217 static void
5218gui_wingoto_xy(x, y)
5219 int x;
5220 int y;
5221{
5222 int row = Y_2_ROW(y);
5223 int col = X_2_COL(x);
5224 win_T *wp;
5225
5226 if (row >= 0 && col >= 0)
5227 {
5228 wp = mouse_find_win(&row, &col);
5229 if (wp != NULL && wp != curwin)
5230 win_goto(wp);
5231 }
5232}
5233#endif
5234
5235/*
5236 * Process file drop. Mouse cursor position, key modifiers, name of files
5237 * and count of files are given. Argument "fnames[count]" has full pathnames
5238 * of dropped files, they will be freed in this function, and caller can't use
5239 * fnames after call this function.
5240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 void
5242gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005243 int x UNUSED;
5244 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 int_u modifiers;
5246 char_u **fnames;
5247 int count;
5248{
5249 int i;
5250 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005251 static int entered = FALSE;
5252
5253 /*
5254 * This function is called by event handlers. Just in case we get a
5255 * second event before the first one is handled, ignore the second one.
5256 * Not sure if this can ever happen, just in case.
5257 */
5258 if (entered)
5259 return;
5260 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 /*
5263 * When the cursor is at the command line, add the file names to the
5264 * command line, don't edit the files.
5265 */
5266 if (State & CMDLINE)
5267 {
5268 shorten_filenames(fnames, count);
5269 for (i = 0; i < count; ++i)
5270 {
5271 if (fnames[i] != NULL)
5272 {
5273 if (i > 0)
5274 add_to_input_buf((char_u*)" ", 1);
5275
5276 /* We don't know what command is used thus we can't be sure
5277 * about which characters need to be escaped. Only escape the
5278 * most common ones. */
5279# ifdef BACKSLASH_IN_FILENAME
5280 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5281# else
5282 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5283# endif
5284 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005285 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 vim_free(p);
5287 vim_free(fnames[i]);
5288 }
5289 }
5290 vim_free(fnames);
5291 }
5292 else
5293 {
5294 /* Go to the window under mouse cursor, then shorten given "fnames" by
5295 * current window, because a window can have local current dir. */
5296# ifdef FEAT_WINDOWS
5297 gui_wingoto_xy(x, y);
5298# endif
5299 shorten_filenames(fnames, count);
5300
5301 /* If Shift held down, remember the first item. */
5302 if ((modifiers & MOUSE_SHIFT) != 0)
5303 p = vim_strsave(fnames[0]);
5304 else
5305 p = NULL;
5306
5307 /* Handle the drop, :edit or :split to get to the file. This also
5308 * frees fnames[]. Skip this if there is only one item it's a
5309 * directory and Shift is held down. */
5310 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5311 && mch_isdir(fnames[0]))
5312 {
5313 vim_free(fnames[0]);
5314 vim_free(fnames);
5315 }
5316 else
5317 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5318
5319 /* If Shift held down, change to first file's directory. If the first
5320 * item is a directory, change to that directory (and let the explorer
5321 * plugin show the contents). */
5322 if (p != NULL)
5323 {
5324 if (mch_isdir(p))
5325 {
5326 if (mch_chdir((char *)p) == 0)
5327 shorten_fnames(TRUE);
5328 }
5329 else if (vim_chdirfile(p) == OK)
5330 shorten_fnames(TRUE);
5331 vim_free(p);
5332 }
5333
5334 /* Update the screen display */
5335 update_screen(NOT_VALID);
5336# ifdef FEAT_MENU
5337 gui_update_menus(0);
5338# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005339#ifdef FEAT_TITLE
5340 maketitle();
5341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 setcursor();
5343 out_flush();
5344 gui_update_cursor(FALSE, FALSE);
5345 gui_mch_flush();
5346 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005347
5348 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349}
5350#endif