Post-release destabilisation! Completely remove the struct type
[u/mdw/putty] / windows / window.c
CommitLineData
eaf1e20a 1/*
2 * window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
3 * emulator and backend in a window.
4 */
5
374330e2 6#include <stdio.h>
7#include <stdlib.h>
1d470ad2 8#include <ctype.h>
ec55b220 9#include <time.h>
95587c73 10#include <limits.h>
a7419ea4 11#include <assert.h>
374330e2 12
06c3eadd 13#ifndef NO_MULTIMON
14#define COMPILE_MULTIMON_STUBS
15#endif
16
32874aea 17#define PUTTY_DO_GLOBALS /* actually _define_ globals */
374330e2 18#include "putty.h"
887035a5 19#include "terminal.h"
d5859615 20#include "storage.h"
374330e2 21#include "win_res.h"
22
7440fd44 23#ifndef NO_MULTIMON
7440fd44 24#include <multimon.h>
25#endif
7440fd44 26
27#include <imm.h>
28#include <commctrl.h>
29#include <richedit.h>
30#include <mmsystem.h>
31
ce0b6f45 32/* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
33 * wParam are used by Windows, and should be masked off, so we shouldn't
34 * attempt to store information in them. Hence all these identifiers have
865e82ad 35 * the low 4 bits clear. Also, identifiers should < 0xF000. */
ce0b6f45 36
6833a413 37#define IDM_SHOWLOG 0x0010
38#define IDM_NEWSESS 0x0020
39#define IDM_DUPSESS 0x0030
e3be8de5 40#define IDM_RESTART 0x0040
41#define IDM_RECONF 0x0050
42#define IDM_CLRSB 0x0060
43#define IDM_RESET 0x0070
70133c0e 44#define IDM_HELP 0x0140
45#define IDM_ABOUT 0x0150
46#define IDM_SAVEDSESS 0x0160
47#define IDM_COPYALL 0x0170
48#define IDM_FULLSCREEN 0x0180
63be7767 49#define IDM_PASTE 0x0190
45a22cae 50#define IDM_SPECIALSEP 0x0200
6833a413 51
125105d1 52#define IDM_SPECIAL_MIN 0x0400
53#define IDM_SPECIAL_MAX 0x0800
54
6833a413 55#define IDM_SAVED_MIN 0x1000
865e82ad 56#define IDM_SAVED_MAX 0x5000
57#define MENU_SAVED_STEP 16
1f1e3232 58/* Maximum number of sessions on saved-session submenu */
865e82ad 59#define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
374330e2 60
604fab0c 61#define WM_IGNORE_CLIP (WM_APP + 2)
62#define WM_FULLSCR_ON_MAX (WM_APP + 3)
63#define WM_AGENT_CALLBACK (WM_APP + 4)
1d432454 64#define WM_GOT_CLIPDATA (WM_APP + 6)
374330e2 65
3cf144db 66/* Needed for Chinese support and apparently not always defined. */
67#ifndef VK_PROCESSKEY
68#define VK_PROCESSKEY 0xE5
69#endif
70
3f0f9388 71/* Mouse wheel support. */
5eaf1e06 72#ifndef WM_MOUSEWHEEL
3f0f9388 73#define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
74#endif
75#ifndef WHEEL_DELTA
76#define WHEEL_DELTA 120
5eaf1e06 77#endif
78
2c02ed02 79static Mouse_Button translate_button(Mouse_Button button);
32874aea 80static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
81static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
82 unsigned char *output);
4a693cfc 83static void conftopalette(void);
26d1da7b 84static void systopalette(void);
374330e2 85static void init_palette(void);
5a73255e 86static void init_fonts(int, int);
4eeb7d09 87static void another_font(int);
88static void deinit_fonts(void);
a5ce2e9f 89static void set_input_locale(HKL);
12745e35 90static void update_savedsess_menu(void);
09644e07 91static void init_flashwindow(void);
374330e2 92
1ba99d2c 93static int is_full_screen(void);
94static void make_full_screen(void);
95static void clear_full_screen(void);
96static void flip_full_screen(void);
1d432454 97static int process_clipdata(HGLOBAL clipdata, int unicode);
1ba99d2c 98
5a73255e 99/* Window layout information */
100static void reset_window(int);
a401e5f3 101static int extra_width, extra_height;
14ce9887 102static int font_width, font_height, font_dualwidth, font_varpitch;
5a73255e 103static int offset_width, offset_height;
104static int was_zoomed = 0;
105static int prev_rows, prev_cols;
106
56f8ea28 107static int pending_netevent = 0;
108static WPARAM pend_netevent_wParam = 0;
109static LPARAM pend_netevent_lParam = 0;
110static void enact_pending_netevent(void);
f8a28d1f 111static void flash_window(int mode);
c1da5066 112static void sys_cursor_update(void);
d0f1bcb4 113static int get_fullscreen_rect(RECT * ss);
59ad2c03 114
c1da5066 115static int caret_x = -1, caret_y = -1;
116
0b4f0bc0 117static int kbd_codepage;
118
b9d7bcad 119static void *ldisc;
6b78788a 120static Backend *back;
121static void *backhandle;
b9d7bcad 122
21d2b241 123static struct unicode_data ucsdata;
95587c73 124static int must_close_session, session_closed;
68a66cc3 125static int reconfiguring = FALSE;
0b4f0bc0 126
45a22cae 127static const struct telnet_special *specials = NULL;
128static HMENU specials_menu = NULL;
129static int n_specials = 0;
125105d1 130
1d432454 131static wchar_t *clipboard_contents;
132static size_t clipboard_length;
133
39934deb 134#define TIMING_TIMER_ID 1234
135static long timing_next_time;
136
63be7767 137static struct {
138 HMENU menu;
63be7767 139} popup_menus[2];
140enum { SYSMENU, CTXMENU };
12745e35 141static HMENU savedsess_menu;
63be7767 142
4a693cfc 143Conf *conf; /* exported to windlg.c */
144
145static void conf_cache_data(void);
146int cursor_type;
147int vtmode;
3ea863a3 148
12745e35 149static struct sesslist sesslist; /* for saved-session menu */
0b4f0bc0 150
c44bf5bd 151struct agent_callback {
152 void (*callback)(void *, void *, int);
153 void *callback_ctx;
154 void *data;
155 int len;
156};
157
374330e2 158#define FONT_NORMAL 0
159#define FONT_BOLD 1
160#define FONT_UNDERLINE 2
161#define FONT_BOLDUND 3
4eeb7d09 162#define FONT_WIDE 0x04
163#define FONT_HIGH 0x08
164#define FONT_NARROW 0x10
5a73255e 165
4eeb7d09 166#define FONT_OEM 0x20
167#define FONT_OEMBOLD 0x21
168#define FONT_OEMUND 0x22
169#define FONT_OEMBOLDUND 0x23
5a73255e 170
171#define FONT_MAXNO 0x2F
4eeb7d09 172#define FONT_SHIFT 5
173static HFONT fonts[FONT_MAXNO];
71f6951d 174static LOGFONT lfont;
4eeb7d09 175static int fontflag[FONT_MAXNO];
374330e2 176static enum {
177 BOLD_COLOURS, BOLD_SHADOW, BOLD_FONT
178} bold_mode;
179static enum {
180 UND_LINE, UND_FONT
181} und_mode;
182static int descent;
183
04a8208b 184#define NCFGCOLOURS 22
cecb13f6 185#define NEXTCOLOURS 240
186#define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
187static COLORREF colours[NALLCOLOURS];
374330e2 188static HPALETTE pal;
189static LPLOGPALETTE logpal;
cecb13f6 190static RGBTRIPLE defpal[NALLCOLOURS];
374330e2 191
934c0b7a 192static HBITMAP caretbm;
193
374330e2 194static int dbltime, lasttime, lastact;
195static Mouse_Button lastbtn;
196
01c034ad 197/* this allows xterm-style mouse handling. */
198static int send_raw_mouse = 0;
199static int wheel_accumulator = 0;
200
755e0524 201static int busy_status = BUSY_NOT;
202
374330e2 203static char *window_name, *icon_name;
204
03f23ad6 205static int compose_state = 0;
206
011b0e33 207static UINT wm_mousewheel = WM_MOUSEWHEEL;
208
0965bee0 209/* Dummy routine, only required in plink. */
b9d7bcad 210void ldisc_update(void *frontend, int echo, int edit)
32874aea 211{
212}
6f34e365 213
c6ccd5c2 214char *get_ttymode(void *frontend, const char *mode)
215{
216 return term_get_ttymode(term, mode);
217}
218
e3be8de5 219static void start_backend(void)
220{
221 const char *error;
222 char msg[1024], *title;
223 char *realhost;
224 int i;
225
226 /*
227 * Select protocol. This is farmed out into a table in a
228 * separate file to enable an ssh-free variant.
229 */
4a693cfc 230 back = backend_from_proto(conf_get_int(conf, CONF_protocol));
e3be8de5 231 if (back == NULL) {
232 char *str = dupprintf("%s Internal Error", appname);
233 MessageBox(NULL, "Unsupported protocol number found",
234 str, MB_OK | MB_ICONEXCLAMATION);
235 sfree(str);
236 cleanup_exit(1);
237 }
238
4a693cfc 239 error = back->init(NULL, &backhandle, conf,
240 conf_get_str(conf, CONF_host),
241 conf_get_int(conf, CONF_port),
242 &realhost,
243 conf_get_int(conf, CONF_tcp_nodelay),
244 conf_get_int(conf, CONF_tcp_keepalives));
e3be8de5 245 back->provide_logctx(backhandle, logctx);
246 if (error) {
247 char *str = dupprintf("%s Error", appname);
248 sprintf(msg, "Unable to open connection to\n"
4a693cfc 249 "%.800s\n" "%s", conf_dest(conf), error);
e3be8de5 250 MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
251 sfree(str);
252 exit(0);
253 }
254 window_name = icon_name = NULL;
4a693cfc 255 title = conf_get_str(conf, CONF_wintitle);
256 if (!*title) {
e3be8de5 257 sprintf(msg, "%s - %s", realhost, appname);
258 title = msg;
259 }
260 sfree(realhost);
261 set_title(NULL, title);
262 set_icon(NULL, title);
263
264 /*
265 * Connect the terminal to the backend for resize purposes.
266 */
267 term_provide_resize_fn(term, back->size, backhandle);
268
269 /*
270 * Set up a line discipline.
271 */
4a693cfc 272 ldisc = ldisc_create(conf, term, back, backhandle, NULL);
e3be8de5 273
274 /*
275 * Destroy the Restart Session menu item. (This will return
276 * failure if it's already absent, as it will be the very first
277 * time we call this function. We ignore that, because as long
278 * as the menu item ends up not being there, we don't care
279 * whether it was us who removed it or not!)
280 */
281 for (i = 0; i < lenof(popup_menus); i++) {
282 DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
283 }
284
8eec6d96 285 must_close_session = FALSE;
e3be8de5 286 session_closed = FALSE;
287}
288
289static void close_session(void)
290{
291 char morestuff[100];
292 int i;
293
294 session_closed = TRUE;
295 sprintf(morestuff, "%.70s (inactive)", appname);
296 set_icon(NULL, morestuff);
297 set_title(NULL, morestuff);
298
299 if (ldisc) {
300 ldisc_free(ldisc);
301 ldisc = NULL;
302 }
303 if (back) {
304 back->free(backhandle);
305 backhandle = NULL;
306 back = NULL;
c1190260 307 term_provide_resize_fn(term, NULL, NULL);
e3be8de5 308 update_specials_menu(NULL);
309 }
310
311 /*
312 * Show the Restart Session menu item. Do a precautionary
313 * delete first to ensure we never end up with more than one.
314 */
315 for (i = 0; i < lenof(popup_menus); i++) {
316 DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
317 InsertMenu(popup_menus[i].menu, IDM_DUPSESS, MF_BYCOMMAND | MF_ENABLED,
318 IDM_RESTART, "&Restart Session");
319 }
4f1aa9a9 320
321 /*
322 * Unset the 'must_close_session' flag, or else we'll come
323 * straight back here the next time we go round the main message
324 * loop - which, worse still, will be immediately (without
325 * blocking) because we've just triggered a WM_SETTEXT by the
326 * window title change above.
327 */
328 must_close_session = FALSE;
e3be8de5 329}
330
32874aea 331int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
332{
374330e2 333 WNDCLASS wndclass;
334 MSG msg;
073e9f42 335 HRESULT hr;
374330e2 336 int guess_width, guess_height;
337
8c3cd914 338 hinst = inst;
28339579 339 hwnd = NULL;
67779be7 340 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
73251d5d 341
8df7a775 342 sk_init();
374330e2 343
344 InitCommonControls();
345
301b66db 346 /* Ensure a Maximize setting in Explorer doesn't maximise the
347 * config box. */
348 defuse_showwindow();
349
4c48c989 350 if (!init_winver())
88485e4d 351 {
4c48c989 352 char *str = dupprintf("%s Fatal Error", appname);
353 MessageBox(NULL, "Windows refuses to report a version",
354 str, MB_OK | MB_ICONEXCLAMATION);
355 sfree(str);
356 return 1;
88485e4d 357 }
358
374330e2 359 /*
011b0e33 360 * If we're running a version of Windows that doesn't support
361 * WM_MOUSEWHEEL, find out what message number we should be
362 * using instead.
363 */
364 if (osVersion.dwMajorVersion < 4 ||
365 (osVersion.dwMajorVersion == 4 &&
366 osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT))
367 wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
368
cb2708d3 369 init_help();
70133c0e 370
09644e07 371 init_flashwindow();
372
4a693cfc 373 conf = conf_new();
374
70133c0e 375 /*
073e9f42 376 * Initialize COM.
377 */
378 hr = CoInitialize(NULL);
379 if (hr != S_OK && hr != S_FALSE) {
380 char *str = dupprintf("%s Fatal Error", appname);
381 MessageBox(NULL, "Failed to initialize COM subsystem",
382 str, MB_OK | MB_ICONEXCLAMATION);
383 sfree(str);
384 return 1;
385 }
386
387 /*
374330e2 388 * Process the command line.
389 */
390 {
391 char *p;
c0a81592 392 int got_host = 0;
df7c7e52 393 /* By default, we bring up the config dialog, rather than launching
394 * a session. This gets set to TRUE if something happens to change
395 * that (e.g., a hostname is specified on the command-line). */
396 int allow_launch = FALSE;
374330e2 397
ffa79828 398 default_protocol = be_default_protocol;
399 /* Find the appropriate default port. */
400 {
9e164d82 401 Backend *b = backend_from_proto(default_protocol);
0d598aa7 402 default_port = 0; /* illegal */
9e164d82 403 if (b)
404 default_port = b->default_port;
ffa79828 405 }
4a693cfc 406 conf_set_int(conf, CONF_logtype, LGTYP_NONE);
e277c42d 407
4a693cfc 408 do_defaults(NULL, conf);
374330e2 409
410 p = cmdline;
374330e2 411
412 /*
c0a81592 413 * Process a couple of command-line options which are more
073e9f42 414 * easily dealt with before the line is broken up into words.
415 * These are the old-fashioned but convenient @sessionname and
416 * the internal-use-only &sharedmemoryhandle, neither of which
417 * are combined with anything else.
e277c42d 418 */
c0a81592 419 while (*p && isspace(*p))
e277c42d 420 p++;
374330e2 421 if (*p == '@') {
073e9f42 422 /*
423 * An initial @ means that the whole of the rest of the
424 * command line should be treated as the name of a saved
425 * session, with _no quoting or escaping_. This makes it a
426 * very convenient means of automated saved-session
427 * launching, via IDM_SAVEDSESS or Windows 7 jump lists.
428 */
f317611e 429 int i = strlen(p);
32874aea 430 while (i > 1 && isspace(p[i - 1]))
f317611e 431 i--;
432 p[i] = '\0';
4a693cfc 433 do_defaults(p + 1, conf);
434 if (!conf_launchable(conf) && !do_config()) {
7440fd44 435 cleanup_exit(0);
374330e2 436 }
df7c7e52 437 allow_launch = TRUE; /* allow it to be launched directly */
374330e2 438 } else if (*p == '&') {
439 /*
440 * An initial & means we've been given a command line
441 * containing the hex value of a HANDLE for a file
4a693cfc 442 * mapping object, which we must then interpret as a
443 * serialised Conf.
374330e2 444 */
445 HANDLE filemap;
4a693cfc 446 void *cp;
447 unsigned cpsize;
448 if (sscanf(p + 1, "%p:%u", &filemap, &cpsize) == 1 &&
374330e2 449 (cp = MapViewOfFile(filemap, FILE_MAP_READ,
4a693cfc 450 0, 0, cpsize)) != NULL) {
451 conf_deserialise(conf, cp, cpsize);
374330e2 452 UnmapViewOfFile(cp);
453 CloseHandle(filemap);
454 } else if (!do_config()) {
7440fd44 455 cleanup_exit(0);
374330e2 456 }
df7c7e52 457 allow_launch = TRUE;
c0a81592 458 } else {
32874aea 459 /*
c0a81592 460 * Otherwise, break up the command line and deal with
461 * it sensibly.
32874aea 462 */
c0a81592 463 int argc, i;
464 char **argv;
465
d3a1a808 466 split_into_argv(cmdline, &argc, &argv, NULL);
c0a81592 467
468 for (i = 0; i < argc; i++) {
469 char *p = argv[i];
470 int ret;
471
5555d393 472 ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
4a693cfc 473 1, conf);
c0a81592 474 if (ret == -2) {
475 cmdline_error("option \"%s\" requires an argument", p);
476 } else if (ret == 2) {
477 i++; /* skip next argument */
478 } else if (ret == 1) {
479 continue; /* nothing further needs doing */
a6551274 480 } else if (!strcmp(p, "-cleanup") ||
481 !strcmp(p, "-cleanup-during-uninstall")) {
c0a81592 482 /*
483 * `putty -cleanup'. Remove all registry
484 * entries associated with PuTTY, and also find
485 * and delete the random seed file.
486 */
f6f450e2 487 char *s1, *s2;
a6551274 488 /* Are we being invoked from an uninstaller? */
489 if (!strcmp(p, "-cleanup-during-uninstall")) {
490 s1 = dupprintf("Remove saved sessions and random seed file?\n"
491 "\n"
492 "If you hit Yes, ALL Registry entries associated\n"
493 "with %s will be removed, as well as the\n"
494 "random seed file. THIS PROCESS WILL\n"
495 "DESTROY YOUR SAVED SESSIONS.\n"
496 "(This only affects the currently logged-in user.)\n"
497 "\n"
498 "If you hit No, uninstallation will proceed, but\n"
499 "saved sessions etc will be left on the machine.",
500 appname);
501 s2 = dupprintf("%s Uninstallation", appname);
502 } else {
503 s1 = dupprintf("This procedure will remove ALL Registry entries\n"
504 "associated with %s, and will also remove\n"
505 "the random seed file. (This only affects the\n"
506 "currently logged-in user.)\n"
507 "\n"
508 "THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
509 "Are you really sure you want to continue?",
510 appname);
511 s2 = dupprintf("%s Warning", appname);
512 }
513 if (message_box(s1, s2,
514 MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2,
28339579 515 HELPCTXID(option_cleanup)) == IDYES) {
c0a81592 516 cleanup_all();
517 }
f6f450e2 518 sfree(s1);
519 sfree(s2);
c0a81592 520 exit(0);
2285d016 521 } else if (!strcmp(p, "-pgpfp")) {
522 pgp_fingerprints();
523 exit(1);
c0a81592 524 } else if (*p != '-') {
525 char *q = p;
526 if (got_host) {
527 /*
528 * If we already have a host name, treat
529 * this argument as a port number. NB we
530 * have to treat this as a saved -P
531 * argument, so that it will be deferred
532 * until it's a good moment to run it.
533 */
4a693cfc 534 int ret = cmdline_process_param("-P", p, 1, conf);
c0a81592 535 assert(ret == 2);
536 } else if (!strncmp(q, "telnet:", 7)) {
537 /*
538 * If the hostname starts with "telnet:",
539 * set the protocol to Telnet and process
540 * the string as a Telnet URL.
541 */
542 char c;
543
544 q += 7;
545 if (q[0] == '/' && q[1] == '/')
546 q += 2;
4a693cfc 547 conf_set_int(conf, CONF_protocol, PROT_TELNET);
c0a81592 548 p = q;
549 while (*p && *p != ':' && *p != '/')
550 p++;
551 c = *p;
552 if (*p)
553 *p++ = '\0';
554 if (c == ':')
4a693cfc 555 conf_set_int(conf, CONF_port, atoi(p));
c0a81592 556 else
4a693cfc 557 conf_set_int(conf, CONF_port, -1);
558 conf_set_str(conf, CONF_host, q);
c0a81592 559 got_host = 1;
560 } else {
561 /*
562 * Otherwise, treat this argument as a host
563 * name.
564 */
565 while (*p && !isspace(*p))
566 p++;
567 if (*p)
568 *p++ = '\0';
4a693cfc 569 conf_set_str(conf, CONF_host, q);
c0a81592 570 got_host = 1;
571 }
86256dc6 572 } else {
573 cmdline_error("unknown option \"%s\"", p);
c0a81592 574 }
374330e2 575 }
576 }
13eafebf 577
4a693cfc 578 cmdline_run_saved(conf);
c0a81592 579
df7c7e52 580 if (loaded_session || got_host)
581 allow_launch = TRUE;
582
4a693cfc 583 if ((!allow_launch || !conf_launchable(conf)) && !do_config()) {
7440fd44 584 cleanup_exit(0);
c0a81592 585 }
586
381b1876 587 /*
4a693cfc 588 * Muck about with the hostname in various ways.
381b1876 589 */
590 {
4a693cfc 591 char *hostbuf = dupstr(conf_get_str(conf, CONF_host));
592 char *host = hostbuf;
593 char *p, *q;
381b1876 594
4a693cfc 595 /*
596 * Trim leading whitespace.
597 */
598 host += strspn(host, " \t");
599
600 /*
601 * See if host is of the form user@host, and separate
602 * out the username if so.
603 */
604 if (host[0] != '\0') {
605 char *atsign = strrchr(host, '@');
606 if (atsign) {
607 *atsign = '\0';
608 conf_set_str(conf, CONF_username, host);
609 host = atsign + 1;
13eafebf 610 }
13eafebf 611 }
503ddb97 612
4a693cfc 613 /*
614 * Trim off a colon suffix if it's there.
615 */
616 host[strcspn(host, ":")] = '\0';
05581745 617
4a693cfc 618 /*
619 * Remove any remaining whitespace.
620 */
621 p = hostbuf;
622 q = host;
623 while (*q) {
624 if (*q != ' ' && *q != '\t')
625 *p++ = *q;
626 q++;
05581745 627 }
4a693cfc 628 *p = '\0';
cae0c023 629
4a693cfc 630 conf_set_str(conf, CONF_host, hostbuf);
631 sfree(hostbuf);
cae0c023 632 }
374330e2 633 }
634
374330e2 635 if (!prev) {
32874aea 636 wndclass.style = 0;
637 wndclass.lpfnWndProc = WndProc;
638 wndclass.cbClsExtra = 0;
639 wndclass.cbWndExtra = 0;
640 wndclass.hInstance = inst;
641 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
642 wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
5a73255e 643 wndclass.hbrBackground = NULL;
32874aea 644 wndclass.lpszMenuName = NULL;
374330e2 645 wndclass.lpszClassName = appname;
646
32874aea 647 RegisterClass(&wndclass);
374330e2 648 }
649
21d2b241 650 memset(&ucsdata, 0, sizeof(ucsdata));
651
4a693cfc 652 conf_cache_data();
653
654 conftopalette();
374330e2 655
656 /*
657 * Guess some defaults for the window size. This all gets
658 * updated later, so we don't really care too much. However, we
659 * do want the font width/height guesses to correspond to a
660 * large font rather than a small one...
661 */
32874aea 662
374330e2 663 font_width = 10;
664 font_height = 20;
665 extra_width = 25;
666 extra_height = 28;
4a693cfc 667 guess_width = extra_width + font_width * conf_get_int(conf, CONF_width);
668 guess_height = extra_height + font_height*conf_get_int(conf, CONF_height);
374330e2 669 {
670 RECT r;
f43e4b67 671 get_fullscreen_rect(&r);
374330e2 672 if (guess_width > r.right - r.left)
673 guess_width = r.right - r.left;
674 if (guess_height > r.bottom - r.top)
675 guess_height = r.bottom - r.top;
676 }
677
c9def1b8 678 {
32874aea 679 int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
680 int exwinmode = 0;
4a693cfc 681 if (!conf_get_int(conf, CONF_scrollbar))
32874aea 682 winmode &= ~(WS_VSCROLL);
4a693cfc 683 if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED)
32874aea 684 winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
4a693cfc 685 if (conf_get_int(conf, CONF_alwaysontop))
32874aea 686 exwinmode |= WS_EX_TOPMOST;
4a693cfc 687 if (conf_get_int(conf, CONF_sunken_edge))
32874aea 688 exwinmode |= WS_EX_CLIENTEDGE;
689 hwnd = CreateWindowEx(exwinmode, appname, appname,
690 winmode, CW_USEDEFAULT, CW_USEDEFAULT,
691 guess_width, guess_height,
692 NULL, NULL, inst, NULL);
693 }
374330e2 694
695 /*
c52dd4c9 696 * Initialise the terminal. (We have to do this _after_
697 * creating the window, since the terminal is the first thing
698 * which will call schedule_timer(), which will in turn call
5ba93094 699 * timer_change_notify() which will expect hwnd to exist.)
c52dd4c9 700 */
4a693cfc 701 term = term_init(conf, &ucsdata, NULL);
702 logctx = log_init(NULL, conf);
c52dd4c9 703 term_provide_logctx(term, logctx);
4a693cfc 704 term_size(term, conf_get_int(conf, CONF_height),
705 conf_get_int(conf, CONF_width),
706 conf_get_int(conf, CONF_savelines));
c52dd4c9 707
708 /*
374330e2 709 * Initialise the fonts, simultaneously correcting the guesses
710 * for font_{width,height}.
711 */
5a73255e 712 init_fonts(0,0);
374330e2 713
714 /*
715 * Correct the guesses for extra_{width,height}.
716 */
717 {
718 RECT cr, wr;
32874aea 719 GetWindowRect(hwnd, &wr);
720 GetClientRect(hwnd, &cr);
4a693cfc 721 offset_width = offset_height = conf_get_int(conf, CONF_window_border);
5a73255e 722 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
723 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
374330e2 724 }
725
726 /*
727 * Resize the window, now we know what size we _really_ want it
728 * to be.
729 */
887035a5 730 guess_width = extra_width + font_width * term->cols;
731 guess_height = extra_height + font_height * term->rows;
32874aea 732 SetWindowPos(hwnd, NULL, 0, 0, guess_width, guess_height,
733 SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
374330e2 734
735 /*
934c0b7a 736 * Set up a caret bitmap, with no content.
737 */
738 {
32874aea 739 char *bits;
740 int size = (font_width + 15) / 16 * 2 * font_height;
3d88e64d 741 bits = snewn(size, char);
32874aea 742 memset(bits, 0, size);
743 caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
744 sfree(bits);
934c0b7a 745 }
5b7ce734 746 CreateCaret(hwnd, caretbm, font_width, font_height);
934c0b7a 747
748 /*
374330e2 749 * Initialise the scroll bar.
750 */
751 {
752 SCROLLINFO si;
753
754 si.cbSize = sizeof(si);
c9def1b8 755 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
374330e2 756 si.nMin = 0;
887035a5 757 si.nMax = term->rows - 1;
758 si.nPage = term->rows;
374330e2 759 si.nPos = 0;
32874aea 760 SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
374330e2 761 }
762
374330e2 763 /*
374330e2 764 * Prepare the mouse handler.
765 */
766 lastact = MA_NOTHING;
01c034ad 767 lastbtn = MBT_NOTHING;
374330e2 768 dbltime = GetDoubleClickTime();
769
770 /*
771 * Set up the session-control options on the system menu.
772 */
773 {
12745e35 774 HMENU m;
775 int j;
f6f450e2 776 char *str;
374330e2 777
63be7767 778 popup_menus[SYSMENU].menu = GetSystemMenu(hwnd, FALSE);
779 popup_menus[CTXMENU].menu = CreatePopupMenu();
780 AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_PASTE, "&Paste");
781
12745e35 782 savedsess_menu = CreateMenu();
0b4f0bc0 783 get_sesslist(&sesslist, TRUE);
12745e35 784 update_savedsess_menu();
63be7767 785
786 for (j = 0; j < lenof(popup_menus); j++) {
787 m = popup_menus[j].menu;
788
789 AppendMenu(m, MF_SEPARATOR, 0, 0);
63be7767 790 AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
791 AppendMenu(m, MF_SEPARATOR, 0, 0);
792 AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
793 AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
12745e35 794 AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) savedsess_menu,
795 "Sa&ved Sessions");
63be7767 796 AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
797 AppendMenu(m, MF_SEPARATOR, 0, 0);
798 AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
799 AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
800 AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
801 AppendMenu(m, MF_SEPARATOR, 0, 0);
4a693cfc 802 AppendMenu(m, (conf_get_int(conf, CONF_resize_action)
803 == RESIZE_DISABLED) ? MF_GRAYED : MF_ENABLED,
804 IDM_FULLSCREEN, "&Full Screen");
63be7767 805 AppendMenu(m, MF_SEPARATOR, 0, 0);
cb2708d3 806 if (has_help())
63be7767 807 AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
808 str = dupprintf("&About %s", appname);
809 AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
810 sfree(str);
811 }
374330e2 812 }
813
533b1743 814 start_backend();
125105d1 815
374330e2 816 /*
a5ce2e9f 817 * Set up the initial input locale.
818 */
819 set_input_locale(GetKeyboardLayout(0));
820
821 /*
00db133f 822 * Finally show the window!
e1c8e0ed 823 */
00db133f 824 ShowWindow(hwnd, show);
825 SetForegroundWindow(hwnd);
e1c8e0ed 826
827 /*
374330e2 828 * Set the palette up.
829 */
830 pal = NULL;
831 logpal = NULL;
832 init_palette();
833
1cff1320 834 term_set_focus(term, GetForegroundWindow() == hwnd);
32874aea 835 UpdateWindow(hwnd);
374330e2 836
34292b1d 837 while (1) {
838 HANDLE *handles;
839 int nhandles, n;
840
841 handles = handle_get_events(&nhandles);
842
843 n = MsgWaitForMultipleObjects(nhandles, handles, FALSE, INFINITE,
844 QS_ALLINPUT);
845
846 if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
847 handle_got_event(handles[n - WAIT_OBJECT_0]);
848 sfree(handles);
95587c73 849 if (must_close_session)
850 close_session();
45d56852 851 } else
852 sfree(handles);
34292b1d 853
b08600c4 854 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
34292b1d 855 if (msg.message == WM_QUIT)
856 goto finished; /* two-level break */
857
32874aea 858 if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
859 DispatchMessage(&msg);
c9def1b8 860 /* Send the paste buffer if there's anything to send */
887035a5 861 term_paste(term);
59ad2c03 862 /* If there's nothing new in the queue then we can do everything
863 * we've delayed, reading the socket, writing, and repainting
864 * the window.
865 */
95587c73 866 if (must_close_session)
867 close_session();
b08600c4 868 }
0ed2f48e 869
34292b1d 870 /* The messages seem unreliable; especially if we're being tricky */
871 term_set_focus(term, GetForegroundWindow() == hwnd);
32874aea 872
56f8ea28 873 if (pending_netevent)
874 enact_pending_netevent();
875
34292b1d 876 net_pending_errors();
374330e2 877 }
878
34292b1d 879 finished:
c0d36a72 880 cleanup_exit(msg.wParam); /* this doesn't return... */
881 return msg.wParam; /* ... but optimiser doesn't know */
93b581bd 882}
883
884/*
885 * Clean up and exit.
886 */
887void cleanup_exit(int code)
888{
374330e2 889 /*
890 * Clean up.
891 */
4eeb7d09 892 deinit_fonts();
374330e2 893 sfree(logpal);
894 if (pal)
895 DeleteObject(pal);
93b581bd 896 sk_cleanup();
374330e2 897
4a693cfc 898 if (conf_get_int(conf, CONF_protocol) == PROT_SSH) {
374330e2 899 random_save_seed();
8f203108 900#ifdef MSCRYPTOAPI
901 crypto_wrapup();
902#endif
903 }
cb2708d3 904 shutdown_help();
374330e2 905
073e9f42 906 /* Clean up COM. */
907 CoUninitialize();
908
93b581bd 909 exit(code);
374330e2 910}
911
912/*
8df7a775 913 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
914 */
32874aea 915char *do_select(SOCKET skt, int startup)
916{
8df7a775 917 int msg, events;
918 if (startup) {
919 msg = WM_NETEVENT;
3ad9d396 920 events = (FD_CONNECT | FD_READ | FD_WRITE |
921 FD_OOB | FD_CLOSE | FD_ACCEPT);
8df7a775 922 } else {
923 msg = events = 0;
924 }
925 if (!hwnd)
926 return "do_select(): internal error (hwnd==NULL)";
7440fd44 927 if (p_WSAAsyncSelect(skt, hwnd, msg, events) == SOCKET_ERROR) {
928 switch (p_WSAGetLastError()) {
32874aea 929 case WSAENETDOWN:
930 return "Network is down";
931 default:
932 return "WSAAsyncSelect(): unknown error";
933 }
8df7a775 934 }
935 return NULL;
936}
937
938/*
12745e35 939 * Refresh the saved-session submenu from `sesslist'.
940 */
941static void update_savedsess_menu(void)
942{
943 int i;
944 while (DeleteMenu(savedsess_menu, 0, MF_BYPOSITION)) ;
945 /* skip sesslist.sessions[0] == Default Settings */
946 for (i = 1;
947 i < ((sesslist.nsessions <= MENU_SAVED_MAX+1) ? sesslist.nsessions
948 : MENU_SAVED_MAX+1);
949 i++)
950 AppendMenu(savedsess_menu, MF_ENABLED,
951 IDM_SAVED_MIN + (i-1)*MENU_SAVED_STEP,
952 sesslist.sessions[i]);
f43e4b67 953 if (sesslist.nsessions <= 1)
954 AppendMenu(savedsess_menu, MF_GRAYED, IDM_SAVED_MIN, "(No sessions)");
12745e35 955}
956
957/*
125105d1 958 * Update the Special Commands submenu.
959 */
960void update_specials_menu(void *frontend)
961{
45a22cae 962 HMENU new_menu;
63be7767 963 int i, j;
125105d1 964
e3be8de5 965 if (back)
966 specials = back->get_specials(backhandle);
967 else
968 specials = NULL;
969
125105d1 970 if (specials) {
6f2d0cde 971 /* We can't use Windows to provide a stack for submenus, so
972 * here's a lame "stack" that will do for now. */
973 HMENU saved_menu = NULL;
974 int nesting = 1;
45a22cae 975 new_menu = CreatePopupMenu();
6f2d0cde 976 for (i = 0; nesting > 0; i++) {
125105d1 977 assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
6f2d0cde 978 switch (specials[i].code) {
979 case TS_SEP:
45a22cae 980 AppendMenu(new_menu, MF_SEPARATOR, 0, 0);
6f2d0cde 981 break;
982 case TS_SUBMENU:
983 assert(nesting < 2);
984 nesting++;
45a22cae 985 saved_menu = new_menu; /* XXX lame stacking */
986 new_menu = CreatePopupMenu();
6f2d0cde 987 AppendMenu(saved_menu, MF_POPUP | MF_ENABLED,
45a22cae 988 (UINT) new_menu, specials[i].name);
6f2d0cde 989 break;
990 case TS_EXITMENU:
991 nesting--;
992 if (nesting) {
45a22cae 993 new_menu = saved_menu; /* XXX lame stacking */
6f2d0cde 994 saved_menu = NULL;
995 }
996 break;
997 default:
45a22cae 998 AppendMenu(new_menu, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
125105d1 999 specials[i].name);
6f2d0cde 1000 break;
1001 }
125105d1 1002 }
6f2d0cde 1003 /* Squirrel the highest special. */
1004 n_specials = i - 1;
1005 } else {
45a22cae 1006 new_menu = NULL;
6f2d0cde 1007 n_specials = 0;
1008 }
e3be8de5 1009
1010 for (j = 0; j < lenof(popup_menus); j++) {
45a22cae 1011 if (specials_menu) {
6f2d0cde 1012 /* XXX does this free up all submenus? */
62ddb51e 1013 DeleteMenu(popup_menus[j].menu, (UINT)specials_menu, MF_BYCOMMAND);
45a22cae 1014 DeleteMenu(popup_menus[j].menu, IDM_SPECIALSEP, MF_BYCOMMAND);
e3be8de5 1015 }
47d1dab7 1016 if (new_menu) {
45a22cae 1017 InsertMenu(popup_menus[j].menu, IDM_SHOWLOG,
1018 MF_BYCOMMAND | MF_POPUP | MF_ENABLED,
1019 (UINT) new_menu, "S&pecial Command");
1020 InsertMenu(popup_menus[j].menu, IDM_SHOWLOG,
1021 MF_BYCOMMAND | MF_SEPARATOR, IDM_SPECIALSEP, 0);
63be7767 1022 }
125105d1 1023 }
47d1dab7 1024 specials_menu = new_menu;
125105d1 1025}
1026
755e0524 1027static void update_mouse_pointer(void)
1028{
1029 LPTSTR curstype;
1030 int force_visible = FALSE;
1031 static int forced_visible = FALSE;
1032 switch (busy_status) {
1033 case BUSY_NOT:
1034 if (send_raw_mouse)
1035 curstype = IDC_ARROW;
1036 else
1037 curstype = IDC_IBEAM;
1038 break;
1039 case BUSY_WAITING:
1040 curstype = IDC_APPSTARTING; /* this may be an abuse */
1041 force_visible = TRUE;
1042 break;
1043 case BUSY_CPU:
1044 curstype = IDC_WAIT;
1045 force_visible = TRUE;
1046 break;
1047 default:
1048 assert(0);
1049 }
1050 {
1051 HCURSOR cursor = LoadCursor(NULL, curstype);
158cb9b1 1052 SetClassLongPtr(hwnd, GCLP_HCURSOR, (LONG_PTR)cursor);
755e0524 1053 SetCursor(cursor); /* force redraw of cursor at current posn */
1054 }
1055 if (force_visible != forced_visible) {
1056 /* We want some cursor shapes to be visible always.
1057 * Along with show_mouseptr(), this manages the ShowCursor()
1058 * counter such that if we switch back to a non-force_visible
1059 * cursor, the previous visibility state is restored. */
1060 ShowCursor(force_visible);
1061 forced_visible = force_visible;
1062 }
1063}
1064
1065void set_busy_status(void *frontend, int status)
1066{
1067 busy_status = status;
1068 update_mouse_pointer();
1069}
1070
125105d1 1071/*
01c034ad 1072 * set or clear the "raw mouse message" mode
1073 */
a8327734 1074void set_raw_mouse_mode(void *frontend, int activate)
01c034ad 1075{
4a693cfc 1076 activate = activate && !conf_get_int(conf, CONF_no_mouse_rep);
01c034ad 1077 send_raw_mouse = activate;
755e0524 1078 update_mouse_pointer();
01c034ad 1079}
1080
1081/*
8d5de777 1082 * Print a message box and close the connection.
1083 */
a8327734 1084void connection_fatal(void *frontend, char *fmt, ...)
32874aea 1085{
8d5de777 1086 va_list ap;
971bcc0a 1087 char *stuff, morestuff[100];
8d5de777 1088
1089 va_start(ap, fmt);
971bcc0a 1090 stuff = dupvprintf(fmt, ap);
8d5de777 1091 va_end(ap);
f6f450e2 1092 sprintf(morestuff, "%.70s Fatal Error", appname);
1093 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
971bcc0a 1094 sfree(stuff);
1095
4a693cfc 1096 if (conf_get_int(conf, CONF_close_on_exit) == FORCE_ON)
32874aea 1097 PostQuitMessage(1);
8d5de777 1098 else {
95587c73 1099 must_close_session = TRUE;
8d5de777 1100 }
1101}
1102
1103/*
c0a81592 1104 * Report an error at the command-line parsing stage.
1105 */
1106void cmdline_error(char *fmt, ...)
1107{
1108 va_list ap;
971bcc0a 1109 char *stuff, morestuff[100];
c0a81592 1110
1111 va_start(ap, fmt);
971bcc0a 1112 stuff = dupvprintf(fmt, ap);
c0a81592 1113 va_end(ap);
f6f450e2 1114 sprintf(morestuff, "%.70s Command Line Error", appname);
1115 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
971bcc0a 1116 sfree(stuff);
c0a81592 1117 exit(1);
1118}
1119
1120/*
59ad2c03 1121 * Actually do the job requested by a WM_NETEVENT
1122 */
56f8ea28 1123static void enact_pending_netevent(void)
32874aea 1124{
9dde0b46 1125 static int reentering = 0;
8df7a775 1126 extern int select_result(WPARAM, LPARAM);
9dde0b46 1127
1128 if (reentering)
32874aea 1129 return; /* don't unpend the pending */
9dde0b46 1130
56f8ea28 1131 pending_netevent = FALSE;
1132
9dde0b46 1133 reentering = 1;
56f8ea28 1134 select_result(pend_netevent_wParam, pend_netevent_lParam);
9dde0b46 1135 reentering = 0;
59ad2c03 1136}
1137
1138/*
374330e2 1139 * Copy the colour palette from the configuration data into defpal.
1140 * This is non-trivial because the colour indices are different.
1141 */
4a693cfc 1142static void conftopalette(void)
32874aea 1143{
374330e2 1144 int i;
1145 static const int ww[] = {
cecb13f6 1146 256, 257, 258, 259, 260, 261,
1147 0, 8, 1, 9, 2, 10, 3, 11,
1148 4, 12, 5, 13, 6, 14, 7, 15
374330e2 1149 };
1150
cecb13f6 1151 for (i = 0; i < 22; i++) {
374330e2 1152 int w = ww[i];
4a693cfc 1153 defpal[w].rgbtRed = conf_get_int_int(conf, CONF_colours, i*3+0);
1154 defpal[w].rgbtGreen = conf_get_int_int(conf, CONF_colours, i*3+1);
1155 defpal[w].rgbtBlue = conf_get_int_int(conf, CONF_colours, i*3+2);
cecb13f6 1156 }
1157 for (i = 0; i < NEXTCOLOURS; i++) {
1158 if (i < 216) {
1159 int r = i / 36, g = (i / 6) % 6, b = i % 6;
8098d585 1160 defpal[i+16].rgbtRed = r ? r * 40 + 55 : 0;
1161 defpal[i+16].rgbtGreen = g ? g * 40 + 55 : 0;
1162 defpal[i+16].rgbtBlue = b ? b * 40 + 55 : 0;
cecb13f6 1163 } else {
1164 int shade = i - 216;
8098d585 1165 shade = shade * 10 + 8;
cecb13f6 1166 defpal[i+16].rgbtRed = defpal[i+16].rgbtGreen =
1167 defpal[i+16].rgbtBlue = shade;
1168 }
374330e2 1169 }
26d1da7b 1170
1171 /* Override with system colours if appropriate */
4a693cfc 1172 if (conf_get_int(conf, CONF_system_colour))
26d1da7b 1173 systopalette();
1174}
1175
1176/*
1177 * Override bit of defpal with colours from the system.
1178 * (NB that this takes a copy the system colours at the time this is called,
1179 * so subsequent colour scheme changes don't take effect. To fix that we'd
1180 * probably want to be using GetSysColorBrush() and the like.)
1181 */
1182static void systopalette(void)
1183{
1184 int i;
1185 static const struct { int nIndex; int norm; int bold; } or[] =
1186 {
cecb13f6 1187 { COLOR_WINDOWTEXT, 256, 257 }, /* Default Foreground */
1188 { COLOR_WINDOW, 258, 259 }, /* Default Background */
1189 { COLOR_HIGHLIGHTTEXT, 260, 260 }, /* Cursor Text */
1190 { COLOR_HIGHLIGHT, 261, 261 }, /* Cursor Colour */
26d1da7b 1191 };
1192
1193 for (i = 0; i < (sizeof(or)/sizeof(or[0])); i++) {
1194 COLORREF colour = GetSysColor(or[i].nIndex);
1195 defpal[or[i].norm].rgbtRed =
1196 defpal[or[i].bold].rgbtRed = GetRValue(colour);
1197 defpal[or[i].norm].rgbtGreen =
1198 defpal[or[i].bold].rgbtGreen = GetGValue(colour);
1199 defpal[or[i].norm].rgbtBlue =
1200 defpal[or[i].bold].rgbtBlue = GetBValue(colour);
1201 }
374330e2 1202}
1203
1204/*
1205 * Set up the colour palette.
1206 */
32874aea 1207static void init_palette(void)
1208{
374330e2 1209 int i;
32874aea 1210 HDC hdc = GetDC(hwnd);
374330e2 1211 if (hdc) {
4a693cfc 1212 if (conf_get_int(conf, CONF_try_palette) &&
1213 GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
3d88e64d 1214 /*
1215 * This is a genuine case where we must use smalloc
1216 * because the snew macros can't cope.
1217 */
374330e2 1218 logpal = smalloc(sizeof(*logpal)
1219 - sizeof(logpal->palPalEntry)
cecb13f6 1220 + NALLCOLOURS * sizeof(PALETTEENTRY));
374330e2 1221 logpal->palVersion = 0x300;
cecb13f6 1222 logpal->palNumEntries = NALLCOLOURS;
1223 for (i = 0; i < NALLCOLOURS; i++) {
374330e2 1224 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
1225 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
1226 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
1227 logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
1228 }
32874aea 1229 pal = CreatePalette(logpal);
374330e2 1230 if (pal) {
32874aea 1231 SelectPalette(hdc, pal, FALSE);
1232 RealizePalette(hdc);
1233 SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), FALSE);
374330e2 1234 }
1235 }
32874aea 1236 ReleaseDC(hwnd, hdc);
374330e2 1237 }
1238 if (pal)
cecb13f6 1239 for (i = 0; i < NALLCOLOURS; i++)
374330e2 1240 colours[i] = PALETTERGB(defpal[i].rgbtRed,
1241 defpal[i].rgbtGreen,
1242 defpal[i].rgbtBlue);
1243 else
cecb13f6 1244 for (i = 0; i < NALLCOLOURS; i++)
374330e2 1245 colours[i] = RGB(defpal[i].rgbtRed,
32874aea 1246 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
374330e2 1247}
1248
1249/*
f0fccd51 1250 * This is a wrapper to ExtTextOut() to force Windows to display
1251 * the precise glyphs we give it. Otherwise it would do its own
1252 * bidi and Arabic shaping, and we would end up uncertain which
1253 * characters it had put where.
1254 */
1255static void exact_textout(HDC hdc, int x, int y, CONST RECT *lprc,
1256 unsigned short *lpString, UINT cbCount,
c6958dfe 1257 CONST INT *lpDx, int opaque)
f0fccd51 1258{
b7625258 1259#ifdef __LCC__
1260 /*
1261 * The LCC include files apparently don't supply the
1262 * GCP_RESULTSW type, but we can make do with GCP_RESULTS
1263 * proper: the differences aren't important to us (the only
1264 * variable-width string parameter is one we don't use anyway).
1265 */
1266 GCP_RESULTS gcpr;
1267#else
f0fccd51 1268 GCP_RESULTSW gcpr;
b7625258 1269#endif
f0fccd51 1270 char *buffer = snewn(cbCount*2+2, char);
1271 char *classbuffer = snewn(cbCount, char);
1272 memset(&gcpr, 0, sizeof(gcpr));
1273 memset(buffer, 0, cbCount*2+2);
1274 memset(classbuffer, GCPCLASS_NEUTRAL, cbCount);
1275
1276 gcpr.lStructSize = sizeof(gcpr);
1277 gcpr.lpGlyphs = (void *)buffer;
44e975e0 1278 gcpr.lpClass = (void *)classbuffer;
f0fccd51 1279 gcpr.nGlyphs = cbCount;
f0fccd51 1280 GetCharacterPlacementW(hdc, lpString, cbCount, 0, &gcpr,
c6958dfe 1281 FLI_MASK | GCP_CLASSIN | GCP_DIACRITIC);
f0fccd51 1282
c6958dfe 1283 ExtTextOut(hdc, x, y,
1284 ETO_GLYPH_INDEX | ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
1285 lprc, buffer, cbCount, lpDx);
f0fccd51 1286}
1287
1288/*
fe75e503 1289 * The exact_textout() wrapper, unfortunately, destroys the useful
1290 * Windows `font linking' behaviour: automatic handling of Unicode
1291 * code points not supported in this font by falling back to a font
1292 * which does contain them. Therefore, we adopt a multi-layered
1293 * approach: for any potentially-bidi text, we use exact_textout(),
1294 * and for everything else we use a simple ExtTextOut as we did
1295 * before exact_textout() was introduced.
1296 */
1297static void general_textout(HDC hdc, int x, int y, CONST RECT *lprc,
1298 unsigned short *lpString, UINT cbCount,
1299 CONST INT *lpDx, int opaque)
1300{
1301 int i, j, xp, xn;
19e569a1 1302 int bkmode = 0, got_bkmode = FALSE;
fe75e503 1303
1304 xp = xn = x;
1305
62ddb51e 1306 for (i = 0; i < (int)cbCount ;) {
fe75e503 1307 int rtl = is_rtl(lpString[i]);
1308
1309 xn += lpDx[i];
1310
62ddb51e 1311 for (j = i+1; j < (int)cbCount; j++) {
fe75e503 1312 if (rtl != is_rtl(lpString[j]))
1313 break;
1314 xn += lpDx[j];
1315 }
1316
1317 /*
1318 * Now [i,j) indicates a maximal substring of lpString
1319 * which should be displayed using the same textout
1320 * function.
1321 */
1322 if (rtl) {
19e569a1 1323 exact_textout(hdc, xp, y, lprc, lpString+i, j-i,
53126a62 1324 font_varpitch ? NULL : lpDx+i, opaque);
fe75e503 1325 } else {
fe75e503 1326 ExtTextOutW(hdc, xp, y, ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
19e569a1 1327 lprc, lpString+i, j-i,
53126a62 1328 font_varpitch ? NULL : lpDx+i);
fe75e503 1329 }
1330
1331 i = j;
1332 xp = xn;
19e569a1 1333
1334 bkmode = GetBkMode(hdc);
1335 got_bkmode = TRUE;
1336 SetBkMode(hdc, TRANSPARENT);
1337 opaque = FALSE;
fe75e503 1338 }
1339
19e569a1 1340 if (got_bkmode)
1341 SetBkMode(hdc, bkmode);
fe75e503 1342}
1343
14ce9887 1344static int get_font_width(HDC hdc, const TEXTMETRIC *tm)
1345{
1346 int ret;
1347 /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
1348 if (!(tm->tmPitchAndFamily & TMPF_FIXED_PITCH)) {
1349 ret = tm->tmAveCharWidth;
1350 } else {
1351#define FIRST '0'
1352#define LAST '9'
1353 ABCFLOAT widths[LAST-FIRST + 1];
1354 int j;
1355
1356 font_varpitch = TRUE;
1357 font_dualwidth = TRUE;
1358 if (GetCharABCWidthsFloat(hdc, FIRST, LAST, widths)) {
1359 ret = 0;
1360 for (j = 0; j < lenof(widths); j++) {
1361 int width = (int)(0.5 + widths[j].abcfA +
1362 widths[j].abcfB + widths[j].abcfC);
1363 if (ret < width)
1364 ret = width;
1365 }
1366 } else {
1367 ret = tm->tmMaxCharWidth;
1368 }
1369#undef FIRST
1370#undef LAST
1371 }
1372 return ret;
1373}
1374
fe75e503 1375/*
4eeb7d09 1376 * Initialise all the fonts we will need initially. There may be as many as
efd51845 1377 * three or as few as one. The other (potentially) twenty-one fonts are done
4eeb7d09 1378 * if/when they are needed.
1379 *
1380 * We also:
374330e2 1381 *
1382 * - check the font width and height, correcting our guesses if
1383 * necessary.
1384 *
1385 * - verify that the bold font is the same width as the ordinary
1386 * one, and engage shadow bolding if not.
1387 *
1388 * - verify that the underlined font is the same width as the
1389 * ordinary one (manual underlining by means of line drawing can
1390 * be done in a pinch).
374330e2 1391 */
5a73255e 1392static void init_fonts(int pick_width, int pick_height)
32874aea 1393{
374330e2 1394 TEXTMETRIC tm;
4eeb7d09 1395 CPINFO cpinfo;
4a693cfc 1396 FontSpec *font;
4eeb7d09 1397 int fontsize[3];
97fc891e 1398 int i;
4a693cfc 1399 int quality;
374330e2 1400 HDC hdc;
1401 int fw_dontcare, fw_bold;
1402
4eeb7d09 1403 for (i = 0; i < FONT_MAXNO; i++)
374330e2 1404 fonts[i] = NULL;
1405
4a693cfc 1406 bold_mode = conf_get_int(conf, CONF_bold_colour) ?
1407 BOLD_COLOURS : BOLD_FONT;
5a73255e 1408 und_mode = UND_FONT;
1409
4a693cfc 1410 font = conf_get_fontspec(conf, CONF_font);
1411 if (font->isbold) {
374330e2 1412 fw_dontcare = FW_BOLD;
5b80d07f 1413 fw_bold = FW_HEAVY;
32874aea 1414 } else {
374330e2 1415 fw_dontcare = FW_DONTCARE;
1416 fw_bold = FW_BOLD;
1417 }
1418
97fc891e 1419 hdc = GetDC(hwnd);
1420
5a73255e 1421 if (pick_height)
1422 font_height = pick_height;
1423 else {
4a693cfc 1424 font_height = font->height;
5a73255e 1425 if (font_height > 0) {
1426 font_height =
1427 -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1428 }
3b2c664e 1429 }
59ad2c03 1430 font_width = pick_width;
97fc891e 1431
4a693cfc 1432 quality = conf_get_int(conf, CONF_font_quality);
374330e2 1433#define f(i,c,w,u) \
97fc891e 1434 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
374330e2 1435 c, OUT_DEFAULT_PRECIS, \
4a693cfc 1436 CLIP_DEFAULT_PRECIS, quality, \
1437 FIXED_PITCH | FF_DONTCARE, font->name)
97fc891e 1438
4a693cfc 1439 f(FONT_NORMAL, font->charset, fw_dontcare, FALSE);
97fc891e 1440
4eeb7d09 1441 SelectObject(hdc, fonts[FONT_NORMAL]);
1442 GetTextMetrics(hdc, &tm);
5a73255e 1443
fd64bb27 1444 GetObject(fonts[FONT_NORMAL], sizeof(LOGFONT), &lfont);
1445
14ce9887 1446 /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
1447 if (!(tm.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
1448 font_varpitch = FALSE;
1449 font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
1450 } else {
1451 font_varpitch = TRUE;
1452 font_dualwidth = TRUE;
1453 }
5a73255e 1454 if (pick_width == 0 || pick_height == 0) {
1455 font_height = tm.tmHeight;
14ce9887 1456 font_width = get_font_width(hdc, &tm);
5a73255e 1457 }
5a73255e 1458
1459#ifdef RDB_DEBUG_PATCH
1460 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1461 tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
1462#endif
97fc891e 1463
4eeb7d09 1464 {
1465 CHARSETINFO info;
1466 DWORD cset = tm.tmCharSet;
1467 memset(&info, 0xFF, sizeof(info));
97fc891e 1468
4eeb7d09 1469 /* !!! Yes the next line is right */
1470 if (cset == OEM_CHARSET)
21d2b241 1471 ucsdata.font_codepage = GetOEMCP();
4eeb7d09 1472 else
21d2b241 1473 if (TranslateCharsetInfo ((DWORD *) cset, &info, TCI_SRCCHARSET))
1474 ucsdata.font_codepage = info.ciACP;
4eeb7d09 1475 else
21d2b241 1476 ucsdata.font_codepage = -1;
32874aea 1477
21d2b241 1478 GetCPInfo(ucsdata.font_codepage, &cpinfo);
1479 ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
4eeb7d09 1480 }
97fc891e 1481
4a693cfc 1482 f(FONT_UNDERLINE, font->charset, fw_dontcare, TRUE);
97fc891e 1483
4eeb7d09 1484 /*
1485 * Some fonts, e.g. 9-pt Courier, draw their underlines
1486 * outside their character cell. We successfully prevent
1487 * screen corruption by clipping the text output, but then
1488 * we lose the underline completely. Here we try to work
1489 * out whether this is such a font, and if it is, we set a
1490 * flag that causes underlines to be drawn by hand.
1491 *
1492 * Having tried other more sophisticated approaches (such
1493 * as examining the TEXTMETRIC structure or requesting the
1494 * height of a string), I think we'll do this the brute
1495 * force way: we create a small bitmap, draw an underlined
1496 * space on it, and test to see whether any pixels are
1497 * foreground-coloured. (Since we expect the underline to
1498 * go all the way across the character cell, we only search
1499 * down a single column of the bitmap, half way across.)
1500 */
1501 {
1502 HDC und_dc;
1503 HBITMAP und_bm, und_oldbm;
1504 int i, gotit;
1505 COLORREF c;
1506
1507 und_dc = CreateCompatibleDC(hdc);
1508 und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
1509 und_oldbm = SelectObject(und_dc, und_bm);
1510 SelectObject(und_dc, fonts[FONT_UNDERLINE]);
1511 SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
1512 SetTextColor(und_dc, RGB(255, 255, 255));
1513 SetBkColor(und_dc, RGB(0, 0, 0));
1514 SetBkMode(und_dc, OPAQUE);
1515 ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
1516 gotit = FALSE;
1517 for (i = 0; i < font_height; i++) {
1518 c = GetPixel(und_dc, font_width / 2, i);
1519 if (c != RGB(0, 0, 0))
1520 gotit = TRUE;
1521 }
1522 SelectObject(und_dc, und_oldbm);
1523 DeleteObject(und_bm);
1524 DeleteDC(und_dc);
1525 if (!gotit) {
1526 und_mode = UND_LINE;
1527 DeleteObject(fonts[FONT_UNDERLINE]);
1528 fonts[FONT_UNDERLINE] = 0;
32874aea 1529 }
4eeb7d09 1530 }
97fc891e 1531
4eeb7d09 1532 if (bold_mode == BOLD_FONT) {
4a693cfc 1533 f(FONT_BOLD, font->charset, fw_bold, FALSE);
374330e2 1534 }
1535#undef f
1536
97fc891e 1537 descent = tm.tmAscent + 1;
1538 if (descent >= font_height)
1539 descent = font_height - 1;
374330e2 1540
4eeb7d09 1541 for (i = 0; i < 3; i++) {
97fc891e 1542 if (fonts[i]) {
32874aea 1543 if (SelectObject(hdc, fonts[i]) && GetTextMetrics(hdc, &tm))
14ce9887 1544 fontsize[i] = get_font_width(hdc, &tm) + 256 * tm.tmHeight;
32874aea 1545 else
4eeb7d09 1546 fontsize[i] = -i;
32874aea 1547 } else
4eeb7d09 1548 fontsize[i] = -i;
374330e2 1549 }
1550
32874aea 1551 ReleaseDC(hwnd, hdc);
374330e2 1552
4eeb7d09 1553 if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
374330e2 1554 und_mode = UND_LINE;
32874aea 1555 DeleteObject(fonts[FONT_UNDERLINE]);
4eeb7d09 1556 fonts[FONT_UNDERLINE] = 0;
374330e2 1557 }
1558
4eeb7d09 1559 if (bold_mode == BOLD_FONT &&
1560 fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
374330e2 1561 bold_mode = BOLD_SHADOW;
32874aea 1562 DeleteObject(fonts[FONT_BOLD]);
4eeb7d09 1563 fonts[FONT_BOLD] = 0;
374330e2 1564 }
4eeb7d09 1565 fontflag[0] = fontflag[1] = fontflag[2] = 1;
1566
4a693cfc 1567 init_ucs(conf, &ucsdata);
4eeb7d09 1568}
1569
1570static void another_font(int fontno)
1571{
1572 int basefont;
4a693cfc 1573 int fw_dontcare, fw_bold, quality;
4eeb7d09 1574 int c, u, w, x;
1575 char *s;
4a693cfc 1576 FontSpec *font;
4eeb7d09 1577
1578 if (fontno < 0 || fontno >= FONT_MAXNO || fontflag[fontno])
1579 return;
1580
1581 basefont = (fontno & ~(FONT_BOLDUND));
1582 if (basefont != fontno && !fontflag[basefont])
1583 another_font(basefont);
97fc891e 1584
4a693cfc 1585 font = conf_get_fontspec(conf, CONF_font);
1586
1587 if (font->isbold) {
4eeb7d09 1588 fw_dontcare = FW_BOLD;
1589 fw_bold = FW_HEAVY;
1590 } else {
1591 fw_dontcare = FW_DONTCARE;
1592 fw_bold = FW_BOLD;
1593 }
1594
4a693cfc 1595 c = font->charset;
4eeb7d09 1596 w = fw_dontcare;
1597 u = FALSE;
4a693cfc 1598 s = font->name;
4eeb7d09 1599 x = font_width;
1600
1601 if (fontno & FONT_WIDE)
1602 x *= 2;
1603 if (fontno & FONT_NARROW)
5a73255e 1604 x = (x+1)/2;
4eeb7d09 1605 if (fontno & FONT_OEM)
1606 c = OEM_CHARSET;
1607 if (fontno & FONT_BOLD)
1608 w = fw_bold;
1609 if (fontno & FONT_UNDERLINE)
1610 u = TRUE;
1611
4a693cfc 1612 quality = conf_get_int(conf, CONF_font_quality);
1613
4eeb7d09 1614 fonts[fontno] =
1615 CreateFont(font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
1616 FALSE, u, FALSE, c, OUT_DEFAULT_PRECIS,
4a693cfc 1617 CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
14ce9887 1618 DEFAULT_PITCH | FF_DONTCARE, s);
4eeb7d09 1619
1620 fontflag[fontno] = 1;
1621}
1622
1623static void deinit_fonts(void)
1624{
1625 int i;
1626 for (i = 0; i < FONT_MAXNO; i++) {
1627 if (fonts[i])
1628 DeleteObject(fonts[i]);
1629 fonts[i] = 0;
1630 fontflag[i] = 0;
374330e2 1631 }
1632}
1633
a8327734 1634void request_resize(void *frontend, int w, int h)
32874aea 1635{
59ad2c03 1636 int width, height;
c9def1b8 1637
1638 /* If the window is maximized supress resizing attempts */
5a73255e 1639 if (IsZoomed(hwnd)) {
4a693cfc 1640 if (conf_get_int(conf, CONF_resize_action) == RESIZE_TERM)
5a73255e 1641 return;
1642 }
32874aea 1643
4a693cfc 1644 if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED) return;
887035a5 1645 if (h == term->rows && w == term->cols) return;
5a73255e 1646
1647 /* Sanity checks ... */
1648 {
32874aea 1649 static int first_time = 1;
1650 static RECT ss;
1651
1652 switch (first_time) {
1653 case 1:
1654 /* Get the size of the screen */
d0f1bcb4 1655 if (get_fullscreen_rect(&ss))
32874aea 1656 /* first_time = 0 */ ;
1657 else {
1658 first_time = 2;
1659 break;
1660 }
1661 case 0:
1662 /* Make sure the values are sane */
5a73255e 1663 width = (ss.right - ss.left - extra_width) / 4;
1664 height = (ss.bottom - ss.top - extra_height) / 6;
32874aea 1665
5a73255e 1666 if (w > width || h > height)
1667 return;
32874aea 1668 if (w < 15)
1669 w = 15;
1670 if (h < 1)
5a73255e 1671 h = 1;
32874aea 1672 }
c9def1b8 1673 }
59ad2c03 1674
4a693cfc 1675 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
5a73255e 1676
4a693cfc 1677 if (conf_get_int(conf, CONF_resize_action) != RESIZE_FONT &&
1678 !IsZoomed(hwnd)) {
5a73255e 1679 width = extra_width + font_width * w;
1680 height = extra_height + font_height * h;
374330e2 1681
5a73255e 1682 SetWindowPos(hwnd, NULL, 0, 0, width, height,
1683 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1684 SWP_NOMOVE | SWP_NOZORDER);
1685 } else
1686 reset_window(0);
1687
1688 InvalidateRect(hwnd, NULL, TRUE);
1689}
1690
1691static void reset_window(int reinit) {
1692 /*
1693 * This function decides how to resize or redraw when the
1694 * user changes something.
1695 *
1696 * This function doesn't like to change the terminal size but if the
1697 * font size is locked that may be it's only soluion.
1698 */
4a693cfc 1699 int win_width, win_height, resize_action, window_border;
5a73255e 1700 RECT cr, wr;
1701
1702#ifdef RDB_DEBUG_PATCH
1703 debug((27, "reset_window()"));
1704#endif
1705
1706 /* Current window sizes ... */
1707 GetWindowRect(hwnd, &wr);
1708 GetClientRect(hwnd, &cr);
1709
1710 win_width = cr.right - cr.left;
1711 win_height = cr.bottom - cr.top;
1712
4a693cfc 1713 resize_action = conf_get_int(conf, CONF_resize_action);
1714 window_border = conf_get_int(conf, CONF_window_border);
1715
1716 if (resize_action == RESIZE_DISABLED)
1717 reinit = 2;
0ed2f48e 1718
5a73255e 1719 /* Are we being forced to reload the fonts ? */
1720 if (reinit>1) {
1721#ifdef RDB_DEBUG_PATCH
1722 debug((27, "reset_window() -- Forced deinit"));
1723#endif
1724 deinit_fonts();
1725 init_fonts(0,0);
1726 }
1727
1728 /* Oh, looks like we're minimised */
1729 if (win_width == 0 || win_height == 0)
1730 return;
1731
1732 /* Is the window out of position ? */
1733 if ( !reinit &&
887035a5 1734 (offset_width != (win_width-font_width*term->cols)/2 ||
1735 offset_height != (win_height-font_height*term->rows)/2) ){
1736 offset_width = (win_width-font_width*term->cols)/2;
1737 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1738 InvalidateRect(hwnd, NULL, TRUE);
1739#ifdef RDB_DEBUG_PATCH
1740 debug((27, "reset_window() -> Reposition terminal"));
1741#endif
1742 }
1743
6003660f 1744 if (IsZoomed(hwnd)) {
5a73255e 1745 /* We're fullscreen, this means we must not change the size of
1746 * the window so it's the font size or the terminal itself.
1747 */
1748
1749 extra_width = wr.right - wr.left - cr.right + cr.left;
1750 extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
1751
4a693cfc 1752 if (resize_action != RESIZE_TERM) {
1753 if (font_width != win_width/term->cols ||
1754 font_height != win_height/term->rows) {
5a73255e 1755 deinit_fonts();
887035a5 1756 init_fonts(win_width/term->cols, win_height/term->rows);
1757 offset_width = (win_width-font_width*term->cols)/2;
1758 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1759 InvalidateRect(hwnd, NULL, TRUE);
1760#ifdef RDB_DEBUG_PATCH
1761 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1762 font_width, font_height));
1763#endif
1764 }
1765 } else {
4a693cfc 1766 if (font_width * term->cols != win_width ||
1767 font_height * term->rows != win_height) {
5a73255e 1768 /* Our only choice at this point is to change the
1769 * size of the terminal; Oh well.
1770 */
887035a5 1771 term_size(term, win_height/font_height, win_width/font_width,
4a693cfc 1772 conf_get_int(conf, CONF_savelines));
887035a5 1773 offset_width = (win_width-font_width*term->cols)/2;
1774 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1775 InvalidateRect(hwnd, NULL, TRUE);
1776#ifdef RDB_DEBUG_PATCH
1777 debug((27, "reset_window() -> Zoomed term_size"));
1778#endif
1779 }
1780 }
1781 return;
1782 }
1783
1784 /* Hmm, a force re-init means we should ignore the current window
1785 * so we resize to the default font size.
1786 */
1787 if (reinit>0) {
1788#ifdef RDB_DEBUG_PATCH
1789 debug((27, "reset_window() -> Forced re-init"));
1790#endif
1791
4a693cfc 1792 offset_width = offset_height = window_border;
5a73255e 1793 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1794 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1795
887035a5 1796 if (win_width != font_width*term->cols + offset_width*2 ||
1797 win_height != font_height*term->rows + offset_height*2) {
5a73255e 1798
1799 /* If this is too large windows will resize it to the maximum
1800 * allowed window size, we will then be back in here and resize
1801 * the font or terminal to fit.
1802 */
1803 SetWindowPos(hwnd, NULL, 0, 0,
887035a5 1804 font_width*term->cols + extra_width,
1805 font_height*term->rows + extra_height,
5a73255e 1806 SWP_NOMOVE | SWP_NOZORDER);
1807 }
0ed2f48e 1808
1809 InvalidateRect(hwnd, NULL, TRUE);
5a73255e 1810 return;
1811 }
1812
1813 /* Okay the user doesn't want us to change the font so we try the
1814 * window. But that may be too big for the screen which forces us
1815 * to change the terminal.
1816 */
4a693cfc 1817 if ((resize_action == RESIZE_TERM && reinit<=0) ||
1818 (resize_action == RESIZE_EITHER && reinit<0) ||
0ed2f48e 1819 reinit>0) {
4a693cfc 1820 offset_width = offset_height = window_border;
5a73255e 1821 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1822 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1823
887035a5 1824 if (win_width != font_width*term->cols + offset_width*2 ||
1825 win_height != font_height*term->rows + offset_height*2) {
5a73255e 1826
1827 static RECT ss;
1828 int width, height;
d0f1bcb4 1829
1830 get_fullscreen_rect(&ss);
5a73255e 1831
5a73255e 1832 width = (ss.right - ss.left - extra_width) / font_width;
1833 height = (ss.bottom - ss.top - extra_height) / font_height;
1834
1835 /* Grrr too big */
887035a5 1836 if ( term->rows > height || term->cols > width ) {
4a693cfc 1837 if (resize_action == RESIZE_EITHER) {
0ed2f48e 1838 /* Make the font the biggest we can */
887035a5 1839 if (term->cols > width)
1840 font_width = (ss.right - ss.left - extra_width)
1841 / term->cols;
1842 if (term->rows > height)
1843 font_height = (ss.bottom - ss.top - extra_height)
1844 / term->rows;
0ed2f48e 1845
1846 deinit_fonts();
1847 init_fonts(font_width, font_height);
1848
1849 width = (ss.right - ss.left - extra_width) / font_width;
1850 height = (ss.bottom - ss.top - extra_height) / font_height;
1851 } else {
887035a5 1852 if ( height > term->rows ) height = term->rows;
1853 if ( width > term->cols ) width = term->cols;
4a693cfc 1854 term_size(term, height, width,
1855 conf_get_int(conf, CONF_savelines));
5a73255e 1856#ifdef RDB_DEBUG_PATCH
0ed2f48e 1857 debug((27, "reset_window() -> term resize to (%d,%d)",
1858 height, width));
5a73255e 1859#endif
0ed2f48e 1860 }
5a73255e 1861 }
1862
1863 SetWindowPos(hwnd, NULL, 0, 0,
887035a5 1864 font_width*term->cols + extra_width,
1865 font_height*term->rows + extra_height,
5a73255e 1866 SWP_NOMOVE | SWP_NOZORDER);
1867
1868 InvalidateRect(hwnd, NULL, TRUE);
1869#ifdef RDB_DEBUG_PATCH
1870 debug((27, "reset_window() -> window resize to (%d,%d)",
887035a5 1871 font_width*term->cols + extra_width,
1872 font_height*term->rows + extra_height));
5a73255e 1873#endif
1874 }
1875 return;
1876 }
1877
1878 /* We're allowed to or must change the font but do we want to ? */
1879
4a693cfc 1880 if (font_width != (win_width-window_border*2)/term->cols ||
1881 font_height != (win_height-window_border*2)/term->rows) {
5a73255e 1882
1883 deinit_fonts();
4a693cfc 1884 init_fonts((win_width-window_border*2)/term->cols,
1885 (win_height-window_border*2)/term->rows);
887035a5 1886 offset_width = (win_width-font_width*term->cols)/2;
1887 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1888
1889 extra_width = wr.right - wr.left - cr.right + cr.left +offset_width*2;
1890 extra_height = wr.bottom - wr.top - cr.bottom + cr.top+offset_height*2;
1891
1892 InvalidateRect(hwnd, NULL, TRUE);
1893#ifdef RDB_DEBUG_PATCH
1894 debug((25, "reset_window() -> font resize to (%d,%d)",
1895 font_width, font_height));
1896#endif
1897 }
374330e2 1898}
1899
a5ce2e9f 1900static void set_input_locale(HKL kl)
1901{
1902 char lbuf[20];
1903
1904 GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
1905 lbuf, sizeof(lbuf));
1906
1907 kbd_codepage = atoi(lbuf);
1908}
1909
6908fed7 1910static void click(Mouse_Button b, int x, int y, int shift, int ctrl, int alt)
32874aea 1911{
fdedf2c8 1912 int thistime = GetMessageTime();
1913
4a693cfc 1914 if (send_raw_mouse &&
1915 !(shift && conf_get_int(conf, CONF_mouse_override))) {
b90840c3 1916 lastbtn = MBT_NOTHING;
fc5b0934 1917 term_mouse(term, b, translate_button(b), MA_CLICK,
1918 x, y, shift, ctrl, alt);
01c034ad 1919 return;
1920 }
1921
fdedf2c8 1922 if (lastbtn == b && thistime - lasttime < dbltime) {
374330e2 1923 lastact = (lastact == MA_CLICK ? MA_2CLK :
1924 lastact == MA_2CLK ? MA_3CLK :
1925 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
1926 } else {
1927 lastbtn = b;
1928 lastact = MA_CLICK;
1929 }
1930 if (lastact != MA_NOTHING)
fc5b0934 1931 term_mouse(term, b, translate_button(b), lastact,
1932 x, y, shift, ctrl, alt);
fdedf2c8 1933 lasttime = thistime;
374330e2 1934}
1935
01c034ad 1936/*
1937 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1938 * into a cooked one (SELECT, EXTEND, PASTE).
1939 */
2c02ed02 1940static Mouse_Button translate_button(Mouse_Button button)
32874aea 1941{
01c034ad 1942 if (button == MBT_LEFT)
1943 return MBT_SELECT;
1944 if (button == MBT_MIDDLE)
4a693cfc 1945 return conf_get_int(conf, CONF_mouse_is_xterm) == 1 ?
1946 MBT_PASTE : MBT_EXTEND;
01c034ad 1947 if (button == MBT_RIGHT)
4a693cfc 1948 return conf_get_int(conf, CONF_mouse_is_xterm) == 1 ?
1949 MBT_EXTEND : MBT_PASTE;
2d466ffd 1950 return 0; /* shouldn't happen */
01c034ad 1951}
1952
32874aea 1953static void show_mouseptr(int show)
1954{
755e0524 1955 /* NB that the counter in ShowCursor() is also frobbed by
1956 * update_mouse_pointer() */
554c540d 1957 static int cursor_visible = 1;
4a693cfc 1958 if (!conf_get_int(conf, CONF_hide_mouseptr))
1959 show = 1; /* override if this feature disabled */
554c540d 1960 if (cursor_visible && !show)
32874aea 1961 ShowCursor(FALSE);
554c540d 1962 else if (!cursor_visible && show)
32874aea 1963 ShowCursor(TRUE);
554c540d 1964 cursor_visible = show;
1965}
1966
6908fed7 1967static int is_alt_pressed(void)
1968{
1969 BYTE keystate[256];
1970 int r = GetKeyboardState(keystate);
1971 if (!r)
1972 return FALSE;
1973 if (keystate[VK_MENU] & 0x80)
1974 return TRUE;
1975 if (keystate[VK_RMENU] & 0x80)
1976 return TRUE;
1977 return FALSE;
1978}
1979
68f9b3d9 1980static int resizing;
1981
95587c73 1982void notify_remote_exit(void *fe)
1983{
4a693cfc 1984 int exitcode, close_on_exit;
95587c73 1985
1986 if (!session_closed &&
1987 (exitcode = back->exitcode(backhandle)) >= 0) {
4a693cfc 1988 close_on_exit = conf_get_int(conf, CONF_close_on_exit);
95587c73 1989 /* Abnormal exits will already have set session_closed and taken
1990 * appropriate action. */
4a693cfc 1991 if (close_on_exit == FORCE_ON ||
1992 (close_on_exit == AUTO && exitcode != INT_MAX)) {
95587c73 1993 PostQuitMessage(0);
1994 } else {
1995 must_close_session = TRUE;
1996 session_closed = TRUE;
1997 /* exitcode == INT_MAX indicates that the connection was closed
1998 * by a fatal error, so an error box will be coming our way and
1999 * we should not generate this informational one. */
2000 if (exitcode != INT_MAX)
2001 MessageBox(hwnd, "Connection closed by remote host",
2002 appname, MB_OK | MB_ICONINFORMATION);
2003 }
2004 }
2005}
39934deb 2006
2007void timer_change_notify(long next)
2008{
2009 long ticks = next - GETTICKCOUNT();
2010 if (ticks <= 0) ticks = 1; /* just in case */
2011 KillTimer(hwnd, TIMING_TIMER_ID);
2012 SetTimer(hwnd, TIMING_TIMER_ID, ticks, NULL);
2013 timing_next_time = next;
2014}
2015
4a693cfc 2016static void conf_cache_data(void)
2017{
2018 /* Cache some items from conf to speed lookups in very hot code */
2019 cursor_type = conf_get_int(conf, CONF_cursor_type);
2020 vtmode = conf_get_int(conf, CONF_vtmode);
2021}
2022
32874aea 2023static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2024 WPARAM wParam, LPARAM lParam)
2025{
374330e2 2026 HDC hdc;
374330e2 2027 static int ignore_clip = FALSE;
3ad8c6db 2028 static int need_backend_resize = FALSE;
1ba99d2c 2029 static int fullscr_on_max = FALSE;
19caf318 2030 static int processed_resize = FALSE;
d8a13f62 2031 static UINT last_mousemove = 0;
4a693cfc 2032 int resize_action;
374330e2 2033
2034 switch (message) {
59ad2c03 2035 case WM_TIMER:
39934deb 2036 if ((UINT_PTR)wParam == TIMING_TIMER_ID) {
2037 long next;
2038
2039 KillTimer(hwnd, TIMING_TIMER_ID);
2040 if (run_timers(timing_next_time, &next)) {
2041 timer_change_notify(next);
2042 } else {
32874aea 2043 }
2044 }
59ad2c03 2045 return 0;
374330e2 2046 case WM_CREATE:
2047 break;
68130d34 2048 case WM_CLOSE:
f6f450e2 2049 {
2050 char *str;
2051 show_mouseptr(1);
2052 str = dupprintf("%s Exit Confirmation", appname);
4a693cfc 2053 if (session_closed || !conf_get_int(conf, CONF_warn_on_close) ||
f6f450e2 2054 MessageBox(hwnd,
2055 "Are you sure you want to close this session?",
84d264cd 2056 str, MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON1)
76347f46 2057 == IDOK)
f6f450e2 2058 DestroyWindow(hwnd);
2059 sfree(str);
2060 }
68130d34 2061 return 0;
374330e2 2062 case WM_DESTROY:
32874aea 2063 show_mouseptr(1);
2064 PostQuitMessage(0);
374330e2 2065 return 0;
12745e35 2066 case WM_INITMENUPOPUP:
2067 if ((HMENU)wParam == savedsess_menu) {
2068 /* About to pop up Saved Sessions sub-menu.
2069 * Refresh the session list. */
2070 get_sesslist(&sesslist, FALSE); /* free */
2071 get_sesslist(&sesslist, TRUE);
2072 update_savedsess_menu();
2073 return 0;
2074 }
2075 break;
63be7767 2076 case WM_COMMAND:
6833a413 2077 case WM_SYSCOMMAND:
2078 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
374330e2 2079 case IDM_SHOWLOG:
c5e9c988 2080 showeventlog(hwnd);
374330e2 2081 break;
2082 case IDM_NEWSESS:
2083 case IDM_DUPSESS:
6833a413 2084 case IDM_SAVEDSESS:
374330e2 2085 {
2086 char b[2048];
2087 char c[30], *cl;
e4e4cc7e 2088 int freecl = FALSE;
8e90e4c6 2089 BOOL inherit_handles;
374330e2 2090 STARTUPINFO si;
2091 PROCESS_INFORMATION pi;
2092 HANDLE filemap = NULL;
2093
2094 if (wParam == IDM_DUPSESS) {
2095 /*
2096 * Allocate a file-mapping memory chunk for the
2097 * config structure.
2098 */
2099 SECURITY_ATTRIBUTES sa;
4a693cfc 2100 void *p;
2101 int size;
2102
2103 size = conf_serialised_size(conf);
374330e2 2104
2105 sa.nLength = sizeof(sa);
2106 sa.lpSecurityDescriptor = NULL;
2107 sa.bInheritHandle = TRUE;
4a0b6d61 2108 filemap = CreateFileMapping(INVALID_HANDLE_VALUE,
374330e2 2109 &sa,
2110 PAGE_READWRITE,
4a693cfc 2111 0, size, NULL);
4a0b6d61 2112 if (filemap && filemap != INVALID_HANDLE_VALUE) {
4a693cfc 2113 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, size);
374330e2 2114 if (p) {
4a693cfc 2115 conf_serialise(conf, p);
374330e2 2116 UnmapViewOfFile(p);
2117 }
2118 }
8e90e4c6 2119 inherit_handles = TRUE;
4a693cfc 2120 sprintf(c, "putty &%p:%u", filemap, (unsigned)size);
374330e2 2121 cl = c;
0a4aa984 2122 } else if (wParam == IDM_SAVEDSESS) {
865e82ad 2123 unsigned int sessno = ((lParam - IDM_SAVED_MIN)
2124 / MENU_SAVED_STEP) + 1;
62ddb51e 2125 if (sessno < (unsigned)sesslist.nsessions) {
1f1e3232 2126 char *session = sesslist.sessions[sessno];
1f1e3232 2127 cl = dupprintf("putty @%s", session);
8e90e4c6 2128 inherit_handles = FALSE;
1f1e3232 2129 freecl = TRUE;
f8d7977b 2130 } else
2131 break;
8e90e4c6 2132 } else /* IDM_NEWSESS */ {
6833a413 2133 cl = NULL;
8e90e4c6 2134 inherit_handles = FALSE;
2135 }
374330e2 2136
32874aea 2137 GetModuleFileName(NULL, b, sizeof(b) - 1);
374330e2 2138 si.cb = sizeof(si);
2139 si.lpReserved = NULL;
2140 si.lpDesktop = NULL;
2141 si.lpTitle = NULL;
2142 si.dwFlags = 0;
2143 si.cbReserved2 = 0;
2144 si.lpReserved2 = NULL;
8e90e4c6 2145 CreateProcess(b, cl, NULL, NULL, inherit_handles,
32874aea 2146 NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
374330e2 2147
2148 if (filemap)
2149 CloseHandle(filemap);
e4e4cc7e 2150 if (freecl)
dcbde236 2151 sfree(cl);
374330e2 2152 }
2153 break;
e3be8de5 2154 case IDM_RESTART:
2155 if (!back) {
2156 logevent(NULL, "----- Session restarted -----");
900f9aca 2157 term_pwron(term, FALSE);
e3be8de5 2158 start_backend();
2159 }
2160
2161 break;
32874aea 2162 case IDM_RECONF:
2163 {
4a693cfc 2164 Conf *prev_conf;
5a73255e 2165 int init_lvl = 1;
68a66cc3 2166 int reconfig_result;
5a73255e 2167
359fd192 2168 if (reconfiguring)
68a66cc3 2169 break;
359fd192 2170 else
68a66cc3 2171 reconfiguring = TRUE;
359fd192 2172
4a693cfc 2173 /*
2174 * Copy the current window title into the stored
2175 * previous configuration, so that doing nothing to
2176 * the window title field in the config box doesn't
2177 * reset the title to its startup state.
2178 */
2179 conf_set_str(conf, CONF_wintitle, window_name);
2180
2181 prev_conf = conf_copy(conf);
e1c8e0ed 2182
68a66cc3 2183 reconfig_result =
b56ff5c3 2184 do_reconfig(hwnd, back ? back->cfg_info(backhandle) : 0);
68a66cc3 2185 reconfiguring = FALSE;
2186 if (!reconfig_result)
32874aea 2187 break;
e1c8e0ed 2188
4a693cfc 2189 conf_cache_data();
2190
2191 resize_action = conf_get_int(conf, CONF_resize_action);
2cae8529 2192 {
2193 /* Disable full-screen if resizing forbidden */
52c70126 2194 int i;
2195 for (i = 0; i < lenof(popup_menus); i++)
2196 EnableMenuItem(popup_menus[i].menu, IDM_FULLSCREEN,
2197 MF_BYCOMMAND |
4a693cfc 2198 (resize_action == RESIZE_DISABLED)
52c70126 2199 ? MF_GRAYED : MF_ENABLED);
2cae8529 2200 /* Gracefully unzoom if necessary */
4a693cfc 2201 if (IsZoomed(hwnd) && (resize_action == RESIZE_DISABLED))
1ba99d2c 2202 ShowWindow(hwnd, SW_RESTORE);
a401e5f3 2203 }
2204
c229ef97 2205 /* Pass new config data to the logging module */
4a693cfc 2206 log_reconfig(logctx, conf);
e1c8e0ed 2207
32874aea 2208 sfree(logpal);
2209 /*
2210 * Flush the line discipline's edit buffer in the
2211 * case where local editing has just been disabled.
2212 */
4a693cfc 2213 ldisc_configure(ldisc, conf);
e3be8de5 2214 if (ldisc)
2215 ldisc_send(ldisc, NULL, 0, 0);
32874aea 2216 if (pal)
2217 DeleteObject(pal);
2218 logpal = NULL;
2219 pal = NULL;
4a693cfc 2220 conftopalette();
32874aea 2221 init_palette();
2222
64734920 2223 /* Pass new config data to the terminal */
4a693cfc 2224 term_reconfig(term, conf);
0d2086c5 2225
86916870 2226 /* Pass new config data to the back end */
e3be8de5 2227 if (back)
4a693cfc 2228 back->reconfig(backhandle, conf);
86916870 2229
5a73255e 2230 /* Screen size changed ? */
4a693cfc 2231 if (conf_get_int(conf, CONF_height) !=
2232 conf_get_int(prev_conf, CONF_height) ||
2233 conf_get_int(conf, CONF_width) !=
2234 conf_get_int(prev_conf, CONF_width) ||
2235 conf_get_int(conf, CONF_savelines) !=
2236 conf_get_int(prev_conf, CONF_savelines) ||
2237 resize_action == RESIZE_FONT ||
2238 (resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
2239 resize_action == RESIZE_DISABLED)
2240 term_size(term, conf_get_int(conf, CONF_height),
2241 conf_get_int(conf, CONF_width),
2242 conf_get_int(conf, CONF_savelines));
5a73255e 2243
32874aea 2244 /* Enable or disable the scroll bar, etc */
2245 {
1e5eefb6 2246 LONG nflg, flag = GetWindowLongPtr(hwnd, GWL_STYLE);
32874aea 2247 LONG nexflag, exflag =
1e5eefb6 2248 GetWindowLongPtr(hwnd, GWL_EXSTYLE);
32874aea 2249
2250 nexflag = exflag;
4a693cfc 2251 if (conf_get_int(conf, CONF_alwaysontop) !=
2252 conf_get_int(prev_conf, CONF_alwaysontop)) {
2253 if (conf_get_int(conf, CONF_alwaysontop)) {
32874aea 2254 nexflag |= WS_EX_TOPMOST;
2255 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
2256 SWP_NOMOVE | SWP_NOSIZE);
2257 } else {
2258 nexflag &= ~(WS_EX_TOPMOST);
2259 SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
2260 SWP_NOMOVE | SWP_NOSIZE);
2261 }
2262 }
4a693cfc 2263 if (conf_get_int(conf, CONF_sunken_edge))
32874aea 2264 nexflag |= WS_EX_CLIENTEDGE;
2265 else
2266 nexflag &= ~(WS_EX_CLIENTEDGE);
2267
2268 nflg = flag;
4a693cfc 2269 if (conf_get_int(conf, is_full_screen() ?
2270 CONF_scrollbar_in_fullscreen :
2271 CONF_scrollbar))
32874aea 2272 nflg |= WS_VSCROLL;
2273 else
2274 nflg &= ~WS_VSCROLL;
811f10b3 2275
4a693cfc 2276 if (resize_action == RESIZE_DISABLED ||
811f10b3 2277 is_full_screen())
2278 nflg &= ~WS_THICKFRAME;
2279 else
2280 nflg |= WS_THICKFRAME;
2281
4a693cfc 2282 if (resize_action == RESIZE_DISABLED)
811f10b3 2283 nflg &= ~WS_MAXIMIZEBOX;
32874aea 2284 else
811f10b3 2285 nflg |= WS_MAXIMIZEBOX;
32874aea 2286
2287 if (nflg != flag || nexflag != exflag) {
32874aea 2288 if (nflg != flag)
1e5eefb6 2289 SetWindowLongPtr(hwnd, GWL_STYLE, nflg);
32874aea 2290 if (nexflag != exflag)
1e5eefb6 2291 SetWindowLongPtr(hwnd, GWL_EXSTYLE, nexflag);
32874aea 2292
32874aea 2293 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
2294 SWP_NOACTIVATE | SWP_NOCOPYBITS |
5a73255e 2295 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
2296 SWP_FRAMECHANGED);
c9def1b8 2297
5a73255e 2298 init_lvl = 2;
e44d78b6 2299 }
32874aea 2300 }
5a73255e 2301
e44d78b6 2302 /* Oops */
4a693cfc 2303 if (resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
e44d78b6 2304 force_normal(hwnd);
5a73255e 2305 init_lvl = 2;
2306 }
2307
4a693cfc 2308 set_title(NULL, conf_get_str(conf, CONF_wintitle));
32874aea 2309 if (IsIconic(hwnd)) {
2310 SetWindowText(hwnd,
4a693cfc 2311 conf_get_int(conf, CONF_win_name_always) ?
2312 window_name : icon_name);
3da0b1d2 2313 }
5a73255e 2314
4a693cfc 2315 {
2316 FontSpec *font = conf_get_fontspec(conf, CONF_font);
2317 FontSpec *prev_font = conf_get_fontspec(prev_conf,
2318 CONF_font);
2319
2320 if (!strcmp(font->name, prev_font->name) ||
2321 !strcmp(conf_get_str(conf, CONF_line_codepage),
2322 conf_get_str(prev_conf, CONF_line_codepage)) ||
2323 font->isbold != prev_font->isbold ||
2324 font->height != prev_font->height ||
2325 font->charset != prev_font->charset ||
2326 conf_get_int(conf, CONF_font_quality) !=
2327 conf_get_int(prev_conf, CONF_font_quality) ||
2328 conf_get_int(conf, CONF_vtmode) !=
2329 conf_get_int(prev_conf, CONF_vtmode) ||
2330 conf_get_int(conf, CONF_bold_colour) !=
2331 conf_get_int(prev_conf, CONF_bold_colour) ||
2332 resize_action == RESIZE_DISABLED ||
2333 resize_action == RESIZE_EITHER ||
2334 resize_action != conf_get_int(prev_conf,
2335 CONF_resize_action))
2336 init_lvl = 2;
2337 }
5a73255e 2338
2339 InvalidateRect(hwnd, NULL, TRUE);
2340 reset_window(init_lvl);
7732d38a 2341 net_pending_errors();
4a693cfc 2342
2343 conf_free(prev_conf);
32874aea 2344 }
2345 break;
bc1235d4 2346 case IDM_COPYALL:
887035a5 2347 term_copyall(term);
bc1235d4 2348 break;
63be7767 2349 case IDM_PASTE:
e9609ce5 2350 request_paste(NULL);
63be7767 2351 break;
32874aea 2352 case IDM_CLRSB:
887035a5 2353 term_clrsb(term);
32874aea 2354 break;
2355 case IDM_RESET:
900f9aca 2356 term_pwron(term, TRUE);
e3be8de5 2357 if (ldisc)
2358 ldisc_send(ldisc, NULL, 0, 0);
32874aea 2359 break;
374330e2 2360 case IDM_ABOUT:
32874aea 2361 showabout(hwnd);
374330e2 2362 break;
70133c0e 2363 case IDM_HELP:
cb2708d3 2364 launch_help(hwnd, NULL);
70133c0e 2365 break;
0d6dcf38 2366 case SC_MOUSEMENU:
2367 /*
2368 * We get this if the System menu has been activated
2369 * using the mouse.
2370 */
2371 show_mouseptr(1);
2372 break;
dfca2656 2373 case SC_KEYMENU:
2374 /*
0d6dcf38 2375 * We get this if the System menu has been activated
2376 * using the keyboard. This might happen from within
2377 * TranslateKey, in which case it really wants to be
2378 * followed by a `space' character to actually _bring
2379 * the menu up_ rather than just sitting there in
2380 * `ready to appear' state.
dfca2656 2381 */
0d6dcf38 2382 show_mouseptr(1); /* make sure pointer is visible */
dfca2656 2383 if( lParam == 0 )
2384 PostMessage(hwnd, WM_CHAR, ' ', 0);
2385 break;
a401e5f3 2386 case IDM_FULLSCREEN:
1ba99d2c 2387 flip_full_screen();
2388 break;
32874aea 2389 default:
1f1e3232 2390 if (wParam >= IDM_SAVED_MIN && wParam < IDM_SAVED_MAX) {
32874aea 2391 SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
2392 }
125105d1 2393 if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
2394 int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
125105d1 2395 /*
2396 * Ensure we haven't been sent a bogus SYSCOMMAND
2397 * which would cause us to reference invalid memory
2398 * and crash. Perhaps I'm just too paranoid here.
2399 */
6f2d0cde 2400 if (i >= n_specials)
2401 break;
2402 if (back)
2403 back->special(backhandle, specials[i].code);
2404 net_pending_errors();
125105d1 2405 }
374330e2 2406 }
2407 break;
37508af4 2408
2409#define X_POS(l) ((int)(short)LOWORD(l))
2410#define Y_POS(l) ((int)(short)HIWORD(l))
2411
5a73255e 2412#define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2413#define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
374330e2 2414 case WM_LBUTTONDOWN:
374330e2 2415 case WM_MBUTTONDOWN:
374330e2 2416 case WM_RBUTTONDOWN:
01c034ad 2417 case WM_LBUTTONUP:
2418 case WM_MBUTTONUP:
374330e2 2419 case WM_RBUTTONUP:
ebc0310d 2420 if (message == WM_RBUTTONDOWN &&
4a693cfc 2421 ((wParam & MK_CONTROL) ||
2422 (conf_get_int(conf, CONF_mouse_is_xterm) == 2))) {
63be7767 2423 POINT cursorpos;
2424
ebc0310d 2425 show_mouseptr(1); /* make sure pointer is visible */
63be7767 2426 GetCursorPos(&cursorpos);
2427 TrackPopupMenu(popup_menus[CTXMENU].menu,
2428 TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2429 cursorpos.x, cursorpos.y,
2430 0, hwnd, NULL);
2431 break;
2432 }
01c034ad 2433 {
2434 int button, press;
6908fed7 2435
01c034ad 2436 switch (message) {
32874aea 2437 case WM_LBUTTONDOWN:
2438 button = MBT_LEFT;
472f9eae 2439 wParam |= MK_LBUTTON;
32874aea 2440 press = 1;
2441 break;
2442 case WM_MBUTTONDOWN:
2443 button = MBT_MIDDLE;
472f9eae 2444 wParam |= MK_MBUTTON;
32874aea 2445 press = 1;
2446 break;
2447 case WM_RBUTTONDOWN:
2448 button = MBT_RIGHT;
472f9eae 2449 wParam |= MK_RBUTTON;
32874aea 2450 press = 1;
2451 break;
2452 case WM_LBUTTONUP:
2453 button = MBT_LEFT;
472f9eae 2454 wParam &= ~MK_LBUTTON;
32874aea 2455 press = 0;
2456 break;
2457 case WM_MBUTTONUP:
2458 button = MBT_MIDDLE;
472f9eae 2459 wParam &= ~MK_MBUTTON;
32874aea 2460 press = 0;
2461 break;
2462 case WM_RBUTTONUP:
2463 button = MBT_RIGHT;
472f9eae 2464 wParam &= ~MK_RBUTTON;
32874aea 2465 press = 0;
2466 break;
2d466ffd 2467 default:
2468 button = press = 0; /* shouldn't happen */
01c034ad 2469 }
2470 show_mouseptr(1);
2cae8529 2471 /*
2472 * Special case: in full-screen mode, if the left
2473 * button is clicked in the very top left corner of the
2474 * window, we put up the System menu instead of doing
2475 * selection.
2476 */
52a4d159 2477 {
2478 char mouse_on_hotspot = 0;
2479 POINT pt;
2480
2481 GetCursorPos(&pt);
2482#ifndef NO_MULTIMON
2483 {
2484 HMONITOR mon;
2485 MONITORINFO mi;
2486
2487 mon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
2488
2489 if (mon != NULL) {
2490 mi.cbSize = sizeof(MONITORINFO);
2491 GetMonitorInfo(mon, &mi);
2492
2493 if (mi.rcMonitor.left == pt.x &&
2494 mi.rcMonitor.top == pt.y) {
2495 mouse_on_hotspot = 1;
2496 }
52a4d159 2497 }
2498 }
2499#else
2500 if (pt.x == 0 && pt.y == 0) {
2501 mouse_on_hotspot = 1;
2502 }
2503#endif
2504 if (is_full_screen() && press &&
2505 button == MBT_LEFT && mouse_on_hotspot) {
2506 SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU,
2507 MAKELPARAM(pt.x, pt.y));
2508 return 0;
2509 }
2cae8529 2510 }
52a4d159 2511
01c034ad 2512 if (press) {
32874aea 2513 click(button,
2514 TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
6908fed7 2515 wParam & MK_SHIFT, wParam & MK_CONTROL,
2516 is_alt_pressed());
01c034ad 2517 SetCapture(hwnd);
2518 } else {
fc5b0934 2519 term_mouse(term, button, translate_button(button), MA_RELEASE,
32874aea 2520 TO_CHR_X(X_POS(lParam)),
2521 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
6908fed7 2522 wParam & MK_CONTROL, is_alt_pressed());
472f9eae 2523 if (!(wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
2524 ReleaseCapture();
01c034ad 2525 }
2526 }
374330e2 2527 return 0;
2528 case WM_MOUSEMOVE:
d8a13f62 2529 {
2530 /*
2531 * Windows seems to like to occasionally send MOUSEMOVE
2532 * events even if the mouse hasn't moved. Don't unhide
2533 * the mouse pointer in this case.
2534 */
2535 static WPARAM wp = 0;
2536 static LPARAM lp = 0;
2537 if (wParam != wp || lParam != lp ||
2538 last_mousemove != WM_MOUSEMOVE) {
2539 show_mouseptr(1);
2540 wp = wParam; lp = lParam;
2541 last_mousemove = WM_MOUSEMOVE;
2542 }
2543 }
374330e2 2544 /*
2545 * Add the mouse position and message time to the random
7d6ee6ff 2546 * number noise.
374330e2 2547 */
32874aea 2548 noise_ultralight(lParam);
374330e2 2549
319bfe5a 2550 if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
2551 GetCapture() == hwnd) {
374330e2 2552 Mouse_Button b;
2553 if (wParam & MK_LBUTTON)
6c6e7711 2554 b = MBT_LEFT;
374330e2 2555 else if (wParam & MK_MBUTTON)
6c6e7711 2556 b = MBT_MIDDLE;
374330e2 2557 else
6c6e7711 2558 b = MBT_RIGHT;
fc5b0934 2559 term_mouse(term, b, translate_button(b), MA_DRAG,
2560 TO_CHR_X(X_POS(lParam)),
32874aea 2561 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
6908fed7 2562 wParam & MK_CONTROL, is_alt_pressed());
374330e2 2563 }
374330e2 2564 return 0;
d318ef8e 2565 case WM_NCMOUSEMOVE:
d8a13f62 2566 {
2567 static WPARAM wp = 0;
2568 static LPARAM lp = 0;
2569 if (wParam != wp || lParam != lp ||
2570 last_mousemove != WM_NCMOUSEMOVE) {
2571 show_mouseptr(1);
2572 wp = wParam; lp = lParam;
2573 last_mousemove = WM_NCMOUSEMOVE;
2574 }
2575 }
32874aea 2576 noise_ultralight(lParam);
f9cbc48c 2577 break;
374330e2 2578 case WM_IGNORE_CLIP:
2579 ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */
2580 break;
2581 case WM_DESTROYCLIPBOARD:
2582 if (!ignore_clip)
887035a5 2583 term_deselect(term);
374330e2 2584 ignore_clip = FALSE;
2585 return 0;
2586 case WM_PAINT:
2587 {
2588 PAINTSTRUCT p;
39934deb 2589
32874aea 2590 HideCaret(hwnd);
2591 hdc = BeginPaint(hwnd, &p);
374330e2 2592 if (pal) {
32874aea 2593 SelectPalette(hdc, pal, TRUE);
2594 RealizePalette(hdc);
374330e2 2595 }
39934deb 2596
8788a416 2597 /*
2598 * We have to be careful about term_paint(). It will
2599 * set a bunch of character cells to INVALID and then
2600 * call do_paint(), which will redraw those cells and
2601 * _then mark them as done_. This may not be accurate:
2602 * when painting in WM_PAINT context we are restricted
2603 * to the rectangle which has just been exposed - so if
2604 * that only covers _part_ of a character cell and the
2605 * rest of it was already visible, that remainder will
2606 * not be redrawn at all. Accordingly, we must not
2607 * paint any character cell in a WM_PAINT context which
2608 * already has a pending update due to terminal output.
2609 * The simplest solution to this - and many, many
2610 * thanks to Hung-Te Lin for working all this out - is
2611 * not to do any actual painting at _all_ if there's a
2612 * pending terminal update: just mark the relevant
2613 * character cells as INVALID and wait for the
2614 * scheduled full update to sort it out.
2615 *
2616 * I have a suspicion this isn't the _right_ solution.
2617 * An alternative approach would be to have terminal.c
2618 * separately track what _should_ be on the terminal
2619 * screen and what _is_ on the terminal screen, and
2620 * have two completely different types of redraw (one
2621 * for full updates, which syncs the former with the
2622 * terminal itself, and one for WM_PAINT which syncs
2623 * the latter with the former); yet another possibility
2624 * would be to have the Windows front end do what the
2625 * GTK one already does, and maintain a bitmap of the
2626 * current terminal appearance so that WM_PAINT becomes
2627 * completely trivial. However, this should do for now.
2628 */
887035a5 2629 term_paint(term, hdc,
5a73255e 2630 (p.rcPaint.left-offset_width)/font_width,
2631 (p.rcPaint.top-offset_height)/font_height,
2632 (p.rcPaint.right-offset_width-1)/font_width,
c1b55581 2633 (p.rcPaint.bottom-offset_height-1)/font_height,
8788a416 2634 !term->window_update_pending);
5a73255e 2635
2636 if (p.fErase ||
2637 p.rcPaint.left < offset_width ||
2638 p.rcPaint.top < offset_height ||
887035a5 2639 p.rcPaint.right >= offset_width + font_width*term->cols ||
2640 p.rcPaint.bottom>= offset_height + font_height*term->rows)
5a73255e 2641 {
2642 HBRUSH fillcolour, oldbrush;
2643 HPEN edge, oldpen;
2644 fillcolour = CreateSolidBrush (
c50f9fde 2645 colours[ATTR_DEFBG>>ATTR_BGSHIFT]);
5a73255e 2646 oldbrush = SelectObject(hdc, fillcolour);
2647 edge = CreatePen(PS_SOLID, 0,
c50f9fde 2648 colours[ATTR_DEFBG>>ATTR_BGSHIFT]);
5a73255e 2649 oldpen = SelectObject(hdc, edge);
2650
bfa7a5db 2651 /*
2652 * Jordan Russell reports that this apparently
2653 * ineffectual IntersectClipRect() call masks a
2654 * Windows NT/2K bug causing strange display
2655 * problems when the PuTTY window is taller than
2656 * the primary monitor. It seems harmless enough...
2657 */
2658 IntersectClipRect(hdc,
2659 p.rcPaint.left, p.rcPaint.top,
2660 p.rcPaint.right, p.rcPaint.bottom);
2661
5a73255e 2662 ExcludeClipRect(hdc,
2663 offset_width, offset_height,
887035a5 2664 offset_width+font_width*term->cols,
2665 offset_height+font_height*term->rows);
5a73255e 2666
2667 Rectangle(hdc, p.rcPaint.left, p.rcPaint.top,
2668 p.rcPaint.right, p.rcPaint.bottom);
2669
31fb1866 2670 /* SelectClipRgn(hdc, NULL); */
5a73255e 2671
2672 SelectObject(hdc, oldbrush);
2673 DeleteObject(fillcolour);
2674 SelectObject(hdc, oldpen);
2675 DeleteObject(edge);
2676 }
32874aea 2677 SelectObject(hdc, GetStockObject(SYSTEM_FONT));
2678 SelectObject(hdc, GetStockObject(WHITE_PEN));
2679 EndPaint(hwnd, &p);
2680 ShowCaret(hwnd);
374330e2 2681 }
2682 return 0;
2683 case WM_NETEVENT:
56f8ea28 2684 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2685 * but the only one that's likely to try to overload us is FD_READ.
2686 * This means buffering just one is fine.
2687 */
2688 if (pending_netevent)
2689 enact_pending_netevent();
2690
2691 pending_netevent = TRUE;
2692 pend_netevent_wParam = wParam;
2693 pend_netevent_lParam = lParam;
2694 if (WSAGETSELECTEVENT(lParam) != FD_READ)
2695 enact_pending_netevent();
2696
39934deb 2697 net_pending_errors();
374330e2 2698 return 0;
2699 case WM_SETFOCUS:
1cff1320 2700 term_set_focus(term, TRUE);
32874aea 2701 CreateCaret(hwnd, caretbm, font_width, font_height);
2702 ShowCaret(hwnd);
f8a28d1f 2703 flash_window(0); /* stop */
32874aea 2704 compose_state = 0;
887035a5 2705 term_update(term);
374330e2 2706 break;
2707 case WM_KILLFOCUS:
32874aea 2708 show_mouseptr(1);
1cff1320 2709 term_set_focus(term, FALSE);
32874aea 2710 DestroyCaret();
c1da5066 2711 caret_x = caret_y = -1; /* ensure caret is replaced next time */
887035a5 2712 term_update(term);
374330e2 2713 break;
73251d5d 2714 case WM_ENTERSIZEMOVE:
5a73255e 2715#ifdef RDB_DEBUG_PATCH
2716 debug((27, "WM_ENTERSIZEMOVE"));
2717#endif
32874aea 2718 EnableSizeTip(1);
2719 resizing = TRUE;
3ad8c6db 2720 need_backend_resize = FALSE;
32874aea 2721 break;
73251d5d 2722 case WM_EXITSIZEMOVE:
32874aea 2723 EnableSizeTip(0);
2724 resizing = FALSE;
5a73255e 2725#ifdef RDB_DEBUG_PATCH
2726 debug((27, "WM_EXITSIZEMOVE"));
2727#endif
2728 if (need_backend_resize) {
4a693cfc 2729 term_size(term, conf_get_int(conf, CONF_height),
2730 conf_get_int(conf, CONF_width),
2731 conf_get_int(conf, CONF_savelines));
5a73255e 2732 InvalidateRect(hwnd, NULL, TRUE);
2733 }
32874aea 2734 break;
374330e2 2735 case WM_SIZING:
5a73255e 2736 /*
2737 * This does two jobs:
2738 * 1) Keep the sizetip uptodate
2739 * 2) Make sure the window size is _stepped_ in units of the font size.
2740 */
4a693cfc 2741 resize_action = conf_get_int(conf, CONF_resize_action);
2742 if (resize_action == RESIZE_TERM ||
2743 (resize_action == RESIZE_EITHER && !is_alt_pressed())) {
374330e2 2744 int width, height, w, h, ew, eh;
32874aea 2745 LPRECT r = (LPRECT) lParam;
374330e2 2746
4a693cfc 2747 if (!need_backend_resize && resize_action == RESIZE_EITHER &&
2748 (conf_get_int(conf, CONF_height) != term->rows ||
2749 conf_get_int(conf, CONF_width) != term->cols)) {
5a73255e 2750 /*
0ed2f48e 2751 * Great! It seems that both the terminal size and the
2752 * font size have been changed and the user is now dragging.
2753 *
2754 * It will now be difficult to get back to the configured
2755 * font size!
2756 *
2757 * This would be easier but it seems to be too confusing.
0ed2f48e 2758 */
4a693cfc 2759 conf_set_int(conf, CONF_height, term->rows);
2760 conf_set_int(conf, CONF_width, term->cols);
0ed2f48e 2761
5a73255e 2762 InvalidateRect(hwnd, NULL, TRUE);
2763 need_backend_resize = TRUE;
2764 }
2765
374330e2 2766 width = r->right - r->left - extra_width;
2767 height = r->bottom - r->top - extra_height;
32874aea 2768 w = (width + font_width / 2) / font_width;
2769 if (w < 1)
2770 w = 1;
2771 h = (height + font_height / 2) / font_height;
2772 if (h < 1)
2773 h = 1;
2774 UpdateSizeTip(hwnd, w, h);
374330e2 2775 ew = width - w * font_width;
2776 eh = height - h * font_height;
2777 if (ew != 0) {
2778 if (wParam == WMSZ_LEFT ||
32874aea 2779 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
374330e2 2780 r->left += ew;
2781 else
2782 r->right -= ew;
2783 }
2784 if (eh != 0) {
2785 if (wParam == WMSZ_TOP ||
32874aea 2786 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
374330e2 2787 r->top += eh;
2788 else
2789 r->bottom -= eh;
2790 }
2791 if (ew || eh)
2792 return 1;
2793 else
2794 return 0;
5a73255e 2795 } else {
2796 int width, height, w, h, rv = 0;
4a693cfc 2797 int window_border = conf_get_int(conf, CONF_window_border);
2798 int ex_width = extra_width + (window_border - offset_width) * 2;
2799 int ex_height = extra_height + (window_border - offset_height) * 2;
5a73255e 2800 LPRECT r = (LPRECT) lParam;
2801
2802 width = r->right - r->left - ex_width;
2803 height = r->bottom - r->top - ex_height;
2804
887035a5 2805 w = (width + term->cols/2)/term->cols;
2806 h = (height + term->rows/2)/term->rows;
2807 if ( r->right != r->left + w*term->cols + ex_width)
5a73255e 2808 rv = 1;
2809
2810 if (wParam == WMSZ_LEFT ||
2811 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
887035a5 2812 r->left = r->right - w*term->cols - ex_width;
5a73255e 2813 else
887035a5 2814 r->right = r->left + w*term->cols + ex_width;
5a73255e 2815
887035a5 2816 if (r->bottom != r->top + h*term->rows + ex_height)
5a73255e 2817 rv = 1;
2818
2819 if (wParam == WMSZ_TOP ||
2820 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
887035a5 2821 r->top = r->bottom - h*term->rows - ex_height;
5a73255e 2822 else
887035a5 2823 r->bottom = r->top + h*term->rows + ex_height;
5a73255e 2824
2825 return rv;
374330e2 2826 }
32874aea 2827 /* break; (never reached) */
1ba99d2c 2828 case WM_FULLSCR_ON_MAX:
2829 fullscr_on_max = TRUE;
2830 break;
c1da5066 2831 case WM_MOVE:
2832 sys_cursor_update();
2833 break;
374330e2 2834 case WM_SIZE:
4a693cfc 2835 resize_action = conf_get_int(conf, CONF_resize_action);
5a73255e 2836#ifdef RDB_DEBUG_PATCH
2837 debug((27, "WM_SIZE %s (%d,%d)",
2838 (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
2839 (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
2840 (wParam == SIZE_RESTORED && resizing) ? "to":
2841 (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
2842 "...",
2843 LOWORD(lParam), HIWORD(lParam)));
2844#endif
0ed2f48e 2845 if (wParam == SIZE_MINIMIZED)
32874aea 2846 SetWindowText(hwnd,
4a693cfc 2847 conf_get_int(conf, CONF_win_name_always) ?
2848 window_name : icon_name);
374330e2 2849 if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
32874aea 2850 SetWindowText(hwnd, window_name);
19caf318 2851 if (wParam == SIZE_RESTORED) {
2852 processed_resize = FALSE;
811f10b3 2853 clear_full_screen();
19caf318 2854 if (processed_resize) {
2855 /*
2856 * Inhibit normal processing of this WM_SIZE; a
2857 * secondary one was triggered just now by
2858 * clear_full_screen which contained the correct
2859 * client area size.
2860 */
2861 return 0;
2862 }
2863 }
811f10b3 2864 if (wParam == SIZE_MAXIMIZED && fullscr_on_max) {
811f10b3 2865 fullscr_on_max = FALSE;
19caf318 2866 processed_resize = FALSE;
d0f1bcb4 2867 make_full_screen();
19caf318 2868 if (processed_resize) {
2869 /*
2870 * Inhibit normal processing of this WM_SIZE; a
2871 * secondary one was triggered just now by
2872 * make_full_screen which contained the correct client
2873 * area size.
2874 */
2875 return 0;
2876 }
811f10b3 2877 }
5a73255e 2878
19caf318 2879 processed_resize = TRUE;
2880
4a693cfc 2881 if (resize_action == RESIZE_DISABLED) {
5a73255e 2882 /* A resize, well it better be a minimize. */
2883 reset_window(-1);
2884 } else {
2885
1a6f78fe 2886 int width, height, w, h;
4a693cfc 2887 int window_border = conf_get_int(conf, CONF_window_border);
374330e2 2888
2889 width = LOWORD(lParam);
2890 height = HIWORD(lParam);
5a73255e 2891
232c6eac 2892 if (wParam == SIZE_MAXIMIZED && !was_zoomed) {
2893 was_zoomed = 1;
2894 prev_rows = term->rows;
2895 prev_cols = term->cols;
4a693cfc 2896 if (resize_action == RESIZE_TERM) {
232c6eac 2897 w = width / font_width;
2898 if (w < 1) w = 1;
2899 h = height / font_height;
2900 if (h < 1) h = 1;
2901
4a693cfc 2902 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
232c6eac 2903 }
2904 reset_window(0);
2905 } else if (wParam == SIZE_RESTORED && was_zoomed) {
2906 was_zoomed = 0;
4a693cfc 2907 if (resize_action == RESIZE_TERM) {
2908 w = (width-window_border*2) / font_width;
754fa076 2909 if (w < 1) w = 1;
4a693cfc 2910 h = (height-window_border*2) / font_height;
754fa076 2911 if (h < 1) h = 1;
4a693cfc 2912 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
4e05e104 2913 reset_window(2);
4a693cfc 2914 } else if (resize_action != RESIZE_FONT)
232c6eac 2915 reset_window(2);
2916 else
2917 reset_window(0);
2918 } else if (wParam == SIZE_MINIMIZED) {
2919 /* do nothing */
4a693cfc 2920 } else if (resize_action == RESIZE_TERM ||
2921 (resize_action == RESIZE_EITHER &&
47779c35 2922 !is_alt_pressed())) {
4a693cfc 2923 w = (width-window_border*2) / font_width;
232c6eac 2924 if (w < 1) w = 1;
4a693cfc 2925 h = (height-window_border*2) / font_height;
232c6eac 2926 if (h < 1) h = 1;
2927
2928 if (resizing) {
2929 /*
2930 * Don't call back->size in mid-resize. (To
2931 * prevent massive numbers of resize events
2932 * getting sent down the connection during an NT
2933 * opaque drag.)
2934 */
3ad8c6db 2935 need_backend_resize = TRUE;
4a693cfc 2936 conf_set_int(conf, CONF_height, h);
2937 conf_set_int(conf, CONF_height, w);
232c6eac 2938 } else {
4a693cfc 2939 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
232c6eac 2940 }
2941 } else {
2942 reset_window(0);
374330e2 2943 }
2944 }
c1da5066 2945 sys_cursor_update();
374330e2 2946 return 0;
2947 case WM_VSCROLL:
2948 switch (LOWORD(wParam)) {
32874aea 2949 case SB_BOTTOM:
887035a5 2950 term_scroll(term, -1, 0);
32874aea 2951 break;
2952 case SB_TOP:
887035a5 2953 term_scroll(term, +1, 0);
32874aea 2954 break;
2955 case SB_LINEDOWN:
887035a5 2956 term_scroll(term, 0, +1);
32874aea 2957 break;
2958 case SB_LINEUP:
887035a5 2959 term_scroll(term, 0, -1);
32874aea 2960 break;
2961 case SB_PAGEDOWN:
887035a5 2962 term_scroll(term, 0, +term->rows / 2);
32874aea 2963 break;
2964 case SB_PAGEUP:
887035a5 2965 term_scroll(term, 0, -term->rows / 2);
32874aea 2966 break;
2967 case SB_THUMBPOSITION:
2968 case SB_THUMBTRACK:
887035a5 2969 term_scroll(term, 1, HIWORD(wParam));
32874aea 2970 break;
2971 }
2972 break;
2973 case WM_PALETTECHANGED:
374330e2 2974 if ((HWND) wParam != hwnd && pal != NULL) {
a8327734 2975 HDC hdc = get_ctx(NULL);
374330e2 2976 if (hdc) {
32874aea 2977 if (RealizePalette(hdc) > 0)
2978 UpdateColors(hdc);
2979 free_ctx(hdc);
374330e2 2980 }
2981 }
2982 break;
2983 case WM_QUERYNEWPALETTE:
2984 if (pal != NULL) {
a8327734 2985 HDC hdc = get_ctx(NULL);
374330e2 2986 if (hdc) {
32874aea 2987 if (RealizePalette(hdc) > 0)
2988 UpdateColors(hdc);
2989 free_ctx(hdc);
374330e2 2990 return TRUE;
2991 }
2992 }
2993 return FALSE;
2994 case WM_KEYDOWN:
2995 case WM_SYSKEYDOWN:
c9def1b8 2996 case WM_KEYUP:
2997 case WM_SYSKEYUP:
374330e2 2998 /*
2999 * Add the scan code and keypress timing to the random
7d6ee6ff 3000 * number noise.
374330e2 3001 */
32874aea 3002 noise_ultralight(lParam);
374330e2 3003
3004 /*
3005 * We don't do TranslateMessage since it disassociates the
3006 * resulting CHAR message from the KEYDOWN that sparked it,
3007 * which we occasionally don't want. Instead, we process
3008 * KEYDOWN, and call the Win32 translator functions so that
3009 * we get the translations under _our_ control.
3010 */
3011 {
3012 unsigned char buf[20];
3013 int len;
3014
43e95114 3015 if (wParam == VK_PROCESSKEY) { /* IME PROCESS key */
3016 if (message == WM_KEYDOWN) {
3017 MSG m;
3018 m.hwnd = hwnd;
3019 m.message = WM_KEYDOWN;
3020 m.wParam = wParam;
3021 m.lParam = lParam & 0xdfff;
3022 TranslateMessage(&m);
3023 } else break; /* pass to Windows for default processing */
32874aea 3024 } else {
3025 len = TranslateKey(message, wParam, lParam, buf);
3cf144db 3026 if (len == -1)
32874aea 3027 return DefWindowProc(hwnd, message, wParam, lParam);
5471d09a 3028
b2a1eade 3029 if (len != 0) {
bca9517a 3030 /*
256cb87c 3031 * Interrupt an ongoing paste. I'm not sure
3032 * this is sensible, but for the moment it's
3033 * preferable to having to faff about buffering
3034 * things.
3035 */
887035a5 3036 term_nopaste(term);
256cb87c 3037
3038 /*
bca9517a 3039 * We need not bother about stdin backlogs
3040 * here, because in GUI PuTTY we can't do
3041 * anything about it anyway; there's no means
3042 * of asking Windows to hold off on KEYDOWN
3043 * messages. We _have_ to buffer everything
3044 * we're sent.
3045 */
887035a5 3046 term_seen_key_event(term);
e3be8de5 3047 if (ldisc)
3048 ldisc_send(ldisc, buf, len, 1);
32874aea 3049 show_mouseptr(0);
bca9517a 3050 }
3cf144db 3051 }
374330e2 3052 }
7732d38a 3053 net_pending_errors();
374330e2 3054 return 0;
4eeb7d09 3055 case WM_INPUTLANGCHANGE:
a5ce2e9f 3056 /* wParam == Font number */
3057 /* lParam == Locale */
3058 set_input_locale((HKL)lParam);
c1da5066 3059 sys_cursor_update();
4eeb7d09 3060 break;
fd64bb27 3061 case WM_IME_STARTCOMPOSITION:
3062 {
71f6951d 3063 HIMC hImc = ImmGetContext(hwnd);
3064 ImmSetCompositionFont(hImc, &lfont);
3065 ImmReleaseContext(hwnd, hImc);
71f6951d 3066 }
3067 break;
88485e4d 3068 case WM_IME_COMPOSITION:
3069 {
3070 HIMC hIMC;
3071 int n;
3072 char *buff;
a3cfd0c8 3073
88485e4d 3074 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
3075 osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
3076
3077 if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
3078 break; /* fall back to DefWindowProc */
3079
3080 hIMC = ImmGetContext(hwnd);
3081 n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3082
3083 if (n > 0) {
a3cfd0c8 3084 int i;
3d88e64d 3085 buff = snewn(n, char);
88485e4d 3086 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
a3cfd0c8 3087 /*
3088 * Jaeyoun Chung reports that Korean character
3089 * input doesn't work correctly if we do a single
3090 * luni_send() covering the whole of buff. So
3091 * instead we luni_send the characters one by one.
3092 */
887035a5 3093 term_seen_key_event(term);
2cb50250 3094 for (i = 0; i < n; i += 2) {
e3be8de5 3095 if (ldisc)
3096 luni_send(ldisc, (unsigned short *)(buff+i), 1, 1);
2cb50250 3097 }
88485e4d 3098 free(buff);
3099 }
3100 ImmReleaseContext(hwnd, hIMC);
3101 return 1;
3102 }
3103
4eeb7d09 3104 case WM_IME_CHAR:
3105 if (wParam & 0xFF00) {
3cf144db 3106 unsigned char buf[2];
3107
3108 buf[1] = wParam;
3109 buf[0] = wParam >> 8;
887035a5 3110 term_seen_key_event(term);
e3be8de5 3111 if (ldisc)
3112 lpage_send(ldisc, kbd_codepage, buf, 2, 1);
4eeb7d09 3113 } else {
3114 char c = (unsigned char) wParam;
887035a5 3115 term_seen_key_event(term);
e3be8de5 3116 if (ldisc)
3117 lpage_send(ldisc, kbd_codepage, &c, 1, 1);
3cf144db 3118 }
4eeb7d09 3119 return (0);
374330e2 3120 case WM_CHAR:
3121 case WM_SYSCHAR:
3122 /*
3123 * Nevertheless, we are prepared to deal with WM_CHAR
3124 * messages, should they crop up. So if someone wants to
3125 * post the things to us as part of a macro manoeuvre,
3126 * we're ready to cope.
3127 */
32874aea 3128 {
4eeb7d09 3129 char c = (unsigned char)wParam;
887035a5 3130 term_seen_key_event(term);
e3be8de5 3131 if (ldisc)
3132 lpage_send(ldisc, CP_ACP, &c, 1, 1);
374330e2 3133 }
3134 return 0;
49a332ff 3135 case WM_SYSCOLORCHANGE:
4a693cfc 3136 if (conf_get_int(conf, CONF_system_colour)) {
49a332ff 3137 /* Refresh palette from system colours. */
3138 /* XXX actually this zaps the entire palette. */
3139 systopalette();
3140 init_palette();
3141 /* Force a repaint of the terminal window. */
3142 term_invalidate(term);
3143 }
3144 break;
c44bf5bd 3145 case WM_AGENT_CALLBACK:
3146 {
3147 struct agent_callback *c = (struct agent_callback *)lParam;
3148 c->callback(c->callback_ctx, c->data, c->len);
3149 sfree(c);
3150 }
3151 return 0;
1d432454 3152 case WM_GOT_CLIPDATA:
3153 if (process_clipdata((HGLOBAL)lParam, wParam))
3154 term_do_paste(term);
3155 return 0;
011b0e33 3156 default:
16837214 3157 if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
c1b55581 3158 int shift_pressed=0, control_pressed=0;
011b0e33 3159
3160 if (message == WM_MOUSEWHEEL) {
3161 wheel_accumulator += (short)HIWORD(wParam);
3162 shift_pressed=LOWORD(wParam) & MK_SHIFT;
3163 control_pressed=LOWORD(wParam) & MK_CONTROL;
3164 } else {
3165 BYTE keys[256];
3166 wheel_accumulator += (int)wParam;
3167 if (GetKeyboardState(keys)!=0) {
3168 shift_pressed=keys[VK_SHIFT]&0x80;
3169 control_pressed=keys[VK_CONTROL]&0x80;
3170 }
3171 }
3172
3173 /* process events when the threshold is reached */
3174 while (abs(wheel_accumulator) >= WHEEL_DELTA) {
3175 int b;
3176
3177 /* reduce amount for next time */
3178 if (wheel_accumulator > 0) {
3179 b = MBT_WHEEL_UP;
3180 wheel_accumulator -= WHEEL_DELTA;
3181 } else if (wheel_accumulator < 0) {
3182 b = MBT_WHEEL_DOWN;
3183 wheel_accumulator += WHEEL_DELTA;
3184 } else
3185 break;
3186
4a693cfc 3187 if (send_raw_mouse && shift_pressed &&
3188 !(conf_get_int(conf, CONF_mouse_override))) {
7a3fbdaf 3189 /* Mouse wheel position is in screen coordinates for
3190 * some reason */
3191 POINT p;
3192 p.x = X_POS(lParam); p.y = Y_POS(lParam);
3193 if (ScreenToClient(hwnd, &p)) {
3194 /* send a mouse-down followed by a mouse up */
3195 term_mouse(term, b, translate_button(b),
3196 MA_CLICK,
3197 TO_CHR_X(p.x),
3198 TO_CHR_Y(p.y), shift_pressed,
3199 control_pressed, is_alt_pressed());
3200 term_mouse(term, b, translate_button(b),
3201 MA_RELEASE, TO_CHR_X(p.x),
3202 TO_CHR_Y(p.y), shift_pressed,
3203 control_pressed, is_alt_pressed());
3204 } /* else: not sure when this can fail */
011b0e33 3205 } else {
3206 /* trigger a scroll */
887035a5 3207 term_scroll(term, 0,
3208 b == MBT_WHEEL_UP ?
3209 -term->rows / 2 : term->rows / 2);
011b0e33 3210 }
3211 }
3212 return 0;
3213 }
374330e2 3214 }
3215
5c4dea05 3216 /*
3217 * Any messages we don't process completely above are passed through to
3218 * DefWindowProc() for default processing.
3219 */
32874aea 3220 return DefWindowProc(hwnd, message, wParam, lParam);
374330e2 3221}
3222
3223/*
ec8679e9 3224 * Move the system caret. (We maintain one, even though it's
3225 * invisible, for the benefit of blind people: apparently some
3226 * helper software tracks the system caret, so we should arrange to
3227 * have one.)
3228 */
a8327734 3229void sys_cursor(void *frontend, int x, int y)
32874aea 3230{
c1da5066 3231 int cx, cy;
3232
887035a5 3233 if (!term->has_focus) return;
c1da5066 3234
3235 /*
3236 * Avoid gratuitously re-updating the cursor position and IMM
3237 * window if there's no actual change required.
3238 */
3239 cx = x * font_width + offset_width;
3240 cy = y * font_height + offset_height;
3241 if (cx == caret_x && cy == caret_y)
3242 return;
3243 caret_x = cx;
3244 caret_y = cy;
3245
3246 sys_cursor_update();
3247}
3248
3249static void sys_cursor_update(void)
3250{
88485e4d 3251 COMPOSITIONFORM cf;
3252 HIMC hIMC;
3253
887035a5 3254 if (!term->has_focus) return;
c1da5066 3255
3256 if (caret_x < 0 || caret_y < 0)
3257 return;
3258
3259 SetCaretPos(caret_x, caret_y);
88485e4d 3260
3261 /* IMM calls on Win98 and beyond only */
3262 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
3263
3264 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
3265 osVersion.dwMinorVersion == 0) return; /* 95 */
3266
3267 /* we should have the IMM functions */
3268 hIMC = ImmGetContext(hwnd);
3269 cf.dwStyle = CFS_POINT;
c1da5066 3270 cf.ptCurrentPos.x = caret_x;
3271 cf.ptCurrentPos.y = caret_y;
88485e4d 3272 ImmSetCompositionWindow(hIMC, &cf);
3273
3274 ImmReleaseContext(hwnd, hIMC);
ec8679e9 3275}
3276
3277/*
374330e2 3278 * Draw a line of text in the window, at given character
3279 * coordinates, in given attributes.
3280 *
3281 * We are allowed to fiddle with the contents of `text'.
3282 */
c6958dfe 3283void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
3284 unsigned long attr, int lattr)
32874aea 3285{
374330e2 3286 COLORREF fg, bg, t;
3287 int nfg, nbg, nfont;
3288 HDC hdc = ctx;
59ad2c03 3289 RECT line_box;
3290 int force_manual_underline = 0;
36566009 3291 int fnt_width, char_width;
4eeb7d09 3292 int text_adjust = 0;
14ce9887 3293 int xoffset = 0;
3294 int maxlen, remaining, opaque;
798a50bf 3295 static int *lpDx = NULL;
3296 static int lpDx_len = 0;
3297 int *lpDx_maybe;
4eeb7d09 3298
36566009 3299 lattr &= LATTR_MODE;
3300
3301 char_width = fnt_width = font_width * (1 + (lattr != LATTR_NORM));
3302
4eeb7d09 3303 if (attr & ATTR_WIDE)
3304 char_width *= 2;
59ad2c03 3305
5a73255e 3306 /* Only want the left half of double width lines */
887035a5 3307 if (lattr != LATTR_NORM && x*2 >= term->cols)
5a73255e 3308 return;
3309
c9def1b8 3310 x *= fnt_width;
374330e2 3311 y *= font_height;
5a73255e 3312 x += offset_width;
3313 y += offset_height;
374330e2 3314
4a693cfc 3315 if ((attr & TATTR_ACTCURS) && (cursor_type == 0 || term->big_cursor)) {
cecb13f6 3316 attr &= ~(ATTR_REVERSE|ATTR_BLINK|ATTR_COLOURS);
3317 if (bold_mode == BOLD_COLOURS)
3318 attr &= ~ATTR_BOLD;
3319
3320 /* cursor fg and bg */
3321 attr |= (260 << ATTR_FGSHIFT) | (261 << ATTR_BGSHIFT);
374330e2 3322 }
3323
3324 nfont = 0;
4a693cfc 3325 if (vtmode == VT_POORMAN && lattr != LATTR_NORM) {
4eeb7d09 3326 /* Assume a poorman font is borken in other ways too. */
3327 lattr = LATTR_WIDE;
3328 } else
3329 switch (lattr) {
3330 case LATTR_NORM:
3331 break;
3332 case LATTR_WIDE:
3333 nfont |= FONT_WIDE;
374330e2 3334 break;
4eeb7d09 3335 default:
3336 nfont |= FONT_WIDE + FONT_HIGH;
3337 break;
3338 }
5a73255e 3339 if (attr & ATTR_NARROW)
3340 nfont |= FONT_NARROW;
4eeb7d09 3341
3342 /* Special hack for the VT100 linedraw glyphs. */
36566009 3343 if (text[0] >= 0x23BA && text[0] <= 0x23BD) {
3344 switch ((unsigned char) (text[0])) {
3345 case 0xBA:
3346 text_adjust = -2 * font_height / 5;
3347 break;
3348 case 0xBB:
3349 text_adjust = -1 * font_height / 5;
3350 break;
3351 case 0xBC:
3352 text_adjust = font_height / 5;
3353 break;
3354 case 0xBD:
3355 text_adjust = 2 * font_height / 5;
3356 break;
3357 }
3358 if (lattr == LATTR_TOP || lattr == LATTR_BOT)
3359 text_adjust *= 2;
3360 text[0] = ucsdata.unitab_xterm['q'];
3361 if (attr & ATTR_UNDER) {
3362 attr &= ~ATTR_UNDER;
3363 force_manual_underline = 1;
374330e2 3364 }
3365 }
3366
4eeb7d09 3367 /* Anything left as an original character set is unprintable. */
36566009 3368 if (DIRECT_CHAR(text[0])) {
3369 int i;
3370 for (i = 0; i < len; i++)
3371 text[i] = 0xFFFD;
4eeb7d09 3372 }
3373
3374 /* OEM CP */
36566009 3375 if ((text[0] & CSET_MASK) == CSET_OEMCP)
4eeb7d09 3376 nfont |= FONT_OEM;
3377
37ca32ed 3378 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
37ca32ed 3379 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
374330e2 3380 if (bold_mode == BOLD_FONT && (attr & ATTR_BOLD))
3381 nfont |= FONT_BOLD;
3382 if (und_mode == UND_FONT && (attr & ATTR_UNDER))
3383 nfont |= FONT_UNDERLINE;
4eeb7d09 3384 another_font(nfont);
32874aea 3385 if (!fonts[nfont]) {
3386 if (nfont & FONT_UNDERLINE)
59ad2c03 3387 force_manual_underline = 1;
3388 /* Don't do the same for manual bold, it could be bad news. */
3389
32874aea 3390 nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
59ad2c03 3391 }
4eeb7d09 3392 another_font(nfont);
3393 if (!fonts[nfont])
3394 nfont = FONT_NORMAL;
374330e2 3395 if (attr & ATTR_REVERSE) {
32874aea 3396 t = nfg;
3397 nfg = nbg;
3398 nbg = t;
374330e2 3399 }
cecb13f6 3400 if (bold_mode == BOLD_COLOURS && (attr & ATTR_BOLD)) {
3401 if (nfg < 16) nfg |= 8;
3402 else if (nfg >= 256) nfg |= 1;
3403 }
3404 if (bold_mode == BOLD_COLOURS && (attr & ATTR_BLINK)) {
3405 if (nbg < 16) nbg |= 8;
3406 else if (nbg >= 256) nbg |= 1;
3407 }
374330e2 3408 fg = colours[nfg];
3409 bg = colours[nbg];
32874aea 3410 SelectObject(hdc, fonts[nfont]);
3411 SetTextColor(hdc, fg);
3412 SetBkColor(hdc, bg);
c6958dfe 3413 if (attr & TATTR_COMBINING)
3414 SetBkMode(hdc, TRANSPARENT);
3415 else
3416 SetBkMode(hdc, OPAQUE);
32874aea 3417 line_box.left = x;
3418 line_box.top = y;
4eeb7d09 3419 line_box.right = x + char_width * len;
32874aea 3420 line_box.bottom = y + font_height;
4eeb7d09 3421
5a73255e 3422 /* Only want the left half of double width lines */
887035a5 3423 if (line_box.right > font_width*term->cols+offset_width)
3424 line_box.right = font_width*term->cols+offset_width;
5a73255e 3425
14ce9887 3426 if (font_varpitch) {
3427 /*
3428 * If we're using a variable-pitch font, we unconditionally
3429 * draw the glyphs one at a time and centre them in their
3430 * character cells (which means in particular that we must
798a50bf 3431 * disable the lpDx mechanism). This gives slightly odd but
14ce9887 3432 * generally reasonable results.
3433 */
3434 xoffset = char_width / 2;
3435 SetTextAlign(hdc, TA_TOP | TA_CENTER | TA_NOUPDATECP);
798a50bf 3436 lpDx_maybe = NULL;
14ce9887 3437 maxlen = 1;
3438 } else {
3439 /*
3440 * In a fixed-pitch font, we draw the whole string in one go
3441 * in the normal way.
3442 */
3443 xoffset = 0;
3444 SetTextAlign(hdc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
798a50bf 3445 lpDx_maybe = lpDx;
14ce9887 3446 maxlen = len;
3447 }
3448
3449 opaque = TRUE; /* start by erasing the rectangle */
3450 for (remaining = len; remaining > 0;
3451 text += len, remaining -= len, x += char_width * len) {
3452 len = (maxlen < remaining ? maxlen : remaining);
3453
798a50bf 3454 if (len > lpDx_len) {
3455 if (len > lpDx_len) {
3456 lpDx_len = len * 9 / 8 + 16;
3457 lpDx = sresize(lpDx, lpDx_len, int);
3458 }
3459 }
3460 {
3461 int i;
3462 for (i = 0; i < len; i++)
3463 lpDx[i] = char_width;
3464 }
3465
14ce9887 3466 /* We're using a private area for direct to font. (512 chars.) */
3467 if (ucsdata.dbcs_screenfont && (text[0] & CSET_MASK) == CSET_ACP) {
3468 /* Ho Hum, dbcs fonts are a PITA! */
3469 /* To display on W9x I have to convert to UCS */
3470 static wchar_t *uni_buf = 0;
3471 static int uni_len = 0;
3472 int nlen, mptr;
3473 if (len > uni_len) {
3474 sfree(uni_buf);
3475 uni_len = len;
3476 uni_buf = snewn(uni_len, wchar_t);
3477 }
36566009 3478
14ce9887 3479 for(nlen = mptr = 0; mptr<len; mptr++) {
3480 uni_buf[nlen] = 0xFFFD;
3481 if (IsDBCSLeadByteEx(ucsdata.font_codepage,
3482 (BYTE) text[mptr])) {
3483 char dbcstext[2];
3484 dbcstext[0] = text[mptr] & 0xFF;
3485 dbcstext[1] = text[mptr+1] & 0xFF;
798a50bf 3486 lpDx[nlen] += char_width;
14ce9887 3487 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
3488 dbcstext, 2, uni_buf+nlen, 1);
3489 mptr++;
3490 }
3491 else
3492 {
3493 char dbcstext[1];
3494 dbcstext[0] = text[mptr] & 0xFF;
3495 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
3496 dbcstext, 1, uni_buf+nlen, 1);
3497 }
3498 nlen++;
3499 }
3500 if (nlen <= 0)
3501 return; /* Eeek! */
3502
3503 ExtTextOutW(hdc, x + xoffset,
3504 y - font_height * (lattr == LATTR_BOT) + text_adjust,
3505 ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
3506 &line_box, uni_buf, nlen,
798a50bf 3507 lpDx_maybe);
14ce9887 3508 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
3509 SetBkMode(hdc, TRANSPARENT);
3510 ExtTextOutW(hdc, x + xoffset - 1,
3511 y - font_height * (lattr ==
3512 LATTR_BOT) + text_adjust,
798a50bf 3513 ETO_CLIPPED, &line_box, uni_buf, nlen, lpDx_maybe);
14ce9887 3514 }
36566009 3515
798a50bf 3516 lpDx[0] = -1;
14ce9887 3517 } else if (DIRECT_FONT(text[0])) {
3518 static char *directbuf = NULL;
3519 static int directlen = 0;
3520 int i;
3521 if (len > directlen) {
3522 directlen = len;
3523 directbuf = sresize(directbuf, directlen, char);
3524 }
4eeb7d09 3525
14ce9887 3526 for (i = 0; i < len; i++)
3527 directbuf[i] = text[i] & 0xFF;
3528
3529 ExtTextOut(hdc, x + xoffset,
3530 y - font_height * (lattr == LATTR_BOT) + text_adjust,
3531 ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
798a50bf 3532 &line_box, directbuf, len, lpDx_maybe);
14ce9887 3533 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
3534 SetBkMode(hdc, TRANSPARENT);
3535
3536 /* GRR: This draws the character outside its box and
3537 * can leave 'droppings' even with the clip box! I
3538 * suppose I could loop it one character at a time ...
3539 * yuk.
3540 *
3541 * Or ... I could do a test print with "W", and use +1
3542 * or -1 for this shift depending on if the leftmost
3543 * column is blank...
3544 */
3545 ExtTextOut(hdc, x + xoffset - 1,
3546 y - font_height * (lattr ==
3547 LATTR_BOT) + text_adjust,
798a50bf 3548 ETO_CLIPPED, &line_box, directbuf, len, lpDx_maybe);
14ce9887 3549 }
3550 } else {
3551 /* And 'normal' unicode characters */
3552 static WCHAR *wbuf = NULL;
3553 static int wlen = 0;
3554 int i;
3555
3556 if (wlen < len) {
3557 sfree(wbuf);
3558 wlen = len;
3559 wbuf = snewn(wlen, WCHAR);
3560 }
c6958dfe 3561
14ce9887 3562 for (i = 0; i < len; i++)
3563 wbuf[i] = text[i];
3564
3565 /* print Glyphs as they are, without Windows' Shaping*/
3566 general_textout(hdc, x + xoffset,
3567 y - font_height * (lattr==LATTR_BOT) + text_adjust,
798a50bf 3568 &line_box, wbuf, len, lpDx,
14ce9887 3569 opaque && !(attr & TATTR_COMBINING));
3570
3571 /* And the shadow bold hack. */
3572 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
3573 SetBkMode(hdc, TRANSPARENT);
3574 ExtTextOutW(hdc, x + xoffset - 1,
3575 y - font_height * (lattr ==
3576 LATTR_BOT) + text_adjust,
798a50bf 3577 ETO_CLIPPED, &line_box, wbuf, len, lpDx_maybe);
14ce9887 3578 }
3579 }
c6958dfe 3580
14ce9887 3581 /*
3582 * If we're looping round again, stop erasing the background
3583 * rectangle.
3584 */
3585 SetBkMode(hdc, TRANSPARENT);
3586 opaque = FALSE;
374330e2 3587 }
4eeb7d09 3588 if (lattr != LATTR_TOP && (force_manual_underline ||
3589 (und_mode == UND_LINE
3590 && (attr & ATTR_UNDER)))) {
32874aea 3591 HPEN oldpen;
4eeb7d09 3592 int dec = descent;
3593 if (lattr == LATTR_BOT)
3594 dec = dec * 2 - font_height;
3595
32874aea 3596 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, fg));
4eeb7d09 3597 MoveToEx(hdc, x, y + dec, NULL);
3598 LineTo(hdc, x + len * char_width, y + dec);
32874aea 3599 oldpen = SelectObject(hdc, oldpen);
3600 DeleteObject(oldpen);
374330e2 3601 }
4eeb7d09 3602}
3603
c6958dfe 3604/*
3605 * Wrapper that handles combining characters.
3606 */
3607void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3608 unsigned long attr, int lattr)
3609{
3610 if (attr & TATTR_COMBINING) {
3611 unsigned long a = 0;
3612 attr &= ~TATTR_COMBINING;
3613 while (len--) {
3614 do_text_internal(ctx, x, y, text, 1, attr | a, lattr);
3615 text++;
3616 a = TATTR_COMBINING;
3617 }
3618 } else
3619 do_text_internal(ctx, x, y, text, len, attr, lattr);
3620}
3621
36566009 3622void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
4eeb7d09 3623 unsigned long attr, int lattr)
3624{
3625
3626 int fnt_width;
3627 int char_width;
3628 HDC hdc = ctx;
4a693cfc 3629 int ctype = cursor_type;
4eeb7d09 3630
8af95aa3 3631 lattr &= LATTR_MODE;
3632
887035a5 3633 if ((attr & TATTR_ACTCURS) && (ctype == 0 || term->big_cursor)) {
36566009 3634 if (*text != UCSWIDE) {
4eeb7d09 3635 do_text(ctx, x, y, text, len, attr, lattr);
3636 return;
3637 }
3638 ctype = 2;
3639 attr |= TATTR_RIGHTCURS;
3640 }
3641
3642 fnt_width = char_width = font_width * (1 + (lattr != LATTR_NORM));
3643 if (attr & ATTR_WIDE)
3644 char_width *= 2;
3645 x *= fnt_width;
3646 y *= font_height;
5a73255e 3647 x += offset_width;
3648 y += offset_height;
4eeb7d09 3649
887035a5 3650 if ((attr & TATTR_PASCURS) && (ctype == 0 || term->big_cursor)) {
374330e2 3651 POINT pts[5];
32874aea 3652 HPEN oldpen;
374330e2 3653 pts[0].x = pts[1].x = pts[4].x = x;
4eeb7d09 3654 pts[2].x = pts[3].x = x + char_width - 1;
374330e2 3655 pts[0].y = pts[3].y = pts[4].y = y;
32874aea 3656 pts[1].y = pts[2].y = y + font_height - 1;
cecb13f6 3657 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[261]));
32874aea 3658 Polyline(hdc, pts, 5);
3659 oldpen = SelectObject(hdc, oldpen);
3660 DeleteObject(oldpen);
4eeb7d09 3661 } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) && ctype != 0) {
32874aea 3662 int startx, starty, dx, dy, length, i;
4eeb7d09 3663 if (ctype == 1) {
32874aea 3664 startx = x;
3665 starty = y + descent;
3666 dx = 1;
3667 dy = 0;
4eeb7d09 3668 length = char_width;
32874aea 3669 } else {
4e30ff69 3670 int xadjust = 0;
4eeb7d09 3671 if (attr & TATTR_RIGHTCURS)
3672 xadjust = char_width - 1;
32874aea 3673 startx = x + xadjust;
3674 starty = y;
3675 dx = 0;
3676 dy = 1;
3677 length = font_height;
3678 }
4eeb7d09 3679 if (attr & TATTR_ACTCURS) {
32874aea 3680 HPEN oldpen;
3681 oldpen =
fdc94dd1 3682 SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[261]));
32874aea 3683 MoveToEx(hdc, startx, starty, NULL);
3684 LineTo(hdc, startx + dx * length, starty + dy * length);
3685 oldpen = SelectObject(hdc, oldpen);
3686 DeleteObject(oldpen);
3687 } else {
3688 for (i = 0; i < length; i++) {
3689 if (i % 2 == 0) {
fdc94dd1 3690 SetPixel(hdc, startx, starty, colours[261]);
32874aea 3691 }
3692 startx += dx;
3693 starty += dy;
3694 }
3695 }
4e30ff69 3696 }
374330e2 3697}
3698
5a73255e 3699/* This function gets the actual width of a character in the normal font.
3700 */
2102eb8a 3701int char_width(Context ctx, int uc) {
5a73255e 3702 HDC hdc = ctx;
3703 int ibuf = 0;
3704
3705 /* If the font max is the same as the font ave width then this
3706 * function is a no-op.
3707 */
3708 if (!font_dualwidth) return 1;
3709
3710 switch (uc & CSET_MASK) {
36566009 3711 case CSET_ASCII:
21d2b241 3712 uc = ucsdata.unitab_line[uc & 0xFF];
5a73255e 3713 break;
36566009 3714 case CSET_LINEDRW:
21d2b241 3715 uc = ucsdata.unitab_xterm[uc & 0xFF];
5a73255e 3716 break;
36566009 3717 case CSET_SCOACS:
21d2b241 3718 uc = ucsdata.unitab_scoacs[uc & 0xFF];
5a73255e 3719 break;
3720 }
3721 if (DIRECT_FONT(uc)) {
21d2b241 3722 if (ucsdata.dbcs_screenfont) return 1;
5a73255e 3723
3724 /* Speedup, I know of no font where ascii is the wrong width */
36566009 3725 if ((uc&~CSET_MASK) >= ' ' && (uc&~CSET_MASK)<= '~')
5a73255e 3726 return 1;
3727
36566009 3728 if ( (uc & CSET_MASK) == CSET_ACP ) {
5a73255e 3729 SelectObject(hdc, fonts[FONT_NORMAL]);
36566009 3730 } else if ( (uc & CSET_MASK) == CSET_OEMCP ) {
5a73255e 3731 another_font(FONT_OEM);
3732 if (!fonts[FONT_OEM]) return 0;
3733
3734 SelectObject(hdc, fonts[FONT_OEM]);
3735 } else
3736 return 0;
3737
36566009 3738 if ( GetCharWidth32(hdc, uc&~CSET_MASK, uc&~CSET_MASK, &ibuf) != 1 &&
3739 GetCharWidth(hdc, uc&~CSET_MASK, uc&~CSET_MASK, &ibuf) != 1)
5a73255e 3740 return 0;
3741 } else {
3742 /* Speedup, I know of no font where ascii is the wrong width */
3743 if (uc >= ' ' && uc <= '~') return 1;
3744
3745 SelectObject(hdc, fonts[FONT_NORMAL]);
3746 if ( GetCharWidth32W(hdc, uc, uc, &ibuf) == 1 )
3747 /* Okay that one worked */ ;
3748 else if ( GetCharWidthW(hdc, uc, uc, &ibuf) == 1 )
3749 /* This should work on 9x too, but it's "less accurate" */ ;
3750 else
3751 return 0;
3752 }
3753
3754 ibuf += font_width / 2 -1;
3755 ibuf /= font_width;
3756
3757 return ibuf;
3758}
3759
374330e2 3760/*
c9def1b8 3761 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
58c840dc 3762 * codes. Returns number of bytes used, zero to drop the message,
3763 * -1 to forward the message to Windows, or another negative number
3764 * to indicate a NUL-terminated "special" string.
374330e2 3765 */
3cf144db 3766static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
32874aea 3767 unsigned char *output)
3768{
374330e2 3769 BYTE keystate[256];
32874aea 3770 int scan, left_alt = 0, key_down, shift_state;
3771 int r, i, code;
3772 unsigned char *p = output;
4eeb7d09 3773 static int alt_sum = 0;
4a693cfc 3774 int funky_type = conf_get_int(conf, CONF_funky_type);
3775 int no_applic_k = conf_get_int(conf, CONF_no_applic_k);
3776 int ctrlaltkeys = conf_get_int(conf, CONF_ctrlaltkeys);
3777 int nethack_keypad = conf_get_int(conf, CONF_nethack_keypad);
374330e2 3778
00e3ba0f 3779 HKL kbd_layout = GetKeyboardLayout(0);
3780
c264be08 3781 /* keys is for ToAsciiEx. There's some ick here, see below. */
3782 static WORD keys[3];
0c50ef57 3783 static int compose_char = 0;
4a693cfc 3784 static WPARAM compose_keycode = 0;
32874aea 3785
c9def1b8 3786 r = GetKeyboardState(keystate);
32874aea 3787 if (!r)
3788 memset(keystate, 0, sizeof(keystate));
3789 else {
ec55b220 3790#if 0
4eeb7d09 3791#define SHOW_TOASCII_RESULT
32874aea 3792 { /* Tell us all about key events */
3793 static BYTE oldstate[256];
3794 static int first = 1;
3795 static int scan;
3796 int ch;
3797 if (first)
3798 memcpy(oldstate, keystate, sizeof(oldstate));
3799 first = 0;
3800
3801 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
3802 debug(("+"));
3803 } else if ((HIWORD(lParam) & KF_UP)
3804 && scan == (HIWORD(lParam) & 0xFF)) {
3805 debug((". U"));
3806 } else {
3807 debug((".\n"));
3808 if (wParam >= VK_F1 && wParam <= VK_F20)
3809 debug(("K_F%d", wParam + 1 - VK_F1));
3810 else
3811 switch (wParam) {
3812 case VK_SHIFT:
3813 debug(("SHIFT"));
3814 break;
3815 case VK_CONTROL:
3816 debug(("CTRL"));
3817 break;
3818 case VK_MENU:
3819 debug(("ALT"));
3820 break;
3821 default:
3822 debug(("VK_%02x", wParam));
3823 }
3824 if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
3825 debug(("*"));
3826 debug((", S%02x", scan = (HIWORD(lParam) & 0xFF)));
3827
3828 ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
3829 if (ch >= ' ' && ch <= '~')
3830 debug((", '%c'", ch));
3831 else if (ch)
3832 debug((", $%02x", ch));
3833
3834 if (keys[0])
3835 debug((", KB0=%02x", keys[0]));
3836 if (keys[1])
3837 debug((", KB1=%02x", keys[1]));
3838 if (keys[2])
3839 debug((", KB2=%02x", keys[2]));
3840
3841 if ((keystate[VK_SHIFT] & 0x80) != 0)
3842 debug((", S"));
3843 if ((keystate[VK_CONTROL] & 0x80) != 0)
3844 debug((", C"));
3845 if ((HIWORD(lParam) & KF_EXTENDED))
3846 debug((", E"));
3847 if ((HIWORD(lParam) & KF_UP))
3848 debug((", U"));
3849 }
3850
3851 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
3852 else if ((HIWORD(lParam) & KF_UP))
3853 oldstate[wParam & 0xFF] ^= 0x80;
3854 else
3855 oldstate[wParam & 0xFF] ^= 0x81;
3856
3857 for (ch = 0; ch < 256; ch++)
3858 if (oldstate[ch] != keystate[ch])
3859 debug((", M%02x=%02x", ch, keystate[ch]));
3860
3861 memcpy(oldstate, keystate, sizeof(oldstate));
3862 }
ec55b220 3863#endif
3864
32874aea 3865 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
2cba1186 3866 keystate[VK_RMENU] = keystate[VK_MENU];
3867 }
3868
c9def1b8 3869
3870 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
4a693cfc 3871 if ((funky_type == FUNKY_VT400 ||
3872 (funky_type <= FUNKY_LINUX && term->app_keypad_keys &&
3873 !no_applic_k))
32874aea 3874 && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
c9def1b8 3875
3876 wParam = VK_EXECUTE;
3877
3878 /* UnToggle NUMLock */
32874aea 3879 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
3880 keystate[VK_NUMLOCK] ^= 1;
c9def1b8 3881 }
3882
3883 /* And write back the 'adjusted' state */
32874aea 3884 SetKeyboardState(keystate);
c9def1b8 3885 }
3886
3887 /* Disable Auto repeat if required */
887035a5 3888 if (term->repeat_off &&
3889 (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
32874aea 3890 return 0;
c9def1b8 3891
32874aea 3892 if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
c9def1b8 3893 left_alt = 1;
3894
32874aea 3895 key_down = ((HIWORD(lParam) & KF_UP) == 0);
c9def1b8 3896
95bbe1ae 3897 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
32874aea 3898 if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
4a693cfc 3899 if (ctrlaltkeys)
95bbe1ae 3900 keystate[VK_MENU] = 0;
3901 else {
3902 keystate[VK_RMENU] = 0x80;
3903 left_alt = 0;
3904 }
3905 }
c9def1b8 3906
3907 scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
32874aea 3908 shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
3909 + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
374330e2 3910
95bbe1ae 3911 /* Note if AltGr was pressed and if it was used as a compose key */
3912 if (!compose_state) {
4a693cfc 3913 compose_keycode = 0x100;
3914 if (conf_get_int(conf, CONF_compose_key)) {
32874aea 3915 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
4a693cfc 3916 compose_keycode = wParam;
95bbe1ae 3917 }
3918 if (wParam == VK_APPS)
4a693cfc 3919 compose_keycode = wParam;
95bbe1ae 3920 }
3921
4a693cfc 3922 if (wParam == compose_keycode) {
32874aea 3923 if (compose_state == 0
3924 && (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0) compose_state =
3925 1;
3926 else if (compose_state == 1 && (HIWORD(lParam) & KF_UP))
95bbe1ae 3927 compose_state = 2;
3928 else
3929 compose_state = 0;
32874aea 3930 } else if (compose_state == 1 && wParam != VK_CONTROL)
95bbe1ae 3931 compose_state = 0;
3932
32874aea 3933 if (compose_state > 1 && left_alt)
3934 compose_state = 0;
67c339f7 3935
c9def1b8 3936 /* Sanitize the number pad if not using a PC NumPad */
4a693cfc 3937 if (left_alt || (term->app_keypad_keys && !no_applic_k
3938 && funky_type != FUNKY_XTERM)
3939 || funky_type == FUNKY_VT400 || nethack_keypad || compose_state) {
32874aea 3940 if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
c9def1b8 3941 int nParam = 0;
32874aea 3942 switch (wParam) {
3943 case VK_INSERT:
3944 nParam = VK_NUMPAD0;
3945 break;
3946 case VK_END:
3947 nParam = VK_NUMPAD1;
3948 break;
3949 case VK_DOWN:
3950 nParam = VK_NUMPAD2;
3951 break;
3952 case VK_NEXT:
3953 nParam = VK_NUMPAD3;
3954 break;
3955 case VK_LEFT:
3956 nParam = VK_NUMPAD4;
3957 break;
3958 case VK_CLEAR:
3959 nParam = VK_NUMPAD5;
3960 break;
3961 case VK_RIGHT:
3962 nParam = VK_NUMPAD6;
3963 break;
3964 case VK_HOME:
3965 nParam = VK_NUMPAD7;
3966 break;
3967 case VK_UP:
3968 nParam = VK_NUMPAD8;
3969 break;
3970 case VK_PRIOR:
3971 nParam = VK_NUMPAD9;
3972 break;
3973 case VK_DELETE:
3974 nParam = VK_DECIMAL;
3975 break;
c9def1b8 3976 }
32874aea 3977 if (nParam) {
3978 if (keystate[VK_NUMLOCK] & 1)
3979 shift_state |= 1;
c9def1b8 3980 wParam = nParam;
3981 }
25d39ef6 3982 }
3983 }
3984
c9def1b8 3985 /* If a key is pressed and AltGr is not active */
32874aea 3986 if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !compose_state) {
3987 /* Okay, prepare for most alts then ... */
3988 if (left_alt)
3989 *p++ = '\033';
374330e2 3990
c9def1b8 3991 /* Lets see if it's a pattern we know all about ... */
3992 if (wParam == VK_PRIOR && shift_state == 1) {
32874aea 3993 SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
3994 return 0;
c9def1b8 3995 }
153580da 3996 if (wParam == VK_PRIOR && shift_state == 2) {
3997 SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
3998 return 0;
3999 }
c9def1b8 4000 if (wParam == VK_NEXT && shift_state == 1) {
32874aea 4001 SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
4002 return 0;
4003 }
153580da 4004 if (wParam == VK_NEXT && shift_state == 2) {
4005 SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
4006 return 0;
4007 }
cf6ddb95 4008 if ((wParam == VK_PRIOR || wParam == VK_NEXT) && shift_state == 3) {
4009 term_scroll_to_selection(term, (wParam == VK_PRIOR ? 0 : 1));
4010 return 0;
4011 }
32874aea 4012 if (wParam == VK_INSERT && shift_state == 1) {
e9609ce5 4013 request_paste(NULL);
32874aea 4014 return 0;
4015 }
4a693cfc 4016 if (left_alt && wParam == VK_F4 && conf_get_int(conf, CONF_alt_f4)) {
32874aea 4017 return -1;
c9def1b8 4018 }
4a693cfc 4019 if (left_alt && wParam == VK_SPACE && conf_get_int(conf,
4020 CONF_alt_space)) {
32874aea 4021 SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
4022 return -1;
c9def1b8 4023 }
4a693cfc 4024 if (left_alt && wParam == VK_RETURN &&
4025 conf_get_int(conf, CONF_fullscreenonaltenter) &&
4026 (conf_get_int(conf, CONF_resize_action) != RESIZE_DISABLED)) {
0ed2f48e 4027 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
4028 flip_full_screen();
8f57d753 4029 return -1;
4030 }
ec55b220 4031 /* Control-Numlock for app-keypad mode switch */
4032 if (wParam == VK_PAUSE && shift_state == 2) {
887035a5 4033 term->app_keypad_keys ^= 1;
ec55b220 4034 return 0;
4035 }
374330e2 4036
c9def1b8 4037 /* Nethack keypad */
4a693cfc 4038 if (nethack_keypad && !left_alt) {
32874aea 4039 switch (wParam) {
4040 case VK_NUMPAD1:
6e67448b 4041 *p++ = "bB\002\002"[shift_state & 3];
32874aea 4042 return p - output;
4043 case VK_NUMPAD2:
6e67448b 4044 *p++ = "jJ\012\012"[shift_state & 3];
32874aea 4045 return p - output;
4046 case VK_NUMPAD3:
6e67448b 4047 *p++ = "nN\016\016"[shift_state & 3];
32874aea 4048 return p - output;
4049 case VK_NUMPAD4:
6e67448b 4050 *p++ = "hH\010\010"[shift_state & 3];
32874aea 4051 return p - output;
4052 case VK_NUMPAD5:
4053 *p++ = shift_state ? '.' : '.';
4054 return p - output;
4055 case VK_NUMPAD6:
6e67448b 4056 *p++ = "lL\014\014"[shift_state & 3];
32874aea 4057 return p - output;
4058 case VK_NUMPAD7:
6e67448b 4059 *p++ = "yY\031\031"[shift_state & 3];
32874aea 4060 return p - output;
4061 case VK_NUMPAD8:
6e67448b 4062 *p++ = "kK\013\013"[shift_state & 3];
32874aea 4063 return p - output;
4064 case VK_NUMPAD9:
6e67448b 4065 *p++ = "uU\025\025"[shift_state & 3];
32874aea 4066 return p - output;
4067 }
c9def1b8 4068 }
4069
4070 /* Application Keypad */
4071 if (!left_alt) {
32874aea 4072 int xkey = 0;
4073
4a693cfc 4074 if (funky_type == FUNKY_VT400 ||
4075 (funky_type <= FUNKY_LINUX &&
4076 term->app_keypad_keys && !no_applic_k)) switch (wParam) {
32874aea 4077 case VK_EXECUTE:
4078 xkey = 'P';
4079 break;
4080 case VK_DIVIDE:
4081 xkey = 'Q';
4082 break;
4083 case VK_MULTIPLY:
4084 xkey = 'R';
4085 break;
4086 case VK_SUBTRACT:
4087 xkey = 'S';
4088 break;
4089 }
4a693cfc 4090 if (term->app_keypad_keys && !no_applic_k)
32874aea 4091 switch (wParam) {
4092 case VK_NUMPAD0:
4093 xkey = 'p';
4094 break;
4095 case VK_NUMPAD1:
4096 xkey = 'q';
4097 break;
4098 case VK_NUMPAD2:
4099 xkey = 'r';
4100 break;
4101 case VK_NUMPAD3:
4102 xkey = 's';
4103 break;
4104 case VK_NUMPAD4:
4105 xkey = 't';
4106 break;
4107 case VK_NUMPAD5:
4108 xkey = 'u';
4109 break;
4110 case VK_NUMPAD6:
4111 xkey = 'v';
4112 break;
4113 case VK_NUMPAD7:
4114 xkey = 'w';
4115 break;
4116 case VK_NUMPAD8:
4117 xkey = 'x';
4118 break;
4119 case VK_NUMPAD9:
4120 xkey = 'y';
4121 break;
4122
4123 case VK_DECIMAL:
4124 xkey = 'n';
4125 break;
4126 case VK_ADD:
4a693cfc 4127 if (funky_type == FUNKY_XTERM) {
32874aea 4128 if (shift_state)
4129 xkey = 'l';
4130 else
4131 xkey = 'k';
4132 } else if (shift_state)
4133 xkey = 'm';
c9def1b8 4134 else
32874aea 4135 xkey = 'l';
4136 break;
4137
4138 case VK_DIVIDE:
4a693cfc 4139 if (funky_type == FUNKY_XTERM)
32874aea 4140 xkey = 'o';
4141 break;
4142 case VK_MULTIPLY:
4a693cfc 4143 if (funky_type == FUNKY_XTERM)
32874aea 4144 xkey = 'j';
4145 break;
4146 case VK_SUBTRACT:
4a693cfc 4147 if (funky_type == FUNKY_XTERM)
32874aea 4148 xkey = 'm';
4149 break;
4150
4151 case VK_RETURN:
4152 if (HIWORD(lParam) & KF_EXTENDED)
4153 xkey = 'M';
4154 break;
c9def1b8 4155 }
32874aea 4156 if (xkey) {
887035a5 4157 if (term->vt52_mode) {
32874aea 4158 if (xkey >= 'P' && xkey <= 'S')
4159 p += sprintf((char *) p, "\x1B%c", xkey);
4160 else
4161 p += sprintf((char *) p, "\x1B?%c", xkey);
4162 } else
4163 p += sprintf((char *) p, "\x1BO%c", xkey);
4164 return p - output;
c9def1b8 4165 }
4166 }
4167
32874aea 4168 if (wParam == VK_BACK && shift_state == 0) { /* Backspace */
4a693cfc 4169 *p++ = (conf_get_int(conf, CONF_bksp_is_delete) ? 0x7F : 0x08);
a5f3e637 4170 *p++ = 0;
4171 return -2;
c9def1b8 4172 }
e8e8d6e2 4173 if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */
4174 /* We do the opposite of what is configured */
4a693cfc 4175 *p++ = (conf_get_int(conf, CONF_bksp_is_delete) ? 0x08 : 0x7F);
e8e8d6e2 4176 *p++ = 0;
4177 return -2;
4178 }
32874aea 4179 if (wParam == VK_TAB && shift_state == 1) { /* Shift tab */
4180 *p++ = 0x1B;
4181 *p++ = '[';
4182 *p++ = 'Z';
4183 return p - output;
c9def1b8 4184 }
32874aea 4185 if (wParam == VK_SPACE && shift_state == 2) { /* Ctrl-Space */
4186 *p++ = 0;
4187 return p - output;
c9def1b8 4188 }
32874aea 4189 if (wParam == VK_SPACE && shift_state == 3) { /* Ctrl-Shift-Space */
4190 *p++ = 160;
4191 return p - output;
c9def1b8 4192 }
32874aea 4193 if (wParam == VK_CANCEL && shift_state == 2) { /* Ctrl-Break */
58c840dc 4194 if (back)
4195 back->special(backhandle, TS_BRK);
4196 return 0;
c9def1b8 4197 }
32874aea 4198 if (wParam == VK_PAUSE) { /* Break/Pause */
4199 *p++ = 26;
4200 *p++ = 0;
4201 return -2;
95bbe1ae 4202 }
c9def1b8 4203 /* Control-2 to Control-8 are special */
32874aea 4204 if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
4205 *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
c9def1b8 4206 return p - output;
4207 }
237f2b6e 4208 if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
c9def1b8 4209 *p++ = 0x1F;
4210 return p - output;
4211 }
e414a330 4212 if (shift_state == 2 && (wParam == 0xDF || wParam == 0xDC)) {
c9def1b8 4213 *p++ = 0x1C;
4214 return p - output;
4215 }
9aa461e4 4216 if (shift_state == 3 && wParam == 0xDE) {
4217 *p++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
4218 return p - output;
4219 }
887035a5 4220 if (shift_state == 0 && wParam == VK_RETURN && term->cr_lf_return) {
32874aea 4221 *p++ = '\r';
4222 *p++ = '\n';
c9def1b8 4223 return p - output;
4224 }
374330e2 4225
c5e9c988 4226 /*
c9def1b8 4227 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
4228 * for integer decimal nn.)
4229 *
4230 * We also deal with the weird ones here. Linux VCs replace F1
4231 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
4232 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
4233 * respectively.
c5e9c988 4234 */
c9def1b8 4235 code = 0;
4236 switch (wParam) {
32874aea 4237 case VK_F1:
4238 code = (keystate[VK_SHIFT] & 0x80 ? 23 : 11);
4239 break;
4240 case VK_F2:
4241 code = (keystate[VK_SHIFT] & 0x80 ? 24 : 12);
4242 break;
4243 case VK_F3:
4244 code = (keystate[VK_SHIFT] & 0x80 ? 25 : 13);
4245 break;
4246 case VK_F4:
4247 code = (keystate[VK_SHIFT] & 0x80 ? 26 : 14);
4248 break;
4249 case VK_F5:
4250 code = (keystate[VK_SHIFT] & 0x80 ? 28 : 15);
4251 break;
4252 case VK_F6:
4253 code = (keystate[VK_SHIFT] & 0x80 ? 29 : 17);
4254 break;
4255 case VK_F7:
4256 code = (keystate[VK_SHIFT] & 0x80 ? 31 : 18);
4257 break;
4258 case VK_F8:
4259 code = (keystate[VK_SHIFT] & 0x80 ? 32 : 19);
4260 break;
4261 case VK_F9:
4262 code = (keystate[VK_SHIFT] & 0x80 ? 33 : 20);
4263 break;
4264 case VK_F10:
4265 code = (keystate[VK_SHIFT] & 0x80 ? 34 : 21);
4266 break;
4267 case VK_F11:
4268 code = 23;
4269 break;
4270 case VK_F12:
4271 code = 24;
4272 break;
4273 case VK_F13:
4274 code = 25;
4275 break;
4276 case VK_F14:
4277 code = 26;
4278 break;
4279 case VK_F15:
4280 code = 28;
4281 break;
4282 case VK_F16:
4283 code = 29;
4284 break;
4285 case VK_F17:
4286 code = 31;
4287 break;
4288 case VK_F18:
4289 code = 32;
4290 break;
4291 case VK_F19:
4292 code = 33;
4293 break;
4294 case VK_F20:
4295 code = 34;
4296 break;
dfca2656 4297 }
4298 if ((shift_state&2) == 0) switch (wParam) {
32874aea 4299 case VK_HOME:
4300 code = 1;
4301 break;
4302 case VK_INSERT:
4303 code = 2;
4304 break;
4305 case VK_DELETE:
4306 code = 3;
4307 break;
4308 case VK_END:
4309 code = 4;
4310 break;
4311 case VK_PRIOR:
4312 code = 5;
4313 break;
4314 case VK_NEXT:
4315 code = 6;
4316 break;
374330e2 4317 }
ec55b220 4318 /* Reorder edit keys to physical order */
4a693cfc 4319 if (funky_type == FUNKY_VT400 && code <= 6)
32874aea 4320 code = "\0\2\1\4\5\3\6"[code];
ec55b220 4321
887035a5 4322 if (term->vt52_mode && code > 0 && code <= 6) {
32874aea 4323 p += sprintf((char *) p, "\x1B%c", " HLMEIG"[code]);
f37caa11 4324 return p - output;
4325 }
4326
4a693cfc 4327 if (funky_type == FUNKY_SCO && code >= 11 && code <= 34) {
4328 /* SCO function keys */
e24b1972 4329 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
4330 int index = 0;
4331 switch (wParam) {
4332 case VK_F1: index = 0; break;
4333 case VK_F2: index = 1; break;
4334 case VK_F3: index = 2; break;
4335 case VK_F4: index = 3; break;
4336 case VK_F5: index = 4; break;
4337 case VK_F6: index = 5; break;
4338 case VK_F7: index = 6; break;
4339 case VK_F8: index = 7; break;
4340 case VK_F9: index = 8; break;
4341 case VK_F10: index = 9; break;
4342 case VK_F11: index = 10; break;
4343 case VK_F12: index = 11; break;
4344 }
4345 if (keystate[VK_SHIFT] & 0x80) index += 12;
4346 if (keystate[VK_CONTROL] & 0x80) index += 24;
4347 p += sprintf((char *) p, "\x1B[%c", codes[index]);
f37caa11 4348 return p - output;
4349 }
4a693cfc 4350 if (funky_type == FUNKY_SCO && /* SCO small keypad */
9bc81a2c 4351 code >= 1 && code <= 6) {
4352 char codes[] = "HL.FIG";
4353 if (code == 3) {
4354 *p++ = '\x7F';
4355 } else {
4356 p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
4357 }
4358 return p - output;
4359 }
4a693cfc 4360 if ((term->vt52_mode || funky_type == FUNKY_VT100P) && code >= 11 && code <= 24) {
f37caa11 4361 int offt = 0;
32874aea 4362 if (code > 15)
4363 offt++;
4364 if (code > 21)
4365 offt++;
887035a5 4366 if (term->vt52_mode)
32874aea 4367 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11 - offt);
f37caa11 4368 else
32874aea 4369 p +=
4370 sprintf((char *) p, "\x1BO%c", code + 'P' - 11 - offt);
f37caa11 4371 return p - output;
4372 }
4a693cfc 4373 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
32874aea 4374 p += sprintf((char *) p, "\x1B[[%c", code + 'A' - 11);
c9def1b8 4375 return p - output;
4376 }
4a693cfc 4377 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
887035a5 4378 if (term->vt52_mode)
32874aea 4379 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11);
ec55b220 4380 else
32874aea 4381 p += sprintf((char *) p, "\x1BO%c", code + 'P' - 11);
c9def1b8 4382 return p - output;
4383 }
4a693cfc 4384 if ((code == 1 || code == 4) &&
4385 conf_get_int(conf, CONF_rxvt_homeend)) {
32874aea 4386 p += sprintf((char *) p, code == 1 ? "\x1B[H" : "\x1BOw");
c9def1b8 4387 return p - output;
4388 }
4389 if (code) {
32874aea 4390 p += sprintf((char *) p, "\x1B[%d~", code);
374330e2 4391 return p - output;
374330e2 4392 }
45dabbc5 4393
c9def1b8 4394 /*
4395 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
4396 * some reason seems to send VK_CLEAR to Windows...).
4397 */
4398 {
4399 char xkey = 0;
4400 switch (wParam) {
32874aea 4401 case VK_UP:
4402 xkey = 'A';
4403 break;
4404 case VK_DOWN:
4405 xkey = 'B';
4406 break;
4407 case VK_RIGHT:
4408 xkey = 'C';
4409 break;
4410 case VK_LEFT:
4411 xkey = 'D';
4412 break;
4413 case VK_CLEAR:
4414 xkey = 'G';
4415 break;
c9def1b8 4416 }
32874aea 4417 if (xkey) {
720f700e 4418 p += format_arrow_key(p, term, xkey, shift_state);
c9def1b8 4419 return p - output;
4420 }
4421 }
0c50ef57 4422
4423 /*
4424 * Finally, deal with Return ourselves. (Win95 seems to
4425 * foul it up when Alt is pressed, for some reason.)
4426 */
32874aea 4427 if (wParam == VK_RETURN) { /* Return */
0c50ef57 4428 *p++ = 0x0D;
a5f3e637 4429 *p++ = 0;
4430 return -2;
0c50ef57 4431 }
4eeb7d09 4432
4433 if (left_alt && wParam >= VK_NUMPAD0 && wParam <= VK_NUMPAD9)
4434 alt_sum = alt_sum * 10 + wParam - VK_NUMPAD0;
4435 else
4436 alt_sum = 0;
67c339f7 4437 }
374330e2 4438
c9def1b8 4439 /* Okay we've done everything interesting; let windows deal with
4440 * the boring stuff */
4441 {
a9c02454 4442 BOOL capsOn=0;
4443
4444 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4a693cfc 4445 if(keystate[VK_CAPITAL] != 0 &&
4446 conf_get_int(conf, CONF_xlat_capslockcyr)) {
a9c02454 4447 capsOn= !left_alt;
4448 keystate[VK_CAPITAL] = 0;
4449 }
4450
c264be08 4451 /* XXX how do we know what the max size of the keys array should
4452 * be is? There's indication on MS' website of an Inquire/InquireEx
4453 * functioning returning a KBINFO structure which tells us. */
4454 if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) {
4455 /* XXX 'keys' parameter is declared in MSDN documentation as
4456 * 'LPWORD lpChar'.
4457 * The experience of a French user indicates that on
4458 * Win98, WORD[] should be passed in, but on Win2K, it should
4459 * be BYTE[]. German WinXP and my Win2K with "US International"
4460 * driver corroborate this.
4461 * Experimentally I've conditionalised the behaviour on the
4462 * Win9x/NT split, but I suspect it's worse than that.
4463 * See wishlist item `win-dead-keys' for more horrible detail
4464 * and speculations. */
4465 BYTE keybs[3];
4466 int i;
4467 r = ToAsciiEx(wParam, scan, keystate, (LPWORD)keybs, 0, kbd_layout);
4468 for (i=0; i<3; i++) keys[i] = keybs[i];
4469 } else {
4470 r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
4471 }
4eeb7d09 4472#ifdef SHOW_TOASCII_RESULT
4473 if (r == 1 && !key_down) {
4474 if (alt_sum) {
21d2b241 4475 if (in_utf(term) || ucsdata.dbcs_screenfont)
4eeb7d09 4476 debug((", (U+%04x)", alt_sum));
4477 else
4478 debug((", LCH(%d)", alt_sum));
4479 } else {
4480 debug((", ACH(%d)", keys[0]));
4481 }
4482 } else if (r > 0) {
4483 int r1;
4484 debug((", ASC("));
4485 for (r1 = 0; r1 < r; r1++) {
4486 debug(("%s%d", r1 ? "," : "", keys[r1]));
4487 }
4488 debug((")"));
4489 }
4490#endif
32874aea 4491 if (r > 0) {
4eeb7d09 4492 WCHAR keybuf;
256cb87c 4493
4494 /*
4495 * Interrupt an ongoing paste. I'm not sure this is
4496 * sensible, but for the moment it's preferable to
4497 * having to faff about buffering things.
4498 */
887035a5 4499 term_nopaste(term);
256cb87c 4500
c9def1b8 4501 p = output;
32874aea 4502 for (i = 0; i < r; i++) {
4503 unsigned char ch = (unsigned char) keys[i];
14963b8f 4504
32874aea 4505 if (compose_state == 2 && (ch & 0x80) == 0 && ch > ' ') {
c9def1b8 4506 compose_char = ch;
32874aea 4507 compose_state++;
c9def1b8 4508 continue;
4509 }
32874aea 4510 if (compose_state == 3 && (ch & 0x80) == 0 && ch > ' ') {
c9def1b8 4511 int nc;
4512 compose_state = 0;
4513
32874aea 4514 if ((nc = check_compose(compose_char, ch)) == -1) {
fe50e814 4515 MessageBeep(MB_ICONHAND);
c9def1b8 4516 return 0;
4517 }
4eeb7d09 4518 keybuf = nc;
887035a5 4519 term_seen_key_event(term);
e3be8de5 4520 if (ldisc)
4521 luni_send(ldisc, &keybuf, 1, 1);
4eeb7d09 4522 continue;
c9def1b8 4523 }
374330e2 4524
c9def1b8 4525 compose_state = 0;
374330e2 4526
4eeb7d09 4527 if (!key_down) {
4528 if (alt_sum) {
21d2b241 4529 if (in_utf(term) || ucsdata.dbcs_screenfont) {
4eeb7d09 4530 keybuf = alt_sum;
887035a5 4531 term_seen_key_event(term);
e3be8de5 4532 if (ldisc)
4533 luni_send(ldisc, &keybuf, 1, 1);
4eeb7d09 4534 } else {
4535 ch = (char) alt_sum;
5471d09a 4536 /*
4537 * We need not bother about stdin
4538 * backlogs here, because in GUI PuTTY
4539 * we can't do anything about it
4540 * anyway; there's no means of asking
4541 * Windows to hold off on KEYDOWN
4542 * messages. We _have_ to buffer
4543 * everything we're sent.
4544 */
887035a5 4545 term_seen_key_event(term);
e3be8de5 4546 if (ldisc)
4547 ldisc_send(ldisc, &ch, 1, 1);
4eeb7d09 4548 }
4549 alt_sum = 0;
405a0c29 4550 } else {
887035a5 4551 term_seen_key_event(term);
e3be8de5 4552 if (ldisc)
4553 lpage_send(ldisc, kbd_codepage, &ch, 1, 1);
405a0c29 4554 }
4eeb7d09 4555 } else {
a9c02454 4556 if(capsOn && ch < 0x80) {
4557 WCHAR cbuf[2];
4558 cbuf[0] = 27;
4559 cbuf[1] = xlat_uskbd2cyrllic(ch);
887035a5 4560 term_seen_key_event(term);
e3be8de5 4561 if (ldisc)
4562 luni_send(ldisc, cbuf+!left_alt, 1+!!left_alt, 1);
a9c02454 4563 } else {
4564 char cbuf[2];
4565 cbuf[0] = '\033';
4566 cbuf[1] = ch;
887035a5 4567 term_seen_key_event(term);
e3be8de5 4568 if (ldisc)
4569 lpage_send(ldisc, kbd_codepage,
4570 cbuf+!left_alt, 1+!!left_alt, 1);
a9c02454 4571 }
c9def1b8 4572 }
bca9517a 4573 show_mouseptr(0);
c9def1b8 4574 }
374330e2 4575
c9def1b8 4576 /* This is so the ALT-Numpad and dead keys work correctly. */
4577 keys[0] = 0;
4578
32874aea 4579 return p - output;
c9def1b8 4580 }
159eba53 4581 /* If we're definitly not building up an ALT-54321 then clear it */
32874aea 4582 if (!left_alt)
4583 keys[0] = 0;
4eeb7d09 4584 /* If we will be using alt_sum fix the 256s */
21d2b241 4585 else if (keys[0] && (in_utf(term) || ucsdata.dbcs_screenfont))
4eeb7d09 4586 keys[0] = 10;
374330e2 4587 }
4588
dfca2656 4589 /*
4590 * ALT alone may or may not want to bring up the System menu.
4591 * If it's not meant to, we return 0 on presses or releases of
4592 * ALT, to show that we've swallowed the keystroke. Otherwise
4593 * we return -1, which means Windows will give the keystroke
4594 * its default handling (i.e. bring up the System menu).
4595 */
4a693cfc 4596 if (wParam == VK_MENU && !conf_get_int(conf, CONF_alt_only))
dfca2656 4597 return 0;
374330e2 4598
c9def1b8 4599 return -1;
374330e2 4600}
4601
a8327734 4602void set_title(void *frontend, char *title)
32874aea 4603{
4604 sfree(window_name);
3d88e64d 4605 window_name = snewn(1 + strlen(title), char);
32874aea 4606 strcpy(window_name, title);
4a693cfc 4607 if (conf_get_int(conf, CONF_win_name_always) || !IsIconic(hwnd))
32874aea 4608 SetWindowText(hwnd, title);
374330e2 4609}
4610
a8327734 4611void set_icon(void *frontend, char *title)
32874aea 4612{
4613 sfree(icon_name);
3d88e64d 4614 icon_name = snewn(1 + strlen(title), char);
32874aea 4615 strcpy(icon_name, title);
4a693cfc 4616 if (!conf_get_int(conf, CONF_win_name_always) && IsIconic(hwnd))
32874aea 4617 SetWindowText(hwnd, title);
374330e2 4618}
4619
a8327734 4620void set_sbar(void *frontend, int total, int start, int page)
32874aea 4621{
374330e2 4622 SCROLLINFO si;
c9def1b8 4623
4a693cfc 4624 if (!conf_get_int(conf, is_full_screen() ?
4625 CONF_scrollbar_in_fullscreen : CONF_scrollbar))
32874aea 4626 return;
c9def1b8 4627
374330e2 4628 si.cbSize = sizeof(si);
c9def1b8 4629 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
374330e2 4630 si.nMin = 0;
4631 si.nMax = total - 1;
4632 si.nPage = page;
4633 si.nPos = start;
c1f5f956 4634 if (hwnd)
32874aea 4635 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
374330e2 4636}
4637
a8327734 4638Context get_ctx(void *frontend)
32874aea 4639{
374330e2 4640 HDC hdc;
4641 if (hwnd) {
32874aea 4642 hdc = GetDC(hwnd);
374330e2 4643 if (hdc && pal)
32874aea 4644 SelectPalette(hdc, pal, FALSE);
374330e2 4645 return hdc;
4646 } else
4647 return NULL;
4648}
4649
32874aea 4650void free_ctx(Context ctx)
4651{
4652 SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
4653 ReleaseDC(hwnd, ctx);
374330e2 4654}
4655
32874aea 4656static void real_palette_set(int n, int r, int g, int b)
4657{
374330e2 4658 if (pal) {
4659 logpal->palPalEntry[n].peRed = r;
4660 logpal->palPalEntry[n].peGreen = g;
4661 logpal->palPalEntry[n].peBlue = b;
4662 logpal->palPalEntry[n].peFlags = PC_NOCOLLAPSE;
4663 colours[n] = PALETTERGB(r, g, b);
cecb13f6 4664 SetPaletteEntries(pal, 0, NALLCOLOURS, logpal->palPalEntry);
374330e2 4665 } else
4666 colours[n] = RGB(r, g, b);
4667}
4668
a8327734 4669void palette_set(void *frontend, int n, int r, int g, int b)
32874aea 4670{
cecb13f6 4671 if (n >= 16)
4672 n += 256 - 16;
4673 if (n > NALLCOLOURS)
4674 return;
4675 real_palette_set(n, r, g, b);
374330e2 4676 if (pal) {
a8327734 4677 HDC hdc = get_ctx(frontend);
32874aea 4678 UnrealizeObject(pal);
4679 RealizePalette(hdc);
4680 free_ctx(hdc);
6092b4ee 4681 } else {
4682 if (n == (ATTR_DEFBG>>ATTR_BGSHIFT))
4683 /* If Default Background changes, we need to ensure any
4684 * space between the text area and the window border is
4685 * redrawn. */
4686 InvalidateRect(hwnd, NULL, TRUE);
374330e2 4687 }
4688}
4689
a8327734 4690void palette_reset(void *frontend)
32874aea 4691{
374330e2 4692 int i;
4693
cecb13f6 4694 /* And this */
4695 for (i = 0; i < NALLCOLOURS; i++) {
374330e2 4696 if (pal) {
4697 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
4698 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
4699 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
4700 logpal->palPalEntry[i].peFlags = 0;
4701 colours[i] = PALETTERGB(defpal[i].rgbtRed,
4702 defpal[i].rgbtGreen,
4703 defpal[i].rgbtBlue);
4704 } else
4705 colours[i] = RGB(defpal[i].rgbtRed,
32874aea 4706 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
374330e2 4707 }
4708
4709 if (pal) {
4710 HDC hdc;
cecb13f6 4711 SetPaletteEntries(pal, 0, NALLCOLOURS, logpal->palPalEntry);
a8327734 4712 hdc = get_ctx(frontend);
32874aea 4713 RealizePalette(hdc);
4714 free_ctx(hdc);
6092b4ee 4715 } else {
4716 /* Default Background may have changed. Ensure any space between
4717 * text area and window border is redrawn. */
4718 InvalidateRect(hwnd, NULL, TRUE);
374330e2 4719 }
4720}
4721
a8327734 4722void write_aclip(void *frontend, char *data, int len, int must_deselect)
32874aea 4723{
374330e2 4724 HGLOBAL clipdata;
4725 void *lock;
4726
32874aea 4727 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
374330e2 4728 if (!clipdata)
4729 return;
32874aea 4730 lock = GlobalLock(clipdata);
374330e2 4731 if (!lock)
4732 return;
32874aea 4733 memcpy(lock, data, len);
4734 ((unsigned char *) lock)[len] = 0;
4735 GlobalUnlock(clipdata);
374330e2 4736
f0df44da 4737 if (!must_deselect)
32874aea 4738 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
f0df44da 4739
32874aea 4740 if (OpenClipboard(hwnd)) {
374330e2 4741 EmptyClipboard();
32874aea 4742 SetClipboardData(CF_TEXT, clipdata);
374330e2 4743 CloseClipboard();
4744 } else
32874aea 4745 GlobalFree(clipdata);
f0df44da 4746
4747 if (!must_deselect)
32874aea 4748 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
374330e2 4749}
4750
4eeb7d09 4751/*
4752 * Note: unlike write_aclip() this will not append a nul.
4753 */
c83c79bd 4754void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
4eeb7d09 4755{
a7419ea4 4756 HGLOBAL clipdata, clipdata2, clipdata3;
4eeb7d09 4757 int len2;
a7419ea4 4758 void *lock, *lock2, *lock3;
4eeb7d09 4759
4760 len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
4761
4762 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
4763 len * sizeof(wchar_t));
4764 clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
4765
0e5e7f46 4766 if (!clipdata || !clipdata2) {
4eeb7d09 4767 if (clipdata)
4768 GlobalFree(clipdata);
4769 if (clipdata2)
4770 GlobalFree(clipdata2);
4771 return;
4772 }
4773 if (!(lock = GlobalLock(clipdata)))
4774 return;
4775 if (!(lock2 = GlobalLock(clipdata2)))
4776 return;
4777
4778 memcpy(lock, data, len * sizeof(wchar_t));
4779 WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
4780
4a693cfc 4781 if (conf_get_int(conf, CONF_rtf_paste)) {
a7419ea4 4782 wchar_t unitab[256];
4783 char *rtf = NULL;
4784 unsigned char *tdata = (unsigned char *)lock2;
4785 wchar_t *udata = (wchar_t *)lock;
4786 int rtflen = 0, uindex = 0, tindex = 0;
4787 int rtfsize = 0;
4788 int multilen, blen, alen, totallen, i;
4789 char before[16], after[4];
c83c79bd 4790 int fgcolour, lastfgcolour = 0;
4791 int bgcolour, lastbgcolour = 0;
4792 int attrBold, lastAttrBold = 0;
4793 int attrUnder, lastAttrUnder = 0;
4794 int palette[NALLCOLOURS];
4795 int numcolours;
4a693cfc 4796 FontSpec *font = conf_get_fontspec(conf, CONF_font);
a7419ea4 4797
4798 get_unitab(CP_ACP, unitab, 0);
4799
4a693cfc 4800 rtfsize = 100 + strlen(font->name);
3d88e64d 4801 rtf = snewn(rtfsize, char);
c83c79bd 4802 rtflen = sprintf(rtf, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
4a693cfc 4803 font->name, font->height*2);
c83c79bd 4804
4805 /*
4806 * Add colour palette
4807 * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
4808 */
4809
4810 /*
4811 * First - Determine all colours in use
4812 * o Foregound and background colours share the same palette
4813 */
4814 if (attr) {
4815 memset(palette, 0, sizeof(palette));
4816 for (i = 0; i < (len-1); i++) {
4817 fgcolour = ((attr[i] & ATTR_FGMASK) >> ATTR_FGSHIFT);
4818 bgcolour = ((attr[i] & ATTR_BGMASK) >> ATTR_BGSHIFT);
4819
4820 if (attr[i] & ATTR_REVERSE) {
4821 int tmpcolour = fgcolour; /* Swap foreground and background */
4822 fgcolour = bgcolour;
4823 bgcolour = tmpcolour;
4824 }
4825
4826 if (bold_mode == BOLD_COLOURS && (attr[i] & ATTR_BOLD)) {
4827 if (fgcolour < 8) /* ANSI colours */
4828 fgcolour += 8;
4829 else if (fgcolour >= 256) /* Default colours */
4830 fgcolour ++;
4831 }
4832
4833 if (attr[i] & ATTR_BLINK) {
4834 if (bgcolour < 8) /* ANSI colours */
4835 bgcolour += 8;
4836 else if (bgcolour >= 256) /* Default colours */
4837 bgcolour ++;
4838 }
4839
4840 palette[fgcolour]++;
4841 palette[bgcolour]++;
4842 }
4843
4844 /*
4845 * Next - Create a reduced palette
4846 */
4847 numcolours = 0;
4848 for (i = 0; i < NALLCOLOURS; i++) {
4849 if (palette[i] != 0)
4850 palette[i] = ++numcolours;
4851 }
4852
4853 /*
4854 * Finally - Write the colour table
4855 */
4856 rtf = sresize(rtf, rtfsize + (numcolours * 25), char);
4857 strcat(rtf, "{\\colortbl ;");
4858 rtflen = strlen(rtf);
4859
4860 for (i = 0; i < NALLCOLOURS; i++) {
4861 if (palette[i] != 0) {
4862 rtflen += sprintf(&rtf[rtflen], "\\red%d\\green%d\\blue%d;", defpal[i].rgbtRed, defpal[i].rgbtGreen, defpal[i].rgbtBlue);
4863 }
4864 }
4865 strcpy(&rtf[rtflen], "}");
4866 rtflen ++;
4867 }
a7419ea4 4868
4869 /*
4870 * We want to construct a piece of RTF that specifies the
4871 * same Unicode text. To do this we will read back in
4872 * parallel from the Unicode data in `udata' and the
4873 * non-Unicode data in `tdata'. For each character in
4874 * `tdata' which becomes the right thing in `udata' when
4875 * looked up in `unitab', we just copy straight over from
4876 * tdata. For each one that doesn't, we must WCToMB it
4877 * individually and produce a \u escape sequence.
4878 *
4879 * It would probably be more robust to just bite the bullet
4880 * and WCToMB each individual Unicode character one by one,
4881 * then MBToWC each one back to see if it was an accurate
4882 * translation; but that strikes me as a horrifying number
4883 * of Windows API calls so I want to see if this faster way
4884 * will work. If it screws up badly we can always revert to
4885 * the simple and slow way.
4886 */
4887 while (tindex < len2 && uindex < len &&
4888 tdata[tindex] && udata[uindex]) {
4889 if (tindex + 1 < len2 &&
4890 tdata[tindex] == '\r' &&
4891 tdata[tindex+1] == '\n') {
4892 tindex++;
4893 uindex++;
c83c79bd 4894 }
4895
4896 /*
4897 * Set text attributes
4898 */
4899 if (attr) {
4900 if (rtfsize < rtflen + 64) {
4901 rtfsize = rtflen + 512;
4902 rtf = sresize(rtf, rtfsize, char);
4903 }
4904
4905 /*
4906 * Determine foreground and background colours
4907 */
4908 fgcolour = ((attr[tindex] & ATTR_FGMASK) >> ATTR_FGSHIFT);
4909 bgcolour = ((attr[tindex] & ATTR_BGMASK) >> ATTR_BGSHIFT);
4910
4911 if (attr[tindex] & ATTR_REVERSE) {
4912 int tmpcolour = fgcolour; /* Swap foreground and background */
4913 fgcolour = bgcolour;
4914 bgcolour = tmpcolour;
4915 }
4916
4917 if (bold_mode == BOLD_COLOURS && (attr[tindex] & ATTR_BOLD)) {
4918 if (fgcolour < 8) /* ANSI colours */
4919 fgcolour += 8;
4920 else if (fgcolour >= 256) /* Default colours */
4921 fgcolour ++;
4922 }
4923
4924 if (attr[tindex] & ATTR_BLINK) {
4925 if (bgcolour < 8) /* ANSI colours */
4926 bgcolour += 8;
4927 else if (bgcolour >= 256) /* Default colours */
4928 bgcolour ++;
4929 }
4930
4931 /*
4932 * Collect other attributes
4933 */
4934 if (bold_mode != BOLD_COLOURS)
4935 attrBold = attr[tindex] & ATTR_BOLD;
4936 else
4937 attrBold = 0;
4938
4939 attrUnder = attr[tindex] & ATTR_UNDER;
4940
4941 /*
4942 * Reverse video
4943 * o If video isn't reversed, ignore colour attributes for default foregound
4944 * or background.
4945 * o Special case where bolded text is displayed using the default foregound
4946 * and background colours - force to bolded RTF.
4947 */
4948 if (!(attr[tindex] & ATTR_REVERSE)) {
4949 if (bgcolour >= 256) /* Default color */
4950 bgcolour = -1; /* No coloring */
4951
4952 if (fgcolour >= 256) { /* Default colour */
4953 if (bold_mode == BOLD_COLOURS && (fgcolour & 1) && bgcolour == -1)
4954 attrBold = ATTR_BOLD; /* Emphasize text with bold attribute */
4955
4956 fgcolour = -1; /* No coloring */
4957 }
4958 }
4959
4960 /*
4961 * Write RTF text attributes
4962 */
4963 if (lastfgcolour != fgcolour) {
4964 lastfgcolour = fgcolour;
4965 rtflen += sprintf(&rtf[rtflen], "\\cf%d ", (fgcolour >= 0) ? palette[fgcolour] : 0);
4966 }
4967
4968 if (lastbgcolour != bgcolour) {
4969 lastbgcolour = bgcolour;
4970 rtflen += sprintf(&rtf[rtflen], "\\highlight%d ", (bgcolour >= 0) ? palette[bgcolour] : 0);
4971 }
4972
4973 if (lastAttrBold != attrBold) {
4974 lastAttrBold = attrBold;
4975 rtflen += sprintf(&rtf[rtflen], "%s", attrBold ? "\\b " : "\\b0 ");
4976 }
4977
4978 if (lastAttrUnder != attrUnder) {
4979 lastAttrUnder = attrUnder;
4980 rtflen += sprintf(&rtf[rtflen], "%s", attrUnder ? "\\ul " : "\\ulnone ");
4981 }
a7419ea4 4982 }
c83c79bd 4983
a7419ea4 4984 if (unitab[tdata[tindex]] == udata[uindex]) {
4985 multilen = 1;
4986 before[0] = '\0';
4987 after[0] = '\0';
4988 blen = alen = 0;
4989 } else {
4990 multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
4991 NULL, 0, NULL, NULL);
4992 if (multilen != 1) {
c3b032a6 4993 blen = sprintf(before, "{\\uc%d\\u%d", multilen,
4994 udata[uindex]);
a7419ea4 4995 alen = 1; strcpy(after, "}");
4996 } else {
4997 blen = sprintf(before, "\\u%d", udata[uindex]);
4998 alen = 0; after[0] = '\0';
4999 }
5000 }
5001 assert(tindex + multilen <= len2);
5002 totallen = blen + alen;
5003 for (i = 0; i < multilen; i++) {
5004 if (tdata[tindex+i] == '\\' ||
5005 tdata[tindex+i] == '{' ||
5006 tdata[tindex+i] == '}')
5007 totallen += 2;
5008 else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A)
5009 totallen += 6; /* \par\r\n */
5010 else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20)
5011 totallen += 4;
5012 else
5013 totallen++;
5014 }
5015
5016 if (rtfsize < rtflen + totallen + 3) {
5017 rtfsize = rtflen + totallen + 512;
3d88e64d 5018 rtf = sresize(rtf, rtfsize, char);
a7419ea4 5019 }
5020
5021 strcpy(rtf + rtflen, before); rtflen += blen;
5022 for (i = 0; i < multilen; i++) {
5023 if (tdata[tindex+i] == '\\' ||
5024 tdata[tindex+i] == '{' ||
5025 tdata[tindex+i] == '}') {
5026 rtf[rtflen++] = '\\';
5027 rtf[rtflen++] = tdata[tindex+i];
5028 } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
5029 rtflen += sprintf(rtf+rtflen, "\\par\r\n");
5030 } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
5031 rtflen += sprintf(rtf+rtflen, "\\'%02x", tdata[tindex+i]);
5032 } else {
5033 rtf[rtflen++] = tdata[tindex+i];
5034 }
5035 }
5036 strcpy(rtf + rtflen, after); rtflen += alen;
5037
5038 tindex += multilen;
5039 uindex++;
5040 }
5041
c83c79bd 5042 rtf[rtflen++] = '}'; /* Terminate RTF stream */
5043 rtf[rtflen++] = '\0';
5044 rtf[rtflen++] = '\0';
a7419ea4 5045
5046 clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtflen);
5047 if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
c83c79bd 5048 memcpy(lock3, rtf, rtflen);
a7419ea4 5049 GlobalUnlock(clipdata3);
5050 }
5051 sfree(rtf);
5052 } else
5053 clipdata3 = NULL;
5054
4eeb7d09 5055 GlobalUnlock(clipdata);
5056 GlobalUnlock(clipdata2);
5057
5058 if (!must_deselect)
5059 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
5060
5061 if (OpenClipboard(hwnd)) {
5062 EmptyClipboard();
5063 SetClipboardData(CF_UNICODETEXT, clipdata);
5064 SetClipboardData(CF_TEXT, clipdata2);
a7419ea4 5065 if (clipdata3)
5066 SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
4eeb7d09 5067 CloseClipboard();
5068 } else {
5069 GlobalFree(clipdata);
5070 GlobalFree(clipdata2);
5071 }
5072
5073 if (!must_deselect)
5074 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
5075}
5076
1d432454 5077static DWORD WINAPI clipboard_read_threadfunc(void *param)
32874aea 5078{
1d432454 5079 HWND hwnd = (HWND)param;
5080 HGLOBAL clipdata;
374330e2 5081
1d432454 5082 if (OpenClipboard(NULL)) {
2d466ffd 5083 if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
1d432454 5084 SendMessage(hwnd, WM_GOT_CLIPDATA, (WPARAM)1, (LPARAM)clipdata);
5085 } else if ((clipdata = GetClipboardData(CF_TEXT))) {
5086 SendMessage(hwnd, WM_GOT_CLIPDATA, (WPARAM)0, (LPARAM)clipdata);
5087 }
5088 CloseClipboard();
5089 }
5090
5091 return 0;
5092}
5093
5094static int process_clipdata(HGLOBAL clipdata, int unicode)
5095{
1d432454 5096 sfree(clipboard_contents);
5097 clipboard_contents = NULL;
5098 clipboard_length = 0;
5099
5100 if (unicode) {
5101 wchar_t *p = GlobalLock(clipdata);
5102 wchar_t *p2;
5103
5104 if (p) {
5105 /* Unwilling to rely on Windows having wcslen() */
5106 for (p2 = p; *p2; p2++);
5107 clipboard_length = p2 - p;
5108 clipboard_contents = snewn(clipboard_length + 1, wchar_t);
5109 memcpy(clipboard_contents, p, clipboard_length * sizeof(wchar_t));
5110 clipboard_contents[clipboard_length] = L'\0';
5111 return TRUE;
5112 }
5113 } else {
5114 char *s = GlobalLock(clipdata);
5115 int i;
5116
5117 if (s) {
4eeb7d09 5118 i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
1d432454 5119 clipboard_contents = snewn(i, wchar_t);
5120 MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1,
5121 clipboard_contents, i);
5122 clipboard_length = i - 1;
5123 clipboard_contents[clipboard_length] = L'\0';
5124 return TRUE;
5125 }
374330e2 5126 }
5127
1d432454 5128 return FALSE;
5129}
5130
5131void request_paste(void *frontend)
5132{
5133 /*
5134 * I always thought pasting was synchronous in Windows; the
5135 * clipboard access functions certainly _look_ synchronous,
5136 * unlike the X ones. But in fact it seems that in some
5137 * situations the contents of the clipboard might not be
5138 * immediately available, and the clipboard-reading functions
5139 * may block. This leads to trouble if the application
5140 * delivering the clipboard data has to get hold of it by -
5141 * for example - talking over a network connection which is
5142 * forwarded through this very PuTTY.
5143 *
5144 * Hence, we spawn a subthread to read the clipboard, and do
5145 * our paste when it's finished. The thread will send a
5146 * message back to our main window when it terminates, and
5147 * that tells us it's OK to paste.
5148 */
5149 DWORD in_threadid; /* required for Win9x */
5150 CreateThread(NULL, 0, clipboard_read_threadfunc,
5151 hwnd, 0, &in_threadid);
5152}
5153
5154void get_clip(void *frontend, wchar_t **p, int *len)
5155{
5156 if (p) {
5157 *p = clipboard_contents;
5158 *len = clipboard_length;
5159 }
374330e2 5160}
5161
4eeb7d09 5162#if 0
374330e2 5163/*
5164 * Move `lines' lines from position `from' to position `to' in the
5165 * window.
5166 */
a8327734 5167void optimised_move(void *frontend, int to, int from, int lines)
32874aea 5168{
374330e2 5169 RECT r;
f67b4e85 5170 int min, max;
374330e2 5171
5172 min = (to < from ? to : from);
5173 max = to + from - min;
374330e2 5174
5a73255e 5175 r.left = offset_width;
887035a5 5176 r.right = offset_width + term->cols * font_width;
5a73255e 5177 r.top = offset_height + min * font_height;
5178 r.bottom = offset_height + (max + lines) * font_height;
32874aea 5179 ScrollWindow(hwnd, 0, (to - from) * font_height, &r, &r);
374330e2 5180}
4eeb7d09 5181#endif
374330e2 5182
5183/*
5184 * Print a message box and perform a fatal exit.
5185 */
32874aea 5186void fatalbox(char *fmt, ...)
5187{
374330e2 5188 va_list ap;
971bcc0a 5189 char *stuff, morestuff[100];
374330e2 5190
5191 va_start(ap, fmt);
971bcc0a 5192 stuff = dupvprintf(fmt, ap);
374330e2 5193 va_end(ap);
f6f450e2 5194 sprintf(morestuff, "%.70s Fatal Error", appname);
5195 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
971bcc0a 5196 sfree(stuff);
93b581bd 5197 cleanup_exit(1);
374330e2 5198}
5199
5200/*
1709795f 5201 * Print a modal (Really Bad) message box and perform a fatal exit.
5202 */
5203void modalfatalbox(char *fmt, ...)
5204{
5205 va_list ap;
971bcc0a 5206 char *stuff, morestuff[100];
1709795f 5207
5208 va_start(ap, fmt);
971bcc0a 5209 stuff = dupvprintf(fmt, ap);
1709795f 5210 va_end(ap);
f6f450e2 5211 sprintf(morestuff, "%.70s Fatal Error", appname);
5212 MessageBox(hwnd, stuff, morestuff,
1709795f 5213 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
971bcc0a 5214 sfree(stuff);
1709795f 5215 cleanup_exit(1);
5216}
5217
9099600a 5218DECL_WINDOWS_FUNCTION(static, BOOL, FlashWindowEx, (PFLASHWINFO));
09644e07 5219
5220static void init_flashwindow(void)
5221{
bda368a5 5222 HMODULE user32_module = load_system32_dll("user32.dll");
9099600a 5223 GET_WINDOWS_FUNCTION(user32_module, FlashWindowEx);
09644e07 5224}
5225
5226static BOOL flash_window_ex(DWORD dwFlags, UINT uCount, DWORD dwTimeout)
5227{
5228 if (p_FlashWindowEx) {
5229 FLASHWINFO fi;
5230 fi.cbSize = sizeof(fi);
5231 fi.hwnd = hwnd;
5232 fi.dwFlags = dwFlags;
5233 fi.uCount = uCount;
5234 fi.dwTimeout = dwTimeout;
5235 return (*p_FlashWindowEx)(&fi);
5236 }
5237 else
5238 return FALSE; /* shrug */
5239}
5240
39934deb 5241static void flash_window(int mode);
5242static long next_flash;
5243static int flashing = 0;
5244
09644e07 5245/*
5246 * Timer for platforms where we must maintain window flashing manually
5247 * (e.g., Win95).
5248 */
39934deb 5249static void flash_window_timer(void *ctx, long now)
5250{
5251 if (flashing && now - next_flash >= 0) {
5252 flash_window(1);
5253 }
5254}
5255
1709795f 5256/*
f8a28d1f 5257 * Manage window caption / taskbar flashing, if enabled.
5258 * 0 = stop, 1 = maintain, 2 = start
5259 */
5260static void flash_window(int mode)
5261{
4a693cfc 5262 int beep_ind = conf_get_int(conf, CONF_beep_ind);
5263 if ((mode == 0) || (beep_ind == B_IND_DISABLED)) {
f8a28d1f 5264 /* stop */
5265 if (flashing) {
f8a28d1f 5266 flashing = 0;
09644e07 5267 if (p_FlashWindowEx)
5268 flash_window_ex(FLASHW_STOP, 0, 0);
5269 else
5270 FlashWindow(hwnd, FALSE);
f8a28d1f 5271 }
5272
5273 } else if (mode == 2) {
5274 /* start */
5275 if (!flashing) {
f8a28d1f 5276 flashing = 1;
09644e07 5277 if (p_FlashWindowEx) {
5278 /* For so-called "steady" mode, we use uCount=2, which
5279 * seems to be the traditional number of flashes used
5280 * by user notifications (e.g., by Explorer).
5281 * uCount=0 appears to enable continuous flashing, per
5282 * "flashing" mode, although I haven't seen this
5283 * documented. */
5284 flash_window_ex(FLASHW_ALL | FLASHW_TIMER,
4a693cfc 5285 (beep_ind == B_IND_FLASH ? 0 : 2),
09644e07 5286 0 /* system cursor blink rate */);
5287 /* No need to schedule timer */
5288 } else {
5289 FlashWindow(hwnd, TRUE);
5290 next_flash = schedule_timer(450, flash_window_timer, hwnd);
5291 }
f8a28d1f 5292 }
5293
4a693cfc 5294 } else if ((mode == 1) && (beep_ind == B_IND_FLASH)) {
f8a28d1f 5295 /* maintain */
09644e07 5296 if (flashing && !p_FlashWindowEx) {
39934deb 5297 FlashWindow(hwnd, TRUE); /* toggle */
5298 next_flash = schedule_timer(450, flash_window_timer, hwnd);
f8a28d1f 5299 }
5300 }
5301}
5302
5303/*
374330e2 5304 * Beep.
5305 */
860a34f8 5306void do_beep(void *frontend, int mode)
32874aea 5307{
03169ad0 5308 if (mode == BELL_DEFAULT) {
eb04402e 5309 /*
5310 * For MessageBeep style bells, we want to be careful of
5311 * timing, because they don't have the nice property of
5312 * PlaySound bells that each one cancels the previous
5313 * active one. So we limit the rate to one per 50ms or so.
5314 */
5315 static long lastbeep = 0;
d51cdf1e 5316 long beepdiff;
eb04402e 5317
d51cdf1e 5318 beepdiff = GetTickCount() - lastbeep;
eb04402e 5319 if (beepdiff >= 0 && beepdiff < 50)
5320 return;
156686ef 5321 MessageBeep(MB_OK);
d51cdf1e 5322 /*
5323 * The above MessageBeep call takes time, so we record the
5324 * time _after_ it finishes rather than before it starts.
5325 */
5326 lastbeep = GetTickCount();
03169ad0 5327 } else if (mode == BELL_WAVEFILE) {
4a693cfc 5328 Filename *bell_wavefile = conf_get_filename(conf, CONF_bell_wavefile);
5329 if (!PlaySound(bell_wavefile->path, NULL,
9a30e26b 5330 SND_ASYNC | SND_FILENAME)) {
4a693cfc 5331 char buf[sizeof(bell_wavefile->path) + 80];
f6f450e2 5332 char otherbuf[100];
03169ad0 5333 sprintf(buf, "Unable to play sound file\n%s\n"
4a693cfc 5334 "Using default sound instead", bell_wavefile->path);
f6f450e2 5335 sprintf(otherbuf, "%.70s Sound Error", appname);
5336 MessageBox(hwnd, buf, otherbuf,
32874aea 5337 MB_OK | MB_ICONEXCLAMATION);
4a693cfc 5338 conf_set_int(conf, CONF_beep, BELL_DEFAULT);
03169ad0 5339 }
85f6b361 5340 } else if (mode == BELL_PCSPEAKER) {
5341 static long lastbeep = 0;
5342 long beepdiff;
5343
5344 beepdiff = GetTickCount() - lastbeep;
5345 if (beepdiff >= 0 && beepdiff < 50)
5346 return;
5347
5348 /*
5349 * We must beep in different ways depending on whether this
5350 * is a 95-series or NT-series OS.
5351 */
5352 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
5353 Beep(800, 100);
5354 else
5355 MessageBeep(-1);
5356 lastbeep = GetTickCount();
03169ad0 5357 }
f8a28d1f 5358 /* Otherwise, either visual bell or disabled; do nothing here */
887035a5 5359 if (!term->has_focus) {
f8a28d1f 5360 flash_window(2); /* start */
5361 }
374330e2 5362}
8f57d753 5363
5364/*
68f9b3d9 5365 * Minimise or restore the window in response to a server-side
5366 * request.
5367 */
a8327734 5368void set_iconic(void *frontend, int iconic)
68f9b3d9 5369{
5370 if (IsIconic(hwnd)) {
5371 if (!iconic)
5372 ShowWindow(hwnd, SW_RESTORE);
5373 } else {
5374 if (iconic)
5375 ShowWindow(hwnd, SW_MINIMIZE);
5376 }
5377}
5378
5379/*
5380 * Move the window in response to a server-side request.
5381 */
a8327734 5382void move_window(void *frontend, int x, int y)
68f9b3d9 5383{
4a693cfc 5384 int resize_action = conf_get_int(conf, CONF_resize_action);
5385 if (resize_action == RESIZE_DISABLED ||
5386 resize_action == RESIZE_FONT ||
d1e0a352 5387 IsZoomed(hwnd))
5388 return;
5389
68f9b3d9 5390 SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
5391}
5392
5393/*
5394 * Move the window to the top or bottom of the z-order in response
5395 * to a server-side request.
5396 */
a8327734 5397void set_zorder(void *frontend, int top)
68f9b3d9 5398{
4a693cfc 5399 if (conf_get_int(conf, CONF_alwaysontop))
68f9b3d9 5400 return; /* ignore */
5401 SetWindowPos(hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
5402 SWP_NOMOVE | SWP_NOSIZE);
5403}
5404
5405/*
5406 * Refresh the window in response to a server-side request.
5407 */
a8327734 5408void refresh_window(void *frontend)
68f9b3d9 5409{
5410 InvalidateRect(hwnd, NULL, TRUE);
5411}
5412
5413/*
5414 * Maximise or restore the window in response to a server-side
5415 * request.
5416 */
a8327734 5417void set_zoomed(void *frontend, int zoomed)
68f9b3d9 5418{
1ba99d2c 5419 if (IsZoomed(hwnd)) {
5420 if (!zoomed)
5421 ShowWindow(hwnd, SW_RESTORE);
68f9b3d9 5422 } else {
5423 if (zoomed)
5424 ShowWindow(hwnd, SW_MAXIMIZE);
5425 }
5426}
5427
5428/*
5429 * Report whether the window is iconic, for terminal reports.
5430 */
a8327734 5431int is_iconic(void *frontend)
68f9b3d9 5432{
5433 return IsIconic(hwnd);
5434}
5435
5436/*
5437 * Report the window's position, for terminal reports.
5438 */
a8327734 5439void get_window_pos(void *frontend, int *x, int *y)
68f9b3d9 5440{
5441 RECT r;
5442 GetWindowRect(hwnd, &r);
5443 *x = r.left;
5444 *y = r.top;
5445}
5446
5447/*
5448 * Report the window's pixel size, for terminal reports.
5449 */
a8327734 5450void get_window_pixels(void *frontend, int *x, int *y)
68f9b3d9 5451{
5452 RECT r;
5453 GetWindowRect(hwnd, &r);
5454 *x = r.right - r.left;
5455 *y = r.bottom - r.top;
5456}
5457
5458/*
5459 * Return the window or icon title.
5460 */
a8327734 5461char *get_window_title(void *frontend, int icon)
68f9b3d9 5462{
5463 return icon ? icon_name : window_name;
5464}
1ba99d2c 5465
5466/*
5467 * See if we're in full-screen mode.
5468 */
4e95095a 5469static int is_full_screen()
1ba99d2c 5470{
5471 if (!IsZoomed(hwnd))
5472 return FALSE;
1e5eefb6 5473 if (GetWindowLongPtr(hwnd, GWL_STYLE) & WS_CAPTION)
1ba99d2c 5474 return FALSE;
5475 return TRUE;
5476}
5477
d0f1bcb4 5478/* Get the rect/size of a full screen window using the nearest available
5479 * monitor in multimon systems; default to something sensible if only
5480 * one monitor is present. */
5481static int get_fullscreen_rect(RECT * ss)
5482{
5d145a14 5483#if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
d0f1bcb4 5484 HMONITOR mon;
5485 MONITORINFO mi;
5486 mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
5487 mi.cbSize = sizeof(mi);
5488 GetMonitorInfo(mon, &mi);
5489
5490 /* structure copy */
5491 *ss = mi.rcMonitor;
5492 return TRUE;
5493#else
5494/* could also use code like this:
5495 ss->left = ss->top = 0;
5496 ss->right = GetSystemMetrics(SM_CXSCREEN);
5497 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
5498*/
e6f8202f 5499 return GetClientRect(GetDesktopWindow(), ss);
d0f1bcb4 5500#endif
5501}
5502
5503
1ba99d2c 5504/*
5505 * Go full-screen. This should only be called when we are already
5506 * maximised.
5507 */
4e95095a 5508static void make_full_screen()
1ba99d2c 5509{
5510 DWORD style;
d0f1bcb4 5511 RECT ss;
1ba99d2c 5512
5513 assert(IsZoomed(hwnd));
5514
d0f1bcb4 5515 if (is_full_screen())
5516 return;
5517
1ba99d2c 5518 /* Remove the window furniture. */
1e5eefb6 5519 style = GetWindowLongPtr(hwnd, GWL_STYLE);
1ba99d2c 5520 style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
4a693cfc 5521 if (conf_get_int(conf, CONF_scrollbar_in_fullscreen))
1ba99d2c 5522 style |= WS_VSCROLL;
5523 else
5524 style &= ~WS_VSCROLL;
1e5eefb6 5525 SetWindowLongPtr(hwnd, GWL_STYLE, style);
1ba99d2c 5526
5527 /* Resize ourselves to exactly cover the nearest monitor. */
d0f1bcb4 5528 get_fullscreen_rect(&ss);
5529 SetWindowPos(hwnd, HWND_TOP, ss.left, ss.top,
5530 ss.right - ss.left,
5531 ss.bottom - ss.top,
5532 SWP_FRAMECHANGED);
1ba99d2c 5533
1620020b 5534 /* We may have changed size as a result */
5535
5536 reset_window(0);
5537
52c70126 5538 /* Tick the menu item in the System and context menus. */
5539 {
5540 int i;
5541 for (i = 0; i < lenof(popup_menus); i++)
5542 CheckMenuItem(popup_menus[i].menu, IDM_FULLSCREEN, MF_CHECKED);
5543 }
1ba99d2c 5544}
5545
5546/*
5547 * Clear the full-screen attributes.
5548 */
4e95095a 5549static void clear_full_screen()
1ba99d2c 5550{
5551 DWORD oldstyle, style;
5552
5553 /* Reinstate the window furniture. */
1e5eefb6 5554 style = oldstyle = GetWindowLongPtr(hwnd, GWL_STYLE);
811f10b3 5555 style |= WS_CAPTION | WS_BORDER;
4a693cfc 5556 if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED)
811f10b3 5557 style &= ~WS_THICKFRAME;
5558 else
5559 style |= WS_THICKFRAME;
4a693cfc 5560 if (conf_get_int(conf, CONF_scrollbar))
1ba99d2c 5561 style |= WS_VSCROLL;
5562 else
5563 style &= ~WS_VSCROLL;
5564 if (style != oldstyle) {
1e5eefb6 5565 SetWindowLongPtr(hwnd, GWL_STYLE, style);
1ba99d2c 5566 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
5567 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
5568 SWP_FRAMECHANGED);
5569 }
5570
52c70126 5571 /* Untick the menu item in the System and context menus. */
5572 {
5573 int i;
5574 for (i = 0; i < lenof(popup_menus); i++)
5575 CheckMenuItem(popup_menus[i].menu, IDM_FULLSCREEN, MF_UNCHECKED);
5576 }
1ba99d2c 5577}
5578
5579/*
5580 * Toggle full-screen mode.
5581 */
4e95095a 5582static void flip_full_screen()
1ba99d2c 5583{
5584 if (is_full_screen()) {
5585 ShowWindow(hwnd, SW_RESTORE);
5586 } else if (IsZoomed(hwnd)) {
5587 make_full_screen();
5588 } else {
5589 SendMessage(hwnd, WM_FULLSCR_ON_MAX, 0, 0);
5590 ShowWindow(hwnd, SW_MAXIMIZE);
5591 }
5592}
ab2c86b9 5593
b9d7bcad 5594void frontend_keypress(void *handle)
ab2c86b9 5595{
5596 /*
5597 * Keypress termination in non-Close-On-Exit mode is not
5598 * currently supported in PuTTY proper, because the window
5599 * always has a perfectly good Close button anyway. So we do
5600 * nothing here.
5601 */
5602 return;
5603}
fbf6cb3b 5604
5605int from_backend(void *frontend, int is_stderr, const char *data, int len)
5606{
5607 return term_data(term, is_stderr, data, len);
5608}
c44bf5bd 5609
edd0cb8a 5610int from_backend_untrusted(void *frontend, const char *data, int len)
5611{
5612 return term_data_untrusted(term, data, len);
5613}
5614
5615int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
5616{
5617 int ret;
5618 ret = cmdline_get_passwd_input(p, in, inlen);
5619 if (ret == -1)
5620 ret = term_get_userpass_input(term, p, in, inlen);
5621 return ret;
5622}
5623
c44bf5bd 5624void agent_schedule_callback(void (*callback)(void *, void *, int),
5625 void *callback_ctx, void *data, int len)
5626{
5627 struct agent_callback *c = snew(struct agent_callback);
5628 c->callback = callback;
5629 c->callback_ctx = callback_ctx;
5630 c->data = data;
5631 c->len = len;
5632 PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);
5633}