blob: 49014a532dfd67c5d765086250bf738eb5e92116 [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)
108 /* Display error messages in a dialog now. */
109 display_errors();
110#endif
111
112#if defined(MAY_FORK) && !defined(__QNXNTO__)
113 /*
114 * Quit the current process and continue in the child.
115 * Makes "gvim file" disconnect from the shell it was started in.
116 * Don't do this when Vim was started with "-f" or the 'f' flag is present
117 * in 'guioptions'.
118 */
119 if (gui.in_use && dofork)
120 {
121 int pipefd[2]; /* pipe between parent and child */
122 int pipe_error;
123 char dummy;
124 pid_t pid = -1;
125
126 /* Setup a pipe between the child and the parent, so that the parent
127 * knows when the child has done the setsid() call and is allowed to
128 * exit. */
129 pipe_error = (pipe(pipefd) < 0);
130 pid = fork();
131 if (pid > 0) /* Parent */
132 {
133 /* Give the child some time to do the setsid(), otherwise the
134 * exit() may kill the child too (when starting gvim from inside a
135 * gvim). */
136 if (pipe_error)
137 ui_delay(300L, TRUE);
138 else
139 {
140 /* The read returns when the child closes the pipe (or when
141 * the child dies for some reason). */
142 close(pipefd[1]);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000143 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 close(pipefd[0]);
145 }
146
147 /* When swapping screens we may need to go to the next line, e.g.,
148 * after a hit-enter prompt and using ":gui". */
149 if (newline_on_exit)
150 mch_errmsg("\r\n");
151
152 /*
153 * The parent must skip the normal exit() processing, the child
154 * will do it. For example, GTK messes up signals when exiting.
155 */
156 _exit(0);
157 }
158
159# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
160 /*
161 * Change our process group. On some systems/shells a CTRL-C in the
162 * shell where Vim was started would otherwise kill gvim!
163 */
164 if (pid == 0) /* child */
165# if defined(HAVE_SETSID)
166 (void)setsid();
167# else
168 (void)setpgid(0, 0);
169# endif
170# endif
171 if (!pipe_error)
172 {
173 close(pipefd[0]);
174 close(pipefd[1]);
175 }
176
177# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
178 /* Tell the session manager our new PID */
179 gui_mch_forked();
180# endif
181 }
182#else
183# if defined(__QNXNTO__)
184 if (gui.in_use && dofork)
185 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
186 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
187# endif
188#endif
189
190#ifdef FEAT_AUTOCMD
Bram Moolenaar265e5072006-08-29 16:13:22 +0000191 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
192 * the GUIFailed event. */
193 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
194 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196
197 --recursive;
198}
199
200/*
201 * Call this when vim starts up, whether or not the GUI is started
202 */
203 void
204gui_prepare(argc, argv)
205 int *argc;
206 char **argv;
207{
208 gui.in_use = FALSE; /* No GUI yet (maybe later) */
209 gui.starting = FALSE; /* No GUI yet (maybe later) */
210 gui_mch_prepare(argc, argv);
211}
212
213/*
214 * Try initializing the GUI and check if it can be started.
215 * Used from main() to check early if "vim -g" can start the GUI.
216 * Used from gui_init() to prepare for starting the GUI.
217 * Returns FAIL or OK.
218 */
219 int
220gui_init_check()
221{
222 static int result = MAYBE;
223
224 if (result != MAYBE)
225 {
226 if (result == FAIL)
227 EMSG(_("E229: Cannot start the GUI"));
228 return result;
229 }
230
231 gui.shell_created = FALSE;
232 gui.dying = FALSE;
233 gui.in_focus = TRUE; /* so the guicursor setting works */
234 gui.dragged_sb = SBAR_NONE;
235 gui.dragged_wp = NULL;
236 gui.pointer_hidden = FALSE;
237 gui.col = 0;
238 gui.row = 0;
239 gui.num_cols = Columns;
240 gui.num_rows = Rows;
241
242 gui.cursor_is_valid = FALSE;
243 gui.scroll_region_top = 0;
244 gui.scroll_region_bot = Rows - 1;
245 gui.scroll_region_left = 0;
246 gui.scroll_region_right = Columns - 1;
247 gui.highlight_mask = HL_NORMAL;
248 gui.char_width = 1;
249 gui.char_height = 1;
250 gui.char_ascent = 0;
251 gui.border_width = 0;
252
253 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200254#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 gui.bold_font = NOFONT;
256 gui.ital_font = NOFONT;
257 gui.boldital_font = NOFONT;
258# ifdef FEAT_XFONTSET
259 gui.fontset = NOFONTSET;
260# endif
261#endif
262
263#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200264# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265# ifdef FONTSET_ALWAYS
266 gui.menu_fontset = NOFONTSET;
267# else
268 gui.menu_font = NOFONT;
269# endif
270# endif
271 gui.menu_is_active = TRUE; /* default: include menu */
272# ifndef FEAT_GUI_GTK
273 gui.menu_height = MENU_DEFAULT_HEIGHT;
274 gui.menu_width = 0;
275# endif
276#endif
277#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
278 gui.toolbar_height = 0;
279#endif
280#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
281 gui.footer_height = 0;
282#endif
283#ifdef FEAT_BEVAL_TIP
284 gui.tooltip_fontset = NOFONTSET;
285#endif
286
287 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
288 gui.prev_wrap = -1;
289
290#ifdef ALWAYS_USE_GUI
291 result = OK;
292#else
293 result = gui_mch_init_check();
294#endif
295 return result;
296}
297
298/*
299 * This is the call which starts the GUI.
300 */
301 void
302gui_init()
303{
304 win_T *wp;
305 static int recursive = 0;
306
307 /*
308 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
309 * function will then be executed at the first call, the rest by the
310 * recursive call. This allow the shell to be opened halfway reading a
311 * gvimrc file.
312 */
313 if (!recursive)
314 {
315 ++recursive;
316
317 clip_init(TRUE);
318
319 /* If can't initialize, don't try doing the rest */
320 if (gui_init_check() == FAIL)
321 {
322 --recursive;
323 clip_init(FALSE);
324 return;
325 }
326
327 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000328 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
329 * breaks the Paste toolbar button.
330 */
331 set_option_value((char_u *)"paste", 0L, NULL, 0);
332
333 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 * Set up system-wide default menus.
335 */
336#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
337 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
338 {
339 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000340 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 sys_menu = FALSE;
342 }
343#endif
344
345 /*
346 * Switch on the mouse by default, unless the user changed it already.
347 * This can then be changed in the .gvimrc.
348 */
349 if (!option_was_set((char_u *)"mouse"))
350 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000351 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
353 /*
354 * If -U option given, use only the initializations from that file and
355 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
356 */
357 if (use_gvimrc != NULL)
358 {
359 if (STRCMP(use_gvimrc, "NONE") != 0
360 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000361 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
363 }
364 else
365 {
366 /*
367 * Get system wide defaults for gvim, only when file name defined.
368 */
369#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000370 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#endif
372
373 /*
374 * Try to read GUI initialization commands from the following
375 * places:
376 * - environment variable GVIMINIT
377 * - the user gvimrc file (~/.gvimrc)
378 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
379 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
380 * The first that exists is used, the rest is ignored.
381 */
382 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
384 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
387 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#endif
389 )
390 {
391#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394 }
395
396 /*
397 * Read initialization commands from ".gvimrc" in current
398 * directory. This is only done if the 'exrc' option is set.
399 * Because of security reasons we disallow shell and write
400 * commands now, except for unix if the file is owned by the user
401 * or 'secure' option has been reset in environment of global
402 * ".gvimrc".
403 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
404 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
405 */
406 if (p_exrc)
407 {
408#ifdef UNIX
409 {
410 struct stat s;
411
412 /* if ".gvimrc" file is not owned by user, set 'secure'
413 * mode */
414 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
415 secure = p_secure;
416 }
417#else
418 secure = p_secure;
419#endif
420
421 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
422 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
423#ifdef SYS_GVIMRC_FILE
424 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
425 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
426#endif
427#ifdef USR_GVIMRC_FILE2
428 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
429 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
430#endif
431#ifdef USR_GVIMRC_FILE3
432 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
433 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
434#endif
435 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000436 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
438 if (secure == 2)
439 need_wait_return = TRUE;
440 secure = 0;
441 }
442 }
443
444 if (need_wait_return || msg_didany)
445 wait_return(TRUE);
446
447 --recursive;
448 }
449
450 /* If recursive call opened the shell, return here from the first call */
451 if (gui.in_use)
452 return;
453
454 /*
455 * Create the GUI shell.
456 */
457 gui.in_use = TRUE; /* Must be set after menus have been set up */
458 if (gui_mch_init() == FAIL)
459 goto error;
460
461 /* Avoid a delay for an error message that was printed in the terminal
462 * where Vim was started. */
463 emsg_on_display = FALSE;
464 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000465 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 need_wait_return = FALSE;
467 msg_didany = FALSE;
468
469 /*
470 * Check validity of any generic resources that may have been loaded.
471 */
472 if (gui.border_width < 0)
473 gui.border_width = 0;
474
475 /*
476 * Set up the fonts. First use a font specified with "-fn" or "-font".
477 */
478 if (font_argument != NULL)
479 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
480 if (
481#ifdef FEAT_XFONTSET
482 (*p_guifontset == NUL
483 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
484#endif
485 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
486 : p_guifont, FALSE) == FAIL)
487 {
488 EMSG(_("E665: Cannot start GUI, no valid font found"));
489 goto error2;
490 }
491#ifdef FEAT_MBYTE
492 if (gui_get_wide_font() == FAIL)
493 EMSG(_("E231: 'guifontwide' invalid"));
494#endif
495
496 gui.num_cols = Columns;
497 gui.num_rows = Rows;
498 gui_reset_scroll_region();
499
500 /* Create initial scrollbars */
501 FOR_ALL_WINDOWS(wp)
502 {
503 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
504 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
505 }
506 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
507
508#ifdef FEAT_MENU
509 gui_create_initial_menus(root_menu);
510#endif
511#ifdef FEAT_SUN_WORKSHOP
512 if (usingSunWorkShop)
513 workshop_init();
514#endif
515#ifdef FEAT_SIGN_ICONS
516 sign_gui_started();
517#endif
518
519 /* Configure the desired menu and scrollbars */
520 gui_init_which_components(NULL);
521
522 /* All components of the GUI have been created now */
523 gui.shell_created = TRUE;
524
525#ifndef FEAT_GUI_GTK
526 /* Set the shell size, adjusted for the screen size. For GTK this only
527 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000528 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529#endif
530#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
531 /* Need to set the size of the menubar after all the menus have been
532 * created. */
533 gui_mch_compute_menu_height((Widget)0);
534#endif
535
536 /*
537 * Actually open the GUI shell.
538 */
539 if (gui_mch_open() != FAIL)
540 {
541#ifdef FEAT_TITLE
542 maketitle();
543 resettitle();
544#endif
545 init_gui_options();
546#ifdef FEAT_ARABIC
547 /* Our GUI can't do bidi. */
548 p_tbidi = FALSE;
549#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000550#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 /* Give GTK+ a chance to put all widget's into place. */
552 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000553
554# ifdef FEAT_MENU
555 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
556 * It was still there to make F10 work. */
557 if (vim_strchr(p_go, GO_MENUS) == NULL)
558 {
559 --gui.starting;
560 gui_mch_enable_menu(FALSE);
561 ++gui.starting;
562 gui_mch_update();
563 }
564# endif
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000567 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000569 /* When 'lines' was set while starting up the topframe may have to be
570 * resized. */
571 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000572
573#ifdef FEAT_BEVAL
574 /* Always create the Balloon Evaluation area, but disable it when
575 * 'ballooneval' is off */
576# ifdef FEAT_GUI_GTK
577 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
578 &general_beval_cb, NULL);
579# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000580# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000581 {
582 extern Widget textArea;
583 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000584 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000585 }
586# else
587# ifdef FEAT_GUI_W32
588 balloonEval = gui_mch_create_beval_area(NULL, NULL,
589 &general_beval_cb, NULL);
590# endif
591# endif
592# endif
593 if (!p_beval)
594 gui_mch_disable_beval_area(balloonEval);
595#endif
596
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
598 if (!im_xim_isvalid_imactivate())
599 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
600#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000601 /* When 'cmdheight' was set during startup it may not have taken
602 * effect yet. */
603 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000604 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 return;
607 }
608
609error2:
610#ifdef FEAT_GUI_X11
611 /* undo gui_mch_init() */
612 gui_mch_uninit();
613#endif
614
615error:
616 gui.in_use = FALSE;
617 clip_init(FALSE);
618}
619
620
621 void
622gui_exit(rc)
623 int rc;
624{
625#ifndef __BEOS__
626 /* don't free the fonts, it leads to a BUS error
627 * richard@whitequeen.com Jul 99 */
628 free_highlight_fonts();
629#endif
630 gui.in_use = FALSE;
631 gui_mch_exit(rc);
632}
633
Bram Moolenaar9372a112005-12-06 19:59:18 +0000634#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000636# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637/*
638 * Called when the GUI shell is closed by the user. If there are no changed
639 * files Vim exits, otherwise there will be a dialog to ask the user what to
640 * do.
641 * When this function returns, Vim should NOT exit!
642 */
643 void
644gui_shell_closed()
645{
646 cmdmod_T save_cmdmod;
647
648 save_cmdmod = cmdmod;
649
650 /* Only exit when there are no changed files */
651 exiting = TRUE;
652# ifdef FEAT_BROWSE
653 cmdmod.browse = TRUE;
654# endif
655# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
656 cmdmod.confirm = TRUE;
657# endif
658 /* If there are changed buffers, present the user with a dialog if
659 * possible, otherwise give an error message. */
660 if (!check_changed_any(FALSE))
661 getout(0);
662
663 exiting = FALSE;
664 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000665 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667#endif
668
669/*
670 * Set the font. "font_list" is a a comma separated list of font names. The
671 * first font name that works is used. If none is found, use the default
672 * font.
673 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
674 * Return OK when able to set the font. When it failed FAIL is returned and
675 * the fonts are unchanged.
676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 int
678gui_init_font(font_list, fontset)
679 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000680 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682#define FONTLEN 320
683 char_u font_name[FONTLEN];
684 int font_list_empty = FALSE;
685 int ret = FAIL;
686
687 if (!gui.in_use)
688 return FAIL;
689
690 font_name[0] = NUL;
691 if (*font_list == NUL)
692 font_list_empty = TRUE;
693 else
694 {
695#ifdef FEAT_XFONTSET
696 /* When using a fontset, the whole list of fonts is one name. */
697 if (fontset)
698 ret = gui_mch_init_font(font_list, TRUE);
699 else
700#endif
701 while (*font_list != NUL)
702 {
703 /* Isolate one comma separated font name. */
704 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
705
706 /* Careful!!! The Win32 version of gui_mch_init_font(), when
707 * called with "*" will change p_guifont to the selected font
708 * name, which frees the old value. This makes font_list
709 * invalid. Thus when OK is returned here, font_list must no
710 * longer be used! */
711 if (gui_mch_init_font(font_name, FALSE) == OK)
712 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200713#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 /* If it's a Unicode font, try setting 'guifontwide' to a
715 * similar double-width font. */
716 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
717 && strstr((char *)font_name, "10646") != NULL)
718 set_guifontwide(font_name);
719#endif
720 ret = OK;
721 break;
722 }
723 }
724 }
725
726 if (ret != OK
727 && STRCMP(font_list, "*") != 0
728 && (font_list_empty || gui.norm_font == NOFONT))
729 {
730 /*
731 * Couldn't load any font in 'font_list', keep the current font if
732 * there is one. If 'font_list' is empty, or if there is no current
733 * font, tell gui_mch_init_font() to try to find a font we can load.
734 */
735 ret = gui_mch_init_font(NULL, FALSE);
736 }
737
738 if (ret == OK)
739 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200740#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 /* Set normal font as current font */
742# ifdef FEAT_XFONTSET
743 if (gui.fontset != NOFONTSET)
744 gui_mch_set_fontset(gui.fontset);
745 else
746# endif
747 gui_mch_set_font(gui.norm_font);
748#endif
749 gui_set_shellsize(FALSE,
750#ifdef MSWIN
751 TRUE
752#else
753 FALSE
754#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000755 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757
758 return ret;
759}
760
761#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200762# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763/*
764 * Try setting 'guifontwide' to a font twice as wide as "name".
765 */
766 static void
767set_guifontwide(name)
768 char_u *name;
769{
770 int i = 0;
771 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
772 char_u *wp = NULL;
773 char_u *p;
774 GuiFont font;
775
776 wp = wide_name;
777 for (p = name; *p != NUL; ++p)
778 {
779 *wp++ = *p;
780 if (*p == '-')
781 {
782 ++i;
783 if (i == 6) /* font type: change "--" to "-*-" */
784 {
785 if (p[1] == '-')
786 *wp++ = '*';
787 }
788 else if (i == 12) /* found the width */
789 {
790 ++p;
791 i = getdigits(&p);
792 if (i != 0)
793 {
794 /* Double the width specification. */
795 sprintf((char *)wp, "%d%s", i * 2, p);
796 font = gui_mch_get_font(wide_name, FALSE);
797 if (font != NOFONT)
798 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000799 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 gui.wide_font = font;
801 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000802 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
804 }
805 break;
806 }
807 }
808 }
809}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200810# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812/*
813 * Get the font for 'guifontwide'.
814 * Return FAIL for an invalid font name.
815 */
816 int
817gui_get_wide_font()
818{
819 GuiFont font = NOFONT;
820 char_u font_name[FONTLEN];
821 char_u *p;
822
823 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
824 return OK; /* Will give an error message later. */
825
826 if (p_guifontwide != NULL && *p_guifontwide != NUL)
827 {
828 for (p = p_guifontwide; *p != NUL; )
829 {
830 /* Isolate one comma separated font name. */
831 (void)copy_option_part(&p, font_name, FONTLEN, ",");
832 font = gui_mch_get_font(font_name, FALSE);
833 if (font != NOFONT)
834 break;
835 }
836 if (font == NOFONT)
837 return FAIL;
838 }
839
840 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200841#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
843 if (font != NOFONT && gui.norm_font != NOFONT
844 && pango_font_description_equal(font, gui.norm_font))
845 {
846 gui.wide_font = NOFONT;
847 gui_mch_free_font(font);
848 }
849 else
850#endif
851 gui.wide_font = font;
852 return OK;
853}
854#endif
855
856 void
857gui_set_cursor(row, col)
858 int row;
859 int col;
860{
861 gui.row = row;
862 gui.col = col;
863}
864
865/*
866 * gui_check_pos - check if the cursor is on the screen.
867 */
868 static void
869gui_check_pos()
870{
871 if (gui.row >= screen_Rows)
872 gui.row = screen_Rows - 1;
873 if (gui.col >= screen_Columns)
874 gui.col = screen_Columns - 1;
875 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
876 gui.cursor_is_valid = FALSE;
877}
878
879/*
880 * Redraw the cursor if necessary or when forced.
881 * Careful: The contents of ScreenLines[] must match what is on the screen,
882 * otherwise this goes wrong. May need to call out_flush() first.
883 */
884 void
885gui_update_cursor(force, clear_selection)
886 int force; /* when TRUE, update even when not moved */
887 int clear_selection;/* clear selection under cursor */
888{
889 int cur_width = 0;
890 int cur_height = 0;
891 int old_hl_mask;
892 int idx;
893 int id;
894 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
895 int cattr; /* cursor attributes */
896 int attr;
897 attrentry_T *aep = NULL;
898
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +0000899 /* Don't update the cursor when halfway busy scrolling or the screen size
900 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
901 if (!can_update_cursor || screen_Columns != gui.num_cols
902 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return;
904
905 gui_check_pos();
906 if (!gui.cursor_is_valid || force
907 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
908 {
909 gui_undraw_cursor();
910 if (gui.row < 0)
911 return;
912#ifdef USE_IM_CONTROL
913 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
914 im_set_position(gui.row, gui.col);
915#endif
916 gui.cursor_row = gui.row;
917 gui.cursor_col = gui.col;
918
919 /* Only write to the screen after ScreenLines[] has been initialized */
920 if (!screen_cleared || ScreenLines == NULL)
921 return;
922
923 /* Clear the selection if we are about to write over it */
924 if (clear_selection)
925 clip_may_clear_selection(gui.row, gui.row);
926 /* Check that the cursor is inside the shell (resizing may have made
927 * it invalid) */
928 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
929 return;
930
931 gui.cursor_is_valid = TRUE;
932
933 /*
934 * How the cursor is drawn depends on the current mode.
935 */
936 idx = get_shape_idx(FALSE);
937 if (State & LANGMAP)
938 id = shape_table[idx].id_lm;
939 else
940 id = shape_table[idx].id;
941
942 /* get the colors and attributes for the cursor. Default is inverted */
943 cfg = INVALCOLOR;
944 cbg = INVALCOLOR;
945 cattr = HL_INVERSE;
946 gui_mch_set_blinking(shape_table[idx].blinkwait,
947 shape_table[idx].blinkon,
948 shape_table[idx].blinkoff);
949 if (id > 0)
950 {
951 cattr = syn_id2colors(id, &cfg, &cbg);
952#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
953 {
954 static int iid;
955 guicolor_T fg, bg;
956
Bram Moolenaarc236c162008-07-13 17:41:49 +0000957 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200958# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000959 preedit_get_status()
960# else
961 im_get_status()
962# endif
963 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
965 iid = syn_name2id((char_u *)"CursorIM");
966 if (iid > 0)
967 {
968 syn_id2colors(iid, &fg, &bg);
969 if (bg != INVALCOLOR)
970 cbg = bg;
971 if (fg != INVALCOLOR)
972 cfg = fg;
973 }
974 }
975 }
976#endif
977 }
978
979 /*
980 * Get the attributes for the character under the cursor.
981 * When no cursor color was given, use the character color.
982 */
983 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
984 if (attr > HL_ALL)
985 aep = syn_gui_attr2entry(attr);
986 if (aep != NULL)
987 {
988 attr = aep->ae_attr;
989 if (cfg == INVALCOLOR)
990 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
991 : aep->ae_u.gui.fg_color);
992 if (cbg == INVALCOLOR)
993 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
994 : aep->ae_u.gui.bg_color);
995 }
996 if (cfg == INVALCOLOR)
997 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
998 if (cbg == INVALCOLOR)
999 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1000
1001#ifdef FEAT_XIM
1002 if (aep != NULL)
1003 {
1004 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1005 : aep->ae_u.gui.bg_color);
1006 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1007 : aep->ae_u.gui.fg_color);
1008 if (xim_bg_color == INVALCOLOR)
1009 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1010 : gui.back_pixel;
1011 if (xim_fg_color == INVALCOLOR)
1012 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1013 : gui.norm_pixel;
1014 }
1015 else
1016 {
1017 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1018 : gui.back_pixel;
1019 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1020 : gui.norm_pixel;
1021 }
1022#endif
1023
1024 attr &= ~HL_INVERSE;
1025 if (cattr & HL_INVERSE)
1026 {
1027 cc = cbg;
1028 cbg = cfg;
1029 cfg = cc;
1030 }
1031 cattr &= ~HL_INVERSE;
1032
1033 /*
1034 * When we don't have window focus, draw a hollow cursor.
1035 */
1036 if (!gui.in_focus)
1037 {
1038 gui_mch_draw_hollow_cursor(cbg);
1039 return;
1040 }
1041
1042 old_hl_mask = gui.highlight_mask;
1043 if (shape_table[idx].shape == SHAPE_BLOCK
1044#ifdef FEAT_HANGULIN
1045 || composing_hangul
1046#endif
1047 )
1048 {
1049 /*
1050 * Draw the text character with the cursor colors. Use the
1051 * character attributes plus the cursor attributes.
1052 */
1053 gui.highlight_mask = (cattr | attr);
1054#ifdef FEAT_HANGULIN
1055 if (composing_hangul)
1056 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1057 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1058 else
1059#endif
1060 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1061 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1062 }
1063 else
1064 {
1065#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1066 int col_off = FALSE;
1067#endif
1068 /*
1069 * First draw the partial cursor, then overwrite with the text
1070 * character, using a transparent background.
1071 */
1072 if (shape_table[idx].shape == SHAPE_VER)
1073 {
1074 cur_height = gui.char_height;
1075 cur_width = (gui.char_width * shape_table[idx].percentage
1076 + 99) / 100;
1077 }
1078 else
1079 {
1080 cur_height = (gui.char_height * shape_table[idx].percentage
1081 + 99) / 100;
1082 cur_width = gui.char_width;
1083 }
1084#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001085 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1086 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
1088 /* Double wide character. */
1089 if (shape_table[idx].shape != SHAPE_VER)
1090 cur_width += gui.char_width;
1091# ifdef FEAT_RIGHTLEFT
1092 if (CURSOR_BAR_RIGHT)
1093 {
1094 /* gui.col points to the left halve of the character but
1095 * the vertical line needs to be on the right halve.
1096 * A double-wide horizontal line is also drawn from the
1097 * right halve in gui_mch_draw_part_cursor(). */
1098 col_off = TRUE;
1099 ++gui.col;
1100 }
1101# endif
1102 }
1103#endif
1104 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1105#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1106 if (col_off)
1107 --gui.col;
1108#endif
1109
1110#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1111 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1112 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1113 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1114 (guicolor_T)0, (guicolor_T)0, 0);
1115#endif
1116 }
1117 gui.highlight_mask = old_hl_mask;
1118 }
1119}
1120
1121#if defined(FEAT_MENU) || defined(PROTO)
1122 void
1123gui_position_menu()
1124{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001125# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (gui.menu_is_active && gui.in_use)
1127 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1128# endif
1129}
1130#endif
1131
1132/*
1133 * Position the various GUI components (text area, menu). The vertical
1134 * scrollbars are NOT handled here. See gui_update_scrollbars().
1135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 static void
1137gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001138 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 int text_area_x;
1141 int text_area_y;
1142 int text_area_width;
1143 int text_area_height;
1144
1145 /* avoid that moving components around generates events */
1146 ++hold_gui_events;
1147
1148 text_area_x = 0;
1149 if (gui.which_scrollbars[SBAR_LEFT])
1150 text_area_x += gui.scrollbar_width;
1151
1152 text_area_y = 0;
1153#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1154 gui.menu_width = total_width;
1155 if (gui.menu_is_active)
1156 text_area_y += gui.menu_height;
1157#endif
1158#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1159 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1160 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1161#endif
1162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001163# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001164 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001165 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001166 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001167#endif
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1170 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1171 {
1172# ifdef FEAT_GUI_ATHENA
1173 gui_mch_set_toolbar_pos(0, text_area_y,
1174 gui.menu_width, gui.toolbar_height);
1175# endif
1176 text_area_y += gui.toolbar_height;
1177 }
1178#endif
1179
1180 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1181 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1182
1183 gui_mch_set_text_area_pos(text_area_x,
1184 text_area_y,
1185 text_area_width,
1186 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001187#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 + xim_get_status_area_height()
1189#endif
1190 );
1191#ifdef FEAT_MENU
1192 gui_position_menu();
1193#endif
1194 if (gui.which_scrollbars[SBAR_BOTTOM])
1195 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1196 text_area_x,
1197 text_area_y + text_area_height,
1198 text_area_width,
1199 gui.scrollbar_height);
1200 gui.left_sbar_x = 0;
1201 gui.right_sbar_x = text_area_x + text_area_width;
1202
1203 --hold_gui_events;
1204}
1205
Bram Moolenaar02743632005-07-25 20:42:36 +00001206/*
1207 * Get the width of the widgets and decorations to the side of the text area.
1208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 int
1210gui_get_base_width()
1211{
1212 int base_width;
1213
1214 base_width = 2 * gui.border_offset;
1215 if (gui.which_scrollbars[SBAR_LEFT])
1216 base_width += gui.scrollbar_width;
1217 if (gui.which_scrollbars[SBAR_RIGHT])
1218 base_width += gui.scrollbar_width;
1219 return base_width;
1220}
1221
Bram Moolenaar02743632005-07-25 20:42:36 +00001222/*
1223 * Get the height of the widgets and decorations above and below the text area.
1224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 int
1226gui_get_base_height()
1227{
1228 int base_height;
1229
1230 base_height = 2 * gui.border_offset;
1231 if (gui.which_scrollbars[SBAR_BOTTOM])
1232 base_height += gui.scrollbar_height;
1233#ifdef FEAT_GUI_GTK
1234 /* We can't take the sizes properly into account until anything is
1235 * realized. Therefore we recalculate all the values here just before
1236 * setting the size. (--mdcki) */
1237#else
1238# ifdef FEAT_MENU
1239 if (gui.menu_is_active)
1240 base_height += gui.menu_height;
1241# endif
1242# ifdef FEAT_TOOLBAR
1243 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1244# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1245 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1246# else
1247 base_height += gui.toolbar_height;
1248# endif
1249# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001250# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1251 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001252 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001253 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255# ifdef FEAT_FOOTER
1256 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1257 base_height += gui.footer_height;
1258# endif
1259# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1260 base_height += gui_mch_text_area_extra_height();
1261# endif
1262#endif
1263 return base_height;
1264}
1265
1266/*
1267 * Should be called after the GUI shell has been resized. Its arguments are
1268 * the new width and height of the shell in pixels.
1269 */
1270 void
1271gui_resize_shell(pixel_width, pixel_height)
1272 int pixel_width;
1273 int pixel_height;
1274{
1275 static int busy = FALSE;
1276
1277 if (!gui.shell_created) /* ignore when still initializing */
1278 return;
1279
1280 /*
1281 * Can't resize the screen while it is being redrawn. Remember the new
1282 * size and handle it later.
1283 */
1284 if (updating_screen || busy)
1285 {
1286 new_pixel_width = pixel_width;
1287 new_pixel_height = pixel_height;
1288 return;
1289 }
1290
1291again:
1292 busy = TRUE;
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 /* Flush pending output before redrawing */
1295 out_flush();
1296
1297 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001298 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 gui_position_components(pixel_width);
1301
1302 gui_reset_scroll_region();
1303 /*
1304 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1305 * at the last line here (why does it have to be one row too low?).
1306 */
1307 if (State == ASKMORE || State == CONFIRM)
1308 gui.row = gui.num_rows;
1309
1310 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1311 * the safe side. */
1312 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1313 || gui.num_rows != Rows || gui.num_cols != Columns)
1314 shell_resized();
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 gui_update_scrollbars(TRUE);
1317 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001318#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 xim_set_status_area();
1320#endif
1321
1322 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 /*
1325 * We could have been called again while redrawing the screen.
1326 * Need to do it all again with the latest size then.
1327 */
1328 if (new_pixel_height)
1329 {
1330 pixel_width = new_pixel_width;
1331 pixel_height = new_pixel_height;
1332 new_pixel_width = 0;
1333 new_pixel_height = 0;
1334 goto again;
1335 }
1336}
1337
1338/*
1339 * Check if gui_resize_shell() must be called.
1340 */
1341 void
1342gui_may_resize_shell()
1343{
1344 int h, w;
1345
1346 if (new_pixel_height)
1347 {
1348 /* careful: gui_resize_shell() may postpone the resize again if we
1349 * were called indirectly by it */
1350 w = new_pixel_width;
1351 h = new_pixel_height;
1352 new_pixel_width = 0;
1353 new_pixel_height = 0;
1354 gui_resize_shell(w, h);
1355 }
1356}
1357
1358 int
1359gui_get_shellsize()
1360{
1361 Rows = gui.num_rows;
1362 Columns = gui.num_cols;
1363 return OK;
1364}
1365
1366/*
1367 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001368 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1369 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001372gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001373 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001375 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 int base_width;
1378 int base_height;
1379 int width;
1380 int height;
1381 int min_width;
1382 int min_height;
1383 int screen_w;
1384 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001385#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001386 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001387 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001388#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001389 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
1391 if (!gui.shell_created)
1392 return;
1393
1394#ifdef MSWIN
1395 /* If not setting to a user specified size and maximized, calculate the
1396 * number of characters that fit in the maximized window. */
1397 if (!mustset && gui_mch_maximized())
1398 {
1399 gui_mch_newfont();
1400 return;
1401 }
1402#endif
1403
1404 base_width = gui_get_base_width();
1405 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001406 if (fit_to_display)
1407 /* Remember the original window position. */
1408 gui_mch_get_winpos(&x, &y);
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#ifdef USE_SUN_WORKSHOP
1411 if (!mustset && usingSunWorkShop
1412 && workshop_get_width_height(&width, &height))
1413 {
1414 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1415 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1416 }
1417 else
1418#endif
1419 {
1420 width = Columns * gui.char_width + base_width;
1421 height = Rows * gui.char_height + base_height;
1422 }
1423
1424 if (fit_to_display)
1425 {
1426 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001427 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
1429 Columns = (screen_w - base_width) / gui.char_width;
1430 if (Columns < MIN_COLUMNS)
1431 Columns = MIN_COLUMNS;
1432 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001433#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001434 ++did_adjust;
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001437 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
1439 Rows = (screen_h - base_height) / gui.char_height;
1440 check_shellsize();
1441 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001442#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001443 ++did_adjust;
1444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001446#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001447 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1448 && height + gui.char_height >= screen_h))
1449 /* don't unmaximize if at maximum size */
1450 un_maximize = FALSE;
1451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453 gui.num_cols = Columns;
1454 gui.num_rows = Rows;
1455
1456 min_width = base_width + MIN_COLUMNS * gui.char_width;
1457 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001458#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001459 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001460#endif
1461
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001462#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001463 if (un_maximize)
1464 {
1465 /* If the window size is smaller than the screen unmaximize the
1466 * window, otherwise resizing won't work. */
1467 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1468 if ((width + gui.char_width < screen_w
1469 || height + gui.char_height * 2 < screen_h)
1470 && gui_mch_maximized())
1471 gui_mch_unmaximize();
1472 }
1473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001476 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001478 if (fit_to_display && x >= 0 && y >= 0)
1479 {
1480 /* Some window managers put the Vim window left of/above the screen.
1481 * Only change the position if it wasn't already negative before
1482 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 gui_mch_update();
1484 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1485 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1486 }
1487
1488 gui_position_components(width);
1489 gui_update_scrollbars(TRUE);
1490 gui_reset_scroll_region();
1491}
1492
1493/*
1494 * Called when Rows and/or Columns has changed.
1495 */
1496 void
1497gui_new_shellsize()
1498{
1499 gui_reset_scroll_region();
1500}
1501
1502/*
1503 * Make scroll region cover whole screen.
1504 */
1505 void
1506gui_reset_scroll_region()
1507{
1508 gui.scroll_region_top = 0;
1509 gui.scroll_region_bot = gui.num_rows - 1;
1510 gui.scroll_region_left = 0;
1511 gui.scroll_region_right = gui.num_cols - 1;
1512}
1513
1514 void
1515gui_start_highlight(mask)
1516 int mask;
1517{
1518 if (mask > HL_ALL) /* highlight code */
1519 gui.highlight_mask = mask;
1520 else /* mask */
1521 gui.highlight_mask |= mask;
1522}
1523
1524 void
1525gui_stop_highlight(mask)
1526 int mask;
1527{
1528 if (mask > HL_ALL) /* highlight code */
1529 gui.highlight_mask = HL_NORMAL;
1530 else /* mask */
1531 gui.highlight_mask &= ~mask;
1532}
1533
1534/*
1535 * Clear a rectangular region of the screen from text pos (row1, col1) to
1536 * (row2, col2) inclusive.
1537 */
1538 void
1539gui_clear_block(row1, col1, row2, col2)
1540 int row1;
1541 int col1;
1542 int row2;
1543 int col2;
1544{
1545 /* Clear the selection if we are about to write over it */
1546 clip_may_clear_selection(row1, row2);
1547
1548 gui_mch_clear_block(row1, col1, row2, col2);
1549
1550 /* Invalidate cursor if it was in this block */
1551 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1552 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1553 gui.cursor_is_valid = FALSE;
1554}
1555
1556/*
1557 * Write code to update the cursor later. This avoids the need to flush the
1558 * output buffer before calling gui_update_cursor().
1559 */
1560 void
1561gui_update_cursor_later()
1562{
1563 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1564}
1565
1566 void
1567gui_write(s, len)
1568 char_u *s;
1569 int len;
1570{
1571 char_u *p;
1572 int arg1 = 0, arg2 = 0;
1573 /* this doesn't make sense, disabled until someone can explain why it
1574 * would be needed */
1575#if 0 && (defined(RISCOS) || defined(WIN16))
1576 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1577 our redraw speed will suffer */
1578#else
1579 int force_cursor = FALSE; /* force cursor update */
1580#endif
1581 int force_scrollbar = FALSE;
1582 static win_T *old_curwin = NULL;
1583
1584/* #define DEBUG_GUI_WRITE */
1585#ifdef DEBUG_GUI_WRITE
1586 {
1587 int i;
1588 char_u *str;
1589
1590 printf("gui_write(%d):\n ", len);
1591 for (i = 0; i < len; i++)
1592 if (s[i] == ESC)
1593 {
1594 if (i != 0)
1595 printf("\n ");
1596 printf("<ESC>");
1597 }
1598 else
1599 {
1600 str = transchar_byte(s[i]);
1601 if (str[0] && str[1])
1602 printf("<%s>", (char *)str);
1603 else
1604 printf("%s", (char *)str);
1605 }
1606 printf("\n");
1607 }
1608#endif
1609 while (len)
1610 {
1611 if (s[0] == ESC && s[1] == '|')
1612 {
1613 p = s + 2;
1614 if (VIM_ISDIGIT(*p))
1615 {
1616 arg1 = getdigits(&p);
1617 if (p > s + len)
1618 break;
1619 if (*p == ';')
1620 {
1621 ++p;
1622 arg2 = getdigits(&p);
1623 if (p > s + len)
1624 break;
1625 }
1626 }
1627 switch (*p)
1628 {
1629 case 'C': /* Clear screen */
1630 clip_scroll_selection(9999);
1631 gui_mch_clear_all();
1632 gui.cursor_is_valid = FALSE;
1633 force_scrollbar = TRUE;
1634 break;
1635 case 'M': /* Move cursor */
1636 gui_set_cursor(arg1, arg2);
1637 break;
1638 case 's': /* force cursor (shape) update */
1639 force_cursor = TRUE;
1640 break;
1641 case 'R': /* Set scroll region */
1642 if (arg1 < arg2)
1643 {
1644 gui.scroll_region_top = arg1;
1645 gui.scroll_region_bot = arg2;
1646 }
1647 else
1648 {
1649 gui.scroll_region_top = arg2;
1650 gui.scroll_region_bot = arg1;
1651 }
1652 break;
1653#ifdef FEAT_VERTSPLIT
1654 case 'V': /* Set vertical scroll region */
1655 if (arg1 < arg2)
1656 {
1657 gui.scroll_region_left = arg1;
1658 gui.scroll_region_right = arg2;
1659 }
1660 else
1661 {
1662 gui.scroll_region_left = arg2;
1663 gui.scroll_region_right = arg1;
1664 }
1665 break;
1666#endif
1667 case 'd': /* Delete line */
1668 gui_delete_lines(gui.row, 1);
1669 break;
1670 case 'D': /* Delete lines */
1671 gui_delete_lines(gui.row, arg1);
1672 break;
1673 case 'i': /* Insert line */
1674 gui_insert_lines(gui.row, 1);
1675 break;
1676 case 'I': /* Insert lines */
1677 gui_insert_lines(gui.row, arg1);
1678 break;
1679 case '$': /* Clear to end-of-line */
1680 gui_clear_block(gui.row, gui.col, gui.row,
1681 (int)Columns - 1);
1682 break;
1683 case 'h': /* Turn on highlighting */
1684 gui_start_highlight(arg1);
1685 break;
1686 case 'H': /* Turn off highlighting */
1687 gui_stop_highlight(arg1);
1688 break;
1689 case 'f': /* flash the window (visual bell) */
1690 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1691 break;
1692 default:
1693 p = s + 1; /* Skip the ESC */
1694 break;
1695 }
1696 len -= (int)(++p - s);
1697 s = p;
1698 }
1699 else if (
1700#ifdef EBCDIC
1701 CtrlChar(s[0]) != 0 /* Ctrl character */
1702#else
1703 s[0] < 0x20 /* Ctrl character */
1704#endif
1705#ifdef FEAT_SIGN_ICONS
1706 && s[0] != SIGN_BYTE
1707# ifdef FEAT_NETBEANS_INTG
1708 && s[0] != MULTISIGN_BYTE
1709# endif
1710#endif
1711 )
1712 {
1713 if (s[0] == '\n') /* NL */
1714 {
1715 gui.col = 0;
1716 if (gui.row < gui.scroll_region_bot)
1717 gui.row++;
1718 else
1719 gui_delete_lines(gui.scroll_region_top, 1);
1720 }
1721 else if (s[0] == '\r') /* CR */
1722 {
1723 gui.col = 0;
1724 }
1725 else if (s[0] == '\b') /* Backspace */
1726 {
1727 if (gui.col)
1728 --gui.col;
1729 }
1730 else if (s[0] == Ctrl_L) /* cursor-right */
1731 {
1732 ++gui.col;
1733 }
1734 else if (s[0] == Ctrl_G) /* Beep */
1735 {
1736 gui_mch_beep();
1737 }
1738 /* Other Ctrl character: shouldn't happen! */
1739
1740 --len; /* Skip this char */
1741 ++s;
1742 }
1743 else
1744 {
1745 p = s;
1746 while (len > 0 && (
1747#ifdef EBCDIC
1748 CtrlChar(*p) == 0
1749#else
1750 *p >= 0x20
1751#endif
1752#ifdef FEAT_SIGN_ICONS
1753 || *p == SIGN_BYTE
1754# ifdef FEAT_NETBEANS_INTG
1755 || *p == MULTISIGN_BYTE
1756# endif
1757#endif
1758 ))
1759 {
1760 len--;
1761 p++;
1762 }
1763 gui_outstr(s, (int)(p - s));
1764 s = p;
1765 }
1766 }
1767
1768 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1769 * set). */
1770 if (force_cursor)
1771 gui_update_cursor(TRUE, TRUE);
1772
1773 /* When switching to another window the dragging must have stopped.
1774 * Required for GTK, dragged_sb isn't reset. */
1775 if (old_curwin != curwin)
1776 gui.dragged_sb = SBAR_NONE;
1777
1778 /* Update the scrollbars after clearing the screen or when switched
1779 * to another window.
1780 * Update the horizontal scrollbar always, it's difficult to check all
1781 * situations where it might change. */
1782 if (force_scrollbar || old_curwin != curwin)
1783 gui_update_scrollbars(force_scrollbar);
1784 else
1785 gui_update_horiz_scrollbar(FALSE);
1786 old_curwin = curwin;
1787
1788 /*
1789 * We need to make sure this is cleared since Athena doesn't tell us when
1790 * he is done dragging. Do the same for GTK.
1791 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001792#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 gui.dragged_sb = SBAR_NONE;
1794#endif
1795
1796 gui_mch_flush(); /* In case vim decides to take a nap */
1797}
1798
1799/*
1800 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1801 * produces wrong results. Call gui_dont_update_cursor() before that code and
1802 * gui_can_update_cursor() afterwards.
1803 */
1804 void
1805gui_dont_update_cursor()
1806{
1807 if (gui.in_use)
1808 {
1809 /* Undraw the cursor now, we probably can't do it after the change. */
1810 gui_undraw_cursor();
1811 can_update_cursor = FALSE;
1812 }
1813}
1814
1815 void
1816gui_can_update_cursor()
1817{
1818 can_update_cursor = TRUE;
1819 /* No need to update the cursor right now, there is always more output
1820 * after scrolling. */
1821}
1822
1823 static void
1824gui_outstr(s, len)
1825 char_u *s;
1826 int len;
1827{
1828 int this_len;
1829#ifdef FEAT_MBYTE
1830 int cells;
1831#endif
1832
1833 if (len == 0)
1834 return;
1835
1836 if (len < 0)
1837 len = (int)STRLEN(s);
1838
1839 while (len > 0)
1840 {
1841#ifdef FEAT_MBYTE
1842 if (has_mbyte)
1843 {
1844 /* Find out how many chars fit in the current line. */
1845 cells = 0;
1846 for (this_len = 0; this_len < len; )
1847 {
1848 cells += (*mb_ptr2cells)(s + this_len);
1849 if (gui.col + cells > Columns)
1850 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001851 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853 if (this_len > len)
1854 this_len = len; /* don't include following composing char */
1855 }
1856 else
1857#endif
1858 if (gui.col + len > Columns)
1859 this_len = Columns - gui.col;
1860 else
1861 this_len = len;
1862
1863 (void)gui_outstr_nowrap(s, this_len,
1864 0, (guicolor_T)0, (guicolor_T)0, 0);
1865 s += this_len;
1866 len -= this_len;
1867#ifdef FEAT_MBYTE
1868 /* fill up for a double-width char that doesn't fit. */
1869 if (len > 0 && gui.col < Columns)
1870 (void)gui_outstr_nowrap((char_u *)" ", 1,
1871 0, (guicolor_T)0, (guicolor_T)0, 0);
1872#endif
1873 /* The cursor may wrap to the next line. */
1874 if (gui.col >= Columns)
1875 {
1876 gui.col = 0;
1877 gui.row++;
1878 }
1879 }
1880}
1881
1882/*
1883 * Output one character (may be one or two display cells).
1884 * Caller must check for valid "off".
1885 * Returns FAIL or OK, just like gui_outstr_nowrap().
1886 */
1887 static int
1888gui_screenchar(off, flags, fg, bg, back)
1889 int off; /* Offset from start of screen */
1890 int flags;
1891 guicolor_T fg, bg; /* colors for cursor */
1892 int back; /* backup this many chars when using bold trick */
1893{
1894#ifdef FEAT_MBYTE
1895 char_u buf[MB_MAXBYTES + 1];
1896
1897 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1898 if (enc_utf8 && ScreenLines[off] == 0)
1899 return OK;
1900
1901 if (enc_utf8 && ScreenLinesUC[off] != 0)
1902 /* Draw UTF-8 multi-byte character. */
1903 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1904 flags, fg, bg, back);
1905
1906 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1907 {
1908 buf[0] = ScreenLines[off];
1909 buf[1] = ScreenLines2[off];
1910 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1911 }
1912
1913 /* Draw non-multi-byte character or DBCS character. */
1914 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001915 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 flags, fg, bg, back);
1917#else
1918 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1919#endif
1920}
1921
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001922#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
1924 * Output the string at the given screen position. This is used in place
1925 * of gui_screenchar() where possible because Pango needs as much context
1926 * as possible to work nicely. It's a lot faster as well.
1927 */
1928 static int
1929gui_screenstr(off, len, flags, fg, bg, back)
1930 int off; /* Offset from start of screen */
1931 int len; /* string length in screen cells */
1932 int flags;
1933 guicolor_T fg, bg; /* colors for cursor */
1934 int back; /* backup this many chars when using bold trick */
1935{
1936 char_u *buf;
1937 int outlen = 0;
1938 int i;
1939 int retval;
1940
1941 if (len <= 0) /* "cannot happen"? */
1942 return OK;
1943
1944 if (enc_utf8)
1945 {
1946 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1947 if (buf == NULL)
1948 return OK; /* not much we could do here... */
1949
1950 for (i = off; i < off + len; ++i)
1951 {
1952 if (ScreenLines[i] == 0)
1953 continue; /* skip second half of double-width char */
1954
1955 if (ScreenLinesUC[i] == 0)
1956 buf[outlen++] = ScreenLines[i];
1957 else
1958 outlen += utfc_char2bytes(i, buf + outlen);
1959 }
1960
1961 buf[outlen] = NUL; /* only to aid debugging */
1962 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1963 vim_free(buf);
1964
1965 return retval;
1966 }
1967 else if (enc_dbcs == DBCS_JPNU)
1968 {
1969 buf = alloc((unsigned)(len * 2 + 1));
1970 if (buf == NULL)
1971 return OK; /* not much we could do here... */
1972
1973 for (i = off; i < off + len; ++i)
1974 {
1975 buf[outlen++] = ScreenLines[i];
1976
1977 /* handle double-byte single-width char */
1978 if (ScreenLines[i] == 0x8e)
1979 buf[outlen++] = ScreenLines2[i];
1980 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1981 buf[outlen++] = ScreenLines[++i];
1982 }
1983
1984 buf[outlen] = NUL; /* only to aid debugging */
1985 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1986 vim_free(buf);
1987
1988 return retval;
1989 }
1990 else
1991 {
1992 return gui_outstr_nowrap(&ScreenLines[off], len,
1993 flags, fg, bg, back);
1994 }
1995}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001996#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998/*
1999 * Output the given string at the current cursor position. If the string is
2000 * too long to fit on the line, then it is truncated.
2001 * "flags":
2002 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2003 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002004 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 * background.
2006 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2007 * it.
2008 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2009 * FAIL (the caller should start drawing "back" chars back).
2010 */
2011 int
2012gui_outstr_nowrap(s, len, flags, fg, bg, back)
2013 char_u *s;
2014 int len;
2015 int flags;
2016 guicolor_T fg, bg; /* colors for cursor */
2017 int back; /* backup this many chars when using bold trick */
2018{
2019 long_u highlight_mask;
2020 long_u hl_mask_todo;
2021 guicolor_T fg_color;
2022 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002023 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002024#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 GuiFont font = NOFONT;
2026# ifdef FEAT_XFONTSET
2027 GuiFontset fontset = NOFONTSET;
2028# endif
2029#endif
2030 attrentry_T *aep = NULL;
2031 int draw_flags;
2032 int col = gui.col;
2033#ifdef FEAT_SIGN_ICONS
2034 int draw_sign = FALSE;
2035# ifdef FEAT_NETBEANS_INTG
2036 int multi_sign = FALSE;
2037# endif
2038#endif
2039
2040 if (len < 0)
2041 len = (int)STRLEN(s);
2042 if (len == 0)
2043 return OK;
2044
2045#ifdef FEAT_SIGN_ICONS
2046 if (*s == SIGN_BYTE
2047# ifdef FEAT_NETBEANS_INTG
2048 || *s == MULTISIGN_BYTE
2049# endif
2050 )
2051 {
2052# ifdef FEAT_NETBEANS_INTG
2053 if (*s == MULTISIGN_BYTE)
2054 multi_sign = TRUE;
2055# endif
2056 /* draw spaces instead */
2057 s = (char_u *)" ";
2058 if (len == 1 && col > 0)
2059 --col;
2060 len = 2;
2061 draw_sign = TRUE;
2062 highlight_mask = 0;
2063 }
2064 else
2065#endif
2066 if (gui.highlight_mask > HL_ALL)
2067 {
2068 aep = syn_gui_attr2entry(gui.highlight_mask);
2069 if (aep == NULL) /* highlighting not set */
2070 highlight_mask = 0;
2071 else
2072 highlight_mask = aep->ae_attr;
2073 }
2074 else
2075 highlight_mask = gui.highlight_mask;
2076 hl_mask_todo = highlight_mask;
2077
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002078#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 /* Set the font */
2080 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2081 font = aep->ae_u.gui.font;
2082# ifdef FEAT_XFONTSET
2083 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2084 fontset = aep->ae_u.gui.fontset;
2085# endif
2086 else
2087 {
2088# ifdef FEAT_XFONTSET
2089 if (gui.fontset != NOFONTSET)
2090 fontset = gui.fontset;
2091 else
2092# endif
2093 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2094 {
2095 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2096 {
2097 font = gui.boldital_font;
2098 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2099 }
2100 else if (gui.bold_font != NOFONT)
2101 {
2102 font = gui.bold_font;
2103 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2104 }
2105 else
2106 font = gui.norm_font;
2107 }
2108 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2109 {
2110 font = gui.ital_font;
2111 hl_mask_todo &= ~HL_ITALIC;
2112 }
2113 else
2114 font = gui.norm_font;
2115 }
2116# ifdef FEAT_XFONTSET
2117 if (fontset != NOFONTSET)
2118 gui_mch_set_fontset(fontset);
2119 else
2120# endif
2121 gui_mch_set_font(font);
2122#endif
2123
2124 draw_flags = 0;
2125
2126 /* Set the color */
2127 bg_color = gui.back_pixel;
2128 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2129 {
2130 draw_flags |= DRAW_CURSOR;
2131 fg_color = fg;
2132 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002133 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 else if (aep != NULL)
2136 {
2137 fg_color = aep->ae_u.gui.fg_color;
2138 if (fg_color == INVALCOLOR)
2139 fg_color = gui.norm_pixel;
2140 bg_color = aep->ae_u.gui.bg_color;
2141 if (bg_color == INVALCOLOR)
2142 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002143 sp_color = aep->ae_u.gui.sp_color;
2144 if (sp_color == INVALCOLOR)
2145 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002150 sp_color = fg_color;
2151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2154 {
2155#if defined(AMIGA) || defined(RISCOS)
2156 gui_mch_set_colors(bg_color, fg_color);
2157#else
2158 gui_mch_set_fg_color(bg_color);
2159 gui_mch_set_bg_color(fg_color);
2160#endif
2161 }
2162 else
2163 {
2164#if defined(AMIGA) || defined(RISCOS)
2165 gui_mch_set_colors(fg_color, bg_color);
2166#else
2167 gui_mch_set_fg_color(fg_color);
2168 gui_mch_set_bg_color(bg_color);
2169#endif
2170 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002171 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173 /* Clear the selection if we are about to write over it */
2174 if (!(flags & GUI_MON_NOCLEAR))
2175 clip_may_clear_selection(gui.row, gui.row);
2176
2177
2178#ifndef MSWIN16_FASTTEXT
2179 /* If there's no bold font, then fake it */
2180 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2181 draw_flags |= DRAW_BOLD;
2182#endif
2183
2184 /*
2185 * When drawing bold or italic characters the spill-over from the left
2186 * neighbor may be destroyed. Let the caller backup to start redrawing
2187 * just after a blank.
2188 */
2189 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2190 return FAIL;
2191
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002192#if defined(RISCOS) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 /* If there's no italic font, then fake it.
2194 * For GTK2, we don't need a different font for italic style. */
2195 if (hl_mask_todo & HL_ITALIC)
2196 draw_flags |= DRAW_ITALIC;
2197
2198 /* Do we underline the text? */
2199 if (hl_mask_todo & HL_UNDERLINE)
2200 draw_flags |= DRAW_UNDERL;
2201#else
2202 /* Do we underline the text? */
2203 if ((hl_mask_todo & HL_UNDERLINE)
2204# ifndef MSWIN16_FASTTEXT
2205 || (hl_mask_todo & HL_ITALIC)
2206# endif
2207 )
2208 draw_flags |= DRAW_UNDERL;
2209#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002210 /* Do we undercurl the text? */
2211 if (hl_mask_todo & HL_UNDERCURL)
2212 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002214 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (flags & GUI_MON_TRS_CURSOR)
2216 draw_flags |= DRAW_TRANSP;
2217
2218 /*
2219 * Draw the text.
2220 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002221#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /* The value returned is the length in display cells */
2223 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2224#else
2225# ifdef FEAT_MBYTE
2226 if (enc_utf8)
2227 {
2228 int start; /* index of bytes to be drawn */
2229 int cells; /* cellwidth of bytes to be drawn */
2230 int thislen; /* length of bytes to be drawin */
2231 int cn; /* cellwidth of current char */
2232 int i; /* index of current char */
2233 int c; /* current char value */
2234 int cl; /* byte length of current char */
2235 int comping; /* current char is composing */
2236 int scol = col; /* screen column */
2237 int dowide; /* use 'guifontwide' */
2238
2239 /* Break the string at a composing character, it has to be drawn on
2240 * top of the previous character. */
2241 start = 0;
2242 cells = 0;
2243 for (i = 0; i < len; i += cl)
2244 {
2245 c = utf_ptr2char(s + i);
2246 cn = utf_char2cells(c);
2247 if (cn > 1
2248# ifdef FEAT_XFONTSET
2249 && fontset == NOFONTSET
2250# endif
2251 && gui.wide_font != NOFONT)
2252 dowide = TRUE;
2253 else
2254 dowide = FALSE;
2255 comping = utf_iscomposing(c);
2256 if (!comping) /* count cells from non-composing chars */
2257 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002258 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (cl == 0) /* hit end of string */
2260 len = i + cl; /* len must be wrong "cannot happen" */
2261
2262 /* print the string so far if it's the last character or there is
2263 * a composing character. */
2264 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002265# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 || (cn > 1
2267# ifdef FEAT_XFONTSET
2268 /* No fontset: At least draw char after wide char at
2269 * right position. */
2270 && fontset == NOFONTSET
2271# endif
2272 )
2273# endif
2274 )
2275 {
2276 if (comping || dowide)
2277 thislen = i - start;
2278 else
2279 thislen = i - start + cl;
2280 if (thislen > 0)
2281 {
2282 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2283 draw_flags);
2284 start += thislen;
2285 }
2286 scol += cells;
2287 cells = 0;
2288 if (dowide)
2289 {
2290 gui_mch_set_font(gui.wide_font);
2291 gui_mch_draw_string(gui.row, scol - cn,
2292 s + start, cl, draw_flags);
2293 gui_mch_set_font(font);
2294 start += cl;
2295 }
2296
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002297# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002298 /* No fontset: draw a space to fill the gap after a wide char
2299 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2301# ifdef FEAT_XFONTSET
2302 && fontset == NOFONTSET
2303# endif
2304 && !dowide)
2305 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2306 1, draw_flags);
2307# endif
2308 }
2309 /* Draw a composing char on top of the previous char. */
2310 if (comping)
2311 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002312# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002313 /* Carbon ATSUI autodraws composing char over previous char */
2314 gui_mch_draw_string(gui.row, scol, s + i, cl,
2315 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002316# else
2317 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2318 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 start = i + cl;
2321 }
2322 }
2323 /* The stuff below assumes "len" is the length in screen columns. */
2324 len = scol - col;
2325 }
2326 else
2327# endif
2328 {
2329 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2330# ifdef FEAT_MBYTE
2331 if (enc_dbcs == DBCS_JPNU)
2332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 /* Get the length in display cells, this can be different from the
2334 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002335 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337# endif
2338 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002339#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
2341 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2342 gui.col = col + len;
2343
2344 /* May need to invert it when it's part of the selection. */
2345 if (flags & GUI_MON_NOCLEAR)
2346 clip_may_redraw_selection(gui.row, col, len);
2347
2348 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2349 {
2350 /* Invalidate the old physical cursor position if we wrote over it */
2351 if (gui.cursor_row == gui.row
2352 && gui.cursor_col >= col
2353 && gui.cursor_col < col + len)
2354 gui.cursor_is_valid = FALSE;
2355 }
2356
2357#ifdef FEAT_SIGN_ICONS
2358 if (draw_sign)
2359 /* Draw the sign on top of the spaces. */
2360 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002361# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_MOTIF) \
2362 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (multi_sign)
2364 netbeans_draw_multisign_indicator(gui.row);
2365# endif
2366#endif
2367
2368 return OK;
2369}
2370
2371/*
2372 * Un-draw the cursor. Actually this just redraws the character at the given
2373 * position. The character just before it too, for when it was in bold.
2374 */
2375 void
2376gui_undraw_cursor()
2377{
2378 if (gui.cursor_is_valid)
2379 {
2380#ifdef FEAT_HANGULIN
2381 if (composing_hangul
2382 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2383 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2384 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2385 gui.norm_pixel, gui.back_pixel, 0);
2386 else
2387 {
2388#endif
2389 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2390 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2391 && gui.cursor_col > 0)
2392 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2393 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2394#ifdef FEAT_HANGULIN
2395 if (composing_hangul)
2396 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2397 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2398 }
2399#endif
2400 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2401 * here in case it wasn't needed to undraw it. */
2402 gui.cursor_is_valid = FALSE;
2403 }
2404}
2405
2406 void
2407gui_redraw(x, y, w, h)
2408 int x;
2409 int y;
2410 int w;
2411 int h;
2412{
2413 int row1, col1, row2, col2;
2414
2415 row1 = Y_2_ROW(y);
2416 col1 = X_2_COL(x);
2417 row2 = Y_2_ROW(y + h - 1);
2418 col2 = X_2_COL(x + w - 1);
2419
2420 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2421
2422 /*
2423 * We may need to redraw the cursor, but don't take it upon us to change
2424 * its location after a scroll.
2425 * (maybe be more strict even and test col too?)
2426 * These things may be outside the update/clipping region and reality may
2427 * not reflect Vims internal ideas if these operations are clipped away.
2428 */
2429 if (gui.row == gui.cursor_row)
2430 gui_update_cursor(TRUE, TRUE);
2431}
2432
2433/*
2434 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2435 * from col1 to col2 (inclusive).
2436 * Return TRUE when the character before the first drawn character has
2437 * different attributes (may have to be redrawn too).
2438 */
2439 int
2440gui_redraw_block(row1, col1, row2, col2, flags)
2441 int row1;
2442 int col1;
2443 int row2;
2444 int col2;
2445 int flags; /* flags for gui_outstr_nowrap() */
2446{
2447 int old_row, old_col;
2448 long_u old_hl_mask;
2449 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002450 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 int idx, len;
2452 int back, nback;
2453 int retval = FALSE;
2454#ifdef FEAT_MBYTE
2455 int orig_col1, orig_col2;
2456#endif
2457
2458 /* Don't try to update when ScreenLines is not valid */
2459 if (!screen_cleared || ScreenLines == NULL)
2460 return retval;
2461
2462 /* Don't try to draw outside the shell! */
2463 /* Check everything, strange values may be caused by a big border width */
2464 col1 = check_col(col1);
2465 col2 = check_col(col2);
2466 row1 = check_row(row1);
2467 row2 = check_row(row2);
2468
2469 /* Remember where our cursor was */
2470 old_row = gui.row;
2471 old_col = gui.col;
2472 old_hl_mask = gui.highlight_mask;
2473#ifdef FEAT_MBYTE
2474 orig_col1 = col1;
2475 orig_col2 = col2;
2476#endif
2477
2478 for (gui.row = row1; gui.row <= row2; gui.row++)
2479 {
2480#ifdef FEAT_MBYTE
2481 /* When only half of a double-wide character is in the block, include
2482 * the other half. */
2483 col1 = orig_col1;
2484 col2 = orig_col2;
2485 off = LineOffset[gui.row];
2486 if (enc_dbcs != 0)
2487 {
2488 if (col1 > 0)
2489 col1 -= dbcs_screen_head_off(ScreenLines + off,
2490 ScreenLines + off + col1);
2491 col2 += dbcs_screen_tail_off(ScreenLines + off,
2492 ScreenLines + off + col2);
2493 }
2494 else if (enc_utf8)
2495 {
2496 if (ScreenLines[off + col1] == 0)
2497 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002498# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2500 ++col2;
2501# endif
2502 }
2503#endif
2504 gui.col = col1;
2505 off = LineOffset[gui.row] + gui.col;
2506 len = col2 - col1 + 1;
2507
2508 /* Find how many chars back this highlighting starts, or where a space
2509 * is. Needed for when the bold trick is used */
2510 for (back = 0; back < col1; ++back)
2511 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2512 || ScreenLines[off - 1 - back] == ' ')
2513 break;
2514 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2515 && ScreenLines[off - 1] != ' ');
2516
2517 /* Break it up in strings of characters with the same attributes. */
2518 /* Print UTF-8 characters individually. */
2519 while (len > 0)
2520 {
2521 first_attr = ScreenAttrs[off];
2522 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002523#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (enc_utf8 && ScreenLinesUC[off] != 0)
2525 {
2526 /* output multi-byte character separately */
2527 nback = gui_screenchar(off, flags,
2528 (guicolor_T)0, (guicolor_T)0, back);
2529 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2530 idx = 2;
2531 else
2532 idx = 1;
2533 }
2534 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2535 {
2536 /* output double-byte, single-width character separately */
2537 nback = gui_screenchar(off, flags,
2538 (guicolor_T)0, (guicolor_T)0, back);
2539 idx = 1;
2540 }
2541 else
2542#endif
2543 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002544#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 for (idx = 0; idx < len; ++idx)
2546 {
2547 if (enc_utf8 && ScreenLines[off + idx] == 0)
2548 continue; /* skip second half of double-width char */
2549 if (ScreenAttrs[off + idx] != first_attr)
2550 break;
2551 }
2552 /* gui_screenstr() takes care of multibyte chars */
2553 nback = gui_screenstr(off, idx, flags,
2554 (guicolor_T)0, (guicolor_T)0, back);
2555#else
2556 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2557 idx++)
2558 {
2559# ifdef FEAT_MBYTE
2560 /* Stop at a multi-byte Unicode character. */
2561 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2562 break;
2563 if (enc_dbcs == DBCS_JPNU)
2564 {
2565 /* Stop at a double-byte single-width char. */
2566 if (ScreenLines[off + idx] == 0x8e)
2567 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002568 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 + off + idx) == 2)
2570 ++idx; /* skip second byte of double-byte char */
2571 }
2572# endif
2573 }
2574 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2575 (guicolor_T)0, (guicolor_T)0, back);
2576#endif
2577 }
2578 if (nback == FAIL)
2579 {
2580 /* Must back up to start drawing where a bold or italic word
2581 * starts. */
2582 off -= back;
2583 len += back;
2584 gui.col -= back;
2585 }
2586 else
2587 {
2588 off += idx;
2589 len -= idx;
2590 }
2591 back = 0;
2592 }
2593 }
2594
2595 /* Put the cursor back where it was */
2596 gui.row = old_row;
2597 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002598 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 return retval;
2601}
2602
2603 static void
2604gui_delete_lines(row, count)
2605 int row;
2606 int count;
2607{
2608 if (count <= 0)
2609 return;
2610
2611 if (row + count > gui.scroll_region_bot)
2612 /* Scrolled out of region, just blank the lines out */
2613 gui_clear_block(row, gui.scroll_region_left,
2614 gui.scroll_region_bot, gui.scroll_region_right);
2615 else
2616 {
2617 gui_mch_delete_lines(row, count);
2618
2619 /* If the cursor was in the deleted lines it's now gone. If the
2620 * cursor was in the scrolled lines adjust its position. */
2621 if (gui.cursor_row >= row
2622 && gui.cursor_col >= gui.scroll_region_left
2623 && gui.cursor_col <= gui.scroll_region_right)
2624 {
2625 if (gui.cursor_row < row + count)
2626 gui.cursor_is_valid = FALSE;
2627 else if (gui.cursor_row <= gui.scroll_region_bot)
2628 gui.cursor_row -= count;
2629 }
2630 }
2631}
2632
2633 static void
2634gui_insert_lines(row, count)
2635 int row;
2636 int count;
2637{
2638 if (count <= 0)
2639 return;
2640
2641 if (row + count > gui.scroll_region_bot)
2642 /* Scrolled out of region, just blank the lines out */
2643 gui_clear_block(row, gui.scroll_region_left,
2644 gui.scroll_region_bot, gui.scroll_region_right);
2645 else
2646 {
2647 gui_mch_insert_lines(row, count);
2648
2649 if (gui.cursor_row >= gui.row
2650 && gui.cursor_col >= gui.scroll_region_left
2651 && gui.cursor_col <= gui.scroll_region_right)
2652 {
2653 if (gui.cursor_row <= gui.scroll_region_bot - count)
2654 gui.cursor_row += count;
2655 else if (gui.cursor_row <= gui.scroll_region_bot)
2656 gui.cursor_is_valid = FALSE;
2657 }
2658 }
2659}
2660
2661/*
2662 * The main GUI input routine. Waits for a character from the keyboard.
2663 * wtime == -1 Wait forever.
2664 * wtime == 0 Don't wait.
2665 * wtime > 0 Wait wtime milliseconds for a character.
2666 * Returns OK if a character was found to be available within the given time,
2667 * or FAIL otherwise.
2668 */
2669 int
2670gui_wait_for_chars(wtime)
2671 long wtime;
2672{
2673 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002675#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 /*
2677 * If we're going to wait a bit, update the menus and mouse shape for the
2678 * current State.
2679 */
2680 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 gui_update_menus(0);
2682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 gui_mch_update();
2685 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 /* Before waiting, flush any output to the screen. */
2691 gui_mch_flush();
2692
2693 if (wtime > 0)
2694 {
2695 /* Blink when waiting for a character. Probably only does something
2696 * for showmatch() */
2697 gui_mch_start_blink();
2698 retval = gui_mch_wait_for_chars(wtime);
2699 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return retval;
2701 }
2702
2703 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002704 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 */
2706 gui_mch_start_blink();
2707
Bram Moolenaar3918c952005-03-15 22:34:55 +00002708 retval = FAIL;
2709 /*
2710 * We may want to trigger the CursorHold event. First wait for
2711 * 'updatetime' and if nothing is typed within that time put the
2712 * K_CURSORHOLD key in the input buffer.
2713 */
2714 if (gui_mch_wait_for_chars(p_ut) == OK)
2715 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002717 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002719 char_u buf[3];
2720
2721 /* Put K_CURSORHOLD in the input buffer. */
2722 buf[0] = CSI;
2723 buf[1] = KS_EXTRA;
2724 buf[2] = (int)KE_CURSORHOLD;
2725 add_to_input_buf(buf, 3);
2726
2727 retval = OK;
2728 }
2729#endif
2730
2731 if (retval == FAIL)
2732 {
2733 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002734 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 gui_mch_stop_blink();
2739 return retval;
2740}
2741
2742/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002743 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 */
2745 static void
2746fill_mouse_coord(p, col, row)
2747 char_u *p;
2748 int col;
2749 int row;
2750{
2751 p[0] = (char_u)(col / 128 + ' ' + 1);
2752 p[1] = (char_u)(col % 128 + ' ' + 1);
2753 p[2] = (char_u)(row / 128 + ' ' + 1);
2754 p[3] = (char_u)(row % 128 + ' ' + 1);
2755}
2756
2757/*
2758 * Generic mouse support function. Add a mouse event to the input buffer with
2759 * the given properties.
2760 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2761 * MOUSE_X1, MOUSE_X2
2762 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002763 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
2764 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 * x, y --- Coordinates of mouse in pixels.
2766 * repeated_click --- TRUE if this click comes only a short time after a
2767 * previous click.
2768 * modifiers --- Bit field which may be any of the following modifiers
2769 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2770 * This function will ignore drag events where the mouse has not moved to a new
2771 * character.
2772 */
2773 void
2774gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2775 int button;
2776 int x;
2777 int y;
2778 int repeated_click;
2779 int_u modifiers;
2780{
2781 static int prev_row = 0, prev_col = 0;
2782 static int prev_button = -1;
2783 static int num_clicks = 1;
2784 char_u string[10];
2785 enum key_extra button_char;
2786 int row, col;
2787#ifdef FEAT_CLIPBOARD
2788 int checkfor;
2789 int did_clip = FALSE;
2790#endif
2791
2792 /*
2793 * Scrolling may happen at any time, also while a selection is present.
2794 */
2795 switch (button)
2796 {
2797 case MOUSE_X1:
2798 button_char = KE_X1MOUSE;
2799 goto button_set;
2800 case MOUSE_X2:
2801 button_char = KE_X2MOUSE;
2802 goto button_set;
2803 case MOUSE_4:
2804 button_char = KE_MOUSEDOWN;
2805 goto button_set;
2806 case MOUSE_5:
2807 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002808 goto button_set;
2809 case MOUSE_6:
2810 button_char = KE_MOUSELEFT;
2811 goto button_set;
2812 case MOUSE_7:
2813 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814button_set:
2815 {
2816 /* Don't put events in the input queue now. */
2817 if (hold_gui_events)
2818 return;
2819
2820 string[3] = CSI;
2821 string[4] = KS_EXTRA;
2822 string[5] = (int)button_char;
2823
2824 /* Pass the pointer coordinates of the scroll event so that we
2825 * know which window to scroll. */
2826 row = gui_xy2colrow(x, y, &col);
2827 string[6] = (char_u)(col / 128 + ' ' + 1);
2828 string[7] = (char_u)(col % 128 + ' ' + 1);
2829 string[8] = (char_u)(row / 128 + ' ' + 1);
2830 string[9] = (char_u)(row % 128 + ' ' + 1);
2831
2832 if (modifiers == 0)
2833 add_to_input_buf(string + 3, 7);
2834 else
2835 {
2836 string[0] = CSI;
2837 string[1] = KS_MODIFIER;
2838 string[2] = 0;
2839 if (modifiers & MOUSE_SHIFT)
2840 string[2] |= MOD_MASK_SHIFT;
2841 if (modifiers & MOUSE_CTRL)
2842 string[2] |= MOD_MASK_CTRL;
2843 if (modifiers & MOUSE_ALT)
2844 string[2] |= MOD_MASK_ALT;
2845 add_to_input_buf(string, 10);
2846 }
2847 return;
2848 }
2849 }
2850
2851#ifdef FEAT_CLIPBOARD
2852 /* If a clipboard selection is in progress, handle it */
2853 if (clip_star.state == SELECT_IN_PROGRESS)
2854 {
2855 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2856 return;
2857 }
2858
2859 /* Determine which mouse settings to look for based on the current mode */
2860 switch (get_real_state())
2861 {
2862 case NORMAL_BUSY:
2863 case OP_PENDING:
2864 case NORMAL: checkfor = MOUSE_NORMAL; break;
2865 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002866 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case REPLACE:
2868 case REPLACE+LANGMAP:
2869#ifdef FEAT_VREPLACE
2870 case VREPLACE:
2871 case VREPLACE+LANGMAP:
2872#endif
2873 case INSERT:
2874 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2875 case ASKMORE:
2876 case HITRETURN: /* At the more- and hit-enter prompt pass the
2877 mouse event for a click on or below the
2878 message line. */
2879 if (Y_2_ROW(y) >= msg_row)
2880 checkfor = MOUSE_NORMAL;
2881 else
2882 checkfor = MOUSE_RETURN;
2883 break;
2884
2885 /*
2886 * On the command line, use the clipboard selection on all lines
2887 * but the command line. But not when pasting.
2888 */
2889 case CMDLINE:
2890 case CMDLINE+LANGMAP:
2891 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2892 checkfor = MOUSE_NONE;
2893 else
2894 checkfor = MOUSE_COMMAND;
2895 break;
2896
2897 default:
2898 checkfor = MOUSE_NONE;
2899 break;
2900 };
2901
2902 /*
2903 * Allow clipboard selection of text on the command line in "normal"
2904 * modes. Don't do this when dragging the status line, or extending a
2905 * Visual selection.
2906 */
2907 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2908 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00002909# ifdef FEAT_WINDOWS
2910 + firstwin->w_winrow
2911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 && button != MOUSE_DRAG
2913# ifdef FEAT_MOUSESHAPE
2914 && !drag_status_line
2915# ifdef FEAT_VERTSPLIT
2916 && !drag_sep_line
2917# endif
2918# endif
2919 )
2920 checkfor = MOUSE_NONE;
2921
2922 /*
2923 * Use modeless selection when holding CTRL and SHIFT pressed.
2924 */
2925 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2926 checkfor = MOUSE_NONEF;
2927
2928 /*
2929 * In Ex mode, always use modeless selection.
2930 */
2931 if (exmode_active)
2932 checkfor = MOUSE_NONE;
2933
2934 /*
2935 * If the mouse settings say to not use the mouse, use the modeless
2936 * selection. But if Visual is active, assume that only the Visual area
2937 * will be selected.
2938 * Exception: On the command line, both the selection is used and a mouse
2939 * key is send.
2940 */
2941 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2942 {
2943#ifdef FEAT_VISUAL
2944 /* Don't do modeless selection in Visual mode. */
2945 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2946 return;
2947#endif
2948
2949 /*
2950 * When 'mousemodel' is "popup", shift-left is translated to right.
2951 * But not when also using Ctrl.
2952 */
2953 if (mouse_model_popup() && button == MOUSE_LEFT
2954 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2955 {
2956 button = MOUSE_RIGHT;
2957 modifiers &= ~ MOUSE_SHIFT;
2958 }
2959
2960 /* If the selection is done, allow the right button to extend it.
2961 * If the selection is cleared, allow the right button to start it
2962 * from the cursor position. */
2963 if (button == MOUSE_RIGHT)
2964 {
2965 if (clip_star.state == SELECT_CLEARED)
2966 {
2967 if (State & CMDLINE)
2968 {
2969 col = msg_col;
2970 row = msg_row;
2971 }
2972 else
2973 {
2974 col = curwin->w_wcol;
2975 row = curwin->w_wrow + W_WINROW(curwin);
2976 }
2977 clip_start_selection(col, row, FALSE);
2978 }
2979 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2980 repeated_click);
2981 did_clip = TRUE;
2982 }
2983 /* Allow the left button to start the selection */
2984 else if (button ==
2985# ifdef RISCOS
2986 /* Only start a drag on a drag event. Otherwise
2987 * we don't get a release event. */
2988 MOUSE_DRAG
2989# else
2990 MOUSE_LEFT
2991# endif
2992 )
2993 {
2994 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2995 did_clip = TRUE;
2996 }
2997# ifdef RISCOS
2998 else if (button == MOUSE_LEFT)
2999 {
3000 clip_clear_selection();
3001 did_clip = TRUE;
3002 }
3003# endif
3004
3005 /* Always allow pasting */
3006 if (button != MOUSE_MIDDLE)
3007 {
3008 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3009 return;
3010 if (checkfor != MOUSE_COMMAND)
3011 button = MOUSE_LEFT;
3012 }
3013 repeated_click = FALSE;
3014 }
3015
3016 if (clip_star.state != SELECT_CLEARED && !did_clip)
3017 clip_clear_selection();
3018#endif
3019
3020 /* Don't put events in the input queue now. */
3021 if (hold_gui_events)
3022 return;
3023
3024 row = gui_xy2colrow(x, y, &col);
3025
3026 /*
3027 * If we are dragging and the mouse hasn't moved far enough to be on a
3028 * different character, then don't send an event to vim.
3029 */
3030 if (button == MOUSE_DRAG)
3031 {
3032 if (row == prev_row && col == prev_col)
3033 return;
3034 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3035 if (y < 0)
3036 row = -1;
3037 }
3038
3039 /*
3040 * If topline has changed (window scrolled) since the last click, reset
3041 * repeated_click, because we don't want starting Visual mode when
3042 * clicking on a different character in the text.
3043 */
3044 if (curwin->w_topline != gui_prev_topline
3045#ifdef FEAT_DIFF
3046 || curwin->w_topfill != gui_prev_topfill
3047#endif
3048 )
3049 repeated_click = FALSE;
3050
3051 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3052 string[1] = KS_MOUSE;
3053 string[2] = KE_FILLER;
3054 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3055 {
3056 if (repeated_click)
3057 {
3058 /*
3059 * Handle multiple clicks. They only count if the mouse is still
3060 * pointing at the same character.
3061 */
3062 if (button != prev_button || row != prev_row || col != prev_col)
3063 num_clicks = 1;
3064 else if (++num_clicks > 4)
3065 num_clicks = 1;
3066 }
3067 else
3068 num_clicks = 1;
3069 prev_button = button;
3070 gui_prev_topline = curwin->w_topline;
3071#ifdef FEAT_DIFF
3072 gui_prev_topfill = curwin->w_topfill;
3073#endif
3074
3075 string[3] = (char_u)(button | 0x20);
3076 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3077 }
3078 else
3079 string[3] = (char_u)button;
3080
3081 string[3] |= modifiers;
3082 fill_mouse_coord(string + 4, col, row);
3083 add_to_input_buf(string, 8);
3084
3085 if (row < 0)
3086 prev_row = 0;
3087 else
3088 prev_row = row;
3089 prev_col = col;
3090
3091 /*
3092 * We need to make sure this is cleared since Athena doesn't tell us when
3093 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3094 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003095#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 gui.dragged_sb = SBAR_NONE;
3097#endif
3098}
3099
3100/*
3101 * Convert x and y coordinate to column and row in text window.
3102 * Corrects for multi-byte character.
3103 * returns column in "*colp" and row as return value;
3104 */
3105 int
3106gui_xy2colrow(x, y, colp)
3107 int x;
3108 int y;
3109 int *colp;
3110{
3111 int col = check_col(X_2_COL(x));
3112 int row = check_row(Y_2_ROW(y));
3113
3114#ifdef FEAT_MBYTE
3115 *colp = mb_fix_col(col, row);
3116#else
3117 *colp = col;
3118#endif
3119 return row;
3120}
3121
3122#if defined(FEAT_MENU) || defined(PROTO)
3123/*
3124 * Callback function for when a menu entry has been selected.
3125 */
3126 void
3127gui_menu_cb(menu)
3128 vimmenu_T *menu;
3129{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003130 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 /* Don't put events in the input queue now. */
3133 if (hold_gui_events)
3134 return;
3135
3136 bytes[0] = CSI;
3137 bytes[1] = KS_MENU;
3138 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003139 add_to_input_buf(bytes, 3);
3140 add_long_to_buf((long_u)menu, bytes);
3141 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143#endif
3144
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003145static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147/*
3148 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003149 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 * in p_go.
3151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 void
3153gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003154 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#ifdef FEAT_MENU
3157 static int prev_menu_is_active = -1;
3158#endif
3159#ifdef FEAT_TOOLBAR
3160 static int prev_toolbar = -1;
3161 int using_toolbar = FALSE;
3162#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003163#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003164 int using_tabline;
3165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#ifdef FEAT_FOOTER
3167 static int prev_footer = -1;
3168 int using_footer = FALSE;
3169#endif
3170#if defined(FEAT_MENU) && !defined(WIN16)
3171 static int prev_tearoff = -1;
3172 int using_tearoff = FALSE;
3173#endif
3174
3175 char_u *p;
3176 int i;
3177#ifdef FEAT_MENU
3178 int grey_old, grey_new;
3179 char_u *temp;
3180#endif
3181 win_T *wp;
3182 int need_set_size;
3183 int fix_size;
3184
3185#ifdef FEAT_MENU
3186 if (oldval != NULL && gui.in_use)
3187 {
3188 /*
3189 * Check if the menu's go from grey to non-grey or vise versa.
3190 */
3191 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3192 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3193 if (grey_old != grey_new)
3194 {
3195 temp = p_go;
3196 p_go = oldval;
3197 gui_update_menus(MENU_ALL_MODES);
3198 p_go = temp;
3199 }
3200 }
3201 gui.menu_is_active = FALSE;
3202#endif
3203
3204 for (i = 0; i < 3; i++)
3205 gui.which_scrollbars[i] = FALSE;
3206 for (p = p_go; *p; p++)
3207 switch (*p)
3208 {
3209 case GO_LEFT:
3210 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3211 break;
3212 case GO_RIGHT:
3213 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3214 break;
3215#ifdef FEAT_VERTSPLIT
3216 case GO_VLEFT:
3217 if (win_hasvertsplit())
3218 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3219 break;
3220 case GO_VRIGHT:
3221 if (win_hasvertsplit())
3222 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3223 break;
3224#endif
3225 case GO_BOT:
3226 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3227 break;
3228#ifdef FEAT_MENU
3229 case GO_MENUS:
3230 gui.menu_is_active = TRUE;
3231 break;
3232#endif
3233 case GO_GREY:
3234 /* make menu's have grey items, ignored here */
3235 break;
3236#ifdef FEAT_TOOLBAR
3237 case GO_TOOLBAR:
3238 using_toolbar = TRUE;
3239 break;
3240#endif
3241#ifdef FEAT_FOOTER
3242 case GO_FOOTER:
3243 using_footer = TRUE;
3244 break;
3245#endif
3246 case GO_TEAROFF:
3247#if defined(FEAT_MENU) && !defined(WIN16)
3248 using_tearoff = TRUE;
3249#endif
3250 break;
3251 default:
3252 /* Ignore options that are not supported */
3253 break;
3254 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (gui.in_use)
3257 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003258 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003260
3261#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003262 /* Update the GUI tab line, it may appear or disappear. This may
3263 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003264 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003265 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003266 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003267 /* We don't want a resize event change "Rows" here, save and
3268 * restore it. Resizing is handled below. */
3269 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003270 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003271 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003272 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003273 if (using_tabline)
3274 fix_size = TRUE;
3275 if (!gui_use_tabline())
3276 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3277 }
3278#endif
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 for (i = 0; i < 3; i++)
3281 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003282 /* The scrollbar needs to be updated when it is shown/unshown and
3283 * when switching tab pages. But the size only changes when it's
3284 * shown/unshown. Thus we need two places to remember whether a
3285 * scrollbar is there or not. */
3286 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003287#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003288 || gui.which_scrollbars[i]
3289 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003290#endif
3291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 if (i == SBAR_BOTTOM)
3294 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3295 gui.which_scrollbars[i]);
3296 else
3297 {
3298 FOR_ALL_WINDOWS(wp)
3299 {
3300 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3301 }
3302 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003303 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3304 {
3305 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003306 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003307 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003308 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003309 if (gui.which_scrollbars[i])
3310 fix_size = TRUE;
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003313#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003314 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003315#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003316 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318
3319#ifdef FEAT_MENU
3320 if (gui.menu_is_active != prev_menu_is_active)
3321 {
3322 /* We don't want a resize event change "Rows" here, save and
3323 * restore it. Resizing is handled below. */
3324 i = Rows;
3325 gui_mch_enable_menu(gui.menu_is_active);
3326 Rows = i;
3327 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003328 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (gui.menu_is_active)
3330 fix_size = TRUE;
3331 }
3332#endif
3333
3334#ifdef FEAT_TOOLBAR
3335 if (using_toolbar != prev_toolbar)
3336 {
3337 gui_mch_show_toolbar(using_toolbar);
3338 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003339 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (using_toolbar)
3341 fix_size = TRUE;
3342 }
3343#endif
3344#ifdef FEAT_FOOTER
3345 if (using_footer != prev_footer)
3346 {
3347 gui_mch_enable_footer(using_footer);
3348 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003349 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (using_footer)
3351 fix_size = TRUE;
3352 }
3353#endif
3354#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3355 if (using_tearoff != prev_tearoff)
3356 {
3357 gui_mch_toggle_tearoffs(using_tearoff);
3358 prev_tearoff = using_tearoff;
3359 }
3360#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003361 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003362 {
3363#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003364 long prev_Columns = Columns;
3365 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 /* Adjust the size of the window to make the text area keep the
3368 * same size and to avoid that part of our window is off-screen
3369 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003370 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003371
3372#ifdef FEAT_GUI_GTK
3373 /* GTK has the annoying habit of sending us resize events when
3374 * changing the window size ourselves. This mostly happens when
3375 * waiting for a character to arrive, quite unpredictably, and may
3376 * change Columns and Rows when we don't want it. Wait for a
3377 * character here to avoid this effect.
3378 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003379 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003380 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003381 * Don't change Rows when adding menu/toolbar/tabline.
3382 * Don't change Columns when adding vertical toolbar. */
3383 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003384 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003385 if ((need_set_size & RESIZE_VERT) == 0)
3386 Rows = prev_Rows;
3387 if ((need_set_size & RESIZE_HOR) == 0)
3388 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003389#endif
3390 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003391#ifdef FEAT_WINDOWS
3392 /* When the console tabline appears or disappears the window positions
3393 * change. */
3394 if (firstwin->w_winrow != tabline_height())
3395 shell_new_rows(); /* recompute window positions and heights */
3396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398}
3399
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003400#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3401/*
3402 * Return TRUE if the GUI is taking care of the tabline.
3403 * It may still be hidden if 'showtabline' is zero.
3404 */
3405 int
3406gui_use_tabline()
3407{
3408 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3409}
3410
3411/*
3412 * Return TRUE if the GUI is showing the tabline.
3413 * This uses 'showtabline'.
3414 */
3415 static int
3416gui_has_tabline()
3417{
3418 if (!gui_use_tabline()
3419 || p_stal == 0
3420 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3421 return FALSE;
3422 return TRUE;
3423}
3424
3425/*
3426 * Update the tabline.
3427 * This may display/undisplay the tabline and update the labels.
3428 */
3429 void
3430gui_update_tabline()
3431{
3432 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003433 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003434
3435 if (!gui.starting && starting == 0)
3436 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003437 /* Updating the tabline uses direct GUI commands, flush
3438 * outstanding instructions first. (esp. clear screen) */
3439 out_flush();
3440 gui_mch_flush();
3441
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003442 if (!showit != !shown)
3443 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003444 if (showit != 0)
3445 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003446
3447 /* When the tabs change from hidden to shown or from shown to
3448 * hidden the size of the text area should remain the same. */
3449 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003450 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003451 }
3452}
3453
3454/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003455 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003456 */
3457 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003458get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003459 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003460 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003461{
3462 int modified = FALSE;
3463 char_u buf[40];
3464 int wincount;
3465 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003466 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003467
Bram Moolenaar57657d82006-04-21 22:12:41 +00003468 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003469 opt = (tooltip ? &p_gtt : &p_gtl);
3470 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003471 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003472 int use_sandbox = FALSE;
3473 int save_called_emsg = called_emsg;
3474 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003475 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003476 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3477 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003478
3479 called_emsg = FALSE;
3480
3481 printer_page_num = tabpage_index(tp);
3482# ifdef FEAT_EVAL
3483 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003484 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003485# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003486 /* It's almost as going to the tabpage, but without autocommands. */
3487 curtab->tp_firstwin = firstwin;
3488 curtab->tp_lastwin = lastwin;
3489 curtab->tp_curwin = curwin;
3490 save_curtab = curtab;
3491 curtab = tp;
3492 topframe = curtab->tp_topframe;
3493 firstwin = curtab->tp_firstwin;
3494 lastwin = curtab->tp_lastwin;
3495 curwin = curtab->tp_curwin;
3496 curbuf = curwin->w_buffer;
3497
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003498 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003499 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003500 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003501 STRCPY(NameBuff, res);
3502
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003503 /* Back to the original curtab. */
3504 curtab = save_curtab;
3505 topframe = curtab->tp_topframe;
3506 firstwin = curtab->tp_firstwin;
3507 lastwin = curtab->tp_lastwin;
3508 curwin = curtab->tp_curwin;
3509 curbuf = curwin->w_buffer;
3510
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003511 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003512 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003513 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003514 called_emsg |= save_called_emsg;
3515 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003516
3517 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3518 * use a default label. */
3519 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003520 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003521 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003522 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003523 if (!tooltip)
3524 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003525
3526 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3527 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3528 if (bufIsChanged(wp->w_buffer))
3529 modified = TRUE;
3530 if (modified || wincount > 1)
3531 {
3532 if (wincount > 1)
3533 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3534 else
3535 buf[0] = NUL;
3536 if (modified)
3537 STRCAT(buf, "+");
3538 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003539 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003540 mch_memmove(NameBuff, buf, STRLEN(buf));
3541 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003542 }
3543}
3544
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003545/*
3546 * Send the event for clicking to select tab page "nr".
3547 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003548 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003549 */
3550 int
3551send_tabline_event(nr)
3552 int nr;
3553{
3554 char_u string[3];
3555
3556 if (nr == tabpage_index(curtab))
3557 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003558
3559 /* Don't put events in the input queue now. */
3560 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003561# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003562 || cmdwin_type != 0
3563# endif
3564 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003565 {
3566 /* Set it back to the current tab page. */
3567 gui_mch_set_curtab(tabpage_index(curtab));
3568 return FALSE;
3569 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003570
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003571 string[0] = CSI;
3572 string[1] = KS_TABLINE;
3573 string[2] = KE_FILLER;
3574 add_to_input_buf(string, 3);
3575 string[0] = nr;
3576 add_to_input_buf_csi(string, 1);
3577 return TRUE;
3578}
3579
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003580/*
3581 * Send a tabline menu event
3582 */
3583 void
3584send_tabline_menu_event(tabidx, event)
3585 int tabidx;
3586 int event;
3587{
3588 char_u string[3];
3589
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003590 /* Don't put events in the input queue now. */
3591 if (hold_gui_events)
3592 return;
3593
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003594 string[0] = CSI;
3595 string[1] = KS_TABMENU;
3596 string[2] = KE_FILLER;
3597 add_to_input_buf(string, 3);
3598 string[0] = tabidx;
3599 string[1] = (char_u)(long)event;
3600 add_to_input_buf_csi(string, 2);
3601}
3602
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605/*
3606 * Scrollbar stuff:
3607 */
3608
Bram Moolenaare45828b2006-02-15 22:12:56 +00003609#if defined(FEAT_WINDOWS) || defined(PROTO)
3610/*
3611 * Remove all scrollbars. Used before switching to another tab page.
3612 */
3613 void
3614gui_remove_scrollbars()
3615{
3616 int i;
3617 win_T *wp;
3618
3619 for (i = 0; i < 3; i++)
3620 {
3621 if (i == SBAR_BOTTOM)
3622 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3623 else
3624 {
3625 FOR_ALL_WINDOWS(wp)
3626 {
3627 gui_do_scrollbar(wp, i, FALSE);
3628 }
3629 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003630 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003631 }
3632}
3633#endif
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 void
3636gui_create_scrollbar(sb, type, wp)
3637 scrollbar_T *sb;
3638 int type;
3639 win_T *wp;
3640{
3641 static int sbar_ident = 0;
3642
3643 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3644 sb->wp = wp;
3645 sb->type = type;
3646 sb->value = 0;
3647#ifdef FEAT_GUI_ATHENA
3648 sb->pixval = 0;
3649#endif
3650 sb->size = 1;
3651 sb->max = 1;
3652 sb->top = 0;
3653 sb->height = 0;
3654#ifdef FEAT_VERTSPLIT
3655 sb->width = 0;
3656#endif
3657 sb->status_height = 0;
3658 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3659}
3660
3661/*
3662 * Find the scrollbar with the given index.
3663 */
3664 scrollbar_T *
3665gui_find_scrollbar(ident)
3666 long ident;
3667{
3668 win_T *wp;
3669
3670 if (gui.bottom_sbar.ident == ident)
3671 return &gui.bottom_sbar;
3672 FOR_ALL_WINDOWS(wp)
3673 {
3674 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3675 return &wp->w_scrollbars[SBAR_LEFT];
3676 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3677 return &wp->w_scrollbars[SBAR_RIGHT];
3678 }
3679 return NULL;
3680}
3681
3682/*
3683 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3684 *
3685 * For Win32, Macintosh and GTK+ 2:
3686 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3687 * you stop dragging the scrollbar. We get here each time the scrollbar is
3688 * dragged another pixel, but as far as the rest of vim goes, it thinks
3689 * we're just hanging in the call to DispatchMessage() in
3690 * process_message(). The DispatchMessage() call that hangs was passed a
3691 * mouse button click event in the scrollbar window. -- webb.
3692 *
3693 * Solution: Do the scrolling right here. But only when allowed.
3694 * Ignore the scrollbars while executing an external command or when there
3695 * are still characters to be processed.
3696 */
3697 void
3698gui_drag_scrollbar(sb, value, still_dragging)
3699 scrollbar_T *sb;
3700 long value;
3701 int still_dragging;
3702{
3703#ifdef FEAT_WINDOWS
3704 win_T *wp;
3705#endif
3706 int sb_num;
3707#ifdef USE_ON_FLY_SCROLL
3708 colnr_T old_leftcol = curwin->w_leftcol;
3709# ifdef FEAT_SCROLLBIND
3710 linenr_T old_topline = curwin->w_topline;
3711# endif
3712# ifdef FEAT_DIFF
3713 int old_topfill = curwin->w_topfill;
3714# endif
3715#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003716 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 int byte_count;
3718#endif
3719
3720 if (sb == NULL)
3721 return;
3722
3723 /* Don't put events in the input queue now. */
3724 if (hold_gui_events)
3725 return;
3726
3727#ifdef FEAT_CMDWIN
3728 if (cmdwin_type != 0 && sb->wp != curwin)
3729 return;
3730#endif
3731
3732 if (still_dragging)
3733 {
3734 if (sb->wp == NULL)
3735 gui.dragged_sb = SBAR_BOTTOM;
3736 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3737 gui.dragged_sb = SBAR_LEFT;
3738 else
3739 gui.dragged_sb = SBAR_RIGHT;
3740 gui.dragged_wp = sb->wp;
3741 }
3742 else
3743 {
3744 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003745#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 /* Keep the "dragged_wp" value until after the scrolling, for when the
3747 * moust button is released. GTK2 doesn't send the button-up event. */
3748 gui.dragged_wp = NULL;
3749#endif
3750 }
3751
3752 /* Vertical sbar info is kept in the first sbar (the left one) */
3753 if (sb->wp != NULL)
3754 sb = &sb->wp->w_scrollbars[0];
3755
3756 /*
3757 * Check validity of value
3758 */
3759 if (value < 0)
3760 value = 0;
3761#ifdef SCROLL_PAST_END
3762 else if (value > sb->max)
3763 value = sb->max;
3764#else
3765 if (value > sb->max - sb->size + 1)
3766 value = sb->max - sb->size + 1;
3767#endif
3768
3769 sb->value = value;
3770
3771#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003772 /* When not allowed to do the scrolling right now, return.
3773 * This also checked input_available(), but that causes the first click in
3774 * a scrollbar to be ignored when Vim doesn't have focus. */
3775 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return;
3777#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003778#ifdef FEAT_INS_EXPAND
3779 /* Disallow scrolling the current window when the completion popup menu is
3780 * visible. */
3781 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3782 return;
3783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
3785#ifdef FEAT_RIGHTLEFT
3786 if (sb->wp == NULL && curwin->w_p_rl)
3787 {
3788 value = sb->max + 1 - sb->size - value;
3789 if (value < 0)
3790 value = 0;
3791 }
3792#endif
3793
3794 if (sb->wp != NULL) /* vertical scrollbar */
3795 {
3796 sb_num = 0;
3797#ifdef FEAT_WINDOWS
3798 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3799 sb_num++;
3800 if (wp == NULL)
3801 return;
3802#else
3803 if (sb->wp != curwin)
3804 return;
3805#endif
3806
3807#ifdef USE_ON_FLY_SCROLL
3808 current_scrollbar = sb_num;
3809 scrollbar_value = value;
3810 if (State & NORMAL)
3811 {
3812 gui_do_scroll();
3813 setcursor();
3814 }
3815 else if (State & INSERT)
3816 {
3817 ins_scroll();
3818 setcursor();
3819 }
3820 else if (State & CMDLINE)
3821 {
3822 if (msg_scrolled == 0)
3823 {
3824 gui_do_scroll();
3825 redrawcmdline();
3826 }
3827 }
3828# ifdef FEAT_FOLDING
3829 /* Value may have been changed for closed fold. */
3830 sb->value = sb->wp->w_topline - 1;
3831# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003832
3833 /* When dragging one scrollbar and there is another one at the other
3834 * side move the thumb of that one too. */
3835 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3836 gui_mch_set_scrollbar_thumb(
3837 &sb->wp->w_scrollbars[
3838 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3839 ? SBAR_LEFT : SBAR_RIGHT],
3840 sb->value, sb->size, sb->max);
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#else
3843 bytes[0] = CSI;
3844 bytes[1] = KS_VER_SCROLLBAR;
3845 bytes[2] = KE_FILLER;
3846 bytes[3] = (char_u)sb_num;
3847 byte_count = 4;
3848#endif
3849 }
3850 else
3851 {
3852#ifdef USE_ON_FLY_SCROLL
3853 scrollbar_value = value;
3854
3855 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003856 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else if (State & INSERT)
3858 ins_horscroll();
3859 else if (State & CMDLINE)
3860 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003861 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003863 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 redrawcmdline();
3865 }
3866 }
3867 if (old_leftcol != curwin->w_leftcol)
3868 {
3869 updateWindow(curwin); /* update window, status and cmdline */
3870 setcursor();
3871 }
3872#else
3873 bytes[0] = CSI;
3874 bytes[1] = KS_HOR_SCROLLBAR;
3875 bytes[2] = KE_FILLER;
3876 byte_count = 3;
3877#endif
3878 }
3879
3880#ifdef USE_ON_FLY_SCROLL
3881# ifdef FEAT_SCROLLBIND
3882 /*
3883 * synchronize other windows, as necessary according to 'scrollbind'
3884 */
3885 if (curwin->w_p_scb
3886 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3887 || (sb->wp == curwin && (curwin->w_topline != old_topline
3888# ifdef FEAT_DIFF
3889 || curwin->w_topfill != old_topfill
3890# endif
3891 ))))
3892 {
3893 do_check_scrollbind(TRUE);
3894 /* need to update the window right here */
3895 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3896 if (wp->w_redr_type > 0)
3897 updateWindow(wp);
3898 setcursor();
3899 }
3900# endif
3901 out_flush();
3902 gui_update_cursor(FALSE, TRUE);
3903#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003904 add_to_input_buf(bytes, byte_count);
3905 add_long_to_buf((long_u)value, bytes);
3906 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907#endif
3908}
3909
3910/*
3911 * Scrollbar stuff:
3912 */
3913
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02003914#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003915/*
3916 * Called when something in the window layout has changed.
3917 */
3918 void
3919gui_may_update_scrollbars()
3920{
3921 if (gui.in_use && starting == 0)
3922 {
3923 out_flush();
3924 gui_init_which_components(NULL);
3925 gui_update_scrollbars(TRUE);
3926 }
3927 need_mouse_correct = TRUE;
3928}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02003929#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 void
3932gui_update_scrollbars(force)
3933 int force; /* Force all scrollbars to get updated */
3934{
3935 win_T *wp;
3936 scrollbar_T *sb;
3937 long val, size, max; /* need 32 bits here */
3938 int which_sb;
3939 int h, y;
3940#ifdef FEAT_VERTSPLIT
3941 static win_T *prev_curwin = NULL;
3942#endif
3943
3944 /* Update the horizontal scrollbar */
3945 gui_update_horiz_scrollbar(force);
3946
3947#ifndef WIN3264
3948 /* Return straight away if there is neither a left nor right scrollbar.
3949 * On MS-Windows this is required anyway for scrollwheel messages. */
3950 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3951 return;
3952#endif
3953
3954 /*
3955 * Don't want to update a scrollbar while we're dragging it. But if we
3956 * have both a left and right scrollbar, and we drag one of them, we still
3957 * need to update the other one.
3958 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003959 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3960 && gui.which_scrollbars[SBAR_LEFT]
3961 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
3963 /*
3964 * If we have two scrollbars and one of them is being dragged, just
3965 * copy the scrollbar position from the dragged one to the other one.
3966 */
3967 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3968 if (gui.dragged_wp != NULL)
3969 gui_mch_set_scrollbar_thumb(
3970 &gui.dragged_wp->w_scrollbars[which_sb],
3971 gui.dragged_wp->w_scrollbars[0].value,
3972 gui.dragged_wp->w_scrollbars[0].size,
3973 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975
3976 /* avoid that moving components around generates events */
3977 ++hold_gui_events;
3978
3979 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3980 {
3981 if (wp->w_buffer == NULL) /* just in case */
3982 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003983 /* Skip a scrollbar that is being dragged. */
3984 if (!force && (gui.dragged_sb == SBAR_LEFT
3985 || gui.dragged_sb == SBAR_RIGHT)
3986 && gui.dragged_wp == wp)
3987 continue;
3988
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989#ifdef SCROLL_PAST_END
3990 max = wp->w_buffer->b_ml.ml_line_count - 1;
3991#else
3992 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3993#endif
3994 if (max < 0) /* empty buffer */
3995 max = 0;
3996 val = wp->w_topline - 1;
3997 size = wp->w_height;
3998#ifdef SCROLL_PAST_END
3999 if (val > max) /* just in case */
4000 val = max;
4001#else
4002 if (size > max + 1) /* just in case */
4003 size = max + 1;
4004 if (val > max - size + 1)
4005 val = max - size + 1;
4006#endif
4007 if (val < 0) /* minimal value is 0 */
4008 val = 0;
4009
4010 /*
4011 * Scrollbar at index 0 (the left one) contains all the information.
4012 * It would be the same info for left and right so we just store it for
4013 * one of them.
4014 */
4015 sb = &wp->w_scrollbars[0];
4016
4017 /*
4018 * Note: no check for valid w_botline. If it's not valid the
4019 * scrollbars will be updated later anyway.
4020 */
4021 if (size < 1 || wp->w_botline - 2 > max)
4022 {
4023 /*
4024 * This can happen during changing files. Just don't update the
4025 * scrollbar for now.
4026 */
4027 sb->height = 0; /* Force update next time */
4028 if (gui.which_scrollbars[SBAR_LEFT])
4029 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4030 if (gui.which_scrollbars[SBAR_RIGHT])
4031 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4032 continue;
4033 }
4034 if (force || sb->height != wp->w_height
4035#ifdef FEAT_WINDOWS
4036 || sb->top != wp->w_winrow
4037 || sb->status_height != wp->w_status_height
4038# ifdef FEAT_VERTSPLIT
4039 || sb->width != wp->w_width
4040 || prev_curwin != curwin
4041# endif
4042#endif
4043 )
4044 {
4045 /* Height, width or position of scrollbar has changed. For
4046 * vertical split: curwin changed. */
4047 sb->height = wp->w_height;
4048#ifdef FEAT_WINDOWS
4049 sb->top = wp->w_winrow;
4050 sb->status_height = wp->w_status_height;
4051# ifdef FEAT_VERTSPLIT
4052 sb->width = wp->w_width;
4053# endif
4054#endif
4055
4056 /* Calculate height and position in pixels */
4057 h = (sb->height + sb->status_height) * gui.char_height;
4058 y = sb->top * gui.char_height + gui.border_offset;
4059#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4060 if (gui.menu_is_active)
4061 y += gui.menu_height;
4062#endif
4063
4064#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4065 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4066# ifdef FEAT_GUI_ATHENA
4067 y += gui.toolbar_height;
4068# else
4069# ifdef FEAT_GUI_MSWIN
4070 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4071# endif
4072# endif
4073#endif
4074
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004075#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4076 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004077 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004078#endif
4079
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080#ifdef FEAT_WINDOWS
4081 if (wp->w_winrow == 0)
4082#endif
4083 {
4084 /* Height of top scrollbar includes width of top border */
4085 h += gui.border_offset;
4086 y -= gui.border_offset;
4087 }
4088 if (gui.which_scrollbars[SBAR_LEFT])
4089 {
4090 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4091 gui.left_sbar_x, y,
4092 gui.scrollbar_width, h);
4093 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4094 }
4095 if (gui.which_scrollbars[SBAR_RIGHT])
4096 {
4097 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4098 gui.right_sbar_x, y,
4099 gui.scrollbar_width, h);
4100 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4101 }
4102 }
4103
4104 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4105 * checking if the thumb moved at least a pixel. Only do this for
4106 * Athena, most other GUIs require the update anyway to make the
4107 * arrows work. */
4108#ifdef FEAT_GUI_ATHENA
4109 if (max == 0)
4110 y = 0;
4111 else
4112 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4113 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4114#else
4115 if (force || sb->value != val || sb->size != size || sb->max != max)
4116#endif
4117 {
4118 /* Thumb of scrollbar has moved */
4119 sb->value = val;
4120#ifdef FEAT_GUI_ATHENA
4121 sb->pixval = y;
4122#endif
4123 sb->size = size;
4124 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004125 if (gui.which_scrollbars[SBAR_LEFT]
4126 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4128 val, size, max);
4129 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004130 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4132 val, size, max);
4133 }
4134 }
4135#ifdef FEAT_VERTSPLIT
4136 prev_curwin = curwin;
4137#endif
4138 --hold_gui_events;
4139}
4140
4141/*
4142 * Enable or disable a scrollbar.
4143 * Check for scrollbars for vertically split windows which are not enabled
4144 * sometimes.
4145 */
4146 static void
4147gui_do_scrollbar(wp, which, enable)
4148 win_T *wp;
4149 int which; /* SBAR_LEFT or SBAR_RIGHT */
4150 int enable; /* TRUE to enable scrollbar */
4151{
4152#ifdef FEAT_VERTSPLIT
4153 int midcol = curwin->w_wincol + curwin->w_width / 2;
4154 int has_midcol = (wp->w_wincol <= midcol
4155 && wp->w_wincol + wp->w_width >= midcol);
4156
4157 /* Only enable scrollbars that contain the middle column of the current
4158 * window. */
4159 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4160 {
4161 /* Scrollbars only on one side. Don't enable scrollbars that don't
4162 * contain the middle column of the current window. */
4163 if (!has_midcol)
4164 enable = FALSE;
4165 }
4166 else
4167 {
4168 /* Scrollbars on both sides. Don't enable scrollbars that neither
4169 * contain the middle column of the current window nor are on the far
4170 * side. */
4171 if (midcol > Columns / 2)
4172 {
4173 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4174 enable = FALSE;
4175 }
4176 else
4177 {
4178 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4179 : !has_midcol)
4180 enable = FALSE;
4181 }
4182 }
4183#endif
4184 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4185}
4186
4187/*
4188 * Scroll a window according to the values set in the globals current_scrollbar
4189 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4190 * or FALSE otherwise.
4191 */
4192 int
4193gui_do_scroll()
4194{
4195 win_T *wp, *save_wp;
4196 int i;
4197 long nlines;
4198 pos_T old_cursor;
4199 linenr_T old_topline;
4200#ifdef FEAT_DIFF
4201 int old_topfill;
4202#endif
4203
4204 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4205 if (wp == NULL)
4206 break;
4207 if (wp == NULL)
4208 /* Couldn't find window */
4209 return FALSE;
4210
4211 /*
4212 * Compute number of lines to scroll. If zero, nothing to do.
4213 */
4214 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4215 if (nlines == 0)
4216 return FALSE;
4217
4218 save_wp = curwin;
4219 old_topline = wp->w_topline;
4220#ifdef FEAT_DIFF
4221 old_topfill = wp->w_topfill;
4222#endif
4223 old_cursor = wp->w_cursor;
4224 curwin = wp;
4225 curbuf = wp->w_buffer;
4226 if (nlines < 0)
4227 scrolldown(-nlines, gui.dragged_wp == NULL);
4228 else
4229 scrollup(nlines, gui.dragged_wp == NULL);
4230 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4231 * the mouse-up event already, but we still want it to behave like when
4232 * dragging. But not the next click in an arrow. */
4233 if (gui.dragged_sb == SBAR_NONE)
4234 gui.dragged_wp = NULL;
4235
4236 if (old_topline != wp->w_topline
4237#ifdef FEAT_DIFF
4238 || old_topfill != wp->w_topfill
4239#endif
4240 )
4241 {
4242 if (p_so != 0)
4243 {
4244 cursor_correct(); /* fix window for 'so' */
4245 update_topline(); /* avoid up/down jump */
4246 }
4247 if (old_cursor.lnum != wp->w_cursor.lnum)
4248 coladvance(wp->w_curswant);
4249#ifdef FEAT_SCROLLBIND
4250 wp->w_scbind_pos = wp->w_topline;
4251#endif
4252 }
4253
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004254 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4255 validate_cursor();
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 curwin = save_wp;
4258 curbuf = save_wp->w_buffer;
4259
4260 /*
4261 * Don't call updateWindow() when nothing has changed (it will overwrite
4262 * the status line!).
4263 */
4264 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004265 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266#ifdef FEAT_DIFF
4267 || old_topfill != wp->w_topfill
4268#endif
4269 )
4270 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004271 int type = VALID;
4272
4273#ifdef FEAT_INS_EXPAND
4274 if (pum_visible())
4275 {
4276 type = NOT_VALID;
4277 wp->w_lines_valid = 0;
4278 }
4279#endif
4280 /* Don't set must_redraw here, it may cause the popup menu to
4281 * disappear when losing focus after a scrollbar drag. */
4282 if (wp->w_redr_type < type)
4283 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 updateWindow(wp); /* update window, status line, and cmdline */
4285 }
4286
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004287#ifdef FEAT_INS_EXPAND
4288 /* May need to redraw the popup menu. */
4289 if (pum_visible())
4290 pum_redraw();
4291#endif
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4294}
4295
4296
4297/*
4298 * Horizontal scrollbar stuff:
4299 */
4300
4301/*
4302 * Return length of line "lnum" for horizontal scrolling.
4303 */
4304 static colnr_T
4305scroll_line_len(lnum)
4306 linenr_T lnum;
4307{
4308 char_u *p;
4309 colnr_T col;
4310 int w;
4311
4312 p = ml_get(lnum);
4313 col = 0;
4314 if (*p != NUL)
4315 for (;;)
4316 {
4317 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004318 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (*p == NUL) /* don't count the last character */
4320 break;
4321 col += w;
4322 }
4323 return col;
4324}
4325
4326/* Remember which line is currently the longest, so that we don't have to
4327 * search for it when scrolling horizontally. */
4328static linenr_T longest_lnum = 0;
4329
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004330/*
4331 * Find longest visible line number. If this is not possible (or not desired,
4332 * by setting 'h' in "guioptions") then the current line number is returned.
4333 */
4334 static linenr_T
4335gui_find_longest_lnum()
4336{
4337 linenr_T ret = 0;
4338
4339 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4340 * line numbers, topline and botline can be invalid when displaying is
4341 * postponed. */
4342 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4343 && curwin->w_topline <= curwin->w_cursor.lnum
4344 && curwin->w_botline > curwin->w_cursor.lnum
4345 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4346 {
4347 linenr_T lnum;
4348 colnr_T n;
4349 long max = 0;
4350
4351 /* Use maximum of all visible lines. Remember the lnum of the
4352 * longest line, closest to the cursor line. Used when scrolling
4353 * below. */
4354 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4355 {
4356 n = scroll_line_len(lnum);
4357 if (n > (colnr_T)max)
4358 {
4359 max = n;
4360 ret = lnum;
4361 }
4362 else if (n == (colnr_T)max
4363 && abs((int)(lnum - curwin->w_cursor.lnum))
4364 < abs((int)(ret - curwin->w_cursor.lnum)))
4365 ret = lnum;
4366 }
4367 }
4368 else
4369 /* Use cursor line only. */
4370 ret = curwin->w_cursor.lnum;
4371
4372 return ret;
4373}
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 static void
4376gui_update_horiz_scrollbar(force)
4377 int force;
4378{
4379 long value, size, max; /* need 32 bit ints here */
4380
4381 if (!gui.which_scrollbars[SBAR_BOTTOM])
4382 return;
4383
4384 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4385 return;
4386
4387 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4388 return;
4389
4390 /*
4391 * It is possible for the cursor to be invalid if we're in the middle of
4392 * something (like changing files). If so, don't do anything for now.
4393 */
4394 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4395 {
4396 gui.bottom_sbar.value = -1;
4397 return;
4398 }
4399
4400 size = W_WIDTH(curwin);
4401 if (curwin->w_p_wrap)
4402 {
4403 value = 0;
4404#ifdef SCROLL_PAST_END
4405 max = 0;
4406#else
4407 max = W_WIDTH(curwin) - 1;
4408#endif
4409 }
4410 else
4411 {
4412 value = curwin->w_leftcol;
4413
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004414 longest_lnum = gui_find_longest_lnum();
4415 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417#ifdef FEAT_VIRTUALEDIT
4418 if (virtual_active())
4419 {
4420 /* May move the cursor even further to the right. */
4421 if (curwin->w_virtcol >= (colnr_T)max)
4422 max = curwin->w_virtcol;
4423 }
4424#endif
4425
4426#ifndef SCROLL_PAST_END
4427 max += W_WIDTH(curwin) - 1;
4428#endif
4429 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004430 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 size -= curwin_col_off();
4432#ifndef SCROLL_PAST_END
4433 max -= curwin_col_off();
4434#endif
4435 }
4436
4437#ifndef SCROLL_PAST_END
4438 if (value > max - size + 1)
4439 value = max - size + 1; /* limit the value to allowable range */
4440#endif
4441
4442#ifdef FEAT_RIGHTLEFT
4443 if (curwin->w_p_rl)
4444 {
4445 value = max + 1 - size - value;
4446 if (value < 0)
4447 {
4448 size += value;
4449 value = 0;
4450 }
4451 }
4452#endif
4453 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4454 && max == gui.bottom_sbar.max)
4455 return;
4456
4457 gui.bottom_sbar.value = value;
4458 gui.bottom_sbar.size = size;
4459 gui.bottom_sbar.max = max;
4460 gui.prev_wrap = curwin->w_p_wrap;
4461
4462 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4463}
4464
4465/*
4466 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4467 */
4468 int
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004469gui_do_horiz_scroll(leftcol, compute_longest_lnum)
4470 colnr_T leftcol;
4471 int compute_longest_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473 /* no wrapping, no scrolling */
4474 if (curwin->w_p_wrap)
4475 return FALSE;
4476
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004477 if (curwin->w_leftcol == leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return FALSE;
4479
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004480 curwin->w_leftcol = leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004483 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004485 && !virtual_active()
4486 && leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004488 if (compute_longest_lnum)
4489 {
4490 curwin->w_cursor.lnum = gui_find_longest_lnum();
4491 curwin->w_cursor.col = 0;
4492 }
4493 /* Do a sanity check on "longest_lnum", just in case. */
4494 else if (longest_lnum >= curwin->w_topline
4495 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
4497 curwin->w_cursor.lnum = longest_lnum;
4498 curwin->w_cursor.col = 0;
4499 }
4500 }
4501
4502 return leftcol_changed();
4503}
4504
4505/*
4506 * Check that none of the colors are the same as the background color
4507 */
4508 void
4509gui_check_colors()
4510{
4511 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4512 {
4513 gui_set_bg_color((char_u *)"White");
4514 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4515 gui_set_fg_color((char_u *)"Black");
4516 }
4517}
4518
Bram Moolenaar3918c952005-03-15 22:34:55 +00004519 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520gui_set_fg_color(name)
4521 char_u *name;
4522{
4523 gui.norm_pixel = gui_get_color(name);
4524 hl_set_fg_color_name(vim_strsave(name));
4525}
4526
Bram Moolenaar3918c952005-03-15 22:34:55 +00004527 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528gui_set_bg_color(name)
4529 char_u *name;
4530{
4531 gui.back_pixel = gui_get_color(name);
4532 hl_set_bg_color_name(vim_strsave(name));
4533}
4534
4535/*
4536 * Allocate a color by name.
4537 * Returns INVALCOLOR and gives an error message when failed.
4538 */
4539 guicolor_T
4540gui_get_color(name)
4541 char_u *name;
4542{
4543 guicolor_T t;
4544
4545 if (*name == NUL)
4546 return INVALCOLOR;
4547 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004550#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 && gui.in_use
4552#endif
4553 )
4554 EMSG2(_("E254: Cannot allocate color %s"), name);
4555 return t;
4556}
4557
4558/*
4559 * Return the grey value of a color (range 0-255).
4560 */
4561 int
4562gui_get_lightness(pixel)
4563 guicolor_T pixel;
4564{
4565 long_u rgb = gui_mch_get_rgb(pixel);
4566
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004567 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004568 + (((rgb >> 8) & 0xff) * 587)
4569 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570}
4571
4572#if defined(FEAT_GUI_X11) || defined(PROTO)
4573 void
4574gui_new_scrollbar_colors()
4575{
4576 win_T *wp;
4577
4578 /* Nothing to do if GUI hasn't started yet. */
4579 if (!gui.in_use)
4580 return;
4581
4582 FOR_ALL_WINDOWS(wp)
4583 {
4584 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4585 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4586 }
4587 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4588}
4589#endif
4590
4591/*
4592 * Call this when focus has changed.
4593 */
4594 void
4595gui_focus_change(in_focus)
4596 int in_focus;
4597{
4598/*
4599 * Skip this code to avoid drawing the cursor when debugging and switching
4600 * between the debugger window and gvim.
4601 */
4602#if 1
4603 gui.in_focus = in_focus;
4604 out_flush(); /* make sure output has been written */
4605 gui_update_cursor(TRUE, FALSE);
4606
4607# ifdef FEAT_XIM
4608 xim_set_focus(in_focus);
4609# endif
4610
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004611 /* Put events in the input queue only when allowed.
4612 * ui_focus_change() isn't called directly, because it invokes
4613 * autocommands and that must not happen asynchronously. */
4614 if (!hold_gui_events)
4615 {
4616 char_u bytes[3];
4617
4618 bytes[0] = CSI;
4619 bytes[1] = KS_EXTRA;
4620 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4621 add_to_input_buf(bytes, 3);
4622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#endif
4624}
4625
4626/*
4627 * Called when the mouse moved (but not when dragging).
4628 */
4629 void
4630gui_mouse_moved(x, y)
4631 int x;
4632 int y;
4633{
4634 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004635 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaard3667a22006-03-16 21:35:52 +00004637 /* Ignore this while still starting up. */
4638 if (!gui.in_use || gui.starting)
4639 return;
4640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#ifdef FEAT_MOUSESHAPE
4642 /* Get window pointer, and update mouse shape as well. */
4643 wp = xy2win(x, y);
4644#endif
4645
4646 /* Only handle this when 'mousefocus' set and ... */
4647 if (p_mousef
4648 && !hold_gui_events /* not holding events */
4649 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4650 && State != HITRETURN /* but not hit-return prompt */
4651 && msg_scrolled == 0 /* no scrolled message */
4652 && !need_mouse_correct /* not moving the pointer */
4653 && gui.in_focus) /* gvim in focus */
4654 {
4655 /* Don't move the mouse when it's left or right of the Vim window */
4656 if (x < 0 || x > Columns * gui.char_width)
4657 return;
4658#ifndef FEAT_MOUSESHAPE
4659 wp = xy2win(x, y);
4660#endif
4661 if (wp == curwin || wp == NULL)
4662 return; /* still in the same old window, or none at all */
4663
Bram Moolenaar9c102382006-05-03 21:26:49 +00004664#ifdef FEAT_WINDOWS
4665 /* Ignore position in the tab pages line. */
4666 if (Y_2_ROW(y) < tabline_height())
4667 return;
4668#endif
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 /*
4671 * format a mouse click on status line input
4672 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004673 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4674 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 */
4676 if (finish_op)
4677 {
4678 /* abort the current operator first */
4679 st[0] = ESC;
4680 add_to_input_buf(st, 1);
4681 }
4682 st[0] = CSI;
4683 st[1] = KS_MOUSE;
4684 st[2] = KE_FILLER;
4685 st[3] = (char_u)MOUSE_LEFT;
4686 fill_mouse_coord(st + 4,
4687#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004688 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689#else
4690 -1,
4691#endif
4692 wp->w_height + W_WINROW(wp));
4693
4694 add_to_input_buf(st, 8);
4695 st[3] = (char_u)MOUSE_RELEASE;
4696 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#ifdef FEAT_GUI_GTK
4698 /* Need to wake up the main loop */
4699 if (gtk_main_level() > 0)
4700 gtk_main_quit();
4701#endif
4702 }
4703}
4704
4705/*
4706 * Called when mouse should be moved to window with focus.
4707 */
4708 void
4709gui_mouse_correct()
4710{
4711 int x, y;
4712 win_T *wp = NULL;
4713
4714 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004715
4716 if (!(gui.in_use && p_mousef))
4717 return;
4718
4719 gui_mch_getmouse(&x, &y);
4720 /* Don't move the mouse when it's left or right of the Vim window */
4721 if (x < 0 || x > Columns * gui.char_width)
4722 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004723 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004724# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004725 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004726# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004727 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004728 wp = xy2win(x, y);
4729 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004731 validate_cline_row();
4732 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4733 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736}
4737
4738/*
4739 * Find window where the mouse pointer "y" coordinate is in.
4740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 static win_T *
4742xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004743 int x UNUSED;
4744 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746#ifdef FEAT_WINDOWS
4747 int row;
4748 int col;
4749 win_T *wp;
4750
4751 row = Y_2_ROW(y);
4752 col = X_2_COL(x);
4753 if (row < 0 || col < 0) /* before first window */
4754 return NULL;
4755 wp = mouse_find_win(&row, &col);
4756# ifdef FEAT_MOUSESHAPE
4757 if (State == HITRETURN || State == ASKMORE)
4758 {
4759 if (Y_2_ROW(y) >= msg_row)
4760 update_mouseshape(SHAPE_IDX_MOREL);
4761 else
4762 update_mouseshape(SHAPE_IDX_MORE);
4763 }
4764 else if (row > wp->w_height) /* below status line */
4765 update_mouseshape(SHAPE_IDX_CLINE);
4766# ifdef FEAT_VERTSPLIT
4767 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004768 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 update_mouseshape(SHAPE_IDX_VSEP);
4770# endif
4771 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004772 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 update_mouseshape(SHAPE_IDX_STATUS);
4774 else
4775 update_mouseshape(-2);
4776# endif
4777 return wp;
4778#else
4779 return firstwin;
4780#endif
4781}
4782
4783/*
4784 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4785 * File names may be given to redefine the args list.
4786 */
4787 void
4788ex_gui(eap)
4789 exarg_T *eap;
4790{
4791 char_u *arg = eap->arg;
4792
4793 /*
4794 * Check for "-f" argument: foreground, don't fork.
4795 * Also don't fork when started with "gvim -f".
4796 * Do fork when using "gui -b".
4797 */
4798 if (arg[0] == '-'
4799 && (arg[1] == 'f' || arg[1] == 'b')
4800 && (arg[2] == NUL || vim_iswhite(arg[2])))
4801 {
4802 gui.dofork = (arg[1] == 'b');
4803 eap->arg = skipwhite(eap->arg + 2);
4804 }
4805 if (!gui.in_use)
4806 {
4807 /* Clear the command. Needed for when forking+exiting, to avoid part
4808 * of the argument ending up after the shell prompt. */
4809 msg_clr_eos_force();
4810 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004811#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004812 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 if (!ends_excmd(*eap->arg))
4816 ex_next(eap);
4817}
4818
4819#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004820 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821/*
4822 * This is shared between Athena, Motif and GTK.
4823 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004824static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
4826/*
4827 * Callback function for do_in_runtimepath().
4828 */
4829 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004830gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004832 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004834 char_u *gfp_buffer = cookie;
4835
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 if (STRLEN(fname) >= MAXPATHL)
4837 *gfp_buffer = NUL;
4838 else
4839 STRCPY(gfp_buffer, fname);
4840}
4841
4842/*
4843 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4844 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4845 */
4846 int
4847gui_find_bitmap(name, buffer, ext)
4848 char_u *name;
4849 char_u *buffer;
4850 char *ext;
4851{
4852 if (STRLEN(name) > MAXPATHL - 14)
4853 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004854 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004855 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4856 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 return FAIL;
4858 return OK;
4859}
4860
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004861# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862/*
4863 * Given the name of the "icon=" argument, try finding the bitmap file for the
4864 * icon. If it is an absolute path name, use it as it is. Otherwise append
4865 * "ext" and search for it in 'runtimepath'.
4866 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4867 * contains "name".
4868 */
4869 void
4870gui_find_iconfile(name, buffer, ext)
4871 char_u *name;
4872 char_u *buffer;
4873 char *ext;
4874{
4875 char_u buf[MAXPATHL + 1];
4876
4877 expand_env(name, buffer, MAXPATHL);
4878 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4879 STRCPY(buffer, buf);
4880}
4881# endif
4882#endif
4883
Bram Moolenaar9372a112005-12-06 19:59:18 +00004884#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 void
4886display_errors()
4887{
4888 char_u *p;
4889
4890 if (isatty(2))
4891 fflush(stderr);
4892 else if (error_ga.ga_data != NULL)
4893 {
4894 /* avoid putting up a message box with blanks only */
4895 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4896 if (!isspace(*p))
4897 {
4898 /* Truncate a very long message, it will go off-screen. */
4899 if (STRLEN(p) > 2000)
4900 STRCPY(p + 2000 - 14, "...(truncated)");
4901 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4902 p, (char_u *)_("&Ok"), 1, NULL);
4903 break;
4904 }
4905 ga_clear(&error_ga);
4906 }
4907}
4908#endif
4909
4910#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4911/*
4912 * Return TRUE if still starting up and there is no place to enter text.
4913 * For GTK and X11 we check if stderr is not a tty, which means we were
4914 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4915 * allow typing on stdin.
4916 */
4917 int
4918no_console_input()
4919{
4920 return ((!gui.in_use || gui.starting)
4921# ifndef NO_CONSOLE
4922 && !isatty(0) && !isatty(2)
4923# endif
4924 );
4925}
4926#endif
4927
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004928#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00004929 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004930 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931/*
4932 * Update the current window and the screen.
4933 */
4934 void
4935gui_update_screen()
4936{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004937#ifdef FEAT_CONCEAL
4938 linenr_T conceal_old_cursor_line = 0;
4939 linenr_T conceal_new_cursor_line = 0;
4940 int conceal_update_lines = FALSE;
4941#endif
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 update_topline();
4944 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004945
4946#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00004947 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004948 if (!finish_op && (
4949# ifdef FEAT_AUTOCMD
4950 has_cursormoved()
4951# endif
4952# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
4953 ||
4954# endif
4955# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004956 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004957# endif
4958 )
4959 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00004960 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004961# ifdef FEAT_AUTOCMD
4962 if (has_cursormoved())
4963 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4964# endif
4965# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004966 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004967 {
4968 conceal_old_cursor_line = last_cursormoved.lnum;
4969 conceal_new_cursor_line = curwin->w_cursor.lnum;
4970 conceal_update_lines = TRUE;
4971 }
4972# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00004973 last_cursormoved = curwin->w_cursor;
4974 }
4975#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 update_screen(0); /* may need to update the screen */
4978 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004979# if defined(FEAT_CONCEAL)
4980 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004981 && (conceal_old_cursor_line != conceal_new_cursor_line
4982 || conceal_cursor_line(curwin)
4983 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004984 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004985 if (conceal_old_cursor_line != conceal_new_cursor_line)
4986 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004987 update_single_line(curwin, conceal_new_cursor_line);
4988 curwin->w_valid &= ~VALID_CROW;
4989 }
4990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 out_flush(); /* make sure output has been written */
4992 gui_update_cursor(TRUE, FALSE);
4993 gui_mch_flush();
4994}
4995#endif
4996
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004997#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4999
5000/*
5001 * Get the text to use in a find/replace dialog. Uses the last search pattern
5002 * if the argument is empty.
5003 * Returns an allocated string.
5004 */
5005 char_u *
5006get_find_dialog_text(arg, wwordp, mcasep)
5007 char_u *arg;
5008 int *wwordp; /* return: TRUE if \< \> found */
5009 int *mcasep; /* return: TRUE if \C found */
5010{
5011 char_u *text;
5012
5013 if (*arg == NUL)
5014 text = last_search_pat();
5015 else
5016 text = arg;
5017 if (text != NULL)
5018 {
5019 text = vim_strsave(text);
5020 if (text != NULL)
5021 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005022 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 int i;
5024
5025 /* Remove "\V" */
5026 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5027 {
5028 mch_memmove(text, text + 2, (size_t)(len - 1));
5029 len -= 2;
5030 }
5031
5032 /* Recognize "\c" and "\C" and remove. */
5033 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5034 {
5035 *mcasep = (text[1] == 'C');
5036 mch_memmove(text, text + 2, (size_t)(len - 1));
5037 len -= 2;
5038 }
5039
5040 /* Recognize "\<text\>" and remove. */
5041 if (len >= 4
5042 && STRNCMP(text, "\\<", 2) == 0
5043 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5044 {
5045 *wwordp = TRUE;
5046 mch_memmove(text, text + 2, (size_t)(len - 4));
5047 text[len - 4] = NUL;
5048 }
5049
5050 /* Recognize "\/" or "\?" and remove. */
5051 for (i = 0; i + 1 < len; ++i)
5052 if (text[i] == '\\' && (text[i + 1] == '/'
5053 || text[i + 1] == '?'))
5054 {
5055 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5056 --len;
5057 }
5058 }
5059 }
5060 return text;
5061}
5062
5063/*
5064 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5065 */
5066 static void
5067concat_esc(gap, text, what)
5068 garray_T *gap;
5069 char_u *text;
5070 int what;
5071{
5072 while (*text != NUL)
5073 {
5074#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005075 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (l > 1)
5078 {
5079 while (--l >= 0)
5080 ga_append(gap, *text++);
5081 continue;
5082 }
5083#endif
5084 if (*text == what)
5085 ga_append(gap, '\\');
5086 ga_append(gap, *text);
5087 ++text;
5088 }
5089}
5090
5091/*
5092 * Handle the press of a button in the find-replace dialog.
5093 * Return TRUE when something was added to the input buffer.
5094 */
5095 int
5096gui_do_findrepl(flags, find_text, repl_text, down)
5097 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5098 char_u *find_text;
5099 char_u *repl_text;
5100 int down; /* Search downwards. */
5101{
5102 garray_T ga;
5103 int i;
5104 int type = (flags & FRD_TYPE_MASK);
5105 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005106 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005107 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005108 static int busy = FALSE;
5109
5110 /* When the screen is being updated we should not change buffers and
5111 * windows structures, it may cause freed memory to be used. Also don't
5112 * do this recursively (pressing "Find" quickly several times. */
5113 if (updating_screen || busy)
5114 return FALSE;
5115
5116 /* refuse replace when text cannot be changed */
5117 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5118 return FALSE;
5119
5120 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005123 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ga_concat(&ga, (char_u *)"%s/");
5125
5126 ga_concat(&ga, (char_u *)"\\V");
5127 if (flags & FRD_MATCH_CASE)
5128 ga_concat(&ga, (char_u *)"\\C");
5129 else
5130 ga_concat(&ga, (char_u *)"\\c");
5131 if (flags & FRD_WHOLE_WORD)
5132 ga_concat(&ga, (char_u *)"\\<");
5133 if (type == FRD_REPLACEALL || down)
5134 concat_esc(&ga, find_text, '/'); /* escape slashes */
5135 else
5136 concat_esc(&ga, find_text, '?'); /* escape '?' */
5137 if (flags & FRD_WHOLE_WORD)
5138 ga_concat(&ga, (char_u *)"\\>");
5139
5140 if (type == FRD_REPLACEALL)
5141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005143 /* escape / and \ */
5144 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5145 if (p != NULL)
5146 ga_concat(&ga, p);
5147 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005149 }
5150 ga_append(&ga, NUL);
5151
5152 if (type == FRD_REPLACE)
5153 {
5154 /* Do the replacement when the text at the cursor matches. Thus no
5155 * replacement is done if the cursor was moved! */
5156 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5157 regmatch.rm_ic = 0;
5158 if (regmatch.regprog != NULL)
5159 {
5160 p = ml_get_cursor();
5161 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5162 && regmatch.startp[0] == p)
5163 {
5164 /* Clear the command line to remove any old "No match"
5165 * error. */
5166 msg_end_prompt();
5167
5168 if (u_save_cursor() == OK)
5169 {
5170 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005171 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005172
5173 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005174 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005175 ins_str(repl_text);
5176 }
5177 }
5178 else
5179 MSG(_("No match at cursor, finding next"));
5180 vim_free(regmatch.regprog);
5181 }
5182 }
5183
5184 if (type == FRD_REPLACEALL)
5185 {
5186 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005187 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 do_cmdline_cmd(ga.ga_data);
5189 }
5190 else
5191 {
5192 /* Search for the next match. */
5193 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005195 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 msg_scroll = i; /* don't let an error message set msg_scroll */
5197 }
5198
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005199 /* Don't want to pass did_emsg to other code, it may cause disabling
5200 * syntax HL if we were busy redrawing. */
5201 did_emsg = save_did_emsg;
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (State & (NORMAL | INSERT))
5204 {
5205 gui_update_screen(); /* update the screen */
5206 msg_didout = 0; /* overwrite any message */
5207 need_wait_return = FALSE; /* don't wait for return */
5208 }
5209
5210 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005211 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 return (ga.ga_len > 0);
5213}
5214
5215#endif
5216
5217#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5218 || defined(FEAT_GUI_MSWIN) \
5219 || defined(FEAT_GUI_MAC) \
5220 || defined(PROTO)
5221
5222#ifdef FEAT_WINDOWS
5223static void gui_wingoto_xy __ARGS((int x, int y));
5224
5225/*
5226 * Jump to the window at specified point (x, y).
5227 */
5228 static void
5229gui_wingoto_xy(x, y)
5230 int x;
5231 int y;
5232{
5233 int row = Y_2_ROW(y);
5234 int col = X_2_COL(x);
5235 win_T *wp;
5236
5237 if (row >= 0 && col >= 0)
5238 {
5239 wp = mouse_find_win(&row, &col);
5240 if (wp != NULL && wp != curwin)
5241 win_goto(wp);
5242 }
5243}
5244#endif
5245
5246/*
5247 * Process file drop. Mouse cursor position, key modifiers, name of files
5248 * and count of files are given. Argument "fnames[count]" has full pathnames
5249 * of dropped files, they will be freed in this function, and caller can't use
5250 * fnames after call this function.
5251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 void
5253gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005254 int x UNUSED;
5255 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 int_u modifiers;
5257 char_u **fnames;
5258 int count;
5259{
5260 int i;
5261 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005262 static int entered = FALSE;
5263
5264 /*
5265 * This function is called by event handlers. Just in case we get a
5266 * second event before the first one is handled, ignore the second one.
5267 * Not sure if this can ever happen, just in case.
5268 */
5269 if (entered)
5270 return;
5271 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 /*
5274 * When the cursor is at the command line, add the file names to the
5275 * command line, don't edit the files.
5276 */
5277 if (State & CMDLINE)
5278 {
5279 shorten_filenames(fnames, count);
5280 for (i = 0; i < count; ++i)
5281 {
5282 if (fnames[i] != NULL)
5283 {
5284 if (i > 0)
5285 add_to_input_buf((char_u*)" ", 1);
5286
5287 /* We don't know what command is used thus we can't be sure
5288 * about which characters need to be escaped. Only escape the
5289 * most common ones. */
5290# ifdef BACKSLASH_IN_FILENAME
5291 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5292# else
5293 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5294# endif
5295 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005296 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 vim_free(p);
5298 vim_free(fnames[i]);
5299 }
5300 }
5301 vim_free(fnames);
5302 }
5303 else
5304 {
5305 /* Go to the window under mouse cursor, then shorten given "fnames" by
5306 * current window, because a window can have local current dir. */
5307# ifdef FEAT_WINDOWS
5308 gui_wingoto_xy(x, y);
5309# endif
5310 shorten_filenames(fnames, count);
5311
5312 /* If Shift held down, remember the first item. */
5313 if ((modifiers & MOUSE_SHIFT) != 0)
5314 p = vim_strsave(fnames[0]);
5315 else
5316 p = NULL;
5317
5318 /* Handle the drop, :edit or :split to get to the file. This also
5319 * frees fnames[]. Skip this if there is only one item it's a
5320 * directory and Shift is held down. */
5321 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5322 && mch_isdir(fnames[0]))
5323 {
5324 vim_free(fnames[0]);
5325 vim_free(fnames);
5326 }
5327 else
5328 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5329
5330 /* If Shift held down, change to first file's directory. If the first
5331 * item is a directory, change to that directory (and let the explorer
5332 * plugin show the contents). */
5333 if (p != NULL)
5334 {
5335 if (mch_isdir(p))
5336 {
5337 if (mch_chdir((char *)p) == 0)
5338 shorten_fnames(TRUE);
5339 }
5340 else if (vim_chdirfile(p) == OK)
5341 shorten_fnames(TRUE);
5342 vim_free(p);
5343 }
5344
5345 /* Update the screen display */
5346 update_screen(NOT_VALID);
5347# ifdef FEAT_MENU
5348 gui_update_menus(0);
5349# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005350#ifdef FEAT_TITLE
5351 maketitle();
5352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 setcursor();
5354 out_flush();
5355 gui_update_cursor(FALSE, FALSE);
5356 gui_mch_flush();
5357 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005358
5359 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360}
5361#endif