blob: af648443f53dcc1d1070f058128e209d2d5467b5 [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. */
524 gui_set_shellsize(FALSE, TRUE);
525#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. */
550 gui_set_shellsize(FALSE, TRUE);
551#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
744 );
745 }
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
1357gui_set_shellsize(mustset, fit_to_display)
1358 int mustset; /* set by the user */
1359 int fit_to_display;
1360{
1361 int base_width;
1362 int base_height;
1363 int width;
1364 int height;
1365 int min_width;
1366 int min_height;
1367 int screen_w;
1368 int screen_h;
1369
1370 if (!gui.shell_created)
1371 return;
1372
1373#ifdef MSWIN
1374 /* If not setting to a user specified size and maximized, calculate the
1375 * number of characters that fit in the maximized window. */
1376 if (!mustset && gui_mch_maximized())
1377 {
1378 gui_mch_newfont();
1379 return;
1380 }
1381#endif
1382
1383 base_width = gui_get_base_width();
1384 base_height = gui_get_base_height();
1385#ifdef USE_SUN_WORKSHOP
1386 if (!mustset && usingSunWorkShop
1387 && workshop_get_width_height(&width, &height))
1388 {
1389 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1390 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1391 }
1392 else
1393#endif
1394 {
1395 width = Columns * gui.char_width + base_width;
1396 height = Rows * gui.char_height + base_height;
1397 }
1398
1399 if (fit_to_display)
1400 {
1401 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1402 if (width > screen_w)
1403 {
1404 Columns = (screen_w - base_width) / gui.char_width;
1405 if (Columns < MIN_COLUMNS)
1406 Columns = MIN_COLUMNS;
1407 width = Columns * gui.char_width + base_width;
1408 }
1409 if (height > screen_h)
1410 {
1411 Rows = (screen_h - base_height) / gui.char_height;
1412 check_shellsize();
1413 height = Rows * gui.char_height + base_height;
1414 }
1415 }
1416 gui.num_cols = Columns;
1417 gui.num_rows = Rows;
1418
1419 min_width = base_width + MIN_COLUMNS * gui.char_width;
1420 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001421# ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001422 min_height += tabline_height() * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425 gui_mch_set_shellsize(width, height, min_width, min_height,
1426 base_width, base_height);
1427 if (fit_to_display)
1428 {
1429 int x, y;
1430
1431 /* Some window managers put the Vim window left of/above the screen. */
1432 gui_mch_update();
1433 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1434 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1435 }
1436
1437 gui_position_components(width);
1438 gui_update_scrollbars(TRUE);
1439 gui_reset_scroll_region();
1440}
1441
1442/*
1443 * Called when Rows and/or Columns has changed.
1444 */
1445 void
1446gui_new_shellsize()
1447{
1448 gui_reset_scroll_region();
1449}
1450
1451/*
1452 * Make scroll region cover whole screen.
1453 */
1454 void
1455gui_reset_scroll_region()
1456{
1457 gui.scroll_region_top = 0;
1458 gui.scroll_region_bot = gui.num_rows - 1;
1459 gui.scroll_region_left = 0;
1460 gui.scroll_region_right = gui.num_cols - 1;
1461}
1462
1463 void
1464gui_start_highlight(mask)
1465 int mask;
1466{
1467 if (mask > HL_ALL) /* highlight code */
1468 gui.highlight_mask = mask;
1469 else /* mask */
1470 gui.highlight_mask |= mask;
1471}
1472
1473 void
1474gui_stop_highlight(mask)
1475 int mask;
1476{
1477 if (mask > HL_ALL) /* highlight code */
1478 gui.highlight_mask = HL_NORMAL;
1479 else /* mask */
1480 gui.highlight_mask &= ~mask;
1481}
1482
1483/*
1484 * Clear a rectangular region of the screen from text pos (row1, col1) to
1485 * (row2, col2) inclusive.
1486 */
1487 void
1488gui_clear_block(row1, col1, row2, col2)
1489 int row1;
1490 int col1;
1491 int row2;
1492 int col2;
1493{
1494 /* Clear the selection if we are about to write over it */
1495 clip_may_clear_selection(row1, row2);
1496
1497 gui_mch_clear_block(row1, col1, row2, col2);
1498
1499 /* Invalidate cursor if it was in this block */
1500 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1501 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1502 gui.cursor_is_valid = FALSE;
1503}
1504
1505/*
1506 * Write code to update the cursor later. This avoids the need to flush the
1507 * output buffer before calling gui_update_cursor().
1508 */
1509 void
1510gui_update_cursor_later()
1511{
1512 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1513}
1514
1515 void
1516gui_write(s, len)
1517 char_u *s;
1518 int len;
1519{
1520 char_u *p;
1521 int arg1 = 0, arg2 = 0;
1522 /* this doesn't make sense, disabled until someone can explain why it
1523 * would be needed */
1524#if 0 && (defined(RISCOS) || defined(WIN16))
1525 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1526 our redraw speed will suffer */
1527#else
1528 int force_cursor = FALSE; /* force cursor update */
1529#endif
1530 int force_scrollbar = FALSE;
1531 static win_T *old_curwin = NULL;
1532
1533/* #define DEBUG_GUI_WRITE */
1534#ifdef DEBUG_GUI_WRITE
1535 {
1536 int i;
1537 char_u *str;
1538
1539 printf("gui_write(%d):\n ", len);
1540 for (i = 0; i < len; i++)
1541 if (s[i] == ESC)
1542 {
1543 if (i != 0)
1544 printf("\n ");
1545 printf("<ESC>");
1546 }
1547 else
1548 {
1549 str = transchar_byte(s[i]);
1550 if (str[0] && str[1])
1551 printf("<%s>", (char *)str);
1552 else
1553 printf("%s", (char *)str);
1554 }
1555 printf("\n");
1556 }
1557#endif
1558 while (len)
1559 {
1560 if (s[0] == ESC && s[1] == '|')
1561 {
1562 p = s + 2;
1563 if (VIM_ISDIGIT(*p))
1564 {
1565 arg1 = getdigits(&p);
1566 if (p > s + len)
1567 break;
1568 if (*p == ';')
1569 {
1570 ++p;
1571 arg2 = getdigits(&p);
1572 if (p > s + len)
1573 break;
1574 }
1575 }
1576 switch (*p)
1577 {
1578 case 'C': /* Clear screen */
1579 clip_scroll_selection(9999);
1580 gui_mch_clear_all();
1581 gui.cursor_is_valid = FALSE;
1582 force_scrollbar = TRUE;
1583 break;
1584 case 'M': /* Move cursor */
1585 gui_set_cursor(arg1, arg2);
1586 break;
1587 case 's': /* force cursor (shape) update */
1588 force_cursor = TRUE;
1589 break;
1590 case 'R': /* Set scroll region */
1591 if (arg1 < arg2)
1592 {
1593 gui.scroll_region_top = arg1;
1594 gui.scroll_region_bot = arg2;
1595 }
1596 else
1597 {
1598 gui.scroll_region_top = arg2;
1599 gui.scroll_region_bot = arg1;
1600 }
1601 break;
1602#ifdef FEAT_VERTSPLIT
1603 case 'V': /* Set vertical scroll region */
1604 if (arg1 < arg2)
1605 {
1606 gui.scroll_region_left = arg1;
1607 gui.scroll_region_right = arg2;
1608 }
1609 else
1610 {
1611 gui.scroll_region_left = arg2;
1612 gui.scroll_region_right = arg1;
1613 }
1614 break;
1615#endif
1616 case 'd': /* Delete line */
1617 gui_delete_lines(gui.row, 1);
1618 break;
1619 case 'D': /* Delete lines */
1620 gui_delete_lines(gui.row, arg1);
1621 break;
1622 case 'i': /* Insert line */
1623 gui_insert_lines(gui.row, 1);
1624 break;
1625 case 'I': /* Insert lines */
1626 gui_insert_lines(gui.row, arg1);
1627 break;
1628 case '$': /* Clear to end-of-line */
1629 gui_clear_block(gui.row, gui.col, gui.row,
1630 (int)Columns - 1);
1631 break;
1632 case 'h': /* Turn on highlighting */
1633 gui_start_highlight(arg1);
1634 break;
1635 case 'H': /* Turn off highlighting */
1636 gui_stop_highlight(arg1);
1637 break;
1638 case 'f': /* flash the window (visual bell) */
1639 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1640 break;
1641 default:
1642 p = s + 1; /* Skip the ESC */
1643 break;
1644 }
1645 len -= (int)(++p - s);
1646 s = p;
1647 }
1648 else if (
1649#ifdef EBCDIC
1650 CtrlChar(s[0]) != 0 /* Ctrl character */
1651#else
1652 s[0] < 0x20 /* Ctrl character */
1653#endif
1654#ifdef FEAT_SIGN_ICONS
1655 && s[0] != SIGN_BYTE
1656# ifdef FEAT_NETBEANS_INTG
1657 && s[0] != MULTISIGN_BYTE
1658# endif
1659#endif
1660 )
1661 {
1662 if (s[0] == '\n') /* NL */
1663 {
1664 gui.col = 0;
1665 if (gui.row < gui.scroll_region_bot)
1666 gui.row++;
1667 else
1668 gui_delete_lines(gui.scroll_region_top, 1);
1669 }
1670 else if (s[0] == '\r') /* CR */
1671 {
1672 gui.col = 0;
1673 }
1674 else if (s[0] == '\b') /* Backspace */
1675 {
1676 if (gui.col)
1677 --gui.col;
1678 }
1679 else if (s[0] == Ctrl_L) /* cursor-right */
1680 {
1681 ++gui.col;
1682 }
1683 else if (s[0] == Ctrl_G) /* Beep */
1684 {
1685 gui_mch_beep();
1686 }
1687 /* Other Ctrl character: shouldn't happen! */
1688
1689 --len; /* Skip this char */
1690 ++s;
1691 }
1692 else
1693 {
1694 p = s;
1695 while (len > 0 && (
1696#ifdef EBCDIC
1697 CtrlChar(*p) == 0
1698#else
1699 *p >= 0x20
1700#endif
1701#ifdef FEAT_SIGN_ICONS
1702 || *p == SIGN_BYTE
1703# ifdef FEAT_NETBEANS_INTG
1704 || *p == MULTISIGN_BYTE
1705# endif
1706#endif
1707 ))
1708 {
1709 len--;
1710 p++;
1711 }
1712 gui_outstr(s, (int)(p - s));
1713 s = p;
1714 }
1715 }
1716
1717 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1718 * set). */
1719 if (force_cursor)
1720 gui_update_cursor(TRUE, TRUE);
1721
1722 /* When switching to another window the dragging must have stopped.
1723 * Required for GTK, dragged_sb isn't reset. */
1724 if (old_curwin != curwin)
1725 gui.dragged_sb = SBAR_NONE;
1726
1727 /* Update the scrollbars after clearing the screen or when switched
1728 * to another window.
1729 * Update the horizontal scrollbar always, it's difficult to check all
1730 * situations where it might change. */
1731 if (force_scrollbar || old_curwin != curwin)
1732 gui_update_scrollbars(force_scrollbar);
1733 else
1734 gui_update_horiz_scrollbar(FALSE);
1735 old_curwin = curwin;
1736
1737 /*
1738 * We need to make sure this is cleared since Athena doesn't tell us when
1739 * he is done dragging. Do the same for GTK.
1740 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001741#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 gui.dragged_sb = SBAR_NONE;
1743#endif
1744
1745 gui_mch_flush(); /* In case vim decides to take a nap */
1746}
1747
1748/*
1749 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1750 * produces wrong results. Call gui_dont_update_cursor() before that code and
1751 * gui_can_update_cursor() afterwards.
1752 */
1753 void
1754gui_dont_update_cursor()
1755{
1756 if (gui.in_use)
1757 {
1758 /* Undraw the cursor now, we probably can't do it after the change. */
1759 gui_undraw_cursor();
1760 can_update_cursor = FALSE;
1761 }
1762}
1763
1764 void
1765gui_can_update_cursor()
1766{
1767 can_update_cursor = TRUE;
1768 /* No need to update the cursor right now, there is always more output
1769 * after scrolling. */
1770}
1771
1772 static void
1773gui_outstr(s, len)
1774 char_u *s;
1775 int len;
1776{
1777 int this_len;
1778#ifdef FEAT_MBYTE
1779 int cells;
1780#endif
1781
1782 if (len == 0)
1783 return;
1784
1785 if (len < 0)
1786 len = (int)STRLEN(s);
1787
1788 while (len > 0)
1789 {
1790#ifdef FEAT_MBYTE
1791 if (has_mbyte)
1792 {
1793 /* Find out how many chars fit in the current line. */
1794 cells = 0;
1795 for (this_len = 0; this_len < len; )
1796 {
1797 cells += (*mb_ptr2cells)(s + this_len);
1798 if (gui.col + cells > Columns)
1799 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001800 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802 if (this_len > len)
1803 this_len = len; /* don't include following composing char */
1804 }
1805 else
1806#endif
1807 if (gui.col + len > Columns)
1808 this_len = Columns - gui.col;
1809 else
1810 this_len = len;
1811
1812 (void)gui_outstr_nowrap(s, this_len,
1813 0, (guicolor_T)0, (guicolor_T)0, 0);
1814 s += this_len;
1815 len -= this_len;
1816#ifdef FEAT_MBYTE
1817 /* fill up for a double-width char that doesn't fit. */
1818 if (len > 0 && gui.col < Columns)
1819 (void)gui_outstr_nowrap((char_u *)" ", 1,
1820 0, (guicolor_T)0, (guicolor_T)0, 0);
1821#endif
1822 /* The cursor may wrap to the next line. */
1823 if (gui.col >= Columns)
1824 {
1825 gui.col = 0;
1826 gui.row++;
1827 }
1828 }
1829}
1830
1831/*
1832 * Output one character (may be one or two display cells).
1833 * Caller must check for valid "off".
1834 * Returns FAIL or OK, just like gui_outstr_nowrap().
1835 */
1836 static int
1837gui_screenchar(off, flags, fg, bg, back)
1838 int off; /* Offset from start of screen */
1839 int flags;
1840 guicolor_T fg, bg; /* colors for cursor */
1841 int back; /* backup this many chars when using bold trick */
1842{
1843#ifdef FEAT_MBYTE
1844 char_u buf[MB_MAXBYTES + 1];
1845
1846 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1847 if (enc_utf8 && ScreenLines[off] == 0)
1848 return OK;
1849
1850 if (enc_utf8 && ScreenLinesUC[off] != 0)
1851 /* Draw UTF-8 multi-byte character. */
1852 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1853 flags, fg, bg, back);
1854
1855 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1856 {
1857 buf[0] = ScreenLines[off];
1858 buf[1] = ScreenLines2[off];
1859 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1860 }
1861
1862 /* Draw non-multi-byte character or DBCS character. */
1863 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001864 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 flags, fg, bg, back);
1866#else
1867 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1868#endif
1869}
1870
1871#ifdef HAVE_GTK2
1872/*
1873 * Output the string at the given screen position. This is used in place
1874 * of gui_screenchar() where possible because Pango needs as much context
1875 * as possible to work nicely. It's a lot faster as well.
1876 */
1877 static int
1878gui_screenstr(off, len, flags, fg, bg, back)
1879 int off; /* Offset from start of screen */
1880 int len; /* string length in screen cells */
1881 int flags;
1882 guicolor_T fg, bg; /* colors for cursor */
1883 int back; /* backup this many chars when using bold trick */
1884{
1885 char_u *buf;
1886 int outlen = 0;
1887 int i;
1888 int retval;
1889
1890 if (len <= 0) /* "cannot happen"? */
1891 return OK;
1892
1893 if (enc_utf8)
1894 {
1895 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1896 if (buf == NULL)
1897 return OK; /* not much we could do here... */
1898
1899 for (i = off; i < off + len; ++i)
1900 {
1901 if (ScreenLines[i] == 0)
1902 continue; /* skip second half of double-width char */
1903
1904 if (ScreenLinesUC[i] == 0)
1905 buf[outlen++] = ScreenLines[i];
1906 else
1907 outlen += utfc_char2bytes(i, buf + outlen);
1908 }
1909
1910 buf[outlen] = NUL; /* only to aid debugging */
1911 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1912 vim_free(buf);
1913
1914 return retval;
1915 }
1916 else if (enc_dbcs == DBCS_JPNU)
1917 {
1918 buf = alloc((unsigned)(len * 2 + 1));
1919 if (buf == NULL)
1920 return OK; /* not much we could do here... */
1921
1922 for (i = off; i < off + len; ++i)
1923 {
1924 buf[outlen++] = ScreenLines[i];
1925
1926 /* handle double-byte single-width char */
1927 if (ScreenLines[i] == 0x8e)
1928 buf[outlen++] = ScreenLines2[i];
1929 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1930 buf[outlen++] = ScreenLines[++i];
1931 }
1932
1933 buf[outlen] = NUL; /* only to aid debugging */
1934 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1935 vim_free(buf);
1936
1937 return retval;
1938 }
1939 else
1940 {
1941 return gui_outstr_nowrap(&ScreenLines[off], len,
1942 flags, fg, bg, back);
1943 }
1944}
1945#endif /* HAVE_GTK2 */
1946
1947/*
1948 * Output the given string at the current cursor position. If the string is
1949 * too long to fit on the line, then it is truncated.
1950 * "flags":
1951 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1952 * actually draw (an inverted) cursor.
1953 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparant
1954 * background.
1955 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1956 * it.
1957 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1958 * FAIL (the caller should start drawing "back" chars back).
1959 */
1960 int
1961gui_outstr_nowrap(s, len, flags, fg, bg, back)
1962 char_u *s;
1963 int len;
1964 int flags;
1965 guicolor_T fg, bg; /* colors for cursor */
1966 int back; /* backup this many chars when using bold trick */
1967{
1968 long_u highlight_mask;
1969 long_u hl_mask_todo;
1970 guicolor_T fg_color;
1971 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001972 guicolor_T sp_color;
Bram Moolenaar9372a112005-12-06 19:59:18 +00001973#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 GuiFont font = NOFONT;
1975# ifdef FEAT_XFONTSET
1976 GuiFontset fontset = NOFONTSET;
1977# endif
1978#endif
1979 attrentry_T *aep = NULL;
1980 int draw_flags;
1981 int col = gui.col;
1982#ifdef FEAT_SIGN_ICONS
1983 int draw_sign = FALSE;
1984# ifdef FEAT_NETBEANS_INTG
1985 int multi_sign = FALSE;
1986# endif
1987#endif
1988
1989 if (len < 0)
1990 len = (int)STRLEN(s);
1991 if (len == 0)
1992 return OK;
1993
1994#ifdef FEAT_SIGN_ICONS
1995 if (*s == SIGN_BYTE
1996# ifdef FEAT_NETBEANS_INTG
1997 || *s == MULTISIGN_BYTE
1998# endif
1999 )
2000 {
2001# ifdef FEAT_NETBEANS_INTG
2002 if (*s == MULTISIGN_BYTE)
2003 multi_sign = TRUE;
2004# endif
2005 /* draw spaces instead */
2006 s = (char_u *)" ";
2007 if (len == 1 && col > 0)
2008 --col;
2009 len = 2;
2010 draw_sign = TRUE;
2011 highlight_mask = 0;
2012 }
2013 else
2014#endif
2015 if (gui.highlight_mask > HL_ALL)
2016 {
2017 aep = syn_gui_attr2entry(gui.highlight_mask);
2018 if (aep == NULL) /* highlighting not set */
2019 highlight_mask = 0;
2020 else
2021 highlight_mask = aep->ae_attr;
2022 }
2023 else
2024 highlight_mask = gui.highlight_mask;
2025 hl_mask_todo = highlight_mask;
2026
Bram Moolenaar9372a112005-12-06 19:59:18 +00002027#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 /* Set the font */
2029 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2030 font = aep->ae_u.gui.font;
2031# ifdef FEAT_XFONTSET
2032 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2033 fontset = aep->ae_u.gui.fontset;
2034# endif
2035 else
2036 {
2037# ifdef FEAT_XFONTSET
2038 if (gui.fontset != NOFONTSET)
2039 fontset = gui.fontset;
2040 else
2041# endif
2042 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2043 {
2044 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2045 {
2046 font = gui.boldital_font;
2047 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2048 }
2049 else if (gui.bold_font != NOFONT)
2050 {
2051 font = gui.bold_font;
2052 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2053 }
2054 else
2055 font = gui.norm_font;
2056 }
2057 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2058 {
2059 font = gui.ital_font;
2060 hl_mask_todo &= ~HL_ITALIC;
2061 }
2062 else
2063 font = gui.norm_font;
2064 }
2065# ifdef FEAT_XFONTSET
2066 if (fontset != NOFONTSET)
2067 gui_mch_set_fontset(fontset);
2068 else
2069# endif
2070 gui_mch_set_font(font);
2071#endif
2072
2073 draw_flags = 0;
2074
2075 /* Set the color */
2076 bg_color = gui.back_pixel;
2077 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2078 {
2079 draw_flags |= DRAW_CURSOR;
2080 fg_color = fg;
2081 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002082 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 else if (aep != NULL)
2085 {
2086 fg_color = aep->ae_u.gui.fg_color;
2087 if (fg_color == INVALCOLOR)
2088 fg_color = gui.norm_pixel;
2089 bg_color = aep->ae_u.gui.bg_color;
2090 if (bg_color == INVALCOLOR)
2091 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002092 sp_color = aep->ae_u.gui.sp_color;
2093 if (sp_color == INVALCOLOR)
2094 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002099 sp_color = fg_color;
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2103 {
2104#if defined(AMIGA) || defined(RISCOS)
2105 gui_mch_set_colors(bg_color, fg_color);
2106#else
2107 gui_mch_set_fg_color(bg_color);
2108 gui_mch_set_bg_color(fg_color);
2109#endif
2110 }
2111 else
2112 {
2113#if defined(AMIGA) || defined(RISCOS)
2114 gui_mch_set_colors(fg_color, bg_color);
2115#else
2116 gui_mch_set_fg_color(fg_color);
2117 gui_mch_set_bg_color(bg_color);
2118#endif
2119 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002120 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 /* Clear the selection if we are about to write over it */
2123 if (!(flags & GUI_MON_NOCLEAR))
2124 clip_may_clear_selection(gui.row, gui.row);
2125
2126
2127#ifndef MSWIN16_FASTTEXT
2128 /* If there's no bold font, then fake it */
2129 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2130 draw_flags |= DRAW_BOLD;
2131#endif
2132
2133 /*
2134 * When drawing bold or italic characters the spill-over from the left
2135 * neighbor may be destroyed. Let the caller backup to start redrawing
2136 * just after a blank.
2137 */
2138 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2139 return FAIL;
2140
Bram Moolenaar9372a112005-12-06 19:59:18 +00002141#if defined(RISCOS) || defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /* If there's no italic font, then fake it.
2143 * For GTK2, we don't need a different font for italic style. */
2144 if (hl_mask_todo & HL_ITALIC)
2145 draw_flags |= DRAW_ITALIC;
2146
2147 /* Do we underline the text? */
2148 if (hl_mask_todo & HL_UNDERLINE)
2149 draw_flags |= DRAW_UNDERL;
2150#else
2151 /* Do we underline the text? */
2152 if ((hl_mask_todo & HL_UNDERLINE)
2153# ifndef MSWIN16_FASTTEXT
2154 || (hl_mask_todo & HL_ITALIC)
2155# endif
2156 )
2157 draw_flags |= DRAW_UNDERL;
2158#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002159 /* Do we undercurl the text? */
2160 if (hl_mask_todo & HL_UNDERCURL)
2161 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 /* Do we draw transparantly? */
2164 if (flags & GUI_MON_TRS_CURSOR)
2165 draw_flags |= DRAW_TRANSP;
2166
2167 /*
2168 * Draw the text.
2169 */
2170#ifdef HAVE_GTK2
2171 /* The value returned is the length in display cells */
2172 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2173#else
2174# ifdef FEAT_MBYTE
2175 if (enc_utf8)
2176 {
2177 int start; /* index of bytes to be drawn */
2178 int cells; /* cellwidth of bytes to be drawn */
2179 int thislen; /* length of bytes to be drawin */
2180 int cn; /* cellwidth of current char */
2181 int i; /* index of current char */
2182 int c; /* current char value */
2183 int cl; /* byte length of current char */
2184 int comping; /* current char is composing */
2185 int scol = col; /* screen column */
2186 int dowide; /* use 'guifontwide' */
2187
2188 /* Break the string at a composing character, it has to be drawn on
2189 * top of the previous character. */
2190 start = 0;
2191 cells = 0;
2192 for (i = 0; i < len; i += cl)
2193 {
2194 c = utf_ptr2char(s + i);
2195 cn = utf_char2cells(c);
2196 if (cn > 1
2197# ifdef FEAT_XFONTSET
2198 && fontset == NOFONTSET
2199# endif
2200 && gui.wide_font != NOFONT)
2201 dowide = TRUE;
2202 else
2203 dowide = FALSE;
2204 comping = utf_iscomposing(c);
2205 if (!comping) /* count cells from non-composing chars */
2206 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002207 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (cl == 0) /* hit end of string */
2209 len = i + cl; /* len must be wrong "cannot happen" */
2210
2211 /* print the string so far if it's the last character or there is
2212 * a composing character. */
2213 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar9372a112005-12-06 19:59:18 +00002214# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 || (cn > 1
2216# ifdef FEAT_XFONTSET
2217 /* No fontset: At least draw char after wide char at
2218 * right position. */
2219 && fontset == NOFONTSET
2220# endif
2221 )
2222# endif
2223 )
2224 {
2225 if (comping || dowide)
2226 thislen = i - start;
2227 else
2228 thislen = i - start + cl;
2229 if (thislen > 0)
2230 {
2231 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2232 draw_flags);
2233 start += thislen;
2234 }
2235 scol += cells;
2236 cells = 0;
2237 if (dowide)
2238 {
2239 gui_mch_set_font(gui.wide_font);
2240 gui_mch_draw_string(gui.row, scol - cn,
2241 s + start, cl, draw_flags);
2242 gui_mch_set_font(font);
2243 start += cl;
2244 }
2245
Bram Moolenaar9372a112005-12-06 19:59:18 +00002246# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002247 /* No fontset: draw a space to fill the gap after a wide char
2248 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2250# ifdef FEAT_XFONTSET
2251 && fontset == NOFONTSET
2252# endif
2253 && !dowide)
2254 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2255 1, draw_flags);
2256# endif
2257 }
2258 /* Draw a composing char on top of the previous char. */
2259 if (comping)
2260 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002261# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002262 /* Carbon ATSUI autodraws composing char over previous char */
2263 gui_mch_draw_string(gui.row, scol, s + i, cl,
2264 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002265# else
2266 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2267 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 start = i + cl;
2270 }
2271 }
2272 /* The stuff below assumes "len" is the length in screen columns. */
2273 len = scol - col;
2274 }
2275 else
2276# endif
2277 {
2278 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2279# ifdef FEAT_MBYTE
2280 if (enc_dbcs == DBCS_JPNU)
2281 {
2282 int clen = 0;
2283 int i;
2284
2285 /* Get the length in display cells, this can be different from the
2286 * number of bytes for "euc-jp". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002287 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 clen += (*mb_ptr2cells)(s + i);
2289 len = clen;
2290 }
2291# endif
2292 }
2293#endif /* !HAVE_GTK2 */
2294
2295 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2296 gui.col = col + len;
2297
2298 /* May need to invert it when it's part of the selection. */
2299 if (flags & GUI_MON_NOCLEAR)
2300 clip_may_redraw_selection(gui.row, col, len);
2301
2302 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2303 {
2304 /* Invalidate the old physical cursor position if we wrote over it */
2305 if (gui.cursor_row == gui.row
2306 && gui.cursor_col >= col
2307 && gui.cursor_col < col + len)
2308 gui.cursor_is_valid = FALSE;
2309 }
2310
2311#ifdef FEAT_SIGN_ICONS
2312 if (draw_sign)
2313 /* Draw the sign on top of the spaces. */
2314 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2315# ifdef FEAT_NETBEANS_INTG
2316 if (multi_sign)
2317 netbeans_draw_multisign_indicator(gui.row);
2318# endif
2319#endif
2320
2321 return OK;
2322}
2323
2324/*
2325 * Un-draw the cursor. Actually this just redraws the character at the given
2326 * position. The character just before it too, for when it was in bold.
2327 */
2328 void
2329gui_undraw_cursor()
2330{
2331 if (gui.cursor_is_valid)
2332 {
2333#ifdef FEAT_HANGULIN
2334 if (composing_hangul
2335 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2336 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2337 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2338 gui.norm_pixel, gui.back_pixel, 0);
2339 else
2340 {
2341#endif
2342 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2343 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2344 && gui.cursor_col > 0)
2345 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2346 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2347#ifdef FEAT_HANGULIN
2348 if (composing_hangul)
2349 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2350 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2351 }
2352#endif
2353 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2354 * here in case it wasn't needed to undraw it. */
2355 gui.cursor_is_valid = FALSE;
2356 }
2357}
2358
2359 void
2360gui_redraw(x, y, w, h)
2361 int x;
2362 int y;
2363 int w;
2364 int h;
2365{
2366 int row1, col1, row2, col2;
2367
2368 row1 = Y_2_ROW(y);
2369 col1 = X_2_COL(x);
2370 row2 = Y_2_ROW(y + h - 1);
2371 col2 = X_2_COL(x + w - 1);
2372
2373 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2374
2375 /*
2376 * We may need to redraw the cursor, but don't take it upon us to change
2377 * its location after a scroll.
2378 * (maybe be more strict even and test col too?)
2379 * These things may be outside the update/clipping region and reality may
2380 * not reflect Vims internal ideas if these operations are clipped away.
2381 */
2382 if (gui.row == gui.cursor_row)
2383 gui_update_cursor(TRUE, TRUE);
2384}
2385
2386/*
2387 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2388 * from col1 to col2 (inclusive).
2389 * Return TRUE when the character before the first drawn character has
2390 * different attributes (may have to be redrawn too).
2391 */
2392 int
2393gui_redraw_block(row1, col1, row2, col2, flags)
2394 int row1;
2395 int col1;
2396 int row2;
2397 int col2;
2398 int flags; /* flags for gui_outstr_nowrap() */
2399{
2400 int old_row, old_col;
2401 long_u old_hl_mask;
2402 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002403 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 int idx, len;
2405 int back, nback;
2406 int retval = FALSE;
2407#ifdef FEAT_MBYTE
2408 int orig_col1, orig_col2;
2409#endif
2410
2411 /* Don't try to update when ScreenLines is not valid */
2412 if (!screen_cleared || ScreenLines == NULL)
2413 return retval;
2414
2415 /* Don't try to draw outside the shell! */
2416 /* Check everything, strange values may be caused by a big border width */
2417 col1 = check_col(col1);
2418 col2 = check_col(col2);
2419 row1 = check_row(row1);
2420 row2 = check_row(row2);
2421
2422 /* Remember where our cursor was */
2423 old_row = gui.row;
2424 old_col = gui.col;
2425 old_hl_mask = gui.highlight_mask;
2426#ifdef FEAT_MBYTE
2427 orig_col1 = col1;
2428 orig_col2 = col2;
2429#endif
2430
2431 for (gui.row = row1; gui.row <= row2; gui.row++)
2432 {
2433#ifdef FEAT_MBYTE
2434 /* When only half of a double-wide character is in the block, include
2435 * the other half. */
2436 col1 = orig_col1;
2437 col2 = orig_col2;
2438 off = LineOffset[gui.row];
2439 if (enc_dbcs != 0)
2440 {
2441 if (col1 > 0)
2442 col1 -= dbcs_screen_head_off(ScreenLines + off,
2443 ScreenLines + off + col1);
2444 col2 += dbcs_screen_tail_off(ScreenLines + off,
2445 ScreenLines + off + col2);
2446 }
2447 else if (enc_utf8)
2448 {
2449 if (ScreenLines[off + col1] == 0)
2450 --col1;
2451# ifdef HAVE_GTK2
2452 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2453 ++col2;
2454# endif
2455 }
2456#endif
2457 gui.col = col1;
2458 off = LineOffset[gui.row] + gui.col;
2459 len = col2 - col1 + 1;
2460
2461 /* Find how many chars back this highlighting starts, or where a space
2462 * is. Needed for when the bold trick is used */
2463 for (back = 0; back < col1; ++back)
2464 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2465 || ScreenLines[off - 1 - back] == ' ')
2466 break;
2467 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2468 && ScreenLines[off - 1] != ' ');
2469
2470 /* Break it up in strings of characters with the same attributes. */
2471 /* Print UTF-8 characters individually. */
2472 while (len > 0)
2473 {
2474 first_attr = ScreenAttrs[off];
2475 gui.highlight_mask = first_attr;
2476#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2477 if (enc_utf8 && ScreenLinesUC[off] != 0)
2478 {
2479 /* output multi-byte character separately */
2480 nback = gui_screenchar(off, flags,
2481 (guicolor_T)0, (guicolor_T)0, back);
2482 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2483 idx = 2;
2484 else
2485 idx = 1;
2486 }
2487 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2488 {
2489 /* output double-byte, single-width character separately */
2490 nback = gui_screenchar(off, flags,
2491 (guicolor_T)0, (guicolor_T)0, back);
2492 idx = 1;
2493 }
2494 else
2495#endif
2496 {
2497#ifdef HAVE_GTK2
2498 for (idx = 0; idx < len; ++idx)
2499 {
2500 if (enc_utf8 && ScreenLines[off + idx] == 0)
2501 continue; /* skip second half of double-width char */
2502 if (ScreenAttrs[off + idx] != first_attr)
2503 break;
2504 }
2505 /* gui_screenstr() takes care of multibyte chars */
2506 nback = gui_screenstr(off, idx, flags,
2507 (guicolor_T)0, (guicolor_T)0, back);
2508#else
2509 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2510 idx++)
2511 {
2512# ifdef FEAT_MBYTE
2513 /* Stop at a multi-byte Unicode character. */
2514 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2515 break;
2516 if (enc_dbcs == DBCS_JPNU)
2517 {
2518 /* Stop at a double-byte single-width char. */
2519 if (ScreenLines[off + idx] == 0x8e)
2520 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002521 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 + off + idx) == 2)
2523 ++idx; /* skip second byte of double-byte char */
2524 }
2525# endif
2526 }
2527 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2528 (guicolor_T)0, (guicolor_T)0, back);
2529#endif
2530 }
2531 if (nback == FAIL)
2532 {
2533 /* Must back up to start drawing where a bold or italic word
2534 * starts. */
2535 off -= back;
2536 len += back;
2537 gui.col -= back;
2538 }
2539 else
2540 {
2541 off += idx;
2542 len -= idx;
2543 }
2544 back = 0;
2545 }
2546 }
2547
2548 /* Put the cursor back where it was */
2549 gui.row = old_row;
2550 gui.col = old_col;
2551 gui.highlight_mask = old_hl_mask;
2552
2553 return retval;
2554}
2555
2556 static void
2557gui_delete_lines(row, count)
2558 int row;
2559 int count;
2560{
2561 if (count <= 0)
2562 return;
2563
2564 if (row + count > gui.scroll_region_bot)
2565 /* Scrolled out of region, just blank the lines out */
2566 gui_clear_block(row, gui.scroll_region_left,
2567 gui.scroll_region_bot, gui.scroll_region_right);
2568 else
2569 {
2570 gui_mch_delete_lines(row, count);
2571
2572 /* If the cursor was in the deleted lines it's now gone. If the
2573 * cursor was in the scrolled lines adjust its position. */
2574 if (gui.cursor_row >= row
2575 && gui.cursor_col >= gui.scroll_region_left
2576 && gui.cursor_col <= gui.scroll_region_right)
2577 {
2578 if (gui.cursor_row < row + count)
2579 gui.cursor_is_valid = FALSE;
2580 else if (gui.cursor_row <= gui.scroll_region_bot)
2581 gui.cursor_row -= count;
2582 }
2583 }
2584}
2585
2586 static void
2587gui_insert_lines(row, count)
2588 int row;
2589 int count;
2590{
2591 if (count <= 0)
2592 return;
2593
2594 if (row + count > gui.scroll_region_bot)
2595 /* Scrolled out of region, just blank the lines out */
2596 gui_clear_block(row, gui.scroll_region_left,
2597 gui.scroll_region_bot, gui.scroll_region_right);
2598 else
2599 {
2600 gui_mch_insert_lines(row, count);
2601
2602 if (gui.cursor_row >= gui.row
2603 && gui.cursor_col >= gui.scroll_region_left
2604 && gui.cursor_col <= gui.scroll_region_right)
2605 {
2606 if (gui.cursor_row <= gui.scroll_region_bot - count)
2607 gui.cursor_row += count;
2608 else if (gui.cursor_row <= gui.scroll_region_bot)
2609 gui.cursor_is_valid = FALSE;
2610 }
2611 }
2612}
2613
2614/*
2615 * The main GUI input routine. Waits for a character from the keyboard.
2616 * wtime == -1 Wait forever.
2617 * wtime == 0 Don't wait.
2618 * wtime > 0 Wait wtime milliseconds for a character.
2619 * Returns OK if a character was found to be available within the given time,
2620 * or FAIL otherwise.
2621 */
2622 int
2623gui_wait_for_chars(wtime)
2624 long wtime;
2625{
2626 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 /*
2629 * If we're going to wait a bit, update the menus and mouse shape for the
2630 * current State.
2631 */
2632 if (wtime != 0)
2633 {
2634#ifdef FEAT_MENU
2635 gui_update_menus(0);
2636#endif
2637 }
2638
2639 gui_mch_update();
2640 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 /* Before waiting, flush any output to the screen. */
2646 gui_mch_flush();
2647
2648 if (wtime > 0)
2649 {
2650 /* Blink when waiting for a character. Probably only does something
2651 * for showmatch() */
2652 gui_mch_start_blink();
2653 retval = gui_mch_wait_for_chars(wtime);
2654 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return retval;
2656 }
2657
2658 /*
2659 * While we are waiting indefenitely for a character, blink the cursor.
2660 */
2661 gui_mch_start_blink();
2662
Bram Moolenaar3918c952005-03-15 22:34:55 +00002663 retval = FAIL;
2664 /*
2665 * We may want to trigger the CursorHold event. First wait for
2666 * 'updatetime' and if nothing is typed within that time put the
2667 * K_CURSORHOLD key in the input buffer.
2668 */
2669 if (gui_mch_wait_for_chars(p_ut) == OK)
2670 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002672 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002674 char_u buf[3];
2675
2676 /* Put K_CURSORHOLD in the input buffer. */
2677 buf[0] = CSI;
2678 buf[1] = KS_EXTRA;
2679 buf[2] = (int)KE_CURSORHOLD;
2680 add_to_input_buf(buf, 3);
2681
2682 retval = OK;
2683 }
2684#endif
2685
2686 if (retval == FAIL)
2687 {
2688 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002689 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 gui_mch_stop_blink();
2694 return retval;
2695}
2696
2697/*
2698 * Fill buffer with mouse coordinates encoded for check_termcode().
2699 */
2700 static void
2701fill_mouse_coord(p, col, row)
2702 char_u *p;
2703 int col;
2704 int row;
2705{
2706 p[0] = (char_u)(col / 128 + ' ' + 1);
2707 p[1] = (char_u)(col % 128 + ' ' + 1);
2708 p[2] = (char_u)(row / 128 + ' ' + 1);
2709 p[3] = (char_u)(row % 128 + ' ' + 1);
2710}
2711
2712/*
2713 * Generic mouse support function. Add a mouse event to the input buffer with
2714 * the given properties.
2715 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2716 * MOUSE_X1, MOUSE_X2
2717 * MOUSE_DRAG, or MOUSE_RELEASE.
2718 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2719 * x, y --- Coordinates of mouse in pixels.
2720 * repeated_click --- TRUE if this click comes only a short time after a
2721 * previous click.
2722 * modifiers --- Bit field which may be any of the following modifiers
2723 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2724 * This function will ignore drag events where the mouse has not moved to a new
2725 * character.
2726 */
2727 void
2728gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2729 int button;
2730 int x;
2731 int y;
2732 int repeated_click;
2733 int_u modifiers;
2734{
2735 static int prev_row = 0, prev_col = 0;
2736 static int prev_button = -1;
2737 static int num_clicks = 1;
2738 char_u string[10];
2739 enum key_extra button_char;
2740 int row, col;
2741#ifdef FEAT_CLIPBOARD
2742 int checkfor;
2743 int did_clip = FALSE;
2744#endif
2745
2746 /*
2747 * Scrolling may happen at any time, also while a selection is present.
2748 */
2749 switch (button)
2750 {
2751 case MOUSE_X1:
2752 button_char = KE_X1MOUSE;
2753 goto button_set;
2754 case MOUSE_X2:
2755 button_char = KE_X2MOUSE;
2756 goto button_set;
2757 case MOUSE_4:
2758 button_char = KE_MOUSEDOWN;
2759 goto button_set;
2760 case MOUSE_5:
2761 button_char = KE_MOUSEUP;
2762button_set:
2763 {
2764 /* Don't put events in the input queue now. */
2765 if (hold_gui_events)
2766 return;
2767
2768 string[3] = CSI;
2769 string[4] = KS_EXTRA;
2770 string[5] = (int)button_char;
2771
2772 /* Pass the pointer coordinates of the scroll event so that we
2773 * know which window to scroll. */
2774 row = gui_xy2colrow(x, y, &col);
2775 string[6] = (char_u)(col / 128 + ' ' + 1);
2776 string[7] = (char_u)(col % 128 + ' ' + 1);
2777 string[8] = (char_u)(row / 128 + ' ' + 1);
2778 string[9] = (char_u)(row % 128 + ' ' + 1);
2779
2780 if (modifiers == 0)
2781 add_to_input_buf(string + 3, 7);
2782 else
2783 {
2784 string[0] = CSI;
2785 string[1] = KS_MODIFIER;
2786 string[2] = 0;
2787 if (modifiers & MOUSE_SHIFT)
2788 string[2] |= MOD_MASK_SHIFT;
2789 if (modifiers & MOUSE_CTRL)
2790 string[2] |= MOD_MASK_CTRL;
2791 if (modifiers & MOUSE_ALT)
2792 string[2] |= MOD_MASK_ALT;
2793 add_to_input_buf(string, 10);
2794 }
2795 return;
2796 }
2797 }
2798
2799#ifdef FEAT_CLIPBOARD
2800 /* If a clipboard selection is in progress, handle it */
2801 if (clip_star.state == SELECT_IN_PROGRESS)
2802 {
2803 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2804 return;
2805 }
2806
2807 /* Determine which mouse settings to look for based on the current mode */
2808 switch (get_real_state())
2809 {
2810 case NORMAL_BUSY:
2811 case OP_PENDING:
2812 case NORMAL: checkfor = MOUSE_NORMAL; break;
2813 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002814 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 case REPLACE:
2816 case REPLACE+LANGMAP:
2817#ifdef FEAT_VREPLACE
2818 case VREPLACE:
2819 case VREPLACE+LANGMAP:
2820#endif
2821 case INSERT:
2822 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2823 case ASKMORE:
2824 case HITRETURN: /* At the more- and hit-enter prompt pass the
2825 mouse event for a click on or below the
2826 message line. */
2827 if (Y_2_ROW(y) >= msg_row)
2828 checkfor = MOUSE_NORMAL;
2829 else
2830 checkfor = MOUSE_RETURN;
2831 break;
2832
2833 /*
2834 * On the command line, use the clipboard selection on all lines
2835 * but the command line. But not when pasting.
2836 */
2837 case CMDLINE:
2838 case CMDLINE+LANGMAP:
2839 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2840 checkfor = MOUSE_NONE;
2841 else
2842 checkfor = MOUSE_COMMAND;
2843 break;
2844
2845 default:
2846 checkfor = MOUSE_NONE;
2847 break;
2848 };
2849
2850 /*
2851 * Allow clipboard selection of text on the command line in "normal"
2852 * modes. Don't do this when dragging the status line, or extending a
2853 * Visual selection.
2854 */
2855 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2856 && Y_2_ROW(y) >= topframe->fr_height
2857 && button != MOUSE_DRAG
2858# ifdef FEAT_MOUSESHAPE
2859 && !drag_status_line
2860# ifdef FEAT_VERTSPLIT
2861 && !drag_sep_line
2862# endif
2863# endif
2864 )
2865 checkfor = MOUSE_NONE;
2866
2867 /*
2868 * Use modeless selection when holding CTRL and SHIFT pressed.
2869 */
2870 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2871 checkfor = MOUSE_NONEF;
2872
2873 /*
2874 * In Ex mode, always use modeless selection.
2875 */
2876 if (exmode_active)
2877 checkfor = MOUSE_NONE;
2878
2879 /*
2880 * If the mouse settings say to not use the mouse, use the modeless
2881 * selection. But if Visual is active, assume that only the Visual area
2882 * will be selected.
2883 * Exception: On the command line, both the selection is used and a mouse
2884 * key is send.
2885 */
2886 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2887 {
2888#ifdef FEAT_VISUAL
2889 /* Don't do modeless selection in Visual mode. */
2890 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2891 return;
2892#endif
2893
2894 /*
2895 * When 'mousemodel' is "popup", shift-left is translated to right.
2896 * But not when also using Ctrl.
2897 */
2898 if (mouse_model_popup() && button == MOUSE_LEFT
2899 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2900 {
2901 button = MOUSE_RIGHT;
2902 modifiers &= ~ MOUSE_SHIFT;
2903 }
2904
2905 /* If the selection is done, allow the right button to extend it.
2906 * If the selection is cleared, allow the right button to start it
2907 * from the cursor position. */
2908 if (button == MOUSE_RIGHT)
2909 {
2910 if (clip_star.state == SELECT_CLEARED)
2911 {
2912 if (State & CMDLINE)
2913 {
2914 col = msg_col;
2915 row = msg_row;
2916 }
2917 else
2918 {
2919 col = curwin->w_wcol;
2920 row = curwin->w_wrow + W_WINROW(curwin);
2921 }
2922 clip_start_selection(col, row, FALSE);
2923 }
2924 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2925 repeated_click);
2926 did_clip = TRUE;
2927 }
2928 /* Allow the left button to start the selection */
2929 else if (button ==
2930# ifdef RISCOS
2931 /* Only start a drag on a drag event. Otherwise
2932 * we don't get a release event. */
2933 MOUSE_DRAG
2934# else
2935 MOUSE_LEFT
2936# endif
2937 )
2938 {
2939 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2940 did_clip = TRUE;
2941 }
2942# ifdef RISCOS
2943 else if (button == MOUSE_LEFT)
2944 {
2945 clip_clear_selection();
2946 did_clip = TRUE;
2947 }
2948# endif
2949
2950 /* Always allow pasting */
2951 if (button != MOUSE_MIDDLE)
2952 {
2953 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2954 return;
2955 if (checkfor != MOUSE_COMMAND)
2956 button = MOUSE_LEFT;
2957 }
2958 repeated_click = FALSE;
2959 }
2960
2961 if (clip_star.state != SELECT_CLEARED && !did_clip)
2962 clip_clear_selection();
2963#endif
2964
2965 /* Don't put events in the input queue now. */
2966 if (hold_gui_events)
2967 return;
2968
2969 row = gui_xy2colrow(x, y, &col);
2970
2971 /*
2972 * If we are dragging and the mouse hasn't moved far enough to be on a
2973 * different character, then don't send an event to vim.
2974 */
2975 if (button == MOUSE_DRAG)
2976 {
2977 if (row == prev_row && col == prev_col)
2978 return;
2979 /* Dragging above the window, set "row" to -1 to cause a scroll. */
2980 if (y < 0)
2981 row = -1;
2982 }
2983
2984 /*
2985 * If topline has changed (window scrolled) since the last click, reset
2986 * repeated_click, because we don't want starting Visual mode when
2987 * clicking on a different character in the text.
2988 */
2989 if (curwin->w_topline != gui_prev_topline
2990#ifdef FEAT_DIFF
2991 || curwin->w_topfill != gui_prev_topfill
2992#endif
2993 )
2994 repeated_click = FALSE;
2995
2996 string[0] = CSI; /* this sequence is recognized by check_termcode() */
2997 string[1] = KS_MOUSE;
2998 string[2] = KE_FILLER;
2999 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3000 {
3001 if (repeated_click)
3002 {
3003 /*
3004 * Handle multiple clicks. They only count if the mouse is still
3005 * pointing at the same character.
3006 */
3007 if (button != prev_button || row != prev_row || col != prev_col)
3008 num_clicks = 1;
3009 else if (++num_clicks > 4)
3010 num_clicks = 1;
3011 }
3012 else
3013 num_clicks = 1;
3014 prev_button = button;
3015 gui_prev_topline = curwin->w_topline;
3016#ifdef FEAT_DIFF
3017 gui_prev_topfill = curwin->w_topfill;
3018#endif
3019
3020 string[3] = (char_u)(button | 0x20);
3021 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3022 }
3023 else
3024 string[3] = (char_u)button;
3025
3026 string[3] |= modifiers;
3027 fill_mouse_coord(string + 4, col, row);
3028 add_to_input_buf(string, 8);
3029
3030 if (row < 0)
3031 prev_row = 0;
3032 else
3033 prev_row = row;
3034 prev_col = col;
3035
3036 /*
3037 * We need to make sure this is cleared since Athena doesn't tell us when
3038 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3039 */
3040#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3041 gui.dragged_sb = SBAR_NONE;
3042#endif
3043}
3044
3045/*
3046 * Convert x and y coordinate to column and row in text window.
3047 * Corrects for multi-byte character.
3048 * returns column in "*colp" and row as return value;
3049 */
3050 int
3051gui_xy2colrow(x, y, colp)
3052 int x;
3053 int y;
3054 int *colp;
3055{
3056 int col = check_col(X_2_COL(x));
3057 int row = check_row(Y_2_ROW(y));
3058
3059#ifdef FEAT_MBYTE
3060 *colp = mb_fix_col(col, row);
3061#else
3062 *colp = col;
3063#endif
3064 return row;
3065}
3066
3067#if defined(FEAT_MENU) || defined(PROTO)
3068/*
3069 * Callback function for when a menu entry has been selected.
3070 */
3071 void
3072gui_menu_cb(menu)
3073 vimmenu_T *menu;
3074{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003075 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 /* Don't put events in the input queue now. */
3078 if (hold_gui_events)
3079 return;
3080
3081 bytes[0] = CSI;
3082 bytes[1] = KS_MENU;
3083 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003084 add_to_input_buf(bytes, 3);
3085 add_long_to_buf((long_u)menu, bytes);
3086 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087}
3088#endif
3089
Bram Moolenaar371d5402006-03-20 21:47:49 +00003090#ifndef FEAT_WINDOWS
3091static int prev_which_scrollbars[3];
3092#endif
Bram Moolenaare45828b2006-02-15 22:12:56 +00003093
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094/*
3095 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003096 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 * in p_go.
3098 */
3099/*ARGSUSED*/
3100 void
3101gui_init_which_components(oldval)
3102 char_u *oldval;
3103{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#ifdef FEAT_MENU
3105 static int prev_menu_is_active = -1;
3106#endif
3107#ifdef FEAT_TOOLBAR
3108 static int prev_toolbar = -1;
3109 int using_toolbar = FALSE;
3110#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003111#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003112 int using_tabline;
3113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#ifdef FEAT_FOOTER
3115 static int prev_footer = -1;
3116 int using_footer = FALSE;
3117#endif
3118#if defined(FEAT_MENU) && !defined(WIN16)
3119 static int prev_tearoff = -1;
3120 int using_tearoff = FALSE;
3121#endif
3122
3123 char_u *p;
3124 int i;
3125#ifdef FEAT_MENU
3126 int grey_old, grey_new;
3127 char_u *temp;
3128#endif
3129 win_T *wp;
3130 int need_set_size;
3131 int fix_size;
3132
3133#ifdef FEAT_MENU
3134 if (oldval != NULL && gui.in_use)
3135 {
3136 /*
3137 * Check if the menu's go from grey to non-grey or vise versa.
3138 */
3139 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3140 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3141 if (grey_old != grey_new)
3142 {
3143 temp = p_go;
3144 p_go = oldval;
3145 gui_update_menus(MENU_ALL_MODES);
3146 p_go = temp;
3147 }
3148 }
3149 gui.menu_is_active = FALSE;
3150#endif
3151
3152 for (i = 0; i < 3; i++)
3153 gui.which_scrollbars[i] = FALSE;
3154 for (p = p_go; *p; p++)
3155 switch (*p)
3156 {
3157 case GO_LEFT:
3158 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3159 break;
3160 case GO_RIGHT:
3161 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3162 break;
3163#ifdef FEAT_VERTSPLIT
3164 case GO_VLEFT:
3165 if (win_hasvertsplit())
3166 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3167 break;
3168 case GO_VRIGHT:
3169 if (win_hasvertsplit())
3170 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3171 break;
3172#endif
3173 case GO_BOT:
3174 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3175 break;
3176#ifdef FEAT_MENU
3177 case GO_MENUS:
3178 gui.menu_is_active = TRUE;
3179 break;
3180#endif
3181 case GO_GREY:
3182 /* make menu's have grey items, ignored here */
3183 break;
3184#ifdef FEAT_TOOLBAR
3185 case GO_TOOLBAR:
3186 using_toolbar = TRUE;
3187 break;
3188#endif
3189#ifdef FEAT_FOOTER
3190 case GO_FOOTER:
3191 using_footer = TRUE;
3192 break;
3193#endif
3194 case GO_TEAROFF:
3195#if defined(FEAT_MENU) && !defined(WIN16)
3196 using_tearoff = TRUE;
3197#endif
3198 break;
3199 default:
3200 /* Ignore options that are not supported */
3201 break;
3202 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (gui.in_use)
3205 {
3206 need_set_size = FALSE;
3207 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003208
3209#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003210 /* Update the GUI tab line, it may appear or disappear. This may
3211 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003212 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003213 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003214 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003215 /* We don't want a resize event change "Rows" here, save and
3216 * restore it. Resizing is handled below. */
3217 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003218 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003219 Rows = i;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003220 need_set_size = TRUE;
3221 if (using_tabline)
3222 fix_size = TRUE;
3223 if (!gui_use_tabline())
3224 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3225 }
3226#endif
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 for (i = 0; i < 3; i++)
3229 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003230 if (gui.which_scrollbars[i] !=
3231#ifdef FEAT_WINDOWS
3232 curtab->tp_prev_which_scrollbars[i]
3233#else
3234 prev_which_scrollbars[i]
3235#endif
3236 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 if (i == SBAR_BOTTOM)
3239 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3240 gui.which_scrollbars[i]);
3241 else
3242 {
3243 FOR_ALL_WINDOWS(wp)
3244 {
3245 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3246 }
3247 }
3248 need_set_size = TRUE;
3249 if (gui.which_scrollbars[i])
3250 fix_size = TRUE;
3251 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003252#ifdef FEAT_WINDOWS
3253 curtab->tp_prev_which_scrollbars[i]
3254#else
3255 prev_which_scrollbars[i]
3256#endif
3257 = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259
3260#ifdef FEAT_MENU
3261 if (gui.menu_is_active != prev_menu_is_active)
3262 {
3263 /* We don't want a resize event change "Rows" here, save and
3264 * restore it. Resizing is handled below. */
3265 i = Rows;
3266 gui_mch_enable_menu(gui.menu_is_active);
3267 Rows = i;
3268 prev_menu_is_active = gui.menu_is_active;
3269 need_set_size = TRUE;
3270 if (gui.menu_is_active)
3271 fix_size = TRUE;
3272 }
3273#endif
3274
3275#ifdef FEAT_TOOLBAR
3276 if (using_toolbar != prev_toolbar)
3277 {
3278 gui_mch_show_toolbar(using_toolbar);
3279 prev_toolbar = using_toolbar;
3280 need_set_size = TRUE;
3281 if (using_toolbar)
3282 fix_size = TRUE;
3283 }
3284#endif
3285#ifdef FEAT_FOOTER
3286 if (using_footer != prev_footer)
3287 {
3288 gui_mch_enable_footer(using_footer);
3289 prev_footer = using_footer;
3290 need_set_size = TRUE;
3291 if (using_footer)
3292 fix_size = TRUE;
3293 }
3294#endif
3295#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3296 if (using_tearoff != prev_tearoff)
3297 {
3298 gui_mch_toggle_tearoffs(using_tearoff);
3299 prev_tearoff = using_tearoff;
3300 }
3301#endif
3302 if (need_set_size)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003303 {
3304#ifdef FEAT_GUI_GTK
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003305 long c = Columns;
3306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /* Adjust the size of the window to make the text area keep the
3308 * same size and to avoid that part of our window is off-screen
3309 * and a scrollbar can't be used, for example. */
3310 gui_set_shellsize(FALSE, fix_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003311
3312#ifdef FEAT_GUI_GTK
3313 /* GTK has the annoying habit of sending us resize events when
3314 * changing the window size ourselves. This mostly happens when
3315 * waiting for a character to arrive, quite unpredictably, and may
3316 * change Columns and Rows when we don't want it. Wait for a
3317 * character here to avoid this effect.
3318 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003319 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003320 * Don't do this while starting up though.
3321 * And don't change Rows, it may have be reduced intentionally
3322 * when adding menu/toolbar/tabline. */
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003323 if (!gui.starting)
3324 (void)char_avail();
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003325 Columns = c;
3326#endif
3327 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003328#ifdef FEAT_WINDOWS
3329 /* When the console tabline appears or disappears the window positions
3330 * change. */
3331 if (firstwin->w_winrow != tabline_height())
3332 shell_new_rows(); /* recompute window positions and heights */
3333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335}
3336
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003337#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3338/*
3339 * Return TRUE if the GUI is taking care of the tabline.
3340 * It may still be hidden if 'showtabline' is zero.
3341 */
3342 int
3343gui_use_tabline()
3344{
3345 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3346}
3347
3348/*
3349 * Return TRUE if the GUI is showing the tabline.
3350 * This uses 'showtabline'.
3351 */
3352 static int
3353gui_has_tabline()
3354{
3355 if (!gui_use_tabline()
3356 || p_stal == 0
3357 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3358 return FALSE;
3359 return TRUE;
3360}
3361
3362/*
3363 * Update the tabline.
3364 * This may display/undisplay the tabline and update the labels.
3365 */
3366 void
3367gui_update_tabline()
3368{
3369 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003370 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003371
3372 if (!gui.starting && starting == 0)
3373 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003374 if (!showit != !shown)
3375 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003376 if (showit != 0)
3377 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003378
3379 /* When the tabs change from hidden to shown or from shown to
3380 * hidden the size of the text area should remain the same. */
3381 if (!showit != !shown)
3382 gui_set_shellsize(FALSE, showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003383 }
3384}
3385
3386/*
3387 * Get the label for tab page "tp" into NameBuff[].
3388 */
3389 void
3390get_tabline_label(tp)
3391 tabpage_T *tp;
3392{
3393 int modified = FALSE;
3394 char_u buf[40];
3395 int wincount;
3396 win_T *wp;
3397
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003398 /* Use 'guitablabel' if it's set. */
3399 if (*p_gtl != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003400 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003401 int use_sandbox = FALSE;
3402 int save_called_emsg = called_emsg;
3403 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003404 tabpage_T *save_curtab;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003405
3406 called_emsg = FALSE;
3407
3408 printer_page_num = tabpage_index(tp);
3409# ifdef FEAT_EVAL
3410 set_vim_var_nr(VV_LNUM, printer_page_num);
3411 use_sandbox = was_set_insecurely((char_u *)"guitablabel", 0);
3412# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003413 /* It's almost as going to the tabpage, but without autocommands. */
3414 curtab->tp_firstwin = firstwin;
3415 curtab->tp_lastwin = lastwin;
3416 curtab->tp_curwin = curwin;
3417 save_curtab = curtab;
3418 curtab = tp;
3419 topframe = curtab->tp_topframe;
3420 firstwin = curtab->tp_firstwin;
3421 lastwin = curtab->tp_lastwin;
3422 curwin = curtab->tp_curwin;
3423 curbuf = curwin->w_buffer;
3424
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003425 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003426 build_stl_str_hl(curwin, res, MAXPATHL, p_gtl, use_sandbox,
3427 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003428 STRCPY(NameBuff, res);
3429
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003430 /* Back to the original curtab. */
3431 curtab = save_curtab;
3432 topframe = curtab->tp_topframe;
3433 firstwin = curtab->tp_firstwin;
3434 lastwin = curtab->tp_lastwin;
3435 curwin = curtab->tp_curwin;
3436 curbuf = curwin->w_buffer;
3437
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003438 if (called_emsg)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003439 set_string_option_direct((char_u *)"guitablabel", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003440 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003441 called_emsg |= save_called_emsg;
3442 }
3443 else
3444 {
3445 /* Get the buffer name into NameBuff[] */
3446 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3447
3448 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3449 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3450 if (bufIsChanged(wp->w_buffer))
3451 modified = TRUE;
3452 if (modified || wincount > 1)
3453 {
3454 if (wincount > 1)
3455 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3456 else
3457 buf[0] = NUL;
3458 if (modified)
3459 STRCAT(buf, "+");
3460 STRCAT(buf, " ");
3461 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3462 mch_memmove(NameBuff, buf, STRLEN(buf));
3463 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003464 }
3465}
3466
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003467/*
3468 * Send the event for clicking to select tab page "nr".
3469 * Returns TRUE if it was done, FALSE when skipped because we are already at
3470 * that tab page.
3471 */
3472 int
3473send_tabline_event(nr)
3474 int nr;
3475{
3476 char_u string[3];
3477
3478 if (nr == tabpage_index(curtab))
3479 return FALSE;
3480 string[0] = CSI;
3481 string[1] = KS_TABLINE;
3482 string[2] = KE_FILLER;
3483 add_to_input_buf(string, 3);
3484 string[0] = nr;
3485 add_to_input_buf_csi(string, 1);
3486 return TRUE;
3487}
3488
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491/*
3492 * Scrollbar stuff:
3493 */
3494
Bram Moolenaare45828b2006-02-15 22:12:56 +00003495#if defined(FEAT_WINDOWS) || defined(PROTO)
3496/*
3497 * Remove all scrollbars. Used before switching to another tab page.
3498 */
3499 void
3500gui_remove_scrollbars()
3501{
3502 int i;
3503 win_T *wp;
3504
3505 for (i = 0; i < 3; i++)
3506 {
3507 if (i == SBAR_BOTTOM)
3508 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3509 else
3510 {
3511 FOR_ALL_WINDOWS(wp)
3512 {
3513 gui_do_scrollbar(wp, i, FALSE);
3514 }
3515 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003516 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003517 }
3518}
3519#endif
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 void
3522gui_create_scrollbar(sb, type, wp)
3523 scrollbar_T *sb;
3524 int type;
3525 win_T *wp;
3526{
3527 static int sbar_ident = 0;
3528
3529 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3530 sb->wp = wp;
3531 sb->type = type;
3532 sb->value = 0;
3533#ifdef FEAT_GUI_ATHENA
3534 sb->pixval = 0;
3535#endif
3536 sb->size = 1;
3537 sb->max = 1;
3538 sb->top = 0;
3539 sb->height = 0;
3540#ifdef FEAT_VERTSPLIT
3541 sb->width = 0;
3542#endif
3543 sb->status_height = 0;
3544 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3545}
3546
3547/*
3548 * Find the scrollbar with the given index.
3549 */
3550 scrollbar_T *
3551gui_find_scrollbar(ident)
3552 long ident;
3553{
3554 win_T *wp;
3555
3556 if (gui.bottom_sbar.ident == ident)
3557 return &gui.bottom_sbar;
3558 FOR_ALL_WINDOWS(wp)
3559 {
3560 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3561 return &wp->w_scrollbars[SBAR_LEFT];
3562 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3563 return &wp->w_scrollbars[SBAR_RIGHT];
3564 }
3565 return NULL;
3566}
3567
3568/*
3569 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3570 *
3571 * For Win32, Macintosh and GTK+ 2:
3572 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3573 * you stop dragging the scrollbar. We get here each time the scrollbar is
3574 * dragged another pixel, but as far as the rest of vim goes, it thinks
3575 * we're just hanging in the call to DispatchMessage() in
3576 * process_message(). The DispatchMessage() call that hangs was passed a
3577 * mouse button click event in the scrollbar window. -- webb.
3578 *
3579 * Solution: Do the scrolling right here. But only when allowed.
3580 * Ignore the scrollbars while executing an external command or when there
3581 * are still characters to be processed.
3582 */
3583 void
3584gui_drag_scrollbar(sb, value, still_dragging)
3585 scrollbar_T *sb;
3586 long value;
3587 int still_dragging;
3588{
3589#ifdef FEAT_WINDOWS
3590 win_T *wp;
3591#endif
3592 int sb_num;
3593#ifdef USE_ON_FLY_SCROLL
3594 colnr_T old_leftcol = curwin->w_leftcol;
3595# ifdef FEAT_SCROLLBIND
3596 linenr_T old_topline = curwin->w_topline;
3597# endif
3598# ifdef FEAT_DIFF
3599 int old_topfill = curwin->w_topfill;
3600# endif
3601#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003602 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 int byte_count;
3604#endif
3605
3606 if (sb == NULL)
3607 return;
3608
3609 /* Don't put events in the input queue now. */
3610 if (hold_gui_events)
3611 return;
3612
3613#ifdef FEAT_CMDWIN
3614 if (cmdwin_type != 0 && sb->wp != curwin)
3615 return;
3616#endif
3617
3618 if (still_dragging)
3619 {
3620 if (sb->wp == NULL)
3621 gui.dragged_sb = SBAR_BOTTOM;
3622 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3623 gui.dragged_sb = SBAR_LEFT;
3624 else
3625 gui.dragged_sb = SBAR_RIGHT;
3626 gui.dragged_wp = sb->wp;
3627 }
3628 else
3629 {
3630 gui.dragged_sb = SBAR_NONE;
3631#ifdef HAVE_GTK2
3632 /* Keep the "dragged_wp" value until after the scrolling, for when the
3633 * moust button is released. GTK2 doesn't send the button-up event. */
3634 gui.dragged_wp = NULL;
3635#endif
3636 }
3637
3638 /* Vertical sbar info is kept in the first sbar (the left one) */
3639 if (sb->wp != NULL)
3640 sb = &sb->wp->w_scrollbars[0];
3641
3642 /*
3643 * Check validity of value
3644 */
3645 if (value < 0)
3646 value = 0;
3647#ifdef SCROLL_PAST_END
3648 else if (value > sb->max)
3649 value = sb->max;
3650#else
3651 if (value > sb->max - sb->size + 1)
3652 value = sb->max - sb->size + 1;
3653#endif
3654
3655 sb->value = value;
3656
3657#ifdef USE_ON_FLY_SCROLL
3658 /* When not allowed to do the scrolling right now, return. */
3659 if (dont_scroll || input_available())
3660 return;
3661#endif
3662
3663#ifdef FEAT_RIGHTLEFT
3664 if (sb->wp == NULL && curwin->w_p_rl)
3665 {
3666 value = sb->max + 1 - sb->size - value;
3667 if (value < 0)
3668 value = 0;
3669 }
3670#endif
3671
3672 if (sb->wp != NULL) /* vertical scrollbar */
3673 {
3674 sb_num = 0;
3675#ifdef FEAT_WINDOWS
3676 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3677 sb_num++;
3678 if (wp == NULL)
3679 return;
3680#else
3681 if (sb->wp != curwin)
3682 return;
3683#endif
3684
3685#ifdef USE_ON_FLY_SCROLL
3686 current_scrollbar = sb_num;
3687 scrollbar_value = value;
3688 if (State & NORMAL)
3689 {
3690 gui_do_scroll();
3691 setcursor();
3692 }
3693 else if (State & INSERT)
3694 {
3695 ins_scroll();
3696 setcursor();
3697 }
3698 else if (State & CMDLINE)
3699 {
3700 if (msg_scrolled == 0)
3701 {
3702 gui_do_scroll();
3703 redrawcmdline();
3704 }
3705 }
3706# ifdef FEAT_FOLDING
3707 /* Value may have been changed for closed fold. */
3708 sb->value = sb->wp->w_topline - 1;
3709# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003710
3711 /* When dragging one scrollbar and there is another one at the other
3712 * side move the thumb of that one too. */
3713 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3714 gui_mch_set_scrollbar_thumb(
3715 &sb->wp->w_scrollbars[
3716 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3717 ? SBAR_LEFT : SBAR_RIGHT],
3718 sb->value, sb->size, sb->max);
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720#else
3721 bytes[0] = CSI;
3722 bytes[1] = KS_VER_SCROLLBAR;
3723 bytes[2] = KE_FILLER;
3724 bytes[3] = (char_u)sb_num;
3725 byte_count = 4;
3726#endif
3727 }
3728 else
3729 {
3730#ifdef USE_ON_FLY_SCROLL
3731 scrollbar_value = value;
3732
3733 if (State & NORMAL)
3734 gui_do_horiz_scroll();
3735 else if (State & INSERT)
3736 ins_horscroll();
3737 else if (State & CMDLINE)
3738 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003739 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
3741 gui_do_horiz_scroll();
3742 redrawcmdline();
3743 }
3744 }
3745 if (old_leftcol != curwin->w_leftcol)
3746 {
3747 updateWindow(curwin); /* update window, status and cmdline */
3748 setcursor();
3749 }
3750#else
3751 bytes[0] = CSI;
3752 bytes[1] = KS_HOR_SCROLLBAR;
3753 bytes[2] = KE_FILLER;
3754 byte_count = 3;
3755#endif
3756 }
3757
3758#ifdef USE_ON_FLY_SCROLL
3759# ifdef FEAT_SCROLLBIND
3760 /*
3761 * synchronize other windows, as necessary according to 'scrollbind'
3762 */
3763 if (curwin->w_p_scb
3764 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3765 || (sb->wp == curwin && (curwin->w_topline != old_topline
3766# ifdef FEAT_DIFF
3767 || curwin->w_topfill != old_topfill
3768# endif
3769 ))))
3770 {
3771 do_check_scrollbind(TRUE);
3772 /* need to update the window right here */
3773 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3774 if (wp->w_redr_type > 0)
3775 updateWindow(wp);
3776 setcursor();
3777 }
3778# endif
3779 out_flush();
3780 gui_update_cursor(FALSE, TRUE);
3781#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003782 add_to_input_buf(bytes, byte_count);
3783 add_long_to_buf((long_u)value, bytes);
3784 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785#endif
3786}
3787
3788/*
3789 * Scrollbar stuff:
3790 */
3791
3792 void
3793gui_update_scrollbars(force)
3794 int force; /* Force all scrollbars to get updated */
3795{
3796 win_T *wp;
3797 scrollbar_T *sb;
3798 long val, size, max; /* need 32 bits here */
3799 int which_sb;
3800 int h, y;
3801#ifdef FEAT_VERTSPLIT
3802 static win_T *prev_curwin = NULL;
3803#endif
3804
3805 /* Update the horizontal scrollbar */
3806 gui_update_horiz_scrollbar(force);
3807
3808#ifndef WIN3264
3809 /* Return straight away if there is neither a left nor right scrollbar.
3810 * On MS-Windows this is required anyway for scrollwheel messages. */
3811 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3812 return;
3813#endif
3814
3815 /*
3816 * Don't want to update a scrollbar while we're dragging it. But if we
3817 * have both a left and right scrollbar, and we drag one of them, we still
3818 * need to update the other one.
3819 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003820 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3821 && gui.which_scrollbars[SBAR_LEFT]
3822 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 /*
3825 * If we have two scrollbars and one of them is being dragged, just
3826 * copy the scrollbar position from the dragged one to the other one.
3827 */
3828 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3829 if (gui.dragged_wp != NULL)
3830 gui_mch_set_scrollbar_thumb(
3831 &gui.dragged_wp->w_scrollbars[which_sb],
3832 gui.dragged_wp->w_scrollbars[0].value,
3833 gui.dragged_wp->w_scrollbars[0].size,
3834 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836
3837 /* avoid that moving components around generates events */
3838 ++hold_gui_events;
3839
3840 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3841 {
3842 if (wp->w_buffer == NULL) /* just in case */
3843 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003844 /* Skip a scrollbar that is being dragged. */
3845 if (!force && (gui.dragged_sb == SBAR_LEFT
3846 || gui.dragged_sb == SBAR_RIGHT)
3847 && gui.dragged_wp == wp)
3848 continue;
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#ifdef SCROLL_PAST_END
3851 max = wp->w_buffer->b_ml.ml_line_count - 1;
3852#else
3853 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3854#endif
3855 if (max < 0) /* empty buffer */
3856 max = 0;
3857 val = wp->w_topline - 1;
3858 size = wp->w_height;
3859#ifdef SCROLL_PAST_END
3860 if (val > max) /* just in case */
3861 val = max;
3862#else
3863 if (size > max + 1) /* just in case */
3864 size = max + 1;
3865 if (val > max - size + 1)
3866 val = max - size + 1;
3867#endif
3868 if (val < 0) /* minimal value is 0 */
3869 val = 0;
3870
3871 /*
3872 * Scrollbar at index 0 (the left one) contains all the information.
3873 * It would be the same info for left and right so we just store it for
3874 * one of them.
3875 */
3876 sb = &wp->w_scrollbars[0];
3877
3878 /*
3879 * Note: no check for valid w_botline. If it's not valid the
3880 * scrollbars will be updated later anyway.
3881 */
3882 if (size < 1 || wp->w_botline - 2 > max)
3883 {
3884 /*
3885 * This can happen during changing files. Just don't update the
3886 * scrollbar for now.
3887 */
3888 sb->height = 0; /* Force update next time */
3889 if (gui.which_scrollbars[SBAR_LEFT])
3890 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3891 if (gui.which_scrollbars[SBAR_RIGHT])
3892 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3893 continue;
3894 }
3895 if (force || sb->height != wp->w_height
3896#ifdef FEAT_WINDOWS
3897 || sb->top != wp->w_winrow
3898 || sb->status_height != wp->w_status_height
3899# ifdef FEAT_VERTSPLIT
3900 || sb->width != wp->w_width
3901 || prev_curwin != curwin
3902# endif
3903#endif
3904 )
3905 {
3906 /* Height, width or position of scrollbar has changed. For
3907 * vertical split: curwin changed. */
3908 sb->height = wp->w_height;
3909#ifdef FEAT_WINDOWS
3910 sb->top = wp->w_winrow;
3911 sb->status_height = wp->w_status_height;
3912# ifdef FEAT_VERTSPLIT
3913 sb->width = wp->w_width;
3914# endif
3915#endif
3916
3917 /* Calculate height and position in pixels */
3918 h = (sb->height + sb->status_height) * gui.char_height;
3919 y = sb->top * gui.char_height + gui.border_offset;
3920#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
3921 if (gui.menu_is_active)
3922 y += gui.menu_height;
3923#endif
3924
3925#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
3926 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
3927# ifdef FEAT_GUI_ATHENA
3928 y += gui.toolbar_height;
3929# else
3930# ifdef FEAT_GUI_MSWIN
3931 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
3932# endif
3933# endif
3934#endif
3935
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003936#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
3937 if (gui_has_tabline())
3938 y += TABLINE_HEIGHT;
3939#endif
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#ifdef FEAT_WINDOWS
3942 if (wp->w_winrow == 0)
3943#endif
3944 {
3945 /* Height of top scrollbar includes width of top border */
3946 h += gui.border_offset;
3947 y -= gui.border_offset;
3948 }
3949 if (gui.which_scrollbars[SBAR_LEFT])
3950 {
3951 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
3952 gui.left_sbar_x, y,
3953 gui.scrollbar_width, h);
3954 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
3955 }
3956 if (gui.which_scrollbars[SBAR_RIGHT])
3957 {
3958 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
3959 gui.right_sbar_x, y,
3960 gui.scrollbar_width, h);
3961 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
3962 }
3963 }
3964
3965 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
3966 * checking if the thumb moved at least a pixel. Only do this for
3967 * Athena, most other GUIs require the update anyway to make the
3968 * arrows work. */
3969#ifdef FEAT_GUI_ATHENA
3970 if (max == 0)
3971 y = 0;
3972 else
3973 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
3974 if (force || sb->pixval != y || sb->size != size || sb->max != max)
3975#else
3976 if (force || sb->value != val || sb->size != size || sb->max != max)
3977#endif
3978 {
3979 /* Thumb of scrollbar has moved */
3980 sb->value = val;
3981#ifdef FEAT_GUI_ATHENA
3982 sb->pixval = y;
3983#endif
3984 sb->size = size;
3985 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003986 if (gui.which_scrollbars[SBAR_LEFT]
3987 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
3989 val, size, max);
3990 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003991 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
3993 val, size, max);
3994 }
3995 }
3996#ifdef FEAT_VERTSPLIT
3997 prev_curwin = curwin;
3998#endif
3999 --hold_gui_events;
4000}
4001
4002/*
4003 * Enable or disable a scrollbar.
4004 * Check for scrollbars for vertically split windows which are not enabled
4005 * sometimes.
4006 */
4007 static void
4008gui_do_scrollbar(wp, which, enable)
4009 win_T *wp;
4010 int which; /* SBAR_LEFT or SBAR_RIGHT */
4011 int enable; /* TRUE to enable scrollbar */
4012{
4013#ifdef FEAT_VERTSPLIT
4014 int midcol = curwin->w_wincol + curwin->w_width / 2;
4015 int has_midcol = (wp->w_wincol <= midcol
4016 && wp->w_wincol + wp->w_width >= midcol);
4017
4018 /* Only enable scrollbars that contain the middle column of the current
4019 * window. */
4020 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4021 {
4022 /* Scrollbars only on one side. Don't enable scrollbars that don't
4023 * contain the middle column of the current window. */
4024 if (!has_midcol)
4025 enable = FALSE;
4026 }
4027 else
4028 {
4029 /* Scrollbars on both sides. Don't enable scrollbars that neither
4030 * contain the middle column of the current window nor are on the far
4031 * side. */
4032 if (midcol > Columns / 2)
4033 {
4034 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4035 enable = FALSE;
4036 }
4037 else
4038 {
4039 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4040 : !has_midcol)
4041 enable = FALSE;
4042 }
4043 }
4044#endif
4045 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4046}
4047
4048/*
4049 * Scroll a window according to the values set in the globals current_scrollbar
4050 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4051 * or FALSE otherwise.
4052 */
4053 int
4054gui_do_scroll()
4055{
4056 win_T *wp, *save_wp;
4057 int i;
4058 long nlines;
4059 pos_T old_cursor;
4060 linenr_T old_topline;
4061#ifdef FEAT_DIFF
4062 int old_topfill;
4063#endif
4064
4065 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4066 if (wp == NULL)
4067 break;
4068 if (wp == NULL)
4069 /* Couldn't find window */
4070 return FALSE;
4071
4072 /*
4073 * Compute number of lines to scroll. If zero, nothing to do.
4074 */
4075 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4076 if (nlines == 0)
4077 return FALSE;
4078
4079 save_wp = curwin;
4080 old_topline = wp->w_topline;
4081#ifdef FEAT_DIFF
4082 old_topfill = wp->w_topfill;
4083#endif
4084 old_cursor = wp->w_cursor;
4085 curwin = wp;
4086 curbuf = wp->w_buffer;
4087 if (nlines < 0)
4088 scrolldown(-nlines, gui.dragged_wp == NULL);
4089 else
4090 scrollup(nlines, gui.dragged_wp == NULL);
4091 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4092 * the mouse-up event already, but we still want it to behave like when
4093 * dragging. But not the next click in an arrow. */
4094 if (gui.dragged_sb == SBAR_NONE)
4095 gui.dragged_wp = NULL;
4096
4097 if (old_topline != wp->w_topline
4098#ifdef FEAT_DIFF
4099 || old_topfill != wp->w_topfill
4100#endif
4101 )
4102 {
4103 if (p_so != 0)
4104 {
4105 cursor_correct(); /* fix window for 'so' */
4106 update_topline(); /* avoid up/down jump */
4107 }
4108 if (old_cursor.lnum != wp->w_cursor.lnum)
4109 coladvance(wp->w_curswant);
4110#ifdef FEAT_SCROLLBIND
4111 wp->w_scbind_pos = wp->w_topline;
4112#endif
4113 }
4114
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004115 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4116 validate_cursor();
4117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 curwin = save_wp;
4119 curbuf = save_wp->w_buffer;
4120
4121 /*
4122 * Don't call updateWindow() when nothing has changed (it will overwrite
4123 * the status line!).
4124 */
4125 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004126 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#ifdef FEAT_DIFF
4128 || old_topfill != wp->w_topfill
4129#endif
4130 )
4131 {
4132 redraw_win_later(wp, VALID);
4133 updateWindow(wp); /* update window, status line, and cmdline */
4134 }
4135
4136 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4137}
4138
4139
4140/*
4141 * Horizontal scrollbar stuff:
4142 */
4143
4144/*
4145 * Return length of line "lnum" for horizontal scrolling.
4146 */
4147 static colnr_T
4148scroll_line_len(lnum)
4149 linenr_T lnum;
4150{
4151 char_u *p;
4152 colnr_T col;
4153 int w;
4154
4155 p = ml_get(lnum);
4156 col = 0;
4157 if (*p != NUL)
4158 for (;;)
4159 {
4160 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004161 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 if (*p == NUL) /* don't count the last character */
4163 break;
4164 col += w;
4165 }
4166 return col;
4167}
4168
4169/* Remember which line is currently the longest, so that we don't have to
4170 * search for it when scrolling horizontally. */
4171static linenr_T longest_lnum = 0;
4172
4173 static void
4174gui_update_horiz_scrollbar(force)
4175 int force;
4176{
4177 long value, size, max; /* need 32 bit ints here */
4178
4179 if (!gui.which_scrollbars[SBAR_BOTTOM])
4180 return;
4181
4182 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4183 return;
4184
4185 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4186 return;
4187
4188 /*
4189 * It is possible for the cursor to be invalid if we're in the middle of
4190 * something (like changing files). If so, don't do anything for now.
4191 */
4192 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4193 {
4194 gui.bottom_sbar.value = -1;
4195 return;
4196 }
4197
4198 size = W_WIDTH(curwin);
4199 if (curwin->w_p_wrap)
4200 {
4201 value = 0;
4202#ifdef SCROLL_PAST_END
4203 max = 0;
4204#else
4205 max = W_WIDTH(curwin) - 1;
4206#endif
4207 }
4208 else
4209 {
4210 value = curwin->w_leftcol;
4211
4212 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4213 * line numbers, topline and botline can be invalid when displaying is
4214 * postponed. */
4215 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4216 && curwin->w_topline <= curwin->w_cursor.lnum
4217 && curwin->w_botline > curwin->w_cursor.lnum
4218 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4219 {
4220 linenr_T lnum;
4221 colnr_T n;
4222
4223 /* Use maximum of all visible lines. Remember the lnum of the
4224 * longest line, clostest to the cursor line. Used when scrolling
4225 * below. */
4226 max = 0;
4227 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4228 {
4229 n = scroll_line_len(lnum);
4230 if (n > (colnr_T)max)
4231 {
4232 max = n;
4233 longest_lnum = lnum;
4234 }
4235 else if (n == (colnr_T)max
4236 && abs((int)(lnum - curwin->w_cursor.lnum))
4237 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4238 longest_lnum = lnum;
4239 }
4240 }
4241 else
4242 /* Use cursor line only. */
4243 max = scroll_line_len(curwin->w_cursor.lnum);
4244#ifdef FEAT_VIRTUALEDIT
4245 if (virtual_active())
4246 {
4247 /* May move the cursor even further to the right. */
4248 if (curwin->w_virtcol >= (colnr_T)max)
4249 max = curwin->w_virtcol;
4250 }
4251#endif
4252
4253#ifndef SCROLL_PAST_END
4254 max += W_WIDTH(curwin) - 1;
4255#endif
4256 /* The line number isn't scrolled, thus there is less space when
4257 * 'number' is set (also for 'foldcolumn'). */
4258 size -= curwin_col_off();
4259#ifndef SCROLL_PAST_END
4260 max -= curwin_col_off();
4261#endif
4262 }
4263
4264#ifndef SCROLL_PAST_END
4265 if (value > max - size + 1)
4266 value = max - size + 1; /* limit the value to allowable range */
4267#endif
4268
4269#ifdef FEAT_RIGHTLEFT
4270 if (curwin->w_p_rl)
4271 {
4272 value = max + 1 - size - value;
4273 if (value < 0)
4274 {
4275 size += value;
4276 value = 0;
4277 }
4278 }
4279#endif
4280 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4281 && max == gui.bottom_sbar.max)
4282 return;
4283
4284 gui.bottom_sbar.value = value;
4285 gui.bottom_sbar.size = size;
4286 gui.bottom_sbar.max = max;
4287 gui.prev_wrap = curwin->w_p_wrap;
4288
4289 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4290}
4291
4292/*
4293 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4294 */
4295 int
4296gui_do_horiz_scroll()
4297{
4298 /* no wrapping, no scrolling */
4299 if (curwin->w_p_wrap)
4300 return FALSE;
4301
4302 if (curwin->w_leftcol == scrollbar_value)
4303 return FALSE;
4304
4305 curwin->w_leftcol = scrollbar_value;
4306
4307 /* When the line of the cursor is too short, move the cursor to the
4308 * longest visible line. Do a sanity check on "longest_lnum", just in
4309 * case. */
4310 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4311 && longest_lnum >= curwin->w_topline
4312 && longest_lnum < curwin->w_botline
4313 && !virtual_active())
4314 {
4315 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4316 {
4317 curwin->w_cursor.lnum = longest_lnum;
4318 curwin->w_cursor.col = 0;
4319 }
4320 }
4321
4322 return leftcol_changed();
4323}
4324
4325/*
4326 * Check that none of the colors are the same as the background color
4327 */
4328 void
4329gui_check_colors()
4330{
4331 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4332 {
4333 gui_set_bg_color((char_u *)"White");
4334 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4335 gui_set_fg_color((char_u *)"Black");
4336 }
4337}
4338
Bram Moolenaar3918c952005-03-15 22:34:55 +00004339 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340gui_set_fg_color(name)
4341 char_u *name;
4342{
4343 gui.norm_pixel = gui_get_color(name);
4344 hl_set_fg_color_name(vim_strsave(name));
4345}
4346
Bram Moolenaar3918c952005-03-15 22:34:55 +00004347 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348gui_set_bg_color(name)
4349 char_u *name;
4350{
4351 gui.back_pixel = gui_get_color(name);
4352 hl_set_bg_color_name(vim_strsave(name));
4353}
4354
4355/*
4356 * Allocate a color by name.
4357 * Returns INVALCOLOR and gives an error message when failed.
4358 */
4359 guicolor_T
4360gui_get_color(name)
4361 char_u *name;
4362{
4363 guicolor_T t;
4364
4365 if (*name == NUL)
4366 return INVALCOLOR;
4367 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004368
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004370#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 && gui.in_use
4372#endif
4373 )
4374 EMSG2(_("E254: Cannot allocate color %s"), name);
4375 return t;
4376}
4377
4378/*
4379 * Return the grey value of a color (range 0-255).
4380 */
4381 int
4382gui_get_lightness(pixel)
4383 guicolor_T pixel;
4384{
4385 long_u rgb = gui_mch_get_rgb(pixel);
4386
4387 return ( (((rgb >> 16) & 0xff) * 299)
4388 + (((rgb >> 8) & 0xff) * 587)
4389 + ((rgb & 0xff) * 114)) / 1000;
4390}
4391
4392#if defined(FEAT_GUI_X11) || defined(PROTO)
4393 void
4394gui_new_scrollbar_colors()
4395{
4396 win_T *wp;
4397
4398 /* Nothing to do if GUI hasn't started yet. */
4399 if (!gui.in_use)
4400 return;
4401
4402 FOR_ALL_WINDOWS(wp)
4403 {
4404 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4405 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4406 }
4407 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4408}
4409#endif
4410
4411/*
4412 * Call this when focus has changed.
4413 */
4414 void
4415gui_focus_change(in_focus)
4416 int in_focus;
4417{
4418/*
4419 * Skip this code to avoid drawing the cursor when debugging and switching
4420 * between the debugger window and gvim.
4421 */
4422#if 1
4423 gui.in_focus = in_focus;
4424 out_flush(); /* make sure output has been written */
4425 gui_update_cursor(TRUE, FALSE);
4426
4427# ifdef FEAT_XIM
4428 xim_set_focus(in_focus);
4429# endif
4430
4431 ui_focus_change(in_focus);
4432#endif
4433}
4434
4435/*
4436 * Called when the mouse moved (but not when dragging).
4437 */
4438 void
4439gui_mouse_moved(x, y)
4440 int x;
4441 int y;
4442{
4443 win_T *wp;
4444 char_u st[6];
4445
Bram Moolenaard3667a22006-03-16 21:35:52 +00004446 /* Ignore this while still starting up. */
4447 if (!gui.in_use || gui.starting)
4448 return;
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#ifdef FEAT_MOUSESHAPE
4451 /* Get window pointer, and update mouse shape as well. */
4452 wp = xy2win(x, y);
4453#endif
4454
4455 /* Only handle this when 'mousefocus' set and ... */
4456 if (p_mousef
4457 && !hold_gui_events /* not holding events */
4458 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4459 && State != HITRETURN /* but not hit-return prompt */
4460 && msg_scrolled == 0 /* no scrolled message */
4461 && !need_mouse_correct /* not moving the pointer */
4462 && gui.in_focus) /* gvim in focus */
4463 {
4464 /* Don't move the mouse when it's left or right of the Vim window */
4465 if (x < 0 || x > Columns * gui.char_width)
4466 return;
4467#ifndef FEAT_MOUSESHAPE
4468 wp = xy2win(x, y);
4469#endif
4470 if (wp == curwin || wp == NULL)
4471 return; /* still in the same old window, or none at all */
4472
4473 /*
4474 * format a mouse click on status line input
4475 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004476 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4477 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 */
4479 if (finish_op)
4480 {
4481 /* abort the current operator first */
4482 st[0] = ESC;
4483 add_to_input_buf(st, 1);
4484 }
4485 st[0] = CSI;
4486 st[1] = KS_MOUSE;
4487 st[2] = KE_FILLER;
4488 st[3] = (char_u)MOUSE_LEFT;
4489 fill_mouse_coord(st + 4,
4490#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004491 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492#else
4493 -1,
4494#endif
4495 wp->w_height + W_WINROW(wp));
4496
4497 add_to_input_buf(st, 8);
4498 st[3] = (char_u)MOUSE_RELEASE;
4499 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500#ifdef FEAT_GUI_GTK
4501 /* Need to wake up the main loop */
4502 if (gtk_main_level() > 0)
4503 gtk_main_quit();
4504#endif
4505 }
4506}
4507
4508/*
4509 * Called when mouse should be moved to window with focus.
4510 */
4511 void
4512gui_mouse_correct()
4513{
4514 int x, y;
4515 win_T *wp = NULL;
4516
4517 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004518
4519 if (!(gui.in_use && p_mousef))
4520 return;
4521
4522 gui_mch_getmouse(&x, &y);
4523 /* Don't move the mouse when it's left or right of the Vim window */
4524 if (x < 0 || x > Columns * gui.char_width)
4525 return;
4526 if (y >= 0)
4527 wp = xy2win(x, y);
4528 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004530 validate_cline_row();
4531 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4532 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
4535}
4536
4537/*
4538 * Find window where the mouse pointer "y" coordinate is in.
4539 */
4540/*ARGSUSED*/
4541 static win_T *
4542xy2win(x, y)
4543 int x;
4544 int y;
4545{
4546#ifdef FEAT_WINDOWS
4547 int row;
4548 int col;
4549 win_T *wp;
4550
4551 row = Y_2_ROW(y);
4552 col = X_2_COL(x);
4553 if (row < 0 || col < 0) /* before first window */
4554 return NULL;
4555 wp = mouse_find_win(&row, &col);
4556# ifdef FEAT_MOUSESHAPE
4557 if (State == HITRETURN || State == ASKMORE)
4558 {
4559 if (Y_2_ROW(y) >= msg_row)
4560 update_mouseshape(SHAPE_IDX_MOREL);
4561 else
4562 update_mouseshape(SHAPE_IDX_MORE);
4563 }
4564 else if (row > wp->w_height) /* below status line */
4565 update_mouseshape(SHAPE_IDX_CLINE);
4566# ifdef FEAT_VERTSPLIT
4567 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4568 && (row != wp->w_height || !stl_connected(wp)))
4569 update_mouseshape(SHAPE_IDX_VSEP);
4570# endif
4571 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4572 && row == wp->w_height)
4573 update_mouseshape(SHAPE_IDX_STATUS);
4574 else
4575 update_mouseshape(-2);
4576# endif
4577 return wp;
4578#else
4579 return firstwin;
4580#endif
4581}
4582
4583/*
4584 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4585 * File names may be given to redefine the args list.
4586 */
4587 void
4588ex_gui(eap)
4589 exarg_T *eap;
4590{
4591 char_u *arg = eap->arg;
4592
4593 /*
4594 * Check for "-f" argument: foreground, don't fork.
4595 * Also don't fork when started with "gvim -f".
4596 * Do fork when using "gui -b".
4597 */
4598 if (arg[0] == '-'
4599 && (arg[1] == 'f' || arg[1] == 'b')
4600 && (arg[2] == NUL || vim_iswhite(arg[2])))
4601 {
4602 gui.dofork = (arg[1] == 'b');
4603 eap->arg = skipwhite(eap->arg + 2);
4604 }
4605 if (!gui.in_use)
4606 {
4607 /* Clear the command. Needed for when forking+exiting, to avoid part
4608 * of the argument ending up after the shell prompt. */
4609 msg_clr_eos_force();
4610 gui_start();
4611 }
4612 if (!ends_excmd(*eap->arg))
4613 ex_next(eap);
4614}
4615
4616#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004617 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618/*
4619 * This is shared between Athena, Motif and GTK.
4620 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004621static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623/*
4624 * Callback function for do_in_runtimepath().
4625 */
4626 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004627gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004629 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004631 char_u *gfp_buffer = cookie;
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (STRLEN(fname) >= MAXPATHL)
4634 *gfp_buffer = NUL;
4635 else
4636 STRCPY(gfp_buffer, fname);
4637}
4638
4639/*
4640 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4641 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4642 */
4643 int
4644gui_find_bitmap(name, buffer, ext)
4645 char_u *name;
4646 char_u *buffer;
4647 char *ext;
4648{
4649 if (STRLEN(name) > MAXPATHL - 14)
4650 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004651 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004652 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4653 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return FAIL;
4655 return OK;
4656}
4657
4658# if !defined(HAVE_GTK2) || defined(PROTO)
4659/*
4660 * Given the name of the "icon=" argument, try finding the bitmap file for the
4661 * icon. If it is an absolute path name, use it as it is. Otherwise append
4662 * "ext" and search for it in 'runtimepath'.
4663 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4664 * contains "name".
4665 */
4666 void
4667gui_find_iconfile(name, buffer, ext)
4668 char_u *name;
4669 char_u *buffer;
4670 char *ext;
4671{
4672 char_u buf[MAXPATHL + 1];
4673
4674 expand_env(name, buffer, MAXPATHL);
4675 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4676 STRCPY(buffer, buf);
4677}
4678# endif
4679#endif
4680
Bram Moolenaar9372a112005-12-06 19:59:18 +00004681#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 void
4683display_errors()
4684{
4685 char_u *p;
4686
4687 if (isatty(2))
4688 fflush(stderr);
4689 else if (error_ga.ga_data != NULL)
4690 {
4691 /* avoid putting up a message box with blanks only */
4692 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4693 if (!isspace(*p))
4694 {
4695 /* Truncate a very long message, it will go off-screen. */
4696 if (STRLEN(p) > 2000)
4697 STRCPY(p + 2000 - 14, "...(truncated)");
4698 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4699 p, (char_u *)_("&Ok"), 1, NULL);
4700 break;
4701 }
4702 ga_clear(&error_ga);
4703 }
4704}
4705#endif
4706
4707#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4708/*
4709 * Return TRUE if still starting up and there is no place to enter text.
4710 * For GTK and X11 we check if stderr is not a tty, which means we were
4711 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4712 * allow typing on stdin.
4713 */
4714 int
4715no_console_input()
4716{
4717 return ((!gui.in_use || gui.starting)
4718# ifndef NO_CONSOLE
4719 && !isatty(0) && !isatty(2)
4720# endif
4721 );
4722}
4723#endif
4724
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004725#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4726 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727/*
4728 * Update the current window and the screen.
4729 */
4730 void
4731gui_update_screen()
4732{
4733 update_topline();
4734 validate_cursor();
4735 update_screen(0); /* may need to update the screen */
4736 setcursor();
4737 out_flush(); /* make sure output has been written */
4738 gui_update_cursor(TRUE, FALSE);
4739 gui_mch_flush();
4740}
4741#endif
4742
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004743#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4745
4746/*
4747 * Get the text to use in a find/replace dialog. Uses the last search pattern
4748 * if the argument is empty.
4749 * Returns an allocated string.
4750 */
4751 char_u *
4752get_find_dialog_text(arg, wwordp, mcasep)
4753 char_u *arg;
4754 int *wwordp; /* return: TRUE if \< \> found */
4755 int *mcasep; /* return: TRUE if \C found */
4756{
4757 char_u *text;
4758
4759 if (*arg == NUL)
4760 text = last_search_pat();
4761 else
4762 text = arg;
4763 if (text != NULL)
4764 {
4765 text = vim_strsave(text);
4766 if (text != NULL)
4767 {
4768 int len = STRLEN(text);
4769 int i;
4770
4771 /* Remove "\V" */
4772 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4773 {
4774 mch_memmove(text, text + 2, (size_t)(len - 1));
4775 len -= 2;
4776 }
4777
4778 /* Recognize "\c" and "\C" and remove. */
4779 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4780 {
4781 *mcasep = (text[1] == 'C');
4782 mch_memmove(text, text + 2, (size_t)(len - 1));
4783 len -= 2;
4784 }
4785
4786 /* Recognize "\<text\>" and remove. */
4787 if (len >= 4
4788 && STRNCMP(text, "\\<", 2) == 0
4789 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4790 {
4791 *wwordp = TRUE;
4792 mch_memmove(text, text + 2, (size_t)(len - 4));
4793 text[len - 4] = NUL;
4794 }
4795
4796 /* Recognize "\/" or "\?" and remove. */
4797 for (i = 0; i + 1 < len; ++i)
4798 if (text[i] == '\\' && (text[i + 1] == '/'
4799 || text[i + 1] == '?'))
4800 {
4801 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4802 --len;
4803 }
4804 }
4805 }
4806 return text;
4807}
4808
4809/*
4810 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4811 */
4812 static void
4813concat_esc(gap, text, what)
4814 garray_T *gap;
4815 char_u *text;
4816 int what;
4817{
4818 while (*text != NUL)
4819 {
4820#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004821 int l = (*mb_ptr2len)(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (l > 1)
4823 {
4824 while (--l >= 0)
4825 ga_append(gap, *text++);
4826 continue;
4827 }
4828#endif
4829 if (*text == what)
4830 ga_append(gap, '\\');
4831 ga_append(gap, *text);
4832 ++text;
4833 }
4834}
4835
4836/*
4837 * Handle the press of a button in the find-replace dialog.
4838 * Return TRUE when something was added to the input buffer.
4839 */
4840 int
4841gui_do_findrepl(flags, find_text, repl_text, down)
4842 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4843 char_u *find_text;
4844 char_u *repl_text;
4845 int down; /* Search downwards. */
4846{
4847 garray_T ga;
4848 int i;
4849 int type = (flags & FRD_TYPE_MASK);
4850 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004851 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004852 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004855 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 ga_concat(&ga, (char_u *)"%s/");
4857
4858 ga_concat(&ga, (char_u *)"\\V");
4859 if (flags & FRD_MATCH_CASE)
4860 ga_concat(&ga, (char_u *)"\\C");
4861 else
4862 ga_concat(&ga, (char_u *)"\\c");
4863 if (flags & FRD_WHOLE_WORD)
4864 ga_concat(&ga, (char_u *)"\\<");
4865 if (type == FRD_REPLACEALL || down)
4866 concat_esc(&ga, find_text, '/'); /* escape slashes */
4867 else
4868 concat_esc(&ga, find_text, '?'); /* escape '?' */
4869 if (flags & FRD_WHOLE_WORD)
4870 ga_concat(&ga, (char_u *)"\\>");
4871
4872 if (type == FRD_REPLACEALL)
4873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004875 /* escape / and \ */
4876 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
4877 if (p != NULL)
4878 ga_concat(&ga, p);
4879 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004881 }
4882 ga_append(&ga, NUL);
4883
4884 if (type == FRD_REPLACE)
4885 {
4886 /* Do the replacement when the text at the cursor matches. Thus no
4887 * replacement is done if the cursor was moved! */
4888 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
4889 regmatch.rm_ic = 0;
4890 if (regmatch.regprog != NULL)
4891 {
4892 p = ml_get_cursor();
4893 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
4894 && regmatch.startp[0] == p)
4895 {
4896 /* Clear the command line to remove any old "No match"
4897 * error. */
4898 msg_end_prompt();
4899
4900 if (u_save_cursor() == OK)
4901 {
4902 /* A button was pressed thus undo should be synced. */
4903 if (no_u_sync == 0)
4904 u_sync();
4905
4906 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00004907 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004908 ins_str(repl_text);
4909 }
4910 }
4911 else
4912 MSG(_("No match at cursor, finding next"));
4913 vim_free(regmatch.regprog);
4914 }
4915 }
4916
4917 if (type == FRD_REPLACEALL)
4918 {
4919 /* A button was pressed, thus undo should be synced. */
4920 if (no_u_sync == 0)
4921 u_sync();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 do_cmdline_cmd(ga.ga_data);
4923 }
4924 else
4925 {
4926 /* Search for the next match. */
4927 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
4929 SEARCH_MSG + SEARCH_MARK);
4930 msg_scroll = i; /* don't let an error message set msg_scroll */
4931 }
4932
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004933 /* Don't want to pass did_emsg to other code, it may cause disabling
4934 * syntax HL if we were busy redrawing. */
4935 did_emsg = save_did_emsg;
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (State & (NORMAL | INSERT))
4938 {
4939 gui_update_screen(); /* update the screen */
4940 msg_didout = 0; /* overwrite any message */
4941 need_wait_return = FALSE; /* don't wait for return */
4942 }
4943
4944 vim_free(ga.ga_data);
4945 return (ga.ga_len > 0);
4946}
4947
4948#endif
4949
4950#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
4951 || defined(FEAT_GUI_MSWIN) \
4952 || defined(FEAT_GUI_MAC) \
4953 || defined(PROTO)
4954
4955#ifdef FEAT_WINDOWS
4956static void gui_wingoto_xy __ARGS((int x, int y));
4957
4958/*
4959 * Jump to the window at specified point (x, y).
4960 */
4961 static void
4962gui_wingoto_xy(x, y)
4963 int x;
4964 int y;
4965{
4966 int row = Y_2_ROW(y);
4967 int col = X_2_COL(x);
4968 win_T *wp;
4969
4970 if (row >= 0 && col >= 0)
4971 {
4972 wp = mouse_find_win(&row, &col);
4973 if (wp != NULL && wp != curwin)
4974 win_goto(wp);
4975 }
4976}
4977#endif
4978
4979/*
4980 * Process file drop. Mouse cursor position, key modifiers, name of files
4981 * and count of files are given. Argument "fnames[count]" has full pathnames
4982 * of dropped files, they will be freed in this function, and caller can't use
4983 * fnames after call this function.
4984 */
4985/*ARGSUSED*/
4986 void
4987gui_handle_drop(x, y, modifiers, fnames, count)
4988 int x;
4989 int y;
4990 int_u modifiers;
4991 char_u **fnames;
4992 int count;
4993{
4994 int i;
4995 char_u *p;
4996
4997 /*
4998 * When the cursor is at the command line, add the file names to the
4999 * command line, don't edit the files.
5000 */
5001 if (State & CMDLINE)
5002 {
5003 shorten_filenames(fnames, count);
5004 for (i = 0; i < count; ++i)
5005 {
5006 if (fnames[i] != NULL)
5007 {
5008 if (i > 0)
5009 add_to_input_buf((char_u*)" ", 1);
5010
5011 /* We don't know what command is used thus we can't be sure
5012 * about which characters need to be escaped. Only escape the
5013 * most common ones. */
5014# ifdef BACKSLASH_IN_FILENAME
5015 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5016# else
5017 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5018# endif
5019 if (p != NULL)
5020 add_to_input_buf(p, (int)STRLEN(p));
5021 vim_free(p);
5022 vim_free(fnames[i]);
5023 }
5024 }
5025 vim_free(fnames);
5026 }
5027 else
5028 {
5029 /* Go to the window under mouse cursor, then shorten given "fnames" by
5030 * current window, because a window can have local current dir. */
5031# ifdef FEAT_WINDOWS
5032 gui_wingoto_xy(x, y);
5033# endif
5034 shorten_filenames(fnames, count);
5035
5036 /* If Shift held down, remember the first item. */
5037 if ((modifiers & MOUSE_SHIFT) != 0)
5038 p = vim_strsave(fnames[0]);
5039 else
5040 p = NULL;
5041
5042 /* Handle the drop, :edit or :split to get to the file. This also
5043 * frees fnames[]. Skip this if there is only one item it's a
5044 * directory and Shift is held down. */
5045 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5046 && mch_isdir(fnames[0]))
5047 {
5048 vim_free(fnames[0]);
5049 vim_free(fnames);
5050 }
5051 else
5052 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5053
5054 /* If Shift held down, change to first file's directory. If the first
5055 * item is a directory, change to that directory (and let the explorer
5056 * plugin show the contents). */
5057 if (p != NULL)
5058 {
5059 if (mch_isdir(p))
5060 {
5061 if (mch_chdir((char *)p) == 0)
5062 shorten_fnames(TRUE);
5063 }
5064 else if (vim_chdirfile(p) == OK)
5065 shorten_fnames(TRUE);
5066 vim_free(p);
5067 }
5068
5069 /* Update the screen display */
5070 update_screen(NOT_VALID);
5071# ifdef FEAT_MENU
5072 gui_update_menus(0);
5073# endif
5074 setcursor();
5075 out_flush();
5076 gui_update_cursor(FALSE, FALSE);
5077 gui_mch_flush();
5078 }
5079}
5080#endif