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