blob: e42cb40449a724949a32302b107db8cd1bf86d78 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
16#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
17static void set_guifontwide __ARGS((char_u *font_name));
18#endif
19static void gui_check_pos __ARGS((void));
20static void gui_position_components __ARGS((int));
21static void gui_outstr __ARGS((char_u *, int));
22static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
23#ifdef HAVE_GTK2
24static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25#endif
26static void gui_delete_lines __ARGS((int row, int count));
27static void gui_insert_lines __ARGS((int row, int count));
28static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
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));
34static void gui_update_horiz_scrollbar __ARGS((int));
Bram Moolenaar3918c952005-03-15 22:34:55 +000035static void gui_set_fg_color __ARGS((char_u *name));
36static void gui_set_bg_color __ARGS((char_u *name));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037static win_T *xy2win __ARGS((int x, int y));
38
39static int can_update_cursor = TRUE; /* can display the cursor */
40
41/*
42 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
43 * this makes the thumb indicate the part of the text that is shown. Motif
44 * can't do this.
45 */
46#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
47# define SCROLL_PAST_END
48#endif
49
50/*
51 * gui_start -- Called when user wants to start the GUI.
52 *
53 * Careful: This function can be called recursively when there is a ":gui"
54 * command in the .gvimrc file. Only the first call should fork, not the
55 * recursive call.
56 */
57 void
58gui_start()
59{
60 char_u *old_term;
61#if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X)
62# define MAY_FORK
63 int dofork = TRUE;
64#endif
65 static int recursive = 0;
66
67 old_term = vim_strsave(T_NAME);
68
69 /*
70 * Set_termname() will call gui_init() to start the GUI.
71 * Set the "starting" flag, to indicate that the GUI will start.
72 *
73 * We don't want to open the GUI shell until after we've read .gvimrc,
74 * otherwise we don't know what font we will use, and hence we don't know
75 * what size the shell should be. So if there are errors in the .gvimrc
76 * file, they will have to go to the terminal: Set full_screen to FALSE.
77 * full_screen will be set to TRUE again by a successful termcapinit().
78 */
79 settmode(TMODE_COOK); /* stop RAW mode */
80 if (full_screen)
81 cursor_on(); /* needed for ":gui" in .vimrc */
82 gui.starting = TRUE;
83 full_screen = FALSE;
84
85#ifdef MAY_FORK
86 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
87 dofork = FALSE;
88#endif
89 ++recursive;
90
91 termcapinit((char_u *)"builtin_gui");
92 gui.starting = recursive - 1;
93
94 if (!gui.in_use) /* failed to start GUI */
95 {
96 termcapinit(old_term); /* back to old term settings */
97 settmode(TMODE_RAW); /* restart RAW mode */
98#ifdef FEAT_TITLE
99 set_title_defaults(); /* set 'title' and 'icon' again */
100#endif
101 }
102
103 vim_free(old_term);
104
Bram Moolenaar9372a112005-12-06 19:59:18 +0000105#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 if (gui.in_use)
107 /* Display error messages in a dialog now. */
108 display_errors();
109#endif
110
111#if defined(MAY_FORK) && !defined(__QNXNTO__)
112 /*
113 * Quit the current process and continue in the child.
114 * Makes "gvim file" disconnect from the shell it was started in.
115 * Don't do this when Vim was started with "-f" or the 'f' flag is present
116 * in 'guioptions'.
117 */
118 if (gui.in_use && dofork)
119 {
120 int pipefd[2]; /* pipe between parent and child */
121 int pipe_error;
122 char dummy;
123 pid_t pid = -1;
124
125 /* Setup a pipe between the child and the parent, so that the parent
126 * knows when the child has done the setsid() call and is allowed to
127 * exit. */
128 pipe_error = (pipe(pipefd) < 0);
129 pid = fork();
130 if (pid > 0) /* Parent */
131 {
132 /* Give the child some time to do the setsid(), otherwise the
133 * exit() may kill the child too (when starting gvim from inside a
134 * gvim). */
135 if (pipe_error)
136 ui_delay(300L, TRUE);
137 else
138 {
139 /* The read returns when the child closes the pipe (or when
140 * the child dies for some reason). */
141 close(pipefd[1]);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000142 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 close(pipefd[0]);
144 }
145
146 /* When swapping screens we may need to go to the next line, e.g.,
147 * after a hit-enter prompt and using ":gui". */
148 if (newline_on_exit)
149 mch_errmsg("\r\n");
150
151 /*
152 * The parent must skip the normal exit() processing, the child
153 * will do it. For example, GTK messes up signals when exiting.
154 */
155 _exit(0);
156 }
157
158# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
159 /*
160 * Change our process group. On some systems/shells a CTRL-C in the
161 * shell where Vim was started would otherwise kill gvim!
162 */
163 if (pid == 0) /* child */
164# if defined(HAVE_SETSID)
165 (void)setsid();
166# else
167 (void)setpgid(0, 0);
168# endif
169# endif
170 if (!pipe_error)
171 {
172 close(pipefd[0]);
173 close(pipefd[1]);
174 }
175
176# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
177 /* Tell the session manager our new PID */
178 gui_mch_forked();
179# endif
180 }
181#else
182# if defined(__QNXNTO__)
183 if (gui.in_use && dofork)
184 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
185 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
186# endif
187#endif
188
189#ifdef FEAT_AUTOCMD
Bram Moolenaar265e5072006-08-29 16:13:22 +0000190 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
191 * the GUIFailed event. */
192 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
193 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#endif
195
196 --recursive;
197}
198
199/*
200 * Call this when vim starts up, whether or not the GUI is started
201 */
202 void
203gui_prepare(argc, argv)
204 int *argc;
205 char **argv;
206{
207 gui.in_use = FALSE; /* No GUI yet (maybe later) */
208 gui.starting = FALSE; /* No GUI yet (maybe later) */
209 gui_mch_prepare(argc, argv);
210}
211
212/*
213 * Try initializing the GUI and check if it can be started.
214 * Used from main() to check early if "vim -g" can start the GUI.
215 * Used from gui_init() to prepare for starting the GUI.
216 * Returns FAIL or OK.
217 */
218 int
219gui_init_check()
220{
221 static int result = MAYBE;
222
223 if (result != MAYBE)
224 {
225 if (result == FAIL)
226 EMSG(_("E229: Cannot start the GUI"));
227 return result;
228 }
229
230 gui.shell_created = FALSE;
231 gui.dying = FALSE;
232 gui.in_focus = TRUE; /* so the guicursor setting works */
233 gui.dragged_sb = SBAR_NONE;
234 gui.dragged_wp = NULL;
235 gui.pointer_hidden = FALSE;
236 gui.col = 0;
237 gui.row = 0;
238 gui.num_cols = Columns;
239 gui.num_rows = Rows;
240
241 gui.cursor_is_valid = FALSE;
242 gui.scroll_region_top = 0;
243 gui.scroll_region_bot = Rows - 1;
244 gui.scroll_region_left = 0;
245 gui.scroll_region_right = Columns - 1;
246 gui.highlight_mask = HL_NORMAL;
247 gui.char_width = 1;
248 gui.char_height = 1;
249 gui.char_ascent = 0;
250 gui.border_width = 0;
251
252 gui.norm_font = NOFONT;
253#ifndef HAVE_GTK2
254 gui.bold_font = NOFONT;
255 gui.ital_font = NOFONT;
256 gui.boldital_font = NOFONT;
257# ifdef FEAT_XFONTSET
258 gui.fontset = NOFONTSET;
259# endif
260#endif
261
262#ifdef FEAT_MENU
263# ifndef HAVE_GTK2
264# ifdef FONTSET_ALWAYS
265 gui.menu_fontset = NOFONTSET;
266# else
267 gui.menu_font = NOFONT;
268# endif
269# endif
270 gui.menu_is_active = TRUE; /* default: include menu */
271# ifndef FEAT_GUI_GTK
272 gui.menu_height = MENU_DEFAULT_HEIGHT;
273 gui.menu_width = 0;
274# endif
275#endif
276#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
277 gui.toolbar_height = 0;
278#endif
279#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
280 gui.footer_height = 0;
281#endif
282#ifdef FEAT_BEVAL_TIP
283 gui.tooltip_fontset = NOFONTSET;
284#endif
285
286 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
287 gui.prev_wrap = -1;
288
289#ifdef ALWAYS_USE_GUI
290 result = OK;
291#else
292 result = gui_mch_init_check();
293#endif
294 return result;
295}
296
297/*
298 * This is the call which starts the GUI.
299 */
300 void
301gui_init()
302{
303 win_T *wp;
304 static int recursive = 0;
305
306 /*
307 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
308 * function will then be executed at the first call, the rest by the
309 * recursive call. This allow the shell to be opened halfway reading a
310 * gvimrc file.
311 */
312 if (!recursive)
313 {
314 ++recursive;
315
316 clip_init(TRUE);
317
318 /* If can't initialize, don't try doing the rest */
319 if (gui_init_check() == FAIL)
320 {
321 --recursive;
322 clip_init(FALSE);
323 return;
324 }
325
326 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000327 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
328 * breaks the Paste toolbar button.
329 */
330 set_option_value((char_u *)"paste", 0L, NULL, 0);
331
332 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 * Set up system-wide default menus.
334 */
335#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
336 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
337 {
338 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000339 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 sys_menu = FALSE;
341 }
342#endif
343
344 /*
345 * Switch on the mouse by default, unless the user changed it already.
346 * This can then be changed in the .gvimrc.
347 */
348 if (!option_was_set((char_u *)"mouse"))
349 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000350 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 /*
353 * If -U option given, use only the initializations from that file and
354 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
355 */
356 if (use_gvimrc != NULL)
357 {
358 if (STRCMP(use_gvimrc, "NONE") != 0
359 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000360 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
362 }
363 else
364 {
365 /*
366 * Get system wide defaults for gvim, only when file name defined.
367 */
368#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000369 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#endif
371
372 /*
373 * Try to read GUI initialization commands from the following
374 * places:
375 * - environment variable GVIMINIT
376 * - the user gvimrc file (~/.gvimrc)
377 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
378 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
379 * The first that exists is used, the rest is ignored.
380 */
381 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
383 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
386 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
388 )
389 {
390#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393 }
394
395 /*
396 * Read initialization commands from ".gvimrc" in current
397 * directory. This is only done if the 'exrc' option is set.
398 * Because of security reasons we disallow shell and write
399 * commands now, except for unix if the file is owned by the user
400 * or 'secure' option has been reset in environment of global
401 * ".gvimrc".
402 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
403 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
404 */
405 if (p_exrc)
406 {
407#ifdef UNIX
408 {
409 struct stat s;
410
411 /* if ".gvimrc" file is not owned by user, set 'secure'
412 * mode */
413 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
414 secure = p_secure;
415 }
416#else
417 secure = p_secure;
418#endif
419
420 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
421 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
422#ifdef SYS_GVIMRC_FILE
423 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
424 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
425#endif
426#ifdef USR_GVIMRC_FILE2
427 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
428 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
429#endif
430#ifdef USR_GVIMRC_FILE3
431 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
432 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
433#endif
434 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000435 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 if (secure == 2)
438 need_wait_return = TRUE;
439 secure = 0;
440 }
441 }
442
443 if (need_wait_return || msg_didany)
444 wait_return(TRUE);
445
446 --recursive;
447 }
448
449 /* If recursive call opened the shell, return here from the first call */
450 if (gui.in_use)
451 return;
452
453 /*
454 * Create the GUI shell.
455 */
456 gui.in_use = TRUE; /* Must be set after menus have been set up */
457 if (gui_mch_init() == FAIL)
458 goto error;
459
460 /* Avoid a delay for an error message that was printed in the terminal
461 * where Vim was started. */
462 emsg_on_display = FALSE;
463 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000464 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 need_wait_return = FALSE;
466 msg_didany = FALSE;
467
468 /*
469 * Check validity of any generic resources that may have been loaded.
470 */
471 if (gui.border_width < 0)
472 gui.border_width = 0;
473
474 /*
475 * Set up the fonts. First use a font specified with "-fn" or "-font".
476 */
477 if (font_argument != NULL)
478 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
479 if (
480#ifdef FEAT_XFONTSET
481 (*p_guifontset == NUL
482 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
483#endif
484 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
485 : p_guifont, FALSE) == FAIL)
486 {
487 EMSG(_("E665: Cannot start GUI, no valid font found"));
488 goto error2;
489 }
490#ifdef FEAT_MBYTE
491 if (gui_get_wide_font() == FAIL)
492 EMSG(_("E231: 'guifontwide' invalid"));
493#endif
494
495 gui.num_cols = Columns;
496 gui.num_rows = Rows;
497 gui_reset_scroll_region();
498
499 /* Create initial scrollbars */
500 FOR_ALL_WINDOWS(wp)
501 {
502 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
503 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
504 }
505 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
506
507#ifdef FEAT_MENU
508 gui_create_initial_menus(root_menu);
509#endif
510#ifdef FEAT_SUN_WORKSHOP
511 if (usingSunWorkShop)
512 workshop_init();
513#endif
514#ifdef FEAT_SIGN_ICONS
515 sign_gui_started();
516#endif
517
518 /* Configure the desired menu and scrollbars */
519 gui_init_which_components(NULL);
520
521 /* All components of the GUI have been created now */
522 gui.shell_created = TRUE;
523
524#ifndef FEAT_GUI_GTK
525 /* Set the shell size, adjusted for the screen size. For GTK this only
526 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000527 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#endif
529#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
530 /* Need to set the size of the menubar after all the menus have been
531 * created. */
532 gui_mch_compute_menu_height((Widget)0);
533#endif
534
535 /*
536 * Actually open the GUI shell.
537 */
538 if (gui_mch_open() != FAIL)
539 {
540#ifdef FEAT_TITLE
541 maketitle();
542 resettitle();
543#endif
544 init_gui_options();
545#ifdef FEAT_ARABIC
546 /* Our GUI can't do bidi. */
547 p_tbidi = FALSE;
548#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000549#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 /* Give GTK+ a chance to put all widget's into place. */
551 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000552
553# ifdef FEAT_MENU
554 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
555 * It was still there to make F10 work. */
556 if (vim_strchr(p_go, GO_MENUS) == NULL)
557 {
558 --gui.starting;
559 gui_mch_enable_menu(FALSE);
560 ++gui.starting;
561 gui_mch_update();
562 }
563# endif
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000566 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000568 /* When 'lines' was set while starting up the topframe may have to be
569 * resized. */
570 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000571
572#ifdef FEAT_BEVAL
573 /* Always create the Balloon Evaluation area, but disable it when
574 * 'ballooneval' is off */
575# ifdef FEAT_GUI_GTK
576 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
577 &general_beval_cb, NULL);
578# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000579# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000580 {
581 extern Widget textArea;
582 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000583 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000584 }
585# else
586# ifdef FEAT_GUI_W32
587 balloonEval = gui_mch_create_beval_area(NULL, NULL,
588 &general_beval_cb, NULL);
589# endif
590# endif
591# endif
592 if (!p_beval)
593 gui_mch_disable_beval_area(balloonEval);
594#endif
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_NETBEANS_INTG
597 if (starting == 0 && usingNetbeans)
598 /* Tell the client that it can start sending commands. */
599 netbeans_startup_done();
600#endif
601#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
602 if (!im_xim_isvalid_imactivate())
603 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
604#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000605 /* When 'cmdheight' was set during startup it may not have taken
606 * effect yet. */
607 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000608 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 return;
611 }
612
613error2:
614#ifdef FEAT_GUI_X11
615 /* undo gui_mch_init() */
616 gui_mch_uninit();
617#endif
618
619error:
620 gui.in_use = FALSE;
621 clip_init(FALSE);
622}
623
624
625 void
626gui_exit(rc)
627 int rc;
628{
629#ifndef __BEOS__
630 /* don't free the fonts, it leads to a BUS error
631 * richard@whitequeen.com Jul 99 */
632 free_highlight_fonts();
633#endif
634 gui.in_use = FALSE;
635 gui_mch_exit(rc);
636}
637
Bram Moolenaar9372a112005-12-06 19:59:18 +0000638#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000640# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
642 * Called when the GUI shell is closed by the user. If there are no changed
643 * files Vim exits, otherwise there will be a dialog to ask the user what to
644 * do.
645 * When this function returns, Vim should NOT exit!
646 */
647 void
648gui_shell_closed()
649{
650 cmdmod_T save_cmdmod;
651
652 save_cmdmod = cmdmod;
653
654 /* Only exit when there are no changed files */
655 exiting = TRUE;
656# ifdef FEAT_BROWSE
657 cmdmod.browse = TRUE;
658# endif
659# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
660 cmdmod.confirm = TRUE;
661# endif
662 /* If there are changed buffers, present the user with a dialog if
663 * possible, otherwise give an error message. */
664 if (!check_changed_any(FALSE))
665 getout(0);
666
667 exiting = FALSE;
668 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000669 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671#endif
672
673/*
674 * Set the font. "font_list" is a a comma separated list of font names. The
675 * first font name that works is used. If none is found, use the default
676 * font.
677 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
678 * Return OK when able to set the font. When it failed FAIL is returned and
679 * the fonts are unchanged.
680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 int
682gui_init_font(font_list, fontset)
683 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000684 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686#define FONTLEN 320
687 char_u font_name[FONTLEN];
688 int font_list_empty = FALSE;
689 int ret = FAIL;
690
691 if (!gui.in_use)
692 return FAIL;
693
694 font_name[0] = NUL;
695 if (*font_list == NUL)
696 font_list_empty = TRUE;
697 else
698 {
699#ifdef FEAT_XFONTSET
700 /* When using a fontset, the whole list of fonts is one name. */
701 if (fontset)
702 ret = gui_mch_init_font(font_list, TRUE);
703 else
704#endif
705 while (*font_list != NUL)
706 {
707 /* Isolate one comma separated font name. */
708 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
709
710 /* Careful!!! The Win32 version of gui_mch_init_font(), when
711 * called with "*" will change p_guifont to the selected font
712 * name, which frees the old value. This makes font_list
713 * invalid. Thus when OK is returned here, font_list must no
714 * longer be used! */
715 if (gui_mch_init_font(font_name, FALSE) == OK)
716 {
717#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
718 /* If it's a Unicode font, try setting 'guifontwide' to a
719 * similar double-width font. */
720 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
721 && strstr((char *)font_name, "10646") != NULL)
722 set_guifontwide(font_name);
723#endif
724 ret = OK;
725 break;
726 }
727 }
728 }
729
730 if (ret != OK
731 && STRCMP(font_list, "*") != 0
732 && (font_list_empty || gui.norm_font == NOFONT))
733 {
734 /*
735 * Couldn't load any font in 'font_list', keep the current font if
736 * there is one. If 'font_list' is empty, or if there is no current
737 * font, tell gui_mch_init_font() to try to find a font we can load.
738 */
739 ret = gui_mch_init_font(NULL, FALSE);
740 }
741
742 if (ret == OK)
743 {
744#ifndef HAVE_GTK2
745 /* Set normal font as current font */
746# ifdef FEAT_XFONTSET
747 if (gui.fontset != NOFONTSET)
748 gui_mch_set_fontset(gui.fontset);
749 else
750# endif
751 gui_mch_set_font(gui.norm_font);
752#endif
753 gui_set_shellsize(FALSE,
754#ifdef MSWIN
755 TRUE
756#else
757 FALSE
758#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000759 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761
762 return ret;
763}
764
765#if defined(FEAT_MBYTE) || defined(PROTO)
766# ifndef HAVE_GTK2
767/*
768 * Try setting 'guifontwide' to a font twice as wide as "name".
769 */
770 static void
771set_guifontwide(name)
772 char_u *name;
773{
774 int i = 0;
775 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
776 char_u *wp = NULL;
777 char_u *p;
778 GuiFont font;
779
780 wp = wide_name;
781 for (p = name; *p != NUL; ++p)
782 {
783 *wp++ = *p;
784 if (*p == '-')
785 {
786 ++i;
787 if (i == 6) /* font type: change "--" to "-*-" */
788 {
789 if (p[1] == '-')
790 *wp++ = '*';
791 }
792 else if (i == 12) /* found the width */
793 {
794 ++p;
795 i = getdigits(&p);
796 if (i != 0)
797 {
798 /* Double the width specification. */
799 sprintf((char *)wp, "%d%s", i * 2, p);
800 font = gui_mch_get_font(wide_name, FALSE);
801 if (font != NOFONT)
802 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000803 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 gui.wide_font = font;
805 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000806 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
808 }
809 break;
810 }
811 }
812 }
813}
814# endif /* !HAVE_GTK2 */
815
816/*
817 * Get the font for 'guifontwide'.
818 * Return FAIL for an invalid font name.
819 */
820 int
821gui_get_wide_font()
822{
823 GuiFont font = NOFONT;
824 char_u font_name[FONTLEN];
825 char_u *p;
826
827 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
828 return OK; /* Will give an error message later. */
829
830 if (p_guifontwide != NULL && *p_guifontwide != NUL)
831 {
832 for (p = p_guifontwide; *p != NUL; )
833 {
834 /* Isolate one comma separated font name. */
835 (void)copy_option_part(&p, font_name, FONTLEN, ",");
836 font = gui_mch_get_font(font_name, FALSE);
837 if (font != NOFONT)
838 break;
839 }
840 if (font == NOFONT)
841 return FAIL;
842 }
843
844 gui_mch_free_font(gui.wide_font);
845#ifdef HAVE_GTK2
846 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
847 if (font != NOFONT && gui.norm_font != NOFONT
848 && pango_font_description_equal(font, gui.norm_font))
849 {
850 gui.wide_font = NOFONT;
851 gui_mch_free_font(font);
852 }
853 else
854#endif
855 gui.wide_font = font;
856 return OK;
857}
858#endif
859
860 void
861gui_set_cursor(row, col)
862 int row;
863 int col;
864{
865 gui.row = row;
866 gui.col = col;
867}
868
869/*
870 * gui_check_pos - check if the cursor is on the screen.
871 */
872 static void
873gui_check_pos()
874{
875 if (gui.row >= screen_Rows)
876 gui.row = screen_Rows - 1;
877 if (gui.col >= screen_Columns)
878 gui.col = screen_Columns - 1;
879 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
880 gui.cursor_is_valid = FALSE;
881}
882
883/*
884 * Redraw the cursor if necessary or when forced.
885 * Careful: The contents of ScreenLines[] must match what is on the screen,
886 * otherwise this goes wrong. May need to call out_flush() first.
887 */
888 void
889gui_update_cursor(force, clear_selection)
890 int force; /* when TRUE, update even when not moved */
891 int clear_selection;/* clear selection under cursor */
892{
893 int cur_width = 0;
894 int cur_height = 0;
895 int old_hl_mask;
896 int idx;
897 int id;
898 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
899 int cattr; /* cursor attributes */
900 int attr;
901 attrentry_T *aep = NULL;
902
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +0000903 /* Don't update the cursor when halfway busy scrolling or the screen size
904 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
905 if (!can_update_cursor || screen_Columns != gui.num_cols
906 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 return;
908
909 gui_check_pos();
910 if (!gui.cursor_is_valid || force
911 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
912 {
913 gui_undraw_cursor();
914 if (gui.row < 0)
915 return;
916#ifdef USE_IM_CONTROL
917 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
918 im_set_position(gui.row, gui.col);
919#endif
920 gui.cursor_row = gui.row;
921 gui.cursor_col = gui.col;
922
923 /* Only write to the screen after ScreenLines[] has been initialized */
924 if (!screen_cleared || ScreenLines == NULL)
925 return;
926
927 /* Clear the selection if we are about to write over it */
928 if (clear_selection)
929 clip_may_clear_selection(gui.row, gui.row);
930 /* Check that the cursor is inside the shell (resizing may have made
931 * it invalid) */
932 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
933 return;
934
935 gui.cursor_is_valid = TRUE;
936
937 /*
938 * How the cursor is drawn depends on the current mode.
939 */
940 idx = get_shape_idx(FALSE);
941 if (State & LANGMAP)
942 id = shape_table[idx].id_lm;
943 else
944 id = shape_table[idx].id;
945
946 /* get the colors and attributes for the cursor. Default is inverted */
947 cfg = INVALCOLOR;
948 cbg = INVALCOLOR;
949 cattr = HL_INVERSE;
950 gui_mch_set_blinking(shape_table[idx].blinkwait,
951 shape_table[idx].blinkon,
952 shape_table[idx].blinkoff);
953 if (id > 0)
954 {
955 cattr = syn_id2colors(id, &cfg, &cbg);
956#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
957 {
958 static int iid;
959 guicolor_T fg, bg;
960
Bram Moolenaarc236c162008-07-13 17:41:49 +0000961 if (
Bram Moolenaara48b1652009-06-24 16:32:08 +0000962# if defined(HAVE_GTK2) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000963 preedit_get_status()
964# else
965 im_get_status()
966# endif
967 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
969 iid = syn_name2id((char_u *)"CursorIM");
970 if (iid > 0)
971 {
972 syn_id2colors(iid, &fg, &bg);
973 if (bg != INVALCOLOR)
974 cbg = bg;
975 if (fg != INVALCOLOR)
976 cfg = fg;
977 }
978 }
979 }
980#endif
981 }
982
983 /*
984 * Get the attributes for the character under the cursor.
985 * When no cursor color was given, use the character color.
986 */
987 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
988 if (attr > HL_ALL)
989 aep = syn_gui_attr2entry(attr);
990 if (aep != NULL)
991 {
992 attr = aep->ae_attr;
993 if (cfg == INVALCOLOR)
994 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
995 : aep->ae_u.gui.fg_color);
996 if (cbg == INVALCOLOR)
997 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
998 : aep->ae_u.gui.bg_color);
999 }
1000 if (cfg == INVALCOLOR)
1001 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1002 if (cbg == INVALCOLOR)
1003 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1004
1005#ifdef FEAT_XIM
1006 if (aep != NULL)
1007 {
1008 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1009 : aep->ae_u.gui.bg_color);
1010 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1011 : aep->ae_u.gui.fg_color);
1012 if (xim_bg_color == INVALCOLOR)
1013 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1014 : gui.back_pixel;
1015 if (xim_fg_color == INVALCOLOR)
1016 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1017 : gui.norm_pixel;
1018 }
1019 else
1020 {
1021 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1022 : gui.back_pixel;
1023 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1024 : gui.norm_pixel;
1025 }
1026#endif
1027
1028 attr &= ~HL_INVERSE;
1029 if (cattr & HL_INVERSE)
1030 {
1031 cc = cbg;
1032 cbg = cfg;
1033 cfg = cc;
1034 }
1035 cattr &= ~HL_INVERSE;
1036
1037 /*
1038 * When we don't have window focus, draw a hollow cursor.
1039 */
1040 if (!gui.in_focus)
1041 {
1042 gui_mch_draw_hollow_cursor(cbg);
1043 return;
1044 }
1045
1046 old_hl_mask = gui.highlight_mask;
1047 if (shape_table[idx].shape == SHAPE_BLOCK
1048#ifdef FEAT_HANGULIN
1049 || composing_hangul
1050#endif
1051 )
1052 {
1053 /*
1054 * Draw the text character with the cursor colors. Use the
1055 * character attributes plus the cursor attributes.
1056 */
1057 gui.highlight_mask = (cattr | attr);
1058#ifdef FEAT_HANGULIN
1059 if (composing_hangul)
1060 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1061 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1062 else
1063#endif
1064 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1065 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1066 }
1067 else
1068 {
1069#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1070 int col_off = FALSE;
1071#endif
1072 /*
1073 * First draw the partial cursor, then overwrite with the text
1074 * character, using a transparent background.
1075 */
1076 if (shape_table[idx].shape == SHAPE_VER)
1077 {
1078 cur_height = gui.char_height;
1079 cur_width = (gui.char_width * shape_table[idx].percentage
1080 + 99) / 100;
1081 }
1082 else
1083 {
1084 cur_height = (gui.char_height * shape_table[idx].percentage
1085 + 99) / 100;
1086 cur_width = gui.char_width;
1087 }
1088#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001089 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1090 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
1092 /* Double wide character. */
1093 if (shape_table[idx].shape != SHAPE_VER)
1094 cur_width += gui.char_width;
1095# ifdef FEAT_RIGHTLEFT
1096 if (CURSOR_BAR_RIGHT)
1097 {
1098 /* gui.col points to the left halve of the character but
1099 * the vertical line needs to be on the right halve.
1100 * A double-wide horizontal line is also drawn from the
1101 * right halve in gui_mch_draw_part_cursor(). */
1102 col_off = TRUE;
1103 ++gui.col;
1104 }
1105# endif
1106 }
1107#endif
1108 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1109#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1110 if (col_off)
1111 --gui.col;
1112#endif
1113
1114#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1115 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1116 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1117 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1118 (guicolor_T)0, (guicolor_T)0, 0);
1119#endif
1120 }
1121 gui.highlight_mask = old_hl_mask;
1122 }
1123}
1124
1125#if defined(FEAT_MENU) || defined(PROTO)
1126 void
1127gui_position_menu()
1128{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001129# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (gui.menu_is_active && gui.in_use)
1131 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1132# endif
1133}
1134#endif
1135
1136/*
1137 * Position the various GUI components (text area, menu). The vertical
1138 * scrollbars are NOT handled here. See gui_update_scrollbars().
1139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 static void
1141gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001142 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143{
1144 int text_area_x;
1145 int text_area_y;
1146 int text_area_width;
1147 int text_area_height;
1148
1149 /* avoid that moving components around generates events */
1150 ++hold_gui_events;
1151
1152 text_area_x = 0;
1153 if (gui.which_scrollbars[SBAR_LEFT])
1154 text_area_x += gui.scrollbar_width;
1155
1156 text_area_y = 0;
1157#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1158 gui.menu_width = total_width;
1159 if (gui.menu_is_active)
1160 text_area_y += gui.menu_height;
1161#endif
1162#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1163 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1164 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1165#endif
1166
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001167# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001168 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001169 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001170 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001171#endif
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1174 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1175 {
1176# ifdef FEAT_GUI_ATHENA
1177 gui_mch_set_toolbar_pos(0, text_area_y,
1178 gui.menu_width, gui.toolbar_height);
1179# endif
1180 text_area_y += gui.toolbar_height;
1181 }
1182#endif
1183
1184 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1185 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1186
1187 gui_mch_set_text_area_pos(text_area_x,
1188 text_area_y,
1189 text_area_width,
1190 text_area_height
1191#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1192 + xim_get_status_area_height()
1193#endif
1194 );
1195#ifdef FEAT_MENU
1196 gui_position_menu();
1197#endif
1198 if (gui.which_scrollbars[SBAR_BOTTOM])
1199 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1200 text_area_x,
1201 text_area_y + text_area_height,
1202 text_area_width,
1203 gui.scrollbar_height);
1204 gui.left_sbar_x = 0;
1205 gui.right_sbar_x = text_area_x + text_area_width;
1206
1207 --hold_gui_events;
1208}
1209
Bram Moolenaar02743632005-07-25 20:42:36 +00001210/*
1211 * Get the width of the widgets and decorations to the side of the text area.
1212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 int
1214gui_get_base_width()
1215{
1216 int base_width;
1217
1218 base_width = 2 * gui.border_offset;
1219 if (gui.which_scrollbars[SBAR_LEFT])
1220 base_width += gui.scrollbar_width;
1221 if (gui.which_scrollbars[SBAR_RIGHT])
1222 base_width += gui.scrollbar_width;
1223 return base_width;
1224}
1225
Bram Moolenaar02743632005-07-25 20:42:36 +00001226/*
1227 * Get the height of the widgets and decorations above and below the text area.
1228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 int
1230gui_get_base_height()
1231{
1232 int base_height;
1233
1234 base_height = 2 * gui.border_offset;
1235 if (gui.which_scrollbars[SBAR_BOTTOM])
1236 base_height += gui.scrollbar_height;
1237#ifdef FEAT_GUI_GTK
1238 /* We can't take the sizes properly into account until anything is
1239 * realized. Therefore we recalculate all the values here just before
1240 * setting the size. (--mdcki) */
1241#else
1242# ifdef FEAT_MENU
1243 if (gui.menu_is_active)
1244 base_height += gui.menu_height;
1245# endif
1246# ifdef FEAT_TOOLBAR
1247 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1248# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1249 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1250# else
1251 base_height += gui.toolbar_height;
1252# endif
1253# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001254# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1255 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001256 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001257 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259# ifdef FEAT_FOOTER
1260 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1261 base_height += gui.footer_height;
1262# endif
1263# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1264 base_height += gui_mch_text_area_extra_height();
1265# endif
1266#endif
1267 return base_height;
1268}
1269
1270/*
1271 * Should be called after the GUI shell has been resized. Its arguments are
1272 * the new width and height of the shell in pixels.
1273 */
1274 void
1275gui_resize_shell(pixel_width, pixel_height)
1276 int pixel_width;
1277 int pixel_height;
1278{
1279 static int busy = FALSE;
1280
1281 if (!gui.shell_created) /* ignore when still initializing */
1282 return;
1283
1284 /*
1285 * Can't resize the screen while it is being redrawn. Remember the new
1286 * size and handle it later.
1287 */
1288 if (updating_screen || busy)
1289 {
1290 new_pixel_width = pixel_width;
1291 new_pixel_height = pixel_height;
1292 return;
1293 }
1294
1295again:
1296 busy = TRUE;
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 /* Flush pending output before redrawing */
1299 out_flush();
1300
1301 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001302 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 gui_position_components(pixel_width);
1305
1306 gui_reset_scroll_region();
1307 /*
1308 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1309 * at the last line here (why does it have to be one row too low?).
1310 */
1311 if (State == ASKMORE || State == CONFIRM)
1312 gui.row = gui.num_rows;
1313
1314 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1315 * the safe side. */
1316 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1317 || gui.num_rows != Rows || gui.num_cols != Columns)
1318 shell_resized();
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 gui_update_scrollbars(TRUE);
1321 gui_update_cursor(FALSE, TRUE);
1322#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1323 xim_set_status_area();
1324#endif
1325
1326 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 /*
1329 * We could have been called again while redrawing the screen.
1330 * Need to do it all again with the latest size then.
1331 */
1332 if (new_pixel_height)
1333 {
1334 pixel_width = new_pixel_width;
1335 pixel_height = new_pixel_height;
1336 new_pixel_width = 0;
1337 new_pixel_height = 0;
1338 goto again;
1339 }
1340}
1341
1342/*
1343 * Check if gui_resize_shell() must be called.
1344 */
1345 void
1346gui_may_resize_shell()
1347{
1348 int h, w;
1349
1350 if (new_pixel_height)
1351 {
1352 /* careful: gui_resize_shell() may postpone the resize again if we
1353 * were called indirectly by it */
1354 w = new_pixel_width;
1355 h = new_pixel_height;
1356 new_pixel_width = 0;
1357 new_pixel_height = 0;
1358 gui_resize_shell(w, h);
1359 }
1360}
1361
1362 int
1363gui_get_shellsize()
1364{
1365 Rows = gui.num_rows;
1366 Columns = gui.num_cols;
1367 return OK;
1368}
1369
1370/*
1371 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001372 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1373 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001376gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001377 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001379 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381 int base_width;
1382 int base_height;
1383 int width;
1384 int height;
1385 int min_width;
1386 int min_height;
1387 int screen_w;
1388 int screen_h;
1389
1390 if (!gui.shell_created)
1391 return;
1392
1393#ifdef MSWIN
1394 /* If not setting to a user specified size and maximized, calculate the
1395 * number of characters that fit in the maximized window. */
1396 if (!mustset && gui_mch_maximized())
1397 {
1398 gui_mch_newfont();
1399 return;
1400 }
1401#endif
1402
1403 base_width = gui_get_base_width();
1404 base_height = gui_get_base_height();
1405#ifdef USE_SUN_WORKSHOP
1406 if (!mustset && usingSunWorkShop
1407 && workshop_get_width_height(&width, &height))
1408 {
1409 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1410 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1411 }
1412 else
1413#endif
1414 {
1415 width = Columns * gui.char_width + base_width;
1416 height = Rows * gui.char_height + base_height;
1417 }
1418
1419 if (fit_to_display)
1420 {
1421 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001422 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424 Columns = (screen_w - base_width) / gui.char_width;
1425 if (Columns < MIN_COLUMNS)
1426 Columns = MIN_COLUMNS;
1427 width = Columns * gui.char_width + base_width;
1428 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001429 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
1431 Rows = (screen_h - base_height) / gui.char_height;
1432 check_shellsize();
1433 height = Rows * gui.char_height + base_height;
1434 }
1435 }
1436 gui.num_cols = Columns;
1437 gui.num_rows = Rows;
1438
1439 min_width = base_width + MIN_COLUMNS * gui.char_width;
1440 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001441# ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001442 min_height += tabline_height() * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001446 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (fit_to_display)
1448 {
1449 int x, y;
1450
1451 /* Some window managers put the Vim window left of/above the screen. */
1452 gui_mch_update();
1453 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1454 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1455 }
1456
1457 gui_position_components(width);
1458 gui_update_scrollbars(TRUE);
1459 gui_reset_scroll_region();
1460}
1461
1462/*
1463 * Called when Rows and/or Columns has changed.
1464 */
1465 void
1466gui_new_shellsize()
1467{
1468 gui_reset_scroll_region();
1469}
1470
1471/*
1472 * Make scroll region cover whole screen.
1473 */
1474 void
1475gui_reset_scroll_region()
1476{
1477 gui.scroll_region_top = 0;
1478 gui.scroll_region_bot = gui.num_rows - 1;
1479 gui.scroll_region_left = 0;
1480 gui.scroll_region_right = gui.num_cols - 1;
1481}
1482
1483 void
1484gui_start_highlight(mask)
1485 int mask;
1486{
1487 if (mask > HL_ALL) /* highlight code */
1488 gui.highlight_mask = mask;
1489 else /* mask */
1490 gui.highlight_mask |= mask;
1491}
1492
1493 void
1494gui_stop_highlight(mask)
1495 int mask;
1496{
1497 if (mask > HL_ALL) /* highlight code */
1498 gui.highlight_mask = HL_NORMAL;
1499 else /* mask */
1500 gui.highlight_mask &= ~mask;
1501}
1502
1503/*
1504 * Clear a rectangular region of the screen from text pos (row1, col1) to
1505 * (row2, col2) inclusive.
1506 */
1507 void
1508gui_clear_block(row1, col1, row2, col2)
1509 int row1;
1510 int col1;
1511 int row2;
1512 int col2;
1513{
1514 /* Clear the selection if we are about to write over it */
1515 clip_may_clear_selection(row1, row2);
1516
1517 gui_mch_clear_block(row1, col1, row2, col2);
1518
1519 /* Invalidate cursor if it was in this block */
1520 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1521 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1522 gui.cursor_is_valid = FALSE;
1523}
1524
1525/*
1526 * Write code to update the cursor later. This avoids the need to flush the
1527 * output buffer before calling gui_update_cursor().
1528 */
1529 void
1530gui_update_cursor_later()
1531{
1532 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1533}
1534
1535 void
1536gui_write(s, len)
1537 char_u *s;
1538 int len;
1539{
1540 char_u *p;
1541 int arg1 = 0, arg2 = 0;
1542 /* this doesn't make sense, disabled until someone can explain why it
1543 * would be needed */
1544#if 0 && (defined(RISCOS) || defined(WIN16))
1545 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1546 our redraw speed will suffer */
1547#else
1548 int force_cursor = FALSE; /* force cursor update */
1549#endif
1550 int force_scrollbar = FALSE;
1551 static win_T *old_curwin = NULL;
1552
1553/* #define DEBUG_GUI_WRITE */
1554#ifdef DEBUG_GUI_WRITE
1555 {
1556 int i;
1557 char_u *str;
1558
1559 printf("gui_write(%d):\n ", len);
1560 for (i = 0; i < len; i++)
1561 if (s[i] == ESC)
1562 {
1563 if (i != 0)
1564 printf("\n ");
1565 printf("<ESC>");
1566 }
1567 else
1568 {
1569 str = transchar_byte(s[i]);
1570 if (str[0] && str[1])
1571 printf("<%s>", (char *)str);
1572 else
1573 printf("%s", (char *)str);
1574 }
1575 printf("\n");
1576 }
1577#endif
1578 while (len)
1579 {
1580 if (s[0] == ESC && s[1] == '|')
1581 {
1582 p = s + 2;
1583 if (VIM_ISDIGIT(*p))
1584 {
1585 arg1 = getdigits(&p);
1586 if (p > s + len)
1587 break;
1588 if (*p == ';')
1589 {
1590 ++p;
1591 arg2 = getdigits(&p);
1592 if (p > s + len)
1593 break;
1594 }
1595 }
1596 switch (*p)
1597 {
1598 case 'C': /* Clear screen */
1599 clip_scroll_selection(9999);
1600 gui_mch_clear_all();
1601 gui.cursor_is_valid = FALSE;
1602 force_scrollbar = TRUE;
1603 break;
1604 case 'M': /* Move cursor */
1605 gui_set_cursor(arg1, arg2);
1606 break;
1607 case 's': /* force cursor (shape) update */
1608 force_cursor = TRUE;
1609 break;
1610 case 'R': /* Set scroll region */
1611 if (arg1 < arg2)
1612 {
1613 gui.scroll_region_top = arg1;
1614 gui.scroll_region_bot = arg2;
1615 }
1616 else
1617 {
1618 gui.scroll_region_top = arg2;
1619 gui.scroll_region_bot = arg1;
1620 }
1621 break;
1622#ifdef FEAT_VERTSPLIT
1623 case 'V': /* Set vertical scroll region */
1624 if (arg1 < arg2)
1625 {
1626 gui.scroll_region_left = arg1;
1627 gui.scroll_region_right = arg2;
1628 }
1629 else
1630 {
1631 gui.scroll_region_left = arg2;
1632 gui.scroll_region_right = arg1;
1633 }
1634 break;
1635#endif
1636 case 'd': /* Delete line */
1637 gui_delete_lines(gui.row, 1);
1638 break;
1639 case 'D': /* Delete lines */
1640 gui_delete_lines(gui.row, arg1);
1641 break;
1642 case 'i': /* Insert line */
1643 gui_insert_lines(gui.row, 1);
1644 break;
1645 case 'I': /* Insert lines */
1646 gui_insert_lines(gui.row, arg1);
1647 break;
1648 case '$': /* Clear to end-of-line */
1649 gui_clear_block(gui.row, gui.col, gui.row,
1650 (int)Columns - 1);
1651 break;
1652 case 'h': /* Turn on highlighting */
1653 gui_start_highlight(arg1);
1654 break;
1655 case 'H': /* Turn off highlighting */
1656 gui_stop_highlight(arg1);
1657 break;
1658 case 'f': /* flash the window (visual bell) */
1659 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1660 break;
1661 default:
1662 p = s + 1; /* Skip the ESC */
1663 break;
1664 }
1665 len -= (int)(++p - s);
1666 s = p;
1667 }
1668 else if (
1669#ifdef EBCDIC
1670 CtrlChar(s[0]) != 0 /* Ctrl character */
1671#else
1672 s[0] < 0x20 /* Ctrl character */
1673#endif
1674#ifdef FEAT_SIGN_ICONS
1675 && s[0] != SIGN_BYTE
1676# ifdef FEAT_NETBEANS_INTG
1677 && s[0] != MULTISIGN_BYTE
1678# endif
1679#endif
1680 )
1681 {
1682 if (s[0] == '\n') /* NL */
1683 {
1684 gui.col = 0;
1685 if (gui.row < gui.scroll_region_bot)
1686 gui.row++;
1687 else
1688 gui_delete_lines(gui.scroll_region_top, 1);
1689 }
1690 else if (s[0] == '\r') /* CR */
1691 {
1692 gui.col = 0;
1693 }
1694 else if (s[0] == '\b') /* Backspace */
1695 {
1696 if (gui.col)
1697 --gui.col;
1698 }
1699 else if (s[0] == Ctrl_L) /* cursor-right */
1700 {
1701 ++gui.col;
1702 }
1703 else if (s[0] == Ctrl_G) /* Beep */
1704 {
1705 gui_mch_beep();
1706 }
1707 /* Other Ctrl character: shouldn't happen! */
1708
1709 --len; /* Skip this char */
1710 ++s;
1711 }
1712 else
1713 {
1714 p = s;
1715 while (len > 0 && (
1716#ifdef EBCDIC
1717 CtrlChar(*p) == 0
1718#else
1719 *p >= 0x20
1720#endif
1721#ifdef FEAT_SIGN_ICONS
1722 || *p == SIGN_BYTE
1723# ifdef FEAT_NETBEANS_INTG
1724 || *p == MULTISIGN_BYTE
1725# endif
1726#endif
1727 ))
1728 {
1729 len--;
1730 p++;
1731 }
1732 gui_outstr(s, (int)(p - s));
1733 s = p;
1734 }
1735 }
1736
1737 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1738 * set). */
1739 if (force_cursor)
1740 gui_update_cursor(TRUE, TRUE);
1741
1742 /* When switching to another window the dragging must have stopped.
1743 * Required for GTK, dragged_sb isn't reset. */
1744 if (old_curwin != curwin)
1745 gui.dragged_sb = SBAR_NONE;
1746
1747 /* Update the scrollbars after clearing the screen or when switched
1748 * to another window.
1749 * Update the horizontal scrollbar always, it's difficult to check all
1750 * situations where it might change. */
1751 if (force_scrollbar || old_curwin != curwin)
1752 gui_update_scrollbars(force_scrollbar);
1753 else
1754 gui_update_horiz_scrollbar(FALSE);
1755 old_curwin = curwin;
1756
1757 /*
1758 * We need to make sure this is cleared since Athena doesn't tell us when
1759 * he is done dragging. Do the same for GTK.
1760 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001761#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 gui.dragged_sb = SBAR_NONE;
1763#endif
1764
1765 gui_mch_flush(); /* In case vim decides to take a nap */
1766}
1767
1768/*
1769 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1770 * produces wrong results. Call gui_dont_update_cursor() before that code and
1771 * gui_can_update_cursor() afterwards.
1772 */
1773 void
1774gui_dont_update_cursor()
1775{
1776 if (gui.in_use)
1777 {
1778 /* Undraw the cursor now, we probably can't do it after the change. */
1779 gui_undraw_cursor();
1780 can_update_cursor = FALSE;
1781 }
1782}
1783
1784 void
1785gui_can_update_cursor()
1786{
1787 can_update_cursor = TRUE;
1788 /* No need to update the cursor right now, there is always more output
1789 * after scrolling. */
1790}
1791
1792 static void
1793gui_outstr(s, len)
1794 char_u *s;
1795 int len;
1796{
1797 int this_len;
1798#ifdef FEAT_MBYTE
1799 int cells;
1800#endif
1801
1802 if (len == 0)
1803 return;
1804
1805 if (len < 0)
1806 len = (int)STRLEN(s);
1807
1808 while (len > 0)
1809 {
1810#ifdef FEAT_MBYTE
1811 if (has_mbyte)
1812 {
1813 /* Find out how many chars fit in the current line. */
1814 cells = 0;
1815 for (this_len = 0; this_len < len; )
1816 {
1817 cells += (*mb_ptr2cells)(s + this_len);
1818 if (gui.col + cells > Columns)
1819 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001820 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
1822 if (this_len > len)
1823 this_len = len; /* don't include following composing char */
1824 }
1825 else
1826#endif
1827 if (gui.col + len > Columns)
1828 this_len = Columns - gui.col;
1829 else
1830 this_len = len;
1831
1832 (void)gui_outstr_nowrap(s, this_len,
1833 0, (guicolor_T)0, (guicolor_T)0, 0);
1834 s += this_len;
1835 len -= this_len;
1836#ifdef FEAT_MBYTE
1837 /* fill up for a double-width char that doesn't fit. */
1838 if (len > 0 && gui.col < Columns)
1839 (void)gui_outstr_nowrap((char_u *)" ", 1,
1840 0, (guicolor_T)0, (guicolor_T)0, 0);
1841#endif
1842 /* The cursor may wrap to the next line. */
1843 if (gui.col >= Columns)
1844 {
1845 gui.col = 0;
1846 gui.row++;
1847 }
1848 }
1849}
1850
1851/*
1852 * Output one character (may be one or two display cells).
1853 * Caller must check for valid "off".
1854 * Returns FAIL or OK, just like gui_outstr_nowrap().
1855 */
1856 static int
1857gui_screenchar(off, flags, fg, bg, back)
1858 int off; /* Offset from start of screen */
1859 int flags;
1860 guicolor_T fg, bg; /* colors for cursor */
1861 int back; /* backup this many chars when using bold trick */
1862{
1863#ifdef FEAT_MBYTE
1864 char_u buf[MB_MAXBYTES + 1];
1865
1866 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1867 if (enc_utf8 && ScreenLines[off] == 0)
1868 return OK;
1869
1870 if (enc_utf8 && ScreenLinesUC[off] != 0)
1871 /* Draw UTF-8 multi-byte character. */
1872 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1873 flags, fg, bg, back);
1874
1875 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1876 {
1877 buf[0] = ScreenLines[off];
1878 buf[1] = ScreenLines2[off];
1879 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1880 }
1881
1882 /* Draw non-multi-byte character or DBCS character. */
1883 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001884 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 flags, fg, bg, back);
1886#else
1887 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1888#endif
1889}
1890
1891#ifdef HAVE_GTK2
1892/*
1893 * Output the string at the given screen position. This is used in place
1894 * of gui_screenchar() where possible because Pango needs as much context
1895 * as possible to work nicely. It's a lot faster as well.
1896 */
1897 static int
1898gui_screenstr(off, len, flags, fg, bg, back)
1899 int off; /* Offset from start of screen */
1900 int len; /* string length in screen cells */
1901 int flags;
1902 guicolor_T fg, bg; /* colors for cursor */
1903 int back; /* backup this many chars when using bold trick */
1904{
1905 char_u *buf;
1906 int outlen = 0;
1907 int i;
1908 int retval;
1909
1910 if (len <= 0) /* "cannot happen"? */
1911 return OK;
1912
1913 if (enc_utf8)
1914 {
1915 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1916 if (buf == NULL)
1917 return OK; /* not much we could do here... */
1918
1919 for (i = off; i < off + len; ++i)
1920 {
1921 if (ScreenLines[i] == 0)
1922 continue; /* skip second half of double-width char */
1923
1924 if (ScreenLinesUC[i] == 0)
1925 buf[outlen++] = ScreenLines[i];
1926 else
1927 outlen += utfc_char2bytes(i, buf + outlen);
1928 }
1929
1930 buf[outlen] = NUL; /* only to aid debugging */
1931 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1932 vim_free(buf);
1933
1934 return retval;
1935 }
1936 else if (enc_dbcs == DBCS_JPNU)
1937 {
1938 buf = alloc((unsigned)(len * 2 + 1));
1939 if (buf == NULL)
1940 return OK; /* not much we could do here... */
1941
1942 for (i = off; i < off + len; ++i)
1943 {
1944 buf[outlen++] = ScreenLines[i];
1945
1946 /* handle double-byte single-width char */
1947 if (ScreenLines[i] == 0x8e)
1948 buf[outlen++] = ScreenLines2[i];
1949 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1950 buf[outlen++] = ScreenLines[++i];
1951 }
1952
1953 buf[outlen] = NUL; /* only to aid debugging */
1954 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1955 vim_free(buf);
1956
1957 return retval;
1958 }
1959 else
1960 {
1961 return gui_outstr_nowrap(&ScreenLines[off], len,
1962 flags, fg, bg, back);
1963 }
1964}
1965#endif /* HAVE_GTK2 */
1966
1967/*
1968 * Output the given string at the current cursor position. If the string is
1969 * too long to fit on the line, then it is truncated.
1970 * "flags":
1971 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1972 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001973 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 * background.
1975 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1976 * it.
1977 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1978 * FAIL (the caller should start drawing "back" chars back).
1979 */
1980 int
1981gui_outstr_nowrap(s, len, flags, fg, bg, back)
1982 char_u *s;
1983 int len;
1984 int flags;
1985 guicolor_T fg, bg; /* colors for cursor */
1986 int back; /* backup this many chars when using bold trick */
1987{
1988 long_u highlight_mask;
1989 long_u hl_mask_todo;
1990 guicolor_T fg_color;
1991 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001992 guicolor_T sp_color;
Bram Moolenaar9372a112005-12-06 19:59:18 +00001993#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 GuiFont font = NOFONT;
1995# ifdef FEAT_XFONTSET
1996 GuiFontset fontset = NOFONTSET;
1997# endif
1998#endif
1999 attrentry_T *aep = NULL;
2000 int draw_flags;
2001 int col = gui.col;
2002#ifdef FEAT_SIGN_ICONS
2003 int draw_sign = FALSE;
2004# ifdef FEAT_NETBEANS_INTG
2005 int multi_sign = FALSE;
2006# endif
2007#endif
2008
2009 if (len < 0)
2010 len = (int)STRLEN(s);
2011 if (len == 0)
2012 return OK;
2013
2014#ifdef FEAT_SIGN_ICONS
2015 if (*s == SIGN_BYTE
2016# ifdef FEAT_NETBEANS_INTG
2017 || *s == MULTISIGN_BYTE
2018# endif
2019 )
2020 {
2021# ifdef FEAT_NETBEANS_INTG
2022 if (*s == MULTISIGN_BYTE)
2023 multi_sign = TRUE;
2024# endif
2025 /* draw spaces instead */
2026 s = (char_u *)" ";
2027 if (len == 1 && col > 0)
2028 --col;
2029 len = 2;
2030 draw_sign = TRUE;
2031 highlight_mask = 0;
2032 }
2033 else
2034#endif
2035 if (gui.highlight_mask > HL_ALL)
2036 {
2037 aep = syn_gui_attr2entry(gui.highlight_mask);
2038 if (aep == NULL) /* highlighting not set */
2039 highlight_mask = 0;
2040 else
2041 highlight_mask = aep->ae_attr;
2042 }
2043 else
2044 highlight_mask = gui.highlight_mask;
2045 hl_mask_todo = highlight_mask;
2046
Bram Moolenaar9372a112005-12-06 19:59:18 +00002047#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 /* Set the font */
2049 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2050 font = aep->ae_u.gui.font;
2051# ifdef FEAT_XFONTSET
2052 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2053 fontset = aep->ae_u.gui.fontset;
2054# endif
2055 else
2056 {
2057# ifdef FEAT_XFONTSET
2058 if (gui.fontset != NOFONTSET)
2059 fontset = gui.fontset;
2060 else
2061# endif
2062 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2063 {
2064 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2065 {
2066 font = gui.boldital_font;
2067 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2068 }
2069 else if (gui.bold_font != NOFONT)
2070 {
2071 font = gui.bold_font;
2072 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2073 }
2074 else
2075 font = gui.norm_font;
2076 }
2077 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2078 {
2079 font = gui.ital_font;
2080 hl_mask_todo &= ~HL_ITALIC;
2081 }
2082 else
2083 font = gui.norm_font;
2084 }
2085# ifdef FEAT_XFONTSET
2086 if (fontset != NOFONTSET)
2087 gui_mch_set_fontset(fontset);
2088 else
2089# endif
2090 gui_mch_set_font(font);
2091#endif
2092
2093 draw_flags = 0;
2094
2095 /* Set the color */
2096 bg_color = gui.back_pixel;
2097 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2098 {
2099 draw_flags |= DRAW_CURSOR;
2100 fg_color = fg;
2101 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002102 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
2104 else if (aep != NULL)
2105 {
2106 fg_color = aep->ae_u.gui.fg_color;
2107 if (fg_color == INVALCOLOR)
2108 fg_color = gui.norm_pixel;
2109 bg_color = aep->ae_u.gui.bg_color;
2110 if (bg_color == INVALCOLOR)
2111 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002112 sp_color = aep->ae_u.gui.sp_color;
2113 if (sp_color == INVALCOLOR)
2114 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002119 sp_color = fg_color;
2120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2123 {
2124#if defined(AMIGA) || defined(RISCOS)
2125 gui_mch_set_colors(bg_color, fg_color);
2126#else
2127 gui_mch_set_fg_color(bg_color);
2128 gui_mch_set_bg_color(fg_color);
2129#endif
2130 }
2131 else
2132 {
2133#if defined(AMIGA) || defined(RISCOS)
2134 gui_mch_set_colors(fg_color, bg_color);
2135#else
2136 gui_mch_set_fg_color(fg_color);
2137 gui_mch_set_bg_color(bg_color);
2138#endif
2139 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002140 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /* Clear the selection if we are about to write over it */
2143 if (!(flags & GUI_MON_NOCLEAR))
2144 clip_may_clear_selection(gui.row, gui.row);
2145
2146
2147#ifndef MSWIN16_FASTTEXT
2148 /* If there's no bold font, then fake it */
2149 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2150 draw_flags |= DRAW_BOLD;
2151#endif
2152
2153 /*
2154 * When drawing bold or italic characters the spill-over from the left
2155 * neighbor may be destroyed. Let the caller backup to start redrawing
2156 * just after a blank.
2157 */
2158 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2159 return FAIL;
2160
Bram Moolenaar9372a112005-12-06 19:59:18 +00002161#if defined(RISCOS) || defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 /* If there's no italic font, then fake it.
2163 * For GTK2, we don't need a different font for italic style. */
2164 if (hl_mask_todo & HL_ITALIC)
2165 draw_flags |= DRAW_ITALIC;
2166
2167 /* Do we underline the text? */
2168 if (hl_mask_todo & HL_UNDERLINE)
2169 draw_flags |= DRAW_UNDERL;
2170#else
2171 /* Do we underline the text? */
2172 if ((hl_mask_todo & HL_UNDERLINE)
2173# ifndef MSWIN16_FASTTEXT
2174 || (hl_mask_todo & HL_ITALIC)
2175# endif
2176 )
2177 draw_flags |= DRAW_UNDERL;
2178#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002179 /* Do we undercurl the text? */
2180 if (hl_mask_todo & HL_UNDERCURL)
2181 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002183 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (flags & GUI_MON_TRS_CURSOR)
2185 draw_flags |= DRAW_TRANSP;
2186
2187 /*
2188 * Draw the text.
2189 */
2190#ifdef HAVE_GTK2
2191 /* The value returned is the length in display cells */
2192 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2193#else
2194# ifdef FEAT_MBYTE
2195 if (enc_utf8)
2196 {
2197 int start; /* index of bytes to be drawn */
2198 int cells; /* cellwidth of bytes to be drawn */
2199 int thislen; /* length of bytes to be drawin */
2200 int cn; /* cellwidth of current char */
2201 int i; /* index of current char */
2202 int c; /* current char value */
2203 int cl; /* byte length of current char */
2204 int comping; /* current char is composing */
2205 int scol = col; /* screen column */
2206 int dowide; /* use 'guifontwide' */
2207
2208 /* Break the string at a composing character, it has to be drawn on
2209 * top of the previous character. */
2210 start = 0;
2211 cells = 0;
2212 for (i = 0; i < len; i += cl)
2213 {
2214 c = utf_ptr2char(s + i);
2215 cn = utf_char2cells(c);
2216 if (cn > 1
2217# ifdef FEAT_XFONTSET
2218 && fontset == NOFONTSET
2219# endif
2220 && gui.wide_font != NOFONT)
2221 dowide = TRUE;
2222 else
2223 dowide = FALSE;
2224 comping = utf_iscomposing(c);
2225 if (!comping) /* count cells from non-composing chars */
2226 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002227 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (cl == 0) /* hit end of string */
2229 len = i + cl; /* len must be wrong "cannot happen" */
2230
2231 /* print the string so far if it's the last character or there is
2232 * a composing character. */
2233 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar9372a112005-12-06 19:59:18 +00002234# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 || (cn > 1
2236# ifdef FEAT_XFONTSET
2237 /* No fontset: At least draw char after wide char at
2238 * right position. */
2239 && fontset == NOFONTSET
2240# endif
2241 )
2242# endif
2243 )
2244 {
2245 if (comping || dowide)
2246 thislen = i - start;
2247 else
2248 thislen = i - start + cl;
2249 if (thislen > 0)
2250 {
2251 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2252 draw_flags);
2253 start += thislen;
2254 }
2255 scol += cells;
2256 cells = 0;
2257 if (dowide)
2258 {
2259 gui_mch_set_font(gui.wide_font);
2260 gui_mch_draw_string(gui.row, scol - cn,
2261 s + start, cl, draw_flags);
2262 gui_mch_set_font(font);
2263 start += cl;
2264 }
2265
Bram Moolenaar9372a112005-12-06 19:59:18 +00002266# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002267 /* No fontset: draw a space to fill the gap after a wide char
2268 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2270# ifdef FEAT_XFONTSET
2271 && fontset == NOFONTSET
2272# endif
2273 && !dowide)
2274 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2275 1, draw_flags);
2276# endif
2277 }
2278 /* Draw a composing char on top of the previous char. */
2279 if (comping)
2280 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002281# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002282 /* Carbon ATSUI autodraws composing char over previous char */
2283 gui_mch_draw_string(gui.row, scol, s + i, cl,
2284 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002285# else
2286 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2287 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 start = i + cl;
2290 }
2291 }
2292 /* The stuff below assumes "len" is the length in screen columns. */
2293 len = scol - col;
2294 }
2295 else
2296# endif
2297 {
2298 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2299# ifdef FEAT_MBYTE
2300 if (enc_dbcs == DBCS_JPNU)
2301 {
2302 int clen = 0;
2303 int i;
2304
2305 /* Get the length in display cells, this can be different from the
2306 * number of bytes for "euc-jp". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002307 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 clen += (*mb_ptr2cells)(s + i);
2309 len = clen;
2310 }
2311# endif
2312 }
2313#endif /* !HAVE_GTK2 */
2314
2315 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2316 gui.col = col + len;
2317
2318 /* May need to invert it when it's part of the selection. */
2319 if (flags & GUI_MON_NOCLEAR)
2320 clip_may_redraw_selection(gui.row, col, len);
2321
2322 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2323 {
2324 /* Invalidate the old physical cursor position if we wrote over it */
2325 if (gui.cursor_row == gui.row
2326 && gui.cursor_col >= col
2327 && gui.cursor_col < col + len)
2328 gui.cursor_is_valid = FALSE;
2329 }
2330
2331#ifdef FEAT_SIGN_ICONS
2332 if (draw_sign)
2333 /* Draw the sign on top of the spaces. */
2334 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2335# ifdef FEAT_NETBEANS_INTG
2336 if (multi_sign)
2337 netbeans_draw_multisign_indicator(gui.row);
2338# endif
2339#endif
2340
2341 return OK;
2342}
2343
2344/*
2345 * Un-draw the cursor. Actually this just redraws the character at the given
2346 * position. The character just before it too, for when it was in bold.
2347 */
2348 void
2349gui_undraw_cursor()
2350{
2351 if (gui.cursor_is_valid)
2352 {
2353#ifdef FEAT_HANGULIN
2354 if (composing_hangul
2355 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2356 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2357 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2358 gui.norm_pixel, gui.back_pixel, 0);
2359 else
2360 {
2361#endif
2362 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2363 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2364 && gui.cursor_col > 0)
2365 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2366 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2367#ifdef FEAT_HANGULIN
2368 if (composing_hangul)
2369 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2370 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2371 }
2372#endif
2373 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2374 * here in case it wasn't needed to undraw it. */
2375 gui.cursor_is_valid = FALSE;
2376 }
2377}
2378
2379 void
2380gui_redraw(x, y, w, h)
2381 int x;
2382 int y;
2383 int w;
2384 int h;
2385{
2386 int row1, col1, row2, col2;
2387
2388 row1 = Y_2_ROW(y);
2389 col1 = X_2_COL(x);
2390 row2 = Y_2_ROW(y + h - 1);
2391 col2 = X_2_COL(x + w - 1);
2392
2393 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2394
2395 /*
2396 * We may need to redraw the cursor, but don't take it upon us to change
2397 * its location after a scroll.
2398 * (maybe be more strict even and test col too?)
2399 * These things may be outside the update/clipping region and reality may
2400 * not reflect Vims internal ideas if these operations are clipped away.
2401 */
2402 if (gui.row == gui.cursor_row)
2403 gui_update_cursor(TRUE, TRUE);
2404}
2405
2406/*
2407 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2408 * from col1 to col2 (inclusive).
2409 * Return TRUE when the character before the first drawn character has
2410 * different attributes (may have to be redrawn too).
2411 */
2412 int
2413gui_redraw_block(row1, col1, row2, col2, flags)
2414 int row1;
2415 int col1;
2416 int row2;
2417 int col2;
2418 int flags; /* flags for gui_outstr_nowrap() */
2419{
2420 int old_row, old_col;
2421 long_u old_hl_mask;
2422 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002423 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int idx, len;
2425 int back, nback;
2426 int retval = FALSE;
2427#ifdef FEAT_MBYTE
2428 int orig_col1, orig_col2;
2429#endif
2430
2431 /* Don't try to update when ScreenLines is not valid */
2432 if (!screen_cleared || ScreenLines == NULL)
2433 return retval;
2434
2435 /* Don't try to draw outside the shell! */
2436 /* Check everything, strange values may be caused by a big border width */
2437 col1 = check_col(col1);
2438 col2 = check_col(col2);
2439 row1 = check_row(row1);
2440 row2 = check_row(row2);
2441
2442 /* Remember where our cursor was */
2443 old_row = gui.row;
2444 old_col = gui.col;
2445 old_hl_mask = gui.highlight_mask;
2446#ifdef FEAT_MBYTE
2447 orig_col1 = col1;
2448 orig_col2 = col2;
2449#endif
2450
2451 for (gui.row = row1; gui.row <= row2; gui.row++)
2452 {
2453#ifdef FEAT_MBYTE
2454 /* When only half of a double-wide character is in the block, include
2455 * the other half. */
2456 col1 = orig_col1;
2457 col2 = orig_col2;
2458 off = LineOffset[gui.row];
2459 if (enc_dbcs != 0)
2460 {
2461 if (col1 > 0)
2462 col1 -= dbcs_screen_head_off(ScreenLines + off,
2463 ScreenLines + off + col1);
2464 col2 += dbcs_screen_tail_off(ScreenLines + off,
2465 ScreenLines + off + col2);
2466 }
2467 else if (enc_utf8)
2468 {
2469 if (ScreenLines[off + col1] == 0)
2470 --col1;
2471# ifdef HAVE_GTK2
2472 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2473 ++col2;
2474# endif
2475 }
2476#endif
2477 gui.col = col1;
2478 off = LineOffset[gui.row] + gui.col;
2479 len = col2 - col1 + 1;
2480
2481 /* Find how many chars back this highlighting starts, or where a space
2482 * is. Needed for when the bold trick is used */
2483 for (back = 0; back < col1; ++back)
2484 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2485 || ScreenLines[off - 1 - back] == ' ')
2486 break;
2487 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2488 && ScreenLines[off - 1] != ' ');
2489
2490 /* Break it up in strings of characters with the same attributes. */
2491 /* Print UTF-8 characters individually. */
2492 while (len > 0)
2493 {
2494 first_attr = ScreenAttrs[off];
2495 gui.highlight_mask = first_attr;
2496#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2497 if (enc_utf8 && ScreenLinesUC[off] != 0)
2498 {
2499 /* output multi-byte character separately */
2500 nback = gui_screenchar(off, flags,
2501 (guicolor_T)0, (guicolor_T)0, back);
2502 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2503 idx = 2;
2504 else
2505 idx = 1;
2506 }
2507 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2508 {
2509 /* output double-byte, single-width character separately */
2510 nback = gui_screenchar(off, flags,
2511 (guicolor_T)0, (guicolor_T)0, back);
2512 idx = 1;
2513 }
2514 else
2515#endif
2516 {
2517#ifdef HAVE_GTK2
2518 for (idx = 0; idx < len; ++idx)
2519 {
2520 if (enc_utf8 && ScreenLines[off + idx] == 0)
2521 continue; /* skip second half of double-width char */
2522 if (ScreenAttrs[off + idx] != first_attr)
2523 break;
2524 }
2525 /* gui_screenstr() takes care of multibyte chars */
2526 nback = gui_screenstr(off, idx, flags,
2527 (guicolor_T)0, (guicolor_T)0, back);
2528#else
2529 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2530 idx++)
2531 {
2532# ifdef FEAT_MBYTE
2533 /* Stop at a multi-byte Unicode character. */
2534 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2535 break;
2536 if (enc_dbcs == DBCS_JPNU)
2537 {
2538 /* Stop at a double-byte single-width char. */
2539 if (ScreenLines[off + idx] == 0x8e)
2540 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002541 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 + off + idx) == 2)
2543 ++idx; /* skip second byte of double-byte char */
2544 }
2545# endif
2546 }
2547 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2548 (guicolor_T)0, (guicolor_T)0, back);
2549#endif
2550 }
2551 if (nback == FAIL)
2552 {
2553 /* Must back up to start drawing where a bold or italic word
2554 * starts. */
2555 off -= back;
2556 len += back;
2557 gui.col -= back;
2558 }
2559 else
2560 {
2561 off += idx;
2562 len -= idx;
2563 }
2564 back = 0;
2565 }
2566 }
2567
2568 /* Put the cursor back where it was */
2569 gui.row = old_row;
2570 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002571 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 return retval;
2574}
2575
2576 static void
2577gui_delete_lines(row, count)
2578 int row;
2579 int count;
2580{
2581 if (count <= 0)
2582 return;
2583
2584 if (row + count > gui.scroll_region_bot)
2585 /* Scrolled out of region, just blank the lines out */
2586 gui_clear_block(row, gui.scroll_region_left,
2587 gui.scroll_region_bot, gui.scroll_region_right);
2588 else
2589 {
2590 gui_mch_delete_lines(row, count);
2591
2592 /* If the cursor was in the deleted lines it's now gone. If the
2593 * cursor was in the scrolled lines adjust its position. */
2594 if (gui.cursor_row >= row
2595 && gui.cursor_col >= gui.scroll_region_left
2596 && gui.cursor_col <= gui.scroll_region_right)
2597 {
2598 if (gui.cursor_row < row + count)
2599 gui.cursor_is_valid = FALSE;
2600 else if (gui.cursor_row <= gui.scroll_region_bot)
2601 gui.cursor_row -= count;
2602 }
2603 }
2604}
2605
2606 static void
2607gui_insert_lines(row, count)
2608 int row;
2609 int count;
2610{
2611 if (count <= 0)
2612 return;
2613
2614 if (row + count > gui.scroll_region_bot)
2615 /* Scrolled out of region, just blank the lines out */
2616 gui_clear_block(row, gui.scroll_region_left,
2617 gui.scroll_region_bot, gui.scroll_region_right);
2618 else
2619 {
2620 gui_mch_insert_lines(row, count);
2621
2622 if (gui.cursor_row >= gui.row
2623 && gui.cursor_col >= gui.scroll_region_left
2624 && gui.cursor_col <= gui.scroll_region_right)
2625 {
2626 if (gui.cursor_row <= gui.scroll_region_bot - count)
2627 gui.cursor_row += count;
2628 else if (gui.cursor_row <= gui.scroll_region_bot)
2629 gui.cursor_is_valid = FALSE;
2630 }
2631 }
2632}
2633
2634/*
2635 * The main GUI input routine. Waits for a character from the keyboard.
2636 * wtime == -1 Wait forever.
2637 * wtime == 0 Don't wait.
2638 * wtime > 0 Wait wtime milliseconds for a character.
2639 * Returns OK if a character was found to be available within the given time,
2640 * or FAIL otherwise.
2641 */
2642 int
2643gui_wait_for_chars(wtime)
2644 long wtime;
2645{
2646 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 /*
2649 * If we're going to wait a bit, update the menus and mouse shape for the
2650 * current State.
2651 */
2652 if (wtime != 0)
2653 {
2654#ifdef FEAT_MENU
2655 gui_update_menus(0);
2656#endif
2657 }
2658
2659 gui_mch_update();
2660 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /* Before waiting, flush any output to the screen. */
2666 gui_mch_flush();
2667
2668 if (wtime > 0)
2669 {
2670 /* Blink when waiting for a character. Probably only does something
2671 * for showmatch() */
2672 gui_mch_start_blink();
2673 retval = gui_mch_wait_for_chars(wtime);
2674 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 return retval;
2676 }
2677
2678 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002679 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
2681 gui_mch_start_blink();
2682
Bram Moolenaar3918c952005-03-15 22:34:55 +00002683 retval = FAIL;
2684 /*
2685 * We may want to trigger the CursorHold event. First wait for
2686 * 'updatetime' and if nothing is typed within that time put the
2687 * K_CURSORHOLD key in the input buffer.
2688 */
2689 if (gui_mch_wait_for_chars(p_ut) == OK)
2690 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002692 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002694 char_u buf[3];
2695
2696 /* Put K_CURSORHOLD in the input buffer. */
2697 buf[0] = CSI;
2698 buf[1] = KS_EXTRA;
2699 buf[2] = (int)KE_CURSORHOLD;
2700 add_to_input_buf(buf, 3);
2701
2702 retval = OK;
2703 }
2704#endif
2705
2706 if (retval == FAIL)
2707 {
2708 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002709 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 gui_mch_stop_blink();
2714 return retval;
2715}
2716
2717/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002718 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 */
2720 static void
2721fill_mouse_coord(p, col, row)
2722 char_u *p;
2723 int col;
2724 int row;
2725{
2726 p[0] = (char_u)(col / 128 + ' ' + 1);
2727 p[1] = (char_u)(col % 128 + ' ' + 1);
2728 p[2] = (char_u)(row / 128 + ' ' + 1);
2729 p[3] = (char_u)(row % 128 + ' ' + 1);
2730}
2731
2732/*
2733 * Generic mouse support function. Add a mouse event to the input buffer with
2734 * the given properties.
2735 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2736 * MOUSE_X1, MOUSE_X2
2737 * MOUSE_DRAG, or MOUSE_RELEASE.
2738 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2739 * x, y --- Coordinates of mouse in pixels.
2740 * repeated_click --- TRUE if this click comes only a short time after a
2741 * previous click.
2742 * modifiers --- Bit field which may be any of the following modifiers
2743 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2744 * This function will ignore drag events where the mouse has not moved to a new
2745 * character.
2746 */
2747 void
2748gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2749 int button;
2750 int x;
2751 int y;
2752 int repeated_click;
2753 int_u modifiers;
2754{
2755 static int prev_row = 0, prev_col = 0;
2756 static int prev_button = -1;
2757 static int num_clicks = 1;
2758 char_u string[10];
2759 enum key_extra button_char;
2760 int row, col;
2761#ifdef FEAT_CLIPBOARD
2762 int checkfor;
2763 int did_clip = FALSE;
2764#endif
2765
2766 /*
2767 * Scrolling may happen at any time, also while a selection is present.
2768 */
2769 switch (button)
2770 {
2771 case MOUSE_X1:
2772 button_char = KE_X1MOUSE;
2773 goto button_set;
2774 case MOUSE_X2:
2775 button_char = KE_X2MOUSE;
2776 goto button_set;
2777 case MOUSE_4:
2778 button_char = KE_MOUSEDOWN;
2779 goto button_set;
2780 case MOUSE_5:
2781 button_char = KE_MOUSEUP;
2782button_set:
2783 {
2784 /* Don't put events in the input queue now. */
2785 if (hold_gui_events)
2786 return;
2787
2788 string[3] = CSI;
2789 string[4] = KS_EXTRA;
2790 string[5] = (int)button_char;
2791
2792 /* Pass the pointer coordinates of the scroll event so that we
2793 * know which window to scroll. */
2794 row = gui_xy2colrow(x, y, &col);
2795 string[6] = (char_u)(col / 128 + ' ' + 1);
2796 string[7] = (char_u)(col % 128 + ' ' + 1);
2797 string[8] = (char_u)(row / 128 + ' ' + 1);
2798 string[9] = (char_u)(row % 128 + ' ' + 1);
2799
2800 if (modifiers == 0)
2801 add_to_input_buf(string + 3, 7);
2802 else
2803 {
2804 string[0] = CSI;
2805 string[1] = KS_MODIFIER;
2806 string[2] = 0;
2807 if (modifiers & MOUSE_SHIFT)
2808 string[2] |= MOD_MASK_SHIFT;
2809 if (modifiers & MOUSE_CTRL)
2810 string[2] |= MOD_MASK_CTRL;
2811 if (modifiers & MOUSE_ALT)
2812 string[2] |= MOD_MASK_ALT;
2813 add_to_input_buf(string, 10);
2814 }
2815 return;
2816 }
2817 }
2818
2819#ifdef FEAT_CLIPBOARD
2820 /* If a clipboard selection is in progress, handle it */
2821 if (clip_star.state == SELECT_IN_PROGRESS)
2822 {
2823 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2824 return;
2825 }
2826
2827 /* Determine which mouse settings to look for based on the current mode */
2828 switch (get_real_state())
2829 {
2830 case NORMAL_BUSY:
2831 case OP_PENDING:
2832 case NORMAL: checkfor = MOUSE_NORMAL; break;
2833 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002834 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 case REPLACE:
2836 case REPLACE+LANGMAP:
2837#ifdef FEAT_VREPLACE
2838 case VREPLACE:
2839 case VREPLACE+LANGMAP:
2840#endif
2841 case INSERT:
2842 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2843 case ASKMORE:
2844 case HITRETURN: /* At the more- and hit-enter prompt pass the
2845 mouse event for a click on or below the
2846 message line. */
2847 if (Y_2_ROW(y) >= msg_row)
2848 checkfor = MOUSE_NORMAL;
2849 else
2850 checkfor = MOUSE_RETURN;
2851 break;
2852
2853 /*
2854 * On the command line, use the clipboard selection on all lines
2855 * but the command line. But not when pasting.
2856 */
2857 case CMDLINE:
2858 case CMDLINE+LANGMAP:
2859 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2860 checkfor = MOUSE_NONE;
2861 else
2862 checkfor = MOUSE_COMMAND;
2863 break;
2864
2865 default:
2866 checkfor = MOUSE_NONE;
2867 break;
2868 };
2869
2870 /*
2871 * Allow clipboard selection of text on the command line in "normal"
2872 * modes. Don't do this when dragging the status line, or extending a
2873 * Visual selection.
2874 */
2875 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2876 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00002877# ifdef FEAT_WINDOWS
2878 + firstwin->w_winrow
2879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 && button != MOUSE_DRAG
2881# ifdef FEAT_MOUSESHAPE
2882 && !drag_status_line
2883# ifdef FEAT_VERTSPLIT
2884 && !drag_sep_line
2885# endif
2886# endif
2887 )
2888 checkfor = MOUSE_NONE;
2889
2890 /*
2891 * Use modeless selection when holding CTRL and SHIFT pressed.
2892 */
2893 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2894 checkfor = MOUSE_NONEF;
2895
2896 /*
2897 * In Ex mode, always use modeless selection.
2898 */
2899 if (exmode_active)
2900 checkfor = MOUSE_NONE;
2901
2902 /*
2903 * If the mouse settings say to not use the mouse, use the modeless
2904 * selection. But if Visual is active, assume that only the Visual area
2905 * will be selected.
2906 * Exception: On the command line, both the selection is used and a mouse
2907 * key is send.
2908 */
2909 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2910 {
2911#ifdef FEAT_VISUAL
2912 /* Don't do modeless selection in Visual mode. */
2913 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2914 return;
2915#endif
2916
2917 /*
2918 * When 'mousemodel' is "popup", shift-left is translated to right.
2919 * But not when also using Ctrl.
2920 */
2921 if (mouse_model_popup() && button == MOUSE_LEFT
2922 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2923 {
2924 button = MOUSE_RIGHT;
2925 modifiers &= ~ MOUSE_SHIFT;
2926 }
2927
2928 /* If the selection is done, allow the right button to extend it.
2929 * If the selection is cleared, allow the right button to start it
2930 * from the cursor position. */
2931 if (button == MOUSE_RIGHT)
2932 {
2933 if (clip_star.state == SELECT_CLEARED)
2934 {
2935 if (State & CMDLINE)
2936 {
2937 col = msg_col;
2938 row = msg_row;
2939 }
2940 else
2941 {
2942 col = curwin->w_wcol;
2943 row = curwin->w_wrow + W_WINROW(curwin);
2944 }
2945 clip_start_selection(col, row, FALSE);
2946 }
2947 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2948 repeated_click);
2949 did_clip = TRUE;
2950 }
2951 /* Allow the left button to start the selection */
2952 else if (button ==
2953# ifdef RISCOS
2954 /* Only start a drag on a drag event. Otherwise
2955 * we don't get a release event. */
2956 MOUSE_DRAG
2957# else
2958 MOUSE_LEFT
2959# endif
2960 )
2961 {
2962 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2963 did_clip = TRUE;
2964 }
2965# ifdef RISCOS
2966 else if (button == MOUSE_LEFT)
2967 {
2968 clip_clear_selection();
2969 did_clip = TRUE;
2970 }
2971# endif
2972
2973 /* Always allow pasting */
2974 if (button != MOUSE_MIDDLE)
2975 {
2976 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2977 return;
2978 if (checkfor != MOUSE_COMMAND)
2979 button = MOUSE_LEFT;
2980 }
2981 repeated_click = FALSE;
2982 }
2983
2984 if (clip_star.state != SELECT_CLEARED && !did_clip)
2985 clip_clear_selection();
2986#endif
2987
2988 /* Don't put events in the input queue now. */
2989 if (hold_gui_events)
2990 return;
2991
2992 row = gui_xy2colrow(x, y, &col);
2993
2994 /*
2995 * If we are dragging and the mouse hasn't moved far enough to be on a
2996 * different character, then don't send an event to vim.
2997 */
2998 if (button == MOUSE_DRAG)
2999 {
3000 if (row == prev_row && col == prev_col)
3001 return;
3002 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3003 if (y < 0)
3004 row = -1;
3005 }
3006
3007 /*
3008 * If topline has changed (window scrolled) since the last click, reset
3009 * repeated_click, because we don't want starting Visual mode when
3010 * clicking on a different character in the text.
3011 */
3012 if (curwin->w_topline != gui_prev_topline
3013#ifdef FEAT_DIFF
3014 || curwin->w_topfill != gui_prev_topfill
3015#endif
3016 )
3017 repeated_click = FALSE;
3018
3019 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3020 string[1] = KS_MOUSE;
3021 string[2] = KE_FILLER;
3022 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3023 {
3024 if (repeated_click)
3025 {
3026 /*
3027 * Handle multiple clicks. They only count if the mouse is still
3028 * pointing at the same character.
3029 */
3030 if (button != prev_button || row != prev_row || col != prev_col)
3031 num_clicks = 1;
3032 else if (++num_clicks > 4)
3033 num_clicks = 1;
3034 }
3035 else
3036 num_clicks = 1;
3037 prev_button = button;
3038 gui_prev_topline = curwin->w_topline;
3039#ifdef FEAT_DIFF
3040 gui_prev_topfill = curwin->w_topfill;
3041#endif
3042
3043 string[3] = (char_u)(button | 0x20);
3044 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3045 }
3046 else
3047 string[3] = (char_u)button;
3048
3049 string[3] |= modifiers;
3050 fill_mouse_coord(string + 4, col, row);
3051 add_to_input_buf(string, 8);
3052
3053 if (row < 0)
3054 prev_row = 0;
3055 else
3056 prev_row = row;
3057 prev_col = col;
3058
3059 /*
3060 * We need to make sure this is cleared since Athena doesn't tell us when
3061 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3062 */
3063#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3064 gui.dragged_sb = SBAR_NONE;
3065#endif
3066}
3067
3068/*
3069 * Convert x and y coordinate to column and row in text window.
3070 * Corrects for multi-byte character.
3071 * returns column in "*colp" and row as return value;
3072 */
3073 int
3074gui_xy2colrow(x, y, colp)
3075 int x;
3076 int y;
3077 int *colp;
3078{
3079 int col = check_col(X_2_COL(x));
3080 int row = check_row(Y_2_ROW(y));
3081
3082#ifdef FEAT_MBYTE
3083 *colp = mb_fix_col(col, row);
3084#else
3085 *colp = col;
3086#endif
3087 return row;
3088}
3089
3090#if defined(FEAT_MENU) || defined(PROTO)
3091/*
3092 * Callback function for when a menu entry has been selected.
3093 */
3094 void
3095gui_menu_cb(menu)
3096 vimmenu_T *menu;
3097{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003098 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 /* Don't put events in the input queue now. */
3101 if (hold_gui_events)
3102 return;
3103
3104 bytes[0] = CSI;
3105 bytes[1] = KS_MENU;
3106 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003107 add_to_input_buf(bytes, 3);
3108 add_long_to_buf((long_u)menu, bytes);
3109 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110}
3111#endif
3112
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003113static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115/*
3116 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003117 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * in p_go.
3119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 void
3121gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003122 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124#ifdef FEAT_MENU
3125 static int prev_menu_is_active = -1;
3126#endif
3127#ifdef FEAT_TOOLBAR
3128 static int prev_toolbar = -1;
3129 int using_toolbar = FALSE;
3130#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003131#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003132 int using_tabline;
3133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#ifdef FEAT_FOOTER
3135 static int prev_footer = -1;
3136 int using_footer = FALSE;
3137#endif
3138#if defined(FEAT_MENU) && !defined(WIN16)
3139 static int prev_tearoff = -1;
3140 int using_tearoff = FALSE;
3141#endif
3142
3143 char_u *p;
3144 int i;
3145#ifdef FEAT_MENU
3146 int grey_old, grey_new;
3147 char_u *temp;
3148#endif
3149 win_T *wp;
3150 int need_set_size;
3151 int fix_size;
3152
3153#ifdef FEAT_MENU
3154 if (oldval != NULL && gui.in_use)
3155 {
3156 /*
3157 * Check if the menu's go from grey to non-grey or vise versa.
3158 */
3159 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3160 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3161 if (grey_old != grey_new)
3162 {
3163 temp = p_go;
3164 p_go = oldval;
3165 gui_update_menus(MENU_ALL_MODES);
3166 p_go = temp;
3167 }
3168 }
3169 gui.menu_is_active = FALSE;
3170#endif
3171
3172 for (i = 0; i < 3; i++)
3173 gui.which_scrollbars[i] = FALSE;
3174 for (p = p_go; *p; p++)
3175 switch (*p)
3176 {
3177 case GO_LEFT:
3178 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3179 break;
3180 case GO_RIGHT:
3181 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3182 break;
3183#ifdef FEAT_VERTSPLIT
3184 case GO_VLEFT:
3185 if (win_hasvertsplit())
3186 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3187 break;
3188 case GO_VRIGHT:
3189 if (win_hasvertsplit())
3190 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3191 break;
3192#endif
3193 case GO_BOT:
3194 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3195 break;
3196#ifdef FEAT_MENU
3197 case GO_MENUS:
3198 gui.menu_is_active = TRUE;
3199 break;
3200#endif
3201 case GO_GREY:
3202 /* make menu's have grey items, ignored here */
3203 break;
3204#ifdef FEAT_TOOLBAR
3205 case GO_TOOLBAR:
3206 using_toolbar = TRUE;
3207 break;
3208#endif
3209#ifdef FEAT_FOOTER
3210 case GO_FOOTER:
3211 using_footer = TRUE;
3212 break;
3213#endif
3214 case GO_TEAROFF:
3215#if defined(FEAT_MENU) && !defined(WIN16)
3216 using_tearoff = TRUE;
3217#endif
3218 break;
3219 default:
3220 /* Ignore options that are not supported */
3221 break;
3222 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003223
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (gui.in_use)
3225 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003226 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003228
3229#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003230 /* Update the GUI tab line, it may appear or disappear. This may
3231 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003232 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003233 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003234 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003235 /* We don't want a resize event change "Rows" here, save and
3236 * restore it. Resizing is handled below. */
3237 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003238 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003239 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003240 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003241 if (using_tabline)
3242 fix_size = TRUE;
3243 if (!gui_use_tabline())
3244 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3245 }
3246#endif
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 for (i = 0; i < 3; i++)
3249 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003250 /* The scrollbar needs to be updated when it is shown/unshown and
3251 * when switching tab pages. But the size only changes when it's
3252 * shown/unshown. Thus we need two places to remember whether a
3253 * scrollbar is there or not. */
3254 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003255#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003256 || gui.which_scrollbars[i]
3257 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003258#endif
3259 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
3261 if (i == SBAR_BOTTOM)
3262 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3263 gui.which_scrollbars[i]);
3264 else
3265 {
3266 FOR_ALL_WINDOWS(wp)
3267 {
3268 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3269 }
3270 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003271 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3272 {
3273 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003274 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003275 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003276 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003277 if (gui.which_scrollbars[i])
3278 fix_size = TRUE;
3279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003281#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003282 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003283#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003284 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286
3287#ifdef FEAT_MENU
3288 if (gui.menu_is_active != prev_menu_is_active)
3289 {
3290 /* We don't want a resize event change "Rows" here, save and
3291 * restore it. Resizing is handled below. */
3292 i = Rows;
3293 gui_mch_enable_menu(gui.menu_is_active);
3294 Rows = i;
3295 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003296 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (gui.menu_is_active)
3298 fix_size = TRUE;
3299 }
3300#endif
3301
3302#ifdef FEAT_TOOLBAR
3303 if (using_toolbar != prev_toolbar)
3304 {
3305 gui_mch_show_toolbar(using_toolbar);
3306 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003307 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (using_toolbar)
3309 fix_size = TRUE;
3310 }
3311#endif
3312#ifdef FEAT_FOOTER
3313 if (using_footer != prev_footer)
3314 {
3315 gui_mch_enable_footer(using_footer);
3316 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003317 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (using_footer)
3319 fix_size = TRUE;
3320 }
3321#endif
3322#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3323 if (using_tearoff != prev_tearoff)
3324 {
3325 gui_mch_toggle_tearoffs(using_tearoff);
3326 prev_tearoff = using_tearoff;
3327 }
3328#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003329 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003330 {
3331#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003332 long prev_Columns = Columns;
3333 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 /* Adjust the size of the window to make the text area keep the
3336 * same size and to avoid that part of our window is off-screen
3337 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003338 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003339
3340#ifdef FEAT_GUI_GTK
3341 /* GTK has the annoying habit of sending us resize events when
3342 * changing the window size ourselves. This mostly happens when
3343 * waiting for a character to arrive, quite unpredictably, and may
3344 * change Columns and Rows when we don't want it. Wait for a
3345 * character here to avoid this effect.
3346 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003347 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003348 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003349 * Don't change Rows when adding menu/toolbar/tabline.
3350 * Don't change Columns when adding vertical toolbar. */
3351 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003352 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003353 if ((need_set_size & RESIZE_VERT) == 0)
3354 Rows = prev_Rows;
3355 if ((need_set_size & RESIZE_HOR) == 0)
3356 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003357#endif
3358 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003359#ifdef FEAT_WINDOWS
3360 /* When the console tabline appears or disappears the window positions
3361 * change. */
3362 if (firstwin->w_winrow != tabline_height())
3363 shell_new_rows(); /* recompute window positions and heights */
3364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366}
3367
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003368#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3369/*
3370 * Return TRUE if the GUI is taking care of the tabline.
3371 * It may still be hidden if 'showtabline' is zero.
3372 */
3373 int
3374gui_use_tabline()
3375{
3376 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3377}
3378
3379/*
3380 * Return TRUE if the GUI is showing the tabline.
3381 * This uses 'showtabline'.
3382 */
3383 static int
3384gui_has_tabline()
3385{
3386 if (!gui_use_tabline()
3387 || p_stal == 0
3388 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3389 return FALSE;
3390 return TRUE;
3391}
3392
3393/*
3394 * Update the tabline.
3395 * This may display/undisplay the tabline and update the labels.
3396 */
3397 void
3398gui_update_tabline()
3399{
3400 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003401 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003402
3403 if (!gui.starting && starting == 0)
3404 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003405 /* Updating the tabline uses direct GUI commands, flush
3406 * outstanding instructions first. (esp. clear screen) */
3407 out_flush();
3408 gui_mch_flush();
3409
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003410 if (!showit != !shown)
3411 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003412 if (showit != 0)
3413 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003414
3415 /* When the tabs change from hidden to shown or from shown to
3416 * hidden the size of the text area should remain the same. */
3417 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003418 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003419 }
3420}
3421
3422/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003423 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003424 */
3425 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003426get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003427 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003428 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003429{
3430 int modified = FALSE;
3431 char_u buf[40];
3432 int wincount;
3433 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003434 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003435
Bram Moolenaar57657d82006-04-21 22:12:41 +00003436 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003437 opt = (tooltip ? &p_gtt : &p_gtl);
3438 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003439 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003440 int use_sandbox = FALSE;
3441 int save_called_emsg = called_emsg;
3442 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003443 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003444 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3445 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003446
3447 called_emsg = FALSE;
3448
3449 printer_page_num = tabpage_index(tp);
3450# ifdef FEAT_EVAL
3451 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003452 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003453# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003454 /* It's almost as going to the tabpage, but without autocommands. */
3455 curtab->tp_firstwin = firstwin;
3456 curtab->tp_lastwin = lastwin;
3457 curtab->tp_curwin = curwin;
3458 save_curtab = curtab;
3459 curtab = tp;
3460 topframe = curtab->tp_topframe;
3461 firstwin = curtab->tp_firstwin;
3462 lastwin = curtab->tp_lastwin;
3463 curwin = curtab->tp_curwin;
3464 curbuf = curwin->w_buffer;
3465
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003466 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003467 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003468 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003469 STRCPY(NameBuff, res);
3470
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003471 /* Back to the original curtab. */
3472 curtab = save_curtab;
3473 topframe = curtab->tp_topframe;
3474 firstwin = curtab->tp_firstwin;
3475 lastwin = curtab->tp_lastwin;
3476 curwin = curtab->tp_curwin;
3477 curbuf = curwin->w_buffer;
3478
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003479 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003480 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003481 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003482 called_emsg |= save_called_emsg;
3483 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003484
3485 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3486 * use a default label. */
3487 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003488 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003489 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003490 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003491 if (!tooltip)
3492 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003493
3494 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3495 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3496 if (bufIsChanged(wp->w_buffer))
3497 modified = TRUE;
3498 if (modified || wincount > 1)
3499 {
3500 if (wincount > 1)
3501 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3502 else
3503 buf[0] = NUL;
3504 if (modified)
3505 STRCAT(buf, "+");
3506 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003507 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003508 mch_memmove(NameBuff, buf, STRLEN(buf));
3509 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003510 }
3511}
3512
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003513/*
3514 * Send the event for clicking to select tab page "nr".
3515 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003516 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003517 */
3518 int
3519send_tabline_event(nr)
3520 int nr;
3521{
3522 char_u string[3];
3523
3524 if (nr == tabpage_index(curtab))
3525 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003526
3527 /* Don't put events in the input queue now. */
3528 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003529# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003530 || cmdwin_type != 0
3531# endif
3532 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003533 {
3534 /* Set it back to the current tab page. */
3535 gui_mch_set_curtab(tabpage_index(curtab));
3536 return FALSE;
3537 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003538
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003539 string[0] = CSI;
3540 string[1] = KS_TABLINE;
3541 string[2] = KE_FILLER;
3542 add_to_input_buf(string, 3);
3543 string[0] = nr;
3544 add_to_input_buf_csi(string, 1);
3545 return TRUE;
3546}
3547
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003548/*
3549 * Send a tabline menu event
3550 */
3551 void
3552send_tabline_menu_event(tabidx, event)
3553 int tabidx;
3554 int event;
3555{
3556 char_u string[3];
3557
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003558 /* Don't put events in the input queue now. */
3559 if (hold_gui_events)
3560 return;
3561
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003562 string[0] = CSI;
3563 string[1] = KS_TABMENU;
3564 string[2] = KE_FILLER;
3565 add_to_input_buf(string, 3);
3566 string[0] = tabidx;
3567 string[1] = (char_u)(long)event;
3568 add_to_input_buf_csi(string, 2);
3569}
3570
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
3573/*
3574 * Scrollbar stuff:
3575 */
3576
Bram Moolenaare45828b2006-02-15 22:12:56 +00003577#if defined(FEAT_WINDOWS) || defined(PROTO)
3578/*
3579 * Remove all scrollbars. Used before switching to another tab page.
3580 */
3581 void
3582gui_remove_scrollbars()
3583{
3584 int i;
3585 win_T *wp;
3586
3587 for (i = 0; i < 3; i++)
3588 {
3589 if (i == SBAR_BOTTOM)
3590 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3591 else
3592 {
3593 FOR_ALL_WINDOWS(wp)
3594 {
3595 gui_do_scrollbar(wp, i, FALSE);
3596 }
3597 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003598 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003599 }
3600}
3601#endif
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 void
3604gui_create_scrollbar(sb, type, wp)
3605 scrollbar_T *sb;
3606 int type;
3607 win_T *wp;
3608{
3609 static int sbar_ident = 0;
3610
3611 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3612 sb->wp = wp;
3613 sb->type = type;
3614 sb->value = 0;
3615#ifdef FEAT_GUI_ATHENA
3616 sb->pixval = 0;
3617#endif
3618 sb->size = 1;
3619 sb->max = 1;
3620 sb->top = 0;
3621 sb->height = 0;
3622#ifdef FEAT_VERTSPLIT
3623 sb->width = 0;
3624#endif
3625 sb->status_height = 0;
3626 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3627}
3628
3629/*
3630 * Find the scrollbar with the given index.
3631 */
3632 scrollbar_T *
3633gui_find_scrollbar(ident)
3634 long ident;
3635{
3636 win_T *wp;
3637
3638 if (gui.bottom_sbar.ident == ident)
3639 return &gui.bottom_sbar;
3640 FOR_ALL_WINDOWS(wp)
3641 {
3642 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3643 return &wp->w_scrollbars[SBAR_LEFT];
3644 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3645 return &wp->w_scrollbars[SBAR_RIGHT];
3646 }
3647 return NULL;
3648}
3649
3650/*
3651 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3652 *
3653 * For Win32, Macintosh and GTK+ 2:
3654 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3655 * you stop dragging the scrollbar. We get here each time the scrollbar is
3656 * dragged another pixel, but as far as the rest of vim goes, it thinks
3657 * we're just hanging in the call to DispatchMessage() in
3658 * process_message(). The DispatchMessage() call that hangs was passed a
3659 * mouse button click event in the scrollbar window. -- webb.
3660 *
3661 * Solution: Do the scrolling right here. But only when allowed.
3662 * Ignore the scrollbars while executing an external command or when there
3663 * are still characters to be processed.
3664 */
3665 void
3666gui_drag_scrollbar(sb, value, still_dragging)
3667 scrollbar_T *sb;
3668 long value;
3669 int still_dragging;
3670{
3671#ifdef FEAT_WINDOWS
3672 win_T *wp;
3673#endif
3674 int sb_num;
3675#ifdef USE_ON_FLY_SCROLL
3676 colnr_T old_leftcol = curwin->w_leftcol;
3677# ifdef FEAT_SCROLLBIND
3678 linenr_T old_topline = curwin->w_topline;
3679# endif
3680# ifdef FEAT_DIFF
3681 int old_topfill = curwin->w_topfill;
3682# endif
3683#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003684 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 int byte_count;
3686#endif
3687
3688 if (sb == NULL)
3689 return;
3690
3691 /* Don't put events in the input queue now. */
3692 if (hold_gui_events)
3693 return;
3694
3695#ifdef FEAT_CMDWIN
3696 if (cmdwin_type != 0 && sb->wp != curwin)
3697 return;
3698#endif
3699
3700 if (still_dragging)
3701 {
3702 if (sb->wp == NULL)
3703 gui.dragged_sb = SBAR_BOTTOM;
3704 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3705 gui.dragged_sb = SBAR_LEFT;
3706 else
3707 gui.dragged_sb = SBAR_RIGHT;
3708 gui.dragged_wp = sb->wp;
3709 }
3710 else
3711 {
3712 gui.dragged_sb = SBAR_NONE;
3713#ifdef HAVE_GTK2
3714 /* Keep the "dragged_wp" value until after the scrolling, for when the
3715 * moust button is released. GTK2 doesn't send the button-up event. */
3716 gui.dragged_wp = NULL;
3717#endif
3718 }
3719
3720 /* Vertical sbar info is kept in the first sbar (the left one) */
3721 if (sb->wp != NULL)
3722 sb = &sb->wp->w_scrollbars[0];
3723
3724 /*
3725 * Check validity of value
3726 */
3727 if (value < 0)
3728 value = 0;
3729#ifdef SCROLL_PAST_END
3730 else if (value > sb->max)
3731 value = sb->max;
3732#else
3733 if (value > sb->max - sb->size + 1)
3734 value = sb->max - sb->size + 1;
3735#endif
3736
3737 sb->value = value;
3738
3739#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003740 /* When not allowed to do the scrolling right now, return.
3741 * This also checked input_available(), but that causes the first click in
3742 * a scrollbar to be ignored when Vim doesn't have focus. */
3743 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 return;
3745#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003746#ifdef FEAT_INS_EXPAND
3747 /* Disallow scrolling the current window when the completion popup menu is
3748 * visible. */
3749 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3750 return;
3751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
3753#ifdef FEAT_RIGHTLEFT
3754 if (sb->wp == NULL && curwin->w_p_rl)
3755 {
3756 value = sb->max + 1 - sb->size - value;
3757 if (value < 0)
3758 value = 0;
3759 }
3760#endif
3761
3762 if (sb->wp != NULL) /* vertical scrollbar */
3763 {
3764 sb_num = 0;
3765#ifdef FEAT_WINDOWS
3766 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3767 sb_num++;
3768 if (wp == NULL)
3769 return;
3770#else
3771 if (sb->wp != curwin)
3772 return;
3773#endif
3774
3775#ifdef USE_ON_FLY_SCROLL
3776 current_scrollbar = sb_num;
3777 scrollbar_value = value;
3778 if (State & NORMAL)
3779 {
3780 gui_do_scroll();
3781 setcursor();
3782 }
3783 else if (State & INSERT)
3784 {
3785 ins_scroll();
3786 setcursor();
3787 }
3788 else if (State & CMDLINE)
3789 {
3790 if (msg_scrolled == 0)
3791 {
3792 gui_do_scroll();
3793 redrawcmdline();
3794 }
3795 }
3796# ifdef FEAT_FOLDING
3797 /* Value may have been changed for closed fold. */
3798 sb->value = sb->wp->w_topline - 1;
3799# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003800
3801 /* When dragging one scrollbar and there is another one at the other
3802 * side move the thumb of that one too. */
3803 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3804 gui_mch_set_scrollbar_thumb(
3805 &sb->wp->w_scrollbars[
3806 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3807 ? SBAR_LEFT : SBAR_RIGHT],
3808 sb->value, sb->size, sb->max);
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810#else
3811 bytes[0] = CSI;
3812 bytes[1] = KS_VER_SCROLLBAR;
3813 bytes[2] = KE_FILLER;
3814 bytes[3] = (char_u)sb_num;
3815 byte_count = 4;
3816#endif
3817 }
3818 else
3819 {
3820#ifdef USE_ON_FLY_SCROLL
3821 scrollbar_value = value;
3822
3823 if (State & NORMAL)
3824 gui_do_horiz_scroll();
3825 else if (State & INSERT)
3826 ins_horscroll();
3827 else if (State & CMDLINE)
3828 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003829 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
3831 gui_do_horiz_scroll();
3832 redrawcmdline();
3833 }
3834 }
3835 if (old_leftcol != curwin->w_leftcol)
3836 {
3837 updateWindow(curwin); /* update window, status and cmdline */
3838 setcursor();
3839 }
3840#else
3841 bytes[0] = CSI;
3842 bytes[1] = KS_HOR_SCROLLBAR;
3843 bytes[2] = KE_FILLER;
3844 byte_count = 3;
3845#endif
3846 }
3847
3848#ifdef USE_ON_FLY_SCROLL
3849# ifdef FEAT_SCROLLBIND
3850 /*
3851 * synchronize other windows, as necessary according to 'scrollbind'
3852 */
3853 if (curwin->w_p_scb
3854 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3855 || (sb->wp == curwin && (curwin->w_topline != old_topline
3856# ifdef FEAT_DIFF
3857 || curwin->w_topfill != old_topfill
3858# endif
3859 ))))
3860 {
3861 do_check_scrollbind(TRUE);
3862 /* need to update the window right here */
3863 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3864 if (wp->w_redr_type > 0)
3865 updateWindow(wp);
3866 setcursor();
3867 }
3868# endif
3869 out_flush();
3870 gui_update_cursor(FALSE, TRUE);
3871#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003872 add_to_input_buf(bytes, byte_count);
3873 add_long_to_buf((long_u)value, bytes);
3874 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875#endif
3876}
3877
3878/*
3879 * Scrollbar stuff:
3880 */
3881
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003882/*
3883 * Called when something in the window layout has changed.
3884 */
3885 void
3886gui_may_update_scrollbars()
3887{
3888 if (gui.in_use && starting == 0)
3889 {
3890 out_flush();
3891 gui_init_which_components(NULL);
3892 gui_update_scrollbars(TRUE);
3893 }
3894 need_mouse_correct = TRUE;
3895}
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 void
3898gui_update_scrollbars(force)
3899 int force; /* Force all scrollbars to get updated */
3900{
3901 win_T *wp;
3902 scrollbar_T *sb;
3903 long val, size, max; /* need 32 bits here */
3904 int which_sb;
3905 int h, y;
3906#ifdef FEAT_VERTSPLIT
3907 static win_T *prev_curwin = NULL;
3908#endif
3909
3910 /* Update the horizontal scrollbar */
3911 gui_update_horiz_scrollbar(force);
3912
3913#ifndef WIN3264
3914 /* Return straight away if there is neither a left nor right scrollbar.
3915 * On MS-Windows this is required anyway for scrollwheel messages. */
3916 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3917 return;
3918#endif
3919
3920 /*
3921 * Don't want to update a scrollbar while we're dragging it. But if we
3922 * have both a left and right scrollbar, and we drag one of them, we still
3923 * need to update the other one.
3924 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003925 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3926 && gui.which_scrollbars[SBAR_LEFT]
3927 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
3929 /*
3930 * If we have two scrollbars and one of them is being dragged, just
3931 * copy the scrollbar position from the dragged one to the other one.
3932 */
3933 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3934 if (gui.dragged_wp != NULL)
3935 gui_mch_set_scrollbar_thumb(
3936 &gui.dragged_wp->w_scrollbars[which_sb],
3937 gui.dragged_wp->w_scrollbars[0].value,
3938 gui.dragged_wp->w_scrollbars[0].size,
3939 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941
3942 /* avoid that moving components around generates events */
3943 ++hold_gui_events;
3944
3945 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3946 {
3947 if (wp->w_buffer == NULL) /* just in case */
3948 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003949 /* Skip a scrollbar that is being dragged. */
3950 if (!force && (gui.dragged_sb == SBAR_LEFT
3951 || gui.dragged_sb == SBAR_RIGHT)
3952 && gui.dragged_wp == wp)
3953 continue;
3954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#ifdef SCROLL_PAST_END
3956 max = wp->w_buffer->b_ml.ml_line_count - 1;
3957#else
3958 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3959#endif
3960 if (max < 0) /* empty buffer */
3961 max = 0;
3962 val = wp->w_topline - 1;
3963 size = wp->w_height;
3964#ifdef SCROLL_PAST_END
3965 if (val > max) /* just in case */
3966 val = max;
3967#else
3968 if (size > max + 1) /* just in case */
3969 size = max + 1;
3970 if (val > max - size + 1)
3971 val = max - size + 1;
3972#endif
3973 if (val < 0) /* minimal value is 0 */
3974 val = 0;
3975
3976 /*
3977 * Scrollbar at index 0 (the left one) contains all the information.
3978 * It would be the same info for left and right so we just store it for
3979 * one of them.
3980 */
3981 sb = &wp->w_scrollbars[0];
3982
3983 /*
3984 * Note: no check for valid w_botline. If it's not valid the
3985 * scrollbars will be updated later anyway.
3986 */
3987 if (size < 1 || wp->w_botline - 2 > max)
3988 {
3989 /*
3990 * This can happen during changing files. Just don't update the
3991 * scrollbar for now.
3992 */
3993 sb->height = 0; /* Force update next time */
3994 if (gui.which_scrollbars[SBAR_LEFT])
3995 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3996 if (gui.which_scrollbars[SBAR_RIGHT])
3997 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3998 continue;
3999 }
4000 if (force || sb->height != wp->w_height
4001#ifdef FEAT_WINDOWS
4002 || sb->top != wp->w_winrow
4003 || sb->status_height != wp->w_status_height
4004# ifdef FEAT_VERTSPLIT
4005 || sb->width != wp->w_width
4006 || prev_curwin != curwin
4007# endif
4008#endif
4009 )
4010 {
4011 /* Height, width or position of scrollbar has changed. For
4012 * vertical split: curwin changed. */
4013 sb->height = wp->w_height;
4014#ifdef FEAT_WINDOWS
4015 sb->top = wp->w_winrow;
4016 sb->status_height = wp->w_status_height;
4017# ifdef FEAT_VERTSPLIT
4018 sb->width = wp->w_width;
4019# endif
4020#endif
4021
4022 /* Calculate height and position in pixels */
4023 h = (sb->height + sb->status_height) * gui.char_height;
4024 y = sb->top * gui.char_height + gui.border_offset;
4025#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4026 if (gui.menu_is_active)
4027 y += gui.menu_height;
4028#endif
4029
4030#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4031 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4032# ifdef FEAT_GUI_ATHENA
4033 y += gui.toolbar_height;
4034# else
4035# ifdef FEAT_GUI_MSWIN
4036 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4037# endif
4038# endif
4039#endif
4040
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004041#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4042 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004043 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004044#endif
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#ifdef FEAT_WINDOWS
4047 if (wp->w_winrow == 0)
4048#endif
4049 {
4050 /* Height of top scrollbar includes width of top border */
4051 h += gui.border_offset;
4052 y -= gui.border_offset;
4053 }
4054 if (gui.which_scrollbars[SBAR_LEFT])
4055 {
4056 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4057 gui.left_sbar_x, y,
4058 gui.scrollbar_width, h);
4059 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4060 }
4061 if (gui.which_scrollbars[SBAR_RIGHT])
4062 {
4063 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4064 gui.right_sbar_x, y,
4065 gui.scrollbar_width, h);
4066 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4067 }
4068 }
4069
4070 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4071 * checking if the thumb moved at least a pixel. Only do this for
4072 * Athena, most other GUIs require the update anyway to make the
4073 * arrows work. */
4074#ifdef FEAT_GUI_ATHENA
4075 if (max == 0)
4076 y = 0;
4077 else
4078 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4079 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4080#else
4081 if (force || sb->value != val || sb->size != size || sb->max != max)
4082#endif
4083 {
4084 /* Thumb of scrollbar has moved */
4085 sb->value = val;
4086#ifdef FEAT_GUI_ATHENA
4087 sb->pixval = y;
4088#endif
4089 sb->size = size;
4090 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004091 if (gui.which_scrollbars[SBAR_LEFT]
4092 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4094 val, size, max);
4095 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004096 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4098 val, size, max);
4099 }
4100 }
4101#ifdef FEAT_VERTSPLIT
4102 prev_curwin = curwin;
4103#endif
4104 --hold_gui_events;
4105}
4106
4107/*
4108 * Enable or disable a scrollbar.
4109 * Check for scrollbars for vertically split windows which are not enabled
4110 * sometimes.
4111 */
4112 static void
4113gui_do_scrollbar(wp, which, enable)
4114 win_T *wp;
4115 int which; /* SBAR_LEFT or SBAR_RIGHT */
4116 int enable; /* TRUE to enable scrollbar */
4117{
4118#ifdef FEAT_VERTSPLIT
4119 int midcol = curwin->w_wincol + curwin->w_width / 2;
4120 int has_midcol = (wp->w_wincol <= midcol
4121 && wp->w_wincol + wp->w_width >= midcol);
4122
4123 /* Only enable scrollbars that contain the middle column of the current
4124 * window. */
4125 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4126 {
4127 /* Scrollbars only on one side. Don't enable scrollbars that don't
4128 * contain the middle column of the current window. */
4129 if (!has_midcol)
4130 enable = FALSE;
4131 }
4132 else
4133 {
4134 /* Scrollbars on both sides. Don't enable scrollbars that neither
4135 * contain the middle column of the current window nor are on the far
4136 * side. */
4137 if (midcol > Columns / 2)
4138 {
4139 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4140 enable = FALSE;
4141 }
4142 else
4143 {
4144 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4145 : !has_midcol)
4146 enable = FALSE;
4147 }
4148 }
4149#endif
4150 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4151}
4152
4153/*
4154 * Scroll a window according to the values set in the globals current_scrollbar
4155 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4156 * or FALSE otherwise.
4157 */
4158 int
4159gui_do_scroll()
4160{
4161 win_T *wp, *save_wp;
4162 int i;
4163 long nlines;
4164 pos_T old_cursor;
4165 linenr_T old_topline;
4166#ifdef FEAT_DIFF
4167 int old_topfill;
4168#endif
4169
4170 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4171 if (wp == NULL)
4172 break;
4173 if (wp == NULL)
4174 /* Couldn't find window */
4175 return FALSE;
4176
4177 /*
4178 * Compute number of lines to scroll. If zero, nothing to do.
4179 */
4180 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4181 if (nlines == 0)
4182 return FALSE;
4183
4184 save_wp = curwin;
4185 old_topline = wp->w_topline;
4186#ifdef FEAT_DIFF
4187 old_topfill = wp->w_topfill;
4188#endif
4189 old_cursor = wp->w_cursor;
4190 curwin = wp;
4191 curbuf = wp->w_buffer;
4192 if (nlines < 0)
4193 scrolldown(-nlines, gui.dragged_wp == NULL);
4194 else
4195 scrollup(nlines, gui.dragged_wp == NULL);
4196 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4197 * the mouse-up event already, but we still want it to behave like when
4198 * dragging. But not the next click in an arrow. */
4199 if (gui.dragged_sb == SBAR_NONE)
4200 gui.dragged_wp = NULL;
4201
4202 if (old_topline != wp->w_topline
4203#ifdef FEAT_DIFF
4204 || old_topfill != wp->w_topfill
4205#endif
4206 )
4207 {
4208 if (p_so != 0)
4209 {
4210 cursor_correct(); /* fix window for 'so' */
4211 update_topline(); /* avoid up/down jump */
4212 }
4213 if (old_cursor.lnum != wp->w_cursor.lnum)
4214 coladvance(wp->w_curswant);
4215#ifdef FEAT_SCROLLBIND
4216 wp->w_scbind_pos = wp->w_topline;
4217#endif
4218 }
4219
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004220 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4221 validate_cursor();
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 curwin = save_wp;
4224 curbuf = save_wp->w_buffer;
4225
4226 /*
4227 * Don't call updateWindow() when nothing has changed (it will overwrite
4228 * the status line!).
4229 */
4230 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004231 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#ifdef FEAT_DIFF
4233 || old_topfill != wp->w_topfill
4234#endif
4235 )
4236 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004237 int type = VALID;
4238
4239#ifdef FEAT_INS_EXPAND
4240 if (pum_visible())
4241 {
4242 type = NOT_VALID;
4243 wp->w_lines_valid = 0;
4244 }
4245#endif
4246 /* Don't set must_redraw here, it may cause the popup menu to
4247 * disappear when losing focus after a scrollbar drag. */
4248 if (wp->w_redr_type < type)
4249 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 updateWindow(wp); /* update window, status line, and cmdline */
4251 }
4252
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004253#ifdef FEAT_INS_EXPAND
4254 /* May need to redraw the popup menu. */
4255 if (pum_visible())
4256 pum_redraw();
4257#endif
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4260}
4261
4262
4263/*
4264 * Horizontal scrollbar stuff:
4265 */
4266
4267/*
4268 * Return length of line "lnum" for horizontal scrolling.
4269 */
4270 static colnr_T
4271scroll_line_len(lnum)
4272 linenr_T lnum;
4273{
4274 char_u *p;
4275 colnr_T col;
4276 int w;
4277
4278 p = ml_get(lnum);
4279 col = 0;
4280 if (*p != NUL)
4281 for (;;)
4282 {
4283 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004284 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (*p == NUL) /* don't count the last character */
4286 break;
4287 col += w;
4288 }
4289 return col;
4290}
4291
4292/* Remember which line is currently the longest, so that we don't have to
4293 * search for it when scrolling horizontally. */
4294static linenr_T longest_lnum = 0;
4295
4296 static void
4297gui_update_horiz_scrollbar(force)
4298 int force;
4299{
4300 long value, size, max; /* need 32 bit ints here */
4301
4302 if (!gui.which_scrollbars[SBAR_BOTTOM])
4303 return;
4304
4305 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4306 return;
4307
4308 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4309 return;
4310
4311 /*
4312 * It is possible for the cursor to be invalid if we're in the middle of
4313 * something (like changing files). If so, don't do anything for now.
4314 */
4315 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4316 {
4317 gui.bottom_sbar.value = -1;
4318 return;
4319 }
4320
4321 size = W_WIDTH(curwin);
4322 if (curwin->w_p_wrap)
4323 {
4324 value = 0;
4325#ifdef SCROLL_PAST_END
4326 max = 0;
4327#else
4328 max = W_WIDTH(curwin) - 1;
4329#endif
4330 }
4331 else
4332 {
4333 value = curwin->w_leftcol;
4334
4335 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4336 * line numbers, topline and botline can be invalid when displaying is
4337 * postponed. */
4338 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4339 && curwin->w_topline <= curwin->w_cursor.lnum
4340 && curwin->w_botline > curwin->w_cursor.lnum
4341 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4342 {
4343 linenr_T lnum;
4344 colnr_T n;
4345
4346 /* Use maximum of all visible lines. Remember the lnum of the
4347 * longest line, clostest to the cursor line. Used when scrolling
4348 * below. */
4349 max = 0;
4350 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4351 {
4352 n = scroll_line_len(lnum);
4353 if (n > (colnr_T)max)
4354 {
4355 max = n;
4356 longest_lnum = lnum;
4357 }
4358 else if (n == (colnr_T)max
4359 && abs((int)(lnum - curwin->w_cursor.lnum))
4360 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4361 longest_lnum = lnum;
4362 }
4363 }
4364 else
4365 /* Use cursor line only. */
4366 max = scroll_line_len(curwin->w_cursor.lnum);
4367#ifdef FEAT_VIRTUALEDIT
4368 if (virtual_active())
4369 {
4370 /* May move the cursor even further to the right. */
4371 if (curwin->w_virtcol >= (colnr_T)max)
4372 max = curwin->w_virtcol;
4373 }
4374#endif
4375
4376#ifndef SCROLL_PAST_END
4377 max += W_WIDTH(curwin) - 1;
4378#endif
4379 /* The line number isn't scrolled, thus there is less space when
4380 * 'number' is set (also for 'foldcolumn'). */
4381 size -= curwin_col_off();
4382#ifndef SCROLL_PAST_END
4383 max -= curwin_col_off();
4384#endif
4385 }
4386
4387#ifndef SCROLL_PAST_END
4388 if (value > max - size + 1)
4389 value = max - size + 1; /* limit the value to allowable range */
4390#endif
4391
4392#ifdef FEAT_RIGHTLEFT
4393 if (curwin->w_p_rl)
4394 {
4395 value = max + 1 - size - value;
4396 if (value < 0)
4397 {
4398 size += value;
4399 value = 0;
4400 }
4401 }
4402#endif
4403 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4404 && max == gui.bottom_sbar.max)
4405 return;
4406
4407 gui.bottom_sbar.value = value;
4408 gui.bottom_sbar.size = size;
4409 gui.bottom_sbar.max = max;
4410 gui.prev_wrap = curwin->w_p_wrap;
4411
4412 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4413}
4414
4415/*
4416 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4417 */
4418 int
4419gui_do_horiz_scroll()
4420{
4421 /* no wrapping, no scrolling */
4422 if (curwin->w_p_wrap)
4423 return FALSE;
4424
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004425 if ((long_u)curwin->w_leftcol == scrollbar_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 return FALSE;
4427
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004428 curwin->w_leftcol = (colnr_T)scrollbar_value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 /* When the line of the cursor is too short, move the cursor to the
4431 * longest visible line. Do a sanity check on "longest_lnum", just in
4432 * case. */
4433 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4434 && longest_lnum >= curwin->w_topline
4435 && longest_lnum < curwin->w_botline
4436 && !virtual_active())
4437 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004438 if (scrollbar_value > (long_u)scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 curwin->w_cursor.lnum = longest_lnum;
4441 curwin->w_cursor.col = 0;
4442 }
4443 }
4444
4445 return leftcol_changed();
4446}
4447
4448/*
4449 * Check that none of the colors are the same as the background color
4450 */
4451 void
4452gui_check_colors()
4453{
4454 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4455 {
4456 gui_set_bg_color((char_u *)"White");
4457 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4458 gui_set_fg_color((char_u *)"Black");
4459 }
4460}
4461
Bram Moolenaar3918c952005-03-15 22:34:55 +00004462 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463gui_set_fg_color(name)
4464 char_u *name;
4465{
4466 gui.norm_pixel = gui_get_color(name);
4467 hl_set_fg_color_name(vim_strsave(name));
4468}
4469
Bram Moolenaar3918c952005-03-15 22:34:55 +00004470 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471gui_set_bg_color(name)
4472 char_u *name;
4473{
4474 gui.back_pixel = gui_get_color(name);
4475 hl_set_bg_color_name(vim_strsave(name));
4476}
4477
4478/*
4479 * Allocate a color by name.
4480 * Returns INVALCOLOR and gives an error message when failed.
4481 */
4482 guicolor_T
4483gui_get_color(name)
4484 char_u *name;
4485{
4486 guicolor_T t;
4487
4488 if (*name == NUL)
4489 return INVALCOLOR;
4490 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004491
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004493#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 && gui.in_use
4495#endif
4496 )
4497 EMSG2(_("E254: Cannot allocate color %s"), name);
4498 return t;
4499}
4500
4501/*
4502 * Return the grey value of a color (range 0-255).
4503 */
4504 int
4505gui_get_lightness(pixel)
4506 guicolor_T pixel;
4507{
4508 long_u rgb = gui_mch_get_rgb(pixel);
4509
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004510 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004511 + (((rgb >> 8) & 0xff) * 587)
4512 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513}
4514
4515#if defined(FEAT_GUI_X11) || defined(PROTO)
4516 void
4517gui_new_scrollbar_colors()
4518{
4519 win_T *wp;
4520
4521 /* Nothing to do if GUI hasn't started yet. */
4522 if (!gui.in_use)
4523 return;
4524
4525 FOR_ALL_WINDOWS(wp)
4526 {
4527 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4528 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4529 }
4530 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4531}
4532#endif
4533
4534/*
4535 * Call this when focus has changed.
4536 */
4537 void
4538gui_focus_change(in_focus)
4539 int in_focus;
4540{
4541/*
4542 * Skip this code to avoid drawing the cursor when debugging and switching
4543 * between the debugger window and gvim.
4544 */
4545#if 1
4546 gui.in_focus = in_focus;
4547 out_flush(); /* make sure output has been written */
4548 gui_update_cursor(TRUE, FALSE);
4549
4550# ifdef FEAT_XIM
4551 xim_set_focus(in_focus);
4552# endif
4553
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004554 /* Put events in the input queue only when allowed.
4555 * ui_focus_change() isn't called directly, because it invokes
4556 * autocommands and that must not happen asynchronously. */
4557 if (!hold_gui_events)
4558 {
4559 char_u bytes[3];
4560
4561 bytes[0] = CSI;
4562 bytes[1] = KS_EXTRA;
4563 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4564 add_to_input_buf(bytes, 3);
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566#endif
4567}
4568
4569/*
4570 * Called when the mouse moved (but not when dragging).
4571 */
4572 void
4573gui_mouse_moved(x, y)
4574 int x;
4575 int y;
4576{
4577 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004578 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaard3667a22006-03-16 21:35:52 +00004580 /* Ignore this while still starting up. */
4581 if (!gui.in_use || gui.starting)
4582 return;
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_MOUSESHAPE
4585 /* Get window pointer, and update mouse shape as well. */
4586 wp = xy2win(x, y);
4587#endif
4588
4589 /* Only handle this when 'mousefocus' set and ... */
4590 if (p_mousef
4591 && !hold_gui_events /* not holding events */
4592 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4593 && State != HITRETURN /* but not hit-return prompt */
4594 && msg_scrolled == 0 /* no scrolled message */
4595 && !need_mouse_correct /* not moving the pointer */
4596 && gui.in_focus) /* gvim in focus */
4597 {
4598 /* Don't move the mouse when it's left or right of the Vim window */
4599 if (x < 0 || x > Columns * gui.char_width)
4600 return;
4601#ifndef FEAT_MOUSESHAPE
4602 wp = xy2win(x, y);
4603#endif
4604 if (wp == curwin || wp == NULL)
4605 return; /* still in the same old window, or none at all */
4606
Bram Moolenaar9c102382006-05-03 21:26:49 +00004607#ifdef FEAT_WINDOWS
4608 /* Ignore position in the tab pages line. */
4609 if (Y_2_ROW(y) < tabline_height())
4610 return;
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * format a mouse click on status line input
4615 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004616 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4617 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
4619 if (finish_op)
4620 {
4621 /* abort the current operator first */
4622 st[0] = ESC;
4623 add_to_input_buf(st, 1);
4624 }
4625 st[0] = CSI;
4626 st[1] = KS_MOUSE;
4627 st[2] = KE_FILLER;
4628 st[3] = (char_u)MOUSE_LEFT;
4629 fill_mouse_coord(st + 4,
4630#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004631 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#else
4633 -1,
4634#endif
4635 wp->w_height + W_WINROW(wp));
4636
4637 add_to_input_buf(st, 8);
4638 st[3] = (char_u)MOUSE_RELEASE;
4639 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#ifdef FEAT_GUI_GTK
4641 /* Need to wake up the main loop */
4642 if (gtk_main_level() > 0)
4643 gtk_main_quit();
4644#endif
4645 }
4646}
4647
4648/*
4649 * Called when mouse should be moved to window with focus.
4650 */
4651 void
4652gui_mouse_correct()
4653{
4654 int x, y;
4655 win_T *wp = NULL;
4656
4657 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004658
4659 if (!(gui.in_use && p_mousef))
4660 return;
4661
4662 gui_mch_getmouse(&x, &y);
4663 /* Don't move the mouse when it's left or right of the Vim window */
4664 if (x < 0 || x > Columns * gui.char_width)
4665 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004666 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004667# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004668 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004669# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004670 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004671 wp = xy2win(x, y);
4672 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004674 validate_cline_row();
4675 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4676 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679}
4680
4681/*
4682 * Find window where the mouse pointer "y" coordinate is in.
4683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 static win_T *
4685xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004686 int x UNUSED;
4687 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689#ifdef FEAT_WINDOWS
4690 int row;
4691 int col;
4692 win_T *wp;
4693
4694 row = Y_2_ROW(y);
4695 col = X_2_COL(x);
4696 if (row < 0 || col < 0) /* before first window */
4697 return NULL;
4698 wp = mouse_find_win(&row, &col);
4699# ifdef FEAT_MOUSESHAPE
4700 if (State == HITRETURN || State == ASKMORE)
4701 {
4702 if (Y_2_ROW(y) >= msg_row)
4703 update_mouseshape(SHAPE_IDX_MOREL);
4704 else
4705 update_mouseshape(SHAPE_IDX_MORE);
4706 }
4707 else if (row > wp->w_height) /* below status line */
4708 update_mouseshape(SHAPE_IDX_CLINE);
4709# ifdef FEAT_VERTSPLIT
4710 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004711 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 update_mouseshape(SHAPE_IDX_VSEP);
4713# endif
4714 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004715 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 update_mouseshape(SHAPE_IDX_STATUS);
4717 else
4718 update_mouseshape(-2);
4719# endif
4720 return wp;
4721#else
4722 return firstwin;
4723#endif
4724}
4725
4726/*
4727 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4728 * File names may be given to redefine the args list.
4729 */
4730 void
4731ex_gui(eap)
4732 exarg_T *eap;
4733{
4734 char_u *arg = eap->arg;
4735
4736 /*
4737 * Check for "-f" argument: foreground, don't fork.
4738 * Also don't fork when started with "gvim -f".
4739 * Do fork when using "gui -b".
4740 */
4741 if (arg[0] == '-'
4742 && (arg[1] == 'f' || arg[1] == 'b')
4743 && (arg[2] == NUL || vim_iswhite(arg[2])))
4744 {
4745 gui.dofork = (arg[1] == 'b');
4746 eap->arg = skipwhite(eap->arg + 2);
4747 }
4748 if (!gui.in_use)
4749 {
4750 /* Clear the command. Needed for when forking+exiting, to avoid part
4751 * of the argument ending up after the shell prompt. */
4752 msg_clr_eos_force();
4753 gui_start();
4754 }
4755 if (!ends_excmd(*eap->arg))
4756 ex_next(eap);
4757}
4758
4759#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004760 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761/*
4762 * This is shared between Athena, Motif and GTK.
4763 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766/*
4767 * Callback function for do_in_runtimepath().
4768 */
4769 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004770gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004772 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004774 char_u *gfp_buffer = cookie;
4775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (STRLEN(fname) >= MAXPATHL)
4777 *gfp_buffer = NUL;
4778 else
4779 STRCPY(gfp_buffer, fname);
4780}
4781
4782/*
4783 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4784 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4785 */
4786 int
4787gui_find_bitmap(name, buffer, ext)
4788 char_u *name;
4789 char_u *buffer;
4790 char *ext;
4791{
4792 if (STRLEN(name) > MAXPATHL - 14)
4793 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004794 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004795 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4796 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 return FAIL;
4798 return OK;
4799}
4800
4801# if !defined(HAVE_GTK2) || defined(PROTO)
4802/*
4803 * Given the name of the "icon=" argument, try finding the bitmap file for the
4804 * icon. If it is an absolute path name, use it as it is. Otherwise append
4805 * "ext" and search for it in 'runtimepath'.
4806 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4807 * contains "name".
4808 */
4809 void
4810gui_find_iconfile(name, buffer, ext)
4811 char_u *name;
4812 char_u *buffer;
4813 char *ext;
4814{
4815 char_u buf[MAXPATHL + 1];
4816
4817 expand_env(name, buffer, MAXPATHL);
4818 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4819 STRCPY(buffer, buf);
4820}
4821# endif
4822#endif
4823
Bram Moolenaar9372a112005-12-06 19:59:18 +00004824#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 void
4826display_errors()
4827{
4828 char_u *p;
4829
4830 if (isatty(2))
4831 fflush(stderr);
4832 else if (error_ga.ga_data != NULL)
4833 {
4834 /* avoid putting up a message box with blanks only */
4835 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4836 if (!isspace(*p))
4837 {
4838 /* Truncate a very long message, it will go off-screen. */
4839 if (STRLEN(p) > 2000)
4840 STRCPY(p + 2000 - 14, "...(truncated)");
4841 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4842 p, (char_u *)_("&Ok"), 1, NULL);
4843 break;
4844 }
4845 ga_clear(&error_ga);
4846 }
4847}
4848#endif
4849
4850#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4851/*
4852 * Return TRUE if still starting up and there is no place to enter text.
4853 * For GTK and X11 we check if stderr is not a tty, which means we were
4854 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4855 * allow typing on stdin.
4856 */
4857 int
4858no_console_input()
4859{
4860 return ((!gui.in_use || gui.starting)
4861# ifndef NO_CONSOLE
4862 && !isatty(0) && !isatty(2)
4863# endif
4864 );
4865}
4866#endif
4867
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004868#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00004869 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004870 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871/*
4872 * Update the current window and the screen.
4873 */
4874 void
4875gui_update_screen()
4876{
4877 update_topline();
4878 validate_cursor();
Bram Moolenaarec80df72008-05-07 19:46:51 +00004879#ifdef FEAT_AUTOCMD
4880 /* Trigger CursorMoved if the cursor moved. */
4881 if (!finish_op && has_cursormoved()
4882 && !equalpos(last_cursormoved, curwin->w_cursor))
4883 {
4884 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4885 last_cursormoved = curwin->w_cursor;
4886 }
4887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 update_screen(0); /* may need to update the screen */
4889 setcursor();
4890 out_flush(); /* make sure output has been written */
4891 gui_update_cursor(TRUE, FALSE);
4892 gui_mch_flush();
4893}
4894#endif
4895
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004896#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4898
4899/*
4900 * Get the text to use in a find/replace dialog. Uses the last search pattern
4901 * if the argument is empty.
4902 * Returns an allocated string.
4903 */
4904 char_u *
4905get_find_dialog_text(arg, wwordp, mcasep)
4906 char_u *arg;
4907 int *wwordp; /* return: TRUE if \< \> found */
4908 int *mcasep; /* return: TRUE if \C found */
4909{
4910 char_u *text;
4911
4912 if (*arg == NUL)
4913 text = last_search_pat();
4914 else
4915 text = arg;
4916 if (text != NULL)
4917 {
4918 text = vim_strsave(text);
4919 if (text != NULL)
4920 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004921 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int i;
4923
4924 /* Remove "\V" */
4925 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4926 {
4927 mch_memmove(text, text + 2, (size_t)(len - 1));
4928 len -= 2;
4929 }
4930
4931 /* Recognize "\c" and "\C" and remove. */
4932 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4933 {
4934 *mcasep = (text[1] == 'C');
4935 mch_memmove(text, text + 2, (size_t)(len - 1));
4936 len -= 2;
4937 }
4938
4939 /* Recognize "\<text\>" and remove. */
4940 if (len >= 4
4941 && STRNCMP(text, "\\<", 2) == 0
4942 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4943 {
4944 *wwordp = TRUE;
4945 mch_memmove(text, text + 2, (size_t)(len - 4));
4946 text[len - 4] = NUL;
4947 }
4948
4949 /* Recognize "\/" or "\?" and remove. */
4950 for (i = 0; i + 1 < len; ++i)
4951 if (text[i] == '\\' && (text[i + 1] == '/'
4952 || text[i + 1] == '?'))
4953 {
4954 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4955 --len;
4956 }
4957 }
4958 }
4959 return text;
4960}
4961
4962/*
4963 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4964 */
4965 static void
4966concat_esc(gap, text, what)
4967 garray_T *gap;
4968 char_u *text;
4969 int what;
4970{
4971 while (*text != NUL)
4972 {
4973#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004974 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (l > 1)
4977 {
4978 while (--l >= 0)
4979 ga_append(gap, *text++);
4980 continue;
4981 }
4982#endif
4983 if (*text == what)
4984 ga_append(gap, '\\');
4985 ga_append(gap, *text);
4986 ++text;
4987 }
4988}
4989
4990/*
4991 * Handle the press of a button in the find-replace dialog.
4992 * Return TRUE when something was added to the input buffer.
4993 */
4994 int
4995gui_do_findrepl(flags, find_text, repl_text, down)
4996 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4997 char_u *find_text;
4998 char_u *repl_text;
4999 int down; /* Search downwards. */
5000{
5001 garray_T ga;
5002 int i;
5003 int type = (flags & FRD_TYPE_MASK);
5004 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005005 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005006 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005009 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 ga_concat(&ga, (char_u *)"%s/");
5011
5012 ga_concat(&ga, (char_u *)"\\V");
5013 if (flags & FRD_MATCH_CASE)
5014 ga_concat(&ga, (char_u *)"\\C");
5015 else
5016 ga_concat(&ga, (char_u *)"\\c");
5017 if (flags & FRD_WHOLE_WORD)
5018 ga_concat(&ga, (char_u *)"\\<");
5019 if (type == FRD_REPLACEALL || down)
5020 concat_esc(&ga, find_text, '/'); /* escape slashes */
5021 else
5022 concat_esc(&ga, find_text, '?'); /* escape '?' */
5023 if (flags & FRD_WHOLE_WORD)
5024 ga_concat(&ga, (char_u *)"\\>");
5025
5026 if (type == FRD_REPLACEALL)
5027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005029 /* escape / and \ */
5030 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5031 if (p != NULL)
5032 ga_concat(&ga, p);
5033 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005035 }
5036 ga_append(&ga, NUL);
5037
5038 if (type == FRD_REPLACE)
5039 {
5040 /* Do the replacement when the text at the cursor matches. Thus no
5041 * replacement is done if the cursor was moved! */
5042 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5043 regmatch.rm_ic = 0;
5044 if (regmatch.regprog != NULL)
5045 {
5046 p = ml_get_cursor();
5047 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5048 && regmatch.startp[0] == p)
5049 {
5050 /* Clear the command line to remove any old "No match"
5051 * error. */
5052 msg_end_prompt();
5053
5054 if (u_save_cursor() == OK)
5055 {
5056 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005057 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005058
5059 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005060 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005061 ins_str(repl_text);
5062 }
5063 }
5064 else
5065 MSG(_("No match at cursor, finding next"));
5066 vim_free(regmatch.regprog);
5067 }
5068 }
5069
5070 if (type == FRD_REPLACEALL)
5071 {
5072 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005073 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 do_cmdline_cmd(ga.ga_data);
5075 }
5076 else
5077 {
5078 /* Search for the next match. */
5079 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005081 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 msg_scroll = i; /* don't let an error message set msg_scroll */
5083 }
5084
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005085 /* Don't want to pass did_emsg to other code, it may cause disabling
5086 * syntax HL if we were busy redrawing. */
5087 did_emsg = save_did_emsg;
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (State & (NORMAL | INSERT))
5090 {
5091 gui_update_screen(); /* update the screen */
5092 msg_didout = 0; /* overwrite any message */
5093 need_wait_return = FALSE; /* don't wait for return */
5094 }
5095
5096 vim_free(ga.ga_data);
5097 return (ga.ga_len > 0);
5098}
5099
5100#endif
5101
5102#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5103 || defined(FEAT_GUI_MSWIN) \
5104 || defined(FEAT_GUI_MAC) \
5105 || defined(PROTO)
5106
5107#ifdef FEAT_WINDOWS
5108static void gui_wingoto_xy __ARGS((int x, int y));
5109
5110/*
5111 * Jump to the window at specified point (x, y).
5112 */
5113 static void
5114gui_wingoto_xy(x, y)
5115 int x;
5116 int y;
5117{
5118 int row = Y_2_ROW(y);
5119 int col = X_2_COL(x);
5120 win_T *wp;
5121
5122 if (row >= 0 && col >= 0)
5123 {
5124 wp = mouse_find_win(&row, &col);
5125 if (wp != NULL && wp != curwin)
5126 win_goto(wp);
5127 }
5128}
5129#endif
5130
5131/*
5132 * Process file drop. Mouse cursor position, key modifiers, name of files
5133 * and count of files are given. Argument "fnames[count]" has full pathnames
5134 * of dropped files, they will be freed in this function, and caller can't use
5135 * fnames after call this function.
5136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 void
5138gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005139 int x UNUSED;
5140 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 int_u modifiers;
5142 char_u **fnames;
5143 int count;
5144{
5145 int i;
5146 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005147 static int entered = FALSE;
5148
5149 /*
5150 * This function is called by event handlers. Just in case we get a
5151 * second event before the first one is handled, ignore the second one.
5152 * Not sure if this can ever happen, just in case.
5153 */
5154 if (entered)
5155 return;
5156 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 /*
5159 * When the cursor is at the command line, add the file names to the
5160 * command line, don't edit the files.
5161 */
5162 if (State & CMDLINE)
5163 {
5164 shorten_filenames(fnames, count);
5165 for (i = 0; i < count; ++i)
5166 {
5167 if (fnames[i] != NULL)
5168 {
5169 if (i > 0)
5170 add_to_input_buf((char_u*)" ", 1);
5171
5172 /* We don't know what command is used thus we can't be sure
5173 * about which characters need to be escaped. Only escape the
5174 * most common ones. */
5175# ifdef BACKSLASH_IN_FILENAME
5176 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5177# else
5178 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5179# endif
5180 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005181 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 vim_free(p);
5183 vim_free(fnames[i]);
5184 }
5185 }
5186 vim_free(fnames);
5187 }
5188 else
5189 {
5190 /* Go to the window under mouse cursor, then shorten given "fnames" by
5191 * current window, because a window can have local current dir. */
5192# ifdef FEAT_WINDOWS
5193 gui_wingoto_xy(x, y);
5194# endif
5195 shorten_filenames(fnames, count);
5196
5197 /* If Shift held down, remember the first item. */
5198 if ((modifiers & MOUSE_SHIFT) != 0)
5199 p = vim_strsave(fnames[0]);
5200 else
5201 p = NULL;
5202
5203 /* Handle the drop, :edit or :split to get to the file. This also
5204 * frees fnames[]. Skip this if there is only one item it's a
5205 * directory and Shift is held down. */
5206 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5207 && mch_isdir(fnames[0]))
5208 {
5209 vim_free(fnames[0]);
5210 vim_free(fnames);
5211 }
5212 else
5213 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5214
5215 /* If Shift held down, change to first file's directory. If the first
5216 * item is a directory, change to that directory (and let the explorer
5217 * plugin show the contents). */
5218 if (p != NULL)
5219 {
5220 if (mch_isdir(p))
5221 {
5222 if (mch_chdir((char *)p) == 0)
5223 shorten_fnames(TRUE);
5224 }
5225 else if (vim_chdirfile(p) == OK)
5226 shorten_fnames(TRUE);
5227 vim_free(p);
5228 }
5229
5230 /* Update the screen display */
5231 update_screen(NOT_VALID);
5232# ifdef FEAT_MENU
5233 gui_update_menus(0);
5234# endif
5235 setcursor();
5236 out_flush();
5237 gui_update_cursor(FALSE, FALSE);
5238 gui_mch_flush();
5239 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005240
5241 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242}
5243#endif