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