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