blob: 09c3027cb1dcd3029d862c5f577470639c0dcce4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
16#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
17static void set_guifontwide __ARGS((char_u *font_name));
18#endif
19static void gui_check_pos __ARGS((void));
20static void gui_position_components __ARGS((int));
21static void gui_outstr __ARGS((char_u *, int));
22static int gui_screenchar __ARGS((int off, int flags, guicolor_T fg, guicolor_T bg, int back));
23#ifdef HAVE_GTK2
24static int gui_screenstr __ARGS((int off, int len, int flags, guicolor_T fg, guicolor_T bg, int back));
25#endif
26static void gui_delete_lines __ARGS((int row, int count));
27static void gui_insert_lines __ARGS((int row, int count));
28static void fill_mouse_coord __ARGS((char_u *p, int col, int row));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
30static int gui_has_tabline __ARGS((void));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static void gui_do_scrollbar __ARGS((win_T *wp, int which, int enable));
33static colnr_T scroll_line_len __ARGS((linenr_T lnum));
34static void gui_update_horiz_scrollbar __ARGS((int));
Bram Moolenaar3918c952005-03-15 22:34:55 +000035static void gui_set_fg_color __ARGS((char_u *name));
36static void gui_set_bg_color __ARGS((char_u *name));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037static win_T *xy2win __ARGS((int x, int y));
38
39static int can_update_cursor = TRUE; /* can display the cursor */
40
41/*
42 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
43 * this makes the thumb indicate the part of the text that is shown. Motif
44 * can't do this.
45 */
46#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
47# define SCROLL_PAST_END
48#endif
49
50/*
51 * gui_start -- Called when user wants to start the GUI.
52 *
53 * Careful: This function can be called recursively when there is a ":gui"
54 * command in the .gvimrc file. Only the first call should fork, not the
55 * recursive call.
56 */
57 void
58gui_start()
59{
60 char_u *old_term;
61#if defined(UNIX) && !defined(__BEOS__) && !defined(MACOS_X)
62# define MAY_FORK
63 int dofork = TRUE;
64#endif
65 static int recursive = 0;
66
67 old_term = vim_strsave(T_NAME);
68
69 /*
70 * Set_termname() will call gui_init() to start the GUI.
71 * Set the "starting" flag, to indicate that the GUI will start.
72 *
73 * We don't want to open the GUI shell until after we've read .gvimrc,
74 * otherwise we don't know what font we will use, and hence we don't know
75 * what size the shell should be. So if there are errors in the .gvimrc
76 * file, they will have to go to the terminal: Set full_screen to FALSE.
77 * full_screen will be set to TRUE again by a successful termcapinit().
78 */
79 settmode(TMODE_COOK); /* stop RAW mode */
80 if (full_screen)
81 cursor_on(); /* needed for ":gui" in .vimrc */
82 gui.starting = TRUE;
83 full_screen = FALSE;
84
85#ifdef MAY_FORK
86 if (!gui.dofork || vim_strchr(p_go, GO_FORG) || recursive)
87 dofork = FALSE;
88#endif
89 ++recursive;
90
91 termcapinit((char_u *)"builtin_gui");
92 gui.starting = recursive - 1;
93
94 if (!gui.in_use) /* failed to start GUI */
95 {
96 termcapinit(old_term); /* back to old term settings */
97 settmode(TMODE_RAW); /* restart RAW mode */
98#ifdef FEAT_TITLE
99 set_title_defaults(); /* set 'title' and 'icon' again */
100#endif
101 }
102
103 vim_free(old_term);
104
Bram Moolenaar9372a112005-12-06 19:59:18 +0000105#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 if (gui.in_use)
107 /* Display error messages in a dialog now. */
108 display_errors();
109#endif
110
111#if defined(MAY_FORK) && !defined(__QNXNTO__)
112 /*
113 * Quit the current process and continue in the child.
114 * Makes "gvim file" disconnect from the shell it was started in.
115 * Don't do this when Vim was started with "-f" or the 'f' flag is present
116 * in 'guioptions'.
117 */
118 if (gui.in_use && dofork)
119 {
120 int pipefd[2]; /* pipe between parent and child */
121 int pipe_error;
122 char dummy;
123 pid_t pid = -1;
124
125 /* Setup a pipe between the child and the parent, so that the parent
126 * knows when the child has done the setsid() call and is allowed to
127 * exit. */
128 pipe_error = (pipe(pipefd) < 0);
129 pid = fork();
130 if (pid > 0) /* Parent */
131 {
132 /* Give the child some time to do the setsid(), otherwise the
133 * exit() may kill the child too (when starting gvim from inside a
134 * gvim). */
135 if (pipe_error)
136 ui_delay(300L, TRUE);
137 else
138 {
139 /* The read returns when the child closes the pipe (or when
140 * the child dies for some reason). */
141 close(pipefd[1]);
142 (void)read(pipefd[0], &dummy, (size_t)1);
143 close(pipefd[0]);
144 }
145
146 /* When swapping screens we may need to go to the next line, e.g.,
147 * after a hit-enter prompt and using ":gui". */
148 if (newline_on_exit)
149 mch_errmsg("\r\n");
150
151 /*
152 * The parent must skip the normal exit() processing, the child
153 * will do it. For example, GTK messes up signals when exiting.
154 */
155 _exit(0);
156 }
157
158# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
159 /*
160 * Change our process group. On some systems/shells a CTRL-C in the
161 * shell where Vim was started would otherwise kill gvim!
162 */
163 if (pid == 0) /* child */
164# if defined(HAVE_SETSID)
165 (void)setsid();
166# else
167 (void)setpgid(0, 0);
168# endif
169# endif
170 if (!pipe_error)
171 {
172 close(pipefd[0]);
173 close(pipefd[1]);
174 }
175
176# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
177 /* Tell the session manager our new PID */
178 gui_mch_forked();
179# endif
180 }
181#else
182# if defined(__QNXNTO__)
183 if (gui.in_use && dofork)
184 procmgr_daemon(0, PROCMGR_DAEMON_KEEPUMASK | PROCMGR_DAEMON_NOCHDIR |
185 PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
186# endif
187#endif
188
189#ifdef FEAT_AUTOCMD
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;
253#ifndef HAVE_GTK2
254 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
263# ifndef HAVE_GTK2
264# 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#ifdef FEAT_NETBEANS_INTG
597 if (starting == 0 && usingNetbeans)
598 /* Tell the client that it can start sending commands. */
599 netbeans_startup_done();
600#endif
601#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
602 if (!im_xim_isvalid_imactivate())
603 EMSG(_("E599: Value of 'imactivatekey' is invalid"));
604#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000605 /* When 'cmdheight' was set during startup it may not have taken
606 * effect yet. */
607 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000608 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 return;
611 }
612
613error2:
614#ifdef FEAT_GUI_X11
615 /* undo gui_mch_init() */
616 gui_mch_uninit();
617#endif
618
619error:
620 gui.in_use = FALSE;
621 clip_init(FALSE);
622}
623
624
625 void
626gui_exit(rc)
627 int rc;
628{
629#ifndef __BEOS__
630 /* don't free the fonts, it leads to a BUS error
631 * richard@whitequeen.com Jul 99 */
632 free_highlight_fonts();
633#endif
634 gui.in_use = FALSE;
635 gui_mch_exit(rc);
636}
637
Bram Moolenaar9372a112005-12-06 19:59:18 +0000638#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000640# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
642 * Called when the GUI shell is closed by the user. If there are no changed
643 * files Vim exits, otherwise there will be a dialog to ask the user what to
644 * do.
645 * When this function returns, Vim should NOT exit!
646 */
647 void
648gui_shell_closed()
649{
650 cmdmod_T save_cmdmod;
651
652 save_cmdmod = cmdmod;
653
654 /* Only exit when there are no changed files */
655 exiting = TRUE;
656# ifdef FEAT_BROWSE
657 cmdmod.browse = TRUE;
658# endif
659# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
660 cmdmod.confirm = TRUE;
661# endif
662 /* If there are changed buffers, present the user with a dialog if
663 * possible, otherwise give an error message. */
664 if (!check_changed_any(FALSE))
665 getout(0);
666
667 exiting = FALSE;
668 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000669 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671#endif
672
673/*
674 * Set the font. "font_list" is a a comma separated list of font names. The
675 * first font name that works is used. If none is found, use the default
676 * font.
677 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
678 * Return OK when able to set the font. When it failed FAIL is returned and
679 * the fonts are unchanged.
680 */
681/*ARGSUSED*/
682 int
683gui_init_font(font_list, fontset)
684 char_u *font_list;
685 int fontset;
686{
687#define FONTLEN 320
688 char_u font_name[FONTLEN];
689 int font_list_empty = FALSE;
690 int ret = FAIL;
691
692 if (!gui.in_use)
693 return FAIL;
694
695 font_name[0] = NUL;
696 if (*font_list == NUL)
697 font_list_empty = TRUE;
698 else
699 {
700#ifdef FEAT_XFONTSET
701 /* When using a fontset, the whole list of fonts is one name. */
702 if (fontset)
703 ret = gui_mch_init_font(font_list, TRUE);
704 else
705#endif
706 while (*font_list != NUL)
707 {
708 /* Isolate one comma separated font name. */
709 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
710
711 /* Careful!!! The Win32 version of gui_mch_init_font(), when
712 * called with "*" will change p_guifont to the selected font
713 * name, which frees the old value. This makes font_list
714 * invalid. Thus when OK is returned here, font_list must no
715 * longer be used! */
716 if (gui_mch_init_font(font_name, FALSE) == OK)
717 {
718#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
719 /* If it's a Unicode font, try setting 'guifontwide' to a
720 * similar double-width font. */
721 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
722 && strstr((char *)font_name, "10646") != NULL)
723 set_guifontwide(font_name);
724#endif
725 ret = OK;
726 break;
727 }
728 }
729 }
730
731 if (ret != OK
732 && STRCMP(font_list, "*") != 0
733 && (font_list_empty || gui.norm_font == NOFONT))
734 {
735 /*
736 * Couldn't load any font in 'font_list', keep the current font if
737 * there is one. If 'font_list' is empty, or if there is no current
738 * font, tell gui_mch_init_font() to try to find a font we can load.
739 */
740 ret = gui_mch_init_font(NULL, FALSE);
741 }
742
743 if (ret == OK)
744 {
745#ifndef HAVE_GTK2
746 /* Set normal font as current font */
747# ifdef FEAT_XFONTSET
748 if (gui.fontset != NOFONTSET)
749 gui_mch_set_fontset(gui.fontset);
750 else
751# endif
752 gui_mch_set_font(gui.norm_font);
753#endif
754 gui_set_shellsize(FALSE,
755#ifdef MSWIN
756 TRUE
757#else
758 FALSE
759#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +0000760 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762
763 return ret;
764}
765
766#if defined(FEAT_MBYTE) || defined(PROTO)
767# ifndef HAVE_GTK2
768/*
769 * Try setting 'guifontwide' to a font twice as wide as "name".
770 */
771 static void
772set_guifontwide(name)
773 char_u *name;
774{
775 int i = 0;
776 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
777 char_u *wp = NULL;
778 char_u *p;
779 GuiFont font;
780
781 wp = wide_name;
782 for (p = name; *p != NUL; ++p)
783 {
784 *wp++ = *p;
785 if (*p == '-')
786 {
787 ++i;
788 if (i == 6) /* font type: change "--" to "-*-" */
789 {
790 if (p[1] == '-')
791 *wp++ = '*';
792 }
793 else if (i == 12) /* found the width */
794 {
795 ++p;
796 i = getdigits(&p);
797 if (i != 0)
798 {
799 /* Double the width specification. */
800 sprintf((char *)wp, "%d%s", i * 2, p);
801 font = gui_mch_get_font(wide_name, FALSE);
802 if (font != NOFONT)
803 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000804 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 gui.wide_font = font;
806 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000807 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809 }
810 break;
811 }
812 }
813 }
814}
815# endif /* !HAVE_GTK2 */
816
817/*
818 * Get the font for 'guifontwide'.
819 * Return FAIL for an invalid font name.
820 */
821 int
822gui_get_wide_font()
823{
824 GuiFont font = NOFONT;
825 char_u font_name[FONTLEN];
826 char_u *p;
827
828 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
829 return OK; /* Will give an error message later. */
830
831 if (p_guifontwide != NULL && *p_guifontwide != NUL)
832 {
833 for (p = p_guifontwide; *p != NUL; )
834 {
835 /* Isolate one comma separated font name. */
836 (void)copy_option_part(&p, font_name, FONTLEN, ",");
837 font = gui_mch_get_font(font_name, FALSE);
838 if (font != NOFONT)
839 break;
840 }
841 if (font == NOFONT)
842 return FAIL;
843 }
844
845 gui_mch_free_font(gui.wide_font);
846#ifdef HAVE_GTK2
847 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
848 if (font != NOFONT && gui.norm_font != NOFONT
849 && pango_font_description_equal(font, gui.norm_font))
850 {
851 gui.wide_font = NOFONT;
852 gui_mch_free_font(font);
853 }
854 else
855#endif
856 gui.wide_font = font;
857 return OK;
858}
859#endif
860
861 void
862gui_set_cursor(row, col)
863 int row;
864 int col;
865{
866 gui.row = row;
867 gui.col = col;
868}
869
870/*
871 * gui_check_pos - check if the cursor is on the screen.
872 */
873 static void
874gui_check_pos()
875{
876 if (gui.row >= screen_Rows)
877 gui.row = screen_Rows - 1;
878 if (gui.col >= screen_Columns)
879 gui.col = screen_Columns - 1;
880 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
881 gui.cursor_is_valid = FALSE;
882}
883
884/*
885 * Redraw the cursor if necessary or when forced.
886 * Careful: The contents of ScreenLines[] must match what is on the screen,
887 * otherwise this goes wrong. May need to call out_flush() first.
888 */
889 void
890gui_update_cursor(force, clear_selection)
891 int force; /* when TRUE, update even when not moved */
892 int clear_selection;/* clear selection under cursor */
893{
894 int cur_width = 0;
895 int cur_height = 0;
896 int old_hl_mask;
897 int idx;
898 int id;
899 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
900 int cattr; /* cursor attributes */
901 int attr;
902 attrentry_T *aep = NULL;
903
904 /* Don't update the cursor when halfway busy scrolling.
905 * ScreenLines[] isn't valid then. */
906 if (!can_update_cursor)
907 return;
908
909 gui_check_pos();
910 if (!gui.cursor_is_valid || force
911 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
912 {
913 gui_undraw_cursor();
914 if (gui.row < 0)
915 return;
916#ifdef USE_IM_CONTROL
917 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
918 im_set_position(gui.row, gui.col);
919#endif
920 gui.cursor_row = gui.row;
921 gui.cursor_col = gui.col;
922
923 /* Only write to the screen after ScreenLines[] has been initialized */
924 if (!screen_cleared || ScreenLines == NULL)
925 return;
926
927 /* Clear the selection if we are about to write over it */
928 if (clear_selection)
929 clip_may_clear_selection(gui.row, gui.row);
930 /* Check that the cursor is inside the shell (resizing may have made
931 * it invalid) */
932 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
933 return;
934
935 gui.cursor_is_valid = TRUE;
936
937 /*
938 * How the cursor is drawn depends on the current mode.
939 */
940 idx = get_shape_idx(FALSE);
941 if (State & LANGMAP)
942 id = shape_table[idx].id_lm;
943 else
944 id = shape_table[idx].id;
945
946 /* get the colors and attributes for the cursor. Default is inverted */
947 cfg = INVALCOLOR;
948 cbg = INVALCOLOR;
949 cattr = HL_INVERSE;
950 gui_mch_set_blinking(shape_table[idx].blinkwait,
951 shape_table[idx].blinkon,
952 shape_table[idx].blinkoff);
953 if (id > 0)
954 {
955 cattr = syn_id2colors(id, &cfg, &cbg);
956#if defined(USE_IM_CONTROL) || defined(FEAT_HANGULIN)
957 {
958 static int iid;
959 guicolor_T fg, bg;
960
961 if (im_get_status())
962 {
963 iid = syn_name2id((char_u *)"CursorIM");
964 if (iid > 0)
965 {
966 syn_id2colors(iid, &fg, &bg);
967 if (bg != INVALCOLOR)
968 cbg = bg;
969 if (fg != INVALCOLOR)
970 cfg = fg;
971 }
972 }
973 }
974#endif
975 }
976
977 /*
978 * Get the attributes for the character under the cursor.
979 * When no cursor color was given, use the character color.
980 */
981 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
982 if (attr > HL_ALL)
983 aep = syn_gui_attr2entry(attr);
984 if (aep != NULL)
985 {
986 attr = aep->ae_attr;
987 if (cfg == INVALCOLOR)
988 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
989 : aep->ae_u.gui.fg_color);
990 if (cbg == INVALCOLOR)
991 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
992 : aep->ae_u.gui.bg_color);
993 }
994 if (cfg == INVALCOLOR)
995 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
996 if (cbg == INVALCOLOR)
997 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
998
999#ifdef FEAT_XIM
1000 if (aep != NULL)
1001 {
1002 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1003 : aep->ae_u.gui.bg_color);
1004 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1005 : aep->ae_u.gui.fg_color);
1006 if (xim_bg_color == INVALCOLOR)
1007 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1008 : gui.back_pixel;
1009 if (xim_fg_color == INVALCOLOR)
1010 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1011 : gui.norm_pixel;
1012 }
1013 else
1014 {
1015 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1016 : gui.back_pixel;
1017 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1018 : gui.norm_pixel;
1019 }
1020#endif
1021
1022 attr &= ~HL_INVERSE;
1023 if (cattr & HL_INVERSE)
1024 {
1025 cc = cbg;
1026 cbg = cfg;
1027 cfg = cc;
1028 }
1029 cattr &= ~HL_INVERSE;
1030
1031 /*
1032 * When we don't have window focus, draw a hollow cursor.
1033 */
1034 if (!gui.in_focus)
1035 {
1036 gui_mch_draw_hollow_cursor(cbg);
1037 return;
1038 }
1039
1040 old_hl_mask = gui.highlight_mask;
1041 if (shape_table[idx].shape == SHAPE_BLOCK
1042#ifdef FEAT_HANGULIN
1043 || composing_hangul
1044#endif
1045 )
1046 {
1047 /*
1048 * Draw the text character with the cursor colors. Use the
1049 * character attributes plus the cursor attributes.
1050 */
1051 gui.highlight_mask = (cattr | attr);
1052#ifdef FEAT_HANGULIN
1053 if (composing_hangul)
1054 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
1055 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1056 else
1057#endif
1058 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1059 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1060 }
1061 else
1062 {
1063#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1064 int col_off = FALSE;
1065#endif
1066 /*
1067 * First draw the partial cursor, then overwrite with the text
1068 * character, using a transparent background.
1069 */
1070 if (shape_table[idx].shape == SHAPE_VER)
1071 {
1072 cur_height = gui.char_height;
1073 cur_width = (gui.char_width * shape_table[idx].percentage
1074 + 99) / 100;
1075 }
1076 else
1077 {
1078 cur_height = (gui.char_height * shape_table[idx].percentage
1079 + 99) / 100;
1080 cur_width = gui.char_width;
1081 }
1082#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00001083 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1084 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 /* Double wide character. */
1087 if (shape_table[idx].shape != SHAPE_VER)
1088 cur_width += gui.char_width;
1089# ifdef FEAT_RIGHTLEFT
1090 if (CURSOR_BAR_RIGHT)
1091 {
1092 /* gui.col points to the left halve of the character but
1093 * the vertical line needs to be on the right halve.
1094 * A double-wide horizontal line is also drawn from the
1095 * right halve in gui_mch_draw_part_cursor(). */
1096 col_off = TRUE;
1097 ++gui.col;
1098 }
1099# endif
1100 }
1101#endif
1102 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
1103#if defined(FEAT_MBYTE) && defined(FEAT_RIGHTLEFT)
1104 if (col_off)
1105 --gui.col;
1106#endif
1107
1108#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1109 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1110 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1111 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1112 (guicolor_T)0, (guicolor_T)0, 0);
1113#endif
1114 }
1115 gui.highlight_mask = old_hl_mask;
1116 }
1117}
1118
1119#if defined(FEAT_MENU) || defined(PROTO)
1120 void
1121gui_position_menu()
1122{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001123# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (gui.menu_is_active && gui.in_use)
1125 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1126# endif
1127}
1128#endif
1129
1130/*
1131 * Position the various GUI components (text area, menu). The vertical
1132 * scrollbars are NOT handled here. See gui_update_scrollbars().
1133 */
1134/*ARGSUSED*/
1135 static void
1136gui_position_components(total_width)
1137 int total_width;
1138{
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
1186#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1187 + 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);
1317#if defined(FEAT_XIM) && !defined(HAVE_GTK2)
1318 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 */
1370/*ARGSUSED*/
1371 void
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001372gui_set_shellsize(mustset, fit_to_display, direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 int mustset; /* set by the user */
1374 int fit_to_display;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001375 int direction; /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 int base_width;
1378 int base_height;
1379 int width;
1380 int height;
1381 int min_width;
1382 int min_height;
1383 int screen_w;
1384 int screen_h;
1385
1386 if (!gui.shell_created)
1387 return;
1388
1389#ifdef MSWIN
1390 /* If not setting to a user specified size and maximized, calculate the
1391 * number of characters that fit in the maximized window. */
1392 if (!mustset && gui_mch_maximized())
1393 {
1394 gui_mch_newfont();
1395 return;
1396 }
1397#endif
1398
1399 base_width = gui_get_base_width();
1400 base_height = gui_get_base_height();
1401#ifdef USE_SUN_WORKSHOP
1402 if (!mustset && usingSunWorkShop
1403 && workshop_get_width_height(&width, &height))
1404 {
1405 Columns = (width - base_width + gui.char_width - 1) / gui.char_width;
1406 Rows = (height - base_height + gui.char_height - 1) / gui.char_height;
1407 }
1408 else
1409#endif
1410 {
1411 width = Columns * gui.char_width + base_width;
1412 height = Rows * gui.char_height + base_height;
1413 }
1414
1415 if (fit_to_display)
1416 {
1417 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001418 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
1420 Columns = (screen_w - base_width) / gui.char_width;
1421 if (Columns < MIN_COLUMNS)
1422 Columns = MIN_COLUMNS;
1423 width = Columns * gui.char_width + base_width;
1424 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001425 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
1427 Rows = (screen_h - base_height) / gui.char_height;
1428 check_shellsize();
1429 height = Rows * gui.char_height + base_height;
1430 }
1431 }
1432 gui.num_cols = Columns;
1433 gui.num_rows = Rows;
1434
1435 min_width = base_width + MIN_COLUMNS * gui.char_width;
1436 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001437# ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001438 min_height += tabline_height() * gui.char_height;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001442 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (fit_to_display)
1444 {
1445 int x, y;
1446
1447 /* Some window managers put the Vim window left of/above the screen. */
1448 gui_mch_update();
1449 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1450 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1451 }
1452
1453 gui_position_components(width);
1454 gui_update_scrollbars(TRUE);
1455 gui_reset_scroll_region();
1456}
1457
1458/*
1459 * Called when Rows and/or Columns has changed.
1460 */
1461 void
1462gui_new_shellsize()
1463{
1464 gui_reset_scroll_region();
1465}
1466
1467/*
1468 * Make scroll region cover whole screen.
1469 */
1470 void
1471gui_reset_scroll_region()
1472{
1473 gui.scroll_region_top = 0;
1474 gui.scroll_region_bot = gui.num_rows - 1;
1475 gui.scroll_region_left = 0;
1476 gui.scroll_region_right = gui.num_cols - 1;
1477}
1478
1479 void
1480gui_start_highlight(mask)
1481 int mask;
1482{
1483 if (mask > HL_ALL) /* highlight code */
1484 gui.highlight_mask = mask;
1485 else /* mask */
1486 gui.highlight_mask |= mask;
1487}
1488
1489 void
1490gui_stop_highlight(mask)
1491 int mask;
1492{
1493 if (mask > HL_ALL) /* highlight code */
1494 gui.highlight_mask = HL_NORMAL;
1495 else /* mask */
1496 gui.highlight_mask &= ~mask;
1497}
1498
1499/*
1500 * Clear a rectangular region of the screen from text pos (row1, col1) to
1501 * (row2, col2) inclusive.
1502 */
1503 void
1504gui_clear_block(row1, col1, row2, col2)
1505 int row1;
1506 int col1;
1507 int row2;
1508 int col2;
1509{
1510 /* Clear the selection if we are about to write over it */
1511 clip_may_clear_selection(row1, row2);
1512
1513 gui_mch_clear_block(row1, col1, row2, col2);
1514
1515 /* Invalidate cursor if it was in this block */
1516 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1517 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1518 gui.cursor_is_valid = FALSE;
1519}
1520
1521/*
1522 * Write code to update the cursor later. This avoids the need to flush the
1523 * output buffer before calling gui_update_cursor().
1524 */
1525 void
1526gui_update_cursor_later()
1527{
1528 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
1529}
1530
1531 void
1532gui_write(s, len)
1533 char_u *s;
1534 int len;
1535{
1536 char_u *p;
1537 int arg1 = 0, arg2 = 0;
1538 /* this doesn't make sense, disabled until someone can explain why it
1539 * would be needed */
1540#if 0 && (defined(RISCOS) || defined(WIN16))
1541 int force_cursor = TRUE; /* JK230798, stop Vim being smart or
1542 our redraw speed will suffer */
1543#else
1544 int force_cursor = FALSE; /* force cursor update */
1545#endif
1546 int force_scrollbar = FALSE;
1547 static win_T *old_curwin = NULL;
1548
1549/* #define DEBUG_GUI_WRITE */
1550#ifdef DEBUG_GUI_WRITE
1551 {
1552 int i;
1553 char_u *str;
1554
1555 printf("gui_write(%d):\n ", len);
1556 for (i = 0; i < len; i++)
1557 if (s[i] == ESC)
1558 {
1559 if (i != 0)
1560 printf("\n ");
1561 printf("<ESC>");
1562 }
1563 else
1564 {
1565 str = transchar_byte(s[i]);
1566 if (str[0] && str[1])
1567 printf("<%s>", (char *)str);
1568 else
1569 printf("%s", (char *)str);
1570 }
1571 printf("\n");
1572 }
1573#endif
1574 while (len)
1575 {
1576 if (s[0] == ESC && s[1] == '|')
1577 {
1578 p = s + 2;
1579 if (VIM_ISDIGIT(*p))
1580 {
1581 arg1 = getdigits(&p);
1582 if (p > s + len)
1583 break;
1584 if (*p == ';')
1585 {
1586 ++p;
1587 arg2 = getdigits(&p);
1588 if (p > s + len)
1589 break;
1590 }
1591 }
1592 switch (*p)
1593 {
1594 case 'C': /* Clear screen */
1595 clip_scroll_selection(9999);
1596 gui_mch_clear_all();
1597 gui.cursor_is_valid = FALSE;
1598 force_scrollbar = TRUE;
1599 break;
1600 case 'M': /* Move cursor */
1601 gui_set_cursor(arg1, arg2);
1602 break;
1603 case 's': /* force cursor (shape) update */
1604 force_cursor = TRUE;
1605 break;
1606 case 'R': /* Set scroll region */
1607 if (arg1 < arg2)
1608 {
1609 gui.scroll_region_top = arg1;
1610 gui.scroll_region_bot = arg2;
1611 }
1612 else
1613 {
1614 gui.scroll_region_top = arg2;
1615 gui.scroll_region_bot = arg1;
1616 }
1617 break;
1618#ifdef FEAT_VERTSPLIT
1619 case 'V': /* Set vertical scroll region */
1620 if (arg1 < arg2)
1621 {
1622 gui.scroll_region_left = arg1;
1623 gui.scroll_region_right = arg2;
1624 }
1625 else
1626 {
1627 gui.scroll_region_left = arg2;
1628 gui.scroll_region_right = arg1;
1629 }
1630 break;
1631#endif
1632 case 'd': /* Delete line */
1633 gui_delete_lines(gui.row, 1);
1634 break;
1635 case 'D': /* Delete lines */
1636 gui_delete_lines(gui.row, arg1);
1637 break;
1638 case 'i': /* Insert line */
1639 gui_insert_lines(gui.row, 1);
1640 break;
1641 case 'I': /* Insert lines */
1642 gui_insert_lines(gui.row, arg1);
1643 break;
1644 case '$': /* Clear to end-of-line */
1645 gui_clear_block(gui.row, gui.col, gui.row,
1646 (int)Columns - 1);
1647 break;
1648 case 'h': /* Turn on highlighting */
1649 gui_start_highlight(arg1);
1650 break;
1651 case 'H': /* Turn off highlighting */
1652 gui_stop_highlight(arg1);
1653 break;
1654 case 'f': /* flash the window (visual bell) */
1655 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1656 break;
1657 default:
1658 p = s + 1; /* Skip the ESC */
1659 break;
1660 }
1661 len -= (int)(++p - s);
1662 s = p;
1663 }
1664 else if (
1665#ifdef EBCDIC
1666 CtrlChar(s[0]) != 0 /* Ctrl character */
1667#else
1668 s[0] < 0x20 /* Ctrl character */
1669#endif
1670#ifdef FEAT_SIGN_ICONS
1671 && s[0] != SIGN_BYTE
1672# ifdef FEAT_NETBEANS_INTG
1673 && s[0] != MULTISIGN_BYTE
1674# endif
1675#endif
1676 )
1677 {
1678 if (s[0] == '\n') /* NL */
1679 {
1680 gui.col = 0;
1681 if (gui.row < gui.scroll_region_bot)
1682 gui.row++;
1683 else
1684 gui_delete_lines(gui.scroll_region_top, 1);
1685 }
1686 else if (s[0] == '\r') /* CR */
1687 {
1688 gui.col = 0;
1689 }
1690 else if (s[0] == '\b') /* Backspace */
1691 {
1692 if (gui.col)
1693 --gui.col;
1694 }
1695 else if (s[0] == Ctrl_L) /* cursor-right */
1696 {
1697 ++gui.col;
1698 }
1699 else if (s[0] == Ctrl_G) /* Beep */
1700 {
1701 gui_mch_beep();
1702 }
1703 /* Other Ctrl character: shouldn't happen! */
1704
1705 --len; /* Skip this char */
1706 ++s;
1707 }
1708 else
1709 {
1710 p = s;
1711 while (len > 0 && (
1712#ifdef EBCDIC
1713 CtrlChar(*p) == 0
1714#else
1715 *p >= 0x20
1716#endif
1717#ifdef FEAT_SIGN_ICONS
1718 || *p == SIGN_BYTE
1719# ifdef FEAT_NETBEANS_INTG
1720 || *p == MULTISIGN_BYTE
1721# endif
1722#endif
1723 ))
1724 {
1725 len--;
1726 p++;
1727 }
1728 gui_outstr(s, (int)(p - s));
1729 s = p;
1730 }
1731 }
1732
1733 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1734 * set). */
1735 if (force_cursor)
1736 gui_update_cursor(TRUE, TRUE);
1737
1738 /* When switching to another window the dragging must have stopped.
1739 * Required for GTK, dragged_sb isn't reset. */
1740 if (old_curwin != curwin)
1741 gui.dragged_sb = SBAR_NONE;
1742
1743 /* Update the scrollbars after clearing the screen or when switched
1744 * to another window.
1745 * Update the horizontal scrollbar always, it's difficult to check all
1746 * situations where it might change. */
1747 if (force_scrollbar || old_curwin != curwin)
1748 gui_update_scrollbars(force_scrollbar);
1749 else
1750 gui_update_horiz_scrollbar(FALSE);
1751 old_curwin = curwin;
1752
1753 /*
1754 * We need to make sure this is cleared since Athena doesn't tell us when
1755 * he is done dragging. Do the same for GTK.
1756 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001757#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 gui.dragged_sb = SBAR_NONE;
1759#endif
1760
1761 gui_mch_flush(); /* In case vim decides to take a nap */
1762}
1763
1764/*
1765 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1766 * produces wrong results. Call gui_dont_update_cursor() before that code and
1767 * gui_can_update_cursor() afterwards.
1768 */
1769 void
1770gui_dont_update_cursor()
1771{
1772 if (gui.in_use)
1773 {
1774 /* Undraw the cursor now, we probably can't do it after the change. */
1775 gui_undraw_cursor();
1776 can_update_cursor = FALSE;
1777 }
1778}
1779
1780 void
1781gui_can_update_cursor()
1782{
1783 can_update_cursor = TRUE;
1784 /* No need to update the cursor right now, there is always more output
1785 * after scrolling. */
1786}
1787
1788 static void
1789gui_outstr(s, len)
1790 char_u *s;
1791 int len;
1792{
1793 int this_len;
1794#ifdef FEAT_MBYTE
1795 int cells;
1796#endif
1797
1798 if (len == 0)
1799 return;
1800
1801 if (len < 0)
1802 len = (int)STRLEN(s);
1803
1804 while (len > 0)
1805 {
1806#ifdef FEAT_MBYTE
1807 if (has_mbyte)
1808 {
1809 /* Find out how many chars fit in the current line. */
1810 cells = 0;
1811 for (this_len = 0; this_len < len; )
1812 {
1813 cells += (*mb_ptr2cells)(s + this_len);
1814 if (gui.col + cells > Columns)
1815 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001816 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
1818 if (this_len > len)
1819 this_len = len; /* don't include following composing char */
1820 }
1821 else
1822#endif
1823 if (gui.col + len > Columns)
1824 this_len = Columns - gui.col;
1825 else
1826 this_len = len;
1827
1828 (void)gui_outstr_nowrap(s, this_len,
1829 0, (guicolor_T)0, (guicolor_T)0, 0);
1830 s += this_len;
1831 len -= this_len;
1832#ifdef FEAT_MBYTE
1833 /* fill up for a double-width char that doesn't fit. */
1834 if (len > 0 && gui.col < Columns)
1835 (void)gui_outstr_nowrap((char_u *)" ", 1,
1836 0, (guicolor_T)0, (guicolor_T)0, 0);
1837#endif
1838 /* The cursor may wrap to the next line. */
1839 if (gui.col >= Columns)
1840 {
1841 gui.col = 0;
1842 gui.row++;
1843 }
1844 }
1845}
1846
1847/*
1848 * Output one character (may be one or two display cells).
1849 * Caller must check for valid "off".
1850 * Returns FAIL or OK, just like gui_outstr_nowrap().
1851 */
1852 static int
1853gui_screenchar(off, flags, fg, bg, back)
1854 int off; /* Offset from start of screen */
1855 int flags;
1856 guicolor_T fg, bg; /* colors for cursor */
1857 int back; /* backup this many chars when using bold trick */
1858{
1859#ifdef FEAT_MBYTE
1860 char_u buf[MB_MAXBYTES + 1];
1861
1862 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
1863 if (enc_utf8 && ScreenLines[off] == 0)
1864 return OK;
1865
1866 if (enc_utf8 && ScreenLinesUC[off] != 0)
1867 /* Draw UTF-8 multi-byte character. */
1868 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
1869 flags, fg, bg, back);
1870
1871 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1872 {
1873 buf[0] = ScreenLines[off];
1874 buf[1] = ScreenLines2[off];
1875 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
1876 }
1877
1878 /* Draw non-multi-byte character or DBCS character. */
1879 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001880 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 flags, fg, bg, back);
1882#else
1883 return gui_outstr_nowrap(ScreenLines + off, 1, flags, fg, bg, back);
1884#endif
1885}
1886
1887#ifdef HAVE_GTK2
1888/*
1889 * Output the string at the given screen position. This is used in place
1890 * of gui_screenchar() where possible because Pango needs as much context
1891 * as possible to work nicely. It's a lot faster as well.
1892 */
1893 static int
1894gui_screenstr(off, len, flags, fg, bg, back)
1895 int off; /* Offset from start of screen */
1896 int len; /* string length in screen cells */
1897 int flags;
1898 guicolor_T fg, bg; /* colors for cursor */
1899 int back; /* backup this many chars when using bold trick */
1900{
1901 char_u *buf;
1902 int outlen = 0;
1903 int i;
1904 int retval;
1905
1906 if (len <= 0) /* "cannot happen"? */
1907 return OK;
1908
1909 if (enc_utf8)
1910 {
1911 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
1912 if (buf == NULL)
1913 return OK; /* not much we could do here... */
1914
1915 for (i = off; i < off + len; ++i)
1916 {
1917 if (ScreenLines[i] == 0)
1918 continue; /* skip second half of double-width char */
1919
1920 if (ScreenLinesUC[i] == 0)
1921 buf[outlen++] = ScreenLines[i];
1922 else
1923 outlen += utfc_char2bytes(i, buf + outlen);
1924 }
1925
1926 buf[outlen] = NUL; /* only to aid debugging */
1927 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1928 vim_free(buf);
1929
1930 return retval;
1931 }
1932 else if (enc_dbcs == DBCS_JPNU)
1933 {
1934 buf = alloc((unsigned)(len * 2 + 1));
1935 if (buf == NULL)
1936 return OK; /* not much we could do here... */
1937
1938 for (i = off; i < off + len; ++i)
1939 {
1940 buf[outlen++] = ScreenLines[i];
1941
1942 /* handle double-byte single-width char */
1943 if (ScreenLines[i] == 0x8e)
1944 buf[outlen++] = ScreenLines2[i];
1945 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
1946 buf[outlen++] = ScreenLines[++i];
1947 }
1948
1949 buf[outlen] = NUL; /* only to aid debugging */
1950 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
1951 vim_free(buf);
1952
1953 return retval;
1954 }
1955 else
1956 {
1957 return gui_outstr_nowrap(&ScreenLines[off], len,
1958 flags, fg, bg, back);
1959 }
1960}
1961#endif /* HAVE_GTK2 */
1962
1963/*
1964 * Output the given string at the current cursor position. If the string is
1965 * too long to fit on the line, then it is truncated.
1966 * "flags":
1967 * GUI_MON_IS_CURSOR should only be used when this function is being called to
1968 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001969 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 * background.
1971 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
1972 * it.
1973 * Returns OK, unless "back" is non-zero and using the bold trick, then return
1974 * FAIL (the caller should start drawing "back" chars back).
1975 */
1976 int
1977gui_outstr_nowrap(s, len, flags, fg, bg, back)
1978 char_u *s;
1979 int len;
1980 int flags;
1981 guicolor_T fg, bg; /* colors for cursor */
1982 int back; /* backup this many chars when using bold trick */
1983{
1984 long_u highlight_mask;
1985 long_u hl_mask_todo;
1986 guicolor_T fg_color;
1987 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001988 guicolor_T sp_color;
Bram Moolenaar9372a112005-12-06 19:59:18 +00001989#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 GuiFont font = NOFONT;
1991# ifdef FEAT_XFONTSET
1992 GuiFontset fontset = NOFONTSET;
1993# endif
1994#endif
1995 attrentry_T *aep = NULL;
1996 int draw_flags;
1997 int col = gui.col;
1998#ifdef FEAT_SIGN_ICONS
1999 int draw_sign = FALSE;
2000# ifdef FEAT_NETBEANS_INTG
2001 int multi_sign = FALSE;
2002# endif
2003#endif
2004
2005 if (len < 0)
2006 len = (int)STRLEN(s);
2007 if (len == 0)
2008 return OK;
2009
2010#ifdef FEAT_SIGN_ICONS
2011 if (*s == SIGN_BYTE
2012# ifdef FEAT_NETBEANS_INTG
2013 || *s == MULTISIGN_BYTE
2014# endif
2015 )
2016 {
2017# ifdef FEAT_NETBEANS_INTG
2018 if (*s == MULTISIGN_BYTE)
2019 multi_sign = TRUE;
2020# endif
2021 /* draw spaces instead */
2022 s = (char_u *)" ";
2023 if (len == 1 && col > 0)
2024 --col;
2025 len = 2;
2026 draw_sign = TRUE;
2027 highlight_mask = 0;
2028 }
2029 else
2030#endif
2031 if (gui.highlight_mask > HL_ALL)
2032 {
2033 aep = syn_gui_attr2entry(gui.highlight_mask);
2034 if (aep == NULL) /* highlighting not set */
2035 highlight_mask = 0;
2036 else
2037 highlight_mask = aep->ae_attr;
2038 }
2039 else
2040 highlight_mask = gui.highlight_mask;
2041 hl_mask_todo = highlight_mask;
2042
Bram Moolenaar9372a112005-12-06 19:59:18 +00002043#if !defined(MSWIN16_FASTTEXT) && !defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 /* Set the font */
2045 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2046 font = aep->ae_u.gui.font;
2047# ifdef FEAT_XFONTSET
2048 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2049 fontset = aep->ae_u.gui.fontset;
2050# endif
2051 else
2052 {
2053# ifdef FEAT_XFONTSET
2054 if (gui.fontset != NOFONTSET)
2055 fontset = gui.fontset;
2056 else
2057# endif
2058 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2059 {
2060 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2061 {
2062 font = gui.boldital_font;
2063 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2064 }
2065 else if (gui.bold_font != NOFONT)
2066 {
2067 font = gui.bold_font;
2068 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2069 }
2070 else
2071 font = gui.norm_font;
2072 }
2073 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2074 {
2075 font = gui.ital_font;
2076 hl_mask_todo &= ~HL_ITALIC;
2077 }
2078 else
2079 font = gui.norm_font;
2080 }
2081# ifdef FEAT_XFONTSET
2082 if (fontset != NOFONTSET)
2083 gui_mch_set_fontset(fontset);
2084 else
2085# endif
2086 gui_mch_set_font(font);
2087#endif
2088
2089 draw_flags = 0;
2090
2091 /* Set the color */
2092 bg_color = gui.back_pixel;
2093 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2094 {
2095 draw_flags |= DRAW_CURSOR;
2096 fg_color = fg;
2097 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002098 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 }
2100 else if (aep != NULL)
2101 {
2102 fg_color = aep->ae_u.gui.fg_color;
2103 if (fg_color == INVALCOLOR)
2104 fg_color = gui.norm_pixel;
2105 bg_color = aep->ae_u.gui.bg_color;
2106 if (bg_color == INVALCOLOR)
2107 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002108 sp_color = aep->ae_u.gui.sp_color;
2109 if (sp_color == INVALCOLOR)
2110 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
2112 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002115 sp_color = fg_color;
2116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
2118 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2119 {
2120#if defined(AMIGA) || defined(RISCOS)
2121 gui_mch_set_colors(bg_color, fg_color);
2122#else
2123 gui_mch_set_fg_color(bg_color);
2124 gui_mch_set_bg_color(fg_color);
2125#endif
2126 }
2127 else
2128 {
2129#if defined(AMIGA) || defined(RISCOS)
2130 gui_mch_set_colors(fg_color, bg_color);
2131#else
2132 gui_mch_set_fg_color(fg_color);
2133 gui_mch_set_bg_color(bg_color);
2134#endif
2135 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002136 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138 /* Clear the selection if we are about to write over it */
2139 if (!(flags & GUI_MON_NOCLEAR))
2140 clip_may_clear_selection(gui.row, gui.row);
2141
2142
2143#ifndef MSWIN16_FASTTEXT
2144 /* If there's no bold font, then fake it */
2145 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2146 draw_flags |= DRAW_BOLD;
2147#endif
2148
2149 /*
2150 * When drawing bold or italic characters the spill-over from the left
2151 * neighbor may be destroyed. Let the caller backup to start redrawing
2152 * just after a blank.
2153 */
2154 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2155 return FAIL;
2156
Bram Moolenaar9372a112005-12-06 19:59:18 +00002157#if defined(RISCOS) || defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /* If there's no italic font, then fake it.
2159 * For GTK2, we don't need a different font for italic style. */
2160 if (hl_mask_todo & HL_ITALIC)
2161 draw_flags |= DRAW_ITALIC;
2162
2163 /* Do we underline the text? */
2164 if (hl_mask_todo & HL_UNDERLINE)
2165 draw_flags |= DRAW_UNDERL;
2166#else
2167 /* Do we underline the text? */
2168 if ((hl_mask_todo & HL_UNDERLINE)
2169# ifndef MSWIN16_FASTTEXT
2170 || (hl_mask_todo & HL_ITALIC)
2171# endif
2172 )
2173 draw_flags |= DRAW_UNDERL;
2174#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002175 /* Do we undercurl the text? */
2176 if (hl_mask_todo & HL_UNDERCURL)
2177 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002179 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (flags & GUI_MON_TRS_CURSOR)
2181 draw_flags |= DRAW_TRANSP;
2182
2183 /*
2184 * Draw the text.
2185 */
2186#ifdef HAVE_GTK2
2187 /* The value returned is the length in display cells */
2188 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2189#else
2190# ifdef FEAT_MBYTE
2191 if (enc_utf8)
2192 {
2193 int start; /* index of bytes to be drawn */
2194 int cells; /* cellwidth of bytes to be drawn */
2195 int thislen; /* length of bytes to be drawin */
2196 int cn; /* cellwidth of current char */
2197 int i; /* index of current char */
2198 int c; /* current char value */
2199 int cl; /* byte length of current char */
2200 int comping; /* current char is composing */
2201 int scol = col; /* screen column */
2202 int dowide; /* use 'guifontwide' */
2203
2204 /* Break the string at a composing character, it has to be drawn on
2205 * top of the previous character. */
2206 start = 0;
2207 cells = 0;
2208 for (i = 0; i < len; i += cl)
2209 {
2210 c = utf_ptr2char(s + i);
2211 cn = utf_char2cells(c);
2212 if (cn > 1
2213# ifdef FEAT_XFONTSET
2214 && fontset == NOFONTSET
2215# endif
2216 && gui.wide_font != NOFONT)
2217 dowide = TRUE;
2218 else
2219 dowide = FALSE;
2220 comping = utf_iscomposing(c);
2221 if (!comping) /* count cells from non-composing chars */
2222 cells += cn;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002223 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (cl == 0) /* hit end of string */
2225 len = i + cl; /* len must be wrong "cannot happen" */
2226
2227 /* print the string so far if it's the last character or there is
2228 * a composing character. */
2229 if (i + cl >= len || (comping && i > start) || dowide
Bram Moolenaar9372a112005-12-06 19:59:18 +00002230# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 || (cn > 1
2232# ifdef FEAT_XFONTSET
2233 /* No fontset: At least draw char after wide char at
2234 * right position. */
2235 && fontset == NOFONTSET
2236# endif
2237 )
2238# endif
2239 )
2240 {
2241 if (comping || dowide)
2242 thislen = i - start;
2243 else
2244 thislen = i - start + cl;
2245 if (thislen > 0)
2246 {
2247 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2248 draw_flags);
2249 start += thislen;
2250 }
2251 scol += cells;
2252 cells = 0;
2253 if (dowide)
2254 {
2255 gui_mch_set_font(gui.wide_font);
2256 gui_mch_draw_string(gui.row, scol - cn,
2257 s + start, cl, draw_flags);
2258 gui_mch_set_font(font);
2259 start += cl;
2260 }
2261
Bram Moolenaar9372a112005-12-06 19:59:18 +00002262# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002263 /* No fontset: draw a space to fill the gap after a wide char
2264 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
2266# ifdef FEAT_XFONTSET
2267 && fontset == NOFONTSET
2268# endif
2269 && !dowide)
2270 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2271 1, draw_flags);
2272# endif
2273 }
2274 /* Draw a composing char on top of the previous char. */
2275 if (comping)
2276 {
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002277# if (defined(__APPLE_CC__) || defined(__MRC__)) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002278 /* Carbon ATSUI autodraws composing char over previous char */
2279 gui_mch_draw_string(gui.row, scol, s + i, cl,
2280 draw_flags | DRAW_TRANSP);
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002281# else
2282 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2283 draw_flags | DRAW_TRANSP);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 start = i + cl;
2286 }
2287 }
2288 /* The stuff below assumes "len" is the length in screen columns. */
2289 len = scol - col;
2290 }
2291 else
2292# endif
2293 {
2294 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
2295# ifdef FEAT_MBYTE
2296 if (enc_dbcs == DBCS_JPNU)
2297 {
2298 int clen = 0;
2299 int i;
2300
2301 /* Get the length in display cells, this can be different from the
2302 * number of bytes for "euc-jp". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002303 for (i = 0; i < len; i += (*mb_ptr2len)(s + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 clen += (*mb_ptr2cells)(s + i);
2305 len = clen;
2306 }
2307# endif
2308 }
2309#endif /* !HAVE_GTK2 */
2310
2311 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2312 gui.col = col + len;
2313
2314 /* May need to invert it when it's part of the selection. */
2315 if (flags & GUI_MON_NOCLEAR)
2316 clip_may_redraw_selection(gui.row, col, len);
2317
2318 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2319 {
2320 /* Invalidate the old physical cursor position if we wrote over it */
2321 if (gui.cursor_row == gui.row
2322 && gui.cursor_col >= col
2323 && gui.cursor_col < col + len)
2324 gui.cursor_is_valid = FALSE;
2325 }
2326
2327#ifdef FEAT_SIGN_ICONS
2328 if (draw_sign)
2329 /* Draw the sign on top of the spaces. */
2330 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
2331# ifdef FEAT_NETBEANS_INTG
2332 if (multi_sign)
2333 netbeans_draw_multisign_indicator(gui.row);
2334# endif
2335#endif
2336
2337 return OK;
2338}
2339
2340/*
2341 * Un-draw the cursor. Actually this just redraws the character at the given
2342 * position. The character just before it too, for when it was in bold.
2343 */
2344 void
2345gui_undraw_cursor()
2346{
2347 if (gui.cursor_is_valid)
2348 {
2349#ifdef FEAT_HANGULIN
2350 if (composing_hangul
2351 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
2352 (void)gui_outstr_nowrap(composing_hangul_buffer, 2,
2353 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2354 gui.norm_pixel, gui.back_pixel, 0);
2355 else
2356 {
2357#endif
2358 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2359 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2360 && gui.cursor_col > 0)
2361 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2362 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2363#ifdef FEAT_HANGULIN
2364 if (composing_hangul)
2365 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2366 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2367 }
2368#endif
2369 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2370 * here in case it wasn't needed to undraw it. */
2371 gui.cursor_is_valid = FALSE;
2372 }
2373}
2374
2375 void
2376gui_redraw(x, y, w, h)
2377 int x;
2378 int y;
2379 int w;
2380 int h;
2381{
2382 int row1, col1, row2, col2;
2383
2384 row1 = Y_2_ROW(y);
2385 col1 = X_2_COL(x);
2386 row2 = Y_2_ROW(y + h - 1);
2387 col2 = X_2_COL(x + w - 1);
2388
2389 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2390
2391 /*
2392 * We may need to redraw the cursor, but don't take it upon us to change
2393 * its location after a scroll.
2394 * (maybe be more strict even and test col too?)
2395 * These things may be outside the update/clipping region and reality may
2396 * not reflect Vims internal ideas if these operations are clipped away.
2397 */
2398 if (gui.row == gui.cursor_row)
2399 gui_update_cursor(TRUE, TRUE);
2400}
2401
2402/*
2403 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2404 * from col1 to col2 (inclusive).
2405 * Return TRUE when the character before the first drawn character has
2406 * different attributes (may have to be redrawn too).
2407 */
2408 int
2409gui_redraw_block(row1, col1, row2, col2, flags)
2410 int row1;
2411 int col1;
2412 int row2;
2413 int col2;
2414 int flags; /* flags for gui_outstr_nowrap() */
2415{
2416 int old_row, old_col;
2417 long_u old_hl_mask;
2418 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002419 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int idx, len;
2421 int back, nback;
2422 int retval = FALSE;
2423#ifdef FEAT_MBYTE
2424 int orig_col1, orig_col2;
2425#endif
2426
2427 /* Don't try to update when ScreenLines is not valid */
2428 if (!screen_cleared || ScreenLines == NULL)
2429 return retval;
2430
2431 /* Don't try to draw outside the shell! */
2432 /* Check everything, strange values may be caused by a big border width */
2433 col1 = check_col(col1);
2434 col2 = check_col(col2);
2435 row1 = check_row(row1);
2436 row2 = check_row(row2);
2437
2438 /* Remember where our cursor was */
2439 old_row = gui.row;
2440 old_col = gui.col;
2441 old_hl_mask = gui.highlight_mask;
2442#ifdef FEAT_MBYTE
2443 orig_col1 = col1;
2444 orig_col2 = col2;
2445#endif
2446
2447 for (gui.row = row1; gui.row <= row2; gui.row++)
2448 {
2449#ifdef FEAT_MBYTE
2450 /* When only half of a double-wide character is in the block, include
2451 * the other half. */
2452 col1 = orig_col1;
2453 col2 = orig_col2;
2454 off = LineOffset[gui.row];
2455 if (enc_dbcs != 0)
2456 {
2457 if (col1 > 0)
2458 col1 -= dbcs_screen_head_off(ScreenLines + off,
2459 ScreenLines + off + col1);
2460 col2 += dbcs_screen_tail_off(ScreenLines + off,
2461 ScreenLines + off + col2);
2462 }
2463 else if (enc_utf8)
2464 {
2465 if (ScreenLines[off + col1] == 0)
2466 --col1;
2467# ifdef HAVE_GTK2
2468 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2469 ++col2;
2470# endif
2471 }
2472#endif
2473 gui.col = col1;
2474 off = LineOffset[gui.row] + gui.col;
2475 len = col2 - col1 + 1;
2476
2477 /* Find how many chars back this highlighting starts, or where a space
2478 * is. Needed for when the bold trick is used */
2479 for (back = 0; back < col1; ++back)
2480 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2481 || ScreenLines[off - 1 - back] == ' ')
2482 break;
2483 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2484 && ScreenLines[off - 1] != ' ');
2485
2486 /* Break it up in strings of characters with the same attributes. */
2487 /* Print UTF-8 characters individually. */
2488 while (len > 0)
2489 {
2490 first_attr = ScreenAttrs[off];
2491 gui.highlight_mask = first_attr;
2492#if defined(FEAT_MBYTE) && !defined(HAVE_GTK2)
2493 if (enc_utf8 && ScreenLinesUC[off] != 0)
2494 {
2495 /* output multi-byte character separately */
2496 nback = gui_screenchar(off, flags,
2497 (guicolor_T)0, (guicolor_T)0, back);
2498 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2499 idx = 2;
2500 else
2501 idx = 1;
2502 }
2503 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2504 {
2505 /* output double-byte, single-width character separately */
2506 nback = gui_screenchar(off, flags,
2507 (guicolor_T)0, (guicolor_T)0, back);
2508 idx = 1;
2509 }
2510 else
2511#endif
2512 {
2513#ifdef HAVE_GTK2
2514 for (idx = 0; idx < len; ++idx)
2515 {
2516 if (enc_utf8 && ScreenLines[off + idx] == 0)
2517 continue; /* skip second half of double-width char */
2518 if (ScreenAttrs[off + idx] != first_attr)
2519 break;
2520 }
2521 /* gui_screenstr() takes care of multibyte chars */
2522 nback = gui_screenstr(off, idx, flags,
2523 (guicolor_T)0, (guicolor_T)0, back);
2524#else
2525 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2526 idx++)
2527 {
2528# ifdef FEAT_MBYTE
2529 /* Stop at a multi-byte Unicode character. */
2530 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2531 break;
2532 if (enc_dbcs == DBCS_JPNU)
2533 {
2534 /* Stop at a double-byte single-width char. */
2535 if (ScreenLines[off + idx] == 0x8e)
2536 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002537 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 + off + idx) == 2)
2539 ++idx; /* skip second byte of double-byte char */
2540 }
2541# endif
2542 }
2543 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2544 (guicolor_T)0, (guicolor_T)0, back);
2545#endif
2546 }
2547 if (nback == FAIL)
2548 {
2549 /* Must back up to start drawing where a bold or italic word
2550 * starts. */
2551 off -= back;
2552 len += back;
2553 gui.col -= back;
2554 }
2555 else
2556 {
2557 off += idx;
2558 len -= idx;
2559 }
2560 back = 0;
2561 }
2562 }
2563
2564 /* Put the cursor back where it was */
2565 gui.row = old_row;
2566 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002567 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
2569 return retval;
2570}
2571
2572 static void
2573gui_delete_lines(row, count)
2574 int row;
2575 int count;
2576{
2577 if (count <= 0)
2578 return;
2579
2580 if (row + count > gui.scroll_region_bot)
2581 /* Scrolled out of region, just blank the lines out */
2582 gui_clear_block(row, gui.scroll_region_left,
2583 gui.scroll_region_bot, gui.scroll_region_right);
2584 else
2585 {
2586 gui_mch_delete_lines(row, count);
2587
2588 /* If the cursor was in the deleted lines it's now gone. If the
2589 * cursor was in the scrolled lines adjust its position. */
2590 if (gui.cursor_row >= row
2591 && gui.cursor_col >= gui.scroll_region_left
2592 && gui.cursor_col <= gui.scroll_region_right)
2593 {
2594 if (gui.cursor_row < row + count)
2595 gui.cursor_is_valid = FALSE;
2596 else if (gui.cursor_row <= gui.scroll_region_bot)
2597 gui.cursor_row -= count;
2598 }
2599 }
2600}
2601
2602 static void
2603gui_insert_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_insert_lines(row, count);
2617
2618 if (gui.cursor_row >= gui.row
2619 && gui.cursor_col >= gui.scroll_region_left
2620 && gui.cursor_col <= gui.scroll_region_right)
2621 {
2622 if (gui.cursor_row <= gui.scroll_region_bot - count)
2623 gui.cursor_row += count;
2624 else if (gui.cursor_row <= gui.scroll_region_bot)
2625 gui.cursor_is_valid = FALSE;
2626 }
2627 }
2628}
2629
2630/*
2631 * The main GUI input routine. Waits for a character from the keyboard.
2632 * wtime == -1 Wait forever.
2633 * wtime == 0 Don't wait.
2634 * wtime > 0 Wait wtime milliseconds for a character.
2635 * Returns OK if a character was found to be available within the given time,
2636 * or FAIL otherwise.
2637 */
2638 int
2639gui_wait_for_chars(wtime)
2640 long wtime;
2641{
2642 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 /*
2645 * If we're going to wait a bit, update the menus and mouse shape for the
2646 * current State.
2647 */
2648 if (wtime != 0)
2649 {
2650#ifdef FEAT_MENU
2651 gui_update_menus(0);
2652#endif
2653 }
2654
2655 gui_mch_update();
2656 if (input_available()) /* Got char, return immediately */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (wtime == 0) /* Don't wait for char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 /* Before waiting, flush any output to the screen. */
2662 gui_mch_flush();
2663
2664 if (wtime > 0)
2665 {
2666 /* Blink when waiting for a character. Probably only does something
2667 * for showmatch() */
2668 gui_mch_start_blink();
2669 retval = gui_mch_wait_for_chars(wtime);
2670 gui_mch_stop_blink();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return retval;
2672 }
2673
2674 /*
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002675 * While we are waiting indefinitely for a character, blink the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
2677 gui_mch_start_blink();
2678
Bram Moolenaar3918c952005-03-15 22:34:55 +00002679 retval = FAIL;
2680 /*
2681 * We may want to trigger the CursorHold event. First wait for
2682 * 'updatetime' and if nothing is typed within that time put the
2683 * K_CURSORHOLD key in the input buffer.
2684 */
2685 if (gui_mch_wait_for_chars(p_ut) == OK)
2686 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#ifdef FEAT_AUTOCMD
Bram Moolenaard35f9712005-12-18 22:02:33 +00002688 else if (trigger_cursorhold())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
Bram Moolenaar3918c952005-03-15 22:34:55 +00002690 char_u buf[3];
2691
2692 /* Put K_CURSORHOLD in the input buffer. */
2693 buf[0] = CSI;
2694 buf[1] = KS_EXTRA;
2695 buf[2] = (int)KE_CURSORHOLD;
2696 add_to_input_buf(buf, 3);
2697
2698 retval = OK;
2699 }
2700#endif
2701
2702 if (retval == FAIL)
2703 {
2704 /* Blocking wait. */
Bram Moolenaar702517d2005-06-27 22:34:07 +00002705 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 retval = gui_mch_wait_for_chars(-1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 gui_mch_stop_blink();
2710 return retval;
2711}
2712
2713/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00002714 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
2716 static void
2717fill_mouse_coord(p, col, row)
2718 char_u *p;
2719 int col;
2720 int row;
2721{
2722 p[0] = (char_u)(col / 128 + ' ' + 1);
2723 p[1] = (char_u)(col % 128 + ' ' + 1);
2724 p[2] = (char_u)(row / 128 + ' ' + 1);
2725 p[3] = (char_u)(row % 128 + ' ' + 1);
2726}
2727
2728/*
2729 * Generic mouse support function. Add a mouse event to the input buffer with
2730 * the given properties.
2731 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
2732 * MOUSE_X1, MOUSE_X2
2733 * MOUSE_DRAG, or MOUSE_RELEASE.
2734 * MOUSE_4 and MOUSE_5 are used for a scroll wheel.
2735 * x, y --- Coordinates of mouse in pixels.
2736 * repeated_click --- TRUE if this click comes only a short time after a
2737 * previous click.
2738 * modifiers --- Bit field which may be any of the following modifiers
2739 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
2740 * This function will ignore drag events where the mouse has not moved to a new
2741 * character.
2742 */
2743 void
2744gui_send_mouse_event(button, x, y, repeated_click, modifiers)
2745 int button;
2746 int x;
2747 int y;
2748 int repeated_click;
2749 int_u modifiers;
2750{
2751 static int prev_row = 0, prev_col = 0;
2752 static int prev_button = -1;
2753 static int num_clicks = 1;
2754 char_u string[10];
2755 enum key_extra button_char;
2756 int row, col;
2757#ifdef FEAT_CLIPBOARD
2758 int checkfor;
2759 int did_clip = FALSE;
2760#endif
2761
2762 /*
2763 * Scrolling may happen at any time, also while a selection is present.
2764 */
2765 switch (button)
2766 {
2767 case MOUSE_X1:
2768 button_char = KE_X1MOUSE;
2769 goto button_set;
2770 case MOUSE_X2:
2771 button_char = KE_X2MOUSE;
2772 goto button_set;
2773 case MOUSE_4:
2774 button_char = KE_MOUSEDOWN;
2775 goto button_set;
2776 case MOUSE_5:
2777 button_char = KE_MOUSEUP;
2778button_set:
2779 {
2780 /* Don't put events in the input queue now. */
2781 if (hold_gui_events)
2782 return;
2783
2784 string[3] = CSI;
2785 string[4] = KS_EXTRA;
2786 string[5] = (int)button_char;
2787
2788 /* Pass the pointer coordinates of the scroll event so that we
2789 * know which window to scroll. */
2790 row = gui_xy2colrow(x, y, &col);
2791 string[6] = (char_u)(col / 128 + ' ' + 1);
2792 string[7] = (char_u)(col % 128 + ' ' + 1);
2793 string[8] = (char_u)(row / 128 + ' ' + 1);
2794 string[9] = (char_u)(row % 128 + ' ' + 1);
2795
2796 if (modifiers == 0)
2797 add_to_input_buf(string + 3, 7);
2798 else
2799 {
2800 string[0] = CSI;
2801 string[1] = KS_MODIFIER;
2802 string[2] = 0;
2803 if (modifiers & MOUSE_SHIFT)
2804 string[2] |= MOD_MASK_SHIFT;
2805 if (modifiers & MOUSE_CTRL)
2806 string[2] |= MOD_MASK_CTRL;
2807 if (modifiers & MOUSE_ALT)
2808 string[2] |= MOD_MASK_ALT;
2809 add_to_input_buf(string, 10);
2810 }
2811 return;
2812 }
2813 }
2814
2815#ifdef FEAT_CLIPBOARD
2816 /* If a clipboard selection is in progress, handle it */
2817 if (clip_star.state == SELECT_IN_PROGRESS)
2818 {
2819 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
2820 return;
2821 }
2822
2823 /* Determine which mouse settings to look for based on the current mode */
2824 switch (get_real_state())
2825 {
2826 case NORMAL_BUSY:
2827 case OP_PENDING:
2828 case NORMAL: checkfor = MOUSE_NORMAL; break;
2829 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00002830 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case REPLACE:
2832 case REPLACE+LANGMAP:
2833#ifdef FEAT_VREPLACE
2834 case VREPLACE:
2835 case VREPLACE+LANGMAP:
2836#endif
2837 case INSERT:
2838 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
2839 case ASKMORE:
2840 case HITRETURN: /* At the more- and hit-enter prompt pass the
2841 mouse event for a click on or below the
2842 message line. */
2843 if (Y_2_ROW(y) >= msg_row)
2844 checkfor = MOUSE_NORMAL;
2845 else
2846 checkfor = MOUSE_RETURN;
2847 break;
2848
2849 /*
2850 * On the command line, use the clipboard selection on all lines
2851 * but the command line. But not when pasting.
2852 */
2853 case CMDLINE:
2854 case CMDLINE+LANGMAP:
2855 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
2856 checkfor = MOUSE_NONE;
2857 else
2858 checkfor = MOUSE_COMMAND;
2859 break;
2860
2861 default:
2862 checkfor = MOUSE_NONE;
2863 break;
2864 };
2865
2866 /*
2867 * Allow clipboard selection of text on the command line in "normal"
2868 * modes. Don't do this when dragging the status line, or extending a
2869 * Visual selection.
2870 */
2871 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
2872 && Y_2_ROW(y) >= topframe->fr_height
Bram Moolenaar8838aee2006-10-08 11:56:24 +00002873# ifdef FEAT_WINDOWS
2874 + firstwin->w_winrow
2875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 && button != MOUSE_DRAG
2877# ifdef FEAT_MOUSESHAPE
2878 && !drag_status_line
2879# ifdef FEAT_VERTSPLIT
2880 && !drag_sep_line
2881# endif
2882# endif
2883 )
2884 checkfor = MOUSE_NONE;
2885
2886 /*
2887 * Use modeless selection when holding CTRL and SHIFT pressed.
2888 */
2889 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
2890 checkfor = MOUSE_NONEF;
2891
2892 /*
2893 * In Ex mode, always use modeless selection.
2894 */
2895 if (exmode_active)
2896 checkfor = MOUSE_NONE;
2897
2898 /*
2899 * If the mouse settings say to not use the mouse, use the modeless
2900 * selection. But if Visual is active, assume that only the Visual area
2901 * will be selected.
2902 * Exception: On the command line, both the selection is used and a mouse
2903 * key is send.
2904 */
2905 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
2906 {
2907#ifdef FEAT_VISUAL
2908 /* Don't do modeless selection in Visual mode. */
2909 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
2910 return;
2911#endif
2912
2913 /*
2914 * When 'mousemodel' is "popup", shift-left is translated to right.
2915 * But not when also using Ctrl.
2916 */
2917 if (mouse_model_popup() && button == MOUSE_LEFT
2918 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
2919 {
2920 button = MOUSE_RIGHT;
2921 modifiers &= ~ MOUSE_SHIFT;
2922 }
2923
2924 /* If the selection is done, allow the right button to extend it.
2925 * If the selection is cleared, allow the right button to start it
2926 * from the cursor position. */
2927 if (button == MOUSE_RIGHT)
2928 {
2929 if (clip_star.state == SELECT_CLEARED)
2930 {
2931 if (State & CMDLINE)
2932 {
2933 col = msg_col;
2934 row = msg_row;
2935 }
2936 else
2937 {
2938 col = curwin->w_wcol;
2939 row = curwin->w_wrow + W_WINROW(curwin);
2940 }
2941 clip_start_selection(col, row, FALSE);
2942 }
2943 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
2944 repeated_click);
2945 did_clip = TRUE;
2946 }
2947 /* Allow the left button to start the selection */
2948 else if (button ==
2949# ifdef RISCOS
2950 /* Only start a drag on a drag event. Otherwise
2951 * we don't get a release event. */
2952 MOUSE_DRAG
2953# else
2954 MOUSE_LEFT
2955# endif
2956 )
2957 {
2958 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
2959 did_clip = TRUE;
2960 }
2961# ifdef RISCOS
2962 else if (button == MOUSE_LEFT)
2963 {
2964 clip_clear_selection();
2965 did_clip = TRUE;
2966 }
2967# endif
2968
2969 /* Always allow pasting */
2970 if (button != MOUSE_MIDDLE)
2971 {
2972 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
2973 return;
2974 if (checkfor != MOUSE_COMMAND)
2975 button = MOUSE_LEFT;
2976 }
2977 repeated_click = FALSE;
2978 }
2979
2980 if (clip_star.state != SELECT_CLEARED && !did_clip)
2981 clip_clear_selection();
2982#endif
2983
2984 /* Don't put events in the input queue now. */
2985 if (hold_gui_events)
2986 return;
2987
2988 row = gui_xy2colrow(x, y, &col);
2989
2990 /*
2991 * If we are dragging and the mouse hasn't moved far enough to be on a
2992 * different character, then don't send an event to vim.
2993 */
2994 if (button == MOUSE_DRAG)
2995 {
2996 if (row == prev_row && col == prev_col)
2997 return;
2998 /* Dragging above the window, set "row" to -1 to cause a scroll. */
2999 if (y < 0)
3000 row = -1;
3001 }
3002
3003 /*
3004 * If topline has changed (window scrolled) since the last click, reset
3005 * repeated_click, because we don't want starting Visual mode when
3006 * clicking on a different character in the text.
3007 */
3008 if (curwin->w_topline != gui_prev_topline
3009#ifdef FEAT_DIFF
3010 || curwin->w_topfill != gui_prev_topfill
3011#endif
3012 )
3013 repeated_click = FALSE;
3014
3015 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3016 string[1] = KS_MOUSE;
3017 string[2] = KE_FILLER;
3018 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3019 {
3020 if (repeated_click)
3021 {
3022 /*
3023 * Handle multiple clicks. They only count if the mouse is still
3024 * pointing at the same character.
3025 */
3026 if (button != prev_button || row != prev_row || col != prev_col)
3027 num_clicks = 1;
3028 else if (++num_clicks > 4)
3029 num_clicks = 1;
3030 }
3031 else
3032 num_clicks = 1;
3033 prev_button = button;
3034 gui_prev_topline = curwin->w_topline;
3035#ifdef FEAT_DIFF
3036 gui_prev_topfill = curwin->w_topfill;
3037#endif
3038
3039 string[3] = (char_u)(button | 0x20);
3040 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3041 }
3042 else
3043 string[3] = (char_u)button;
3044
3045 string[3] |= modifiers;
3046 fill_mouse_coord(string + 4, col, row);
3047 add_to_input_buf(string, 8);
3048
3049 if (row < 0)
3050 prev_row = 0;
3051 else
3052 prev_row = row;
3053 prev_col = col;
3054
3055 /*
3056 * We need to make sure this is cleared since Athena doesn't tell us when
3057 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3058 */
3059#if defined(FEAT_GUI_ATHENA) || defined(HAVE_GTK2)
3060 gui.dragged_sb = SBAR_NONE;
3061#endif
3062}
3063
3064/*
3065 * Convert x and y coordinate to column and row in text window.
3066 * Corrects for multi-byte character.
3067 * returns column in "*colp" and row as return value;
3068 */
3069 int
3070gui_xy2colrow(x, y, colp)
3071 int x;
3072 int y;
3073 int *colp;
3074{
3075 int col = check_col(X_2_COL(x));
3076 int row = check_row(Y_2_ROW(y));
3077
3078#ifdef FEAT_MBYTE
3079 *colp = mb_fix_col(col, row);
3080#else
3081 *colp = col;
3082#endif
3083 return row;
3084}
3085
3086#if defined(FEAT_MENU) || defined(PROTO)
3087/*
3088 * Callback function for when a menu entry has been selected.
3089 */
3090 void
3091gui_menu_cb(menu)
3092 vimmenu_T *menu;
3093{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003094 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /* Don't put events in the input queue now. */
3097 if (hold_gui_events)
3098 return;
3099
3100 bytes[0] = CSI;
3101 bytes[1] = KS_MENU;
3102 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003103 add_to_input_buf(bytes, 3);
3104 add_long_to_buf((long_u)menu, bytes);
3105 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107#endif
3108
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003109static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111/*
3112 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003113 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 * in p_go.
3115 */
3116/*ARGSUSED*/
3117 void
3118gui_init_which_components(oldval)
3119 char_u *oldval;
3120{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#ifdef FEAT_MENU
3122 static int prev_menu_is_active = -1;
3123#endif
3124#ifdef FEAT_TOOLBAR
3125 static int prev_toolbar = -1;
3126 int using_toolbar = FALSE;
3127#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003128#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003129 int using_tabline;
3130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#ifdef FEAT_FOOTER
3132 static int prev_footer = -1;
3133 int using_footer = FALSE;
3134#endif
3135#if defined(FEAT_MENU) && !defined(WIN16)
3136 static int prev_tearoff = -1;
3137 int using_tearoff = FALSE;
3138#endif
3139
3140 char_u *p;
3141 int i;
3142#ifdef FEAT_MENU
3143 int grey_old, grey_new;
3144 char_u *temp;
3145#endif
3146 win_T *wp;
3147 int need_set_size;
3148 int fix_size;
3149
3150#ifdef FEAT_MENU
3151 if (oldval != NULL && gui.in_use)
3152 {
3153 /*
3154 * Check if the menu's go from grey to non-grey or vise versa.
3155 */
3156 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3157 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3158 if (grey_old != grey_new)
3159 {
3160 temp = p_go;
3161 p_go = oldval;
3162 gui_update_menus(MENU_ALL_MODES);
3163 p_go = temp;
3164 }
3165 }
3166 gui.menu_is_active = FALSE;
3167#endif
3168
3169 for (i = 0; i < 3; i++)
3170 gui.which_scrollbars[i] = FALSE;
3171 for (p = p_go; *p; p++)
3172 switch (*p)
3173 {
3174 case GO_LEFT:
3175 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3176 break;
3177 case GO_RIGHT:
3178 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3179 break;
3180#ifdef FEAT_VERTSPLIT
3181 case GO_VLEFT:
3182 if (win_hasvertsplit())
3183 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3184 break;
3185 case GO_VRIGHT:
3186 if (win_hasvertsplit())
3187 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3188 break;
3189#endif
3190 case GO_BOT:
3191 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3192 break;
3193#ifdef FEAT_MENU
3194 case GO_MENUS:
3195 gui.menu_is_active = TRUE;
3196 break;
3197#endif
3198 case GO_GREY:
3199 /* make menu's have grey items, ignored here */
3200 break;
3201#ifdef FEAT_TOOLBAR
3202 case GO_TOOLBAR:
3203 using_toolbar = TRUE;
3204 break;
3205#endif
3206#ifdef FEAT_FOOTER
3207 case GO_FOOTER:
3208 using_footer = TRUE;
3209 break;
3210#endif
3211 case GO_TEAROFF:
3212#if defined(FEAT_MENU) && !defined(WIN16)
3213 using_tearoff = TRUE;
3214#endif
3215 break;
3216 default:
3217 /* Ignore options that are not supported */
3218 break;
3219 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (gui.in_use)
3222 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003223 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003225
3226#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003227 /* Update the GUI tab line, it may appear or disappear. This may
3228 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003229 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003230 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003231 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003232 /* We don't want a resize event change "Rows" here, save and
3233 * restore it. Resizing is handled below. */
3234 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003235 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003236 Rows = i;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003237 need_set_size = RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003238 if (using_tabline)
3239 fix_size = TRUE;
3240 if (!gui_use_tabline())
3241 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3242 }
3243#endif
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 for (i = 0; i < 3; i++)
3246 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003247 /* The scrollbar needs to be updated when it is shown/unshown and
3248 * when switching tab pages. But the size only changes when it's
3249 * shown/unshown. Thus we need two places to remember whether a
3250 * scrollbar is there or not. */
3251 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003252#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003253 || gui.which_scrollbars[i]
3254 != curtab->tp_prev_which_scrollbars[i]
Bram Moolenaar371d5402006-03-20 21:47:49 +00003255#endif
3256 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
3258 if (i == SBAR_BOTTOM)
3259 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3260 gui.which_scrollbars[i]);
3261 else
3262 {
3263 FOR_ALL_WINDOWS(wp)
3264 {
3265 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3266 }
3267 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003268 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3269 {
3270 if (i == SBAR_BOTTOM)
3271 need_set_size = RESIZE_VERT;
3272 else
3273 need_set_size = RESIZE_HOR;
3274 if (gui.which_scrollbars[i])
3275 fix_size = TRUE;
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003278#ifdef FEAT_WINDOWS
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003279 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar371d5402006-03-20 21:47:49 +00003280#endif
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003281 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283
3284#ifdef FEAT_MENU
3285 if (gui.menu_is_active != prev_menu_is_active)
3286 {
3287 /* We don't want a resize event change "Rows" here, save and
3288 * restore it. Resizing is handled below. */
3289 i = Rows;
3290 gui_mch_enable_menu(gui.menu_is_active);
3291 Rows = i;
3292 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003293 need_set_size = RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 if (gui.menu_is_active)
3295 fix_size = TRUE;
3296 }
3297#endif
3298
3299#ifdef FEAT_TOOLBAR
3300 if (using_toolbar != prev_toolbar)
3301 {
3302 gui_mch_show_toolbar(using_toolbar);
3303 prev_toolbar = using_toolbar;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003304 need_set_size = RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (using_toolbar)
3306 fix_size = TRUE;
3307 }
3308#endif
3309#ifdef FEAT_FOOTER
3310 if (using_footer != prev_footer)
3311 {
3312 gui_mch_enable_footer(using_footer);
3313 prev_footer = using_footer;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003314 need_set_size = RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (using_footer)
3316 fix_size = TRUE;
3317 }
3318#endif
3319#if defined(FEAT_MENU) && !defined(WIN16) && !(defined(WIN3264) && !defined(FEAT_TEAROFF))
3320 if (using_tearoff != prev_tearoff)
3321 {
3322 gui_mch_toggle_tearoffs(using_tearoff);
3323 prev_tearoff = using_tearoff;
3324 }
3325#endif
3326 if (need_set_size)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003327 {
3328#ifdef FEAT_GUI_GTK
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003329 long c = Columns;
3330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 /* Adjust the size of the window to make the text area keep the
3332 * same size and to avoid that part of our window is off-screen
3333 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003334 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003335
3336#ifdef FEAT_GUI_GTK
3337 /* GTK has the annoying habit of sending us resize events when
3338 * changing the window size ourselves. This mostly happens when
3339 * waiting for a character to arrive, quite unpredictably, and may
3340 * change Columns and Rows when we don't want it. Wait for a
3341 * character here to avoid this effect.
3342 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003343 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003344 * Don't do this while starting up though.
3345 * And don't change Rows, it may have be reduced intentionally
3346 * when adding menu/toolbar/tabline. */
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003347 if (!gui.starting)
3348 (void)char_avail();
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003349 Columns = c;
3350#endif
3351 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003352#ifdef FEAT_WINDOWS
3353 /* When the console tabline appears or disappears the window positions
3354 * change. */
3355 if (firstwin->w_winrow != tabline_height())
3356 shell_new_rows(); /* recompute window positions and heights */
3357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359}
3360
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003361#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3362/*
3363 * Return TRUE if the GUI is taking care of the tabline.
3364 * It may still be hidden if 'showtabline' is zero.
3365 */
3366 int
3367gui_use_tabline()
3368{
3369 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3370}
3371
3372/*
3373 * Return TRUE if the GUI is showing the tabline.
3374 * This uses 'showtabline'.
3375 */
3376 static int
3377gui_has_tabline()
3378{
3379 if (!gui_use_tabline()
3380 || p_stal == 0
3381 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3382 return FALSE;
3383 return TRUE;
3384}
3385
3386/*
3387 * Update the tabline.
3388 * This may display/undisplay the tabline and update the labels.
3389 */
3390 void
3391gui_update_tabline()
3392{
3393 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003394 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003395
3396 if (!gui.starting && starting == 0)
3397 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003398 /* Updating the tabline uses direct GUI commands, flush
3399 * outstanding instructions first. (esp. clear screen) */
3400 out_flush();
3401 gui_mch_flush();
3402
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003403 if (!showit != !shown)
3404 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003405 if (showit != 0)
3406 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003407
3408 /* When the tabs change from hidden to shown or from shown to
3409 * hidden the size of the text area should remain the same. */
3410 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003411 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003412 }
3413}
3414
3415/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003416 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003417 */
3418 void
Bram Moolenaar57657d82006-04-21 22:12:41 +00003419get_tabline_label(tp, tooltip)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003420 tabpage_T *tp;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003421 int tooltip; /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003422{
3423 int modified = FALSE;
3424 char_u buf[40];
3425 int wincount;
3426 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003427 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003428
Bram Moolenaar57657d82006-04-21 22:12:41 +00003429 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003430 opt = (tooltip ? &p_gtt : &p_gtl);
3431 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003432 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003433 int use_sandbox = FALSE;
3434 int save_called_emsg = called_emsg;
3435 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003436 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003437 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3438 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003439
3440 called_emsg = FALSE;
3441
3442 printer_page_num = tabpage_index(tp);
3443# ifdef FEAT_EVAL
3444 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003445 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003446# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003447 /* It's almost as going to the tabpage, but without autocommands. */
3448 curtab->tp_firstwin = firstwin;
3449 curtab->tp_lastwin = lastwin;
3450 curtab->tp_curwin = curwin;
3451 save_curtab = curtab;
3452 curtab = tp;
3453 topframe = curtab->tp_topframe;
3454 firstwin = curtab->tp_firstwin;
3455 lastwin = curtab->tp_lastwin;
3456 curwin = curtab->tp_curwin;
3457 curbuf = curwin->w_buffer;
3458
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003459 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003460 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003461 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003462 STRCPY(NameBuff, res);
3463
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003464 /* Back to the original curtab. */
3465 curtab = save_curtab;
3466 topframe = curtab->tp_topframe;
3467 firstwin = curtab->tp_firstwin;
3468 lastwin = curtab->tp_lastwin;
3469 curwin = curtab->tp_curwin;
3470 curbuf = curwin->w_buffer;
3471
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003472 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003473 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003474 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003475 called_emsg |= save_called_emsg;
3476 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003477
3478 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3479 * use a default label. */
3480 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003481 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003482 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003483 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003484 if (!tooltip)
3485 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003486
3487 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3488 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3489 if (bufIsChanged(wp->w_buffer))
3490 modified = TRUE;
3491 if (modified || wincount > 1)
3492 {
3493 if (wincount > 1)
3494 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3495 else
3496 buf[0] = NUL;
3497 if (modified)
3498 STRCAT(buf, "+");
3499 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003500 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003501 mch_memmove(NameBuff, buf, STRLEN(buf));
3502 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003503 }
3504}
3505
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003506/*
3507 * Send the event for clicking to select tab page "nr".
3508 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003509 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003510 */
3511 int
3512send_tabline_event(nr)
3513 int nr;
3514{
3515 char_u string[3];
3516
3517 if (nr == tabpage_index(curtab))
3518 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003519
3520 /* Don't put events in the input queue now. */
3521 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003522# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003523 || cmdwin_type != 0
3524# endif
3525 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003526 {
3527 /* Set it back to the current tab page. */
3528 gui_mch_set_curtab(tabpage_index(curtab));
3529 return FALSE;
3530 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003531
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003532 string[0] = CSI;
3533 string[1] = KS_TABLINE;
3534 string[2] = KE_FILLER;
3535 add_to_input_buf(string, 3);
3536 string[0] = nr;
3537 add_to_input_buf_csi(string, 1);
3538 return TRUE;
3539}
3540
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003541/*
3542 * Send a tabline menu event
3543 */
3544 void
3545send_tabline_menu_event(tabidx, event)
3546 int tabidx;
3547 int event;
3548{
3549 char_u string[3];
3550
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003551 /* Don't put events in the input queue now. */
3552 if (hold_gui_events)
3553 return;
3554
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003555 string[0] = CSI;
3556 string[1] = KS_TABMENU;
3557 string[2] = KE_FILLER;
3558 add_to_input_buf(string, 3);
3559 string[0] = tabidx;
3560 string[1] = (char_u)(long)event;
3561 add_to_input_buf_csi(string, 2);
3562}
3563
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566/*
3567 * Scrollbar stuff:
3568 */
3569
Bram Moolenaare45828b2006-02-15 22:12:56 +00003570#if defined(FEAT_WINDOWS) || defined(PROTO)
3571/*
3572 * Remove all scrollbars. Used before switching to another tab page.
3573 */
3574 void
3575gui_remove_scrollbars()
3576{
3577 int i;
3578 win_T *wp;
3579
3580 for (i = 0; i < 3; i++)
3581 {
3582 if (i == SBAR_BOTTOM)
3583 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3584 else
3585 {
3586 FOR_ALL_WINDOWS(wp)
3587 {
3588 gui_do_scrollbar(wp, i, FALSE);
3589 }
3590 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003591 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003592 }
3593}
3594#endif
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 void
3597gui_create_scrollbar(sb, type, wp)
3598 scrollbar_T *sb;
3599 int type;
3600 win_T *wp;
3601{
3602 static int sbar_ident = 0;
3603
3604 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3605 sb->wp = wp;
3606 sb->type = type;
3607 sb->value = 0;
3608#ifdef FEAT_GUI_ATHENA
3609 sb->pixval = 0;
3610#endif
3611 sb->size = 1;
3612 sb->max = 1;
3613 sb->top = 0;
3614 sb->height = 0;
3615#ifdef FEAT_VERTSPLIT
3616 sb->width = 0;
3617#endif
3618 sb->status_height = 0;
3619 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3620}
3621
3622/*
3623 * Find the scrollbar with the given index.
3624 */
3625 scrollbar_T *
3626gui_find_scrollbar(ident)
3627 long ident;
3628{
3629 win_T *wp;
3630
3631 if (gui.bottom_sbar.ident == ident)
3632 return &gui.bottom_sbar;
3633 FOR_ALL_WINDOWS(wp)
3634 {
3635 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3636 return &wp->w_scrollbars[SBAR_LEFT];
3637 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3638 return &wp->w_scrollbars[SBAR_RIGHT];
3639 }
3640 return NULL;
3641}
3642
3643/*
3644 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3645 *
3646 * For Win32, Macintosh and GTK+ 2:
3647 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3648 * you stop dragging the scrollbar. We get here each time the scrollbar is
3649 * dragged another pixel, but as far as the rest of vim goes, it thinks
3650 * we're just hanging in the call to DispatchMessage() in
3651 * process_message(). The DispatchMessage() call that hangs was passed a
3652 * mouse button click event in the scrollbar window. -- webb.
3653 *
3654 * Solution: Do the scrolling right here. But only when allowed.
3655 * Ignore the scrollbars while executing an external command or when there
3656 * are still characters to be processed.
3657 */
3658 void
3659gui_drag_scrollbar(sb, value, still_dragging)
3660 scrollbar_T *sb;
3661 long value;
3662 int still_dragging;
3663{
3664#ifdef FEAT_WINDOWS
3665 win_T *wp;
3666#endif
3667 int sb_num;
3668#ifdef USE_ON_FLY_SCROLL
3669 colnr_T old_leftcol = curwin->w_leftcol;
3670# ifdef FEAT_SCROLLBIND
3671 linenr_T old_topline = curwin->w_topline;
3672# endif
3673# ifdef FEAT_DIFF
3674 int old_topfill = curwin->w_topfill;
3675# endif
3676#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003677 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 int byte_count;
3679#endif
3680
3681 if (sb == NULL)
3682 return;
3683
3684 /* Don't put events in the input queue now. */
3685 if (hold_gui_events)
3686 return;
3687
3688#ifdef FEAT_CMDWIN
3689 if (cmdwin_type != 0 && sb->wp != curwin)
3690 return;
3691#endif
3692
3693 if (still_dragging)
3694 {
3695 if (sb->wp == NULL)
3696 gui.dragged_sb = SBAR_BOTTOM;
3697 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3698 gui.dragged_sb = SBAR_LEFT;
3699 else
3700 gui.dragged_sb = SBAR_RIGHT;
3701 gui.dragged_wp = sb->wp;
3702 }
3703 else
3704 {
3705 gui.dragged_sb = SBAR_NONE;
3706#ifdef HAVE_GTK2
3707 /* Keep the "dragged_wp" value until after the scrolling, for when the
3708 * moust button is released. GTK2 doesn't send the button-up event. */
3709 gui.dragged_wp = NULL;
3710#endif
3711 }
3712
3713 /* Vertical sbar info is kept in the first sbar (the left one) */
3714 if (sb->wp != NULL)
3715 sb = &sb->wp->w_scrollbars[0];
3716
3717 /*
3718 * Check validity of value
3719 */
3720 if (value < 0)
3721 value = 0;
3722#ifdef SCROLL_PAST_END
3723 else if (value > sb->max)
3724 value = sb->max;
3725#else
3726 if (value > sb->max - sb->size + 1)
3727 value = sb->max - sb->size + 1;
3728#endif
3729
3730 sb->value = value;
3731
3732#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003733 /* When not allowed to do the scrolling right now, return.
3734 * This also checked input_available(), but that causes the first click in
3735 * a scrollbar to be ignored when Vim doesn't have focus. */
3736 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 return;
3738#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003739#ifdef FEAT_INS_EXPAND
3740 /* Disallow scrolling the current window when the completion popup menu is
3741 * visible. */
3742 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3743 return;
3744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746#ifdef FEAT_RIGHTLEFT
3747 if (sb->wp == NULL && curwin->w_p_rl)
3748 {
3749 value = sb->max + 1 - sb->size - value;
3750 if (value < 0)
3751 value = 0;
3752 }
3753#endif
3754
3755 if (sb->wp != NULL) /* vertical scrollbar */
3756 {
3757 sb_num = 0;
3758#ifdef FEAT_WINDOWS
3759 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3760 sb_num++;
3761 if (wp == NULL)
3762 return;
3763#else
3764 if (sb->wp != curwin)
3765 return;
3766#endif
3767
3768#ifdef USE_ON_FLY_SCROLL
3769 current_scrollbar = sb_num;
3770 scrollbar_value = value;
3771 if (State & NORMAL)
3772 {
3773 gui_do_scroll();
3774 setcursor();
3775 }
3776 else if (State & INSERT)
3777 {
3778 ins_scroll();
3779 setcursor();
3780 }
3781 else if (State & CMDLINE)
3782 {
3783 if (msg_scrolled == 0)
3784 {
3785 gui_do_scroll();
3786 redrawcmdline();
3787 }
3788 }
3789# ifdef FEAT_FOLDING
3790 /* Value may have been changed for closed fold. */
3791 sb->value = sb->wp->w_topline - 1;
3792# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00003793
3794 /* When dragging one scrollbar and there is another one at the other
3795 * side move the thumb of that one too. */
3796 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
3797 gui_mch_set_scrollbar_thumb(
3798 &sb->wp->w_scrollbars[
3799 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
3800 ? SBAR_LEFT : SBAR_RIGHT],
3801 sb->value, sb->size, sb->max);
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#else
3804 bytes[0] = CSI;
3805 bytes[1] = KS_VER_SCROLLBAR;
3806 bytes[2] = KE_FILLER;
3807 bytes[3] = (char_u)sb_num;
3808 byte_count = 4;
3809#endif
3810 }
3811 else
3812 {
3813#ifdef USE_ON_FLY_SCROLL
3814 scrollbar_value = value;
3815
3816 if (State & NORMAL)
3817 gui_do_horiz_scroll();
3818 else if (State & INSERT)
3819 ins_horscroll();
3820 else if (State & CMDLINE)
3821 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003822 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 gui_do_horiz_scroll();
3825 redrawcmdline();
3826 }
3827 }
3828 if (old_leftcol != curwin->w_leftcol)
3829 {
3830 updateWindow(curwin); /* update window, status and cmdline */
3831 setcursor();
3832 }
3833#else
3834 bytes[0] = CSI;
3835 bytes[1] = KS_HOR_SCROLLBAR;
3836 bytes[2] = KE_FILLER;
3837 byte_count = 3;
3838#endif
3839 }
3840
3841#ifdef USE_ON_FLY_SCROLL
3842# ifdef FEAT_SCROLLBIND
3843 /*
3844 * synchronize other windows, as necessary according to 'scrollbind'
3845 */
3846 if (curwin->w_p_scb
3847 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
3848 || (sb->wp == curwin && (curwin->w_topline != old_topline
3849# ifdef FEAT_DIFF
3850 || curwin->w_topfill != old_topfill
3851# endif
3852 ))))
3853 {
3854 do_check_scrollbind(TRUE);
3855 /* need to update the window right here */
3856 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3857 if (wp->w_redr_type > 0)
3858 updateWindow(wp);
3859 setcursor();
3860 }
3861# endif
3862 out_flush();
3863 gui_update_cursor(FALSE, TRUE);
3864#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003865 add_to_input_buf(bytes, byte_count);
3866 add_long_to_buf((long_u)value, bytes);
3867 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868#endif
3869}
3870
3871/*
3872 * Scrollbar stuff:
3873 */
3874
3875 void
3876gui_update_scrollbars(force)
3877 int force; /* Force all scrollbars to get updated */
3878{
3879 win_T *wp;
3880 scrollbar_T *sb;
3881 long val, size, max; /* need 32 bits here */
3882 int which_sb;
3883 int h, y;
3884#ifdef FEAT_VERTSPLIT
3885 static win_T *prev_curwin = NULL;
3886#endif
3887
3888 /* Update the horizontal scrollbar */
3889 gui_update_horiz_scrollbar(force);
3890
3891#ifndef WIN3264
3892 /* Return straight away if there is neither a left nor right scrollbar.
3893 * On MS-Windows this is required anyway for scrollwheel messages. */
3894 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
3895 return;
3896#endif
3897
3898 /*
3899 * Don't want to update a scrollbar while we're dragging it. But if we
3900 * have both a left and right scrollbar, and we drag one of them, we still
3901 * need to update the other one.
3902 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003903 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
3904 && gui.which_scrollbars[SBAR_LEFT]
3905 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
3907 /*
3908 * If we have two scrollbars and one of them is being dragged, just
3909 * copy the scrollbar position from the dragged one to the other one.
3910 */
3911 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
3912 if (gui.dragged_wp != NULL)
3913 gui_mch_set_scrollbar_thumb(
3914 &gui.dragged_wp->w_scrollbars[which_sb],
3915 gui.dragged_wp->w_scrollbars[0].value,
3916 gui.dragged_wp->w_scrollbars[0].size,
3917 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919
3920 /* avoid that moving components around generates events */
3921 ++hold_gui_events;
3922
3923 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
3924 {
3925 if (wp->w_buffer == NULL) /* just in case */
3926 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003927 /* Skip a scrollbar that is being dragged. */
3928 if (!force && (gui.dragged_sb == SBAR_LEFT
3929 || gui.dragged_sb == SBAR_RIGHT)
3930 && gui.dragged_wp == wp)
3931 continue;
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#ifdef SCROLL_PAST_END
3934 max = wp->w_buffer->b_ml.ml_line_count - 1;
3935#else
3936 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
3937#endif
3938 if (max < 0) /* empty buffer */
3939 max = 0;
3940 val = wp->w_topline - 1;
3941 size = wp->w_height;
3942#ifdef SCROLL_PAST_END
3943 if (val > max) /* just in case */
3944 val = max;
3945#else
3946 if (size > max + 1) /* just in case */
3947 size = max + 1;
3948 if (val > max - size + 1)
3949 val = max - size + 1;
3950#endif
3951 if (val < 0) /* minimal value is 0 */
3952 val = 0;
3953
3954 /*
3955 * Scrollbar at index 0 (the left one) contains all the information.
3956 * It would be the same info for left and right so we just store it for
3957 * one of them.
3958 */
3959 sb = &wp->w_scrollbars[0];
3960
3961 /*
3962 * Note: no check for valid w_botline. If it's not valid the
3963 * scrollbars will be updated later anyway.
3964 */
3965 if (size < 1 || wp->w_botline - 2 > max)
3966 {
3967 /*
3968 * This can happen during changing files. Just don't update the
3969 * scrollbar for now.
3970 */
3971 sb->height = 0; /* Force update next time */
3972 if (gui.which_scrollbars[SBAR_LEFT])
3973 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
3974 if (gui.which_scrollbars[SBAR_RIGHT])
3975 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
3976 continue;
3977 }
3978 if (force || sb->height != wp->w_height
3979#ifdef FEAT_WINDOWS
3980 || sb->top != wp->w_winrow
3981 || sb->status_height != wp->w_status_height
3982# ifdef FEAT_VERTSPLIT
3983 || sb->width != wp->w_width
3984 || prev_curwin != curwin
3985# endif
3986#endif
3987 )
3988 {
3989 /* Height, width or position of scrollbar has changed. For
3990 * vertical split: curwin changed. */
3991 sb->height = wp->w_height;
3992#ifdef FEAT_WINDOWS
3993 sb->top = wp->w_winrow;
3994 sb->status_height = wp->w_status_height;
3995# ifdef FEAT_VERTSPLIT
3996 sb->width = wp->w_width;
3997# endif
3998#endif
3999
4000 /* Calculate height and position in pixels */
4001 h = (sb->height + sb->status_height) * gui.char_height;
4002 y = sb->top * gui.char_height + gui.border_offset;
4003#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4004 if (gui.menu_is_active)
4005 y += gui.menu_height;
4006#endif
4007
4008#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4009 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4010# ifdef FEAT_GUI_ATHENA
4011 y += gui.toolbar_height;
4012# else
4013# ifdef FEAT_GUI_MSWIN
4014 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4015# endif
4016# endif
4017#endif
4018
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004019#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4020 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004021 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004022#endif
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024#ifdef FEAT_WINDOWS
4025 if (wp->w_winrow == 0)
4026#endif
4027 {
4028 /* Height of top scrollbar includes width of top border */
4029 h += gui.border_offset;
4030 y -= gui.border_offset;
4031 }
4032 if (gui.which_scrollbars[SBAR_LEFT])
4033 {
4034 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4035 gui.left_sbar_x, y,
4036 gui.scrollbar_width, h);
4037 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4038 }
4039 if (gui.which_scrollbars[SBAR_RIGHT])
4040 {
4041 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4042 gui.right_sbar_x, y,
4043 gui.scrollbar_width, h);
4044 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4045 }
4046 }
4047
4048 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4049 * checking if the thumb moved at least a pixel. Only do this for
4050 * Athena, most other GUIs require the update anyway to make the
4051 * arrows work. */
4052#ifdef FEAT_GUI_ATHENA
4053 if (max == 0)
4054 y = 0;
4055 else
4056 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4057 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4058#else
4059 if (force || sb->value != val || sb->size != size || sb->max != max)
4060#endif
4061 {
4062 /* Thumb of scrollbar has moved */
4063 sb->value = val;
4064#ifdef FEAT_GUI_ATHENA
4065 sb->pixval = y;
4066#endif
4067 sb->size = size;
4068 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004069 if (gui.which_scrollbars[SBAR_LEFT]
4070 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4072 val, size, max);
4073 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004074 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4076 val, size, max);
4077 }
4078 }
4079#ifdef FEAT_VERTSPLIT
4080 prev_curwin = curwin;
4081#endif
4082 --hold_gui_events;
4083}
4084
4085/*
4086 * Enable or disable a scrollbar.
4087 * Check for scrollbars for vertically split windows which are not enabled
4088 * sometimes.
4089 */
4090 static void
4091gui_do_scrollbar(wp, which, enable)
4092 win_T *wp;
4093 int which; /* SBAR_LEFT or SBAR_RIGHT */
4094 int enable; /* TRUE to enable scrollbar */
4095{
4096#ifdef FEAT_VERTSPLIT
4097 int midcol = curwin->w_wincol + curwin->w_width / 2;
4098 int has_midcol = (wp->w_wincol <= midcol
4099 && wp->w_wincol + wp->w_width >= midcol);
4100
4101 /* Only enable scrollbars that contain the middle column of the current
4102 * window. */
4103 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4104 {
4105 /* Scrollbars only on one side. Don't enable scrollbars that don't
4106 * contain the middle column of the current window. */
4107 if (!has_midcol)
4108 enable = FALSE;
4109 }
4110 else
4111 {
4112 /* Scrollbars on both sides. Don't enable scrollbars that neither
4113 * contain the middle column of the current window nor are on the far
4114 * side. */
4115 if (midcol > Columns / 2)
4116 {
4117 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4118 enable = FALSE;
4119 }
4120 else
4121 {
4122 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4123 : !has_midcol)
4124 enable = FALSE;
4125 }
4126 }
4127#endif
4128 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4129}
4130
4131/*
4132 * Scroll a window according to the values set in the globals current_scrollbar
4133 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4134 * or FALSE otherwise.
4135 */
4136 int
4137gui_do_scroll()
4138{
4139 win_T *wp, *save_wp;
4140 int i;
4141 long nlines;
4142 pos_T old_cursor;
4143 linenr_T old_topline;
4144#ifdef FEAT_DIFF
4145 int old_topfill;
4146#endif
4147
4148 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4149 if (wp == NULL)
4150 break;
4151 if (wp == NULL)
4152 /* Couldn't find window */
4153 return FALSE;
4154
4155 /*
4156 * Compute number of lines to scroll. If zero, nothing to do.
4157 */
4158 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4159 if (nlines == 0)
4160 return FALSE;
4161
4162 save_wp = curwin;
4163 old_topline = wp->w_topline;
4164#ifdef FEAT_DIFF
4165 old_topfill = wp->w_topfill;
4166#endif
4167 old_cursor = wp->w_cursor;
4168 curwin = wp;
4169 curbuf = wp->w_buffer;
4170 if (nlines < 0)
4171 scrolldown(-nlines, gui.dragged_wp == NULL);
4172 else
4173 scrollup(nlines, gui.dragged_wp == NULL);
4174 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4175 * the mouse-up event already, but we still want it to behave like when
4176 * dragging. But not the next click in an arrow. */
4177 if (gui.dragged_sb == SBAR_NONE)
4178 gui.dragged_wp = NULL;
4179
4180 if (old_topline != wp->w_topline
4181#ifdef FEAT_DIFF
4182 || old_topfill != wp->w_topfill
4183#endif
4184 )
4185 {
4186 if (p_so != 0)
4187 {
4188 cursor_correct(); /* fix window for 'so' */
4189 update_topline(); /* avoid up/down jump */
4190 }
4191 if (old_cursor.lnum != wp->w_cursor.lnum)
4192 coladvance(wp->w_curswant);
4193#ifdef FEAT_SCROLLBIND
4194 wp->w_scbind_pos = wp->w_topline;
4195#endif
4196 }
4197
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004198 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4199 validate_cursor();
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 curwin = save_wp;
4202 curbuf = save_wp->w_buffer;
4203
4204 /*
4205 * Don't call updateWindow() when nothing has changed (it will overwrite
4206 * the status line!).
4207 */
4208 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004209 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210#ifdef FEAT_DIFF
4211 || old_topfill != wp->w_topfill
4212#endif
4213 )
4214 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004215 int type = VALID;
4216
4217#ifdef FEAT_INS_EXPAND
4218 if (pum_visible())
4219 {
4220 type = NOT_VALID;
4221 wp->w_lines_valid = 0;
4222 }
4223#endif
4224 /* Don't set must_redraw here, it may cause the popup menu to
4225 * disappear when losing focus after a scrollbar drag. */
4226 if (wp->w_redr_type < type)
4227 wp->w_redr_type = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 updateWindow(wp); /* update window, status line, and cmdline */
4229 }
4230
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004231#ifdef FEAT_INS_EXPAND
4232 /* May need to redraw the popup menu. */
4233 if (pum_visible())
4234 pum_redraw();
4235#endif
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 return (wp == curwin && !equalpos(curwin->w_cursor, old_cursor));
4238}
4239
4240
4241/*
4242 * Horizontal scrollbar stuff:
4243 */
4244
4245/*
4246 * Return length of line "lnum" for horizontal scrolling.
4247 */
4248 static colnr_T
4249scroll_line_len(lnum)
4250 linenr_T lnum;
4251{
4252 char_u *p;
4253 colnr_T col;
4254 int w;
4255
4256 p = ml_get(lnum);
4257 col = 0;
4258 if (*p != NUL)
4259 for (;;)
4260 {
4261 w = chartabsize(p, col);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004262 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (*p == NUL) /* don't count the last character */
4264 break;
4265 col += w;
4266 }
4267 return col;
4268}
4269
4270/* Remember which line is currently the longest, so that we don't have to
4271 * search for it when scrolling horizontally. */
4272static linenr_T longest_lnum = 0;
4273
4274 static void
4275gui_update_horiz_scrollbar(force)
4276 int force;
4277{
4278 long value, size, max; /* need 32 bit ints here */
4279
4280 if (!gui.which_scrollbars[SBAR_BOTTOM])
4281 return;
4282
4283 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4284 return;
4285
4286 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4287 return;
4288
4289 /*
4290 * It is possible for the cursor to be invalid if we're in the middle of
4291 * something (like changing files). If so, don't do anything for now.
4292 */
4293 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4294 {
4295 gui.bottom_sbar.value = -1;
4296 return;
4297 }
4298
4299 size = W_WIDTH(curwin);
4300 if (curwin->w_p_wrap)
4301 {
4302 value = 0;
4303#ifdef SCROLL_PAST_END
4304 max = 0;
4305#else
4306 max = W_WIDTH(curwin) - 1;
4307#endif
4308 }
4309 else
4310 {
4311 value = curwin->w_leftcol;
4312
4313 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4314 * line numbers, topline and botline can be invalid when displaying is
4315 * postponed. */
4316 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4317 && curwin->w_topline <= curwin->w_cursor.lnum
4318 && curwin->w_botline > curwin->w_cursor.lnum
4319 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4320 {
4321 linenr_T lnum;
4322 colnr_T n;
4323
4324 /* Use maximum of all visible lines. Remember the lnum of the
4325 * longest line, clostest to the cursor line. Used when scrolling
4326 * below. */
4327 max = 0;
4328 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4329 {
4330 n = scroll_line_len(lnum);
4331 if (n > (colnr_T)max)
4332 {
4333 max = n;
4334 longest_lnum = lnum;
4335 }
4336 else if (n == (colnr_T)max
4337 && abs((int)(lnum - curwin->w_cursor.lnum))
4338 < abs((int)(longest_lnum - curwin->w_cursor.lnum)))
4339 longest_lnum = lnum;
4340 }
4341 }
4342 else
4343 /* Use cursor line only. */
4344 max = scroll_line_len(curwin->w_cursor.lnum);
4345#ifdef FEAT_VIRTUALEDIT
4346 if (virtual_active())
4347 {
4348 /* May move the cursor even further to the right. */
4349 if (curwin->w_virtcol >= (colnr_T)max)
4350 max = curwin->w_virtcol;
4351 }
4352#endif
4353
4354#ifndef SCROLL_PAST_END
4355 max += W_WIDTH(curwin) - 1;
4356#endif
4357 /* The line number isn't scrolled, thus there is less space when
4358 * 'number' is set (also for 'foldcolumn'). */
4359 size -= curwin_col_off();
4360#ifndef SCROLL_PAST_END
4361 max -= curwin_col_off();
4362#endif
4363 }
4364
4365#ifndef SCROLL_PAST_END
4366 if (value > max - size + 1)
4367 value = max - size + 1; /* limit the value to allowable range */
4368#endif
4369
4370#ifdef FEAT_RIGHTLEFT
4371 if (curwin->w_p_rl)
4372 {
4373 value = max + 1 - size - value;
4374 if (value < 0)
4375 {
4376 size += value;
4377 value = 0;
4378 }
4379 }
4380#endif
4381 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4382 && max == gui.bottom_sbar.max)
4383 return;
4384
4385 gui.bottom_sbar.value = value;
4386 gui.bottom_sbar.size = size;
4387 gui.bottom_sbar.max = max;
4388 gui.prev_wrap = curwin->w_p_wrap;
4389
4390 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4391}
4392
4393/*
4394 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4395 */
4396 int
4397gui_do_horiz_scroll()
4398{
4399 /* no wrapping, no scrolling */
4400 if (curwin->w_p_wrap)
4401 return FALSE;
4402
4403 if (curwin->w_leftcol == scrollbar_value)
4404 return FALSE;
4405
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004406 curwin->w_leftcol = (colnr_T)scrollbar_value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 /* When the line of the cursor is too short, move the cursor to the
4409 * longest visible line. Do a sanity check on "longest_lnum", just in
4410 * case. */
4411 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4412 && longest_lnum >= curwin->w_topline
4413 && longest_lnum < curwin->w_botline
4414 && !virtual_active())
4415 {
4416 if (scrollbar_value > scroll_line_len(curwin->w_cursor.lnum))
4417 {
4418 curwin->w_cursor.lnum = longest_lnum;
4419 curwin->w_cursor.col = 0;
4420 }
4421 }
4422
4423 return leftcol_changed();
4424}
4425
4426/*
4427 * Check that none of the colors are the same as the background color
4428 */
4429 void
4430gui_check_colors()
4431{
4432 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4433 {
4434 gui_set_bg_color((char_u *)"White");
4435 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4436 gui_set_fg_color((char_u *)"Black");
4437 }
4438}
4439
Bram Moolenaar3918c952005-03-15 22:34:55 +00004440 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441gui_set_fg_color(name)
4442 char_u *name;
4443{
4444 gui.norm_pixel = gui_get_color(name);
4445 hl_set_fg_color_name(vim_strsave(name));
4446}
4447
Bram Moolenaar3918c952005-03-15 22:34:55 +00004448 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449gui_set_bg_color(name)
4450 char_u *name;
4451{
4452 gui.back_pixel = gui_get_color(name);
4453 hl_set_bg_color_name(vim_strsave(name));
4454}
4455
4456/*
4457 * Allocate a color by name.
4458 * Returns INVALCOLOR and gives an error message when failed.
4459 */
4460 guicolor_T
4461gui_get_color(name)
4462 char_u *name;
4463{
4464 guicolor_T t;
4465
4466 if (*name == NUL)
4467 return INVALCOLOR;
4468 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004471#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 && gui.in_use
4473#endif
4474 )
4475 EMSG2(_("E254: Cannot allocate color %s"), name);
4476 return t;
4477}
4478
4479/*
4480 * Return the grey value of a color (range 0-255).
4481 */
4482 int
4483gui_get_lightness(pixel)
4484 guicolor_T pixel;
4485{
4486 long_u rgb = gui_mch_get_rgb(pixel);
4487
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004488 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004489 + (((rgb >> 8) & 0xff) * 587)
4490 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491}
4492
4493#if defined(FEAT_GUI_X11) || defined(PROTO)
4494 void
4495gui_new_scrollbar_colors()
4496{
4497 win_T *wp;
4498
4499 /* Nothing to do if GUI hasn't started yet. */
4500 if (!gui.in_use)
4501 return;
4502
4503 FOR_ALL_WINDOWS(wp)
4504 {
4505 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4506 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4507 }
4508 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4509}
4510#endif
4511
4512/*
4513 * Call this when focus has changed.
4514 */
4515 void
4516gui_focus_change(in_focus)
4517 int in_focus;
4518{
4519/*
4520 * Skip this code to avoid drawing the cursor when debugging and switching
4521 * between the debugger window and gvim.
4522 */
4523#if 1
4524 gui.in_focus = in_focus;
4525 out_flush(); /* make sure output has been written */
4526 gui_update_cursor(TRUE, FALSE);
4527
4528# ifdef FEAT_XIM
4529 xim_set_focus(in_focus);
4530# endif
4531
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004532 /* Put events in the input queue only when allowed.
4533 * ui_focus_change() isn't called directly, because it invokes
4534 * autocommands and that must not happen asynchronously. */
4535 if (!hold_gui_events)
4536 {
4537 char_u bytes[3];
4538
4539 bytes[0] = CSI;
4540 bytes[1] = KS_EXTRA;
4541 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4542 add_to_input_buf(bytes, 3);
4543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#endif
4545}
4546
4547/*
4548 * Called when the mouse moved (but not when dragging).
4549 */
4550 void
4551gui_mouse_moved(x, y)
4552 int x;
4553 int y;
4554{
4555 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004556 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaard3667a22006-03-16 21:35:52 +00004558 /* Ignore this while still starting up. */
4559 if (!gui.in_use || gui.starting)
4560 return;
4561
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562#ifdef FEAT_MOUSESHAPE
4563 /* Get window pointer, and update mouse shape as well. */
4564 wp = xy2win(x, y);
4565#endif
4566
4567 /* Only handle this when 'mousefocus' set and ... */
4568 if (p_mousef
4569 && !hold_gui_events /* not holding events */
4570 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4571 && State != HITRETURN /* but not hit-return prompt */
4572 && msg_scrolled == 0 /* no scrolled message */
4573 && !need_mouse_correct /* not moving the pointer */
4574 && gui.in_focus) /* gvim in focus */
4575 {
4576 /* Don't move the mouse when it's left or right of the Vim window */
4577 if (x < 0 || x > Columns * gui.char_width)
4578 return;
4579#ifndef FEAT_MOUSESHAPE
4580 wp = xy2win(x, y);
4581#endif
4582 if (wp == curwin || wp == NULL)
4583 return; /* still in the same old window, or none at all */
4584
Bram Moolenaar9c102382006-05-03 21:26:49 +00004585#ifdef FEAT_WINDOWS
4586 /* Ignore position in the tab pages line. */
4587 if (Y_2_ROW(y) < tabline_height())
4588 return;
4589#endif
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /*
4592 * format a mouse click on status line input
4593 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004594 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4595 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 */
4597 if (finish_op)
4598 {
4599 /* abort the current operator first */
4600 st[0] = ESC;
4601 add_to_input_buf(st, 1);
4602 }
4603 st[0] = CSI;
4604 st[1] = KS_MOUSE;
4605 st[2] = KE_FILLER;
4606 st[3] = (char_u)MOUSE_LEFT;
4607 fill_mouse_coord(st + 4,
4608#ifdef FEAT_VERTSPLIT
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004609 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#else
4611 -1,
4612#endif
4613 wp->w_height + W_WINROW(wp));
4614
4615 add_to_input_buf(st, 8);
4616 st[3] = (char_u)MOUSE_RELEASE;
4617 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#ifdef FEAT_GUI_GTK
4619 /* Need to wake up the main loop */
4620 if (gtk_main_level() > 0)
4621 gtk_main_quit();
4622#endif
4623 }
4624}
4625
4626/*
4627 * Called when mouse should be moved to window with focus.
4628 */
4629 void
4630gui_mouse_correct()
4631{
4632 int x, y;
4633 win_T *wp = NULL;
4634
4635 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004636
4637 if (!(gui.in_use && p_mousef))
4638 return;
4639
4640 gui_mch_getmouse(&x, &y);
4641 /* Don't move the mouse when it's left or right of the Vim window */
4642 if (x < 0 || x > Columns * gui.char_width)
4643 return;
Bram Moolenaar8798be02006-05-13 10:11:39 +00004644 if (y >= 0
Bram Moolenaar9c102382006-05-03 21:26:49 +00004645# ifdef FEAT_WINDOWS
Bram Moolenaar8798be02006-05-13 10:11:39 +00004646 && Y_2_ROW(y) >= tabline_height()
Bram Moolenaar9c102382006-05-03 21:26:49 +00004647# endif
Bram Moolenaar8798be02006-05-13 10:11:39 +00004648 )
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004649 wp = xy2win(x, y);
4650 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004652 validate_cline_row();
4653 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4654 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657}
4658
4659/*
4660 * Find window where the mouse pointer "y" coordinate is in.
4661 */
4662/*ARGSUSED*/
4663 static win_T *
4664xy2win(x, y)
4665 int x;
4666 int y;
4667{
4668#ifdef FEAT_WINDOWS
4669 int row;
4670 int col;
4671 win_T *wp;
4672
4673 row = Y_2_ROW(y);
4674 col = X_2_COL(x);
4675 if (row < 0 || col < 0) /* before first window */
4676 return NULL;
4677 wp = mouse_find_win(&row, &col);
4678# ifdef FEAT_MOUSESHAPE
4679 if (State == HITRETURN || State == ASKMORE)
4680 {
4681 if (Y_2_ROW(y) >= msg_row)
4682 update_mouseshape(SHAPE_IDX_MOREL);
4683 else
4684 update_mouseshape(SHAPE_IDX_MORE);
4685 }
4686 else if (row > wp->w_height) /* below status line */
4687 update_mouseshape(SHAPE_IDX_CLINE);
4688# ifdef FEAT_VERTSPLIT
4689 else if (!(State & CMDLINE) && W_VSEP_WIDTH(wp) > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004690 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 update_mouseshape(SHAPE_IDX_VSEP);
4692# endif
4693 else if (!(State & CMDLINE) && W_STATUS_HEIGHT(wp) > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004694 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 update_mouseshape(SHAPE_IDX_STATUS);
4696 else
4697 update_mouseshape(-2);
4698# endif
4699 return wp;
4700#else
4701 return firstwin;
4702#endif
4703}
4704
4705/*
4706 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4707 * File names may be given to redefine the args list.
4708 */
4709 void
4710ex_gui(eap)
4711 exarg_T *eap;
4712{
4713 char_u *arg = eap->arg;
4714
4715 /*
4716 * Check for "-f" argument: foreground, don't fork.
4717 * Also don't fork when started with "gvim -f".
4718 * Do fork when using "gui -b".
4719 */
4720 if (arg[0] == '-'
4721 && (arg[1] == 'f' || arg[1] == 'b')
4722 && (arg[2] == NUL || vim_iswhite(arg[2])))
4723 {
4724 gui.dofork = (arg[1] == 'b');
4725 eap->arg = skipwhite(eap->arg + 2);
4726 }
4727 if (!gui.in_use)
4728 {
4729 /* Clear the command. Needed for when forking+exiting, to avoid part
4730 * of the argument ending up after the shell prompt. */
4731 msg_clr_eos_force();
4732 gui_start();
4733 }
4734 if (!ends_excmd(*eap->arg))
4735 ex_next(eap);
4736}
4737
4738#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) \
Bram Moolenaar9372a112005-12-06 19:59:18 +00004739 || defined(FEAT_GUI_PHOTON)) && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740/*
4741 * This is shared between Athena, Motif and GTK.
4742 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743static void gfp_setname __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745/*
4746 * Callback function for do_in_runtimepath().
4747 */
4748 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004749gfp_setname(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004751 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004753 char_u *gfp_buffer = cookie;
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (STRLEN(fname) >= MAXPATHL)
4756 *gfp_buffer = NUL;
4757 else
4758 STRCPY(gfp_buffer, fname);
4759}
4760
4761/*
4762 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4763 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4764 */
4765 int
4766gui_find_bitmap(name, buffer, ext)
4767 char_u *name;
4768 char_u *buffer;
4769 char *ext;
4770{
4771 if (STRLEN(name) > MAXPATHL - 14)
4772 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004773 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004774 if (do_in_runtimepath(buffer, FALSE, gfp_setname, buffer) == FAIL
4775 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 return FAIL;
4777 return OK;
4778}
4779
4780# if !defined(HAVE_GTK2) || defined(PROTO)
4781/*
4782 * Given the name of the "icon=" argument, try finding the bitmap file for the
4783 * icon. If it is an absolute path name, use it as it is. Otherwise append
4784 * "ext" and search for it in 'runtimepath'.
4785 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
4786 * contains "name".
4787 */
4788 void
4789gui_find_iconfile(name, buffer, ext)
4790 char_u *name;
4791 char_u *buffer;
4792 char *ext;
4793{
4794 char_u buf[MAXPATHL + 1];
4795
4796 expand_env(name, buffer, MAXPATHL);
4797 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
4798 STRCPY(buffer, buf);
4799}
4800# endif
4801#endif
4802
Bram Moolenaar9372a112005-12-06 19:59:18 +00004803#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 void
4805display_errors()
4806{
4807 char_u *p;
4808
4809 if (isatty(2))
4810 fflush(stderr);
4811 else if (error_ga.ga_data != NULL)
4812 {
4813 /* avoid putting up a message box with blanks only */
4814 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
4815 if (!isspace(*p))
4816 {
4817 /* Truncate a very long message, it will go off-screen. */
4818 if (STRLEN(p) > 2000)
4819 STRCPY(p + 2000 - 14, "...(truncated)");
4820 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
4821 p, (char_u *)_("&Ok"), 1, NULL);
4822 break;
4823 }
4824 ga_clear(&error_ga);
4825 }
4826}
4827#endif
4828
4829#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
4830/*
4831 * Return TRUE if still starting up and there is no place to enter text.
4832 * For GTK and X11 we check if stderr is not a tty, which means we were
4833 * (probably) started from the desktop. Also check stdin, "vim >& file" does
4834 * allow typing on stdin.
4835 */
4836 int
4837no_console_input()
4838{
4839 return ((!gui.in_use || gui.starting)
4840# ifndef NO_CONSOLE
4841 && !isatty(0) && !isatty(2)
4842# endif
4843 );
4844}
4845#endif
4846
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004847#if defined(FIND_REPLACE_DIALOG) || defined(FEAT_SUN_WORKSHOP) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00004848 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004849 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850/*
4851 * Update the current window and the screen.
4852 */
4853 void
4854gui_update_screen()
4855{
4856 update_topline();
4857 validate_cursor();
Bram Moolenaarec80df72008-05-07 19:46:51 +00004858#ifdef FEAT_AUTOCMD
4859 /* Trigger CursorMoved if the cursor moved. */
4860 if (!finish_op && has_cursormoved()
4861 && !equalpos(last_cursormoved, curwin->w_cursor))
4862 {
4863 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
4864 last_cursormoved = curwin->w_cursor;
4865 }
4866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 update_screen(0); /* may need to update the screen */
4868 setcursor();
4869 out_flush(); /* make sure output has been written */
4870 gui_update_cursor(TRUE, FALSE);
4871 gui_mch_flush();
4872}
4873#endif
4874
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004875#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876static void concat_esc __ARGS((garray_T *gap, char_u *text, int what));
4877
4878/*
4879 * Get the text to use in a find/replace dialog. Uses the last search pattern
4880 * if the argument is empty.
4881 * Returns an allocated string.
4882 */
4883 char_u *
4884get_find_dialog_text(arg, wwordp, mcasep)
4885 char_u *arg;
4886 int *wwordp; /* return: TRUE if \< \> found */
4887 int *mcasep; /* return: TRUE if \C found */
4888{
4889 char_u *text;
4890
4891 if (*arg == NUL)
4892 text = last_search_pat();
4893 else
4894 text = arg;
4895 if (text != NULL)
4896 {
4897 text = vim_strsave(text);
4898 if (text != NULL)
4899 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004900 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int i;
4902
4903 /* Remove "\V" */
4904 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
4905 {
4906 mch_memmove(text, text + 2, (size_t)(len - 1));
4907 len -= 2;
4908 }
4909
4910 /* Recognize "\c" and "\C" and remove. */
4911 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
4912 {
4913 *mcasep = (text[1] == 'C');
4914 mch_memmove(text, text + 2, (size_t)(len - 1));
4915 len -= 2;
4916 }
4917
4918 /* Recognize "\<text\>" and remove. */
4919 if (len >= 4
4920 && STRNCMP(text, "\\<", 2) == 0
4921 && STRNCMP(text + len - 2, "\\>", 2) == 0)
4922 {
4923 *wwordp = TRUE;
4924 mch_memmove(text, text + 2, (size_t)(len - 4));
4925 text[len - 4] = NUL;
4926 }
4927
4928 /* Recognize "\/" or "\?" and remove. */
4929 for (i = 0; i + 1 < len; ++i)
4930 if (text[i] == '\\' && (text[i + 1] == '/'
4931 || text[i + 1] == '?'))
4932 {
4933 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
4934 --len;
4935 }
4936 }
4937 }
4938 return text;
4939}
4940
4941/*
4942 * Concatenate "text" to grow array "gap", escaping "what" with a backslash.
4943 */
4944 static void
4945concat_esc(gap, text, what)
4946 garray_T *gap;
4947 char_u *text;
4948 int what;
4949{
4950 while (*text != NUL)
4951 {
4952#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004953 int l = (*mb_ptr2len)(text);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (l > 1)
4956 {
4957 while (--l >= 0)
4958 ga_append(gap, *text++);
4959 continue;
4960 }
4961#endif
4962 if (*text == what)
4963 ga_append(gap, '\\');
4964 ga_append(gap, *text);
4965 ++text;
4966 }
4967}
4968
4969/*
4970 * Handle the press of a button in the find-replace dialog.
4971 * Return TRUE when something was added to the input buffer.
4972 */
4973 int
4974gui_do_findrepl(flags, find_text, repl_text, down)
4975 int flags; /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
4976 char_u *find_text;
4977 char_u *repl_text;
4978 int down; /* Search downwards. */
4979{
4980 garray_T ga;
4981 int i;
4982 int type = (flags & FRD_TYPE_MASK);
4983 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004984 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004985 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00004988 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 ga_concat(&ga, (char_u *)"%s/");
4990
4991 ga_concat(&ga, (char_u *)"\\V");
4992 if (flags & FRD_MATCH_CASE)
4993 ga_concat(&ga, (char_u *)"\\C");
4994 else
4995 ga_concat(&ga, (char_u *)"\\c");
4996 if (flags & FRD_WHOLE_WORD)
4997 ga_concat(&ga, (char_u *)"\\<");
4998 if (type == FRD_REPLACEALL || down)
4999 concat_esc(&ga, find_text, '/'); /* escape slashes */
5000 else
5001 concat_esc(&ga, find_text, '?'); /* escape '?' */
5002 if (flags & FRD_WHOLE_WORD)
5003 ga_concat(&ga, (char_u *)"\\>");
5004
5005 if (type == FRD_REPLACEALL)
5006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005008 /* escape / and \ */
5009 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5010 if (p != NULL)
5011 ga_concat(&ga, p);
5012 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005014 }
5015 ga_append(&ga, NUL);
5016
5017 if (type == FRD_REPLACE)
5018 {
5019 /* Do the replacement when the text at the cursor matches. Thus no
5020 * replacement is done if the cursor was moved! */
5021 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5022 regmatch.rm_ic = 0;
5023 if (regmatch.regprog != NULL)
5024 {
5025 p = ml_get_cursor();
5026 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5027 && regmatch.startp[0] == p)
5028 {
5029 /* Clear the command line to remove any old "No match"
5030 * error. */
5031 msg_end_prompt();
5032
5033 if (u_save_cursor() == OK)
5034 {
5035 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005036 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005037
5038 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005039 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005040 ins_str(repl_text);
5041 }
5042 }
5043 else
5044 MSG(_("No match at cursor, finding next"));
5045 vim_free(regmatch.regprog);
5046 }
5047 }
5048
5049 if (type == FRD_REPLACEALL)
5050 {
5051 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005052 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 do_cmdline_cmd(ga.ga_data);
5054 }
5055 else
5056 {
5057 /* Search for the next match. */
5058 i = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 do_search(NULL, down ? '/' : '?', ga.ga_data, 1L,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00005060 SEARCH_MSG + SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 msg_scroll = i; /* don't let an error message set msg_scroll */
5062 }
5063
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005064 /* Don't want to pass did_emsg to other code, it may cause disabling
5065 * syntax HL if we were busy redrawing. */
5066 did_emsg = save_did_emsg;
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (State & (NORMAL | INSERT))
5069 {
5070 gui_update_screen(); /* update the screen */
5071 msg_didout = 0; /* overwrite any message */
5072 need_wait_return = FALSE; /* don't wait for return */
5073 }
5074
5075 vim_free(ga.ga_data);
5076 return (ga.ga_len > 0);
5077}
5078
5079#endif
5080
5081#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
5082 || defined(FEAT_GUI_MSWIN) \
5083 || defined(FEAT_GUI_MAC) \
5084 || defined(PROTO)
5085
5086#ifdef FEAT_WINDOWS
5087static void gui_wingoto_xy __ARGS((int x, int y));
5088
5089/*
5090 * Jump to the window at specified point (x, y).
5091 */
5092 static void
5093gui_wingoto_xy(x, y)
5094 int x;
5095 int y;
5096{
5097 int row = Y_2_ROW(y);
5098 int col = X_2_COL(x);
5099 win_T *wp;
5100
5101 if (row >= 0 && col >= 0)
5102 {
5103 wp = mouse_find_win(&row, &col);
5104 if (wp != NULL && wp != curwin)
5105 win_goto(wp);
5106 }
5107}
5108#endif
5109
5110/*
5111 * Process file drop. Mouse cursor position, key modifiers, name of files
5112 * and count of files are given. Argument "fnames[count]" has full pathnames
5113 * of dropped files, they will be freed in this function, and caller can't use
5114 * fnames after call this function.
5115 */
5116/*ARGSUSED*/
5117 void
5118gui_handle_drop(x, y, modifiers, fnames, count)
5119 int x;
5120 int y;
5121 int_u modifiers;
5122 char_u **fnames;
5123 int count;
5124{
5125 int i;
5126 char_u *p;
5127
5128 /*
5129 * When the cursor is at the command line, add the file names to the
5130 * command line, don't edit the files.
5131 */
5132 if (State & CMDLINE)
5133 {
5134 shorten_filenames(fnames, count);
5135 for (i = 0; i < count; ++i)
5136 {
5137 if (fnames[i] != NULL)
5138 {
5139 if (i > 0)
5140 add_to_input_buf((char_u*)" ", 1);
5141
5142 /* We don't know what command is used thus we can't be sure
5143 * about which characters need to be escaped. Only escape the
5144 * most common ones. */
5145# ifdef BACKSLASH_IN_FILENAME
5146 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5147# else
5148 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5149# endif
5150 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005151 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 vim_free(p);
5153 vim_free(fnames[i]);
5154 }
5155 }
5156 vim_free(fnames);
5157 }
5158 else
5159 {
5160 /* Go to the window under mouse cursor, then shorten given "fnames" by
5161 * current window, because a window can have local current dir. */
5162# ifdef FEAT_WINDOWS
5163 gui_wingoto_xy(x, y);
5164# endif
5165 shorten_filenames(fnames, count);
5166
5167 /* If Shift held down, remember the first item. */
5168 if ((modifiers & MOUSE_SHIFT) != 0)
5169 p = vim_strsave(fnames[0]);
5170 else
5171 p = NULL;
5172
5173 /* Handle the drop, :edit or :split to get to the file. This also
5174 * frees fnames[]. Skip this if there is only one item it's a
5175 * directory and Shift is held down. */
5176 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5177 && mch_isdir(fnames[0]))
5178 {
5179 vim_free(fnames[0]);
5180 vim_free(fnames);
5181 }
5182 else
5183 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0);
5184
5185 /* If Shift held down, change to first file's directory. If the first
5186 * item is a directory, change to that directory (and let the explorer
5187 * plugin show the contents). */
5188 if (p != NULL)
5189 {
5190 if (mch_isdir(p))
5191 {
5192 if (mch_chdir((char *)p) == 0)
5193 shorten_fnames(TRUE);
5194 }
5195 else if (vim_chdirfile(p) == OK)
5196 shorten_fnames(TRUE);
5197 vim_free(p);
5198 }
5199
5200 /* Update the screen display */
5201 update_screen(NOT_VALID);
5202# ifdef FEAT_MENU
5203 gui_update_menus(0);
5204# endif
5205 setcursor();
5206 out_flush();
5207 gui_update_cursor(FALSE, FALSE);
5208 gui_mch_flush();
5209 }
5210}
5211#endif