blob: 68999926f6d382f859c28f5310c0f0f7539eb0db [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
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020016#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static 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));
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020023#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +000024static 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]);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000142 ignored = (int)read(pipefd[0], &dummy, (size_t)1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 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
Bram Moolenaar265e5072006-08-29 16:13:22 +0000190 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
191 * the GUIFailed event. */
192 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
193 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#endif
195
196 --recursive;
197}
198
199/*
200 * Call this when vim starts up, whether or not the GUI is started
201 */
202 void
203gui_prepare(argc, argv)
204 int *argc;
205 char **argv;
206{
207 gui.in_use = FALSE; /* No GUI yet (maybe later) */
208 gui.starting = FALSE; /* No GUI yet (maybe later) */
209 gui_mch_prepare(argc, argv);
210}
211
212/*
213 * Try initializing the GUI and check if it can be started.
214 * Used from main() to check early if "vim -g" can start the GUI.
215 * Used from gui_init() to prepare for starting the GUI.
216 * Returns FAIL or OK.
217 */
218 int
219gui_init_check()
220{
221 static int result = MAYBE;
222
223 if (result != MAYBE)
224 {
225 if (result == FAIL)
226 EMSG(_("E229: Cannot start the GUI"));
227 return result;
228 }
229
230 gui.shell_created = FALSE;
231 gui.dying = FALSE;
232 gui.in_focus = TRUE; /* so the guicursor setting works */
233 gui.dragged_sb = SBAR_NONE;
234 gui.dragged_wp = NULL;
235 gui.pointer_hidden = FALSE;
236 gui.col = 0;
237 gui.row = 0;
238 gui.num_cols = Columns;
239 gui.num_rows = Rows;
240
241 gui.cursor_is_valid = FALSE;
242 gui.scroll_region_top = 0;
243 gui.scroll_region_bot = Rows - 1;
244 gui.scroll_region_left = 0;
245 gui.scroll_region_right = Columns - 1;
246 gui.highlight_mask = HL_NORMAL;
247 gui.char_width = 1;
248 gui.char_height = 1;
249 gui.char_ascent = 0;
250 gui.border_width = 0;
251
252 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200253#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 gui.bold_font = NOFONT;
255 gui.ital_font = NOFONT;
256 gui.boldital_font = NOFONT;
257# ifdef FEAT_XFONTSET
258 gui.fontset = NOFONTSET;
259# endif
260#endif
261
262#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200263# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264# ifdef FONTSET_ALWAYS
265 gui.menu_fontset = NOFONTSET;
266# else
267 gui.menu_font = NOFONT;
268# endif
269# endif
270 gui.menu_is_active = TRUE; /* default: include menu */
271# ifndef FEAT_GUI_GTK
272 gui.menu_height = MENU_DEFAULT_HEIGHT;
273 gui.menu_width = 0;
274# endif
275#endif
276#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
277 gui.toolbar_height = 0;
278#endif
279#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
280 gui.footer_height = 0;
281#endif
282#ifdef FEAT_BEVAL_TIP
283 gui.tooltip_fontset = NOFONTSET;
284#endif
285
286 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
287 gui.prev_wrap = -1;
288
289#ifdef ALWAYS_USE_GUI
290 result = OK;
291#else
292 result = gui_mch_init_check();
293#endif
294 return result;
295}
296
297/*
298 * This is the call which starts the GUI.
299 */
300 void
301gui_init()
302{
303 win_T *wp;
304 static int recursive = 0;
305
306 /*
307 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
308 * function will then be executed at the first call, the rest by the
309 * recursive call. This allow the shell to be opened halfway reading a
310 * gvimrc file.
311 */
312 if (!recursive)
313 {
314 ++recursive;
315
316 clip_init(TRUE);
317
318 /* If can't initialize, don't try doing the rest */
319 if (gui_init_check() == FAIL)
320 {
321 --recursive;
322 clip_init(FALSE);
323 return;
324 }
325
326 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000327 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
328 * breaks the Paste toolbar button.
329 */
330 set_option_value((char_u *)"paste", 0L, NULL, 0);
331
332 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 * Set up system-wide default menus.
334 */
335#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
336 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
337 {
338 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000339 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 sys_menu = FALSE;
341 }
342#endif
343
344 /*
345 * Switch on the mouse by default, unless the user changed it already.
346 * This can then be changed in the .gvimrc.
347 */
348 if (!option_was_set((char_u *)"mouse"))
349 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000350 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 /*
353 * If -U option given, use only the initializations from that file and
354 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
355 */
356 if (use_gvimrc != NULL)
357 {
358 if (STRCMP(use_gvimrc, "NONE") != 0
359 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000360 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 EMSG2(_("E230: Cannot read from \"%s\""), use_gvimrc);
362 }
363 else
364 {
365 /*
366 * Get system wide defaults for gvim, only when file name defined.
367 */
368#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000369 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#endif
371
372 /*
373 * Try to read GUI initialization commands from the following
374 * places:
375 * - environment variable GVIMINIT
376 * - the user gvimrc file (~/.gvimrc)
377 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
378 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
379 * The first that exists is used, the rest is ignored.
380 */
381 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
383 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
386 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
388 )
389 {
390#ifdef USR_GVIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391 (void)do_source((char_u *)USR_GVIMRC_FILE3, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393 }
394
395 /*
396 * Read initialization commands from ".gvimrc" in current
397 * directory. This is only done if the 'exrc' option is set.
398 * Because of security reasons we disallow shell and write
399 * commands now, except for unix if the file is owned by the user
400 * or 'secure' option has been reset in environment of global
401 * ".gvimrc".
402 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
403 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
404 */
405 if (p_exrc)
406 {
407#ifdef UNIX
408 {
409 struct stat s;
410
411 /* if ".gvimrc" file is not owned by user, set 'secure'
412 * mode */
413 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
414 secure = p_secure;
415 }
416#else
417 secure = p_secure;
418#endif
419
420 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
421 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
422#ifdef SYS_GVIMRC_FILE
423 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
424 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
425#endif
426#ifdef USR_GVIMRC_FILE2
427 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
428 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
429#endif
430#ifdef USR_GVIMRC_FILE3
431 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
432 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
433#endif
434 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000435 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 if (secure == 2)
438 need_wait_return = TRUE;
439 secure = 0;
440 }
441 }
442
443 if (need_wait_return || msg_didany)
444 wait_return(TRUE);
445
446 --recursive;
447 }
448
449 /* If recursive call opened the shell, return here from the first call */
450 if (gui.in_use)
451 return;
452
453 /*
454 * Create the GUI shell.
455 */
456 gui.in_use = TRUE; /* Must be set after menus have been set up */
457 if (gui_mch_init() == FAIL)
458 goto error;
459
460 /* Avoid a delay for an error message that was printed in the terminal
461 * where Vim was started. */
462 emsg_on_display = FALSE;
463 msg_scrolled = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000464 clear_sb_text();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 need_wait_return = FALSE;
466 msg_didany = FALSE;
467
468 /*
469 * Check validity of any generic resources that may have been loaded.
470 */
471 if (gui.border_width < 0)
472 gui.border_width = 0;
473
474 /*
475 * Set up the fonts. First use a font specified with "-fn" or "-font".
476 */
477 if (font_argument != NULL)
478 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
479 if (
480#ifdef FEAT_XFONTSET
481 (*p_guifontset == NUL
482 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
483#endif
484 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
485 : p_guifont, FALSE) == FAIL)
486 {
487 EMSG(_("E665: Cannot start GUI, no valid font found"));
488 goto error2;
489 }
490#ifdef FEAT_MBYTE
491 if (gui_get_wide_font() == FAIL)
492 EMSG(_("E231: 'guifontwide' invalid"));
493#endif
494
495 gui.num_cols = Columns;
496 gui.num_rows = Rows;
497 gui_reset_scroll_region();
498
499 /* Create initial scrollbars */
500 FOR_ALL_WINDOWS(wp)
501 {
502 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
503 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
504 }
505 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
506
507#ifdef FEAT_MENU
508 gui_create_initial_menus(root_menu);
509#endif
510#ifdef FEAT_SUN_WORKSHOP
511 if (usingSunWorkShop)
512 workshop_init();
513#endif
514#ifdef FEAT_SIGN_ICONS
515 sign_gui_started();
516#endif
517
518 /* Configure the desired menu and scrollbars */
519 gui_init_which_components(NULL);
520
521 /* All components of the GUI have been created now */
522 gui.shell_created = TRUE;
523
524#ifndef FEAT_GUI_GTK
525 /* Set the shell size, adjusted for the screen size. For GTK this only
526 * works after the shell has been opened, thus it is further down. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000527 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528#endif
529#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
530 /* Need to set the size of the menubar after all the menus have been
531 * created. */
532 gui_mch_compute_menu_height((Widget)0);
533#endif
534
535 /*
536 * Actually open the GUI shell.
537 */
538 if (gui_mch_open() != FAIL)
539 {
540#ifdef FEAT_TITLE
541 maketitle();
542 resettitle();
543#endif
544 init_gui_options();
545#ifdef FEAT_ARABIC
546 /* Our GUI can't do bidi. */
547 p_tbidi = FALSE;
548#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000549#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 /* Give GTK+ a chance to put all widget's into place. */
551 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000552
553# ifdef FEAT_MENU
554 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
555 * It was still there to make F10 work. */
556 if (vim_strchr(p_go, GO_MENUS) == NULL)
557 {
558 --gui.starting;
559 gui_mch_enable_menu(FALSE);
560 ++gui.starting;
561 gui_mch_update();
562 }
563# endif
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 /* Now make sure the shell fits on the screen. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000566 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000568 /* When 'lines' was set while starting up the topframe may have to be
569 * resized. */
570 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000571
572#ifdef FEAT_BEVAL
573 /* Always create the Balloon Evaluation area, but disable it when
574 * 'ballooneval' is off */
575# ifdef FEAT_GUI_GTK
576 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
577 &general_beval_cb, NULL);
578# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000579# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000580 {
581 extern Widget textArea;
582 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000583 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000584 }
585# else
586# ifdef FEAT_GUI_W32
587 balloonEval = gui_mch_create_beval_area(NULL, NULL,
588 &general_beval_cb, NULL);
589# endif
590# endif
591# endif
592 if (!p_beval)
593 gui_mch_disable_beval_area(balloonEval);
594#endif
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
597 if (!im_xim_isvalid_imactivate())
598 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
599#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000600 /* When 'cmdheight' was set during startup it may not have taken
601 * effect yet. */
602 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000603 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
605 return;
606 }
607
608error2:
609#ifdef FEAT_GUI_X11
610 /* undo gui_mch_init() */
611 gui_mch_uninit();
612#endif
613
614error:
615 gui.in_use = FALSE;
616 clip_init(FALSE);
617}
618
619
620 void
621gui_exit(rc)
622 int rc;
623{
624#ifndef __BEOS__
625 /* don't free the fonts, it leads to a BUS error
626 * richard@whitequeen.com Jul 99 */
627 free_highlight_fonts();
628#endif
629 gui.in_use = FALSE;
630 gui_mch_exit(rc);
631}
632
Bram Moolenaar9372a112005-12-06 19:59:18 +0000633#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000635# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
637 * Called when the GUI shell is closed by the user. If there are no changed
638 * files Vim exits, otherwise there will be a dialog to ask the user what to
639 * do.
640 * When this function returns, Vim should NOT exit!
641 */
642 void
643gui_shell_closed()
644{
645 cmdmod_T save_cmdmod;
646
647 save_cmdmod = cmdmod;
648
649 /* Only exit when there are no changed files */
650 exiting = TRUE;
651# ifdef FEAT_BROWSE
652 cmdmod.browse = TRUE;
653# endif
654# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
655 cmdmod.confirm = TRUE;
656# endif
657 /* If there are changed buffers, present the user with a dialog if
658 * possible, otherwise give an error message. */
659 if (!check_changed_any(FALSE))
660 getout(0);
661
662 exiting = FALSE;
663 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000664 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665}
666#endif
667
668/*
669 * Set the font. "font_list" is a a comma separated list of font names. The
670 * first font name that works is used. If none is found, use the default
671 * font.
672 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
673 * Return OK when able to set the font. When it failed FAIL is returned and
674 * the fonts are unchanged.
675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int
677gui_init_font(font_list, fontset)
678 char_u *font_list;
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000679 int fontset UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
681#define FONTLEN 320
682 char_u font_name[FONTLEN];
683 int font_list_empty = FALSE;
684 int ret = FAIL;
685
686 if (!gui.in_use)
687 return FAIL;
688
689 font_name[0] = NUL;
690 if (*font_list == NUL)
691 font_list_empty = TRUE;
692 else
693 {
694#ifdef FEAT_XFONTSET
695 /* When using a fontset, the whole list of fonts is one name. */
696 if (fontset)
697 ret = gui_mch_init_font(font_list, TRUE);
698 else
699#endif
700 while (*font_list != NUL)
701 {
702 /* Isolate one comma separated font name. */
703 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
704
705 /* Careful!!! The Win32 version of gui_mch_init_font(), when
706 * called with "*" will change p_guifont to the selected font
707 * name, which frees the old value. This makes font_list
708 * invalid. Thus when OK is returned here, font_list must no
709 * longer be used! */
710 if (gui_mch_init_font(font_name, FALSE) == OK)
711 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200712#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 /* If it's a Unicode font, try setting 'guifontwide' to a
714 * similar double-width font. */
715 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
716 && strstr((char *)font_name, "10646") != NULL)
717 set_guifontwide(font_name);
718#endif
719 ret = OK;
720 break;
721 }
722 }
723 }
724
725 if (ret != OK
726 && STRCMP(font_list, "*") != 0
727 && (font_list_empty || gui.norm_font == NOFONT))
728 {
729 /*
730 * Couldn't load any font in 'font_list', keep the current font if
731 * there is one. If 'font_list' is empty, or if there is no current
732 * font, tell gui_mch_init_font() to try to find a font we can load.
733 */
734 ret = gui_mch_init_font(NULL, FALSE);
735 }
736
737 if (ret == OK)
738 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200739#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 /* Set normal font as current font */
741# ifdef FEAT_XFONTSET
742 if (gui.fontset != NOFONTSET)
743 gui_mch_set_fontset(gui.fontset);
744 else
745# endif
746 gui_mch_set_font(gui.norm_font);
747#endif
748 gui_set_shellsize(FALSE,
749#ifdef MSWIN
750 TRUE
751#else
752 FALSE
753#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000754 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756
757 return ret;
758}
759
760#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200761# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762/*
763 * Try setting 'guifontwide' to a font twice as wide as "name".
764 */
765 static void
766set_guifontwide(name)
767 char_u *name;
768{
769 int i = 0;
770 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
771 char_u *wp = NULL;
772 char_u *p;
773 GuiFont font;
774
775 wp = wide_name;
776 for (p = name; *p != NUL; ++p)
777 {
778 *wp++ = *p;
779 if (*p == '-')
780 {
781 ++i;
782 if (i == 6) /* font type: change "--" to "-*-" */
783 {
784 if (p[1] == '-')
785 *wp++ = '*';
786 }
787 else if (i == 12) /* found the width */
788 {
789 ++p;
790 i = getdigits(&p);
791 if (i != 0)
792 {
793 /* Double the width specification. */
794 sprintf((char *)wp, "%d%s", i * 2, p);
795 font = gui_mch_get_font(wide_name, FALSE);
796 if (font != NOFONT)
797 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000798 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 gui.wide_font = font;
800 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000801 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803 }
804 break;
805 }
806 }
807 }
808}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200809# endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
812 * Get the font for 'guifontwide'.
813 * Return FAIL for an invalid font name.
814 */
815 int
816gui_get_wide_font()
817{
818 GuiFont font = NOFONT;
819 char_u font_name[FONTLEN];
820 char_u *p;
821
822 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
823 return OK; /* Will give an error message later. */
824
825 if (p_guifontwide != NULL && *p_guifontwide != NUL)
826 {
827 for (p = p_guifontwide; *p != NUL; )
828 {
829 /* Isolate one comma separated font name. */
830 (void)copy_option_part(&p, font_name, FONTLEN, ",");
831 font = gui_mch_get_font(font_name, FALSE);
832 if (font != NOFONT)
833 break;
834 }
835 if (font == NOFONT)
836 return FAIL;
837 }
838
839 gui_mch_free_font(gui.wide_font);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200840#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
842 if (font != NOFONT && gui.norm_font != NOFONT
843 && pango_font_description_equal(font, gui.norm_font))
844 {
845 gui.wide_font = NOFONT;
846 gui_mch_free_font(font);
847 }
848 else
849#endif
850 gui.wide_font = font;
851 return OK;
852}
853#endif
854
855 void
856gui_set_cursor(row, col)
857 int row;
858 int col;
859{
860 gui.row = row;
861 gui.col = col;
862}
863
864/*
865 * gui_check_pos - check if the cursor is on the screen.
866 */
867 static void
868gui_check_pos()
869{
870 if (gui.row >= screen_Rows)
871 gui.row = screen_Rows - 1;
872 if (gui.col >= screen_Columns)
873 gui.col = screen_Columns - 1;
874 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
875 gui.cursor_is_valid = FALSE;
876}
877
878/*
879 * Redraw the cursor if necessary or when forced.
880 * Careful: The contents of ScreenLines[] must match what is on the screen,
881 * otherwise this goes wrong. May need to call out_flush() first.
882 */
883 void
884gui_update_cursor(force, clear_selection)
885 int force; /* when TRUE, update even when not moved */
886 int clear_selection;/* clear selection under cursor */
887{
888 int cur_width = 0;
889 int cur_height = 0;
890 int old_hl_mask;
891 int idx;
892 int id;
893 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
894 int cattr; /* cursor attributes */
895 int attr;
896 attrentry_T *aep = NULL;
897
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +0000898 /* Don't update the cursor when halfway busy scrolling or the screen size
899 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
900 if (!can_update_cursor || screen_Columns != gui.num_cols
901 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 return;
903
904 gui_check_pos();
905 if (!gui.cursor_is_valid || force
906 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
907 {
908 gui_undraw_cursor();
909 if (gui.row < 0)
910 return;
911#ifdef USE_IM_CONTROL
912 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
913 im_set_position(gui.row, gui.col);
914#endif
915 gui.cursor_row = gui.row;
916 gui.cursor_col = gui.col;
917
918 /* Only write to the screen after ScreenLines[] has been initialized */
919 if (!screen_cleared || ScreenLines == NULL)
920 return;
921
922 /* Clear the selection if we are about to write over it */
923 if (clear_selection)
924 clip_may_clear_selection(gui.row, gui.row);
925 /* Check that the cursor is inside the shell (resizing may have made
926 * it invalid) */
927 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
928 return;
929
930 gui.cursor_is_valid = TRUE;
931
932 /*
933 * How the cursor is drawn depends on the current mode.
934 */
935 idx = get_shape_idx(FALSE);
936 if (State & LANGMAP)
937 id = shape_table[idx].id_lm;
938 else
939 id = shape_table[idx].id;
940
941 /* get the colors and attributes for the cursor. Default is inverted */
942 cfg = INVALCOLOR;
943 cbg = INVALCOLOR;
944 cattr = HL_INVERSE;
945 gui_mch_set_blinking(shape_table[idx].blinkwait,
946 shape_table[idx].blinkon,
947 shape_table[idx].blinkoff);
948 if (id > 0)
949 {
950 cattr = syn_id2colors(id, &cfg, &cbg);
951#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
952 {
953 static int iid;
954 guicolor_T fg, bg;
955
Bram Moolenaarc236c162008-07-13 17:41:49 +0000956 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200957# if defined(FEAT_GUI_GTK) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000958 preedit_get_status()
959# else
960 im_get_status()
961# endif
962 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 {
964 iid = syn_name2id((char_u *)"CursorIM");
965 if (iid > 0)
966 {
967 syn_id2colors(iid, &fg, &bg);
968 if (bg != INVALCOLOR)
969 cbg = bg;
970 if (fg != INVALCOLOR)
971 cfg = fg;
972 }
973 }
974 }
975#endif
976 }
977
978 /*
979 * Get the attributes for the character under the cursor.
980 * When no cursor color was given, use the character color.
981 */
982 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
983 if (attr > HL_ALL)
984 aep = syn_gui_attr2entry(attr);
985 if (aep != NULL)
986 {
987 attr = aep->ae_attr;
988 if (cfg == INVALCOLOR)
989 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
990 : aep->ae_u.gui.fg_color);
991 if (cbg == INVALCOLOR)
992 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
993 : aep->ae_u.gui.bg_color);
994 }
995 if (cfg == INVALCOLOR)
996 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
997 if (cbg == INVALCOLOR)
998 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
999
1000#ifdef FEAT_XIM
1001 if (aep != NULL)
1002 {
1003 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1004 : aep->ae_u.gui.bg_color);
1005 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1006 : aep->ae_u.gui.fg_color);
1007 if (xim_bg_color == INVALCOLOR)
1008 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1009 : gui.back_pixel;
1010 if (xim_fg_color == INVALCOLOR)
1011 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1012 : gui.norm_pixel;
1013 }
1014 else
1015 {
1016 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1017 : gui.back_pixel;
1018 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1019 : gui.norm_pixel;
1020 }
1021#endif
1022
1023 attr &= ~HL_INVERSE;
1024 if (cattr & HL_INVERSE)
1025 {
1026 cc = cbg;
1027 cbg = cfg;
1028 cfg = cc;
1029 }
1030 cattr &= ~HL_INVERSE;
1031
1032 /*
1033 * When we don't have window focus, draw a hollow cursor.
1034 */
1035 if (!gui.in_focus)
1036 {
1037 gui_mch_draw_hollow_cursor(cbg);
1038 return;
1039 }
1040
1041 old_hl_mask = gui.highlight_mask;
1042 if (shape_table[idx].shape == SHAPE_BLOCK
1043#ifdef FEAT_HANGULIN
1044 || composing_hangul
1045#endif
1046 )
1047 {
1048 /*
1049 * Draw the text character with the cursor colors. Use the
1050 * character attributes plus the cursor attributes.
1051 */
1052 gui.highlight_mask = (cattr | attr);
1053#ifdef FEAT_HANGULIN
1054 if (composing_hangul)
1055 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1056 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1057 else
1058#endif
1059 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1060 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1061 }
1062 else
1063 {
1064#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1065 int col_off = FALSE;
1066#endif
1067 /*
1068 * First draw the partial cursor, then overwrite with the text
1069 * character, using a transparent background.
1070 */
1071 if (shape_table[idx].shape == SHAPE_VER)
1072 {
1073 cur_height = gui.char_height;
1074 cur_width = (gui.char_width * shape_table[idx].percentage
1075 + 99) / 100;
1076 }
1077 else
1078 {
1079 cur_height = (gui.char_height * shape_table[idx].percentage
1080 + 99) / 100;
1081 cur_width = gui.char_width;
1082 }
1083#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001084 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1085 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
1087 /* Double wide character. */
1088 if (shape_table[idx].shape != SHAPE_VER)
1089 cur_width += gui.char_width;
1090# ifdef FEAT_RIGHTLEFT
1091 if (CURSOR_BAR_RIGHT)
1092 {
1093 /* gui.col points to the left halve of the character but
1094 * the vertical line needs to be on the right halve.
1095 * A double-wide horizontal line is also drawn from the
1096 * right halve in gui_mch_draw_part_cursor(). */
1097 col_off = TRUE;
1098 ++gui.col;
1099 }
1100# endif
1101 }
1102#endif
1103 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1104#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1105 if (col_off)
1106 --gui.col;
1107#endif
1108
1109#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1110 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1111 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1112 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1113 (guicolor_T)0, (guicolor_T)0, 0);
1114#endif
1115 }
1116 gui.highlight_mask = old_hl_mask;
1117 }
1118}
1119
1120#if defined(FEAT_MENU) || defined(PROTO)
1121 void
1122gui_position_menu()
1123{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001124# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (gui.menu_is_active && gui.in_use)
1126 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1127# endif
1128}
1129#endif
1130
1131/*
1132 * Position the various GUI components (text area, menu). The vertical
1133 * scrollbars are NOT handled here. See gui_update_scrollbars().
1134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 static void
1136gui_position_components(total_width)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001137 int total_width UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138{
1139 int text_area_x;
1140 int text_area_y;
1141 int text_area_width;
1142 int text_area_height;
1143
1144 /* avoid that moving components around generates events */
1145 ++hold_gui_events;
1146
1147 text_area_x = 0;
1148 if (gui.which_scrollbars[SBAR_LEFT])
1149 text_area_x += gui.scrollbar_width;
1150
1151 text_area_y = 0;
1152#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1153 gui.menu_width = total_width;
1154 if (gui.menu_is_active)
1155 text_area_y += gui.menu_height;
1156#endif
1157#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1158 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1159 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1160#endif
1161
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001162# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001163 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001164 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001165 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001166#endif
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1169 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1170 {
1171# ifdef FEAT_GUI_ATHENA
1172 gui_mch_set_toolbar_pos(0, text_area_y,
1173 gui.menu_width, gui.toolbar_height);
1174# endif
1175 text_area_y += gui.toolbar_height;
1176 }
1177#endif
1178
1179 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1180 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1181
1182 gui_mch_set_text_area_pos(text_area_x,
1183 text_area_y,
1184 text_area_width,
1185 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001186#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 + xim_get_status_area_height()
1188#endif
1189 );
1190#ifdef FEAT_MENU
1191 gui_position_menu();
1192#endif
1193 if (gui.which_scrollbars[SBAR_BOTTOM])
1194 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1195 text_area_x,
1196 text_area_y + text_area_height,
1197 text_area_width,
1198 gui.scrollbar_height);
1199 gui.left_sbar_x = 0;
1200 gui.right_sbar_x = text_area_x + text_area_width;
1201
1202 --hold_gui_events;
1203}
1204
Bram Moolenaar02743632005-07-25 20:42:36 +00001205/*
1206 * Get the width of the widgets and decorations to the side of the text area.
1207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 int
1209gui_get_base_width()
1210{
1211 int base_width;
1212
1213 base_width = 2 * gui.border_offset;
1214 if (gui.which_scrollbars[SBAR_LEFT])
1215 base_width += gui.scrollbar_width;
1216 if (gui.which_scrollbars[SBAR_RIGHT])
1217 base_width += gui.scrollbar_width;
1218 return base_width;
1219}
1220
Bram Moolenaar02743632005-07-25 20:42:36 +00001221/*
1222 * Get the height of the widgets and decorations above and below the text area.
1223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 int
1225gui_get_base_height()
1226{
1227 int base_height;
1228
1229 base_height = 2 * gui.border_offset;
1230 if (gui.which_scrollbars[SBAR_BOTTOM])
1231 base_height += gui.scrollbar_height;
1232#ifdef FEAT_GUI_GTK
1233 /* We can't take the sizes properly into account until anything is
1234 * realized. Therefore we recalculate all the values here just before
1235 * setting the size. (--mdcki) */
1236#else
1237# ifdef FEAT_MENU
1238 if (gui.menu_is_active)
1239 base_height += gui.menu_height;
1240# endif
1241# ifdef FEAT_TOOLBAR
1242 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1243# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1244 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1245# else
1246 base_height += gui.toolbar_height;
1247# endif
1248# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001249# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1250 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001251 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001252 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254# ifdef FEAT_FOOTER
1255 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1256 base_height += gui.footer_height;
1257# endif
1258# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1259 base_height += gui_mch_text_area_extra_height();
1260# endif
1261#endif
1262 return base_height;
1263}
1264
1265/*
1266 * Should be called after the GUI shell has been resized. Its arguments are
1267 * the new width and height of the shell in pixels.
1268 */
1269 void
1270gui_resize_shell(pixel_width, pixel_height)
1271 int pixel_width;
1272 int pixel_height;
1273{
1274 static int busy = FALSE;
1275
1276 if (!gui.shell_created) /* ignore when still initializing */
1277 return;
1278
1279 /*
1280 * Can't resize the screen while it is being redrawn. Remember the new
1281 * size and handle it later.
1282 */
1283 if (updating_screen || busy)
1284 {
1285 new_pixel_width = pixel_width;
1286 new_pixel_height = pixel_height;
1287 return;
1288 }
1289
1290again:
1291 busy = TRUE;
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 /* Flush pending output before redrawing */
1294 out_flush();
1295
1296 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001297 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299 gui_position_components(pixel_width);
1300
1301 gui_reset_scroll_region();
1302 /*
1303 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1304 * at the last line here (why does it have to be one row too low?).
1305 */
1306 if (State == ASKMORE || State == CONFIRM)
1307 gui.row = gui.num_rows;
1308
1309 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1310 * the safe side. */
1311 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1312 || gui.num_rows != Rows || gui.num_cols != Columns)
1313 shell_resized();
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 gui_update_scrollbars(TRUE);
1316 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001317#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 xim_set_status_area();
1319#endif
1320
1321 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 /*
1324 * We could have been called again while redrawing the screen.
1325 * Need to do it all again with the latest size then.
1326 */
1327 if (new_pixel_height)
1328 {
1329 pixel_width = new_pixel_width;
1330 pixel_height = new_pixel_height;
1331 new_pixel_width = 0;
1332 new_pixel_height = 0;
1333 goto again;
1334 }
1335}
1336
1337/*
1338 * Check if gui_resize_shell() must be called.
1339 */
1340 void
1341gui_may_resize_shell()
1342{
1343 int h, w;
1344
1345 if (new_pixel_height)
1346 {
1347 /* careful: gui_resize_shell() may postpone the resize again if we
1348 * were called indirectly by it */
1349 w = new_pixel_width;
1350 h = new_pixel_height;
1351 new_pixel_width = 0;
1352 new_pixel_height = 0;
1353 gui_resize_shell(w, h);
1354 }
1355}
1356
1357 int
1358gui_get_shellsize()
1359{
1360 Rows = gui.num_rows;
1361 Columns = gui.num_cols;
1362 return OK;
1363}
1364
1365/*
1366 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001367 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1368 * on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001371gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001372 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001374 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 int base_width;
1377 int base_height;
1378 int width;
1379 int height;
1380 int min_width;
1381 int min_height;
1382 int screen_w;
1383 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001384#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001385 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001386 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001387#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001388 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
1390 if (!gui.shell_created)
1391 return;
1392
1393#ifdef MSWIN
1394 /* If not setting to a user specified size and maximized, calculate the
1395 * number of characters that fit in the maximized window. */
1396 if (!mustset && gui_mch_maximized())
1397 {
1398 gui_mch_newfont();
1399 return;
1400 }
1401#endif
1402
1403 base_width = gui_get_base_width();
1404 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001405 if (fit_to_display)
1406 /* Remember the original window position. */
1407 gui_mch_get_winpos(&x, &y);
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#ifdef USE_SUN_WORKSHOP
1410 if (!mustset && usingSunWorkShop
1411 && workshop_get_width_height(&width, &height))
1412 {
1413 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1414 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1415 }
1416 else
1417#endif
1418 {
1419 width = Columns * gui.char_width + base_width;
1420 height = Rows * gui.char_height + base_height;
1421 }
1422
1423 if (fit_to_display)
1424 {
1425 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001426 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 Columns = (screen_w - base_width) / gui.char_width;
1429 if (Columns < MIN_COLUMNS)
1430 Columns = MIN_COLUMNS;
1431 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001432#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001433 ++did_adjust;
1434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001436 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 {
1438 Rows = (screen_h - base_height) / gui.char_height;
1439 check_shellsize();
1440 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001441#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001442 ++did_adjust;
1443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001445#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001446 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1447 && height + gui.char_height >= screen_h))
1448 /* don't unmaximize if at maximum size */
1449 un_maximize = FALSE;
1450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 gui.num_cols = Columns;
1453 gui.num_rows = Rows;
1454
1455 min_width = base_width + MIN_COLUMNS * gui.char_width;
1456 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001457#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001458 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001459#endif
1460
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001461#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001462 if (un_maximize)
1463 {
1464 /* If the window size is smaller than the screen unmaximize the
1465 * window, otherwise resizing won't work. */
1466 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1467 if ((width + gui.char_width < screen_w
1468 || height + gui.char_height * 2 < screen_h)
1469 && gui_mch_maximized())
1470 gui_mch_unmaximize();
1471 }
1472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
1474 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001475 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001477 if (fit_to_display && x >= 0 && y >= 0)
1478 {
1479 /* Some window managers put the Vim window left of/above the screen.
1480 * Only change the position if it wasn't already negative before
1481 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 gui_mch_update();
1483 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1484 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1485 }
1486
1487 gui_position_components(width);
1488 gui_update_scrollbars(TRUE);
1489 gui_reset_scroll_region();
1490}
1491
1492/*
1493 * Called when Rows and/or Columns has changed.
1494 */
1495 void
1496gui_new_shellsize()
1497{
1498 gui_reset_scroll_region();
1499}
1500
1501/*
1502 * Make scroll region cover whole screen.
1503 */
1504 void
1505gui_reset_scroll_region()
1506{
1507 gui.scroll_region_top = 0;
1508 gui.scroll_region_bot = gui.num_rows - 1;
1509 gui.scroll_region_left = 0;
1510 gui.scroll_region_right = gui.num_cols - 1;
1511}
1512
1513 void
1514gui_start_highlight(mask)
1515 int mask;
1516{
1517 if (mask > HL_ALL) /* highlight code */
1518 gui.highlight_mask = mask;
1519 else /* mask */
1520 gui.highlight_mask |= mask;
1521}
1522
1523 void
1524gui_stop_highlight(mask)
1525 int mask;
1526{
1527 if (mask > HL_ALL) /* highlight code */
1528 gui.highlight_mask = HL_NORMAL;
1529 else /* mask */
1530 gui.highlight_mask &= ~mask;
1531}
1532
1533/*
1534 * Clear a rectangular region of the screen from text pos (row1, col1) to
1535 * (row2, col2) inclusive.
1536 */
1537 void
1538gui_clear_block(row1, col1, row2, col2)
1539 int row1;
1540 int col1;
1541 int row2;
1542 int col2;
1543{
1544 /* Clear the selection if we are about to write over it */
1545 clip_may_clear_selection(row1, row2);
1546
1547 gui_mch_clear_block(row1, col1, row2, col2);
1548
1549 /* Invalidate cursor if it was in this block */
1550 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1551 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1552 gui.cursor_is_valid = FALSE;
1553}
1554
1555/*
1556 * Write code to update the cursor later. This avoids the need to flush the
1557 * output buffer before calling gui_update_cursor().
1558 */
1559 void
1560gui_update_cursor_later()
1561{
1562 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1563}
1564
1565 void
1566gui_write(s, len)
1567 char_u *s;
1568 int len;
1569{
1570 char_u *p;
1571 int arg1 = 0, arg2 = 0;
1572 /* this doesn't make sense, disabled until someone can explain why it
1573 * would be needed */
1574#if 0 && (defined(RISCOS) || defined(WIN16))
1575 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1576 our redraw speed will suffer */
1577#else
1578 int force_cursor = FALSE; /* force cursor update */
1579#endif
1580 int force_scrollbar = FALSE;
1581 static win_T *old_curwin = NULL;
1582
1583/* #define DEBUG_GUI_WRITE */
1584#ifdef DEBUG_GUI_WRITE
1585 {
1586 int i;
1587 char_u *str;
1588
1589 printf("gui_write(%d):\n ", len);
1590 for (i = 0; i < len; i++)
1591 if (s[i] == ESC)
1592 {
1593 if (i != 0)
1594 printf("\n ");
1595 printf("<ESC>");
1596 }
1597 else
1598 {
1599 str = transchar_byte(s[i]);
1600 if (str[0] && str[1])
1601 printf("<%s>", (char *)str);
1602 else
1603 printf("%s", (char *)str);
1604 }
1605 printf("\n");
1606 }
1607#endif
1608 while (len)
1609 {
1610 if (s[0] == ESC && s[1] == '|')
1611 {
1612 p = s + 2;
1613 if (VIM_ISDIGIT(*p))
1614 {
1615 arg1 = getdigits(&p);
1616 if (p > s + len)
1617 break;
1618 if (*p == ';')
1619 {
1620 ++p;
1621 arg2 = getdigits(&p);
1622 if (p > s + len)
1623 break;
1624 }
1625 }
1626 switch (*p)
1627 {
1628 case 'C': /* Clear screen */
1629 clip_scroll_selection(9999);
1630 gui_mch_clear_all();
1631 gui.cursor_is_valid = FALSE;
1632 force_scrollbar = TRUE;
1633 break;
1634 case 'M': /* Move cursor */
1635 gui_set_cursor(arg1, arg2);
1636 break;
1637 case 's': /* force cursor (shape) update */
1638 force_cursor = TRUE;
1639 break;
1640 case 'R': /* Set scroll region */
1641 if (arg1 < arg2)
1642 {
1643 gui.scroll_region_top = arg1;
1644 gui.scroll_region_bot = arg2;
1645 }
1646 else
1647 {
1648 gui.scroll_region_top = arg2;
1649 gui.scroll_region_bot = arg1;
1650 }
1651 break;
1652#ifdef FEAT_VERTSPLIT
1653 case 'V': /* Set vertical scroll region */
1654 if (arg1 < arg2)
1655 {
1656 gui.scroll_region_left = arg1;
1657 gui.scroll_region_right = arg2;
1658 }
1659 else
1660 {
1661 gui.scroll_region_left = arg2;
1662 gui.scroll_region_right = arg1;
1663 }
1664 break;
1665#endif
1666 case 'd': /* Delete line */
1667 gui_delete_lines(gui.row, 1);
1668 break;
1669 case 'D': /* Delete lines */
1670 gui_delete_lines(gui.row, arg1);
1671 break;
1672 case 'i': /* Insert line */
1673 gui_insert_lines(gui.row, 1);
1674 break;
1675 case 'I': /* Insert lines */
1676 gui_insert_lines(gui.row, arg1);
1677 break;
1678 case '$': /* Clear to end-of-line */
1679 gui_clear_block(gui.row, gui.col, gui.row,
1680 (int)Columns - 1);
1681 break;
1682 case 'h': /* Turn on highlighting */
1683 gui_start_highlight(arg1);
1684 break;
1685 case 'H': /* Turn off highlighting */
1686 gui_stop_highlight(arg1);
1687 break;
1688 case 'f': /* flash the window (visual bell) */
1689 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1690 break;
1691 default:
1692 p = s + 1; /* Skip the ESC */
1693 break;
1694 }
1695 len -= (int)(++p - s);
1696 s = p;
1697 }
1698 else if (
1699#ifdef EBCDIC
1700 CtrlChar(s[0]) != 0 /* Ctrl character */
1701#else
1702 s[0] < 0x20 /* Ctrl character */
1703#endif
1704#ifdef FEAT_SIGN_ICONS
1705 && s[0] != SIGN_BYTE
1706# ifdef FEAT_NETBEANS_INTG
1707 && s[0] != MULTISIGN_BYTE
1708# endif
1709#endif
1710 )
1711 {
1712 if (s[0] == '\n') /* NL */
1713 {
1714 gui.col = 0;
1715 if (gui.row < gui.scroll_region_bot)
1716 gui.row++;
1717 else
1718 gui_delete_lines(gui.scroll_region_top, 1);
1719 }
1720 else if (s[0] == '\r') /* CR */
1721 {
1722 gui.col = 0;
1723 }
1724 else if (s[0] == '\b') /* Backspace */
1725 {
1726 if (gui.col)
1727 --gui.col;
1728 }
1729 else if (s[0] == Ctrl_L) /* cursor-right */
1730 {
1731 ++gui.col;
1732 }
1733 else if (s[0] == Ctrl_G) /* Beep */
1734 {
1735 gui_mch_beep();
1736 }
1737 /* Other Ctrl character: shouldn't happen! */
1738
1739 --len; /* Skip this char */
1740 ++s;
1741 }
1742 else
1743 {
1744 p = s;
1745 while (len > 0 && (
1746#ifdef EBCDIC
1747 CtrlChar(*p) == 0
1748#else
1749 *p >= 0x20
1750#endif
1751#ifdef FEAT_SIGN_ICONS
1752 || *p == SIGN_BYTE
1753# ifdef FEAT_NETBEANS_INTG
1754 || *p == MULTISIGN_BYTE
1755# endif
1756#endif
1757 ))
1758 {
1759 len--;
1760 p++;
1761 }
1762 gui_outstr(s, (int)(p - s));
1763 s = p;
1764 }
1765 }
1766
1767 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1768 * set). */
1769 if (force_cursor)
1770 gui_update_cursor(TRUE, TRUE);
1771
1772 /* When switching to another window the dragging must have stopped.
1773 * Required for GTK, dragged_sb isn't reset. */
1774 if (old_curwin != curwin)
1775 gui.dragged_sb = SBAR_NONE;
1776
1777 /* Update the scrollbars after clearing the screen or when switched
1778 * to another window.
1779 * Update the horizontal scrollbar always, it's difficult to check all
1780 * situations where it might change. */
1781 if (force_scrollbar || old_curwin != curwin)
1782 gui_update_scrollbars(force_scrollbar);
1783 else
1784 gui_update_horiz_scrollbar(FALSE);
1785 old_curwin = curwin;
1786
1787 /*
1788 * We need to make sure this is cleared since Athena doesn't tell us when
1789 * he is done dragging. Do the same for GTK.
1790 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001791#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 gui.dragged_sb = SBAR_NONE;
1793#endif
1794
1795 gui_mch_flush(); /* In case vim decides to take a nap */
1796}
1797
1798/*
1799 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1800 * produces wrong results. Call gui_dont_update_cursor() before that code and
1801 * gui_can_update_cursor() afterwards.
1802 */
1803 void
1804gui_dont_update_cursor()
1805{
1806 if (gui.in_use)
1807 {
1808 /* Undraw the cursor now, we probably can't do it after the change. */
1809 gui_undraw_cursor();
1810 can_update_cursor = FALSE;
1811 }
1812}
1813
1814 void
1815gui_can_update_cursor()
1816{
1817 can_update_cursor = TRUE;
1818 /* No need to update the cursor right now, there is always more output
1819 * after scrolling. */
1820}
1821
1822 static void
1823gui_outstr(s, len)
1824 char_u *s;
1825 int len;
1826{
1827 int this_len;
1828#ifdef FEAT_MBYTE
1829 int cells;
1830#endif
1831
1832 if (len == 0)
1833 return;
1834
1835 if (len < 0)
1836 len = (int)STRLEN(s);
1837
1838 while (len > 0)
1839 {
1840#ifdef FEAT_MBYTE
1841 if (has_mbyte)
1842 {
1843 /* Find out how many chars fit in the current line. */
1844 cells = 0;
1845 for (this_len = 0; this_len < len; )
1846 {
1847 cells += (*mb_ptr2cells)(s + this_len);
1848 if (gui.col + cells > Columns)
1849 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001850 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852 if (this_len > len)
1853 this_len = len; /* don't include following composing char */
1854 }
1855 else
1856#endif
1857 if (gui.col + len > Columns)
1858 this_len = Columns - gui.col;
1859 else
1860 this_len = len;
1861
1862 (void)gui_outstr_nowrap(s, this_len,
1863 0, (guicolor_T)0, (guicolor_T)0, 0);
1864 s += this_len;
1865 len -= this_len;
1866#ifdef FEAT_MBYTE
1867 /* fill up for a double-width char that doesn't fit. */
1868 if (len > 0 && gui.col < Columns)
1869 (void)gui_outstr_nowrap((char_u *)" ", 1,
1870 0, (guicolor_T)0, (guicolor_T)0, 0);
1871#endif
1872 /* The cursor may wrap to the next line. */
1873 if (gui.col >= Columns)
1874 {
1875 gui.col = 0;
1876 gui.row++;
1877 }
1878 }
1879}
1880
1881/*
1882 * Output one character (may be one or two display cells).
1883 * Caller must check for valid "off".
1884 * Returns FAIL or OK, just like gui_outstr_nowrap().
1885 */
1886 static int
1887gui_screenchar(off, flags, fg, bg, back)
1888 int off; /* Offset from start of screen */
1889 int flags;
1890 guicolor_T fg, bg; /* colors for cursor */
1891 int back; /* backup this many chars when using bold trick */
1892{
1893#ifdef FEAT_MBYTE
1894 char_u buf[MB_MAXBYTES + 1];
1895
1896 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1897 if (enc_utf8 && ScreenLines[off] == 0)
1898 return OK;
1899
1900 if (enc_utf8 && ScreenLinesUC[off] != 0)
1901 /* Draw UTF-8 multi-byte character. */
1902 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1903 flags, fg, bg, back);
1904
1905 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1906 {
1907 buf[0] = ScreenLines[off];
1908 buf[1] = ScreenLines2[off];
1909 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1910 }
1911
1912 /* Draw non-multi-byte character or DBCS character. */
1913 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001914 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 flags, fg, bg, back);
1916#else
1917 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1918#endif
1919}
1920
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001921#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
1923 * Output the string at the given screen position. This is used in place
1924 * of gui_screenchar() where possible because Pango needs as much context
1925 * as possible to work nicely. It's a lot faster as well.
1926 */
1927 static int
1928gui_screenstr(off, len, flags, fg, bg, back)
1929 int off; /* Offset from start of screen */
1930 int len; /* string length in screen cells */
1931 int flags;
1932 guicolor_T fg, bg; /* colors for cursor */
1933 int back; /* backup this many chars when using bold trick */
1934{
1935 char_u *buf;
1936 int outlen = 0;
1937 int i;
1938 int retval;
1939
1940 if (len <= 0) /* "cannot happen"? */
1941 return OK;
1942
1943 if (enc_utf8)
1944 {
1945 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1946 if (buf == NULL)
1947 return OK; /* not much we could do here... */
1948
1949 for (i = off; i < off + len; ++i)
1950 {
1951 if (ScreenLines[i] == 0)
1952 continue; /* skip second half of double-width char */
1953
1954 if (ScreenLinesUC[i] == 0)
1955 buf[outlen++] = ScreenLines[i];
1956 else
1957 outlen += utfc_char2bytes(i, buf + outlen);
1958 }
1959
1960 buf[outlen] = NUL; /* only to aid debugging */
1961 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1962 vim_free(buf);
1963
1964 return retval;
1965 }
1966 else if (enc_dbcs == DBCS_JPNU)
1967 {
1968 buf = alloc((unsigned)(len * 2 + 1));
1969 if (buf == NULL)
1970 return OK; /* not much we could do here... */
1971
1972 for (i = off; i < off + len; ++i)
1973 {
1974 buf[outlen++] = ScreenLines[i];
1975
1976 /* handle double-byte single-width char */
1977 if (ScreenLines[i] == 0x8e)
1978 buf[outlen++] = ScreenLines2[i];
1979 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1980 buf[outlen++] = ScreenLines[++i];
1981 }
1982
1983 buf[outlen] = NUL; /* only to aid debugging */
1984 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1985 vim_free(buf);
1986
1987 return retval;
1988 }
1989 else
1990 {
1991 return gui_outstr_nowrap(&ScreenLines[off], len,
1992 flags, fg, bg, back);
1993 }
1994}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001995#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997/*
1998 * Output the given string at the current cursor position. If the string is
1999 * too long to fit on the line, then it is truncated.
2000 * "flags":
2001 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2002 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002003 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 * background.
2005 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2006 * it.
2007 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2008 * FAIL (the caller should start drawing "back" chars back).
2009 */
2010 int
2011gui_outstr_nowrap(s, len, flags, fg, bg, back)
2012 char_u *s;
2013 int len;
2014 int flags;
2015 guicolor_T fg, bg; /* colors for cursor */
2016 int back; /* backup this many chars when using bold trick */
2017{
2018 long_u highlight_mask;
2019 long_u hl_mask_todo;
2020 guicolor_T fg_color;
2021 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002022 guicolor_T sp_color;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002023#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 GuiFont font = NOFONT;
2025# ifdef FEAT_XFONTSET
2026 GuiFontset fontset = NOFONTSET;
2027# endif
2028#endif
2029 attrentry_T *aep = NULL;
2030 int draw_flags;
2031 int col = gui.col;
2032#ifdef FEAT_SIGN_ICONS
2033 int draw_sign = FALSE;
2034# ifdef FEAT_NETBEANS_INTG
2035 int multi_sign = FALSE;
2036# endif
2037#endif
2038
2039 if (len < 0)
2040 len = (int)STRLEN(s);
2041 if (len == 0)
2042 return OK;
2043
2044#ifdef FEAT_SIGN_ICONS
2045 if (*s == SIGN_BYTE
2046# ifdef FEAT_NETBEANS_INTG
2047 || *s == MULTISIGN_BYTE
2048# endif
2049 )
2050 {
2051# ifdef FEAT_NETBEANS_INTG
2052 if (*s == MULTISIGN_BYTE)
2053 multi_sign = TRUE;
2054# endif
2055 /* draw spaces instead */
2056 s = (char_u *)" ";
2057 if (len == 1 && col > 0)
2058 --col;
2059 len = 2;
2060 draw_sign = TRUE;
2061 highlight_mask = 0;
2062 }
2063 else
2064#endif
2065 if (gui.highlight_mask > HL_ALL)
2066 {
2067 aep = syn_gui_attr2entry(gui.highlight_mask);
2068 if (aep == NULL) /* highlighting not set */
2069 highlight_mask = 0;
2070 else
2071 highlight_mask = aep->ae_attr;
2072 }
2073 else
2074 highlight_mask = gui.highlight_mask;
2075 hl_mask_todo = highlight_mask;
2076
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002077#if !defined(MSWIN16_FASTTEXT) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /* Set the font */
2079 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2080 font = aep->ae_u.gui.font;
2081# ifdef FEAT_XFONTSET
2082 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2083 fontset = aep->ae_u.gui.fontset;
2084# endif
2085 else
2086 {
2087# ifdef FEAT_XFONTSET
2088 if (gui.fontset != NOFONTSET)
2089 fontset = gui.fontset;
2090 else
2091# endif
2092 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2093 {
2094 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2095 {
2096 font = gui.boldital_font;
2097 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2098 }
2099 else if (gui.bold_font != NOFONT)
2100 {
2101 font = gui.bold_font;
2102 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2103 }
2104 else
2105 font = gui.norm_font;
2106 }
2107 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2108 {
2109 font = gui.ital_font;
2110 hl_mask_todo &= ~HL_ITALIC;
2111 }
2112 else
2113 font = gui.norm_font;
2114 }
2115# ifdef FEAT_XFONTSET
2116 if (fontset != NOFONTSET)
2117 gui_mch_set_fontset(fontset);
2118 else
2119# endif
2120 gui_mch_set_font(font);
2121#endif
2122
2123 draw_flags = 0;
2124
2125 /* Set the color */
2126 bg_color = gui.back_pixel;
2127 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2128 {
2129 draw_flags |= DRAW_CURSOR;
2130 fg_color = fg;
2131 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002132 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134 else if (aep != NULL)
2135 {
2136 fg_color = aep->ae_u.gui.fg_color;
2137 if (fg_color == INVALCOLOR)
2138 fg_color = gui.norm_pixel;
2139 bg_color = aep->ae_u.gui.bg_color;
2140 if (bg_color == INVALCOLOR)
2141 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002142 sp_color = aep->ae_u.gui.sp_color;
2143 if (sp_color == INVALCOLOR)
2144 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002149 sp_color = fg_color;
2150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2153 {
2154#if defined(AMIGA) || defined(RISCOS)
2155 gui_mch_set_colors(bg_color, fg_color);
2156#else
2157 gui_mch_set_fg_color(bg_color);
2158 gui_mch_set_bg_color(fg_color);
2159#endif
2160 }
2161 else
2162 {
2163#if defined(AMIGA) || defined(RISCOS)
2164 gui_mch_set_colors(fg_color, bg_color);
2165#else
2166 gui_mch_set_fg_color(fg_color);
2167 gui_mch_set_bg_color(bg_color);
2168#endif
2169 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002170 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172 /* Clear the selection if we are about to write over it */
2173 if (!(flags & GUI_MON_NOCLEAR))
2174 clip_may_clear_selection(gui.row, gui.row);
2175
2176
2177#ifndef MSWIN16_FASTTEXT
2178 /* If there's no bold font, then fake it */
2179 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2180 draw_flags |= DRAW_BOLD;
2181#endif
2182
2183 /*
2184 * When drawing bold or italic characters the spill-over from the left
2185 * neighbor may be destroyed. Let the caller backup to start redrawing
2186 * just after a blank.
2187 */
2188 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2189 return FAIL;
2190
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002191#if defined(RISCOS) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 /* If there's no italic font, then fake it.
2193 * For GTK2, we don't need a different font for italic style. */
2194 if (hl_mask_todo & HL_ITALIC)
2195 draw_flags |= DRAW_ITALIC;
2196
2197 /* Do we underline the text? */
2198 if (hl_mask_todo & HL_UNDERLINE)
2199 draw_flags |= DRAW_UNDERL;
2200#else
2201 /* Do we underline the text? */
2202 if ((hl_mask_todo & HL_UNDERLINE)
2203# ifndef MSWIN16_FASTTEXT
2204 || (hl_mask_todo & HL_ITALIC)
2205# endif
2206 )
2207 draw_flags |= DRAW_UNDERL;
2208#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002209 /* Do we undercurl the text? */
2210 if (hl_mask_todo & HL_UNDERCURL)
2211 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002213 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (flags & GUI_MON_TRS_CURSOR)
2215 draw_flags |= DRAW_TRANSP;
2216
2217 /*
2218 * Draw the text.
2219 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002220#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 /* The value returned is the length in display cells */
2222 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2223#else
2224# ifdef FEAT_MBYTE
2225 if (enc_utf8)
2226 {
2227 int start; /* index of bytes to be drawn */
2228 int cells; /* cellwidth of bytes to be drawn */
2229 int thislen; /* length of bytes to be drawin */
2230 int cn; /* cellwidth of current char */
2231 int i; /* index of current char */
2232 int c; /* current char value */
2233 int cl; /* byte length of current char */
2234 int comping; /* current char is composing */
2235 int scol = col; /* screen column */
2236 int dowide; /* use 'guifontwide' */
2237
2238 /* Break the string at a composing character, it has to be drawn on
2239 * top of the previous character. */
2240 start = 0;
2241 cells = 0;
2242 for (i = 0; i < len; i += cl)
2243 {
2244 c = utf_ptr2char(s + i);
2245 cn = utf_char2cells(c);
2246 if (cn > 1
2247# ifdef FEAT_XFONTSET
2248 && fontset == NOFONTSET
2249# endif
2250 && gui.wide_font != NOFONT)
2251 dowide = TRUE;
2252 else
2253 dowide = FALSE;
2254 comping = utf_iscomposing(c);
2255 if (!comping) /* count cells from non-composing chars */
2256 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002257 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (cl == 0) /* hit end of string */
2259 len = i + cl; /* len must be wrong "cannot happen" */
2260
2261 /* print the string so far if it's the last character or there is
2262 * a composing character. */
2263 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002264# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 || (cn > 1
2266# ifdef FEAT_XFONTSET
2267 /* No fontset: At least draw char after wide char at
2268 * right position. */
2269 && fontset == NOFONTSET
2270# endif
2271 )
2272# endif
2273 )
2274 {
2275 if (comping || dowide)
2276 thislen = i - start;
2277 else
2278 thislen = i - start + cl;
2279 if (thislen > 0)
2280 {
2281 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2282 draw_flags);
2283 start += thislen;
2284 }
2285 scol += cells;
2286 cells = 0;
2287 if (dowide)
2288 {
2289 gui_mch_set_font(gui.wide_font);
2290 gui_mch_draw_string(gui.row, scol - cn,
2291 s + start, cl, draw_flags);
2292 gui_mch_set_font(font);
2293 start += cl;
2294 }
2295
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002296# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002297 /* No fontset: draw a space to fill the gap after a wide char
2298 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2300# ifdef FEAT_XFONTSET
2301 && fontset == NOFONTSET
2302# endif
2303 && !dowide)
2304 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2305 1, draw_flags);
2306# endif
2307 }
2308 /* Draw a composing char on top of the previous char. */
2309 if (comping)
2310 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002311# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002312 /* Carbon ATSUI autodraws composing char over previous char */
2313 gui_mch_draw_string(gui.row, scol, s + i, cl,
2314 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002315# else
2316 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2317 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 start = i + cl;
2320 }
2321 }
2322 /* The stuff below assumes "len" is the length in screen columns. */
2323 len = scol - col;
2324 }
2325 else
2326# endif
2327 {
2328 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2329# ifdef FEAT_MBYTE
2330 if (enc_dbcs == DBCS_JPNU)
2331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 /* Get the length in display cells, this can be different from the
2333 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002334 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336# endif
2337 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002338#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2341 gui.col = col + len;
2342
2343 /* May need to invert it when it's part of the selection. */
2344 if (flags & GUI_MON_NOCLEAR)
2345 clip_may_redraw_selection(gui.row, col, len);
2346
2347 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2348 {
2349 /* Invalidate the old physical cursor position if we wrote over it */
2350 if (gui.cursor_row == gui.row
2351 && gui.cursor_col >= col
2352 && gui.cursor_col < col + len)
2353 gui.cursor_is_valid = FALSE;
2354 }
2355
2356#ifdef FEAT_SIGN_ICONS
2357 if (draw_sign)
2358 /* Draw the sign on top of the spaces. */
2359 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002360# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_MOTIF) \
2361 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (multi_sign)
2363 netbeans_draw_multisign_indicator(gui.row);
2364# endif
2365#endif
2366
2367 return OK;
2368}
2369
2370/*
2371 * Un-draw the cursor. Actually this just redraws the character at the given
2372 * position. The character just before it too, for when it was in bold.
2373 */
2374 void
2375gui_undraw_cursor()
2376{
2377 if (gui.cursor_is_valid)
2378 {
2379#ifdef FEAT_HANGULIN
2380 if (composing_hangul
2381 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2382 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2383 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2384 gui.norm_pixel, gui.back_pixel, 0);
2385 else
2386 {
2387#endif
2388 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2389 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2390 && gui.cursor_col > 0)
2391 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2392 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2393#ifdef FEAT_HANGULIN
2394 if (composing_hangul)
2395 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2396 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2397 }
2398#endif
2399 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2400 * here in case it wasn't needed to undraw it. */
2401 gui.cursor_is_valid = FALSE;
2402 }
2403}
2404
2405 void
2406gui_redraw(x, y, w, h)
2407 int x;
2408 int y;
2409 int w;
2410 int h;
2411{
2412 int row1, col1, row2, col2;
2413
2414 row1 = Y_2_ROW(y);
2415 col1 = X_2_COL(x);
2416 row2 = Y_2_ROW(y + h - 1);
2417 col2 = X_2_COL(x + w - 1);
2418
2419 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2420
2421 /*
2422 * We may need to redraw the cursor, but don't take it upon us to change
2423 * its location after a scroll.
2424 * (maybe be more strict even and test col too?)
2425 * These things may be outside the update/clipping region and reality may
2426 * not reflect Vims internal ideas if these operations are clipped away.
2427 */
2428 if (gui.row == gui.cursor_row)
2429 gui_update_cursor(TRUE, TRUE);
2430}
2431
2432/*
2433 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2434 * from col1 to col2 (inclusive).
2435 * Return TRUE when the character before the first drawn character has
2436 * different attributes (may have to be redrawn too).
2437 */
2438 int
2439gui_redraw_block(row1, col1, row2, col2, flags)
2440 int row1;
2441 int col1;
2442 int row2;
2443 int col2;
2444 int flags; /* flags for gui_outstr_nowrap() */
2445{
2446 int old_row, old_col;
2447 long_u old_hl_mask;
2448 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002449 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 int idx, len;
2451 int back, nback;
2452 int retval = FALSE;
2453#ifdef FEAT_MBYTE
2454 int orig_col1, orig_col2;
2455#endif
2456
2457 /* Don't try to update when ScreenLines is not valid */
2458 if (!screen_cleared || ScreenLines == NULL)
2459 return retval;
2460
2461 /* Don't try to draw outside the shell! */
2462 /* Check everything, strange values may be caused by a big border width */
2463 col1 = check_col(col1);
2464 col2 = check_col(col2);
2465 row1 = check_row(row1);
2466 row2 = check_row(row2);
2467
2468 /* Remember where our cursor was */
2469 old_row = gui.row;
2470 old_col = gui.col;
2471 old_hl_mask = gui.highlight_mask;
2472#ifdef FEAT_MBYTE
2473 orig_col1 = col1;
2474 orig_col2 = col2;
2475#endif
2476
2477 for (gui.row = row1; gui.row <= row2; gui.row++)
2478 {
2479#ifdef FEAT_MBYTE
2480 /* When only half of a double-wide character is in the block, include
2481 * the other half. */
2482 col1 = orig_col1;
2483 col2 = orig_col2;
2484 off = LineOffset[gui.row];
2485 if (enc_dbcs != 0)
2486 {
2487 if (col1 > 0)
2488 col1 -= dbcs_screen_head_off(ScreenLines + off,
2489 ScreenLines + off + col1);
2490 col2 += dbcs_screen_tail_off(ScreenLines + off,
2491 ScreenLines + off + col2);
2492 }
2493 else if (enc_utf8)
2494 {
2495 if (ScreenLines[off + col1] == 0)
2496 --col1;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002497# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2499 ++col2;
2500# endif
2501 }
2502#endif
2503 gui.col = col1;
2504 off = LineOffset[gui.row] + gui.col;
2505 len = col2 - col1 + 1;
2506
2507 /* Find how many chars back this highlighting starts, or where a space
2508 * is. Needed for when the bold trick is used */
2509 for (back = 0; back < col1; ++back)
2510 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2511 || ScreenLines[off - 1 - back] == ' ')
2512 break;
2513 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2514 && ScreenLines[off - 1] != ' ');
2515
2516 /* Break it up in strings of characters with the same attributes. */
2517 /* Print UTF-8 characters individually. */
2518 while (len > 0)
2519 {
2520 first_attr = ScreenAttrs[off];
2521 gui.highlight_mask = first_attr;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002522#if defined(FEAT_MBYTE) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (enc_utf8 && ScreenLinesUC[off] != 0)
2524 {
2525 /* output multi-byte character separately */
2526 nback = gui_screenchar(off, flags,
2527 (guicolor_T)0, (guicolor_T)0, back);
2528 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2529 idx = 2;
2530 else
2531 idx = 1;
2532 }
2533 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2534 {
2535 /* output double-byte, single-width character separately */
2536 nback = gui_screenchar(off, flags,
2537 (guicolor_T)0, (guicolor_T)0, back);
2538 idx = 1;
2539 }
2540 else
2541#endif
2542 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002543#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 for (idx = 0; idx < len; ++idx)
2545 {
2546 if (enc_utf8 && ScreenLines[off + idx] == 0)
2547 continue; /* skip second half of double-width char */
2548 if (ScreenAttrs[off + idx] != first_attr)
2549 break;
2550 }
2551 /* gui_screenstr() takes care of multibyte chars */
2552 nback = gui_screenstr(off, idx, flags,
2553 (guicolor_T)0, (guicolor_T)0, back);
2554#else
2555 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2556 idx++)
2557 {
2558# ifdef FEAT_MBYTE
2559 /* Stop at a multi-byte Unicode character. */
2560 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2561 break;
2562 if (enc_dbcs == DBCS_JPNU)
2563 {
2564 /* Stop at a double-byte single-width char. */
2565 if (ScreenLines[off + idx] == 0x8e)
2566 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002567 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 + off + idx) == 2)
2569 ++idx; /* skip second byte of double-byte char */
2570 }
2571# endif
2572 }
2573 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2574 (guicolor_T)0, (guicolor_T)0, back);
2575#endif
2576 }
2577 if (nback == FAIL)
2578 {
2579 /* Must back up to start drawing where a bold or italic word
2580 * starts. */
2581 off -= back;
2582 len += back;
2583 gui.col -= back;
2584 }
2585 else
2586 {
2587 off += idx;
2588 len -= idx;
2589 }
2590 back = 0;
2591 }
2592 }
2593
2594 /* Put the cursor back where it was */
2595 gui.row = old_row;
2596 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002597 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 return retval;
2600}
2601
2602 static void
2603gui_delete_lines(row, count)
2604 int row;
2605 int count;
2606{
2607 if (count <= 0)
2608 return;
2609
2610 if (row + count > gui.scroll_region_bot)
2611 /* Scrolled out of region, just blank the lines out */
2612 gui_clear_block(row, gui.scroll_region_left,
2613 gui.scroll_region_bot, gui.scroll_region_right);
2614 else
2615 {
2616 gui_mch_delete_lines(row, count);
2617
2618 /* If the cursor was in the deleted lines it's now gone. If the
2619 * cursor was in the scrolled lines adjust its position. */
2620 if (gui.cursor_row >= row
2621 && gui.cursor_col >= gui.scroll_region_left
2622 && gui.cursor_col <= gui.scroll_region_right)
2623 {
2624 if (gui.cursor_row < row + count)
2625 gui.cursor_is_valid = FALSE;
2626 else if (gui.cursor_row <= gui.scroll_region_bot)
2627 gui.cursor_row -= count;
2628 }
2629 }
2630}
2631
2632 static void
2633gui_insert_lines(row, count)
2634 int row;
2635 int count;
2636{
2637 if (count <= 0)
2638 return;
2639
2640 if (row + count > gui.scroll_region_bot)
2641 /* Scrolled out of region, just blank the lines out */
2642 gui_clear_block(row, gui.scroll_region_left,
2643 gui.scroll_region_bot, gui.scroll_region_right);
2644 else
2645 {
2646 gui_mch_insert_lines(row, count);
2647
2648 if (gui.cursor_row >= gui.row
2649 && gui.cursor_col >= gui.scroll_region_left
2650 && gui.cursor_col <= gui.scroll_region_right)
2651 {
2652 if (gui.cursor_row <= gui.scroll_region_bot - count)
2653 gui.cursor_row += count;
2654 else if (gui.cursor_row <= gui.scroll_region_bot)
2655 gui.cursor_is_valid = FALSE;
2656 }
2657 }
2658}
2659
2660/*
2661 * The main GUI input routine. Waits for a character from the keyboard.
2662 * wtime == -1 Wait forever.
2663 * wtime == 0 Don't wait.
2664 * wtime > 0 Wait wtime milliseconds for a character.
2665 * Returns OK if a character was found to be available within the given time,
2666 * or FAIL otherwise.
2667 */
2668 int
2669gui_wait_for_chars(wtime)
2670 long wtime;
2671{
2672 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02002674#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 /*
2676 * If we're going to wait a bit, update the menus and mouse shape for the
2677 * current State.
2678 */
2679 if (wtime != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 gui_update_menus(0);
2681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 gui_mch_update();
2684 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
2689 /* Before waiting, flush any output to the screen. */
2690 gui_mch_flush();
2691
2692 if (wtime > 0)
2693 {
2694 /* Blink when waiting for a character. Probably only does something
2695 * for showmatch() */
2696 gui_mch_start_blink();
2697 retval = gui_mch_wait_for_chars(wtime);
2698 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return retval;
2700 }
2701
2702 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002703 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
2705 gui_mch_start_blink();
2706
Bram Moolenaar3918c952005-03-15 22:34:55 +00002707 retval = FAIL;
2708 /*
2709 * We may want to trigger the CursorHold event. First wait for
2710 * 'updatetime' and if nothing is typed within that time put the
2711 * K_CURSORHOLD key in the input buffer.
2712 */
2713 if (gui_mch_wait_for_chars(p_ut) == OK)
2714 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002716 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002718 char_u buf[3];
2719
2720 /* Put K_CURSORHOLD in the input buffer. */
2721 buf[0] = CSI;
2722 buf[1] = KS_EXTRA;
2723 buf[2] = (int)KE_CURSORHOLD;
2724 add_to_input_buf(buf, 3);
2725
2726 retval = OK;
2727 }
2728#endif
2729
2730 if (retval == FAIL)
2731 {
2732 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002733 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 gui_mch_stop_blink();
2738 return retval;
2739}
2740
2741/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002742 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 */
2744 static void
2745fill_mouse_coord(p, col, row)
2746 char_u *p;
2747 int col;
2748 int row;
2749{
2750 p[0] = (char_u)(col / 128 + ' ' + 1);
2751 p[1] = (char_u)(col % 128 + ' ' + 1);
2752 p[2] = (char_u)(row / 128 + ' ' + 1);
2753 p[3] = (char_u)(row % 128 + ' ' + 1);
2754}
2755
2756/*
2757 * Generic mouse support function. Add a mouse event to the input buffer with
2758 * the given properties.
2759 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2760 * MOUSE_X1, MOUSE_X2
2761 * MOUSE_DRAG, or MOUSE_RELEASE.
2762 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2763 * x, y --- Coordinates of mouse in pixels.
2764 * repeated_click --- TRUE if this click comes only a short time after a
2765 * previous click.
2766 * modifiers --- Bit field which may be any of the following modifiers
2767 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2768 * This function will ignore drag events where the mouse has not moved to a new
2769 * character.
2770 */
2771 void
2772gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2773 int button;
2774 int x;
2775 int y;
2776 int repeated_click;
2777 int_u modifiers;
2778{
2779 static int prev_row = 0, prev_col = 0;
2780 static int prev_button = -1;
2781 static int num_clicks = 1;
2782 char_u string[10];
2783 enum key_extra button_char;
2784 int row, col;
2785#ifdef FEAT_CLIPBOARD
2786 int checkfor;
2787 int did_clip = FALSE;
2788#endif
2789
2790 /*
2791 * Scrolling may happen at any time, also while a selection is present.
2792 */
2793 switch (button)
2794 {
2795 case MOUSE_X1:
2796 button_char = KE_X1MOUSE;
2797 goto button_set;
2798 case MOUSE_X2:
2799 button_char = KE_X2MOUSE;
2800 goto button_set;
2801 case MOUSE_4:
2802 button_char = KE_MOUSEDOWN;
2803 goto button_set;
2804 case MOUSE_5:
2805 button_char = KE_MOUSEUP;
2806button_set:
2807 {
2808 /* Don't put events in the input queue now. */
2809 if (hold_gui_events)
2810 return;
2811
2812 string[3] = CSI;
2813 string[4] = KS_EXTRA;
2814 string[5] = (int)button_char;
2815
2816 /* Pass the pointer coordinates of the scroll event so that we
2817 * know which window to scroll. */
2818 row = gui_xy2colrow(x, y, &col);
2819 string[6] = (char_u)(col / 128 + ' ' + 1);
2820 string[7] = (char_u)(col % 128 + ' ' + 1);
2821 string[8] = (char_u)(row / 128 + ' ' + 1);
2822 string[9] = (char_u)(row % 128 + ' ' + 1);
2823
2824 if (modifiers == 0)
2825 add_to_input_buf(string + 3, 7);
2826 else
2827 {
2828 string[0] = CSI;
2829 string[1] = KS_MODIFIER;
2830 string[2] = 0;
2831 if (modifiers & MOUSE_SHIFT)
2832 string[2] |= MOD_MASK_SHIFT;
2833 if (modifiers & MOUSE_CTRL)
2834 string[2] |= MOD_MASK_CTRL;
2835 if (modifiers & MOUSE_ALT)
2836 string[2] |= MOD_MASK_ALT;
2837 add_to_input_buf(string, 10);
2838 }
2839 return;
2840 }
2841 }
2842
2843#ifdef FEAT_CLIPBOARD
2844 /* If a clipboard selection is in progress, handle it */
2845 if (clip_star.state == SELECT_IN_PROGRESS)
2846 {
2847 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2848 return;
2849 }
2850
2851 /* Determine which mouse settings to look for based on the current mode */
2852 switch (get_real_state())
2853 {
2854 case NORMAL_BUSY:
2855 case OP_PENDING:
2856 case NORMAL: checkfor = MOUSE_NORMAL; break;
2857 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002858 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case REPLACE:
2860 case REPLACE+LANGMAP:
2861#ifdef FEAT_VREPLACE
2862 case VREPLACE:
2863 case VREPLACE+LANGMAP:
2864#endif
2865 case INSERT:
2866 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2867 case ASKMORE:
2868 case HITRETURN: /* At the more- and hit-enter prompt pass the
2869 mouse event for a click on or below the
2870 message line. */
2871 if (Y_2_ROW(y) >= msg_row)
2872 checkfor = MOUSE_NORMAL;
2873 else
2874 checkfor = MOUSE_RETURN;
2875 break;
2876
2877 /*
2878 * On the command line, use the clipboard selection on all lines
2879 * but the command line. But not when pasting.
2880 */
2881 case CMDLINE:
2882 case CMDLINE+LANGMAP:
2883 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2884 checkfor = MOUSE_NONE;
2885 else
2886 checkfor = MOUSE_COMMAND;
2887 break;
2888
2889 default:
2890 checkfor = MOUSE_NONE;
2891 break;
2892 };
2893
2894 /*
2895 * Allow clipboard selection of text on the command line in "normal"
2896 * modes. Don't do this when dragging the status line, or extending a
2897 * Visual selection.
2898 */
2899 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2900 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00002901# ifdef FEAT_WINDOWS
2902 + firstwin->w_winrow
2903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 && button != MOUSE_DRAG
2905# ifdef FEAT_MOUSESHAPE
2906 && !drag_status_line
2907# ifdef FEAT_VERTSPLIT
2908 && !drag_sep_line
2909# endif
2910# endif
2911 )
2912 checkfor = MOUSE_NONE;
2913
2914 /*
2915 * Use modeless selection when holding CTRL and SHIFT pressed.
2916 */
2917 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2918 checkfor = MOUSE_NONEF;
2919
2920 /*
2921 * In Ex mode, always use modeless selection.
2922 */
2923 if (exmode_active)
2924 checkfor = MOUSE_NONE;
2925
2926 /*
2927 * If the mouse settings say to not use the mouse, use the modeless
2928 * selection. But if Visual is active, assume that only the Visual area
2929 * will be selected.
2930 * Exception: On the command line, both the selection is used and a mouse
2931 * key is send.
2932 */
2933 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2934 {
2935#ifdef FEAT_VISUAL
2936 /* Don't do modeless selection in Visual mode. */
2937 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2938 return;
2939#endif
2940
2941 /*
2942 * When 'mousemodel' is "popup", shift-left is translated to right.
2943 * But not when also using Ctrl.
2944 */
2945 if (mouse_model_popup() && button == MOUSE_LEFT
2946 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2947 {
2948 button = MOUSE_RIGHT;
2949 modifiers &= ~ MOUSE_SHIFT;
2950 }
2951
2952 /* If the selection is done, allow the right button to extend it.
2953 * If the selection is cleared, allow the right button to start it
2954 * from the cursor position. */
2955 if (button == MOUSE_RIGHT)
2956 {
2957 if (clip_star.state == SELECT_CLEARED)
2958 {
2959 if (State & CMDLINE)
2960 {
2961 col = msg_col;
2962 row = msg_row;
2963 }
2964 else
2965 {
2966 col = curwin->w_wcol;
2967 row = curwin->w_wrow + W_WINROW(curwin);
2968 }
2969 clip_start_selection(col, row, FALSE);
2970 }
2971 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2972 repeated_click);
2973 did_clip = TRUE;
2974 }
2975 /* Allow the left button to start the selection */
2976 else if (button ==
2977# ifdef RISCOS
2978 /* Only start a drag on a drag event. Otherwise
2979 * we don't get a release event. */
2980 MOUSE_DRAG
2981# else
2982 MOUSE_LEFT
2983# endif
2984 )
2985 {
2986 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2987 did_clip = TRUE;
2988 }
2989# ifdef RISCOS
2990 else if (button == MOUSE_LEFT)
2991 {
2992 clip_clear_selection();
2993 did_clip = TRUE;
2994 }
2995# endif
2996
2997 /* Always allow pasting */
2998 if (button != MOUSE_MIDDLE)
2999 {
3000 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3001 return;
3002 if (checkfor != MOUSE_COMMAND)
3003 button = MOUSE_LEFT;
3004 }
3005 repeated_click = FALSE;
3006 }
3007
3008 if (clip_star.state != SELECT_CLEARED && !did_clip)
3009 clip_clear_selection();
3010#endif
3011
3012 /* Don't put events in the input queue now. */
3013 if (hold_gui_events)
3014 return;
3015
3016 row = gui_xy2colrow(x, y, &col);
3017
3018 /*
3019 * If we are dragging and the mouse hasn't moved far enough to be on a
3020 * different character, then don't send an event to vim.
3021 */
3022 if (button == MOUSE_DRAG)
3023 {
3024 if (row == prev_row && col == prev_col)
3025 return;
3026 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3027 if (y < 0)
3028 row = -1;
3029 }
3030
3031 /*
3032 * If topline has changed (window scrolled) since the last click, reset
3033 * repeated_click, because we don't want starting Visual mode when
3034 * clicking on a different character in the text.
3035 */
3036 if (curwin->w_topline != gui_prev_topline
3037#ifdef FEAT_DIFF
3038 || curwin->w_topfill != gui_prev_topfill
3039#endif
3040 )
3041 repeated_click = FALSE;
3042
3043 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3044 string[1] = KS_MOUSE;
3045 string[2] = KE_FILLER;
3046 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3047 {
3048 if (repeated_click)
3049 {
3050 /*
3051 * Handle multiple clicks. They only count if the mouse is still
3052 * pointing at the same character.
3053 */
3054 if (button != prev_button || row != prev_row || col != prev_col)
3055 num_clicks = 1;
3056 else if (++num_clicks > 4)
3057 num_clicks = 1;
3058 }
3059 else
3060 num_clicks = 1;
3061 prev_button = button;
3062 gui_prev_topline = curwin->w_topline;
3063#ifdef FEAT_DIFF
3064 gui_prev_topfill = curwin->w_topfill;
3065#endif
3066
3067 string[3] = (char_u)(button | 0x20);
3068 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3069 }
3070 else
3071 string[3] = (char_u)button;
3072
3073 string[3] |= modifiers;
3074 fill_mouse_coord(string + 4, col, row);
3075 add_to_input_buf(string, 8);
3076
3077 if (row < 0)
3078 prev_row = 0;
3079 else
3080 prev_row = row;
3081 prev_col = col;
3082
3083 /*
3084 * We need to make sure this is cleared since Athena doesn't tell us when
3085 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3086 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003087#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 gui.dragged_sb = SBAR_NONE;
3089#endif
3090}
3091
3092/*
3093 * Convert x and y coordinate to column and row in text window.
3094 * Corrects for multi-byte character.
3095 * returns column in "*colp" and row as return value;
3096 */
3097 int
3098gui_xy2colrow(x, y, colp)
3099 int x;
3100 int y;
3101 int *colp;
3102{
3103 int col = check_col(X_2_COL(x));
3104 int row = check_row(Y_2_ROW(y));
3105
3106#ifdef FEAT_MBYTE
3107 *colp = mb_fix_col(col, row);
3108#else
3109 *colp = col;
3110#endif
3111 return row;
3112}
3113
3114#if defined(FEAT_MENU) || defined(PROTO)
3115/*
3116 * Callback function for when a menu entry has been selected.
3117 */
3118 void
3119gui_menu_cb(menu)
3120 vimmenu_T *menu;
3121{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003122 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /* Don't put events in the input queue now. */
3125 if (hold_gui_events)
3126 return;
3127
3128 bytes[0] = CSI;
3129 bytes[1] = KS_MENU;
3130 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003131 add_to_input_buf(bytes, 3);
3132 add_long_to_buf((long_u)menu, bytes);
3133 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134}
3135#endif
3136
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003137static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139/*
3140 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003141 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * in p_go.
3143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 void
3145gui_init_which_components(oldval)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003146 char_u *oldval UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#ifdef FEAT_MENU
3149 static int prev_menu_is_active = -1;
3150#endif
3151#ifdef FEAT_TOOLBAR
3152 static int prev_toolbar = -1;
3153 int using_toolbar = FALSE;
3154#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003155#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003156 int using_tabline;
3157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158#ifdef FEAT_FOOTER
3159 static int prev_footer = -1;
3160 int using_footer = FALSE;
3161#endif
3162#if defined(FEAT_MENU) && !defined(WIN16)
3163 static int prev_tearoff = -1;
3164 int using_tearoff = FALSE;
3165#endif
3166
3167 char_u *p;
3168 int i;
3169#ifdef FEAT_MENU
3170 int grey_old, grey_new;
3171 char_u *temp;
3172#endif
3173 win_T *wp;
3174 int need_set_size;
3175 int fix_size;
3176
3177#ifdef FEAT_MENU
3178 if (oldval != NULL && gui.in_use)
3179 {
3180 /*
3181 * Check if the menu's go from grey to non-grey or vise versa.
3182 */
3183 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3184 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3185 if (grey_old != grey_new)
3186 {
3187 temp = p_go;
3188 p_go = oldval;
3189 gui_update_menus(MENU_ALL_MODES);
3190 p_go = temp;
3191 }
3192 }
3193 gui.menu_is_active = FALSE;
3194#endif
3195
3196 for (i = 0; i < 3; i++)
3197 gui.which_scrollbars[i] = FALSE;
3198 for (p = p_go; *p; p++)
3199 switch (*p)
3200 {
3201 case GO_LEFT:
3202 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3203 break;
3204 case GO_RIGHT:
3205 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3206 break;
3207#ifdef FEAT_VERTSPLIT
3208 case GO_VLEFT:
3209 if (win_hasvertsplit())
3210 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3211 break;
3212 case GO_VRIGHT:
3213 if (win_hasvertsplit())
3214 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3215 break;
3216#endif
3217 case GO_BOT:
3218 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3219 break;
3220#ifdef FEAT_MENU
3221 case GO_MENUS:
3222 gui.menu_is_active = TRUE;
3223 break;
3224#endif
3225 case GO_GREY:
3226 /* make menu's have grey items, ignored here */
3227 break;
3228#ifdef FEAT_TOOLBAR
3229 case GO_TOOLBAR:
3230 using_toolbar = TRUE;
3231 break;
3232#endif
3233#ifdef FEAT_FOOTER
3234 case GO_FOOTER:
3235 using_footer = TRUE;
3236 break;
3237#endif
3238 case GO_TEAROFF:
3239#if defined(FEAT_MENU) && !defined(WIN16)
3240 using_tearoff = TRUE;
3241#endif
3242 break;
3243 default:
3244 /* Ignore options that are not supported */
3245 break;
3246 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (gui.in_use)
3249 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003250 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252
3253#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003254 /* Update the GUI tab line, it may appear or disappear. This may
3255 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003256 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003257 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003258 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003259 /* We don't want a resize event change "Rows" here, save and
3260 * restore it. Resizing is handled below. */
3261 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003262 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003263 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003264 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003265 if (using_tabline)
3266 fix_size = TRUE;
3267 if (!gui_use_tabline())
3268 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3269 }
3270#endif
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 for (i = 0; i < 3; i++)
3273 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003274 /* The scrollbar needs to be updated when it is shown/unshown and
3275 * when switching tab pages. But the size only changes when it's
3276 * shown/unshown. Thus we need two places to remember whether a
3277 * scrollbar is there or not. */
3278 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003279#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003280 || gui.which_scrollbars[i]
3281 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003282#endif
3283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 if (i == SBAR_BOTTOM)
3286 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3287 gui.which_scrollbars[i]);
3288 else
3289 {
3290 FOR_ALL_WINDOWS(wp)
3291 {
3292 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3293 }
3294 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003295 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3296 {
3297 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003298 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003299 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003300 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003301 if (gui.which_scrollbars[i])
3302 fix_size = TRUE;
3303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003305#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003306 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003307#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003308 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310
3311#ifdef FEAT_MENU
3312 if (gui.menu_is_active != prev_menu_is_active)
3313 {
3314 /* We don't want a resize event change "Rows" here, save and
3315 * restore it. Resizing is handled below. */
3316 i = Rows;
3317 gui_mch_enable_menu(gui.menu_is_active);
3318 Rows = i;
3319 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003320 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (gui.menu_is_active)
3322 fix_size = TRUE;
3323 }
3324#endif
3325
3326#ifdef FEAT_TOOLBAR
3327 if (using_toolbar != prev_toolbar)
3328 {
3329 gui_mch_show_toolbar(using_toolbar);
3330 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003331 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (using_toolbar)
3333 fix_size = TRUE;
3334 }
3335#endif
3336#ifdef FEAT_FOOTER
3337 if (using_footer != prev_footer)
3338 {
3339 gui_mch_enable_footer(using_footer);
3340 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003341 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (using_footer)
3343 fix_size = TRUE;
3344 }
3345#endif
3346#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3347 if (using_tearoff != prev_tearoff)
3348 {
3349 gui_mch_toggle_tearoffs(using_tearoff);
3350 prev_tearoff = using_tearoff;
3351 }
3352#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003353 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003354 {
3355#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003356 long prev_Columns = Columns;
3357 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 /* Adjust the size of the window to make the text area keep the
3360 * same size and to avoid that part of our window is off-screen
3361 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003362 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003363
3364#ifdef FEAT_GUI_GTK
3365 /* GTK has the annoying habit of sending us resize events when
3366 * changing the window size ourselves. This mostly happens when
3367 * waiting for a character to arrive, quite unpredictably, and may
3368 * change Columns and Rows when we don't want it. Wait for a
3369 * character here to avoid this effect.
3370 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003371 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003372 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003373 * Don't change Rows when adding menu/toolbar/tabline.
3374 * Don't change Columns when adding vertical toolbar. */
3375 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003376 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003377 if ((need_set_size & RESIZE_VERT) == 0)
3378 Rows = prev_Rows;
3379 if ((need_set_size & RESIZE_HOR) == 0)
3380 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003381#endif
3382 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003383#ifdef FEAT_WINDOWS
3384 /* When the console tabline appears or disappears the window positions
3385 * change. */
3386 if (firstwin->w_winrow != tabline_height())
3387 shell_new_rows(); /* recompute window positions and heights */
3388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390}
3391
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003392#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3393/*
3394 * Return TRUE if the GUI is taking care of the tabline.
3395 * It may still be hidden if 'showtabline' is zero.
3396 */
3397 int
3398gui_use_tabline()
3399{
3400 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3401}
3402
3403/*
3404 * Return TRUE if the GUI is showing the tabline.
3405 * This uses 'showtabline'.
3406 */
3407 static int
3408gui_has_tabline()
3409{
3410 if (!gui_use_tabline()
3411 || p_stal == 0
3412 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3413 return FALSE;
3414 return TRUE;
3415}
3416
3417/*
3418 * Update the tabline.
3419 * This may display/undisplay the tabline and update the labels.
3420 */
3421 void
3422gui_update_tabline()
3423{
3424 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003425 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003426
3427 if (!gui.starting && starting == 0)
3428 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003429 /* Updating the tabline uses direct GUI commands, flush
3430 * outstanding instructions first. (esp. clear screen) */
3431 out_flush();
3432 gui_mch_flush();
3433
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003434 if (!showit != !shown)
3435 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003436 if (showit != 0)
3437 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003438
3439 /* When the tabs change from hidden to shown or from shown to
3440 * hidden the size of the text area should remain the same. */
3441 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003442 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003443 }
3444}
3445
3446/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003447 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003448 */
3449 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003450get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003451 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003452 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003453{
3454 int modified = FALSE;
3455 char_u buf[40];
3456 int wincount;
3457 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003458 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003459
Bram Moolenaar57657d82006-04-21 22:12:41 +00003460 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003461 opt = (tooltip ? &p_gtt : &p_gtl);
3462 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003463 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003464 int use_sandbox = FALSE;
3465 int save_called_emsg = called_emsg;
3466 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003467 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003468 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3469 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003470
3471 called_emsg = FALSE;
3472
3473 printer_page_num = tabpage_index(tp);
3474# ifdef FEAT_EVAL
3475 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003476 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003477# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003478 /* It's almost as going to the tabpage, but without autocommands. */
3479 curtab->tp_firstwin = firstwin;
3480 curtab->tp_lastwin = lastwin;
3481 curtab->tp_curwin = curwin;
3482 save_curtab = curtab;
3483 curtab = tp;
3484 topframe = curtab->tp_topframe;
3485 firstwin = curtab->tp_firstwin;
3486 lastwin = curtab->tp_lastwin;
3487 curwin = curtab->tp_curwin;
3488 curbuf = curwin->w_buffer;
3489
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003490 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003491 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003492 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003493 STRCPY(NameBuff, res);
3494
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003495 /* Back to the original curtab. */
3496 curtab = save_curtab;
3497 topframe = curtab->tp_topframe;
3498 firstwin = curtab->tp_firstwin;
3499 lastwin = curtab->tp_lastwin;
3500 curwin = curtab->tp_curwin;
3501 curbuf = curwin->w_buffer;
3502
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003503 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003504 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003505 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003506 called_emsg |= save_called_emsg;
3507 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003508
3509 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3510 * use a default label. */
3511 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003512 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003513 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003514 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003515 if (!tooltip)
3516 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003517
3518 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3519 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3520 if (bufIsChanged(wp->w_buffer))
3521 modified = TRUE;
3522 if (modified || wincount > 1)
3523 {
3524 if (wincount > 1)
3525 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3526 else
3527 buf[0] = NUL;
3528 if (modified)
3529 STRCAT(buf, "+");
3530 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003531 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003532 mch_memmove(NameBuff, buf, STRLEN(buf));
3533 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003534 }
3535}
3536
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003537/*
3538 * Send the event for clicking to select tab page "nr".
3539 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003540 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003541 */
3542 int
3543send_tabline_event(nr)
3544 int nr;
3545{
3546 char_u string[3];
3547
3548 if (nr == tabpage_index(curtab))
3549 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003550
3551 /* Don't put events in the input queue now. */
3552 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003553# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003554 || cmdwin_type != 0
3555# endif
3556 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003557 {
3558 /* Set it back to the current tab page. */
3559 gui_mch_set_curtab(tabpage_index(curtab));
3560 return FALSE;
3561 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003562
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003563 string[0] = CSI;
3564 string[1] = KS_TABLINE;
3565 string[2] = KE_FILLER;
3566 add_to_input_buf(string, 3);
3567 string[0] = nr;
3568 add_to_input_buf_csi(string, 1);
3569 return TRUE;
3570}
3571
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003572/*
3573 * Send a tabline menu event
3574 */
3575 void
3576send_tabline_menu_event(tabidx, event)
3577 int tabidx;
3578 int event;
3579{
3580 char_u string[3];
3581
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003582 /* Don't put events in the input queue now. */
3583 if (hold_gui_events)
3584 return;
3585
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003586 string[0] = CSI;
3587 string[1] = KS_TABMENU;
3588 string[2] = KE_FILLER;
3589 add_to_input_buf(string, 3);
3590 string[0] = tabidx;
3591 string[1] = (char_u)(long)event;
3592 add_to_input_buf_csi(string, 2);
3593}
3594
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597/*
3598 * Scrollbar stuff:
3599 */
3600
Bram Moolenaare45828b2006-02-15 22:12:56 +00003601#if defined(FEAT_WINDOWS) || defined(PROTO)
3602/*
3603 * Remove all scrollbars. Used before switching to another tab page.
3604 */
3605 void
3606gui_remove_scrollbars()
3607{
3608 int i;
3609 win_T *wp;
3610
3611 for (i = 0; i < 3; i++)
3612 {
3613 if (i == SBAR_BOTTOM)
3614 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3615 else
3616 {
3617 FOR_ALL_WINDOWS(wp)
3618 {
3619 gui_do_scrollbar(wp, i, FALSE);
3620 }
3621 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003622 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003623 }
3624}
3625#endif
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 void
3628gui_create_scrollbar(sb, type, wp)
3629 scrollbar_T *sb;
3630 int type;
3631 win_T *wp;
3632{
3633 static int sbar_ident = 0;
3634
3635 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3636 sb->wp = wp;
3637 sb->type = type;
3638 sb->value = 0;
3639#ifdef FEAT_GUI_ATHENA
3640 sb->pixval = 0;
3641#endif
3642 sb->size = 1;
3643 sb->max = 1;
3644 sb->top = 0;
3645 sb->height = 0;
3646#ifdef FEAT_VERTSPLIT
3647 sb->width = 0;
3648#endif
3649 sb->status_height = 0;
3650 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3651}
3652
3653/*
3654 * Find the scrollbar with the given index.
3655 */
3656 scrollbar_T *
3657gui_find_scrollbar(ident)
3658 long ident;
3659{
3660 win_T *wp;
3661
3662 if (gui.bottom_sbar.ident == ident)
3663 return &gui.bottom_sbar;
3664 FOR_ALL_WINDOWS(wp)
3665 {
3666 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3667 return &wp->w_scrollbars[SBAR_LEFT];
3668 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3669 return &wp->w_scrollbars[SBAR_RIGHT];
3670 }
3671 return NULL;
3672}
3673
3674/*
3675 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3676 *
3677 * For Win32, Macintosh and GTK+ 2:
3678 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3679 * you stop dragging the scrollbar. We get here each time the scrollbar is
3680 * dragged another pixel, but as far as the rest of vim goes, it thinks
3681 * we're just hanging in the call to DispatchMessage() in
3682 * process_message(). The DispatchMessage() call that hangs was passed a
3683 * mouse button click event in the scrollbar window. -- webb.
3684 *
3685 * Solution: Do the scrolling right here. But only when allowed.
3686 * Ignore the scrollbars while executing an external command or when there
3687 * are still characters to be processed.
3688 */
3689 void
3690gui_drag_scrollbar(sb, value, still_dragging)
3691 scrollbar_T *sb;
3692 long value;
3693 int still_dragging;
3694{
3695#ifdef FEAT_WINDOWS
3696 win_T *wp;
3697#endif
3698 int sb_num;
3699#ifdef USE_ON_FLY_SCROLL
3700 colnr_T old_leftcol = curwin->w_leftcol;
3701# ifdef FEAT_SCROLLBIND
3702 linenr_T old_topline = curwin->w_topline;
3703# endif
3704# ifdef FEAT_DIFF
3705 int old_topfill = curwin->w_topfill;
3706# endif
3707#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003708 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 int byte_count;
3710#endif
3711
3712 if (sb == NULL)
3713 return;
3714
3715 /* Don't put events in the input queue now. */
3716 if (hold_gui_events)
3717 return;
3718
3719#ifdef FEAT_CMDWIN
3720 if (cmdwin_type != 0 && sb->wp != curwin)
3721 return;
3722#endif
3723
3724 if (still_dragging)
3725 {
3726 if (sb->wp == NULL)
3727 gui.dragged_sb = SBAR_BOTTOM;
3728 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3729 gui.dragged_sb = SBAR_LEFT;
3730 else
3731 gui.dragged_sb = SBAR_RIGHT;
3732 gui.dragged_wp = sb->wp;
3733 }
3734 else
3735 {
3736 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003737#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 /* Keep the "dragged_wp" value until after the scrolling, for when the
3739 * moust button is released. GTK2 doesn't send the button-up event. */
3740 gui.dragged_wp = NULL;
3741#endif
3742 }
3743
3744 /* Vertical sbar info is kept in the first sbar (the left one) */
3745 if (sb->wp != NULL)
3746 sb = &sb->wp->w_scrollbars[0];
3747
3748 /*
3749 * Check validity of value
3750 */
3751 if (value < 0)
3752 value = 0;
3753#ifdef SCROLL_PAST_END
3754 else if (value > sb->max)
3755 value = sb->max;
3756#else
3757 if (value > sb->max - sb->size + 1)
3758 value = sb->max - sb->size + 1;
3759#endif
3760
3761 sb->value = value;
3762
3763#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003764 /* When not allowed to do the scrolling right now, return.
3765 * This also checked input_available(), but that causes the first click in
3766 * a scrollbar to be ignored when Vim doesn't have focus. */
3767 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return;
3769#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003770#ifdef FEAT_INS_EXPAND
3771 /* Disallow scrolling the current window when the completion popup menu is
3772 * visible. */
3773 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3774 return;
3775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777#ifdef FEAT_RIGHTLEFT
3778 if (sb->wp == NULL && curwin->w_p_rl)
3779 {
3780 value = sb->max + 1 - sb->size - value;
3781 if (value < 0)
3782 value = 0;
3783 }
3784#endif
3785
3786 if (sb->wp != NULL) /* vertical scrollbar */
3787 {
3788 sb_num = 0;
3789#ifdef FEAT_WINDOWS
3790 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3791 sb_num++;
3792 if (wp == NULL)
3793 return;
3794#else
3795 if (sb->wp != curwin)
3796 return;
3797#endif
3798
3799#ifdef USE_ON_FLY_SCROLL
3800 current_scrollbar = sb_num;
3801 scrollbar_value = value;
3802 if (State & NORMAL)
3803 {
3804 gui_do_scroll();
3805 setcursor();
3806 }
3807 else if (State & INSERT)
3808 {
3809 ins_scroll();
3810 setcursor();
3811 }
3812 else if (State & CMDLINE)
3813 {
3814 if (msg_scrolled == 0)
3815 {
3816 gui_do_scroll();
3817 redrawcmdline();
3818 }
3819 }
3820# ifdef FEAT_FOLDING
3821 /* Value may have been changed for closed fold. */
3822 sb->value = sb->wp->w_topline - 1;
3823# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003824
3825 /* When dragging one scrollbar and there is another one at the other
3826 * side move the thumb of that one too. */
3827 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3828 gui_mch_set_scrollbar_thumb(
3829 &sb->wp->w_scrollbars[
3830 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3831 ? SBAR_LEFT : SBAR_RIGHT],
3832 sb->value, sb->size, sb->max);
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834#else
3835 bytes[0] = CSI;
3836 bytes[1] = KS_VER_SCROLLBAR;
3837 bytes[2] = KE_FILLER;
3838 bytes[3] = (char_u)sb_num;
3839 byte_count = 4;
3840#endif
3841 }
3842 else
3843 {
3844#ifdef USE_ON_FLY_SCROLL
3845 scrollbar_value = value;
3846
3847 if (State & NORMAL)
3848 gui_do_horiz_scroll();
3849 else if (State & INSERT)
3850 ins_horscroll();
3851 else if (State & CMDLINE)
3852 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003853 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 gui_do_horiz_scroll();
3856 redrawcmdline();
3857 }
3858 }
3859 if (old_leftcol != curwin->w_leftcol)
3860 {
3861 updateWindow(curwin); /* update window, status and cmdline */
3862 setcursor();
3863 }
3864#else
3865 bytes[0] = CSI;
3866 bytes[1] = KS_HOR_SCROLLBAR;
3867 bytes[2] = KE_FILLER;
3868 byte_count = 3;
3869#endif
3870 }
3871
3872#ifdef USE_ON_FLY_SCROLL
3873# ifdef FEAT_SCROLLBIND
3874 /*
3875 * synchronize other windows, as necessary according to 'scrollbind'
3876 */
3877 if (curwin->w_p_scb
3878 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3879 || (sb->wp == curwin && (curwin->w_topline != old_topline
3880# ifdef FEAT_DIFF
3881 || curwin->w_topfill != old_topfill
3882# endif
3883 ))))
3884 {
3885 do_check_scrollbind(TRUE);
3886 /* need to update the window right here */
3887 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3888 if (wp->w_redr_type > 0)
3889 updateWindow(wp);
3890 setcursor();
3891 }
3892# endif
3893 out_flush();
3894 gui_update_cursor(FALSE, TRUE);
3895#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003896 add_to_input_buf(bytes, byte_count);
3897 add_long_to_buf((long_u)value, bytes);
3898 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899#endif
3900}
3901
3902/*
3903 * Scrollbar stuff:
3904 */
3905
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02003906#if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003907/*
3908 * Called when something in the window layout has changed.
3909 */
3910 void
3911gui_may_update_scrollbars()
3912{
3913 if (gui.in_use && starting == 0)
3914 {
3915 out_flush();
3916 gui_init_which_components(NULL);
3917 gui_update_scrollbars(TRUE);
3918 }
3919 need_mouse_correct = TRUE;
3920}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02003921#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 void
3924gui_update_scrollbars(force)
3925 int force; /* Force all scrollbars to get updated */
3926{
3927 win_T *wp;
3928 scrollbar_T *sb;
3929 long val, size, max; /* need 32 bits here */
3930 int which_sb;
3931 int h, y;
3932#ifdef FEAT_VERTSPLIT
3933 static win_T *prev_curwin = NULL;
3934#endif
3935
3936 /* Update the horizontal scrollbar */
3937 gui_update_horiz_scrollbar(force);
3938
3939#ifndef WIN3264
3940 /* Return straight away if there is neither a left nor right scrollbar.
3941 * On MS-Windows this is required anyway for scrollwheel messages. */
3942 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3943 return;
3944#endif
3945
3946 /*
3947 * Don't want to update a scrollbar while we're dragging it. But if we
3948 * have both a left and right scrollbar, and we drag one of them, we still
3949 * need to update the other one.
3950 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003951 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3952 && gui.which_scrollbars[SBAR_LEFT]
3953 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
3955 /*
3956 * If we have two scrollbars and one of them is being dragged, just
3957 * copy the scrollbar position from the dragged one to the other one.
3958 */
3959 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3960 if (gui.dragged_wp != NULL)
3961 gui_mch_set_scrollbar_thumb(
3962 &gui.dragged_wp->w_scrollbars[which_sb],
3963 gui.dragged_wp->w_scrollbars[0].value,
3964 gui.dragged_wp->w_scrollbars[0].size,
3965 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967
3968 /* avoid that moving components around generates events */
3969 ++hold_gui_events;
3970
3971 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3972 {
3973 if (wp->w_buffer == NULL) /* just in case */
3974 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003975 /* Skip a scrollbar that is being dragged. */
3976 if (!force && (gui.dragged_sb == SBAR_LEFT
3977 || gui.dragged_sb == SBAR_RIGHT)
3978 && gui.dragged_wp == wp)
3979 continue;
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#ifdef SCROLL_PAST_END
3982 max = wp->w_buffer->b_ml.ml_line_count - 1;
3983#else
3984 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3985#endif
3986 if (max < 0) /* empty buffer */
3987 max = 0;
3988 val = wp->w_topline - 1;
3989 size = wp->w_height;
3990#ifdef SCROLL_PAST_END
3991 if (val > max) /* just in case */
3992 val = max;
3993#else
3994 if (size > max + 1) /* just in case */
3995 size = max + 1;
3996 if (val > max - size + 1)
3997 val = max - size + 1;
3998#endif
3999 if (val < 0) /* minimal value is 0 */
4000 val = 0;
4001
4002 /*
4003 * Scrollbar at index 0 (the left one) contains all the information.
4004 * It would be the same info for left and right so we just store it for
4005 * one of them.
4006 */
4007 sb = &wp->w_scrollbars[0];
4008
4009 /*
4010 * Note: no check for valid w_botline. If it's not valid the
4011 * scrollbars will be updated later anyway.
4012 */
4013 if (size < 1 || wp->w_botline - 2 > max)
4014 {
4015 /*
4016 * This can happen during changing files. Just don't update the
4017 * scrollbar for now.
4018 */
4019 sb->height = 0; /* Force update next time */
4020 if (gui.which_scrollbars[SBAR_LEFT])
4021 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4022 if (gui.which_scrollbars[SBAR_RIGHT])
4023 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4024 continue;
4025 }
4026 if (force || sb->height != wp->w_height
4027#ifdef FEAT_WINDOWS
4028 || sb->top != wp->w_winrow
4029 || sb->status_height != wp->w_status_height
4030# ifdef FEAT_VERTSPLIT
4031 || sb->width != wp->w_width
4032 || prev_curwin != curwin
4033# endif
4034#endif
4035 )
4036 {
4037 /* Height, width or position of scrollbar has changed. For
4038 * vertical split: curwin changed. */
4039 sb->height = wp->w_height;
4040#ifdef FEAT_WINDOWS
4041 sb->top = wp->w_winrow;
4042 sb->status_height = wp->w_status_height;
4043# ifdef FEAT_VERTSPLIT
4044 sb->width = wp->w_width;
4045# endif
4046#endif
4047
4048 /* Calculate height and position in pixels */
4049 h = (sb->height + sb->status_height) * gui.char_height;
4050 y = sb->top * gui.char_height + gui.border_offset;
4051#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4052 if (gui.menu_is_active)
4053 y += gui.menu_height;
4054#endif
4055
4056#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4057 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4058# ifdef FEAT_GUI_ATHENA
4059 y += gui.toolbar_height;
4060# else
4061# ifdef FEAT_GUI_MSWIN
4062 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4063# endif
4064# endif
4065#endif
4066
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004067#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4068 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004069 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004070#endif
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#ifdef FEAT_WINDOWS
4073 if (wp->w_winrow == 0)
4074#endif
4075 {
4076 /* Height of top scrollbar includes width of top border */
4077 h += gui.border_offset;
4078 y -= gui.border_offset;
4079 }
4080 if (gui.which_scrollbars[SBAR_LEFT])
4081 {
4082 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4083 gui.left_sbar_x, y,
4084 gui.scrollbar_width, h);
4085 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4086 }
4087 if (gui.which_scrollbars[SBAR_RIGHT])
4088 {
4089 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4090 gui.right_sbar_x, y,
4091 gui.scrollbar_width, h);
4092 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4093 }
4094 }
4095
4096 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4097 * checking if the thumb moved at least a pixel. Only do this for
4098 * Athena, most other GUIs require the update anyway to make the
4099 * arrows work. */
4100#ifdef FEAT_GUI_ATHENA
4101 if (max == 0)
4102 y = 0;
4103 else
4104 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4105 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4106#else
4107 if (force || sb->value != val || sb->size != size || sb->max != max)
4108#endif
4109 {
4110 /* Thumb of scrollbar has moved */
4111 sb->value = val;
4112#ifdef FEAT_GUI_ATHENA
4113 sb->pixval = y;
4114#endif
4115 sb->size = size;
4116 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004117 if (gui.which_scrollbars[SBAR_LEFT]
4118 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4120 val, size, max);
4121 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004122 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4124 val, size, max);
4125 }
4126 }
4127#ifdef FEAT_VERTSPLIT
4128 prev_curwin = curwin;
4129#endif
4130 --hold_gui_events;
4131}
4132
4133/*
4134 * Enable or disable a scrollbar.
4135 * Check for scrollbars for vertically split windows which are not enabled
4136 * sometimes.
4137 */
4138 static void
4139gui_do_scrollbar(wp, which, enable)
4140 win_T *wp;
4141 int which; /* SBAR_LEFT or SBAR_RIGHT */
4142 int enable; /* TRUE to enable scrollbar */
4143{
4144#ifdef FEAT_VERTSPLIT
4145 int midcol = curwin->w_wincol + curwin->w_width / 2;
4146 int has_midcol = (wp->w_wincol <= midcol
4147 && wp->w_wincol + wp->w_width >= midcol);
4148
4149 /* Only enable scrollbars that contain the middle column of the current
4150 * window. */
4151 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4152 {
4153 /* Scrollbars only on one side. Don't enable scrollbars that don't
4154 * contain the middle column of the current window. */
4155 if (!has_midcol)
4156 enable = FALSE;
4157 }
4158 else
4159 {
4160 /* Scrollbars on both sides. Don't enable scrollbars that neither
4161 * contain the middle column of the current window nor are on the far
4162 * side. */
4163 if (midcol > Columns / 2)
4164 {
4165 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4166 enable = FALSE;
4167 }
4168 else
4169 {
4170 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4171 : !has_midcol)
4172 enable = FALSE;
4173 }
4174 }
4175#endif
4176 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4177}
4178
4179/*
4180 * Scroll a window according to the values set in the globals current_scrollbar
4181 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4182 * or FALSE otherwise.
4183 */
4184 int
4185gui_do_scroll()
4186{
4187 win_T *wp, *save_wp;
4188 int i;
4189 long nlines;
4190 pos_T old_cursor;
4191 linenr_T old_topline;
4192#ifdef FEAT_DIFF
4193 int old_topfill;
4194#endif
4195
4196 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4197 if (wp == NULL)
4198 break;
4199 if (wp == NULL)
4200 /* Couldn't find window */
4201 return FALSE;
4202
4203 /*
4204 * Compute number of lines to scroll. If zero, nothing to do.
4205 */
4206 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4207 if (nlines == 0)
4208 return FALSE;
4209
4210 save_wp = curwin;
4211 old_topline = wp->w_topline;
4212#ifdef FEAT_DIFF
4213 old_topfill = wp->w_topfill;
4214#endif
4215 old_cursor = wp->w_cursor;
4216 curwin = wp;
4217 curbuf = wp->w_buffer;
4218 if (nlines < 0)
4219 scrolldown(-nlines, gui.dragged_wp == NULL);
4220 else
4221 scrollup(nlines, gui.dragged_wp == NULL);
4222 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4223 * the mouse-up event already, but we still want it to behave like when
4224 * dragging. But not the next click in an arrow. */
4225 if (gui.dragged_sb == SBAR_NONE)
4226 gui.dragged_wp = NULL;
4227
4228 if (old_topline != wp->w_topline
4229#ifdef FEAT_DIFF
4230 || old_topfill != wp->w_topfill
4231#endif
4232 )
4233 {
4234 if (p_so != 0)
4235 {
4236 cursor_correct(); /* fix window for 'so' */
4237 update_topline(); /* avoid up/down jump */
4238 }
4239 if (old_cursor.lnum != wp->w_cursor.lnum)
4240 coladvance(wp->w_curswant);
4241#ifdef FEAT_SCROLLBIND
4242 wp->w_scbind_pos = wp->w_topline;
4243#endif
4244 }
4245
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004246 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4247 validate_cursor();
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 curwin = save_wp;
4250 curbuf = save_wp->w_buffer;
4251
4252 /*
4253 * Don't call updateWindow() when nothing has changed (it will overwrite
4254 * the status line!).
4255 */
4256 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004257 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#ifdef FEAT_DIFF
4259 || old_topfill != wp->w_topfill
4260#endif
4261 )
4262 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004263 int type = VALID;
4264
4265#ifdef FEAT_INS_EXPAND
4266 if (pum_visible())
4267 {
4268 type = NOT_VALID;
4269 wp->w_lines_valid = 0;
4270 }
4271#endif
4272 /* Don't set must_redraw here, it may cause the popup menu to
4273 * disappear when losing focus after a scrollbar drag. */
4274 if (wp->w_redr_type < type)
4275 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 updateWindow(wp); /* update window, status line, and cmdline */
4277 }
4278
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004279#ifdef FEAT_INS_EXPAND
4280 /* May need to redraw the popup menu. */
4281 if (pum_visible())
4282 pum_redraw();
4283#endif
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4286}
4287
4288
4289/*
4290 * Horizontal scrollbar stuff:
4291 */
4292
4293/*
4294 * Return length of line "lnum" for horizontal scrolling.
4295 */
4296 static colnr_T
4297scroll_line_len(lnum)
4298 linenr_T lnum;
4299{
4300 char_u *p;
4301 colnr_T col;
4302 int w;
4303
4304 p = ml_get(lnum);
4305 col = 0;
4306 if (*p != NUL)
4307 for (;;)
4308 {
4309 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004310 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (*p == NUL) /* don't count the last character */
4312 break;
4313 col += w;
4314 }
4315 return col;
4316}
4317
4318/* Remember which line is currently the longest, so that we don't have to
4319 * search for it when scrolling horizontally. */
4320static linenr_T longest_lnum = 0;
4321
4322 static void
4323gui_update_horiz_scrollbar(force)
4324 int force;
4325{
4326 long value, size, max; /* need 32 bit ints here */
4327
4328 if (!gui.which_scrollbars[SBAR_BOTTOM])
4329 return;
4330
4331 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4332 return;
4333
4334 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4335 return;
4336
4337 /*
4338 * It is possible for the cursor to be invalid if we're in the middle of
4339 * something (like changing files). If so, don't do anything for now.
4340 */
4341 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4342 {
4343 gui.bottom_sbar.value = -1;
4344 return;
4345 }
4346
4347 size = W_WIDTH(curwin);
4348 if (curwin->w_p_wrap)
4349 {
4350 value = 0;
4351#ifdef SCROLL_PAST_END
4352 max = 0;
4353#else
4354 max = W_WIDTH(curwin) - 1;
4355#endif
4356 }
4357 else
4358 {
4359 value = curwin->w_leftcol;
4360
4361 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4362 * line numbers, topline and botline can be invalid when displaying is
4363 * postponed. */
4364 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4365 && curwin->w_topline <= curwin->w_cursor.lnum
4366 && curwin->w_botline > curwin->w_cursor.lnum
4367 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4368 {
4369 linenr_T lnum;
4370 colnr_T n;
4371
4372 /* Use maximum of all visible lines. Remember the lnum of the
4373 * longest line, clostest to the cursor line. Used when scrolling
4374 * below. */
4375 max = 0;
4376 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4377 {
4378 n = scroll_line_len(lnum);
4379 if (n > (colnr_T)max)
4380 {
4381 max = n;
4382 longest_lnum = lnum;
4383 }
4384 else if (n == (colnr_T)max
4385 && abs((int)(lnum - curwin->w_cursor.lnum))
4386 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4387 longest_lnum = lnum;
4388 }
4389 }
4390 else
4391 /* Use cursor line only. */
4392 max = scroll_line_len(curwin->w_cursor.lnum);
4393#ifdef FEAT_VIRTUALEDIT
4394 if (virtual_active())
4395 {
4396 /* May move the cursor even further to the right. */
4397 if (curwin->w_virtcol >= (colnr_T)max)
4398 max = curwin->w_virtcol;
4399 }
4400#endif
4401
4402#ifndef SCROLL_PAST_END
4403 max += W_WIDTH(curwin) - 1;
4404#endif
4405 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004406 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 size -= curwin_col_off();
4408#ifndef SCROLL_PAST_END
4409 max -= curwin_col_off();
4410#endif
4411 }
4412
4413#ifndef SCROLL_PAST_END
4414 if (value > max - size + 1)
4415 value = max - size + 1; /* limit the value to allowable range */
4416#endif
4417
4418#ifdef FEAT_RIGHTLEFT
4419 if (curwin->w_p_rl)
4420 {
4421 value = max + 1 - size - value;
4422 if (value < 0)
4423 {
4424 size += value;
4425 value = 0;
4426 }
4427 }
4428#endif
4429 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4430 && max == gui.bottom_sbar.max)
4431 return;
4432
4433 gui.bottom_sbar.value = value;
4434 gui.bottom_sbar.size = size;
4435 gui.bottom_sbar.max = max;
4436 gui.prev_wrap = curwin->w_p_wrap;
4437
4438 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4439}
4440
4441/*
4442 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4443 */
4444 int
4445gui_do_horiz_scroll()
4446{
4447 /* no wrapping, no scrolling */
4448 if (curwin->w_p_wrap)
4449 return FALSE;
4450
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004451 if ((long_u)curwin->w_leftcol == scrollbar_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 return FALSE;
4453
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004454 curwin->w_leftcol = (colnr_T)scrollbar_value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 /* When the line of the cursor is too short, move the cursor to the
4457 * longest visible line. Do a sanity check on "longest_lnum", just in
4458 * case. */
4459 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4460 && longest_lnum >= curwin->w_topline
4461 && longest_lnum < curwin->w_botline
4462 && !virtual_active())
4463 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004464 if (scrollbar_value > (long_u)scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
4466 curwin->w_cursor.lnum = longest_lnum;
4467 curwin->w_cursor.col = 0;
4468 }
4469 }
4470
4471 return leftcol_changed();
4472}
4473
4474/*
4475 * Check that none of the colors are the same as the background color
4476 */
4477 void
4478gui_check_colors()
4479{
4480 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4481 {
4482 gui_set_bg_color((char_u *)"White");
4483 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4484 gui_set_fg_color((char_u *)"Black");
4485 }
4486}
4487
Bram Moolenaar3918c952005-03-15 22:34:55 +00004488 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489gui_set_fg_color(name)
4490 char_u *name;
4491{
4492 gui.norm_pixel = gui_get_color(name);
4493 hl_set_fg_color_name(vim_strsave(name));
4494}
4495
Bram Moolenaar3918c952005-03-15 22:34:55 +00004496 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497gui_set_bg_color(name)
4498 char_u *name;
4499{
4500 gui.back_pixel = gui_get_color(name);
4501 hl_set_bg_color_name(vim_strsave(name));
4502}
4503
4504/*
4505 * Allocate a color by name.
4506 * Returns INVALCOLOR and gives an error message when failed.
4507 */
4508 guicolor_T
4509gui_get_color(name)
4510 char_u *name;
4511{
4512 guicolor_T t;
4513
4514 if (*name == NUL)
4515 return INVALCOLOR;
4516 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004519#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 && gui.in_use
4521#endif
4522 )
4523 EMSG2(_("E254: Cannot allocate color %s"), name);
4524 return t;
4525}
4526
4527/*
4528 * Return the grey value of a color (range 0-255).
4529 */
4530 int
4531gui_get_lightness(pixel)
4532 guicolor_T pixel;
4533{
4534 long_u rgb = gui_mch_get_rgb(pixel);
4535
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004536 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004537 + (((rgb >> 8) & 0xff) * 587)
4538 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
4541#if defined(FEAT_GUI_X11) || defined(PROTO)
4542 void
4543gui_new_scrollbar_colors()
4544{
4545 win_T *wp;
4546
4547 /* Nothing to do if GUI hasn't started yet. */
4548 if (!gui.in_use)
4549 return;
4550
4551 FOR_ALL_WINDOWS(wp)
4552 {
4553 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4554 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4555 }
4556 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4557}
4558#endif
4559
4560/*
4561 * Call this when focus has changed.
4562 */
4563 void
4564gui_focus_change(in_focus)
4565 int in_focus;
4566{
4567/*
4568 * Skip this code to avoid drawing the cursor when debugging and switching
4569 * between the debugger window and gvim.
4570 */
4571#if 1
4572 gui.in_focus = in_focus;
4573 out_flush(); /* make sure output has been written */
4574 gui_update_cursor(TRUE, FALSE);
4575
4576# ifdef FEAT_XIM
4577 xim_set_focus(in_focus);
4578# endif
4579
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004580 /* Put events in the input queue only when allowed.
4581 * ui_focus_change() isn't called directly, because it invokes
4582 * autocommands and that must not happen asynchronously. */
4583 if (!hold_gui_events)
4584 {
4585 char_u bytes[3];
4586
4587 bytes[0] = CSI;
4588 bytes[1] = KS_EXTRA;
4589 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4590 add_to_input_buf(bytes, 3);
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#endif
4593}
4594
4595/*
4596 * Called when the mouse moved (but not when dragging).
4597 */
4598 void
4599gui_mouse_moved(x, y)
4600 int x;
4601 int y;
4602{
4603 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004604 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
Bram Moolenaard3667a22006-03-16 21:35:52 +00004606 /* Ignore this while still starting up. */
4607 if (!gui.in_use || gui.starting)
4608 return;
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#ifdef FEAT_MOUSESHAPE
4611 /* Get window pointer, and update mouse shape as well. */
4612 wp = xy2win(x, y);
4613#endif
4614
4615 /* Only handle this when 'mousefocus' set and ... */
4616 if (p_mousef
4617 && !hold_gui_events /* not holding events */
4618 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4619 && State != HITRETURN /* but not hit-return prompt */
4620 && msg_scrolled == 0 /* no scrolled message */
4621 && !need_mouse_correct /* not moving the pointer */
4622 && gui.in_focus) /* gvim in focus */
4623 {
4624 /* Don't move the mouse when it's left or right of the Vim window */
4625 if (x < 0 || x > Columns * gui.char_width)
4626 return;
4627#ifndef FEAT_MOUSESHAPE
4628 wp = xy2win(x, y);
4629#endif
4630 if (wp == curwin || wp == NULL)
4631 return; /* still in the same old window, or none at all */
4632
Bram Moolenaar9c102382006-05-03 21:26:49 +00004633#ifdef FEAT_WINDOWS
4634 /* Ignore position in the tab pages line. */
4635 if (Y_2_ROW(y) < tabline_height())
4636 return;
4637#endif
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /*
4640 * format a mouse click on status line input
4641 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004642 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4643 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 */
4645 if (finish_op)
4646 {
4647 /* abort the current operator first */
4648 st[0] = ESC;
4649 add_to_input_buf(st, 1);
4650 }
4651 st[0] = CSI;
4652 st[1] = KS_MOUSE;
4653 st[2] = KE_FILLER;
4654 st[3] = (char_u)MOUSE_LEFT;
4655 fill_mouse_coord(st + 4,
4656#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004657 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#else
4659 -1,
4660#endif
4661 wp->w_height + W_WINROW(wp));
4662
4663 add_to_input_buf(st, 8);
4664 st[3] = (char_u)MOUSE_RELEASE;
4665 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#ifdef FEAT_GUI_GTK
4667 /* Need to wake up the main loop */
4668 if (gtk_main_level() > 0)
4669 gtk_main_quit();
4670#endif
4671 }
4672}
4673
4674/*
4675 * Called when mouse should be moved to window with focus.
4676 */
4677 void
4678gui_mouse_correct()
4679{
4680 int x, y;
4681 win_T *wp = NULL;
4682
4683 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004684
4685 if (!(gui.in_use && p_mousef))
4686 return;
4687
4688 gui_mch_getmouse(&x, &y);
4689 /* Don't move the mouse when it's left or right of the Vim window */
4690 if (x < 0 || x > Columns * gui.char_width)
4691 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004692 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004693# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004694 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004695# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004696 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004697 wp = xy2win(x, y);
4698 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004700 validate_cline_row();
4701 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4702 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705}
4706
4707/*
4708 * Find window where the mouse pointer "y" coordinate is in.
4709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 static win_T *
4711xy2win(x, y)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004712 int x UNUSED;
4713 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715#ifdef FEAT_WINDOWS
4716 int row;
4717 int col;
4718 win_T *wp;
4719
4720 row = Y_2_ROW(y);
4721 col = X_2_COL(x);
4722 if (row < 0 || col < 0) /* before first window */
4723 return NULL;
4724 wp = mouse_find_win(&row, &col);
4725# ifdef FEAT_MOUSESHAPE
4726 if (State == HITRETURN || State == ASKMORE)
4727 {
4728 if (Y_2_ROW(y) >= msg_row)
4729 update_mouseshape(SHAPE_IDX_MOREL);
4730 else
4731 update_mouseshape(SHAPE_IDX_MORE);
4732 }
4733 else if (row > wp->w_height) /* below status line */
4734 update_mouseshape(SHAPE_IDX_CLINE);
4735# ifdef FEAT_VERTSPLIT
4736 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004737 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 update_mouseshape(SHAPE_IDX_VSEP);
4739# endif
4740 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004741 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 update_mouseshape(SHAPE_IDX_STATUS);
4743 else
4744 update_mouseshape(-2);
4745# endif
4746 return wp;
4747#else
4748 return firstwin;
4749#endif
4750}
4751
4752/*
4753 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4754 * File names may be given to redefine the args list.
4755 */
4756 void
4757ex_gui(eap)
4758 exarg_T *eap;
4759{
4760 char_u *arg = eap->arg;
4761
4762 /*
4763 * Check for "-f" argument: foreground, don't fork.
4764 * Also don't fork when started with "gvim -f".
4765 * Do fork when using "gui -b".
4766 */
4767 if (arg[0] == '-'
4768 && (arg[1] == 'f' || arg[1] == 'b')
4769 && (arg[2] == NUL || vim_iswhite(arg[2])))
4770 {
4771 gui.dofork = (arg[1] == 'b');
4772 eap->arg = skipwhite(eap->arg + 2);
4773 }
4774 if (!gui.in_use)
4775 {
4776 /* Clear the command. Needed for when forking+exiting, to avoid part
4777 * of the argument ending up after the shell prompt. */
4778 msg_clr_eos_force();
4779 gui_start();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004780#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004781 netbeans_gui_register();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 if (!ends_excmd(*eap->arg))
4785 ex_next(eap);
4786}
4787
4788#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004789 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790/*
4791 * This is shared between Athena, Motif and GTK.
4792 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004793static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795/*
4796 * Callback function for do_in_runtimepath().
4797 */
4798 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004801 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004803 char_u *gfp_buffer = cookie;
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (STRLEN(fname) >= MAXPATHL)
4806 *gfp_buffer = NUL;
4807 else
4808 STRCPY(gfp_buffer, fname);
4809}
4810
4811/*
4812 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4813 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4814 */
4815 int
4816gui_find_bitmap(name, buffer, ext)
4817 char_u *name;
4818 char_u *buffer;
4819 char *ext;
4820{
4821 if (STRLEN(name) > MAXPATHL - 14)
4822 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004823 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004824 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4825 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 return FAIL;
4827 return OK;
4828}
4829
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004830# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831/*
4832 * Given the name of the "icon=" argument, try finding the bitmap file for the
4833 * icon. If it is an absolute path name, use it as it is. Otherwise append
4834 * "ext" and search for it in 'runtimepath'.
4835 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4836 * contains "name".
4837 */
4838 void
4839gui_find_iconfile(name, buffer, ext)
4840 char_u *name;
4841 char_u *buffer;
4842 char *ext;
4843{
4844 char_u buf[MAXPATHL + 1];
4845
4846 expand_env(name, buffer, MAXPATHL);
4847 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4848 STRCPY(buffer, buf);
4849}
4850# endif
4851#endif
4852
Bram Moolenaar9372a112005-12-06 19:59:18 +00004853#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 void
4855display_errors()
4856{
4857 char_u *p;
4858
4859 if (isatty(2))
4860 fflush(stderr);
4861 else if (error_ga.ga_data != NULL)
4862 {
4863 /* avoid putting up a message box with blanks only */
4864 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4865 if (!isspace(*p))
4866 {
4867 /* Truncate a very long message, it will go off-screen. */
4868 if (STRLEN(p) > 2000)
4869 STRCPY(p + 2000 - 14, "...(truncated)");
4870 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4871 p, (char_u *)_("&Ok"), 1, NULL);
4872 break;
4873 }
4874 ga_clear(&error_ga);
4875 }
4876}
4877#endif
4878
4879#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4880/*
4881 * Return TRUE if still starting up and there is no place to enter text.
4882 * For GTK and X11 we check if stderr is not a tty, which means we were
4883 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4884 * allow typing on stdin.
4885 */
4886 int
4887no_console_input()
4888{
4889 return ((!gui.in_use || gui.starting)
4890# ifndef NO_CONSOLE
4891 && !isatty(0) && !isatty(2)
4892# endif
4893 );
4894}
4895#endif
4896
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004897#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00004898 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004899 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900/*
4901 * Update the current window and the screen.
4902 */
4903 void
4904gui_update_screen()
4905{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004906#ifdef FEAT_CONCEAL
4907 linenr_T conceal_old_cursor_line = 0;
4908 linenr_T conceal_new_cursor_line = 0;
4909 int conceal_update_lines = FALSE;
4910#endif
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 update_topline();
4913 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004914
4915#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaarec80df72008-05-07 19:46:51 +00004916 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004917 if (!finish_op && (
4918# ifdef FEAT_AUTOCMD
4919 has_cursormoved()
4920# endif
4921# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
4922 ||
4923# endif
4924# ifdef FEAT_CONCEAL
4925 curwin->w_p_conceal
4926# endif
4927 )
4928 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00004929 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004930# ifdef FEAT_AUTOCMD
4931 if (has_cursormoved())
4932 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4933# endif
4934# ifdef FEAT_CONCEAL
4935 if (curwin->w_p_conceal)
4936 {
4937 conceal_old_cursor_line = last_cursormoved.lnum;
4938 conceal_new_cursor_line = curwin->w_cursor.lnum;
4939 conceal_update_lines = TRUE;
4940 }
4941# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00004942 last_cursormoved = curwin->w_cursor;
4943 }
4944#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004945
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 update_screen(0); /* may need to update the screen */
4947 setcursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004948# if defined(FEAT_CONCEAL)
4949 if (conceal_update_lines
4950 && conceal_old_cursor_line != conceal_new_cursor_line)
4951 {
4952 update_single_line(curwin, conceal_old_cursor_line);
4953 update_single_line(curwin, conceal_new_cursor_line);
4954 curwin->w_valid &= ~VALID_CROW;
4955 }
4956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 out_flush(); /* make sure output has been written */
4958 gui_update_cursor(TRUE, FALSE);
4959 gui_mch_flush();
4960}
4961#endif
4962
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004963#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4965
4966/*
4967 * Get the text to use in a find/replace dialog. Uses the last search pattern
4968 * if the argument is empty.
4969 * Returns an allocated string.
4970 */
4971 char_u *
4972get_find_dialog_text(arg, wwordp, mcasep)
4973 char_u *arg;
4974 int *wwordp; /* return: TRUE if \< \> found */
4975 int *mcasep; /* return: TRUE if \C found */
4976{
4977 char_u *text;
4978
4979 if (*arg == NUL)
4980 text = last_search_pat();
4981 else
4982 text = arg;
4983 if (text != NULL)
4984 {
4985 text = vim_strsave(text);
4986 if (text != NULL)
4987 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004988 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int i;
4990
4991 /* Remove "\V" */
4992 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4993 {
4994 mch_memmove(text, text + 2, (size_t)(len - 1));
4995 len -= 2;
4996 }
4997
4998 /* Recognize "\c" and "\C" and remove. */
4999 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5000 {
5001 *mcasep = (text[1] == 'C');
5002 mch_memmove(text, text + 2, (size_t)(len - 1));
5003 len -= 2;
5004 }
5005
5006 /* Recognize "\<text\>" and remove. */
5007 if (len >= 4
5008 && STRNCMP(text, "\\<", 2) == 0
5009 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5010 {
5011 *wwordp = TRUE;
5012 mch_memmove(text, text + 2, (size_t)(len - 4));
5013 text[len - 4] = NUL;
5014 }
5015
5016 /* Recognize "\/" or "\?" and remove. */
5017 for (i = 0; i + 1 < len; ++i)
5018 if (text[i] == '\\' && (text[i + 1] == '/'
5019 || text[i + 1] == '?'))
5020 {
5021 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5022 --len;
5023 }
5024 }
5025 }
5026 return text;
5027}
5028
5029/*
5030 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
5031 */
5032 static void
5033concat_esc(gap, text, what)
5034 garray_T *gap;
5035 char_u *text;
5036 int what;
5037{
5038 while (*text != NUL)
5039 {
5040#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005041 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (l > 1)
5044 {
5045 while (--l >= 0)
5046 ga_append(gap, *text++);
5047 continue;
5048 }
5049#endif
5050 if (*text == what)
5051 ga_append(gap, '\\');
5052 ga_append(gap, *text);
5053 ++text;
5054 }
5055}
5056
5057/*
5058 * Handle the press of a button in the find-replace dialog.
5059 * Return TRUE when something was added to the input buffer.
5060 */
5061 int
5062gui_do_findrepl(flags, find_text, repl_text, down)
5063 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5064 char_u *find_text;
5065 char_u *repl_text;
5066 int down; /* Search downwards. */
5067{
5068 garray_T ga;
5069 int i;
5070 int type = (flags & FRD_TYPE_MASK);
5071 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005072 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005073 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005074 static int busy = FALSE;
5075
5076 /* When the screen is being updated we should not change buffers and
5077 * windows structures, it may cause freed memory to be used. Also don't
5078 * do this recursively (pressing "Find" quickly several times. */
5079 if (updating_screen || busy)
5080 return FALSE;
5081
5082 /* refuse replace when text cannot be changed */
5083 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5084 return FALSE;
5085
5086 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005089 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 ga_concat(&ga, (char_u *)"%s/");
5091
5092 ga_concat(&ga, (char_u *)"\\V");
5093 if (flags & FRD_MATCH_CASE)
5094 ga_concat(&ga, (char_u *)"\\C");
5095 else
5096 ga_concat(&ga, (char_u *)"\\c");
5097 if (flags & FRD_WHOLE_WORD)
5098 ga_concat(&ga, (char_u *)"\\<");
5099 if (type == FRD_REPLACEALL || down)
5100 concat_esc(&ga, find_text, '/'); /* escape slashes */
5101 else
5102 concat_esc(&ga, find_text, '?'); /* escape '?' */
5103 if (flags & FRD_WHOLE_WORD)
5104 ga_concat(&ga, (char_u *)"\\>");
5105
5106 if (type == FRD_REPLACEALL)
5107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005109 /* escape / and \ */
5110 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5111 if (p != NULL)
5112 ga_concat(&ga, p);
5113 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005115 }
5116 ga_append(&ga, NUL);
5117
5118 if (type == FRD_REPLACE)
5119 {
5120 /* Do the replacement when the text at the cursor matches. Thus no
5121 * replacement is done if the cursor was moved! */
5122 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5123 regmatch.rm_ic = 0;
5124 if (regmatch.regprog != NULL)
5125 {
5126 p = ml_get_cursor();
5127 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5128 && regmatch.startp[0] == p)
5129 {
5130 /* Clear the command line to remove any old "No match"
5131 * error. */
5132 msg_end_prompt();
5133
5134 if (u_save_cursor() == OK)
5135 {
5136 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005137 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005138
5139 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005140 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005141 ins_str(repl_text);
5142 }
5143 }
5144 else
5145 MSG(_("No match at cursor, finding next"));
5146 vim_free(regmatch.regprog);
5147 }
5148 }
5149
5150 if (type == FRD_REPLACEALL)
5151 {
5152 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005153 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 do_cmdline_cmd(ga.ga_data);
5155 }
5156 else
5157 {
5158 /* Search for the next match. */
5159 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005161 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 msg_scroll = i; /* don't let an error message set msg_scroll */
5163 }
5164
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005165 /* Don't want to pass did_emsg to other code, it may cause disabling
5166 * syntax HL if we were busy redrawing. */
5167 did_emsg = save_did_emsg;
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (State & (NORMAL | INSERT))
5170 {
5171 gui_update_screen(); /* update the screen */
5172 msg_didout = 0; /* overwrite any message */
5173 need_wait_return = FALSE; /* don't wait for return */
5174 }
5175
5176 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005177 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 return (ga.ga_len > 0);
5179}
5180
5181#endif
5182
5183#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5184 || defined(FEAT_GUI_MSWIN) \
5185 || defined(FEAT_GUI_MAC) \
5186 || defined(PROTO)
5187
5188#ifdef FEAT_WINDOWS
5189static void gui_wingoto_xy __ARGS((int x, int y));
5190
5191/*
5192 * Jump to the window at specified point (x, y).
5193 */
5194 static void
5195gui_wingoto_xy(x, y)
5196 int x;
5197 int y;
5198{
5199 int row = Y_2_ROW(y);
5200 int col = X_2_COL(x);
5201 win_T *wp;
5202
5203 if (row >= 0 && col >= 0)
5204 {
5205 wp = mouse_find_win(&row, &col);
5206 if (wp != NULL && wp != curwin)
5207 win_goto(wp);
5208 }
5209}
5210#endif
5211
5212/*
5213 * Process file drop. Mouse cursor position, key modifiers, name of files
5214 * and count of files are given. Argument "fnames[count]" has full pathnames
5215 * of dropped files, they will be freed in this function, and caller can't use
5216 * fnames after call this function.
5217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 void
5219gui_handle_drop(x, y, modifiers, fnames, count)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00005220 int x UNUSED;
5221 int y UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 int_u modifiers;
5223 char_u **fnames;
5224 int count;
5225{
5226 int i;
5227 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005228 static int entered = FALSE;
5229
5230 /*
5231 * This function is called by event handlers. Just in case we get a
5232 * second event before the first one is handled, ignore the second one.
5233 * Not sure if this can ever happen, just in case.
5234 */
5235 if (entered)
5236 return;
5237 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 /*
5240 * When the cursor is at the command line, add the file names to the
5241 * command line, don't edit the files.
5242 */
5243 if (State & CMDLINE)
5244 {
5245 shorten_filenames(fnames, count);
5246 for (i = 0; i < count; ++i)
5247 {
5248 if (fnames[i] != NULL)
5249 {
5250 if (i > 0)
5251 add_to_input_buf((char_u*)" ", 1);
5252
5253 /* We don't know what command is used thus we can't be sure
5254 * about which characters need to be escaped. Only escape the
5255 * most common ones. */
5256# ifdef BACKSLASH_IN_FILENAME
5257 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5258# else
5259 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5260# endif
5261 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005262 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 vim_free(p);
5264 vim_free(fnames[i]);
5265 }
5266 }
5267 vim_free(fnames);
5268 }
5269 else
5270 {
5271 /* Go to the window under mouse cursor, then shorten given "fnames" by
5272 * current window, because a window can have local current dir. */
5273# ifdef FEAT_WINDOWS
5274 gui_wingoto_xy(x, y);
5275# endif
5276 shorten_filenames(fnames, count);
5277
5278 /* If Shift held down, remember the first item. */
5279 if ((modifiers & MOUSE_SHIFT) != 0)
5280 p = vim_strsave(fnames[0]);
5281 else
5282 p = NULL;
5283
5284 /* Handle the drop, :edit or :split to get to the file. This also
5285 * frees fnames[]. Skip this if there is only one item it's a
5286 * directory and Shift is held down. */
5287 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5288 && mch_isdir(fnames[0]))
5289 {
5290 vim_free(fnames[0]);
5291 vim_free(fnames);
5292 }
5293 else
5294 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5295
5296 /* If Shift held down, change to first file's directory. If the first
5297 * item is a directory, change to that directory (and let the explorer
5298 * plugin show the contents). */
5299 if (p != NULL)
5300 {
5301 if (mch_isdir(p))
5302 {
5303 if (mch_chdir((char *)p) == 0)
5304 shorten_fnames(TRUE);
5305 }
5306 else if (vim_chdirfile(p) == OK)
5307 shorten_fnames(TRUE);
5308 vim_free(p);
5309 }
5310
5311 /* Update the screen display */
5312 update_screen(NOT_VALID);
5313# ifdef FEAT_MENU
5314 gui_update_menus(0);
5315# endif
Bram Moolenaarca7e1f22010-05-22 15:50:12 +02005316#ifdef FEAT_TITLE
5317 maketitle();
5318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 setcursor();
5320 out_flush();
5321 gui_update_cursor(FALSE, FALSE);
5322 gui_mch_flush();
5323 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005324
5325 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327#endif