blob: bcc3c68bc4bacd02a7fc1f2f860175ba537bfa3b [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 */
681/*ARGSUSED*/
682 int
683gui_init_font(font_list, fontset)
684 char_u *font_list;
685 int fontset;
686{
687#define FONTLEN 320
688 char_u font_name[FONTLEN];
689 int font_list_empty = FALSE;
690 int ret = FAIL;
691
692 if (!gui.in_use)
693 return FAIL;
694
695 font_name[0] = NUL;
696 if (*font_list == NUL)
697 font_list_empty = TRUE;
698 else
699 {
700#ifdef FEAT_XFONTSET
701 /* When using a fontset, the whole list of fonts is one name. */
702 if (fontset)
703 ret = gui_mch_init_font(font_list, TRUE);
704 else
705#endif
706 while (*font_list != NUL)
707 {
708 /* Isolate one comma separated font name. */
709 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
710
711 /* Careful!!! The Win32 version of gui_mch_init_font(), when
712 * called with "*" will change p_guifont to the selected font
713 * name, which frees the old value. This makes font_list
714 * invalid. Thus when OK is returned here, font_list must no
715 * longer be used! */
716 if (gui_mch_init_font(font_name, FALSE) == OK)
717 {
718#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
719 /* If it's a Unicode font, try setting 'guifontwide' to a
720 * similar double-width font. */
721 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
722 && strstr((char *)font_name, "10646") != NULL)
723 set_guifontwide(font_name);
724#endif
725 ret = OK;
726 break;
727 }
728 }
729 }
730
731 if (ret != OK
732 && STRCMP(font_list, "*") != 0
733 && (font_list_empty || gui.norm_font == NOFONT))
734 {
735 /*
736 * Couldn't load any font in 'font_list', keep the current font if
737 * there is one. If 'font_list' is empty, or if there is no current
738 * font, tell gui_mch_init_font() to try to find a font we can load.
739 */
740 ret = gui_mch_init_font(NULL, FALSE);
741 }
742
743 if (ret == OK)
744 {
745#ifndef HAVE_GTK2
746 /* Set normal font as current font */
747# ifdef FEAT_XFONTSET
748 if (gui.fontset != NOFONTSET)
749 gui_mch_set_fontset(gui.fontset);
750 else
751# endif
752 gui_mch_set_font(gui.norm_font);
753#endif
754 gui_set_shellsize(FALSE,
755#ifdef MSWIN
756 TRUE
757#else
758 FALSE
759#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000760 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762
763 return ret;
764}
765
766#if defined(FEAT_MBYTE) || defined(PROTO)
767# ifndef HAVE_GTK2
768/*
769 * Try setting 'guifontwide' to a font twice as wide as "name".
770 */
771 static void
772set_guifontwide(name)
773 char_u *name;
774{
775 int i = 0;
776 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
777 char_u *wp = NULL;
778 char_u *p;
779 GuiFont font;
780
781 wp = wide_name;
782 for (p = name; *p != NUL; ++p)
783 {
784 *wp++ = *p;
785 if (*p == '-')
786 {
787 ++i;
788 if (i == 6) /* font type: change "--" to "-*-" */
789 {
790 if (p[1] == '-')
791 *wp++ = '*';
792 }
793 else if (i == 12) /* found the width */
794 {
795 ++p;
796 i = getdigits(&p);
797 if (i != 0)
798 {
799 /* Double the width specification. */
800 sprintf((char *)wp, "%d%s", i * 2, p);
801 font = gui_mch_get_font(wide_name, FALSE);
802 if (font != NOFONT)
803 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000804 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 gui.wide_font = font;
806 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000807 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809 }
810 break;
811 }
812 }
813 }
814}
815# endif /* !HAVE_GTK2 */
816
817/*
818 * Get the font for 'guifontwide'.
819 * Return FAIL for an invalid font name.
820 */
821 int
822gui_get_wide_font()
823{
824 GuiFont font = NOFONT;
825 char_u font_name[FONTLEN];
826 char_u *p;
827
828 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
829 return OK; /* Will give an error message later. */
830
831 if (p_guifontwide != NULL && *p_guifontwide != NUL)
832 {
833 for (p = p_guifontwide; *p != NUL; )
834 {
835 /* Isolate one comma separated font name. */
836 (void)copy_option_part(&p, font_name, FONTLEN, ",");
837 font = gui_mch_get_font(font_name, FALSE);
838 if (font != NOFONT)
839 break;
840 }
841 if (font == NOFONT)
842 return FAIL;
843 }
844
845 gui_mch_free_font(gui.wide_font);
846#ifdef HAVE_GTK2
847 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
848 if (font != NOFONT && gui.norm_font != NOFONT
849 && pango_font_description_equal(font, gui.norm_font))
850 {
851 gui.wide_font = NOFONT;
852 gui_mch_free_font(font);
853 }
854 else
855#endif
856 gui.wide_font = font;
857 return OK;
858}
859#endif
860
861 void
862gui_set_cursor(row, col)
863 int row;
864 int col;
865{
866 gui.row = row;
867 gui.col = col;
868}
869
870/*
871 * gui_check_pos - check if the cursor is on the screen.
872 */
873 static void
874gui_check_pos()
875{
876 if (gui.row >= screen_Rows)
877 gui.row = screen_Rows - 1;
878 if (gui.col >= screen_Columns)
879 gui.col = screen_Columns - 1;
880 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
881 gui.cursor_is_valid = FALSE;
882}
883
884/*
885 * Redraw the cursor if necessary or when forced.
886 * Careful: The contents of ScreenLines[] must match what is on the screen,
887 * otherwise this goes wrong. May need to call out_flush() first.
888 */
889 void
890gui_update_cursor(force, clear_selection)
891 int force; /* when TRUE, update even when not moved */
892 int clear_selection;/* clear selection under cursor */
893{
894 int cur_width = 0;
895 int cur_height = 0;
896 int old_hl_mask;
897 int idx;
898 int id;
899 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
900 int cattr; /* cursor attributes */
901 int attr;
902 attrentry_T *aep = NULL;
903
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +0000904 /* Don't update the cursor when halfway busy scrolling or the screen size
905 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
906 if (!can_update_cursor || screen_Columns != gui.num_cols
907 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return;
909
910 gui_check_pos();
911 if (!gui.cursor_is_valid || force
912 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
913 {
914 gui_undraw_cursor();
915 if (gui.row < 0)
916 return;
917#ifdef USE_IM_CONTROL
918 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
919 im_set_position(gui.row, gui.col);
920#endif
921 gui.cursor_row = gui.row;
922 gui.cursor_col = gui.col;
923
924 /* Only write to the screen after ScreenLines[] has been initialized */
925 if (!screen_cleared || ScreenLines == NULL)
926 return;
927
928 /* Clear the selection if we are about to write over it */
929 if (clear_selection)
930 clip_may_clear_selection(gui.row, gui.row);
931 /* Check that the cursor is inside the shell (resizing may have made
932 * it invalid) */
933 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
934 return;
935
936 gui.cursor_is_valid = TRUE;
937
938 /*
939 * How the cursor is drawn depends on the current mode.
940 */
941 idx = get_shape_idx(FALSE);
942 if (State & LANGMAP)
943 id = shape_table[idx].id_lm;
944 else
945 id = shape_table[idx].id;
946
947 /* get the colors and attributes for the cursor. Default is inverted */
948 cfg = INVALCOLOR;
949 cbg = INVALCOLOR;
950 cattr = HL_INVERSE;
951 gui_mch_set_blinking(shape_table[idx].blinkwait,
952 shape_table[idx].blinkon,
953 shape_table[idx].blinkoff);
954 if (id > 0)
955 {
956 cattr = syn_id2colors(id, &cfg, &cbg);
957#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
958 {
959 static int iid;
960 guicolor_T fg, bg;
961
Bram Moolenaarc236c162008-07-13 17:41:49 +0000962 if (
963# ifdef HAVE_GTK2
964 preedit_get_status()
965# else
966 im_get_status()
967# endif
968 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
970 iid = syn_name2id((char_u *)"CursorIM");
971 if (iid > 0)
972 {
973 syn_id2colors(iid, &fg, &bg);
974 if (bg != INVALCOLOR)
975 cbg = bg;
976 if (fg != INVALCOLOR)
977 cfg = fg;
978 }
979 }
980 }
981#endif
982 }
983
984 /*
985 * Get the attributes for the character under the cursor.
986 * When no cursor color was given, use the character color.
987 */
988 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
989 if (attr > HL_ALL)
990 aep = syn_gui_attr2entry(attr);
991 if (aep != NULL)
992 {
993 attr = aep->ae_attr;
994 if (cfg == INVALCOLOR)
995 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
996 : aep->ae_u.gui.fg_color);
997 if (cbg == INVALCOLOR)
998 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
999 : aep->ae_u.gui.bg_color);
1000 }
1001 if (cfg == INVALCOLOR)
1002 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1003 if (cbg == INVALCOLOR)
1004 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1005
1006#ifdef FEAT_XIM
1007 if (aep != NULL)
1008 {
1009 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1010 : aep->ae_u.gui.bg_color);
1011 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1012 : aep->ae_u.gui.fg_color);
1013 if (xim_bg_color == INVALCOLOR)
1014 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1015 : gui.back_pixel;
1016 if (xim_fg_color == INVALCOLOR)
1017 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1018 : gui.norm_pixel;
1019 }
1020 else
1021 {
1022 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1023 : gui.back_pixel;
1024 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1025 : gui.norm_pixel;
1026 }
1027#endif
1028
1029 attr &= ~HL_INVERSE;
1030 if (cattr & HL_INVERSE)
1031 {
1032 cc = cbg;
1033 cbg = cfg;
1034 cfg = cc;
1035 }
1036 cattr &= ~HL_INVERSE;
1037
1038 /*
1039 * When we don't have window focus, draw a hollow cursor.
1040 */
1041 if (!gui.in_focus)
1042 {
1043 gui_mch_draw_hollow_cursor(cbg);
1044 return;
1045 }
1046
1047 old_hl_mask = gui.highlight_mask;
1048 if (shape_table[idx].shape == SHAPE_BLOCK
1049#ifdef FEAT_HANGULIN
1050 || composing_hangul
1051#endif
1052 )
1053 {
1054 /*
1055 * Draw the text character with the cursor colors. Use the
1056 * character attributes plus the cursor attributes.
1057 */
1058 gui.highlight_mask = (cattr | attr);
1059#ifdef FEAT_HANGULIN
1060 if (composing_hangul)
1061 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1062 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1063 else
1064#endif
1065 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1066 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1067 }
1068 else
1069 {
1070#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1071 int col_off = FALSE;
1072#endif
1073 /*
1074 * First draw the partial cursor, then overwrite with the text
1075 * character, using a transparent background.
1076 */
1077 if (shape_table[idx].shape == SHAPE_VER)
1078 {
1079 cur_height = gui.char_height;
1080 cur_width = (gui.char_width * shape_table[idx].percentage
1081 + 99) / 100;
1082 }
1083 else
1084 {
1085 cur_height = (gui.char_height * shape_table[idx].percentage
1086 + 99) / 100;
1087 cur_width = gui.char_width;
1088 }
1089#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001090 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1091 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
1093 /* Double wide character. */
1094 if (shape_table[idx].shape != SHAPE_VER)
1095 cur_width += gui.char_width;
1096# ifdef FEAT_RIGHTLEFT
1097 if (CURSOR_BAR_RIGHT)
1098 {
1099 /* gui.col points to the left halve of the character but
1100 * the vertical line needs to be on the right halve.
1101 * A double-wide horizontal line is also drawn from the
1102 * right halve in gui_mch_draw_part_cursor(). */
1103 col_off = TRUE;
1104 ++gui.col;
1105 }
1106# endif
1107 }
1108#endif
1109 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1110#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1111 if (col_off)
1112 --gui.col;
1113#endif
1114
1115#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1116 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1117 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1118 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1119 (guicolor_T)0, (guicolor_T)0, 0);
1120#endif
1121 }
1122 gui.highlight_mask = old_hl_mask;
1123 }
1124}
1125
1126#if defined(FEAT_MENU) || defined(PROTO)
1127 void
1128gui_position_menu()
1129{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001130# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (gui.menu_is_active && gui.in_use)
1132 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1133# endif
1134}
1135#endif
1136
1137/*
1138 * Position the various GUI components (text area, menu). The vertical
1139 * scrollbars are NOT handled here. See gui_update_scrollbars().
1140 */
1141/*ARGSUSED*/
1142 static void
1143gui_position_components(total_width)
1144 int total_width;
1145{
1146 int text_area_x;
1147 int text_area_y;
1148 int text_area_width;
1149 int text_area_height;
1150
1151 /* avoid that moving components around generates events */
1152 ++hold_gui_events;
1153
1154 text_area_x = 0;
1155 if (gui.which_scrollbars[SBAR_LEFT])
1156 text_area_x += gui.scrollbar_width;
1157
1158 text_area_y = 0;
1159#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1160 gui.menu_width = total_width;
1161 if (gui.menu_is_active)
1162 text_area_y += gui.menu_height;
1163#endif
1164#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1165 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1166 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1167#endif
1168
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001169# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001170 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001171 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001172 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001173#endif
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1176 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1177 {
1178# ifdef FEAT_GUI_ATHENA
1179 gui_mch_set_toolbar_pos(0, text_area_y,
1180 gui.menu_width, gui.toolbar_height);
1181# endif
1182 text_area_y += gui.toolbar_height;
1183 }
1184#endif
1185
1186 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1187 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1188
1189 gui_mch_set_text_area_pos(text_area_x,
1190 text_area_y,
1191 text_area_width,
1192 text_area_height
1193#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1194 + xim_get_status_area_height()
1195#endif
1196 );
1197#ifdef FEAT_MENU
1198 gui_position_menu();
1199#endif
1200 if (gui.which_scrollbars[SBAR_BOTTOM])
1201 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1202 text_area_x,
1203 text_area_y + text_area_height,
1204 text_area_width,
1205 gui.scrollbar_height);
1206 gui.left_sbar_x = 0;
1207 gui.right_sbar_x = text_area_x + text_area_width;
1208
1209 --hold_gui_events;
1210}
1211
Bram Moolenaar02743632005-07-25 20:42:36 +00001212/*
1213 * Get the width of the widgets and decorations to the side of the text area.
1214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 int
1216gui_get_base_width()
1217{
1218 int base_width;
1219
1220 base_width = 2 * gui.border_offset;
1221 if (gui.which_scrollbars[SBAR_LEFT])
1222 base_width += gui.scrollbar_width;
1223 if (gui.which_scrollbars[SBAR_RIGHT])
1224 base_width += gui.scrollbar_width;
1225 return base_width;
1226}
1227
Bram Moolenaar02743632005-07-25 20:42:36 +00001228/*
1229 * Get the height of the widgets and decorations above and below the text area.
1230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 int
1232gui_get_base_height()
1233{
1234 int base_height;
1235
1236 base_height = 2 * gui.border_offset;
1237 if (gui.which_scrollbars[SBAR_BOTTOM])
1238 base_height += gui.scrollbar_height;
1239#ifdef FEAT_GUI_GTK
1240 /* We can't take the sizes properly into account until anything is
1241 * realized. Therefore we recalculate all the values here just before
1242 * setting the size. (--mdcki) */
1243#else
1244# ifdef FEAT_MENU
1245 if (gui.menu_is_active)
1246 base_height += gui.menu_height;
1247# endif
1248# ifdef FEAT_TOOLBAR
1249 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1250# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1251 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1252# else
1253 base_height += gui.toolbar_height;
1254# endif
1255# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001256# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1257 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001258 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001259 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261# ifdef FEAT_FOOTER
1262 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1263 base_height += gui.footer_height;
1264# endif
1265# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1266 base_height += gui_mch_text_area_extra_height();
1267# endif
1268#endif
1269 return base_height;
1270}
1271
1272/*
1273 * Should be called after the GUI shell has been resized. Its arguments are
1274 * the new width and height of the shell in pixels.
1275 */
1276 void
1277gui_resize_shell(pixel_width, pixel_height)
1278 int pixel_width;
1279 int pixel_height;
1280{
1281 static int busy = FALSE;
1282
1283 if (!gui.shell_created) /* ignore when still initializing */
1284 return;
1285
1286 /*
1287 * Can't resize the screen while it is being redrawn. Remember the new
1288 * size and handle it later.
1289 */
1290 if (updating_screen || busy)
1291 {
1292 new_pixel_width = pixel_width;
1293 new_pixel_height = pixel_height;
1294 return;
1295 }
1296
1297again:
1298 busy = TRUE;
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /* Flush pending output before redrawing */
1301 out_flush();
1302
1303 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001304 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 gui_position_components(pixel_width);
1307
1308 gui_reset_scroll_region();
1309 /*
1310 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1311 * at the last line here (why does it have to be one row too low?).
1312 */
1313 if (State == ASKMORE || State == CONFIRM)
1314 gui.row = gui.num_rows;
1315
1316 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1317 * the safe side. */
1318 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1319 || gui.num_rows != Rows || gui.num_cols != Columns)
1320 shell_resized();
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 gui_update_scrollbars(TRUE);
1323 gui_update_cursor(FALSE, TRUE);
1324#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1325 xim_set_status_area();
1326#endif
1327
1328 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 /*
1331 * We could have been called again while redrawing the screen.
1332 * Need to do it all again with the latest size then.
1333 */
1334 if (new_pixel_height)
1335 {
1336 pixel_width = new_pixel_width;
1337 pixel_height = new_pixel_height;
1338 new_pixel_width = 0;
1339 new_pixel_height = 0;
1340 goto again;
1341 }
1342}
1343
1344/*
1345 * Check if gui_resize_shell() must be called.
1346 */
1347 void
1348gui_may_resize_shell()
1349{
1350 int h, w;
1351
1352 if (new_pixel_height)
1353 {
1354 /* careful: gui_resize_shell() may postpone the resize again if we
1355 * were called indirectly by it */
1356 w = new_pixel_width;
1357 h = new_pixel_height;
1358 new_pixel_width = 0;
1359 new_pixel_height = 0;
1360 gui_resize_shell(w, h);
1361 }
1362}
1363
1364 int
1365gui_get_shellsize()
1366{
1367 Rows = gui.num_rows;
1368 Columns = gui.num_cols;
1369 return OK;
1370}
1371
1372/*
1373 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001374 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1375 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 */
1377/*ARGSUSED*/
1378 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001379gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 int mustset; /* set by the user */
1381 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001382 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 int base_width;
1385 int base_height;
1386 int width;
1387 int height;
1388 int min_width;
1389 int min_height;
1390 int screen_w;
1391 int screen_h;
1392
1393 if (!gui.shell_created)
1394 return;
1395
1396#ifdef MSWIN
1397 /* If not setting to a user specified size and maximized, calculate the
1398 * number of characters that fit in the maximized window. */
1399 if (!mustset && gui_mch_maximized())
1400 {
1401 gui_mch_newfont();
1402 return;
1403 }
1404#endif
1405
1406 base_width = gui_get_base_width();
1407 base_height = gui_get_base_height();
1408#ifdef USE_SUN_WORKSHOP
1409 if (!mustset && usingSunWorkShop
1410 && workshop_get_width_height(&width, &height))
1411 {
1412 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1413 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1414 }
1415 else
1416#endif
1417 {
1418 width = Columns * gui.char_width + base_width;
1419 height = Rows * gui.char_height + base_height;
1420 }
1421
1422 if (fit_to_display)
1423 {
1424 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001425 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
1427 Columns = (screen_w - base_width) / gui.char_width;
1428 if (Columns < MIN_COLUMNS)
1429 Columns = MIN_COLUMNS;
1430 width = Columns * gui.char_width + base_width;
1431 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001432 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 Rows = (screen_h - base_height) / gui.char_height;
1435 check_shellsize();
1436 height = Rows * gui.char_height + base_height;
1437 }
1438 }
1439 gui.num_cols = Columns;
1440 gui.num_rows = Rows;
1441
1442 min_width = base_width + MIN_COLUMNS * gui.char_width;
1443 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001444# ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001445 min_height += tabline_height() * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001449 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (fit_to_display)
1451 {
1452 int x, y;
1453
1454 /* Some window managers put the Vim window left of/above the screen. */
1455 gui_mch_update();
1456 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1457 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1458 }
1459
1460 gui_position_components(width);
1461 gui_update_scrollbars(TRUE);
1462 gui_reset_scroll_region();
1463}
1464
1465/*
1466 * Called when Rows and/or Columns has changed.
1467 */
1468 void
1469gui_new_shellsize()
1470{
1471 gui_reset_scroll_region();
1472}
1473
1474/*
1475 * Make scroll region cover whole screen.
1476 */
1477 void
1478gui_reset_scroll_region()
1479{
1480 gui.scroll_region_top = 0;
1481 gui.scroll_region_bot = gui.num_rows - 1;
1482 gui.scroll_region_left = 0;
1483 gui.scroll_region_right = gui.num_cols - 1;
1484}
1485
1486 void
1487gui_start_highlight(mask)
1488 int mask;
1489{
1490 if (mask > HL_ALL) /* highlight code */
1491 gui.highlight_mask = mask;
1492 else /* mask */
1493 gui.highlight_mask |= mask;
1494}
1495
1496 void
1497gui_stop_highlight(mask)
1498 int mask;
1499{
1500 if (mask > HL_ALL) /* highlight code */
1501 gui.highlight_mask = HL_NORMAL;
1502 else /* mask */
1503 gui.highlight_mask &= ~mask;
1504}
1505
1506/*
1507 * Clear a rectangular region of the screen from text pos (row1, col1) to
1508 * (row2, col2) inclusive.
1509 */
1510 void
1511gui_clear_block(row1, col1, row2, col2)
1512 int row1;
1513 int col1;
1514 int row2;
1515 int col2;
1516{
1517 /* Clear the selection if we are about to write over it */
1518 clip_may_clear_selection(row1, row2);
1519
1520 gui_mch_clear_block(row1, col1, row2, col2);
1521
1522 /* Invalidate cursor if it was in this block */
1523 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1524 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1525 gui.cursor_is_valid = FALSE;
1526}
1527
1528/*
1529 * Write code to update the cursor later. This avoids the need to flush the
1530 * output buffer before calling gui_update_cursor().
1531 */
1532 void
1533gui_update_cursor_later()
1534{
1535 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1536}
1537
1538 void
1539gui_write(s, len)
1540 char_u *s;
1541 int len;
1542{
1543 char_u *p;
1544 int arg1 = 0, arg2 = 0;
1545 /* this doesn't make sense, disabled until someone can explain why it
1546 * would be needed */
1547#if 0 && (defined(RISCOS) || defined(WIN16))
1548 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1549 our redraw speed will suffer */
1550#else
1551 int force_cursor = FALSE; /* force cursor update */
1552#endif
1553 int force_scrollbar = FALSE;
1554 static win_T *old_curwin = NULL;
1555
1556/* #define DEBUG_GUI_WRITE */
1557#ifdef DEBUG_GUI_WRITE
1558 {
1559 int i;
1560 char_u *str;
1561
1562 printf("gui_write(%d):\n ", len);
1563 for (i = 0; i < len; i++)
1564 if (s[i] == ESC)
1565 {
1566 if (i != 0)
1567 printf("\n ");
1568 printf("<ESC>");
1569 }
1570 else
1571 {
1572 str = transchar_byte(s[i]);
1573 if (str[0] && str[1])
1574 printf("<%s>", (char *)str);
1575 else
1576 printf("%s", (char *)str);
1577 }
1578 printf("\n");
1579 }
1580#endif
1581 while (len)
1582 {
1583 if (s[0] == ESC && s[1] == '|')
1584 {
1585 p = s + 2;
1586 if (VIM_ISDIGIT(*p))
1587 {
1588 arg1 = getdigits(&p);
1589 if (p > s + len)
1590 break;
1591 if (*p == ';')
1592 {
1593 ++p;
1594 arg2 = getdigits(&p);
1595 if (p > s + len)
1596 break;
1597 }
1598 }
1599 switch (*p)
1600 {
1601 case 'C': /* Clear screen */
1602 clip_scroll_selection(9999);
1603 gui_mch_clear_all();
1604 gui.cursor_is_valid = FALSE;
1605 force_scrollbar = TRUE;
1606 break;
1607 case 'M': /* Move cursor */
1608 gui_set_cursor(arg1, arg2);
1609 break;
1610 case 's': /* force cursor (shape) update */
1611 force_cursor = TRUE;
1612 break;
1613 case 'R': /* Set scroll region */
1614 if (arg1 < arg2)
1615 {
1616 gui.scroll_region_top = arg1;
1617 gui.scroll_region_bot = arg2;
1618 }
1619 else
1620 {
1621 gui.scroll_region_top = arg2;
1622 gui.scroll_region_bot = arg1;
1623 }
1624 break;
1625#ifdef FEAT_VERTSPLIT
1626 case 'V': /* Set vertical scroll region */
1627 if (arg1 < arg2)
1628 {
1629 gui.scroll_region_left = arg1;
1630 gui.scroll_region_right = arg2;
1631 }
1632 else
1633 {
1634 gui.scroll_region_left = arg2;
1635 gui.scroll_region_right = arg1;
1636 }
1637 break;
1638#endif
1639 case 'd': /* Delete line */
1640 gui_delete_lines(gui.row, 1);
1641 break;
1642 case 'D': /* Delete lines */
1643 gui_delete_lines(gui.row, arg1);
1644 break;
1645 case 'i': /* Insert line */
1646 gui_insert_lines(gui.row, 1);
1647 break;
1648 case 'I': /* Insert lines */
1649 gui_insert_lines(gui.row, arg1);
1650 break;
1651 case '$': /* Clear to end-of-line */
1652 gui_clear_block(gui.row, gui.col, gui.row,
1653 (int)Columns - 1);
1654 break;
1655 case 'h': /* Turn on highlighting */
1656 gui_start_highlight(arg1);
1657 break;
1658 case 'H': /* Turn off highlighting */
1659 gui_stop_highlight(arg1);
1660 break;
1661 case 'f': /* flash the window (visual bell) */
1662 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1663 break;
1664 default:
1665 p = s + 1; /* Skip the ESC */
1666 break;
1667 }
1668 len -= (int)(++p - s);
1669 s = p;
1670 }
1671 else if (
1672#ifdef EBCDIC
1673 CtrlChar(s[0]) != 0 /* Ctrl character */
1674#else
1675 s[0] < 0x20 /* Ctrl character */
1676#endif
1677#ifdef FEAT_SIGN_ICONS
1678 && s[0] != SIGN_BYTE
1679# ifdef FEAT_NETBEANS_INTG
1680 && s[0] != MULTISIGN_BYTE
1681# endif
1682#endif
1683 )
1684 {
1685 if (s[0] == '\n') /* NL */
1686 {
1687 gui.col = 0;
1688 if (gui.row < gui.scroll_region_bot)
1689 gui.row++;
1690 else
1691 gui_delete_lines(gui.scroll_region_top, 1);
1692 }
1693 else if (s[0] == '\r') /* CR */
1694 {
1695 gui.col = 0;
1696 }
1697 else if (s[0] == '\b') /* Backspace */
1698 {
1699 if (gui.col)
1700 --gui.col;
1701 }
1702 else if (s[0] == Ctrl_L) /* cursor-right */
1703 {
1704 ++gui.col;
1705 }
1706 else if (s[0] == Ctrl_G) /* Beep */
1707 {
1708 gui_mch_beep();
1709 }
1710 /* Other Ctrl character: shouldn't happen! */
1711
1712 --len; /* Skip this char */
1713 ++s;
1714 }
1715 else
1716 {
1717 p = s;
1718 while (len > 0 && (
1719#ifdef EBCDIC
1720 CtrlChar(*p) == 0
1721#else
1722 *p >= 0x20
1723#endif
1724#ifdef FEAT_SIGN_ICONS
1725 || *p == SIGN_BYTE
1726# ifdef FEAT_NETBEANS_INTG
1727 || *p == MULTISIGN_BYTE
1728# endif
1729#endif
1730 ))
1731 {
1732 len--;
1733 p++;
1734 }
1735 gui_outstr(s, (int)(p - s));
1736 s = p;
1737 }
1738 }
1739
1740 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1741 * set). */
1742 if (force_cursor)
1743 gui_update_cursor(TRUE, TRUE);
1744
1745 /* When switching to another window the dragging must have stopped.
1746 * Required for GTK, dragged_sb isn't reset. */
1747 if (old_curwin != curwin)
1748 gui.dragged_sb = SBAR_NONE;
1749
1750 /* Update the scrollbars after clearing the screen or when switched
1751 * to another window.
1752 * Update the horizontal scrollbar always, it's difficult to check all
1753 * situations where it might change. */
1754 if (force_scrollbar || old_curwin != curwin)
1755 gui_update_scrollbars(force_scrollbar);
1756 else
1757 gui_update_horiz_scrollbar(FALSE);
1758 old_curwin = curwin;
1759
1760 /*
1761 * We need to make sure this is cleared since Athena doesn't tell us when
1762 * he is done dragging. Do the same for GTK.
1763 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001764#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 gui.dragged_sb = SBAR_NONE;
1766#endif
1767
1768 gui_mch_flush(); /* In case vim decides to take a nap */
1769}
1770
1771/*
1772 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1773 * produces wrong results. Call gui_dont_update_cursor() before that code and
1774 * gui_can_update_cursor() afterwards.
1775 */
1776 void
1777gui_dont_update_cursor()
1778{
1779 if (gui.in_use)
1780 {
1781 /* Undraw the cursor now, we probably can't do it after the change. */
1782 gui_undraw_cursor();
1783 can_update_cursor = FALSE;
1784 }
1785}
1786
1787 void
1788gui_can_update_cursor()
1789{
1790 can_update_cursor = TRUE;
1791 /* No need to update the cursor right now, there is always more output
1792 * after scrolling. */
1793}
1794
1795 static void
1796gui_outstr(s, len)
1797 char_u *s;
1798 int len;
1799{
1800 int this_len;
1801#ifdef FEAT_MBYTE
1802 int cells;
1803#endif
1804
1805 if (len == 0)
1806 return;
1807
1808 if (len < 0)
1809 len = (int)STRLEN(s);
1810
1811 while (len > 0)
1812 {
1813#ifdef FEAT_MBYTE
1814 if (has_mbyte)
1815 {
1816 /* Find out how many chars fit in the current line. */
1817 cells = 0;
1818 for (this_len = 0; this_len < len; )
1819 {
1820 cells += (*mb_ptr2cells)(s + this_len);
1821 if (gui.col + cells > Columns)
1822 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001823 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
1825 if (this_len > len)
1826 this_len = len; /* don't include following composing char */
1827 }
1828 else
1829#endif
1830 if (gui.col + len > Columns)
1831 this_len = Columns - gui.col;
1832 else
1833 this_len = len;
1834
1835 (void)gui_outstr_nowrap(s, this_len,
1836 0, (guicolor_T)0, (guicolor_T)0, 0);
1837 s += this_len;
1838 len -= this_len;
1839#ifdef FEAT_MBYTE
1840 /* fill up for a double-width char that doesn't fit. */
1841 if (len > 0 && gui.col < Columns)
1842 (void)gui_outstr_nowrap((char_u *)" ", 1,
1843 0, (guicolor_T)0, (guicolor_T)0, 0);
1844#endif
1845 /* The cursor may wrap to the next line. */
1846 if (gui.col >= Columns)
1847 {
1848 gui.col = 0;
1849 gui.row++;
1850 }
1851 }
1852}
1853
1854/*
1855 * Output one character (may be one or two display cells).
1856 * Caller must check for valid "off".
1857 * Returns FAIL or OK, just like gui_outstr_nowrap().
1858 */
1859 static int
1860gui_screenchar(off, flags, fg, bg, back)
1861 int off; /* Offset from start of screen */
1862 int flags;
1863 guicolor_T fg, bg; /* colors for cursor */
1864 int back; /* backup this many chars when using bold trick */
1865{
1866#ifdef FEAT_MBYTE
1867 char_u buf[MB_MAXBYTES + 1];
1868
1869 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1870 if (enc_utf8 && ScreenLines[off] == 0)
1871 return OK;
1872
1873 if (enc_utf8 && ScreenLinesUC[off] != 0)
1874 /* Draw UTF-8 multi-byte character. */
1875 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1876 flags, fg, bg, back);
1877
1878 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1879 {
1880 buf[0] = ScreenLines[off];
1881 buf[1] = ScreenLines2[off];
1882 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1883 }
1884
1885 /* Draw non-multi-byte character or DBCS character. */
1886 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001887 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 flags, fg, bg, back);
1889#else
1890 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1891#endif
1892}
1893
1894#ifdef HAVE_GTK2
1895/*
1896 * Output the string at the given screen position. This is used in place
1897 * of gui_screenchar() where possible because Pango needs as much context
1898 * as possible to work nicely. It's a lot faster as well.
1899 */
1900 static int
1901gui_screenstr(off, len, flags, fg, bg, back)
1902 int off; /* Offset from start of screen */
1903 int len; /* string length in screen cells */
1904 int flags;
1905 guicolor_T fg, bg; /* colors for cursor */
1906 int back; /* backup this many chars when using bold trick */
1907{
1908 char_u *buf;
1909 int outlen = 0;
1910 int i;
1911 int retval;
1912
1913 if (len <= 0) /* "cannot happen"? */
1914 return OK;
1915
1916 if (enc_utf8)
1917 {
1918 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1919 if (buf == NULL)
1920 return OK; /* not much we could do here... */
1921
1922 for (i = off; i < off + len; ++i)
1923 {
1924 if (ScreenLines[i] == 0)
1925 continue; /* skip second half of double-width char */
1926
1927 if (ScreenLinesUC[i] == 0)
1928 buf[outlen++] = ScreenLines[i];
1929 else
1930 outlen += utfc_char2bytes(i, buf + outlen);
1931 }
1932
1933 buf[outlen] = NUL; /* only to aid debugging */
1934 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1935 vim_free(buf);
1936
1937 return retval;
1938 }
1939 else if (enc_dbcs == DBCS_JPNU)
1940 {
1941 buf = alloc((unsigned)(len * 2 + 1));
1942 if (buf == NULL)
1943 return OK; /* not much we could do here... */
1944
1945 for (i = off; i < off + len; ++i)
1946 {
1947 buf[outlen++] = ScreenLines[i];
1948
1949 /* handle double-byte single-width char */
1950 if (ScreenLines[i] == 0x8e)
1951 buf[outlen++] = ScreenLines2[i];
1952 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1953 buf[outlen++] = ScreenLines[++i];
1954 }
1955
1956 buf[outlen] = NUL; /* only to aid debugging */
1957 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1958 vim_free(buf);
1959
1960 return retval;
1961 }
1962 else
1963 {
1964 return gui_outstr_nowrap(&ScreenLines[off], len,
1965 flags, fg, bg, back);
1966 }
1967}
1968#endif /* HAVE_GTK2 */
1969
1970/*
1971 * Output the given string at the current cursor position. If the string is
1972 * too long to fit on the line, then it is truncated.
1973 * "flags":
1974 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1975 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001976 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * background.
1978 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1979 * it.
1980 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1981 * FAIL (the caller should start drawing "back" chars back).
1982 */
1983 int
1984gui_outstr_nowrap(s, len, flags, fg, bg, back)
1985 char_u *s;
1986 int len;
1987 int flags;
1988 guicolor_T fg, bg; /* colors for cursor */
1989 int back; /* backup this many chars when using bold trick */
1990{
1991 long_u highlight_mask;
1992 long_u hl_mask_todo;
1993 guicolor_T fg_color;
1994 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001995 guicolor_T sp_color;
Bram Moolenaar9372a112005-12-06 19:59:18 +00001996#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 GuiFont font = NOFONT;
1998# ifdef FEAT_XFONTSET
1999 GuiFontset fontset = NOFONTSET;
2000# endif
2001#endif
2002 attrentry_T *aep = NULL;
2003 int draw_flags;
2004 int col = gui.col;
2005#ifdef FEAT_SIGN_ICONS
2006 int draw_sign = FALSE;
2007# ifdef FEAT_NETBEANS_INTG
2008 int multi_sign = FALSE;
2009# endif
2010#endif
2011
2012 if (len < 0)
2013 len = (int)STRLEN(s);
2014 if (len == 0)
2015 return OK;
2016
2017#ifdef FEAT_SIGN_ICONS
2018 if (*s == SIGN_BYTE
2019# ifdef FEAT_NETBEANS_INTG
2020 || *s == MULTISIGN_BYTE
2021# endif
2022 )
2023 {
2024# ifdef FEAT_NETBEANS_INTG
2025 if (*s == MULTISIGN_BYTE)
2026 multi_sign = TRUE;
2027# endif
2028 /* draw spaces instead */
2029 s = (char_u *)" ";
2030 if (len == 1 && col > 0)
2031 --col;
2032 len = 2;
2033 draw_sign = TRUE;
2034 highlight_mask = 0;
2035 }
2036 else
2037#endif
2038 if (gui.highlight_mask > HL_ALL)
2039 {
2040 aep = syn_gui_attr2entry(gui.highlight_mask);
2041 if (aep == NULL) /* highlighting not set */
2042 highlight_mask = 0;
2043 else
2044 highlight_mask = aep->ae_attr;
2045 }
2046 else
2047 highlight_mask = gui.highlight_mask;
2048 hl_mask_todo = highlight_mask;
2049
Bram Moolenaar9372a112005-12-06 19:59:18 +00002050#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 /* Set the font */
2052 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2053 font = aep->ae_u.gui.font;
2054# ifdef FEAT_XFONTSET
2055 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2056 fontset = aep->ae_u.gui.fontset;
2057# endif
2058 else
2059 {
2060# ifdef FEAT_XFONTSET
2061 if (gui.fontset != NOFONTSET)
2062 fontset = gui.fontset;
2063 else
2064# endif
2065 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2066 {
2067 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2068 {
2069 font = gui.boldital_font;
2070 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2071 }
2072 else if (gui.bold_font != NOFONT)
2073 {
2074 font = gui.bold_font;
2075 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2076 }
2077 else
2078 font = gui.norm_font;
2079 }
2080 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2081 {
2082 font = gui.ital_font;
2083 hl_mask_todo &= ~HL_ITALIC;
2084 }
2085 else
2086 font = gui.norm_font;
2087 }
2088# ifdef FEAT_XFONTSET
2089 if (fontset != NOFONTSET)
2090 gui_mch_set_fontset(fontset);
2091 else
2092# endif
2093 gui_mch_set_font(font);
2094#endif
2095
2096 draw_flags = 0;
2097
2098 /* Set the color */
2099 bg_color = gui.back_pixel;
2100 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2101 {
2102 draw_flags |= DRAW_CURSOR;
2103 fg_color = fg;
2104 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002105 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 else if (aep != NULL)
2108 {
2109 fg_color = aep->ae_u.gui.fg_color;
2110 if (fg_color == INVALCOLOR)
2111 fg_color = gui.norm_pixel;
2112 bg_color = aep->ae_u.gui.bg_color;
2113 if (bg_color == INVALCOLOR)
2114 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002115 sp_color = aep->ae_u.gui.sp_color;
2116 if (sp_color == INVALCOLOR)
2117 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
2119 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002122 sp_color = fg_color;
2123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2126 {
2127#if defined(AMIGA) || defined(RISCOS)
2128 gui_mch_set_colors(bg_color, fg_color);
2129#else
2130 gui_mch_set_fg_color(bg_color);
2131 gui_mch_set_bg_color(fg_color);
2132#endif
2133 }
2134 else
2135 {
2136#if defined(AMIGA) || defined(RISCOS)
2137 gui_mch_set_colors(fg_color, bg_color);
2138#else
2139 gui_mch_set_fg_color(fg_color);
2140 gui_mch_set_bg_color(bg_color);
2141#endif
2142 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002143 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 /* Clear the selection if we are about to write over it */
2146 if (!(flags & GUI_MON_NOCLEAR))
2147 clip_may_clear_selection(gui.row, gui.row);
2148
2149
2150#ifndef MSWIN16_FASTTEXT
2151 /* If there's no bold font, then fake it */
2152 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2153 draw_flags |= DRAW_BOLD;
2154#endif
2155
2156 /*
2157 * When drawing bold or italic characters the spill-over from the left
2158 * neighbor may be destroyed. Let the caller backup to start redrawing
2159 * just after a blank.
2160 */
2161 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2162 return FAIL;
2163
Bram Moolenaar9372a112005-12-06 19:59:18 +00002164#if defined(RISCOS) || defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 /* If there's no italic font, then fake it.
2166 * For GTK2, we don't need a different font for italic style. */
2167 if (hl_mask_todo & HL_ITALIC)
2168 draw_flags |= DRAW_ITALIC;
2169
2170 /* Do we underline the text? */
2171 if (hl_mask_todo & HL_UNDERLINE)
2172 draw_flags |= DRAW_UNDERL;
2173#else
2174 /* Do we underline the text? */
2175 if ((hl_mask_todo & HL_UNDERLINE)
2176# ifndef MSWIN16_FASTTEXT
2177 || (hl_mask_todo & HL_ITALIC)
2178# endif
2179 )
2180 draw_flags |= DRAW_UNDERL;
2181#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002182 /* Do we undercurl the text? */
2183 if (hl_mask_todo & HL_UNDERCURL)
2184 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002186 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (flags & GUI_MON_TRS_CURSOR)
2188 draw_flags |= DRAW_TRANSP;
2189
2190 /*
2191 * Draw the text.
2192 */
2193#ifdef HAVE_GTK2
2194 /* The value returned is the length in display cells */
2195 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2196#else
2197# ifdef FEAT_MBYTE
2198 if (enc_utf8)
2199 {
2200 int start; /* index of bytes to be drawn */
2201 int cells; /* cellwidth of bytes to be drawn */
2202 int thislen; /* length of bytes to be drawin */
2203 int cn; /* cellwidth of current char */
2204 int i; /* index of current char */
2205 int c; /* current char value */
2206 int cl; /* byte length of current char */
2207 int comping; /* current char is composing */
2208 int scol = col; /* screen column */
2209 int dowide; /* use 'guifontwide' */
2210
2211 /* Break the string at a composing character, it has to be drawn on
2212 * top of the previous character. */
2213 start = 0;
2214 cells = 0;
2215 for (i = 0; i < len; i += cl)
2216 {
2217 c = utf_ptr2char(s + i);
2218 cn = utf_char2cells(c);
2219 if (cn > 1
2220# ifdef FEAT_XFONTSET
2221 && fontset == NOFONTSET
2222# endif
2223 && gui.wide_font != NOFONT)
2224 dowide = TRUE;
2225 else
2226 dowide = FALSE;
2227 comping = utf_iscomposing(c);
2228 if (!comping) /* count cells from non-composing chars */
2229 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002230 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (cl == 0) /* hit end of string */
2232 len = i + cl; /* len must be wrong "cannot happen" */
2233
2234 /* print the string so far if it's the last character or there is
2235 * a composing character. */
2236 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar9372a112005-12-06 19:59:18 +00002237# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 || (cn > 1
2239# ifdef FEAT_XFONTSET
2240 /* No fontset: At least draw char after wide char at
2241 * right position. */
2242 && fontset == NOFONTSET
2243# endif
2244 )
2245# endif
2246 )
2247 {
2248 if (comping || dowide)
2249 thislen = i - start;
2250 else
2251 thislen = i - start + cl;
2252 if (thislen > 0)
2253 {
2254 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2255 draw_flags);
2256 start += thislen;
2257 }
2258 scol += cells;
2259 cells = 0;
2260 if (dowide)
2261 {
2262 gui_mch_set_font(gui.wide_font);
2263 gui_mch_draw_string(gui.row, scol - cn,
2264 s + start, cl, draw_flags);
2265 gui_mch_set_font(font);
2266 start += cl;
2267 }
2268
Bram Moolenaar9372a112005-12-06 19:59:18 +00002269# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002270 /* No fontset: draw a space to fill the gap after a wide char
2271 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2273# ifdef FEAT_XFONTSET
2274 && fontset == NOFONTSET
2275# endif
2276 && !dowide)
2277 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2278 1, draw_flags);
2279# endif
2280 }
2281 /* Draw a composing char on top of the previous char. */
2282 if (comping)
2283 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002284# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002285 /* Carbon ATSUI autodraws composing char over previous char */
2286 gui_mch_draw_string(gui.row, scol, s + i, cl,
2287 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002288# else
2289 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2290 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 start = i + cl;
2293 }
2294 }
2295 /* The stuff below assumes "len" is the length in screen columns. */
2296 len = scol - col;
2297 }
2298 else
2299# endif
2300 {
2301 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2302# ifdef FEAT_MBYTE
2303 if (enc_dbcs == DBCS_JPNU)
2304 {
2305 int clen = 0;
2306 int i;
2307
2308 /* Get the length in display cells, this can be different from the
2309 * number of bytes for "euc-jp". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002310 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 clen += (*mb_ptr2cells)(s + i);
2312 len = clen;
2313 }
2314# endif
2315 }
2316#endif /* !HAVE_GTK2 */
2317
2318 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2319 gui.col = col + len;
2320
2321 /* May need to invert it when it's part of the selection. */
2322 if (flags & GUI_MON_NOCLEAR)
2323 clip_may_redraw_selection(gui.row, col, len);
2324
2325 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2326 {
2327 /* Invalidate the old physical cursor position if we wrote over it */
2328 if (gui.cursor_row == gui.row
2329 && gui.cursor_col >= col
2330 && gui.cursor_col < col + len)
2331 gui.cursor_is_valid = FALSE;
2332 }
2333
2334#ifdef FEAT_SIGN_ICONS
2335 if (draw_sign)
2336 /* Draw the sign on top of the spaces. */
2337 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2338# ifdef FEAT_NETBEANS_INTG
2339 if (multi_sign)
2340 netbeans_draw_multisign_indicator(gui.row);
2341# endif
2342#endif
2343
2344 return OK;
2345}
2346
2347/*
2348 * Un-draw the cursor. Actually this just redraws the character at the given
2349 * position. The character just before it too, for when it was in bold.
2350 */
2351 void
2352gui_undraw_cursor()
2353{
2354 if (gui.cursor_is_valid)
2355 {
2356#ifdef FEAT_HANGULIN
2357 if (composing_hangul
2358 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2359 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2360 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2361 gui.norm_pixel, gui.back_pixel, 0);
2362 else
2363 {
2364#endif
2365 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2366 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2367 && gui.cursor_col > 0)
2368 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2369 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2370#ifdef FEAT_HANGULIN
2371 if (composing_hangul)
2372 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2373 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2374 }
2375#endif
2376 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2377 * here in case it wasn't needed to undraw it. */
2378 gui.cursor_is_valid = FALSE;
2379 }
2380}
2381
2382 void
2383gui_redraw(x, y, w, h)
2384 int x;
2385 int y;
2386 int w;
2387 int h;
2388{
2389 int row1, col1, row2, col2;
2390
2391 row1 = Y_2_ROW(y);
2392 col1 = X_2_COL(x);
2393 row2 = Y_2_ROW(y + h - 1);
2394 col2 = X_2_COL(x + w - 1);
2395
2396 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2397
2398 /*
2399 * We may need to redraw the cursor, but don't take it upon us to change
2400 * its location after a scroll.
2401 * (maybe be more strict even and test col too?)
2402 * These things may be outside the update/clipping region and reality may
2403 * not reflect Vims internal ideas if these operations are clipped away.
2404 */
2405 if (gui.row == gui.cursor_row)
2406 gui_update_cursor(TRUE, TRUE);
2407}
2408
2409/*
2410 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2411 * from col1 to col2 (inclusive).
2412 * Return TRUE when the character before the first drawn character has
2413 * different attributes (may have to be redrawn too).
2414 */
2415 int
2416gui_redraw_block(row1, col1, row2, col2, flags)
2417 int row1;
2418 int col1;
2419 int row2;
2420 int col2;
2421 int flags; /* flags for gui_outstr_nowrap() */
2422{
2423 int old_row, old_col;
2424 long_u old_hl_mask;
2425 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002426 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 int idx, len;
2428 int back, nback;
2429 int retval = FALSE;
2430#ifdef FEAT_MBYTE
2431 int orig_col1, orig_col2;
2432#endif
2433
2434 /* Don't try to update when ScreenLines is not valid */
2435 if (!screen_cleared || ScreenLines == NULL)
2436 return retval;
2437
2438 /* Don't try to draw outside the shell! */
2439 /* Check everything, strange values may be caused by a big border width */
2440 col1 = check_col(col1);
2441 col2 = check_col(col2);
2442 row1 = check_row(row1);
2443 row2 = check_row(row2);
2444
2445 /* Remember where our cursor was */
2446 old_row = gui.row;
2447 old_col = gui.col;
2448 old_hl_mask = gui.highlight_mask;
2449#ifdef FEAT_MBYTE
2450 orig_col1 = col1;
2451 orig_col2 = col2;
2452#endif
2453
2454 for (gui.row = row1; gui.row <= row2; gui.row++)
2455 {
2456#ifdef FEAT_MBYTE
2457 /* When only half of a double-wide character is in the block, include
2458 * the other half. */
2459 col1 = orig_col1;
2460 col2 = orig_col2;
2461 off = LineOffset[gui.row];
2462 if (enc_dbcs != 0)
2463 {
2464 if (col1 > 0)
2465 col1 -= dbcs_screen_head_off(ScreenLines + off,
2466 ScreenLines + off + col1);
2467 col2 += dbcs_screen_tail_off(ScreenLines + off,
2468 ScreenLines + off + col2);
2469 }
2470 else if (enc_utf8)
2471 {
2472 if (ScreenLines[off + col1] == 0)
2473 --col1;
2474# ifdef HAVE_GTK2
2475 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2476 ++col2;
2477# endif
2478 }
2479#endif
2480 gui.col = col1;
2481 off = LineOffset[gui.row] + gui.col;
2482 len = col2 - col1 + 1;
2483
2484 /* Find how many chars back this highlighting starts, or where a space
2485 * is. Needed for when the bold trick is used */
2486 for (back = 0; back < col1; ++back)
2487 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2488 || ScreenLines[off - 1 - back] == ' ')
2489 break;
2490 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2491 && ScreenLines[off - 1] != ' ');
2492
2493 /* Break it up in strings of characters with the same attributes. */
2494 /* Print UTF-8 characters individually. */
2495 while (len > 0)
2496 {
2497 first_attr = ScreenAttrs[off];
2498 gui.highlight_mask = first_attr;
2499#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2500 if (enc_utf8 && ScreenLinesUC[off] != 0)
2501 {
2502 /* output multi-byte character separately */
2503 nback = gui_screenchar(off, flags,
2504 (guicolor_T)0, (guicolor_T)0, back);
2505 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2506 idx = 2;
2507 else
2508 idx = 1;
2509 }
2510 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2511 {
2512 /* output double-byte, single-width character separately */
2513 nback = gui_screenchar(off, flags,
2514 (guicolor_T)0, (guicolor_T)0, back);
2515 idx = 1;
2516 }
2517 else
2518#endif
2519 {
2520#ifdef HAVE_GTK2
2521 for (idx = 0; idx < len; ++idx)
2522 {
2523 if (enc_utf8 && ScreenLines[off + idx] == 0)
2524 continue; /* skip second half of double-width char */
2525 if (ScreenAttrs[off + idx] != first_attr)
2526 break;
2527 }
2528 /* gui_screenstr() takes care of multibyte chars */
2529 nback = gui_screenstr(off, idx, flags,
2530 (guicolor_T)0, (guicolor_T)0, back);
2531#else
2532 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2533 idx++)
2534 {
2535# ifdef FEAT_MBYTE
2536 /* Stop at a multi-byte Unicode character. */
2537 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2538 break;
2539 if (enc_dbcs == DBCS_JPNU)
2540 {
2541 /* Stop at a double-byte single-width char. */
2542 if (ScreenLines[off + idx] == 0x8e)
2543 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002544 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 + off + idx) == 2)
2546 ++idx; /* skip second byte of double-byte char */
2547 }
2548# endif
2549 }
2550 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2551 (guicolor_T)0, (guicolor_T)0, back);
2552#endif
2553 }
2554 if (nback == FAIL)
2555 {
2556 /* Must back up to start drawing where a bold or italic word
2557 * starts. */
2558 off -= back;
2559 len += back;
2560 gui.col -= back;
2561 }
2562 else
2563 {
2564 off += idx;
2565 len -= idx;
2566 }
2567 back = 0;
2568 }
2569 }
2570
2571 /* Put the cursor back where it was */
2572 gui.row = old_row;
2573 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002574 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 return retval;
2577}
2578
2579 static void
2580gui_delete_lines(row, count)
2581 int row;
2582 int count;
2583{
2584 if (count <= 0)
2585 return;
2586
2587 if (row + count > gui.scroll_region_bot)
2588 /* Scrolled out of region, just blank the lines out */
2589 gui_clear_block(row, gui.scroll_region_left,
2590 gui.scroll_region_bot, gui.scroll_region_right);
2591 else
2592 {
2593 gui_mch_delete_lines(row, count);
2594
2595 /* If the cursor was in the deleted lines it's now gone. If the
2596 * cursor was in the scrolled lines adjust its position. */
2597 if (gui.cursor_row >= row
2598 && gui.cursor_col >= gui.scroll_region_left
2599 && gui.cursor_col <= gui.scroll_region_right)
2600 {
2601 if (gui.cursor_row < row + count)
2602 gui.cursor_is_valid = FALSE;
2603 else if (gui.cursor_row <= gui.scroll_region_bot)
2604 gui.cursor_row -= count;
2605 }
2606 }
2607}
2608
2609 static void
2610gui_insert_lines(row, count)
2611 int row;
2612 int count;
2613{
2614 if (count <= 0)
2615 return;
2616
2617 if (row + count > gui.scroll_region_bot)
2618 /* Scrolled out of region, just blank the lines out */
2619 gui_clear_block(row, gui.scroll_region_left,
2620 gui.scroll_region_bot, gui.scroll_region_right);
2621 else
2622 {
2623 gui_mch_insert_lines(row, count);
2624
2625 if (gui.cursor_row >= gui.row
2626 && gui.cursor_col >= gui.scroll_region_left
2627 && gui.cursor_col <= gui.scroll_region_right)
2628 {
2629 if (gui.cursor_row <= gui.scroll_region_bot - count)
2630 gui.cursor_row += count;
2631 else if (gui.cursor_row <= gui.scroll_region_bot)
2632 gui.cursor_is_valid = FALSE;
2633 }
2634 }
2635}
2636
2637/*
2638 * The main GUI input routine. Waits for a character from the keyboard.
2639 * wtime == -1 Wait forever.
2640 * wtime == 0 Don't wait.
2641 * wtime > 0 Wait wtime milliseconds for a character.
2642 * Returns OK if a character was found to be available within the given time,
2643 * or FAIL otherwise.
2644 */
2645 int
2646gui_wait_for_chars(wtime)
2647 long wtime;
2648{
2649 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /*
2652 * If we're going to wait a bit, update the menus and mouse shape for the
2653 * current State.
2654 */
2655 if (wtime != 0)
2656 {
2657#ifdef FEAT_MENU
2658 gui_update_menus(0);
2659#endif
2660 }
2661
2662 gui_mch_update();
2663 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 /* Before waiting, flush any output to the screen. */
2669 gui_mch_flush();
2670
2671 if (wtime > 0)
2672 {
2673 /* Blink when waiting for a character. Probably only does something
2674 * for showmatch() */
2675 gui_mch_start_blink();
2676 retval = gui_mch_wait_for_chars(wtime);
2677 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return retval;
2679 }
2680
2681 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002682 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 */
2684 gui_mch_start_blink();
2685
Bram Moolenaar3918c952005-03-15 22:34:55 +00002686 retval = FAIL;
2687 /*
2688 * We may want to trigger the CursorHold event. First wait for
2689 * 'updatetime' and if nothing is typed within that time put the
2690 * K_CURSORHOLD key in the input buffer.
2691 */
2692 if (gui_mch_wait_for_chars(p_ut) == OK)
2693 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002695 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002697 char_u buf[3];
2698
2699 /* Put K_CURSORHOLD in the input buffer. */
2700 buf[0] = CSI;
2701 buf[1] = KS_EXTRA;
2702 buf[2] = (int)KE_CURSORHOLD;
2703 add_to_input_buf(buf, 3);
2704
2705 retval = OK;
2706 }
2707#endif
2708
2709 if (retval == FAIL)
2710 {
2711 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002712 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 gui_mch_stop_blink();
2717 return retval;
2718}
2719
2720/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002721 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 */
2723 static void
2724fill_mouse_coord(p, col, row)
2725 char_u *p;
2726 int col;
2727 int row;
2728{
2729 p[0] = (char_u)(col / 128 + ' ' + 1);
2730 p[1] = (char_u)(col % 128 + ' ' + 1);
2731 p[2] = (char_u)(row / 128 + ' ' + 1);
2732 p[3] = (char_u)(row % 128 + ' ' + 1);
2733}
2734
2735/*
2736 * Generic mouse support function. Add a mouse event to the input buffer with
2737 * the given properties.
2738 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2739 * MOUSE_X1, MOUSE_X2
2740 * MOUSE_DRAG, or MOUSE_RELEASE.
2741 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2742 * x, y --- Coordinates of mouse in pixels.
2743 * repeated_click --- TRUE if this click comes only a short time after a
2744 * previous click.
2745 * modifiers --- Bit field which may be any of the following modifiers
2746 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2747 * This function will ignore drag events where the mouse has not moved to a new
2748 * character.
2749 */
2750 void
2751gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2752 int button;
2753 int x;
2754 int y;
2755 int repeated_click;
2756 int_u modifiers;
2757{
2758 static int prev_row = 0, prev_col = 0;
2759 static int prev_button = -1;
2760 static int num_clicks = 1;
2761 char_u string[10];
2762 enum key_extra button_char;
2763 int row, col;
2764#ifdef FEAT_CLIPBOARD
2765 int checkfor;
2766 int did_clip = FALSE;
2767#endif
2768
2769 /*
2770 * Scrolling may happen at any time, also while a selection is present.
2771 */
2772 switch (button)
2773 {
2774 case MOUSE_X1:
2775 button_char = KE_X1MOUSE;
2776 goto button_set;
2777 case MOUSE_X2:
2778 button_char = KE_X2MOUSE;
2779 goto button_set;
2780 case MOUSE_4:
2781 button_char = KE_MOUSEDOWN;
2782 goto button_set;
2783 case MOUSE_5:
2784 button_char = KE_MOUSEUP;
2785button_set:
2786 {
2787 /* Don't put events in the input queue now. */
2788 if (hold_gui_events)
2789 return;
2790
2791 string[3] = CSI;
2792 string[4] = KS_EXTRA;
2793 string[5] = (int)button_char;
2794
2795 /* Pass the pointer coordinates of the scroll event so that we
2796 * know which window to scroll. */
2797 row = gui_xy2colrow(x, y, &col);
2798 string[6] = (char_u)(col / 128 + ' ' + 1);
2799 string[7] = (char_u)(col % 128 + ' ' + 1);
2800 string[8] = (char_u)(row / 128 + ' ' + 1);
2801 string[9] = (char_u)(row % 128 + ' ' + 1);
2802
2803 if (modifiers == 0)
2804 add_to_input_buf(string + 3, 7);
2805 else
2806 {
2807 string[0] = CSI;
2808 string[1] = KS_MODIFIER;
2809 string[2] = 0;
2810 if (modifiers & MOUSE_SHIFT)
2811 string[2] |= MOD_MASK_SHIFT;
2812 if (modifiers & MOUSE_CTRL)
2813 string[2] |= MOD_MASK_CTRL;
2814 if (modifiers & MOUSE_ALT)
2815 string[2] |= MOD_MASK_ALT;
2816 add_to_input_buf(string, 10);
2817 }
2818 return;
2819 }
2820 }
2821
2822#ifdef FEAT_CLIPBOARD
2823 /* If a clipboard selection is in progress, handle it */
2824 if (clip_star.state == SELECT_IN_PROGRESS)
2825 {
2826 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2827 return;
2828 }
2829
2830 /* Determine which mouse settings to look for based on the current mode */
2831 switch (get_real_state())
2832 {
2833 case NORMAL_BUSY:
2834 case OP_PENDING:
2835 case NORMAL: checkfor = MOUSE_NORMAL; break;
2836 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002837 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case REPLACE:
2839 case REPLACE+LANGMAP:
2840#ifdef FEAT_VREPLACE
2841 case VREPLACE:
2842 case VREPLACE+LANGMAP:
2843#endif
2844 case INSERT:
2845 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2846 case ASKMORE:
2847 case HITRETURN: /* At the more- and hit-enter prompt pass the
2848 mouse event for a click on or below the
2849 message line. */
2850 if (Y_2_ROW(y) >= msg_row)
2851 checkfor = MOUSE_NORMAL;
2852 else
2853 checkfor = MOUSE_RETURN;
2854 break;
2855
2856 /*
2857 * On the command line, use the clipboard selection on all lines
2858 * but the command line. But not when pasting.
2859 */
2860 case CMDLINE:
2861 case CMDLINE+LANGMAP:
2862 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2863 checkfor = MOUSE_NONE;
2864 else
2865 checkfor = MOUSE_COMMAND;
2866 break;
2867
2868 default:
2869 checkfor = MOUSE_NONE;
2870 break;
2871 };
2872
2873 /*
2874 * Allow clipboard selection of text on the command line in "normal"
2875 * modes. Don't do this when dragging the status line, or extending a
2876 * Visual selection.
2877 */
2878 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2879 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00002880# ifdef FEAT_WINDOWS
2881 + firstwin->w_winrow
2882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 && button != MOUSE_DRAG
2884# ifdef FEAT_MOUSESHAPE
2885 && !drag_status_line
2886# ifdef FEAT_VERTSPLIT
2887 && !drag_sep_line
2888# endif
2889# endif
2890 )
2891 checkfor = MOUSE_NONE;
2892
2893 /*
2894 * Use modeless selection when holding CTRL and SHIFT pressed.
2895 */
2896 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2897 checkfor = MOUSE_NONEF;
2898
2899 /*
2900 * In Ex mode, always use modeless selection.
2901 */
2902 if (exmode_active)
2903 checkfor = MOUSE_NONE;
2904
2905 /*
2906 * If the mouse settings say to not use the mouse, use the modeless
2907 * selection. But if Visual is active, assume that only the Visual area
2908 * will be selected.
2909 * Exception: On the command line, both the selection is used and a mouse
2910 * key is send.
2911 */
2912 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2913 {
2914#ifdef FEAT_VISUAL
2915 /* Don't do modeless selection in Visual mode. */
2916 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2917 return;
2918#endif
2919
2920 /*
2921 * When 'mousemodel' is "popup", shift-left is translated to right.
2922 * But not when also using Ctrl.
2923 */
2924 if (mouse_model_popup() && button == MOUSE_LEFT
2925 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2926 {
2927 button = MOUSE_RIGHT;
2928 modifiers &= ~ MOUSE_SHIFT;
2929 }
2930
2931 /* If the selection is done, allow the right button to extend it.
2932 * If the selection is cleared, allow the right button to start it
2933 * from the cursor position. */
2934 if (button == MOUSE_RIGHT)
2935 {
2936 if (clip_star.state == SELECT_CLEARED)
2937 {
2938 if (State & CMDLINE)
2939 {
2940 col = msg_col;
2941 row = msg_row;
2942 }
2943 else
2944 {
2945 col = curwin->w_wcol;
2946 row = curwin->w_wrow + W_WINROW(curwin);
2947 }
2948 clip_start_selection(col, row, FALSE);
2949 }
2950 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2951 repeated_click);
2952 did_clip = TRUE;
2953 }
2954 /* Allow the left button to start the selection */
2955 else if (button ==
2956# ifdef RISCOS
2957 /* Only start a drag on a drag event. Otherwise
2958 * we don't get a release event. */
2959 MOUSE_DRAG
2960# else
2961 MOUSE_LEFT
2962# endif
2963 )
2964 {
2965 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2966 did_clip = TRUE;
2967 }
2968# ifdef RISCOS
2969 else if (button == MOUSE_LEFT)
2970 {
2971 clip_clear_selection();
2972 did_clip = TRUE;
2973 }
2974# endif
2975
2976 /* Always allow pasting */
2977 if (button != MOUSE_MIDDLE)
2978 {
2979 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2980 return;
2981 if (checkfor != MOUSE_COMMAND)
2982 button = MOUSE_LEFT;
2983 }
2984 repeated_click = FALSE;
2985 }
2986
2987 if (clip_star.state != SELECT_CLEARED && !did_clip)
2988 clip_clear_selection();
2989#endif
2990
2991 /* Don't put events in the input queue now. */
2992 if (hold_gui_events)
2993 return;
2994
2995 row = gui_xy2colrow(x, y, &col);
2996
2997 /*
2998 * If we are dragging and the mouse hasn't moved far enough to be on a
2999 * different character, then don't send an event to vim.
3000 */
3001 if (button == MOUSE_DRAG)
3002 {
3003 if (row == prev_row && col == prev_col)
3004 return;
3005 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3006 if (y < 0)
3007 row = -1;
3008 }
3009
3010 /*
3011 * If topline has changed (window scrolled) since the last click, reset
3012 * repeated_click, because we don't want starting Visual mode when
3013 * clicking on a different character in the text.
3014 */
3015 if (curwin->w_topline != gui_prev_topline
3016#ifdef FEAT_DIFF
3017 || curwin->w_topfill != gui_prev_topfill
3018#endif
3019 )
3020 repeated_click = FALSE;
3021
3022 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3023 string[1] = KS_MOUSE;
3024 string[2] = KE_FILLER;
3025 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3026 {
3027 if (repeated_click)
3028 {
3029 /*
3030 * Handle multiple clicks. They only count if the mouse is still
3031 * pointing at the same character.
3032 */
3033 if (button != prev_button || row != prev_row || col != prev_col)
3034 num_clicks = 1;
3035 else if (++num_clicks > 4)
3036 num_clicks = 1;
3037 }
3038 else
3039 num_clicks = 1;
3040 prev_button = button;
3041 gui_prev_topline = curwin->w_topline;
3042#ifdef FEAT_DIFF
3043 gui_prev_topfill = curwin->w_topfill;
3044#endif
3045
3046 string[3] = (char_u)(button | 0x20);
3047 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3048 }
3049 else
3050 string[3] = (char_u)button;
3051
3052 string[3] |= modifiers;
3053 fill_mouse_coord(string + 4, col, row);
3054 add_to_input_buf(string, 8);
3055
3056 if (row < 0)
3057 prev_row = 0;
3058 else
3059 prev_row = row;
3060 prev_col = col;
3061
3062 /*
3063 * We need to make sure this is cleared since Athena doesn't tell us when
3064 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3065 */
3066#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3067 gui.dragged_sb = SBAR_NONE;
3068#endif
3069}
3070
3071/*
3072 * Convert x and y coordinate to column and row in text window.
3073 * Corrects for multi-byte character.
3074 * returns column in "*colp" and row as return value;
3075 */
3076 int
3077gui_xy2colrow(x, y, colp)
3078 int x;
3079 int y;
3080 int *colp;
3081{
3082 int col = check_col(X_2_COL(x));
3083 int row = check_row(Y_2_ROW(y));
3084
3085#ifdef FEAT_MBYTE
3086 *colp = mb_fix_col(col, row);
3087#else
3088 *colp = col;
3089#endif
3090 return row;
3091}
3092
3093#if defined(FEAT_MENU) || defined(PROTO)
3094/*
3095 * Callback function for when a menu entry has been selected.
3096 */
3097 void
3098gui_menu_cb(menu)
3099 vimmenu_T *menu;
3100{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003101 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
3103 /* Don't put events in the input queue now. */
3104 if (hold_gui_events)
3105 return;
3106
3107 bytes[0] = CSI;
3108 bytes[1] = KS_MENU;
3109 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003110 add_to_input_buf(bytes, 3);
3111 add_long_to_buf((long_u)menu, bytes);
3112 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113}
3114#endif
3115
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003116static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118/*
3119 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003120 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * in p_go.
3122 */
3123/*ARGSUSED*/
3124 void
3125gui_init_which_components(oldval)
3126 char_u *oldval;
3127{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128#ifdef FEAT_MENU
3129 static int prev_menu_is_active = -1;
3130#endif
3131#ifdef FEAT_TOOLBAR
3132 static int prev_toolbar = -1;
3133 int using_toolbar = FALSE;
3134#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003135#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003136 int using_tabline;
3137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#ifdef FEAT_FOOTER
3139 static int prev_footer = -1;
3140 int using_footer = FALSE;
3141#endif
3142#if defined(FEAT_MENU) && !defined(WIN16)
3143 static int prev_tearoff = -1;
3144 int using_tearoff = FALSE;
3145#endif
3146
3147 char_u *p;
3148 int i;
3149#ifdef FEAT_MENU
3150 int grey_old, grey_new;
3151 char_u *temp;
3152#endif
3153 win_T *wp;
3154 int need_set_size;
3155 int fix_size;
3156
3157#ifdef FEAT_MENU
3158 if (oldval != NULL && gui.in_use)
3159 {
3160 /*
3161 * Check if the menu's go from grey to non-grey or vise versa.
3162 */
3163 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3164 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3165 if (grey_old != grey_new)
3166 {
3167 temp = p_go;
3168 p_go = oldval;
3169 gui_update_menus(MENU_ALL_MODES);
3170 p_go = temp;
3171 }
3172 }
3173 gui.menu_is_active = FALSE;
3174#endif
3175
3176 for (i = 0; i < 3; i++)
3177 gui.which_scrollbars[i] = FALSE;
3178 for (p = p_go; *p; p++)
3179 switch (*p)
3180 {
3181 case GO_LEFT:
3182 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3183 break;
3184 case GO_RIGHT:
3185 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3186 break;
3187#ifdef FEAT_VERTSPLIT
3188 case GO_VLEFT:
3189 if (win_hasvertsplit())
3190 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3191 break;
3192 case GO_VRIGHT:
3193 if (win_hasvertsplit())
3194 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3195 break;
3196#endif
3197 case GO_BOT:
3198 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3199 break;
3200#ifdef FEAT_MENU
3201 case GO_MENUS:
3202 gui.menu_is_active = TRUE;
3203 break;
3204#endif
3205 case GO_GREY:
3206 /* make menu's have grey items, ignored here */
3207 break;
3208#ifdef FEAT_TOOLBAR
3209 case GO_TOOLBAR:
3210 using_toolbar = TRUE;
3211 break;
3212#endif
3213#ifdef FEAT_FOOTER
3214 case GO_FOOTER:
3215 using_footer = TRUE;
3216 break;
3217#endif
3218 case GO_TEAROFF:
3219#if defined(FEAT_MENU) && !defined(WIN16)
3220 using_tearoff = TRUE;
3221#endif
3222 break;
3223 default:
3224 /* Ignore options that are not supported */
3225 break;
3226 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (gui.in_use)
3229 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003230 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003232
3233#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003234 /* Update the GUI tab line, it may appear or disappear. This may
3235 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003236 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003237 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003238 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003239 /* We don't want a resize event change "Rows" here, save and
3240 * restore it. Resizing is handled below. */
3241 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003242 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003243 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003244 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003245 if (using_tabline)
3246 fix_size = TRUE;
3247 if (!gui_use_tabline())
3248 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3249 }
3250#endif
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 for (i = 0; i < 3; i++)
3253 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003254 /* The scrollbar needs to be updated when it is shown/unshown and
3255 * when switching tab pages. But the size only changes when it's
3256 * shown/unshown. Thus we need two places to remember whether a
3257 * scrollbar is there or not. */
3258 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003259#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003260 || gui.which_scrollbars[i]
3261 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003262#endif
3263 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 if (i == SBAR_BOTTOM)
3266 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3267 gui.which_scrollbars[i]);
3268 else
3269 {
3270 FOR_ALL_WINDOWS(wp)
3271 {
3272 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3273 }
3274 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003275 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3276 {
3277 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003278 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003279 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003280 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003281 if (gui.which_scrollbars[i])
3282 fix_size = TRUE;
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003285#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003286 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003287#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003288 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290
3291#ifdef FEAT_MENU
3292 if (gui.menu_is_active != prev_menu_is_active)
3293 {
3294 /* We don't want a resize event change "Rows" here, save and
3295 * restore it. Resizing is handled below. */
3296 i = Rows;
3297 gui_mch_enable_menu(gui.menu_is_active);
3298 Rows = i;
3299 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003300 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 if (gui.menu_is_active)
3302 fix_size = TRUE;
3303 }
3304#endif
3305
3306#ifdef FEAT_TOOLBAR
3307 if (using_toolbar != prev_toolbar)
3308 {
3309 gui_mch_show_toolbar(using_toolbar);
3310 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003311 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (using_toolbar)
3313 fix_size = TRUE;
3314 }
3315#endif
3316#ifdef FEAT_FOOTER
3317 if (using_footer != prev_footer)
3318 {
3319 gui_mch_enable_footer(using_footer);
3320 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003321 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (using_footer)
3323 fix_size = TRUE;
3324 }
3325#endif
3326#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3327 if (using_tearoff != prev_tearoff)
3328 {
3329 gui_mch_toggle_tearoffs(using_tearoff);
3330 prev_tearoff = using_tearoff;
3331 }
3332#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003333 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003334 {
3335#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003336 long prev_Columns = Columns;
3337 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 /* Adjust the size of the window to make the text area keep the
3340 * same size and to avoid that part of our window is off-screen
3341 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003342 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003343
3344#ifdef FEAT_GUI_GTK
3345 /* GTK has the annoying habit of sending us resize events when
3346 * changing the window size ourselves. This mostly happens when
3347 * waiting for a character to arrive, quite unpredictably, and may
3348 * change Columns and Rows when we don't want it. Wait for a
3349 * character here to avoid this effect.
3350 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003351 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003352 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003353 * Don't change Rows when adding menu/toolbar/tabline.
3354 * Don't change Columns when adding vertical toolbar. */
3355 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003356 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003357 if ((need_set_size & RESIZE_VERT) == 0)
3358 Rows = prev_Rows;
3359 if ((need_set_size & RESIZE_HOR) == 0)
3360 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003361#endif
3362 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003363#ifdef FEAT_WINDOWS
3364 /* When the console tabline appears or disappears the window positions
3365 * change. */
3366 if (firstwin->w_winrow != tabline_height())
3367 shell_new_rows(); /* recompute window positions and heights */
3368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370}
3371
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003372#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3373/*
3374 * Return TRUE if the GUI is taking care of the tabline.
3375 * It may still be hidden if 'showtabline' is zero.
3376 */
3377 int
3378gui_use_tabline()
3379{
3380 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3381}
3382
3383/*
3384 * Return TRUE if the GUI is showing the tabline.
3385 * This uses 'showtabline'.
3386 */
3387 static int
3388gui_has_tabline()
3389{
3390 if (!gui_use_tabline()
3391 || p_stal == 0
3392 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3393 return FALSE;
3394 return TRUE;
3395}
3396
3397/*
3398 * Update the tabline.
3399 * This may display/undisplay the tabline and update the labels.
3400 */
3401 void
3402gui_update_tabline()
3403{
3404 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003405 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003406
3407 if (!gui.starting && starting == 0)
3408 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003409 /* Updating the tabline uses direct GUI commands, flush
3410 * outstanding instructions first. (esp. clear screen) */
3411 out_flush();
3412 gui_mch_flush();
3413
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003414 if (!showit != !shown)
3415 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003416 if (showit != 0)
3417 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003418
3419 /* When the tabs change from hidden to shown or from shown to
3420 * hidden the size of the text area should remain the same. */
3421 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003422 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003423 }
3424}
3425
3426/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003427 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003428 */
3429 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003430get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003431 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003432 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003433{
3434 int modified = FALSE;
3435 char_u buf[40];
3436 int wincount;
3437 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003438 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003439
Bram Moolenaar57657d82006-04-21 22:12:41 +00003440 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003441 opt = (tooltip ? &p_gtt : &p_gtl);
3442 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003443 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003444 int use_sandbox = FALSE;
3445 int save_called_emsg = called_emsg;
3446 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003447 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003448 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3449 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003450
3451 called_emsg = FALSE;
3452
3453 printer_page_num = tabpage_index(tp);
3454# ifdef FEAT_EVAL
3455 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003456 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003457# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003458 /* It's almost as going to the tabpage, but without autocommands. */
3459 curtab->tp_firstwin = firstwin;
3460 curtab->tp_lastwin = lastwin;
3461 curtab->tp_curwin = curwin;
3462 save_curtab = curtab;
3463 curtab = tp;
3464 topframe = curtab->tp_topframe;
3465 firstwin = curtab->tp_firstwin;
3466 lastwin = curtab->tp_lastwin;
3467 curwin = curtab->tp_curwin;
3468 curbuf = curwin->w_buffer;
3469
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003470 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003471 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003472 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003473 STRCPY(NameBuff, res);
3474
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003475 /* Back to the original curtab. */
3476 curtab = save_curtab;
3477 topframe = curtab->tp_topframe;
3478 firstwin = curtab->tp_firstwin;
3479 lastwin = curtab->tp_lastwin;
3480 curwin = curtab->tp_curwin;
3481 curbuf = curwin->w_buffer;
3482
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003483 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003484 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003485 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003486 called_emsg |= save_called_emsg;
3487 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003488
3489 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3490 * use a default label. */
3491 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003492 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003493 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003494 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003495 if (!tooltip)
3496 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003497
3498 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3499 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3500 if (bufIsChanged(wp->w_buffer))
3501 modified = TRUE;
3502 if (modified || wincount > 1)
3503 {
3504 if (wincount > 1)
3505 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3506 else
3507 buf[0] = NUL;
3508 if (modified)
3509 STRCAT(buf, "+");
3510 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003511 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003512 mch_memmove(NameBuff, buf, STRLEN(buf));
3513 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003514 }
3515}
3516
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003517/*
3518 * Send the event for clicking to select tab page "nr".
3519 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003520 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003521 */
3522 int
3523send_tabline_event(nr)
3524 int nr;
3525{
3526 char_u string[3];
3527
3528 if (nr == tabpage_index(curtab))
3529 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003530
3531 /* Don't put events in the input queue now. */
3532 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003533# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003534 || cmdwin_type != 0
3535# endif
3536 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003537 {
3538 /* Set it back to the current tab page. */
3539 gui_mch_set_curtab(tabpage_index(curtab));
3540 return FALSE;
3541 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003542
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003543 string[0] = CSI;
3544 string[1] = KS_TABLINE;
3545 string[2] = KE_FILLER;
3546 add_to_input_buf(string, 3);
3547 string[0] = nr;
3548 add_to_input_buf_csi(string, 1);
3549 return TRUE;
3550}
3551
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003552/*
3553 * Send a tabline menu event
3554 */
3555 void
3556send_tabline_menu_event(tabidx, event)
3557 int tabidx;
3558 int event;
3559{
3560 char_u string[3];
3561
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003562 /* Don't put events in the input queue now. */
3563 if (hold_gui_events)
3564 return;
3565
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003566 string[0] = CSI;
3567 string[1] = KS_TABMENU;
3568 string[2] = KE_FILLER;
3569 add_to_input_buf(string, 3);
3570 string[0] = tabidx;
3571 string[1] = (char_u)(long)event;
3572 add_to_input_buf_csi(string, 2);
3573}
3574
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577/*
3578 * Scrollbar stuff:
3579 */
3580
Bram Moolenaare45828b2006-02-15 22:12:56 +00003581#if defined(FEAT_WINDOWS) || defined(PROTO)
3582/*
3583 * Remove all scrollbars. Used before switching to another tab page.
3584 */
3585 void
3586gui_remove_scrollbars()
3587{
3588 int i;
3589 win_T *wp;
3590
3591 for (i = 0; i < 3; i++)
3592 {
3593 if (i == SBAR_BOTTOM)
3594 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3595 else
3596 {
3597 FOR_ALL_WINDOWS(wp)
3598 {
3599 gui_do_scrollbar(wp, i, FALSE);
3600 }
3601 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003602 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003603 }
3604}
3605#endif
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 void
3608gui_create_scrollbar(sb, type, wp)
3609 scrollbar_T *sb;
3610 int type;
3611 win_T *wp;
3612{
3613 static int sbar_ident = 0;
3614
3615 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3616 sb->wp = wp;
3617 sb->type = type;
3618 sb->value = 0;
3619#ifdef FEAT_GUI_ATHENA
3620 sb->pixval = 0;
3621#endif
3622 sb->size = 1;
3623 sb->max = 1;
3624 sb->top = 0;
3625 sb->height = 0;
3626#ifdef FEAT_VERTSPLIT
3627 sb->width = 0;
3628#endif
3629 sb->status_height = 0;
3630 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3631}
3632
3633/*
3634 * Find the scrollbar with the given index.
3635 */
3636 scrollbar_T *
3637gui_find_scrollbar(ident)
3638 long ident;
3639{
3640 win_T *wp;
3641
3642 if (gui.bottom_sbar.ident == ident)
3643 return &gui.bottom_sbar;
3644 FOR_ALL_WINDOWS(wp)
3645 {
3646 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3647 return &wp->w_scrollbars[SBAR_LEFT];
3648 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3649 return &wp->w_scrollbars[SBAR_RIGHT];
3650 }
3651 return NULL;
3652}
3653
3654/*
3655 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3656 *
3657 * For Win32, Macintosh and GTK+ 2:
3658 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3659 * you stop dragging the scrollbar. We get here each time the scrollbar is
3660 * dragged another pixel, but as far as the rest of vim goes, it thinks
3661 * we're just hanging in the call to DispatchMessage() in
3662 * process_message(). The DispatchMessage() call that hangs was passed a
3663 * mouse button click event in the scrollbar window. -- webb.
3664 *
3665 * Solution: Do the scrolling right here. But only when allowed.
3666 * Ignore the scrollbars while executing an external command or when there
3667 * are still characters to be processed.
3668 */
3669 void
3670gui_drag_scrollbar(sb, value, still_dragging)
3671 scrollbar_T *sb;
3672 long value;
3673 int still_dragging;
3674{
3675#ifdef FEAT_WINDOWS
3676 win_T *wp;
3677#endif
3678 int sb_num;
3679#ifdef USE_ON_FLY_SCROLL
3680 colnr_T old_leftcol = curwin->w_leftcol;
3681# ifdef FEAT_SCROLLBIND
3682 linenr_T old_topline = curwin->w_topline;
3683# endif
3684# ifdef FEAT_DIFF
3685 int old_topfill = curwin->w_topfill;
3686# endif
3687#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003688 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 int byte_count;
3690#endif
3691
3692 if (sb == NULL)
3693 return;
3694
3695 /* Don't put events in the input queue now. */
3696 if (hold_gui_events)
3697 return;
3698
3699#ifdef FEAT_CMDWIN
3700 if (cmdwin_type != 0 && sb->wp != curwin)
3701 return;
3702#endif
3703
3704 if (still_dragging)
3705 {
3706 if (sb->wp == NULL)
3707 gui.dragged_sb = SBAR_BOTTOM;
3708 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3709 gui.dragged_sb = SBAR_LEFT;
3710 else
3711 gui.dragged_sb = SBAR_RIGHT;
3712 gui.dragged_wp = sb->wp;
3713 }
3714 else
3715 {
3716 gui.dragged_sb = SBAR_NONE;
3717#ifdef HAVE_GTK2
3718 /* Keep the "dragged_wp" value until after the scrolling, for when the
3719 * moust button is released. GTK2 doesn't send the button-up event. */
3720 gui.dragged_wp = NULL;
3721#endif
3722 }
3723
3724 /* Vertical sbar info is kept in the first sbar (the left one) */
3725 if (sb->wp != NULL)
3726 sb = &sb->wp->w_scrollbars[0];
3727
3728 /*
3729 * Check validity of value
3730 */
3731 if (value < 0)
3732 value = 0;
3733#ifdef SCROLL_PAST_END
3734 else if (value > sb->max)
3735 value = sb->max;
3736#else
3737 if (value > sb->max - sb->size + 1)
3738 value = sb->max - sb->size + 1;
3739#endif
3740
3741 sb->value = value;
3742
3743#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003744 /* When not allowed to do the scrolling right now, return.
3745 * This also checked input_available(), but that causes the first click in
3746 * a scrollbar to be ignored when Vim doesn't have focus. */
3747 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 return;
3749#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003750#ifdef FEAT_INS_EXPAND
3751 /* Disallow scrolling the current window when the completion popup menu is
3752 * visible. */
3753 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3754 return;
3755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
3757#ifdef FEAT_RIGHTLEFT
3758 if (sb->wp == NULL && curwin->w_p_rl)
3759 {
3760 value = sb->max + 1 - sb->size - value;
3761 if (value < 0)
3762 value = 0;
3763 }
3764#endif
3765
3766 if (sb->wp != NULL) /* vertical scrollbar */
3767 {
3768 sb_num = 0;
3769#ifdef FEAT_WINDOWS
3770 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3771 sb_num++;
3772 if (wp == NULL)
3773 return;
3774#else
3775 if (sb->wp != curwin)
3776 return;
3777#endif
3778
3779#ifdef USE_ON_FLY_SCROLL
3780 current_scrollbar = sb_num;
3781 scrollbar_value = value;
3782 if (State & NORMAL)
3783 {
3784 gui_do_scroll();
3785 setcursor();
3786 }
3787 else if (State & INSERT)
3788 {
3789 ins_scroll();
3790 setcursor();
3791 }
3792 else if (State & CMDLINE)
3793 {
3794 if (msg_scrolled == 0)
3795 {
3796 gui_do_scroll();
3797 redrawcmdline();
3798 }
3799 }
3800# ifdef FEAT_FOLDING
3801 /* Value may have been changed for closed fold. */
3802 sb->value = sb->wp->w_topline - 1;
3803# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003804
3805 /* When dragging one scrollbar and there is another one at the other
3806 * side move the thumb of that one too. */
3807 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3808 gui_mch_set_scrollbar_thumb(
3809 &sb->wp->w_scrollbars[
3810 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3811 ? SBAR_LEFT : SBAR_RIGHT],
3812 sb->value, sb->size, sb->max);
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814#else
3815 bytes[0] = CSI;
3816 bytes[1] = KS_VER_SCROLLBAR;
3817 bytes[2] = KE_FILLER;
3818 bytes[3] = (char_u)sb_num;
3819 byte_count = 4;
3820#endif
3821 }
3822 else
3823 {
3824#ifdef USE_ON_FLY_SCROLL
3825 scrollbar_value = value;
3826
3827 if (State & NORMAL)
3828 gui_do_horiz_scroll();
3829 else if (State & INSERT)
3830 ins_horscroll();
3831 else if (State & CMDLINE)
3832 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003833 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
3835 gui_do_horiz_scroll();
3836 redrawcmdline();
3837 }
3838 }
3839 if (old_leftcol != curwin->w_leftcol)
3840 {
3841 updateWindow(curwin); /* update window, status and cmdline */
3842 setcursor();
3843 }
3844#else
3845 bytes[0] = CSI;
3846 bytes[1] = KS_HOR_SCROLLBAR;
3847 bytes[2] = KE_FILLER;
3848 byte_count = 3;
3849#endif
3850 }
3851
3852#ifdef USE_ON_FLY_SCROLL
3853# ifdef FEAT_SCROLLBIND
3854 /*
3855 * synchronize other windows, as necessary according to 'scrollbind'
3856 */
3857 if (curwin->w_p_scb
3858 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3859 || (sb->wp == curwin && (curwin->w_topline != old_topline
3860# ifdef FEAT_DIFF
3861 || curwin->w_topfill != old_topfill
3862# endif
3863 ))))
3864 {
3865 do_check_scrollbind(TRUE);
3866 /* need to update the window right here */
3867 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3868 if (wp->w_redr_type > 0)
3869 updateWindow(wp);
3870 setcursor();
3871 }
3872# endif
3873 out_flush();
3874 gui_update_cursor(FALSE, TRUE);
3875#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003876 add_to_input_buf(bytes, byte_count);
3877 add_long_to_buf((long_u)value, bytes);
3878 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879#endif
3880}
3881
3882/*
3883 * Scrollbar stuff:
3884 */
3885
3886 void
3887gui_update_scrollbars(force)
3888 int force; /* Force all scrollbars to get updated */
3889{
3890 win_T *wp;
3891 scrollbar_T *sb;
3892 long val, size, max; /* need 32 bits here */
3893 int which_sb;
3894 int h, y;
3895#ifdef FEAT_VERTSPLIT
3896 static win_T *prev_curwin = NULL;
3897#endif
3898
3899 /* Update the horizontal scrollbar */
3900 gui_update_horiz_scrollbar(force);
3901
3902#ifndef WIN3264
3903 /* Return straight away if there is neither a left nor right scrollbar.
3904 * On MS-Windows this is required anyway for scrollwheel messages. */
3905 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3906 return;
3907#endif
3908
3909 /*
3910 * Don't want to update a scrollbar while we're dragging it. But if we
3911 * have both a left and right scrollbar, and we drag one of them, we still
3912 * need to update the other one.
3913 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003914 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3915 && gui.which_scrollbars[SBAR_LEFT]
3916 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
3918 /*
3919 * If we have two scrollbars and one of them is being dragged, just
3920 * copy the scrollbar position from the dragged one to the other one.
3921 */
3922 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3923 if (gui.dragged_wp != NULL)
3924 gui_mch_set_scrollbar_thumb(
3925 &gui.dragged_wp->w_scrollbars[which_sb],
3926 gui.dragged_wp->w_scrollbars[0].value,
3927 gui.dragged_wp->w_scrollbars[0].size,
3928 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930
3931 /* avoid that moving components around generates events */
3932 ++hold_gui_events;
3933
3934 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3935 {
3936 if (wp->w_buffer == NULL) /* just in case */
3937 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003938 /* Skip a scrollbar that is being dragged. */
3939 if (!force && (gui.dragged_sb == SBAR_LEFT
3940 || gui.dragged_sb == SBAR_RIGHT)
3941 && gui.dragged_wp == wp)
3942 continue;
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#ifdef SCROLL_PAST_END
3945 max = wp->w_buffer->b_ml.ml_line_count - 1;
3946#else
3947 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3948#endif
3949 if (max < 0) /* empty buffer */
3950 max = 0;
3951 val = wp->w_topline - 1;
3952 size = wp->w_height;
3953#ifdef SCROLL_PAST_END
3954 if (val > max) /* just in case */
3955 val = max;
3956#else
3957 if (size > max + 1) /* just in case */
3958 size = max + 1;
3959 if (val > max - size + 1)
3960 val = max - size + 1;
3961#endif
3962 if (val < 0) /* minimal value is 0 */
3963 val = 0;
3964
3965 /*
3966 * Scrollbar at index 0 (the left one) contains all the information.
3967 * It would be the same info for left and right so we just store it for
3968 * one of them.
3969 */
3970 sb = &wp->w_scrollbars[0];
3971
3972 /*
3973 * Note: no check for valid w_botline. If it's not valid the
3974 * scrollbars will be updated later anyway.
3975 */
3976 if (size < 1 || wp->w_botline - 2 > max)
3977 {
3978 /*
3979 * This can happen during changing files. Just don't update the
3980 * scrollbar for now.
3981 */
3982 sb->height = 0; /* Force update next time */
3983 if (gui.which_scrollbars[SBAR_LEFT])
3984 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3985 if (gui.which_scrollbars[SBAR_RIGHT])
3986 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3987 continue;
3988 }
3989 if (force || sb->height != wp->w_height
3990#ifdef FEAT_WINDOWS
3991 || sb->top != wp->w_winrow
3992 || sb->status_height != wp->w_status_height
3993# ifdef FEAT_VERTSPLIT
3994 || sb->width != wp->w_width
3995 || prev_curwin != curwin
3996# endif
3997#endif
3998 )
3999 {
4000 /* Height, width or position of scrollbar has changed. For
4001 * vertical split: curwin changed. */
4002 sb->height = wp->w_height;
4003#ifdef FEAT_WINDOWS
4004 sb->top = wp->w_winrow;
4005 sb->status_height = wp->w_status_height;
4006# ifdef FEAT_VERTSPLIT
4007 sb->width = wp->w_width;
4008# endif
4009#endif
4010
4011 /* Calculate height and position in pixels */
4012 h = (sb->height + sb->status_height) * gui.char_height;
4013 y = sb->top * gui.char_height + gui.border_offset;
4014#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4015 if (gui.menu_is_active)
4016 y += gui.menu_height;
4017#endif
4018
4019#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4020 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4021# ifdef FEAT_GUI_ATHENA
4022 y += gui.toolbar_height;
4023# else
4024# ifdef FEAT_GUI_MSWIN
4025 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4026# endif
4027# endif
4028#endif
4029
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004030#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4031 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004032 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004033#endif
4034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035#ifdef FEAT_WINDOWS
4036 if (wp->w_winrow == 0)
4037#endif
4038 {
4039 /* Height of top scrollbar includes width of top border */
4040 h += gui.border_offset;
4041 y -= gui.border_offset;
4042 }
4043 if (gui.which_scrollbars[SBAR_LEFT])
4044 {
4045 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4046 gui.left_sbar_x, y,
4047 gui.scrollbar_width, h);
4048 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4049 }
4050 if (gui.which_scrollbars[SBAR_RIGHT])
4051 {
4052 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4053 gui.right_sbar_x, y,
4054 gui.scrollbar_width, h);
4055 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4056 }
4057 }
4058
4059 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4060 * checking if the thumb moved at least a pixel. Only do this for
4061 * Athena, most other GUIs require the update anyway to make the
4062 * arrows work. */
4063#ifdef FEAT_GUI_ATHENA
4064 if (max == 0)
4065 y = 0;
4066 else
4067 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4068 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4069#else
4070 if (force || sb->value != val || sb->size != size || sb->max != max)
4071#endif
4072 {
4073 /* Thumb of scrollbar has moved */
4074 sb->value = val;
4075#ifdef FEAT_GUI_ATHENA
4076 sb->pixval = y;
4077#endif
4078 sb->size = size;
4079 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004080 if (gui.which_scrollbars[SBAR_LEFT]
4081 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4083 val, size, max);
4084 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004085 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4087 val, size, max);
4088 }
4089 }
4090#ifdef FEAT_VERTSPLIT
4091 prev_curwin = curwin;
4092#endif
4093 --hold_gui_events;
4094}
4095
4096/*
4097 * Enable or disable a scrollbar.
4098 * Check for scrollbars for vertically split windows which are not enabled
4099 * sometimes.
4100 */
4101 static void
4102gui_do_scrollbar(wp, which, enable)
4103 win_T *wp;
4104 int which; /* SBAR_LEFT or SBAR_RIGHT */
4105 int enable; /* TRUE to enable scrollbar */
4106{
4107#ifdef FEAT_VERTSPLIT
4108 int midcol = curwin->w_wincol + curwin->w_width / 2;
4109 int has_midcol = (wp->w_wincol <= midcol
4110 && wp->w_wincol + wp->w_width >= midcol);
4111
4112 /* Only enable scrollbars that contain the middle column of the current
4113 * window. */
4114 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4115 {
4116 /* Scrollbars only on one side. Don't enable scrollbars that don't
4117 * contain the middle column of the current window. */
4118 if (!has_midcol)
4119 enable = FALSE;
4120 }
4121 else
4122 {
4123 /* Scrollbars on both sides. Don't enable scrollbars that neither
4124 * contain the middle column of the current window nor are on the far
4125 * side. */
4126 if (midcol > Columns / 2)
4127 {
4128 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4129 enable = FALSE;
4130 }
4131 else
4132 {
4133 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4134 : !has_midcol)
4135 enable = FALSE;
4136 }
4137 }
4138#endif
4139 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4140}
4141
4142/*
4143 * Scroll a window according to the values set in the globals current_scrollbar
4144 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4145 * or FALSE otherwise.
4146 */
4147 int
4148gui_do_scroll()
4149{
4150 win_T *wp, *save_wp;
4151 int i;
4152 long nlines;
4153 pos_T old_cursor;
4154 linenr_T old_topline;
4155#ifdef FEAT_DIFF
4156 int old_topfill;
4157#endif
4158
4159 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4160 if (wp == NULL)
4161 break;
4162 if (wp == NULL)
4163 /* Couldn't find window */
4164 return FALSE;
4165
4166 /*
4167 * Compute number of lines to scroll. If zero, nothing to do.
4168 */
4169 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4170 if (nlines == 0)
4171 return FALSE;
4172
4173 save_wp = curwin;
4174 old_topline = wp->w_topline;
4175#ifdef FEAT_DIFF
4176 old_topfill = wp->w_topfill;
4177#endif
4178 old_cursor = wp->w_cursor;
4179 curwin = wp;
4180 curbuf = wp->w_buffer;
4181 if (nlines < 0)
4182 scrolldown(-nlines, gui.dragged_wp == NULL);
4183 else
4184 scrollup(nlines, gui.dragged_wp == NULL);
4185 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4186 * the mouse-up event already, but we still want it to behave like when
4187 * dragging. But not the next click in an arrow. */
4188 if (gui.dragged_sb == SBAR_NONE)
4189 gui.dragged_wp = NULL;
4190
4191 if (old_topline != wp->w_topline
4192#ifdef FEAT_DIFF
4193 || old_topfill != wp->w_topfill
4194#endif
4195 )
4196 {
4197 if (p_so != 0)
4198 {
4199 cursor_correct(); /* fix window for 'so' */
4200 update_topline(); /* avoid up/down jump */
4201 }
4202 if (old_cursor.lnum != wp->w_cursor.lnum)
4203 coladvance(wp->w_curswant);
4204#ifdef FEAT_SCROLLBIND
4205 wp->w_scbind_pos = wp->w_topline;
4206#endif
4207 }
4208
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004209 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4210 validate_cursor();
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 curwin = save_wp;
4213 curbuf = save_wp->w_buffer;
4214
4215 /*
4216 * Don't call updateWindow() when nothing has changed (it will overwrite
4217 * the status line!).
4218 */
4219 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004220 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifdef FEAT_DIFF
4222 || old_topfill != wp->w_topfill
4223#endif
4224 )
4225 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004226 int type = VALID;
4227
4228#ifdef FEAT_INS_EXPAND
4229 if (pum_visible())
4230 {
4231 type = NOT_VALID;
4232 wp->w_lines_valid = 0;
4233 }
4234#endif
4235 /* Don't set must_redraw here, it may cause the popup menu to
4236 * disappear when losing focus after a scrollbar drag. */
4237 if (wp->w_redr_type < type)
4238 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 updateWindow(wp); /* update window, status line, and cmdline */
4240 }
4241
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004242#ifdef FEAT_INS_EXPAND
4243 /* May need to redraw the popup menu. */
4244 if (pum_visible())
4245 pum_redraw();
4246#endif
4247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4249}
4250
4251
4252/*
4253 * Horizontal scrollbar stuff:
4254 */
4255
4256/*
4257 * Return length of line "lnum" for horizontal scrolling.
4258 */
4259 static colnr_T
4260scroll_line_len(lnum)
4261 linenr_T lnum;
4262{
4263 char_u *p;
4264 colnr_T col;
4265 int w;
4266
4267 p = ml_get(lnum);
4268 col = 0;
4269 if (*p != NUL)
4270 for (;;)
4271 {
4272 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004273 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (*p == NUL) /* don't count the last character */
4275 break;
4276 col += w;
4277 }
4278 return col;
4279}
4280
4281/* Remember which line is currently the longest, so that we don't have to
4282 * search for it when scrolling horizontally. */
4283static linenr_T longest_lnum = 0;
4284
4285 static void
4286gui_update_horiz_scrollbar(force)
4287 int force;
4288{
4289 long value, size, max; /* need 32 bit ints here */
4290
4291 if (!gui.which_scrollbars[SBAR_BOTTOM])
4292 return;
4293
4294 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4295 return;
4296
4297 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4298 return;
4299
4300 /*
4301 * It is possible for the cursor to be invalid if we're in the middle of
4302 * something (like changing files). If so, don't do anything for now.
4303 */
4304 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4305 {
4306 gui.bottom_sbar.value = -1;
4307 return;
4308 }
4309
4310 size = W_WIDTH(curwin);
4311 if (curwin->w_p_wrap)
4312 {
4313 value = 0;
4314#ifdef SCROLL_PAST_END
4315 max = 0;
4316#else
4317 max = W_WIDTH(curwin) - 1;
4318#endif
4319 }
4320 else
4321 {
4322 value = curwin->w_leftcol;
4323
4324 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4325 * line numbers, topline and botline can be invalid when displaying is
4326 * postponed. */
4327 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4328 && curwin->w_topline <= curwin->w_cursor.lnum
4329 && curwin->w_botline > curwin->w_cursor.lnum
4330 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4331 {
4332 linenr_T lnum;
4333 colnr_T n;
4334
4335 /* Use maximum of all visible lines. Remember the lnum of the
4336 * longest line, clostest to the cursor line. Used when scrolling
4337 * below. */
4338 max = 0;
4339 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4340 {
4341 n = scroll_line_len(lnum);
4342 if (n > (colnr_T)max)
4343 {
4344 max = n;
4345 longest_lnum = lnum;
4346 }
4347 else if (n == (colnr_T)max
4348 && abs((int)(lnum - curwin->w_cursor.lnum))
4349 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4350 longest_lnum = lnum;
4351 }
4352 }
4353 else
4354 /* Use cursor line only. */
4355 max = scroll_line_len(curwin->w_cursor.lnum);
4356#ifdef FEAT_VIRTUALEDIT
4357 if (virtual_active())
4358 {
4359 /* May move the cursor even further to the right. */
4360 if (curwin->w_virtcol >= (colnr_T)max)
4361 max = curwin->w_virtcol;
4362 }
4363#endif
4364
4365#ifndef SCROLL_PAST_END
4366 max += W_WIDTH(curwin) - 1;
4367#endif
4368 /* The line number isn't scrolled, thus there is less space when
4369 * 'number' is set (also for 'foldcolumn'). */
4370 size -= curwin_col_off();
4371#ifndef SCROLL_PAST_END
4372 max -= curwin_col_off();
4373#endif
4374 }
4375
4376#ifndef SCROLL_PAST_END
4377 if (value > max - size + 1)
4378 value = max - size + 1; /* limit the value to allowable range */
4379#endif
4380
4381#ifdef FEAT_RIGHTLEFT
4382 if (curwin->w_p_rl)
4383 {
4384 value = max + 1 - size - value;
4385 if (value < 0)
4386 {
4387 size += value;
4388 value = 0;
4389 }
4390 }
4391#endif
4392 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4393 && max == gui.bottom_sbar.max)
4394 return;
4395
4396 gui.bottom_sbar.value = value;
4397 gui.bottom_sbar.size = size;
4398 gui.bottom_sbar.max = max;
4399 gui.prev_wrap = curwin->w_p_wrap;
4400
4401 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4402}
4403
4404/*
4405 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4406 */
4407 int
4408gui_do_horiz_scroll()
4409{
4410 /* no wrapping, no scrolling */
4411 if (curwin->w_p_wrap)
4412 return FALSE;
4413
4414 if (curwin->w_leftcol == scrollbar_value)
4415 return FALSE;
4416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004417 curwin->w_leftcol = (colnr_T)scrollbar_value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 /* When the line of the cursor is too short, move the cursor to the
4420 * longest visible line. Do a sanity check on "longest_lnum", just in
4421 * case. */
4422 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4423 && longest_lnum >= curwin->w_topline
4424 && longest_lnum < curwin->w_botline
4425 && !virtual_active())
4426 {
4427 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4428 {
4429 curwin->w_cursor.lnum = longest_lnum;
4430 curwin->w_cursor.col = 0;
4431 }
4432 }
4433
4434 return leftcol_changed();
4435}
4436
4437/*
4438 * Check that none of the colors are the same as the background color
4439 */
4440 void
4441gui_check_colors()
4442{
4443 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4444 {
4445 gui_set_bg_color((char_u *)"White");
4446 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4447 gui_set_fg_color((char_u *)"Black");
4448 }
4449}
4450
Bram Moolenaar3918c952005-03-15 22:34:55 +00004451 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452gui_set_fg_color(name)
4453 char_u *name;
4454{
4455 gui.norm_pixel = gui_get_color(name);
4456 hl_set_fg_color_name(vim_strsave(name));
4457}
4458
Bram Moolenaar3918c952005-03-15 22:34:55 +00004459 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460gui_set_bg_color(name)
4461 char_u *name;
4462{
4463 gui.back_pixel = gui_get_color(name);
4464 hl_set_bg_color_name(vim_strsave(name));
4465}
4466
4467/*
4468 * Allocate a color by name.
4469 * Returns INVALCOLOR and gives an error message when failed.
4470 */
4471 guicolor_T
4472gui_get_color(name)
4473 char_u *name;
4474{
4475 guicolor_T t;
4476
4477 if (*name == NUL)
4478 return INVALCOLOR;
4479 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004482#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 && gui.in_use
4484#endif
4485 )
4486 EMSG2(_("E254: Cannot allocate color %s"), name);
4487 return t;
4488}
4489
4490/*
4491 * Return the grey value of a color (range 0-255).
4492 */
4493 int
4494gui_get_lightness(pixel)
4495 guicolor_T pixel;
4496{
4497 long_u rgb = gui_mch_get_rgb(pixel);
4498
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004499 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004500 + (((rgb >> 8) & 0xff) * 587)
4501 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502}
4503
4504#if defined(FEAT_GUI_X11) || defined(PROTO)
4505 void
4506gui_new_scrollbar_colors()
4507{
4508 win_T *wp;
4509
4510 /* Nothing to do if GUI hasn't started yet. */
4511 if (!gui.in_use)
4512 return;
4513
4514 FOR_ALL_WINDOWS(wp)
4515 {
4516 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4517 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4518 }
4519 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4520}
4521#endif
4522
4523/*
4524 * Call this when focus has changed.
4525 */
4526 void
4527gui_focus_change(in_focus)
4528 int in_focus;
4529{
4530/*
4531 * Skip this code to avoid drawing the cursor when debugging and switching
4532 * between the debugger window and gvim.
4533 */
4534#if 1
4535 gui.in_focus = in_focus;
4536 out_flush(); /* make sure output has been written */
4537 gui_update_cursor(TRUE, FALSE);
4538
4539# ifdef FEAT_XIM
4540 xim_set_focus(in_focus);
4541# endif
4542
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004543 /* Put events in the input queue only when allowed.
4544 * ui_focus_change() isn't called directly, because it invokes
4545 * autocommands and that must not happen asynchronously. */
4546 if (!hold_gui_events)
4547 {
4548 char_u bytes[3];
4549
4550 bytes[0] = CSI;
4551 bytes[1] = KS_EXTRA;
4552 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4553 add_to_input_buf(bytes, 3);
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#endif
4556}
4557
4558/*
4559 * Called when the mouse moved (but not when dragging).
4560 */
4561 void
4562gui_mouse_moved(x, y)
4563 int x;
4564 int y;
4565{
4566 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004567 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaard3667a22006-03-16 21:35:52 +00004569 /* Ignore this while still starting up. */
4570 if (!gui.in_use || gui.starting)
4571 return;
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifdef FEAT_MOUSESHAPE
4574 /* Get window pointer, and update mouse shape as well. */
4575 wp = xy2win(x, y);
4576#endif
4577
4578 /* Only handle this when 'mousefocus' set and ... */
4579 if (p_mousef
4580 && !hold_gui_events /* not holding events */
4581 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4582 && State != HITRETURN /* but not hit-return prompt */
4583 && msg_scrolled == 0 /* no scrolled message */
4584 && !need_mouse_correct /* not moving the pointer */
4585 && gui.in_focus) /* gvim in focus */
4586 {
4587 /* Don't move the mouse when it's left or right of the Vim window */
4588 if (x < 0 || x > Columns * gui.char_width)
4589 return;
4590#ifndef FEAT_MOUSESHAPE
4591 wp = xy2win(x, y);
4592#endif
4593 if (wp == curwin || wp == NULL)
4594 return; /* still in the same old window, or none at all */
4595
Bram Moolenaar9c102382006-05-03 21:26:49 +00004596#ifdef FEAT_WINDOWS
4597 /* Ignore position in the tab pages line. */
4598 if (Y_2_ROW(y) < tabline_height())
4599 return;
4600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 /*
4603 * format a mouse click on status line input
4604 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004605 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4606 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 */
4608 if (finish_op)
4609 {
4610 /* abort the current operator first */
4611 st[0] = ESC;
4612 add_to_input_buf(st, 1);
4613 }
4614 st[0] = CSI;
4615 st[1] = KS_MOUSE;
4616 st[2] = KE_FILLER;
4617 st[3] = (char_u)MOUSE_LEFT;
4618 fill_mouse_coord(st + 4,
4619#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004620 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#else
4622 -1,
4623#endif
4624 wp->w_height + W_WINROW(wp));
4625
4626 add_to_input_buf(st, 8);
4627 st[3] = (char_u)MOUSE_RELEASE;
4628 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#ifdef FEAT_GUI_GTK
4630 /* Need to wake up the main loop */
4631 if (gtk_main_level() > 0)
4632 gtk_main_quit();
4633#endif
4634 }
4635}
4636
4637/*
4638 * Called when mouse should be moved to window with focus.
4639 */
4640 void
4641gui_mouse_correct()
4642{
4643 int x, y;
4644 win_T *wp = NULL;
4645
4646 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004647
4648 if (!(gui.in_use && p_mousef))
4649 return;
4650
4651 gui_mch_getmouse(&x, &y);
4652 /* Don't move the mouse when it's left or right of the Vim window */
4653 if (x < 0 || x > Columns * gui.char_width)
4654 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004655 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004656# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004657 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004658# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004659 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004660 wp = xy2win(x, y);
4661 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004663 validate_cline_row();
4664 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4665 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668}
4669
4670/*
4671 * Find window where the mouse pointer "y" coordinate is in.
4672 */
4673/*ARGSUSED*/
4674 static win_T *
4675xy2win(x, y)
4676 int x;
4677 int y;
4678{
4679#ifdef FEAT_WINDOWS
4680 int row;
4681 int col;
4682 win_T *wp;
4683
4684 row = Y_2_ROW(y);
4685 col = X_2_COL(x);
4686 if (row < 0 || col < 0) /* before first window */
4687 return NULL;
4688 wp = mouse_find_win(&row, &col);
4689# ifdef FEAT_MOUSESHAPE
4690 if (State == HITRETURN || State == ASKMORE)
4691 {
4692 if (Y_2_ROW(y) >= msg_row)
4693 update_mouseshape(SHAPE_IDX_MOREL);
4694 else
4695 update_mouseshape(SHAPE_IDX_MORE);
4696 }
4697 else if (row > wp->w_height) /* below status line */
4698 update_mouseshape(SHAPE_IDX_CLINE);
4699# ifdef FEAT_VERTSPLIT
4700 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004701 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 update_mouseshape(SHAPE_IDX_VSEP);
4703# endif
4704 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004705 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 update_mouseshape(SHAPE_IDX_STATUS);
4707 else
4708 update_mouseshape(-2);
4709# endif
4710 return wp;
4711#else
4712 return firstwin;
4713#endif
4714}
4715
4716/*
4717 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4718 * File names may be given to redefine the args list.
4719 */
4720 void
4721ex_gui(eap)
4722 exarg_T *eap;
4723{
4724 char_u *arg = eap->arg;
4725
4726 /*
4727 * Check for "-f" argument: foreground, don't fork.
4728 * Also don't fork when started with "gvim -f".
4729 * Do fork when using "gui -b".
4730 */
4731 if (arg[0] == '-'
4732 && (arg[1] == 'f' || arg[1] == 'b')
4733 && (arg[2] == NUL || vim_iswhite(arg[2])))
4734 {
4735 gui.dofork = (arg[1] == 'b');
4736 eap->arg = skipwhite(eap->arg + 2);
4737 }
4738 if (!gui.in_use)
4739 {
4740 /* Clear the command. Needed for when forking+exiting, to avoid part
4741 * of the argument ending up after the shell prompt. */
4742 msg_clr_eos_force();
4743 gui_start();
4744 }
4745 if (!ends_excmd(*eap->arg))
4746 ex_next(eap);
4747}
4748
4749#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004750 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751/*
4752 * This is shared between Athena, Motif and GTK.
4753 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004754static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756/*
4757 * Callback function for do_in_runtimepath().
4758 */
4759 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004760gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004762 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764 char_u *gfp_buffer = cookie;
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (STRLEN(fname) >= MAXPATHL)
4767 *gfp_buffer = NUL;
4768 else
4769 STRCPY(gfp_buffer, fname);
4770}
4771
4772/*
4773 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4774 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4775 */
4776 int
4777gui_find_bitmap(name, buffer, ext)
4778 char_u *name;
4779 char_u *buffer;
4780 char *ext;
4781{
4782 if (STRLEN(name) > MAXPATHL - 14)
4783 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004784 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004785 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4786 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 return FAIL;
4788 return OK;
4789}
4790
4791# if !defined(HAVE_GTK2) || defined(PROTO)
4792/*
4793 * Given the name of the "icon=" argument, try finding the bitmap file for the
4794 * icon. If it is an absolute path name, use it as it is. Otherwise append
4795 * "ext" and search for it in 'runtimepath'.
4796 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4797 * contains "name".
4798 */
4799 void
4800gui_find_iconfile(name, buffer, ext)
4801 char_u *name;
4802 char_u *buffer;
4803 char *ext;
4804{
4805 char_u buf[MAXPATHL + 1];
4806
4807 expand_env(name, buffer, MAXPATHL);
4808 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4809 STRCPY(buffer, buf);
4810}
4811# endif
4812#endif
4813
Bram Moolenaar9372a112005-12-06 19:59:18 +00004814#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 void
4816display_errors()
4817{
4818 char_u *p;
4819
4820 if (isatty(2))
4821 fflush(stderr);
4822 else if (error_ga.ga_data != NULL)
4823 {
4824 /* avoid putting up a message box with blanks only */
4825 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4826 if (!isspace(*p))
4827 {
4828 /* Truncate a very long message, it will go off-screen. */
4829 if (STRLEN(p) > 2000)
4830 STRCPY(p + 2000 - 14, "...(truncated)");
4831 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4832 p, (char_u *)_("&Ok"), 1, NULL);
4833 break;
4834 }
4835 ga_clear(&error_ga);
4836 }
4837}
4838#endif
4839
4840#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4841/*
4842 * Return TRUE if still starting up and there is no place to enter text.
4843 * For GTK and X11 we check if stderr is not a tty, which means we were
4844 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4845 * allow typing on stdin.
4846 */
4847 int
4848no_console_input()
4849{
4850 return ((!gui.in_use || gui.starting)
4851# ifndef NO_CONSOLE
4852 && !isatty(0) && !isatty(2)
4853# endif
4854 );
4855}
4856#endif
4857
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004858#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00004859 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004860 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861/*
4862 * Update the current window and the screen.
4863 */
4864 void
4865gui_update_screen()
4866{
4867 update_topline();
4868 validate_cursor();
Bram Moolenaarec80df72008-05-07 19:46:51 +00004869#ifdef FEAT_AUTOCMD
4870 /* Trigger CursorMoved if the cursor moved. */
4871 if (!finish_op && has_cursormoved()
4872 && !equalpos(last_cursormoved, curwin->w_cursor))
4873 {
4874 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4875 last_cursormoved = curwin->w_cursor;
4876 }
4877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 update_screen(0); /* may need to update the screen */
4879 setcursor();
4880 out_flush(); /* make sure output has been written */
4881 gui_update_cursor(TRUE, FALSE);
4882 gui_mch_flush();
4883}
4884#endif
4885
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004886#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4888
4889/*
4890 * Get the text to use in a find/replace dialog. Uses the last search pattern
4891 * if the argument is empty.
4892 * Returns an allocated string.
4893 */
4894 char_u *
4895get_find_dialog_text(arg, wwordp, mcasep)
4896 char_u *arg;
4897 int *wwordp; /* return: TRUE if \< \> found */
4898 int *mcasep; /* return: TRUE if \C found */
4899{
4900 char_u *text;
4901
4902 if (*arg == NUL)
4903 text = last_search_pat();
4904 else
4905 text = arg;
4906 if (text != NULL)
4907 {
4908 text = vim_strsave(text);
4909 if (text != NULL)
4910 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004911 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int i;
4913
4914 /* Remove "\V" */
4915 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4916 {
4917 mch_memmove(text, text + 2, (size_t)(len - 1));
4918 len -= 2;
4919 }
4920
4921 /* Recognize "\c" and "\C" and remove. */
4922 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4923 {
4924 *mcasep = (text[1] == 'C');
4925 mch_memmove(text, text + 2, (size_t)(len - 1));
4926 len -= 2;
4927 }
4928
4929 /* Recognize "\<text\>" and remove. */
4930 if (len >= 4
4931 && STRNCMP(text, "\\<", 2) == 0
4932 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4933 {
4934 *wwordp = TRUE;
4935 mch_memmove(text, text + 2, (size_t)(len - 4));
4936 text[len - 4] = NUL;
4937 }
4938
4939 /* Recognize "\/" or "\?" and remove. */
4940 for (i = 0; i + 1 < len; ++i)
4941 if (text[i] == '\\' && (text[i + 1] == '/'
4942 || text[i + 1] == '?'))
4943 {
4944 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4945 --len;
4946 }
4947 }
4948 }
4949 return text;
4950}
4951
4952/*
4953 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4954 */
4955 static void
4956concat_esc(gap, text, what)
4957 garray_T *gap;
4958 char_u *text;
4959 int what;
4960{
4961 while (*text != NUL)
4962 {
4963#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004964 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (l > 1)
4967 {
4968 while (--l >= 0)
4969 ga_append(gap, *text++);
4970 continue;
4971 }
4972#endif
4973 if (*text == what)
4974 ga_append(gap, '\\');
4975 ga_append(gap, *text);
4976 ++text;
4977 }
4978}
4979
4980/*
4981 * Handle the press of a button in the find-replace dialog.
4982 * Return TRUE when something was added to the input buffer.
4983 */
4984 int
4985gui_do_findrepl(flags, find_text, repl_text, down)
4986 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4987 char_u *find_text;
4988 char_u *repl_text;
4989 int down; /* Search downwards. */
4990{
4991 garray_T ga;
4992 int i;
4993 int type = (flags & FRD_TYPE_MASK);
4994 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004995 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004996 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
4998 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004999 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 ga_concat(&ga, (char_u *)"%s/");
5001
5002 ga_concat(&ga, (char_u *)"\\V");
5003 if (flags & FRD_MATCH_CASE)
5004 ga_concat(&ga, (char_u *)"\\C");
5005 else
5006 ga_concat(&ga, (char_u *)"\\c");
5007 if (flags & FRD_WHOLE_WORD)
5008 ga_concat(&ga, (char_u *)"\\<");
5009 if (type == FRD_REPLACEALL || down)
5010 concat_esc(&ga, find_text, '/'); /* escape slashes */
5011 else
5012 concat_esc(&ga, find_text, '?'); /* escape '?' */
5013 if (flags & FRD_WHOLE_WORD)
5014 ga_concat(&ga, (char_u *)"\\>");
5015
5016 if (type == FRD_REPLACEALL)
5017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005019 /* escape / and \ */
5020 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5021 if (p != NULL)
5022 ga_concat(&ga, p);
5023 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005025 }
5026 ga_append(&ga, NUL);
5027
5028 if (type == FRD_REPLACE)
5029 {
5030 /* Do the replacement when the text at the cursor matches. Thus no
5031 * replacement is done if the cursor was moved! */
5032 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5033 regmatch.rm_ic = 0;
5034 if (regmatch.regprog != NULL)
5035 {
5036 p = ml_get_cursor();
5037 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5038 && regmatch.startp[0] == p)
5039 {
5040 /* Clear the command line to remove any old "No match"
5041 * error. */
5042 msg_end_prompt();
5043
5044 if (u_save_cursor() == OK)
5045 {
5046 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005047 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005048
5049 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005050 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005051 ins_str(repl_text);
5052 }
5053 }
5054 else
5055 MSG(_("No match at cursor, finding next"));
5056 vim_free(regmatch.regprog);
5057 }
5058 }
5059
5060 if (type == FRD_REPLACEALL)
5061 {
5062 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005063 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 do_cmdline_cmd(ga.ga_data);
5065 }
5066 else
5067 {
5068 /* Search for the next match. */
5069 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005071 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 msg_scroll = i; /* don't let an error message set msg_scroll */
5073 }
5074
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005075 /* Don't want to pass did_emsg to other code, it may cause disabling
5076 * syntax HL if we were busy redrawing. */
5077 did_emsg = save_did_emsg;
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (State & (NORMAL | INSERT))
5080 {
5081 gui_update_screen(); /* update the screen */
5082 msg_didout = 0; /* overwrite any message */
5083 need_wait_return = FALSE; /* don't wait for return */
5084 }
5085
5086 vim_free(ga.ga_data);
5087 return (ga.ga_len > 0);
5088}
5089
5090#endif
5091
5092#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5093 || defined(FEAT_GUI_MSWIN) \
5094 || defined(FEAT_GUI_MAC) \
5095 || defined(PROTO)
5096
5097#ifdef FEAT_WINDOWS
5098static void gui_wingoto_xy __ARGS((int x, int y));
5099
5100/*
5101 * Jump to the window at specified point (x, y).
5102 */
5103 static void
5104gui_wingoto_xy(x, y)
5105 int x;
5106 int y;
5107{
5108 int row = Y_2_ROW(y);
5109 int col = X_2_COL(x);
5110 win_T *wp;
5111
5112 if (row >= 0 && col >= 0)
5113 {
5114 wp = mouse_find_win(&row, &col);
5115 if (wp != NULL && wp != curwin)
5116 win_goto(wp);
5117 }
5118}
5119#endif
5120
5121/*
5122 * Process file drop. Mouse cursor position, key modifiers, name of files
5123 * and count of files are given. Argument "fnames[count]" has full pathnames
5124 * of dropped files, they will be freed in this function, and caller can't use
5125 * fnames after call this function.
5126 */
5127/*ARGSUSED*/
5128 void
5129gui_handle_drop(x, y, modifiers, fnames, count)
5130 int x;
5131 int y;
5132 int_u modifiers;
5133 char_u **fnames;
5134 int count;
5135{
5136 int i;
5137 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005138 static int entered = FALSE;
5139
5140 /*
5141 * This function is called by event handlers. Just in case we get a
5142 * second event before the first one is handled, ignore the second one.
5143 * Not sure if this can ever happen, just in case.
5144 */
5145 if (entered)
5146 return;
5147 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 /*
5150 * When the cursor is at the command line, add the file names to the
5151 * command line, don't edit the files.
5152 */
5153 if (State & CMDLINE)
5154 {
5155 shorten_filenames(fnames, count);
5156 for (i = 0; i < count; ++i)
5157 {
5158 if (fnames[i] != NULL)
5159 {
5160 if (i > 0)
5161 add_to_input_buf((char_u*)" ", 1);
5162
5163 /* We don't know what command is used thus we can't be sure
5164 * about which characters need to be escaped. Only escape the
5165 * most common ones. */
5166# ifdef BACKSLASH_IN_FILENAME
5167 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5168# else
5169 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5170# endif
5171 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005172 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 vim_free(p);
5174 vim_free(fnames[i]);
5175 }
5176 }
5177 vim_free(fnames);
5178 }
5179 else
5180 {
5181 /* Go to the window under mouse cursor, then shorten given "fnames" by
5182 * current window, because a window can have local current dir. */
5183# ifdef FEAT_WINDOWS
5184 gui_wingoto_xy(x, y);
5185# endif
5186 shorten_filenames(fnames, count);
5187
5188 /* If Shift held down, remember the first item. */
5189 if ((modifiers & MOUSE_SHIFT) != 0)
5190 p = vim_strsave(fnames[0]);
5191 else
5192 p = NULL;
5193
5194 /* Handle the drop, :edit or :split to get to the file. This also
5195 * frees fnames[]. Skip this if there is only one item it's a
5196 * directory and Shift is held down. */
5197 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5198 && mch_isdir(fnames[0]))
5199 {
5200 vim_free(fnames[0]);
5201 vim_free(fnames);
5202 }
5203 else
5204 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5205
5206 /* If Shift held down, change to first file's directory. If the first
5207 * item is a directory, change to that directory (and let the explorer
5208 * plugin show the contents). */
5209 if (p != NULL)
5210 {
5211 if (mch_isdir(p))
5212 {
5213 if (mch_chdir((char *)p) == 0)
5214 shorten_fnames(TRUE);
5215 }
5216 else if (vim_chdirfile(p) == OK)
5217 shorten_fnames(TRUE);
5218 vim_free(p);
5219 }
5220
5221 /* Update the screen display */
5222 update_screen(NOT_VALID);
5223# ifdef FEAT_MENU
5224 gui_update_menus(0);
5225# endif
5226 setcursor();
5227 out_flush();
5228 gui_update_cursor(FALSE, FALSE);
5229 gui_mch_flush();
5230 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005231
5232 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233}
5234#endif