blob: 88d97a326ad811c3a9573fe5a1e61f829b6c67ad [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,
349 (char_u *)"a", OPT_FREE);
350
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,
791 wide_name, OPT_FREE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000792# ifdef FEAT_EVAL
793 set_option_scriptID((char_u *)"gfw", current_SID);
794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
796 }
797 break;
798 }
799 }
800 }
801}
802# endif /* !HAVE_GTK2 */
803
804/*
805 * Get the font for 'guifontwide'.
806 * Return FAIL for an invalid font name.
807 */
808 int
809gui_get_wide_font()
810{
811 GuiFont font = NOFONT;
812 char_u font_name[FONTLEN];
813 char_u *p;
814
815 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
816 return OK; /* Will give an error message later. */
817
818 if (p_guifontwide != NULL && *p_guifontwide != NUL)
819 {
820 for (p = p_guifontwide; *p != NUL; )
821 {
822 /* Isolate one comma separated font name. */
823 (void)copy_option_part(&p, font_name, FONTLEN, ",");
824 font = gui_mch_get_font(font_name, FALSE);
825 if (font != NOFONT)
826 break;
827 }
828 if (font == NOFONT)
829 return FAIL;
830 }
831
832 gui_mch_free_font(gui.wide_font);
833#ifdef HAVE_GTK2
834 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
835 if (font != NOFONT && gui.norm_font != NOFONT
836 && pango_font_description_equal(font, gui.norm_font))
837 {
838 gui.wide_font = NOFONT;
839 gui_mch_free_font(font);
840 }
841 else
842#endif
843 gui.wide_font = font;
844 return OK;
845}
846#endif
847
848 void
849gui_set_cursor(row, col)
850 int row;
851 int col;
852{
853 gui.row = row;
854 gui.col = col;
855}
856
857/*
858 * gui_check_pos - check if the cursor is on the screen.
859 */
860 static void
861gui_check_pos()
862{
863 if (gui.row >= screen_Rows)
864 gui.row = screen_Rows - 1;
865 if (gui.col >= screen_Columns)
866 gui.col = screen_Columns - 1;
867 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
868 gui.cursor_is_valid = FALSE;
869}
870
871/*
872 * Redraw the cursor if necessary or when forced.
873 * Careful: The contents of ScreenLines[] must match what is on the screen,
874 * otherwise this goes wrong. May need to call out_flush() first.
875 */
876 void
877gui_update_cursor(force, clear_selection)
878 int force; /* when TRUE, update even when not moved */
879 int clear_selection;/* clear selection under cursor */
880{
881 int cur_width = 0;
882 int cur_height = 0;
883 int old_hl_mask;
884 int idx;
885 int id;
886 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
887 int cattr; /* cursor attributes */
888 int attr;
889 attrentry_T *aep = NULL;
890
891 /* Don't update the cursor when halfway busy scrolling.
892 * ScreenLines[] isn't valid then. */
893 if (!can_update_cursor)
894 return;
895
896 gui_check_pos();
897 if (!gui.cursor_is_valid || force
898 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
899 {
900 gui_undraw_cursor();
901 if (gui.row < 0)
902 return;
903#ifdef USE_IM_CONTROL
904 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
905 im_set_position(gui.row, gui.col);
906#endif
907 gui.cursor_row = gui.row;
908 gui.cursor_col = gui.col;
909
910 /* Only write to the screen after ScreenLines[] has been initialized */
911 if (!screen_cleared || ScreenLines == NULL)
912 return;
913
914 /* Clear the selection if we are about to write over it */
915 if (clear_selection)
916 clip_may_clear_selection(gui.row, gui.row);
917 /* Check that the cursor is inside the shell (resizing may have made
918 * it invalid) */
919 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
920 return;
921
922 gui.cursor_is_valid = TRUE;
923
924 /*
925 * How the cursor is drawn depends on the current mode.
926 */
927 idx = get_shape_idx(FALSE);
928 if (State & LANGMAP)
929 id = shape_table[idx].id_lm;
930 else
931 id = shape_table[idx].id;
932
933 /* get the colors and attributes for the cursor. Default is inverted */
934 cfg = INVALCOLOR;
935 cbg = INVALCOLOR;
936 cattr = HL_INVERSE;
937 gui_mch_set_blinking(shape_table[idx].blinkwait,
938 shape_table[idx].blinkon,
939 shape_table[idx].blinkoff);
940 if (id > 0)
941 {
942 cattr = syn_id2colors(id, &cfg, &cbg);
943#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
944 {
945 static int iid;
946 guicolor_T fg, bg;
947
948 if (im_get_status())
949 {
950 iid = syn_name2id((char_u *)"CursorIM");
951 if (iid > 0)
952 {
953 syn_id2colors(iid, &fg, &bg);
954 if (bg != INVALCOLOR)
955 cbg = bg;
956 if (fg != INVALCOLOR)
957 cfg = fg;
958 }
959 }
960 }
961#endif
962 }
963
964 /*
965 * Get the attributes for the character under the cursor.
966 * When no cursor color was given, use the character color.
967 */
968 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
969 if (attr > HL_ALL)
970 aep = syn_gui_attr2entry(attr);
971 if (aep != NULL)
972 {
973 attr = aep->ae_attr;
974 if (cfg == INVALCOLOR)
975 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
976 : aep->ae_u.gui.fg_color);
977 if (cbg == INVALCOLOR)
978 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
979 : aep->ae_u.gui.bg_color);
980 }
981 if (cfg == INVALCOLOR)
982 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
983 if (cbg == INVALCOLOR)
984 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
985
986#ifdef FEAT_XIM
987 if (aep != NULL)
988 {
989 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
990 : aep->ae_u.gui.bg_color);
991 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
992 : aep->ae_u.gui.fg_color);
993 if (xim_bg_color == INVALCOLOR)
994 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
995 : gui.back_pixel;
996 if (xim_fg_color == INVALCOLOR)
997 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
998 : gui.norm_pixel;
999 }
1000 else
1001 {
1002 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1003 : gui.back_pixel;
1004 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1005 : gui.norm_pixel;
1006 }
1007#endif
1008
1009 attr &= ~HL_INVERSE;
1010 if (cattr & HL_INVERSE)
1011 {
1012 cc = cbg;
1013 cbg = cfg;
1014 cfg = cc;
1015 }
1016 cattr &= ~HL_INVERSE;
1017
1018 /*
1019 * When we don't have window focus, draw a hollow cursor.
1020 */
1021 if (!gui.in_focus)
1022 {
1023 gui_mch_draw_hollow_cursor(cbg);
1024 return;
1025 }
1026
1027 old_hl_mask = gui.highlight_mask;
1028 if (shape_table[idx].shape == SHAPE_BLOCK
1029#ifdef FEAT_HANGULIN
1030 || composing_hangul
1031#endif
1032 )
1033 {
1034 /*
1035 * Draw the text character with the cursor colors. Use the
1036 * character attributes plus the cursor attributes.
1037 */
1038 gui.highlight_mask = (cattr | attr);
1039#ifdef FEAT_HANGULIN
1040 if (composing_hangul)
1041 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1042 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1043 else
1044#endif
1045 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1046 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1047 }
1048 else
1049 {
1050#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1051 int col_off = FALSE;
1052#endif
1053 /*
1054 * First draw the partial cursor, then overwrite with the text
1055 * character, using a transparent background.
1056 */
1057 if (shape_table[idx].shape == SHAPE_VER)
1058 {
1059 cur_height = gui.char_height;
1060 cur_width = (gui.char_width * shape_table[idx].percentage
1061 + 99) / 100;
1062 }
1063 else
1064 {
1065 cur_height = (gui.char_height * shape_table[idx].percentage
1066 + 99) / 100;
1067 cur_width = gui.char_width;
1068 }
1069#ifdef FEAT_MBYTE
1070 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col) > 1)
1071 {
1072 /* Double wide character. */
1073 if (shape_table[idx].shape != SHAPE_VER)
1074 cur_width += gui.char_width;
1075# ifdef FEAT_RIGHTLEFT
1076 if (CURSOR_BAR_RIGHT)
1077 {
1078 /* gui.col points to the left halve of the character but
1079 * the vertical line needs to be on the right halve.
1080 * A double-wide horizontal line is also drawn from the
1081 * right halve in gui_mch_draw_part_cursor(). */
1082 col_off = TRUE;
1083 ++gui.col;
1084 }
1085# endif
1086 }
1087#endif
1088 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1089#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1090 if (col_off)
1091 --gui.col;
1092#endif
1093
1094#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1095 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1096 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1097 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1098 (guicolor_T)0, (guicolor_T)0, 0);
1099#endif
1100 }
1101 gui.highlight_mask = old_hl_mask;
1102 }
1103}
1104
1105#if defined(FEAT_MENU) || defined(PROTO)
1106 void
1107gui_position_menu()
1108{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001109# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (gui.menu_is_active && gui.in_use)
1111 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1112# endif
1113}
1114#endif
1115
1116/*
1117 * Position the various GUI components (text area, menu). The vertical
1118 * scrollbars are NOT handled here. See gui_update_scrollbars().
1119 */
1120/*ARGSUSED*/
1121 static void
1122gui_position_components(total_width)
1123 int total_width;
1124{
1125 int text_area_x;
1126 int text_area_y;
1127 int text_area_width;
1128 int text_area_height;
1129
1130 /* avoid that moving components around generates events */
1131 ++hold_gui_events;
1132
1133 text_area_x = 0;
1134 if (gui.which_scrollbars[SBAR_LEFT])
1135 text_area_x += gui.scrollbar_width;
1136
1137 text_area_y = 0;
1138#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1139 gui.menu_width = total_width;
1140 if (gui.menu_is_active)
1141 text_area_y += gui.menu_height;
1142#endif
1143#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1144 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1145 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1146#endif
1147
1148#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1149 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1150 {
1151# ifdef FEAT_GUI_ATHENA
1152 gui_mch_set_toolbar_pos(0, text_area_y,
1153 gui.menu_width, gui.toolbar_height);
1154# endif
1155 text_area_y += gui.toolbar_height;
1156 }
1157#endif
1158
1159 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1160 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1161
1162 gui_mch_set_text_area_pos(text_area_x,
1163 text_area_y,
1164 text_area_width,
1165 text_area_height
1166#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1167 + xim_get_status_area_height()
1168#endif
1169 );
1170#ifdef FEAT_MENU
1171 gui_position_menu();
1172#endif
1173 if (gui.which_scrollbars[SBAR_BOTTOM])
1174 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1175 text_area_x,
1176 text_area_y + text_area_height,
1177 text_area_width,
1178 gui.scrollbar_height);
1179 gui.left_sbar_x = 0;
1180 gui.right_sbar_x = text_area_x + text_area_width;
1181
1182 --hold_gui_events;
1183}
1184
Bram Moolenaar02743632005-07-25 20:42:36 +00001185/*
1186 * Get the width of the widgets and decorations to the side of the text area.
1187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 int
1189gui_get_base_width()
1190{
1191 int base_width;
1192
1193 base_width = 2 * gui.border_offset;
1194 if (gui.which_scrollbars[SBAR_LEFT])
1195 base_width += gui.scrollbar_width;
1196 if (gui.which_scrollbars[SBAR_RIGHT])
1197 base_width += gui.scrollbar_width;
1198 return base_width;
1199}
1200
Bram Moolenaar02743632005-07-25 20:42:36 +00001201/*
1202 * Get the height of the widgets and decorations above and below the text area.
1203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 int
1205gui_get_base_height()
1206{
1207 int base_height;
1208
1209 base_height = 2 * gui.border_offset;
1210 if (gui.which_scrollbars[SBAR_BOTTOM])
1211 base_height += gui.scrollbar_height;
1212#ifdef FEAT_GUI_GTK
1213 /* We can't take the sizes properly into account until anything is
1214 * realized. Therefore we recalculate all the values here just before
1215 * setting the size. (--mdcki) */
1216#else
1217# ifdef FEAT_MENU
1218 if (gui.menu_is_active)
1219 base_height += gui.menu_height;
1220# endif
1221# ifdef FEAT_TOOLBAR
1222 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1223# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1224 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1225# else
1226 base_height += gui.toolbar_height;
1227# endif
1228# endif
1229# ifdef FEAT_FOOTER
1230 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1231 base_height += gui.footer_height;
1232# endif
1233# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1234 base_height += gui_mch_text_area_extra_height();
1235# endif
1236#endif
1237 return base_height;
1238}
1239
1240/*
1241 * Should be called after the GUI shell has been resized. Its arguments are
1242 * the new width and height of the shell in pixels.
1243 */
1244 void
1245gui_resize_shell(pixel_width, pixel_height)
1246 int pixel_width;
1247 int pixel_height;
1248{
1249 static int busy = FALSE;
1250
1251 if (!gui.shell_created) /* ignore when still initializing */
1252 return;
1253
1254 /*
1255 * Can't resize the screen while it is being redrawn. Remember the new
1256 * size and handle it later.
1257 */
1258 if (updating_screen || busy)
1259 {
1260 new_pixel_width = pixel_width;
1261 new_pixel_height = pixel_height;
1262 return;
1263 }
1264
1265again:
1266 busy = TRUE;
1267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 /* Flush pending output before redrawing */
1269 out_flush();
1270
1271 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
1272 gui.num_rows = (pixel_height - gui_get_base_height()
1273#if !defined(FEAT_GUI_PHOTON) && !defined(FEAT_GUI_MSWIN)
1274 + (gui.char_height / 2)
1275#endif
1276 ) / gui.char_height;
1277
1278 gui_position_components(pixel_width);
1279
1280 gui_reset_scroll_region();
1281 /*
1282 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1283 * at the last line here (why does it have to be one row too low?).
1284 */
1285 if (State == ASKMORE || State == CONFIRM)
1286 gui.row = gui.num_rows;
1287
1288 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1289 * the safe side. */
1290 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1291 || gui.num_rows != Rows || gui.num_cols != Columns)
1292 shell_resized();
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 gui_update_scrollbars(TRUE);
1295 gui_update_cursor(FALSE, TRUE);
1296#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1297 xim_set_status_area();
1298#endif
1299
1300 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001301
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 /*
1303 * We could have been called again while redrawing the screen.
1304 * Need to do it all again with the latest size then.
1305 */
1306 if (new_pixel_height)
1307 {
1308 pixel_width = new_pixel_width;
1309 pixel_height = new_pixel_height;
1310 new_pixel_width = 0;
1311 new_pixel_height = 0;
1312 goto again;
1313 }
1314}
1315
1316/*
1317 * Check if gui_resize_shell() must be called.
1318 */
1319 void
1320gui_may_resize_shell()
1321{
1322 int h, w;
1323
1324 if (new_pixel_height)
1325 {
1326 /* careful: gui_resize_shell() may postpone the resize again if we
1327 * were called indirectly by it */
1328 w = new_pixel_width;
1329 h = new_pixel_height;
1330 new_pixel_width = 0;
1331 new_pixel_height = 0;
1332 gui_resize_shell(w, h);
1333 }
1334}
1335
1336 int
1337gui_get_shellsize()
1338{
1339 Rows = gui.num_rows;
1340 Columns = gui.num_cols;
1341 return OK;
1342}
1343
1344/*
1345 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001346 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1347 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349/*ARGSUSED*/
1350 void
1351gui_set_shellsize(mustset, fit_to_display)
1352 int mustset; /* set by the user */
1353 int fit_to_display;
1354{
1355 int base_width;
1356 int base_height;
1357 int width;
1358 int height;
1359 int min_width;
1360 int min_height;
1361 int screen_w;
1362 int screen_h;
1363
1364 if (!gui.shell_created)
1365 return;
1366
1367#ifdef MSWIN
1368 /* If not setting to a user specified size and maximized, calculate the
1369 * number of characters that fit in the maximized window. */
1370 if (!mustset && gui_mch_maximized())
1371 {
1372 gui_mch_newfont();
1373 return;
1374 }
1375#endif
1376
1377 base_width = gui_get_base_width();
1378 base_height = gui_get_base_height();
1379#ifdef USE_SUN_WORKSHOP
1380 if (!mustset && usingSunWorkShop
1381 && workshop_get_width_height(&width, &height))
1382 {
1383 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1384 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1385 }
1386 else
1387#endif
1388 {
1389 width = Columns * gui.char_width + base_width;
1390 height = Rows * gui.char_height + base_height;
1391 }
1392
1393 if (fit_to_display)
1394 {
1395 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1396 if (width > screen_w)
1397 {
1398 Columns = (screen_w - base_width) / gui.char_width;
1399 if (Columns < MIN_COLUMNS)
1400 Columns = MIN_COLUMNS;
1401 width = Columns * gui.char_width + base_width;
1402 }
1403 if (height > screen_h)
1404 {
1405 Rows = (screen_h - base_height) / gui.char_height;
1406 check_shellsize();
1407 height = Rows * gui.char_height + base_height;
1408 }
1409 }
1410 gui.num_cols = Columns;
1411 gui.num_rows = Rows;
1412
1413 min_width = base_width + MIN_COLUMNS * gui.char_width;
1414 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001415# ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001416 min_height += tabline_height() * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 gui_mch_set_shellsize(width, height, min_width, min_height,
1420 base_width, base_height);
1421 if (fit_to_display)
1422 {
1423 int x, y;
1424
1425 /* Some window managers put the Vim window left of/above the screen. */
1426 gui_mch_update();
1427 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1428 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1429 }
1430
1431 gui_position_components(width);
1432 gui_update_scrollbars(TRUE);
1433 gui_reset_scroll_region();
1434}
1435
1436/*
1437 * Called when Rows and/or Columns has changed.
1438 */
1439 void
1440gui_new_shellsize()
1441{
1442 gui_reset_scroll_region();
1443}
1444
1445/*
1446 * Make scroll region cover whole screen.
1447 */
1448 void
1449gui_reset_scroll_region()
1450{
1451 gui.scroll_region_top = 0;
1452 gui.scroll_region_bot = gui.num_rows - 1;
1453 gui.scroll_region_left = 0;
1454 gui.scroll_region_right = gui.num_cols - 1;
1455}
1456
1457 void
1458gui_start_highlight(mask)
1459 int mask;
1460{
1461 if (mask > HL_ALL) /* highlight code */
1462 gui.highlight_mask = mask;
1463 else /* mask */
1464 gui.highlight_mask |= mask;
1465}
1466
1467 void
1468gui_stop_highlight(mask)
1469 int mask;
1470{
1471 if (mask > HL_ALL) /* highlight code */
1472 gui.highlight_mask = HL_NORMAL;
1473 else /* mask */
1474 gui.highlight_mask &= ~mask;
1475}
1476
1477/*
1478 * Clear a rectangular region of the screen from text pos (row1, col1) to
1479 * (row2, col2) inclusive.
1480 */
1481 void
1482gui_clear_block(row1, col1, row2, col2)
1483 int row1;
1484 int col1;
1485 int row2;
1486 int col2;
1487{
1488 /* Clear the selection if we are about to write over it */
1489 clip_may_clear_selection(row1, row2);
1490
1491 gui_mch_clear_block(row1, col1, row2, col2);
1492
1493 /* Invalidate cursor if it was in this block */
1494 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1495 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1496 gui.cursor_is_valid = FALSE;
1497}
1498
1499/*
1500 * Write code to update the cursor later. This avoids the need to flush the
1501 * output buffer before calling gui_update_cursor().
1502 */
1503 void
1504gui_update_cursor_later()
1505{
1506 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1507}
1508
1509 void
1510gui_write(s, len)
1511 char_u *s;
1512 int len;
1513{
1514 char_u *p;
1515 int arg1 = 0, arg2 = 0;
1516 /* this doesn't make sense, disabled until someone can explain why it
1517 * would be needed */
1518#if 0 && (defined(RISCOS) || defined(WIN16))
1519 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1520 our redraw speed will suffer */
1521#else
1522 int force_cursor = FALSE; /* force cursor update */
1523#endif
1524 int force_scrollbar = FALSE;
1525 static win_T *old_curwin = NULL;
1526
1527/* #define DEBUG_GUI_WRITE */
1528#ifdef DEBUG_GUI_WRITE
1529 {
1530 int i;
1531 char_u *str;
1532
1533 printf("gui_write(%d):\n ", len);
1534 for (i = 0; i < len; i++)
1535 if (s[i] == ESC)
1536 {
1537 if (i != 0)
1538 printf("\n ");
1539 printf("<ESC>");
1540 }
1541 else
1542 {
1543 str = transchar_byte(s[i]);
1544 if (str[0] && str[1])
1545 printf("<%s>", (char *)str);
1546 else
1547 printf("%s", (char *)str);
1548 }
1549 printf("\n");
1550 }
1551#endif
1552 while (len)
1553 {
1554 if (s[0] == ESC && s[1] == '|')
1555 {
1556 p = s + 2;
1557 if (VIM_ISDIGIT(*p))
1558 {
1559 arg1 = getdigits(&p);
1560 if (p > s + len)
1561 break;
1562 if (*p == ';')
1563 {
1564 ++p;
1565 arg2 = getdigits(&p);
1566 if (p > s + len)
1567 break;
1568 }
1569 }
1570 switch (*p)
1571 {
1572 case 'C': /* Clear screen */
1573 clip_scroll_selection(9999);
1574 gui_mch_clear_all();
1575 gui.cursor_is_valid = FALSE;
1576 force_scrollbar = TRUE;
1577 break;
1578 case 'M': /* Move cursor */
1579 gui_set_cursor(arg1, arg2);
1580 break;
1581 case 's': /* force cursor (shape) update */
1582 force_cursor = TRUE;
1583 break;
1584 case 'R': /* Set scroll region */
1585 if (arg1 < arg2)
1586 {
1587 gui.scroll_region_top = arg1;
1588 gui.scroll_region_bot = arg2;
1589 }
1590 else
1591 {
1592 gui.scroll_region_top = arg2;
1593 gui.scroll_region_bot = arg1;
1594 }
1595 break;
1596#ifdef FEAT_VERTSPLIT
1597 case 'V': /* Set vertical scroll region */
1598 if (arg1 < arg2)
1599 {
1600 gui.scroll_region_left = arg1;
1601 gui.scroll_region_right = arg2;
1602 }
1603 else
1604 {
1605 gui.scroll_region_left = arg2;
1606 gui.scroll_region_right = arg1;
1607 }
1608 break;
1609#endif
1610 case 'd': /* Delete line */
1611 gui_delete_lines(gui.row, 1);
1612 break;
1613 case 'D': /* Delete lines */
1614 gui_delete_lines(gui.row, arg1);
1615 break;
1616 case 'i': /* Insert line */
1617 gui_insert_lines(gui.row, 1);
1618 break;
1619 case 'I': /* Insert lines */
1620 gui_insert_lines(gui.row, arg1);
1621 break;
1622 case '$': /* Clear to end-of-line */
1623 gui_clear_block(gui.row, gui.col, gui.row,
1624 (int)Columns - 1);
1625 break;
1626 case 'h': /* Turn on highlighting */
1627 gui_start_highlight(arg1);
1628 break;
1629 case 'H': /* Turn off highlighting */
1630 gui_stop_highlight(arg1);
1631 break;
1632 case 'f': /* flash the window (visual bell) */
1633 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1634 break;
1635 default:
1636 p = s + 1; /* Skip the ESC */
1637 break;
1638 }
1639 len -= (int)(++p - s);
1640 s = p;
1641 }
1642 else if (
1643#ifdef EBCDIC
1644 CtrlChar(s[0]) != 0 /* Ctrl character */
1645#else
1646 s[0] < 0x20 /* Ctrl character */
1647#endif
1648#ifdef FEAT_SIGN_ICONS
1649 && s[0] != SIGN_BYTE
1650# ifdef FEAT_NETBEANS_INTG
1651 && s[0] != MULTISIGN_BYTE
1652# endif
1653#endif
1654 )
1655 {
1656 if (s[0] == '\n') /* NL */
1657 {
1658 gui.col = 0;
1659 if (gui.row < gui.scroll_region_bot)
1660 gui.row++;
1661 else
1662 gui_delete_lines(gui.scroll_region_top, 1);
1663 }
1664 else if (s[0] == '\r') /* CR */
1665 {
1666 gui.col = 0;
1667 }
1668 else if (s[0] == '\b') /* Backspace */
1669 {
1670 if (gui.col)
1671 --gui.col;
1672 }
1673 else if (s[0] == Ctrl_L) /* cursor-right */
1674 {
1675 ++gui.col;
1676 }
1677 else if (s[0] == Ctrl_G) /* Beep */
1678 {
1679 gui_mch_beep();
1680 }
1681 /* Other Ctrl character: shouldn't happen! */
1682
1683 --len; /* Skip this char */
1684 ++s;
1685 }
1686 else
1687 {
1688 p = s;
1689 while (len > 0 && (
1690#ifdef EBCDIC
1691 CtrlChar(*p) == 0
1692#else
1693 *p >= 0x20
1694#endif
1695#ifdef FEAT_SIGN_ICONS
1696 || *p == SIGN_BYTE
1697# ifdef FEAT_NETBEANS_INTG
1698 || *p == MULTISIGN_BYTE
1699# endif
1700#endif
1701 ))
1702 {
1703 len--;
1704 p++;
1705 }
1706 gui_outstr(s, (int)(p - s));
1707 s = p;
1708 }
1709 }
1710
1711 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1712 * set). */
1713 if (force_cursor)
1714 gui_update_cursor(TRUE, TRUE);
1715
1716 /* When switching to another window the dragging must have stopped.
1717 * Required for GTK, dragged_sb isn't reset. */
1718 if (old_curwin != curwin)
1719 gui.dragged_sb = SBAR_NONE;
1720
1721 /* Update the scrollbars after clearing the screen or when switched
1722 * to another window.
1723 * Update the horizontal scrollbar always, it's difficult to check all
1724 * situations where it might change. */
1725 if (force_scrollbar || old_curwin != curwin)
1726 gui_update_scrollbars(force_scrollbar);
1727 else
1728 gui_update_horiz_scrollbar(FALSE);
1729 old_curwin = curwin;
1730
1731 /*
1732 * We need to make sure this is cleared since Athena doesn't tell us when
1733 * he is done dragging. Do the same for GTK.
1734 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001735#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 gui.dragged_sb = SBAR_NONE;
1737#endif
1738
1739 gui_mch_flush(); /* In case vim decides to take a nap */
1740}
1741
1742/*
1743 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1744 * produces wrong results. Call gui_dont_update_cursor() before that code and
1745 * gui_can_update_cursor() afterwards.
1746 */
1747 void
1748gui_dont_update_cursor()
1749{
1750 if (gui.in_use)
1751 {
1752 /* Undraw the cursor now, we probably can't do it after the change. */
1753 gui_undraw_cursor();
1754 can_update_cursor = FALSE;
1755 }
1756}
1757
1758 void
1759gui_can_update_cursor()
1760{
1761 can_update_cursor = TRUE;
1762 /* No need to update the cursor right now, there is always more output
1763 * after scrolling. */
1764}
1765
1766 static void
1767gui_outstr(s, len)
1768 char_u *s;
1769 int len;
1770{
1771 int this_len;
1772#ifdef FEAT_MBYTE
1773 int cells;
1774#endif
1775
1776 if (len == 0)
1777 return;
1778
1779 if (len < 0)
1780 len = (int)STRLEN(s);
1781
1782 while (len > 0)
1783 {
1784#ifdef FEAT_MBYTE
1785 if (has_mbyte)
1786 {
1787 /* Find out how many chars fit in the current line. */
1788 cells = 0;
1789 for (this_len = 0; this_len < len; )
1790 {
1791 cells += (*mb_ptr2cells)(s + this_len);
1792 if (gui.col + cells > Columns)
1793 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001794 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
1796 if (this_len > len)
1797 this_len = len; /* don't include following composing char */
1798 }
1799 else
1800#endif
1801 if (gui.col + len > Columns)
1802 this_len = Columns - gui.col;
1803 else
1804 this_len = len;
1805
1806 (void)gui_outstr_nowrap(s, this_len,
1807 0, (guicolor_T)0, (guicolor_T)0, 0);
1808 s += this_len;
1809 len -= this_len;
1810#ifdef FEAT_MBYTE
1811 /* fill up for a double-width char that doesn't fit. */
1812 if (len > 0 && gui.col < Columns)
1813 (void)gui_outstr_nowrap((char_u *)" ", 1,
1814 0, (guicolor_T)0, (guicolor_T)0, 0);
1815#endif
1816 /* The cursor may wrap to the next line. */
1817 if (gui.col >= Columns)
1818 {
1819 gui.col = 0;
1820 gui.row++;
1821 }
1822 }
1823}
1824
1825/*
1826 * Output one character (may be one or two display cells).
1827 * Caller must check for valid "off".
1828 * Returns FAIL or OK, just like gui_outstr_nowrap().
1829 */
1830 static int
1831gui_screenchar(off, flags, fg, bg, back)
1832 int off; /* Offset from start of screen */
1833 int flags;
1834 guicolor_T fg, bg; /* colors for cursor */
1835 int back; /* backup this many chars when using bold trick */
1836{
1837#ifdef FEAT_MBYTE
1838 char_u buf[MB_MAXBYTES + 1];
1839
1840 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1841 if (enc_utf8 && ScreenLines[off] == 0)
1842 return OK;
1843
1844 if (enc_utf8 && ScreenLinesUC[off] != 0)
1845 /* Draw UTF-8 multi-byte character. */
1846 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1847 flags, fg, bg, back);
1848
1849 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1850 {
1851 buf[0] = ScreenLines[off];
1852 buf[1] = ScreenLines2[off];
1853 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1854 }
1855
1856 /* Draw non-multi-byte character or DBCS character. */
1857 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001858 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 flags, fg, bg, back);
1860#else
1861 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1862#endif
1863}
1864
1865#ifdef HAVE_GTK2
1866/*
1867 * Output the string at the given screen position. This is used in place
1868 * of gui_screenchar() where possible because Pango needs as much context
1869 * as possible to work nicely. It's a lot faster as well.
1870 */
1871 static int
1872gui_screenstr(off, len, flags, fg, bg, back)
1873 int off; /* Offset from start of screen */
1874 int len; /* string length in screen cells */
1875 int flags;
1876 guicolor_T fg, bg; /* colors for cursor */
1877 int back; /* backup this many chars when using bold trick */
1878{
1879 char_u *buf;
1880 int outlen = 0;
1881 int i;
1882 int retval;
1883
1884 if (len <= 0) /* "cannot happen"? */
1885 return OK;
1886
1887 if (enc_utf8)
1888 {
1889 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1890 if (buf == NULL)
1891 return OK; /* not much we could do here... */
1892
1893 for (i = off; i < off + len; ++i)
1894 {
1895 if (ScreenLines[i] == 0)
1896 continue; /* skip second half of double-width char */
1897
1898 if (ScreenLinesUC[i] == 0)
1899 buf[outlen++] = ScreenLines[i];
1900 else
1901 outlen += utfc_char2bytes(i, buf + outlen);
1902 }
1903
1904 buf[outlen] = NUL; /* only to aid debugging */
1905 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1906 vim_free(buf);
1907
1908 return retval;
1909 }
1910 else if (enc_dbcs == DBCS_JPNU)
1911 {
1912 buf = alloc((unsigned)(len * 2 + 1));
1913 if (buf == NULL)
1914 return OK; /* not much we could do here... */
1915
1916 for (i = off; i < off + len; ++i)
1917 {
1918 buf[outlen++] = ScreenLines[i];
1919
1920 /* handle double-byte single-width char */
1921 if (ScreenLines[i] == 0x8e)
1922 buf[outlen++] = ScreenLines2[i];
1923 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1924 buf[outlen++] = ScreenLines[++i];
1925 }
1926
1927 buf[outlen] = NUL; /* only to aid debugging */
1928 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1929 vim_free(buf);
1930
1931 return retval;
1932 }
1933 else
1934 {
1935 return gui_outstr_nowrap(&ScreenLines[off], len,
1936 flags, fg, bg, back);
1937 }
1938}
1939#endif /* HAVE_GTK2 */
1940
1941/*
1942 * Output the given string at the current cursor position. If the string is
1943 * too long to fit on the line, then it is truncated.
1944 * "flags":
1945 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1946 * actually draw (an inverted) cursor.
1947 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparant
1948 * background.
1949 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1950 * it.
1951 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1952 * FAIL (the caller should start drawing "back" chars back).
1953 */
1954 int
1955gui_outstr_nowrap(s, len, flags, fg, bg, back)
1956 char_u *s;
1957 int len;
1958 int flags;
1959 guicolor_T fg, bg; /* colors for cursor */
1960 int back; /* backup this many chars when using bold trick */
1961{
1962 long_u highlight_mask;
1963 long_u hl_mask_todo;
1964 guicolor_T fg_color;
1965 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001966 guicolor_T sp_color;
Bram Moolenaar9372a112005-12-06 19:59:18 +00001967#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 GuiFont font = NOFONT;
1969# ifdef FEAT_XFONTSET
1970 GuiFontset fontset = NOFONTSET;
1971# endif
1972#endif
1973 attrentry_T *aep = NULL;
1974 int draw_flags;
1975 int col = gui.col;
1976#ifdef FEAT_SIGN_ICONS
1977 int draw_sign = FALSE;
1978# ifdef FEAT_NETBEANS_INTG
1979 int multi_sign = FALSE;
1980# endif
1981#endif
1982
1983 if (len < 0)
1984 len = (int)STRLEN(s);
1985 if (len == 0)
1986 return OK;
1987
1988#ifdef FEAT_SIGN_ICONS
1989 if (*s == SIGN_BYTE
1990# ifdef FEAT_NETBEANS_INTG
1991 || *s == MULTISIGN_BYTE
1992# endif
1993 )
1994 {
1995# ifdef FEAT_NETBEANS_INTG
1996 if (*s == MULTISIGN_BYTE)
1997 multi_sign = TRUE;
1998# endif
1999 /* draw spaces instead */
2000 s = (char_u *)" ";
2001 if (len == 1 && col > 0)
2002 --col;
2003 len = 2;
2004 draw_sign = TRUE;
2005 highlight_mask = 0;
2006 }
2007 else
2008#endif
2009 if (gui.highlight_mask > HL_ALL)
2010 {
2011 aep = syn_gui_attr2entry(gui.highlight_mask);
2012 if (aep == NULL) /* highlighting not set */
2013 highlight_mask = 0;
2014 else
2015 highlight_mask = aep->ae_attr;
2016 }
2017 else
2018 highlight_mask = gui.highlight_mask;
2019 hl_mask_todo = highlight_mask;
2020
Bram Moolenaar9372a112005-12-06 19:59:18 +00002021#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /* Set the font */
2023 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2024 font = aep->ae_u.gui.font;
2025# ifdef FEAT_XFONTSET
2026 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2027 fontset = aep->ae_u.gui.fontset;
2028# endif
2029 else
2030 {
2031# ifdef FEAT_XFONTSET
2032 if (gui.fontset != NOFONTSET)
2033 fontset = gui.fontset;
2034 else
2035# endif
2036 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2037 {
2038 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2039 {
2040 font = gui.boldital_font;
2041 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2042 }
2043 else if (gui.bold_font != NOFONT)
2044 {
2045 font = gui.bold_font;
2046 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2047 }
2048 else
2049 font = gui.norm_font;
2050 }
2051 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2052 {
2053 font = gui.ital_font;
2054 hl_mask_todo &= ~HL_ITALIC;
2055 }
2056 else
2057 font = gui.norm_font;
2058 }
2059# ifdef FEAT_XFONTSET
2060 if (fontset != NOFONTSET)
2061 gui_mch_set_fontset(fontset);
2062 else
2063# endif
2064 gui_mch_set_font(font);
2065#endif
2066
2067 draw_flags = 0;
2068
2069 /* Set the color */
2070 bg_color = gui.back_pixel;
2071 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2072 {
2073 draw_flags |= DRAW_CURSOR;
2074 fg_color = fg;
2075 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002076 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078 else if (aep != NULL)
2079 {
2080 fg_color = aep->ae_u.gui.fg_color;
2081 if (fg_color == INVALCOLOR)
2082 fg_color = gui.norm_pixel;
2083 bg_color = aep->ae_u.gui.bg_color;
2084 if (bg_color == INVALCOLOR)
2085 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002086 sp_color = aep->ae_u.gui.sp_color;
2087 if (sp_color == INVALCOLOR)
2088 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002093 sp_color = fg_color;
2094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2097 {
2098#if defined(AMIGA) || defined(RISCOS)
2099 gui_mch_set_colors(bg_color, fg_color);
2100#else
2101 gui_mch_set_fg_color(bg_color);
2102 gui_mch_set_bg_color(fg_color);
2103#endif
2104 }
2105 else
2106 {
2107#if defined(AMIGA) || defined(RISCOS)
2108 gui_mch_set_colors(fg_color, bg_color);
2109#else
2110 gui_mch_set_fg_color(fg_color);
2111 gui_mch_set_bg_color(bg_color);
2112#endif
2113 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002114 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116 /* Clear the selection if we are about to write over it */
2117 if (!(flags & GUI_MON_NOCLEAR))
2118 clip_may_clear_selection(gui.row, gui.row);
2119
2120
2121#ifndef MSWIN16_FASTTEXT
2122 /* If there's no bold font, then fake it */
2123 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2124 draw_flags |= DRAW_BOLD;
2125#endif
2126
2127 /*
2128 * When drawing bold or italic characters the spill-over from the left
2129 * neighbor may be destroyed. Let the caller backup to start redrawing
2130 * just after a blank.
2131 */
2132 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2133 return FAIL;
2134
Bram Moolenaar9372a112005-12-06 19:59:18 +00002135#if defined(RISCOS) || defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 /* If there's no italic font, then fake it.
2137 * For GTK2, we don't need a different font for italic style. */
2138 if (hl_mask_todo & HL_ITALIC)
2139 draw_flags |= DRAW_ITALIC;
2140
2141 /* Do we underline the text? */
2142 if (hl_mask_todo & HL_UNDERLINE)
2143 draw_flags |= DRAW_UNDERL;
2144#else
2145 /* Do we underline the text? */
2146 if ((hl_mask_todo & HL_UNDERLINE)
2147# ifndef MSWIN16_FASTTEXT
2148 || (hl_mask_todo & HL_ITALIC)
2149# endif
2150 )
2151 draw_flags |= DRAW_UNDERL;
2152#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002153 /* Do we undercurl the text? */
2154 if (hl_mask_todo & HL_UNDERCURL)
2155 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 /* Do we draw transparantly? */
2158 if (flags & GUI_MON_TRS_CURSOR)
2159 draw_flags |= DRAW_TRANSP;
2160
2161 /*
2162 * Draw the text.
2163 */
2164#ifdef HAVE_GTK2
2165 /* The value returned is the length in display cells */
2166 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2167#else
2168# ifdef FEAT_MBYTE
2169 if (enc_utf8)
2170 {
2171 int start; /* index of bytes to be drawn */
2172 int cells; /* cellwidth of bytes to be drawn */
2173 int thislen; /* length of bytes to be drawin */
2174 int cn; /* cellwidth of current char */
2175 int i; /* index of current char */
2176 int c; /* current char value */
2177 int cl; /* byte length of current char */
2178 int comping; /* current char is composing */
2179 int scol = col; /* screen column */
2180 int dowide; /* use 'guifontwide' */
2181
2182 /* Break the string at a composing character, it has to be drawn on
2183 * top of the previous character. */
2184 start = 0;
2185 cells = 0;
2186 for (i = 0; i < len; i += cl)
2187 {
2188 c = utf_ptr2char(s + i);
2189 cn = utf_char2cells(c);
2190 if (cn > 1
2191# ifdef FEAT_XFONTSET
2192 && fontset == NOFONTSET
2193# endif
2194 && gui.wide_font != NOFONT)
2195 dowide = TRUE;
2196 else
2197 dowide = FALSE;
2198 comping = utf_iscomposing(c);
2199 if (!comping) /* count cells from non-composing chars */
2200 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002201 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (cl == 0) /* hit end of string */
2203 len = i + cl; /* len must be wrong "cannot happen" */
2204
2205 /* print the string so far if it's the last character or there is
2206 * a composing character. */
2207 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar9372a112005-12-06 19:59:18 +00002208# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 || (cn > 1
2210# ifdef FEAT_XFONTSET
2211 /* No fontset: At least draw char after wide char at
2212 * right position. */
2213 && fontset == NOFONTSET
2214# endif
2215 )
2216# endif
2217 )
2218 {
2219 if (comping || dowide)
2220 thislen = i - start;
2221 else
2222 thislen = i - start + cl;
2223 if (thislen > 0)
2224 {
2225 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2226 draw_flags);
2227 start += thislen;
2228 }
2229 scol += cells;
2230 cells = 0;
2231 if (dowide)
2232 {
2233 gui_mch_set_font(gui.wide_font);
2234 gui_mch_draw_string(gui.row, scol - cn,
2235 s + start, cl, draw_flags);
2236 gui_mch_set_font(font);
2237 start += cl;
2238 }
2239
Bram Moolenaar9372a112005-12-06 19:59:18 +00002240# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002241 /* No fontset: draw a space to fill the gap after a wide char
2242 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2244# ifdef FEAT_XFONTSET
2245 && fontset == NOFONTSET
2246# endif
2247 && !dowide)
2248 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2249 1, draw_flags);
2250# endif
2251 }
2252 /* Draw a composing char on top of the previous char. */
2253 if (comping)
2254 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002255# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002256 /* Carbon ATSUI autodraws composing char over previous char */
2257 gui_mch_draw_string(gui.row, scol, s + i, cl,
2258 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002259# else
2260 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2261 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 start = i + cl;
2264 }
2265 }
2266 /* The stuff below assumes "len" is the length in screen columns. */
2267 len = scol - col;
2268 }
2269 else
2270# endif
2271 {
2272 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2273# ifdef FEAT_MBYTE
2274 if (enc_dbcs == DBCS_JPNU)
2275 {
2276 int clen = 0;
2277 int i;
2278
2279 /* Get the length in display cells, this can be different from the
2280 * number of bytes for "euc-jp". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002281 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 clen += (*mb_ptr2cells)(s + i);
2283 len = clen;
2284 }
2285# endif
2286 }
2287#endif /* !HAVE_GTK2 */
2288
2289 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2290 gui.col = col + len;
2291
2292 /* May need to invert it when it's part of the selection. */
2293 if (flags & GUI_MON_NOCLEAR)
2294 clip_may_redraw_selection(gui.row, col, len);
2295
2296 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2297 {
2298 /* Invalidate the old physical cursor position if we wrote over it */
2299 if (gui.cursor_row == gui.row
2300 && gui.cursor_col >= col
2301 && gui.cursor_col < col + len)
2302 gui.cursor_is_valid = FALSE;
2303 }
2304
2305#ifdef FEAT_SIGN_ICONS
2306 if (draw_sign)
2307 /* Draw the sign on top of the spaces. */
2308 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2309# ifdef FEAT_NETBEANS_INTG
2310 if (multi_sign)
2311 netbeans_draw_multisign_indicator(gui.row);
2312# endif
2313#endif
2314
2315 return OK;
2316}
2317
2318/*
2319 * Un-draw the cursor. Actually this just redraws the character at the given
2320 * position. The character just before it too, for when it was in bold.
2321 */
2322 void
2323gui_undraw_cursor()
2324{
2325 if (gui.cursor_is_valid)
2326 {
2327#ifdef FEAT_HANGULIN
2328 if (composing_hangul
2329 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2330 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2331 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2332 gui.norm_pixel, gui.back_pixel, 0);
2333 else
2334 {
2335#endif
2336 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2337 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2338 && gui.cursor_col > 0)
2339 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2340 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2341#ifdef FEAT_HANGULIN
2342 if (composing_hangul)
2343 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2344 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2345 }
2346#endif
2347 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2348 * here in case it wasn't needed to undraw it. */
2349 gui.cursor_is_valid = FALSE;
2350 }
2351}
2352
2353 void
2354gui_redraw(x, y, w, h)
2355 int x;
2356 int y;
2357 int w;
2358 int h;
2359{
2360 int row1, col1, row2, col2;
2361
2362 row1 = Y_2_ROW(y);
2363 col1 = X_2_COL(x);
2364 row2 = Y_2_ROW(y + h - 1);
2365 col2 = X_2_COL(x + w - 1);
2366
2367 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2368
2369 /*
2370 * We may need to redraw the cursor, but don't take it upon us to change
2371 * its location after a scroll.
2372 * (maybe be more strict even and test col too?)
2373 * These things may be outside the update/clipping region and reality may
2374 * not reflect Vims internal ideas if these operations are clipped away.
2375 */
2376 if (gui.row == gui.cursor_row)
2377 gui_update_cursor(TRUE, TRUE);
2378}
2379
2380/*
2381 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2382 * from col1 to col2 (inclusive).
2383 * Return TRUE when the character before the first drawn character has
2384 * different attributes (may have to be redrawn too).
2385 */
2386 int
2387gui_redraw_block(row1, col1, row2, col2, flags)
2388 int row1;
2389 int col1;
2390 int row2;
2391 int col2;
2392 int flags; /* flags for gui_outstr_nowrap() */
2393{
2394 int old_row, old_col;
2395 long_u old_hl_mask;
2396 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002397 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int idx, len;
2399 int back, nback;
2400 int retval = FALSE;
2401#ifdef FEAT_MBYTE
2402 int orig_col1, orig_col2;
2403#endif
2404
2405 /* Don't try to update when ScreenLines is not valid */
2406 if (!screen_cleared || ScreenLines == NULL)
2407 return retval;
2408
2409 /* Don't try to draw outside the shell! */
2410 /* Check everything, strange values may be caused by a big border width */
2411 col1 = check_col(col1);
2412 col2 = check_col(col2);
2413 row1 = check_row(row1);
2414 row2 = check_row(row2);
2415
2416 /* Remember where our cursor was */
2417 old_row = gui.row;
2418 old_col = gui.col;
2419 old_hl_mask = gui.highlight_mask;
2420#ifdef FEAT_MBYTE
2421 orig_col1 = col1;
2422 orig_col2 = col2;
2423#endif
2424
2425 for (gui.row = row1; gui.row <= row2; gui.row++)
2426 {
2427#ifdef FEAT_MBYTE
2428 /* When only half of a double-wide character is in the block, include
2429 * the other half. */
2430 col1 = orig_col1;
2431 col2 = orig_col2;
2432 off = LineOffset[gui.row];
2433 if (enc_dbcs != 0)
2434 {
2435 if (col1 > 0)
2436 col1 -= dbcs_screen_head_off(ScreenLines + off,
2437 ScreenLines + off + col1);
2438 col2 += dbcs_screen_tail_off(ScreenLines + off,
2439 ScreenLines + off + col2);
2440 }
2441 else if (enc_utf8)
2442 {
2443 if (ScreenLines[off + col1] == 0)
2444 --col1;
2445# ifdef HAVE_GTK2
2446 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2447 ++col2;
2448# endif
2449 }
2450#endif
2451 gui.col = col1;
2452 off = LineOffset[gui.row] + gui.col;
2453 len = col2 - col1 + 1;
2454
2455 /* Find how many chars back this highlighting starts, or where a space
2456 * is. Needed for when the bold trick is used */
2457 for (back = 0; back < col1; ++back)
2458 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2459 || ScreenLines[off - 1 - back] == ' ')
2460 break;
2461 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2462 && ScreenLines[off - 1] != ' ');
2463
2464 /* Break it up in strings of characters with the same attributes. */
2465 /* Print UTF-8 characters individually. */
2466 while (len > 0)
2467 {
2468 first_attr = ScreenAttrs[off];
2469 gui.highlight_mask = first_attr;
2470#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2471 if (enc_utf8 && ScreenLinesUC[off] != 0)
2472 {
2473 /* output multi-byte character separately */
2474 nback = gui_screenchar(off, flags,
2475 (guicolor_T)0, (guicolor_T)0, back);
2476 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2477 idx = 2;
2478 else
2479 idx = 1;
2480 }
2481 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2482 {
2483 /* output double-byte, single-width character separately */
2484 nback = gui_screenchar(off, flags,
2485 (guicolor_T)0, (guicolor_T)0, back);
2486 idx = 1;
2487 }
2488 else
2489#endif
2490 {
2491#ifdef HAVE_GTK2
2492 for (idx = 0; idx < len; ++idx)
2493 {
2494 if (enc_utf8 && ScreenLines[off + idx] == 0)
2495 continue; /* skip second half of double-width char */
2496 if (ScreenAttrs[off + idx] != first_attr)
2497 break;
2498 }
2499 /* gui_screenstr() takes care of multibyte chars */
2500 nback = gui_screenstr(off, idx, flags,
2501 (guicolor_T)0, (guicolor_T)0, back);
2502#else
2503 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2504 idx++)
2505 {
2506# ifdef FEAT_MBYTE
2507 /* Stop at a multi-byte Unicode character. */
2508 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2509 break;
2510 if (enc_dbcs == DBCS_JPNU)
2511 {
2512 /* Stop at a double-byte single-width char. */
2513 if (ScreenLines[off + idx] == 0x8e)
2514 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002515 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 + off + idx) == 2)
2517 ++idx; /* skip second byte of double-byte char */
2518 }
2519# endif
2520 }
2521 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2522 (guicolor_T)0, (guicolor_T)0, back);
2523#endif
2524 }
2525 if (nback == FAIL)
2526 {
2527 /* Must back up to start drawing where a bold or italic word
2528 * starts. */
2529 off -= back;
2530 len += back;
2531 gui.col -= back;
2532 }
2533 else
2534 {
2535 off += idx;
2536 len -= idx;
2537 }
2538 back = 0;
2539 }
2540 }
2541
2542 /* Put the cursor back where it was */
2543 gui.row = old_row;
2544 gui.col = old_col;
2545 gui.highlight_mask = old_hl_mask;
2546
2547 return retval;
2548}
2549
2550 static void
2551gui_delete_lines(row, count)
2552 int row;
2553 int count;
2554{
2555 if (count <= 0)
2556 return;
2557
2558 if (row + count > gui.scroll_region_bot)
2559 /* Scrolled out of region, just blank the lines out */
2560 gui_clear_block(row, gui.scroll_region_left,
2561 gui.scroll_region_bot, gui.scroll_region_right);
2562 else
2563 {
2564 gui_mch_delete_lines(row, count);
2565
2566 /* If the cursor was in the deleted lines it's now gone. If the
2567 * cursor was in the scrolled lines adjust its position. */
2568 if (gui.cursor_row >= row
2569 && gui.cursor_col >= gui.scroll_region_left
2570 && gui.cursor_col <= gui.scroll_region_right)
2571 {
2572 if (gui.cursor_row < row + count)
2573 gui.cursor_is_valid = FALSE;
2574 else if (gui.cursor_row <= gui.scroll_region_bot)
2575 gui.cursor_row -= count;
2576 }
2577 }
2578}
2579
2580 static void
2581gui_insert_lines(row, count)
2582 int row;
2583 int count;
2584{
2585 if (count <= 0)
2586 return;
2587
2588 if (row + count > gui.scroll_region_bot)
2589 /* Scrolled out of region, just blank the lines out */
2590 gui_clear_block(row, gui.scroll_region_left,
2591 gui.scroll_region_bot, gui.scroll_region_right);
2592 else
2593 {
2594 gui_mch_insert_lines(row, count);
2595
2596 if (gui.cursor_row >= gui.row
2597 && gui.cursor_col >= gui.scroll_region_left
2598 && gui.cursor_col <= gui.scroll_region_right)
2599 {
2600 if (gui.cursor_row <= gui.scroll_region_bot - count)
2601 gui.cursor_row += count;
2602 else if (gui.cursor_row <= gui.scroll_region_bot)
2603 gui.cursor_is_valid = FALSE;
2604 }
2605 }
2606}
2607
2608/*
2609 * The main GUI input routine. Waits for a character from the keyboard.
2610 * wtime == -1 Wait forever.
2611 * wtime == 0 Don't wait.
2612 * wtime > 0 Wait wtime milliseconds for a character.
2613 * Returns OK if a character was found to be available within the given time,
2614 * or FAIL otherwise.
2615 */
2616 int
2617gui_wait_for_chars(wtime)
2618 long wtime;
2619{
2620 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 /*
2623 * If we're going to wait a bit, update the menus and mouse shape for the
2624 * current State.
2625 */
2626 if (wtime != 0)
2627 {
2628#ifdef FEAT_MENU
2629 gui_update_menus(0);
2630#endif
2631 }
2632
2633 gui_mch_update();
2634 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 /* Before waiting, flush any output to the screen. */
2640 gui_mch_flush();
2641
2642 if (wtime > 0)
2643 {
2644 /* Blink when waiting for a character. Probably only does something
2645 * for showmatch() */
2646 gui_mch_start_blink();
2647 retval = gui_mch_wait_for_chars(wtime);
2648 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 return retval;
2650 }
2651
2652 /*
2653 * While we are waiting indefenitely for a character, blink the cursor.
2654 */
2655 gui_mch_start_blink();
2656
Bram Moolenaar3918c952005-03-15 22:34:55 +00002657 retval = FAIL;
2658 /*
2659 * We may want to trigger the CursorHold event. First wait for
2660 * 'updatetime' and if nothing is typed within that time put the
2661 * K_CURSORHOLD key in the input buffer.
2662 */
2663 if (gui_mch_wait_for_chars(p_ut) == OK)
2664 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002666 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002668 char_u buf[3];
2669
2670 /* Put K_CURSORHOLD in the input buffer. */
2671 buf[0] = CSI;
2672 buf[1] = KS_EXTRA;
2673 buf[2] = (int)KE_CURSORHOLD;
2674 add_to_input_buf(buf, 3);
2675
2676 retval = OK;
2677 }
2678#endif
2679
2680 if (retval == FAIL)
2681 {
2682 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002683 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 gui_mch_stop_blink();
2688 return retval;
2689}
2690
2691/*
2692 * Fill buffer with mouse coordinates encoded for check_termcode().
2693 */
2694 static void
2695fill_mouse_coord(p, col, row)
2696 char_u *p;
2697 int col;
2698 int row;
2699{
2700 p[0] = (char_u)(col / 128 + ' ' + 1);
2701 p[1] = (char_u)(col % 128 + ' ' + 1);
2702 p[2] = (char_u)(row / 128 + ' ' + 1);
2703 p[3] = (char_u)(row % 128 + ' ' + 1);
2704}
2705
2706/*
2707 * Generic mouse support function. Add a mouse event to the input buffer with
2708 * the given properties.
2709 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2710 * MOUSE_X1, MOUSE_X2
2711 * MOUSE_DRAG, or MOUSE_RELEASE.
2712 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2713 * x, y --- Coordinates of mouse in pixels.
2714 * repeated_click --- TRUE if this click comes only a short time after a
2715 * previous click.
2716 * modifiers --- Bit field which may be any of the following modifiers
2717 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2718 * This function will ignore drag events where the mouse has not moved to a new
2719 * character.
2720 */
2721 void
2722gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2723 int button;
2724 int x;
2725 int y;
2726 int repeated_click;
2727 int_u modifiers;
2728{
2729 static int prev_row = 0, prev_col = 0;
2730 static int prev_button = -1;
2731 static int num_clicks = 1;
2732 char_u string[10];
2733 enum key_extra button_char;
2734 int row, col;
2735#ifdef FEAT_CLIPBOARD
2736 int checkfor;
2737 int did_clip = FALSE;
2738#endif
2739
2740 /*
2741 * Scrolling may happen at any time, also while a selection is present.
2742 */
2743 switch (button)
2744 {
2745 case MOUSE_X1:
2746 button_char = KE_X1MOUSE;
2747 goto button_set;
2748 case MOUSE_X2:
2749 button_char = KE_X2MOUSE;
2750 goto button_set;
2751 case MOUSE_4:
2752 button_char = KE_MOUSEDOWN;
2753 goto button_set;
2754 case MOUSE_5:
2755 button_char = KE_MOUSEUP;
2756button_set:
2757 {
2758 /* Don't put events in the input queue now. */
2759 if (hold_gui_events)
2760 return;
2761
2762 string[3] = CSI;
2763 string[4] = KS_EXTRA;
2764 string[5] = (int)button_char;
2765
2766 /* Pass the pointer coordinates of the scroll event so that we
2767 * know which window to scroll. */
2768 row = gui_xy2colrow(x, y, &col);
2769 string[6] = (char_u)(col / 128 + ' ' + 1);
2770 string[7] = (char_u)(col % 128 + ' ' + 1);
2771 string[8] = (char_u)(row / 128 + ' ' + 1);
2772 string[9] = (char_u)(row % 128 + ' ' + 1);
2773
2774 if (modifiers == 0)
2775 add_to_input_buf(string + 3, 7);
2776 else
2777 {
2778 string[0] = CSI;
2779 string[1] = KS_MODIFIER;
2780 string[2] = 0;
2781 if (modifiers & MOUSE_SHIFT)
2782 string[2] |= MOD_MASK_SHIFT;
2783 if (modifiers & MOUSE_CTRL)
2784 string[2] |= MOD_MASK_CTRL;
2785 if (modifiers & MOUSE_ALT)
2786 string[2] |= MOD_MASK_ALT;
2787 add_to_input_buf(string, 10);
2788 }
2789 return;
2790 }
2791 }
2792
2793#ifdef FEAT_CLIPBOARD
2794 /* If a clipboard selection is in progress, handle it */
2795 if (clip_star.state == SELECT_IN_PROGRESS)
2796 {
2797 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2798 return;
2799 }
2800
2801 /* Determine which mouse settings to look for based on the current mode */
2802 switch (get_real_state())
2803 {
2804 case NORMAL_BUSY:
2805 case OP_PENDING:
2806 case NORMAL: checkfor = MOUSE_NORMAL; break;
2807 case VISUAL: checkfor = MOUSE_VISUAL; break;
2808 case REPLACE:
2809 case REPLACE+LANGMAP:
2810#ifdef FEAT_VREPLACE
2811 case VREPLACE:
2812 case VREPLACE+LANGMAP:
2813#endif
2814 case INSERT:
2815 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2816 case ASKMORE:
2817 case HITRETURN: /* At the more- and hit-enter prompt pass the
2818 mouse event for a click on or below the
2819 message line. */
2820 if (Y_2_ROW(y) >= msg_row)
2821 checkfor = MOUSE_NORMAL;
2822 else
2823 checkfor = MOUSE_RETURN;
2824 break;
2825
2826 /*
2827 * On the command line, use the clipboard selection on all lines
2828 * but the command line. But not when pasting.
2829 */
2830 case CMDLINE:
2831 case CMDLINE+LANGMAP:
2832 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2833 checkfor = MOUSE_NONE;
2834 else
2835 checkfor = MOUSE_COMMAND;
2836 break;
2837
2838 default:
2839 checkfor = MOUSE_NONE;
2840 break;
2841 };
2842
2843 /*
2844 * Allow clipboard selection of text on the command line in "normal"
2845 * modes. Don't do this when dragging the status line, or extending a
2846 * Visual selection.
2847 */
2848 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2849 && Y_2_ROW(y) >= topframe->fr_height
2850 && button != MOUSE_DRAG
2851# ifdef FEAT_MOUSESHAPE
2852 && !drag_status_line
2853# ifdef FEAT_VERTSPLIT
2854 && !drag_sep_line
2855# endif
2856# endif
2857 )
2858 checkfor = MOUSE_NONE;
2859
2860 /*
2861 * Use modeless selection when holding CTRL and SHIFT pressed.
2862 */
2863 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2864 checkfor = MOUSE_NONEF;
2865
2866 /*
2867 * In Ex mode, always use modeless selection.
2868 */
2869 if (exmode_active)
2870 checkfor = MOUSE_NONE;
2871
2872 /*
2873 * If the mouse settings say to not use the mouse, use the modeless
2874 * selection. But if Visual is active, assume that only the Visual area
2875 * will be selected.
2876 * Exception: On the command line, both the selection is used and a mouse
2877 * key is send.
2878 */
2879 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2880 {
2881#ifdef FEAT_VISUAL
2882 /* Don't do modeless selection in Visual mode. */
2883 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2884 return;
2885#endif
2886
2887 /*
2888 * When 'mousemodel' is "popup", shift-left is translated to right.
2889 * But not when also using Ctrl.
2890 */
2891 if (mouse_model_popup() && button == MOUSE_LEFT
2892 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2893 {
2894 button = MOUSE_RIGHT;
2895 modifiers &= ~ MOUSE_SHIFT;
2896 }
2897
2898 /* If the selection is done, allow the right button to extend it.
2899 * If the selection is cleared, allow the right button to start it
2900 * from the cursor position. */
2901 if (button == MOUSE_RIGHT)
2902 {
2903 if (clip_star.state == SELECT_CLEARED)
2904 {
2905 if (State & CMDLINE)
2906 {
2907 col = msg_col;
2908 row = msg_row;
2909 }
2910 else
2911 {
2912 col = curwin->w_wcol;
2913 row = curwin->w_wrow + W_WINROW(curwin);
2914 }
2915 clip_start_selection(col, row, FALSE);
2916 }
2917 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2918 repeated_click);
2919 did_clip = TRUE;
2920 }
2921 /* Allow the left button to start the selection */
2922 else if (button ==
2923# ifdef RISCOS
2924 /* Only start a drag on a drag event. Otherwise
2925 * we don't get a release event. */
2926 MOUSE_DRAG
2927# else
2928 MOUSE_LEFT
2929# endif
2930 )
2931 {
2932 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2933 did_clip = TRUE;
2934 }
2935# ifdef RISCOS
2936 else if (button == MOUSE_LEFT)
2937 {
2938 clip_clear_selection();
2939 did_clip = TRUE;
2940 }
2941# endif
2942
2943 /* Always allow pasting */
2944 if (button != MOUSE_MIDDLE)
2945 {
2946 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2947 return;
2948 if (checkfor != MOUSE_COMMAND)
2949 button = MOUSE_LEFT;
2950 }
2951 repeated_click = FALSE;
2952 }
2953
2954 if (clip_star.state != SELECT_CLEARED && !did_clip)
2955 clip_clear_selection();
2956#endif
2957
2958 /* Don't put events in the input queue now. */
2959 if (hold_gui_events)
2960 return;
2961
2962 row = gui_xy2colrow(x, y, &col);
2963
2964 /*
2965 * If we are dragging and the mouse hasn't moved far enough to be on a
2966 * different character, then don't send an event to vim.
2967 */
2968 if (button == MOUSE_DRAG)
2969 {
2970 if (row == prev_row && col == prev_col)
2971 return;
2972 /* Dragging above the window, set "row" to -1 to cause a scroll. */
2973 if (y < 0)
2974 row = -1;
2975 }
2976
2977 /*
2978 * If topline has changed (window scrolled) since the last click, reset
2979 * repeated_click, because we don't want starting Visual mode when
2980 * clicking on a different character in the text.
2981 */
2982 if (curwin->w_topline != gui_prev_topline
2983#ifdef FEAT_DIFF
2984 || curwin->w_topfill != gui_prev_topfill
2985#endif
2986 )
2987 repeated_click = FALSE;
2988
2989 string[0] = CSI; /* this sequence is recognized by check_termcode() */
2990 string[1] = KS_MOUSE;
2991 string[2] = KE_FILLER;
2992 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
2993 {
2994 if (repeated_click)
2995 {
2996 /*
2997 * Handle multiple clicks. They only count if the mouse is still
2998 * pointing at the same character.
2999 */
3000 if (button != prev_button || row != prev_row || col != prev_col)
3001 num_clicks = 1;
3002 else if (++num_clicks > 4)
3003 num_clicks = 1;
3004 }
3005 else
3006 num_clicks = 1;
3007 prev_button = button;
3008 gui_prev_topline = curwin->w_topline;
3009#ifdef FEAT_DIFF
3010 gui_prev_topfill = curwin->w_topfill;
3011#endif
3012
3013 string[3] = (char_u)(button | 0x20);
3014 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3015 }
3016 else
3017 string[3] = (char_u)button;
3018
3019 string[3] |= modifiers;
3020 fill_mouse_coord(string + 4, col, row);
3021 add_to_input_buf(string, 8);
3022
3023 if (row < 0)
3024 prev_row = 0;
3025 else
3026 prev_row = row;
3027 prev_col = col;
3028
3029 /*
3030 * We need to make sure this is cleared since Athena doesn't tell us when
3031 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3032 */
3033#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3034 gui.dragged_sb = SBAR_NONE;
3035#endif
3036}
3037
3038/*
3039 * Convert x and y coordinate to column and row in text window.
3040 * Corrects for multi-byte character.
3041 * returns column in "*colp" and row as return value;
3042 */
3043 int
3044gui_xy2colrow(x, y, colp)
3045 int x;
3046 int y;
3047 int *colp;
3048{
3049 int col = check_col(X_2_COL(x));
3050 int row = check_row(Y_2_ROW(y));
3051
3052#ifdef FEAT_MBYTE
3053 *colp = mb_fix_col(col, row);
3054#else
3055 *colp = col;
3056#endif
3057 return row;
3058}
3059
3060#if defined(FEAT_MENU) || defined(PROTO)
3061/*
3062 * Callback function for when a menu entry has been selected.
3063 */
3064 void
3065gui_menu_cb(menu)
3066 vimmenu_T *menu;
3067{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003068 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 /* Don't put events in the input queue now. */
3071 if (hold_gui_events)
3072 return;
3073
3074 bytes[0] = CSI;
3075 bytes[1] = KS_MENU;
3076 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003077 add_to_input_buf(bytes, 3);
3078 add_long_to_buf((long_u)menu, bytes);
3079 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080}
3081#endif
3082
Bram Moolenaare45828b2006-02-15 22:12:56 +00003083static int prev_which_scrollbars[3] = {-1, -1, -1};
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085/*
3086 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003087 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * in p_go.
3089 */
3090/*ARGSUSED*/
3091 void
3092gui_init_which_components(oldval)
3093 char_u *oldval;
3094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#ifdef FEAT_MENU
3096 static int prev_menu_is_active = -1;
3097#endif
3098#ifdef FEAT_TOOLBAR
3099 static int prev_toolbar = -1;
3100 int using_toolbar = FALSE;
3101#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003102#ifdef FEAT_GUI_TABLINE
3103 static int prev_has_tabline = FALSE;
3104 int using_tabline;
3105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106#ifdef FEAT_FOOTER
3107 static int prev_footer = -1;
3108 int using_footer = FALSE;
3109#endif
3110#if defined(FEAT_MENU) && !defined(WIN16)
3111 static int prev_tearoff = -1;
3112 int using_tearoff = FALSE;
3113#endif
3114
3115 char_u *p;
3116 int i;
3117#ifdef FEAT_MENU
3118 int grey_old, grey_new;
3119 char_u *temp;
3120#endif
3121 win_T *wp;
3122 int need_set_size;
3123 int fix_size;
3124
3125#ifdef FEAT_MENU
3126 if (oldval != NULL && gui.in_use)
3127 {
3128 /*
3129 * Check if the menu's go from grey to non-grey or vise versa.
3130 */
3131 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3132 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3133 if (grey_old != grey_new)
3134 {
3135 temp = p_go;
3136 p_go = oldval;
3137 gui_update_menus(MENU_ALL_MODES);
3138 p_go = temp;
3139 }
3140 }
3141 gui.menu_is_active = FALSE;
3142#endif
3143
3144 for (i = 0; i < 3; i++)
3145 gui.which_scrollbars[i] = FALSE;
3146 for (p = p_go; *p; p++)
3147 switch (*p)
3148 {
3149 case GO_LEFT:
3150 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3151 break;
3152 case GO_RIGHT:
3153 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3154 break;
3155#ifdef FEAT_VERTSPLIT
3156 case GO_VLEFT:
3157 if (win_hasvertsplit())
3158 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3159 break;
3160 case GO_VRIGHT:
3161 if (win_hasvertsplit())
3162 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3163 break;
3164#endif
3165 case GO_BOT:
3166 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3167 break;
3168#ifdef FEAT_MENU
3169 case GO_MENUS:
3170 gui.menu_is_active = TRUE;
3171 break;
3172#endif
3173 case GO_GREY:
3174 /* make menu's have grey items, ignored here */
3175 break;
3176#ifdef FEAT_TOOLBAR
3177 case GO_TOOLBAR:
3178 using_toolbar = TRUE;
3179 break;
3180#endif
3181#ifdef FEAT_FOOTER
3182 case GO_FOOTER:
3183 using_footer = TRUE;
3184 break;
3185#endif
3186 case GO_TEAROFF:
3187#if defined(FEAT_MENU) && !defined(WIN16)
3188 using_tearoff = TRUE;
3189#endif
3190 break;
3191 default:
3192 /* Ignore options that are not supported */
3193 break;
3194 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (gui.in_use)
3197 {
3198 need_set_size = FALSE;
3199 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003200
3201#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003202 /* Update the GUI tab line, it may appear or disappear. This may
3203 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003204 using_tabline = gui_has_tabline();
3205 if (prev_has_tabline != using_tabline)
3206 {
3207 prev_has_tabline = using_tabline;
3208 gui_update_tabline();
3209 need_set_size = TRUE;
3210 if (using_tabline)
3211 fix_size = TRUE;
3212 if (!gui_use_tabline())
3213 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3214 }
3215#endif
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 for (i = 0; i < 3; i++)
3218 {
3219 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3220 {
3221 if (i == SBAR_BOTTOM)
3222 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3223 gui.which_scrollbars[i]);
3224 else
3225 {
3226 FOR_ALL_WINDOWS(wp)
3227 {
3228 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3229 }
3230 }
3231 need_set_size = TRUE;
3232 if (gui.which_scrollbars[i])
3233 fix_size = TRUE;
3234 }
3235 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3236 }
3237
3238#ifdef FEAT_MENU
3239 if (gui.menu_is_active != prev_menu_is_active)
3240 {
3241 /* We don't want a resize event change "Rows" here, save and
3242 * restore it. Resizing is handled below. */
3243 i = Rows;
3244 gui_mch_enable_menu(gui.menu_is_active);
3245 Rows = i;
3246 prev_menu_is_active = gui.menu_is_active;
3247 need_set_size = TRUE;
3248 if (gui.menu_is_active)
3249 fix_size = TRUE;
3250 }
3251#endif
3252
3253#ifdef FEAT_TOOLBAR
3254 if (using_toolbar != prev_toolbar)
3255 {
3256 gui_mch_show_toolbar(using_toolbar);
3257 prev_toolbar = using_toolbar;
3258 need_set_size = TRUE;
3259 if (using_toolbar)
3260 fix_size = TRUE;
3261 }
3262#endif
3263#ifdef FEAT_FOOTER
3264 if (using_footer != prev_footer)
3265 {
3266 gui_mch_enable_footer(using_footer);
3267 prev_footer = using_footer;
3268 need_set_size = TRUE;
3269 if (using_footer)
3270 fix_size = TRUE;
3271 }
3272#endif
3273#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3274 if (using_tearoff != prev_tearoff)
3275 {
3276 gui_mch_toggle_tearoffs(using_tearoff);
3277 prev_tearoff = using_tearoff;
3278 }
3279#endif
3280 if (need_set_size)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003281 {
3282#ifdef FEAT_GUI_GTK
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003283 long c = Columns;
3284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /* Adjust the size of the window to make the text area keep the
3286 * same size and to avoid that part of our window is off-screen
3287 * and a scrollbar can't be used, for example. */
3288 gui_set_shellsize(FALSE, fix_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003289
3290#ifdef FEAT_GUI_GTK
3291 /* GTK has the annoying habit of sending us resize events when
3292 * changing the window size ourselves. This mostly happens when
3293 * waiting for a character to arrive, quite unpredictably, and may
3294 * change Columns and Rows when we don't want it. Wait for a
3295 * character here to avoid this effect.
3296 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003297 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003298 * Don't do this while starting up though.
3299 * And don't change Rows, it may have be reduced intentionally
3300 * when adding menu/toolbar/tabline. */
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003301 if (!gui.starting)
3302 (void)char_avail();
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003303 Columns = c;
3304#endif
3305 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003306#ifdef FEAT_WINDOWS
3307 /* When the console tabline appears or disappears the window positions
3308 * change. */
3309 if (firstwin->w_winrow != tabline_height())
3310 shell_new_rows(); /* recompute window positions and heights */
3311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313}
3314
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003315#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3316/*
3317 * Return TRUE if the GUI is taking care of the tabline.
3318 * It may still be hidden if 'showtabline' is zero.
3319 */
3320 int
3321gui_use_tabline()
3322{
3323 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3324}
3325
3326/*
3327 * Return TRUE if the GUI is showing the tabline.
3328 * This uses 'showtabline'.
3329 */
3330 static int
3331gui_has_tabline()
3332{
3333 if (!gui_use_tabline()
3334 || p_stal == 0
3335 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3336 return FALSE;
3337 return TRUE;
3338}
3339
3340/*
3341 * Update the tabline.
3342 * This may display/undisplay the tabline and update the labels.
3343 */
3344 void
3345gui_update_tabline()
3346{
3347 int showit = gui_has_tabline();
3348
3349 if (!gui.starting && starting == 0)
3350 {
3351 gui_mch_show_tabline(showit);
3352 if (showit != 0)
3353 gui_mch_update_tabline();
3354 }
3355}
3356
3357/*
3358 * Get the label for tab page "tp" into NameBuff[].
3359 */
3360 void
3361get_tabline_label(tp)
3362 tabpage_T *tp;
3363{
3364 int modified = FALSE;
3365 char_u buf[40];
3366 int wincount;
3367 win_T *wp;
3368
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003369 /* Use 'guitablabel' if it's set. */
3370 if (*p_gtl != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003371 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003372 int use_sandbox = FALSE;
3373 int save_called_emsg = called_emsg;
3374 char_u res[MAXPATHL];
3375
3376 called_emsg = FALSE;
3377
3378 printer_page_num = tabpage_index(tp);
3379# ifdef FEAT_EVAL
3380 set_vim_var_nr(VV_LNUM, printer_page_num);
3381 use_sandbox = was_set_insecurely((char_u *)"guitablabel", 0);
3382# endif
3383 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
3384 build_stl_str_hl(tp == curtab ? curwin : tp->tp_curwin,
3385 res, MAXPATHL, p_gtl, use_sandbox,
3386 0, (int)Columns, NULL, NULL);
3387 STRCPY(NameBuff, res);
3388
3389 if (called_emsg)
3390 {
3391 set_string_option_direct((char_u *)"guitablabel", -1,
3392 (char_u *)"", OPT_FREE);
3393# ifdef FEAT_EVAL
3394 set_option_scriptID((char_u *)"guitablabel", SID_ERROR);
3395# endif
3396 }
3397 called_emsg |= save_called_emsg;
3398 }
3399 else
3400 {
3401 /* Get the buffer name into NameBuff[] */
3402 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3403
3404 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3405 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3406 if (bufIsChanged(wp->w_buffer))
3407 modified = TRUE;
3408 if (modified || wincount > 1)
3409 {
3410 if (wincount > 1)
3411 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3412 else
3413 buf[0] = NUL;
3414 if (modified)
3415 STRCAT(buf, "+");
3416 STRCAT(buf, " ");
3417 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3418 mch_memmove(NameBuff, buf, STRLEN(buf));
3419 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003420 }
3421}
3422
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003423/*
3424 * Send the event for clicking to select tab page "nr".
3425 * Returns TRUE if it was done, FALSE when skipped because we are already at
3426 * that tab page.
3427 */
3428 int
3429send_tabline_event(nr)
3430 int nr;
3431{
3432 char_u string[3];
3433
3434 if (nr == tabpage_index(curtab))
3435 return FALSE;
3436 string[0] = CSI;
3437 string[1] = KS_TABLINE;
3438 string[2] = KE_FILLER;
3439 add_to_input_buf(string, 3);
3440 string[0] = nr;
3441 add_to_input_buf_csi(string, 1);
3442 return TRUE;
3443}
3444
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447/*
3448 * Scrollbar stuff:
3449 */
3450
Bram Moolenaare45828b2006-02-15 22:12:56 +00003451#if defined(FEAT_WINDOWS) || defined(PROTO)
3452/*
3453 * Remove all scrollbars. Used before switching to another tab page.
3454 */
3455 void
3456gui_remove_scrollbars()
3457{
3458 int i;
3459 win_T *wp;
3460
3461 for (i = 0; i < 3; i++)
3462 {
3463 if (i == SBAR_BOTTOM)
3464 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3465 else
3466 {
3467 FOR_ALL_WINDOWS(wp)
3468 {
3469 gui_do_scrollbar(wp, i, FALSE);
3470 }
3471 }
3472 prev_which_scrollbars[i] = -1;
3473 }
3474}
3475#endif
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 void
3478gui_create_scrollbar(sb, type, wp)
3479 scrollbar_T *sb;
3480 int type;
3481 win_T *wp;
3482{
3483 static int sbar_ident = 0;
3484
3485 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3486 sb->wp = wp;
3487 sb->type = type;
3488 sb->value = 0;
3489#ifdef FEAT_GUI_ATHENA
3490 sb->pixval = 0;
3491#endif
3492 sb->size = 1;
3493 sb->max = 1;
3494 sb->top = 0;
3495 sb->height = 0;
3496#ifdef FEAT_VERTSPLIT
3497 sb->width = 0;
3498#endif
3499 sb->status_height = 0;
3500 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3501}
3502
3503/*
3504 * Find the scrollbar with the given index.
3505 */
3506 scrollbar_T *
3507gui_find_scrollbar(ident)
3508 long ident;
3509{
3510 win_T *wp;
3511
3512 if (gui.bottom_sbar.ident == ident)
3513 return &gui.bottom_sbar;
3514 FOR_ALL_WINDOWS(wp)
3515 {
3516 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3517 return &wp->w_scrollbars[SBAR_LEFT];
3518 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3519 return &wp->w_scrollbars[SBAR_RIGHT];
3520 }
3521 return NULL;
3522}
3523
3524/*
3525 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3526 *
3527 * For Win32, Macintosh and GTK+ 2:
3528 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3529 * you stop dragging the scrollbar. We get here each time the scrollbar is
3530 * dragged another pixel, but as far as the rest of vim goes, it thinks
3531 * we're just hanging in the call to DispatchMessage() in
3532 * process_message(). The DispatchMessage() call that hangs was passed a
3533 * mouse button click event in the scrollbar window. -- webb.
3534 *
3535 * Solution: Do the scrolling right here. But only when allowed.
3536 * Ignore the scrollbars while executing an external command or when there
3537 * are still characters to be processed.
3538 */
3539 void
3540gui_drag_scrollbar(sb, value, still_dragging)
3541 scrollbar_T *sb;
3542 long value;
3543 int still_dragging;
3544{
3545#ifdef FEAT_WINDOWS
3546 win_T *wp;
3547#endif
3548 int sb_num;
3549#ifdef USE_ON_FLY_SCROLL
3550 colnr_T old_leftcol = curwin->w_leftcol;
3551# ifdef FEAT_SCROLLBIND
3552 linenr_T old_topline = curwin->w_topline;
3553# endif
3554# ifdef FEAT_DIFF
3555 int old_topfill = curwin->w_topfill;
3556# endif
3557#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003558 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int byte_count;
3560#endif
3561
3562 if (sb == NULL)
3563 return;
3564
3565 /* Don't put events in the input queue now. */
3566 if (hold_gui_events)
3567 return;
3568
3569#ifdef FEAT_CMDWIN
3570 if (cmdwin_type != 0 && sb->wp != curwin)
3571 return;
3572#endif
3573
3574 if (still_dragging)
3575 {
3576 if (sb->wp == NULL)
3577 gui.dragged_sb = SBAR_BOTTOM;
3578 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3579 gui.dragged_sb = SBAR_LEFT;
3580 else
3581 gui.dragged_sb = SBAR_RIGHT;
3582 gui.dragged_wp = sb->wp;
3583 }
3584 else
3585 {
3586 gui.dragged_sb = SBAR_NONE;
3587#ifdef HAVE_GTK2
3588 /* Keep the "dragged_wp" value until after the scrolling, for when the
3589 * moust button is released. GTK2 doesn't send the button-up event. */
3590 gui.dragged_wp = NULL;
3591#endif
3592 }
3593
3594 /* Vertical sbar info is kept in the first sbar (the left one) */
3595 if (sb->wp != NULL)
3596 sb = &sb->wp->w_scrollbars[0];
3597
3598 /*
3599 * Check validity of value
3600 */
3601 if (value < 0)
3602 value = 0;
3603#ifdef SCROLL_PAST_END
3604 else if (value > sb->max)
3605 value = sb->max;
3606#else
3607 if (value > sb->max - sb->size + 1)
3608 value = sb->max - sb->size + 1;
3609#endif
3610
3611 sb->value = value;
3612
3613#ifdef USE_ON_FLY_SCROLL
3614 /* When not allowed to do the scrolling right now, return. */
3615 if (dont_scroll || input_available())
3616 return;
3617#endif
3618
3619#ifdef FEAT_RIGHTLEFT
3620 if (sb->wp == NULL && curwin->w_p_rl)
3621 {
3622 value = sb->max + 1 - sb->size - value;
3623 if (value < 0)
3624 value = 0;
3625 }
3626#endif
3627
3628 if (sb->wp != NULL) /* vertical scrollbar */
3629 {
3630 sb_num = 0;
3631#ifdef FEAT_WINDOWS
3632 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3633 sb_num++;
3634 if (wp == NULL)
3635 return;
3636#else
3637 if (sb->wp != curwin)
3638 return;
3639#endif
3640
3641#ifdef USE_ON_FLY_SCROLL
3642 current_scrollbar = sb_num;
3643 scrollbar_value = value;
3644 if (State & NORMAL)
3645 {
3646 gui_do_scroll();
3647 setcursor();
3648 }
3649 else if (State & INSERT)
3650 {
3651 ins_scroll();
3652 setcursor();
3653 }
3654 else if (State & CMDLINE)
3655 {
3656 if (msg_scrolled == 0)
3657 {
3658 gui_do_scroll();
3659 redrawcmdline();
3660 }
3661 }
3662# ifdef FEAT_FOLDING
3663 /* Value may have been changed for closed fold. */
3664 sb->value = sb->wp->w_topline - 1;
3665# endif
3666#else
3667 bytes[0] = CSI;
3668 bytes[1] = KS_VER_SCROLLBAR;
3669 bytes[2] = KE_FILLER;
3670 bytes[3] = (char_u)sb_num;
3671 byte_count = 4;
3672#endif
3673 }
3674 else
3675 {
3676#ifdef USE_ON_FLY_SCROLL
3677 scrollbar_value = value;
3678
3679 if (State & NORMAL)
3680 gui_do_horiz_scroll();
3681 else if (State & INSERT)
3682 ins_horscroll();
3683 else if (State & CMDLINE)
3684 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003685 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 gui_do_horiz_scroll();
3688 redrawcmdline();
3689 }
3690 }
3691 if (old_leftcol != curwin->w_leftcol)
3692 {
3693 updateWindow(curwin); /* update window, status and cmdline */
3694 setcursor();
3695 }
3696#else
3697 bytes[0] = CSI;
3698 bytes[1] = KS_HOR_SCROLLBAR;
3699 bytes[2] = KE_FILLER;
3700 byte_count = 3;
3701#endif
3702 }
3703
3704#ifdef USE_ON_FLY_SCROLL
3705# ifdef FEAT_SCROLLBIND
3706 /*
3707 * synchronize other windows, as necessary according to 'scrollbind'
3708 */
3709 if (curwin->w_p_scb
3710 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3711 || (sb->wp == curwin && (curwin->w_topline != old_topline
3712# ifdef FEAT_DIFF
3713 || curwin->w_topfill != old_topfill
3714# endif
3715 ))))
3716 {
3717 do_check_scrollbind(TRUE);
3718 /* need to update the window right here */
3719 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3720 if (wp->w_redr_type > 0)
3721 updateWindow(wp);
3722 setcursor();
3723 }
3724# endif
3725 out_flush();
3726 gui_update_cursor(FALSE, TRUE);
3727#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003728 add_to_input_buf(bytes, byte_count);
3729 add_long_to_buf((long_u)value, bytes);
3730 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731#endif
3732}
3733
3734/*
3735 * Scrollbar stuff:
3736 */
3737
3738 void
3739gui_update_scrollbars(force)
3740 int force; /* Force all scrollbars to get updated */
3741{
3742 win_T *wp;
3743 scrollbar_T *sb;
3744 long val, size, max; /* need 32 bits here */
3745 int which_sb;
3746 int h, y;
3747#ifdef FEAT_VERTSPLIT
3748 static win_T *prev_curwin = NULL;
3749#endif
3750
3751 /* Update the horizontal scrollbar */
3752 gui_update_horiz_scrollbar(force);
3753
3754#ifndef WIN3264
3755 /* Return straight away if there is neither a left nor right scrollbar.
3756 * On MS-Windows this is required anyway for scrollwheel messages. */
3757 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3758 return;
3759#endif
3760
3761 /*
3762 * Don't want to update a scrollbar while we're dragging it. But if we
3763 * have both a left and right scrollbar, and we drag one of them, we still
3764 * need to update the other one.
3765 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003766 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3767 && gui.which_scrollbars[SBAR_LEFT]
3768 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
3770 /*
3771 * If we have two scrollbars and one of them is being dragged, just
3772 * copy the scrollbar position from the dragged one to the other one.
3773 */
3774 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3775 if (gui.dragged_wp != NULL)
3776 gui_mch_set_scrollbar_thumb(
3777 &gui.dragged_wp->w_scrollbars[which_sb],
3778 gui.dragged_wp->w_scrollbars[0].value,
3779 gui.dragged_wp->w_scrollbars[0].size,
3780 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782
3783 /* avoid that moving components around generates events */
3784 ++hold_gui_events;
3785
3786 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3787 {
3788 if (wp->w_buffer == NULL) /* just in case */
3789 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003790 /* Skip a scrollbar that is being dragged. */
3791 if (!force && (gui.dragged_sb == SBAR_LEFT
3792 || gui.dragged_sb == SBAR_RIGHT)
3793 && gui.dragged_wp == wp)
3794 continue;
3795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef SCROLL_PAST_END
3797 max = wp->w_buffer->b_ml.ml_line_count - 1;
3798#else
3799 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3800#endif
3801 if (max < 0) /* empty buffer */
3802 max = 0;
3803 val = wp->w_topline - 1;
3804 size = wp->w_height;
3805#ifdef SCROLL_PAST_END
3806 if (val > max) /* just in case */
3807 val = max;
3808#else
3809 if (size > max + 1) /* just in case */
3810 size = max + 1;
3811 if (val > max - size + 1)
3812 val = max - size + 1;
3813#endif
3814 if (val < 0) /* minimal value is 0 */
3815 val = 0;
3816
3817 /*
3818 * Scrollbar at index 0 (the left one) contains all the information.
3819 * It would be the same info for left and right so we just store it for
3820 * one of them.
3821 */
3822 sb = &wp->w_scrollbars[0];
3823
3824 /*
3825 * Note: no check for valid w_botline. If it's not valid the
3826 * scrollbars will be updated later anyway.
3827 */
3828 if (size < 1 || wp->w_botline - 2 > max)
3829 {
3830 /*
3831 * This can happen during changing files. Just don't update the
3832 * scrollbar for now.
3833 */
3834 sb->height = 0; /* Force update next time */
3835 if (gui.which_scrollbars[SBAR_LEFT])
3836 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3837 if (gui.which_scrollbars[SBAR_RIGHT])
3838 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3839 continue;
3840 }
3841 if (force || sb->height != wp->w_height
3842#ifdef FEAT_WINDOWS
3843 || sb->top != wp->w_winrow
3844 || sb->status_height != wp->w_status_height
3845# ifdef FEAT_VERTSPLIT
3846 || sb->width != wp->w_width
3847 || prev_curwin != curwin
3848# endif
3849#endif
3850 )
3851 {
3852 /* Height, width or position of scrollbar has changed. For
3853 * vertical split: curwin changed. */
3854 sb->height = wp->w_height;
3855#ifdef FEAT_WINDOWS
3856 sb->top = wp->w_winrow;
3857 sb->status_height = wp->w_status_height;
3858# ifdef FEAT_VERTSPLIT
3859 sb->width = wp->w_width;
3860# endif
3861#endif
3862
3863 /* Calculate height and position in pixels */
3864 h = (sb->height + sb->status_height) * gui.char_height;
3865 y = sb->top * gui.char_height + gui.border_offset;
3866#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
3867 if (gui.menu_is_active)
3868 y += gui.menu_height;
3869#endif
3870
3871#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
3872 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
3873# ifdef FEAT_GUI_ATHENA
3874 y += gui.toolbar_height;
3875# else
3876# ifdef FEAT_GUI_MSWIN
3877 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
3878# endif
3879# endif
3880#endif
3881
3882#ifdef FEAT_WINDOWS
3883 if (wp->w_winrow == 0)
3884#endif
3885 {
3886 /* Height of top scrollbar includes width of top border */
3887 h += gui.border_offset;
3888 y -= gui.border_offset;
3889 }
3890 if (gui.which_scrollbars[SBAR_LEFT])
3891 {
3892 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
3893 gui.left_sbar_x, y,
3894 gui.scrollbar_width, h);
3895 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
3896 }
3897 if (gui.which_scrollbars[SBAR_RIGHT])
3898 {
3899 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
3900 gui.right_sbar_x, y,
3901 gui.scrollbar_width, h);
3902 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
3903 }
3904 }
3905
3906 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
3907 * checking if the thumb moved at least a pixel. Only do this for
3908 * Athena, most other GUIs require the update anyway to make the
3909 * arrows work. */
3910#ifdef FEAT_GUI_ATHENA
3911 if (max == 0)
3912 y = 0;
3913 else
3914 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
3915 if (force || sb->pixval != y || sb->size != size || sb->max != max)
3916#else
3917 if (force || sb->value != val || sb->size != size || sb->max != max)
3918#endif
3919 {
3920 /* Thumb of scrollbar has moved */
3921 sb->value = val;
3922#ifdef FEAT_GUI_ATHENA
3923 sb->pixval = y;
3924#endif
3925 sb->size = size;
3926 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003927 if (gui.which_scrollbars[SBAR_LEFT]
3928 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
3930 val, size, max);
3931 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003932 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
3934 val, size, max);
3935 }
3936 }
3937#ifdef FEAT_VERTSPLIT
3938 prev_curwin = curwin;
3939#endif
3940 --hold_gui_events;
3941}
3942
3943/*
3944 * Enable or disable a scrollbar.
3945 * Check for scrollbars for vertically split windows which are not enabled
3946 * sometimes.
3947 */
3948 static void
3949gui_do_scrollbar(wp, which, enable)
3950 win_T *wp;
3951 int which; /* SBAR_LEFT or SBAR_RIGHT */
3952 int enable; /* TRUE to enable scrollbar */
3953{
3954#ifdef FEAT_VERTSPLIT
3955 int midcol = curwin->w_wincol + curwin->w_width / 2;
3956 int has_midcol = (wp->w_wincol <= midcol
3957 && wp->w_wincol + wp->w_width >= midcol);
3958
3959 /* Only enable scrollbars that contain the middle column of the current
3960 * window. */
3961 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
3962 {
3963 /* Scrollbars only on one side. Don't enable scrollbars that don't
3964 * contain the middle column of the current window. */
3965 if (!has_midcol)
3966 enable = FALSE;
3967 }
3968 else
3969 {
3970 /* Scrollbars on both sides. Don't enable scrollbars that neither
3971 * contain the middle column of the current window nor are on the far
3972 * side. */
3973 if (midcol > Columns / 2)
3974 {
3975 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
3976 enable = FALSE;
3977 }
3978 else
3979 {
3980 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
3981 : !has_midcol)
3982 enable = FALSE;
3983 }
3984 }
3985#endif
3986 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
3987}
3988
3989/*
3990 * Scroll a window according to the values set in the globals current_scrollbar
3991 * and scrollbar_value. Return TRUE if the cursor in the current window moved
3992 * or FALSE otherwise.
3993 */
3994 int
3995gui_do_scroll()
3996{
3997 win_T *wp, *save_wp;
3998 int i;
3999 long nlines;
4000 pos_T old_cursor;
4001 linenr_T old_topline;
4002#ifdef FEAT_DIFF
4003 int old_topfill;
4004#endif
4005
4006 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4007 if (wp == NULL)
4008 break;
4009 if (wp == NULL)
4010 /* Couldn't find window */
4011 return FALSE;
4012
4013 /*
4014 * Compute number of lines to scroll. If zero, nothing to do.
4015 */
4016 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4017 if (nlines == 0)
4018 return FALSE;
4019
4020 save_wp = curwin;
4021 old_topline = wp->w_topline;
4022#ifdef FEAT_DIFF
4023 old_topfill = wp->w_topfill;
4024#endif
4025 old_cursor = wp->w_cursor;
4026 curwin = wp;
4027 curbuf = wp->w_buffer;
4028 if (nlines < 0)
4029 scrolldown(-nlines, gui.dragged_wp == NULL);
4030 else
4031 scrollup(nlines, gui.dragged_wp == NULL);
4032 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4033 * the mouse-up event already, but we still want it to behave like when
4034 * dragging. But not the next click in an arrow. */
4035 if (gui.dragged_sb == SBAR_NONE)
4036 gui.dragged_wp = NULL;
4037
4038 if (old_topline != wp->w_topline
4039#ifdef FEAT_DIFF
4040 || old_topfill != wp->w_topfill
4041#endif
4042 )
4043 {
4044 if (p_so != 0)
4045 {
4046 cursor_correct(); /* fix window for 'so' */
4047 update_topline(); /* avoid up/down jump */
4048 }
4049 if (old_cursor.lnum != wp->w_cursor.lnum)
4050 coladvance(wp->w_curswant);
4051#ifdef FEAT_SCROLLBIND
4052 wp->w_scbind_pos = wp->w_topline;
4053#endif
4054 }
4055
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004056 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4057 validate_cursor();
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 curwin = save_wp;
4060 curbuf = save_wp->w_buffer;
4061
4062 /*
4063 * Don't call updateWindow() when nothing has changed (it will overwrite
4064 * the status line!).
4065 */
4066 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004067 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068#ifdef FEAT_DIFF
4069 || old_topfill != wp->w_topfill
4070#endif
4071 )
4072 {
4073 redraw_win_later(wp, VALID);
4074 updateWindow(wp); /* update window, status line, and cmdline */
4075 }
4076
4077 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4078}
4079
4080
4081/*
4082 * Horizontal scrollbar stuff:
4083 */
4084
4085/*
4086 * Return length of line "lnum" for horizontal scrolling.
4087 */
4088 static colnr_T
4089scroll_line_len(lnum)
4090 linenr_T lnum;
4091{
4092 char_u *p;
4093 colnr_T col;
4094 int w;
4095
4096 p = ml_get(lnum);
4097 col = 0;
4098 if (*p != NUL)
4099 for (;;)
4100 {
4101 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004102 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (*p == NUL) /* don't count the last character */
4104 break;
4105 col += w;
4106 }
4107 return col;
4108}
4109
4110/* Remember which line is currently the longest, so that we don't have to
4111 * search for it when scrolling horizontally. */
4112static linenr_T longest_lnum = 0;
4113
4114 static void
4115gui_update_horiz_scrollbar(force)
4116 int force;
4117{
4118 long value, size, max; /* need 32 bit ints here */
4119
4120 if (!gui.which_scrollbars[SBAR_BOTTOM])
4121 return;
4122
4123 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4124 return;
4125
4126 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4127 return;
4128
4129 /*
4130 * It is possible for the cursor to be invalid if we're in the middle of
4131 * something (like changing files). If so, don't do anything for now.
4132 */
4133 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4134 {
4135 gui.bottom_sbar.value = -1;
4136 return;
4137 }
4138
4139 size = W_WIDTH(curwin);
4140 if (curwin->w_p_wrap)
4141 {
4142 value = 0;
4143#ifdef SCROLL_PAST_END
4144 max = 0;
4145#else
4146 max = W_WIDTH(curwin) - 1;
4147#endif
4148 }
4149 else
4150 {
4151 value = curwin->w_leftcol;
4152
4153 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4154 * line numbers, topline and botline can be invalid when displaying is
4155 * postponed. */
4156 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4157 && curwin->w_topline <= curwin->w_cursor.lnum
4158 && curwin->w_botline > curwin->w_cursor.lnum
4159 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4160 {
4161 linenr_T lnum;
4162 colnr_T n;
4163
4164 /* Use maximum of all visible lines. Remember the lnum of the
4165 * longest line, clostest to the cursor line. Used when scrolling
4166 * below. */
4167 max = 0;
4168 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4169 {
4170 n = scroll_line_len(lnum);
4171 if (n > (colnr_T)max)
4172 {
4173 max = n;
4174 longest_lnum = lnum;
4175 }
4176 else if (n == (colnr_T)max
4177 && abs((int)(lnum - curwin->w_cursor.lnum))
4178 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4179 longest_lnum = lnum;
4180 }
4181 }
4182 else
4183 /* Use cursor line only. */
4184 max = scroll_line_len(curwin->w_cursor.lnum);
4185#ifdef FEAT_VIRTUALEDIT
4186 if (virtual_active())
4187 {
4188 /* May move the cursor even further to the right. */
4189 if (curwin->w_virtcol >= (colnr_T)max)
4190 max = curwin->w_virtcol;
4191 }
4192#endif
4193
4194#ifndef SCROLL_PAST_END
4195 max += W_WIDTH(curwin) - 1;
4196#endif
4197 /* The line number isn't scrolled, thus there is less space when
4198 * 'number' is set (also for 'foldcolumn'). */
4199 size -= curwin_col_off();
4200#ifndef SCROLL_PAST_END
4201 max -= curwin_col_off();
4202#endif
4203 }
4204
4205#ifndef SCROLL_PAST_END
4206 if (value > max - size + 1)
4207 value = max - size + 1; /* limit the value to allowable range */
4208#endif
4209
4210#ifdef FEAT_RIGHTLEFT
4211 if (curwin->w_p_rl)
4212 {
4213 value = max + 1 - size - value;
4214 if (value < 0)
4215 {
4216 size += value;
4217 value = 0;
4218 }
4219 }
4220#endif
4221 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4222 && max == gui.bottom_sbar.max)
4223 return;
4224
4225 gui.bottom_sbar.value = value;
4226 gui.bottom_sbar.size = size;
4227 gui.bottom_sbar.max = max;
4228 gui.prev_wrap = curwin->w_p_wrap;
4229
4230 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4231}
4232
4233/*
4234 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4235 */
4236 int
4237gui_do_horiz_scroll()
4238{
4239 /* no wrapping, no scrolling */
4240 if (curwin->w_p_wrap)
4241 return FALSE;
4242
4243 if (curwin->w_leftcol == scrollbar_value)
4244 return FALSE;
4245
4246 curwin->w_leftcol = scrollbar_value;
4247
4248 /* When the line of the cursor is too short, move the cursor to the
4249 * longest visible line. Do a sanity check on "longest_lnum", just in
4250 * case. */
4251 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4252 && longest_lnum >= curwin->w_topline
4253 && longest_lnum < curwin->w_botline
4254 && !virtual_active())
4255 {
4256 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4257 {
4258 curwin->w_cursor.lnum = longest_lnum;
4259 curwin->w_cursor.col = 0;
4260 }
4261 }
4262
4263 return leftcol_changed();
4264}
4265
4266/*
4267 * Check that none of the colors are the same as the background color
4268 */
4269 void
4270gui_check_colors()
4271{
4272 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4273 {
4274 gui_set_bg_color((char_u *)"White");
4275 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4276 gui_set_fg_color((char_u *)"Black");
4277 }
4278}
4279
Bram Moolenaar3918c952005-03-15 22:34:55 +00004280 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281gui_set_fg_color(name)
4282 char_u *name;
4283{
4284 gui.norm_pixel = gui_get_color(name);
4285 hl_set_fg_color_name(vim_strsave(name));
4286}
4287
Bram Moolenaar3918c952005-03-15 22:34:55 +00004288 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289gui_set_bg_color(name)
4290 char_u *name;
4291{
4292 gui.back_pixel = gui_get_color(name);
4293 hl_set_bg_color_name(vim_strsave(name));
4294}
4295
4296/*
4297 * Allocate a color by name.
4298 * Returns INVALCOLOR and gives an error message when failed.
4299 */
4300 guicolor_T
4301gui_get_color(name)
4302 char_u *name;
4303{
4304 guicolor_T t;
4305
4306 if (*name == NUL)
4307 return INVALCOLOR;
4308 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004311#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 && gui.in_use
4313#endif
4314 )
4315 EMSG2(_("E254: Cannot allocate color %s"), name);
4316 return t;
4317}
4318
4319/*
4320 * Return the grey value of a color (range 0-255).
4321 */
4322 int
4323gui_get_lightness(pixel)
4324 guicolor_T pixel;
4325{
4326 long_u rgb = gui_mch_get_rgb(pixel);
4327
4328 return ( (((rgb >> 16) & 0xff) * 299)
4329 + (((rgb >> 8) & 0xff) * 587)
4330 + ((rgb & 0xff) * 114)) / 1000;
4331}
4332
4333#if defined(FEAT_GUI_X11) || defined(PROTO)
4334 void
4335gui_new_scrollbar_colors()
4336{
4337 win_T *wp;
4338
4339 /* Nothing to do if GUI hasn't started yet. */
4340 if (!gui.in_use)
4341 return;
4342
4343 FOR_ALL_WINDOWS(wp)
4344 {
4345 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4346 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4347 }
4348 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4349}
4350#endif
4351
4352/*
4353 * Call this when focus has changed.
4354 */
4355 void
4356gui_focus_change(in_focus)
4357 int in_focus;
4358{
4359/*
4360 * Skip this code to avoid drawing the cursor when debugging and switching
4361 * between the debugger window and gvim.
4362 */
4363#if 1
4364 gui.in_focus = in_focus;
4365 out_flush(); /* make sure output has been written */
4366 gui_update_cursor(TRUE, FALSE);
4367
4368# ifdef FEAT_XIM
4369 xim_set_focus(in_focus);
4370# endif
4371
4372 ui_focus_change(in_focus);
4373#endif
4374}
4375
4376/*
4377 * Called when the mouse moved (but not when dragging).
4378 */
4379 void
4380gui_mouse_moved(x, y)
4381 int x;
4382 int y;
4383{
4384 win_T *wp;
4385 char_u st[6];
4386
4387#ifdef FEAT_MOUSESHAPE
4388 /* Get window pointer, and update mouse shape as well. */
4389 wp = xy2win(x, y);
4390#endif
4391
4392 /* Only handle this when 'mousefocus' set and ... */
4393 if (p_mousef
4394 && !hold_gui_events /* not holding events */
4395 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4396 && State != HITRETURN /* but not hit-return prompt */
4397 && msg_scrolled == 0 /* no scrolled message */
4398 && !need_mouse_correct /* not moving the pointer */
4399 && gui.in_focus) /* gvim in focus */
4400 {
4401 /* Don't move the mouse when it's left or right of the Vim window */
4402 if (x < 0 || x > Columns * gui.char_width)
4403 return;
4404#ifndef FEAT_MOUSESHAPE
4405 wp = xy2win(x, y);
4406#endif
4407 if (wp == curwin || wp == NULL)
4408 return; /* still in the same old window, or none at all */
4409
4410 /*
4411 * format a mouse click on status line input
4412 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004413 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4414 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
4416 if (finish_op)
4417 {
4418 /* abort the current operator first */
4419 st[0] = ESC;
4420 add_to_input_buf(st, 1);
4421 }
4422 st[0] = CSI;
4423 st[1] = KS_MOUSE;
4424 st[2] = KE_FILLER;
4425 st[3] = (char_u)MOUSE_LEFT;
4426 fill_mouse_coord(st + 4,
4427#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004428 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429#else
4430 -1,
4431#endif
4432 wp->w_height + W_WINROW(wp));
4433
4434 add_to_input_buf(st, 8);
4435 st[3] = (char_u)MOUSE_RELEASE;
4436 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#ifdef FEAT_GUI_GTK
4438 /* Need to wake up the main loop */
4439 if (gtk_main_level() > 0)
4440 gtk_main_quit();
4441#endif
4442 }
4443}
4444
4445/*
4446 * Called when mouse should be moved to window with focus.
4447 */
4448 void
4449gui_mouse_correct()
4450{
4451 int x, y;
4452 win_T *wp = NULL;
4453
4454 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004455
4456 if (!(gui.in_use && p_mousef))
4457 return;
4458
4459 gui_mch_getmouse(&x, &y);
4460 /* Don't move the mouse when it's left or right of the Vim window */
4461 if (x < 0 || x > Columns * gui.char_width)
4462 return;
4463 if (y >= 0)
4464 wp = xy2win(x, y);
4465 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004467 validate_cline_row();
4468 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4469 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472}
4473
4474/*
4475 * Find window where the mouse pointer "y" coordinate is in.
4476 */
4477/*ARGSUSED*/
4478 static win_T *
4479xy2win(x, y)
4480 int x;
4481 int y;
4482{
4483#ifdef FEAT_WINDOWS
4484 int row;
4485 int col;
4486 win_T *wp;
4487
4488 row = Y_2_ROW(y);
4489 col = X_2_COL(x);
4490 if (row < 0 || col < 0) /* before first window */
4491 return NULL;
4492 wp = mouse_find_win(&row, &col);
4493# ifdef FEAT_MOUSESHAPE
4494 if (State == HITRETURN || State == ASKMORE)
4495 {
4496 if (Y_2_ROW(y) >= msg_row)
4497 update_mouseshape(SHAPE_IDX_MOREL);
4498 else
4499 update_mouseshape(SHAPE_IDX_MORE);
4500 }
4501 else if (row > wp->w_height) /* below status line */
4502 update_mouseshape(SHAPE_IDX_CLINE);
4503# ifdef FEAT_VERTSPLIT
4504 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4505 && (row != wp->w_height || !stl_connected(wp)))
4506 update_mouseshape(SHAPE_IDX_VSEP);
4507# endif
4508 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4509 && row == wp->w_height)
4510 update_mouseshape(SHAPE_IDX_STATUS);
4511 else
4512 update_mouseshape(-2);
4513# endif
4514 return wp;
4515#else
4516 return firstwin;
4517#endif
4518}
4519
4520/*
4521 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4522 * File names may be given to redefine the args list.
4523 */
4524 void
4525ex_gui(eap)
4526 exarg_T *eap;
4527{
4528 char_u *arg = eap->arg;
4529
4530 /*
4531 * Check for "-f" argument: foreground, don't fork.
4532 * Also don't fork when started with "gvim -f".
4533 * Do fork when using "gui -b".
4534 */
4535 if (arg[0] == '-'
4536 && (arg[1] == 'f' || arg[1] == 'b')
4537 && (arg[2] == NUL || vim_iswhite(arg[2])))
4538 {
4539 gui.dofork = (arg[1] == 'b');
4540 eap->arg = skipwhite(eap->arg + 2);
4541 }
4542 if (!gui.in_use)
4543 {
4544 /* Clear the command. Needed for when forking+exiting, to avoid part
4545 * of the argument ending up after the shell prompt. */
4546 msg_clr_eos_force();
4547 gui_start();
4548 }
4549 if (!ends_excmd(*eap->arg))
4550 ex_next(eap);
4551}
4552
4553#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004554 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555/*
4556 * This is shared between Athena, Motif and GTK.
4557 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004558static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560/*
4561 * Callback function for do_in_runtimepath().
4562 */
4563 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004566 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568 char_u *gfp_buffer = cookie;
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (STRLEN(fname) >= MAXPATHL)
4571 *gfp_buffer = NUL;
4572 else
4573 STRCPY(gfp_buffer, fname);
4574}
4575
4576/*
4577 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4578 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4579 */
4580 int
4581gui_find_bitmap(name, buffer, ext)
4582 char_u *name;
4583 char_u *buffer;
4584 char *ext;
4585{
4586 if (STRLEN(name) > MAXPATHL - 14)
4587 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004588 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4590 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 return FAIL;
4592 return OK;
4593}
4594
4595# if !defined(HAVE_GTK2) || defined(PROTO)
4596/*
4597 * Given the name of the "icon=" argument, try finding the bitmap file for the
4598 * icon. If it is an absolute path name, use it as it is. Otherwise append
4599 * "ext" and search for it in 'runtimepath'.
4600 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4601 * contains "name".
4602 */
4603 void
4604gui_find_iconfile(name, buffer, ext)
4605 char_u *name;
4606 char_u *buffer;
4607 char *ext;
4608{
4609 char_u buf[MAXPATHL + 1];
4610
4611 expand_env(name, buffer, MAXPATHL);
4612 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4613 STRCPY(buffer, buf);
4614}
4615# endif
4616#endif
4617
Bram Moolenaar9372a112005-12-06 19:59:18 +00004618#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 void
4620display_errors()
4621{
4622 char_u *p;
4623
4624 if (isatty(2))
4625 fflush(stderr);
4626 else if (error_ga.ga_data != NULL)
4627 {
4628 /* avoid putting up a message box with blanks only */
4629 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4630 if (!isspace(*p))
4631 {
4632 /* Truncate a very long message, it will go off-screen. */
4633 if (STRLEN(p) > 2000)
4634 STRCPY(p + 2000 - 14, "...(truncated)");
4635 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4636 p, (char_u *)_("&Ok"), 1, NULL);
4637 break;
4638 }
4639 ga_clear(&error_ga);
4640 }
4641}
4642#endif
4643
4644#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4645/*
4646 * Return TRUE if still starting up and there is no place to enter text.
4647 * For GTK and X11 we check if stderr is not a tty, which means we were
4648 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4649 * allow typing on stdin.
4650 */
4651 int
4652no_console_input()
4653{
4654 return ((!gui.in_use || gui.starting)
4655# ifndef NO_CONSOLE
4656 && !isatty(0) && !isatty(2)
4657# endif
4658 );
4659}
4660#endif
4661
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004662#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4663 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664/*
4665 * Update the current window and the screen.
4666 */
4667 void
4668gui_update_screen()
4669{
4670 update_topline();
4671 validate_cursor();
4672 update_screen(0); /* may need to update the screen */
4673 setcursor();
4674 out_flush(); /* make sure output has been written */
4675 gui_update_cursor(TRUE, FALSE);
4676 gui_mch_flush();
4677}
4678#endif
4679
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004680#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4682
4683/*
4684 * Get the text to use in a find/replace dialog. Uses the last search pattern
4685 * if the argument is empty.
4686 * Returns an allocated string.
4687 */
4688 char_u *
4689get_find_dialog_text(arg, wwordp, mcasep)
4690 char_u *arg;
4691 int *wwordp; /* return: TRUE if \< \> found */
4692 int *mcasep; /* return: TRUE if \C found */
4693{
4694 char_u *text;
4695
4696 if (*arg == NUL)
4697 text = last_search_pat();
4698 else
4699 text = arg;
4700 if (text != NULL)
4701 {
4702 text = vim_strsave(text);
4703 if (text != NULL)
4704 {
4705 int len = STRLEN(text);
4706 int i;
4707
4708 /* Remove "\V" */
4709 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4710 {
4711 mch_memmove(text, text + 2, (size_t)(len - 1));
4712 len -= 2;
4713 }
4714
4715 /* Recognize "\c" and "\C" and remove. */
4716 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4717 {
4718 *mcasep = (text[1] == 'C');
4719 mch_memmove(text, text + 2, (size_t)(len - 1));
4720 len -= 2;
4721 }
4722
4723 /* Recognize "\<text\>" and remove. */
4724 if (len >= 4
4725 && STRNCMP(text, "\\<", 2) == 0
4726 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4727 {
4728 *wwordp = TRUE;
4729 mch_memmove(text, text + 2, (size_t)(len - 4));
4730 text[len - 4] = NUL;
4731 }
4732
4733 /* Recognize "\/" or "\?" and remove. */
4734 for (i = 0; i + 1 < len; ++i)
4735 if (text[i] == '\\' && (text[i + 1] == '/'
4736 || text[i + 1] == '?'))
4737 {
4738 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4739 --len;
4740 }
4741 }
4742 }
4743 return text;
4744}
4745
4746/*
4747 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4748 */
4749 static void
4750concat_esc(gap, text, what)
4751 garray_T *gap;
4752 char_u *text;
4753 int what;
4754{
4755 while (*text != NUL)
4756 {
4757#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004758 int l = (*mb_ptr2len)(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (l > 1)
4760 {
4761 while (--l >= 0)
4762 ga_append(gap, *text++);
4763 continue;
4764 }
4765#endif
4766 if (*text == what)
4767 ga_append(gap, '\\');
4768 ga_append(gap, *text);
4769 ++text;
4770 }
4771}
4772
4773/*
4774 * Handle the press of a button in the find-replace dialog.
4775 * Return TRUE when something was added to the input buffer.
4776 */
4777 int
4778gui_do_findrepl(flags, find_text, repl_text, down)
4779 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4780 char_u *find_text;
4781 char_u *repl_text;
4782 int down; /* Search downwards. */
4783{
4784 garray_T ga;
4785 int i;
4786 int type = (flags & FRD_TYPE_MASK);
4787 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004788 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004789 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004792 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 ga_concat(&ga, (char_u *)"%s/");
4794
4795 ga_concat(&ga, (char_u *)"\\V");
4796 if (flags & FRD_MATCH_CASE)
4797 ga_concat(&ga, (char_u *)"\\C");
4798 else
4799 ga_concat(&ga, (char_u *)"\\c");
4800 if (flags & FRD_WHOLE_WORD)
4801 ga_concat(&ga, (char_u *)"\\<");
4802 if (type == FRD_REPLACEALL || down)
4803 concat_esc(&ga, find_text, '/'); /* escape slashes */
4804 else
4805 concat_esc(&ga, find_text, '?'); /* escape '?' */
4806 if (flags & FRD_WHOLE_WORD)
4807 ga_concat(&ga, (char_u *)"\\>");
4808
4809 if (type == FRD_REPLACEALL)
4810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 ga_concat(&ga, (char_u *)"/");
4812 concat_esc(&ga, repl_text, '/'); /* escape slashes */
4813 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004814 }
4815 ga_append(&ga, NUL);
4816
4817 if (type == FRD_REPLACE)
4818 {
4819 /* Do the replacement when the text at the cursor matches. Thus no
4820 * replacement is done if the cursor was moved! */
4821 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
4822 regmatch.rm_ic = 0;
4823 if (regmatch.regprog != NULL)
4824 {
4825 p = ml_get_cursor();
4826 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
4827 && regmatch.startp[0] == p)
4828 {
4829 /* Clear the command line to remove any old "No match"
4830 * error. */
4831 msg_end_prompt();
4832
4833 if (u_save_cursor() == OK)
4834 {
4835 /* A button was pressed thus undo should be synced. */
4836 if (no_u_sync == 0)
4837 u_sync();
4838
4839 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00004840 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004841 ins_str(repl_text);
4842 }
4843 }
4844 else
4845 MSG(_("No match at cursor, finding next"));
4846 vim_free(regmatch.regprog);
4847 }
4848 }
4849
4850 if (type == FRD_REPLACEALL)
4851 {
4852 /* A button was pressed, thus undo should be synced. */
4853 if (no_u_sync == 0)
4854 u_sync();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 do_cmdline_cmd(ga.ga_data);
4856 }
4857 else
4858 {
4859 /* Search for the next match. */
4860 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
4862 SEARCH_MSG + SEARCH_MARK);
4863 msg_scroll = i; /* don't let an error message set msg_scroll */
4864 }
4865
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004866 /* Don't want to pass did_emsg to other code, it may cause disabling
4867 * syntax HL if we were busy redrawing. */
4868 did_emsg = save_did_emsg;
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (State & (NORMAL | INSERT))
4871 {
4872 gui_update_screen(); /* update the screen */
4873 msg_didout = 0; /* overwrite any message */
4874 need_wait_return = FALSE; /* don't wait for return */
4875 }
4876
4877 vim_free(ga.ga_data);
4878 return (ga.ga_len > 0);
4879}
4880
4881#endif
4882
4883#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
4884 || defined(FEAT_GUI_MSWIN) \
4885 || defined(FEAT_GUI_MAC) \
4886 || defined(PROTO)
4887
4888#ifdef FEAT_WINDOWS
4889static void gui_wingoto_xy __ARGS((int x, int y));
4890
4891/*
4892 * Jump to the window at specified point (x, y).
4893 */
4894 static void
4895gui_wingoto_xy(x, y)
4896 int x;
4897 int y;
4898{
4899 int row = Y_2_ROW(y);
4900 int col = X_2_COL(x);
4901 win_T *wp;
4902
4903 if (row >= 0 && col >= 0)
4904 {
4905 wp = mouse_find_win(&row, &col);
4906 if (wp != NULL && wp != curwin)
4907 win_goto(wp);
4908 }
4909}
4910#endif
4911
4912/*
4913 * Process file drop. Mouse cursor position, key modifiers, name of files
4914 * and count of files are given. Argument "fnames[count]" has full pathnames
4915 * of dropped files, they will be freed in this function, and caller can't use
4916 * fnames after call this function.
4917 */
4918/*ARGSUSED*/
4919 void
4920gui_handle_drop(x, y, modifiers, fnames, count)
4921 int x;
4922 int y;
4923 int_u modifiers;
4924 char_u **fnames;
4925 int count;
4926{
4927 int i;
4928 char_u *p;
4929
4930 /*
4931 * When the cursor is at the command line, add the file names to the
4932 * command line, don't edit the files.
4933 */
4934 if (State & CMDLINE)
4935 {
4936 shorten_filenames(fnames, count);
4937 for (i = 0; i < count; ++i)
4938 {
4939 if (fnames[i] != NULL)
4940 {
4941 if (i > 0)
4942 add_to_input_buf((char_u*)" ", 1);
4943
4944 /* We don't know what command is used thus we can't be sure
4945 * about which characters need to be escaped. Only escape the
4946 * most common ones. */
4947# ifdef BACKSLASH_IN_FILENAME
4948 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
4949# else
4950 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
4951# endif
4952 if (p != NULL)
4953 add_to_input_buf(p, (int)STRLEN(p));
4954 vim_free(p);
4955 vim_free(fnames[i]);
4956 }
4957 }
4958 vim_free(fnames);
4959 }
4960 else
4961 {
4962 /* Go to the window under mouse cursor, then shorten given "fnames" by
4963 * current window, because a window can have local current dir. */
4964# ifdef FEAT_WINDOWS
4965 gui_wingoto_xy(x, y);
4966# endif
4967 shorten_filenames(fnames, count);
4968
4969 /* If Shift held down, remember the first item. */
4970 if ((modifiers & MOUSE_SHIFT) != 0)
4971 p = vim_strsave(fnames[0]);
4972 else
4973 p = NULL;
4974
4975 /* Handle the drop, :edit or :split to get to the file. This also
4976 * frees fnames[]. Skip this if there is only one item it's a
4977 * directory and Shift is held down. */
4978 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
4979 && mch_isdir(fnames[0]))
4980 {
4981 vim_free(fnames[0]);
4982 vim_free(fnames);
4983 }
4984 else
4985 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
4986
4987 /* If Shift held down, change to first file's directory. If the first
4988 * item is a directory, change to that directory (and let the explorer
4989 * plugin show the contents). */
4990 if (p != NULL)
4991 {
4992 if (mch_isdir(p))
4993 {
4994 if (mch_chdir((char *)p) == 0)
4995 shorten_fnames(TRUE);
4996 }
4997 else if (vim_chdirfile(p) == OK)
4998 shorten_fnames(TRUE);
4999 vim_free(p);
5000 }
5001
5002 /* Update the screen display */
5003 update_screen(NOT_VALID);
5004# ifdef FEAT_MENU
5005 gui_update_menus(0);
5006# endif
5007 setcursor();
5008 out_flush();
5009 gui_update_cursor(FALSE, FALSE);
5010 gui_mch_flush();
5011 }
5012}
5013#endif