blob: 03b38b01a4752ae05642b278e2b8b0fd3483e8a0 [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]);
142 (void)read(pipefd[0], &dummy, (size_t)1);
143 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
190 /* If the GUI started successfully, trigger the GUIEnter event */
191 if (gui.in_use)
192 apply_autocmds(EVENT_GUIENTER, NULL, NULL, FALSE, curbuf);
193#endif
194
195 --recursive;
196}
197
198/*
199 * Call this when vim starts up, whether or not the GUI is started
200 */
201 void
202gui_prepare(argc, argv)
203 int *argc;
204 char **argv;
205{
206 gui.in_use = FALSE; /* No GUI yet (maybe later) */
207 gui.starting = FALSE; /* No GUI yet (maybe later) */
208 gui_mch_prepare(argc, argv);
209}
210
211/*
212 * Try initializing the GUI and check if it can be started.
213 * Used from main() to check early if "vim -g" can start the GUI.
214 * Used from gui_init() to prepare for starting the GUI.
215 * Returns FAIL or OK.
216 */
217 int
218gui_init_check()
219{
220 static int result = MAYBE;
221
222 if (result != MAYBE)
223 {
224 if (result == FAIL)
225 EMSG(_("E229: Cannot start the GUI"));
226 return result;
227 }
228
229 gui.shell_created = FALSE;
230 gui.dying = FALSE;
231 gui.in_focus = TRUE; /* so the guicursor setting works */
232 gui.dragged_sb = SBAR_NONE;
233 gui.dragged_wp = NULL;
234 gui.pointer_hidden = FALSE;
235 gui.col = 0;
236 gui.row = 0;
237 gui.num_cols = Columns;
238 gui.num_rows = Rows;
239
240 gui.cursor_is_valid = FALSE;
241 gui.scroll_region_top = 0;
242 gui.scroll_region_bot = Rows - 1;
243 gui.scroll_region_left = 0;
244 gui.scroll_region_right = Columns - 1;
245 gui.highlight_mask = HL_NORMAL;
246 gui.char_width = 1;
247 gui.char_height = 1;
248 gui.char_ascent = 0;
249 gui.border_width = 0;
250
251 gui.norm_font = NOFONT;
252#ifndef HAVE_GTK2
253 gui.bold_font = NOFONT;
254 gui.ital_font = NOFONT;
255 gui.boldital_font = NOFONT;
256# ifdef FEAT_XFONTSET
257 gui.fontset = NOFONTSET;
258# endif
259#endif
260
261#ifdef FEAT_MENU
262# ifndef HAVE_GTK2
263# ifdef FONTSET_ALWAYS
264 gui.menu_fontset = NOFONTSET;
265# else
266 gui.menu_font = NOFONT;
267# endif
268# endif
269 gui.menu_is_active = TRUE; /* default: include menu */
270# ifndef FEAT_GUI_GTK
271 gui.menu_height = MENU_DEFAULT_HEIGHT;
272 gui.menu_width = 0;
273# endif
274#endif
275#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
276 gui.toolbar_height = 0;
277#endif
278#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
279 gui.footer_height = 0;
280#endif
281#ifdef FEAT_BEVAL_TIP
282 gui.tooltip_fontset = NOFONTSET;
283#endif
284
285 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
286 gui.prev_wrap = -1;
287
288#ifdef ALWAYS_USE_GUI
289 result = OK;
290#else
291 result = gui_mch_init_check();
292#endif
293 return result;
294}
295
296/*
297 * This is the call which starts the GUI.
298 */
299 void
300gui_init()
301{
302 win_T *wp;
303 static int recursive = 0;
304
305 /*
306 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
307 * function will then be executed at the first call, the rest by the
308 * recursive call. This allow the shell to be opened halfway reading a
309 * gvimrc file.
310 */
311 if (!recursive)
312 {
313 ++recursive;
314
315 clip_init(TRUE);
316
317 /* If can't initialize, don't try doing the rest */
318 if (gui_init_check() == FAIL)
319 {
320 --recursive;
321 clip_init(FALSE);
322 return;
323 }
324
325 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000326 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
327 * breaks the Paste toolbar button.
328 */
329 set_option_value((char_u *)"paste", 0L, NULL, 0);
330
331 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 * Set up system-wide default menus.
333 */
334#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
335 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
336 {
337 sys_menu = TRUE;
338 do_source((char_u *)SYS_MENU_FILE, FALSE, FALSE);
339 sys_menu = FALSE;
340 }
341#endif
342
343 /*
344 * Switch on the mouse by default, unless the user changed it already.
345 * This can then be changed in the .gvimrc.
346 */
347 if (!option_was_set((char_u *)"mouse"))
348 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000349 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351 /*
352 * If -U option given, use only the initializations from that file and
353 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
354 */
355 if (use_gvimrc != NULL)
356 {
357 if (STRCMP(use_gvimrc, "NONE") != 0
358 && STRCMP(use_gvimrc, "NORC") != 0
359 && do_source(use_gvimrc, FALSE, FALSE) != OK)
360 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
361 }
362 else
363 {
364 /*
365 * Get system wide defaults for gvim, only when file name defined.
366 */
367#ifdef SYS_GVIMRC_FILE
368 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, FALSE);
369#endif
370
371 /*
372 * Try to read GUI initialization commands from the following
373 * places:
374 * - environment variable GVIMINIT
375 * - the user gvimrc file (~/.gvimrc)
376 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
377 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
378 * The first that exists is used, the rest is ignored.
379 */
380 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000381 && do_source((char_u *)USR_GVIMRC_FILE, TRUE, TRUE) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#ifdef USR_GVIMRC_FILE2
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000383 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE, TRUE) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#endif
385 )
386 {
387#ifdef USR_GVIMRC_FILE3
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000388 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
390 }
391
392 /*
393 * Read initialization commands from ".gvimrc" in current
394 * directory. This is only done if the 'exrc' option is set.
395 * Because of security reasons we disallow shell and write
396 * commands now, except for unix if the file is owned by the user
397 * or 'secure' option has been reset in environment of global
398 * ".gvimrc".
399 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
400 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
401 */
402 if (p_exrc)
403 {
404#ifdef UNIX
405 {
406 struct stat s;
407
408 /* if ".gvimrc" file is not owned by user, set 'secure'
409 * mode */
410 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
411 secure = p_secure;
412 }
413#else
414 secure = p_secure;
415#endif
416
417 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
418 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
419#ifdef SYS_GVIMRC_FILE
420 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
421 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
422#endif
423#ifdef USR_GVIMRC_FILE2
424 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
425 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
426#endif
427#ifdef USR_GVIMRC_FILE3
428 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
429 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
430#endif
431 )
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000432 do_source((char_u *)GVIMRC_FILE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434 if (secure == 2)
435 need_wait_return = TRUE;
436 secure = 0;
437 }
438 }
439
440 if (need_wait_return || msg_didany)
441 wait_return(TRUE);
442
443 --recursive;
444 }
445
446 /* If recursive call opened the shell, return here from the first call */
447 if (gui.in_use)
448 return;
449
450 /*
451 * Create the GUI shell.
452 */
453 gui.in_use = TRUE; /* Must be set after menus have been set up */
454 if (gui_mch_init() == FAIL)
455 goto error;
456
457 /* Avoid a delay for an error message that was printed in the terminal
458 * where Vim was started. */
459 emsg_on_display = FALSE;
460 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000461 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 need_wait_return = FALSE;
463 msg_didany = FALSE;
464
465 /*
466 * Check validity of any generic resources that may have been loaded.
467 */
468 if (gui.border_width < 0)
469 gui.border_width = 0;
470
471 /*
472 * Set up the fonts. First use a font specified with "-fn" or "-font".
473 */
474 if (font_argument != NULL)
475 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
476 if (
477#ifdef FEAT_XFONTSET
478 (*p_guifontset == NUL
479 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
480#endif
481 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
482 : p_guifont, FALSE) == FAIL)
483 {
484 EMSG(_("E665: Cannot start GUI, no valid font found"));
485 goto error2;
486 }
487#ifdef FEAT_MBYTE
488 if (gui_get_wide_font() == FAIL)
489 EMSG(_("E231: 'guifontwide' invalid"));
490#endif
491
492 gui.num_cols = Columns;
493 gui.num_rows = Rows;
494 gui_reset_scroll_region();
495
496 /* Create initial scrollbars */
497 FOR_ALL_WINDOWS(wp)
498 {
499 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
500 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
501 }
502 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
503
504#ifdef FEAT_MENU
505 gui_create_initial_menus(root_menu);
506#endif
507#ifdef FEAT_SUN_WORKSHOP
508 if (usingSunWorkShop)
509 workshop_init();
510#endif
511#ifdef FEAT_SIGN_ICONS
512 sign_gui_started();
513#endif
514
515 /* Configure the desired menu and scrollbars */
516 gui_init_which_components(NULL);
517
518 /* All components of the GUI have been created now */
519 gui.shell_created = TRUE;
520
521#ifndef FEAT_GUI_GTK
522 /* Set the shell size, adjusted for the screen size. For GTK this only
523 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000524 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
527 /* Need to set the size of the menubar after all the menus have been
528 * created. */
529 gui_mch_compute_menu_height((Widget)0);
530#endif
531
532 /*
533 * Actually open the GUI shell.
534 */
535 if (gui_mch_open() != FAIL)
536 {
537#ifdef FEAT_TITLE
538 maketitle();
539 resettitle();
540#endif
541 init_gui_options();
542#ifdef FEAT_ARABIC
543 /* Our GUI can't do bidi. */
544 p_tbidi = FALSE;
545#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000546#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 /* Give GTK+ a chance to put all widget's into place. */
548 gui_mch_update();
549 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000550 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000552 /* When 'lines' was set while starting up the topframe may have to be
553 * resized. */
554 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000555
556#ifdef FEAT_BEVAL
557 /* Always create the Balloon Evaluation area, but disable it when
558 * 'ballooneval' is off */
559# ifdef FEAT_GUI_GTK
560 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
561 &general_beval_cb, NULL);
562# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000563# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000564 {
565 extern Widget textArea;
566 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000567 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000568 }
569# else
570# ifdef FEAT_GUI_W32
571 balloonEval = gui_mch_create_beval_area(NULL, NULL,
572 &general_beval_cb, NULL);
573# endif
574# endif
575# endif
576 if (!p_beval)
577 gui_mch_disable_beval_area(balloonEval);
578#endif
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580#ifdef FEAT_NETBEANS_INTG
581 if (starting == 0 && usingNetbeans)
582 /* Tell the client that it can start sending commands. */
583 netbeans_startup_done();
584#endif
585#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
586 if (!im_xim_isvalid_imactivate())
587 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
588#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000589 /* When 'cmdheight' was set during startup it may not have taken
590 * effect yet. */
591 if (p_ch != 1L)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000592 command_height(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 return;
595 }
596
597error2:
598#ifdef FEAT_GUI_X11
599 /* undo gui_mch_init() */
600 gui_mch_uninit();
601#endif
602
603error:
604 gui.in_use = FALSE;
605 clip_init(FALSE);
606}
607
608
609 void
610gui_exit(rc)
611 int rc;
612{
613#ifndef __BEOS__
614 /* don't free the fonts, it leads to a BUS error
615 * richard@whitequeen.com Jul 99 */
616 free_highlight_fonts();
617#endif
618 gui.in_use = FALSE;
619 gui_mch_exit(rc);
620}
621
Bram Moolenaar9372a112005-12-06 19:59:18 +0000622#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
624/*
625 * Called when the GUI shell is closed by the user. If there are no changed
626 * files Vim exits, otherwise there will be a dialog to ask the user what to
627 * do.
628 * When this function returns, Vim should NOT exit!
629 */
630 void
631gui_shell_closed()
632{
633 cmdmod_T save_cmdmod;
634
635 save_cmdmod = cmdmod;
636
637 /* Only exit when there are no changed files */
638 exiting = TRUE;
639# ifdef FEAT_BROWSE
640 cmdmod.browse = TRUE;
641# endif
642# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
643 cmdmod.confirm = TRUE;
644# endif
645 /* If there are changed buffers, present the user with a dialog if
646 * possible, otherwise give an error message. */
647 if (!check_changed_any(FALSE))
648 getout(0);
649
650 exiting = FALSE;
651 cmdmod = save_cmdmod;
652 setcursor(); /* position cursor */
653 out_flush();
654}
655#endif
656
657/*
658 * Set the font. "font_list" is a a comma separated list of font names. The
659 * first font name that works is used. If none is found, use the default
660 * font.
661 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
662 * Return OK when able to set the font. When it failed FAIL is returned and
663 * the fonts are unchanged.
664 */
665/*ARGSUSED*/
666 int
667gui_init_font(font_list, fontset)
668 char_u *font_list;
669 int fontset;
670{
671#define FONTLEN 320
672 char_u font_name[FONTLEN];
673 int font_list_empty = FALSE;
674 int ret = FAIL;
675
676 if (!gui.in_use)
677 return FAIL;
678
679 font_name[0] = NUL;
680 if (*font_list == NUL)
681 font_list_empty = TRUE;
682 else
683 {
684#ifdef FEAT_XFONTSET
685 /* When using a fontset, the whole list of fonts is one name. */
686 if (fontset)
687 ret = gui_mch_init_font(font_list, TRUE);
688 else
689#endif
690 while (*font_list != NUL)
691 {
692 /* Isolate one comma separated font name. */
693 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
694
695 /* Careful!!! The Win32 version of gui_mch_init_font(), when
696 * called with "*" will change p_guifont to the selected font
697 * name, which frees the old value. This makes font_list
698 * invalid. Thus when OK is returned here, font_list must no
699 * longer be used! */
700 if (gui_mch_init_font(font_name, FALSE) == OK)
701 {
702#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
703 /* If it's a Unicode font, try setting 'guifontwide' to a
704 * similar double-width font. */
705 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
706 && strstr((char *)font_name, "10646") != NULL)
707 set_guifontwide(font_name);
708#endif
709 ret = OK;
710 break;
711 }
712 }
713 }
714
715 if (ret != OK
716 && STRCMP(font_list, "*") != 0
717 && (font_list_empty || gui.norm_font == NOFONT))
718 {
719 /*
720 * Couldn't load any font in 'font_list', keep the current font if
721 * there is one. If 'font_list' is empty, or if there is no current
722 * font, tell gui_mch_init_font() to try to find a font we can load.
723 */
724 ret = gui_mch_init_font(NULL, FALSE);
725 }
726
727 if (ret == OK)
728 {
729#ifndef HAVE_GTK2
730 /* Set normal font as current font */
731# ifdef FEAT_XFONTSET
732 if (gui.fontset != NOFONTSET)
733 gui_mch_set_fontset(gui.fontset);
734 else
735# endif
736 gui_mch_set_font(gui.norm_font);
737#endif
738 gui_set_shellsize(FALSE,
739#ifdef MSWIN
740 TRUE
741#else
742 FALSE
743#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000744 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746
747 return ret;
748}
749
750#if defined(FEAT_MBYTE) || defined(PROTO)
751# ifndef HAVE_GTK2
752/*
753 * Try setting 'guifontwide' to a font twice as wide as "name".
754 */
755 static void
756set_guifontwide(name)
757 char_u *name;
758{
759 int i = 0;
760 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
761 char_u *wp = NULL;
762 char_u *p;
763 GuiFont font;
764
765 wp = wide_name;
766 for (p = name; *p != NUL; ++p)
767 {
768 *wp++ = *p;
769 if (*p == '-')
770 {
771 ++i;
772 if (i == 6) /* font type: change "--" to "-*-" */
773 {
774 if (p[1] == '-')
775 *wp++ = '*';
776 }
777 else if (i == 12) /* found the width */
778 {
779 ++p;
780 i = getdigits(&p);
781 if (i != 0)
782 {
783 /* Double the width specification. */
784 sprintf((char *)wp, "%d%s", i * 2, p);
785 font = gui_mch_get_font(wide_name, FALSE);
786 if (font != NOFONT)
787 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000788 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 gui.wide_font = font;
790 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000791 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 }
794 break;
795 }
796 }
797 }
798}
799# endif /* !HAVE_GTK2 */
800
801/*
802 * Get the font for 'guifontwide'.
803 * Return FAIL for an invalid font name.
804 */
805 int
806gui_get_wide_font()
807{
808 GuiFont font = NOFONT;
809 char_u font_name[FONTLEN];
810 char_u *p;
811
812 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
813 return OK; /* Will give an error message later. */
814
815 if (p_guifontwide != NULL && *p_guifontwide != NUL)
816 {
817 for (p = p_guifontwide; *p != NUL; )
818 {
819 /* Isolate one comma separated font name. */
820 (void)copy_option_part(&p, font_name, FONTLEN, ",");
821 font = gui_mch_get_font(font_name, FALSE);
822 if (font != NOFONT)
823 break;
824 }
825 if (font == NOFONT)
826 return FAIL;
827 }
828
829 gui_mch_free_font(gui.wide_font);
830#ifdef HAVE_GTK2
831 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
832 if (font != NOFONT && gui.norm_font != NOFONT
833 && pango_font_description_equal(font, gui.norm_font))
834 {
835 gui.wide_font = NOFONT;
836 gui_mch_free_font(font);
837 }
838 else
839#endif
840 gui.wide_font = font;
841 return OK;
842}
843#endif
844
845 void
846gui_set_cursor(row, col)
847 int row;
848 int col;
849{
850 gui.row = row;
851 gui.col = col;
852}
853
854/*
855 * gui_check_pos - check if the cursor is on the screen.
856 */
857 static void
858gui_check_pos()
859{
860 if (gui.row >= screen_Rows)
861 gui.row = screen_Rows - 1;
862 if (gui.col >= screen_Columns)
863 gui.col = screen_Columns - 1;
864 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
865 gui.cursor_is_valid = FALSE;
866}
867
868/*
869 * Redraw the cursor if necessary or when forced.
870 * Careful: The contents of ScreenLines[] must match what is on the screen,
871 * otherwise this goes wrong. May need to call out_flush() first.
872 */
873 void
874gui_update_cursor(force, clear_selection)
875 int force; /* when TRUE, update even when not moved */
876 int clear_selection;/* clear selection under cursor */
877{
878 int cur_width = 0;
879 int cur_height = 0;
880 int old_hl_mask;
881 int idx;
882 int id;
883 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
884 int cattr; /* cursor attributes */
885 int attr;
886 attrentry_T *aep = NULL;
887
888 /* Don't update the cursor when halfway busy scrolling.
889 * ScreenLines[] isn't valid then. */
890 if (!can_update_cursor)
891 return;
892
893 gui_check_pos();
894 if (!gui.cursor_is_valid || force
895 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
896 {
897 gui_undraw_cursor();
898 if (gui.row < 0)
899 return;
900#ifdef USE_IM_CONTROL
901 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
902 im_set_position(gui.row, gui.col);
903#endif
904 gui.cursor_row = gui.row;
905 gui.cursor_col = gui.col;
906
907 /* Only write to the screen after ScreenLines[] has been initialized */
908 if (!screen_cleared || ScreenLines == NULL)
909 return;
910
911 /* Clear the selection if we are about to write over it */
912 if (clear_selection)
913 clip_may_clear_selection(gui.row, gui.row);
914 /* Check that the cursor is inside the shell (resizing may have made
915 * it invalid) */
916 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
917 return;
918
919 gui.cursor_is_valid = TRUE;
920
921 /*
922 * How the cursor is drawn depends on the current mode.
923 */
924 idx = get_shape_idx(FALSE);
925 if (State & LANGMAP)
926 id = shape_table[idx].id_lm;
927 else
928 id = shape_table[idx].id;
929
930 /* get the colors and attributes for the cursor. Default is inverted */
931 cfg = INVALCOLOR;
932 cbg = INVALCOLOR;
933 cattr = HL_INVERSE;
934 gui_mch_set_blinking(shape_table[idx].blinkwait,
935 shape_table[idx].blinkon,
936 shape_table[idx].blinkoff);
937 if (id > 0)
938 {
939 cattr = syn_id2colors(id, &cfg, &cbg);
940#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
941 {
942 static int iid;
943 guicolor_T fg, bg;
944
945 if (im_get_status())
946 {
947 iid = syn_name2id((char_u *)"CursorIM");
948 if (iid > 0)
949 {
950 syn_id2colors(iid, &fg, &bg);
951 if (bg != INVALCOLOR)
952 cbg = bg;
953 if (fg != INVALCOLOR)
954 cfg = fg;
955 }
956 }
957 }
958#endif
959 }
960
961 /*
962 * Get the attributes for the character under the cursor.
963 * When no cursor color was given, use the character color.
964 */
965 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
966 if (attr > HL_ALL)
967 aep = syn_gui_attr2entry(attr);
968 if (aep != NULL)
969 {
970 attr = aep->ae_attr;
971 if (cfg == INVALCOLOR)
972 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
973 : aep->ae_u.gui.fg_color);
974 if (cbg == INVALCOLOR)
975 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
976 : aep->ae_u.gui.bg_color);
977 }
978 if (cfg == INVALCOLOR)
979 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
980 if (cbg == INVALCOLOR)
981 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
982
983#ifdef FEAT_XIM
984 if (aep != NULL)
985 {
986 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
987 : aep->ae_u.gui.bg_color);
988 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
989 : aep->ae_u.gui.fg_color);
990 if (xim_bg_color == INVALCOLOR)
991 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
992 : gui.back_pixel;
993 if (xim_fg_color == INVALCOLOR)
994 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
995 : gui.norm_pixel;
996 }
997 else
998 {
999 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1000 : gui.back_pixel;
1001 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1002 : gui.norm_pixel;
1003 }
1004#endif
1005
1006 attr &= ~HL_INVERSE;
1007 if (cattr & HL_INVERSE)
1008 {
1009 cc = cbg;
1010 cbg = cfg;
1011 cfg = cc;
1012 }
1013 cattr &= ~HL_INVERSE;
1014
1015 /*
1016 * When we don't have window focus, draw a hollow cursor.
1017 */
1018 if (!gui.in_focus)
1019 {
1020 gui_mch_draw_hollow_cursor(cbg);
1021 return;
1022 }
1023
1024 old_hl_mask = gui.highlight_mask;
1025 if (shape_table[idx].shape == SHAPE_BLOCK
1026#ifdef FEAT_HANGULIN
1027 || composing_hangul
1028#endif
1029 )
1030 {
1031 /*
1032 * Draw the text character with the cursor colors. Use the
1033 * character attributes plus the cursor attributes.
1034 */
1035 gui.highlight_mask = (cattr | attr);
1036#ifdef FEAT_HANGULIN
1037 if (composing_hangul)
1038 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1039 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1040 else
1041#endif
1042 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1043 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1044 }
1045 else
1046 {
1047#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1048 int col_off = FALSE;
1049#endif
1050 /*
1051 * First draw the partial cursor, then overwrite with the text
1052 * character, using a transparent background.
1053 */
1054 if (shape_table[idx].shape == SHAPE_VER)
1055 {
1056 cur_height = gui.char_height;
1057 cur_width = (gui.char_width * shape_table[idx].percentage
1058 + 99) / 100;
1059 }
1060 else
1061 {
1062 cur_height = (gui.char_height * shape_table[idx].percentage
1063 + 99) / 100;
1064 cur_width = gui.char_width;
1065 }
1066#ifdef FEAT_MBYTE
1067 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col) > 1)
1068 {
1069 /* Double wide character. */
1070 if (shape_table[idx].shape != SHAPE_VER)
1071 cur_width += gui.char_width;
1072# ifdef FEAT_RIGHTLEFT
1073 if (CURSOR_BAR_RIGHT)
1074 {
1075 /* gui.col points to the left halve of the character but
1076 * the vertical line needs to be on the right halve.
1077 * A double-wide horizontal line is also drawn from the
1078 * right halve in gui_mch_draw_part_cursor(). */
1079 col_off = TRUE;
1080 ++gui.col;
1081 }
1082# endif
1083 }
1084#endif
1085 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1086#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1087 if (col_off)
1088 --gui.col;
1089#endif
1090
1091#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1092 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1093 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1094 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1095 (guicolor_T)0, (guicolor_T)0, 0);
1096#endif
1097 }
1098 gui.highlight_mask = old_hl_mask;
1099 }
1100}
1101
1102#if defined(FEAT_MENU) || defined(PROTO)
1103 void
1104gui_position_menu()
1105{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001106# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (gui.menu_is_active && gui.in_use)
1108 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1109# endif
1110}
1111#endif
1112
1113/*
1114 * Position the various GUI components (text area, menu). The vertical
1115 * scrollbars are NOT handled here. See gui_update_scrollbars().
1116 */
1117/*ARGSUSED*/
1118 static void
1119gui_position_components(total_width)
1120 int total_width;
1121{
1122 int text_area_x;
1123 int text_area_y;
1124 int text_area_width;
1125 int text_area_height;
1126
1127 /* avoid that moving components around generates events */
1128 ++hold_gui_events;
1129
1130 text_area_x = 0;
1131 if (gui.which_scrollbars[SBAR_LEFT])
1132 text_area_x += gui.scrollbar_width;
1133
1134 text_area_y = 0;
1135#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1136 gui.menu_width = total_width;
1137 if (gui.menu_is_active)
1138 text_area_y += gui.menu_height;
1139#endif
1140#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1141 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1142 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1143#endif
1144
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001145#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
1146 if (gui_has_tabline())
1147 text_area_y += TABLINE_HEIGHT;
1148#endif
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1151 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1152 {
1153# ifdef FEAT_GUI_ATHENA
1154 gui_mch_set_toolbar_pos(0, text_area_y,
1155 gui.menu_width, gui.toolbar_height);
1156# endif
1157 text_area_y += gui.toolbar_height;
1158 }
1159#endif
1160
1161 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1162 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1163
1164 gui_mch_set_text_area_pos(text_area_x,
1165 text_area_y,
1166 text_area_width,
1167 text_area_height
1168#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1169 + xim_get_status_area_height()
1170#endif
1171 );
1172#ifdef FEAT_MENU
1173 gui_position_menu();
1174#endif
1175 if (gui.which_scrollbars[SBAR_BOTTOM])
1176 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1177 text_area_x,
1178 text_area_y + text_area_height,
1179 text_area_width,
1180 gui.scrollbar_height);
1181 gui.left_sbar_x = 0;
1182 gui.right_sbar_x = text_area_x + text_area_width;
1183
1184 --hold_gui_events;
1185}
1186
Bram Moolenaar02743632005-07-25 20:42:36 +00001187/*
1188 * Get the width of the widgets and decorations to the side of the text area.
1189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 int
1191gui_get_base_width()
1192{
1193 int base_width;
1194
1195 base_width = 2 * gui.border_offset;
1196 if (gui.which_scrollbars[SBAR_LEFT])
1197 base_width += gui.scrollbar_width;
1198 if (gui.which_scrollbars[SBAR_RIGHT])
1199 base_width += gui.scrollbar_width;
1200 return base_width;
1201}
1202
Bram Moolenaar02743632005-07-25 20:42:36 +00001203/*
1204 * Get the height of the widgets and decorations above and below the text area.
1205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 int
1207gui_get_base_height()
1208{
1209 int base_height;
1210
1211 base_height = 2 * gui.border_offset;
1212 if (gui.which_scrollbars[SBAR_BOTTOM])
1213 base_height += gui.scrollbar_height;
1214#ifdef FEAT_GUI_GTK
1215 /* We can't take the sizes properly into account until anything is
1216 * realized. Therefore we recalculate all the values here just before
1217 * setting the size. (--mdcki) */
1218#else
1219# ifdef FEAT_MENU
1220 if (gui.menu_is_active)
1221 base_height += gui.menu_height;
1222# endif
1223# ifdef FEAT_TOOLBAR
1224 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1225# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1226 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1227# else
1228 base_height += gui.toolbar_height;
1229# endif
1230# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001231# if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
1232 if (gui_has_tabline())
1233 base_height += TABLINE_HEIGHT;
1234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235# ifdef FEAT_FOOTER
1236 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1237 base_height += gui.footer_height;
1238# endif
1239# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1240 base_height += gui_mch_text_area_extra_height();
1241# endif
1242#endif
1243 return base_height;
1244}
1245
1246/*
1247 * Should be called after the GUI shell has been resized. Its arguments are
1248 * the new width and height of the shell in pixels.
1249 */
1250 void
1251gui_resize_shell(pixel_width, pixel_height)
1252 int pixel_width;
1253 int pixel_height;
1254{
1255 static int busy = FALSE;
1256
1257 if (!gui.shell_created) /* ignore when still initializing */
1258 return;
1259
1260 /*
1261 * Can't resize the screen while it is being redrawn. Remember the new
1262 * size and handle it later.
1263 */
1264 if (updating_screen || busy)
1265 {
1266 new_pixel_width = pixel_width;
1267 new_pixel_height = pixel_height;
1268 return;
1269 }
1270
1271again:
1272 busy = TRUE;
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 /* Flush pending output before redrawing */
1275 out_flush();
1276
1277 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1278 gui.num_rows = (pixel_height - gui_get_base_height()
1279#if !defined(FEAT_GUI_PHOTON) && !defined(FEAT_GUI_MSWIN)
1280 + (gui.char_height / 2)
1281#endif
1282 ) / gui.char_height;
1283
1284 gui_position_components(pixel_width);
1285
1286 gui_reset_scroll_region();
1287 /*
1288 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1289 * at the last line here (why does it have to be one row too low?).
1290 */
1291 if (State == ASKMORE || State == CONFIRM)
1292 gui.row = gui.num_rows;
1293
1294 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1295 * the safe side. */
1296 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1297 || gui.num_rows != Rows || gui.num_cols != Columns)
1298 shell_resized();
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 gui_update_scrollbars(TRUE);
1301 gui_update_cursor(FALSE, TRUE);
1302#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1303 xim_set_status_area();
1304#endif
1305
1306 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 /*
1309 * We could have been called again while redrawing the screen.
1310 * Need to do it all again with the latest size then.
1311 */
1312 if (new_pixel_height)
1313 {
1314 pixel_width = new_pixel_width;
1315 pixel_height = new_pixel_height;
1316 new_pixel_width = 0;
1317 new_pixel_height = 0;
1318 goto again;
1319 }
1320}
1321
1322/*
1323 * Check if gui_resize_shell() must be called.
1324 */
1325 void
1326gui_may_resize_shell()
1327{
1328 int h, w;
1329
1330 if (new_pixel_height)
1331 {
1332 /* careful: gui_resize_shell() may postpone the resize again if we
1333 * were called indirectly by it */
1334 w = new_pixel_width;
1335 h = new_pixel_height;
1336 new_pixel_width = 0;
1337 new_pixel_height = 0;
1338 gui_resize_shell(w, h);
1339 }
1340}
1341
1342 int
1343gui_get_shellsize()
1344{
1345 Rows = gui.num_rows;
1346 Columns = gui.num_cols;
1347 return OK;
1348}
1349
1350/*
1351 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001352 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1353 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
1355/*ARGSUSED*/
1356 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001357gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 int mustset; /* set by the user */
1359 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001360 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 int base_width;
1363 int base_height;
1364 int width;
1365 int height;
1366 int min_width;
1367 int min_height;
1368 int screen_w;
1369 int screen_h;
1370
1371 if (!gui.shell_created)
1372 return;
1373
1374#ifdef MSWIN
1375 /* If not setting to a user specified size and maximized, calculate the
1376 * number of characters that fit in the maximized window. */
1377 if (!mustset && gui_mch_maximized())
1378 {
1379 gui_mch_newfont();
1380 return;
1381 }
1382#endif
1383
1384 base_width = gui_get_base_width();
1385 base_height = gui_get_base_height();
1386#ifdef USE_SUN_WORKSHOP
1387 if (!mustset && usingSunWorkShop
1388 && workshop_get_width_height(&width, &height))
1389 {
1390 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1391 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1392 }
1393 else
1394#endif
1395 {
1396 width = Columns * gui.char_width + base_width;
1397 height = Rows * gui.char_height + base_height;
1398 }
1399
1400 if (fit_to_display)
1401 {
1402 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001403 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 Columns = (screen_w - base_width) / gui.char_width;
1406 if (Columns < MIN_COLUMNS)
1407 Columns = MIN_COLUMNS;
1408 width = Columns * gui.char_width + base_width;
1409 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001410 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
1412 Rows = (screen_h - base_height) / gui.char_height;
1413 check_shellsize();
1414 height = Rows * gui.char_height + base_height;
1415 }
1416 }
1417 gui.num_cols = Columns;
1418 gui.num_rows = Rows;
1419
1420 min_width = base_width + MIN_COLUMNS * gui.char_width;
1421 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001422# ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001423 min_height += tabline_height() * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001427 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (fit_to_display)
1429 {
1430 int x, y;
1431
1432 /* Some window managers put the Vim window left of/above the screen. */
1433 gui_mch_update();
1434 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1435 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1436 }
1437
1438 gui_position_components(width);
1439 gui_update_scrollbars(TRUE);
1440 gui_reset_scroll_region();
1441}
1442
1443/*
1444 * Called when Rows and/or Columns has changed.
1445 */
1446 void
1447gui_new_shellsize()
1448{
1449 gui_reset_scroll_region();
1450}
1451
1452/*
1453 * Make scroll region cover whole screen.
1454 */
1455 void
1456gui_reset_scroll_region()
1457{
1458 gui.scroll_region_top = 0;
1459 gui.scroll_region_bot = gui.num_rows - 1;
1460 gui.scroll_region_left = 0;
1461 gui.scroll_region_right = gui.num_cols - 1;
1462}
1463
1464 void
1465gui_start_highlight(mask)
1466 int mask;
1467{
1468 if (mask > HL_ALL) /* highlight code */
1469 gui.highlight_mask = mask;
1470 else /* mask */
1471 gui.highlight_mask |= mask;
1472}
1473
1474 void
1475gui_stop_highlight(mask)
1476 int mask;
1477{
1478 if (mask > HL_ALL) /* highlight code */
1479 gui.highlight_mask = HL_NORMAL;
1480 else /* mask */
1481 gui.highlight_mask &= ~mask;
1482}
1483
1484/*
1485 * Clear a rectangular region of the screen from text pos (row1, col1) to
1486 * (row2, col2) inclusive.
1487 */
1488 void
1489gui_clear_block(row1, col1, row2, col2)
1490 int row1;
1491 int col1;
1492 int row2;
1493 int col2;
1494{
1495 /* Clear the selection if we are about to write over it */
1496 clip_may_clear_selection(row1, row2);
1497
1498 gui_mch_clear_block(row1, col1, row2, col2);
1499
1500 /* Invalidate cursor if it was in this block */
1501 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1502 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1503 gui.cursor_is_valid = FALSE;
1504}
1505
1506/*
1507 * Write code to update the cursor later. This avoids the need to flush the
1508 * output buffer before calling gui_update_cursor().
1509 */
1510 void
1511gui_update_cursor_later()
1512{
1513 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1514}
1515
1516 void
1517gui_write(s, len)
1518 char_u *s;
1519 int len;
1520{
1521 char_u *p;
1522 int arg1 = 0, arg2 = 0;
1523 /* this doesn't make sense, disabled until someone can explain why it
1524 * would be needed */
1525#if 0 && (defined(RISCOS) || defined(WIN16))
1526 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1527 our redraw speed will suffer */
1528#else
1529 int force_cursor = FALSE; /* force cursor update */
1530#endif
1531 int force_scrollbar = FALSE;
1532 static win_T *old_curwin = NULL;
1533
1534/* #define DEBUG_GUI_WRITE */
1535#ifdef DEBUG_GUI_WRITE
1536 {
1537 int i;
1538 char_u *str;
1539
1540 printf("gui_write(%d):\n ", len);
1541 for (i = 0; i < len; i++)
1542 if (s[i] == ESC)
1543 {
1544 if (i != 0)
1545 printf("\n ");
1546 printf("<ESC>");
1547 }
1548 else
1549 {
1550 str = transchar_byte(s[i]);
1551 if (str[0] && str[1])
1552 printf("<%s>", (char *)str);
1553 else
1554 printf("%s", (char *)str);
1555 }
1556 printf("\n");
1557 }
1558#endif
1559 while (len)
1560 {
1561 if (s[0] == ESC && s[1] == '|')
1562 {
1563 p = s + 2;
1564 if (VIM_ISDIGIT(*p))
1565 {
1566 arg1 = getdigits(&p);
1567 if (p > s + len)
1568 break;
1569 if (*p == ';')
1570 {
1571 ++p;
1572 arg2 = getdigits(&p);
1573 if (p > s + len)
1574 break;
1575 }
1576 }
1577 switch (*p)
1578 {
1579 case 'C': /* Clear screen */
1580 clip_scroll_selection(9999);
1581 gui_mch_clear_all();
1582 gui.cursor_is_valid = FALSE;
1583 force_scrollbar = TRUE;
1584 break;
1585 case 'M': /* Move cursor */
1586 gui_set_cursor(arg1, arg2);
1587 break;
1588 case 's': /* force cursor (shape) update */
1589 force_cursor = TRUE;
1590 break;
1591 case 'R': /* Set scroll region */
1592 if (arg1 < arg2)
1593 {
1594 gui.scroll_region_top = arg1;
1595 gui.scroll_region_bot = arg2;
1596 }
1597 else
1598 {
1599 gui.scroll_region_top = arg2;
1600 gui.scroll_region_bot = arg1;
1601 }
1602 break;
1603#ifdef FEAT_VERTSPLIT
1604 case 'V': /* Set vertical scroll region */
1605 if (arg1 < arg2)
1606 {
1607 gui.scroll_region_left = arg1;
1608 gui.scroll_region_right = arg2;
1609 }
1610 else
1611 {
1612 gui.scroll_region_left = arg2;
1613 gui.scroll_region_right = arg1;
1614 }
1615 break;
1616#endif
1617 case 'd': /* Delete line */
1618 gui_delete_lines(gui.row, 1);
1619 break;
1620 case 'D': /* Delete lines */
1621 gui_delete_lines(gui.row, arg1);
1622 break;
1623 case 'i': /* Insert line */
1624 gui_insert_lines(gui.row, 1);
1625 break;
1626 case 'I': /* Insert lines */
1627 gui_insert_lines(gui.row, arg1);
1628 break;
1629 case '$': /* Clear to end-of-line */
1630 gui_clear_block(gui.row, gui.col, gui.row,
1631 (int)Columns - 1);
1632 break;
1633 case 'h': /* Turn on highlighting */
1634 gui_start_highlight(arg1);
1635 break;
1636 case 'H': /* Turn off highlighting */
1637 gui_stop_highlight(arg1);
1638 break;
1639 case 'f': /* flash the window (visual bell) */
1640 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1641 break;
1642 default:
1643 p = s + 1; /* Skip the ESC */
1644 break;
1645 }
1646 len -= (int)(++p - s);
1647 s = p;
1648 }
1649 else if (
1650#ifdef EBCDIC
1651 CtrlChar(s[0]) != 0 /* Ctrl character */
1652#else
1653 s[0] < 0x20 /* Ctrl character */
1654#endif
1655#ifdef FEAT_SIGN_ICONS
1656 && s[0] != SIGN_BYTE
1657# ifdef FEAT_NETBEANS_INTG
1658 && s[0] != MULTISIGN_BYTE
1659# endif
1660#endif
1661 )
1662 {
1663 if (s[0] == '\n') /* NL */
1664 {
1665 gui.col = 0;
1666 if (gui.row < gui.scroll_region_bot)
1667 gui.row++;
1668 else
1669 gui_delete_lines(gui.scroll_region_top, 1);
1670 }
1671 else if (s[0] == '\r') /* CR */
1672 {
1673 gui.col = 0;
1674 }
1675 else if (s[0] == '\b') /* Backspace */
1676 {
1677 if (gui.col)
1678 --gui.col;
1679 }
1680 else if (s[0] == Ctrl_L) /* cursor-right */
1681 {
1682 ++gui.col;
1683 }
1684 else if (s[0] == Ctrl_G) /* Beep */
1685 {
1686 gui_mch_beep();
1687 }
1688 /* Other Ctrl character: shouldn't happen! */
1689
1690 --len; /* Skip this char */
1691 ++s;
1692 }
1693 else
1694 {
1695 p = s;
1696 while (len > 0 && (
1697#ifdef EBCDIC
1698 CtrlChar(*p) == 0
1699#else
1700 *p >= 0x20
1701#endif
1702#ifdef FEAT_SIGN_ICONS
1703 || *p == SIGN_BYTE
1704# ifdef FEAT_NETBEANS_INTG
1705 || *p == MULTISIGN_BYTE
1706# endif
1707#endif
1708 ))
1709 {
1710 len--;
1711 p++;
1712 }
1713 gui_outstr(s, (int)(p - s));
1714 s = p;
1715 }
1716 }
1717
1718 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1719 * set). */
1720 if (force_cursor)
1721 gui_update_cursor(TRUE, TRUE);
1722
1723 /* When switching to another window the dragging must have stopped.
1724 * Required for GTK, dragged_sb isn't reset. */
1725 if (old_curwin != curwin)
1726 gui.dragged_sb = SBAR_NONE;
1727
1728 /* Update the scrollbars after clearing the screen or when switched
1729 * to another window.
1730 * Update the horizontal scrollbar always, it's difficult to check all
1731 * situations where it might change. */
1732 if (force_scrollbar || old_curwin != curwin)
1733 gui_update_scrollbars(force_scrollbar);
1734 else
1735 gui_update_horiz_scrollbar(FALSE);
1736 old_curwin = curwin;
1737
1738 /*
1739 * We need to make sure this is cleared since Athena doesn't tell us when
1740 * he is done dragging. Do the same for GTK.
1741 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001742#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 gui.dragged_sb = SBAR_NONE;
1744#endif
1745
1746 gui_mch_flush(); /* In case vim decides to take a nap */
1747}
1748
1749/*
1750 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1751 * produces wrong results. Call gui_dont_update_cursor() before that code and
1752 * gui_can_update_cursor() afterwards.
1753 */
1754 void
1755gui_dont_update_cursor()
1756{
1757 if (gui.in_use)
1758 {
1759 /* Undraw the cursor now, we probably can't do it after the change. */
1760 gui_undraw_cursor();
1761 can_update_cursor = FALSE;
1762 }
1763}
1764
1765 void
1766gui_can_update_cursor()
1767{
1768 can_update_cursor = TRUE;
1769 /* No need to update the cursor right now, there is always more output
1770 * after scrolling. */
1771}
1772
1773 static void
1774gui_outstr(s, len)
1775 char_u *s;
1776 int len;
1777{
1778 int this_len;
1779#ifdef FEAT_MBYTE
1780 int cells;
1781#endif
1782
1783 if (len == 0)
1784 return;
1785
1786 if (len < 0)
1787 len = (int)STRLEN(s);
1788
1789 while (len > 0)
1790 {
1791#ifdef FEAT_MBYTE
1792 if (has_mbyte)
1793 {
1794 /* Find out how many chars fit in the current line. */
1795 cells = 0;
1796 for (this_len = 0; this_len < len; )
1797 {
1798 cells += (*mb_ptr2cells)(s + this_len);
1799 if (gui.col + cells > Columns)
1800 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001801 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803 if (this_len > len)
1804 this_len = len; /* don't include following composing char */
1805 }
1806 else
1807#endif
1808 if (gui.col + len > Columns)
1809 this_len = Columns - gui.col;
1810 else
1811 this_len = len;
1812
1813 (void)gui_outstr_nowrap(s, this_len,
1814 0, (guicolor_T)0, (guicolor_T)0, 0);
1815 s += this_len;
1816 len -= this_len;
1817#ifdef FEAT_MBYTE
1818 /* fill up for a double-width char that doesn't fit. */
1819 if (len > 0 && gui.col < Columns)
1820 (void)gui_outstr_nowrap((char_u *)" ", 1,
1821 0, (guicolor_T)0, (guicolor_T)0, 0);
1822#endif
1823 /* The cursor may wrap to the next line. */
1824 if (gui.col >= Columns)
1825 {
1826 gui.col = 0;
1827 gui.row++;
1828 }
1829 }
1830}
1831
1832/*
1833 * Output one character (may be one or two display cells).
1834 * Caller must check for valid "off".
1835 * Returns FAIL or OK, just like gui_outstr_nowrap().
1836 */
1837 static int
1838gui_screenchar(off, flags, fg, bg, back)
1839 int off; /* Offset from start of screen */
1840 int flags;
1841 guicolor_T fg, bg; /* colors for cursor */
1842 int back; /* backup this many chars when using bold trick */
1843{
1844#ifdef FEAT_MBYTE
1845 char_u buf[MB_MAXBYTES + 1];
1846
1847 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1848 if (enc_utf8 && ScreenLines[off] == 0)
1849 return OK;
1850
1851 if (enc_utf8 && ScreenLinesUC[off] != 0)
1852 /* Draw UTF-8 multi-byte character. */
1853 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1854 flags, fg, bg, back);
1855
1856 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1857 {
1858 buf[0] = ScreenLines[off];
1859 buf[1] = ScreenLines2[off];
1860 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1861 }
1862
1863 /* Draw non-multi-byte character or DBCS character. */
1864 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001865 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 flags, fg, bg, back);
1867#else
1868 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1869#endif
1870}
1871
1872#ifdef HAVE_GTK2
1873/*
1874 * Output the string at the given screen position. This is used in place
1875 * of gui_screenchar() where possible because Pango needs as much context
1876 * as possible to work nicely. It's a lot faster as well.
1877 */
1878 static int
1879gui_screenstr(off, len, flags, fg, bg, back)
1880 int off; /* Offset from start of screen */
1881 int len; /* string length in screen cells */
1882 int flags;
1883 guicolor_T fg, bg; /* colors for cursor */
1884 int back; /* backup this many chars when using bold trick */
1885{
1886 char_u *buf;
1887 int outlen = 0;
1888 int i;
1889 int retval;
1890
1891 if (len <= 0) /* "cannot happen"? */
1892 return OK;
1893
1894 if (enc_utf8)
1895 {
1896 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1897 if (buf == NULL)
1898 return OK; /* not much we could do here... */
1899
1900 for (i = off; i < off + len; ++i)
1901 {
1902 if (ScreenLines[i] == 0)
1903 continue; /* skip second half of double-width char */
1904
1905 if (ScreenLinesUC[i] == 0)
1906 buf[outlen++] = ScreenLines[i];
1907 else
1908 outlen += utfc_char2bytes(i, buf + outlen);
1909 }
1910
1911 buf[outlen] = NUL; /* only to aid debugging */
1912 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1913 vim_free(buf);
1914
1915 return retval;
1916 }
1917 else if (enc_dbcs == DBCS_JPNU)
1918 {
1919 buf = alloc((unsigned)(len * 2 + 1));
1920 if (buf == NULL)
1921 return OK; /* not much we could do here... */
1922
1923 for (i = off; i < off + len; ++i)
1924 {
1925 buf[outlen++] = ScreenLines[i];
1926
1927 /* handle double-byte single-width char */
1928 if (ScreenLines[i] == 0x8e)
1929 buf[outlen++] = ScreenLines2[i];
1930 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1931 buf[outlen++] = ScreenLines[++i];
1932 }
1933
1934 buf[outlen] = NUL; /* only to aid debugging */
1935 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1936 vim_free(buf);
1937
1938 return retval;
1939 }
1940 else
1941 {
1942 return gui_outstr_nowrap(&ScreenLines[off], len,
1943 flags, fg, bg, back);
1944 }
1945}
1946#endif /* HAVE_GTK2 */
1947
1948/*
1949 * Output the given string at the current cursor position. If the string is
1950 * too long to fit on the line, then it is truncated.
1951 * "flags":
1952 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1953 * actually draw (an inverted) cursor.
1954 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparant
1955 * background.
1956 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1957 * it.
1958 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1959 * FAIL (the caller should start drawing "back" chars back).
1960 */
1961 int
1962gui_outstr_nowrap(s, len, flags, fg, bg, back)
1963 char_u *s;
1964 int len;
1965 int flags;
1966 guicolor_T fg, bg; /* colors for cursor */
1967 int back; /* backup this many chars when using bold trick */
1968{
1969 long_u highlight_mask;
1970 long_u hl_mask_todo;
1971 guicolor_T fg_color;
1972 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001973 guicolor_T sp_color;
Bram Moolenaar9372a112005-12-06 19:59:18 +00001974#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 GuiFont font = NOFONT;
1976# ifdef FEAT_XFONTSET
1977 GuiFontset fontset = NOFONTSET;
1978# endif
1979#endif
1980 attrentry_T *aep = NULL;
1981 int draw_flags;
1982 int col = gui.col;
1983#ifdef FEAT_SIGN_ICONS
1984 int draw_sign = FALSE;
1985# ifdef FEAT_NETBEANS_INTG
1986 int multi_sign = FALSE;
1987# endif
1988#endif
1989
1990 if (len < 0)
1991 len = (int)STRLEN(s);
1992 if (len == 0)
1993 return OK;
1994
1995#ifdef FEAT_SIGN_ICONS
1996 if (*s == SIGN_BYTE
1997# ifdef FEAT_NETBEANS_INTG
1998 || *s == MULTISIGN_BYTE
1999# endif
2000 )
2001 {
2002# ifdef FEAT_NETBEANS_INTG
2003 if (*s == MULTISIGN_BYTE)
2004 multi_sign = TRUE;
2005# endif
2006 /* draw spaces instead */
2007 s = (char_u *)" ";
2008 if (len == 1 && col > 0)
2009 --col;
2010 len = 2;
2011 draw_sign = TRUE;
2012 highlight_mask = 0;
2013 }
2014 else
2015#endif
2016 if (gui.highlight_mask > HL_ALL)
2017 {
2018 aep = syn_gui_attr2entry(gui.highlight_mask);
2019 if (aep == NULL) /* highlighting not set */
2020 highlight_mask = 0;
2021 else
2022 highlight_mask = aep->ae_attr;
2023 }
2024 else
2025 highlight_mask = gui.highlight_mask;
2026 hl_mask_todo = highlight_mask;
2027
Bram Moolenaar9372a112005-12-06 19:59:18 +00002028#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 /* Set the font */
2030 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2031 font = aep->ae_u.gui.font;
2032# ifdef FEAT_XFONTSET
2033 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2034 fontset = aep->ae_u.gui.fontset;
2035# endif
2036 else
2037 {
2038# ifdef FEAT_XFONTSET
2039 if (gui.fontset != NOFONTSET)
2040 fontset = gui.fontset;
2041 else
2042# endif
2043 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2044 {
2045 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2046 {
2047 font = gui.boldital_font;
2048 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2049 }
2050 else if (gui.bold_font != NOFONT)
2051 {
2052 font = gui.bold_font;
2053 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2054 }
2055 else
2056 font = gui.norm_font;
2057 }
2058 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2059 {
2060 font = gui.ital_font;
2061 hl_mask_todo &= ~HL_ITALIC;
2062 }
2063 else
2064 font = gui.norm_font;
2065 }
2066# ifdef FEAT_XFONTSET
2067 if (fontset != NOFONTSET)
2068 gui_mch_set_fontset(fontset);
2069 else
2070# endif
2071 gui_mch_set_font(font);
2072#endif
2073
2074 draw_flags = 0;
2075
2076 /* Set the color */
2077 bg_color = gui.back_pixel;
2078 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2079 {
2080 draw_flags |= DRAW_CURSOR;
2081 fg_color = fg;
2082 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002083 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 else if (aep != NULL)
2086 {
2087 fg_color = aep->ae_u.gui.fg_color;
2088 if (fg_color == INVALCOLOR)
2089 fg_color = gui.norm_pixel;
2090 bg_color = aep->ae_u.gui.bg_color;
2091 if (bg_color == INVALCOLOR)
2092 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002093 sp_color = aep->ae_u.gui.sp_color;
2094 if (sp_color == INVALCOLOR)
2095 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002100 sp_color = fg_color;
2101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2104 {
2105#if defined(AMIGA) || defined(RISCOS)
2106 gui_mch_set_colors(bg_color, fg_color);
2107#else
2108 gui_mch_set_fg_color(bg_color);
2109 gui_mch_set_bg_color(fg_color);
2110#endif
2111 }
2112 else
2113 {
2114#if defined(AMIGA) || defined(RISCOS)
2115 gui_mch_set_colors(fg_color, bg_color);
2116#else
2117 gui_mch_set_fg_color(fg_color);
2118 gui_mch_set_bg_color(bg_color);
2119#endif
2120 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002121 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 /* Clear the selection if we are about to write over it */
2124 if (!(flags & GUI_MON_NOCLEAR))
2125 clip_may_clear_selection(gui.row, gui.row);
2126
2127
2128#ifndef MSWIN16_FASTTEXT
2129 /* If there's no bold font, then fake it */
2130 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2131 draw_flags |= DRAW_BOLD;
2132#endif
2133
2134 /*
2135 * When drawing bold or italic characters the spill-over from the left
2136 * neighbor may be destroyed. Let the caller backup to start redrawing
2137 * just after a blank.
2138 */
2139 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2140 return FAIL;
2141
Bram Moolenaar9372a112005-12-06 19:59:18 +00002142#if defined(RISCOS) || defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 /* If there's no italic font, then fake it.
2144 * For GTK2, we don't need a different font for italic style. */
2145 if (hl_mask_todo & HL_ITALIC)
2146 draw_flags |= DRAW_ITALIC;
2147
2148 /* Do we underline the text? */
2149 if (hl_mask_todo & HL_UNDERLINE)
2150 draw_flags |= DRAW_UNDERL;
2151#else
2152 /* Do we underline the text? */
2153 if ((hl_mask_todo & HL_UNDERLINE)
2154# ifndef MSWIN16_FASTTEXT
2155 || (hl_mask_todo & HL_ITALIC)
2156# endif
2157 )
2158 draw_flags |= DRAW_UNDERL;
2159#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002160 /* Do we undercurl the text? */
2161 if (hl_mask_todo & HL_UNDERCURL)
2162 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 /* Do we draw transparantly? */
2165 if (flags & GUI_MON_TRS_CURSOR)
2166 draw_flags |= DRAW_TRANSP;
2167
2168 /*
2169 * Draw the text.
2170 */
2171#ifdef HAVE_GTK2
2172 /* The value returned is the length in display cells */
2173 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2174#else
2175# ifdef FEAT_MBYTE
2176 if (enc_utf8)
2177 {
2178 int start; /* index of bytes to be drawn */
2179 int cells; /* cellwidth of bytes to be drawn */
2180 int thislen; /* length of bytes to be drawin */
2181 int cn; /* cellwidth of current char */
2182 int i; /* index of current char */
2183 int c; /* current char value */
2184 int cl; /* byte length of current char */
2185 int comping; /* current char is composing */
2186 int scol = col; /* screen column */
2187 int dowide; /* use 'guifontwide' */
2188
2189 /* Break the string at a composing character, it has to be drawn on
2190 * top of the previous character. */
2191 start = 0;
2192 cells = 0;
2193 for (i = 0; i < len; i += cl)
2194 {
2195 c = utf_ptr2char(s + i);
2196 cn = utf_char2cells(c);
2197 if (cn > 1
2198# ifdef FEAT_XFONTSET
2199 && fontset == NOFONTSET
2200# endif
2201 && gui.wide_font != NOFONT)
2202 dowide = TRUE;
2203 else
2204 dowide = FALSE;
2205 comping = utf_iscomposing(c);
2206 if (!comping) /* count cells from non-composing chars */
2207 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002208 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (cl == 0) /* hit end of string */
2210 len = i + cl; /* len must be wrong "cannot happen" */
2211
2212 /* print the string so far if it's the last character or there is
2213 * a composing character. */
2214 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar9372a112005-12-06 19:59:18 +00002215# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 || (cn > 1
2217# ifdef FEAT_XFONTSET
2218 /* No fontset: At least draw char after wide char at
2219 * right position. */
2220 && fontset == NOFONTSET
2221# endif
2222 )
2223# endif
2224 )
2225 {
2226 if (comping || dowide)
2227 thislen = i - start;
2228 else
2229 thislen = i - start + cl;
2230 if (thislen > 0)
2231 {
2232 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2233 draw_flags);
2234 start += thislen;
2235 }
2236 scol += cells;
2237 cells = 0;
2238 if (dowide)
2239 {
2240 gui_mch_set_font(gui.wide_font);
2241 gui_mch_draw_string(gui.row, scol - cn,
2242 s + start, cl, draw_flags);
2243 gui_mch_set_font(font);
2244 start += cl;
2245 }
2246
Bram Moolenaar9372a112005-12-06 19:59:18 +00002247# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002248 /* No fontset: draw a space to fill the gap after a wide char
2249 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2251# ifdef FEAT_XFONTSET
2252 && fontset == NOFONTSET
2253# endif
2254 && !dowide)
2255 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2256 1, draw_flags);
2257# endif
2258 }
2259 /* Draw a composing char on top of the previous char. */
2260 if (comping)
2261 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002262# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002263 /* Carbon ATSUI autodraws composing char over previous char */
2264 gui_mch_draw_string(gui.row, scol, s + i, cl,
2265 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002266# else
2267 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2268 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 start = i + cl;
2271 }
2272 }
2273 /* The stuff below assumes "len" is the length in screen columns. */
2274 len = scol - col;
2275 }
2276 else
2277# endif
2278 {
2279 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2280# ifdef FEAT_MBYTE
2281 if (enc_dbcs == DBCS_JPNU)
2282 {
2283 int clen = 0;
2284 int i;
2285
2286 /* Get the length in display cells, this can be different from the
2287 * number of bytes for "euc-jp". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002288 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 clen += (*mb_ptr2cells)(s + i);
2290 len = clen;
2291 }
2292# endif
2293 }
2294#endif /* !HAVE_GTK2 */
2295
2296 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2297 gui.col = col + len;
2298
2299 /* May need to invert it when it's part of the selection. */
2300 if (flags & GUI_MON_NOCLEAR)
2301 clip_may_redraw_selection(gui.row, col, len);
2302
2303 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2304 {
2305 /* Invalidate the old physical cursor position if we wrote over it */
2306 if (gui.cursor_row == gui.row
2307 && gui.cursor_col >= col
2308 && gui.cursor_col < col + len)
2309 gui.cursor_is_valid = FALSE;
2310 }
2311
2312#ifdef FEAT_SIGN_ICONS
2313 if (draw_sign)
2314 /* Draw the sign on top of the spaces. */
2315 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2316# ifdef FEAT_NETBEANS_INTG
2317 if (multi_sign)
2318 netbeans_draw_multisign_indicator(gui.row);
2319# endif
2320#endif
2321
2322 return OK;
2323}
2324
2325/*
2326 * Un-draw the cursor. Actually this just redraws the character at the given
2327 * position. The character just before it too, for when it was in bold.
2328 */
2329 void
2330gui_undraw_cursor()
2331{
2332 if (gui.cursor_is_valid)
2333 {
2334#ifdef FEAT_HANGULIN
2335 if (composing_hangul
2336 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2337 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2338 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2339 gui.norm_pixel, gui.back_pixel, 0);
2340 else
2341 {
2342#endif
2343 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2344 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2345 && gui.cursor_col > 0)
2346 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2347 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2348#ifdef FEAT_HANGULIN
2349 if (composing_hangul)
2350 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2351 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2352 }
2353#endif
2354 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2355 * here in case it wasn't needed to undraw it. */
2356 gui.cursor_is_valid = FALSE;
2357 }
2358}
2359
2360 void
2361gui_redraw(x, y, w, h)
2362 int x;
2363 int y;
2364 int w;
2365 int h;
2366{
2367 int row1, col1, row2, col2;
2368
2369 row1 = Y_2_ROW(y);
2370 col1 = X_2_COL(x);
2371 row2 = Y_2_ROW(y + h - 1);
2372 col2 = X_2_COL(x + w - 1);
2373
2374 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2375
2376 /*
2377 * We may need to redraw the cursor, but don't take it upon us to change
2378 * its location after a scroll.
2379 * (maybe be more strict even and test col too?)
2380 * These things may be outside the update/clipping region and reality may
2381 * not reflect Vims internal ideas if these operations are clipped away.
2382 */
2383 if (gui.row == gui.cursor_row)
2384 gui_update_cursor(TRUE, TRUE);
2385}
2386
2387/*
2388 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2389 * from col1 to col2 (inclusive).
2390 * Return TRUE when the character before the first drawn character has
2391 * different attributes (may have to be redrawn too).
2392 */
2393 int
2394gui_redraw_block(row1, col1, row2, col2, flags)
2395 int row1;
2396 int col1;
2397 int row2;
2398 int col2;
2399 int flags; /* flags for gui_outstr_nowrap() */
2400{
2401 int old_row, old_col;
2402 long_u old_hl_mask;
2403 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002404 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 int idx, len;
2406 int back, nback;
2407 int retval = FALSE;
2408#ifdef FEAT_MBYTE
2409 int orig_col1, orig_col2;
2410#endif
2411
2412 /* Don't try to update when ScreenLines is not valid */
2413 if (!screen_cleared || ScreenLines == NULL)
2414 return retval;
2415
2416 /* Don't try to draw outside the shell! */
2417 /* Check everything, strange values may be caused by a big border width */
2418 col1 = check_col(col1);
2419 col2 = check_col(col2);
2420 row1 = check_row(row1);
2421 row2 = check_row(row2);
2422
2423 /* Remember where our cursor was */
2424 old_row = gui.row;
2425 old_col = gui.col;
2426 old_hl_mask = gui.highlight_mask;
2427#ifdef FEAT_MBYTE
2428 orig_col1 = col1;
2429 orig_col2 = col2;
2430#endif
2431
2432 for (gui.row = row1; gui.row <= row2; gui.row++)
2433 {
2434#ifdef FEAT_MBYTE
2435 /* When only half of a double-wide character is in the block, include
2436 * the other half. */
2437 col1 = orig_col1;
2438 col2 = orig_col2;
2439 off = LineOffset[gui.row];
2440 if (enc_dbcs != 0)
2441 {
2442 if (col1 > 0)
2443 col1 -= dbcs_screen_head_off(ScreenLines + off,
2444 ScreenLines + off + col1);
2445 col2 += dbcs_screen_tail_off(ScreenLines + off,
2446 ScreenLines + off + col2);
2447 }
2448 else if (enc_utf8)
2449 {
2450 if (ScreenLines[off + col1] == 0)
2451 --col1;
2452# ifdef HAVE_GTK2
2453 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2454 ++col2;
2455# endif
2456 }
2457#endif
2458 gui.col = col1;
2459 off = LineOffset[gui.row] + gui.col;
2460 len = col2 - col1 + 1;
2461
2462 /* Find how many chars back this highlighting starts, or where a space
2463 * is. Needed for when the bold trick is used */
2464 for (back = 0; back < col1; ++back)
2465 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2466 || ScreenLines[off - 1 - back] == ' ')
2467 break;
2468 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2469 && ScreenLines[off - 1] != ' ');
2470
2471 /* Break it up in strings of characters with the same attributes. */
2472 /* Print UTF-8 characters individually. */
2473 while (len > 0)
2474 {
2475 first_attr = ScreenAttrs[off];
2476 gui.highlight_mask = first_attr;
2477#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2478 if (enc_utf8 && ScreenLinesUC[off] != 0)
2479 {
2480 /* output multi-byte character separately */
2481 nback = gui_screenchar(off, flags,
2482 (guicolor_T)0, (guicolor_T)0, back);
2483 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2484 idx = 2;
2485 else
2486 idx = 1;
2487 }
2488 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2489 {
2490 /* output double-byte, single-width character separately */
2491 nback = gui_screenchar(off, flags,
2492 (guicolor_T)0, (guicolor_T)0, back);
2493 idx = 1;
2494 }
2495 else
2496#endif
2497 {
2498#ifdef HAVE_GTK2
2499 for (idx = 0; idx < len; ++idx)
2500 {
2501 if (enc_utf8 && ScreenLines[off + idx] == 0)
2502 continue; /* skip second half of double-width char */
2503 if (ScreenAttrs[off + idx] != first_attr)
2504 break;
2505 }
2506 /* gui_screenstr() takes care of multibyte chars */
2507 nback = gui_screenstr(off, idx, flags,
2508 (guicolor_T)0, (guicolor_T)0, back);
2509#else
2510 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2511 idx++)
2512 {
2513# ifdef FEAT_MBYTE
2514 /* Stop at a multi-byte Unicode character. */
2515 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2516 break;
2517 if (enc_dbcs == DBCS_JPNU)
2518 {
2519 /* Stop at a double-byte single-width char. */
2520 if (ScreenLines[off + idx] == 0x8e)
2521 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002522 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 + off + idx) == 2)
2524 ++idx; /* skip second byte of double-byte char */
2525 }
2526# endif
2527 }
2528 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2529 (guicolor_T)0, (guicolor_T)0, back);
2530#endif
2531 }
2532 if (nback == FAIL)
2533 {
2534 /* Must back up to start drawing where a bold or italic word
2535 * starts. */
2536 off -= back;
2537 len += back;
2538 gui.col -= back;
2539 }
2540 else
2541 {
2542 off += idx;
2543 len -= idx;
2544 }
2545 back = 0;
2546 }
2547 }
2548
2549 /* Put the cursor back where it was */
2550 gui.row = old_row;
2551 gui.col = old_col;
2552 gui.highlight_mask = old_hl_mask;
2553
2554 return retval;
2555}
2556
2557 static void
2558gui_delete_lines(row, count)
2559 int row;
2560 int count;
2561{
2562 if (count <= 0)
2563 return;
2564
2565 if (row + count > gui.scroll_region_bot)
2566 /* Scrolled out of region, just blank the lines out */
2567 gui_clear_block(row, gui.scroll_region_left,
2568 gui.scroll_region_bot, gui.scroll_region_right);
2569 else
2570 {
2571 gui_mch_delete_lines(row, count);
2572
2573 /* If the cursor was in the deleted lines it's now gone. If the
2574 * cursor was in the scrolled lines adjust its position. */
2575 if (gui.cursor_row >= row
2576 && gui.cursor_col >= gui.scroll_region_left
2577 && gui.cursor_col <= gui.scroll_region_right)
2578 {
2579 if (gui.cursor_row < row + count)
2580 gui.cursor_is_valid = FALSE;
2581 else if (gui.cursor_row <= gui.scroll_region_bot)
2582 gui.cursor_row -= count;
2583 }
2584 }
2585}
2586
2587 static void
2588gui_insert_lines(row, count)
2589 int row;
2590 int count;
2591{
2592 if (count <= 0)
2593 return;
2594
2595 if (row + count > gui.scroll_region_bot)
2596 /* Scrolled out of region, just blank the lines out */
2597 gui_clear_block(row, gui.scroll_region_left,
2598 gui.scroll_region_bot, gui.scroll_region_right);
2599 else
2600 {
2601 gui_mch_insert_lines(row, count);
2602
2603 if (gui.cursor_row >= gui.row
2604 && gui.cursor_col >= gui.scroll_region_left
2605 && gui.cursor_col <= gui.scroll_region_right)
2606 {
2607 if (gui.cursor_row <= gui.scroll_region_bot - count)
2608 gui.cursor_row += count;
2609 else if (gui.cursor_row <= gui.scroll_region_bot)
2610 gui.cursor_is_valid = FALSE;
2611 }
2612 }
2613}
2614
2615/*
2616 * The main GUI input routine. Waits for a character from the keyboard.
2617 * wtime == -1 Wait forever.
2618 * wtime == 0 Don't wait.
2619 * wtime > 0 Wait wtime milliseconds for a character.
2620 * Returns OK if a character was found to be available within the given time,
2621 * or FAIL otherwise.
2622 */
2623 int
2624gui_wait_for_chars(wtime)
2625 long wtime;
2626{
2627 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 /*
2630 * If we're going to wait a bit, update the menus and mouse shape for the
2631 * current State.
2632 */
2633 if (wtime != 0)
2634 {
2635#ifdef FEAT_MENU
2636 gui_update_menus(0);
2637#endif
2638 }
2639
2640 gui_mch_update();
2641 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 /* Before waiting, flush any output to the screen. */
2647 gui_mch_flush();
2648
2649 if (wtime > 0)
2650 {
2651 /* Blink when waiting for a character. Probably only does something
2652 * for showmatch() */
2653 gui_mch_start_blink();
2654 retval = gui_mch_wait_for_chars(wtime);
2655 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 return retval;
2657 }
2658
2659 /*
2660 * While we are waiting indefenitely for a character, blink the cursor.
2661 */
2662 gui_mch_start_blink();
2663
Bram Moolenaar3918c952005-03-15 22:34:55 +00002664 retval = FAIL;
2665 /*
2666 * We may want to trigger the CursorHold event. First wait for
2667 * 'updatetime' and if nothing is typed within that time put the
2668 * K_CURSORHOLD key in the input buffer.
2669 */
2670 if (gui_mch_wait_for_chars(p_ut) == OK)
2671 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002673 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002675 char_u buf[3];
2676
2677 /* Put K_CURSORHOLD in the input buffer. */
2678 buf[0] = CSI;
2679 buf[1] = KS_EXTRA;
2680 buf[2] = (int)KE_CURSORHOLD;
2681 add_to_input_buf(buf, 3);
2682
2683 retval = OK;
2684 }
2685#endif
2686
2687 if (retval == FAIL)
2688 {
2689 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002690 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 gui_mch_stop_blink();
2695 return retval;
2696}
2697
2698/*
2699 * Fill buffer with mouse coordinates encoded for check_termcode().
2700 */
2701 static void
2702fill_mouse_coord(p, col, row)
2703 char_u *p;
2704 int col;
2705 int row;
2706{
2707 p[0] = (char_u)(col / 128 + ' ' + 1);
2708 p[1] = (char_u)(col % 128 + ' ' + 1);
2709 p[2] = (char_u)(row / 128 + ' ' + 1);
2710 p[3] = (char_u)(row % 128 + ' ' + 1);
2711}
2712
2713/*
2714 * Generic mouse support function. Add a mouse event to the input buffer with
2715 * the given properties.
2716 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2717 * MOUSE_X1, MOUSE_X2
2718 * MOUSE_DRAG, or MOUSE_RELEASE.
2719 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2720 * x, y --- Coordinates of mouse in pixels.
2721 * repeated_click --- TRUE if this click comes only a short time after a
2722 * previous click.
2723 * modifiers --- Bit field which may be any of the following modifiers
2724 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2725 * This function will ignore drag events where the mouse has not moved to a new
2726 * character.
2727 */
2728 void
2729gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2730 int button;
2731 int x;
2732 int y;
2733 int repeated_click;
2734 int_u modifiers;
2735{
2736 static int prev_row = 0, prev_col = 0;
2737 static int prev_button = -1;
2738 static int num_clicks = 1;
2739 char_u string[10];
2740 enum key_extra button_char;
2741 int row, col;
2742#ifdef FEAT_CLIPBOARD
2743 int checkfor;
2744 int did_clip = FALSE;
2745#endif
2746
2747 /*
2748 * Scrolling may happen at any time, also while a selection is present.
2749 */
2750 switch (button)
2751 {
2752 case MOUSE_X1:
2753 button_char = KE_X1MOUSE;
2754 goto button_set;
2755 case MOUSE_X2:
2756 button_char = KE_X2MOUSE;
2757 goto button_set;
2758 case MOUSE_4:
2759 button_char = KE_MOUSEDOWN;
2760 goto button_set;
2761 case MOUSE_5:
2762 button_char = KE_MOUSEUP;
2763button_set:
2764 {
2765 /* Don't put events in the input queue now. */
2766 if (hold_gui_events)
2767 return;
2768
2769 string[3] = CSI;
2770 string[4] = KS_EXTRA;
2771 string[5] = (int)button_char;
2772
2773 /* Pass the pointer coordinates of the scroll event so that we
2774 * know which window to scroll. */
2775 row = gui_xy2colrow(x, y, &col);
2776 string[6] = (char_u)(col / 128 + ' ' + 1);
2777 string[7] = (char_u)(col % 128 + ' ' + 1);
2778 string[8] = (char_u)(row / 128 + ' ' + 1);
2779 string[9] = (char_u)(row % 128 + ' ' + 1);
2780
2781 if (modifiers == 0)
2782 add_to_input_buf(string + 3, 7);
2783 else
2784 {
2785 string[0] = CSI;
2786 string[1] = KS_MODIFIER;
2787 string[2] = 0;
2788 if (modifiers & MOUSE_SHIFT)
2789 string[2] |= MOD_MASK_SHIFT;
2790 if (modifiers & MOUSE_CTRL)
2791 string[2] |= MOD_MASK_CTRL;
2792 if (modifiers & MOUSE_ALT)
2793 string[2] |= MOD_MASK_ALT;
2794 add_to_input_buf(string, 10);
2795 }
2796 return;
2797 }
2798 }
2799
2800#ifdef FEAT_CLIPBOARD
2801 /* If a clipboard selection is in progress, handle it */
2802 if (clip_star.state == SELECT_IN_PROGRESS)
2803 {
2804 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2805 return;
2806 }
2807
2808 /* Determine which mouse settings to look for based on the current mode */
2809 switch (get_real_state())
2810 {
2811 case NORMAL_BUSY:
2812 case OP_PENDING:
2813 case NORMAL: checkfor = MOUSE_NORMAL; break;
2814 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002815 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 case REPLACE:
2817 case REPLACE+LANGMAP:
2818#ifdef FEAT_VREPLACE
2819 case VREPLACE:
2820 case VREPLACE+LANGMAP:
2821#endif
2822 case INSERT:
2823 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2824 case ASKMORE:
2825 case HITRETURN: /* At the more- and hit-enter prompt pass the
2826 mouse event for a click on or below the
2827 message line. */
2828 if (Y_2_ROW(y) >= msg_row)
2829 checkfor = MOUSE_NORMAL;
2830 else
2831 checkfor = MOUSE_RETURN;
2832 break;
2833
2834 /*
2835 * On the command line, use the clipboard selection on all lines
2836 * but the command line. But not when pasting.
2837 */
2838 case CMDLINE:
2839 case CMDLINE+LANGMAP:
2840 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2841 checkfor = MOUSE_NONE;
2842 else
2843 checkfor = MOUSE_COMMAND;
2844 break;
2845
2846 default:
2847 checkfor = MOUSE_NONE;
2848 break;
2849 };
2850
2851 /*
2852 * Allow clipboard selection of text on the command line in "normal"
2853 * modes. Don't do this when dragging the status line, or extending a
2854 * Visual selection.
2855 */
2856 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2857 && Y_2_ROW(y) >= topframe->fr_height
2858 && button != MOUSE_DRAG
2859# ifdef FEAT_MOUSESHAPE
2860 && !drag_status_line
2861# ifdef FEAT_VERTSPLIT
2862 && !drag_sep_line
2863# endif
2864# endif
2865 )
2866 checkfor = MOUSE_NONE;
2867
2868 /*
2869 * Use modeless selection when holding CTRL and SHIFT pressed.
2870 */
2871 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2872 checkfor = MOUSE_NONEF;
2873
2874 /*
2875 * In Ex mode, always use modeless selection.
2876 */
2877 if (exmode_active)
2878 checkfor = MOUSE_NONE;
2879
2880 /*
2881 * If the mouse settings say to not use the mouse, use the modeless
2882 * selection. But if Visual is active, assume that only the Visual area
2883 * will be selected.
2884 * Exception: On the command line, both the selection is used and a mouse
2885 * key is send.
2886 */
2887 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2888 {
2889#ifdef FEAT_VISUAL
2890 /* Don't do modeless selection in Visual mode. */
2891 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2892 return;
2893#endif
2894
2895 /*
2896 * When 'mousemodel' is "popup", shift-left is translated to right.
2897 * But not when also using Ctrl.
2898 */
2899 if (mouse_model_popup() && button == MOUSE_LEFT
2900 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2901 {
2902 button = MOUSE_RIGHT;
2903 modifiers &= ~ MOUSE_SHIFT;
2904 }
2905
2906 /* If the selection is done, allow the right button to extend it.
2907 * If the selection is cleared, allow the right button to start it
2908 * from the cursor position. */
2909 if (button == MOUSE_RIGHT)
2910 {
2911 if (clip_star.state == SELECT_CLEARED)
2912 {
2913 if (State & CMDLINE)
2914 {
2915 col = msg_col;
2916 row = msg_row;
2917 }
2918 else
2919 {
2920 col = curwin->w_wcol;
2921 row = curwin->w_wrow + W_WINROW(curwin);
2922 }
2923 clip_start_selection(col, row, FALSE);
2924 }
2925 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2926 repeated_click);
2927 did_clip = TRUE;
2928 }
2929 /* Allow the left button to start the selection */
2930 else if (button ==
2931# ifdef RISCOS
2932 /* Only start a drag on a drag event. Otherwise
2933 * we don't get a release event. */
2934 MOUSE_DRAG
2935# else
2936 MOUSE_LEFT
2937# endif
2938 )
2939 {
2940 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2941 did_clip = TRUE;
2942 }
2943# ifdef RISCOS
2944 else if (button == MOUSE_LEFT)
2945 {
2946 clip_clear_selection();
2947 did_clip = TRUE;
2948 }
2949# endif
2950
2951 /* Always allow pasting */
2952 if (button != MOUSE_MIDDLE)
2953 {
2954 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2955 return;
2956 if (checkfor != MOUSE_COMMAND)
2957 button = MOUSE_LEFT;
2958 }
2959 repeated_click = FALSE;
2960 }
2961
2962 if (clip_star.state != SELECT_CLEARED && !did_clip)
2963 clip_clear_selection();
2964#endif
2965
2966 /* Don't put events in the input queue now. */
2967 if (hold_gui_events)
2968 return;
2969
2970 row = gui_xy2colrow(x, y, &col);
2971
2972 /*
2973 * If we are dragging and the mouse hasn't moved far enough to be on a
2974 * different character, then don't send an event to vim.
2975 */
2976 if (button == MOUSE_DRAG)
2977 {
2978 if (row == prev_row && col == prev_col)
2979 return;
2980 /* Dragging above the window, set "row" to -1 to cause a scroll. */
2981 if (y < 0)
2982 row = -1;
2983 }
2984
2985 /*
2986 * If topline has changed (window scrolled) since the last click, reset
2987 * repeated_click, because we don't want starting Visual mode when
2988 * clicking on a different character in the text.
2989 */
2990 if (curwin->w_topline != gui_prev_topline
2991#ifdef FEAT_DIFF
2992 || curwin->w_topfill != gui_prev_topfill
2993#endif
2994 )
2995 repeated_click = FALSE;
2996
2997 string[0] = CSI; /* this sequence is recognized by check_termcode() */
2998 string[1] = KS_MOUSE;
2999 string[2] = KE_FILLER;
3000 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3001 {
3002 if (repeated_click)
3003 {
3004 /*
3005 * Handle multiple clicks. They only count if the mouse is still
3006 * pointing at the same character.
3007 */
3008 if (button != prev_button || row != prev_row || col != prev_col)
3009 num_clicks = 1;
3010 else if (++num_clicks > 4)
3011 num_clicks = 1;
3012 }
3013 else
3014 num_clicks = 1;
3015 prev_button = button;
3016 gui_prev_topline = curwin->w_topline;
3017#ifdef FEAT_DIFF
3018 gui_prev_topfill = curwin->w_topfill;
3019#endif
3020
3021 string[3] = (char_u)(button | 0x20);
3022 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3023 }
3024 else
3025 string[3] = (char_u)button;
3026
3027 string[3] |= modifiers;
3028 fill_mouse_coord(string + 4, col, row);
3029 add_to_input_buf(string, 8);
3030
3031 if (row < 0)
3032 prev_row = 0;
3033 else
3034 prev_row = row;
3035 prev_col = col;
3036
3037 /*
3038 * We need to make sure this is cleared since Athena doesn't tell us when
3039 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3040 */
3041#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3042 gui.dragged_sb = SBAR_NONE;
3043#endif
3044}
3045
3046/*
3047 * Convert x and y coordinate to column and row in text window.
3048 * Corrects for multi-byte character.
3049 * returns column in "*colp" and row as return value;
3050 */
3051 int
3052gui_xy2colrow(x, y, colp)
3053 int x;
3054 int y;
3055 int *colp;
3056{
3057 int col = check_col(X_2_COL(x));
3058 int row = check_row(Y_2_ROW(y));
3059
3060#ifdef FEAT_MBYTE
3061 *colp = mb_fix_col(col, row);
3062#else
3063 *colp = col;
3064#endif
3065 return row;
3066}
3067
3068#if defined(FEAT_MENU) || defined(PROTO)
3069/*
3070 * Callback function for when a menu entry has been selected.
3071 */
3072 void
3073gui_menu_cb(menu)
3074 vimmenu_T *menu;
3075{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003076 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 /* Don't put events in the input queue now. */
3079 if (hold_gui_events)
3080 return;
3081
3082 bytes[0] = CSI;
3083 bytes[1] = KS_MENU;
3084 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003085 add_to_input_buf(bytes, 3);
3086 add_long_to_buf((long_u)menu, bytes);
3087 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089#endif
3090
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003091static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093/*
3094 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003095 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * in p_go.
3097 */
3098/*ARGSUSED*/
3099 void
3100gui_init_which_components(oldval)
3101 char_u *oldval;
3102{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#ifdef FEAT_MENU
3104 static int prev_menu_is_active = -1;
3105#endif
3106#ifdef FEAT_TOOLBAR
3107 static int prev_toolbar = -1;
3108 int using_toolbar = FALSE;
3109#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003110#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003111 int using_tabline;
3112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#ifdef FEAT_FOOTER
3114 static int prev_footer = -1;
3115 int using_footer = FALSE;
3116#endif
3117#if defined(FEAT_MENU) && !defined(WIN16)
3118 static int prev_tearoff = -1;
3119 int using_tearoff = FALSE;
3120#endif
3121
3122 char_u *p;
3123 int i;
3124#ifdef FEAT_MENU
3125 int grey_old, grey_new;
3126 char_u *temp;
3127#endif
3128 win_T *wp;
3129 int need_set_size;
3130 int fix_size;
3131
3132#ifdef FEAT_MENU
3133 if (oldval != NULL && gui.in_use)
3134 {
3135 /*
3136 * Check if the menu's go from grey to non-grey or vise versa.
3137 */
3138 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3139 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3140 if (grey_old != grey_new)
3141 {
3142 temp = p_go;
3143 p_go = oldval;
3144 gui_update_menus(MENU_ALL_MODES);
3145 p_go = temp;
3146 }
3147 }
3148 gui.menu_is_active = FALSE;
3149#endif
3150
3151 for (i = 0; i < 3; i++)
3152 gui.which_scrollbars[i] = FALSE;
3153 for (p = p_go; *p; p++)
3154 switch (*p)
3155 {
3156 case GO_LEFT:
3157 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3158 break;
3159 case GO_RIGHT:
3160 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3161 break;
3162#ifdef FEAT_VERTSPLIT
3163 case GO_VLEFT:
3164 if (win_hasvertsplit())
3165 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3166 break;
3167 case GO_VRIGHT:
3168 if (win_hasvertsplit())
3169 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3170 break;
3171#endif
3172 case GO_BOT:
3173 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3174 break;
3175#ifdef FEAT_MENU
3176 case GO_MENUS:
3177 gui.menu_is_active = TRUE;
3178 break;
3179#endif
3180 case GO_GREY:
3181 /* make menu's have grey items, ignored here */
3182 break;
3183#ifdef FEAT_TOOLBAR
3184 case GO_TOOLBAR:
3185 using_toolbar = TRUE;
3186 break;
3187#endif
3188#ifdef FEAT_FOOTER
3189 case GO_FOOTER:
3190 using_footer = TRUE;
3191 break;
3192#endif
3193 case GO_TEAROFF:
3194#if defined(FEAT_MENU) && !defined(WIN16)
3195 using_tearoff = TRUE;
3196#endif
3197 break;
3198 default:
3199 /* Ignore options that are not supported */
3200 break;
3201 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (gui.in_use)
3204 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003205 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003207
3208#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003209 /* Update the GUI tab line, it may appear or disappear. This may
3210 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003211 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003212 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003213 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003214 /* We don't want a resize event change "Rows" here, save and
3215 * restore it. Resizing is handled below. */
3216 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003217 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003218 Rows = i;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003219 need_set_size = RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003220 if (using_tabline)
3221 fix_size = TRUE;
3222 if (!gui_use_tabline())
3223 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3224 }
3225#endif
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 for (i = 0; i < 3; i++)
3228 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003229 /* The scrollbar needs to be updated when it is shown/unshown and
3230 * when switching tab pages. But the size only changes when it's
3231 * shown/unshown. Thus we need two places to remember whether a
3232 * scrollbar is there or not. */
3233 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003234#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003235 || gui.which_scrollbars[i]
3236 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003237#endif
3238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 if (i == SBAR_BOTTOM)
3241 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3242 gui.which_scrollbars[i]);
3243 else
3244 {
3245 FOR_ALL_WINDOWS(wp)
3246 {
3247 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3248 }
3249 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003250 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3251 {
3252 if (i == SBAR_BOTTOM)
3253 need_set_size = RESIZE_VERT;
3254 else
3255 need_set_size = RESIZE_HOR;
3256 if (gui.which_scrollbars[i])
3257 fix_size = TRUE;
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003260#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003261 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003262#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003263 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265
3266#ifdef FEAT_MENU
3267 if (gui.menu_is_active != prev_menu_is_active)
3268 {
3269 /* We don't want a resize event change "Rows" here, save and
3270 * restore it. Resizing is handled below. */
3271 i = Rows;
3272 gui_mch_enable_menu(gui.menu_is_active);
3273 Rows = i;
3274 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003275 need_set_size = RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (gui.menu_is_active)
3277 fix_size = TRUE;
3278 }
3279#endif
3280
3281#ifdef FEAT_TOOLBAR
3282 if (using_toolbar != prev_toolbar)
3283 {
3284 gui_mch_show_toolbar(using_toolbar);
3285 prev_toolbar = using_toolbar;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003286 need_set_size = RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (using_toolbar)
3288 fix_size = TRUE;
3289 }
3290#endif
3291#ifdef FEAT_FOOTER
3292 if (using_footer != prev_footer)
3293 {
3294 gui_mch_enable_footer(using_footer);
3295 prev_footer = using_footer;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003296 need_set_size = RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (using_footer)
3298 fix_size = TRUE;
3299 }
3300#endif
3301#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3302 if (using_tearoff != prev_tearoff)
3303 {
3304 gui_mch_toggle_tearoffs(using_tearoff);
3305 prev_tearoff = using_tearoff;
3306 }
3307#endif
3308 if (need_set_size)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003309 {
3310#ifdef FEAT_GUI_GTK
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003311 long c = Columns;
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /* Adjust the size of the window to make the text area keep the
3314 * same size and to avoid that part of our window is off-screen
3315 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003316 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003317
3318#ifdef FEAT_GUI_GTK
3319 /* GTK has the annoying habit of sending us resize events when
3320 * changing the window size ourselves. This mostly happens when
3321 * waiting for a character to arrive, quite unpredictably, and may
3322 * change Columns and Rows when we don't want it. Wait for a
3323 * character here to avoid this effect.
3324 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003325 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003326 * Don't do this while starting up though.
3327 * And don't change Rows, it may have be reduced intentionally
3328 * when adding menu/toolbar/tabline. */
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003329 if (!gui.starting)
3330 (void)char_avail();
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003331 Columns = c;
3332#endif
3333 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003334#ifdef FEAT_WINDOWS
3335 /* When the console tabline appears or disappears the window positions
3336 * change. */
3337 if (firstwin->w_winrow != tabline_height())
3338 shell_new_rows(); /* recompute window positions and heights */
3339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341}
3342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003343#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3344/*
3345 * Return TRUE if the GUI is taking care of the tabline.
3346 * It may still be hidden if 'showtabline' is zero.
3347 */
3348 int
3349gui_use_tabline()
3350{
3351 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3352}
3353
3354/*
3355 * Return TRUE if the GUI is showing the tabline.
3356 * This uses 'showtabline'.
3357 */
3358 static int
3359gui_has_tabline()
3360{
3361 if (!gui_use_tabline()
3362 || p_stal == 0
3363 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3364 return FALSE;
3365 return TRUE;
3366}
3367
3368/*
3369 * Update the tabline.
3370 * This may display/undisplay the tabline and update the labels.
3371 */
3372 void
3373gui_update_tabline()
3374{
3375 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003376 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003377
3378 if (!gui.starting && starting == 0)
3379 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003380 if (!showit != !shown)
3381 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003382 if (showit != 0)
3383 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003384
3385 /* When the tabs change from hidden to shown or from shown to
3386 * hidden the size of the text area should remain the same. */
3387 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003388 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003389 }
3390}
3391
3392/*
3393 * Get the label for tab page "tp" into NameBuff[].
3394 */
3395 void
3396get_tabline_label(tp)
3397 tabpage_T *tp;
3398{
3399 int modified = FALSE;
3400 char_u buf[40];
3401 int wincount;
3402 win_T *wp;
3403
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003404 /* Use 'guitablabel' if it's set. */
3405 if (*p_gtl != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003406 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003407 int use_sandbox = FALSE;
3408 int save_called_emsg = called_emsg;
3409 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003410 tabpage_T *save_curtab;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003411
3412 called_emsg = FALSE;
3413
3414 printer_page_num = tabpage_index(tp);
3415# ifdef FEAT_EVAL
3416 set_vim_var_nr(VV_LNUM, printer_page_num);
3417 use_sandbox = was_set_insecurely((char_u *)"guitablabel", 0);
3418# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003419 /* It's almost as going to the tabpage, but without autocommands. */
3420 curtab->tp_firstwin = firstwin;
3421 curtab->tp_lastwin = lastwin;
3422 curtab->tp_curwin = curwin;
3423 save_curtab = curtab;
3424 curtab = tp;
3425 topframe = curtab->tp_topframe;
3426 firstwin = curtab->tp_firstwin;
3427 lastwin = curtab->tp_lastwin;
3428 curwin = curtab->tp_curwin;
3429 curbuf = curwin->w_buffer;
3430
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003431 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003432 build_stl_str_hl(curwin, res, MAXPATHL, p_gtl, use_sandbox,
3433 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003434 STRCPY(NameBuff, res);
3435
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003436 /* Back to the original curtab. */
3437 curtab = save_curtab;
3438 topframe = curtab->tp_topframe;
3439 firstwin = curtab->tp_firstwin;
3440 lastwin = curtab->tp_lastwin;
3441 curwin = curtab->tp_curwin;
3442 curbuf = curwin->w_buffer;
3443
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003444 if (called_emsg)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003445 set_string_option_direct((char_u *)"guitablabel", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003446 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003447 called_emsg |= save_called_emsg;
3448 }
3449 else
3450 {
3451 /* Get the buffer name into NameBuff[] */
3452 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3453
3454 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3455 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3456 if (bufIsChanged(wp->w_buffer))
3457 modified = TRUE;
3458 if (modified || wincount > 1)
3459 {
3460 if (wincount > 1)
3461 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3462 else
3463 buf[0] = NUL;
3464 if (modified)
3465 STRCAT(buf, "+");
3466 STRCAT(buf, " ");
3467 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3468 mch_memmove(NameBuff, buf, STRLEN(buf));
3469 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003470 }
3471}
3472
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003473/*
3474 * Send the event for clicking to select tab page "nr".
3475 * Returns TRUE if it was done, FALSE when skipped because we are already at
3476 * that tab page.
3477 */
3478 int
3479send_tabline_event(nr)
3480 int nr;
3481{
3482 char_u string[3];
3483
3484 if (nr == tabpage_index(curtab))
3485 return FALSE;
3486 string[0] = CSI;
3487 string[1] = KS_TABLINE;
3488 string[2] = KE_FILLER;
3489 add_to_input_buf(string, 3);
3490 string[0] = nr;
3491 add_to_input_buf_csi(string, 1);
3492 return TRUE;
3493}
3494
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497/*
3498 * Scrollbar stuff:
3499 */
3500
Bram Moolenaare45828b2006-02-15 22:12:56 +00003501#if defined(FEAT_WINDOWS) || defined(PROTO)
3502/*
3503 * Remove all scrollbars. Used before switching to another tab page.
3504 */
3505 void
3506gui_remove_scrollbars()
3507{
3508 int i;
3509 win_T *wp;
3510
3511 for (i = 0; i < 3; i++)
3512 {
3513 if (i == SBAR_BOTTOM)
3514 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3515 else
3516 {
3517 FOR_ALL_WINDOWS(wp)
3518 {
3519 gui_do_scrollbar(wp, i, FALSE);
3520 }
3521 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003522 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003523 }
3524}
3525#endif
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 void
3528gui_create_scrollbar(sb, type, wp)
3529 scrollbar_T *sb;
3530 int type;
3531 win_T *wp;
3532{
3533 static int sbar_ident = 0;
3534
3535 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3536 sb->wp = wp;
3537 sb->type = type;
3538 sb->value = 0;
3539#ifdef FEAT_GUI_ATHENA
3540 sb->pixval = 0;
3541#endif
3542 sb->size = 1;
3543 sb->max = 1;
3544 sb->top = 0;
3545 sb->height = 0;
3546#ifdef FEAT_VERTSPLIT
3547 sb->width = 0;
3548#endif
3549 sb->status_height = 0;
3550 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3551}
3552
3553/*
3554 * Find the scrollbar with the given index.
3555 */
3556 scrollbar_T *
3557gui_find_scrollbar(ident)
3558 long ident;
3559{
3560 win_T *wp;
3561
3562 if (gui.bottom_sbar.ident == ident)
3563 return &gui.bottom_sbar;
3564 FOR_ALL_WINDOWS(wp)
3565 {
3566 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3567 return &wp->w_scrollbars[SBAR_LEFT];
3568 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3569 return &wp->w_scrollbars[SBAR_RIGHT];
3570 }
3571 return NULL;
3572}
3573
3574/*
3575 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3576 *
3577 * For Win32, Macintosh and GTK+ 2:
3578 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3579 * you stop dragging the scrollbar. We get here each time the scrollbar is
3580 * dragged another pixel, but as far as the rest of vim goes, it thinks
3581 * we're just hanging in the call to DispatchMessage() in
3582 * process_message(). The DispatchMessage() call that hangs was passed a
3583 * mouse button click event in the scrollbar window. -- webb.
3584 *
3585 * Solution: Do the scrolling right here. But only when allowed.
3586 * Ignore the scrollbars while executing an external command or when there
3587 * are still characters to be processed.
3588 */
3589 void
3590gui_drag_scrollbar(sb, value, still_dragging)
3591 scrollbar_T *sb;
3592 long value;
3593 int still_dragging;
3594{
3595#ifdef FEAT_WINDOWS
3596 win_T *wp;
3597#endif
3598 int sb_num;
3599#ifdef USE_ON_FLY_SCROLL
3600 colnr_T old_leftcol = curwin->w_leftcol;
3601# ifdef FEAT_SCROLLBIND
3602 linenr_T old_topline = curwin->w_topline;
3603# endif
3604# ifdef FEAT_DIFF
3605 int old_topfill = curwin->w_topfill;
3606# endif
3607#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003608 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 int byte_count;
3610#endif
3611
3612 if (sb == NULL)
3613 return;
3614
3615 /* Don't put events in the input queue now. */
3616 if (hold_gui_events)
3617 return;
3618
3619#ifdef FEAT_CMDWIN
3620 if (cmdwin_type != 0 && sb->wp != curwin)
3621 return;
3622#endif
3623
3624 if (still_dragging)
3625 {
3626 if (sb->wp == NULL)
3627 gui.dragged_sb = SBAR_BOTTOM;
3628 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3629 gui.dragged_sb = SBAR_LEFT;
3630 else
3631 gui.dragged_sb = SBAR_RIGHT;
3632 gui.dragged_wp = sb->wp;
3633 }
3634 else
3635 {
3636 gui.dragged_sb = SBAR_NONE;
3637#ifdef HAVE_GTK2
3638 /* Keep the "dragged_wp" value until after the scrolling, for when the
3639 * moust button is released. GTK2 doesn't send the button-up event. */
3640 gui.dragged_wp = NULL;
3641#endif
3642 }
3643
3644 /* Vertical sbar info is kept in the first sbar (the left one) */
3645 if (sb->wp != NULL)
3646 sb = &sb->wp->w_scrollbars[0];
3647
3648 /*
3649 * Check validity of value
3650 */
3651 if (value < 0)
3652 value = 0;
3653#ifdef SCROLL_PAST_END
3654 else if (value > sb->max)
3655 value = sb->max;
3656#else
3657 if (value > sb->max - sb->size + 1)
3658 value = sb->max - sb->size + 1;
3659#endif
3660
3661 sb->value = value;
3662
3663#ifdef USE_ON_FLY_SCROLL
3664 /* When not allowed to do the scrolling right now, return. */
3665 if (dont_scroll || input_available())
3666 return;
3667#endif
3668
3669#ifdef FEAT_RIGHTLEFT
3670 if (sb->wp == NULL && curwin->w_p_rl)
3671 {
3672 value = sb->max + 1 - sb->size - value;
3673 if (value < 0)
3674 value = 0;
3675 }
3676#endif
3677
3678 if (sb->wp != NULL) /* vertical scrollbar */
3679 {
3680 sb_num = 0;
3681#ifdef FEAT_WINDOWS
3682 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3683 sb_num++;
3684 if (wp == NULL)
3685 return;
3686#else
3687 if (sb->wp != curwin)
3688 return;
3689#endif
3690
3691#ifdef USE_ON_FLY_SCROLL
3692 current_scrollbar = sb_num;
3693 scrollbar_value = value;
3694 if (State & NORMAL)
3695 {
3696 gui_do_scroll();
3697 setcursor();
3698 }
3699 else if (State & INSERT)
3700 {
3701 ins_scroll();
3702 setcursor();
3703 }
3704 else if (State & CMDLINE)
3705 {
3706 if (msg_scrolled == 0)
3707 {
3708 gui_do_scroll();
3709 redrawcmdline();
3710 }
3711 }
3712# ifdef FEAT_FOLDING
3713 /* Value may have been changed for closed fold. */
3714 sb->value = sb->wp->w_topline - 1;
3715# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003716
3717 /* When dragging one scrollbar and there is another one at the other
3718 * side move the thumb of that one too. */
3719 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3720 gui_mch_set_scrollbar_thumb(
3721 &sb->wp->w_scrollbars[
3722 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3723 ? SBAR_LEFT : SBAR_RIGHT],
3724 sb->value, sb->size, sb->max);
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726#else
3727 bytes[0] = CSI;
3728 bytes[1] = KS_VER_SCROLLBAR;
3729 bytes[2] = KE_FILLER;
3730 bytes[3] = (char_u)sb_num;
3731 byte_count = 4;
3732#endif
3733 }
3734 else
3735 {
3736#ifdef USE_ON_FLY_SCROLL
3737 scrollbar_value = value;
3738
3739 if (State & NORMAL)
3740 gui_do_horiz_scroll();
3741 else if (State & INSERT)
3742 ins_horscroll();
3743 else if (State & CMDLINE)
3744 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003745 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 gui_do_horiz_scroll();
3748 redrawcmdline();
3749 }
3750 }
3751 if (old_leftcol != curwin->w_leftcol)
3752 {
3753 updateWindow(curwin); /* update window, status and cmdline */
3754 setcursor();
3755 }
3756#else
3757 bytes[0] = CSI;
3758 bytes[1] = KS_HOR_SCROLLBAR;
3759 bytes[2] = KE_FILLER;
3760 byte_count = 3;
3761#endif
3762 }
3763
3764#ifdef USE_ON_FLY_SCROLL
3765# ifdef FEAT_SCROLLBIND
3766 /*
3767 * synchronize other windows, as necessary according to 'scrollbind'
3768 */
3769 if (curwin->w_p_scb
3770 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3771 || (sb->wp == curwin && (curwin->w_topline != old_topline
3772# ifdef FEAT_DIFF
3773 || curwin->w_topfill != old_topfill
3774# endif
3775 ))))
3776 {
3777 do_check_scrollbind(TRUE);
3778 /* need to update the window right here */
3779 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3780 if (wp->w_redr_type > 0)
3781 updateWindow(wp);
3782 setcursor();
3783 }
3784# endif
3785 out_flush();
3786 gui_update_cursor(FALSE, TRUE);
3787#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003788 add_to_input_buf(bytes, byte_count);
3789 add_long_to_buf((long_u)value, bytes);
3790 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791#endif
3792}
3793
3794/*
3795 * Scrollbar stuff:
3796 */
3797
3798 void
3799gui_update_scrollbars(force)
3800 int force; /* Force all scrollbars to get updated */
3801{
3802 win_T *wp;
3803 scrollbar_T *sb;
3804 long val, size, max; /* need 32 bits here */
3805 int which_sb;
3806 int h, y;
3807#ifdef FEAT_VERTSPLIT
3808 static win_T *prev_curwin = NULL;
3809#endif
3810
3811 /* Update the horizontal scrollbar */
3812 gui_update_horiz_scrollbar(force);
3813
3814#ifndef WIN3264
3815 /* Return straight away if there is neither a left nor right scrollbar.
3816 * On MS-Windows this is required anyway for scrollwheel messages. */
3817 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3818 return;
3819#endif
3820
3821 /*
3822 * Don't want to update a scrollbar while we're dragging it. But if we
3823 * have both a left and right scrollbar, and we drag one of them, we still
3824 * need to update the other one.
3825 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003826 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3827 && gui.which_scrollbars[SBAR_LEFT]
3828 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
3830 /*
3831 * If we have two scrollbars and one of them is being dragged, just
3832 * copy the scrollbar position from the dragged one to the other one.
3833 */
3834 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3835 if (gui.dragged_wp != NULL)
3836 gui_mch_set_scrollbar_thumb(
3837 &gui.dragged_wp->w_scrollbars[which_sb],
3838 gui.dragged_wp->w_scrollbars[0].value,
3839 gui.dragged_wp->w_scrollbars[0].size,
3840 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842
3843 /* avoid that moving components around generates events */
3844 ++hold_gui_events;
3845
3846 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3847 {
3848 if (wp->w_buffer == NULL) /* just in case */
3849 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003850 /* Skip a scrollbar that is being dragged. */
3851 if (!force && (gui.dragged_sb == SBAR_LEFT
3852 || gui.dragged_sb == SBAR_RIGHT)
3853 && gui.dragged_wp == wp)
3854 continue;
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#ifdef SCROLL_PAST_END
3857 max = wp->w_buffer->b_ml.ml_line_count - 1;
3858#else
3859 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3860#endif
3861 if (max < 0) /* empty buffer */
3862 max = 0;
3863 val = wp->w_topline - 1;
3864 size = wp->w_height;
3865#ifdef SCROLL_PAST_END
3866 if (val > max) /* just in case */
3867 val = max;
3868#else
3869 if (size > max + 1) /* just in case */
3870 size = max + 1;
3871 if (val > max - size + 1)
3872 val = max - size + 1;
3873#endif
3874 if (val < 0) /* minimal value is 0 */
3875 val = 0;
3876
3877 /*
3878 * Scrollbar at index 0 (the left one) contains all the information.
3879 * It would be the same info for left and right so we just store it for
3880 * one of them.
3881 */
3882 sb = &wp->w_scrollbars[0];
3883
3884 /*
3885 * Note: no check for valid w_botline. If it's not valid the
3886 * scrollbars will be updated later anyway.
3887 */
3888 if (size < 1 || wp->w_botline - 2 > max)
3889 {
3890 /*
3891 * This can happen during changing files. Just don't update the
3892 * scrollbar for now.
3893 */
3894 sb->height = 0; /* Force update next time */
3895 if (gui.which_scrollbars[SBAR_LEFT])
3896 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3897 if (gui.which_scrollbars[SBAR_RIGHT])
3898 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3899 continue;
3900 }
3901 if (force || sb->height != wp->w_height
3902#ifdef FEAT_WINDOWS
3903 || sb->top != wp->w_winrow
3904 || sb->status_height != wp->w_status_height
3905# ifdef FEAT_VERTSPLIT
3906 || sb->width != wp->w_width
3907 || prev_curwin != curwin
3908# endif
3909#endif
3910 )
3911 {
3912 /* Height, width or position of scrollbar has changed. For
3913 * vertical split: curwin changed. */
3914 sb->height = wp->w_height;
3915#ifdef FEAT_WINDOWS
3916 sb->top = wp->w_winrow;
3917 sb->status_height = wp->w_status_height;
3918# ifdef FEAT_VERTSPLIT
3919 sb->width = wp->w_width;
3920# endif
3921#endif
3922
3923 /* Calculate height and position in pixels */
3924 h = (sb->height + sb->status_height) * gui.char_height;
3925 y = sb->top * gui.char_height + gui.border_offset;
3926#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
3927 if (gui.menu_is_active)
3928 y += gui.menu_height;
3929#endif
3930
3931#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
3932 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
3933# ifdef FEAT_GUI_ATHENA
3934 y += gui.toolbar_height;
3935# else
3936# ifdef FEAT_GUI_MSWIN
3937 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
3938# endif
3939# endif
3940#endif
3941
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003942#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
3943 if (gui_has_tabline())
3944 y += TABLINE_HEIGHT;
3945#endif
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947#ifdef FEAT_WINDOWS
3948 if (wp->w_winrow == 0)
3949#endif
3950 {
3951 /* Height of top scrollbar includes width of top border */
3952 h += gui.border_offset;
3953 y -= gui.border_offset;
3954 }
3955 if (gui.which_scrollbars[SBAR_LEFT])
3956 {
3957 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
3958 gui.left_sbar_x, y,
3959 gui.scrollbar_width, h);
3960 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
3961 }
3962 if (gui.which_scrollbars[SBAR_RIGHT])
3963 {
3964 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
3965 gui.right_sbar_x, y,
3966 gui.scrollbar_width, h);
3967 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
3968 }
3969 }
3970
3971 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
3972 * checking if the thumb moved at least a pixel. Only do this for
3973 * Athena, most other GUIs require the update anyway to make the
3974 * arrows work. */
3975#ifdef FEAT_GUI_ATHENA
3976 if (max == 0)
3977 y = 0;
3978 else
3979 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
3980 if (force || sb->pixval != y || sb->size != size || sb->max != max)
3981#else
3982 if (force || sb->value != val || sb->size != size || sb->max != max)
3983#endif
3984 {
3985 /* Thumb of scrollbar has moved */
3986 sb->value = val;
3987#ifdef FEAT_GUI_ATHENA
3988 sb->pixval = y;
3989#endif
3990 sb->size = size;
3991 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003992 if (gui.which_scrollbars[SBAR_LEFT]
3993 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
3995 val, size, max);
3996 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003997 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
3999 val, size, max);
4000 }
4001 }
4002#ifdef FEAT_VERTSPLIT
4003 prev_curwin = curwin;
4004#endif
4005 --hold_gui_events;
4006}
4007
4008/*
4009 * Enable or disable a scrollbar.
4010 * Check for scrollbars for vertically split windows which are not enabled
4011 * sometimes.
4012 */
4013 static void
4014gui_do_scrollbar(wp, which, enable)
4015 win_T *wp;
4016 int which; /* SBAR_LEFT or SBAR_RIGHT */
4017 int enable; /* TRUE to enable scrollbar */
4018{
4019#ifdef FEAT_VERTSPLIT
4020 int midcol = curwin->w_wincol + curwin->w_width / 2;
4021 int has_midcol = (wp->w_wincol <= midcol
4022 && wp->w_wincol + wp->w_width >= midcol);
4023
4024 /* Only enable scrollbars that contain the middle column of the current
4025 * window. */
4026 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4027 {
4028 /* Scrollbars only on one side. Don't enable scrollbars that don't
4029 * contain the middle column of the current window. */
4030 if (!has_midcol)
4031 enable = FALSE;
4032 }
4033 else
4034 {
4035 /* Scrollbars on both sides. Don't enable scrollbars that neither
4036 * contain the middle column of the current window nor are on the far
4037 * side. */
4038 if (midcol > Columns / 2)
4039 {
4040 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4041 enable = FALSE;
4042 }
4043 else
4044 {
4045 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4046 : !has_midcol)
4047 enable = FALSE;
4048 }
4049 }
4050#endif
4051 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4052}
4053
4054/*
4055 * Scroll a window according to the values set in the globals current_scrollbar
4056 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4057 * or FALSE otherwise.
4058 */
4059 int
4060gui_do_scroll()
4061{
4062 win_T *wp, *save_wp;
4063 int i;
4064 long nlines;
4065 pos_T old_cursor;
4066 linenr_T old_topline;
4067#ifdef FEAT_DIFF
4068 int old_topfill;
4069#endif
4070
4071 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4072 if (wp == NULL)
4073 break;
4074 if (wp == NULL)
4075 /* Couldn't find window */
4076 return FALSE;
4077
4078 /*
4079 * Compute number of lines to scroll. If zero, nothing to do.
4080 */
4081 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4082 if (nlines == 0)
4083 return FALSE;
4084
4085 save_wp = curwin;
4086 old_topline = wp->w_topline;
4087#ifdef FEAT_DIFF
4088 old_topfill = wp->w_topfill;
4089#endif
4090 old_cursor = wp->w_cursor;
4091 curwin = wp;
4092 curbuf = wp->w_buffer;
4093 if (nlines < 0)
4094 scrolldown(-nlines, gui.dragged_wp == NULL);
4095 else
4096 scrollup(nlines, gui.dragged_wp == NULL);
4097 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4098 * the mouse-up event already, but we still want it to behave like when
4099 * dragging. But not the next click in an arrow. */
4100 if (gui.dragged_sb == SBAR_NONE)
4101 gui.dragged_wp = NULL;
4102
4103 if (old_topline != wp->w_topline
4104#ifdef FEAT_DIFF
4105 || old_topfill != wp->w_topfill
4106#endif
4107 )
4108 {
4109 if (p_so != 0)
4110 {
4111 cursor_correct(); /* fix window for 'so' */
4112 update_topline(); /* avoid up/down jump */
4113 }
4114 if (old_cursor.lnum != wp->w_cursor.lnum)
4115 coladvance(wp->w_curswant);
4116#ifdef FEAT_SCROLLBIND
4117 wp->w_scbind_pos = wp->w_topline;
4118#endif
4119 }
4120
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004121 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4122 validate_cursor();
4123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 curwin = save_wp;
4125 curbuf = save_wp->w_buffer;
4126
4127 /*
4128 * Don't call updateWindow() when nothing has changed (it will overwrite
4129 * the status line!).
4130 */
4131 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004132 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#ifdef FEAT_DIFF
4134 || old_topfill != wp->w_topfill
4135#endif
4136 )
4137 {
4138 redraw_win_later(wp, VALID);
4139 updateWindow(wp); /* update window, status line, and cmdline */
4140 }
4141
4142 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4143}
4144
4145
4146/*
4147 * Horizontal scrollbar stuff:
4148 */
4149
4150/*
4151 * Return length of line "lnum" for horizontal scrolling.
4152 */
4153 static colnr_T
4154scroll_line_len(lnum)
4155 linenr_T lnum;
4156{
4157 char_u *p;
4158 colnr_T col;
4159 int w;
4160
4161 p = ml_get(lnum);
4162 col = 0;
4163 if (*p != NUL)
4164 for (;;)
4165 {
4166 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004167 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (*p == NUL) /* don't count the last character */
4169 break;
4170 col += w;
4171 }
4172 return col;
4173}
4174
4175/* Remember which line is currently the longest, so that we don't have to
4176 * search for it when scrolling horizontally. */
4177static linenr_T longest_lnum = 0;
4178
4179 static void
4180gui_update_horiz_scrollbar(force)
4181 int force;
4182{
4183 long value, size, max; /* need 32 bit ints here */
4184
4185 if (!gui.which_scrollbars[SBAR_BOTTOM])
4186 return;
4187
4188 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4189 return;
4190
4191 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4192 return;
4193
4194 /*
4195 * It is possible for the cursor to be invalid if we're in the middle of
4196 * something (like changing files). If so, don't do anything for now.
4197 */
4198 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4199 {
4200 gui.bottom_sbar.value = -1;
4201 return;
4202 }
4203
4204 size = W_WIDTH(curwin);
4205 if (curwin->w_p_wrap)
4206 {
4207 value = 0;
4208#ifdef SCROLL_PAST_END
4209 max = 0;
4210#else
4211 max = W_WIDTH(curwin) - 1;
4212#endif
4213 }
4214 else
4215 {
4216 value = curwin->w_leftcol;
4217
4218 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4219 * line numbers, topline and botline can be invalid when displaying is
4220 * postponed. */
4221 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4222 && curwin->w_topline <= curwin->w_cursor.lnum
4223 && curwin->w_botline > curwin->w_cursor.lnum
4224 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4225 {
4226 linenr_T lnum;
4227 colnr_T n;
4228
4229 /* Use maximum of all visible lines. Remember the lnum of the
4230 * longest line, clostest to the cursor line. Used when scrolling
4231 * below. */
4232 max = 0;
4233 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4234 {
4235 n = scroll_line_len(lnum);
4236 if (n > (colnr_T)max)
4237 {
4238 max = n;
4239 longest_lnum = lnum;
4240 }
4241 else if (n == (colnr_T)max
4242 && abs((int)(lnum - curwin->w_cursor.lnum))
4243 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4244 longest_lnum = lnum;
4245 }
4246 }
4247 else
4248 /* Use cursor line only. */
4249 max = scroll_line_len(curwin->w_cursor.lnum);
4250#ifdef FEAT_VIRTUALEDIT
4251 if (virtual_active())
4252 {
4253 /* May move the cursor even further to the right. */
4254 if (curwin->w_virtcol >= (colnr_T)max)
4255 max = curwin->w_virtcol;
4256 }
4257#endif
4258
4259#ifndef SCROLL_PAST_END
4260 max += W_WIDTH(curwin) - 1;
4261#endif
4262 /* The line number isn't scrolled, thus there is less space when
4263 * 'number' is set (also for 'foldcolumn'). */
4264 size -= curwin_col_off();
4265#ifndef SCROLL_PAST_END
4266 max -= curwin_col_off();
4267#endif
4268 }
4269
4270#ifndef SCROLL_PAST_END
4271 if (value > max - size + 1)
4272 value = max - size + 1; /* limit the value to allowable range */
4273#endif
4274
4275#ifdef FEAT_RIGHTLEFT
4276 if (curwin->w_p_rl)
4277 {
4278 value = max + 1 - size - value;
4279 if (value < 0)
4280 {
4281 size += value;
4282 value = 0;
4283 }
4284 }
4285#endif
4286 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4287 && max == gui.bottom_sbar.max)
4288 return;
4289
4290 gui.bottom_sbar.value = value;
4291 gui.bottom_sbar.size = size;
4292 gui.bottom_sbar.max = max;
4293 gui.prev_wrap = curwin->w_p_wrap;
4294
4295 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4296}
4297
4298/*
4299 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4300 */
4301 int
4302gui_do_horiz_scroll()
4303{
4304 /* no wrapping, no scrolling */
4305 if (curwin->w_p_wrap)
4306 return FALSE;
4307
4308 if (curwin->w_leftcol == scrollbar_value)
4309 return FALSE;
4310
4311 curwin->w_leftcol = scrollbar_value;
4312
4313 /* When the line of the cursor is too short, move the cursor to the
4314 * longest visible line. Do a sanity check on "longest_lnum", just in
4315 * case. */
4316 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4317 && longest_lnum >= curwin->w_topline
4318 && longest_lnum < curwin->w_botline
4319 && !virtual_active())
4320 {
4321 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4322 {
4323 curwin->w_cursor.lnum = longest_lnum;
4324 curwin->w_cursor.col = 0;
4325 }
4326 }
4327
4328 return leftcol_changed();
4329}
4330
4331/*
4332 * Check that none of the colors are the same as the background color
4333 */
4334 void
4335gui_check_colors()
4336{
4337 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4338 {
4339 gui_set_bg_color((char_u *)"White");
4340 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4341 gui_set_fg_color((char_u *)"Black");
4342 }
4343}
4344
Bram Moolenaar3918c952005-03-15 22:34:55 +00004345 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346gui_set_fg_color(name)
4347 char_u *name;
4348{
4349 gui.norm_pixel = gui_get_color(name);
4350 hl_set_fg_color_name(vim_strsave(name));
4351}
4352
Bram Moolenaar3918c952005-03-15 22:34:55 +00004353 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354gui_set_bg_color(name)
4355 char_u *name;
4356{
4357 gui.back_pixel = gui_get_color(name);
4358 hl_set_bg_color_name(vim_strsave(name));
4359}
4360
4361/*
4362 * Allocate a color by name.
4363 * Returns INVALCOLOR and gives an error message when failed.
4364 */
4365 guicolor_T
4366gui_get_color(name)
4367 char_u *name;
4368{
4369 guicolor_T t;
4370
4371 if (*name == NUL)
4372 return INVALCOLOR;
4373 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004376#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 && gui.in_use
4378#endif
4379 )
4380 EMSG2(_("E254: Cannot allocate color %s"), name);
4381 return t;
4382}
4383
4384/*
4385 * Return the grey value of a color (range 0-255).
4386 */
4387 int
4388gui_get_lightness(pixel)
4389 guicolor_T pixel;
4390{
4391 long_u rgb = gui_mch_get_rgb(pixel);
4392
4393 return ( (((rgb >> 16) & 0xff) * 299)
4394 + (((rgb >> 8) & 0xff) * 587)
4395 + ((rgb & 0xff) * 114)) / 1000;
4396}
4397
4398#if defined(FEAT_GUI_X11) || defined(PROTO)
4399 void
4400gui_new_scrollbar_colors()
4401{
4402 win_T *wp;
4403
4404 /* Nothing to do if GUI hasn't started yet. */
4405 if (!gui.in_use)
4406 return;
4407
4408 FOR_ALL_WINDOWS(wp)
4409 {
4410 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4411 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4412 }
4413 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4414}
4415#endif
4416
4417/*
4418 * Call this when focus has changed.
4419 */
4420 void
4421gui_focus_change(in_focus)
4422 int in_focus;
4423{
4424/*
4425 * Skip this code to avoid drawing the cursor when debugging and switching
4426 * between the debugger window and gvim.
4427 */
4428#if 1
4429 gui.in_focus = in_focus;
4430 out_flush(); /* make sure output has been written */
4431 gui_update_cursor(TRUE, FALSE);
4432
4433# ifdef FEAT_XIM
4434 xim_set_focus(in_focus);
4435# endif
4436
4437 ui_focus_change(in_focus);
4438#endif
4439}
4440
4441/*
4442 * Called when the mouse moved (but not when dragging).
4443 */
4444 void
4445gui_mouse_moved(x, y)
4446 int x;
4447 int y;
4448{
4449 win_T *wp;
4450 char_u st[6];
4451
Bram Moolenaard3667a22006-03-16 21:35:52 +00004452 /* Ignore this while still starting up. */
4453 if (!gui.in_use || gui.starting)
4454 return;
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#ifdef FEAT_MOUSESHAPE
4457 /* Get window pointer, and update mouse shape as well. */
4458 wp = xy2win(x, y);
4459#endif
4460
4461 /* Only handle this when 'mousefocus' set and ... */
4462 if (p_mousef
4463 && !hold_gui_events /* not holding events */
4464 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4465 && State != HITRETURN /* but not hit-return prompt */
4466 && msg_scrolled == 0 /* no scrolled message */
4467 && !need_mouse_correct /* not moving the pointer */
4468 && gui.in_focus) /* gvim in focus */
4469 {
4470 /* Don't move the mouse when it's left or right of the Vim window */
4471 if (x < 0 || x > Columns * gui.char_width)
4472 return;
4473#ifndef FEAT_MOUSESHAPE
4474 wp = xy2win(x, y);
4475#endif
4476 if (wp == curwin || wp == NULL)
4477 return; /* still in the same old window, or none at all */
4478
4479 /*
4480 * format a mouse click on status line input
4481 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004482 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4483 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
4485 if (finish_op)
4486 {
4487 /* abort the current operator first */
4488 st[0] = ESC;
4489 add_to_input_buf(st, 1);
4490 }
4491 st[0] = CSI;
4492 st[1] = KS_MOUSE;
4493 st[2] = KE_FILLER;
4494 st[3] = (char_u)MOUSE_LEFT;
4495 fill_mouse_coord(st + 4,
4496#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004497 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#else
4499 -1,
4500#endif
4501 wp->w_height + W_WINROW(wp));
4502
4503 add_to_input_buf(st, 8);
4504 st[3] = (char_u)MOUSE_RELEASE;
4505 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#ifdef FEAT_GUI_GTK
4507 /* Need to wake up the main loop */
4508 if (gtk_main_level() > 0)
4509 gtk_main_quit();
4510#endif
4511 }
4512}
4513
4514/*
4515 * Called when mouse should be moved to window with focus.
4516 */
4517 void
4518gui_mouse_correct()
4519{
4520 int x, y;
4521 win_T *wp = NULL;
4522
4523 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004524
4525 if (!(gui.in_use && p_mousef))
4526 return;
4527
4528 gui_mch_getmouse(&x, &y);
4529 /* Don't move the mouse when it's left or right of the Vim window */
4530 if (x < 0 || x > Columns * gui.char_width)
4531 return;
4532 if (y >= 0)
4533 wp = xy2win(x, y);
4534 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004536 validate_cline_row();
4537 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4538 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541}
4542
4543/*
4544 * Find window where the mouse pointer "y" coordinate is in.
4545 */
4546/*ARGSUSED*/
4547 static win_T *
4548xy2win(x, y)
4549 int x;
4550 int y;
4551{
4552#ifdef FEAT_WINDOWS
4553 int row;
4554 int col;
4555 win_T *wp;
4556
4557 row = Y_2_ROW(y);
4558 col = X_2_COL(x);
4559 if (row < 0 || col < 0) /* before first window */
4560 return NULL;
4561 wp = mouse_find_win(&row, &col);
4562# ifdef FEAT_MOUSESHAPE
4563 if (State == HITRETURN || State == ASKMORE)
4564 {
4565 if (Y_2_ROW(y) >= msg_row)
4566 update_mouseshape(SHAPE_IDX_MOREL);
4567 else
4568 update_mouseshape(SHAPE_IDX_MORE);
4569 }
4570 else if (row > wp->w_height) /* below status line */
4571 update_mouseshape(SHAPE_IDX_CLINE);
4572# ifdef FEAT_VERTSPLIT
4573 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4574 && (row != wp->w_height || !stl_connected(wp)))
4575 update_mouseshape(SHAPE_IDX_VSEP);
4576# endif
4577 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4578 && row == wp->w_height)
4579 update_mouseshape(SHAPE_IDX_STATUS);
4580 else
4581 update_mouseshape(-2);
4582# endif
4583 return wp;
4584#else
4585 return firstwin;
4586#endif
4587}
4588
4589/*
4590 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4591 * File names may be given to redefine the args list.
4592 */
4593 void
4594ex_gui(eap)
4595 exarg_T *eap;
4596{
4597 char_u *arg = eap->arg;
4598
4599 /*
4600 * Check for "-f" argument: foreground, don't fork.
4601 * Also don't fork when started with "gvim -f".
4602 * Do fork when using "gui -b".
4603 */
4604 if (arg[0] == '-'
4605 && (arg[1] == 'f' || arg[1] == 'b')
4606 && (arg[2] == NUL || vim_iswhite(arg[2])))
4607 {
4608 gui.dofork = (arg[1] == 'b');
4609 eap->arg = skipwhite(eap->arg + 2);
4610 }
4611 if (!gui.in_use)
4612 {
4613 /* Clear the command. Needed for when forking+exiting, to avoid part
4614 * of the argument ending up after the shell prompt. */
4615 msg_clr_eos_force();
4616 gui_start();
4617 }
4618 if (!ends_excmd(*eap->arg))
4619 ex_next(eap);
4620}
4621
4622#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004623 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624/*
4625 * This is shared between Athena, Motif and GTK.
4626 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004627static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629/*
4630 * Callback function for do_in_runtimepath().
4631 */
4632 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004633gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004635 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 char_u *gfp_buffer = cookie;
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (STRLEN(fname) >= MAXPATHL)
4640 *gfp_buffer = NUL;
4641 else
4642 STRCPY(gfp_buffer, fname);
4643}
4644
4645/*
4646 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4647 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4648 */
4649 int
4650gui_find_bitmap(name, buffer, ext)
4651 char_u *name;
4652 char_u *buffer;
4653 char *ext;
4654{
4655 if (STRLEN(name) > MAXPATHL - 14)
4656 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004657 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004658 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4659 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
4661 return OK;
4662}
4663
4664# if !defined(HAVE_GTK2) || defined(PROTO)
4665/*
4666 * Given the name of the "icon=" argument, try finding the bitmap file for the
4667 * icon. If it is an absolute path name, use it as it is. Otherwise append
4668 * "ext" and search for it in 'runtimepath'.
4669 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4670 * contains "name".
4671 */
4672 void
4673gui_find_iconfile(name, buffer, ext)
4674 char_u *name;
4675 char_u *buffer;
4676 char *ext;
4677{
4678 char_u buf[MAXPATHL + 1];
4679
4680 expand_env(name, buffer, MAXPATHL);
4681 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4682 STRCPY(buffer, buf);
4683}
4684# endif
4685#endif
4686
Bram Moolenaar9372a112005-12-06 19:59:18 +00004687#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 void
4689display_errors()
4690{
4691 char_u *p;
4692
4693 if (isatty(2))
4694 fflush(stderr);
4695 else if (error_ga.ga_data != NULL)
4696 {
4697 /* avoid putting up a message box with blanks only */
4698 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4699 if (!isspace(*p))
4700 {
4701 /* Truncate a very long message, it will go off-screen. */
4702 if (STRLEN(p) > 2000)
4703 STRCPY(p + 2000 - 14, "...(truncated)");
4704 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4705 p, (char_u *)_("&Ok"), 1, NULL);
4706 break;
4707 }
4708 ga_clear(&error_ga);
4709 }
4710}
4711#endif
4712
4713#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4714/*
4715 * Return TRUE if still starting up and there is no place to enter text.
4716 * For GTK and X11 we check if stderr is not a tty, which means we were
4717 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4718 * allow typing on stdin.
4719 */
4720 int
4721no_console_input()
4722{
4723 return ((!gui.in_use || gui.starting)
4724# ifndef NO_CONSOLE
4725 && !isatty(0) && !isatty(2)
4726# endif
4727 );
4728}
4729#endif
4730
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004731#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4732 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733/*
4734 * Update the current window and the screen.
4735 */
4736 void
4737gui_update_screen()
4738{
4739 update_topline();
4740 validate_cursor();
4741 update_screen(0); /* may need to update the screen */
4742 setcursor();
4743 out_flush(); /* make sure output has been written */
4744 gui_update_cursor(TRUE, FALSE);
4745 gui_mch_flush();
4746}
4747#endif
4748
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004749#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4751
4752/*
4753 * Get the text to use in a find/replace dialog. Uses the last search pattern
4754 * if the argument is empty.
4755 * Returns an allocated string.
4756 */
4757 char_u *
4758get_find_dialog_text(arg, wwordp, mcasep)
4759 char_u *arg;
4760 int *wwordp; /* return: TRUE if \< \> found */
4761 int *mcasep; /* return: TRUE if \C found */
4762{
4763 char_u *text;
4764
4765 if (*arg == NUL)
4766 text = last_search_pat();
4767 else
4768 text = arg;
4769 if (text != NULL)
4770 {
4771 text = vim_strsave(text);
4772 if (text != NULL)
4773 {
4774 int len = STRLEN(text);
4775 int i;
4776
4777 /* Remove "\V" */
4778 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4779 {
4780 mch_memmove(text, text + 2, (size_t)(len - 1));
4781 len -= 2;
4782 }
4783
4784 /* Recognize "\c" and "\C" and remove. */
4785 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4786 {
4787 *mcasep = (text[1] == 'C');
4788 mch_memmove(text, text + 2, (size_t)(len - 1));
4789 len -= 2;
4790 }
4791
4792 /* Recognize "\<text\>" and remove. */
4793 if (len >= 4
4794 && STRNCMP(text, "\\<", 2) == 0
4795 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4796 {
4797 *wwordp = TRUE;
4798 mch_memmove(text, text + 2, (size_t)(len - 4));
4799 text[len - 4] = NUL;
4800 }
4801
4802 /* Recognize "\/" or "\?" and remove. */
4803 for (i = 0; i + 1 < len; ++i)
4804 if (text[i] == '\\' && (text[i + 1] == '/'
4805 || text[i + 1] == '?'))
4806 {
4807 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4808 --len;
4809 }
4810 }
4811 }
4812 return text;
4813}
4814
4815/*
4816 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4817 */
4818 static void
4819concat_esc(gap, text, what)
4820 garray_T *gap;
4821 char_u *text;
4822 int what;
4823{
4824 while (*text != NUL)
4825 {
4826#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004827 int l = (*mb_ptr2len)(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (l > 1)
4829 {
4830 while (--l >= 0)
4831 ga_append(gap, *text++);
4832 continue;
4833 }
4834#endif
4835 if (*text == what)
4836 ga_append(gap, '\\');
4837 ga_append(gap, *text);
4838 ++text;
4839 }
4840}
4841
4842/*
4843 * Handle the press of a button in the find-replace dialog.
4844 * Return TRUE when something was added to the input buffer.
4845 */
4846 int
4847gui_do_findrepl(flags, find_text, repl_text, down)
4848 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4849 char_u *find_text;
4850 char_u *repl_text;
4851 int down; /* Search downwards. */
4852{
4853 garray_T ga;
4854 int i;
4855 int type = (flags & FRD_TYPE_MASK);
4856 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004857 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004858 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004861 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 ga_concat(&ga, (char_u *)"%s/");
4863
4864 ga_concat(&ga, (char_u *)"\\V");
4865 if (flags & FRD_MATCH_CASE)
4866 ga_concat(&ga, (char_u *)"\\C");
4867 else
4868 ga_concat(&ga, (char_u *)"\\c");
4869 if (flags & FRD_WHOLE_WORD)
4870 ga_concat(&ga, (char_u *)"\\<");
4871 if (type == FRD_REPLACEALL || down)
4872 concat_esc(&ga, find_text, '/'); /* escape slashes */
4873 else
4874 concat_esc(&ga, find_text, '?'); /* escape '?' */
4875 if (flags & FRD_WHOLE_WORD)
4876 ga_concat(&ga, (char_u *)"\\>");
4877
4878 if (type == FRD_REPLACEALL)
4879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004881 /* escape / and \ */
4882 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
4883 if (p != NULL)
4884 ga_concat(&ga, p);
4885 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004887 }
4888 ga_append(&ga, NUL);
4889
4890 if (type == FRD_REPLACE)
4891 {
4892 /* Do the replacement when the text at the cursor matches. Thus no
4893 * replacement is done if the cursor was moved! */
4894 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
4895 regmatch.rm_ic = 0;
4896 if (regmatch.regprog != NULL)
4897 {
4898 p = ml_get_cursor();
4899 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
4900 && regmatch.startp[0] == p)
4901 {
4902 /* Clear the command line to remove any old "No match"
4903 * error. */
4904 msg_end_prompt();
4905
4906 if (u_save_cursor() == OK)
4907 {
4908 /* A button was pressed thus undo should be synced. */
4909 if (no_u_sync == 0)
4910 u_sync();
4911
4912 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00004913 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004914 ins_str(repl_text);
4915 }
4916 }
4917 else
4918 MSG(_("No match at cursor, finding next"));
4919 vim_free(regmatch.regprog);
4920 }
4921 }
4922
4923 if (type == FRD_REPLACEALL)
4924 {
4925 /* A button was pressed, thus undo should be synced. */
4926 if (no_u_sync == 0)
4927 u_sync();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 do_cmdline_cmd(ga.ga_data);
4929 }
4930 else
4931 {
4932 /* Search for the next match. */
4933 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
4935 SEARCH_MSG + SEARCH_MARK);
4936 msg_scroll = i; /* don't let an error message set msg_scroll */
4937 }
4938
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004939 /* Don't want to pass did_emsg to other code, it may cause disabling
4940 * syntax HL if we were busy redrawing. */
4941 did_emsg = save_did_emsg;
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (State & (NORMAL | INSERT))
4944 {
4945 gui_update_screen(); /* update the screen */
4946 msg_didout = 0; /* overwrite any message */
4947 need_wait_return = FALSE; /* don't wait for return */
4948 }
4949
4950 vim_free(ga.ga_data);
4951 return (ga.ga_len > 0);
4952}
4953
4954#endif
4955
4956#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
4957 || defined(FEAT_GUI_MSWIN) \
4958 || defined(FEAT_GUI_MAC) \
4959 || defined(PROTO)
4960
4961#ifdef FEAT_WINDOWS
4962static void gui_wingoto_xy __ARGS((int x, int y));
4963
4964/*
4965 * Jump to the window at specified point (x, y).
4966 */
4967 static void
4968gui_wingoto_xy(x, y)
4969 int x;
4970 int y;
4971{
4972 int row = Y_2_ROW(y);
4973 int col = X_2_COL(x);
4974 win_T *wp;
4975
4976 if (row >= 0 && col >= 0)
4977 {
4978 wp = mouse_find_win(&row, &col);
4979 if (wp != NULL && wp != curwin)
4980 win_goto(wp);
4981 }
4982}
4983#endif
4984
4985/*
4986 * Process file drop. Mouse cursor position, key modifiers, name of files
4987 * and count of files are given. Argument "fnames[count]" has full pathnames
4988 * of dropped files, they will be freed in this function, and caller can't use
4989 * fnames after call this function.
4990 */
4991/*ARGSUSED*/
4992 void
4993gui_handle_drop(x, y, modifiers, fnames, count)
4994 int x;
4995 int y;
4996 int_u modifiers;
4997 char_u **fnames;
4998 int count;
4999{
5000 int i;
5001 char_u *p;
5002
5003 /*
5004 * When the cursor is at the command line, add the file names to the
5005 * command line, don't edit the files.
5006 */
5007 if (State & CMDLINE)
5008 {
5009 shorten_filenames(fnames, count);
5010 for (i = 0; i < count; ++i)
5011 {
5012 if (fnames[i] != NULL)
5013 {
5014 if (i > 0)
5015 add_to_input_buf((char_u*)" ", 1);
5016
5017 /* We don't know what command is used thus we can't be sure
5018 * about which characters need to be escaped. Only escape the
5019 * most common ones. */
5020# ifdef BACKSLASH_IN_FILENAME
5021 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5022# else
5023 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5024# endif
5025 if (p != NULL)
5026 add_to_input_buf(p, (int)STRLEN(p));
5027 vim_free(p);
5028 vim_free(fnames[i]);
5029 }
5030 }
5031 vim_free(fnames);
5032 }
5033 else
5034 {
5035 /* Go to the window under mouse cursor, then shorten given "fnames" by
5036 * current window, because a window can have local current dir. */
5037# ifdef FEAT_WINDOWS
5038 gui_wingoto_xy(x, y);
5039# endif
5040 shorten_filenames(fnames, count);
5041
5042 /* If Shift held down, remember the first item. */
5043 if ((modifiers & MOUSE_SHIFT) != 0)
5044 p = vim_strsave(fnames[0]);
5045 else
5046 p = NULL;
5047
5048 /* Handle the drop, :edit or :split to get to the file. This also
5049 * frees fnames[]. Skip this if there is only one item it's a
5050 * directory and Shift is held down. */
5051 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5052 && mch_isdir(fnames[0]))
5053 {
5054 vim_free(fnames[0]);
5055 vim_free(fnames);
5056 }
5057 else
5058 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5059
5060 /* If Shift held down, change to first file's directory. If the first
5061 * item is a directory, change to that directory (and let the explorer
5062 * plugin show the contents). */
5063 if (p != NULL)
5064 {
5065 if (mch_isdir(p))
5066 {
5067 if (mch_chdir((char *)p) == 0)
5068 shorten_fnames(TRUE);
5069 }
5070 else if (vim_chdirfile(p) == OK)
5071 shorten_fnames(TRUE);
5072 vim_free(p);
5073 }
5074
5075 /* Update the screen display */
5076 update_screen(NOT_VALID);
5077# ifdef FEAT_MENU
5078 gui_update_menus(0);
5079# endif
5080 setcursor();
5081 out_flush();
5082 gui_update_cursor(FALSE, FALSE);
5083 gui_mch_flush();
5084 }
5085}
5086#endif