7 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
15 #define COMPILE_MULTIMON_STUBS
25 #define IDM_SHOWLOG 0x0010
26 #define IDM_NEWSESS 0x0020
27 #define IDM_DUPSESS 0x0030
28 #define IDM_RECONF 0x0040
29 #define IDM_CLRSB 0x0050
30 #define IDM_RESET 0x0060
31 #define IDM_HELP 0x0140
32 #define IDM_ABOUT 0x0150
33 #define IDM_SAVEDSESS 0x0160
34 #define IDM_COPYALL 0x0170
35 #define IDM_FULLSCREEN 0x0180
37 #define IDM_SESSLGP 0x0250 /* log type printable */
38 #define IDM_SESSLGA 0x0260 /* log type all chars */
39 #define IDM_SESSLGE 0x0270 /* log end */
41 #define IDM_SPECIAL_MIN 0x0400
42 #define IDM_SPECIAL_MAX 0x0800
44 #define IDM_SAVED_MIN 0x1000
45 #define IDM_SAVED_MAX 0x2000
47 #define WM_IGNORE_CLIP (WM_XUSER + 2)
48 #define WM_FULLSCR_ON_MAX (WM_XUSER + 3)
49 #define WM_AGENT_CALLBACK (WM_XUSER + 4)
51 /* Needed for Chinese support and apparently not always defined. */
53 #define VK_PROCESSKEY 0xE5
56 /* Mouse wheel support. */
58 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
61 #define WHEEL_DELTA 120
64 static Mouse_Button translate_button(Mouse_Button button);
65 static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
66 static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
67 unsigned char *output);
68 static void cfgtopalette(void);
69 static void systopalette(void);
70 static void init_palette(void);
71 static void init_fonts(int, int);
72 static void another_font(int);
73 static void deinit_fonts(void);
74 static void set_input_locale(HKL);
76 static int is_full_screen(void);
77 static void make_full_screen(void);
78 static void clear_full_screen(void);
79 static void flip_full_screen(void);
81 /* Window layout information */
82 static void reset_window(int);
83 static int extra_width, extra_height;
84 static int font_width, font_height, font_dualwidth;
85 static int offset_width, offset_height;
86 static int was_zoomed = 0;
87 static int prev_rows, prev_cols;
89 static int pending_netevent = 0;
90 static WPARAM pend_netevent_wParam = 0;
91 static LPARAM pend_netevent_lParam = 0;
92 static void enact_pending_netevent(void);
93 static void flash_window(int mode);
94 static void sys_cursor_update(void);
95 static int is_shift_pressed(void);
96 static int get_fullscreen_rect(RECT * ss);
98 static time_t last_movement = 0;
100 static int caret_x = -1, caret_y = -1;
102 static int kbd_codepage;
105 static Backend *back;
106 static void *backhandle;
108 static struct unicode_data ucsdata;
109 static int session_closed;
111 static const struct telnet_special *specials;
112 static int specials_menu_position;
114 Config cfg; /* exported to windlg.c */
116 extern struct sesslist sesslist; /* imported from windlg.c */
118 struct agent_callback {
119 void (*callback)(void *, void *, int);
125 #define FONT_NORMAL 0
127 #define FONT_UNDERLINE 2
128 #define FONT_BOLDUND 3
129 #define FONT_WIDE 0x04
130 #define FONT_HIGH 0x08
131 #define FONT_NARROW 0x10
133 #define FONT_OEM 0x20
134 #define FONT_OEMBOLD 0x21
135 #define FONT_OEMUND 0x22
136 #define FONT_OEMBOLDUND 0x23
138 #define FONT_MAXNO 0x2F
140 static HFONT fonts[FONT_MAXNO];
141 static LOGFONT lfont;
142 static int fontflag[FONT_MAXNO];
144 BOLD_COLOURS, BOLD_SHADOW, BOLD_FONT
152 static COLORREF colours[NCOLOURS];
154 static LPLOGPALETTE logpal;
155 static RGBTRIPLE defpal[NCOLOURS];
159 static HBITMAP caretbm;
161 static int dbltime, lasttime, lastact;
162 static Mouse_Button lastbtn;
164 /* this allows xterm-style mouse handling. */
165 static int send_raw_mouse = 0;
166 static int wheel_accumulator = 0;
168 static char *window_name, *icon_name;
170 static int compose_state = 0;
172 static UINT wm_mousewheel = WM_MOUSEWHEEL;
174 /* Dummy routine, only required in plink. */
175 void ldisc_update(void *frontend, int echo, int edit)
179 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
183 int guess_width, guess_height;
186 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
190 InitCommonControls();
192 /* Ensure a Maximize setting in Explorer doesn't maximise the
198 char *str = dupprintf("%s Fatal Error", appname);
199 MessageBox(NULL, "Windows refuses to report a version",
200 str, MB_OK | MB_ICONEXCLAMATION);
206 * If we're running a version of Windows that doesn't support
207 * WM_MOUSEWHEEL, find out what message number we should be
210 if (osVersion.dwMajorVersion < 4 ||
211 (osVersion.dwMajorVersion == 4 &&
212 osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT))
213 wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
216 * See if we can find our Help file.
219 char b[2048], *p, *q, *r;
221 GetModuleFileName(NULL, b, sizeof(b) - 1);
223 p = strrchr(b, '\\');
224 if (p && p >= r) r = p+1;
226 if (q && q >= r) r = q+1;
227 strcpy(r, "putty.hlp");
228 if ( (fp = fopen(b, "r")) != NULL) {
229 help_path = dupstr(b);
233 strcpy(r, "putty.cnt");
234 if ( (fp = fopen(b, "r")) != NULL) {
235 help_has_contents = TRUE;
238 help_has_contents = FALSE;
242 * Process the command line.
248 default_protocol = be_default_protocol;
249 /* Find the appropriate default port. */
252 default_port = 0; /* illegal */
253 for (i = 0; backends[i].backend != NULL; i++)
254 if (backends[i].protocol == default_protocol) {
255 default_port = backends[i].backend->default_port;
259 cfg.logtype = LGTYP_NONE;
261 do_defaults(NULL, &cfg);
266 * Process a couple of command-line options which are more
267 * easily dealt with before the line is broken up into
268 * words. These are the soon-to-be-defunct @sessionname and
269 * the internal-use-only &sharedmemoryhandle, neither of
270 * which are combined with anything else.
272 while (*p && isspace(*p))
276 while (i > 1 && isspace(p[i - 1]))
279 do_defaults(p + 1, &cfg);
280 if (!*cfg.host && !do_config()) {
283 } else if (*p == '&') {
285 * An initial & means we've been given a command line
286 * containing the hex value of a HANDLE for a file
287 * mapping object, which we must then extract as a
292 if (sscanf(p + 1, "%p", &filemap) == 1 &&
293 (cp = MapViewOfFile(filemap, FILE_MAP_READ,
294 0, 0, sizeof(Config))) != NULL) {
297 CloseHandle(filemap);
298 } else if (!do_config()) {
303 * Otherwise, break up the command line and deal with
309 split_into_argv(cmdline, &argc, &argv, NULL);
311 for (i = 0; i < argc; i++) {
315 ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
318 cmdline_error("option \"%s\" requires an argument", p);
319 } else if (ret == 2) {
320 i++; /* skip next argument */
321 } else if (ret == 1) {
322 continue; /* nothing further needs doing */
323 } else if (!strcmp(p, "-cleanup")) {
325 * `putty -cleanup'. Remove all registry
326 * entries associated with PuTTY, and also find
327 * and delete the random seed file.
330 s1 = dupprintf("This procedure will remove ALL Registry\n"
331 "entries associated with %s, and will\n"
332 "also remove the random seed file.\n"
334 "THIS PROCESS WILL DESTROY YOUR SAVED\n"
335 "SESSIONS. Are you really sure you want\n"
336 "to continue?", appname);
337 s2 = dupprintf("%s Warning", appname);
338 if (MessageBox(NULL, s1, s2,
339 MB_YESNO | MB_ICONWARNING) == IDYES) {
345 } else if (*p != '-') {
349 * If we already have a host name, treat
350 * this argument as a port number. NB we
351 * have to treat this as a saved -P
352 * argument, so that it will be deferred
353 * until it's a good moment to run it.
355 int ret = cmdline_process_param("-P", p, 1, &cfg);
357 } else if (!strncmp(q, "telnet:", 7)) {
359 * If the hostname starts with "telnet:",
360 * set the protocol to Telnet and process
361 * the string as a Telnet URL.
366 if (q[0] == '/' && q[1] == '/')
368 cfg.protocol = PROT_TELNET;
370 while (*p && *p != ':' && *p != '/')
379 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
380 cfg.host[sizeof(cfg.host) - 1] = '\0';
384 * Otherwise, treat this argument as a host
387 while (*p && !isspace(*p))
391 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
392 cfg.host[sizeof(cfg.host) - 1] = '\0';
396 cmdline_error("unknown option \"%s\"", p);
401 cmdline_run_saved(&cfg);
403 if (!*cfg.host && !do_config()) {
408 * Trim leading whitespace off the hostname if it's there.
411 int space = strspn(cfg.host, " \t");
412 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
415 /* See if host is of the form user@host */
416 if (cfg.host[0] != '\0') {
417 char *atsign = strchr(cfg.host, '@');
418 /* Make sure we're not overflowing the user field */
420 if (atsign - cfg.host < sizeof cfg.username) {
421 strncpy(cfg.username, cfg.host, atsign - cfg.host);
422 cfg.username[atsign - cfg.host] = '\0';
424 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
429 * Trim a colon suffix off the hostname if it's there.
431 cfg.host[strcspn(cfg.host, ":")] = '\0';
434 * Remove any remaining whitespace from the hostname.
438 while (cfg.host[p2] != '\0') {
439 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
440 cfg.host[p1] = cfg.host[p2];
450 * Select protocol. This is farmed out into a table in a
451 * separate file to enable an ssh-free variant.
456 for (i = 0; backends[i].backend != NULL; i++)
457 if (backends[i].protocol == cfg.protocol) {
458 back = backends[i].backend;
462 char *str = dupprintf("%s Internal Error", appname);
463 MessageBox(NULL, "Unsupported protocol number found",
464 str, MB_OK | MB_ICONEXCLAMATION);
470 /* Check for invalid Port number (i.e. zero) */
472 char *str = dupprintf("%s Internal Error", appname);
473 MessageBox(NULL, "Invalid Port Number",
474 str, MB_OK | MB_ICONEXCLAMATION);
481 wndclass.lpfnWndProc = WndProc;
482 wndclass.cbClsExtra = 0;
483 wndclass.cbWndExtra = 0;
484 wndclass.hInstance = inst;
485 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
486 wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
487 wndclass.hbrBackground = NULL;
488 wndclass.lpszMenuName = NULL;
489 wndclass.lpszClassName = appname;
491 RegisterClass(&wndclass);
496 memset(&ucsdata, 0, sizeof(ucsdata));
498 term = term_init(&cfg, &ucsdata, NULL);
499 logctx = log_init(NULL, &cfg);
500 term_provide_logctx(term, logctx);
505 * Guess some defaults for the window size. This all gets
506 * updated later, so we don't really care too much. However, we
507 * do want the font width/height guesses to correspond to a
508 * large font rather than a small one...
515 term_size(term, cfg.height, cfg.width, cfg.savelines);
516 guess_width = extra_width + font_width * term->cols;
517 guess_height = extra_height + font_height * term->rows;
520 get_fullscreen_rect(&r);
521 if (guess_width > r.right - r.left)
522 guess_width = r.right - r.left;
523 if (guess_height > r.bottom - r.top)
524 guess_height = r.bottom - r.top;
528 int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
531 winmode &= ~(WS_VSCROLL);
532 if (cfg.resize_action == RESIZE_DISABLED)
533 winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
535 exwinmode |= WS_EX_TOPMOST;
537 exwinmode |= WS_EX_CLIENTEDGE;
538 hwnd = CreateWindowEx(exwinmode, appname, appname,
539 winmode, CW_USEDEFAULT, CW_USEDEFAULT,
540 guess_width, guess_height,
541 NULL, NULL, inst, NULL);
545 * Initialise the fonts, simultaneously correcting the guesses
546 * for font_{width,height}.
551 * Correct the guesses for extra_{width,height}.
555 GetWindowRect(hwnd, &wr);
556 GetClientRect(hwnd, &cr);
557 offset_width = offset_height = cfg.window_border;
558 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
559 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
563 * Resize the window, now we know what size we _really_ want it
566 guess_width = extra_width + font_width * term->cols;
567 guess_height = extra_height + font_height * term->rows;
568 SetWindowPos(hwnd, NULL, 0, 0, guess_width, guess_height,
569 SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
572 * Set up a caret bitmap, with no content.
576 int size = (font_width + 15) / 16 * 2 * font_height;
577 bits = snewn(size, char);
578 memset(bits, 0, size);
579 caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
582 CreateCaret(hwnd, caretbm, font_width, font_height);
585 * Initialise the scroll bar.
590 si.cbSize = sizeof(si);
591 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
593 si.nMax = term->rows - 1;
594 si.nPage = term->rows;
596 SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
600 * Start up the telnet connection.
604 char msg[1024], *title;
607 error = back->init(NULL, &backhandle, &cfg,
608 cfg.host, cfg.port, &realhost, cfg.tcp_nodelay);
609 back->provide_logctx(backhandle, logctx);
611 char *str = dupprintf("%s Error", appname);
612 sprintf(msg, "Unable to open connection to\n"
613 "%.800s\n" "%s", cfg.host, error);
614 MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
618 window_name = icon_name = NULL;
620 title = cfg.wintitle;
622 sprintf(msg, "%s - %s", realhost, appname);
626 set_title(NULL, title);
627 set_icon(NULL, title);
631 * Connect the terminal to the backend for resize purposes.
633 term_provide_resize_fn(term, back->size, backhandle);
636 * Set up a line discipline.
638 ldisc = ldisc_create(&cfg, term, back, backhandle, NULL);
640 session_closed = FALSE;
643 * Prepare the mouse handler.
645 lastact = MA_NOTHING;
646 lastbtn = MBT_NOTHING;
647 dbltime = GetDoubleClickTime();
650 * Set up the session-control options on the system menu.
653 HMENU m = GetSystemMenu(hwnd, FALSE);
658 AppendMenu(m, MF_SEPARATOR, 0, 0);
659 specials_menu_position = GetMenuItemCount(m);
660 AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
661 AppendMenu(m, MF_SEPARATOR, 0, 0);
662 AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
663 AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
665 get_sesslist(&sesslist, TRUE);
667 i < ((sesslist.nsessions < 256) ? sesslist.nsessions : 256);
669 AppendMenu(s, MF_ENABLED, IDM_SAVED_MIN + (16 * i),
670 sesslist.sessions[i]);
671 AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) s, "Sa&ved Sessions");
672 AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
673 AppendMenu(m, MF_SEPARATOR, 0, 0);
674 AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
675 AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
676 AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
677 AppendMenu(m, MF_SEPARATOR, 0, 0);
678 AppendMenu(m, (cfg.resize_action == RESIZE_DISABLED) ?
679 MF_GRAYED : MF_ENABLED, IDM_FULLSCREEN, "&Full Screen");
680 AppendMenu(m, MF_SEPARATOR, 0, 0);
682 AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
683 str = dupprintf("&About %s", appname);
684 AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
688 update_specials_menu(NULL);
691 * Set up the initial input locale.
693 set_input_locale(GetKeyboardLayout(0));
696 * Open the initial log file if there is one.
701 * Finally show the window!
703 ShowWindow(hwnd, show);
704 SetForegroundWindow(hwnd);
707 * Set the palette up.
713 term->has_focus = (GetForegroundWindow() == hwnd);
716 if (GetMessage(&msg, NULL, 0, 0) == 1) {
717 int timer_id = 0, long_timer = 0;
719 while (msg.message != WM_QUIT) {
720 /* Sometimes DispatchMessage calls routines that use their own
721 * GetMessage loop, setup this timer so we get some control back.
723 * Also call term_update() from the timer so that if the host
724 * is sending data flat out we still do redraws.
726 if (timer_id && long_timer) {
727 KillTimer(hwnd, timer_id);
728 long_timer = timer_id = 0;
731 timer_id = SetTimer(hwnd, 1, 20, NULL);
732 if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
733 DispatchMessage(&msg);
735 /* Make sure we blink everything that needs it. */
738 /* Send the paste buffer if there's anything to send */
741 /* If there's nothing new in the queue then we can do everything
742 * we've delayed, reading the socket, writing, and repainting
745 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
748 if (pending_netevent) {
749 enact_pending_netevent();
751 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
755 /* Okay there is now nothing to do so we make sure the screen is
756 * completely up to date then tell windows to call us in a little
760 KillTimer(hwnd, timer_id);
764 if (GetCapture() != hwnd ||
766 !(cfg.mouse_override && is_shift_pressed())))
771 flash_window(1); /* maintain */
773 /* The messages seem unreliable; especially if we're being tricky */
774 term->has_focus = (GetForegroundWindow() == hwnd);
777 /* Hmm, term_update didn't want to do an update too soon ... */
778 timer_id = SetTimer(hwnd, 1, 50, NULL);
779 else if (!term->has_focus)
780 timer_id = SetTimer(hwnd, 1, 500, NULL);
782 timer_id = SetTimer(hwnd, 1, 100, NULL);
785 /* There's no point rescanning everything in the message queue
786 * so we do an apparently unnecessary wait here
789 if (GetMessage(&msg, NULL, 0, 0) != 1)
794 cleanup_exit(msg.wParam); /* this doesn't return... */
795 return msg.wParam; /* ... but optimiser doesn't know */
801 void cleanup_exit(int code)
812 if (cfg.protocol == PROT_SSH) {
823 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
825 char *do_select(SOCKET skt, int startup)
830 events = (FD_CONNECT | FD_READ | FD_WRITE |
831 FD_OOB | FD_CLOSE | FD_ACCEPT);
836 return "do_select(): internal error (hwnd==NULL)";
837 if (p_WSAAsyncSelect(skt, hwnd, msg, events) == SOCKET_ERROR) {
838 switch (p_WSAGetLastError()) {
840 return "Network is down";
842 return "WSAAsyncSelect(): unknown error";
849 * Update the Special Commands submenu.
851 void update_specials_menu(void *frontend)
853 HMENU m = GetSystemMenu(hwnd, FALSE);
854 int menu_already_exists = (specials != NULL);
857 specials = back->get_specials(backhandle);
859 HMENU p = CreateMenu();
860 for (i = 0; specials[i].name; i++) {
861 assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
862 if (*specials[i].name)
863 AppendMenu(p, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
866 AppendMenu(p, MF_SEPARATOR, 0, 0);
868 if (menu_already_exists)
869 DeleteMenu(m, specials_menu_position, MF_BYPOSITION);
871 InsertMenu(m, specials_menu_position,
872 MF_BYPOSITION | MF_SEPARATOR, 0, 0);
873 InsertMenu(m, specials_menu_position,
874 MF_BYPOSITION | MF_POPUP | MF_ENABLED,
875 (UINT) p, "Special Command");
880 * set or clear the "raw mouse message" mode
882 void set_raw_mouse_mode(void *frontend, int activate)
884 activate = activate && !cfg.no_mouse_rep;
885 send_raw_mouse = activate;
886 SetCursor(LoadCursor(NULL, activate ? IDC_ARROW : IDC_IBEAM));
890 * Print a message box and close the connection.
892 void connection_fatal(void *frontend, char *fmt, ...)
895 char *stuff, morestuff[100];
898 stuff = dupvprintf(fmt, ap);
900 sprintf(morestuff, "%.70s Fatal Error", appname);
901 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
904 if (cfg.close_on_exit == FORCE_ON)
907 session_closed = TRUE;
908 sprintf(morestuff, "%.70s (inactive)", appname);
909 set_icon(NULL, morestuff);
910 set_title(NULL, morestuff);
915 * Report an error at the command-line parsing stage.
917 void cmdline_error(char *fmt, ...)
920 char *stuff, morestuff[100];
923 stuff = dupvprintf(fmt, ap);
925 sprintf(morestuff, "%.70s Command Line Error", appname);
926 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
932 * Actually do the job requested by a WM_NETEVENT
934 static void enact_pending_netevent(void)
936 static int reentering = 0;
937 extern int select_result(WPARAM, LPARAM);
941 return; /* don't unpend the pending */
943 pending_netevent = FALSE;
946 ret = select_result(pend_netevent_wParam, pend_netevent_lParam);
949 if (ret == 0 && !session_closed) {
950 /* Abnormal exits will already have set session_closed and taken
951 * appropriate action. */
952 if (cfg.close_on_exit == FORCE_ON ||
953 cfg.close_on_exit == AUTO) PostQuitMessage(0);
956 session_closed = TRUE;
957 sprintf(morestuff, "%.70s (inactive)", appname);
958 set_icon(NULL, morestuff);
959 set_title(NULL, morestuff);
960 MessageBox(hwnd, "Connection closed by remote host",
961 appname, MB_OK | MB_ICONINFORMATION);
967 * Copy the colour palette from the configuration data into defpal.
968 * This is non-trivial because the colour indices are different.
970 static void cfgtopalette(void)
973 static const int ww[] = {
974 6, 7, 8, 9, 10, 11, 12, 13,
975 14, 15, 16, 17, 18, 19, 20, 21,
976 0, 1, 2, 3, 4, 4, 5, 5
979 for (i = 0; i < 24; i++) {
981 defpal[i].rgbtRed = cfg.colours[w][0];
982 defpal[i].rgbtGreen = cfg.colours[w][1];
983 defpal[i].rgbtBlue = cfg.colours[w][2];
986 /* Override with system colours if appropriate */
987 if (cfg.system_colour)
992 * Override bit of defpal with colours from the system.
993 * (NB that this takes a copy the system colours at the time this is called,
994 * so subsequent colour scheme changes don't take effect. To fix that we'd
995 * probably want to be using GetSysColorBrush() and the like.)
997 static void systopalette(void)
1000 static const struct { int nIndex; int norm; int bold; } or[] =
1002 { COLOR_WINDOWTEXT, 16, 17 }, /* Default Foreground */
1003 { COLOR_WINDOW, 18, 19 }, /* Default Background */
1004 { COLOR_HIGHLIGHTTEXT, 20, 21 }, /* Cursor Text */
1005 { COLOR_HIGHLIGHT, 22, 23 }, /* Cursor Colour */
1008 for (i = 0; i < (sizeof(or)/sizeof(or[0])); i++) {
1009 COLORREF colour = GetSysColor(or[i].nIndex);
1010 defpal[or[i].norm].rgbtRed =
1011 defpal[or[i].bold].rgbtRed = GetRValue(colour);
1012 defpal[or[i].norm].rgbtGreen =
1013 defpal[or[i].bold].rgbtGreen = GetGValue(colour);
1014 defpal[or[i].norm].rgbtBlue =
1015 defpal[or[i].bold].rgbtBlue = GetBValue(colour);
1020 * Set up the colour palette.
1022 static void init_palette(void)
1025 HDC hdc = GetDC(hwnd);
1027 if (cfg.try_palette && GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
1029 * This is a genuine case where we must use smalloc
1030 * because the snew macros can't cope.
1032 logpal = smalloc(sizeof(*logpal)
1033 - sizeof(logpal->palPalEntry)
1034 + NCOLOURS * sizeof(PALETTEENTRY));
1035 logpal->palVersion = 0x300;
1036 logpal->palNumEntries = NCOLOURS;
1037 for (i = 0; i < NCOLOURS; i++) {
1038 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
1039 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
1040 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
1041 logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
1043 pal = CreatePalette(logpal);
1045 SelectPalette(hdc, pal, FALSE);
1046 RealizePalette(hdc);
1047 SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), FALSE);
1050 ReleaseDC(hwnd, hdc);
1053 for (i = 0; i < NCOLOURS; i++)
1054 colours[i] = PALETTERGB(defpal[i].rgbtRed,
1055 defpal[i].rgbtGreen,
1056 defpal[i].rgbtBlue);
1058 for (i = 0; i < NCOLOURS; i++)
1059 colours[i] = RGB(defpal[i].rgbtRed,
1060 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
1064 * Initialise all the fonts we will need initially. There may be as many as
1065 * three or as few as one. The other (poentially) twentyone fonts are done
1066 * if/when they are needed.
1070 * - check the font width and height, correcting our guesses if
1073 * - verify that the bold font is the same width as the ordinary
1074 * one, and engage shadow bolding if not.
1076 * - verify that the underlined font is the same width as the
1077 * ordinary one (manual underlining by means of line drawing can
1078 * be done in a pinch).
1080 static void init_fonts(int pick_width, int pick_height)
1087 int fw_dontcare, fw_bold;
1089 for (i = 0; i < FONT_MAXNO; i++)
1092 bold_mode = cfg.bold_colour ? BOLD_COLOURS : BOLD_FONT;
1093 und_mode = UND_FONT;
1095 if (cfg.font.isbold) {
1096 fw_dontcare = FW_BOLD;
1099 fw_dontcare = FW_DONTCARE;
1106 font_height = pick_height;
1108 font_height = cfg.font.height;
1109 if (font_height > 0) {
1111 -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1114 font_width = pick_width;
1116 #define f(i,c,w,u) \
1117 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1118 c, OUT_DEFAULT_PRECIS, \
1119 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
1120 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1122 f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);
1124 lfont.lfHeight = font_height;
1125 lfont.lfWidth = font_width;
1126 lfont.lfEscapement = 0;
1127 lfont.lfOrientation = 0;
1128 lfont.lfWeight = fw_dontcare;
1129 lfont.lfItalic = FALSE;
1130 lfont.lfUnderline = FALSE;
1131 lfont.lfStrikeOut = FALSE;
1132 lfont.lfCharSet = cfg.font.charset;
1133 lfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1134 lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1135 lfont.lfQuality = DEFAULT_QUALITY;
1136 lfont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1137 strncpy(lfont.lfFaceName, cfg.font.name, LF_FACESIZE);
1139 SelectObject(hdc, fonts[FONT_NORMAL]);
1140 GetTextMetrics(hdc, &tm);
1142 if (pick_width == 0 || pick_height == 0) {
1143 font_height = tm.tmHeight;
1144 font_width = tm.tmAveCharWidth;
1146 font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
1148 #ifdef RDB_DEBUG_PATCH
1149 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1150 tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
1155 DWORD cset = tm.tmCharSet;
1156 memset(&info, 0xFF, sizeof(info));
1158 /* !!! Yes the next line is right */
1159 if (cset == OEM_CHARSET)
1160 ucsdata.font_codepage = GetOEMCP();
1162 if (TranslateCharsetInfo ((DWORD *) cset, &info, TCI_SRCCHARSET))
1163 ucsdata.font_codepage = info.ciACP;
1165 ucsdata.font_codepage = -1;
1167 GetCPInfo(ucsdata.font_codepage, &cpinfo);
1168 ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
1171 f(FONT_UNDERLINE, cfg.font.charset, fw_dontcare, TRUE);
1174 * Some fonts, e.g. 9-pt Courier, draw their underlines
1175 * outside their character cell. We successfully prevent
1176 * screen corruption by clipping the text output, but then
1177 * we lose the underline completely. Here we try to work
1178 * out whether this is such a font, and if it is, we set a
1179 * flag that causes underlines to be drawn by hand.
1181 * Having tried other more sophisticated approaches (such
1182 * as examining the TEXTMETRIC structure or requesting the
1183 * height of a string), I think we'll do this the brute
1184 * force way: we create a small bitmap, draw an underlined
1185 * space on it, and test to see whether any pixels are
1186 * foreground-coloured. (Since we expect the underline to
1187 * go all the way across the character cell, we only search
1188 * down a single column of the bitmap, half way across.)
1192 HBITMAP und_bm, und_oldbm;
1196 und_dc = CreateCompatibleDC(hdc);
1197 und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
1198 und_oldbm = SelectObject(und_dc, und_bm);
1199 SelectObject(und_dc, fonts[FONT_UNDERLINE]);
1200 SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
1201 SetTextColor(und_dc, RGB(255, 255, 255));
1202 SetBkColor(und_dc, RGB(0, 0, 0));
1203 SetBkMode(und_dc, OPAQUE);
1204 ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
1206 for (i = 0; i < font_height; i++) {
1207 c = GetPixel(und_dc, font_width / 2, i);
1208 if (c != RGB(0, 0, 0))
1211 SelectObject(und_dc, und_oldbm);
1212 DeleteObject(und_bm);
1215 und_mode = UND_LINE;
1216 DeleteObject(fonts[FONT_UNDERLINE]);
1217 fonts[FONT_UNDERLINE] = 0;
1221 if (bold_mode == BOLD_FONT) {
1222 f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);
1226 descent = tm.tmAscent + 1;
1227 if (descent >= font_height)
1228 descent = font_height - 1;
1230 for (i = 0; i < 3; i++) {
1232 if (SelectObject(hdc, fonts[i]) && GetTextMetrics(hdc, &tm))
1233 fontsize[i] = tm.tmAveCharWidth + 256 * tm.tmHeight;
1240 ReleaseDC(hwnd, hdc);
1242 if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
1243 und_mode = UND_LINE;
1244 DeleteObject(fonts[FONT_UNDERLINE]);
1245 fonts[FONT_UNDERLINE] = 0;
1248 if (bold_mode == BOLD_FONT &&
1249 fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
1250 bold_mode = BOLD_SHADOW;
1251 DeleteObject(fonts[FONT_BOLD]);
1252 fonts[FONT_BOLD] = 0;
1254 fontflag[0] = fontflag[1] = fontflag[2] = 1;
1256 init_ucs(&cfg, &ucsdata);
1259 static void another_font(int fontno)
1262 int fw_dontcare, fw_bold;
1266 if (fontno < 0 || fontno >= FONT_MAXNO || fontflag[fontno])
1269 basefont = (fontno & ~(FONT_BOLDUND));
1270 if (basefont != fontno && !fontflag[basefont])
1271 another_font(basefont);
1273 if (cfg.font.isbold) {
1274 fw_dontcare = FW_BOLD;
1277 fw_dontcare = FW_DONTCARE;
1281 c = cfg.font.charset;
1287 if (fontno & FONT_WIDE)
1289 if (fontno & FONT_NARROW)
1291 if (fontno & FONT_OEM)
1293 if (fontno & FONT_BOLD)
1295 if (fontno & FONT_UNDERLINE)
1299 CreateFont(font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
1300 FALSE, u, FALSE, c, OUT_DEFAULT_PRECIS,
1301 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1302 FIXED_PITCH | FF_DONTCARE, s);
1304 fontflag[fontno] = 1;
1307 static void deinit_fonts(void)
1310 for (i = 0; i < FONT_MAXNO; i++) {
1312 DeleteObject(fonts[i]);
1318 void request_resize(void *frontend, int w, int h)
1322 /* If the window is maximized supress resizing attempts */
1323 if (IsZoomed(hwnd)) {
1324 if (cfg.resize_action == RESIZE_TERM)
1328 if (cfg.resize_action == RESIZE_DISABLED) return;
1329 if (h == term->rows && w == term->cols) return;
1331 /* Sanity checks ... */
1333 static int first_time = 1;
1336 switch (first_time) {
1338 /* Get the size of the screen */
1339 if (get_fullscreen_rect(&ss))
1340 /* first_time = 0 */ ;
1346 /* Make sure the values are sane */
1347 width = (ss.right - ss.left - extra_width) / 4;
1348 height = (ss.bottom - ss.top - extra_height) / 6;
1350 if (w > width || h > height)
1359 term_size(term, h, w, cfg.savelines);
1361 if (cfg.resize_action != RESIZE_FONT && !IsZoomed(hwnd)) {
1362 width = extra_width + font_width * w;
1363 height = extra_height + font_height * h;
1365 SetWindowPos(hwnd, NULL, 0, 0, width, height,
1366 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1367 SWP_NOMOVE | SWP_NOZORDER);
1371 InvalidateRect(hwnd, NULL, TRUE);
1374 static void reset_window(int reinit) {
1376 * This function decides how to resize or redraw when the
1377 * user changes something.
1379 * This function doesn't like to change the terminal size but if the
1380 * font size is locked that may be it's only soluion.
1382 int win_width, win_height;
1385 #ifdef RDB_DEBUG_PATCH
1386 debug((27, "reset_window()"));
1389 /* Current window sizes ... */
1390 GetWindowRect(hwnd, &wr);
1391 GetClientRect(hwnd, &cr);
1393 win_width = cr.right - cr.left;
1394 win_height = cr.bottom - cr.top;
1396 if (cfg.resize_action == RESIZE_DISABLED) reinit = 2;
1398 /* Are we being forced to reload the fonts ? */
1400 #ifdef RDB_DEBUG_PATCH
1401 debug((27, "reset_window() -- Forced deinit"));
1407 /* Oh, looks like we're minimised */
1408 if (win_width == 0 || win_height == 0)
1411 /* Is the window out of position ? */
1413 (offset_width != (win_width-font_width*term->cols)/2 ||
1414 offset_height != (win_height-font_height*term->rows)/2) ){
1415 offset_width = (win_width-font_width*term->cols)/2;
1416 offset_height = (win_height-font_height*term->rows)/2;
1417 InvalidateRect(hwnd, NULL, TRUE);
1418 #ifdef RDB_DEBUG_PATCH
1419 debug((27, "reset_window() -> Reposition terminal"));
1423 if (IsZoomed(hwnd)) {
1424 /* We're fullscreen, this means we must not change the size of
1425 * the window so it's the font size or the terminal itself.
1428 extra_width = wr.right - wr.left - cr.right + cr.left;
1429 extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
1431 if (cfg.resize_action != RESIZE_TERM) {
1432 if ( font_width != win_width/term->cols ||
1433 font_height != win_height/term->rows) {
1435 init_fonts(win_width/term->cols, win_height/term->rows);
1436 offset_width = (win_width-font_width*term->cols)/2;
1437 offset_height = (win_height-font_height*term->rows)/2;
1438 InvalidateRect(hwnd, NULL, TRUE);
1439 #ifdef RDB_DEBUG_PATCH
1440 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1441 font_width, font_height));
1445 if ( font_width != win_width/term->cols ||
1446 font_height != win_height/term->rows) {
1447 /* Our only choice at this point is to change the
1448 * size of the terminal; Oh well.
1450 term_size(term, win_height/font_height, win_width/font_width,
1452 offset_width = (win_width-font_width*term->cols)/2;
1453 offset_height = (win_height-font_height*term->rows)/2;
1454 InvalidateRect(hwnd, NULL, TRUE);
1455 #ifdef RDB_DEBUG_PATCH
1456 debug((27, "reset_window() -> Zoomed term_size"));
1463 /* Hmm, a force re-init means we should ignore the current window
1464 * so we resize to the default font size.
1467 #ifdef RDB_DEBUG_PATCH
1468 debug((27, "reset_window() -> Forced re-init"));
1471 offset_width = offset_height = cfg.window_border;
1472 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1473 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1475 if (win_width != font_width*term->cols + offset_width*2 ||
1476 win_height != font_height*term->rows + offset_height*2) {
1478 /* If this is too large windows will resize it to the maximum
1479 * allowed window size, we will then be back in here and resize
1480 * the font or terminal to fit.
1482 SetWindowPos(hwnd, NULL, 0, 0,
1483 font_width*term->cols + extra_width,
1484 font_height*term->rows + extra_height,
1485 SWP_NOMOVE | SWP_NOZORDER);
1488 InvalidateRect(hwnd, NULL, TRUE);
1492 /* Okay the user doesn't want us to change the font so we try the
1493 * window. But that may be too big for the screen which forces us
1494 * to change the terminal.
1496 if ((cfg.resize_action == RESIZE_TERM && reinit<=0) ||
1497 (cfg.resize_action == RESIZE_EITHER && reinit<0) ||
1499 offset_width = offset_height = cfg.window_border;
1500 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1501 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1503 if (win_width != font_width*term->cols + offset_width*2 ||
1504 win_height != font_height*term->rows + offset_height*2) {
1509 get_fullscreen_rect(&ss);
1511 width = (ss.right - ss.left - extra_width) / font_width;
1512 height = (ss.bottom - ss.top - extra_height) / font_height;
1515 if ( term->rows > height || term->cols > width ) {
1516 if (cfg.resize_action == RESIZE_EITHER) {
1517 /* Make the font the biggest we can */
1518 if (term->cols > width)
1519 font_width = (ss.right - ss.left - extra_width)
1521 if (term->rows > height)
1522 font_height = (ss.bottom - ss.top - extra_height)
1526 init_fonts(font_width, font_height);
1528 width = (ss.right - ss.left - extra_width) / font_width;
1529 height = (ss.bottom - ss.top - extra_height) / font_height;
1531 if ( height > term->rows ) height = term->rows;
1532 if ( width > term->cols ) width = term->cols;
1533 term_size(term, height, width, cfg.savelines);
1534 #ifdef RDB_DEBUG_PATCH
1535 debug((27, "reset_window() -> term resize to (%d,%d)",
1541 SetWindowPos(hwnd, NULL, 0, 0,
1542 font_width*term->cols + extra_width,
1543 font_height*term->rows + extra_height,
1544 SWP_NOMOVE | SWP_NOZORDER);
1546 InvalidateRect(hwnd, NULL, TRUE);
1547 #ifdef RDB_DEBUG_PATCH
1548 debug((27, "reset_window() -> window resize to (%d,%d)",
1549 font_width*term->cols + extra_width,
1550 font_height*term->rows + extra_height));
1556 /* We're allowed to or must change the font but do we want to ? */
1558 if (font_width != (win_width-cfg.window_border*2)/term->cols ||
1559 font_height != (win_height-cfg.window_border*2)/term->rows) {
1562 init_fonts((win_width-cfg.window_border*2)/term->cols,
1563 (win_height-cfg.window_border*2)/term->rows);
1564 offset_width = (win_width-font_width*term->cols)/2;
1565 offset_height = (win_height-font_height*term->rows)/2;
1567 extra_width = wr.right - wr.left - cr.right + cr.left +offset_width*2;
1568 extra_height = wr.bottom - wr.top - cr.bottom + cr.top+offset_height*2;
1570 InvalidateRect(hwnd, NULL, TRUE);
1571 #ifdef RDB_DEBUG_PATCH
1572 debug((25, "reset_window() -> font resize to (%d,%d)",
1573 font_width, font_height));
1578 static void set_input_locale(HKL kl)
1582 GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
1583 lbuf, sizeof(lbuf));
1585 kbd_codepage = atoi(lbuf);
1588 static void click(Mouse_Button b, int x, int y, int shift, int ctrl, int alt)
1590 int thistime = GetMessageTime();
1592 if (send_raw_mouse && !(cfg.mouse_override && shift)) {
1593 lastbtn = MBT_NOTHING;
1594 term_mouse(term, b, translate_button(b), MA_CLICK,
1595 x, y, shift, ctrl, alt);
1599 if (lastbtn == b && thistime - lasttime < dbltime) {
1600 lastact = (lastact == MA_CLICK ? MA_2CLK :
1601 lastact == MA_2CLK ? MA_3CLK :
1602 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
1607 if (lastact != MA_NOTHING)
1608 term_mouse(term, b, translate_button(b), lastact,
1609 x, y, shift, ctrl, alt);
1610 lasttime = thistime;
1614 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1615 * into a cooked one (SELECT, EXTEND, PASTE).
1617 static Mouse_Button translate_button(Mouse_Button button)
1619 if (button == MBT_LEFT)
1621 if (button == MBT_MIDDLE)
1622 return cfg.mouse_is_xterm ? MBT_PASTE : MBT_EXTEND;
1623 if (button == MBT_RIGHT)
1624 return cfg.mouse_is_xterm ? MBT_EXTEND : MBT_PASTE;
1625 return 0; /* shouldn't happen */
1628 static void show_mouseptr(int show)
1630 static int cursor_visible = 1;
1631 if (!cfg.hide_mouseptr) /* override if this feature disabled */
1633 if (cursor_visible && !show)
1635 else if (!cursor_visible && show)
1637 cursor_visible = show;
1640 static int is_alt_pressed(void)
1643 int r = GetKeyboardState(keystate);
1646 if (keystate[VK_MENU] & 0x80)
1648 if (keystate[VK_RMENU] & 0x80)
1653 static int is_shift_pressed(void)
1656 int r = GetKeyboardState(keystate);
1659 if (keystate[VK_SHIFT] & 0x80)
1664 static int resizing;
1666 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1667 WPARAM wParam, LPARAM lParam)
1670 static int ignore_clip = FALSE;
1671 static int need_backend_resize = FALSE;
1672 static int fullscr_on_max = FALSE;
1676 if (pending_netevent)
1677 enact_pending_netevent();
1678 if (GetCapture() != hwnd ||
1679 (send_raw_mouse && !(cfg.mouse_override && is_shift_pressed())))
1685 if (cfg.ping_interval > 0) {
1688 if (now - last_movement > cfg.ping_interval) {
1689 back->special(backhandle, TS_PING);
1690 last_movement = now;
1693 net_pending_errors();
1701 str = dupprintf("%s Exit Confirmation", appname);
1702 if (!cfg.warn_on_close || session_closed ||
1704 "Are you sure you want to close this session?",
1705 str, MB_ICONWARNING | MB_OKCANCEL) == IDOK)
1706 DestroyWindow(hwnd);
1715 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1727 PROCESS_INFORMATION pi;
1728 HANDLE filemap = NULL;
1730 if (wParam == IDM_DUPSESS) {
1732 * Allocate a file-mapping memory chunk for the
1735 SECURITY_ATTRIBUTES sa;
1738 sa.nLength = sizeof(sa);
1739 sa.lpSecurityDescriptor = NULL;
1740 sa.bInheritHandle = TRUE;
1741 filemap = CreateFileMapping((HANDLE) 0xFFFFFFFF,
1744 0, sizeof(Config), NULL);
1746 p = (Config *) MapViewOfFile(filemap,
1748 0, 0, sizeof(Config));
1750 *p = cfg; /* structure copy */
1754 sprintf(c, "putty &%p", filemap);
1756 } else if (wParam == IDM_SAVEDSESS) {
1757 if ((lParam - IDM_SAVED_MIN) / 16 < sesslist.nsessions) {
1759 sesslist.sessions[(lParam - IDM_SAVED_MIN) / 16];
1760 cl = snewn(16 + strlen(session), char);
1761 /* 8, but play safe */
1764 /* not a very important failure mode */
1766 sprintf(cl, "putty @%s", session);
1774 GetModuleFileName(NULL, b, sizeof(b) - 1);
1776 si.lpReserved = NULL;
1777 si.lpDesktop = NULL;
1781 si.lpReserved2 = NULL;
1782 CreateProcess(b, cl, NULL, NULL, TRUE,
1783 NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
1786 CloseHandle(filemap);
1796 GetWindowText(hwnd, cfg.wintitle, sizeof(cfg.wintitle));
1799 if (!do_reconfig(hwnd))
1803 /* Disable full-screen if resizing forbidden */
1804 HMENU m = GetSystemMenu (hwnd, FALSE);
1805 EnableMenuItem(m, IDM_FULLSCREEN, MF_BYCOMMAND |
1806 (cfg.resize_action == RESIZE_DISABLED)
1807 ? MF_GRAYED : MF_ENABLED);
1808 /* Gracefully unzoom if necessary */
1809 if (IsZoomed(hwnd) &&
1810 (cfg.resize_action == RESIZE_DISABLED)) {
1811 ShowWindow(hwnd, SW_RESTORE);
1815 /* Pass new config data to the logging module */
1816 log_reconfig(logctx, &cfg);
1820 * Flush the line discipline's edit buffer in the
1821 * case where local editing has just been disabled.
1823 ldisc_send(ldisc, NULL, 0, 0);
1831 /* Pass new config data to the terminal */
1832 term_reconfig(term, &cfg);
1834 /* Pass new config data to the back end */
1835 back->reconfig(backhandle, &cfg);
1837 /* Screen size changed ? */
1838 if (cfg.height != prev_cfg.height ||
1839 cfg.width != prev_cfg.width ||
1840 cfg.savelines != prev_cfg.savelines ||
1841 cfg.resize_action == RESIZE_FONT ||
1842 (cfg.resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
1843 cfg.resize_action == RESIZE_DISABLED)
1844 term_size(term, cfg.height, cfg.width, cfg.savelines);
1846 /* Enable or disable the scroll bar, etc */
1848 LONG nflg, flag = GetWindowLong(hwnd, GWL_STYLE);
1849 LONG nexflag, exflag =
1850 GetWindowLong(hwnd, GWL_EXSTYLE);
1853 if (cfg.alwaysontop != prev_cfg.alwaysontop) {
1854 if (cfg.alwaysontop) {
1855 nexflag |= WS_EX_TOPMOST;
1856 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1857 SWP_NOMOVE | SWP_NOSIZE);
1859 nexflag &= ~(WS_EX_TOPMOST);
1860 SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
1861 SWP_NOMOVE | SWP_NOSIZE);
1864 if (cfg.sunken_edge)
1865 nexflag |= WS_EX_CLIENTEDGE;
1867 nexflag &= ~(WS_EX_CLIENTEDGE);
1870 if (is_full_screen() ?
1871 cfg.scrollbar_in_fullscreen : cfg.scrollbar)
1874 nflg &= ~WS_VSCROLL;
1876 if (cfg.resize_action == RESIZE_DISABLED ||
1878 nflg &= ~WS_THICKFRAME;
1880 nflg |= WS_THICKFRAME;
1882 if (cfg.resize_action == RESIZE_DISABLED)
1883 nflg &= ~WS_MAXIMIZEBOX;
1885 nflg |= WS_MAXIMIZEBOX;
1887 if (nflg != flag || nexflag != exflag) {
1889 SetWindowLong(hwnd, GWL_STYLE, nflg);
1890 if (nexflag != exflag)
1891 SetWindowLong(hwnd, GWL_EXSTYLE, nexflag);
1893 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
1894 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1895 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1903 if (cfg.resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
1908 set_title(NULL, cfg.wintitle);
1909 if (IsIconic(hwnd)) {
1911 cfg.win_name_always ? window_name :
1915 if (strcmp(cfg.font.name, prev_cfg.font.name) != 0 ||
1916 strcmp(cfg.line_codepage, prev_cfg.line_codepage) != 0 ||
1917 cfg.font.isbold != prev_cfg.font.isbold ||
1918 cfg.font.height != prev_cfg.font.height ||
1919 cfg.font.charset != prev_cfg.font.charset ||
1920 cfg.vtmode != prev_cfg.vtmode ||
1921 cfg.bold_colour != prev_cfg.bold_colour ||
1922 cfg.resize_action == RESIZE_DISABLED ||
1923 cfg.resize_action == RESIZE_EITHER ||
1924 (cfg.resize_action != prev_cfg.resize_action))
1927 InvalidateRect(hwnd, NULL, TRUE);
1928 reset_window(init_lvl);
1929 net_pending_errors();
1940 ldisc_send(ldisc, NULL, 0, 0);
1946 WinHelp(hwnd, help_path,
1947 help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1951 * We get this if the System menu has been activated
1958 * We get this if the System menu has been activated
1959 * using the keyboard. This might happen from within
1960 * TranslateKey, in which case it really wants to be
1961 * followed by a `space' character to actually _bring
1962 * the menu up_ rather than just sitting there in
1963 * `ready to appear' state.
1965 show_mouseptr(1); /* make sure pointer is visible */
1967 PostMessage(hwnd, WM_CHAR, ' ', 0);
1969 case IDM_FULLSCREEN:
1973 if (wParam >= IDM_SAVED_MIN && wParam <= IDM_SAVED_MAX) {
1974 SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
1976 if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
1977 int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
1980 * Ensure we haven't been sent a bogus SYSCOMMAND
1981 * which would cause us to reference invalid memory
1982 * and crash. Perhaps I'm just too paranoid here.
1984 for (j = 0; j < i; j++)
1985 if (!specials || !specials[j].name)
1988 back->special(backhandle, specials[i].code);
1989 net_pending_errors();
1995 #define X_POS(l) ((int)(short)LOWORD(l))
1996 #define Y_POS(l) ((int)(short)HIWORD(l))
1998 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
1999 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2000 case WM_LBUTTONDOWN:
2001 case WM_MBUTTONDOWN:
2002 case WM_RBUTTONDOWN:
2010 case WM_LBUTTONDOWN:
2014 case WM_MBUTTONDOWN:
2015 button = MBT_MIDDLE;
2018 case WM_RBUTTONDOWN:
2027 button = MBT_MIDDLE;
2035 button = press = 0; /* shouldn't happen */
2039 * Special case: in full-screen mode, if the left
2040 * button is clicked in the very top left corner of the
2041 * window, we put up the System menu instead of doing
2044 if (is_full_screen() && press && button == MBT_LEFT &&
2045 X_POS(lParam) == 0 && Y_POS(lParam) == 0) {
2046 SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU, 0);
2051 TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
2052 wParam & MK_SHIFT, wParam & MK_CONTROL,
2056 term_mouse(term, button, translate_button(button), MA_RELEASE,
2057 TO_CHR_X(X_POS(lParam)),
2058 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
2059 wParam & MK_CONTROL, is_alt_pressed());
2067 * Add the mouse position and message time to the random
2070 noise_ultralight(lParam);
2072 if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
2073 GetCapture() == hwnd) {
2075 if (wParam & MK_LBUTTON)
2077 else if (wParam & MK_MBUTTON)
2081 term_mouse(term, b, translate_button(b), MA_DRAG,
2082 TO_CHR_X(X_POS(lParam)),
2083 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
2084 wParam & MK_CONTROL, is_alt_pressed());
2087 case WM_NCMOUSEMOVE:
2089 noise_ultralight(lParam);
2091 case WM_IGNORE_CLIP:
2092 ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */
2094 case WM_DESTROYCLIPBOARD:
2096 term_deselect(term);
2097 ignore_clip = FALSE;
2103 hdc = BeginPaint(hwnd, &p);
2105 SelectPalette(hdc, pal, TRUE);
2106 RealizePalette(hdc);
2108 term_paint(term, hdc,
2109 (p.rcPaint.left-offset_width)/font_width,
2110 (p.rcPaint.top-offset_height)/font_height,
2111 (p.rcPaint.right-offset_width-1)/font_width,
2112 (p.rcPaint.bottom-offset_height-1)/font_height,
2116 p.rcPaint.left < offset_width ||
2117 p.rcPaint.top < offset_height ||
2118 p.rcPaint.right >= offset_width + font_width*term->cols ||
2119 p.rcPaint.bottom>= offset_height + font_height*term->rows)
2121 HBRUSH fillcolour, oldbrush;
2123 fillcolour = CreateSolidBrush (
2124 colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
2125 oldbrush = SelectObject(hdc, fillcolour);
2126 edge = CreatePen(PS_SOLID, 0,
2127 colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
2128 oldpen = SelectObject(hdc, edge);
2131 * Jordan Russell reports that this apparently
2132 * ineffectual IntersectClipRect() call masks a
2133 * Windows NT/2K bug causing strange display
2134 * problems when the PuTTY window is taller than
2135 * the primary monitor. It seems harmless enough...
2137 IntersectClipRect(hdc,
2138 p.rcPaint.left, p.rcPaint.top,
2139 p.rcPaint.right, p.rcPaint.bottom);
2141 ExcludeClipRect(hdc,
2142 offset_width, offset_height,
2143 offset_width+font_width*term->cols,
2144 offset_height+font_height*term->rows);
2146 Rectangle(hdc, p.rcPaint.left, p.rcPaint.top,
2147 p.rcPaint.right, p.rcPaint.bottom);
2149 // SelectClipRgn(hdc, NULL);
2151 SelectObject(hdc, oldbrush);
2152 DeleteObject(fillcolour);
2153 SelectObject(hdc, oldpen);
2156 SelectObject(hdc, GetStockObject(SYSTEM_FONT));
2157 SelectObject(hdc, GetStockObject(WHITE_PEN));
2163 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2164 * but the only one that's likely to try to overload us is FD_READ.
2165 * This means buffering just one is fine.
2167 if (pending_netevent)
2168 enact_pending_netevent();
2170 pending_netevent = TRUE;
2171 pend_netevent_wParam = wParam;
2172 pend_netevent_lParam = lParam;
2173 if (WSAGETSELECTEVENT(lParam) != FD_READ)
2174 enact_pending_netevent();
2176 time(&last_movement);
2179 term->has_focus = TRUE;
2180 CreateCaret(hwnd, caretbm, font_width, font_height);
2182 flash_window(0); /* stop */
2189 term->has_focus = FALSE;
2191 caret_x = caret_y = -1; /* ensure caret is replaced next time */
2195 case WM_ENTERSIZEMOVE:
2196 #ifdef RDB_DEBUG_PATCH
2197 debug((27, "WM_ENTERSIZEMOVE"));
2201 need_backend_resize = FALSE;
2203 case WM_EXITSIZEMOVE:
2206 #ifdef RDB_DEBUG_PATCH
2207 debug((27, "WM_EXITSIZEMOVE"));
2209 if (need_backend_resize) {
2210 term_size(term, cfg.height, cfg.width, cfg.savelines);
2211 InvalidateRect(hwnd, NULL, TRUE);
2216 * This does two jobs:
2217 * 1) Keep the sizetip uptodate
2218 * 2) Make sure the window size is _stepped_ in units of the font size.
2220 if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
2221 int width, height, w, h, ew, eh;
2222 LPRECT r = (LPRECT) lParam;
2224 if ( !need_backend_resize && cfg.resize_action == RESIZE_EITHER &&
2225 (cfg.height != term->rows || cfg.width != term->cols )) {
2227 * Great! It seems that both the terminal size and the
2228 * font size have been changed and the user is now dragging.
2230 * It will now be difficult to get back to the configured
2233 * This would be easier but it seems to be too confusing.
2235 term_size(term, cfg.height, cfg.width, cfg.savelines);
2238 cfg.height=term->rows; cfg.width=term->cols;
2240 InvalidateRect(hwnd, NULL, TRUE);
2241 need_backend_resize = TRUE;
2244 width = r->right - r->left - extra_width;
2245 height = r->bottom - r->top - extra_height;
2246 w = (width + font_width / 2) / font_width;
2249 h = (height + font_height / 2) / font_height;
2252 UpdateSizeTip(hwnd, w, h);
2253 ew = width - w * font_width;
2254 eh = height - h * font_height;
2256 if (wParam == WMSZ_LEFT ||
2257 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
2263 if (wParam == WMSZ_TOP ||
2264 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
2274 int width, height, w, h, rv = 0;
2275 int ex_width = extra_width + (cfg.window_border - offset_width) * 2;
2276 int ex_height = extra_height + (cfg.window_border - offset_height) * 2;
2277 LPRECT r = (LPRECT) lParam;
2279 width = r->right - r->left - ex_width;
2280 height = r->bottom - r->top - ex_height;
2282 w = (width + term->cols/2)/term->cols;
2283 h = (height + term->rows/2)/term->rows;
2284 if ( r->right != r->left + w*term->cols + ex_width)
2287 if (wParam == WMSZ_LEFT ||
2288 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
2289 r->left = r->right - w*term->cols - ex_width;
2291 r->right = r->left + w*term->cols + ex_width;
2293 if (r->bottom != r->top + h*term->rows + ex_height)
2296 if (wParam == WMSZ_TOP ||
2297 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
2298 r->top = r->bottom - h*term->rows - ex_height;
2300 r->bottom = r->top + h*term->rows + ex_height;
2304 /* break; (never reached) */
2305 case WM_FULLSCR_ON_MAX:
2306 fullscr_on_max = TRUE;
2309 sys_cursor_update();
2312 #ifdef RDB_DEBUG_PATCH
2313 debug((27, "WM_SIZE %s (%d,%d)",
2314 (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
2315 (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
2316 (wParam == SIZE_RESTORED && resizing) ? "to":
2317 (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
2319 LOWORD(lParam), HIWORD(lParam)));
2321 if (wParam == SIZE_MINIMIZED)
2323 cfg.win_name_always ? window_name : icon_name);
2324 if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
2325 SetWindowText(hwnd, window_name);
2326 if (wParam == SIZE_RESTORED)
2327 clear_full_screen();
2328 if (wParam == SIZE_MAXIMIZED && fullscr_on_max) {
2329 fullscr_on_max = FALSE;
2333 if (cfg.resize_action == RESIZE_DISABLED) {
2334 /* A resize, well it better be a minimize. */
2338 int width, height, w, h;
2340 width = LOWORD(lParam);
2341 height = HIWORD(lParam);
2344 if (wParam == SIZE_MAXIMIZED && !was_zoomed) {
2346 prev_rows = term->rows;
2347 prev_cols = term->cols;
2348 if (cfg.resize_action == RESIZE_TERM) {
2349 w = width / font_width;
2351 h = height / font_height;
2354 term_size(term, h, w, cfg.savelines);
2357 } else if (wParam == SIZE_RESTORED && was_zoomed) {
2359 if (cfg.resize_action == RESIZE_TERM)
2360 term_size(term, prev_rows, prev_cols, cfg.savelines);
2361 if (cfg.resize_action != RESIZE_FONT)
2366 /* This is an unexpected resize, these will normally happen
2367 * if the window is too large. Probably either the user
2368 * selected a huge font or the screen size has changed.
2370 * This is also called with minimize.
2372 else reset_window(-1);
2376 * Don't call back->size in mid-resize. (To prevent
2377 * massive numbers of resize events getting sent
2378 * down the connection during an NT opaque drag.)
2381 if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
2382 need_backend_resize = TRUE;
2383 w = (width-cfg.window_border*2) / font_width;
2385 h = (height-cfg.window_border*2) / font_height;
2394 sys_cursor_update();
2397 switch (LOWORD(wParam)) {
2399 term_scroll(term, -1, 0);
2402 term_scroll(term, +1, 0);
2405 term_scroll(term, 0, +1);
2408 term_scroll(term, 0, -1);
2411 term_scroll(term, 0, +term->rows / 2);
2414 term_scroll(term, 0, -term->rows / 2);
2416 case SB_THUMBPOSITION:
2418 term_scroll(term, 1, HIWORD(wParam));
2422 case WM_PALETTECHANGED:
2423 if ((HWND) wParam != hwnd && pal != NULL) {
2424 HDC hdc = get_ctx(NULL);
2426 if (RealizePalette(hdc) > 0)
2432 case WM_QUERYNEWPALETTE:
2434 HDC hdc = get_ctx(NULL);
2436 if (RealizePalette(hdc) > 0)
2448 * Add the scan code and keypress timing to the random
2451 noise_ultralight(lParam);
2454 * We don't do TranslateMessage since it disassociates the
2455 * resulting CHAR message from the KEYDOWN that sparked it,
2456 * which we occasionally don't want. Instead, we process
2457 * KEYDOWN, and call the Win32 translator functions so that
2458 * we get the translations under _our_ control.
2461 unsigned char buf[20];
2464 if (wParam == VK_PROCESSKEY) {
2467 m.message = WM_KEYDOWN;
2469 m.lParam = lParam & 0xdfff;
2470 TranslateMessage(&m);
2472 len = TranslateKey(message, wParam, lParam, buf);
2474 return DefWindowProc(hwnd, message, wParam, lParam);
2478 * Interrupt an ongoing paste. I'm not sure
2479 * this is sensible, but for the moment it's
2480 * preferable to having to faff about buffering
2486 * We need not bother about stdin backlogs
2487 * here, because in GUI PuTTY we can't do
2488 * anything about it anyway; there's no means
2489 * of asking Windows to hold off on KEYDOWN
2490 * messages. We _have_ to buffer everything
2493 term_seen_key_event(term);
2494 ldisc_send(ldisc, buf, len, 1);
2499 net_pending_errors();
2501 case WM_INPUTLANGCHANGE:
2502 /* wParam == Font number */
2503 /* lParam == Locale */
2504 set_input_locale((HKL)lParam);
2505 sys_cursor_update();
2508 if(wParam == IMN_SETOPENSTATUS) {
2509 HIMC hImc = ImmGetContext(hwnd);
2510 ImmSetCompositionFont(hImc, &lfont);
2511 ImmReleaseContext(hwnd, hImc);
2515 case WM_IME_COMPOSITION:
2521 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
2522 osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
2524 if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2525 break; /* fall back to DefWindowProc */
2527 hIMC = ImmGetContext(hwnd);
2528 n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
2532 buff = snewn(n, char);
2533 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
2535 * Jaeyoun Chung reports that Korean character
2536 * input doesn't work correctly if we do a single
2537 * luni_send() covering the whole of buff. So
2538 * instead we luni_send the characters one by one.
2540 term_seen_key_event(term);
2541 for (i = 0; i < n; i += 2) {
2542 luni_send(ldisc, (unsigned short *)(buff+i), 1, 1);
2546 ImmReleaseContext(hwnd, hIMC);
2551 if (wParam & 0xFF00) {
2552 unsigned char buf[2];
2555 buf[0] = wParam >> 8;
2556 term_seen_key_event(term);
2557 lpage_send(ldisc, kbd_codepage, buf, 2, 1);
2559 char c = (unsigned char) wParam;
2560 term_seen_key_event(term);
2561 lpage_send(ldisc, kbd_codepage, &c, 1, 1);
2567 * Nevertheless, we are prepared to deal with WM_CHAR
2568 * messages, should they crop up. So if someone wants to
2569 * post the things to us as part of a macro manoeuvre,
2570 * we're ready to cope.
2573 char c = (unsigned char)wParam;
2574 term_seen_key_event(term);
2575 lpage_send(ldisc, CP_ACP, &c, 1, 1);
2579 if (send_raw_mouse && LOWORD(lParam) == HTCLIENT) {
2580 SetCursor(LoadCursor(NULL, IDC_ARROW));
2584 case WM_AGENT_CALLBACK:
2586 struct agent_callback *c = (struct agent_callback *)lParam;
2587 c->callback(c->callback_ctx, c->data, c->len);
2592 if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
2593 int shift_pressed=0, control_pressed=0;
2595 if (message == WM_MOUSEWHEEL) {
2596 wheel_accumulator += (short)HIWORD(wParam);
2597 shift_pressed=LOWORD(wParam) & MK_SHIFT;
2598 control_pressed=LOWORD(wParam) & MK_CONTROL;
2601 wheel_accumulator += (int)wParam;
2602 if (GetKeyboardState(keys)!=0) {
2603 shift_pressed=keys[VK_SHIFT]&0x80;
2604 control_pressed=keys[VK_CONTROL]&0x80;
2608 /* process events when the threshold is reached */
2609 while (abs(wheel_accumulator) >= WHEEL_DELTA) {
2612 /* reduce amount for next time */
2613 if (wheel_accumulator > 0) {
2615 wheel_accumulator -= WHEEL_DELTA;
2616 } else if (wheel_accumulator < 0) {
2618 wheel_accumulator += WHEEL_DELTA;
2622 if (send_raw_mouse &&
2623 !(cfg.mouse_override && shift_pressed)) {
2624 /* send a mouse-down followed by a mouse up */
2625 term_mouse(term, b, translate_button(b),
2627 TO_CHR_X(X_POS(lParam)),
2628 TO_CHR_Y(Y_POS(lParam)), shift_pressed,
2629 control_pressed, is_alt_pressed());
2630 term_mouse(term, b, translate_button(b),
2631 MA_RELEASE, TO_CHR_X(X_POS(lParam)),
2632 TO_CHR_Y(Y_POS(lParam)), shift_pressed,
2633 control_pressed, is_alt_pressed());
2635 /* trigger a scroll */
2636 term_scroll(term, 0,
2638 -term->rows / 2 : term->rows / 2);
2645 return DefWindowProc(hwnd, message, wParam, lParam);
2649 * Move the system caret. (We maintain one, even though it's
2650 * invisible, for the benefit of blind people: apparently some
2651 * helper software tracks the system caret, so we should arrange to
2654 void sys_cursor(void *frontend, int x, int y)
2658 if (!term->has_focus) return;
2661 * Avoid gratuitously re-updating the cursor position and IMM
2662 * window if there's no actual change required.
2664 cx = x * font_width + offset_width;
2665 cy = y * font_height + offset_height;
2666 if (cx == caret_x && cy == caret_y)
2671 sys_cursor_update();
2674 static void sys_cursor_update(void)
2679 if (!term->has_focus) return;
2681 if (caret_x < 0 || caret_y < 0)
2684 SetCaretPos(caret_x, caret_y);
2686 /* IMM calls on Win98 and beyond only */
2687 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
2689 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
2690 osVersion.dwMinorVersion == 0) return; /* 95 */
2692 /* we should have the IMM functions */
2693 hIMC = ImmGetContext(hwnd);
2694 cf.dwStyle = CFS_POINT;
2695 cf.ptCurrentPos.x = caret_x;
2696 cf.ptCurrentPos.y = caret_y;
2697 ImmSetCompositionWindow(hIMC, &cf);
2699 ImmReleaseContext(hwnd, hIMC);
2703 * Draw a line of text in the window, at given character
2704 * coordinates, in given attributes.
2706 * We are allowed to fiddle with the contents of `text'.
2708 void do_text(Context ctx, int x, int y, char *text, int len,
2709 unsigned long attr, int lattr)
2712 int nfg, nbg, nfont;
2715 int force_manual_underline = 0;
2716 int fnt_width = font_width * (1 + (lattr != LATTR_NORM));
2717 int char_width = fnt_width;
2718 int text_adjust = 0;
2719 static int *IpDx = 0, IpDxLEN = 0;
2721 if (attr & ATTR_WIDE)
2724 if (len > IpDxLEN || IpDx[0] != char_width) {
2726 if (len > IpDxLEN) {
2728 IpDx = snewn(len + 16, int);
2729 IpDxLEN = (len + 16);
2731 for (i = 0; i < IpDxLEN; i++)
2732 IpDx[i] = char_width;
2735 /* Only want the left half of double width lines */
2736 if (lattr != LATTR_NORM && x*2 >= term->cols)
2744 if ((attr & TATTR_ACTCURS) && (cfg.cursor_type == 0 || term->big_cursor)) {
2745 attr &= ATTR_CUR_AND | (bold_mode != BOLD_COLOURS ? ATTR_BOLD : 0);
2746 attr ^= ATTR_CUR_XOR;
2750 if (cfg.vtmode == VT_POORMAN && lattr != LATTR_NORM) {
2751 /* Assume a poorman font is borken in other ways too. */
2761 nfont |= FONT_WIDE + FONT_HIGH;
2764 if (attr & ATTR_NARROW)
2765 nfont |= FONT_NARROW;
2767 /* Special hack for the VT100 linedraw glyphs. */
2768 if ((attr & CSET_MASK) == 0x2300) {
2769 if (text[0] >= (char) 0xBA && text[0] <= (char) 0xBD) {
2770 switch ((unsigned char) (text[0])) {
2772 text_adjust = -2 * font_height / 5;
2775 text_adjust = -1 * font_height / 5;
2778 text_adjust = font_height / 5;
2781 text_adjust = 2 * font_height / 5;
2784 if (lattr == LATTR_TOP || lattr == LATTR_BOT)
2787 text[0] = (char) (ucsdata.unitab_xterm['q'] & CHAR_MASK);
2788 attr |= (ucsdata.unitab_xterm['q'] & CSET_MASK);
2789 if (attr & ATTR_UNDER) {
2790 attr &= ~ATTR_UNDER;
2791 force_manual_underline = 1;
2796 /* Anything left as an original character set is unprintable. */
2797 if (DIRECT_CHAR(attr)) {
2800 memset(text, 0xFD, len);
2804 if ((attr & CSET_MASK) == ATTR_OEMCP)
2807 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
2808 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
2809 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
2810 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
2811 if (bold_mode == BOLD_FONT && (attr & ATTR_BOLD))
2813 if (und_mode == UND_FONT && (attr & ATTR_UNDER))
2814 nfont |= FONT_UNDERLINE;
2815 another_font(nfont);
2816 if (!fonts[nfont]) {
2817 if (nfont & FONT_UNDERLINE)
2818 force_manual_underline = 1;
2819 /* Don't do the same for manual bold, it could be bad news. */
2821 nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
2823 another_font(nfont);
2825 nfont = FONT_NORMAL;
2826 if (attr & ATTR_REVERSE) {
2831 if (bold_mode == BOLD_COLOURS && (attr & ATTR_BOLD))
2833 if (bold_mode == BOLD_COLOURS && (attr & ATTR_BLINK))
2837 SelectObject(hdc, fonts[nfont]);
2838 SetTextColor(hdc, fg);
2839 SetBkColor(hdc, bg);
2840 SetBkMode(hdc, OPAQUE);
2843 line_box.right = x + char_width * len;
2844 line_box.bottom = y + font_height;
2846 /* Only want the left half of double width lines */
2847 if (line_box.right > font_width*term->cols+offset_width)
2848 line_box.right = font_width*term->cols+offset_width;
2850 /* We're using a private area for direct to font. (512 chars.) */
2851 if (ucsdata.dbcs_screenfont && (attr & CSET_MASK) == ATTR_ACP) {
2852 /* Ho Hum, dbcs fonts are a PITA! */
2853 /* To display on W9x I have to convert to UCS */
2854 static wchar_t *uni_buf = 0;
2855 static int uni_len = 0;
2857 if (len > uni_len) {
2860 uni_buf = snewn(uni_len, wchar_t);
2863 for(nlen = mptr = 0; mptr<len; mptr++) {
2864 uni_buf[nlen] = 0xFFFD;
2865 if (IsDBCSLeadByteEx(ucsdata.font_codepage, (BYTE) text[mptr])) {
2866 IpDx[nlen] += char_width;
2867 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
2868 text+mptr, 2, uni_buf+nlen, 1);
2873 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
2874 text+mptr, 1, uni_buf+nlen, 1);
2882 y - font_height * (lattr == LATTR_BOT) + text_adjust,
2883 ETO_CLIPPED | ETO_OPAQUE, &line_box, uni_buf, nlen, IpDx);
2884 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
2885 SetBkMode(hdc, TRANSPARENT);
2886 ExtTextOutW(hdc, x - 1,
2887 y - font_height * (lattr ==
2888 LATTR_BOT) + text_adjust,
2889 ETO_CLIPPED, &line_box, uni_buf, nlen, IpDx);
2893 } else if (DIRECT_FONT(attr)) {
2895 y - font_height * (lattr == LATTR_BOT) + text_adjust,
2896 ETO_CLIPPED | ETO_OPAQUE, &line_box, text, len, IpDx);
2897 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
2898 SetBkMode(hdc, TRANSPARENT);
2900 /* GRR: This draws the character outside it's box and can leave
2901 * 'droppings' even with the clip box! I suppose I could loop it
2902 * one character at a time ... yuk.
2904 * Or ... I could do a test print with "W", and use +1 or -1 for this
2905 * shift depending on if the leftmost column is blank...
2907 ExtTextOut(hdc, x - 1,
2908 y - font_height * (lattr ==
2909 LATTR_BOT) + text_adjust,
2910 ETO_CLIPPED, &line_box, text, len, IpDx);
2913 /* And 'normal' unicode characters */
2914 static WCHAR *wbuf = NULL;
2915 static int wlen = 0;
2920 wbuf = snewn(wlen, WCHAR);
2922 for (i = 0; i < len; i++)
2923 wbuf[i] = (WCHAR) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
2926 y - font_height * (lattr == LATTR_BOT) + text_adjust,
2927 ETO_CLIPPED | ETO_OPAQUE, &line_box, wbuf, len, IpDx);
2929 /* And the shadow bold hack. */
2930 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
2931 SetBkMode(hdc, TRANSPARENT);
2932 ExtTextOutW(hdc, x - 1,
2933 y - font_height * (lattr ==
2934 LATTR_BOT) + text_adjust,
2935 ETO_CLIPPED, &line_box, wbuf, len, IpDx);
2938 if (lattr != LATTR_TOP && (force_manual_underline ||
2939 (und_mode == UND_LINE
2940 && (attr & ATTR_UNDER)))) {
2943 if (lattr == LATTR_BOT)
2944 dec = dec * 2 - font_height;
2946 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, fg));
2947 MoveToEx(hdc, x, y + dec, NULL);
2948 LineTo(hdc, x + len * char_width, y + dec);
2949 oldpen = SelectObject(hdc, oldpen);
2950 DeleteObject(oldpen);
2954 void do_cursor(Context ctx, int x, int y, char *text, int len,
2955 unsigned long attr, int lattr)
2961 int ctype = cfg.cursor_type;
2963 if ((attr & TATTR_ACTCURS) && (ctype == 0 || term->big_cursor)) {
2964 if (((attr & CSET_MASK) | (unsigned char) *text) != UCSWIDE) {
2965 do_text(ctx, x, y, text, len, attr, lattr);
2969 attr |= TATTR_RIGHTCURS;
2972 fnt_width = char_width = font_width * (1 + (lattr != LATTR_NORM));
2973 if (attr & ATTR_WIDE)
2980 if ((attr & TATTR_PASCURS) && (ctype == 0 || term->big_cursor)) {
2983 pts[0].x = pts[1].x = pts[4].x = x;
2984 pts[2].x = pts[3].x = x + char_width - 1;
2985 pts[0].y = pts[3].y = pts[4].y = y;
2986 pts[1].y = pts[2].y = y + font_height - 1;
2987 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
2988 Polyline(hdc, pts, 5);
2989 oldpen = SelectObject(hdc, oldpen);
2990 DeleteObject(oldpen);
2991 } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) && ctype != 0) {
2992 int startx, starty, dx, dy, length, i;
2995 starty = y + descent;
2998 length = char_width;
3001 if (attr & TATTR_RIGHTCURS)
3002 xadjust = char_width - 1;
3003 startx = x + xadjust;
3007 length = font_height;
3009 if (attr & TATTR_ACTCURS) {
3012 SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
3013 MoveToEx(hdc, startx, starty, NULL);
3014 LineTo(hdc, startx + dx * length, starty + dy * length);
3015 oldpen = SelectObject(hdc, oldpen);
3016 DeleteObject(oldpen);
3018 for (i = 0; i < length; i++) {
3020 SetPixel(hdc, startx, starty, colours[23]);
3029 /* This function gets the actual width of a character in the normal font.
3031 int char_width(Context ctx, int uc) {
3035 /* If the font max is the same as the font ave width then this
3036 * function is a no-op.
3038 if (!font_dualwidth) return 1;
3040 switch (uc & CSET_MASK) {
3042 uc = ucsdata.unitab_line[uc & 0xFF];
3045 uc = ucsdata.unitab_xterm[uc & 0xFF];
3048 uc = ucsdata.unitab_scoacs[uc & 0xFF];
3051 if (DIRECT_FONT(uc)) {
3052 if (ucsdata.dbcs_screenfont) return 1;
3054 /* Speedup, I know of no font where ascii is the wrong width */
3055 if ((uc&CHAR_MASK) >= ' ' && (uc&CHAR_MASK)<= '~')
3058 if ( (uc & CSET_MASK) == ATTR_ACP ) {
3059 SelectObject(hdc, fonts[FONT_NORMAL]);
3060 } else if ( (uc & CSET_MASK) == ATTR_OEMCP ) {
3061 another_font(FONT_OEM);
3062 if (!fonts[FONT_OEM]) return 0;
3064 SelectObject(hdc, fonts[FONT_OEM]);
3068 if ( GetCharWidth32(hdc, uc&CHAR_MASK, uc&CHAR_MASK, &ibuf) != 1 &&
3069 GetCharWidth(hdc, uc&CHAR_MASK, uc&CHAR_MASK, &ibuf) != 1)
3072 /* Speedup, I know of no font where ascii is the wrong width */
3073 if (uc >= ' ' && uc <= '~') return 1;
3075 SelectObject(hdc, fonts[FONT_NORMAL]);
3076 if ( GetCharWidth32W(hdc, uc, uc, &ibuf) == 1 )
3077 /* Okay that one worked */ ;
3078 else if ( GetCharWidthW(hdc, uc, uc, &ibuf) == 1 )
3079 /* This should work on 9x too, but it's "less accurate" */ ;
3084 ibuf += font_width / 2 -1;
3091 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3092 * codes. Returns number of bytes used or zero to drop the message
3093 * or -1 to forward the message to windows.
3095 static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
3096 unsigned char *output)
3099 int scan, left_alt = 0, key_down, shift_state;
3101 unsigned char *p = output;
3102 static int alt_sum = 0;
3104 HKL kbd_layout = GetKeyboardLayout(0);
3106 static WORD keys[3];
3107 static int compose_char = 0;
3108 static WPARAM compose_key = 0;
3110 r = GetKeyboardState(keystate);
3112 memset(keystate, 0, sizeof(keystate));
3115 #define SHOW_TOASCII_RESULT
3116 { /* Tell us all about key events */
3117 static BYTE oldstate[256];
3118 static int first = 1;
3122 memcpy(oldstate, keystate, sizeof(oldstate));
3125 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
3127 } else if ((HIWORD(lParam) & KF_UP)
3128 && scan == (HIWORD(lParam) & 0xFF)) {
3132 if (wParam >= VK_F1 && wParam <= VK_F20)
3133 debug(("K_F%d", wParam + 1 - VK_F1));
3146 debug(("VK_%02x", wParam));
3148 if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
3150 debug((", S%02x", scan = (HIWORD(lParam) & 0xFF)));
3152 ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
3153 if (ch >= ' ' && ch <= '~')
3154 debug((", '%c'", ch));
3156 debug((", $%02x", ch));
3159 debug((", KB0=%02x", keys[0]));
3161 debug((", KB1=%02x", keys[1]));
3163 debug((", KB2=%02x", keys[2]));
3165 if ((keystate[VK_SHIFT] & 0x80) != 0)
3167 if ((keystate[VK_CONTROL] & 0x80) != 0)
3169 if ((HIWORD(lParam) & KF_EXTENDED))
3171 if ((HIWORD(lParam) & KF_UP))
3175 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
3176 else if ((HIWORD(lParam) & KF_UP))
3177 oldstate[wParam & 0xFF] ^= 0x80;
3179 oldstate[wParam & 0xFF] ^= 0x81;
3181 for (ch = 0; ch < 256; ch++)
3182 if (oldstate[ch] != keystate[ch])
3183 debug((", M%02x=%02x", ch, keystate[ch]));
3185 memcpy(oldstate, keystate, sizeof(oldstate));
3189 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
3190 keystate[VK_RMENU] = keystate[VK_MENU];
3194 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3195 if ((cfg.funky_type == 3 ||
3196 (cfg.funky_type <= 1 && term->app_keypad_keys &&
3198 && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
3200 wParam = VK_EXECUTE;
3202 /* UnToggle NUMLock */
3203 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
3204 keystate[VK_NUMLOCK] ^= 1;
3207 /* And write back the 'adjusted' state */
3208 SetKeyboardState(keystate);
3211 /* Disable Auto repeat if required */
3212 if (term->repeat_off &&
3213 (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
3216 if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
3219 key_down = ((HIWORD(lParam) & KF_UP) == 0);
3221 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3222 if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
3223 if (cfg.ctrlaltkeys)
3224 keystate[VK_MENU] = 0;
3226 keystate[VK_RMENU] = 0x80;
3231 scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
3232 shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
3233 + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
3235 /* Note if AltGr was pressed and if it was used as a compose key */
3236 if (!compose_state) {
3237 compose_key = 0x100;
3238 if (cfg.compose_key) {
3239 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
3240 compose_key = wParam;
3242 if (wParam == VK_APPS)
3243 compose_key = wParam;
3246 if (wParam == compose_key) {
3247 if (compose_state == 0
3248 && (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0) compose_state =
3250 else if (compose_state == 1 && (HIWORD(lParam) & KF_UP))
3254 } else if (compose_state == 1 && wParam != VK_CONTROL)
3257 if (compose_state > 1 && left_alt)
3260 /* Sanitize the number pad if not using a PC NumPad */
3261 if (left_alt || (term->app_keypad_keys && !cfg.no_applic_k
3262 && cfg.funky_type != 2)
3263 || cfg.funky_type == 3 || cfg.nethack_keypad || compose_state) {
3264 if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
3268 nParam = VK_NUMPAD0;
3271 nParam = VK_NUMPAD1;
3274 nParam = VK_NUMPAD2;
3277 nParam = VK_NUMPAD3;
3280 nParam = VK_NUMPAD4;
3283 nParam = VK_NUMPAD5;
3286 nParam = VK_NUMPAD6;
3289 nParam = VK_NUMPAD7;
3292 nParam = VK_NUMPAD8;
3295 nParam = VK_NUMPAD9;
3298 nParam = VK_DECIMAL;
3302 if (keystate[VK_NUMLOCK] & 1)
3309 /* If a key is pressed and AltGr is not active */
3310 if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !compose_state) {
3311 /* Okay, prepare for most alts then ... */
3315 /* Lets see if it's a pattern we know all about ... */
3316 if (wParam == VK_PRIOR && shift_state == 1) {
3317 SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
3320 if (wParam == VK_PRIOR && shift_state == 2) {
3321 SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
3324 if (wParam == VK_NEXT && shift_state == 1) {
3325 SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
3328 if (wParam == VK_NEXT && shift_state == 2) {
3329 SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
3332 if (wParam == VK_INSERT && shift_state == 1) {
3333 term_do_paste(term);
3336 if (left_alt && wParam == VK_F4 && cfg.alt_f4) {
3339 if (left_alt && wParam == VK_SPACE && cfg.alt_space) {
3340 SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
3343 if (left_alt && wParam == VK_RETURN && cfg.fullscreenonaltenter &&
3344 (cfg.resize_action != RESIZE_DISABLED)) {
3345 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
3349 /* Control-Numlock for app-keypad mode switch */
3350 if (wParam == VK_PAUSE && shift_state == 2) {
3351 term->app_keypad_keys ^= 1;
3355 /* Nethack keypad */
3356 if (cfg.nethack_keypad && !left_alt) {
3359 *p++ = shift_state ? 'B' : 'b';
3362 *p++ = shift_state ? 'J' : 'j';
3365 *p++ = shift_state ? 'N' : 'n';
3368 *p++ = shift_state ? 'H' : 'h';
3371 *p++ = shift_state ? '.' : '.';
3374 *p++ = shift_state ? 'L' : 'l';
3377 *p++ = shift_state ? 'Y' : 'y';
3380 *p++ = shift_state ? 'K' : 'k';
3383 *p++ = shift_state ? 'U' : 'u';
3388 /* Application Keypad */
3392 if (cfg.funky_type == 3 ||
3393 (cfg.funky_type <= 1 &&
3394 term->app_keypad_keys && !cfg.no_applic_k)) switch (wParam) {
3408 if (term->app_keypad_keys && !cfg.no_applic_k)
3445 if (cfg.funky_type == 2) {
3450 } else if (shift_state)
3457 if (cfg.funky_type == 2)
3461 if (cfg.funky_type == 2)
3465 if (cfg.funky_type == 2)
3470 if (HIWORD(lParam) & KF_EXTENDED)
3475 if (term->vt52_mode) {
3476 if (xkey >= 'P' && xkey <= 'S')
3477 p += sprintf((char *) p, "\x1B%c", xkey);
3479 p += sprintf((char *) p, "\x1B?%c", xkey);
3481 p += sprintf((char *) p, "\x1BO%c", xkey);
3486 if (wParam == VK_BACK && shift_state == 0) { /* Backspace */
3487 *p++ = (cfg.bksp_is_delete ? 0x7F : 0x08);
3491 if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */
3492 /* We do the opposite of what is configured */
3493 *p++ = (cfg.bksp_is_delete ? 0x08 : 0x7F);
3497 if (wParam == VK_TAB && shift_state == 1) { /* Shift tab */
3503 if (wParam == VK_SPACE && shift_state == 2) { /* Ctrl-Space */
3507 if (wParam == VK_SPACE && shift_state == 3) { /* Ctrl-Shift-Space */
3511 if (wParam == VK_CANCEL && shift_state == 2) { /* Ctrl-Break */
3516 if (wParam == VK_PAUSE) { /* Break/Pause */
3521 /* Control-2 to Control-8 are special */
3522 if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
3523 *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
3526 if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
3530 if (shift_state == 2 && wParam == 0xDF) {
3534 if (shift_state == 3 && wParam == 0xDE) {
3535 *p++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
3538 if (shift_state == 0 && wParam == VK_RETURN && term->cr_lf_return) {
3545 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3546 * for integer decimal nn.)
3548 * We also deal with the weird ones here. Linux VCs replace F1
3549 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3550 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3556 code = (keystate[VK_SHIFT] & 0x80 ? 23 : 11);
3559 code = (keystate[VK_SHIFT] & 0x80 ? 24 : 12);
3562 code = (keystate[VK_SHIFT] & 0x80 ? 25 : 13);
3565 code = (keystate[VK_SHIFT] & 0x80 ? 26 : 14);
3568 code = (keystate[VK_SHIFT] & 0x80 ? 28 : 15);
3571 code = (keystate[VK_SHIFT] & 0x80 ? 29 : 17);
3574 code = (keystate[VK_SHIFT] & 0x80 ? 31 : 18);
3577 code = (keystate[VK_SHIFT] & 0x80 ? 32 : 19);
3580 code = (keystate[VK_SHIFT] & 0x80 ? 33 : 20);
3583 code = (keystate[VK_SHIFT] & 0x80 ? 34 : 21);
3616 if ((shift_state&2) == 0) switch (wParam) {
3636 /* Reorder edit keys to physical order */
3637 if (cfg.funky_type == 3 && code <= 6)
3638 code = "\0\2\1\4\5\3\6"[code];
3640 if (term->vt52_mode && code > 0 && code <= 6) {
3641 p += sprintf((char *) p, "\x1B%c", " HLMEIG"[code]);
3645 if (cfg.funky_type == 5 && /* SCO function keys */
3646 code >= 11 && code <= 34) {
3647 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
3650 case VK_F1: index = 0; break;
3651 case VK_F2: index = 1; break;
3652 case VK_F3: index = 2; break;
3653 case VK_F4: index = 3; break;
3654 case VK_F5: index = 4; break;
3655 case VK_F6: index = 5; break;
3656 case VK_F7: index = 6; break;
3657 case VK_F8: index = 7; break;
3658 case VK_F9: index = 8; break;
3659 case VK_F10: index = 9; break;
3660 case VK_F11: index = 10; break;
3661 case VK_F12: index = 11; break;
3663 if (keystate[VK_SHIFT] & 0x80) index += 12;
3664 if (keystate[VK_CONTROL] & 0x80) index += 24;
3665 p += sprintf((char *) p, "\x1B[%c", codes[index]);
3668 if (cfg.funky_type == 5 && /* SCO small keypad */
3669 code >= 1 && code <= 6) {
3670 char codes[] = "HL.FIG";
3674 p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
3678 if ((term->vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
3684 if (term->vt52_mode)
3685 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11 - offt);
3688 sprintf((char *) p, "\x1BO%c", code + 'P' - 11 - offt);
3691 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
3692 p += sprintf((char *) p, "\x1B[[%c", code + 'A' - 11);
3695 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
3696 if (term->vt52_mode)
3697 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11);
3699 p += sprintf((char *) p, "\x1BO%c", code + 'P' - 11);
3702 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
3703 p += sprintf((char *) p, code == 1 ? "\x1B[H" : "\x1BOw");
3707 p += sprintf((char *) p, "\x1B[%d~", code);
3712 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
3713 * some reason seems to send VK_CLEAR to Windows...).
3735 if (term->vt52_mode)
3736 p += sprintf((char *) p, "\x1B%c", xkey);
3738 int app_flg = (term->app_cursor_keys && !cfg.no_applic_c);
3741 * RDB: VT100 & VT102 manuals both state the
3742 * app cursor keys only work if the app keypad
3745 * SGT: That may well be true, but xterm
3746 * disagrees and so does at least one
3747 * application, so I've #if'ed this out and the
3748 * behaviour is back to PuTTY's original: app
3749 * cursor and app keypad are independently
3750 * switchable modes. If anyone complains about
3751 * _this_ I'll have to put in a configurable
3754 if (!term->app_keypad_keys)
3757 /* Useful mapping of Ctrl-arrows */
3758 if (shift_state == 2)
3762 p += sprintf((char *) p, "\x1BO%c", xkey);
3764 p += sprintf((char *) p, "\x1B[%c", xkey);
3771 * Finally, deal with Return ourselves. (Win95 seems to
3772 * foul it up when Alt is pressed, for some reason.)
3774 if (wParam == VK_RETURN) { /* Return */
3780 if (left_alt && wParam >= VK_NUMPAD0 && wParam <= VK_NUMPAD9)
3781 alt_sum = alt_sum * 10 + wParam - VK_NUMPAD0;
3786 /* Okay we've done everything interesting; let windows deal with
3787 * the boring stuff */
3791 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
3792 if(cfg.xlat_capslockcyr && keystate[VK_CAPITAL] != 0) {
3794 keystate[VK_CAPITAL] = 0;
3797 r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
3798 #ifdef SHOW_TOASCII_RESULT
3799 if (r == 1 && !key_down) {
3801 if (in_utf(term) || ucsdata.dbcs_screenfont)
3802 debug((", (U+%04x)", alt_sum));
3804 debug((", LCH(%d)", alt_sum));
3806 debug((", ACH(%d)", keys[0]));
3811 for (r1 = 0; r1 < r; r1++) {
3812 debug(("%s%d", r1 ? "," : "", keys[r1]));
3821 * Interrupt an ongoing paste. I'm not sure this is
3822 * sensible, but for the moment it's preferable to
3823 * having to faff about buffering things.
3828 for (i = 0; i < r; i++) {
3829 unsigned char ch = (unsigned char) keys[i];
3831 if (compose_state == 2 && (ch & 0x80) == 0 && ch > ' ') {
3836 if (compose_state == 3 && (ch & 0x80) == 0 && ch > ' ') {
3840 if ((nc = check_compose(compose_char, ch)) == -1) {
3841 MessageBeep(MB_ICONHAND);
3845 term_seen_key_event(term);
3846 luni_send(ldisc, &keybuf, 1, 1);
3854 if (in_utf(term) || ucsdata.dbcs_screenfont) {
3856 term_seen_key_event(term);
3857 luni_send(ldisc, &keybuf, 1, 1);
3859 ch = (char) alt_sum;
3861 * We need not bother about stdin
3862 * backlogs here, because in GUI PuTTY
3863 * we can't do anything about it
3864 * anyway; there's no means of asking
3865 * Windows to hold off on KEYDOWN
3866 * messages. We _have_ to buffer
3867 * everything we're sent.
3869 term_seen_key_event(term);
3870 ldisc_send(ldisc, &ch, 1, 1);
3874 term_seen_key_event(term);
3875 lpage_send(ldisc, kbd_codepage, &ch, 1, 1);
3878 if(capsOn && ch < 0x80) {
3881 cbuf[1] = xlat_uskbd2cyrllic(ch);
3882 term_seen_key_event(term);
3883 luni_send(ldisc, cbuf+!left_alt, 1+!!left_alt, 1);
3888 term_seen_key_event(term);
3889 lpage_send(ldisc, kbd_codepage,
3890 cbuf+!left_alt, 1+!!left_alt, 1);
3896 /* This is so the ALT-Numpad and dead keys work correctly. */
3901 /* If we're definitly not building up an ALT-54321 then clear it */
3904 /* If we will be using alt_sum fix the 256s */
3905 else if (keys[0] && (in_utf(term) || ucsdata.dbcs_screenfont))
3910 * ALT alone may or may not want to bring up the System menu.
3911 * If it's not meant to, we return 0 on presses or releases of
3912 * ALT, to show that we've swallowed the keystroke. Otherwise
3913 * we return -1, which means Windows will give the keystroke
3914 * its default handling (i.e. bring up the System menu).
3916 if (wParam == VK_MENU && !cfg.alt_only)
3922 void request_paste(void *frontend)
3925 * In Windows, pasting is synchronous: we can read the
3926 * clipboard with no difficulty, so request_paste() can just go
3929 term_do_paste(term);
3932 void set_title(void *frontend, char *title)
3935 window_name = snewn(1 + strlen(title), char);
3936 strcpy(window_name, title);
3937 if (cfg.win_name_always || !IsIconic(hwnd))
3938 SetWindowText(hwnd, title);
3941 void set_icon(void *frontend, char *title)
3944 icon_name = snewn(1 + strlen(title), char);
3945 strcpy(icon_name, title);
3946 if (!cfg.win_name_always && IsIconic(hwnd))
3947 SetWindowText(hwnd, title);
3950 void set_sbar(void *frontend, int total, int start, int page)
3954 if (is_full_screen() ? !cfg.scrollbar_in_fullscreen : !cfg.scrollbar)
3957 si.cbSize = sizeof(si);
3958 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
3960 si.nMax = total - 1;
3964 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
3967 Context get_ctx(void *frontend)
3973 SelectPalette(hdc, pal, FALSE);
3979 void free_ctx(Context ctx)
3981 SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
3982 ReleaseDC(hwnd, ctx);
3985 static void real_palette_set(int n, int r, int g, int b)
3988 logpal->palPalEntry[n].peRed = r;
3989 logpal->palPalEntry[n].peGreen = g;
3990 logpal->palPalEntry[n].peBlue = b;
3991 logpal->palPalEntry[n].peFlags = PC_NOCOLLAPSE;
3992 colours[n] = PALETTERGB(r, g, b);
3993 SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
3995 colours[n] = RGB(r, g, b);
3998 void palette_set(void *frontend, int n, int r, int g, int b)
4000 static const int first[21] = {
4001 0, 2, 4, 6, 8, 10, 12, 14,
4002 1, 3, 5, 7, 9, 11, 13, 15,
4005 real_palette_set(first[n], r, g, b);
4007 real_palette_set(first[n] + 1, r, g, b);
4009 HDC hdc = get_ctx(frontend);
4010 UnrealizeObject(pal);
4011 RealizePalette(hdc);
4016 void palette_reset(void *frontend)
4020 for (i = 0; i < NCOLOURS; i++) {
4022 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
4023 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
4024 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
4025 logpal->palPalEntry[i].peFlags = 0;
4026 colours[i] = PALETTERGB(defpal[i].rgbtRed,
4027 defpal[i].rgbtGreen,
4028 defpal[i].rgbtBlue);
4030 colours[i] = RGB(defpal[i].rgbtRed,
4031 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
4036 SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
4037 hdc = get_ctx(frontend);
4038 RealizePalette(hdc);
4043 void write_aclip(void *frontend, char *data, int len, int must_deselect)
4048 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
4051 lock = GlobalLock(clipdata);
4054 memcpy(lock, data, len);
4055 ((unsigned char *) lock)[len] = 0;
4056 GlobalUnlock(clipdata);
4059 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
4061 if (OpenClipboard(hwnd)) {
4063 SetClipboardData(CF_TEXT, clipdata);
4066 GlobalFree(clipdata);
4069 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
4073 * Note: unlike write_aclip() this will not append a nul.
4075 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
4077 HGLOBAL clipdata, clipdata2, clipdata3;
4079 void *lock, *lock2, *lock3;
4081 len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
4083 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
4084 len * sizeof(wchar_t));
4085 clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
4087 if (!clipdata || !clipdata2) {
4089 GlobalFree(clipdata);
4091 GlobalFree(clipdata2);
4094 if (!(lock = GlobalLock(clipdata)))
4096 if (!(lock2 = GlobalLock(clipdata2)))
4099 memcpy(lock, data, len * sizeof(wchar_t));
4100 WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
4102 if (cfg.rtf_paste) {
4103 wchar_t unitab[256];
4105 unsigned char *tdata = (unsigned char *)lock2;
4106 wchar_t *udata = (wchar_t *)lock;
4107 int rtflen = 0, uindex = 0, tindex = 0;
4109 int multilen, blen, alen, totallen, i;
4110 char before[16], after[4];
4112 get_unitab(CP_ACP, unitab, 0);
4114 rtfsize = 100 + strlen(cfg.font.name);
4115 rtf = snewn(rtfsize, char);
4116 sprintf(rtf, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
4117 GetACP(), cfg.font.name);
4118 rtflen = strlen(rtf);
4121 * We want to construct a piece of RTF that specifies the
4122 * same Unicode text. To do this we will read back in
4123 * parallel from the Unicode data in `udata' and the
4124 * non-Unicode data in `tdata'. For each character in
4125 * `tdata' which becomes the right thing in `udata' when
4126 * looked up in `unitab', we just copy straight over from
4127 * tdata. For each one that doesn't, we must WCToMB it
4128 * individually and produce a \u escape sequence.
4130 * It would probably be more robust to just bite the bullet
4131 * and WCToMB each individual Unicode character one by one,
4132 * then MBToWC each one back to see if it was an accurate
4133 * translation; but that strikes me as a horrifying number
4134 * of Windows API calls so I want to see if this faster way
4135 * will work. If it screws up badly we can always revert to
4136 * the simple and slow way.
4138 while (tindex < len2 && uindex < len &&
4139 tdata[tindex] && udata[uindex]) {
4140 if (tindex + 1 < len2 &&
4141 tdata[tindex] == '\r' &&
4142 tdata[tindex+1] == '\n') {
4146 if (unitab[tdata[tindex]] == udata[uindex]) {
4152 multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
4153 NULL, 0, NULL, NULL);
4154 if (multilen != 1) {
4155 blen = sprintf(before, "{\\uc%d\\u%d", multilen,
4157 alen = 1; strcpy(after, "}");
4159 blen = sprintf(before, "\\u%d", udata[uindex]);
4160 alen = 0; after[0] = '\0';
4163 assert(tindex + multilen <= len2);
4164 totallen = blen + alen;
4165 for (i = 0; i < multilen; i++) {
4166 if (tdata[tindex+i] == '\\' ||
4167 tdata[tindex+i] == '{' ||
4168 tdata[tindex+i] == '}')
4170 else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A)
4171 totallen += 6; /* \par\r\n */
4172 else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20)
4178 if (rtfsize < rtflen + totallen + 3) {
4179 rtfsize = rtflen + totallen + 512;
4180 rtf = sresize(rtf, rtfsize, char);
4183 strcpy(rtf + rtflen, before); rtflen += blen;
4184 for (i = 0; i < multilen; i++) {
4185 if (tdata[tindex+i] == '\\' ||
4186 tdata[tindex+i] == '{' ||
4187 tdata[tindex+i] == '}') {
4188 rtf[rtflen++] = '\\';
4189 rtf[rtflen++] = tdata[tindex+i];
4190 } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
4191 rtflen += sprintf(rtf+rtflen, "\\par\r\n");
4192 } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
4193 rtflen += sprintf(rtf+rtflen, "\\'%02x", tdata[tindex+i]);
4195 rtf[rtflen++] = tdata[tindex+i];
4198 strcpy(rtf + rtflen, after); rtflen += alen;
4204 strcpy(rtf + rtflen, "}");
4207 clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtflen);
4208 if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
4210 GlobalUnlock(clipdata3);
4216 GlobalUnlock(clipdata);
4217 GlobalUnlock(clipdata2);
4220 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
4222 if (OpenClipboard(hwnd)) {
4224 SetClipboardData(CF_UNICODETEXT, clipdata);
4225 SetClipboardData(CF_TEXT, clipdata2);
4227 SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
4230 GlobalFree(clipdata);
4231 GlobalFree(clipdata2);
4235 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
4238 void get_clip(void *frontend, wchar_t ** p, int *len)
4240 static HGLOBAL clipdata = NULL;
4241 static wchar_t *converted = 0;
4250 GlobalUnlock(clipdata);
4253 } else if (OpenClipboard(NULL)) {
4254 if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
4256 *p = GlobalLock(clipdata);
4258 for (p2 = *p; *p2; p2++);
4262 } else if ( (clipdata = GetClipboardData(CF_TEXT)) ) {
4266 s = GlobalLock(clipdata);
4267 i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
4268 *p = converted = snewn(i, wchar_t);
4269 MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, converted, i);
4282 * Move `lines' lines from position `from' to position `to' in the
4285 void optimised_move(void *frontend, int to, int from, int lines)
4290 min = (to < from ? to : from);
4291 max = to + from - min;
4293 r.left = offset_width;
4294 r.right = offset_width + term->cols * font_width;
4295 r.top = offset_height + min * font_height;
4296 r.bottom = offset_height + (max + lines) * font_height;
4297 ScrollWindow(hwnd, 0, (to - from) * font_height, &r, &r);
4302 * Print a message box and perform a fatal exit.
4304 void fatalbox(char *fmt, ...)
4307 char *stuff, morestuff[100];
4310 stuff = dupvprintf(fmt, ap);
4312 sprintf(morestuff, "%.70s Fatal Error", appname);
4313 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
4319 * Print a modal (Really Bad) message box and perform a fatal exit.
4321 void modalfatalbox(char *fmt, ...)
4324 char *stuff, morestuff[100];
4327 stuff = dupvprintf(fmt, ap);
4329 sprintf(morestuff, "%.70s Fatal Error", appname);
4330 MessageBox(hwnd, stuff, morestuff,
4331 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
4337 * Manage window caption / taskbar flashing, if enabled.
4338 * 0 = stop, 1 = maintain, 2 = start
4340 static void flash_window(int mode)
4342 static long last_flash = 0;
4343 static int flashing = 0;
4344 if ((mode == 0) || (cfg.beep_ind == B_IND_DISABLED)) {
4347 FlashWindow(hwnd, FALSE);
4351 } else if (mode == 2) {
4354 last_flash = GetTickCount();
4356 FlashWindow(hwnd, TRUE);
4359 } else if ((mode == 1) && (cfg.beep_ind == B_IND_FLASH)) {
4362 long now = GetTickCount();
4363 long fdiff = now - last_flash;
4364 if (fdiff < 0 || fdiff > 450) {
4366 FlashWindow(hwnd, TRUE); /* toggle */
4375 void beep(void *frontend, int mode)
4377 if (mode == BELL_DEFAULT) {
4379 * For MessageBeep style bells, we want to be careful of
4380 * timing, because they don't have the nice property of
4381 * PlaySound bells that each one cancels the previous
4382 * active one. So we limit the rate to one per 50ms or so.
4384 static long lastbeep = 0;
4387 beepdiff = GetTickCount() - lastbeep;
4388 if (beepdiff >= 0 && beepdiff < 50)
4392 * The above MessageBeep call takes time, so we record the
4393 * time _after_ it finishes rather than before it starts.
4395 lastbeep = GetTickCount();
4396 } else if (mode == BELL_WAVEFILE) {
4397 if (!PlaySound(cfg.bell_wavefile.path, NULL,
4398 SND_ASYNC | SND_FILENAME)) {
4399 char buf[sizeof(cfg.bell_wavefile) + 80];
4401 sprintf(buf, "Unable to play sound file\n%s\n"
4402 "Using default sound instead", cfg.bell_wavefile);
4403 sprintf(otherbuf, "%.70s Sound Error", appname);
4404 MessageBox(hwnd, buf, otherbuf,
4405 MB_OK | MB_ICONEXCLAMATION);
4406 cfg.beep = BELL_DEFAULT;
4408 } else if (mode == BELL_PCSPEAKER) {
4409 static long lastbeep = 0;
4412 beepdiff = GetTickCount() - lastbeep;
4413 if (beepdiff >= 0 && beepdiff < 50)
4417 * We must beep in different ways depending on whether this
4418 * is a 95-series or NT-series OS.
4420 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
4424 lastbeep = GetTickCount();
4426 /* Otherwise, either visual bell or disabled; do nothing here */
4427 if (!term->has_focus) {
4428 flash_window(2); /* start */
4433 * Minimise or restore the window in response to a server-side
4436 void set_iconic(void *frontend, int iconic)
4438 if (IsIconic(hwnd)) {
4440 ShowWindow(hwnd, SW_RESTORE);
4443 ShowWindow(hwnd, SW_MINIMIZE);
4448 * Move the window in response to a server-side request.
4450 void move_window(void *frontend, int x, int y)
4452 if (cfg.resize_action == RESIZE_DISABLED ||
4453 cfg.resize_action == RESIZE_FONT ||
4457 SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
4461 * Move the window to the top or bottom of the z-order in response
4462 * to a server-side request.
4464 void set_zorder(void *frontend, int top)
4466 if (cfg.alwaysontop)
4467 return; /* ignore */
4468 SetWindowPos(hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
4469 SWP_NOMOVE | SWP_NOSIZE);
4473 * Refresh the window in response to a server-side request.
4475 void refresh_window(void *frontend)
4477 InvalidateRect(hwnd, NULL, TRUE);
4481 * Maximise or restore the window in response to a server-side
4484 void set_zoomed(void *frontend, int zoomed)
4486 if (IsZoomed(hwnd)) {
4488 ShowWindow(hwnd, SW_RESTORE);
4491 ShowWindow(hwnd, SW_MAXIMIZE);
4496 * Report whether the window is iconic, for terminal reports.
4498 int is_iconic(void *frontend)
4500 return IsIconic(hwnd);
4504 * Report the window's position, for terminal reports.
4506 void get_window_pos(void *frontend, int *x, int *y)
4509 GetWindowRect(hwnd, &r);
4515 * Report the window's pixel size, for terminal reports.
4517 void get_window_pixels(void *frontend, int *x, int *y)
4520 GetWindowRect(hwnd, &r);
4521 *x = r.right - r.left;
4522 *y = r.bottom - r.top;
4526 * Return the window or icon title.
4528 char *get_window_title(void *frontend, int icon)
4530 return icon ? icon_name : window_name;
4534 * See if we're in full-screen mode.
4536 int is_full_screen()
4538 if (!IsZoomed(hwnd))
4540 if (GetWindowLong(hwnd, GWL_STYLE) & WS_CAPTION)
4545 /* Get the rect/size of a full screen window using the nearest available
4546 * monitor in multimon systems; default to something sensible if only
4547 * one monitor is present. */
4548 static int get_fullscreen_rect(RECT * ss)
4550 #ifdef MONITOR_DEFAULTTONEAREST
4553 mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
4554 mi.cbSize = sizeof(mi);
4555 GetMonitorInfo(mon, &mi);
4557 /* structure copy */
4561 /* could also use code like this:
4562 ss->left = ss->top = 0;
4563 ss->right = GetSystemMetrics(SM_CXSCREEN);
4564 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
4566 return GetClientRect(GetDesktopWindow(), ss);
4572 * Go full-screen. This should only be called when we are already
4575 void make_full_screen()
4580 assert(IsZoomed(hwnd));
4582 if (is_full_screen())
4585 /* Remove the window furniture. */
4586 style = GetWindowLong(hwnd, GWL_STYLE);
4587 style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
4588 if (cfg.scrollbar_in_fullscreen)
4589 style |= WS_VSCROLL;
4591 style &= ~WS_VSCROLL;
4592 SetWindowLong(hwnd, GWL_STYLE, style);
4594 /* Resize ourselves to exactly cover the nearest monitor. */
4595 get_fullscreen_rect(&ss);
4596 SetWindowPos(hwnd, HWND_TOP, ss.left, ss.top,
4601 /* Tick the menu item in the System menu. */
4602 CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
4607 * Clear the full-screen attributes.
4609 void clear_full_screen()
4611 DWORD oldstyle, style;
4613 /* Reinstate the window furniture. */
4614 style = oldstyle = GetWindowLong(hwnd, GWL_STYLE);
4615 style |= WS_CAPTION | WS_BORDER;
4616 if (cfg.resize_action == RESIZE_DISABLED)
4617 style &= ~WS_THICKFRAME;
4619 style |= WS_THICKFRAME;
4621 style |= WS_VSCROLL;
4623 style &= ~WS_VSCROLL;
4624 if (style != oldstyle) {
4625 SetWindowLong(hwnd, GWL_STYLE, style);
4626 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
4627 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
4631 /* Untick the menu item in the System menu. */
4632 CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
4637 * Toggle full-screen mode.
4639 void flip_full_screen()
4641 if (is_full_screen()) {
4642 ShowWindow(hwnd, SW_RESTORE);
4643 } else if (IsZoomed(hwnd)) {
4646 SendMessage(hwnd, WM_FULLSCR_ON_MAX, 0, 0);
4647 ShowWindow(hwnd, SW_MAXIMIZE);
4651 void frontend_keypress(void *handle)
4654 * Keypress termination in non-Close-On-Exit mode is not
4655 * currently supported in PuTTY proper, because the window
4656 * always has a perfectly good Close button anyway. So we do
4662 int from_backend(void *frontend, int is_stderr, const char *data, int len)
4664 return term_data(term, is_stderr, data, len);
4667 void agent_schedule_callback(void (*callback)(void *, void *, int),
4668 void *callback_ctx, void *data, int len)
4670 struct agent_callback *c = snew(struct agent_callback);
4671 c->callback = callback;
4672 c->callback_ctx = callback_ctx;
4675 PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);