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