blob: 9b97ffd2fca76d49c6b85789ca618dd3ff607d2d [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
3202 /* Update the tab line, it may appear or disappear. */
3203 using_tabline = gui_has_tabline();
3204 if (prev_has_tabline != using_tabline)
3205 {
3206 prev_has_tabline = using_tabline;
3207 gui_update_tabline();
3208 need_set_size = TRUE;
3209 if (using_tabline)
3210 fix_size = TRUE;
3211 if (!gui_use_tabline())
3212 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3213 }
3214#endif
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 for (i = 0; i < 3; i++)
3217 {
3218 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3219 {
3220 if (i == SBAR_BOTTOM)
3221 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3222 gui.which_scrollbars[i]);
3223 else
3224 {
3225 FOR_ALL_WINDOWS(wp)
3226 {
3227 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3228 }
3229 }
3230 need_set_size = TRUE;
3231 if (gui.which_scrollbars[i])
3232 fix_size = TRUE;
3233 }
3234 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3235 }
3236
3237#ifdef FEAT_MENU
3238 if (gui.menu_is_active != prev_menu_is_active)
3239 {
3240 /* We don't want a resize event change "Rows" here, save and
3241 * restore it. Resizing is handled below. */
3242 i = Rows;
3243 gui_mch_enable_menu(gui.menu_is_active);
3244 Rows = i;
3245 prev_menu_is_active = gui.menu_is_active;
3246 need_set_size = TRUE;
3247 if (gui.menu_is_active)
3248 fix_size = TRUE;
3249 }
3250#endif
3251
3252#ifdef FEAT_TOOLBAR
3253 if (using_toolbar != prev_toolbar)
3254 {
3255 gui_mch_show_toolbar(using_toolbar);
3256 prev_toolbar = using_toolbar;
3257 need_set_size = TRUE;
3258 if (using_toolbar)
3259 fix_size = TRUE;
3260 }
3261#endif
3262#ifdef FEAT_FOOTER
3263 if (using_footer != prev_footer)
3264 {
3265 gui_mch_enable_footer(using_footer);
3266 prev_footer = using_footer;
3267 need_set_size = TRUE;
3268 if (using_footer)
3269 fix_size = TRUE;
3270 }
3271#endif
3272#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3273 if (using_tearoff != prev_tearoff)
3274 {
3275 gui_mch_toggle_tearoffs(using_tearoff);
3276 prev_tearoff = using_tearoff;
3277 }
3278#endif
3279 if (need_set_size)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003280 {
3281#ifdef FEAT_GUI_GTK
3282 long r = Rows;
3283 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".
3298 * Don't do this while starting up though. */
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003299 if (!gui.starting)
3300 (void)char_avail();
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003301 Rows = r;
3302 Columns = c;
3303#endif
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306}
3307
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003308#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3309/*
3310 * Return TRUE if the GUI is taking care of the tabline.
3311 * It may still be hidden if 'showtabline' is zero.
3312 */
3313 int
3314gui_use_tabline()
3315{
3316 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3317}
3318
3319/*
3320 * Return TRUE if the GUI is showing the tabline.
3321 * This uses 'showtabline'.
3322 */
3323 static int
3324gui_has_tabline()
3325{
3326 if (!gui_use_tabline()
3327 || p_stal == 0
3328 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3329 return FALSE;
3330 return TRUE;
3331}
3332
3333/*
3334 * Update the tabline.
3335 * This may display/undisplay the tabline and update the labels.
3336 */
3337 void
3338gui_update_tabline()
3339{
3340 int showit = gui_has_tabline();
3341
3342 if (!gui.starting && starting == 0)
3343 {
3344 gui_mch_show_tabline(showit);
3345 if (showit != 0)
3346 gui_mch_update_tabline();
3347 }
3348}
3349
3350/*
3351 * Get the label for tab page "tp" into NameBuff[].
3352 */
3353 void
3354get_tabline_label(tp)
3355 tabpage_T *tp;
3356{
3357 int modified = FALSE;
3358 char_u buf[40];
3359 int wincount;
3360 win_T *wp;
3361
3362 /* Get the buffer name into NameBuff[] */
3363 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
3364
3365 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3366 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3367 if (bufIsChanged(wp->w_buffer))
3368 modified = TRUE;
3369 if (modified || wincount > 1)
3370 {
3371 if (wincount > 1)
3372 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3373 else
3374 buf[0] = NUL;
3375 if (modified)
3376 STRCAT(buf, "+");
3377 STRCAT(buf, " ");
3378 mch_memmove(NameBuff + STRLEN(buf), NameBuff, STRLEN(NameBuff) + 1);
3379 mch_memmove(NameBuff, buf, STRLEN(buf));
3380 }
3381}
3382
3383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385/*
3386 * Scrollbar stuff:
3387 */
3388
Bram Moolenaare45828b2006-02-15 22:12:56 +00003389#if defined(FEAT_WINDOWS) || defined(PROTO)
3390/*
3391 * Remove all scrollbars. Used before switching to another tab page.
3392 */
3393 void
3394gui_remove_scrollbars()
3395{
3396 int i;
3397 win_T *wp;
3398
3399 for (i = 0; i < 3; i++)
3400 {
3401 if (i == SBAR_BOTTOM)
3402 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3403 else
3404 {
3405 FOR_ALL_WINDOWS(wp)
3406 {
3407 gui_do_scrollbar(wp, i, FALSE);
3408 }
3409 }
3410 prev_which_scrollbars[i] = -1;
3411 }
3412}
3413#endif
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 void
3416gui_create_scrollbar(sb, type, wp)
3417 scrollbar_T *sb;
3418 int type;
3419 win_T *wp;
3420{
3421 static int sbar_ident = 0;
3422
3423 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3424 sb->wp = wp;
3425 sb->type = type;
3426 sb->value = 0;
3427#ifdef FEAT_GUI_ATHENA
3428 sb->pixval = 0;
3429#endif
3430 sb->size = 1;
3431 sb->max = 1;
3432 sb->top = 0;
3433 sb->height = 0;
3434#ifdef FEAT_VERTSPLIT
3435 sb->width = 0;
3436#endif
3437 sb->status_height = 0;
3438 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3439}
3440
3441/*
3442 * Find the scrollbar with the given index.
3443 */
3444 scrollbar_T *
3445gui_find_scrollbar(ident)
3446 long ident;
3447{
3448 win_T *wp;
3449
3450 if (gui.bottom_sbar.ident == ident)
3451 return &gui.bottom_sbar;
3452 FOR_ALL_WINDOWS(wp)
3453 {
3454 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3455 return &wp->w_scrollbars[SBAR_LEFT];
3456 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3457 return &wp->w_scrollbars[SBAR_RIGHT];
3458 }
3459 return NULL;
3460}
3461
3462/*
3463 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3464 *
3465 * For Win32, Macintosh and GTK+ 2:
3466 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3467 * you stop dragging the scrollbar. We get here each time the scrollbar is
3468 * dragged another pixel, but as far as the rest of vim goes, it thinks
3469 * we're just hanging in the call to DispatchMessage() in
3470 * process_message(). The DispatchMessage() call that hangs was passed a
3471 * mouse button click event in the scrollbar window. -- webb.
3472 *
3473 * Solution: Do the scrolling right here. But only when allowed.
3474 * Ignore the scrollbars while executing an external command or when there
3475 * are still characters to be processed.
3476 */
3477 void
3478gui_drag_scrollbar(sb, value, still_dragging)
3479 scrollbar_T *sb;
3480 long value;
3481 int still_dragging;
3482{
3483#ifdef FEAT_WINDOWS
3484 win_T *wp;
3485#endif
3486 int sb_num;
3487#ifdef USE_ON_FLY_SCROLL
3488 colnr_T old_leftcol = curwin->w_leftcol;
3489# ifdef FEAT_SCROLLBIND
3490 linenr_T old_topline = curwin->w_topline;
3491# endif
3492# ifdef FEAT_DIFF
3493 int old_topfill = curwin->w_topfill;
3494# endif
3495#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003496 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 int byte_count;
3498#endif
3499
3500 if (sb == NULL)
3501 return;
3502
3503 /* Don't put events in the input queue now. */
3504 if (hold_gui_events)
3505 return;
3506
3507#ifdef FEAT_CMDWIN
3508 if (cmdwin_type != 0 && sb->wp != curwin)
3509 return;
3510#endif
3511
3512 if (still_dragging)
3513 {
3514 if (sb->wp == NULL)
3515 gui.dragged_sb = SBAR_BOTTOM;
3516 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3517 gui.dragged_sb = SBAR_LEFT;
3518 else
3519 gui.dragged_sb = SBAR_RIGHT;
3520 gui.dragged_wp = sb->wp;
3521 }
3522 else
3523 {
3524 gui.dragged_sb = SBAR_NONE;
3525#ifdef HAVE_GTK2
3526 /* Keep the "dragged_wp" value until after the scrolling, for when the
3527 * moust button is released. GTK2 doesn't send the button-up event. */
3528 gui.dragged_wp = NULL;
3529#endif
3530 }
3531
3532 /* Vertical sbar info is kept in the first sbar (the left one) */
3533 if (sb->wp != NULL)
3534 sb = &sb->wp->w_scrollbars[0];
3535
3536 /*
3537 * Check validity of value
3538 */
3539 if (value < 0)
3540 value = 0;
3541#ifdef SCROLL_PAST_END
3542 else if (value > sb->max)
3543 value = sb->max;
3544#else
3545 if (value > sb->max - sb->size + 1)
3546 value = sb->max - sb->size + 1;
3547#endif
3548
3549 sb->value = value;
3550
3551#ifdef USE_ON_FLY_SCROLL
3552 /* When not allowed to do the scrolling right now, return. */
3553 if (dont_scroll || input_available())
3554 return;
3555#endif
3556
3557#ifdef FEAT_RIGHTLEFT
3558 if (sb->wp == NULL && curwin->w_p_rl)
3559 {
3560 value = sb->max + 1 - sb->size - value;
3561 if (value < 0)
3562 value = 0;
3563 }
3564#endif
3565
3566 if (sb->wp != NULL) /* vertical scrollbar */
3567 {
3568 sb_num = 0;
3569#ifdef FEAT_WINDOWS
3570 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3571 sb_num++;
3572 if (wp == NULL)
3573 return;
3574#else
3575 if (sb->wp != curwin)
3576 return;
3577#endif
3578
3579#ifdef USE_ON_FLY_SCROLL
3580 current_scrollbar = sb_num;
3581 scrollbar_value = value;
3582 if (State & NORMAL)
3583 {
3584 gui_do_scroll();
3585 setcursor();
3586 }
3587 else if (State & INSERT)
3588 {
3589 ins_scroll();
3590 setcursor();
3591 }
3592 else if (State & CMDLINE)
3593 {
3594 if (msg_scrolled == 0)
3595 {
3596 gui_do_scroll();
3597 redrawcmdline();
3598 }
3599 }
3600# ifdef FEAT_FOLDING
3601 /* Value may have been changed for closed fold. */
3602 sb->value = sb->wp->w_topline - 1;
3603# endif
3604#else
3605 bytes[0] = CSI;
3606 bytes[1] = KS_VER_SCROLLBAR;
3607 bytes[2] = KE_FILLER;
3608 bytes[3] = (char_u)sb_num;
3609 byte_count = 4;
3610#endif
3611 }
3612 else
3613 {
3614#ifdef USE_ON_FLY_SCROLL
3615 scrollbar_value = value;
3616
3617 if (State & NORMAL)
3618 gui_do_horiz_scroll();
3619 else if (State & INSERT)
3620 ins_horscroll();
3621 else if (State & CMDLINE)
3622 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003623 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 gui_do_horiz_scroll();
3626 redrawcmdline();
3627 }
3628 }
3629 if (old_leftcol != curwin->w_leftcol)
3630 {
3631 updateWindow(curwin); /* update window, status and cmdline */
3632 setcursor();
3633 }
3634#else
3635 bytes[0] = CSI;
3636 bytes[1] = KS_HOR_SCROLLBAR;
3637 bytes[2] = KE_FILLER;
3638 byte_count = 3;
3639#endif
3640 }
3641
3642#ifdef USE_ON_FLY_SCROLL
3643# ifdef FEAT_SCROLLBIND
3644 /*
3645 * synchronize other windows, as necessary according to 'scrollbind'
3646 */
3647 if (curwin->w_p_scb
3648 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3649 || (sb->wp == curwin && (curwin->w_topline != old_topline
3650# ifdef FEAT_DIFF
3651 || curwin->w_topfill != old_topfill
3652# endif
3653 ))))
3654 {
3655 do_check_scrollbind(TRUE);
3656 /* need to update the window right here */
3657 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3658 if (wp->w_redr_type > 0)
3659 updateWindow(wp);
3660 setcursor();
3661 }
3662# endif
3663 out_flush();
3664 gui_update_cursor(FALSE, TRUE);
3665#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003666 add_to_input_buf(bytes, byte_count);
3667 add_long_to_buf((long_u)value, bytes);
3668 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669#endif
3670}
3671
3672/*
3673 * Scrollbar stuff:
3674 */
3675
3676 void
3677gui_update_scrollbars(force)
3678 int force; /* Force all scrollbars to get updated */
3679{
3680 win_T *wp;
3681 scrollbar_T *sb;
3682 long val, size, max; /* need 32 bits here */
3683 int which_sb;
3684 int h, y;
3685#ifdef FEAT_VERTSPLIT
3686 static win_T *prev_curwin = NULL;
3687#endif
3688
3689 /* Update the horizontal scrollbar */
3690 gui_update_horiz_scrollbar(force);
3691
3692#ifndef WIN3264
3693 /* Return straight away if there is neither a left nor right scrollbar.
3694 * On MS-Windows this is required anyway for scrollwheel messages. */
3695 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3696 return;
3697#endif
3698
3699 /*
3700 * Don't want to update a scrollbar while we're dragging it. But if we
3701 * have both a left and right scrollbar, and we drag one of them, we still
3702 * need to update the other one.
3703 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003704 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3705 && gui.which_scrollbars[SBAR_LEFT]
3706 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
3708 /*
3709 * If we have two scrollbars and one of them is being dragged, just
3710 * copy the scrollbar position from the dragged one to the other one.
3711 */
3712 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3713 if (gui.dragged_wp != NULL)
3714 gui_mch_set_scrollbar_thumb(
3715 &gui.dragged_wp->w_scrollbars[which_sb],
3716 gui.dragged_wp->w_scrollbars[0].value,
3717 gui.dragged_wp->w_scrollbars[0].size,
3718 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720
3721 /* avoid that moving components around generates events */
3722 ++hold_gui_events;
3723
3724 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3725 {
3726 if (wp->w_buffer == NULL) /* just in case */
3727 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003728 /* Skip a scrollbar that is being dragged. */
3729 if (!force && (gui.dragged_sb == SBAR_LEFT
3730 || gui.dragged_sb == SBAR_RIGHT)
3731 && gui.dragged_wp == wp)
3732 continue;
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#ifdef SCROLL_PAST_END
3735 max = wp->w_buffer->b_ml.ml_line_count - 1;
3736#else
3737 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3738#endif
3739 if (max < 0) /* empty buffer */
3740 max = 0;
3741 val = wp->w_topline - 1;
3742 size = wp->w_height;
3743#ifdef SCROLL_PAST_END
3744 if (val > max) /* just in case */
3745 val = max;
3746#else
3747 if (size > max + 1) /* just in case */
3748 size = max + 1;
3749 if (val > max - size + 1)
3750 val = max - size + 1;
3751#endif
3752 if (val < 0) /* minimal value is 0 */
3753 val = 0;
3754
3755 /*
3756 * Scrollbar at index 0 (the left one) contains all the information.
3757 * It would be the same info for left and right so we just store it for
3758 * one of them.
3759 */
3760 sb = &wp->w_scrollbars[0];
3761
3762 /*
3763 * Note: no check for valid w_botline. If it's not valid the
3764 * scrollbars will be updated later anyway.
3765 */
3766 if (size < 1 || wp->w_botline - 2 > max)
3767 {
3768 /*
3769 * This can happen during changing files. Just don't update the
3770 * scrollbar for now.
3771 */
3772 sb->height = 0; /* Force update next time */
3773 if (gui.which_scrollbars[SBAR_LEFT])
3774 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3775 if (gui.which_scrollbars[SBAR_RIGHT])
3776 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3777 continue;
3778 }
3779 if (force || sb->height != wp->w_height
3780#ifdef FEAT_WINDOWS
3781 || sb->top != wp->w_winrow
3782 || sb->status_height != wp->w_status_height
3783# ifdef FEAT_VERTSPLIT
3784 || sb->width != wp->w_width
3785 || prev_curwin != curwin
3786# endif
3787#endif
3788 )
3789 {
3790 /* Height, width or position of scrollbar has changed. For
3791 * vertical split: curwin changed. */
3792 sb->height = wp->w_height;
3793#ifdef FEAT_WINDOWS
3794 sb->top = wp->w_winrow;
3795 sb->status_height = wp->w_status_height;
3796# ifdef FEAT_VERTSPLIT
3797 sb->width = wp->w_width;
3798# endif
3799#endif
3800
3801 /* Calculate height and position in pixels */
3802 h = (sb->height + sb->status_height) * gui.char_height;
3803 y = sb->top * gui.char_height + gui.border_offset;
3804#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
3805 if (gui.menu_is_active)
3806 y += gui.menu_height;
3807#endif
3808
3809#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
3810 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
3811# ifdef FEAT_GUI_ATHENA
3812 y += gui.toolbar_height;
3813# else
3814# ifdef FEAT_GUI_MSWIN
3815 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
3816# endif
3817# endif
3818#endif
3819
3820#ifdef FEAT_WINDOWS
3821 if (wp->w_winrow == 0)
3822#endif
3823 {
3824 /* Height of top scrollbar includes width of top border */
3825 h += gui.border_offset;
3826 y -= gui.border_offset;
3827 }
3828 if (gui.which_scrollbars[SBAR_LEFT])
3829 {
3830 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
3831 gui.left_sbar_x, y,
3832 gui.scrollbar_width, h);
3833 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
3834 }
3835 if (gui.which_scrollbars[SBAR_RIGHT])
3836 {
3837 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
3838 gui.right_sbar_x, y,
3839 gui.scrollbar_width, h);
3840 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
3841 }
3842 }
3843
3844 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
3845 * checking if the thumb moved at least a pixel. Only do this for
3846 * Athena, most other GUIs require the update anyway to make the
3847 * arrows work. */
3848#ifdef FEAT_GUI_ATHENA
3849 if (max == 0)
3850 y = 0;
3851 else
3852 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
3853 if (force || sb->pixval != y || sb->size != size || sb->max != max)
3854#else
3855 if (force || sb->value != val || sb->size != size || sb->max != max)
3856#endif
3857 {
3858 /* Thumb of scrollbar has moved */
3859 sb->value = val;
3860#ifdef FEAT_GUI_ATHENA
3861 sb->pixval = y;
3862#endif
3863 sb->size = size;
3864 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003865 if (gui.which_scrollbars[SBAR_LEFT]
3866 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
3868 val, size, max);
3869 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003870 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
3872 val, size, max);
3873 }
3874 }
3875#ifdef FEAT_VERTSPLIT
3876 prev_curwin = curwin;
3877#endif
3878 --hold_gui_events;
3879}
3880
3881/*
3882 * Enable or disable a scrollbar.
3883 * Check for scrollbars for vertically split windows which are not enabled
3884 * sometimes.
3885 */
3886 static void
3887gui_do_scrollbar(wp, which, enable)
3888 win_T *wp;
3889 int which; /* SBAR_LEFT or SBAR_RIGHT */
3890 int enable; /* TRUE to enable scrollbar */
3891{
3892#ifdef FEAT_VERTSPLIT
3893 int midcol = curwin->w_wincol + curwin->w_width / 2;
3894 int has_midcol = (wp->w_wincol <= midcol
3895 && wp->w_wincol + wp->w_width >= midcol);
3896
3897 /* Only enable scrollbars that contain the middle column of the current
3898 * window. */
3899 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
3900 {
3901 /* Scrollbars only on one side. Don't enable scrollbars that don't
3902 * contain the middle column of the current window. */
3903 if (!has_midcol)
3904 enable = FALSE;
3905 }
3906 else
3907 {
3908 /* Scrollbars on both sides. Don't enable scrollbars that neither
3909 * contain the middle column of the current window nor are on the far
3910 * side. */
3911 if (midcol > Columns / 2)
3912 {
3913 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
3914 enable = FALSE;
3915 }
3916 else
3917 {
3918 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
3919 : !has_midcol)
3920 enable = FALSE;
3921 }
3922 }
3923#endif
3924 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
3925}
3926
3927/*
3928 * Scroll a window according to the values set in the globals current_scrollbar
3929 * and scrollbar_value. Return TRUE if the cursor in the current window moved
3930 * or FALSE otherwise.
3931 */
3932 int
3933gui_do_scroll()
3934{
3935 win_T *wp, *save_wp;
3936 int i;
3937 long nlines;
3938 pos_T old_cursor;
3939 linenr_T old_topline;
3940#ifdef FEAT_DIFF
3941 int old_topfill;
3942#endif
3943
3944 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
3945 if (wp == NULL)
3946 break;
3947 if (wp == NULL)
3948 /* Couldn't find window */
3949 return FALSE;
3950
3951 /*
3952 * Compute number of lines to scroll. If zero, nothing to do.
3953 */
3954 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
3955 if (nlines == 0)
3956 return FALSE;
3957
3958 save_wp = curwin;
3959 old_topline = wp->w_topline;
3960#ifdef FEAT_DIFF
3961 old_topfill = wp->w_topfill;
3962#endif
3963 old_cursor = wp->w_cursor;
3964 curwin = wp;
3965 curbuf = wp->w_buffer;
3966 if (nlines < 0)
3967 scrolldown(-nlines, gui.dragged_wp == NULL);
3968 else
3969 scrollup(nlines, gui.dragged_wp == NULL);
3970 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
3971 * the mouse-up event already, but we still want it to behave like when
3972 * dragging. But not the next click in an arrow. */
3973 if (gui.dragged_sb == SBAR_NONE)
3974 gui.dragged_wp = NULL;
3975
3976 if (old_topline != wp->w_topline
3977#ifdef FEAT_DIFF
3978 || old_topfill != wp->w_topfill
3979#endif
3980 )
3981 {
3982 if (p_so != 0)
3983 {
3984 cursor_correct(); /* fix window for 'so' */
3985 update_topline(); /* avoid up/down jump */
3986 }
3987 if (old_cursor.lnum != wp->w_cursor.lnum)
3988 coladvance(wp->w_curswant);
3989#ifdef FEAT_SCROLLBIND
3990 wp->w_scbind_pos = wp->w_topline;
3991#endif
3992 }
3993
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003994 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
3995 validate_cursor();
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 curwin = save_wp;
3998 curbuf = save_wp->w_buffer;
3999
4000 /*
4001 * Don't call updateWindow() when nothing has changed (it will overwrite
4002 * the status line!).
4003 */
4004 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004005 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#ifdef FEAT_DIFF
4007 || old_topfill != wp->w_topfill
4008#endif
4009 )
4010 {
4011 redraw_win_later(wp, VALID);
4012 updateWindow(wp); /* update window, status line, and cmdline */
4013 }
4014
4015 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4016}
4017
4018
4019/*
4020 * Horizontal scrollbar stuff:
4021 */
4022
4023/*
4024 * Return length of line "lnum" for horizontal scrolling.
4025 */
4026 static colnr_T
4027scroll_line_len(lnum)
4028 linenr_T lnum;
4029{
4030 char_u *p;
4031 colnr_T col;
4032 int w;
4033
4034 p = ml_get(lnum);
4035 col = 0;
4036 if (*p != NUL)
4037 for (;;)
4038 {
4039 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004040 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (*p == NUL) /* don't count the last character */
4042 break;
4043 col += w;
4044 }
4045 return col;
4046}
4047
4048/* Remember which line is currently the longest, so that we don't have to
4049 * search for it when scrolling horizontally. */
4050static linenr_T longest_lnum = 0;
4051
4052 static void
4053gui_update_horiz_scrollbar(force)
4054 int force;
4055{
4056 long value, size, max; /* need 32 bit ints here */
4057
4058 if (!gui.which_scrollbars[SBAR_BOTTOM])
4059 return;
4060
4061 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4062 return;
4063
4064 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4065 return;
4066
4067 /*
4068 * It is possible for the cursor to be invalid if we're in the middle of
4069 * something (like changing files). If so, don't do anything for now.
4070 */
4071 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4072 {
4073 gui.bottom_sbar.value = -1;
4074 return;
4075 }
4076
4077 size = W_WIDTH(curwin);
4078 if (curwin->w_p_wrap)
4079 {
4080 value = 0;
4081#ifdef SCROLL_PAST_END
4082 max = 0;
4083#else
4084 max = W_WIDTH(curwin) - 1;
4085#endif
4086 }
4087 else
4088 {
4089 value = curwin->w_leftcol;
4090
4091 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4092 * line numbers, topline and botline can be invalid when displaying is
4093 * postponed. */
4094 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4095 && curwin->w_topline <= curwin->w_cursor.lnum
4096 && curwin->w_botline > curwin->w_cursor.lnum
4097 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4098 {
4099 linenr_T lnum;
4100 colnr_T n;
4101
4102 /* Use maximum of all visible lines. Remember the lnum of the
4103 * longest line, clostest to the cursor line. Used when scrolling
4104 * below. */
4105 max = 0;
4106 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4107 {
4108 n = scroll_line_len(lnum);
4109 if (n > (colnr_T)max)
4110 {
4111 max = n;
4112 longest_lnum = lnum;
4113 }
4114 else if (n == (colnr_T)max
4115 && abs((int)(lnum - curwin->w_cursor.lnum))
4116 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4117 longest_lnum = lnum;
4118 }
4119 }
4120 else
4121 /* Use cursor line only. */
4122 max = scroll_line_len(curwin->w_cursor.lnum);
4123#ifdef FEAT_VIRTUALEDIT
4124 if (virtual_active())
4125 {
4126 /* May move the cursor even further to the right. */
4127 if (curwin->w_virtcol >= (colnr_T)max)
4128 max = curwin->w_virtcol;
4129 }
4130#endif
4131
4132#ifndef SCROLL_PAST_END
4133 max += W_WIDTH(curwin) - 1;
4134#endif
4135 /* The line number isn't scrolled, thus there is less space when
4136 * 'number' is set (also for 'foldcolumn'). */
4137 size -= curwin_col_off();
4138#ifndef SCROLL_PAST_END
4139 max -= curwin_col_off();
4140#endif
4141 }
4142
4143#ifndef SCROLL_PAST_END
4144 if (value > max - size + 1)
4145 value = max - size + 1; /* limit the value to allowable range */
4146#endif
4147
4148#ifdef FEAT_RIGHTLEFT
4149 if (curwin->w_p_rl)
4150 {
4151 value = max + 1 - size - value;
4152 if (value < 0)
4153 {
4154 size += value;
4155 value = 0;
4156 }
4157 }
4158#endif
4159 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4160 && max == gui.bottom_sbar.max)
4161 return;
4162
4163 gui.bottom_sbar.value = value;
4164 gui.bottom_sbar.size = size;
4165 gui.bottom_sbar.max = max;
4166 gui.prev_wrap = curwin->w_p_wrap;
4167
4168 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4169}
4170
4171/*
4172 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4173 */
4174 int
4175gui_do_horiz_scroll()
4176{
4177 /* no wrapping, no scrolling */
4178 if (curwin->w_p_wrap)
4179 return FALSE;
4180
4181 if (curwin->w_leftcol == scrollbar_value)
4182 return FALSE;
4183
4184 curwin->w_leftcol = scrollbar_value;
4185
4186 /* When the line of the cursor is too short, move the cursor to the
4187 * longest visible line. Do a sanity check on "longest_lnum", just in
4188 * case. */
4189 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4190 && longest_lnum >= curwin->w_topline
4191 && longest_lnum < curwin->w_botline
4192 && !virtual_active())
4193 {
4194 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4195 {
4196 curwin->w_cursor.lnum = longest_lnum;
4197 curwin->w_cursor.col = 0;
4198 }
4199 }
4200
4201 return leftcol_changed();
4202}
4203
4204/*
4205 * Check that none of the colors are the same as the background color
4206 */
4207 void
4208gui_check_colors()
4209{
4210 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4211 {
4212 gui_set_bg_color((char_u *)"White");
4213 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4214 gui_set_fg_color((char_u *)"Black");
4215 }
4216}
4217
Bram Moolenaar3918c952005-03-15 22:34:55 +00004218 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219gui_set_fg_color(name)
4220 char_u *name;
4221{
4222 gui.norm_pixel = gui_get_color(name);
4223 hl_set_fg_color_name(vim_strsave(name));
4224}
4225
Bram Moolenaar3918c952005-03-15 22:34:55 +00004226 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227gui_set_bg_color(name)
4228 char_u *name;
4229{
4230 gui.back_pixel = gui_get_color(name);
4231 hl_set_bg_color_name(vim_strsave(name));
4232}
4233
4234/*
4235 * Allocate a color by name.
4236 * Returns INVALCOLOR and gives an error message when failed.
4237 */
4238 guicolor_T
4239gui_get_color(name)
4240 char_u *name;
4241{
4242 guicolor_T t;
4243
4244 if (*name == NUL)
4245 return INVALCOLOR;
4246 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004249#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 && gui.in_use
4251#endif
4252 )
4253 EMSG2(_("E254: Cannot allocate color %s"), name);
4254 return t;
4255}
4256
4257/*
4258 * Return the grey value of a color (range 0-255).
4259 */
4260 int
4261gui_get_lightness(pixel)
4262 guicolor_T pixel;
4263{
4264 long_u rgb = gui_mch_get_rgb(pixel);
4265
4266 return ( (((rgb >> 16) & 0xff) * 299)
4267 + (((rgb >> 8) & 0xff) * 587)
4268 + ((rgb & 0xff) * 114)) / 1000;
4269}
4270
4271#if defined(FEAT_GUI_X11) || defined(PROTO)
4272 void
4273gui_new_scrollbar_colors()
4274{
4275 win_T *wp;
4276
4277 /* Nothing to do if GUI hasn't started yet. */
4278 if (!gui.in_use)
4279 return;
4280
4281 FOR_ALL_WINDOWS(wp)
4282 {
4283 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4284 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4285 }
4286 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4287}
4288#endif
4289
4290/*
4291 * Call this when focus has changed.
4292 */
4293 void
4294gui_focus_change(in_focus)
4295 int in_focus;
4296{
4297/*
4298 * Skip this code to avoid drawing the cursor when debugging and switching
4299 * between the debugger window and gvim.
4300 */
4301#if 1
4302 gui.in_focus = in_focus;
4303 out_flush(); /* make sure output has been written */
4304 gui_update_cursor(TRUE, FALSE);
4305
4306# ifdef FEAT_XIM
4307 xim_set_focus(in_focus);
4308# endif
4309
4310 ui_focus_change(in_focus);
4311#endif
4312}
4313
4314/*
4315 * Called when the mouse moved (but not when dragging).
4316 */
4317 void
4318gui_mouse_moved(x, y)
4319 int x;
4320 int y;
4321{
4322 win_T *wp;
4323 char_u st[6];
4324
4325#ifdef FEAT_MOUSESHAPE
4326 /* Get window pointer, and update mouse shape as well. */
4327 wp = xy2win(x, y);
4328#endif
4329
4330 /* Only handle this when 'mousefocus' set and ... */
4331 if (p_mousef
4332 && !hold_gui_events /* not holding events */
4333 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4334 && State != HITRETURN /* but not hit-return prompt */
4335 && msg_scrolled == 0 /* no scrolled message */
4336 && !need_mouse_correct /* not moving the pointer */
4337 && gui.in_focus) /* gvim in focus */
4338 {
4339 /* Don't move the mouse when it's left or right of the Vim window */
4340 if (x < 0 || x > Columns * gui.char_width)
4341 return;
4342#ifndef FEAT_MOUSESHAPE
4343 wp = xy2win(x, y);
4344#endif
4345 if (wp == curwin || wp == NULL)
4346 return; /* still in the same old window, or none at all */
4347
4348 /*
4349 * format a mouse click on status line input
4350 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004351 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4352 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 */
4354 if (finish_op)
4355 {
4356 /* abort the current operator first */
4357 st[0] = ESC;
4358 add_to_input_buf(st, 1);
4359 }
4360 st[0] = CSI;
4361 st[1] = KS_MOUSE;
4362 st[2] = KE_FILLER;
4363 st[3] = (char_u)MOUSE_LEFT;
4364 fill_mouse_coord(st + 4,
4365#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004366 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#else
4368 -1,
4369#endif
4370 wp->w_height + W_WINROW(wp));
4371
4372 add_to_input_buf(st, 8);
4373 st[3] = (char_u)MOUSE_RELEASE;
4374 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375#ifdef FEAT_GUI_GTK
4376 /* Need to wake up the main loop */
4377 if (gtk_main_level() > 0)
4378 gtk_main_quit();
4379#endif
4380 }
4381}
4382
4383/*
4384 * Called when mouse should be moved to window with focus.
4385 */
4386 void
4387gui_mouse_correct()
4388{
4389 int x, y;
4390 win_T *wp = NULL;
4391
4392 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004393
4394 if (!(gui.in_use && p_mousef))
4395 return;
4396
4397 gui_mch_getmouse(&x, &y);
4398 /* Don't move the mouse when it's left or right of the Vim window */
4399 if (x < 0 || x > Columns * gui.char_width)
4400 return;
4401 if (y >= 0)
4402 wp = xy2win(x, y);
4403 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004405 validate_cline_row();
4406 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4407 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410}
4411
4412/*
4413 * Find window where the mouse pointer "y" coordinate is in.
4414 */
4415/*ARGSUSED*/
4416 static win_T *
4417xy2win(x, y)
4418 int x;
4419 int y;
4420{
4421#ifdef FEAT_WINDOWS
4422 int row;
4423 int col;
4424 win_T *wp;
4425
4426 row = Y_2_ROW(y);
4427 col = X_2_COL(x);
4428 if (row < 0 || col < 0) /* before first window */
4429 return NULL;
4430 wp = mouse_find_win(&row, &col);
4431# ifdef FEAT_MOUSESHAPE
4432 if (State == HITRETURN || State == ASKMORE)
4433 {
4434 if (Y_2_ROW(y) >= msg_row)
4435 update_mouseshape(SHAPE_IDX_MOREL);
4436 else
4437 update_mouseshape(SHAPE_IDX_MORE);
4438 }
4439 else if (row > wp->w_height) /* below status line */
4440 update_mouseshape(SHAPE_IDX_CLINE);
4441# ifdef FEAT_VERTSPLIT
4442 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
4443 && (row != wp->w_height || !stl_connected(wp)))
4444 update_mouseshape(SHAPE_IDX_VSEP);
4445# endif
4446 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
4447 && row == wp->w_height)
4448 update_mouseshape(SHAPE_IDX_STATUS);
4449 else
4450 update_mouseshape(-2);
4451# endif
4452 return wp;
4453#else
4454 return firstwin;
4455#endif
4456}
4457
4458/*
4459 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4460 * File names may be given to redefine the args list.
4461 */
4462 void
4463ex_gui(eap)
4464 exarg_T *eap;
4465{
4466 char_u *arg = eap->arg;
4467
4468 /*
4469 * Check for "-f" argument: foreground, don't fork.
4470 * Also don't fork when started with "gvim -f".
4471 * Do fork when using "gui -b".
4472 */
4473 if (arg[0] == '-'
4474 && (arg[1] == 'f' || arg[1] == 'b')
4475 && (arg[2] == NUL || vim_iswhite(arg[2])))
4476 {
4477 gui.dofork = (arg[1] == 'b');
4478 eap->arg = skipwhite(eap->arg + 2);
4479 }
4480 if (!gui.in_use)
4481 {
4482 /* Clear the command. Needed for when forking+exiting, to avoid part
4483 * of the argument ending up after the shell prompt. */
4484 msg_clr_eos_force();
4485 gui_start();
4486 }
4487 if (!ends_excmd(*eap->arg))
4488 ex_next(eap);
4489}
4490
4491#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004492 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493/*
4494 * This is shared between Athena, Motif and GTK.
4495 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004496static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498/*
4499 * Callback function for do_in_runtimepath().
4500 */
4501 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004502gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004506 char_u *gfp_buffer = cookie;
4507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (STRLEN(fname) >= MAXPATHL)
4509 *gfp_buffer = NUL;
4510 else
4511 STRCPY(gfp_buffer, fname);
4512}
4513
4514/*
4515 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4516 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4517 */
4518 int
4519gui_find_bitmap(name, buffer, ext)
4520 char_u *name;
4521 char_u *buffer;
4522 char *ext;
4523{
4524 if (STRLEN(name) > MAXPATHL - 14)
4525 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004526 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004527 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4528 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 return FAIL;
4530 return OK;
4531}
4532
4533# if !defined(HAVE_GTK2) || defined(PROTO)
4534/*
4535 * Given the name of the "icon=" argument, try finding the bitmap file for the
4536 * icon. If it is an absolute path name, use it as it is. Otherwise append
4537 * "ext" and search for it in 'runtimepath'.
4538 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4539 * contains "name".
4540 */
4541 void
4542gui_find_iconfile(name, buffer, ext)
4543 char_u *name;
4544 char_u *buffer;
4545 char *ext;
4546{
4547 char_u buf[MAXPATHL + 1];
4548
4549 expand_env(name, buffer, MAXPATHL);
4550 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4551 STRCPY(buffer, buf);
4552}
4553# endif
4554#endif
4555
Bram Moolenaar9372a112005-12-06 19:59:18 +00004556#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 void
4558display_errors()
4559{
4560 char_u *p;
4561
4562 if (isatty(2))
4563 fflush(stderr);
4564 else if (error_ga.ga_data != NULL)
4565 {
4566 /* avoid putting up a message box with blanks only */
4567 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4568 if (!isspace(*p))
4569 {
4570 /* Truncate a very long message, it will go off-screen. */
4571 if (STRLEN(p) > 2000)
4572 STRCPY(p + 2000 - 14, "...(truncated)");
4573 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4574 p, (char_u *)_("&Ok"), 1, NULL);
4575 break;
4576 }
4577 ga_clear(&error_ga);
4578 }
4579}
4580#endif
4581
4582#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4583/*
4584 * Return TRUE if still starting up and there is no place to enter text.
4585 * For GTK and X11 we check if stderr is not a tty, which means we were
4586 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4587 * allow typing on stdin.
4588 */
4589 int
4590no_console_input()
4591{
4592 return ((!gui.in_use || gui.starting)
4593# ifndef NO_CONSOLE
4594 && !isatty(0) && !isatty(2)
4595# endif
4596 );
4597}
4598#endif
4599
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004600#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
4601 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602/*
4603 * Update the current window and the screen.
4604 */
4605 void
4606gui_update_screen()
4607{
4608 update_topline();
4609 validate_cursor();
4610 update_screen(0); /* may need to update the screen */
4611 setcursor();
4612 out_flush(); /* make sure output has been written */
4613 gui_update_cursor(TRUE, FALSE);
4614 gui_mch_flush();
4615}
4616#endif
4617
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004618#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4620
4621/*
4622 * Get the text to use in a find/replace dialog. Uses the last search pattern
4623 * if the argument is empty.
4624 * Returns an allocated string.
4625 */
4626 char_u *
4627get_find_dialog_text(arg, wwordp, mcasep)
4628 char_u *arg;
4629 int *wwordp; /* return: TRUE if \< \> found */
4630 int *mcasep; /* return: TRUE if \C found */
4631{
4632 char_u *text;
4633
4634 if (*arg == NUL)
4635 text = last_search_pat();
4636 else
4637 text = arg;
4638 if (text != NULL)
4639 {
4640 text = vim_strsave(text);
4641 if (text != NULL)
4642 {
4643 int len = STRLEN(text);
4644 int i;
4645
4646 /* Remove "\V" */
4647 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4648 {
4649 mch_memmove(text, text + 2, (size_t)(len - 1));
4650 len -= 2;
4651 }
4652
4653 /* Recognize "\c" and "\C" and remove. */
4654 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4655 {
4656 *mcasep = (text[1] == 'C');
4657 mch_memmove(text, text + 2, (size_t)(len - 1));
4658 len -= 2;
4659 }
4660
4661 /* Recognize "\<text\>" and remove. */
4662 if (len >= 4
4663 && STRNCMP(text, "\\<", 2) == 0
4664 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4665 {
4666 *wwordp = TRUE;
4667 mch_memmove(text, text + 2, (size_t)(len - 4));
4668 text[len - 4] = NUL;
4669 }
4670
4671 /* Recognize "\/" or "\?" and remove. */
4672 for (i = 0; i + 1 < len; ++i)
4673 if (text[i] == '\\' && (text[i + 1] == '/'
4674 || text[i + 1] == '?'))
4675 {
4676 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4677 --len;
4678 }
4679 }
4680 }
4681 return text;
4682}
4683
4684/*
4685 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4686 */
4687 static void
4688concat_esc(gap, text, what)
4689 garray_T *gap;
4690 char_u *text;
4691 int what;
4692{
4693 while (*text != NUL)
4694 {
4695#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004696 int l = (*mb_ptr2len)(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 if (l > 1)
4698 {
4699 while (--l >= 0)
4700 ga_append(gap, *text++);
4701 continue;
4702 }
4703#endif
4704 if (*text == what)
4705 ga_append(gap, '\\');
4706 ga_append(gap, *text);
4707 ++text;
4708 }
4709}
4710
4711/*
4712 * Handle the press of a button in the find-replace dialog.
4713 * Return TRUE when something was added to the input buffer.
4714 */
4715 int
4716gui_do_findrepl(flags, find_text, repl_text, down)
4717 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4718 char_u *find_text;
4719 char_u *repl_text;
4720 int down; /* Search downwards. */
4721{
4722 garray_T ga;
4723 int i;
4724 int type = (flags & FRD_TYPE_MASK);
4725 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004726 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004727 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004730 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 ga_concat(&ga, (char_u *)"%s/");
4732
4733 ga_concat(&ga, (char_u *)"\\V");
4734 if (flags & FRD_MATCH_CASE)
4735 ga_concat(&ga, (char_u *)"\\C");
4736 else
4737 ga_concat(&ga, (char_u *)"\\c");
4738 if (flags & FRD_WHOLE_WORD)
4739 ga_concat(&ga, (char_u *)"\\<");
4740 if (type == FRD_REPLACEALL || down)
4741 concat_esc(&ga, find_text, '/'); /* escape slashes */
4742 else
4743 concat_esc(&ga, find_text, '?'); /* escape '?' */
4744 if (flags & FRD_WHOLE_WORD)
4745 ga_concat(&ga, (char_u *)"\\>");
4746
4747 if (type == FRD_REPLACEALL)
4748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 ga_concat(&ga, (char_u *)"/");
4750 concat_esc(&ga, repl_text, '/'); /* escape slashes */
4751 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004752 }
4753 ga_append(&ga, NUL);
4754
4755 if (type == FRD_REPLACE)
4756 {
4757 /* Do the replacement when the text at the cursor matches. Thus no
4758 * replacement is done if the cursor was moved! */
4759 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
4760 regmatch.rm_ic = 0;
4761 if (regmatch.regprog != NULL)
4762 {
4763 p = ml_get_cursor();
4764 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
4765 && regmatch.startp[0] == p)
4766 {
4767 /* Clear the command line to remove any old "No match"
4768 * error. */
4769 msg_end_prompt();
4770
4771 if (u_save_cursor() == OK)
4772 {
4773 /* A button was pressed thus undo should be synced. */
4774 if (no_u_sync == 0)
4775 u_sync();
4776
4777 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00004778 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004779 ins_str(repl_text);
4780 }
4781 }
4782 else
4783 MSG(_("No match at cursor, finding next"));
4784 vim_free(regmatch.regprog);
4785 }
4786 }
4787
4788 if (type == FRD_REPLACEALL)
4789 {
4790 /* A button was pressed, thus undo should be synced. */
4791 if (no_u_sync == 0)
4792 u_sync();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 do_cmdline_cmd(ga.ga_data);
4794 }
4795 else
4796 {
4797 /* Search for the next match. */
4798 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
4800 SEARCH_MSG + SEARCH_MARK);
4801 msg_scroll = i; /* don't let an error message set msg_scroll */
4802 }
4803
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004804 /* Don't want to pass did_emsg to other code, it may cause disabling
4805 * syntax HL if we were busy redrawing. */
4806 did_emsg = save_did_emsg;
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (State & (NORMAL | INSERT))
4809 {
4810 gui_update_screen(); /* update the screen */
4811 msg_didout = 0; /* overwrite any message */
4812 need_wait_return = FALSE; /* don't wait for return */
4813 }
4814
4815 vim_free(ga.ga_data);
4816 return (ga.ga_len > 0);
4817}
4818
4819#endif
4820
4821#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
4822 || defined(FEAT_GUI_MSWIN) \
4823 || defined(FEAT_GUI_MAC) \
4824 || defined(PROTO)
4825
4826#ifdef FEAT_WINDOWS
4827static void gui_wingoto_xy __ARGS((int x, int y));
4828
4829/*
4830 * Jump to the window at specified point (x, y).
4831 */
4832 static void
4833gui_wingoto_xy(x, y)
4834 int x;
4835 int y;
4836{
4837 int row = Y_2_ROW(y);
4838 int col = X_2_COL(x);
4839 win_T *wp;
4840
4841 if (row >= 0 && col >= 0)
4842 {
4843 wp = mouse_find_win(&row, &col);
4844 if (wp != NULL && wp != curwin)
4845 win_goto(wp);
4846 }
4847}
4848#endif
4849
4850/*
4851 * Process file drop. Mouse cursor position, key modifiers, name of files
4852 * and count of files are given. Argument "fnames[count]" has full pathnames
4853 * of dropped files, they will be freed in this function, and caller can't use
4854 * fnames after call this function.
4855 */
4856/*ARGSUSED*/
4857 void
4858gui_handle_drop(x, y, modifiers, fnames, count)
4859 int x;
4860 int y;
4861 int_u modifiers;
4862 char_u **fnames;
4863 int count;
4864{
4865 int i;
4866 char_u *p;
4867
4868 /*
4869 * When the cursor is at the command line, add the file names to the
4870 * command line, don't edit the files.
4871 */
4872 if (State & CMDLINE)
4873 {
4874 shorten_filenames(fnames, count);
4875 for (i = 0; i < count; ++i)
4876 {
4877 if (fnames[i] != NULL)
4878 {
4879 if (i > 0)
4880 add_to_input_buf((char_u*)" ", 1);
4881
4882 /* We don't know what command is used thus we can't be sure
4883 * about which characters need to be escaped. Only escape the
4884 * most common ones. */
4885# ifdef BACKSLASH_IN_FILENAME
4886 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
4887# else
4888 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
4889# endif
4890 if (p != NULL)
4891 add_to_input_buf(p, (int)STRLEN(p));
4892 vim_free(p);
4893 vim_free(fnames[i]);
4894 }
4895 }
4896 vim_free(fnames);
4897 }
4898 else
4899 {
4900 /* Go to the window under mouse cursor, then shorten given "fnames" by
4901 * current window, because a window can have local current dir. */
4902# ifdef FEAT_WINDOWS
4903 gui_wingoto_xy(x, y);
4904# endif
4905 shorten_filenames(fnames, count);
4906
4907 /* If Shift held down, remember the first item. */
4908 if ((modifiers & MOUSE_SHIFT) != 0)
4909 p = vim_strsave(fnames[0]);
4910 else
4911 p = NULL;
4912
4913 /* Handle the drop, :edit or :split to get to the file. This also
4914 * frees fnames[]. Skip this if there is only one item it's a
4915 * directory and Shift is held down. */
4916 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
4917 && mch_isdir(fnames[0]))
4918 {
4919 vim_free(fnames[0]);
4920 vim_free(fnames);
4921 }
4922 else
4923 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
4924
4925 /* If Shift held down, change to first file's directory. If the first
4926 * item is a directory, change to that directory (and let the explorer
4927 * plugin show the contents). */
4928 if (p != NULL)
4929 {
4930 if (mch_isdir(p))
4931 {
4932 if (mch_chdir((char *)p) == 0)
4933 shorten_fnames(TRUE);
4934 }
4935 else if (vim_chdirfile(p) == OK)
4936 shorten_fnames(TRUE);
4937 vim_free(p);
4938 }
4939
4940 /* Update the screen display */
4941 update_screen(NOT_VALID);
4942# ifdef FEAT_MENU
4943 gui_update_menus(0);
4944# endif
4945 setcursor();
4946 out_flush();
4947 gui_update_cursor(FALSE, FALSE);
4948 gui_mch_flush();
4949 }
4950}
4951#endif