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