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