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