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