Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * GUI 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 | * gui_w48.c: This file is included in gui_w16.c and gui_w32.c. |
| 12 | * |
| 13 | * GUI support for Microsoft Windows (Win16 + Win32 = Win48 :-) |
| 14 | * |
| 15 | * The combined efforts of: |
| 16 | * George V. Reilly <george@reilly.org> |
| 17 | * Robert Webb |
| 18 | * Vince Negri |
| 19 | * ...and contributions from many others |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "vim.h" |
| 24 | #include "version.h" /* used by dialog box routine for default title */ |
| 25 | #ifdef DEBUG |
| 26 | # include <tchar.h> |
| 27 | #endif |
| 28 | #ifndef __MINGW32__ |
| 29 | # include <shellapi.h> |
| 30 | #endif |
| 31 | #if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL) |
| 32 | # include <commctrl.h> |
| 33 | #endif |
| 34 | #ifdef WIN16 |
| 35 | # include <commdlg.h> |
| 36 | # include <shellapi.h> |
| 37 | # ifdef WIN16_3DLOOK |
| 38 | # include <ctl3d.h> |
| 39 | # endif |
| 40 | #endif |
| 41 | #include <windowsx.h> |
| 42 | |
| 43 | #ifdef GLOBAL_IME |
| 44 | # include "glbl_ime.h" |
| 45 | #endif |
| 46 | |
| 47 | #ifdef FEAT_MENU |
| 48 | # define MENUHINTS /* show menu hints in command line */ |
| 49 | #endif |
| 50 | |
| 51 | /* Some parameters for dialog boxes. All in pixels. */ |
| 52 | #define DLG_PADDING_X 10 |
| 53 | #define DLG_PADDING_Y 10 |
| 54 | #define DLG_OLD_STYLE_PADDING_X 5 |
| 55 | #define DLG_OLD_STYLE_PADDING_Y 5 |
| 56 | #define DLG_VERT_PADDING_X 4 /* For vertical buttons */ |
| 57 | #define DLG_VERT_PADDING_Y 4 |
| 58 | #define DLG_ICON_WIDTH 34 |
| 59 | #define DLG_ICON_HEIGHT 34 |
| 60 | #define DLG_MIN_WIDTH 150 |
| 61 | #define DLG_FONT_NAME "MS Sans Serif" |
| 62 | #define DLG_FONT_POINT_SIZE 8 |
| 63 | #define DLG_MIN_MAX_WIDTH 400 |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 64 | #define DLG_MIN_MAX_HEIGHT 400 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | |
| 66 | #define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */ |
| 67 | |
| 68 | #ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */ |
| 69 | # define WM_XBUTTONDOWN 0x020B |
| 70 | # define WM_XBUTTONUP 0x020C |
| 71 | # define WM_XBUTTONDBLCLK 0x020D |
| 72 | # define MK_XBUTTON1 0x0020 |
| 73 | # define MK_XBUTTON2 0x0040 |
| 74 | #endif |
| 75 | |
| 76 | #ifdef PROTO |
| 77 | /* |
| 78 | * Define a few things for generating prototypes. This is just to avoid |
| 79 | * syntax errors, the defines do not need to be correct. |
| 80 | */ |
| 81 | # define APIENTRY |
| 82 | # define CALLBACK |
| 83 | # define CONST |
| 84 | # define FAR |
| 85 | # define NEAR |
| 86 | # define _cdecl |
| 87 | typedef int BOOL; |
| 88 | typedef int BYTE; |
| 89 | typedef int DWORD; |
| 90 | typedef int WCHAR; |
| 91 | typedef int ENUMLOGFONT; |
| 92 | typedef int FINDREPLACE; |
| 93 | typedef int HANDLE; |
| 94 | typedef int HBITMAP; |
| 95 | typedef int HBRUSH; |
| 96 | typedef int HDROP; |
| 97 | typedef int INT; |
| 98 | typedef int LOGFONT[]; |
| 99 | typedef int LPARAM; |
| 100 | typedef int LPCREATESTRUCT; |
| 101 | typedef int LPCSTR; |
| 102 | typedef int LPCTSTR; |
| 103 | typedef int LPRECT; |
| 104 | typedef int LPSTR; |
| 105 | typedef int LPWINDOWPOS; |
| 106 | typedef int LPWORD; |
| 107 | typedef int LRESULT; |
| 108 | # undef MSG |
| 109 | typedef int MSG; |
| 110 | typedef int NEWTEXTMETRIC; |
| 111 | typedef int OSVERSIONINFO; |
| 112 | typedef int PWORD; |
| 113 | typedef int RECT; |
| 114 | typedef int UINT; |
| 115 | typedef int WORD; |
| 116 | typedef int WPARAM; |
| 117 | typedef int POINT; |
| 118 | typedef void *HINSTANCE; |
| 119 | typedef void *HMENU; |
| 120 | typedef void *HWND; |
| 121 | typedef void *HDC; |
| 122 | typedef void VOID; |
| 123 | typedef int LPNMHDR; |
| 124 | typedef int LONG; |
| 125 | #endif |
| 126 | |
| 127 | #ifndef GET_X_LPARAM |
| 128 | # define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp)) |
| 129 | #endif |
| 130 | |
| 131 | static void _OnPaint( HWND hwnd); |
| 132 | static void clear_rect(RECT *rcp); |
| 133 | static int gui_mswin_get_menu_height(int fix_window); |
| 134 | |
| 135 | static WORD s_dlgfntheight; /* height of the dialog font */ |
| 136 | static WORD s_dlgfntwidth; /* width of the dialog font */ |
| 137 | |
| 138 | #ifdef FEAT_MENU |
| 139 | static HMENU s_menuBar = NULL; |
| 140 | #endif |
| 141 | #ifdef FEAT_TEAROFF |
| 142 | static void rebuild_tearoff(vimmenu_T *menu); |
| 143 | static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */ |
| 144 | #endif |
| 145 | |
| 146 | /* Flag that is set while processing a message that must not be interupted by |
| 147 | * processing another message. */ |
| 148 | static int s_busy_processing = FALSE; |
| 149 | |
| 150 | static int destroying = FALSE; /* call DestroyWindow() ourselves */ |
| 151 | |
| 152 | #ifdef MSWIN_FIND_REPLACE |
| 153 | static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */ |
| 154 | static FINDREPLACE s_findrep_struct; |
| 155 | static HWND s_findrep_hwnd = NULL; |
| 156 | static int s_findrep_is_find; /* TRUE for find dialog, FALSE |
| 157 | for find/replace dialog */ |
| 158 | #endif |
| 159 | |
| 160 | static HINSTANCE s_hinst = NULL; |
| 161 | #if !defined(FEAT_SNIFF) && !defined(FEAT_GUI) |
| 162 | static |
| 163 | #endif |
| 164 | HWND s_hwnd = NULL; |
| 165 | static HDC s_hdc = NULL; |
| 166 | static HBRUSH s_brush = NULL; |
| 167 | |
| 168 | #ifdef FEAT_TOOLBAR |
| 169 | static HWND s_toolbarhwnd = NULL; |
| 170 | #endif |
| 171 | |
| 172 | static WPARAM s_wParam = 0; |
| 173 | static LPARAM s_lParam = 0; |
| 174 | |
| 175 | static HWND s_textArea = NULL; |
| 176 | static UINT s_uMsg = 0; |
| 177 | |
| 178 | static char_u *s_textfield; /* Used by dialogs to pass back strings */ |
| 179 | |
| 180 | static int s_need_activate = FALSE; |
| 181 | |
| 182 | /* This variable is set when waiting for an event, which is the only moment |
| 183 | * scrollbar dragging can be done directly. It's not allowed while commands |
| 184 | * are executed, because it may move the cursor and that may cause unexpected |
| 185 | * problems (e.g., while ":s" is working). |
| 186 | */ |
| 187 | static int allow_scrollbar = FALSE; |
| 188 | |
| 189 | #ifdef GLOBAL_IME |
| 190 | # define MyTranslateMessage(x) global_ime_TranslateMessage(x) |
| 191 | #else |
| 192 | # define MyTranslateMessage(x) TranslateMessage(x) |
| 193 | #endif |
| 194 | |
| 195 | #if (defined(WIN3264) && defined(FEAT_MBYTE)) || defined(GLOBAL_IME) |
| 196 | /* use of WindowProc depends on wide_WindowProc */ |
| 197 | # define MyWindowProc vim_WindowProc |
| 198 | #else |
| 199 | /* use ordinary WindowProc */ |
| 200 | # define MyWindowProc DefWindowProc |
| 201 | #endif |
| 202 | |
| 203 | extern int current_font_height; /* this is in os_mswin.c */ |
| 204 | |
| 205 | static struct |
| 206 | { |
| 207 | UINT key_sym; |
| 208 | char_u vim_code0; |
| 209 | char_u vim_code1; |
| 210 | } special_keys[] = |
| 211 | { |
| 212 | {VK_UP, 'k', 'u'}, |
| 213 | {VK_DOWN, 'k', 'd'}, |
| 214 | {VK_LEFT, 'k', 'l'}, |
| 215 | {VK_RIGHT, 'k', 'r'}, |
| 216 | |
| 217 | {VK_F1, 'k', '1'}, |
| 218 | {VK_F2, 'k', '2'}, |
| 219 | {VK_F3, 'k', '3'}, |
| 220 | {VK_F4, 'k', '4'}, |
| 221 | {VK_F5, 'k', '5'}, |
| 222 | {VK_F6, 'k', '6'}, |
| 223 | {VK_F7, 'k', '7'}, |
| 224 | {VK_F8, 'k', '8'}, |
| 225 | {VK_F9, 'k', '9'}, |
| 226 | {VK_F10, 'k', ';'}, |
| 227 | |
| 228 | {VK_F11, 'F', '1'}, |
| 229 | {VK_F12, 'F', '2'}, |
| 230 | {VK_F13, 'F', '3'}, |
| 231 | {VK_F14, 'F', '4'}, |
| 232 | {VK_F15, 'F', '5'}, |
| 233 | {VK_F16, 'F', '6'}, |
| 234 | {VK_F17, 'F', '7'}, |
| 235 | {VK_F18, 'F', '8'}, |
| 236 | {VK_F19, 'F', '9'}, |
| 237 | {VK_F20, 'F', 'A'}, |
| 238 | |
| 239 | {VK_F21, 'F', 'B'}, |
| 240 | #ifdef FEAT_NETBEANS_INTG |
| 241 | {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */ |
| 242 | #endif |
| 243 | {VK_F22, 'F', 'C'}, |
| 244 | {VK_F23, 'F', 'D'}, |
| 245 | {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */ |
| 246 | |
| 247 | {VK_HELP, '%', '1'}, |
| 248 | {VK_BACK, 'k', 'b'}, |
| 249 | {VK_INSERT, 'k', 'I'}, |
| 250 | {VK_DELETE, 'k', 'D'}, |
| 251 | {VK_HOME, 'k', 'h'}, |
| 252 | {VK_END, '@', '7'}, |
| 253 | {VK_PRIOR, 'k', 'P'}, |
| 254 | {VK_NEXT, 'k', 'N'}, |
| 255 | {VK_PRINT, '%', '9'}, |
| 256 | {VK_ADD, 'K', '6'}, |
| 257 | {VK_SUBTRACT, 'K', '7'}, |
| 258 | {VK_DIVIDE, 'K', '8'}, |
| 259 | {VK_MULTIPLY, 'K', '9'}, |
| 260 | {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */ |
| 261 | {VK_DECIMAL, 'K', 'B'}, |
| 262 | |
| 263 | {VK_NUMPAD0, 'K', 'C'}, |
| 264 | {VK_NUMPAD1, 'K', 'D'}, |
| 265 | {VK_NUMPAD2, 'K', 'E'}, |
| 266 | {VK_NUMPAD3, 'K', 'F'}, |
| 267 | {VK_NUMPAD4, 'K', 'G'}, |
| 268 | {VK_NUMPAD5, 'K', 'H'}, |
| 269 | {VK_NUMPAD6, 'K', 'I'}, |
| 270 | {VK_NUMPAD7, 'K', 'J'}, |
| 271 | {VK_NUMPAD8, 'K', 'K'}, |
| 272 | {VK_NUMPAD9, 'K', 'L'}, |
| 273 | |
| 274 | /* Keys that we want to be able to use any modifier with: */ |
| 275 | {VK_SPACE, ' ', NUL}, |
| 276 | {VK_TAB, TAB, NUL}, |
| 277 | {VK_ESCAPE, ESC, NUL}, |
| 278 | {NL, NL, NUL}, |
| 279 | {CAR, CAR, NUL}, |
| 280 | |
| 281 | /* End of list marker: */ |
| 282 | {0, 0, 0} |
| 283 | }; |
| 284 | |
| 285 | /* Local variables */ |
| 286 | static int s_button_pending = -1; |
| 287 | static int s_x_pending; |
| 288 | static int s_y_pending; |
| 289 | static UINT s_kFlags_pending; |
| 290 | static UINT s_wait_timer = 0; /* Timer for get char from user */ |
| 291 | static int s_timed_out = FALSE; |
| 292 | static int dead_key = 0; /* 0 - no dead key, 1 - dead key pressed */ |
| 293 | |
| 294 | #ifdef WIN3264 |
| 295 | static OSVERSIONINFO os_version; /* like it says. Init in gui_mch_init() */ |
| 296 | #endif |
| 297 | |
| 298 | #ifdef FEAT_BEVAL |
| 299 | /* balloon-eval WM_NOTIFY_HANDLER */ |
| 300 | static void Handle_WM_Notify __ARGS((HWND hwnd, LPNMHDR pnmh)); |
| 301 | static void TrackUserActivity __ARGS((UINT uMsg)); |
| 302 | #endif |
| 303 | |
| 304 | /* |
| 305 | * For control IME. |
| 306 | */ |
| 307 | #ifdef FEAT_MBYTE |
| 308 | # ifdef USE_IM_CONTROL |
| 309 | static LOGFONT norm_logfont; |
| 310 | # endif |
| 311 | #endif |
| 312 | |
| 313 | #ifdef FEAT_MBYTE_IME |
| 314 | static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData); |
| 315 | #endif |
| 316 | |
| 317 | #ifdef DEBUG |
| 318 | /* |
| 319 | * Print out the last Windows error message |
| 320 | */ |
| 321 | static void |
| 322 | print_windows_error(void) |
| 323 | { |
| 324 | LPVOID lpMsgBuf; |
| 325 | |
| 326 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 327 | NULL, GetLastError(), |
| 328 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 329 | (LPTSTR) &lpMsgBuf, 0, NULL); |
| 330 | TRACE1("Error: %s\n", lpMsgBuf); |
| 331 | LocalFree(lpMsgBuf); |
| 332 | } |
| 333 | #endif /* DEBUG */ |
| 334 | |
| 335 | /* |
| 336 | * Cursor blink functions. |
| 337 | * |
| 338 | * This is a simple state machine: |
| 339 | * BLINK_NONE not blinking at all |
| 340 | * BLINK_OFF blinking, cursor is not shown |
| 341 | * BLINK_ON blinking, cursor is shown |
| 342 | */ |
| 343 | |
| 344 | #define BLINK_NONE 0 |
| 345 | #define BLINK_OFF 1 |
| 346 | #define BLINK_ON 2 |
| 347 | |
| 348 | static int blink_state = BLINK_NONE; |
| 349 | static long_u blink_waittime = 700; |
| 350 | static long_u blink_ontime = 400; |
| 351 | static long_u blink_offtime = 250; |
| 352 | static UINT blink_timer = 0; |
| 353 | |
| 354 | void |
| 355 | gui_mch_set_blinking(long wait, long on, long off) |
| 356 | { |
| 357 | blink_waittime = wait; |
| 358 | blink_ontime = on; |
| 359 | blink_offtime = off; |
| 360 | } |
| 361 | |
| 362 | /* ARGSUSED */ |
| 363 | static VOID CALLBACK |
| 364 | _OnBlinkTimer( |
| 365 | HWND hwnd, |
| 366 | UINT uMsg, |
| 367 | UINT idEvent, |
| 368 | DWORD dwTime) |
| 369 | { |
| 370 | MSG msg; |
| 371 | |
| 372 | /* |
| 373 | TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer); |
| 374 | */ |
| 375 | |
| 376 | KillTimer(NULL, idEvent); |
| 377 | |
| 378 | /* Eat spurious WM_TIMER messages */ |
| 379 | while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE)) |
| 380 | ; |
| 381 | |
| 382 | if (blink_state == BLINK_ON) |
| 383 | { |
| 384 | gui_undraw_cursor(); |
| 385 | blink_state = BLINK_OFF; |
| 386 | blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime, |
| 387 | (TIMERPROC)_OnBlinkTimer); |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | gui_update_cursor(TRUE, FALSE); |
| 392 | blink_state = BLINK_ON; |
| 393 | blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime, |
| 394 | (TIMERPROC)_OnBlinkTimer); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | gui_mswin_rm_blink_timer(void) |
| 400 | { |
| 401 | MSG msg; |
| 402 | |
| 403 | if (blink_timer != 0) |
| 404 | { |
| 405 | KillTimer(NULL, blink_timer); |
| 406 | /* Eat spurious WM_TIMER messages */ |
| 407 | while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE)) |
| 408 | ; |
| 409 | blink_timer = 0; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Stop the cursor blinking. Show the cursor if it wasn't shown. |
| 415 | */ |
| 416 | void |
| 417 | gui_mch_stop_blink(void) |
| 418 | { |
| 419 | gui_mswin_rm_blink_timer(); |
| 420 | if (blink_state == BLINK_OFF) |
| 421 | gui_update_cursor(TRUE, FALSE); |
| 422 | blink_state = BLINK_NONE; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Start the cursor blinking. If it was already blinking, this restarts the |
| 427 | * waiting time and shows the cursor. |
| 428 | */ |
| 429 | void |
| 430 | gui_mch_start_blink(void) |
| 431 | { |
| 432 | gui_mswin_rm_blink_timer(); |
| 433 | |
| 434 | /* Only switch blinking on if none of the times is zero */ |
| 435 | if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus) |
| 436 | { |
| 437 | blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime, |
| 438 | (TIMERPROC)_OnBlinkTimer); |
| 439 | blink_state = BLINK_ON; |
| 440 | gui_update_cursor(TRUE, FALSE); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Call-back routines. |
| 446 | */ |
| 447 | |
| 448 | static VOID CALLBACK |
| 449 | _OnTimer( |
| 450 | HWND hwnd, |
| 451 | UINT uMsg, |
| 452 | UINT idEvent, |
| 453 | DWORD dwTime) |
| 454 | { |
| 455 | MSG msg; |
| 456 | |
| 457 | /* |
| 458 | TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer); |
| 459 | */ |
| 460 | KillTimer(NULL, idEvent); |
| 461 | s_timed_out = TRUE; |
| 462 | |
| 463 | /* Eat spurious WM_TIMER messages */ |
| 464 | while (PeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE)) |
| 465 | ; |
| 466 | if (idEvent == s_wait_timer) |
| 467 | s_wait_timer = 0; |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | _OnDeadChar( |
| 472 | HWND hwnd, |
| 473 | UINT ch, |
| 474 | int cRepeat) |
| 475 | { |
| 476 | dead_key = 1; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Convert Unicode character "ch" to bytes in "string[slen]". |
| 481 | * Return the length. |
| 482 | */ |
| 483 | static int |
| 484 | char_to_string(int ch, char_u *string, int slen) |
| 485 | { |
| 486 | int len; |
| 487 | int i; |
| 488 | #ifdef FEAT_MBYTE |
| 489 | WCHAR wstring[2]; |
| 490 | char_u *ws = NULL;; |
| 491 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 492 | if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | { |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 494 | /* On Windows 95/98 we apparently get the character in the active |
| 495 | * codepage, not in UCS-2. If conversion is needed convert it to |
| 496 | * UCS-2 first. */ |
| 497 | if ((int)GetACP() == enc_codepage) |
| 498 | len = 0; /* no conversion required */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | else |
| 500 | { |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 501 | string[0] = ch; |
| 502 | len = MultiByteToWideChar(GetACP(), 0, string, 1, wstring, 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 505 | else |
| 506 | { |
| 507 | wstring[0] = ch; |
| 508 | len = 1; |
| 509 | } |
| 510 | |
| 511 | if (len > 0) |
| 512 | { |
| 513 | /* "ch" is a UTF-16 character. Convert it to a string of bytes. When |
| 514 | * "enc_codepage" is non-zero use the standard Win32 function, |
| 515 | * otherwise use our own conversion function (e.g., for UTF-8). */ |
| 516 | if (enc_codepage > 0) |
| 517 | len = WideCharToMultiByte(enc_codepage, 0, wstring, len, |
| 518 | string, slen, 0, NULL); |
| 519 | else |
| 520 | { |
| 521 | len = 1; |
| 522 | ws = ucs2_to_enc(wstring, &len); |
| 523 | if (ws == NULL) |
| 524 | len = 0; |
| 525 | else |
| 526 | { |
| 527 | if (len > slen) /* just in case */ |
| 528 | len = slen; |
| 529 | mch_memmove(string, ws, len); |
| 530 | vim_free(ws); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 535 | if (len == 0) |
| 536 | #endif |
| 537 | { |
| 538 | string[0] = ch; |
| 539 | len = 1; |
| 540 | } |
| 541 | |
| 542 | for (i = 0; i < len; ++i) |
| 543 | if (string[i] == CSI && len <= slen - 2) |
| 544 | { |
| 545 | /* Insert CSI as K_CSI. */ |
| 546 | mch_memmove(string + i + 3, string + i + 1, len - i - 1); |
| 547 | string[++i] = KS_EXTRA; |
| 548 | string[++i] = (int)KE_CSI; |
| 549 | len += 2; |
| 550 | } |
| 551 | |
| 552 | return len; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Key hit, add it to the input buffer. |
| 557 | */ |
| 558 | static void |
| 559 | _OnChar( |
| 560 | HWND hwnd, |
| 561 | UINT ch, |
| 562 | int cRepeat) |
| 563 | { |
| 564 | char_u string[40]; |
| 565 | int len = 0; |
| 566 | |
| 567 | len = char_to_string(ch, string, 40); |
| 568 | if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts) |
| 569 | { |
| 570 | trash_input_buf(); |
| 571 | got_int = TRUE; |
| 572 | } |
| 573 | |
| 574 | add_to_input_buf(string, len); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Alt-Key hit, add it to the input buffer. |
| 579 | */ |
| 580 | static void |
| 581 | _OnSysChar( |
| 582 | HWND hwnd, |
| 583 | UINT cch, |
| 584 | int cRepeat) |
| 585 | { |
| 586 | char_u string[40]; /* Enough for multibyte character */ |
| 587 | int len; |
| 588 | int modifiers; |
| 589 | int ch = cch; /* special keys are negative */ |
| 590 | |
| 591 | /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */ |
| 592 | |
| 593 | /* OK, we have a character key (given by ch) which was entered with the |
| 594 | * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note |
| 595 | * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless |
| 596 | * CAPSLOCK is pressed) at this point. |
| 597 | */ |
| 598 | modifiers = MOD_MASK_ALT; |
| 599 | if (GetKeyState(VK_SHIFT) & 0x8000) |
| 600 | modifiers |= MOD_MASK_SHIFT; |
| 601 | if (GetKeyState(VK_CONTROL) & 0x8000) |
| 602 | modifiers |= MOD_MASK_CTRL; |
| 603 | |
| 604 | ch = simplify_key(ch, &modifiers); |
| 605 | /* remove the SHIFT modifier for keys where it's already included, e.g., |
| 606 | * '(' and '*' */ |
| 607 | if (ch < 0x100 && !isalpha(ch) && isprint(ch)) |
| 608 | modifiers &= ~MOD_MASK_SHIFT; |
| 609 | |
| 610 | /* Interpret the ALT key as making the key META, include SHIFT, etc. */ |
| 611 | ch = extract_modifiers(ch, &modifiers); |
| 612 | if (ch == CSI) |
| 613 | ch = K_CSI; |
| 614 | |
| 615 | len = 0; |
| 616 | if (modifiers) |
| 617 | { |
| 618 | string[len++] = CSI; |
| 619 | string[len++] = KS_MODIFIER; |
| 620 | string[len++] = modifiers; |
| 621 | } |
| 622 | |
| 623 | if (IS_SPECIAL((int)ch)) |
| 624 | { |
| 625 | string[len++] = CSI; |
| 626 | string[len++] = K_SECOND((int)ch); |
| 627 | string[len++] = K_THIRD((int)ch); |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | /* Although the documentation isn't clear about it, we assume "ch" is |
| 632 | * a Unicode character. */ |
| 633 | len += char_to_string(ch, string + len, 40 - len); |
| 634 | } |
| 635 | |
| 636 | add_to_input_buf(string, len); |
| 637 | } |
| 638 | |
| 639 | static void |
| 640 | _OnMouseEvent( |
| 641 | int button, |
| 642 | int x, |
| 643 | int y, |
| 644 | int repeated_click, |
| 645 | UINT keyFlags) |
| 646 | { |
| 647 | int vim_modifiers = 0x0; |
| 648 | |
| 649 | if (keyFlags & MK_SHIFT) |
| 650 | vim_modifiers |= MOUSE_SHIFT; |
| 651 | if (keyFlags & MK_CONTROL) |
| 652 | vim_modifiers |= MOUSE_CTRL; |
| 653 | if (GetKeyState(VK_MENU) & 0x8000) |
| 654 | vim_modifiers |= MOUSE_ALT; |
| 655 | |
| 656 | gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers); |
| 657 | } |
| 658 | |
| 659 | static void |
| 660 | _OnMouseButtonDown( |
| 661 | HWND hwnd, |
| 662 | BOOL fDoubleClick, |
| 663 | int x, |
| 664 | int y, |
| 665 | UINT keyFlags) |
| 666 | { |
| 667 | static LONG s_prevTime = 0; |
| 668 | |
| 669 | LONG currentTime = GetMessageTime(); |
| 670 | int button = -1; |
| 671 | int repeated_click; |
| 672 | |
| 673 | /* Give main window the focus: this is so the cursor isn't hollow. */ |
| 674 | (void)SetFocus(s_hwnd); |
| 675 | |
| 676 | if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK) |
| 677 | button = MOUSE_LEFT; |
| 678 | else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK) |
| 679 | button = MOUSE_MIDDLE; |
| 680 | else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK) |
| 681 | button = MOUSE_RIGHT; |
| 682 | #ifndef WIN16 /*<VN>*/ |
| 683 | else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK) |
| 684 | { |
| 685 | #ifndef GET_XBUTTON_WPARAM |
| 686 | # define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam)) |
| 687 | #endif |
| 688 | button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2); |
| 689 | } |
| 690 | else if (s_uMsg == WM_CAPTURECHANGED) |
| 691 | { |
| 692 | /* on W95/NT4, somehow you get in here with an odd Msg |
| 693 | * if you press one button while holding down the other..*/ |
| 694 | if (s_button_pending == MOUSE_LEFT) |
| 695 | button = MOUSE_RIGHT; |
| 696 | else |
| 697 | button = MOUSE_LEFT; |
| 698 | } |
| 699 | #endif |
| 700 | if (button >= 0) |
| 701 | { |
| 702 | repeated_click = ((int)(currentTime - s_prevTime) < p_mouset); |
| 703 | |
| 704 | /* |
| 705 | * Holding down the left and right buttons simulates pushing the middle |
| 706 | * button. |
| 707 | */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 708 | if (repeated_click |
| 709 | && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT) |
| 710 | || (button == MOUSE_RIGHT |
| 711 | && s_button_pending == MOUSE_LEFT))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 712 | { |
| 713 | /* |
| 714 | * Hmm, gui.c will ignore more than one button down at a time, so |
| 715 | * pretend we let go of it first. |
| 716 | */ |
| 717 | gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0); |
| 718 | button = MOUSE_MIDDLE; |
| 719 | repeated_click = FALSE; |
| 720 | s_button_pending = -1; |
| 721 | _OnMouseEvent(button, x, y, repeated_click, keyFlags); |
| 722 | } |
| 723 | else if ((repeated_click) |
| 724 | || (mouse_model_popup() && (button == MOUSE_RIGHT))) |
| 725 | { |
| 726 | if (s_button_pending > -1) |
| 727 | { |
| 728 | _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags); |
| 729 | s_button_pending = -1; |
| 730 | } |
| 731 | /* TRACE("Button down at x %d, y %d\n", x, y); */ |
| 732 | _OnMouseEvent(button, x, y, repeated_click, keyFlags); |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | /* |
| 737 | * If this is the first press (i.e. not a multiple click) don't |
| 738 | * action immediately, but store and wait for: |
| 739 | * i) button-up |
| 740 | * ii) mouse move |
| 741 | * iii) another button press |
| 742 | * before using it. |
| 743 | * This enables us to make left+right simulate middle button, |
| 744 | * without left or right being actioned first. The side-effect is |
| 745 | * that if you click and hold the mouse without dragging, the |
| 746 | * cursor doesn't move until you release the button. In practice |
| 747 | * this is hardly a problem. |
| 748 | */ |
| 749 | s_button_pending = button; |
| 750 | s_x_pending = x; |
| 751 | s_y_pending = y; |
| 752 | s_kFlags_pending = keyFlags; |
| 753 | } |
| 754 | |
| 755 | s_prevTime = currentTime; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static void |
| 760 | _OnMouseMoveOrRelease( |
| 761 | HWND hwnd, |
| 762 | int x, |
| 763 | int y, |
| 764 | UINT keyFlags) |
| 765 | { |
| 766 | int button; |
| 767 | |
| 768 | if (s_button_pending > -1) |
| 769 | { |
| 770 | /* Delayed action for mouse down event */ |
| 771 | _OnMouseEvent(s_button_pending, s_x_pending, |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 772 | s_y_pending, FALSE, s_kFlags_pending); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | s_button_pending = -1; |
| 774 | } |
| 775 | if (s_uMsg == WM_MOUSEMOVE) |
| 776 | { |
| 777 | /* |
| 778 | * It's only a MOUSE_DRAG if one or more mouse buttons are being held |
| 779 | * down. |
| 780 | */ |
| 781 | if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON |
| 782 | | MK_XBUTTON1 | MK_XBUTTON2))) |
| 783 | { |
| 784 | gui_mouse_moved(x, y); |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * While button is down, keep grabbing mouse move events when |
| 790 | * the mouse goes outside the window |
| 791 | */ |
| 792 | SetCapture(s_textArea); |
| 793 | button = MOUSE_DRAG; |
| 794 | /* TRACE(" move at x %d, y %d\n", x, y); */ |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | ReleaseCapture(); |
| 799 | button = MOUSE_RELEASE; |
| 800 | /* TRACE(" up at x %d, y %d\n", x, y); */ |
| 801 | } |
| 802 | |
| 803 | _OnMouseEvent(button, x, y, FALSE, keyFlags); |
| 804 | } |
| 805 | |
| 806 | #ifdef FEAT_MENU |
| 807 | /* |
| 808 | * Find the vimmenu_T with the given id |
| 809 | */ |
| 810 | static vimmenu_T * |
| 811 | gui_mswin_find_menu( |
| 812 | vimmenu_T *pMenu, |
| 813 | int id) |
| 814 | { |
| 815 | vimmenu_T *pChildMenu; |
| 816 | |
| 817 | while (pMenu) |
| 818 | { |
| 819 | if (pMenu->id == (UINT)id) |
| 820 | break; |
| 821 | if (pMenu->children != NULL) |
| 822 | { |
| 823 | pChildMenu = gui_mswin_find_menu(pMenu->children, id); |
| 824 | if (pChildMenu) |
| 825 | { |
| 826 | pMenu = pChildMenu; |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | pMenu = pMenu->next; |
| 831 | } |
| 832 | return pMenu; |
| 833 | } |
| 834 | |
| 835 | static void |
| 836 | _OnMenu( |
| 837 | HWND hwnd, |
| 838 | int id, |
| 839 | HWND hwndCtl, |
| 840 | UINT codeNotify) |
| 841 | { |
| 842 | vimmenu_T *pMenu; |
| 843 | |
| 844 | pMenu = gui_mswin_find_menu(root_menu, id); |
| 845 | if (pMenu) |
| 846 | gui_menu_cb(pMenu); |
| 847 | } |
| 848 | #endif |
| 849 | |
| 850 | #ifdef MSWIN_FIND_REPLACE |
| 851 | /* |
| 852 | * Handle a Find/Replace window message. |
| 853 | */ |
| 854 | static void |
| 855 | _OnFindRepl(void) |
| 856 | { |
| 857 | int flags = 0; |
| 858 | int down; |
| 859 | |
| 860 | if (s_findrep_struct.Flags & FR_DIALOGTERM) |
| 861 | /* Give main window the focus back. */ |
| 862 | (void)SetFocus(s_hwnd); |
| 863 | |
| 864 | if (s_findrep_struct.Flags & FR_FINDNEXT) |
| 865 | { |
| 866 | flags = FRD_FINDNEXT; |
| 867 | |
| 868 | /* Give main window the focus back: this is so the cursor isn't |
| 869 | * hollow. */ |
| 870 | (void)SetFocus(s_hwnd); |
| 871 | } |
| 872 | else if (s_findrep_struct.Flags & FR_REPLACE) |
| 873 | { |
| 874 | flags = FRD_REPLACE; |
| 875 | |
| 876 | /* Give main window the focus back: this is so the cursor isn't |
| 877 | * hollow. */ |
| 878 | (void)SetFocus(s_hwnd); |
| 879 | } |
| 880 | else if (s_findrep_struct.Flags & FR_REPLACEALL) |
| 881 | { |
| 882 | flags = FRD_REPLACEALL; |
| 883 | } |
| 884 | |
| 885 | if (flags != 0) |
| 886 | { |
| 887 | /* Call the generic GUI function to do the actual work. */ |
| 888 | if (s_findrep_struct.Flags & FR_WHOLEWORD) |
| 889 | flags |= FRD_WHOLE_WORD; |
| 890 | if (s_findrep_struct.Flags & FR_MATCHCASE) |
| 891 | flags |= FRD_MATCH_CASE; |
| 892 | down = (s_findrep_struct.Flags & FR_DOWN) != 0; |
| 893 | gui_do_findrepl(flags, s_findrep_struct.lpstrFindWhat, |
| 894 | s_findrep_struct.lpstrReplaceWith, down); |
| 895 | } |
| 896 | } |
| 897 | #endif |
| 898 | |
| 899 | static void |
| 900 | HandleMouseHide(UINT uMsg, LPARAM lParam) |
| 901 | { |
| 902 | static LPARAM last_lParam = 0L; |
| 903 | |
| 904 | /* We sometimes get a mousemove when the mouse didn't move... */ |
| 905 | if (uMsg == WM_MOUSEMOVE) |
| 906 | { |
| 907 | if (lParam == last_lParam) |
| 908 | return; |
| 909 | last_lParam = lParam; |
| 910 | } |
| 911 | |
| 912 | /* Handle specially, to centralise coding. We need to be sure we catch all |
| 913 | * possible events which should cause us to restore the cursor (as it is a |
| 914 | * shared resource, we take full responsibility for it). |
| 915 | */ |
| 916 | switch (uMsg) |
| 917 | { |
| 918 | case WM_KEYUP: |
| 919 | case WM_CHAR: |
| 920 | /* |
| 921 | * blank out the pointer if necessary |
| 922 | */ |
| 923 | if (p_mh) |
| 924 | gui_mch_mousehide(TRUE); |
| 925 | break; |
| 926 | |
| 927 | case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */ |
| 928 | case WM_SYSCHAR: |
| 929 | case WM_MOUSEMOVE: /* show the pointer on any mouse action */ |
| 930 | case WM_LBUTTONDOWN: |
| 931 | case WM_LBUTTONUP: |
| 932 | case WM_MBUTTONDOWN: |
| 933 | case WM_MBUTTONUP: |
| 934 | case WM_RBUTTONDOWN: |
| 935 | case WM_RBUTTONUP: |
| 936 | case WM_XBUTTONDOWN: |
| 937 | case WM_XBUTTONUP: |
| 938 | case WM_NCMOUSEMOVE: |
| 939 | case WM_NCLBUTTONDOWN: |
| 940 | case WM_NCLBUTTONUP: |
| 941 | case WM_NCMBUTTONDOWN: |
| 942 | case WM_NCMBUTTONUP: |
| 943 | case WM_NCRBUTTONDOWN: |
| 944 | case WM_NCRBUTTONUP: |
| 945 | case WM_KILLFOCUS: |
| 946 | /* |
| 947 | * if the pointer is currently hidden, then we should show it. |
| 948 | */ |
| 949 | gui_mch_mousehide(FALSE); |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | static LRESULT CALLBACK |
| 955 | _TextAreaWndProc( |
| 956 | HWND hwnd, |
| 957 | UINT uMsg, |
| 958 | WPARAM wParam, |
| 959 | LPARAM lParam) |
| 960 | { |
| 961 | /* |
| 962 | TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n", |
| 963 | hwnd, uMsg, wParam, lParam); |
| 964 | */ |
| 965 | |
| 966 | HandleMouseHide(uMsg, lParam); |
| 967 | |
| 968 | s_uMsg = uMsg; |
| 969 | s_wParam = wParam; |
| 970 | s_lParam = lParam; |
| 971 | |
| 972 | #ifdef FEAT_BEVAL |
| 973 | TrackUserActivity(uMsg); |
| 974 | #endif |
| 975 | |
| 976 | switch (uMsg) |
| 977 | { |
| 978 | HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown); |
| 979 | HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown); |
| 980 | HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease); |
| 981 | HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown); |
| 982 | HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown); |
| 983 | HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease); |
| 984 | HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease); |
| 985 | HANDLE_MSG(hwnd, WM_PAINT, _OnPaint); |
| 986 | HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown); |
| 987 | HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown); |
| 988 | HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease); |
| 989 | #ifndef WIN16 /*<VN>*/ |
| 990 | HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown); |
| 991 | HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown); |
| 992 | HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease); |
| 993 | #endif |
| 994 | |
| 995 | #ifdef FEAT_BEVAL |
| 996 | case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam); |
| 997 | return TRUE; |
| 998 | #endif |
| 999 | |
| 1000 | default: |
| 1001 | return MyWindowProc(hwnd, uMsg, wParam, lParam); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | #if (defined(WIN3264) && defined(FEAT_MBYTE)) \ |
| 1006 | || defined(GLOBAL_IME) \ |
| 1007 | || defined(PROTO) |
| 1008 | # ifdef PROTO |
| 1009 | typedef int WINAPI; |
| 1010 | # endif |
| 1011 | |
| 1012 | LRESULT WINAPI |
| 1013 | vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 1014 | { |
| 1015 | # ifdef GLOBAL_IME |
| 1016 | return global_ime_DefWindowProc(hwnd, message, wParam, lParam); |
| 1017 | # else |
| 1018 | if (wide_WindowProc) |
| 1019 | return DefWindowProcW(hwnd, message, wParam, lParam); |
| 1020 | return DefWindowProc(hwnd, message, wParam, lParam); |
| 1021 | #endif |
| 1022 | } |
| 1023 | #endif |
| 1024 | |
| 1025 | /* |
| 1026 | * Called when the foreground or background color has been changed. |
| 1027 | */ |
| 1028 | void |
| 1029 | gui_mch_new_colors(void) |
| 1030 | { |
| 1031 | /* nothing to do? */ |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Set the colors to their default values. |
| 1036 | */ |
| 1037 | void |
| 1038 | gui_mch_def_colors() |
| 1039 | { |
| 1040 | gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT); |
| 1041 | gui.back_pixel = GetSysColor(COLOR_WINDOW); |
| 1042 | gui.def_norm_pixel = gui.norm_pixel; |
| 1043 | gui.def_back_pixel = gui.back_pixel; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * Open the GUI window which was created by a call to gui_mch_init(). |
| 1048 | */ |
| 1049 | int |
| 1050 | gui_mch_open(void) |
| 1051 | { |
| 1052 | #ifndef SW_SHOWDEFAULT |
| 1053 | # define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */ |
| 1054 | #endif |
| 1055 | /* Actually open the window, if not already visible |
| 1056 | * (may be done already in gui_mch_set_shellsize) */ |
| 1057 | if (!IsWindowVisible(s_hwnd)) |
| 1058 | ShowWindow(s_hwnd, SW_SHOWDEFAULT); |
| 1059 | |
| 1060 | return OK; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Get the position of the top left corner of the window. |
| 1065 | */ |
| 1066 | int |
| 1067 | gui_mch_get_winpos(int *x, int *y) |
| 1068 | { |
| 1069 | RECT rect; |
| 1070 | |
| 1071 | GetWindowRect(s_hwnd, &rect); |
| 1072 | *x = rect.left; |
| 1073 | *y = rect.top; |
| 1074 | return OK; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Set the position of the top left corner of the window to the given |
| 1079 | * coordinates. |
| 1080 | */ |
| 1081 | void |
| 1082 | gui_mch_set_winpos(int x, int y) |
| 1083 | { |
| 1084 | SetWindowPos(s_hwnd, NULL, x, y, 0, 0, |
| 1085 | SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); |
| 1086 | } |
| 1087 | void |
| 1088 | gui_mch_set_text_area_pos(int x, int y, int w, int h) |
| 1089 | { |
| 1090 | static int oldx = 0; |
| 1091 | static int oldy = 0; |
| 1092 | |
| 1093 | SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE); |
| 1094 | |
| 1095 | #ifdef FEAT_TOOLBAR |
| 1096 | if (vim_strchr(p_go, GO_TOOLBAR) != NULL) |
| 1097 | SendMessage(s_toolbarhwnd, WM_SIZE, |
| 1098 | (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16))); |
| 1099 | #endif |
| 1100 | /* When side scroll bar is unshown, the size of window will change. |
| 1101 | * then, the text area move left or right. thus client rect should be |
| 1102 | * forcely redraw. (Yasuhiro Matsumoto) */ |
| 1103 | if (oldx != x || oldy != y) |
| 1104 | { |
| 1105 | InvalidateRect(s_hwnd, NULL, FALSE); |
| 1106 | oldx = x; |
| 1107 | oldy = y; |
| 1108 | } |
| 1109 | |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | /* |
| 1114 | * Scrollbar stuff: |
| 1115 | */ |
| 1116 | |
| 1117 | void |
| 1118 | gui_mch_enable_scrollbar( |
| 1119 | scrollbar_T *sb, |
| 1120 | int flag) |
| 1121 | { |
| 1122 | ShowScrollBar(sb->id, SB_CTL, flag); |
| 1123 | |
| 1124 | /* TODO: When the window is maximized, the size of the window stays the |
| 1125 | * same, thus the size of the text area changes. On Win98 it's OK, on Win |
| 1126 | * NT 4.0 it's not... */ |
| 1127 | } |
| 1128 | |
| 1129 | void |
| 1130 | gui_mch_set_scrollbar_pos( |
| 1131 | scrollbar_T *sb, |
| 1132 | int x, |
| 1133 | int y, |
| 1134 | int w, |
| 1135 | int h) |
| 1136 | { |
| 1137 | SetWindowPos(sb->id, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW); |
| 1138 | } |
| 1139 | |
| 1140 | void |
| 1141 | gui_mch_create_scrollbar( |
| 1142 | scrollbar_T *sb, |
| 1143 | int orient) /* SBAR_VERT or SBAR_HORIZ */ |
| 1144 | { |
| 1145 | sb->id = CreateWindow( |
| 1146 | "SCROLLBAR", "Scrollbar", |
| 1147 | WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0, |
| 1148 | 10, /* Any value will do for now */ |
| 1149 | 10, /* Any value will do for now */ |
| 1150 | s_hwnd, NULL, |
| 1151 | s_hinst, NULL); |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * Find the scrollbar with the given hwnd. |
| 1156 | */ |
| 1157 | static scrollbar_T * |
| 1158 | gui_mswin_find_scrollbar(HWND hwnd) |
| 1159 | { |
| 1160 | win_T *wp; |
| 1161 | |
| 1162 | if (gui.bottom_sbar.id == hwnd) |
| 1163 | return &gui.bottom_sbar; |
| 1164 | FOR_ALL_WINDOWS(wp) |
| 1165 | { |
| 1166 | if (wp->w_scrollbars[SBAR_LEFT].id == hwnd) |
| 1167 | return &wp->w_scrollbars[SBAR_LEFT]; |
| 1168 | if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd) |
| 1169 | return &wp->w_scrollbars[SBAR_RIGHT]; |
| 1170 | } |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Get the character size of a font. |
| 1176 | */ |
| 1177 | static void |
| 1178 | GetFontSize(GuiFont font) |
| 1179 | { |
| 1180 | HWND hwnd = GetDesktopWindow(); |
| 1181 | HDC hdc = GetWindowDC(hwnd); |
| 1182 | HFONT hfntOld = SelectFont(hdc, (HFONT)font); |
| 1183 | TEXTMETRIC tm; |
| 1184 | |
| 1185 | GetTextMetrics(hdc, &tm); |
| 1186 | gui.char_width = tm.tmAveCharWidth + tm.tmOverhang; |
| 1187 | |
| 1188 | gui.char_height = tm.tmHeight |
| 1189 | #ifndef MSWIN16_FASTTEXT |
| 1190 | + p_linespace |
| 1191 | #endif |
| 1192 | ; |
| 1193 | |
| 1194 | SelectFont(hdc, hfntOld); |
| 1195 | |
| 1196 | ReleaseDC(hwnd, hdc); |
| 1197 | } |
| 1198 | |
| 1199 | int |
| 1200 | gui_mch_adjust_charsize(void) |
| 1201 | { |
| 1202 | GetFontSize(gui.norm_font); |
| 1203 | return OK; |
| 1204 | } |
| 1205 | |
| 1206 | static GuiFont |
| 1207 | get_font_handle(LOGFONT *lf) |
| 1208 | { |
| 1209 | HFONT font = NULL; |
| 1210 | |
| 1211 | /* Load the font */ |
| 1212 | font = CreateFontIndirect(lf); |
| 1213 | |
| 1214 | if (font == NULL) |
| 1215 | return NOFONT; |
| 1216 | |
| 1217 | return (GuiFont)font; |
| 1218 | } |
| 1219 | |
| 1220 | static int |
| 1221 | pixels_to_points(int pixels, int vertical) |
| 1222 | { |
| 1223 | int points; |
| 1224 | HWND hwnd; |
| 1225 | HDC hdc; |
| 1226 | |
| 1227 | hwnd = GetDesktopWindow(); |
| 1228 | hdc = GetWindowDC(hwnd); |
| 1229 | |
| 1230 | points = MulDiv(pixels, 72, |
| 1231 | GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX)); |
| 1232 | |
| 1233 | ReleaseDC(hwnd, hdc); |
| 1234 | |
| 1235 | return points; |
| 1236 | } |
| 1237 | |
| 1238 | GuiFont |
| 1239 | gui_mch_get_font( |
| 1240 | char_u *name, |
| 1241 | int giveErrorIfMissing) |
| 1242 | { |
| 1243 | LOGFONT lf; |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1244 | GuiFont font = NOFONT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1245 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1246 | if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK) |
| 1247 | font = get_font_handle(&lf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1248 | if (font == NOFONT && giveErrorIfMissing) |
| 1249 | EMSG2(_(e_font), name); |
| 1250 | return font; |
| 1251 | } |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1252 | |
Bram Moolenaar | dfccaf0 | 2004-12-31 20:56:11 +0000 | [diff] [blame] | 1253 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1254 | /* |
| 1255 | * Return the name of font "font" in allocated memory. |
| 1256 | * Don't know how to get the actual name, thus use the provided name. |
| 1257 | */ |
| 1258 | char_u * |
| 1259 | gui_mch_get_fontname(font, name) |
| 1260 | GuiFont font; |
| 1261 | char_u *name; |
| 1262 | { |
| 1263 | if (name == NULL) |
| 1264 | return NULL; |
| 1265 | return vim_strsave(name); |
| 1266 | } |
Bram Moolenaar | dfccaf0 | 2004-12-31 20:56:11 +0000 | [diff] [blame] | 1267 | #endif |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 1268 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | void |
| 1270 | gui_mch_free_font(GuiFont font) |
| 1271 | { |
| 1272 | if (font) |
| 1273 | DeleteObject((HFONT)font); |
| 1274 | } |
| 1275 | |
| 1276 | static int |
| 1277 | hex_digit(int c) |
| 1278 | { |
| 1279 | if (VIM_ISDIGIT(c)) |
| 1280 | return c - '0'; |
| 1281 | c = TOLOWER_ASC(c); |
| 1282 | if (c >= 'a' && c <= 'f') |
| 1283 | return c - 'a' + 10; |
| 1284 | return -1000; |
| 1285 | } |
| 1286 | /* |
| 1287 | * Return the Pixel value (color) for the given color name. |
| 1288 | * Return INVALCOLOR for error. |
| 1289 | */ |
| 1290 | guicolor_T |
| 1291 | gui_mch_get_color(char_u *name) |
| 1292 | { |
| 1293 | typedef struct guicolor_tTable |
| 1294 | { |
| 1295 | char *name; |
| 1296 | COLORREF color; |
| 1297 | } guicolor_tTable; |
| 1298 | |
| 1299 | static guicolor_tTable table[] = |
| 1300 | { |
| 1301 | {"Black", RGB(0x00, 0x00, 0x00)}, |
| 1302 | {"DarkGray", RGB(0x80, 0x80, 0x80)}, |
| 1303 | {"DarkGrey", RGB(0x80, 0x80, 0x80)}, |
| 1304 | {"Gray", RGB(0xC0, 0xC0, 0xC0)}, |
| 1305 | {"Grey", RGB(0xC0, 0xC0, 0xC0)}, |
| 1306 | {"LightGray", RGB(0xE0, 0xE0, 0xE0)}, |
| 1307 | {"LightGrey", RGB(0xE0, 0xE0, 0xE0)}, |
| 1308 | {"White", RGB(0xFF, 0xFF, 0xFF)}, |
| 1309 | {"DarkRed", RGB(0x80, 0x00, 0x00)}, |
| 1310 | {"Red", RGB(0xFF, 0x00, 0x00)}, |
| 1311 | {"LightRed", RGB(0xFF, 0xA0, 0xA0)}, |
| 1312 | {"DarkBlue", RGB(0x00, 0x00, 0x80)}, |
| 1313 | {"Blue", RGB(0x00, 0x00, 0xFF)}, |
| 1314 | {"LightBlue", RGB(0xA0, 0xA0, 0xFF)}, |
| 1315 | {"DarkGreen", RGB(0x00, 0x80, 0x00)}, |
| 1316 | {"Green", RGB(0x00, 0xFF, 0x00)}, |
| 1317 | {"LightGreen", RGB(0xA0, 0xFF, 0xA0)}, |
| 1318 | {"DarkCyan", RGB(0x00, 0x80, 0x80)}, |
| 1319 | {"Cyan", RGB(0x00, 0xFF, 0xFF)}, |
| 1320 | {"LightCyan", RGB(0xA0, 0xFF, 0xFF)}, |
| 1321 | {"DarkMagenta", RGB(0x80, 0x00, 0x80)}, |
| 1322 | {"Magenta", RGB(0xFF, 0x00, 0xFF)}, |
| 1323 | {"LightMagenta", RGB(0xFF, 0xA0, 0xFF)}, |
| 1324 | {"Brown", RGB(0x80, 0x40, 0x40)}, |
| 1325 | {"Yellow", RGB(0xFF, 0xFF, 0x00)}, |
| 1326 | {"LightYellow", RGB(0xFF, 0xFF, 0xA0)}, |
| 1327 | {"DarkYellow", RGB(0xBB, 0xBB, 0x00)}, |
| 1328 | {"SeaGreen", RGB(0x2E, 0x8B, 0x57)}, |
| 1329 | {"Orange", RGB(0xFF, 0xA5, 0x00)}, |
| 1330 | {"Purple", RGB(0xA0, 0x20, 0xF0)}, |
| 1331 | {"SlateBlue", RGB(0x6A, 0x5A, 0xCD)}, |
| 1332 | {"Violet", RGB(0xEE, 0x82, 0xEE)}, |
| 1333 | }; |
| 1334 | |
| 1335 | typedef struct SysColorTable |
| 1336 | { |
| 1337 | char *name; |
| 1338 | int color; |
| 1339 | } SysColorTable; |
| 1340 | |
| 1341 | static SysColorTable sys_table[] = |
| 1342 | { |
| 1343 | #ifdef WIN3264 |
| 1344 | {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW}, |
| 1345 | {"SYS_3DHILIGHT", COLOR_3DHILIGHT}, |
| 1346 | #ifndef __MINGW32__ |
| 1347 | {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT}, |
| 1348 | #endif |
| 1349 | {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT}, |
| 1350 | {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT}, |
| 1351 | {"SYS_3DLIGHT", COLOR_3DLIGHT}, |
| 1352 | {"SYS_3DSHADOW", COLOR_3DSHADOW}, |
| 1353 | {"SYS_DESKTOP", COLOR_DESKTOP}, |
| 1354 | {"SYS_INFOBK", COLOR_INFOBK}, |
| 1355 | {"SYS_INFOTEXT", COLOR_INFOTEXT}, |
| 1356 | {"SYS_3DFACE", COLOR_3DFACE}, |
| 1357 | #endif |
| 1358 | {"SYS_BTNFACE", COLOR_BTNFACE}, |
| 1359 | {"SYS_BTNSHADOW", COLOR_BTNSHADOW}, |
| 1360 | {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER}, |
| 1361 | {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION}, |
| 1362 | {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE}, |
| 1363 | {"SYS_BACKGROUND", COLOR_BACKGROUND}, |
| 1364 | {"SYS_BTNTEXT", COLOR_BTNTEXT}, |
| 1365 | {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT}, |
| 1366 | {"SYS_GRAYTEXT", COLOR_GRAYTEXT}, |
| 1367 | {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT}, |
| 1368 | {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT}, |
| 1369 | {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER}, |
| 1370 | {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION}, |
| 1371 | {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT}, |
| 1372 | {"SYS_MENU", COLOR_MENU}, |
| 1373 | {"SYS_MENUTEXT", COLOR_MENUTEXT}, |
| 1374 | {"SYS_SCROLLBAR", COLOR_SCROLLBAR}, |
| 1375 | {"SYS_WINDOW", COLOR_WINDOW}, |
| 1376 | {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME}, |
| 1377 | {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT} |
| 1378 | }; |
| 1379 | |
| 1380 | int r, g, b; |
| 1381 | int i; |
| 1382 | |
| 1383 | if (name[0] == '#' && strlen(name) == 7) |
| 1384 | { |
| 1385 | /* Name is in "#rrggbb" format */ |
| 1386 | r = hex_digit(name[1]) * 16 + hex_digit(name[2]); |
| 1387 | g = hex_digit(name[3]) * 16 + hex_digit(name[4]); |
| 1388 | b = hex_digit(name[5]) * 16 + hex_digit(name[6]); |
| 1389 | if (r < 0 || g < 0 || b < 0) |
| 1390 | return INVALCOLOR; |
| 1391 | return RGB(r, g, b); |
| 1392 | } |
| 1393 | else |
| 1394 | { |
| 1395 | /* Check if the name is one of the colors we know */ |
| 1396 | for (i = 0; i < sizeof(table) / sizeof(table[0]); i++) |
| 1397 | if (STRICMP(name, table[i].name) == 0) |
| 1398 | return table[i].color; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Try to look up a system colour. |
| 1403 | */ |
| 1404 | for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++) |
| 1405 | if (STRICMP(name, sys_table[i].name) == 0) |
| 1406 | return GetSysColor(sys_table[i].color); |
| 1407 | |
| 1408 | /* |
| 1409 | * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt". |
| 1410 | */ |
| 1411 | { |
| 1412 | #define LINE_LEN 100 |
| 1413 | FILE *fd; |
| 1414 | char line[LINE_LEN]; |
| 1415 | char_u *fname; |
| 1416 | |
| 1417 | fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt"); |
| 1418 | if (fname == NULL) |
| 1419 | return INVALCOLOR; |
| 1420 | |
| 1421 | fd = fopen((char *)fname, "rt"); |
| 1422 | vim_free(fname); |
| 1423 | if (fd == NULL) |
| 1424 | return INVALCOLOR; |
| 1425 | |
| 1426 | while (!feof(fd)) |
| 1427 | { |
| 1428 | int len; |
| 1429 | int pos; |
| 1430 | char *color; |
| 1431 | |
| 1432 | fgets(line, LINE_LEN, fd); |
| 1433 | len = (int)STRLEN(line); |
| 1434 | |
| 1435 | if (len <= 1 || line[len-1] != '\n') |
| 1436 | continue; |
| 1437 | |
| 1438 | line[len-1] = '\0'; |
| 1439 | |
| 1440 | i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos); |
| 1441 | if (i != 3) |
| 1442 | continue; |
| 1443 | |
| 1444 | color = line + pos; |
| 1445 | |
| 1446 | if (STRICMP(color, name) == 0) |
| 1447 | { |
| 1448 | fclose(fd); |
| 1449 | return (guicolor_T) RGB(r, g, b); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | fclose(fd); |
| 1454 | } |
| 1455 | |
| 1456 | return INVALCOLOR; |
| 1457 | } |
| 1458 | /* |
| 1459 | * Return OK if the key with the termcap name "name" is supported. |
| 1460 | */ |
| 1461 | int |
| 1462 | gui_mch_haskey(char_u *name) |
| 1463 | { |
| 1464 | int i; |
| 1465 | |
| 1466 | for (i = 0; special_keys[i].vim_code1 != NUL; i++) |
| 1467 | if (name[0] == special_keys[i].vim_code0 && |
| 1468 | name[1] == special_keys[i].vim_code1) |
| 1469 | return OK; |
| 1470 | return FAIL; |
| 1471 | } |
| 1472 | |
| 1473 | void |
| 1474 | gui_mch_beep(void) |
| 1475 | { |
| 1476 | MessageBeep(MB_OK); |
| 1477 | } |
| 1478 | /* |
| 1479 | * Invert a rectangle from row r, column c, for nr rows and nc columns. |
| 1480 | */ |
| 1481 | void |
| 1482 | gui_mch_invert_rectangle( |
| 1483 | int r, |
| 1484 | int c, |
| 1485 | int nr, |
| 1486 | int nc) |
| 1487 | { |
| 1488 | RECT rc; |
| 1489 | |
| 1490 | /* |
| 1491 | * Note: InvertRect() excludes right and bottom of rectangle. |
| 1492 | */ |
| 1493 | rc.left = FILL_X(c); |
| 1494 | rc.top = FILL_Y(r); |
| 1495 | rc.right = rc.left + nc * gui.char_width; |
| 1496 | rc.bottom = rc.top + nr * gui.char_height; |
| 1497 | InvertRect(s_hdc, &rc); |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * Iconify the GUI window. |
| 1502 | */ |
| 1503 | void |
| 1504 | gui_mch_iconify(void) |
| 1505 | { |
| 1506 | ShowWindow(s_hwnd, SW_MINIMIZE); |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * Draw a cursor without focus. |
| 1511 | */ |
| 1512 | void |
| 1513 | gui_mch_draw_hollow_cursor(guicolor_T color) |
| 1514 | { |
| 1515 | HBRUSH hbr; |
| 1516 | RECT rc; |
| 1517 | |
| 1518 | /* |
| 1519 | * Note: FrameRect() excludes right and bottom of rectangle. |
| 1520 | */ |
| 1521 | rc.left = FILL_X(gui.col); |
| 1522 | rc.top = FILL_Y(gui.row); |
| 1523 | rc.right = rc.left + gui.char_width; |
| 1524 | #ifdef FEAT_MBYTE |
| 1525 | if (mb_lefthalve(gui.row, gui.col)) |
| 1526 | rc.right += gui.char_width; |
| 1527 | #endif |
| 1528 | rc.bottom = rc.top + gui.char_height; |
| 1529 | hbr = CreateSolidBrush(color); |
| 1530 | FrameRect(s_hdc, &rc, hbr); |
| 1531 | DeleteBrush(hbr); |
| 1532 | } |
| 1533 | /* |
| 1534 | * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using |
| 1535 | * color "color". |
| 1536 | */ |
| 1537 | void |
| 1538 | gui_mch_draw_part_cursor( |
| 1539 | int w, |
| 1540 | int h, |
| 1541 | guicolor_T color) |
| 1542 | { |
| 1543 | HBRUSH hbr; |
| 1544 | RECT rc; |
| 1545 | |
| 1546 | /* |
| 1547 | * Note: FillRect() excludes right and bottom of rectangle. |
| 1548 | */ |
| 1549 | rc.left = |
| 1550 | #ifdef FEAT_RIGHTLEFT |
| 1551 | /* vertical line should be on the right of current point */ |
| 1552 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
| 1553 | #endif |
| 1554 | FILL_X(gui.col); |
| 1555 | rc.top = FILL_Y(gui.row) + gui.char_height - h; |
| 1556 | rc.right = rc.left + w; |
| 1557 | rc.bottom = rc.top + h; |
| 1558 | hbr = CreateSolidBrush(color); |
| 1559 | FillRect(s_hdc, &rc, hbr); |
| 1560 | DeleteBrush(hbr); |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * Process a single Windows message. |
| 1565 | * If one is not available we hang until one is. |
| 1566 | */ |
| 1567 | static void |
| 1568 | process_message(void) |
| 1569 | { |
| 1570 | MSG msg; |
| 1571 | UINT vk = 0; /* Virtual key */ |
| 1572 | char_u string[40]; |
| 1573 | int i; |
| 1574 | int modifiers = 0; |
| 1575 | int key; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1576 | #ifdef FEAT_MENU |
| 1577 | static char_u k10[] = {K_SPECIAL, 'k', ';', 0}; |
| 1578 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1579 | |
| 1580 | GetMessage(&msg, NULL, 0, 0); |
| 1581 | |
| 1582 | #ifdef FEAT_OLE |
| 1583 | /* Look after OLE Automation commands */ |
| 1584 | if (msg.message == WM_OLE) |
| 1585 | { |
| 1586 | char_u *str = (char_u *)msg.lParam; |
| 1587 | add_to_input_buf(str, (int)STRLEN(str)); |
| 1588 | vim_free(str); |
| 1589 | return; |
| 1590 | } |
| 1591 | #endif |
| 1592 | |
| 1593 | #ifdef FEAT_NETBEANS_INTG |
| 1594 | if (msg.message == WM_NETBEANS) |
| 1595 | { |
| 1596 | messageFromNetbeansW32(); |
| 1597 | return; |
| 1598 | } |
| 1599 | #endif |
| 1600 | |
| 1601 | #ifdef FEAT_SNIFF |
| 1602 | if (sniff_request_waiting && want_sniff_request) |
| 1603 | { |
| 1604 | static char_u bytes[3] = {CSI, (char_u)KS_EXTRA, (char_u)KE_SNIFF}; |
| 1605 | add_to_input_buf(bytes, 3); /* K_SNIFF */ |
| 1606 | sniff_request_waiting = 0; |
| 1607 | want_sniff_request = 0; |
| 1608 | /* request is handled in normal.c */ |
| 1609 | } |
| 1610 | if (msg.message == WM_USER) |
| 1611 | return; |
| 1612 | #endif |
| 1613 | |
| 1614 | #ifdef MSWIN_FIND_REPLACE |
| 1615 | /* Don't process messages used by the dialog */ |
| 1616 | if (s_findrep_hwnd != NULL && IsDialogMessage(s_findrep_hwnd, &msg)) |
| 1617 | { |
| 1618 | HandleMouseHide(msg.message, msg.lParam); |
| 1619 | return; |
| 1620 | } |
| 1621 | #endif |
| 1622 | |
| 1623 | /* |
| 1624 | * Check if it's a special key that we recognise. If not, call |
| 1625 | * TranslateMessage(). |
| 1626 | */ |
| 1627 | if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN) |
| 1628 | { |
| 1629 | vk = (int) msg.wParam; |
| 1630 | /* handle key after dead key, but ignore shift, alt and control */ |
| 1631 | if (dead_key && vk != VK_SHIFT && vk != VK_MENU && vk != VK_CONTROL) |
| 1632 | { |
| 1633 | dead_key = 0; |
| 1634 | /* handle non-alphabetic keys (ones that hopefully cannot generate |
| 1635 | * umlaut-characters), unless when control is down */ |
| 1636 | if (vk < 'A' || vk > 'Z' || (GetKeyState(VK_CONTROL) & 0x8000)) |
| 1637 | { |
| 1638 | MSG dm; |
| 1639 | |
| 1640 | dm.message = msg.message; |
| 1641 | dm.hwnd = msg.hwnd; |
| 1642 | dm.wParam = VK_SPACE; |
| 1643 | MyTranslateMessage(&dm); /* generate dead character */ |
| 1644 | if (vk != VK_SPACE) /* and send current character once more */ |
| 1645 | PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam); |
| 1646 | return; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /* Check for CTRL-BREAK */ |
| 1651 | if (vk == VK_CANCEL) |
| 1652 | { |
| 1653 | trash_input_buf(); |
| 1654 | got_int = TRUE; |
| 1655 | string[0] = Ctrl_C; |
| 1656 | add_to_input_buf(string, 1); |
| 1657 | } |
| 1658 | |
| 1659 | for (i = 0; special_keys[i].key_sym != 0; i++) |
| 1660 | { |
| 1661 | /* ignore VK_SPACE when ALT key pressed: system menu */ |
| 1662 | if (special_keys[i].key_sym == vk |
| 1663 | && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000))) |
| 1664 | { |
| 1665 | #ifdef FEAT_MENU |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1666 | /* Check for <F10>: Windows selects the menu. When <F10> is |
| 1667 | * mapped we want to use the mapping instead. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1668 | if (vk == VK_F10 |
| 1669 | && gui.menu_is_active |
Bram Moolenaar | c1087e6 | 2005-05-20 21:22:17 +0000 | [diff] [blame] | 1670 | && check_map(k10, State, FALSE, TRUE) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | #endif |
| 1673 | if (GetKeyState(VK_SHIFT) & 0x8000) |
| 1674 | modifiers |= MOD_MASK_SHIFT; |
| 1675 | /* |
| 1676 | * Don't use caps-lock as shift, because these are special keys |
| 1677 | * being considered here, and we only want letters to get |
| 1678 | * shifted -- webb |
| 1679 | */ |
| 1680 | /* |
| 1681 | if (GetKeyState(VK_CAPITAL) & 0x0001) |
| 1682 | modifiers ^= MOD_MASK_SHIFT; |
| 1683 | */ |
| 1684 | if (GetKeyState(VK_CONTROL) & 0x8000) |
| 1685 | modifiers |= MOD_MASK_CTRL; |
| 1686 | if (GetKeyState(VK_MENU) & 0x8000) |
| 1687 | modifiers |= MOD_MASK_ALT; |
| 1688 | |
| 1689 | if (special_keys[i].vim_code1 == NUL) |
| 1690 | key = special_keys[i].vim_code0; |
| 1691 | else |
| 1692 | key = TO_SPECIAL(special_keys[i].vim_code0, |
| 1693 | special_keys[i].vim_code1); |
| 1694 | key = simplify_key(key, &modifiers); |
| 1695 | if (key == CSI) |
| 1696 | key = K_CSI; |
| 1697 | |
| 1698 | if (modifiers) |
| 1699 | { |
| 1700 | string[0] = CSI; |
| 1701 | string[1] = KS_MODIFIER; |
| 1702 | string[2] = modifiers; |
| 1703 | add_to_input_buf(string, 3); |
| 1704 | } |
| 1705 | |
| 1706 | if (IS_SPECIAL(key)) |
| 1707 | { |
| 1708 | string[0] = CSI; |
| 1709 | string[1] = K_SECOND(key); |
| 1710 | string[2] = K_THIRD(key); |
| 1711 | add_to_input_buf(string, 3); |
| 1712 | } |
| 1713 | else |
| 1714 | { |
| 1715 | int len; |
| 1716 | |
| 1717 | /* Handle "key" as a Unicode character. */ |
| 1718 | len = char_to_string(key, string, 40); |
| 1719 | add_to_input_buf(string, len); |
| 1720 | } |
| 1721 | break; |
| 1722 | } |
| 1723 | } |
| 1724 | if (special_keys[i].key_sym == 0) |
| 1725 | { |
| 1726 | /* Some keys need C-S- where they should only need C-. |
| 1727 | * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since |
| 1728 | * system startup (Helmut Stiegler, 2003 Oct 3). */ |
| 1729 | if (vk != 0xff |
| 1730 | && (GetKeyState(VK_CONTROL) & 0x8000) |
| 1731 | && !(GetKeyState(VK_SHIFT) & 0x8000) |
| 1732 | && !(GetKeyState(VK_MENU) & 0x8000)) |
| 1733 | { |
| 1734 | /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */ |
| 1735 | if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^') |
| 1736 | { |
| 1737 | string[0] = Ctrl_HAT; |
| 1738 | add_to_input_buf(string, 1); |
| 1739 | } |
| 1740 | /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */ |
| 1741 | else if (vk == 0xBD) /* QWERTY for CTRL-'-' */ |
| 1742 | { |
| 1743 | string[0] = Ctrl__; |
| 1744 | add_to_input_buf(string, 1); |
| 1745 | } |
| 1746 | /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */ |
| 1747 | else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@') |
| 1748 | { |
| 1749 | string[0] = Ctrl_AT; |
| 1750 | add_to_input_buf(string, 1); |
| 1751 | } |
| 1752 | else |
| 1753 | MyTranslateMessage(&msg); |
| 1754 | } |
| 1755 | else |
| 1756 | MyTranslateMessage(&msg); |
| 1757 | } |
| 1758 | } |
| 1759 | #ifdef FEAT_MBYTE_IME |
| 1760 | else if (msg.message == WM_IME_NOTIFY) |
| 1761 | _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam); |
| 1762 | else if (msg.message == WM_KEYUP && im_get_status()) |
| 1763 | /* added for non-MS IME (Yasuhiro Matsumoto) */ |
| 1764 | MyTranslateMessage(&msg); |
| 1765 | #endif |
| 1766 | #if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME) |
| 1767 | /* GIME_TEST */ |
| 1768 | else if (msg.message == WM_IME_STARTCOMPOSITION) |
| 1769 | { |
| 1770 | POINT point; |
| 1771 | |
| 1772 | global_ime_set_font(&norm_logfont); |
| 1773 | point.x = FILL_X(gui.col); |
| 1774 | point.y = FILL_Y(gui.row); |
| 1775 | MapWindowPoints(s_textArea, s_hwnd, &point, 1); |
| 1776 | global_ime_set_position(&point); |
| 1777 | } |
| 1778 | #endif |
| 1779 | |
| 1780 | #ifdef FEAT_MENU |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1781 | /* Check for <F10>: Default effect is to select the menu. When <F10> is |
| 1782 | * mapped we need to stop it here to avoid strange effects (e.g., for the |
| 1783 | * key-up event) */ |
Bram Moolenaar | c1087e6 | 2005-05-20 21:22:17 +0000 | [diff] [blame] | 1784 | if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | #endif |
| 1786 | DispatchMessage(&msg); |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Catch up with any queued events. This may put keyboard input into the |
| 1791 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 1792 | * nothing in the event queue (& no timers pending), then we return |
| 1793 | * immediately. |
| 1794 | */ |
| 1795 | void |
| 1796 | gui_mch_update(void) |
| 1797 | { |
| 1798 | MSG msg; |
| 1799 | |
| 1800 | if (!s_busy_processing) |
| 1801 | while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) |
| 1802 | && !vim_is_input_buf_full()) |
| 1803 | process_message(); |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * GUI input routine called by gui_wait_for_chars(). Waits for a character |
| 1808 | * from the keyboard. |
| 1809 | * wtime == -1 Wait forever. |
| 1810 | * wtime == 0 This should never happen. |
| 1811 | * wtime > 0 Wait wtime milliseconds for a character. |
| 1812 | * Returns OK if a character was found to be available within the given time, |
| 1813 | * or FAIL otherwise. |
| 1814 | */ |
| 1815 | int |
| 1816 | gui_mch_wait_for_chars(int wtime) |
| 1817 | { |
| 1818 | MSG msg; |
| 1819 | int focus; |
| 1820 | |
| 1821 | s_timed_out = FALSE; |
| 1822 | |
| 1823 | if (wtime > 0) |
| 1824 | { |
| 1825 | /* Don't do anything while processing a (scroll) message. */ |
| 1826 | if (s_busy_processing) |
| 1827 | return FAIL; |
| 1828 | s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime, |
| 1829 | (TIMERPROC)_OnTimer); |
| 1830 | } |
| 1831 | |
| 1832 | allow_scrollbar = TRUE; |
| 1833 | |
| 1834 | focus = gui.in_focus; |
| 1835 | while (!s_timed_out) |
| 1836 | { |
| 1837 | /* Stop or start blinking when focus changes */ |
| 1838 | if (gui.in_focus != focus) |
| 1839 | { |
| 1840 | if (gui.in_focus) |
| 1841 | gui_mch_start_blink(); |
| 1842 | else |
| 1843 | gui_mch_stop_blink(); |
| 1844 | focus = gui.in_focus; |
| 1845 | } |
| 1846 | |
| 1847 | if (s_need_activate) |
| 1848 | { |
| 1849 | #ifdef WIN32 |
| 1850 | (void)SetForegroundWindow(s_hwnd); |
| 1851 | #else |
| 1852 | (void)SetActiveWindow(s_hwnd); |
| 1853 | #endif |
| 1854 | s_need_activate = FALSE; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * Don't use gui_mch_update() because then we will spin-lock until a |
| 1859 | * char arrives, instead we use GetMessage() to hang until an |
| 1860 | * event arrives. No need to check for input_buf_full because we are |
| 1861 | * returning as soon as it contains a single char -- webb |
| 1862 | */ |
| 1863 | process_message(); |
| 1864 | |
| 1865 | if (input_available()) |
| 1866 | { |
| 1867 | if (s_wait_timer != 0 && !s_timed_out) |
| 1868 | { |
| 1869 | KillTimer(NULL, s_wait_timer); |
| 1870 | |
| 1871 | /* Eat spurious WM_TIMER messages */ |
| 1872 | while (PeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE)) |
| 1873 | ; |
| 1874 | s_wait_timer = 0; |
| 1875 | } |
| 1876 | allow_scrollbar = FALSE; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1877 | |
| 1878 | /* Clear pending mouse button, the release event may have been |
| 1879 | * taken by the dialog window. */ |
| 1880 | s_button_pending = -1; |
| 1881 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1882 | return OK; |
| 1883 | } |
| 1884 | } |
| 1885 | allow_scrollbar = FALSE; |
| 1886 | return FAIL; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Clear a rectangular region of the screen from text pos (row1, col1) to |
| 1891 | * (row2, col2) inclusive. |
| 1892 | */ |
| 1893 | void |
| 1894 | gui_mch_clear_block( |
| 1895 | int row1, |
| 1896 | int col1, |
| 1897 | int row2, |
| 1898 | int col2) |
| 1899 | { |
| 1900 | RECT rc; |
| 1901 | |
| 1902 | /* |
| 1903 | * Clear one extra pixel at the far right, for when bold characters have |
| 1904 | * spilled over to the window border. |
| 1905 | * Note: FillRect() excludes right and bottom of rectangle. |
| 1906 | */ |
| 1907 | rc.left = FILL_X(col1); |
| 1908 | rc.top = FILL_Y(row1); |
| 1909 | rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1); |
| 1910 | rc.bottom = FILL_Y(row2 + 1); |
| 1911 | clear_rect(&rc); |
| 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * Clear the whole text window. |
| 1916 | */ |
| 1917 | void |
| 1918 | gui_mch_clear_all(void) |
| 1919 | { |
| 1920 | RECT rc; |
| 1921 | |
| 1922 | rc.left = 0; |
| 1923 | rc.top = 0; |
| 1924 | rc.right = Columns * gui.char_width + 2 * gui.border_width; |
| 1925 | rc.bottom = Rows * gui.char_height + 2 * gui.border_width; |
| 1926 | clear_rect(&rc); |
| 1927 | } |
| 1928 | /* |
| 1929 | * Menu stuff. |
| 1930 | */ |
| 1931 | |
| 1932 | void |
| 1933 | gui_mch_enable_menu(int flag) |
| 1934 | { |
| 1935 | #ifdef FEAT_MENU |
| 1936 | SetMenu(s_hwnd, flag ? s_menuBar : NULL); |
| 1937 | #endif |
| 1938 | } |
| 1939 | |
| 1940 | void |
| 1941 | gui_mch_set_menu_pos( |
| 1942 | int x, |
| 1943 | int y, |
| 1944 | int w, |
| 1945 | int h) |
| 1946 | { |
| 1947 | /* It will be in the right place anyway */ |
| 1948 | } |
| 1949 | |
| 1950 | #if defined(FEAT_MENU) || defined(PROTO) |
| 1951 | /* |
| 1952 | * Make menu item hidden or not hidden |
| 1953 | */ |
| 1954 | void |
| 1955 | gui_mch_menu_hidden( |
| 1956 | vimmenu_T *menu, |
| 1957 | int hidden) |
| 1958 | { |
| 1959 | /* |
| 1960 | * This doesn't do what we want. Hmm, just grey the menu items for now. |
| 1961 | */ |
| 1962 | /* |
| 1963 | if (hidden) |
| 1964 | EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED); |
| 1965 | else |
| 1966 | EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED); |
| 1967 | */ |
| 1968 | gui_mch_menu_grey(menu, hidden); |
| 1969 | } |
| 1970 | |
| 1971 | /* |
| 1972 | * This is called after setting all the menus to grey/hidden or not. |
| 1973 | */ |
| 1974 | void |
| 1975 | gui_mch_draw_menubar(void) |
| 1976 | { |
| 1977 | DrawMenuBar(s_hwnd); |
| 1978 | } |
| 1979 | #endif /*FEAT_MENU*/ |
| 1980 | |
| 1981 | #ifndef PROTO |
| 1982 | void |
| 1983 | #ifdef VIMDLL |
| 1984 | _export |
| 1985 | #endif |
| 1986 | _cdecl |
| 1987 | SaveInst(HINSTANCE hInst) |
| 1988 | { |
| 1989 | s_hinst = hInst; |
| 1990 | } |
| 1991 | #endif |
| 1992 | |
| 1993 | /* |
| 1994 | * Return the RGB value of a pixel as a long. |
| 1995 | */ |
| 1996 | long_u |
| 1997 | gui_mch_get_rgb(guicolor_T pixel) |
| 1998 | { |
| 1999 | return (GetRValue(pixel) << 16) + (GetGValue(pixel) << 8) |
| 2000 | + GetBValue(pixel); |
| 2001 | } |
| 2002 | |
| 2003 | #if defined(FEAT_GUI_DIALOG) || defined(PROTO) |
| 2004 | /* Convert pixels in X to dialog units */ |
| 2005 | static WORD |
| 2006 | PixelToDialogX(int numPixels) |
| 2007 | { |
| 2008 | return (WORD)((numPixels * 4) / s_dlgfntwidth); |
| 2009 | } |
| 2010 | |
| 2011 | /* Convert pixels in Y to dialog units */ |
| 2012 | static WORD |
| 2013 | PixelToDialogY(int numPixels) |
| 2014 | { |
| 2015 | return (WORD)((numPixels * 8) / s_dlgfntheight); |
| 2016 | } |
| 2017 | |
| 2018 | /* Return the width in pixels of the given text in the given DC. */ |
| 2019 | static int |
| 2020 | GetTextWidth(HDC hdc, char_u *str, int len) |
| 2021 | { |
| 2022 | SIZE size; |
| 2023 | |
| 2024 | GetTextExtentPoint(hdc, str, len, &size); |
| 2025 | return size.cx; |
| 2026 | } |
| 2027 | |
| 2028 | #ifdef FEAT_MBYTE |
| 2029 | /* |
| 2030 | * Return the width in pixels of the given text in the given DC, taking care |
| 2031 | * of 'encoding' to active codepage conversion. |
| 2032 | */ |
| 2033 | static int |
| 2034 | GetTextWidthEnc(HDC hdc, char_u *str, int len) |
| 2035 | { |
| 2036 | SIZE size; |
| 2037 | WCHAR *wstr; |
| 2038 | int n; |
| 2039 | int wlen = len; |
| 2040 | |
| 2041 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2042 | { |
| 2043 | /* 'encoding' differs from active codepage: convert text and use wide |
| 2044 | * function */ |
| 2045 | wstr = enc_to_ucs2(str, &wlen); |
| 2046 | if (wstr != NULL) |
| 2047 | { |
| 2048 | n = GetTextExtentPointW(hdc, wstr, wlen, &size); |
| 2049 | vim_free(wstr); |
| 2050 | if (n) |
| 2051 | return size.cx; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | return GetTextWidth(hdc, str, len); |
| 2056 | } |
| 2057 | #else |
| 2058 | # define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l)) |
| 2059 | #endif |
| 2060 | |
| 2061 | /* |
| 2062 | * A quick little routine that will center one window over another, handy for |
| 2063 | * dialog boxes. Taken from the Win32SDK samples. |
| 2064 | */ |
| 2065 | static BOOL |
| 2066 | CenterWindow( |
| 2067 | HWND hwndChild, |
| 2068 | HWND hwndParent) |
| 2069 | { |
| 2070 | RECT rChild, rParent; |
| 2071 | int wChild, hChild, wParent, hParent; |
| 2072 | int wScreen, hScreen, xNew, yNew; |
| 2073 | HDC hdc; |
| 2074 | |
| 2075 | GetWindowRect(hwndChild, &rChild); |
| 2076 | wChild = rChild.right - rChild.left; |
| 2077 | hChild = rChild.bottom - rChild.top; |
| 2078 | |
| 2079 | /* If Vim is minimized put the window in the middle of the screen. */ |
| 2080 | if (IsMinimized(hwndParent)) |
| 2081 | { |
| 2082 | #ifdef WIN16 |
| 2083 | rParent.left = 0; |
| 2084 | rParent.top = 0; |
| 2085 | rParent.right = GetSystemMetrics(SM_CXSCREEN); |
| 2086 | rParent.bottom = GetSystemMetrics(SM_CYFULLSCREEN); |
| 2087 | #else |
| 2088 | SystemParametersInfo(SPI_GETWORKAREA, 0, &rParent, 0); |
| 2089 | #endif |
| 2090 | } |
| 2091 | else |
| 2092 | GetWindowRect(hwndParent, &rParent); |
| 2093 | wParent = rParent.right - rParent.left; |
| 2094 | hParent = rParent.bottom - rParent.top; |
| 2095 | |
| 2096 | hdc = GetDC(hwndChild); |
| 2097 | wScreen = GetDeviceCaps (hdc, HORZRES); |
| 2098 | hScreen = GetDeviceCaps (hdc, VERTRES); |
| 2099 | ReleaseDC(hwndChild, hdc); |
| 2100 | |
| 2101 | xNew = rParent.left + ((wParent - wChild) /2); |
| 2102 | if (xNew < 0) |
| 2103 | { |
| 2104 | xNew = 0; |
| 2105 | } |
| 2106 | else if ((xNew+wChild) > wScreen) |
| 2107 | { |
| 2108 | xNew = wScreen - wChild; |
| 2109 | } |
| 2110 | |
| 2111 | yNew = rParent.top + ((hParent - hChild) /2); |
| 2112 | if (yNew < 0) |
| 2113 | yNew = 0; |
| 2114 | else if ((yNew+hChild) > hScreen) |
| 2115 | yNew = hScreen - hChild; |
| 2116 | |
| 2117 | return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0, |
| 2118 | SWP_NOSIZE | SWP_NOZORDER); |
| 2119 | } |
| 2120 | #endif /* FEAT_GUI_DIALOG */ |
| 2121 | |
| 2122 | void |
| 2123 | gui_mch_activate_window(void) |
| 2124 | { |
| 2125 | (void)SetActiveWindow(s_hwnd); |
| 2126 | } |
| 2127 | |
| 2128 | #if defined(FEAT_TOOLBAR) || defined(PROTO) |
| 2129 | void |
| 2130 | gui_mch_show_toolbar(int showit) |
| 2131 | { |
| 2132 | if (s_toolbarhwnd == NULL) |
| 2133 | return; |
| 2134 | |
| 2135 | if (showit) |
| 2136 | ShowWindow(s_toolbarhwnd, SW_SHOW); |
| 2137 | else |
| 2138 | ShowWindow(s_toolbarhwnd, SW_HIDE); |
| 2139 | } |
| 2140 | |
| 2141 | /* Then number of bitmaps is fixed. Exit is missing! */ |
| 2142 | #define TOOLBAR_BITMAP_COUNT 31 |
| 2143 | |
| 2144 | #endif |
| 2145 | |
| 2146 | /* |
| 2147 | * ":simalt" command. |
| 2148 | */ |
| 2149 | void |
| 2150 | ex_simalt(exarg_T *eap) |
| 2151 | { |
| 2152 | char_u *keys = eap->arg; |
| 2153 | |
| 2154 | PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0); |
| 2155 | while (*keys) |
| 2156 | { |
| 2157 | if (*keys == '~') |
| 2158 | *keys = ' '; /* for showing system menu */ |
| 2159 | PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0); |
| 2160 | keys++; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * Create the find & replace dialogs. |
| 2166 | * You can't have both at once: ":find" when replace is showing, destroys |
| 2167 | * the replace dialog first, and the other way around. |
| 2168 | */ |
| 2169 | #ifdef MSWIN_FIND_REPLACE |
| 2170 | static void |
| 2171 | initialise_findrep(char_u *initial_string) |
| 2172 | { |
| 2173 | int wword = FALSE; |
| 2174 | int mcase = !p_ic; |
| 2175 | char_u *entry_text; |
| 2176 | |
| 2177 | /* Get the search string to use. */ |
| 2178 | entry_text = get_find_dialog_text(initial_string, &wword, &mcase); |
| 2179 | |
| 2180 | s_findrep_struct.hwndOwner = s_hwnd; |
| 2181 | s_findrep_struct.Flags = FR_DOWN; |
| 2182 | if (mcase) |
| 2183 | s_findrep_struct.Flags |= FR_MATCHCASE; |
| 2184 | if (wword) |
| 2185 | s_findrep_struct.Flags |= FR_WHOLEWORD; |
| 2186 | if (entry_text != NULL && *entry_text != NUL) |
| 2187 | { |
| 2188 | STRNCPY(s_findrep_struct.lpstrFindWhat, entry_text, |
| 2189 | s_findrep_struct.wFindWhatLen); |
| 2190 | s_findrep_struct.lpstrFindWhat[s_findrep_struct.wFindWhatLen - 1] = NUL; |
| 2191 | s_findrep_struct.lpstrReplaceWith[0] = NUL; |
| 2192 | } |
| 2193 | vim_free(entry_text); |
| 2194 | } |
| 2195 | #endif |
| 2196 | |
| 2197 | void |
| 2198 | gui_mch_find_dialog(exarg_T *eap) |
| 2199 | { |
| 2200 | #ifdef MSWIN_FIND_REPLACE |
| 2201 | if (s_findrep_msg != 0) |
| 2202 | { |
| 2203 | if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find) |
| 2204 | DestroyWindow(s_findrep_hwnd); |
| 2205 | |
| 2206 | if (!IsWindow(s_findrep_hwnd)) |
| 2207 | { |
| 2208 | initialise_findrep(eap->arg); |
| 2209 | s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct); |
| 2210 | } |
| 2211 | |
| 2212 | (void)SetWindowText(s_findrep_hwnd, |
| 2213 | (LPCSTR)_("Find string (use '\\\\' to find a '\\')")); |
| 2214 | (void)SetFocus(s_findrep_hwnd); |
| 2215 | |
| 2216 | s_findrep_is_find = TRUE; |
| 2217 | } |
| 2218 | #endif |
| 2219 | } |
| 2220 | |
| 2221 | |
| 2222 | void |
| 2223 | gui_mch_replace_dialog(exarg_T *eap) |
| 2224 | { |
| 2225 | #ifdef MSWIN_FIND_REPLACE |
| 2226 | if (s_findrep_msg != 0) |
| 2227 | { |
| 2228 | if (IsWindow(s_findrep_hwnd) && s_findrep_is_find) |
| 2229 | DestroyWindow(s_findrep_hwnd); |
| 2230 | |
| 2231 | if (!IsWindow(s_findrep_hwnd)) |
| 2232 | { |
| 2233 | initialise_findrep(eap->arg); |
| 2234 | s_findrep_hwnd = ReplaceText((LPFINDREPLACE) &s_findrep_struct); |
| 2235 | } |
| 2236 | |
| 2237 | (void)SetWindowText(s_findrep_hwnd, |
| 2238 | (LPCSTR)_("Find & Replace (use '\\\\' to find a '\\')")); |
| 2239 | (void)SetFocus(s_findrep_hwnd); |
| 2240 | |
| 2241 | s_findrep_is_find = FALSE; |
| 2242 | } |
| 2243 | #endif |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | /* |
| 2248 | * Set visibility of the pointer. |
| 2249 | */ |
| 2250 | void |
| 2251 | gui_mch_mousehide(int hide) |
| 2252 | { |
| 2253 | if (hide != gui.pointer_hidden) |
| 2254 | { |
| 2255 | ShowCursor(!hide); |
| 2256 | gui.pointer_hidden = hide; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | #ifdef FEAT_MENU |
| 2261 | static void |
| 2262 | gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y) |
| 2263 | { |
| 2264 | /* Unhide the mouse, we don't get move events here. */ |
| 2265 | gui_mch_mousehide(FALSE); |
| 2266 | |
| 2267 | (void)TrackPopupMenu( |
| 2268 | (HMENU)menu->submenu_id, |
| 2269 | TPM_LEFTALIGN | TPM_LEFTBUTTON, |
| 2270 | x, y, |
| 2271 | (int)0, /*reserved param*/ |
| 2272 | s_hwnd, |
| 2273 | NULL); |
| 2274 | /* |
| 2275 | * NOTE: The pop-up menu can eat the mouse up event. |
| 2276 | * We deal with this in normal.c. |
| 2277 | */ |
| 2278 | } |
| 2279 | #endif |
| 2280 | |
| 2281 | /* |
| 2282 | * Got a message when the system will go down. |
| 2283 | */ |
| 2284 | static void |
| 2285 | _OnEndSession(void) |
| 2286 | { |
| 2287 | getout_preserve_modified(1); |
| 2288 | } |
| 2289 | |
| 2290 | /* |
| 2291 | * Get this message when the user clicks on the cross in the top right corner |
| 2292 | * of a Windows95 window. |
| 2293 | */ |
| 2294 | static void |
| 2295 | _OnClose( |
| 2296 | HWND hwnd) |
| 2297 | { |
| 2298 | gui_shell_closed(); |
| 2299 | } |
| 2300 | |
| 2301 | /* |
| 2302 | * Get a message when the window is being destroyed. |
| 2303 | */ |
| 2304 | static void |
| 2305 | _OnDestroy( |
| 2306 | HWND hwnd) |
| 2307 | { |
| 2308 | #ifdef WIN16_3DLOOK |
| 2309 | Ctl3dUnregister(s_hinst); |
| 2310 | #endif |
| 2311 | if (!destroying) |
| 2312 | _OnClose(hwnd); |
| 2313 | } |
| 2314 | |
| 2315 | static void |
| 2316 | _OnPaint( |
| 2317 | HWND hwnd) |
| 2318 | { |
| 2319 | if (!IsMinimized(hwnd)) |
| 2320 | { |
| 2321 | PAINTSTRUCT ps; |
| 2322 | |
| 2323 | out_flush(); /* make sure all output has been processed */ |
| 2324 | (void)BeginPaint(hwnd, &ps); |
| 2325 | |
| 2326 | #ifdef FEAT_MBYTE |
| 2327 | /* prevent multi-byte characters from misprinting on an invalid |
| 2328 | * rectangle */ |
| 2329 | if (has_mbyte) |
| 2330 | { |
| 2331 | RECT rect; |
| 2332 | |
| 2333 | GetClientRect(hwnd, &rect); |
| 2334 | ps.rcPaint.left = rect.left; |
| 2335 | ps.rcPaint.right = rect.right; |
| 2336 | } |
| 2337 | #endif |
| 2338 | |
| 2339 | if (!IsRectEmpty(&ps.rcPaint)) |
| 2340 | gui_redraw(ps.rcPaint.left, ps.rcPaint.top, |
| 2341 | ps.rcPaint.right - ps.rcPaint.left + 1, |
| 2342 | ps.rcPaint.bottom - ps.rcPaint.top + 1); |
| 2343 | EndPaint(hwnd, &ps); |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | static void |
| 2348 | _OnSize( |
| 2349 | HWND hwnd, |
| 2350 | UINT state, |
| 2351 | int cx, |
| 2352 | int cy) |
| 2353 | { |
| 2354 | if (!IsMinimized(hwnd)) |
| 2355 | { |
| 2356 | gui_resize_shell(cx, cy); |
| 2357 | |
| 2358 | #ifdef FEAT_MENU |
| 2359 | /* Menu bar may wrap differently now */ |
| 2360 | gui_mswin_get_menu_height(TRUE); |
| 2361 | #endif |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | static void |
| 2366 | _OnSetFocus( |
| 2367 | HWND hwnd, |
| 2368 | HWND hwndOldFocus) |
| 2369 | { |
| 2370 | gui_focus_change(TRUE); |
| 2371 | (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0); |
| 2372 | } |
| 2373 | |
| 2374 | static void |
| 2375 | _OnKillFocus( |
| 2376 | HWND hwnd, |
| 2377 | HWND hwndNewFocus) |
| 2378 | { |
| 2379 | gui_focus_change(FALSE); |
| 2380 | (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0); |
| 2381 | } |
| 2382 | |
| 2383 | /* |
| 2384 | * Get a message when the user switches back to vim |
| 2385 | */ |
| 2386 | #ifdef WIN16 |
| 2387 | static BOOL |
| 2388 | #else |
| 2389 | static LRESULT |
| 2390 | #endif |
| 2391 | _OnActivateApp( |
| 2392 | HWND hwnd, |
| 2393 | BOOL fActivate, |
| 2394 | #ifdef WIN16 |
| 2395 | HTASK dwThreadId |
| 2396 | #else |
| 2397 | DWORD dwThreadId |
| 2398 | #endif |
| 2399 | ) |
| 2400 | { |
| 2401 | /* we call gui_focus_change() in _OnSetFocus() */ |
| 2402 | /* gui_focus_change((int)fActivate); */ |
| 2403 | return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId); |
| 2404 | } |
| 2405 | |
| 2406 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 2407 | void |
| 2408 | gui_mch_destroy_scrollbar(scrollbar_T *sb) |
| 2409 | { |
| 2410 | DestroyWindow(sb->id); |
| 2411 | } |
| 2412 | #endif |
| 2413 | |
| 2414 | /* |
Bram Moolenaar | 9588a0f | 2005-01-08 21:45:39 +0000 | [diff] [blame] | 2415 | * Get current mouse coordinates in text window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2416 | */ |
Bram Moolenaar | 9588a0f | 2005-01-08 21:45:39 +0000 | [diff] [blame] | 2417 | void |
Bram Moolenaar | a40c500 | 2005-01-09 21:16:21 +0000 | [diff] [blame] | 2418 | gui_mch_getmouse(int *x, int *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2419 | { |
| 2420 | RECT rct; |
| 2421 | POINT mp; |
| 2422 | |
| 2423 | (void)GetWindowRect(s_textArea, &rct); |
| 2424 | (void)GetCursorPos((LPPOINT)&mp); |
Bram Moolenaar | 9588a0f | 2005-01-08 21:45:39 +0000 | [diff] [blame] | 2425 | *x = (int)(mp.x - rct.left); |
| 2426 | *y = (int)(mp.y - rct.top); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | * Move mouse pointer to character at (x, y). |
| 2431 | */ |
| 2432 | void |
| 2433 | gui_mch_setmouse(int x, int y) |
| 2434 | { |
| 2435 | RECT rct; |
| 2436 | |
| 2437 | (void)GetWindowRect(s_textArea, &rct); |
| 2438 | (void)SetCursorPos(x + gui.border_offset + rct.left, |
| 2439 | y + gui.border_offset + rct.top); |
| 2440 | } |
| 2441 | |
| 2442 | static void |
| 2443 | gui_mswin_get_valid_dimensions( |
| 2444 | int w, |
| 2445 | int h, |
| 2446 | int *valid_w, |
| 2447 | int *valid_h) |
| 2448 | { |
| 2449 | int base_width, base_height; |
| 2450 | |
| 2451 | base_width = gui_get_base_width() |
| 2452 | + GetSystemMetrics(SM_CXFRAME) * 2; |
| 2453 | base_height = gui_get_base_height() |
| 2454 | + GetSystemMetrics(SM_CYFRAME) * 2 |
| 2455 | + GetSystemMetrics(SM_CYCAPTION) |
| 2456 | #ifdef FEAT_MENU |
| 2457 | + gui_mswin_get_menu_height(FALSE) |
| 2458 | #endif |
| 2459 | ; |
| 2460 | *valid_w = base_width + |
| 2461 | ((w - base_width) / gui.char_width) * gui.char_width; |
| 2462 | *valid_h = base_height + |
| 2463 | ((h - base_height) / gui.char_height) * gui.char_height; |
| 2464 | } |
| 2465 | |
| 2466 | void |
| 2467 | gui_mch_get_screen_dimensions(int *screen_w, int *screen_h) |
| 2468 | { |
| 2469 | |
| 2470 | *screen_w = GetSystemMetrics(SM_CXFULLSCREEN) |
| 2471 | - GetSystemMetrics(SM_CXFRAME) * 2; |
| 2472 | /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include |
| 2473 | * the menubar for MSwin, we subtract it from the screen height, so that |
| 2474 | * the window size can be made to fit on the screen. */ |
| 2475 | *screen_h = GetSystemMetrics(SM_CYFULLSCREEN) |
| 2476 | - GetSystemMetrics(SM_CYFRAME) * 2 |
| 2477 | #ifdef FEAT_MENU |
| 2478 | - gui_mswin_get_menu_height(FALSE) |
| 2479 | #endif |
| 2480 | ; |
| 2481 | } |
| 2482 | |
| 2483 | void |
| 2484 | gui_mch_flash(int msec) |
| 2485 | { |
| 2486 | RECT rc; |
| 2487 | |
| 2488 | /* |
| 2489 | * Note: InvertRect() excludes right and bottom of rectangle. |
| 2490 | */ |
| 2491 | rc.left = 0; |
| 2492 | rc.top = 0; |
| 2493 | rc.right = gui.num_cols * gui.char_width; |
| 2494 | rc.bottom = gui.num_rows * gui.char_height; |
| 2495 | InvertRect(s_hdc, &rc); |
| 2496 | gui_mch_flush(); /* make sure it's displayed */ |
| 2497 | |
| 2498 | ui_delay((long)msec, TRUE); /* wait for a few msec */ |
| 2499 | |
| 2500 | InvertRect(s_hdc, &rc); |
| 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | * Return flags used for scrolling. |
| 2505 | * The SW_INVALIDATE is required when part of the window is covered or |
| 2506 | * off-screen. Refer to MS KB Q75236. |
| 2507 | */ |
| 2508 | static int |
| 2509 | get_scroll_flags(void) |
| 2510 | { |
| 2511 | HWND hwnd; |
| 2512 | RECT rcVim, rcOther, rcDest; |
| 2513 | |
| 2514 | GetWindowRect(s_hwnd, &rcVim); |
| 2515 | for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; ) |
| 2516 | if (IsWindowVisible(hwnd)) |
| 2517 | { |
| 2518 | GetWindowRect(hwnd, &rcOther); |
| 2519 | if (IntersectRect(&rcDest, &rcVim, &rcOther)) |
| 2520 | return SW_INVALIDATE; |
| 2521 | } |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * Delete the given number of lines from the given row, scrolling up any |
| 2527 | * text further down within the scroll region. |
| 2528 | */ |
| 2529 | void |
| 2530 | gui_mch_delete_lines( |
| 2531 | int row, |
| 2532 | int num_lines) |
| 2533 | { |
| 2534 | RECT rc; |
| 2535 | |
| 2536 | rc.left = FILL_X(gui.scroll_region_left); |
| 2537 | rc.right = FILL_X(gui.scroll_region_right + 1); |
| 2538 | rc.top = FILL_Y(row); |
| 2539 | rc.bottom = FILL_Y(gui.scroll_region_bot + 1); |
| 2540 | |
| 2541 | ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height, |
| 2542 | &rc, &rc, NULL, NULL, get_scroll_flags()); |
| 2543 | |
| 2544 | UpdateWindow(s_textArea); |
| 2545 | /* This seems to be required to avoid the cursor disappearing when |
| 2546 | * scrolling such that the cursor ends up in the top-left character on |
| 2547 | * the screen... But why? (Webb) */ |
| 2548 | /* It's probably fixed by disabling drawing the cursor while scrolling. */ |
| 2549 | /* gui.cursor_is_valid = FALSE; */ |
| 2550 | |
| 2551 | gui_clear_block(gui.scroll_region_bot - num_lines + 1, |
| 2552 | gui.scroll_region_left, |
| 2553 | gui.scroll_region_bot, gui.scroll_region_right); |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * Insert the given number of lines before the given row, scrolling down any |
| 2558 | * following text within the scroll region. |
| 2559 | */ |
| 2560 | void |
| 2561 | gui_mch_insert_lines( |
| 2562 | int row, |
| 2563 | int num_lines) |
| 2564 | { |
| 2565 | RECT rc; |
| 2566 | |
| 2567 | rc.left = FILL_X(gui.scroll_region_left); |
| 2568 | rc.right = FILL_X(gui.scroll_region_right + 1); |
| 2569 | rc.top = FILL_Y(row); |
| 2570 | rc.bottom = FILL_Y(gui.scroll_region_bot + 1); |
| 2571 | /* The SW_INVALIDATE is required when part of the window is covered or |
| 2572 | * off-screen. How do we avoid it when it's not needed? */ |
| 2573 | ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height, |
| 2574 | &rc, &rc, NULL, NULL, get_scroll_flags()); |
| 2575 | |
| 2576 | UpdateWindow(s_textArea); |
| 2577 | |
| 2578 | gui_clear_block(row, gui.scroll_region_left, |
| 2579 | row + num_lines - 1, gui.scroll_region_right); |
| 2580 | } |
| 2581 | |
| 2582 | |
| 2583 | void |
| 2584 | gui_mch_exit(int rc) |
| 2585 | { |
| 2586 | ReleaseDC(s_textArea, s_hdc); |
| 2587 | DeleteObject(s_brush); |
| 2588 | |
| 2589 | #ifdef FEAT_TEAROFF |
| 2590 | /* Unload the tearoff bitmap */ |
| 2591 | (void)DeleteObject((HGDIOBJ)s_htearbitmap); |
| 2592 | #endif |
| 2593 | |
| 2594 | /* Destroy our window (if we have one). */ |
| 2595 | if (s_hwnd != NULL) |
| 2596 | { |
| 2597 | destroying = TRUE; /* ignore WM_DESTROY message now */ |
| 2598 | DestroyWindow(s_hwnd); |
| 2599 | } |
| 2600 | |
| 2601 | #ifdef GLOBAL_IME |
| 2602 | global_ime_end(); |
| 2603 | #endif |
| 2604 | } |
| 2605 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2606 | static char_u * |
| 2607 | logfont2name(LOGFONT lf) |
| 2608 | { |
| 2609 | char *p; |
| 2610 | char *res; |
| 2611 | char *charset_name; |
| 2612 | |
| 2613 | charset_name = charset_id2name((int)lf.lfCharSet); |
| 2614 | res = alloc((unsigned)(strlen(lf.lfFaceName) + 20 |
| 2615 | + (charset_name == NULL ? 0 : strlen(charset_name) + 2))); |
| 2616 | if (res != NULL) |
| 2617 | { |
| 2618 | p = res; |
| 2619 | /* make a normal font string out of the lf thing:*/ |
| 2620 | sprintf((char *)p, "%s:h%d", lf.lfFaceName, pixels_to_points( |
| 2621 | lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE)); |
| 2622 | while (*p) |
| 2623 | { |
| 2624 | if (*p == ' ') |
| 2625 | *p = '_'; |
| 2626 | ++p; |
| 2627 | } |
| 2628 | #ifndef MSWIN16_FASTTEXT |
| 2629 | if (lf.lfItalic) |
| 2630 | STRCAT(p, ":i"); |
| 2631 | if (lf.lfWeight >= FW_BOLD) |
| 2632 | STRCAT(p, ":b"); |
| 2633 | #endif |
| 2634 | if (lf.lfUnderline) |
| 2635 | STRCAT(p, ":u"); |
| 2636 | if (lf.lfStrikeOut) |
| 2637 | STRCAT(p, ":s"); |
| 2638 | if (charset_name != NULL) |
| 2639 | { |
| 2640 | STRCAT(p, ":c"); |
| 2641 | STRCAT(p, charset_name); |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | return res; |
| 2646 | } |
| 2647 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2648 | /* |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2649 | * Initialise vim to use the font with the given name. |
| 2650 | * Return FAIL if the font could not be loaded, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2651 | */ |
| 2652 | int |
| 2653 | gui_mch_init_font(char_u *font_name, int fontset) |
| 2654 | { |
| 2655 | LOGFONT lf; |
| 2656 | GuiFont font = NOFONT; |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2657 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | |
| 2659 | /* Load the font */ |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2660 | if (get_logfont(&lf, font_name, NULL, TRUE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2661 | font = get_font_handle(&lf); |
| 2662 | if (font == NOFONT) |
| 2663 | return FAIL; |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2664 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | if (font_name == NULL) |
| 2666 | font_name = lf.lfFaceName; |
| 2667 | #if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME) |
| 2668 | norm_logfont = lf; |
| 2669 | #endif |
| 2670 | #ifdef FEAT_MBYTE_IME |
| 2671 | im_set_font(&lf); |
| 2672 | #endif |
| 2673 | gui_mch_free_font(gui.norm_font); |
| 2674 | gui.norm_font = font; |
| 2675 | current_font_height = lf.lfHeight; |
| 2676 | GetFontSize(font); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2678 | p = logfont2name(lf); |
| 2679 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | { |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2681 | hl_set_font_name(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2683 | /* When setting 'guifont' to "*" replace it with the actual font name. |
| 2684 | * */ |
| 2685 | if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2687 | vim_free(p_guifont); |
| 2688 | p_guifont = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2689 | } |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 2690 | else |
| 2691 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | #ifndef MSWIN16_FASTTEXT |
| 2695 | gui_mch_free_font(gui.ital_font); |
| 2696 | gui.ital_font = NOFONT; |
| 2697 | gui_mch_free_font(gui.bold_font); |
| 2698 | gui.bold_font = NOFONT; |
| 2699 | gui_mch_free_font(gui.boldital_font); |
| 2700 | gui.boldital_font = NOFONT; |
| 2701 | |
| 2702 | if (!lf.lfItalic) |
| 2703 | { |
| 2704 | lf.lfItalic = TRUE; |
| 2705 | gui.ital_font = get_font_handle(&lf); |
| 2706 | lf.lfItalic = FALSE; |
| 2707 | } |
| 2708 | if (lf.lfWeight < FW_BOLD) |
| 2709 | { |
| 2710 | lf.lfWeight = FW_BOLD; |
| 2711 | gui.bold_font = get_font_handle(&lf); |
| 2712 | if (!lf.lfItalic) |
| 2713 | { |
| 2714 | lf.lfItalic = TRUE; |
| 2715 | gui.boldital_font = get_font_handle(&lf); |
| 2716 | } |
| 2717 | } |
| 2718 | #endif |
| 2719 | |
| 2720 | return OK; |
| 2721 | } |
| 2722 | |
| 2723 | /* |
| 2724 | * Return TRUE if the GUI window is maximized, filling the whole screen. |
| 2725 | */ |
| 2726 | int |
| 2727 | gui_mch_maximized() |
| 2728 | { |
| 2729 | return IsZoomed(s_hwnd); |
| 2730 | } |
| 2731 | |
| 2732 | /* |
| 2733 | * Called when the font changed while the window is maximized. Compute the |
| 2734 | * new Rows and Columns. This is like resizing the window. |
| 2735 | */ |
| 2736 | void |
| 2737 | gui_mch_newfont() |
| 2738 | { |
| 2739 | RECT rect; |
| 2740 | |
| 2741 | GetWindowRect(s_hwnd, &rect); |
| 2742 | gui_resize_shell(rect.right - rect.left |
| 2743 | - GetSystemMetrics(SM_CXFRAME) * 2, |
| 2744 | rect.bottom - rect.top |
| 2745 | - GetSystemMetrics(SM_CYFRAME) * 2 |
| 2746 | - GetSystemMetrics(SM_CYCAPTION) |
| 2747 | #ifdef FEAT_MENU |
| 2748 | - gui_mswin_get_menu_height(FALSE) |
| 2749 | #endif |
| 2750 | ); |
| 2751 | } |
| 2752 | |
| 2753 | /* |
| 2754 | * Set the window title |
| 2755 | */ |
| 2756 | void |
| 2757 | gui_mch_settitle( |
| 2758 | char_u *title, |
| 2759 | char_u *icon) |
| 2760 | { |
| 2761 | #ifdef FEAT_MBYTE |
| 2762 | if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP()) |
| 2763 | { |
| 2764 | WCHAR *wbuf; |
| 2765 | int n; |
| 2766 | |
| 2767 | /* Convert the title from 'encoding' to ucs2. */ |
| 2768 | wbuf = (WCHAR *)enc_to_ucs2(title, NULL); |
| 2769 | if (wbuf != NULL) |
| 2770 | { |
| 2771 | n = SetWindowTextW(s_hwnd, wbuf); |
| 2772 | vim_free(wbuf); |
| 2773 | if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 2774 | return; |
| 2775 | /* Retry with non-wide function (for Windows 98). */ |
| 2776 | } |
| 2777 | } |
| 2778 | #endif |
| 2779 | SetWindowText(s_hwnd, (LPCSTR)(title == NULL ? "VIM" : (char *)title)); |
| 2780 | } |
| 2781 | |
| 2782 | #ifdef FEAT_MOUSESHAPE |
| 2783 | /* Table for shape IDCs. Keep in sync with the mshape_names[] table in |
| 2784 | * misc2.c! */ |
| 2785 | static LPCSTR mshape_idcs[] = |
| 2786 | { |
| 2787 | MAKEINTRESOURCE(IDC_ARROW), /* arrow */ |
| 2788 | MAKEINTRESOURCE(0), /* blank */ |
| 2789 | MAKEINTRESOURCE(IDC_IBEAM), /* beam */ |
| 2790 | MAKEINTRESOURCE(IDC_SIZENS), /* updown */ |
| 2791 | MAKEINTRESOURCE(IDC_SIZENS), /* udsizing */ |
| 2792 | MAKEINTRESOURCE(IDC_SIZEWE), /* leftright */ |
| 2793 | MAKEINTRESOURCE(IDC_SIZEWE), /* lrsizing */ |
| 2794 | MAKEINTRESOURCE(IDC_WAIT), /* busy */ |
| 2795 | #ifdef WIN3264 |
| 2796 | MAKEINTRESOURCE(IDC_NO), /* no */ |
| 2797 | #else |
| 2798 | MAKEINTRESOURCE(IDC_ICON), /* no */ |
| 2799 | #endif |
| 2800 | MAKEINTRESOURCE(IDC_ARROW), /* crosshair */ |
| 2801 | MAKEINTRESOURCE(IDC_ARROW), /* hand1 */ |
| 2802 | MAKEINTRESOURCE(IDC_ARROW), /* hand2 */ |
| 2803 | MAKEINTRESOURCE(IDC_ARROW), /* pencil */ |
| 2804 | MAKEINTRESOURCE(IDC_ARROW), /* question */ |
| 2805 | MAKEINTRESOURCE(IDC_ARROW), /* right-arrow */ |
| 2806 | MAKEINTRESOURCE(IDC_UPARROW), /* up-arrow */ |
| 2807 | MAKEINTRESOURCE(IDC_ARROW) /* last one */ |
| 2808 | }; |
| 2809 | |
| 2810 | void |
| 2811 | mch_set_mouse_shape(int shape) |
| 2812 | { |
| 2813 | LPCSTR idc; |
| 2814 | |
| 2815 | if (shape == MSHAPE_HIDE) |
| 2816 | ShowCursor(FALSE); |
| 2817 | else |
| 2818 | { |
| 2819 | if (shape >= MSHAPE_NUMBERED) |
| 2820 | idc = MAKEINTRESOURCE(IDC_ARROW); |
| 2821 | else |
| 2822 | idc = mshape_idcs[shape]; |
| 2823 | #ifdef _WIN64 |
| 2824 | SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc)); |
| 2825 | #else |
| 2826 | # ifdef WIN32 |
| 2827 | SetClassLong(s_textArea, GCL_HCURSOR, (LONG)LoadCursor(NULL, idc)); |
| 2828 | # else |
| 2829 | SetClassWord(s_textArea, GCW_HCURSOR, (WORD)LoadCursor(NULL, idc)); |
| 2830 | # endif |
| 2831 | #endif |
| 2832 | if (!p_mh) |
| 2833 | { |
| 2834 | POINT mp; |
| 2835 | |
| 2836 | /* Set the position to make it redrawn with the new shape. */ |
| 2837 | (void)GetCursorPos((LPPOINT)&mp); |
| 2838 | (void)SetCursorPos(mp.x, mp.y); |
| 2839 | ShowCursor(TRUE); |
| 2840 | } |
| 2841 | } |
| 2842 | } |
| 2843 | #endif |
| 2844 | |
| 2845 | #ifdef FEAT_BROWSE |
| 2846 | /* |
| 2847 | * The file browser exists in two versions: with "W" uses wide characters, |
| 2848 | * without "W" the current codepage. When FEAT_MBYTE is defined and on |
| 2849 | * Windows NT/2000/XP the "W" functions are used. |
| 2850 | */ |
| 2851 | |
| 2852 | # if defined(FEAT_MBYTE) && defined(WIN3264) |
| 2853 | /* |
| 2854 | * Wide version of convert_filter(). Keep in sync! |
| 2855 | */ |
| 2856 | static WCHAR * |
| 2857 | convert_filterW(char_u *s) |
| 2858 | { |
| 2859 | WCHAR *res; |
| 2860 | unsigned s_len = (unsigned)STRLEN(s); |
| 2861 | unsigned i; |
| 2862 | |
| 2863 | res = (WCHAR *)alloc((s_len + 3) * sizeof(WCHAR)); |
| 2864 | if (res != NULL) |
| 2865 | { |
| 2866 | for (i = 0; i < s_len; ++i) |
| 2867 | if (s[i] == '\t' || s[i] == '\n') |
| 2868 | res[i] = '\0'; |
| 2869 | else |
| 2870 | res[i] = s[i]; |
| 2871 | res[s_len] = NUL; |
| 2872 | /* Add two extra NULs to make sure it's properly terminated. */ |
| 2873 | res[s_len + 1] = NUL; |
| 2874 | res[s_len + 2] = NUL; |
| 2875 | } |
| 2876 | return res; |
| 2877 | } |
| 2878 | |
| 2879 | /* |
| 2880 | * Wide version of gui_mch_browse(). Keep in sync! |
| 2881 | */ |
| 2882 | static char_u * |
| 2883 | gui_mch_browseW( |
| 2884 | int saving, |
| 2885 | char_u *title, |
| 2886 | char_u *dflt, |
| 2887 | char_u *ext, |
| 2888 | char_u *initdir, |
| 2889 | char_u *filter) |
| 2890 | { |
| 2891 | /* We always use the wide function. This means enc_to_ucs2() must work, |
| 2892 | * otherwise it fails miserably! */ |
| 2893 | OPENFILENAMEW fileStruct; |
| 2894 | WCHAR fileBuf[MAXPATHL]; |
| 2895 | WCHAR *wp; |
| 2896 | int i; |
| 2897 | WCHAR *titlep = NULL; |
| 2898 | WCHAR *extp = NULL; |
| 2899 | WCHAR *initdirp = NULL; |
| 2900 | WCHAR *filterp; |
| 2901 | char_u *p; |
| 2902 | |
| 2903 | if (dflt == NULL) |
| 2904 | fileBuf[0] = NUL; |
| 2905 | else |
| 2906 | { |
| 2907 | wp = enc_to_ucs2(dflt, NULL); |
| 2908 | if (wp == NULL) |
| 2909 | fileBuf[0] = NUL; |
| 2910 | else |
| 2911 | { |
| 2912 | for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i) |
| 2913 | fileBuf[i] = wp[i]; |
| 2914 | fileBuf[i] = NUL; |
| 2915 | vim_free(wp); |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | /* Convert the filter to Windows format. */ |
| 2920 | filterp = convert_filterW(filter); |
| 2921 | |
| 2922 | memset(&fileStruct, 0, sizeof(OPENFILENAMEW)); |
| 2923 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
| 2924 | /* be compatible with Windows NT 4.0 */ |
| 2925 | /* TODO: what to use for OPENFILENAMEW??? */ |
| 2926 | fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400); |
| 2927 | #else |
| 2928 | fileStruct.lStructSize = sizeof(fileStruct); |
| 2929 | #endif |
| 2930 | |
| 2931 | if (title != NULL) |
| 2932 | titlep = enc_to_ucs2(title, NULL); |
| 2933 | fileStruct.lpstrTitle = titlep; |
| 2934 | |
| 2935 | if (ext != NULL) |
| 2936 | extp = enc_to_ucs2(ext, NULL); |
| 2937 | fileStruct.lpstrDefExt = extp; |
| 2938 | |
| 2939 | fileStruct.lpstrFile = fileBuf; |
| 2940 | fileStruct.nMaxFile = MAXPATHL; |
| 2941 | fileStruct.lpstrFilter = filterp; |
| 2942 | fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/ |
| 2943 | /* has an initial dir been specified? */ |
| 2944 | if (initdir != NULL && *initdir != NUL) |
| 2945 | { |
| 2946 | /* Must have backslashes here, no matter what 'shellslash' says */ |
| 2947 | initdirp = enc_to_ucs2(initdir, NULL); |
| 2948 | if (initdirp != NULL) |
| 2949 | { |
| 2950 | for (wp = initdirp; *wp != NUL; ++wp) |
| 2951 | if (*wp == '/') |
| 2952 | *wp = '\\'; |
| 2953 | } |
| 2954 | fileStruct.lpstrInitialDir = initdirp; |
| 2955 | } |
| 2956 | |
| 2957 | /* |
| 2958 | * TODO: Allow selection of multiple files. Needs another arg to this |
| 2959 | * function to ask for it, and need to use OFN_ALLOWMULTISELECT below. |
| 2960 | * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on |
| 2961 | * files that don't exist yet, so I haven't put it in. What about |
| 2962 | * OFN_PATHMUSTEXIST? |
| 2963 | * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog. |
| 2964 | */ |
| 2965 | fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY); |
| 2966 | #ifdef FEAT_SHORTCUT |
| 2967 | if (curbuf->b_p_bin) |
| 2968 | fileStruct.Flags |= OFN_NODEREFERENCELINKS; |
| 2969 | #endif |
| 2970 | if (saving) |
| 2971 | { |
| 2972 | if (!GetSaveFileNameW(&fileStruct)) |
| 2973 | return NULL; |
| 2974 | } |
| 2975 | else |
| 2976 | { |
| 2977 | if (!GetOpenFileNameW(&fileStruct)) |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
| 2981 | vim_free(filterp); |
| 2982 | vim_free(initdirp); |
| 2983 | vim_free(titlep); |
| 2984 | vim_free(extp); |
| 2985 | |
| 2986 | /* Convert from UCS2 to 'encoding'. */ |
| 2987 | p = ucs2_to_enc(fileBuf, NULL); |
| 2988 | if (p != NULL) |
| 2989 | /* when out of memory we get garbage for non-ASCII chars */ |
| 2990 | STRCPY(fileBuf, p); |
| 2991 | vim_free(p); |
| 2992 | |
| 2993 | /* Give focus back to main window (when using MDI). */ |
| 2994 | SetFocus(s_hwnd); |
| 2995 | |
| 2996 | /* Shorten the file name if possible */ |
| 2997 | mch_dirname(IObuff, IOSIZE); |
| 2998 | p = shorten_fname((char_u *)fileBuf, IObuff); |
| 2999 | if (p == NULL) |
| 3000 | p = (char_u *)fileBuf; |
| 3001 | return vim_strsave(p); |
| 3002 | } |
| 3003 | # endif /* FEAT_MBYTE */ |
| 3004 | |
| 3005 | |
| 3006 | /* |
| 3007 | * Convert the string s to the proper format for a filter string by replacing |
| 3008 | * the \t and \n delimeters with \0. |
| 3009 | * Returns the converted string in allocated memory. |
| 3010 | * |
| 3011 | * Keep in sync with convert_filterW() above! |
| 3012 | */ |
| 3013 | static char_u * |
| 3014 | convert_filter(char_u *s) |
| 3015 | { |
| 3016 | char_u *res; |
| 3017 | unsigned s_len = (unsigned)STRLEN(s); |
| 3018 | unsigned i; |
| 3019 | |
| 3020 | res = alloc(s_len + 3); |
| 3021 | if (res != NULL) |
| 3022 | { |
| 3023 | for (i = 0; i < s_len; ++i) |
| 3024 | if (s[i] == '\t' || s[i] == '\n') |
| 3025 | res[i] = '\0'; |
| 3026 | else |
| 3027 | res[i] = s[i]; |
| 3028 | res[s_len] = NUL; |
| 3029 | /* Add two extra NULs to make sure it's properly terminated. */ |
| 3030 | res[s_len + 1] = NUL; |
| 3031 | res[s_len + 2] = NUL; |
| 3032 | } |
| 3033 | return res; |
| 3034 | } |
| 3035 | |
| 3036 | /* |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 3037 | * Select a directory. |
| 3038 | */ |
| 3039 | char_u * |
| 3040 | gui_mch_browsedir(char_u *title, char_u *initdir) |
| 3041 | { |
| 3042 | /* We fake this: Use a filter that doesn't select anything and a default |
| 3043 | * file name that won't be used. */ |
| 3044 | return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL, |
| 3045 | initdir, (char_u *)_("Directory\t*.nothing\n")); |
| 3046 | } |
| 3047 | |
| 3048 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3049 | * Pop open a file browser and return the file selected, in allocated memory, |
| 3050 | * or NULL if Cancel is hit. |
| 3051 | * saving - TRUE if the file will be saved to, FALSE if it will be opened. |
| 3052 | * title - Title message for the file browser dialog. |
| 3053 | * dflt - Default name of file. |
| 3054 | * ext - Default extension to be added to files without extensions. |
| 3055 | * initdir - directory in which to open the browser (NULL = current dir) |
| 3056 | * filter - Filter for matched files to choose from. |
| 3057 | * |
| 3058 | * Keep in sync with gui_mch_browseW() above! |
| 3059 | */ |
| 3060 | char_u * |
| 3061 | gui_mch_browse( |
| 3062 | int saving, |
| 3063 | char_u *title, |
| 3064 | char_u *dflt, |
| 3065 | char_u *ext, |
| 3066 | char_u *initdir, |
| 3067 | char_u *filter) |
| 3068 | { |
| 3069 | OPENFILENAME fileStruct; |
| 3070 | char_u fileBuf[MAXPATHL]; |
| 3071 | char_u *initdirp = NULL; |
| 3072 | char_u *filterp; |
| 3073 | char_u *p; |
| 3074 | |
| 3075 | # if defined(FEAT_MBYTE) && defined(WIN3264) |
| 3076 | if (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT) |
| 3077 | return gui_mch_browseW(saving, title, dflt, ext, initdir, filter); |
| 3078 | # endif |
| 3079 | |
| 3080 | if (dflt == NULL) |
| 3081 | fileBuf[0] = NUL; |
| 3082 | else |
| 3083 | { |
| 3084 | STRNCPY(fileBuf, dflt, MAXPATHL - 1); |
| 3085 | fileBuf[MAXPATHL - 1] = NUL; |
| 3086 | } |
| 3087 | |
| 3088 | /* Convert the filter to Windows format. */ |
| 3089 | filterp = convert_filter(filter); |
| 3090 | |
| 3091 | memset(&fileStruct, 0, sizeof(OPENFILENAME)); |
| 3092 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
| 3093 | /* be compatible with Windows NT 4.0 */ |
| 3094 | fileStruct.lStructSize = sizeof(OPENFILENAME_SIZE_VERSION_400); |
| 3095 | #else |
| 3096 | fileStruct.lStructSize = sizeof(fileStruct); |
| 3097 | #endif |
| 3098 | |
| 3099 | fileStruct.lpstrTitle = title; |
| 3100 | fileStruct.lpstrDefExt = ext; |
| 3101 | |
| 3102 | fileStruct.lpstrFile = fileBuf; |
| 3103 | fileStruct.nMaxFile = MAXPATHL; |
| 3104 | fileStruct.lpstrFilter = filterp; |
| 3105 | fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/ |
| 3106 | /* has an initial dir been specified? */ |
| 3107 | if (initdir != NULL && *initdir != NUL) |
| 3108 | { |
| 3109 | /* Must have backslashes here, no matter what 'shellslash' says */ |
| 3110 | initdirp = vim_strsave(initdir); |
| 3111 | if (initdirp != NULL) |
| 3112 | for (p = initdirp; *p != NUL; ++p) |
| 3113 | if (*p == '/') |
| 3114 | *p = '\\'; |
| 3115 | fileStruct.lpstrInitialDir = initdirp; |
| 3116 | } |
| 3117 | |
| 3118 | /* |
| 3119 | * TODO: Allow selection of multiple files. Needs another arg to this |
| 3120 | * function to ask for it, and need to use OFN_ALLOWMULTISELECT below. |
| 3121 | * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on |
| 3122 | * files that don't exist yet, so I haven't put it in. What about |
| 3123 | * OFN_PATHMUSTEXIST? |
| 3124 | * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog. |
| 3125 | */ |
| 3126 | fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY); |
| 3127 | #ifdef FEAT_SHORTCUT |
| 3128 | if (curbuf->b_p_bin) |
| 3129 | fileStruct.Flags |= OFN_NODEREFERENCELINKS; |
| 3130 | #endif |
| 3131 | if (saving) |
| 3132 | { |
| 3133 | if (!GetSaveFileName(&fileStruct)) |
| 3134 | return NULL; |
| 3135 | } |
| 3136 | else |
| 3137 | { |
| 3138 | if (!GetOpenFileName(&fileStruct)) |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | |
| 3142 | vim_free(filterp); |
| 3143 | vim_free(initdirp); |
| 3144 | |
| 3145 | /* Give focus back to main window (when using MDI). */ |
| 3146 | SetFocus(s_hwnd); |
| 3147 | |
| 3148 | /* Shorten the file name if possible */ |
| 3149 | mch_dirname(IObuff, IOSIZE); |
| 3150 | p = shorten_fname((char_u *)fileBuf, IObuff); |
| 3151 | if (p == NULL) |
| 3152 | p = (char_u *)fileBuf; |
| 3153 | return vim_strsave(p); |
| 3154 | } |
| 3155 | #endif /* FEAT_BROWSE */ |
| 3156 | |
| 3157 | static void |
| 3158 | _OnDropFiles( |
| 3159 | HWND hwnd, |
| 3160 | HDROP hDrop) |
| 3161 | { |
| 3162 | #ifdef FEAT_WINDOWS |
| 3163 | #ifdef WIN3264 |
| 3164 | # define BUFPATHLEN _MAX_PATH |
| 3165 | # define DRAGQVAL 0xFFFFFFFF |
| 3166 | #else |
| 3167 | # define BUFPATHLEN MAXPATHL |
| 3168 | # define DRAGQVAL 0xFFFF |
| 3169 | #endif |
| 3170 | #ifdef FEAT_MBYTE |
| 3171 | WCHAR wszFile[BUFPATHLEN]; |
| 3172 | #endif |
| 3173 | char szFile[BUFPATHLEN]; |
| 3174 | UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0); |
| 3175 | UINT i; |
| 3176 | char_u **fnames; |
| 3177 | POINT pt; |
| 3178 | int_u modifiers = 0; |
| 3179 | |
| 3180 | /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */ |
| 3181 | |
| 3182 | /* Obtain dropped position */ |
| 3183 | DragQueryPoint(hDrop, &pt); |
| 3184 | MapWindowPoints(s_hwnd, s_textArea, &pt, 1); |
| 3185 | |
| 3186 | # ifdef FEAT_VISUAL |
| 3187 | reset_VIsual(); |
| 3188 | # endif |
| 3189 | |
| 3190 | fnames = (char_u **)alloc(cFiles * sizeof(char_u *)); |
| 3191 | |
| 3192 | if (fnames != NULL) |
| 3193 | for (i = 0; i < cFiles; ++i) |
| 3194 | { |
| 3195 | #ifdef FEAT_MBYTE |
| 3196 | if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0) |
| 3197 | fnames[i] = ucs2_to_enc(wszFile, NULL); |
| 3198 | else |
| 3199 | #endif |
| 3200 | { |
| 3201 | DragQueryFile(hDrop, i, szFile, BUFPATHLEN); |
| 3202 | fnames[i] = vim_strsave(szFile); |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | DragFinish(hDrop); |
| 3207 | |
| 3208 | if (fnames != NULL) |
| 3209 | { |
| 3210 | if ((GetKeyState(VK_SHIFT) & 0x8000) != 0) |
| 3211 | modifiers |= MOUSE_SHIFT; |
| 3212 | if ((GetKeyState(VK_CONTROL) & 0x8000) != 0) |
| 3213 | modifiers |= MOUSE_CTRL; |
| 3214 | if ((GetKeyState(VK_MENU) & 0x8000) != 0) |
| 3215 | modifiers |= MOUSE_ALT; |
| 3216 | |
| 3217 | gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles); |
| 3218 | |
| 3219 | s_need_activate = TRUE; |
| 3220 | } |
| 3221 | #endif |
| 3222 | } |
| 3223 | |
| 3224 | static int |
| 3225 | _OnScroll( |
| 3226 | HWND hwnd, |
| 3227 | HWND hwndCtl, |
| 3228 | UINT code, |
| 3229 | int pos) |
| 3230 | { |
| 3231 | static UINT prev_code = 0; /* code of previous call */ |
| 3232 | scrollbar_T *sb, *sb_info; |
| 3233 | long val; |
| 3234 | int dragging = FALSE; |
| 3235 | int dont_scroll_save = dont_scroll; |
| 3236 | #ifndef WIN3264 |
| 3237 | int nPos; |
| 3238 | #else |
| 3239 | SCROLLINFO si; |
| 3240 | |
| 3241 | si.cbSize = sizeof(si); |
| 3242 | si.fMask = SIF_POS; |
| 3243 | #endif |
| 3244 | |
| 3245 | sb = gui_mswin_find_scrollbar(hwndCtl); |
| 3246 | if (sb == NULL) |
| 3247 | return 0; |
| 3248 | |
| 3249 | if (sb->wp != NULL) /* Left or right scrollbar */ |
| 3250 | { |
| 3251 | /* |
| 3252 | * Careful: need to get scrollbar info out of first (left) scrollbar |
| 3253 | * for window, but keep real scrollbar too because we must pass it to |
| 3254 | * gui_drag_scrollbar(). |
| 3255 | */ |
| 3256 | sb_info = &sb->wp->w_scrollbars[0]; |
| 3257 | } |
| 3258 | else /* Bottom scrollbar */ |
| 3259 | sb_info = sb; |
| 3260 | val = sb_info->value; |
| 3261 | |
| 3262 | switch (code) |
| 3263 | { |
| 3264 | case SB_THUMBTRACK: |
| 3265 | val = pos; |
| 3266 | dragging = TRUE; |
| 3267 | if (sb->scroll_shift > 0) |
| 3268 | val <<= sb->scroll_shift; |
| 3269 | break; |
| 3270 | case SB_LINEDOWN: |
| 3271 | val++; |
| 3272 | break; |
| 3273 | case SB_LINEUP: |
| 3274 | val--; |
| 3275 | break; |
| 3276 | case SB_PAGEDOWN: |
| 3277 | val += (sb_info->size > 2 ? sb_info->size - 2 : 1); |
| 3278 | break; |
| 3279 | case SB_PAGEUP: |
| 3280 | val -= (sb_info->size > 2 ? sb_info->size - 2 : 1); |
| 3281 | break; |
| 3282 | case SB_TOP: |
| 3283 | val = 0; |
| 3284 | break; |
| 3285 | case SB_BOTTOM: |
| 3286 | val = sb_info->max; |
| 3287 | break; |
| 3288 | case SB_ENDSCROLL: |
| 3289 | if (prev_code == SB_THUMBTRACK) |
| 3290 | { |
| 3291 | /* |
| 3292 | * "pos" only gives us 16-bit data. In case of large file, |
| 3293 | * use GetScrollPos() which returns 32-bit. Unfortunately it |
| 3294 | * is not valid while the scrollbar is being dragged. |
| 3295 | */ |
| 3296 | val = GetScrollPos(hwndCtl, SB_CTL); |
| 3297 | if (sb->scroll_shift > 0) |
| 3298 | val <<= sb->scroll_shift; |
| 3299 | } |
| 3300 | break; |
| 3301 | |
| 3302 | default: |
| 3303 | /* TRACE("Unknown scrollbar event %d\n", code); */ |
| 3304 | return 0; |
| 3305 | } |
| 3306 | prev_code = code; |
| 3307 | |
| 3308 | #ifdef WIN3264 |
| 3309 | si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val; |
| 3310 | SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE); |
| 3311 | #else |
| 3312 | nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val; |
| 3313 | SetScrollPos(hwndCtl, SB_CTL, nPos, TRUE); |
| 3314 | #endif |
| 3315 | |
| 3316 | /* |
| 3317 | * When moving a vertical scrollbar, move the other vertical scrollbar too. |
| 3318 | */ |
| 3319 | if (sb->wp != NULL) |
| 3320 | { |
| 3321 | scrollbar_T *sba = sb->wp->w_scrollbars; |
| 3322 | HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id; |
| 3323 | |
| 3324 | #ifdef WIN3264 |
| 3325 | SetScrollInfo(id, SB_CTL, &si, TRUE); |
| 3326 | #else |
| 3327 | SetScrollPos(id, SB_CTL, nPos, TRUE); |
| 3328 | #endif |
| 3329 | } |
| 3330 | |
| 3331 | /* Don't let us be interrupted here by another message. */ |
| 3332 | s_busy_processing = TRUE; |
| 3333 | |
| 3334 | /* When "allow_scrollbar" is FALSE still need to remember the new |
| 3335 | * position, but don't actually scroll by setting "dont_scroll". */ |
| 3336 | dont_scroll = !allow_scrollbar; |
| 3337 | |
| 3338 | gui_drag_scrollbar(sb, val, dragging); |
| 3339 | |
| 3340 | s_busy_processing = FALSE; |
| 3341 | dont_scroll = dont_scroll_save; |
| 3342 | |
| 3343 | return 0; |
| 3344 | } |
| 3345 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3346 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3347 | /* |
| 3348 | * Get command line arguments. |
| 3349 | * Use "prog" as the name of the program and "cmdline" as the arguments. |
| 3350 | * Copy the arguments to allocated memory. |
| 3351 | * Return the number of arguments (including program name). |
| 3352 | * Return pointers to the arguments in "argvp". |
| 3353 | * Return pointer to buffer in "tofree". |
| 3354 | * Returns zero when out of memory. |
| 3355 | */ |
| 3356 | int |
| 3357 | get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree) |
| 3358 | { |
| 3359 | int i; |
| 3360 | char *p; |
| 3361 | char *progp; |
| 3362 | char *pnew = NULL; |
| 3363 | char *newcmdline; |
| 3364 | int inquote; |
| 3365 | int argc; |
| 3366 | char **argv = NULL; |
| 3367 | int round; |
| 3368 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3369 | #ifdef FEAT_MBYTE |
| 3370 | /* Try using the Unicode version first, it takes care of conversion when |
| 3371 | * 'encoding' is changed. */ |
| 3372 | argc = get_cmd_argsW(&argv); |
| 3373 | if (argc != 0) |
| 3374 | goto done; |
| 3375 | #endif |
| 3376 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3377 | /* Handle the program name. Remove the ".exe" extension, and find the 1st |
| 3378 | * non-space. */ |
| 3379 | p = strrchr(prog, '.'); |
| 3380 | if (p != NULL) |
| 3381 | *p = NUL; |
| 3382 | for (progp = prog; *progp == ' '; ++progp) |
| 3383 | ; |
| 3384 | |
| 3385 | /* The command line is copied to allocated memory, so that we can change |
| 3386 | * it. Add the size of the string, the separating NUL and a terminating |
| 3387 | * NUL. */ |
| 3388 | newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2); |
| 3389 | if (newcmdline == NULL) |
| 3390 | return 0; |
| 3391 | |
| 3392 | /* |
| 3393 | * First round: count the number of arguments ("pnew" == NULL). |
| 3394 | * Second round: produce the arguments. |
| 3395 | */ |
| 3396 | for (round = 1; round <= 2; ++round) |
| 3397 | { |
| 3398 | /* First argument is the program name. */ |
| 3399 | if (pnew != NULL) |
| 3400 | { |
| 3401 | argv[0] = pnew; |
| 3402 | strcpy(pnew, progp); |
| 3403 | pnew += strlen(pnew); |
| 3404 | *pnew++ = NUL; |
| 3405 | } |
| 3406 | |
| 3407 | /* |
| 3408 | * Isolate each argument and put it in argv[]. |
| 3409 | */ |
| 3410 | p = cmdline; |
| 3411 | argc = 1; |
| 3412 | while (*p != NUL) |
| 3413 | { |
| 3414 | inquote = FALSE; |
| 3415 | if (pnew != NULL) |
| 3416 | argv[argc] = pnew; |
| 3417 | ++argc; |
| 3418 | while (*p != NUL && (inquote || (*p != ' ' && *p != '\t'))) |
| 3419 | { |
| 3420 | /* Backslashes are only special when followed by a double |
| 3421 | * quote. */ |
| 3422 | i = strspn(p, "\\"); |
| 3423 | if (p[i] == '"') |
| 3424 | { |
| 3425 | /* Halve the number of backslashes. */ |
| 3426 | if (i > 1 && pnew != NULL) |
| 3427 | { |
| 3428 | memset(pnew, '\\', i / 2); |
| 3429 | pnew += i / 2; |
| 3430 | } |
| 3431 | |
| 3432 | /* Even nr of backslashes toggles quoting, uneven copies |
| 3433 | * the double quote. */ |
| 3434 | if ((i & 1) == 0) |
| 3435 | inquote = !inquote; |
| 3436 | else if (pnew != NULL) |
| 3437 | *pnew++ = '"'; |
| 3438 | p += i + 1; |
| 3439 | } |
| 3440 | else if (i > 0) |
| 3441 | { |
| 3442 | /* Copy span of backslashes unmodified. */ |
| 3443 | if (pnew != NULL) |
| 3444 | { |
| 3445 | memset(pnew, '\\', i); |
| 3446 | pnew += i; |
| 3447 | } |
| 3448 | p += i; |
| 3449 | } |
| 3450 | else |
| 3451 | { |
| 3452 | if (pnew != NULL) |
| 3453 | *pnew++ = *p; |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 3454 | #ifdef FEAT_MBYTE |
| 3455 | /* Can't use mb_* functions, because 'encoding' is not |
| 3456 | * initialized yet here. */ |
| 3457 | if (IsDBCSLeadByte(*p)) |
| 3458 | { |
| 3459 | ++p; |
| 3460 | if (pnew != NULL) |
| 3461 | *pnew++ = *p; |
| 3462 | } |
| 3463 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3464 | ++p; |
| 3465 | } |
| 3466 | } |
| 3467 | |
| 3468 | if (pnew != NUL) |
| 3469 | *pnew++ = NUL; |
| 3470 | while (*p == ' ' || *p == '\t') |
| 3471 | ++p; /* advance until a non-space */ |
| 3472 | } |
| 3473 | |
| 3474 | if (round == 1) |
| 3475 | { |
| 3476 | argv = (char **)malloc((argc + 1) * sizeof(char *)); |
| 3477 | if (argv == NULL ) |
| 3478 | return 0; /* malloc error */ |
| 3479 | pnew = newcmdline; |
| 3480 | } |
| 3481 | } |
| 3482 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3483 | done: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3484 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3485 | argv[argc] = NULL; /* NULL-terminated list */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3486 | *argvp = argv; |
| 3487 | return argc; |
| 3488 | } |