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