Debian Codegraph @

src/main-win.c

raw symbols SCIP
1 /**
2 * \file main-win.c
3 * \brief Provide a front end for Microsoft Windows
4 *
5 * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, Robert Ruehlmann,
6 * and others
7 *
8 * This work is free software; you can redistribute it and/or modify it
9 * under the terms of either:
10 *
11 * a) the GNU General Public License as published by the Free Software
12 * Foundation, version 2, or
13 *
14 * b) the "Angband licence":
15 * This software may be copied and distributed for educational, research,
16 * and not for profit purposes provided that this copyright and statement
17 * are included in all such copies. Other copyrights may also apply.
18 *
19 * This file helps Angband work with Windows computers.
20 *
21 * To use this file, use an appropriate "Makefile" or "Project File",
22 * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
23 * make sure to obtain various extra files as described below.
24 *
25 * The Windows version has been tested to compile with Visual C++ 5.0
26 * and 6.0, Cygwin 1.0, Borland C++ 5.5 command line tools, and lcc-win32.
27 *
28 *
29 * The "lib/customize/font-win.prf" contains attr/char mappings for use with the
30 * normal "*.fon" font files in the "lib/fonts/" directory.
31 *
32 * The "lib/customize/graf-win.prf" contains attr/char mappings for use with the
33 * special "*.png" graphics files in the "lib/tiles/" directory, which
34 * are activated by a menu item.
35 *
36 *
37 * The "Term_xtra_win_clear()" function should probably do a low-level
38 * clear of the current window, and redraw the borders and other things,
39 * if only for efficiency. XXX XXX XXX
40 *
41 * A simpler method is needed for selecting the "tile size" for windows.
42 * XXX XXX XXX
43 *
44 * ToDo: The screensaver mode should implement ScreenSaverConfigureDialog,
45 * DefScreenSaverProc, and ScreenSaverProc.
46 *
47 * Initial framework (and most code) by Ben Harrison ([email protected]).
48 *
49 * Original code by Skirmantas Kligys ([email protected]).
50 *
51 * Additional code by Ross E Becker ([email protected]),
52 * and Chris R. Martin ([email protected]).
53 *
54 * Additional code by Robert Ruehlmann <[email protected]>.
55 */
56
57 #include "angband.h"
58 #include "buildid.h"
59 #include "cmds.h"
60 #include "cave.h"
61 #include "game-world.h"
62 #include "grafmode.h"
63 #include "init.h"
64 #include "savefile.h"
65 #include "ui-command.h"
66 #include "ui-display.h"
67 #include "ui-game.h"
68 #include "ui-init.h"
69 #include "ui-input.h"
70 #include "ui-map.h"
71 #include "ui-output.h"
72 #include "ui-prefs.h"
73 #include "win/win-menu.h"
74
75 /* Make sure the winver allows the AlphaBlend function */
76 #if (WINVER < 0x0500)
77 #define WINVER 0x0500
78 #endif
79
80 #include <locale.h>
81
82 #define uint unsigned int
83
84 #if defined(WINDOWS) && !defined(USE_SDL) && !defined(USE_SDL2)
85
86 #include "sound.h"
87 #include "snd-win.h"
88
89 #define HAS_CLEANUP
90
91 #ifdef ALLOW_BORG
92
93 /*
94 * Hack -- allow use of "screen saver" mode
95 */
96 #define USE_SAVER
97
98 #endif /* ALLOW_BORG */
99
100 /**
101 * This may need to be removed for some compilers XXX XXX XXX
102 */
103 #define STRICT
104
105 /**
106 * Exclude parts of WINDOWS.H that are not needed
107 */
108 #define NOCOMM /* Comm driver APIs and definitions */
109 #define NOLOGERROR /* LogError() and related definitions */
110 #define NOPROFILER /* Profiler APIs */
111 #define NOLFILEIO /* _l* file I/O routines */
112 #define NOOPENFILE /* OpenFile and related definitions */
113 #define NORESOURCE /* Resource management */
114 #define NOATOM /* Atom management */
115 #define NOLANGUAGE /* Character test routines */
116 #define NOLSTRING /* lstr* string management routines */
117 #define NODBCS /* Double-byte character set routines */
118 #define NOKEYBOARDINFO /* Keyboard driver routines */
119 #define NOCOLOR /* COLOUR_* color values */
120 #define NODRAWTEXT /* DrawText() and related definitions */
121 #define NOSCALABLEFONT /* Truetype scalable font support */
122 #define NOMETAFILE /* Metafile support */
123 #define NOSYSTEMPARAMSINFO /* SystemParametersInfo() and SPI_* definitions */
124 #define NODEFERWINDOWPOS /* DeferWindowPos and related definitions */
125 #define NOKEYSTATES /* MK_* message key state flags */
126 #define NOWH /* SetWindowsHook and related WH_* definitions */
127 #define NOCLIPBOARD /* Clipboard APIs and definitions */
128 #define NOICONS /* IDI_* icon IDs */
129 #define NOMDI /* MDI support */
130 #define NOHELP /* Help support */
131
132 /* Not defined since it breaks Borland C++ 5.5 */
133 /* #define NOCTLMGR */ /* Control management and controls */
134
135 /**
136 * Exclude parts of WINDOWS.H that are not needed (Win32)
137 */
138 #define WIN32_LEAN_AND_MEAN
139 /*#define NONLS*/ /* All NLS defines and routines */
140 #define NOSERVICE /* All Service Controller routines, SERVICE_ equates, etc. */
141 #define NOKANJI /* Kanji support stuff. */
142 #define NOMCX /* Modem Configuration Extensions */
143
144 /**
145 * Include the "windows" support file
146 */
147 #include <windows.h>
148 #include <windowsx.h>
149
150 #ifndef GetWindowLongPtr
151 #define GetWindowLongPtr GetWindowLong
152 #endif
153 #ifndef SetWindowLongPtr
154 #define SetWindowLongPtr SetWindowLong
155 #endif
156 #ifndef GWLP_USERDATA
157 #define GWLP_USERDATA GWL_USERDATA
158 #endif
159
160 /**
161 * Exclude parts of MMSYSTEM.H that are not needed
162 */
163 #define MMNODRV /* Installable driver support */
164 #define MMNOWAVE /* Waveform support */
165 #define MMNOMIDI /* MIDI support */
166 #define MMNOAUX /* Auxiliary audio support */
167 #define MMNOTIMER /* Timer support */
168 #define MMNOJOY /* Joystick support */
169 /*#define MMNOMCI */ /* MCI support */
170 #define MMNOMMIO /* Multimedia file I/O support */
171 #define MMNOMMSYSTEM /* General MMSYSTEM functions */
172
173 #include <mmsystem.h>
174
175 #include <commdlg.h>
176 #include <shellapi.h>
177
178 /**
179 * Include the support for loading bitmaps and saving screenshots
180 */
181 #include "win/readdib.h"
182 #include "win/scrnshot.h"
183
184 #include <wingdi.h>
185
186 /**
187 * Hack -- Fake declarations from "dos.h" XXX XXX XXX
188 */
189 #define INVALID_FILE_NAME (DWORD)0xFFFFFFFF
190
191 /**
192 * Silliness in WIN32 drawing routine
193 */
194 #define MoveTo(H,X,Y) MoveToEx(H, X, Y, NULL)
195
196 /**
197 * Silliness for Windows 95
198 */
199 #ifndef WS_EX_TOOLWINDOW
200 # define WS_EX_TOOLWINDOW 0
201 #endif /* WS_EX_TOOLWINDOW */
202
203 /**
204 * Foreground color bits (hard-coded by DOS)
205 */
206 #define VID_BLACK 0x00
207 #define VID_BLUE 0x01
208 #define VID_GREEN 0x02
209 #define VID_CYAN 0x03
210 #define VID_RED 0x04
211 #define VID_MAGENTA 0x05
212 #define VID_YELLOW 0x06
213 #define VID_WHITE 0x07
214
215 /**
216 * Bright text (hard-coded by DOS)
217 */
218 #define VID_BRIGHT 0x08
219
220 /**
221 * Background color bits (hard-coded by DOS)
222 */
223 #define VUD_BLACK 0x00
224 #define VUD_BLUE 0x10
225 #define VUD_GREEN 0x20
226 #define VUD_CYAN 0x30
227 #define VUD_RED 0x40
228 #define VUD_MAGENTA 0x50
229 #define VUD_YELLOW 0x60
230 #define VUD_WHITE 0x70
231
232 /**
233 * Blinking text (hard-coded by DOS)
234 */
235 #define VUD_BRIGHT 0x80
236
237 /**
238 * Font settings
239 */
240 #define DEFAULT_FONT "8X12x.FON"
241
242
243 /**
244 * Extra "term" data
245 *
246 * Note the use of "font_want" for the names of the font file requested by
247 * the user, and the use of "font_file" for the currently active font file.
248 *
249 * The "font_file" is uppercased, and takes the form "8X13.FON", while
250 * "font_want" can be in almost any form as long as it could be construed
251 * as attempting to represent the name of a font.
252 */
253 #include "win/win-term.h"
254
255 bool use_graphics_nice;
256
257 /**
258 * An array of term_data's
259 */
260 static term_data data[MAX_TERM_DATA];
261
262 /**
263 * Hack -- global "window creation" pointer
264 */
265 static term_data *my_td;
266
267
268 /**
269 * game in progress
270 */
271 bool game_in_progress = false;
272
273 /**
274 * note when "open"/"new" become valid
275 */
276 bool initialized = false;
277
278 /**
279 * This is a handle to a file; used to protect against playing with the same
280 * character simultaneously in multiple instances of the application. Using
281 * a named mutex did not appear to work (at least with wine). Using a file
282 * has the disadvantage that a system crash while playing a character will
283 * require the player to delete a file in order to bypass the check that
284 * another copy of the game is running.
285 */
286 static HANDLE multapp_file = INVALID_HANDLE_VALUE;
287 static bool create_savefile_tracking_file(bool message_on_failure);
288 static bool monitor_existing_savefile(void);
289 static void monitor_new_savefile(game_event_type ev_type,
290 game_event_data *ev_data, void *user);
291 static void finish_monitoring_savefile(game_event_type ev_type,
292 game_event_data *ev_data, void *user);
293
294 /* prototype functions passed to windows */
295 size_t Term_mbstowcs_win(wchar_t* dest, const char* src, int n);
296 int Term_wcsz_win(void);
297 int Term_wctomb_win(char* s, wchar_t wchar);
298 int Term_iswprint_win(wint_t wc);
299 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
300
301
302 /**
303 * screen paletted, i.e. 256 colors
304 */
305 bool paletted = false;
306
307 /**
308 * 16 colors screen, don't use RGB()
309 */
310 bool colors16 = false;
311
312 static bool low_priority = false;
313
314 /**
315 * Saved instance handle
316 */
317 static HINSTANCE hInstance;
318
319 /**
320 * Yellow brush for the cursor
321 */
322 static HBRUSH hbrYellow;
323
324 /**
325 * An icon
326 */
327 static HICON hIcon;
328
329 /**
330 * A palette
331 */
332 static HPALETTE hPal;
333
334
335 #ifdef USE_SAVER
336
337 /**
338 * The screen saver window
339 */
340 static HWND hwndSaver;
341
342 static HANDLE screensaverSemaphore;
343
344 static char saverfilename[1024];
345
346 static HMENU main_menu;
347
348 #define MOUSE_SENS 10
349
350 #endif /* USE_SAVER */
351
352 static bool screensaver_active = false;
353
354
355 /**
356 * Flag set once "graphics" has been initialized
357 */
358 static bool can_use_graphics = false;
359
360 /**
361 * Flag set when switching tilesizes
362 */
363 static bool change_tilesize = false;
364
365 /**
366 * The global bitmap
367 */
368 static DIBINIT infGraph;
369
370 /**
371 * The global bitmap mask
372 */
373 static DIBINIT infMask;
374
375 static int overdraw = 0;
376 static int overdrawmax = -1;
377
378 static int alphablend = 0;
379 static BLENDFUNCTION blendfn;
380
381 /**
382 * Full path to ANGBAND.INI
383 */
384 static char *ini_file = NULL;
385
386 /**
387 * Name of application
388 */
389 static const char *AppName = VERSION_NAME;
390
391 /**
392 * Name of sub-window type
393 */
394 static const char *AngList = "AngList";
395
396 /**
397 * The "complex" color values
398 */
399 static COLORREF win_clr[MAX_COLORS];
400
401
402 /**
403 * The "simple" color values
404 *
405 * See "main-ibm.c" for original table information
406 *
407 * The entries below are taken from the "color bits" defined above.
408 *
409 * Note that many of the choices below suck, but so do crappy monitors.
410 */
411 static uint8_t win_pal[MAX_COLORS] =
412 {
413 VID_BLACK, /* Dark */
414 VID_WHITE, /* White */
415 VID_CYAN, /* Slate XXX */
416 VID_RED | VID_BRIGHT, /* Orange XXX */
417 VID_RED, /* Red */
418 VID_GREEN, /* Green */
419 VID_BLUE, /* Blue */
420 VID_YELLOW, /* Umber XXX */
421 VID_BLACK | VID_BRIGHT, /* Light Dark */
422 VID_CYAN | VID_BRIGHT, /* Light Slate XXX */
423 VID_MAGENTA, /* Violet XXX */
424 VID_YELLOW | VID_BRIGHT, /* Yellow */
425 VID_MAGENTA | VID_BRIGHT, /* Light Red XXX */
426 VID_GREEN | VID_BRIGHT, /* Light Green */
427 VID_BLUE | VID_BRIGHT, /* Light Blue */
428 VID_YELLOW /* Light Umber XXX */
429 };
430
431
432 static int gamma_correction;
433
434 static void show_win_error(void)
435 {
436 LPVOID lpMsgBuf;
437
438 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
439 NULL, GetLastError(),
440 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
441 (LPTSTR) &lpMsgBuf, 0, NULL);
442
443 MessageBox(NULL, lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION);
444
445 LocalFree(lpMsgBuf);
446 }
447
448
449 /**
450 * Hack -- given a simple filename, extract the "font size" info
451 *
452 * Return a pointer to a static buffer holding the capitalized base name.
453 */
454 static char *analyze_font(char *path, int *wp, int *hp)
455 {
456 int wid, hgt;
457
458 char *s, *p;
459
460 /* Start at the end */
461 p = path + strlen(path) - 1;
462
463 /* Back up to divider */
464 while ((p >= path) && (*p != ':') && (*p != '\\')) --p;
465
466 /* Advance to file name */
467 ++p;
468
469 /* Capitalize */
470 for (s = p; *s; ++s) {
471 /* Capitalize (be paranoid) */
472 if (islower((unsigned char)*s)) *s = toupper((unsigned char)*s);
473 }
474
475 /* Find first 'X' */
476 s = strchr(p, 'X');
477
478 /* Extract font width */
479 wid = atoi(p);
480
481 /* Extract height */
482 hgt = s ? atoi(s+1) : 0;
483
484 /* Save results */
485 (*wp) = wid;
486 (*hp) = hgt;
487
488 /* Result */
489 return (p);
490 }
491
492
493 /**
494 * Check for existence of a directory
495 */
496 static bool check_dir(const char *s)
497 {
498 int i;
499 char path[1024];
500 DWORD attrib;
501
502 /* Copy it */
503 my_strcpy(path, s, sizeof(path));
504
505 /* Check length */
506 i = strlen(path);
507
508 /* Remove trailing backslash */
509 if (i && (path[i-1] == '\\')) path[--i] = '\0';
510
511 /* Examine */
512 attrib = GetFileAttributes(path);
513
514 /* Require valid filename */
515 if (attrib == INVALID_FILE_NAME) return (false);
516
517 /* Require directory */
518 if (!(attrib & FILE_ATTRIBUTE_DIRECTORY)) return (false);
519
520 /* Success */
521 return (true);
522 }
523
524
525 /**
526 * Validate a file
527 */
528 static void validate_file(const char *s)
529 {
530 /* Verify or fail */
531 if (!file_exists(s))
532 quit_fmt("Cannot find required file:\n%s", s);
533 }
534
535
536 /**
537 * Validate a directory
538 */
539 static void validate_dir(const char *s)
540 {
541 /* Verify or fail */
542 if (!check_dir(s))
543 quit_fmt("Cannot find required directory:\n%s", s);
544 }
545
546
547 /**
548 * Get the "size" for a window
549 */
550 static void term_getsize(term_data *td)
551 {
552 RECT rc;
553
554 int wid, hgt;
555
556 /* Paranoia */
557 if (td->cols < 1) td->cols = 1;
558 if (td->rows < 1) td->rows = 1;
559
560 if (use_graphics_nice) {
561 /*
562 * This is the gamut of multipliers available in the
563 * IDM_OPTIONS_TILE_* constants, sorted in increasing order
564 * of the area of the scaled up block.
565 */
566 const struct { int w, h; } multipliers[] = {
567 { 1, 1 },
568 { 2, 1 },
569 { 3, 1 },
570 { 2, 2 },
571 { 4, 2 },
572 { 3, 3 },
573 { 4, 4 },
574 { 6, 3 },
575 { 8, 4 },
576 { 6, 6 },
577 { 8, 8 },
578 { 16, 8 },
579 { 16, 16 },
580 };
581 long best;
582 int ibest, i;
583
584 if (current_graphics_mode && current_graphics_mode->grafID) {
585 if (current_graphics_mode->file[0]) {
586 char *end;
587 td->tile_wid = strtol(
588 current_graphics_mode->file,&end,10);
589 td->tile_hgt = strtol(end+1,NULL,10);
590 } else {
591 td->tile_wid =
592 current_graphics_mode->cell_width;
593 td->tile_hgt =
594 current_graphics_mode->cell_height;
595 }
596 if ((td->tile_wid == 0) || (td->tile_hgt == 0)) {
597 td->tile_wid =
598 current_graphics_mode->cell_width;
599 td->tile_hgt =
600 current_graphics_mode->cell_height;
601 }
602 if ((td->tile_wid == 0) || (td->tile_hgt == 0)) {
603 td->tile_wid = td->font_wid;
604 td->tile_hgt = td->font_hgt;
605 }
606 } else {
607 /* Reset the tile info */
608 td->tile_wid = td->font_wid;
609 td->tile_hgt = td->font_hgt;
610 }
611
612 /*
613 * If the tile is enough smaller than the font in either
614 * dimension, consider using a scaled up version (preserving
615 * the aspect ratio) of the tile as the target size.
616 */
617 if (td->tile_wid <= (2 * td->font_wid) / 3
618 || td->tile_hgt <= (2 * td->font_wid) / 3) {
619 int area_ratio = (int) (((long) td->font_wid
620 * (long) td->font_hgt + ((long) td->tile_wid
621 * (long) td->tile_hgt) / 2)
622 / ((long) td->tile_wid * (long) td->tile_hgt));
623
624 best = abs((long) td->font_wid * (long) td->font_hgt
625 - (long) td->tile_wid * (long) td->tile_hgt);
626 ibest = 1;
627 i = 2;
628 while (!best && (i - 1) * (i - 1) <= area_ratio) {
629 int try_best = abs((long) td->font_wid
630 * (long) td->font_hgt
631 - ((long) td->tile_wid * i)
632 * ((long) td->tile_hgt * i));
633
634 if (best > try_best) {
635 best = try_best;
636 ibest = i;
637 }
638 ++i;
639 }
640 td->tile_wid *= i;
641 td->tile_hgt *= i;
642 }
643
644 /*
645 * Find the best multiplier (size of the scaled up font does
646 * not exceed the tile size in either dimension, and the area
647 * of the scaled up font is closest to the area of the tile).
648 */
649 ibest = -1;
650 best = (long) td->tile_wid * (long) td->tile_hgt;
651 for (i = 0; i < (int) N_ELEMENTS(multipliers) && best; ++i) {
652 uint sclw = td->font_wid * multipliers[i].w;
653 uint sclh = td->font_hgt * multipliers[i].h;
654
655 if (sclw <= td->tile_wid && sclh <= td->tile_hgt) {
656 int try_best = abs(
657 (long) td->tile_wid
658 * (long) td->tile_hgt
659 - (long) sclw * (long) sclh);
660
661 if (best > try_best) {
662 best = try_best;
663 ibest = i;
664 }
665 }
666 }
667
668 if (ibest >= 0) {
669 tile_width = multipliers[ibest].w;
670 tile_height = multipliers[ibest].h;
671 td->tile_wid /= tile_width;
672 td->tile_hgt /= tile_height;
673 } else {
674 tile_width = 1;
675 tile_height = 1;
676 }
677
678 if (td->tile_wid < td->font_wid) td->tile_wid = td->font_wid;
679 if (td->tile_hgt < td->font_hgt) td->tile_hgt = td->font_hgt;
680 }
681
682 /* Window sizes */
683 wid = td->cols * td->tile_wid + td->size_ow1 + td->size_ow2;
684 hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
685
686 /* Client window size */
687 rc.left = 0;
688 rc.right = rc.left + wid;
689 rc.top = 0;
690 rc.bottom = rc.top + hgt;
691
692 /* Get total window size (without menu for sub-windows) */
693 AdjustWindowRectEx(&rc, td->dwStyle, true, td->dwExStyle);
694
695 /* Total size */
696 td->size_wid = rc.right - rc.left;
697 td->size_hgt = rc.bottom - rc.top;
698
699 /* See CreateWindowEx */
700 if (!td->w) return;
701
702 /* Extract actual location */
703 GetWindowRect(td->w, &rc);
704
705 /* Save the location */
706 td->pos_x = rc.left;
707 td->pos_y = rc.top;
708 }
709
710
711 /**
712 * Write the "prefs" for a single term
713 */
714 static void save_prefs_aux(term_data *td, const char *sec_name)
715 {
716 char buf[1024];
717
718 RECT rc;
719
720 WINDOWPLACEMENT lpwndpl;
721
722 /* Paranoia */
723 if (!td->w) return;
724
725 /* Visible */
726 my_strcpy(buf, td->visible ? "1" : "0", sizeof(buf));
727 WritePrivateProfileStringA(sec_name, "Visible", buf, ini_file);
728
729 /* Font */
730 my_strcpy(buf, td->font_file ? td->font_file : DEFAULT_FONT, sizeof(buf));
731 WritePrivateProfileStringA(sec_name, "Font", buf, ini_file);
732
733 /* Bizarre */
734 my_strcpy(buf, td->bizarre ? "1" : "0", sizeof(buf));
735 WritePrivateProfileStringA(sec_name, "Bizarre", buf, ini_file);
736
737 /* Tile size (x) */
738 strnfmt(buf, sizeof(buf), "%d", td->tile_wid);
739 WritePrivateProfileStringA(sec_name, "TileWid", buf, ini_file);
740
741 /* Tile size (y) */
742 strnfmt(buf, sizeof(buf), "%d", td->tile_hgt);
743 WritePrivateProfileStringA(sec_name, "TileHgt", buf, ini_file);
744
745 /* Window size (x) */
746 strnfmt(buf, sizeof(buf), "%d", td->cols);
747 WritePrivateProfileStringA(sec_name, "NumCols", buf, ini_file);
748
749 /* Window size (y) */
750 strnfmt(buf, sizeof(buf), "%d", td->rows);
751 WritePrivateProfileStringA(sec_name, "NumRows", buf, ini_file);
752
753 /* Get window placement and dimensions */
754 lpwndpl.length = sizeof(WINDOWPLACEMENT);
755 GetWindowPlacement(td->w, &lpwndpl);
756
757 /* Acquire position in *normal* mode (not minimized) */
758 rc = lpwndpl.rcNormalPosition;
759
760 /* Get information about the placement of the window */
761 if (lpwndpl.showCmd & SW_SHOWMAXIMIZED)
762 td->maximized = true;
763 else
764 td->maximized = false;
765
766 /* Window position (x) */
767 strnfmt(buf, sizeof(buf), "%ld", rc.left);
768 WritePrivateProfileStringA(sec_name, "PositionX", buf, ini_file);
769
770 /* Window position (y) */
771 strnfmt(buf, sizeof(buf), "%ld", rc.top);
772 WritePrivateProfileStringA(sec_name, "PositionY", buf, ini_file);
773
774 /* Maximized */
775 my_strcpy(buf, td->maximized ? "1" : "0", sizeof(buf));
776 WritePrivateProfileStringA(sec_name, "Maximized", buf, ini_file);
777 }
778
779
780 /**
781 * Write the "prefs"
782 *
783 * We assume that the windows have all been initialized
784 */
785 static void save_prefs(void)
786 {
787 int i;
788
789 char buf[128];
790
791 /* Save the "arg_graphics" flag */
792 strnfmt(buf, sizeof(buf), "%d", arg_graphics);
793 WritePrivateProfileStringA("Angband", "Graphics", buf, ini_file);
794
795 /* Save the "use_graphics_nice" flag */
796 my_strcpy(buf, arg_graphics_nice ? "1" : "0", sizeof(buf));
797 WritePrivateProfileStringA("Angband", "Graphics_Nice", buf, ini_file);
798
799 /* Save the tile width */
800 strnfmt(buf, sizeof(buf), "%d", tile_width);
801 WritePrivateProfileStringA("Angband", "TileWidth", buf, ini_file);
802
803 /* Save the tile height */
804 strnfmt(buf, sizeof(buf), "%d", tile_height);
805 WritePrivateProfileStringA("Angband", "TileHeight", buf, ini_file);
806
807 /* Save window prefs */
808 for (i = 0; i < MAX_TERM_DATA; i++) {
809 term_data *td = &data[i];
810
811 strnfmt(buf, sizeof(buf), "Term-%d", i);
812
813 save_prefs_aux(td, buf);
814 }
815 }
816
817
818 /**
819 * Load the "prefs" for a single term
820 */
821 static void load_prefs_aux(term_data *td, const char *sec_name)
822 {
823 char tmp[1024];
824
825 int wid, hgt;
826
827 /* Visible */
828 td->visible = (GetPrivateProfileIntA(sec_name, "Visible", td->visible,
829 ini_file) != 0);
830
831 /* Maximized */
832 td->maximized = (GetPrivateProfileIntA(sec_name, "Maximized",
833 td->maximized, ini_file) != 0);
834
835 /* Desired font, with default */
836 GetPrivateProfileStringA(sec_name, "Font", DEFAULT_FONT, tmp, 127,
837 ini_file);
838
839 /* Bizarre */
840 td->bizarre = (GetPrivateProfileInt(sec_name, "Bizarre", true,
841 ini_file) != 0);
842
843 /* Analyze font, save desired font name */
844 td->font_want = string_make(analyze_font(tmp, &wid, &hgt));
845
846 /* Tile size */
847 td->tile_wid = GetPrivateProfileIntA(sec_name, "TileWid", wid, ini_file);
848 td->tile_hgt = GetPrivateProfileIntA(sec_name, "TileHgt", hgt, ini_file);
849
850 /* Window size */
851 td->cols = GetPrivateProfileIntA(sec_name, "NumCols", td->cols, ini_file);
852 td->rows = GetPrivateProfileIntA(sec_name, "NumRows", td->rows, ini_file);
853
854 /* Window position */
855 td->pos_x = GetPrivateProfileIntA(sec_name, "PositionX", td->pos_x,
856 ini_file);
857 td->pos_y = GetPrivateProfileIntA(sec_name, "PositionY", td->pos_y,
858 ini_file);
859 }
860
861
862 /**
863 * Load the "prefs"
864 */
865 static void load_prefs(void)
866 {
867 int i;
868
869 char buf[1024];
870 bool first_start;
871
872 if (file_exists(ini_file)) {
873 first_start = false;
874 } else {
875 first_start = true;
876 }
877
878 /* Extract the "arg_graphics" flag */
879 arg_graphics = GetPrivateProfileIntA("Angband", "Graphics",
880 GRAPHICS_NONE, ini_file);
881
882 /* Extract the "arg_graphics_nice" flag */
883 arg_graphics_nice = GetPrivateProfileIntA("Angband", "Graphics_Nice",
884 true, ini_file);
885
886 /* Extract the tile width */
887 tile_width = GetPrivateProfileIntA("Angband", "TileWidth", false,
888 ini_file);
889
890 /* Extract the tile height */
891 tile_height = GetPrivateProfileIntA("Angband", "TileHeight", false,
892 ini_file);
893
894 /* Extract the "arg_wizard" flag */
895 arg_wizard = (GetPrivateProfileIntA("Angband", "Wizard", 0, ini_file) != 0);
896
897 /* Extract the gamma correction */
898 gamma_correction = GetPrivateProfileIntA("Angband", "Gamma", 0, ini_file);
899
900
901 /* Load window prefs */
902 for (i = 0; i < MAX_TERM_DATA; i++) {
903 term_data *td = &data[i];
904
905 strnfmt(buf, sizeof(buf), "Term-%d", i);
906
907 load_prefs_aux(td, buf);
908 }
909
910 if (first_start) {
911 default_layout_win(data,MAX_TERM_DATA);
912 }
913
914 /* Paranoia */
915 if (data[0].cols < 80) data[0].cols = 80;
916 if (data[0].rows < 24) data[0].rows = 24;
917 }
918
919 /**
920 * Create the new global palette based on the bitmap palette
921 * (if any), and the standard 16 entry palette derived from
922 * "win_clr[]" which is used for the basic 16 Angband colors.
923 *
924 * This function is never called before all windows are ready.
925 *
926 * This function returns false if the new palette could not be
927 * prepared, which should normally be a fatal error. XXX XXX
928 *
929 * Note that only some machines actually use a "palette".
930 */
931 static int new_palette(void)
932 {
933 HPALETTE hBmPal;
934 HPALETTE hNewPal;
935 HDC hdc;
936 int i, nEntries;
937 int pLogPalSize;
938 LPLOGPALETTE pLogPal;
939 LPPALETTEENTRY lppe;
940
941 term_data *td;
942
943
944 /* This makes no sense */
945 if (!paletted) return (true);
946
947 /* No bitmap */
948 lppe = NULL;
949 nEntries = 0;
950
951 /* Check the bitmap palette */
952 hBmPal = infGraph.hPalette;
953
954 /* Use the bitmap */
955 if (hBmPal) {
956 lppe = mem_alloc(256 * sizeof(PALETTEENTRY));
957 nEntries = GetPaletteEntries(hBmPal, 0, 255, lppe);
958 if ((nEntries == 0) || (nEntries > 220)) {
959 /* Warn the user */
960 plog("Please switch to high- or true-color mode.");
961
962 /* Cleanup */
963 mem_free(lppe);
964
965 /* Fail */
966 return (false);
967 }
968 }
969
970 /* Size of palette */
971 pLogPalSize = sizeof(LOGPALETTE) + (nEntries + 16) * sizeof(PALETTEENTRY);
972
973 /* Allocate palette */
974 pLogPal = (LPLOGPALETTE)mem_alloc(pLogPalSize);
975
976 /* Version */
977 pLogPal->palVersion = 0x300;
978
979 /* Make room for bitmap and normal data */
980 pLogPal->palNumEntries = nEntries + 16;
981
982 /* Save the bitmap data */
983 for (i = 0; i < nEntries; i++)
984 pLogPal->palPalEntry[i] = lppe[i];
985
986 /* Save the normal data */
987 for (i = 0; i < BASIC_COLORS; i++) {
988 LPPALETTEENTRY p;
989
990 /* Access the entry */
991 p = &(pLogPal->palPalEntry[i+nEntries]);
992
993 /* Save the colors */
994 p->peRed = GetRValue(win_clr[i]);
995 p->peGreen = GetGValue(win_clr[i]);
996 p->peBlue = GetBValue(win_clr[i]);
997
998
999 if (gamma_correction > 0) {
1000 p->peRed = gamma_table[p->peRed];
1001 p->peGreen = gamma_table[p->peGreen];
1002 p->peBlue = gamma_table[p->peBlue];
1003 }
1004
1005
1006 /* Save the flags */
1007 p->peFlags = PC_NOCOLLAPSE;
1008 }
1009
1010 /* Free something */
1011 if (lppe) mem_free(lppe);
1012
1013 /* Create a new palette, or fail */
1014 hNewPal = CreatePalette(pLogPal);
1015 if (!hNewPal) quit("Cannot create palette!");
1016
1017 /* Free the palette */
1018 mem_free(pLogPal);
1019
1020 /* Main window */
1021 td = &data[0];
1022
1023 /* Realize the palette */
1024 hdc = GetDC(td->w);
1025 SelectPalette(hdc, hNewPal, 0);
1026 i = RealizePalette(hdc);
1027 ReleaseDC(td->w, hdc);
1028 if (i == 0) quit("Cannot realize palette!");
1029
1030 /* Sub-windows */
1031 for (i = 1; i < MAX_TERM_DATA; i++) {
1032 td = &data[i];
1033
1034 hdc = GetDC(td->w);
1035 SelectPalette(hdc, hNewPal, 0);
1036 ReleaseDC(td->w, hdc);
1037 }
1038
1039 /* Delete old palette */
1040 if (hPal) DeleteObject(hPal);
1041
1042 /* Save new palette */
1043 hPal = hNewPal;
1044
1045 /* Success */
1046 return (true);
1047 }
1048
1049
1050 /**
1051 * Initialize graphics
1052 */
1053 static bool init_graphics(void)
1054 {
1055 /* Initialize once */
1056 char buf[1024];
1057 int wid, hgt;
1058 const char *name;
1059 const char *path;
1060 graphics_mode *mode = NULL;
1061
1062 if (arg_graphics) {
1063 mode = get_graphics_mode(arg_graphics);
1064 }
1065 if (!mode) {
1066 mode = get_graphics_mode(1);
1067 }
1068 if (mode) {
1069 if (!mode->pref[0]) {
1070 plog_fmt("invalid tile prefname '%s'", mode->menuname);
1071 return false;
1072 }
1073 wid = mode->cell_width;
1074 hgt = mode->cell_height;
1075 if ((wid < 2) || (hgt < 2)) {
1076 plog_fmt("invalid tile dimensions in tileset: '%s'",
1077 mode->menuname);
1078 return false;
1079 }
1080
1081 path = mode->path;
1082 name = mode->file;
1083
1084 overdraw = mode->overdrawRow;
1085 overdrawmax = mode->overdrawMax;
1086 alphablend = mode->alphablend;
1087
1088 current_graphics_mode = mode;
1089 } else {
1090 plog("could not find graphics mode");
1091 return false;
1092 }
1093
1094 /* Access the bitmap file */
1095 path_build(buf, sizeof(buf), path, name);
1096
1097 /* Load the image or quit */
1098 if (alphablend) {
1099 /* see if the given file is already pre mulitiplied */
1100 if (strstr(name, "_pre")) {
1101 /* if so, just load it */
1102 if (!ReadDIB2_PNG(data[0].w, buf, &infGraph, NULL, false)) {
1103 plog_fmt("Cannot read file '%s'", name);
1104 return false;
1105 }
1106 } else {
1107 /* if not, see if there is already a premultiplied tileset */
1108 /* the there is load it */
1109 char *ext;
1110 char modname[1024];
1111 bool have_space = 0;
1112 my_strcpy(modname, buf, sizeof(modname));
1113 ext = strstr(modname, ".png");
1114 /* make sure we have enough space to make the desired name */
1115 if (ext && ((size_t)(ext - modname)
1116 < sizeof(modname) - 9)) {
1117 have_space = true;
1118 my_strcpy(ext, "_pre.png",
1119 sizeof(modname) - (ext - buf));
1120 if (!file_exists(modname)) {
1121 /* if the file does not exist, mark that we need to
1122 * create it, so clear the extension pointer */
1123 ext = NULL;
1124 } else if (file_newer(buf, modname)) {
1125 /* if the base file is newer than the premultiplied file,
1126 * mark that we need to recreate the premultiplied file. */
1127 ext = NULL;
1128 }
1129 }
1130 if (ext && have_space) {
1131 /* at this point we know the file exists, so load it */
1132 if (!ReadDIB2_PNG(data[0].w, modname, &infGraph, NULL, false)) {
1133 plog_fmt("Cannot read premultiplied version of file '%s'",
1134 name);
1135 return false;
1136 }
1137 } else {
1138 /* if not, load the base file and premultiply it */
1139 if (!ReadDIB2_PNG(data[0].w, buf, &infGraph, NULL, true)) {
1140 plog_fmt("Cannot read file '%s'", name);
1141 return false;
1142 }
1143 /* save the premultiplied file */
1144 /* saving alpha without a mask is not working yet
1145 if (SavePNG(data[0].w, modname,
1146 infGraph.hBitmap,infGraph.hPalette,
1147 1, NULL,
1148 infGraph.ImageWidth, infGraph.ImageHeight, false) < 0) {
1149 plog_fmt("Cannot write premultiplied version of file '%s'", name);
1150 }*/
1151 }
1152 }
1153 } else {
1154 if (!ReadDIB2_PNG(data[0].w, buf, &infGraph, &infMask, false)) {
1155 plog_fmt("Cannot read file '%s'", name);
1156 return false;
1157 }
1158 }
1159
1160 /* Save the new sizes */
1161 infGraph.CellWidth = wid;
1162 infGraph.CellHeight = hgt;
1163
1164 /* Activate a palette */
1165 if (!new_palette()){
1166 /* Free bitmap XXX XXX XXX */
1167
1168 /* Oops */
1169 plog("Cannot activate palette!");
1170 return (false);
1171 }
1172
1173 /* Graphics available */
1174 can_use_graphics = arg_graphics;
1175
1176 /* Result */
1177 return (can_use_graphics);
1178 }
1179
1180 #if defined(SOUND) && !defined(SOUND_SDL) && !defined(SOUND_SDL2)
1181
1182 /* Supported file types */
1183 enum {
1184 WIN_NULL = 0,
1185 WIN_MP3,
1186 WIN_WAV
1187 };
1188
1189 const struct sound_file_type supported_sound_files[] = { {".mp3", WIN_MP3},
1190 {".wav", WIN_WAV},
1191 {"", WIN_NULL} };
1192
1193 typedef struct
1194 {
1195 int type;
1196 MCI_OPEN_PARMS op;
1197 char *filename;
1198 } win_sample;
1199
1200 /**
1201 * Load a sound
1202 */
1203 static bool load_sound_win(const char *filename, int ftyp,
1204 struct sound_data *sd)
1205 {
1206 win_sample *sample = NULL;
1207
1208 sample = (win_sample *)(sd->plat_data);
1209
1210 switch (ftyp) {
1211 case WIN_MP3:
1212 if (!sample)
1213 sample = mem_zalloc(sizeof(*sample));
1214
1215 /* Open if not already */
1216 if (!sample->op.wDeviceID) {
1217 sample->op.dwCallback = 0;
1218 sample->op.lpstrDeviceType = (char*)MCI_ALL_DEVICE_ID;
1219 sample->op.lpstrElementName = filename;
1220 sample->op.lpstrAlias = NULL;
1221
1222 /* Open command */
1223 mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT | MCI_WAIT, (size_t)(&sample->op));
1224 }
1225
1226 if (0 != sample->op.wDeviceID) {
1227 sd->status = SOUND_ST_LOADED;
1228 } else {
1229 mem_free(sample);
1230 sample = NULL;
1231 }
1232 break;
1233
1234 case WIN_WAV:
1235 if (!sample)
1236 sample = mem_alloc(sizeof(*sample));
1237
1238 sample->filename = mem_zalloc(strlen(filename) + 1);
1239 my_strcpy(sample->filename, filename, strlen(filename) + 1);
1240 sd->status = SOUND_ST_LOADED;
1241 break;
1242
1243 default:
1244 plog_fmt("Sound: Oops - Unsupported file type");
1245 break;
1246 }
1247
1248 if (sample) {
1249 sample->type = ftyp;
1250 }
1251 sd->plat_data = (void *)sample;
1252
1253 return (NULL != sample);
1254 }
1255
1256 /**
1257 * Play a sound
1258 */
1259 static bool play_sound_win(struct sound_data *sd)
1260 {
1261 MCI_PLAY_PARMS pp;
1262
1263 win_sample *sample = (win_sample *)(sd->plat_data);
1264
1265 if (sample) {
1266 switch (sample->type) {
1267 case WIN_MP3:
1268 if (sample->op.wDeviceID) {
1269 /* Play command */
1270 pp.dwCallback = 0;
1271 pp.dwFrom = 0;
1272 return (!mciSendCommand(sample->op.wDeviceID, MCI_PLAY, MCI_NOTIFY | MCI_FROM, (size_t)&pp));
1273 }
1274 break;
1275
1276 case WIN_WAV:
1277 if (sample->filename)
1278 {
1279 /* If another sound is currently playing, stop it */
1280 if (PlaySound(NULL, 0, SND_PURGE))
1281 /* Play the sound, catch errors */
1282 return PlaySound(sample->filename, 0, SND_FILENAME | SND_ASYNC);
1283 }
1284 break;
1285 default:
1286 /* Not supported */
1287 break;
1288 }
1289 }
1290
1291 return true;
1292 }
1293
1294 static bool unload_sound_win(struct sound_data *sd)
1295 {
1296 win_sample *sample = (win_sample *)(sd->plat_data);
1297
1298 if (sample) {
1299 switch (sample->type) {
1300 case WIN_MP3:
1301 if (sample->op.wDeviceID)
1302 mciSendCommand(sample->op.wDeviceID, MCI_CLOSE, MCI_WAIT, (size_t)(&sample->op));
1303
1304 break;
1305
1306 case WIN_WAV:
1307 mem_free(sample->filename);
1308 break;
1309
1310 default:
1311 break;
1312 }
1313
1314 mem_free(sample);
1315 sd->plat_data = NULL;
1316 sd->status = SOUND_ST_UNKNOWN;
1317 }
1318
1319 return true;
1320 }
1321
1322 static bool open_audio_win(void)
1323 {
1324 return true;
1325 }
1326
1327 static bool close_audio_win(void)
1328 {
1329 return true;
1330 }
1331
1332 static const struct sound_file_type *supported_files_win(void)
1333 {
1334 return supported_sound_files;
1335 }
1336
1337 /**
1338 * Initialize sound
1339 */
1340 errr init_sound_win(struct sound_hooks *hooks, int argc, char **argv)
1341 {
1342 hooks->open_audio_hook = open_audio_win;
1343 hooks->supported_files_hook = supported_files_win;
1344 hooks->close_audio_hook = close_audio_win;
1345 hooks->load_sound_hook = load_sound_win;
1346 hooks->unload_sound_hook = unload_sound_win;
1347 hooks->play_sound_hook = play_sound_win;
1348
1349 /* Success */
1350 return (0);
1351 }
1352 #endif /* SOUND && !SOUND_SDL && !SOUND_SDL2 */
1353
1354
1355 /**
1356 * Resize a window
1357 */
1358 static void term_window_resize(const term_data *td)
1359 {
1360 /* Require window */
1361 if (!td->w) return;
1362
1363 /* Resize the window */
1364 SetWindowPos(td->w, 0, 0, 0, td->size_wid, td->size_hgt,
1365 SWP_NOMOVE | SWP_NOZORDER);
1366
1367 /* Redraw later */
1368 InvalidateRect(td->w, NULL, true);
1369 }
1370
1371
1372 /**
1373 * Remove a font, given its filename.
1374 */
1375 static void term_remove_font(const char *name)
1376 {
1377 char buf[1024];
1378
1379 /* Build path to the file */
1380 my_strcpy(buf, ANGBAND_DIR_FONTS, sizeof(buf));
1381 my_strcat(buf, "\\", sizeof(buf));
1382 my_strcat(buf, name, sizeof(buf));
1383
1384 /* Remove it */
1385 RemoveFontResource(buf);
1386
1387 /* Notify other applications of the change XXX */
1388 PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
1389
1390 return;
1391 }
1392
1393
1394 /**
1395 * Force the use of a new "font file" for a term_data
1396 *
1397 * This function may be called before the "window" is ready
1398 *
1399 * This function returns zero only if everything succeeds.
1400 *
1401 * Note that the "font name" must be capitalized!!!
1402 */
1403 static errr term_force_font(term_data *td, const char *path)
1404 {
1405 int i;
1406
1407 int wid, hgt;
1408
1409 char *base;
1410
1411 char buf[1024];
1412
1413
1414 /* Check we have a path */
1415 if (!path) return (1);
1416
1417
1418 /* Forget the old font (if needed) */
1419 if (td->font_id) DeleteObject(td->font_id);
1420
1421 /* Forget old font */
1422 if (td->font_file) {
1423 bool used = false;
1424
1425 /* Scan windows */
1426 for (i = 0; i < MAX_TERM_DATA; i++) {
1427 /* Check "screen" */
1428 if ((td != &data[i]) && (data[i].font_file) &&
1429 (streq(data[i].font_file, td->font_file))) {
1430 used = true;
1431 }
1432 }
1433
1434 /* Remove unused font resources */
1435 if (!used) term_remove_font(td->font_file);
1436
1437 /* Free the old name */
1438 string_free(td->font_file);
1439
1440 /* Forget it */
1441 td->font_file = NULL;
1442 }
1443
1444
1445
1446 /* Local copy */
1447 my_strcpy(buf, path, sizeof(buf));
1448
1449 /* Analyze font path */
1450 base = analyze_font(buf, &wid, &hgt);
1451
1452 /* Verify suffix */
1453 if (!suffix(base, ".FON")) return (1);
1454
1455 /* Verify file */
1456 if (!file_exists(buf)) return (1);
1457
1458
1459 /* Load the new font */
1460 if (!AddFontResourceEx(buf, FR_PRIVATE, 0)) return (1);
1461
1462 /* Save new font name */
1463 td->font_file = string_make(base);
1464
1465 /* Remove the "suffix" */
1466 base[strlen(base)-4] = '\0';
1467
1468 /* Create the font (using the 'base' of the font file name!) */
1469 td->font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0,
1470 ANSI_CHARSET, OUT_DEFAULT_PRECIS,
1471 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1472 FIXED_PITCH | FF_DONTCARE, base);
1473
1474 /* Hack -- Unknown size */
1475 if (!wid || !hgt)
1476 {
1477 HDC hdcDesktop;
1478 HFONT hfOld;
1479 TEXTMETRIC tm;
1480
1481 /* all this trouble to get the cell size */
1482 hdcDesktop = GetDC(HWND_DESKTOP);
1483 hfOld = SelectObject(hdcDesktop, td->font_id);
1484 GetTextMetrics(hdcDesktop, &tm);
1485 SelectObject(hdcDesktop, hfOld);
1486 ReleaseDC(HWND_DESKTOP, hdcDesktop);
1487
1488 /* Font size info */
1489 wid = tm.tmAveCharWidth;
1490 hgt = tm.tmHeight;
1491 }
1492
1493 /* Save the size info */
1494 td->font_wid = wid;
1495 td->font_hgt = hgt;
1496
1497 /* Success */
1498 return (0);
1499 }
1500
1501
1502
1503 /**
1504 * Allow the user to change the font for this window.
1505 */
1506 static void term_change_font(term_data *td)
1507 {
1508 OPENFILENAME ofn;
1509
1510 char tmp[1024] = "";
1511
1512 /* Extract a default if possible */
1513 if (td->font_file) my_strcpy(tmp, td->font_file, sizeof(tmp));
1514
1515 /* Ask for a choice */
1516 memset(&ofn, 0, sizeof(ofn));
1517 ofn.lStructSize = sizeof(ofn);
1518 ofn.hwndOwner = data[0].w;
1519 ofn.lpstrFilter = "Angband Font Files (*.fon)\0*.fon\0";
1520 ofn.nFilterIndex = 1;
1521 ofn.lpstrFile = tmp;
1522 ofn.nMaxFile = 128;
1523 ofn.lpstrInitialDir = ANGBAND_DIR_FONTS;
1524 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
1525 ofn.lpstrDefExt = "fon";
1526
1527 /* Force choice if legal */
1528 if (GetOpenFileName(&ofn)) {
1529 /* Remember if tile size matched the font size. */
1530 bool tile_match_size = !use_graphics_nice
1531 && td->tile_wid == td->font_wid
1532 && td->tile_hgt == td->font_hgt;
1533
1534 /* Force the font */
1535 if (term_force_font(td, tmp)) {
1536 /* Access the standard font file */
1537 path_build(tmp, sizeof(tmp), ANGBAND_DIR_FONTS, DEFAULT_FONT);
1538
1539 /* Force the use of that font */
1540 (void)term_force_font(td, tmp);
1541
1542 /* Reset the tile info */
1543 td->tile_wid = td->font_wid;
1544 td->tile_hgt = td->font_hgt;
1545 }
1546
1547 /* HACK - Assume bizarre */
1548 td->bizarre = true;
1549
1550 /*
1551 * Reset the tile info (if not already set or if matching
1552 * the font size.
1553 */
1554 if (!td->tile_wid || !td->tile_hgt || tile_match_size) {
1555 td->tile_wid = td->font_wid;
1556 td->tile_hgt = td->font_hgt;
1557 }
1558
1559 /* Analyze the font */
1560 term_getsize(td);
1561
1562 /* Resize the window */
1563 term_window_resize(td);
1564 }
1565 }
1566
1567
1568 static void windows_map_aux(void);
1569
1570
1571 /**
1572 * Hack -- redraw a term_data
1573 */
1574 static void term_data_redraw(term_data *td)
1575 {
1576 if (td->map_active) {
1577 /* Redraw the map */
1578 windows_map_aux();
1579 } else {
1580 /* Activate the term */
1581 Term_activate(&td->t);
1582
1583 /* Redraw the contents */
1584 Term_redraw();
1585
1586 /* Restore the term */
1587 Term_activate(term_screen);
1588 }
1589 }
1590
1591
1592 /**
1593 * Hack -- redraw a term_data
1594 */
1595 static void term_data_redraw_section(term_data *td, int x1, int y1,
1596 int x2, int y2)
1597 {
1598 /* Activate the term */
1599 Term_activate(&td->t);
1600
1601 /* Redraw the area */
1602 Term_redraw_section(x1, y1, x2, y2);
1603
1604 /* Restore the term */
1605 Term_activate(term_screen);
1606 }
1607
1608
1609
1610 /**
1611 * ------------------------------------------------------------------------
1612 * Function hooks needed by "Term"
1613 * ------------------------------------------------------------------------ */
1614
1615
1616
1617 #if 0
1618
1619 /**
1620 * Initialize a new Term
1621 */
1622 static void Term_init_win(term *t)
1623 {
1624 /* XXX Unused */
1625 }
1626
1627
1628 /**
1629 * Nuke an old Term
1630 */
1631 static void Term_nuke_win(term *t)
1632 {
1633 /* XXX Unused */
1634 }
1635
1636 #endif /* 0 */
1637
1638
1639 static errr Term_pict_win(int x, int y, int n,
1640 const int *ap, const wchar_t *cp,
1641 const int *tap, const wchar_t *tcp);
1642 static errr Term_pict_win_alpha(int x, int y, int n,
1643 const int *ap, const wchar_t *cp,
1644 const int *tap, const wchar_t *tcp);
1645
1646 /**
1647 * React to global changes
1648 */
1649 static errr Term_xtra_win_react(void)
1650 {
1651 int i;
1652
1653 /* Get the main window */
1654 term_data *td = &data[0];
1655
1656 /* Simple or complex color */
1657 if (colors16) {
1658 /* Save the default colors */
1659 for (i = 0; i < MAX_COLORS; i++) {
1660 /* Simply accept the desired colors */
1661 win_pal[i] = angband_color_table[i][0];
1662 }
1663 } else {
1664 COLORREF code;
1665
1666 uint8_t rv, gv, bv;
1667
1668 bool change = false;
1669
1670 /* Save the default colors */
1671 for (i = 0; i < MAX_COLORS; i++) {
1672 /* Extract desired values */
1673 rv = angband_color_table[i][1];
1674 gv = angband_color_table[i][2];
1675 bv = angband_color_table[i][3];
1676
1677 if (gamma_correction > 0) {
1678 rv = gamma_table[rv];
1679 gv = gamma_table[gv];
1680 bv = gamma_table[bv];
1681 }
1682
1683 /* Extract a full color code */
1684 code = PALETTERGB(rv, gv, bv);
1685
1686 /* Activate changes */
1687 if (win_clr[i] != code) {
1688 /* Note the change */
1689 change = true;
1690
1691 /* Apply the desired color */
1692 win_clr[i] = code;
1693 }
1694 }
1695
1696 /* Activate the palette if needed */
1697 if (change) (void)new_palette();
1698 }
1699
1700 /* Handle "arg_graphics_nice" */
1701 if (use_graphics_nice != arg_graphics_nice) {
1702 /* Change setting */
1703 use_graphics_nice = arg_graphics_nice;
1704
1705 /* HACK - Assume bizarre */
1706 td->bizarre = true;
1707
1708 /* Analyze the font */
1709 term_getsize(td);
1710
1711 /* Resize the window */
1712 term_window_resize(td);
1713 }
1714
1715 /* Handle "arg_graphics" */
1716 if (use_graphics != arg_graphics) {
1717 /* Free the bitmap stuff */
1718 FreeDIB(&infGraph);
1719 FreeDIB(&infMask);
1720
1721 /* Initialize (if needed) */
1722 if (arg_graphics && !init_graphics()) {
1723 /* Warning */
1724 plog("Cannot initialize graphics!");
1725
1726 /* Cannot enable */
1727 arg_graphics = GRAPHICS_NONE;
1728 } else {
1729 if (alphablend || overdraw) {
1730 td->t.pict_hook = Term_pict_win_alpha;
1731 } else {
1732 td->t.pict_hook = Term_pict_win;
1733 }
1734 }
1735 td->t.dblh_hook = (overdraw) ? is_dh_tile : NULL;
1736
1737 /* make sure the current graphics mode is set */
1738 current_graphics_mode = get_graphics_mode(arg_graphics);
1739
1740 /* Change setting */
1741 use_graphics = arg_graphics;
1742
1743 if (use_graphics_nice) {
1744 /* HACK - Assume bizarre */
1745 td->bizarre = true;
1746
1747 /* Analyze the font */
1748 term_getsize(td);
1749
1750 /* Resize the window */
1751 term_window_resize(td);
1752 }
1753
1754 /* Reset visuals */
1755 if (character_dungeon) reset_visuals(true);
1756 }
1757
1758 /* Handle "change_tilesize" */
1759 if (change_tilesize) {
1760 /* Reset visuals */
1761 reset_visuals(true);
1762
1763 /* Reset the panel */
1764 verify_panel();
1765
1766 /* Reset the flag */
1767 change_tilesize = false;
1768 }
1769
1770
1771 /* Clean up windows */
1772 for (i = 0; i < MAX_TERM_DATA; i++) {
1773 term *old = Term;
1774
1775 td = &data[i];
1776
1777 /* Update resized windows */
1778 if ((td->cols != td->t.wid) || (td->rows != td->t.hgt)) {
1779 /* Activate */
1780 Term_activate(&td->t);
1781
1782 /* Hack -- Resize the term */
1783 Term_resize(td->cols, td->rows);
1784
1785 /* Redraw the contents */
1786 Term_redraw();
1787
1788 /* Restore */
1789 Term_activate(old);
1790 }
1791 }
1792
1793
1794 /* Success */
1795 return (0);
1796 }
1797
1798
1799 /**
1800 * Process at least one event
1801 */
1802 static errr Term_xtra_win_event(int v)
1803 {
1804 MSG msg;
1805
1806 /* Wait or check for an event */
1807 if (v)
1808 {
1809 int i = 0;
1810 while (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1811 /* Do animation updates (once every ten iterations), then
1812 * sleep 0.02s and try again */
1813 if (i == 0) idle_update();
1814 Sleep(20);
1815 i = (i + 1) % 10;
1816 }
1817 TranslateMessage(&msg);
1818 DispatchMessage(&msg);
1819 } else if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1820 TranslateMessage(&msg);
1821 DispatchMessage(&msg);
1822 }
1823
1824 /* Success */
1825 return 0;
1826 }
1827
1828
1829 /**
1830 * Process all pending events
1831 */
1832 static errr Term_xtra_win_flush(void)
1833 {
1834 MSG msg;
1835
1836 /* Process all pending events */
1837 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1838 TranslateMessage(&msg);
1839 DispatchMessage(&msg);
1840 }
1841
1842 /* Success */
1843 return (0);
1844 }
1845
1846
1847 /**
1848 * Hack -- clear the screen
1849 *
1850 * Make this more efficient XXX XXX XXX
1851 */
1852 static errr Term_xtra_win_clear(void)
1853 {
1854 term_data *td = (term_data*)(Term->data);
1855
1856 HDC hdc;
1857 RECT rc;
1858 HBRUSH brush;
1859
1860 /* Rectangle to erase */
1861 GetClientRect(td->w, &rc);
1862
1863 /* Erase it */
1864 hdc = GetDC(td->w);
1865 brush = CreateSolidBrush((colors16) ? RGB(0, 0, 0) : win_clr[0]);
1866 FillRect(hdc, &rc, brush);
1867 DeleteObject(brush);
1868 ReleaseDC(td->w, hdc);
1869
1870 /* Success */
1871 return 0;
1872 }
1873
1874
1875 /**
1876 * Hack -- make a noise
1877 */
1878 static errr Term_xtra_win_noise(void)
1879 {
1880 MessageBeep(MB_ICONASTERISK);
1881 return (0);
1882 }
1883
1884 /**
1885 * Delay for "x" milliseconds
1886 */
1887 static int Term_xtra_win_delay(int v)
1888 {
1889 /* Sleep */
1890 if (v > 0) Sleep(v);
1891
1892 /* Success */
1893 return (0);
1894 }
1895
1896
1897 /**
1898 * Do a "special thing"
1899 */
1900 static errr Term_xtra_win(int n, int v)
1901 {
1902 /* Handle a subset of the legal requests */
1903 switch (n)
1904 {
1905 /* Make a bell sound */
1906 case TERM_XTRA_NOISE:
1907 {
1908 return (Term_xtra_win_noise());
1909 }
1910
1911 /* Process random events */
1912 case TERM_XTRA_BORED:
1913 {
1914 return (Term_xtra_win_event(0));
1915 }
1916
1917 /* Process an event */
1918 case TERM_XTRA_EVENT:
1919 {
1920 return (Term_xtra_win_event(v));
1921 }
1922
1923 /* Flush all events */
1924 case TERM_XTRA_FLUSH:
1925 {
1926 return (Term_xtra_win_flush());
1927 }
1928
1929 /* Clear the screen */
1930 case TERM_XTRA_CLEAR:
1931 {
1932 return (Term_xtra_win_clear());
1933 }
1934
1935 /* React to global changes */
1936 case TERM_XTRA_REACT:
1937 {
1938 return (Term_xtra_win_react());
1939 }
1940
1941 /* Delay for some milliseconds */
1942 case TERM_XTRA_DELAY:
1943 {
1944 return (Term_xtra_win_delay(v));
1945 }
1946 }
1947
1948 /* Oops */
1949 return 1;
1950 }
1951
1952
1953
1954 /**
1955 * Low level graphics (Assumes valid input).
1956 *
1957 * Draw a "cursor" at (x,y), using a "yellow box".
1958 */
1959 static errr Term_curs_win(int x, int y)
1960 {
1961 term_data *td = (term_data*)(Term->data);
1962
1963 RECT rc;
1964 HDC hdc;
1965
1966 int tile_wid, tile_hgt;
1967
1968 if (td->map_active) {
1969 tile_wid = td->map_tile_wid;
1970 tile_hgt = td->map_tile_hgt;
1971 } else {
1972 tile_wid = td->tile_wid;
1973 tile_hgt = td->tile_hgt;
1974 }
1975
1976 /* Frame the grid */
1977 rc.left = x * tile_wid + td->size_ow1;
1978 rc.right = rc.left + tile_wid;
1979 rc.top = y * tile_hgt + td->size_oh1;
1980 rc.bottom = rc.top + tile_hgt;
1981
1982 /* Cursor is done as a yellow "box" */
1983 hdc = GetDC(td->w);
1984 FrameRect(hdc, &rc, hbrYellow);
1985 ReleaseDC(td->w, hdc);
1986
1987 /* Success */
1988 return 0;
1989 }
1990
1991
1992 /**
1993 * Low level graphics (Assumes valid input).
1994 *
1995 * Draw a "cursor" at (x,y), using a "yellow box".
1996 */
1997 static errr Term_bigcurs_win(int x, int y)
1998 {
1999 term_data *td = (term_data*)(Term->data);
2000
2001 RECT rc;
2002 HDC hdc;
2003
2004 int tile_wid, tile_hgt;
2005
2006 if (td->map_active) {
2007 /* Normal cursor in map window */
2008 Term_curs_win(x, y);
2009 return 0;
2010 } else {
2011 tile_wid = td->tile_wid;
2012 tile_hgt = td->tile_hgt;
2013 }
2014
2015 /* Frame the grid */
2016 rc.left = x * tile_wid + td->size_ow1;
2017 rc.right = rc.left + tile_width * tile_wid;
2018 rc.top = y * tile_hgt + td->size_oh1;
2019 rc.bottom = rc.top + tile_height * tile_hgt;
2020
2021 /* Cursor is done as a yellow "box" */
2022 hdc = GetDC(td->w);
2023 FrameRect(hdc, &rc, hbrYellow);
2024 ReleaseDC(td->w, hdc);
2025
2026 /* Success */
2027 return 0;
2028 }
2029
2030
2031 /**
2032 * Help Term_wipe_win(), Term_pict_win(), and Term_pict_win_alpha():
2033 * fill a nc x nr block of characters with the color c where the upper
2034 * left corner of the block is at (x,y).
2035 */
2036 static errr Term_wipe_win_helper(int x, int y, int nc, int nr, COLORREF c)
2037 {
2038 term_data *td = (term_data*)(Term->data);
2039
2040 HDC hdc;
2041 RECT rc;
2042
2043 /* Rectangle to erase in client coords */
2044 rc.left = x * td->tile_wid + td->size_ow1;
2045 rc.right = rc.left + nc * td->tile_wid;
2046 rc.top = y * td->tile_hgt + td->size_oh1;
2047 rc.bottom = rc.top + nr * td->tile_hgt;
2048
2049 hdc = GetDC(td->w);
2050 SetBkColor(hdc, c);
2051 SelectObject(hdc, td->font_id);
2052 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2053 ReleaseDC(td->w, hdc);
2054
2055 /* Success */
2056 return 0;
2057 }
2058
2059
2060 /**
2061 * Low level graphics (Assumes valid input).
2062 *
2063 * Erase a "block" of "n" characters starting at (x,y).
2064 */
2065 static errr Term_wipe_win(int x, int y, int n)
2066 {
2067 return Term_wipe_win_helper(x, y, n, 1,
2068 (colors16) ? RGB(0, 0, 0) : win_clr[0]);
2069 }
2070
2071
2072 /**
2073 * Low level graphics. Assumes valid input.
2074 *
2075 * Draw several ("n") chars, with an attr, at a given location.
2076 *
2077 * All "graphic" data is handled by "Term_pict_win()", below.
2078 *
2079 * One would think there is a more efficient method for telling a window
2080 * what color it should be using to draw with, but perhaps simply changing
2081 * it every time is not too inefficient. XXX XXX XXX
2082 */
2083 static errr Term_text_win(int x, int y, int n, int a, const wchar_t *s)
2084 {
2085 term_data *td = (term_data*)(Term->data);
2086 RECT rc;
2087 HDC hdc;
2088
2089 /* Total rectangle */
2090 rc.left = x * td->tile_wid + td->size_ow1;
2091 rc.right = rc.left + n * td->tile_wid;
2092 rc.top = y * td->tile_hgt + td->size_oh1;
2093 rc.bottom = rc.top + td->tile_hgt;
2094
2095 /* Acquire DC */
2096 hdc = GetDC(td->w);
2097
2098 /* Foreground color */
2099 if (colors16) {
2100 SetTextColor(hdc, PALETTEINDEX(win_pal[a % MAX_COLORS]));
2101 /* Background color */
2102 SetBkColor(hdc, RGB(0, 0, 0));
2103 } else {
2104 if (paletted)
2105 SetTextColor(hdc, win_clr[(a % MAX_COLORS) & 0x1F]);
2106 else
2107 SetTextColor(hdc, win_clr[a % MAX_COLORS]);
2108
2109 /* Determine the background colour - from Sil */
2110 switch (a / MULT_BG)
2111 {
2112 case BG_SAME:
2113 /* Background same as foreground*/
2114 SetBkColor(hdc, win_clr[a % MAX_COLORS]);
2115 break;
2116 case BG_DARK:
2117 /* Highlight Background */
2118 SetBkColor(hdc, win_clr[COLOUR_SHADE]);
2119 break;
2120 case BG_BLACK:
2121 default:
2122 /* Default Background */
2123 SetBkColor(hdc, win_clr[0]);
2124 break;
2125 }
2126 }
2127
2128 /* Use the font */
2129 SelectObject(hdc, td->font_id);
2130
2131 /* Bizarre or normal size */
2132 if (td->bizarre ||
2133 (td->tile_hgt != td->font_hgt) ||
2134 (td->tile_wid != td->font_wid)) {
2135 int i;
2136
2137 /* Erase complete rectangle */
2138 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2139
2140 /* New rectangle */
2141 rc.left += ((td->tile_wid - td->font_wid) / 2);
2142 rc.right = rc.left + td->font_wid;
2143 rc.top += ((td->tile_hgt - td->font_hgt) / 2);
2144 rc.bottom = rc.top + td->font_hgt;
2145
2146 /* Dump each character */
2147 for (i = 0; i < n; i++) {
2148 /* Dump the text */
2149 ExtTextOutW(hdc, rc.left, rc.top, 0, &rc,
2150 s+i, 1, NULL);
2151
2152 /* Advance */
2153 rc.left += td->tile_wid;
2154 rc.right += td->tile_wid;
2155 }
2156 } else {
2157 /* Dump the text */
2158 ExtTextOutW(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc,
2159 s, n, NULL);
2160 }
2161
2162 /* Release DC */
2163 ReleaseDC(td->w, hdc);
2164
2165 /* Success */
2166 return 0;
2167 }
2168
2169
2170 /**
2171 * Low level graphics. Assumes valid input.
2172 *
2173 * Draw an array of "special" attr/char pairs at the given location.
2174 *
2175 * We use the "Term_pict_win()" function for "graphic" data, which are
2176 * encoded by setting the "high-bits" of both the "attr" and the "char"
2177 * data. We use the "attr" to represent the "row" of the main bitmap,
2178 * and the "char" to represent the "col" of the main bitmap. The use
2179 * of this function is induced by the "higher_pict" flag.
2180 *
2181 * If "graphics" is not available, we simply "wipe" the given grids.
2182 */
2183 static errr Term_pict_win(int x, int y, int n,
2184 const int *ap, const wchar_t *cp,
2185 const int *tap, const wchar_t *tcp)
2186 {
2187 term_data *td = (term_data*)(Term->data);
2188
2189 int i;
2190 int mw, mh;
2191 int x1, y1, w1, h1;
2192 int x2, y2, w2, h2, tw2, th2;
2193 int x3, y3;
2194
2195 HDC hdcMask;
2196 HDC hdc;
2197 HDC hdcSrc;
2198 HBITMAP hbmSrcOld;
2199
2200 /* Size of bitmap cell */
2201 w1 = infGraph.CellWidth;
2202 h1 = infGraph.CellHeight;
2203
2204 /* Size of window cell */
2205 if (td->map_active) {
2206 w2 = td->map_tile_wid;
2207 h2 = td->map_tile_hgt;
2208 mw = 1;
2209 mh = 1;
2210 } else {
2211 w2 = td->tile_wid;
2212 h2 = td->tile_hgt;
2213
2214 /* Large tile mode */
2215 mw = tile_width;
2216 mh = tile_height;
2217 }
2218 tw2 = mw * w2;
2219 th2 = mh * h2;
2220
2221 /* Erase the grids */
2222 Term_wipe_win_helper(x, y, n * mw, mh, RGB(0, 0, 0));
2223
2224 /* Location of window cell */
2225 x2 = x * w2 + td->size_ow1;
2226 y2 = y * h2 + td->size_oh1;
2227
2228 /* Info */
2229 hdc = GetDC(td->w);
2230
2231 /* More info */
2232 hdcSrc = CreateCompatibleDC(hdc);
2233 hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
2234
2235 if (infMask.hBitmap) {
2236 hdcMask = CreateCompatibleDC(hdc);
2237 SelectObject(hdcMask, infMask.hBitmap);
2238 } else {
2239 hdcMask = NULL;
2240 }
2241
2242 /* Draw attr/char pairs */
2243 for (i = n-1; i >= 0; i--, x2 -= w2) {
2244 int a = ap[i];
2245 wchar_t c = cp[i];
2246
2247 /* Extract picture */
2248 int row = (a & 0x7F);
2249 int col = (c & 0x7F);
2250
2251 /* Location of bitmap cell */
2252 x1 = col * w1;
2253 y1 = row * h1;
2254
2255 if (hdcMask) {
2256 x3 = (tcp[i] & 0x7F) * w1;
2257 y3 = (tap[i] & 0x7F) * h1;
2258
2259 /* Perfect size */
2260 if ((w1 == tw2) && (h1 == th2)) {
2261 /* Copy the terrain picture from the bitmap to the window */
2262 BitBlt(hdc, x2, y2, tw2, th2, hdcSrc, x3, y3, SRCCOPY);
2263
2264 /* Only draw if terrain and overlay are different */
2265 if ((x1 != x3) || (y1 != y3)) {
2266 /* Mask out the tile */
2267 BitBlt(hdc, x2, y2, tw2, th2, hdcMask, x1, y1, SRCAND);
2268
2269 /* Draw the tile */
2270 BitBlt(hdc, x2, y2, tw2, th2, hdcSrc, x1, y1, SRCPAINT);
2271 }
2272
2273 /* Need to stretch */
2274 } else {
2275 /* Set the correct mode for stretching the tiles */
2276 SetStretchBltMode(hdc, COLORONCOLOR);
2277
2278 /* Copy the terrain picture from the bitmap to the window */
2279 StretchBlt(hdc, x2, y2, tw2, th2, hdcSrc, x3, y3, w1, h1,
2280 SRCCOPY);
2281
2282 /* Only draw if terrain and overlay are different */
2283 if ((x1 != x3) || (y1 != y3)) {
2284 /* Mask out the tile */
2285 StretchBlt(hdc, x2, y2, tw2, th2, hdcMask, x1, y1, w1, h1,
2286 SRCAND);
2287
2288 /* Draw the tile */
2289 StretchBlt(hdc, x2, y2, tw2, th2, hdcSrc, x1, y1, w1, h1,
2290 SRCPAINT);
2291 }
2292 }
2293 } else {
2294 /* Perfect size */
2295 if ((w1 == tw2) && (h1 == th2)) {
2296 /* Copy the picture from the bitmap to the window */
2297 BitBlt(hdc, x2, y2, tw2, th2, hdcSrc, x1, y1, SRCCOPY);
2298
2299 /* Need to stretch */
2300 } else {
2301 /* Set the correct mode for stretching the tiles */
2302 SetStretchBltMode(hdc, COLORONCOLOR);
2303
2304 /* Copy the picture from the bitmap to the window */
2305 StretchBlt(hdc, x2, y2, tw2, th2, hdcSrc, x1, y1, w1, h1,
2306 SRCCOPY);
2307 }
2308 }
2309 }
2310
2311 /* Release */
2312 SelectObject(hdcSrc, hbmSrcOld);
2313 DeleteDC(hdcSrc);
2314
2315 if (hdcMask) {
2316 /* Release */
2317 SelectObject(hdcMask, hbmSrcOld);
2318 DeleteDC(hdcMask);
2319 }
2320
2321 /* Release */
2322 ReleaseDC(td->w, hdc);
2323
2324 /* Success */
2325 return 0;
2326 }
2327
2328
2329 /**
2330 * Windows cannot naturally handle UTF-8 using the standard locale and
2331 * C library routines, such as mbstowcs().
2332 *
2333 * We assume external files are in UTF-8, and explicitly convert.
2334 *
2335 * MultiByteToWideChar returns number of wchars, including terminating L'\0'
2336 * mbstowcs requires the count without the terminating L'\0'
2337 * dest == NULL corresponds to querying for the size needed, achieved in the
2338 * Windows fn by setting the dstlen (last) param to 0.
2339 * If n is too small for all the chars in src, the Windows fn fails, but we
2340 * require success and a partial conversion. So allocate space for it to
2341 * succeed, and do the partial copy into dest.
2342 */
2343 size_t Term_mbstowcs_win(wchar_t *dest, const char *src, int n)
2344 {
2345 int res;
2346 int required;
2347 wchar_t *tmp;
2348
2349 if (dest) {
2350 res = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1,
2351 dest, n);
2352 if (res)
2353 return (size_t)(res - 1);
2354 else {
2355 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
2356 required = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
2357 src, -1, NULL, 0);
2358 tmp = malloc(required * sizeof(wchar_t));
2359 MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, tmp,
2360 required);
2361 memcpy(dest, tmp, n * sizeof(wchar_t));
2362 free(tmp);
2363 return n;
2364 } else {
2365 return (size_t)-1;
2366 }
2367 }
2368 } else {
2369 return (size_t)(MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
2370 src, -1, NULL, 0) - 1);
2371 }
2372 }
2373
2374 int Term_wcsz_win(void)
2375 {
2376 /*
2377 * Any Unicode code point is representable by at most 4 bytes in UTF-8.
2378 */
2379 return 4;
2380 }
2381
2382
2383 /**
2384 * Convert back to UTF-8 from a wide character.
2385 *
2386 * This is a necessary counterpart to Term_mbstowcs_win(): since the forward
2387 * conversion from UTF-8 to wide characters was overridden, need to override
2388 * the reverse conversion so that file output from screen captures and text
2389 * blocks properly translates any multibyte characters.
2390 */
2391 int Term_wctomb_win(char *s, wchar_t wchar)
2392 {
2393 /*
2394 * If only want compatibility with Vista and later, could use
2395 * WC_ERR_INVALID_CHARS rather than zero for the second argument;
2396 * that would give an error if converting something not representable
2397 * in UTF-8.
2398 */
2399 int n = WideCharToMultiByte(CP_UTF8, 0, &wchar, 1, s, Term_wcsz_win(),
2400 NULL, NULL);
2401 return (n > 0) ? n : -1;
2402 }
2403
2404
2405 /**
2406 * Return whether a wide character is printable.
2407 *
2408 * This is a necessary counterpart to Term_mbstowcs_win() so that screening
2409 * of wide characters in the core's text_out_to_screen() is consistent with what
2410 * Term_mbstowcs_win() does.
2411 */
2412 int Term_iswprint_win(wint_t wc)
2413 {
2414 /*
2415 * It's a UTF-16 value, but can cast and test as UTF-32 (if it's the
2416 * leading part of a surrogate pair it'll be treated as unprintable
2417 * which is desirable: on Windows, ui-term as it is can't handle
2418 * characters that have to be encoded as surrogate pairs in UTF-16).
2419 */
2420 return utf32_isprint((uint32_t) wc);
2421 }
2422
2423
2424 #ifndef AC_SRC_ALPHA
2425 #define AC_SRC_ALPHA 0x01
2426 #endif
2427 static errr Term_pict_win_alpha(int x, int y, int n,
2428 const int *ap, const wchar_t *cp,
2429 const int *tap, const wchar_t *tcp)
2430 {
2431 term_data *td = (term_data*)(Term->data);
2432 int dhrclip = (overdraw) ?
2433 Term_get_first_tile_row(Term) + tile_height - 1 : 0;
2434 int i;
2435 int mw, mh;
2436 int x1, y1, w1, h1;
2437 int x2, y2, w2, h2, tw2, th2;
2438 int x3, y3;
2439
2440 HDC hdc;
2441 HDC hdcSrc;
2442 HBITMAP hbmSrcOld;
2443
2444 /* Size of bitmap cell */
2445 w1 = infGraph.CellWidth;
2446 h1 = infGraph.CellHeight;
2447
2448 /* Size of window cell */
2449 if (td->map_active) {
2450 w2 = td->map_tile_wid;
2451 h2 = td->map_tile_hgt;
2452 mw = 1;
2453 mh = 1;
2454 } else {
2455 w2 = td->tile_wid;
2456 h2 = td->tile_hgt;
2457
2458 /* Large tile mode */
2459 mw = tile_width;
2460 mh = tile_height;
2461 }
2462 tw2 = mw * w2;
2463 th2 = mh * h2;
2464
2465 /* Erase the grids */
2466 Term_wipe_win_helper(x, y, n * mw, mh, RGB(0, 0, 0));
2467
2468 /* Location of window cell */
2469 x2 = x * w2 + td->size_ow1;
2470 y2 = y * h2 + td->size_oh1;
2471
2472 /* Info */
2473 hdc = GetDC(td->w);
2474
2475 /* More info */
2476 hdcSrc = CreateCompatibleDC(hdc);
2477 hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
2478
2479 /* Draw attr/char pairs */
2480 for (i = n-1; i >= 0; i--, x2 -= w2) {
2481 int a = ap[i];
2482 wchar_t c = cp[i];
2483
2484 /* Extract picture */
2485 int row = (a & 0x7F);
2486 int col = (c & 0x7F);
2487 int trow = (tap[i] & 0x7F);
2488
2489 /* Location of bitmap cell */
2490 x1 = col * w1;
2491 y1 = row * h1;
2492
2493 x3 = (tcp[i] & 0x7F) * w1;
2494 y3 = trow * h1;
2495
2496 /* Set the correct mode for stretching the tiles */
2497 SetStretchBltMode(hdc, COLORONCOLOR);
2498
2499 /* Perfect size */
2500 if ((w1 == tw2) && (h1 == th2)) {
2501 /* Copy the terrain picture from the bitmap to the window */
2502 BitBlt(hdc, x2, y2, tw2, th2, hdcSrc, x3, y3, SRCCOPY);
2503 } else {
2504 /* Copy the terrain picture from the bitmap to the window */
2505 StretchBlt(hdc, x2, y2, tw2, th2, hdcSrc, x3, y3, w1, h1, SRCCOPY);
2506 }
2507
2508 if (overdraw && trow >= overdraw && y > dhrclip &&
2509 trow <= overdrawmax) {
2510 AlphaBlend(hdc, x2, y2-th2, tw2, th2, hdcSrc, x3, y3-h1, w1, h1,
2511 blendfn);
2512 }
2513
2514 /* Only draw if terrain and overlay are different */
2515 if ((x1 != x3) || (y1 != y3))
2516 {
2517 /* Copy the picture from the bitmap to the window */
2518 if (overdraw && row >= overdraw && y > dhrclip &&
2519 row <= overdrawmax) {
2520 AlphaBlend(hdc, x2, y2-th2, tw2, th2*2, hdcSrc, x1, y1-h1, w1,
2521 h1*2, blendfn);
2522 } else {
2523 AlphaBlend(hdc, x2, y2, tw2, th2, hdcSrc, x1, y1, w1, h1,
2524 blendfn);
2525 }
2526 }
2527 }
2528
2529 /* Release */
2530 SelectObject(hdcSrc, hbmSrcOld);
2531 DeleteDC(hdcSrc);
2532
2533 /* Release */
2534 ReleaseDC(td->w, hdc);
2535
2536 /* Success */
2537 return 0;
2538 }
2539
2540
2541 static void windows_map_aux(void)
2542 {
2543 term_data *td = &data[0];
2544 int a;
2545 wchar_t c;
2546 int x, min_x, max_x;
2547 int y, min_y, max_y;
2548 int ta;
2549 wchar_t tc;
2550
2551 td->map_tile_wid = (td->tile_wid * td->cols) / cave->width;
2552 td->map_tile_hgt = (td->tile_hgt * td->rows) / cave->height;
2553
2554 min_x = 0;
2555 min_y = 0;
2556 max_x = cave->width;
2557 max_y = cave->height;
2558
2559 /* Draw the map */
2560 for (x = min_x; x < max_x; x++) {
2561 for (y = min_y; y < max_y; y++) {
2562 struct grid_data g;
2563
2564 map_info(loc(x, y), &g);
2565 grid_data_as_text(&g, &a, &c, &ta, &tc);
2566
2567 /* Ignore non-graphics */
2568 if (a & 0x80)
2569 Term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
2570 }
2571 }
2572
2573 /* Highlight the player */
2574 Term_curs_win(player->grid.x - min_x, player->grid.y - min_y);
2575 }
2576
2577
2578 /**
2579 * MEGA_HACK - Display a graphical map of the dungeon.
2580 */
2581 static void windows_map(void)
2582 {
2583 term_data *td = &data[0];
2584 ui_event ch;
2585
2586 /* Only in graphics mode since the fonts can't be scaled */
2587 if (!use_graphics) return;
2588
2589 /* Prevent various menu-actions from working */
2590 initialized = false;
2591
2592 /* Clear screen */
2593 Term_xtra_win_clear();
2594
2595 td->map_active = true;
2596
2597 /* Draw the map */
2598 windows_map_aux();
2599
2600 /* Wait for a keypress, flush key buffer */
2601 Term_inkey(&ch, true, true);
2602 Term_flush();
2603
2604 /* Switch off the map display */
2605 td->map_active = false;
2606
2607 /* Restore screen */
2608 Term_xtra_win_clear();
2609 Term_redraw();
2610
2611 /* We are ready again */
2612 initialized = true;
2613 }
2614
2615
2616 /**
2617 * ------------------------------------------------------------------------
2618 * Other routines
2619 * ------------------------------------------------------------------------ */
2620
2621
2622
2623 /**
2624 * Create and initialize a "term_data" given a title
2625 */
2626 static void term_data_link(term_data *td)
2627 {
2628 term *t = &td->t;
2629
2630 /* Initialize the term */
2631 term_init(t, td->cols, td->rows, td->keys);
2632
2633 /* Use a "software" cursor */
2634 t->soft_cursor = true;
2635
2636 /* Differentiate between BS/^h, Tab/^i, etc. */
2637 t->complex_input = true;
2638
2639 /* Use "Term_pict" for "graphic" data */
2640 t->higher_pict = true;
2641
2642 #if 0
2643 /* Prepare the init/nuke hooks */
2644 t->init_hook = Term_init_win;
2645 t->nuke_hook = Term_nuke_win;
2646 #endif /* 0 */
2647
2648 /* Prepare the template hooks */
2649 t->xtra_hook = Term_xtra_win;
2650 t->curs_hook = Term_curs_win;
2651 t->bigcurs_hook = Term_bigcurs_win;
2652 t->wipe_hook = Term_wipe_win;
2653 t->text_hook = Term_text_win;
2654 t->pict_hook = Term_pict_win;
2655 t->dblh_hook = NULL;
2656
2657 /* Remember where we came from */
2658 t->data = td;
2659 }
2660
2661
2662 /**
2663 * Create the windows
2664 *
2665 * First, instantiate the "default" values, then read the "ini_file"
2666 * to over-ride selected values, then create the windows, and fonts.
2667 *
2668 * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
2669 * must make active to realize the palette. XXX XXX XXX
2670 */
2671 static void init_windows(void)
2672 {
2673 int i;
2674
2675 term_data *td;
2676
2677 char buf[1024];
2678
2679 WINDOWPLACEMENT lpwndpl;
2680
2681 MENUITEMINFO mii;
2682 HMENU hm;
2683 graphics_mode *mode;
2684
2685 /* Main window */
2686 td = &data[0];
2687 memset(td, 0, sizeof(term_data));
2688 td->s = angband_term_name[0];
2689 td->keys = 1024;
2690 td->rows = 24;
2691 td->cols = 80;
2692 td->visible = true;
2693 td->size_ow1 = 2;
2694 td->size_ow2 = 2;
2695 td->size_oh1 = 2;
2696 td->size_oh2 = 2;
2697 td->pos_x = 30;
2698 td->pos_y = 20;
2699
2700 /* Sub windows */
2701 for (i = 1; i < MAX_TERM_DATA; i++) {
2702 td = &data[i];
2703 memset(td, 0, sizeof(term_data));
2704 td->s = angband_term_name[i];
2705 td->keys = 16;
2706 td->rows = 24;
2707 td->cols = 80;
2708 td->visible = false;
2709 td->size_ow1 = 1;
2710 td->size_ow2 = 1;
2711 td->size_oh1 = 1;
2712 td->size_oh2 = 1;
2713 td->pos_x = (7 - i) * 30;
2714 td->pos_y = (7 - i) * 20;
2715 }
2716
2717
2718 /* Load prefs */
2719 load_prefs();
2720
2721
2722 /* Main window (need these before term_getsize gets called) */
2723 td = &data[0];
2724 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU |
2725 WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION |
2726 WS_VISIBLE);
2727 if (td->maximized) td->dwStyle |= WS_MAXIMIZE;
2728 td->dwExStyle = 0;
2729 td->visible = true;
2730
2731 /* Sub windows (need these before term_getsize gets called) */
2732 for (i = 1; i < MAX_TERM_DATA; i++) {
2733 td = &data[i];
2734 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU | WS_CAPTION);
2735 td->dwExStyle = (WS_EX_TOOLWINDOW);
2736 }
2737
2738
2739 /* All windows */
2740 for (i = 0; i < MAX_TERM_DATA; i++) {
2741 td = &data[i];
2742
2743 /* Access the standard font file */
2744 path_build(buf, sizeof(buf), ANGBAND_DIR_FONTS, td->font_want);
2745
2746 /* Activate the chosen font */
2747 if (term_force_font(td, buf)) {
2748 /* Access the standard font file */
2749 path_build(buf, sizeof(buf), ANGBAND_DIR_FONTS, DEFAULT_FONT);
2750
2751 /* Force the use of that font */
2752 (void)term_force_font(td, buf);
2753
2754 /* Oops */
2755 td->tile_wid = 8;
2756 td->tile_hgt = 13;
2757
2758 /* HACK - Assume bizarre */
2759 td->bizarre = true;
2760 }
2761
2762 /* Analyze the font */
2763 term_getsize(td);
2764
2765 /* Resize the window */
2766 term_window_resize(td);
2767 }
2768
2769
2770 /* Sub windows (reverse order) */
2771 for (i = MAX_TERM_DATA - 1; i >= 1; --i) {
2772 td = &data[i];
2773
2774 my_td = td;
2775 td->w = CreateWindowEx(td->dwExStyle, AngList,
2776 td->s, td->dwStyle,
2777 td->pos_x, td->pos_y,
2778 td->size_wid, td->size_hgt,
2779 HWND_DESKTOP, NULL, hInstance, NULL);
2780 my_td = NULL;
2781 if (!td->w) quit("Failed to create sub-window");
2782
2783 term_data_link(td);
2784 angband_term[i] = &td->t;
2785
2786 if (td->visible) {
2787 td->size_hack = true;
2788 ShowWindow(td->w, SW_SHOW);
2789 td->size_hack = false;
2790
2791 /* Activate the window */
2792 SetActiveWindow(td->w);
2793
2794 /* Bring window to top, place it correctly */
2795 lpwndpl.length = sizeof(WINDOWPLACEMENT);
2796 lpwndpl.showCmd = SW_SHOWNORMAL;
2797 lpwndpl.rcNormalPosition = (RECT) { td->pos_x, td->pos_y,
2798 td->pos_x + td->size_wid,
2799 td->pos_y + td->size_hgt };
2800 SetWindowPlacement(td->w, &lpwndpl);
2801 //SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
2802 }
2803 }
2804
2805
2806 /* Main window */
2807 td = &data[0];
2808
2809 /* Main window */
2810 my_td = td;
2811 td->w = CreateWindowEx(td->dwExStyle, AppName,
2812 td->s, td->dwStyle,
2813 td->pos_x, td->pos_y,
2814 td->size_wid, td->size_hgt,
2815 HWND_DESKTOP, NULL, hInstance, NULL);
2816 my_td = NULL;
2817 if (!td->w) quit_fmt("Failed to create %s window", VERSION_NAME);
2818
2819 term_data_link(td);
2820 term_screen = &td->t;
2821 text_mbcs_hook = Term_mbstowcs_win;
2822 text_wctomb_hook = Term_wctomb_win;
2823 text_wcsz_hook = Term_wcsz_win;
2824 text_iswprint_hook = Term_iswprint_win;
2825
2826 /* Activate the main window */
2827 SetActiveWindow(td->w);
2828
2829 /* Bring window to top, place it correctly */
2830 lpwndpl.length = sizeof(WINDOWPLACEMENT);
2831 lpwndpl.showCmd = SW_SHOWNORMAL;
2832 lpwndpl.rcNormalPosition = (RECT) { td->pos_x, td->pos_y,
2833 td->pos_x + td->size_wid,
2834 td->pos_y + td->size_hgt };
2835 SetWindowPlacement(td->w, &lpwndpl);
2836 /* Bring main window back to top */
2837 //SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
2838
2839 if (gamma_correction > 0)
2840 build_gamma_table(gamma_correction);
2841
2842 /* New palette XXX XXX XXX */
2843 (void)new_palette();
2844
2845
2846 /* Create a "brush" for drawing the "cursor" */
2847 hbrYellow = CreateSolidBrush(win_clr[COLOUR_YELLOW]);
2848
2849 /* Populate the graphic options sub menu with the graphics modes */
2850 hm = GetMenu(data[0].w);
2851 mii.cbSize = sizeof(MENUITEMINFO);
2852 mii.fMask = MIIM_ID | MIIM_TYPE;
2853 mii.fType = MFT_STRING;
2854 mode = graphics_modes;
2855 while (mode) {
2856 if (mode->grafID != GRAPHICS_NONE) {
2857 mii.wID = mode->grafID + IDM_OPTIONS_GRAPHICS_NONE;
2858 mii.dwTypeData = mode->menuname;
2859 mii.cch = strlen(mode->menuname);
2860 InsertMenuItem(hm,IDM_OPTIONS_GRAPHICS_NICE, false, &mii);
2861 }
2862 mode = mode->pNext;
2863 }
2864 /* mii.cbSize = sizeof(MENUITEMINFO); */
2865 mii.fMask = MIIM_TYPE;
2866 mii.fType = MFT_SEPARATOR;
2867 mii.wID = 399;
2868 mii.dwTypeData = 0;
2869 mii.cch = 0;
2870 InsertMenuItem(hm,IDM_OPTIONS_GRAPHICS_NICE, false, &mii);
2871
2872 /* setup the alpha blending function */
2873 blendfn.BlendOp = AC_SRC_OVER;
2874 blendfn.BlendFlags = 0;
2875 blendfn.AlphaFormat = AC_SRC_ALPHA;
2876 blendfn.SourceConstantAlpha = 255;
2877
2878
2879 /* Process pending messages */
2880 (void)Term_xtra_win_flush();
2881 }
2882
2883
2884 #ifdef USE_SAVER
2885
2886 /**
2887 * Stop the screensaver
2888 */
2889 static void stop_screensaver(void)
2890 {
2891 if (screensaver)
2892 SendMessage(data[0].w, WM_CLOSE, 0, 0);
2893 else
2894 SendMessage(data[0].w, WM_COMMAND, IDM_OPTIONS_SAVER, 0);
2895 }
2896
2897 #endif /* USE_SAVER */
2898
2899
2900 /**
2901 * Prepare the menus
2902 */
2903 static void setup_menus(void)
2904 {
2905 size_t i;
2906 graphics_mode *mode;
2907
2908 HMENU hm = GetMenu(data[0].w);
2909
2910 #ifdef USE_SAVER
2911 main_menu = hm;
2912 #endif /* USE_SAVER */
2913
2914 /* Menu "File", Disable all */
2915 EnableMenuItem(hm, IDM_FILE_NEW,
2916 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2917 EnableMenuItem(hm, IDM_FILE_OPEN,
2918 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2919 EnableMenuItem(hm, IDM_FILE_SAVE,
2920 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2921 EnableMenuItem(hm, IDM_FILE_EXIT,
2922 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2923
2924 EnableMenuItem(hm, IDM_WINDOW_OPT,
2925 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2926
2927 /* No character available */
2928 if (!character_generated) {
2929 /* Menu "File", Item "New" */
2930 EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
2931
2932 /* Menu "File", Item "Open" */
2933 EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
2934 }
2935
2936 /* A character available */
2937 if (game_in_progress && character_generated && inkey_flag) {
2938 /* Menu "File", Item "Save" */
2939 EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_ENABLED);
2940 /* Allow accessing the window options */
2941 EnableMenuItem(hm, IDM_WINDOW_OPT, MF_BYCOMMAND | MF_ENABLED);
2942 }
2943
2944 if (!game_in_progress || !character_generated || inkey_flag) {
2945 /* Menu "File", Item "Exit" */
2946 EnableMenuItem(hm, IDM_FILE_EXIT, MF_BYCOMMAND | MF_ENABLED);
2947 }
2948
2949
2950 /* Menu "Window::Visibility" */
2951 for (i = 0; i < MAX_TERM_DATA; i++) {
2952 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
2953 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2954
2955 CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i,
2956 (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
2957
2958 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
2959 MF_BYCOMMAND | MF_ENABLED);
2960 }
2961
2962 /* Menu "Window::Font" */
2963 for (i = 0; i < MAX_TERM_DATA; i++) {
2964 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
2965 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2966
2967 if (data[i].visible) {
2968 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
2969 MF_BYCOMMAND | MF_ENABLED);
2970 }
2971 }
2972
2973 /* Menu "Window::Bizarre Display" */
2974 for (i = 0; i < MAX_TERM_DATA; i++) {
2975 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
2976 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2977
2978 CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
2979 (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
2980
2981 if (data[i].visible) {
2982 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
2983 MF_BYCOMMAND | MF_ENABLED);
2984 }
2985 }
2986
2987 /* Menu "Window::Increase Tile Width" */
2988 for (i = 0; i < MAX_TERM_DATA; i++) {
2989 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
2990 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2991
2992 if (data[i].visible) {
2993 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
2994 MF_BYCOMMAND | MF_ENABLED);
2995 }
2996 }
2997
2998 /* Menu "Window::Decrease Tile Width" */
2999 for (i = 0; i < MAX_TERM_DATA; i++) {
3000 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3001 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3002
3003 if (data[i].visible) {
3004 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3005 MF_BYCOMMAND | MF_ENABLED);
3006 }
3007 }
3008
3009 /* Menu "Window::Increase Tile Height" */
3010 for (i = 0; i < MAX_TERM_DATA; i++) {
3011 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3012 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3013
3014 if (data[i].visible) {
3015 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3016 MF_BYCOMMAND | MF_ENABLED);
3017 }
3018 }
3019
3020 /* Menu "Window::Decrease Tile Height" */
3021 for (i = 0; i < MAX_TERM_DATA; i++) {
3022 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3023 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3024
3025 if (data[i].visible) {
3026 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3027 MF_BYCOMMAND | MF_ENABLED);
3028 }
3029 }
3030
3031 /* Menu "Options", disable all */
3032 mode = graphics_modes;
3033 while (mode) {
3034 EnableMenuItem(hm, mode->grafID + IDM_OPTIONS_GRAPHICS_NONE,
3035 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3036 mode = mode->pNext;
3037 }
3038
3039 EnableMenuItem(hm, IDM_OPTIONS_GRAPHICS_NICE,
3040 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3041
3042 for (i = IDM_OPTIONS_TILE_1x1; i <= IDM_OPTIONS_TILE_16x16; i++) {
3043 EnableMenuItem(hm, i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3044 }
3045 for (i = IDM_TILE_FONT; i < IDM_TILE_12X13; i++) {
3046 EnableMenuItem(hm, i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3047 }
3048
3049 EnableMenuItem(hm, IDM_TILE_12X20,
3050 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3051 EnableMenuItem(hm, IDM_TILE_16X25,
3052 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3053
3054 EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3055 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3056 EnableMenuItem(hm, IDM_OPTIONS_LOW_PRIORITY,
3057 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3058
3059 /* Menu "Options", Item "Map" */
3060 if (inkey_flag && initialized && (use_graphics != GRAPHICS_NONE))
3061 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3062 MF_BYCOMMAND | MF_ENABLED);
3063 else
3064 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3065 MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3066
3067 /* Menu "Options", update all */
3068 mode = graphics_modes;
3069 while (mode) {
3070 CheckMenuItem(hm, mode->grafID + IDM_OPTIONS_GRAPHICS_NONE,
3071 (arg_graphics == mode->grafID ?
3072 MF_CHECKED : MF_UNCHECKED));
3073 mode = mode->pNext;
3074 }
3075
3076 CheckMenuItem(hm, IDM_OPTIONS_GRAPHICS_NICE,
3077 (arg_graphics_nice ? MF_CHECKED : MF_UNCHECKED));
3078
3079 if ((tile_width == 1) && (tile_height == 1))
3080 CheckMenuItem(hm, IDM_OPTIONS_TILE_1x1, MF_CHECKED);
3081 else
3082 CheckMenuItem(hm, IDM_OPTIONS_TILE_1x1, MF_UNCHECKED);
3083
3084 if ((tile_width == 2) && (tile_height == 1))
3085 CheckMenuItem(hm, IDM_OPTIONS_TILE_2x1, MF_CHECKED);
3086 else
3087 CheckMenuItem(hm, IDM_OPTIONS_TILE_2x1, MF_UNCHECKED);
3088
3089 if ((tile_width == 2) && (tile_height == 2))
3090 CheckMenuItem(hm, IDM_OPTIONS_TILE_2x2, MF_CHECKED);
3091 else
3092 CheckMenuItem(hm, IDM_OPTIONS_TILE_2x2, MF_UNCHECKED);
3093
3094 if ((tile_width == 3) && (tile_height == 1))
3095 CheckMenuItem(hm, IDM_OPTIONS_TILE_3x1, MF_CHECKED);
3096 else
3097 CheckMenuItem(hm, IDM_OPTIONS_TILE_3x1, MF_UNCHECKED);
3098
3099 if ((tile_width == 3) && (tile_height == 3))
3100 CheckMenuItem(hm, IDM_OPTIONS_TILE_3x3, MF_CHECKED);
3101 else
3102 CheckMenuItem(hm, IDM_OPTIONS_TILE_3x3, MF_UNCHECKED);
3103
3104 if ((tile_width == 4) && (tile_height == 2))
3105 CheckMenuItem(hm, IDM_OPTIONS_TILE_4x2, MF_CHECKED);
3106 else
3107 CheckMenuItem(hm, IDM_OPTIONS_TILE_4x2, MF_UNCHECKED);
3108
3109 if ((tile_width == 4) && (tile_height == 4))
3110 CheckMenuItem(hm, IDM_OPTIONS_TILE_4x4, MF_CHECKED);
3111 else
3112 CheckMenuItem(hm, IDM_OPTIONS_TILE_4x4, MF_UNCHECKED);
3113
3114 if ((tile_width == 6) && (tile_height == 3))
3115 CheckMenuItem(hm, IDM_OPTIONS_TILE_6x3, MF_CHECKED);
3116 else
3117 CheckMenuItem(hm, IDM_OPTIONS_TILE_6x3, MF_UNCHECKED);
3118
3119 if ((tile_width == 6) && (tile_height == 6))
3120 CheckMenuItem(hm, IDM_OPTIONS_TILE_6x6, MF_CHECKED);
3121 else
3122 CheckMenuItem(hm, IDM_OPTIONS_TILE_6x6, MF_UNCHECKED);
3123
3124 if ((tile_width == 8) && (tile_height == 4))
3125 CheckMenuItem(hm, IDM_OPTIONS_TILE_8x4, MF_CHECKED);
3126 else
3127 CheckMenuItem(hm, IDM_OPTIONS_TILE_8x4, MF_UNCHECKED);
3128
3129 if ((tile_width == 8) && (tile_height == 8))
3130 CheckMenuItem(hm, IDM_OPTIONS_TILE_8x8, MF_CHECKED);
3131 else
3132 CheckMenuItem(hm, IDM_OPTIONS_TILE_8x8, MF_UNCHECKED);
3133
3134 if ((tile_width == 16) && (tile_height == 8))
3135 CheckMenuItem(hm, IDM_OPTIONS_TILE_16x8, MF_CHECKED);
3136 else
3137 CheckMenuItem(hm, IDM_OPTIONS_TILE_16x8, MF_UNCHECKED);
3138
3139 if ((tile_width == 16) && (tile_height == 16))
3140 CheckMenuItem(hm, IDM_OPTIONS_TILE_16x16, MF_CHECKED);
3141 else
3142 CheckMenuItem(hm, IDM_OPTIONS_TILE_16x16, MF_UNCHECKED);
3143
3144 i = data[0].tile_hgt;
3145 if ((data[0].tile_wid == data[0].font_wid) && (i == data[0].font_hgt))
3146 CheckMenuItem(hm, IDM_TILE_FONT, MF_CHECKED);
3147 else
3148 CheckMenuItem(hm, IDM_TILE_FONT, MF_UNCHECKED);
3149
3150 if ((data[0].tile_wid == 8) && (i == 16))
3151 CheckMenuItem(hm, IDM_TILE_08X16, MF_CHECKED);
3152 else
3153 CheckMenuItem(hm, IDM_TILE_08X16, MF_UNCHECKED);
3154
3155 #ifdef USE_SAVER
3156 CheckMenuItem(hm, IDM_OPTIONS_SAVER,
3157 (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
3158 #endif /* USE_SAVER */
3159
3160 CheckMenuItem(hm, IDM_OPTIONS_LOW_PRIORITY,
3161 (low_priority ? MF_CHECKED : MF_UNCHECKED));
3162
3163 if (inkey_flag && initialized) {
3164 /* Menu "Options", Item "Graphics" */
3165 mode = graphics_modes;
3166 while (mode) {
3167 if ((mode->grafID == 0) || (mode->file[0])) {
3168 EnableMenuItem(hm, mode->grafID + IDM_OPTIONS_GRAPHICS_NONE, MF_ENABLED);
3169 }
3170 mode = mode->pNext;
3171 }
3172
3173 EnableMenuItem(hm, IDM_OPTIONS_GRAPHICS_NICE, MF_ENABLED);
3174
3175 /* Only enable the multiplier entries if using a tile set. */
3176 if (current_graphics_mode && current_graphics_mode->grafID) {
3177 for (i = IDM_OPTIONS_TILE_1x1;
3178 i <= IDM_OPTIONS_TILE_16x16; i++) {
3179 EnableMenuItem(hm, i, MF_ENABLED);
3180 }
3181 }
3182 for (i = IDM_TILE_FONT; i < IDM_TILE_12X13; i++) {
3183 EnableMenuItem(hm, i, MF_ENABLED);
3184 }
3185
3186 EnableMenuItem(hm, IDM_TILE_12X20, MF_ENABLED);
3187 EnableMenuItem(hm, IDM_TILE_16X25, MF_ENABLED);
3188 }
3189
3190 #ifdef USE_SAVER
3191 /* Menu "Options", Item "ScreenSaver" */
3192 EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3193 MF_BYCOMMAND | MF_ENABLED);
3194 #endif /* USE_SAVER */
3195
3196 EnableMenuItem(hm, IDM_OPTIONS_LOW_PRIORITY,
3197 MF_BYCOMMAND | MF_ENABLED);
3198 }
3199
3200
3201 /**
3202 * Check for double clicked (or dragged) savefile
3203 *
3204 * Apparently, Windows copies the entire filename into the first
3205 * piece of the "command line string". Perhaps we should extract
3206 * the "basename" of that filename and append it to the "save" dir.
3207 */
3208 static void check_for_save_file(LPSTR cmd_line)
3209 {
3210 char *s, *p;
3211
3212 /* First arg */
3213 s = cmd_line;
3214
3215 /* No args */
3216 if (!s || !*s) return;
3217
3218 /* Next arg */
3219 p = strchr(s, ' ');
3220
3221 /* Tokenize */
3222 if (p) *p = '\0';
3223
3224 /* Extract filename */
3225 my_strcpy(savefile, s, sizeof(savefile));
3226
3227 /* Validate the file */
3228 validate_file(savefile);
3229
3230 /* Start game */
3231 game_in_progress = true;
3232 Term_fresh();
3233 play_game(GAME_LOAD);
3234 quit(NULL);
3235 }
3236
3237
3238 #ifdef USE_SAVER
3239
3240 #ifdef ALLOW_BORG
3241
3242 /*
3243 * Hook into the inkey() function so that flushing keypresses
3244 * doesn't affect us.
3245 *
3246 * ToDo: Try to implement recording and playing back of games
3247 * by saving/reading the keypresses to/from a file. Note that
3248 * interrupting certain actions (resting, running, and other
3249 * repeated actions) would mess that up, so this would have to
3250 * be switched off when recording.
3251 */
3252
3253 extern struct keypress(*inkey_hack)(int flush_first);
3254
3255 static struct keypress screensaver_inkey_hack_buffer[1024];
3256
3257 static struct keypress screensaver_inkey_hack(int flush_first)
3258 {
3259 static size_t screensaver_inkey_hack_index = 0;
3260
3261 if (screensaver_inkey_hack_index < sizeof(screensaver_inkey_hack_buffer))
3262 return (screensaver_inkey_hack_buffer[screensaver_inkey_hack_index++]);
3263 else
3264 {
3265 struct keypress key = { EVT_KBRD, ESCAPE, 0 };
3266 return key;
3267 }
3268 }
3269
3270 #endif /* ALLOW_BORG */
3271
3272 /**
3273 * Start the screensaver
3274 */
3275 static void start_screensaver(void)
3276 {
3277 bool file_exist;
3278 #ifdef ALLOW_BORG
3279 int i, j;
3280 struct keypress key = { EVT_KBRD, 0, 0 };
3281 #endif /* ALLOW_BORG */
3282
3283 /* Set up the display handlers and things. */
3284 init_display();
3285 init_angband();
3286
3287 textui_init();
3288
3289 /* Set 'savefile' to a safe name */
3290 savefile_set_name(saverfilename, true, false);
3291
3292 /* Does the savefile already exist? */
3293 file_exist = file_exists(savefile);
3294
3295 /* Don't try to load a non-existant savefile */
3296 if (!file_exist) savefile[0] = '\0';
3297
3298 /* Game in progress */
3299 game_in_progress = true;
3300
3301 Term_fresh();
3302
3303 /* Screensaver mode on */
3304 SendMessage(data[0].w, WM_COMMAND, IDM_OPTIONS_SAVER, 0);
3305
3306 /* Low priority */
3307 SendMessage(data[0].w, WM_COMMAND, IDM_OPTIONS_LOW_PRIORITY, 0);
3308
3309 #ifdef ALLOW_BORG
3310 /*
3311 * MegaHack - Try to start the Borg.
3312 *
3313 * The simulated keypresses will be processed when play_game()
3314 * is called.
3315 */
3316
3317 inkey_hack = screensaver_inkey_hack;
3318 j = 0;
3319
3320 /*
3321 * If no savefile is present or then go through the steps necessary
3322 * to create a random character. If a savefile already is present
3323 * then the simulated keypresses will either clean away any [-more-]
3324 * prompts (if the character is alive), or create a new random
3325 * character.
3326 *
3327 * Luckily it's possible to send the same keypresses no matter if
3328 * the character is alive, dead, or not even yet created.
3329 */
3330 key.code = ESCAPE;
3331 screensaver_inkey_hack_buffer[j++] = key; /* Gender */
3332 screensaver_inkey_hack_buffer[j++] = key; /* Race */
3333 screensaver_inkey_hack_buffer[j++] = key; /* Class */
3334 key.code = 'n';
3335 screensaver_inkey_hack_buffer[j++] = key; /* Modify options */
3336 key.code = KC_ENTER;
3337 screensaver_inkey_hack_buffer[j++] = key; /* Reroll */
3338
3339 if (!file_exist)
3340 {
3341 /* Savefile name */
3342 int n = strlen(saverfilename);
3343 for (i = 0; i < n; i++)
3344 {
3345 key.code = saverfilename[i];
3346 screensaver_inkey_hack_buffer[j++] = key;
3347 }
3348 }
3349
3350 key.code = KC_ENTER;
3351 screensaver_inkey_hack_buffer[j++] = key; /* Return */
3352 key.code = ESCAPE;
3353 screensaver_inkey_hack_buffer[j++] = key; /* Character info */
3354
3355 /*
3356 * Make sure the "verify_special" options is off, so that we can
3357 * get into Borg mode without confirmation.
3358 *
3359 * Try just marking the savefile correctly.
3360 */
3361 player->noscore |= (NOSCORE_BORG);
3362
3363 /*
3364 * Make sure the "OPT(cheat_live)" option is set, so that the Borg can
3365 * automatically restart.
3366 */
3367 key.code = '5';
3368 screensaver_inkey_hack_buffer[j++] = key; /* Cheat options */
3369
3370 /* Cursor down to "cheat live" */
3371 key.code = '2';
3372 for (i = 0; i < OPT_cheat_live - OPT_cheat_hear - 1; i++)
3373 screensaver_inkey_hack_buffer[j++] = key;
3374
3375 key.code = 'y';
3376 screensaver_inkey_hack_buffer[j++] = key; /* Switch on "OPT(cheat_live)" */
3377 key.code = ESCAPE;
3378 screensaver_inkey_hack_buffer[j++] = key; /* Leave cheat options */
3379 screensaver_inkey_hack_buffer[j++] = key; /* Leave options */
3380
3381 /*
3382 * Now start the Borg!
3383 */
3384
3385 key.code = KTRL('Z');
3386 screensaver_inkey_hack_buffer[j++] = key; /* Enter borgmode */
3387 key.code = 'z';
3388 screensaver_inkey_hack_buffer[j++] = key; /* Run Borg */
3389 #endif /* ALLOW_BORG */
3390
3391
3392 /* Play game */
3393 play_game(GAME_LOAD);
3394 }
3395
3396 #endif /* USE_SAVER */
3397
3398
3399 /**
3400 * Display a help file
3401 */
3402 static void display_help(void)
3403 {
3404 Term_keypress('?',0);
3405 }
3406
3407
3408 /**
3409 * Process a menu command
3410 */
3411 static void process_menus(WORD wCmd)
3412 {
3413 int i;
3414
3415 term_data *td;
3416
3417 OPENFILENAME ofn;
3418
3419 /* Analyze */
3420 switch (wCmd)
3421 {
3422 /* New game */
3423 case IDM_FILE_NEW:
3424 {
3425 if (!initialized) {
3426 plog("You cannot do that yet...");
3427 } else if (game_in_progress) {
3428 plog("You can't start a new game while you're still playing!");
3429 } else {
3430 /* Start game */
3431 game_in_progress = true;
3432 Term_fresh();
3433 play_game(GAME_NEW);
3434 quit(NULL);
3435 }
3436 break;
3437 }
3438
3439 /* Open game */
3440 case IDM_FILE_OPEN:
3441 {
3442 if (!initialized) {
3443 plog("You cannot do that yet...");
3444 } else if (game_in_progress) {
3445 plog("You can't open a new game while you're still playing!");
3446 } else {
3447 memset(&ofn, 0, sizeof(ofn));
3448 ofn.lStructSize = sizeof(ofn);
3449 ofn.hwndOwner = data[0].w;
3450 ofn.lpstrFilter = "Save Files (*.)\0*\0";
3451 ofn.nFilterIndex = 1;
3452 ofn.lpstrFile = savefile;
3453 ofn.nMaxFile = 1024;
3454 ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
3455 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
3456
3457 if (GetOpenFileName(&ofn)) {
3458 /* Load 'savefile' */
3459 validate_file(savefile);
3460
3461 if (monitor_existing_savefile()) {
3462 /* Start game */
3463 game_in_progress = true;
3464 Term_fresh();
3465 play_game(GAME_LOAD);
3466 quit(NULL);
3467 }
3468 }
3469 }
3470 break;
3471 }
3472
3473 /* Save game */
3474 case IDM_FILE_SAVE:
3475 {
3476 if (game_in_progress && character_generated && inkey_flag) {
3477 /* Hack -- Forget messages */
3478 msg_flag = false;
3479
3480 /* Save the game */
3481 save_game();
3482 } else {
3483 /* Paranoia */
3484 plog("You may not do that right now.");
3485 }
3486 break;
3487 }
3488
3489 /* Exit */
3490 case IDM_FILE_EXIT:
3491 {
3492 if (game_in_progress && character_generated) {
3493 /* Paranoia */
3494 if (!inkey_flag) {
3495 plog("You may not do that right now.");
3496 break;
3497 }
3498
3499 /* Hack -- Forget messages */
3500 msg_flag = false;
3501
3502 /* Save the game */
3503 save_game();
3504 close_game(true);
3505 }
3506 quit(NULL);
3507 break;
3508 }
3509
3510 case IDM_WINDOW_VIS_0:
3511 {
3512 plog("You are not allowed to do that!");
3513
3514 break;
3515 }
3516
3517 /* Window visibility */
3518 case IDM_WINDOW_VIS_1:
3519 case IDM_WINDOW_VIS_2:
3520 case IDM_WINDOW_VIS_3:
3521 case IDM_WINDOW_VIS_4:
3522 case IDM_WINDOW_VIS_5:
3523 case IDM_WINDOW_VIS_6:
3524 case IDM_WINDOW_VIS_7:
3525 {
3526 i = wCmd - IDM_WINDOW_VIS_0;
3527
3528 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3529
3530 td = &data[i];
3531
3532 if (!td->visible) {
3533 td->visible = true;
3534 ShowWindow(td->w, SW_SHOW);
3535 term_data_redraw(td);
3536 } else {
3537 td->visible = false;
3538 ShowWindow(td->w, SW_HIDE);
3539 }
3540
3541 break;
3542 }
3543
3544 /* Window fonts */
3545 case IDM_WINDOW_FONT_0:
3546 case IDM_WINDOW_FONT_1:
3547 case IDM_WINDOW_FONT_2:
3548 case IDM_WINDOW_FONT_3:
3549 case IDM_WINDOW_FONT_4:
3550 case IDM_WINDOW_FONT_5:
3551 case IDM_WINDOW_FONT_6:
3552 case IDM_WINDOW_FONT_7:
3553 {
3554 if ((use_graphics_nice) && (!inkey_flag || !initialized)) {
3555 plog("You may not do that right now.");
3556 break;
3557 }
3558
3559 i = wCmd - IDM_WINDOW_FONT_0;
3560
3561 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3562
3563 td = &data[i];
3564
3565 term_change_font(td);
3566
3567 if (use_graphics_nice) {
3568 /* Hack -- Force redraw */
3569 Term_key_push(KTRL('R'));
3570 }
3571
3572 break;
3573 }
3574
3575 /* Bizarre Display */
3576 case IDM_WINDOW_BIZ_0:
3577 case IDM_WINDOW_BIZ_1:
3578 case IDM_WINDOW_BIZ_2:
3579 case IDM_WINDOW_BIZ_3:
3580 case IDM_WINDOW_BIZ_4:
3581 case IDM_WINDOW_BIZ_5:
3582 case IDM_WINDOW_BIZ_6:
3583 case IDM_WINDOW_BIZ_7:
3584 {
3585 i = wCmd - IDM_WINDOW_BIZ_0;
3586
3587 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3588
3589 td = &data[i];
3590
3591 td->bizarre = !td->bizarre;
3592
3593 term_getsize(td);
3594
3595 term_window_resize(td);
3596
3597 break;
3598 }
3599
3600 /* Increase Tile Width */
3601 case IDM_WINDOW_I_WID_0:
3602 case IDM_WINDOW_I_WID_1:
3603 case IDM_WINDOW_I_WID_2:
3604 case IDM_WINDOW_I_WID_3:
3605 case IDM_WINDOW_I_WID_4:
3606 case IDM_WINDOW_I_WID_5:
3607 case IDM_WINDOW_I_WID_6:
3608 case IDM_WINDOW_I_WID_7:
3609 {
3610 i = wCmd - IDM_WINDOW_I_WID_0;
3611
3612 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3613
3614 td = &data[i];
3615
3616 td->tile_wid += 1;
3617
3618 term_getsize(td);
3619
3620 term_window_resize(td);
3621
3622 break;
3623 }
3624
3625 /* Decrease Tile Height */
3626 case IDM_WINDOW_D_WID_0:
3627 case IDM_WINDOW_D_WID_1:
3628 case IDM_WINDOW_D_WID_2:
3629 case IDM_WINDOW_D_WID_3:
3630 case IDM_WINDOW_D_WID_4:
3631 case IDM_WINDOW_D_WID_5:
3632 case IDM_WINDOW_D_WID_6:
3633 case IDM_WINDOW_D_WID_7:
3634 {
3635 i = wCmd - IDM_WINDOW_D_WID_0;
3636
3637 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3638
3639 td = &data[i];
3640
3641 td->tile_wid -= 1;
3642
3643 term_getsize(td);
3644
3645 term_window_resize(td);
3646
3647 break;
3648 }
3649
3650 /* Increase Tile Height */
3651 case IDM_WINDOW_I_HGT_0:
3652 case IDM_WINDOW_I_HGT_1:
3653 case IDM_WINDOW_I_HGT_2:
3654 case IDM_WINDOW_I_HGT_3:
3655 case IDM_WINDOW_I_HGT_4:
3656 case IDM_WINDOW_I_HGT_5:
3657 case IDM_WINDOW_I_HGT_6:
3658 case IDM_WINDOW_I_HGT_7:
3659 {
3660 i = wCmd - IDM_WINDOW_I_HGT_0;
3661
3662 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3663
3664 td = &data[i];
3665
3666 td->tile_hgt += 1;
3667
3668 term_getsize(td);
3669
3670 term_window_resize(td);
3671
3672 break;
3673 }
3674
3675 /* Decrease Tile Height */
3676 case IDM_WINDOW_D_HGT_0:
3677 case IDM_WINDOW_D_HGT_1:
3678 case IDM_WINDOW_D_HGT_2:
3679 case IDM_WINDOW_D_HGT_3:
3680 case IDM_WINDOW_D_HGT_4:
3681 case IDM_WINDOW_D_HGT_5:
3682 case IDM_WINDOW_D_HGT_6:
3683 case IDM_WINDOW_D_HGT_7: {
3684 i = wCmd - IDM_WINDOW_D_HGT_0;
3685
3686 if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3687
3688 td = &data[i];
3689 td->tile_hgt -= 1;
3690
3691 term_getsize(td);
3692 term_window_resize(td);
3693
3694 break;
3695 }
3696
3697 case IDM_WINDOW_OPT: {
3698 Term_keypress('=',0);
3699 Term_keypress('w',0);
3700
3701 break;
3702 }
3703 case IDM_WINDOW_RESET: {
3704 /* Paranoia */
3705 if (!inkey_flag || !initialized) {
3706 plog("You may not do that right now.");
3707 break;
3708 }
3709
3710
3711 if (MessageBox(NULL,
3712 "This will reset the size and layout of the angband windows\n based on your screen size. Do you want to continue?",
3713 VERSION_NAME, MB_YESNO|MB_ICONWARNING) == IDYES) {
3714 term *old = Term;
3715 RECT rc;
3716
3717 (void)default_layout_win(data,MAX_TERM_DATA);
3718
3719 for (i = 0; i < MAX_TERM_DATA; i++) {
3720 /* Activate */
3721 Term_activate(&(data[i].t));
3722
3723 /* Resize the term */
3724 Term_resize(data[i].cols, data[i].rows);
3725 }
3726 /* Restore */
3727 Term_activate(old);
3728
3729 /* Do something to sub-windows */
3730 for (i = MAX_TERM_DATA - 1; i >= 0; i--) {
3731 if (!(data[i].w)) continue;
3732
3733 /* Client window size */
3734 rc.left = 0;
3735 rc.top = 0;
3736 rc.right = rc.left + data[i].cols * data[i].tile_wid +
3737 data[i].size_ow1 + data[i].size_ow2;
3738 rc.bottom = rc.top + data[i].rows * data[i].tile_hgt +
3739 data[i].size_oh1 + data[i].size_oh2;
3740
3741 /* Get total window size (without menu for sub-windows) */
3742 AdjustWindowRectEx(&rc, data[i].dwStyle, true,
3743 data[i].dwExStyle);
3744
3745 /* Total size */
3746 data[i].size_wid = rc.right - rc.left;
3747 data[i].size_hgt = rc.bottom - rc.top;
3748
3749 if (i == 0) {
3750 SetWindowPos(data[i].w, 0, data[i].pos_x, data[i].pos_y,
3751 data[i].size_wid, data[i].size_hgt, 0);
3752 } else {
3753 SetWindowPos(data[i].w, data[0].w, data[i].pos_x,
3754 data[i].pos_y, data[i].size_wid,
3755 data[i].size_hgt, 0);
3756 }
3757 if (data[i].visible) {
3758 ShowWindow(data[i].w, SW_SHOW);
3759 } else {
3760 ShowWindow(data[i].w, SW_HIDE);
3761 }
3762
3763 /* Redraw later */
3764 InvalidateRect(data[i].w, NULL, true);
3765 }
3766
3767 /* Focus on main window */
3768 SetFocus(data[0].w);
3769
3770 /* React to changes */
3771 Term_xtra_win_react();
3772
3773 /* Hack -- Force redraw */
3774 Term_key_push(KTRL('R'));
3775 }
3776
3777 break;
3778 }
3779
3780 case IDM_OPTIONS_GRAPHICS_NICE: {
3781 /* Paranoia */
3782 if (!inkey_flag || !initialized) {
3783 plog("You may not do that right now.");
3784 break;
3785 }
3786
3787 /* Toggle "arg_graphics_nice" */
3788 arg_graphics_nice = !arg_graphics_nice;
3789
3790 /* React to changes */
3791 Term_xtra_win_react();
3792
3793 /* Hack -- Force redraw */
3794 Term_key_push(KTRL('R'));
3795
3796 break;
3797 }
3798
3799 case IDM_OPTIONS_TILE_1x1:
3800 case IDM_OPTIONS_TILE_2x1:
3801 case IDM_OPTIONS_TILE_2x2:
3802 case IDM_OPTIONS_TILE_3x1:
3803 case IDM_OPTIONS_TILE_3x3:
3804 case IDM_OPTIONS_TILE_4x2:
3805 case IDM_OPTIONS_TILE_4x4:
3806 case IDM_OPTIONS_TILE_6x3:
3807 case IDM_OPTIONS_TILE_6x6:
3808 case IDM_OPTIONS_TILE_8x4:
3809 case IDM_OPTIONS_TILE_8x8:
3810 case IDM_OPTIONS_TILE_16x8:
3811 case IDM_OPTIONS_TILE_16x16:
3812 {
3813 /* Paranoia */
3814 if (!inkey_flag || !initialized) {
3815 plog("You may not do that right now.");
3816 break;
3817 }
3818 switch (wCmd)
3819 {
3820 case IDM_OPTIONS_TILE_1x1:
3821 {
3822 tile_width = 1;
3823 tile_height = 1;
3824 break;
3825 }
3826 case IDM_OPTIONS_TILE_2x1:
3827 {
3828 tile_width = 2;
3829 tile_height = 1;
3830 break;
3831 }
3832 case IDM_OPTIONS_TILE_2x2:
3833 {
3834 tile_width = 2;
3835 tile_height = 2;
3836 break;
3837 }
3838 case IDM_OPTIONS_TILE_3x1:
3839 {
3840 tile_width = 3;
3841 tile_height = 1;
3842 break;
3843 }
3844 case IDM_OPTIONS_TILE_3x3:
3845 {
3846 tile_width = 3;
3847 tile_height = 3;
3848 break;
3849 }
3850 case IDM_OPTIONS_TILE_4x2:
3851 {
3852 tile_width = 4;
3853 tile_height = 2;
3854 break;
3855 }
3856 case IDM_OPTIONS_TILE_4x4:
3857 {
3858 tile_width = 4;
3859 tile_height = 4;
3860 break;
3861 }
3862 case IDM_OPTIONS_TILE_6x3:
3863 {
3864 tile_width = 6;
3865 tile_height = 3;
3866 break;
3867 }
3868 case IDM_OPTIONS_TILE_6x6:
3869 {
3870 tile_width = 6;
3871 tile_height = 6;
3872 break;
3873 }
3874 case IDM_OPTIONS_TILE_8x4:
3875 {
3876 tile_width = 8;
3877 tile_height = 4;
3878 break;
3879 }
3880 case IDM_OPTIONS_TILE_8x8:
3881 {
3882 tile_width = 8;
3883 tile_height = 8;
3884 break;
3885 }
3886 case IDM_OPTIONS_TILE_16x8:
3887 {
3888 tile_width = 16;
3889 tile_height = 8;
3890 break;
3891 }
3892 case IDM_OPTIONS_TILE_16x16:
3893 {
3894 tile_width = 16;
3895 tile_height = 16;
3896 break;
3897 }
3898 }
3899
3900 /* Set flag */
3901 change_tilesize = true;
3902
3903 /* React to changes */
3904 Term_xtra_win_react();
3905
3906 /* Hack -- Force redraw */
3907 Term_key_push(KTRL('R'));
3908
3909 break;
3910 }
3911
3912 case IDM_TILE_FONT:
3913 case IDM_TILE_08X08:
3914 case IDM_TILE_16X16:
3915 case IDM_TILE_32X32:
3916 case IDM_TILE_08X16:
3917 case IDM_TILE_10X20:
3918 case IDM_TILE_16X32:
3919 case IDM_TILE_08X13:
3920 case IDM_TILE_10X17:
3921 case IDM_TILE_16X25:
3922 case IDM_TILE_12X20:
3923 {
3924 /* Paranoia */
3925 if (!inkey_flag || !initialized) {
3926 plog("You may not do that right now.");
3927 break;
3928 }
3929 td = &data[0];
3930 switch (wCmd)
3931 {
3932 case IDM_TILE_FONT:
3933 {
3934 td->tile_wid = td->font_wid;
3935 td->tile_hgt = td->font_hgt;
3936 break;
3937 }
3938 case IDM_TILE_08X16:
3939 {
3940 td->tile_wid = 8;
3941 td->tile_hgt = 16;
3942 break;
3943 }
3944 case IDM_TILE_08X08:
3945 {
3946 td->tile_wid = 8;
3947 td->tile_hgt = 8;
3948 break;
3949 }
3950 case IDM_TILE_16X16:
3951 {
3952 td->tile_wid = 16;
3953 td->tile_hgt = 16;
3954 break;
3955 }
3956 case IDM_TILE_32X32:
3957 {
3958 td->tile_wid = 32;
3959 td->tile_hgt = 32;
3960 break;
3961 }
3962 case IDM_TILE_10X20:
3963 {
3964 td->tile_wid = 10;
3965 td->tile_hgt = 20;
3966 break;
3967 }
3968 case IDM_TILE_16X32:
3969 {
3970 td->tile_wid = 16;
3971 td->tile_hgt = 32;
3972 break;
3973 }
3974 case IDM_TILE_12X20:
3975 {
3976 td->tile_wid = 12;
3977 td->tile_hgt = 20;
3978 break;
3979 }
3980 case IDM_TILE_16X25:
3981 {
3982 td->tile_wid = 16;
3983 td->tile_hgt = 25;
3984 break;
3985 }
3986 }
3987
3988 /* React to changes */
3989 term_getsize(td);
3990
3991 term_window_resize(td);
3992
3993 break;
3994 }
3995
3996 #ifdef USE_SAVER
3997
3998 case IDM_OPTIONS_SAVER:
3999 {
4000 if (hwndSaver) {
4001 DestroyWindow(hwndSaver);
4002 hwndSaver = NULL;
4003 screensaver_active = false;
4004
4005 /* Switch main menu back on */
4006 SetMenu(data[0].w, main_menu);
4007
4008 for (i = MAX_TERM_DATA - 1; i >= 0; --i) {
4009 td = &data[i];
4010
4011 if (td->visible) {
4012 /* Turn the Windows back to normal */
4013 SetWindowLong(td->w, GWL_STYLE, td->dwStyle);
4014
4015 /* Push the window to the top */
4016 SetWindowPos(td->w, HWND_NOTOPMOST, 0, 0, 0, 0,
4017 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
4018 }
4019 }
4020
4021 ShowCursor(true);
4022 } else {
4023 /* Create a screen saver window */
4024 hwndSaver = CreateWindowEx(WS_EX_TOPMOST,
4025 "WindowsScreenSaverClass",
4026 "Angband Screensaver",
4027 WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4028 0, 0, GetSystemMetrics(SM_CXSCREEN),
4029 GetSystemMetrics(SM_CYSCREEN),
4030 NULL, NULL, hInstance, NULL);
4031 if (hwndSaver) {
4032 for (i = MAX_TERM_DATA - 1; i >= 0; --i) {
4033 td = &data[i];
4034
4035 if (td->visible) {
4036 /* Switch off border and titlebar */
4037 SetWindowLong(td->w, GWL_STYLE, WS_VISIBLE);
4038
4039 /* Switch off menu */
4040 SetMenu(td->w, NULL);
4041
4042 /* Push the window to the top */
4043 SetWindowPos(td->w, HWND_TOPMOST, 0, 0, 0, 0,
4044 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
4045 }
4046 }
4047
4048 ShowCursor(false);
4049
4050 screensaver_active = true;
4051 } else {
4052 plog("Failed to create saver window");
4053 }
4054 }
4055
4056 break;
4057 }
4058
4059 #endif /* USE_SAVER */
4060
4061 case IDM_OPTIONS_LOW_PRIORITY:
4062 {
4063 /* Lower or reset the priority of the current process */
4064 if (low_priority)
4065 SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
4066 else
4067 SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
4068
4069 /* Toggle priority */
4070 low_priority = !low_priority;
4071
4072 break;
4073 }
4074
4075 case IDM_OPTIONS_MAP:
4076 {
4077 /* Paranoia */
4078 if (!inkey_flag || !initialized) {
4079 plog("You may not do that right now.");
4080 break;
4081 }
4082
4083 windows_map();
4084 break;
4085 }
4086
4087 case IDM_HELP_GENERAL:
4088 {
4089 display_help();
4090 break;
4091 }
4092
4093 case IDM_OPTIONS_SCREENSHOT:{
4094 char filename[1024];
4095 char path[1024];
4096 time_t ltime;
4097 struct tm *today;
4098 int len;
4099
4100 time( &ltime );
4101 today = localtime( &ltime );
4102 strnfmt(filename, sizeof(filename), "%s", player->full_name);
4103 len = strlen(filename);
4104 strftime(filename+len, sizeof(filename)-len, "_%Y%b%d_%H%M%S.png",
4105 today);
4106
4107 /* Get the system-specific path */
4108 path_build(path, sizeof(path), ANGBAND_DIR_USER, filename);
4109 td = &data[0];
4110 if (!SaveWindow_PNG(td->w, path)) {
4111 plog("Screenshot Save Failed.");
4112 }
4113 break;
4114 }
4115
4116 default: {
4117 if ((wCmd >= IDM_OPTIONS_GRAPHICS_NONE) &&
4118 (wCmd <= IDM_OPTIONS_GRAPHICS_NONE + graphics_mode_high_id)) {
4119 int selected_mode = 0;
4120 int desired_mode = wCmd - IDM_OPTIONS_GRAPHICS_NONE;
4121
4122 /* Paranoia */
4123 if (!inkey_flag || !initialized) {
4124 plog("You may not do that right now.");
4125 break;
4126 }
4127
4128 i = 0;
4129 do {
4130 if (graphics_modes[i].grafID == desired_mode) {
4131 selected_mode = desired_mode;
4132 break;
4133 }
4134 } while (graphics_modes[i++].grafID != 0);
4135
4136 /* Toggle "arg_graphics" */
4137 if (arg_graphics != selected_mode) {
4138 arg_graphics = selected_mode;
4139
4140 /* hard code values when switching to text mode */
4141 if ((selected_mode == GRAPHICS_NONE)
4142 && !use_graphics_nice) {
4143 td = &data[0];
4144 td->tile_wid = td->font_wid;
4145 td->tile_hgt = td->font_hgt;
4146 tile_width = 1;
4147 tile_height = 1;
4148
4149 /* React to changes */
4150 term_getsize(td);
4151
4152 term_window_resize(td);
4153 }
4154
4155
4156 /* React to changes */
4157 Term_xtra_win_react();
4158
4159 /* Hack -- Force redraw */
4160 Term_key_push(KTRL('R'));
4161 }
4162 }
4163 break;
4164 }
4165 }
4166 }
4167
4168
4169 /**
4170 * Redraw a section of a window
4171 */
4172 static void handle_wm_paint(HWND hWnd)
4173 {
4174 int x1, y1, x2, y2;
4175 PAINTSTRUCT ps;
4176 term_data *td;
4177
4178 /* Acquire proper "term_data" info */
4179 td = (term_data *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
4180
4181 BeginPaint(hWnd, &ps);
4182
4183 if (td->map_active) {
4184 /* Redraw the map */
4185 /* ToDo: Only redraw the necessary parts */
4186 windows_map_aux();
4187 } else {
4188 /* Get the area that should be updated (rounding up/down) */
4189 /* ToDo: Take the window borders into account */
4190 x1 = (ps.rcPaint.left / td->tile_wid) - 1;
4191 x2 = (ps.rcPaint.right / td->tile_wid) + 1;
4192 y1 = (ps.rcPaint.top / td->tile_hgt) - 1;
4193 y2 = (ps.rcPaint.bottom / td->tile_hgt) + 1;
4194
4195 /* Redraw */
4196 if (td) term_data_redraw_section(td, x1, y1, x2, y2);
4197 }
4198
4199 EndPaint(hWnd, &ps);
4200 }
4201
4202
4203 static int extract_modifiers(keycode_t ch, bool kp) {
4204 bool mc = false;
4205 bool ms = false;
4206 bool ma = false;
4207
4208 /* Extract the modifiers */
4209 if (GetKeyState(VK_CONTROL) & 0x8000) mc = true;
4210 if (GetKeyState(VK_SHIFT) & 0x8000) ms = true;
4211 if (GetKeyState(VK_MENU) & 0x8000) ma = true;
4212
4213 return
4214 (mc && (kp || MODS_INCLUDE_CONTROL(ch)) ? KC_MOD_CONTROL : 0) |
4215 (ms && (kp || MODS_INCLUDE_SHIFT(ch)) ? KC_MOD_SHIFT : 0) |
4216 (ma ? KC_MOD_ALT : 0) | (kp ? KC_MOD_KEYPAD : 0);
4217 }
4218
4219 /**
4220 * We ignore the modifier keys (shift, control, alt, num lock, scroll lock),
4221 * and the normal keys (escape, tab, return, letters, numbers, etc), but we
4222 * catch the keypad keys (with and without numlock set, including keypad 5),
4223 * the function keys (including the "menu" key which maps to F10), and the
4224 * "pause" key (between scroll lock and numlock). We also catch a few odd
4225 * keys which I do not recognize, but which are listed among keys which we
4226 * do catch, so they should be harmless to catch.
4227 *
4228 * return whether the keypress was NOT handled
4229 */
4230 static bool handle_keydown(WPARAM wParam, LPARAM lParam)
4231 {
4232 keycode_t ch = 0;
4233
4234 bool kp = false;
4235
4236 #ifdef USE_SAVER
4237 if (screensaver_active) {
4238 stop_screensaver();
4239 return true;
4240 }
4241 #endif /* USE_SAVER */
4242
4243 /* for VK_ http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx */
4244 switch (wParam) {
4245 case VK_F1: ch = KC_F1; break;
4246 case VK_F2: ch = KC_F2; break;
4247 case VK_F3: ch = KC_F3; break;
4248 case VK_F4: ch = KC_F4; break;
4249 case VK_F5: ch = KC_F5; break;
4250 case VK_F6: ch = KC_F6; break;
4251 case VK_F7: ch = KC_F7; break;
4252 case VK_F8: ch = KC_F8; break;
4253 case VK_F9: ch = KC_F9; break;
4254 case VK_F10: ch = KC_F10; break;
4255 case VK_F11: ch = KC_F11; break;
4256 case VK_F12: ch = KC_F12; break;
4257 case VK_F13: ch = KC_F13; break;
4258 case VK_F14: ch = KC_F14; break;
4259 case VK_F15: ch = KC_F15; break;
4260
4261 case VK_INSERT: ch = KC_INSERT; break;
4262 case VK_DELETE: ch = KC_DELETE; break;
4263 /* Backspace is calling both backspace and delete
4264 Removed the backspace call, so it only calls delete */
4265 case VK_BACK: break;
4266 /* Tab is registering as ^i; don't read it here*/
4267 case VK_TAB: break;
4268 case VK_PRIOR: ch = KC_PGUP; break;
4269 case VK_NEXT: ch = KC_PGDOWN; break;
4270 case VK_END: ch = KC_END; break;
4271 case VK_HOME: ch = KC_HOME; break;
4272 case VK_LEFT: ch = ARROW_LEFT; break;
4273 case VK_RIGHT: ch = ARROW_RIGHT; break;
4274 case VK_UP: ch = ARROW_UP; break;
4275 case VK_DOWN: ch = ARROW_DOWN; break;
4276
4277 case VK_CLEAR: ch = '5'; kp=true; break;
4278 case VK_PAUSE: ch = KC_PAUSE; break;
4279 }
4280
4281 /* we could fall back on using the scancode */
4282 /* obtained using LOBYTE(HIWORD(lParam)) */
4283 /* see http://source.winehq.org/source/include/dinput.h#L468 */
4284
4285 if (ch) {
4286 int mods = extract_modifiers(ch, kp);
4287 /* printf("ch=%d mods=%d\n", ch, mods); */
4288 /* fflush(stdout); */
4289 Term_keypress(ch, mods);
4290 return false;
4291 }
4292 return true;
4293 }
4294
4295
4296
4297 static LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4298 WPARAM wParam, LPARAM lParam)
4299 {
4300 HDC hdc;
4301 term_data *td;
4302 int i;
4303
4304 int xPos, yPos, button, vsc, vk, mods;
4305 bool kp = false, extended_key;
4306 keycode_t ch;
4307
4308 #ifdef USE_SAVER
4309 static int iMouse = 0;
4310 static WORD xMouse = 0;
4311 static WORD yMouse = 0;
4312
4313 int dx, dy;
4314 #endif /* USE_SAVER */
4315
4316 /* Acquire proper "term_data" info */
4317 td = (term_data *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
4318
4319 /* Handle message */
4320 switch (uMsg)
4321 {
4322 /* XXX XXX XXX */
4323 case WM_NCCREATE:
4324 {
4325 SetWindowLongPtr(hWnd, GWLP_USERDATA, (size_t) my_td);
4326 break;
4327 }
4328
4329 /* XXX XXX XXX */
4330 case WM_CREATE:
4331 {
4332 return 0;
4333 }
4334
4335 case WM_GETMINMAXINFO:
4336 {
4337 MINMAXINFO FAR *lpmmi;
4338 RECT rc;
4339
4340 lpmmi = (MINMAXINFO FAR *)lParam;
4341
4342 /* this message was sent before WM_NCCREATE */
4343 if (!td) return 1;
4344
4345 /* Minimum window size is 80x24 */
4346 rc.left = rc.top = 0;
4347 rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 +
4348 td->size_ow2;
4349 rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 +
4350 td->size_oh2 + 1;
4351
4352 /* Adjust */
4353 AdjustWindowRectEx(&rc, td->dwStyle, true, td->dwExStyle);
4354
4355 /* Save minimum size */
4356 lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4357 lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4358
4359 return 0;
4360 }
4361
4362 case WM_PAINT:
4363 {
4364 handle_wm_paint(hWnd);
4365
4366 return 0;
4367 }
4368
4369 case WM_SYSKEYDOWN:
4370 case WM_KEYDOWN:
4371 {
4372 return handle_keydown(wParam, lParam);
4373 }
4374
4375 case WM_CHAR:
4376 {
4377 vsc = LOBYTE(HIWORD(lParam));
4378 extended_key = HIBYTE(HIWORD(lParam)) & 0x1;
4379 vk = MapVirtualKey(vsc, 1);
4380 /* printf("wParam=%d lParam=%d vsc=%d vk=%d kp=%d\n", */
4381 /* wParam, lParam, vsc, vk, extended_key); */
4382 /* fflush(stdout); */
4383
4384 if (!game_in_progress) {
4385 /* Handle keyboard shortcuts pre-game */
4386 switch (wParam) {
4387 case KTRL('N'): process_menus(IDM_FILE_NEW); break;
4388 case KTRL('O'): process_menus(IDM_FILE_OPEN); break;
4389 case KTRL('X'): process_menus(IDM_FILE_EXIT); break;
4390 default: return true;
4391 }
4392 return false;
4393 }
4394
4395 // We don't want to translate some keys to their ascii values
4396 // so we have to intercept them here.
4397 switch (vk)
4398 {
4399 case 8: // fix backspace
4400 ch = KC_BACKSPACE;
4401 break;
4402 case 9: // fix tab
4403 ch = KC_TAB;
4404 break;
4405 case 13: // fix enter
4406 ch = KC_ENTER;
4407 if(extended_key) kp = true;
4408 break;
4409 case 27: // fix escape
4410 ch = ESCAPE;
4411 break;
4412 default:
4413 Term_keypress(wParam, 0);
4414 return 0;
4415 }
4416
4417 mods = extract_modifiers(ch, kp);
4418 Term_keypress(ch, mods);
4419
4420 return 0;
4421 }
4422
4423 case WM_MBUTTONDOWN:
4424 case WM_RBUTTONDOWN:
4425 case WM_LBUTTONDOWN:
4426 {
4427 if (screensaver_active) {
4428 #ifdef USE_SAVER
4429 stop_screensaver();
4430 #else
4431 screensaver_active = false;
4432 #endif /* USE_SAVER */
4433 return 0;
4434 } else {
4435 /* Get the text grid */
4436 xPos = GET_X_LPARAM(lParam);
4437 yPos = GET_Y_LPARAM(lParam);
4438 xPos /= td->tile_wid;
4439 yPos /= td->tile_hgt;
4440
4441 if (uMsg == WM_LBUTTONDOWN)
4442 button = 1;
4443 else if (uMsg == WM_RBUTTONDOWN)
4444 button = 2;
4445 else
4446 button = 3;
4447
4448 /* Extract the modifiers */
4449 /* XXX using the numbers below rather than KC_MOD_CONTROL, KCMOD_SHIFT,
4450 * and KC_MOD_ALT, to avoid having to shift them all the time. They
4451 * need to be shifted because I don't want to change the function
4452 * parameters (which would break the other platforms, which I can't
4453 * test), so the mods need to be encoded into the button.
4454 */
4455 if (GetKeyState(VK_CONTROL) & 0x8000) button |= 16;
4456 if (GetKeyState(VK_SHIFT) & 0x8000) button |= 32;
4457 if (GetKeyState(VK_MENU) & 0x8000) button |= 64;
4458
4459 Term_mousepress(xPos,yPos,button);
4460 }
4461 break;
4462 }
4463
4464 #ifdef USE_SAVER
4465 case WM_MOUSEMOVE:
4466 {
4467 if (!screensaver_active) break;
4468
4469 if (iMouse) {
4470 dx = LOWORD(lParam) - xMouse;
4471 dy = HIWORD(lParam) - yMouse;
4472
4473 if (dx < 0) dx = -dx;
4474 if (dy < 0) dy = -dy;
4475
4476 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
4477 stop_screensaver();
4478 }
4479
4480 /* Save last location */
4481 iMouse = 1;
4482 xMouse = LOWORD(lParam);
4483 yMouse = HIWORD(lParam);
4484
4485 return 0;
4486 }
4487 #endif /* USE_SAVER */
4488
4489 case WM_INITMENU:
4490 {
4491 setup_menus();
4492 return 0;
4493 }
4494
4495 #ifndef WM_QUERYENDSESSION
4496 #define WM_QUERYENDSESSION 0x0011
4497 #endif
4498
4499 case WM_QUERYENDSESSION:
4500 case WM_QUIT: {
4501 if (game_in_progress && character_generated) {
4502 if (uMsg == WM_QUERYENDSESSION && !inkey_flag) {
4503 plog("Please exit any open menus before closing the game.");
4504 return false;
4505 }
4506
4507 msg_flag = false;
4508 save_game();
4509 close_game(true);
4510 }
4511
4512 quit(NULL);
4513 return true;
4514 }
4515
4516
4517 case WM_CLOSE:
4518 {
4519 if (game_in_progress && character_generated) {
4520 if (!inkey_flag) {
4521 plog("Please exit any open menus before closing the game.");
4522 return 0;
4523 }
4524
4525 /* Hack -- Forget messages */
4526 msg_flag = false;
4527 save_game();
4528 close_game(true);
4529 }
4530
4531 quit(NULL);
4532 return 0;
4533 }
4534
4535 case WM_COMMAND:
4536 {
4537 process_menus(LOWORD(wParam));
4538 return 0;
4539 }
4540
4541 case WM_SIZE:
4542 {
4543 /* this message was sent before WM_NCCREATE */
4544 if (!td) return 1;
4545
4546 /* it was sent from inside CreateWindowEx */
4547 if (!td->w) return 1;
4548
4549 /* was sent from WM_SIZE */
4550 if (td->size_hack) return 1;
4551
4552 switch (wParam)
4553 {
4554 case SIZE_MINIMIZED:
4555 {
4556 /* Hide sub-windows */
4557 for (i = 1; i < MAX_TERM_DATA; i++)
4558 if (data[i].visible)
4559 ShowWindow(data[i].w, SW_HIDE);
4560
4561 return 0;
4562 }
4563
4564 case SIZE_MAXIMIZED:
4565 {
4566 /* fall through XXX XXX XXX */
4567 }
4568
4569 case SIZE_RESTORED:
4570 {
4571 int cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4572 int rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4573
4574 /* New size */
4575 if ((td->cols != cols) || (td->rows != rows)) {
4576 /* Save the new size */
4577 td->cols = cols;
4578 td->rows = rows;
4579
4580 /* Activate */
4581 Term_activate(&td->t);
4582
4583 /* Resize the term */
4584 Term_resize(td->cols, td->rows);
4585
4586 /* Redraw later */
4587 InvalidateRect(td->w, NULL, true);
4588 }
4589
4590 td->size_hack = true;
4591
4592 /* Show sub-windows */
4593 for (i = 1; i < MAX_TERM_DATA; i++)
4594 if (data[i].visible)
4595 ShowWindow(data[i].w, SW_SHOW);
4596
4597 td->size_hack = false;
4598
4599 return 0;
4600 }
4601 }
4602 break;
4603 }
4604
4605 case WM_PALETTECHANGED:
4606 {
4607 /* Ignore if palette change caused by itself */
4608 if ((HWND)wParam == hWnd) return 0;
4609
4610 /* Fall through... */
4611 }
4612
4613 case WM_QUERYNEWPALETTE:
4614 {
4615 if (!paletted) return 0;
4616
4617 hdc = GetDC(hWnd);
4618
4619 SelectPalette(hdc, hPal, false);
4620
4621 i = RealizePalette(hdc);
4622
4623 /* if any palette entries changed, repaint the window. */
4624 if (i) InvalidateRect(hWnd, NULL, true);
4625
4626 ReleaseDC(hWnd, hdc);
4627
4628 return 0;
4629 }
4630
4631 case WM_ACTIVATE:
4632 {
4633 if (wParam && !HIWORD(lParam)) {
4634 /* Do something to sub-windows */
4635 for (i = MAX_TERM_DATA - 1; i >= 0; i--)
4636 SetWindowPos(data[i].w, hWnd, 0, 0, 0, 0,
4637 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
4638
4639 /* Focus on main window */
4640 SetFocus(hWnd);
4641
4642 return 0;
4643 }
4644
4645 break;
4646 }
4647 }
4648
4649 return DefWindowProc(hWnd, uMsg, wParam, lParam);
4650 }
4651
4652
4653 static LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4654 WPARAM wParam, LPARAM lParam)
4655 {
4656 term_data *td;
4657 HDC hdc;
4658 int i;
4659
4660 #ifdef USE_SAVER
4661 static int iMouse = 0;
4662 static WORD xMouse = 0;
4663 static WORD yMouse = 0;
4664
4665 int dx, dy;
4666 #endif /* USE_SAVER */
4667
4668
4669 /* Acquire proper "term_data" info */
4670 td = (term_data *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
4671
4672 /* Process message */
4673 switch (uMsg)
4674 {
4675 /* XXX XXX XXX */
4676 case WM_NCCREATE:
4677 {
4678 SetWindowLongPtr(hWnd, GWLP_USERDATA, (size_t)(my_td));
4679 break;
4680 }
4681
4682 /* XXX XXX XXX */
4683 case WM_CREATE:
4684 {
4685 return 0;
4686 }
4687
4688 case WM_GETMINMAXINFO:
4689 {
4690 #if 0
4691 MINMAXINFO FAR *lpmmi;
4692 RECT rc;
4693
4694 /* this message was sent before WM_NCCREATE */
4695 if (!td) return 1;
4696
4697 lpmmi = (MINMAXINFO FAR *)lParam;
4698
4699 /* Minimum size */
4700 rc.left = rc.top = 0;
4701 rc.right = rc.left + 8 * td->tile_wid + td->size_ow1 + td->size_ow2;
4702 rc.bottom = rc.top + 2 * td->tile_hgt + td->size_oh1 + td->size_oh2;
4703
4704 /* Adjust */
4705 AdjustWindowRectEx(&rc, td->dwStyle, true, td->dwExStyle);
4706
4707 /* Save the minimum size */
4708 lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4709 lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4710
4711 /* Maximum window size */
4712 rc.left = rc.top = 0;
4713 rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 +
4714 td->size_ow2;
4715 rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 +
4716 td->size_oh2;
4717
4718 /* Paranoia */
4719 rc.right += (td->tile_wid - 1);
4720 rc.bottom += (td->tile_hgt - 1);
4721
4722 /* Adjust */
4723 AdjustWindowRectEx(&rc, td->dwStyle, true, td->dwExStyle);
4724
4725 /* Save maximum size */
4726 lpmmi->ptMaxSize.x = rc.right - rc.left;
4727 lpmmi->ptMaxSize.y = rc.bottom - rc.top;
4728
4729 /* Save the maximum size */
4730 lpmmi->ptMaxTrackSize.x = rc.right - rc.left;
4731 lpmmi->ptMaxTrackSize.y = rc.bottom - rc.top;
4732 #endif /* 0 */
4733 return 0;
4734 }
4735
4736 case WM_SIZE:
4737 {
4738 int cols;
4739 int rows;
4740
4741 /* this message was sent before WM_NCCREATE */
4742 if (!td) return 1;
4743
4744 /* it was sent from inside CreateWindowEx */
4745 if (!td->w) return 1;
4746
4747 /* was sent from inside WM_SIZE */
4748 if (td->size_hack) return 1;
4749
4750 td->size_hack = true;
4751
4752 cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4753 rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4754
4755 /* New size */
4756 if ((td->cols != cols) || (td->rows != rows)) {
4757 /* Save old term */
4758 term *old_term = Term;
4759
4760 /* Save the new size */
4761 td->cols = cols;
4762 td->rows = rows;
4763
4764 /* Activate */
4765 Term_activate(&td->t);
4766
4767 /* Resize the term */
4768 Term_resize(td->cols, td->rows);
4769
4770 /* Activate */
4771 Term_activate(old_term);
4772
4773 /* Redraw later */
4774 InvalidateRect(td->w, NULL, true);
4775
4776 /* HACK - Redraw all windows */
4777 if (character_dungeon) do_cmd_redraw();
4778 }
4779
4780 td->size_hack = false;
4781
4782 return 0;
4783 }
4784
4785 case WM_PAINT:
4786 {
4787 handle_wm_paint(hWnd);
4788
4789 return 0;
4790 }
4791
4792 case WM_SYSKEYDOWN:
4793 case WM_KEYDOWN:
4794 {
4795 return handle_keydown(wParam, lParam);
4796 break;
4797 }
4798
4799 case WM_CHAR:
4800 {
4801 Term_keypress(wParam, 0);
4802 return 0;
4803 }
4804
4805
4806 #ifdef USE_SAVER
4807 case WM_MBUTTONDOWN:
4808 case WM_RBUTTONDOWN:
4809 case WM_LBUTTONDOWN:
4810 {
4811 if (screensaver_active) {
4812 stop_screensaver();
4813 return 0;
4814 }
4815 break;
4816 }
4817
4818 case WM_MOUSEMOVE:
4819 {
4820 if (!screensaver_active) break;
4821
4822 if (iMouse) {
4823 dx = LOWORD(lParam) - xMouse;
4824 dy = HIWORD(lParam) - yMouse;
4825
4826 if (dx < 0) dx = -dx;
4827 if (dy < 0) dy = -dy;
4828
4829 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
4830 stop_screensaver();
4831 }
4832
4833 /* Save last location */
4834 iMouse = 1;
4835 xMouse = LOWORD(lParam);
4836 yMouse = HIWORD(lParam);
4837
4838 return 0;
4839 }
4840 #endif /* USE_SAVER */
4841
4842 case WM_PALETTECHANGED:
4843 {
4844 /* ignore if palette change caused by itself */
4845 if ((HWND)wParam == hWnd) return false;
4846 /* otherwise, fall through!!! */
4847 }
4848
4849 case WM_QUERYNEWPALETTE:
4850 {
4851 if (!paletted) return 0;
4852 hdc = GetDC(hWnd);
4853 SelectPalette(hdc, hPal, false);
4854 i = RealizePalette(hdc);
4855 /* if any palette entries changed, repaint the window. */
4856 if (i) InvalidateRect(hWnd, NULL, true);
4857 ReleaseDC(hWnd, hdc);
4858 return 0;
4859 }
4860
4861 case WM_NCLBUTTONDOWN:
4862 {
4863
4864 #ifdef HTCLOSE
4865 if (wParam == HTCLOSE) wParam = HTSYSMENU;
4866 #endif /* HTCLOSE */
4867
4868 if (wParam == HTSYSMENU) {
4869 if (td->visible) {
4870 td->visible = false;
4871 ShowWindow(td->w, SW_HIDE);
4872 }
4873
4874 return 0;
4875 }
4876
4877 break;
4878 }
4879 }
4880
4881 return DefWindowProc(hWnd, uMsg, wParam, lParam);
4882 }
4883
4884
4885 #ifdef USE_SAVER
4886
4887 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4888 WPARAM wParam, LPARAM lParam)
4889 {
4890 static int iMouse = 0;
4891 static WORD xMouse = 0;
4892 static WORD yMouse = 0;
4893
4894 int dx, dy;
4895
4896
4897 /* Process */
4898 switch (uMsg)
4899 {
4900 /* XXX XXX XXX */
4901 case WM_NCCREATE:
4902 {
4903 break;
4904 }
4905
4906 case WM_SETCURSOR:
4907 {
4908 SetCursor(NULL);
4909 return 0;
4910 }
4911
4912 #if 0
4913 case WM_ACTIVATE:
4914 {
4915 if (LOWORD(wParam) == WA_INACTIVE) break;
4916
4917 /* else fall through */
4918 }
4919 #endif /* 0 */
4920
4921 case WM_LBUTTONDOWN:
4922 case WM_MBUTTONDOWN:
4923 case WM_RBUTTONDOWN:
4924 case WM_KEYDOWN:
4925 {
4926 stop_screensaver();
4927 return 0;
4928 }
4929
4930 case WM_MOUSEMOVE:
4931 {
4932 if (iMouse) {
4933 dx = LOWORD(lParam) - xMouse;
4934 dy = HIWORD(lParam) - yMouse;
4935
4936 if (dx < 0) dx = -dx;
4937 if (dy < 0) dy = -dy;
4938
4939 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
4940 stop_screensaver();
4941 }
4942
4943 /* Save last location */
4944 iMouse = 1;
4945 xMouse = LOWORD(lParam);
4946 yMouse = HIWORD(lParam);
4947
4948 return 0;
4949 }
4950
4951 case WM_CLOSE:
4952 {
4953 DestroyWindow(hwndSaver);
4954 if (screensaver)
4955 SendMessage(data[0].w, WM_CLOSE, 0, 0);
4956 hwndSaver = NULL;
4957 return 0;
4958 }
4959 }
4960
4961 /* Oops */
4962 return DefWindowProc(hWnd, uMsg, wParam, lParam);
4963 }
4964
4965 #endif /* USE_SAVER */
4966
4967
4968
4969
4970
4971 /**
4972 * ------------------------------------------------------------------------
4973 * Temporary Hooks
4974 * ------------------------------------------------------------------------ */
4975
4976
4977
4978 /**
4979 * Display warning message (see "z-util.c")
4980 */
4981 static void hack_plog(const char *str)
4982 {
4983 /* Give a warning */
4984 if (str)
4985 MessageBox(NULL, str, "Warning", MB_ICONEXCLAMATION | MB_OK);
4986 }
4987
4988
4989 /**
4990 * Display error message and quit (see "z-util.c")
4991 */
4992 static void hack_quit(const char *str)
4993 {
4994 /* Give a warning */
4995 if (str)
4996 MessageBox(NULL, str, "Error",
4997 MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
4998
4999 /* Unregister the classes */
5000 UnregisterClass(AppName, hInstance);
5001
5002 /* Destroy the icon */
5003 if (hIcon) DestroyIcon(hIcon);
5004
5005 /* Clean up guarding access to the savefile. */
5006 finish_monitoring_savefile(EVENT_LEAVE_GAME, NULL, NULL);
5007
5008 #ifdef USE_SAVER
5009 if (screensaverSemaphore)
5010 CloseHandle(screensaverSemaphore);
5011 #endif /* USE_SAVER */
5012
5013 /* Exit */
5014 exit(0);
5015 }
5016
5017
5018
5019 /**
5020 * ------------------------------------------------------------------------
5021 * Various hooks
5022 * ------------------------------------------------------------------------ */
5023
5024
5025
5026 /**
5027 * Display warning message (see "z-util.c")
5028 */
5029 static void hook_plog(const char *str)
5030 {
5031 #ifdef USE_SAVER
5032 if (screensaver_active) return;
5033 #endif /* USE_SAVER */
5034
5035 /* Warning */
5036 if (str)
5037 MessageBox(data[0].w, str, "Warning", MB_ICONEXCLAMATION | MB_OK);
5038 }
5039
5040
5041 /**
5042 * Display error message and quit (see "z-util.c")
5043 */
5044 static void hook_quit(const char *str)
5045 {
5046 int i;
5047
5048
5049 #ifdef USE_SAVER
5050 if (!screensaver_active)
5051 #endif /* USE_SAVER */
5052 {
5053 /* Give a warning */
5054 if (str)
5055 MessageBox(data[0].w, str, "Error",
5056 MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5057
5058 /* Save the preferences */
5059 save_prefs();
5060 }
5061
5062 /*** Could use 'Term_nuke_win()' XXX XXX XXX */
5063
5064 /* Destroy all windows */
5065 for (i = MAX_TERM_DATA - 1; i >= 0; --i) {
5066 /* Remove all fonts from the system, free resources */
5067 if (data[i].font_file) term_remove_font(data[i].font_file);
5068 if (data[i].font_id) DeleteObject(data[i].font_id);
5069 if (data[i].font_want) string_free(data[i].font_want);
5070
5071 /* Kill the window */
5072 if (data[i].w) DestroyWindow(data[i].w);
5073 data[i].w = 0;
5074 term_nuke(&data[i].t);
5075 }
5076
5077 /* Free the bitmap stuff */
5078 FreeDIB(&infGraph);
5079 FreeDIB(&infMask);
5080
5081 close_graphics_modes();
5082
5083 /*** Free some other stuff ***/
5084
5085 DeleteObject(hbrYellow);
5086
5087 if (hPal) DeleteObject(hPal);
5088
5089 UnregisterClass(AppName, hInstance);
5090
5091 if (hIcon) DestroyIcon(hIcon);
5092
5093 /* Free strings */
5094 string_free(ini_file);
5095 string_free(argv0);
5096
5097 #ifdef HAS_CLEANUP
5098 textui_cleanup();
5099 cleanup_angband();
5100 close_sound();
5101 #endif /* HAS_CLEANUP */
5102
5103 exit(0);
5104 }
5105
5106
5107 /**
5108 * ------------------------------------------------------------------------
5109 * Initialize
5110 * ------------------------------------------------------------------------ */
5111
5112 /**
5113 * Init some stuff
5114 */
5115 static void init_stuff(void)
5116 {
5117 int i;
5118
5119 char path[1024];
5120 #ifdef USE_SAVER
5121 char tmp[1024];
5122 #endif /* USE_SAVER */
5123
5124 /* Get program name with full path */
5125 if (GetModuleFileName(hInstance, path, sizeof(path)) == 0)
5126 show_win_error();
5127
5128 /* Paranoia */
5129 path[sizeof(path) - 1] = '\0';
5130
5131 /* Save the "program name" */
5132 argv0 = string_make(path);
5133
5134 /* Get the name of the "*.ini" file */
5135 my_strcpy(path + strlen(path) - 4, ".INI", 5);
5136
5137 #ifdef USE_SAVER
5138 /* Try to get the path to the Angband folder */
5139 if (screensaver) {
5140 /* Extract the filename of the savefile for the screensaver */
5141 GetPrivateProfileStringA("Angband", "SaverFile", "",
5142 saverfilename, sizeof(saverfilename), path);
5143
5144 GetPrivateProfileStringA("Angband", "AngbandPath", "", tmp,
5145 sizeof(tmp), path);
5146 strnfmt(path, sizeof(path), "%sangband.ini", tmp);
5147 }
5148
5149 #endif /* USE_SAVER */
5150
5151 /* Save the name of the ini-file */
5152 ini_file = string_make(path);
5153
5154 /* Analyze the path */
5155 i = strlen(path);
5156
5157 /* Get the path */
5158 for (; i > 0; i--) {
5159 if (path[i] == '\\') {
5160 /* End of path */
5161 break;
5162 }
5163 }
5164
5165 /* Add "lib" to the path */
5166 my_strcpy(path + i + 1, "lib\\", sizeof(path) - i - 1);
5167
5168 /* Validate the path */
5169 validate_dir(path);
5170
5171 /* Init the file paths */
5172 init_file_paths(path, path, path);
5173
5174 /* Hack -- Validate the paths */
5175 validate_dir(ANGBAND_DIR_GAMEDATA);
5176 validate_dir(ANGBAND_DIR_CUSTOMIZE);
5177 validate_dir(ANGBAND_DIR_HELP);
5178 validate_dir(ANGBAND_DIR_SCREENS);
5179 validate_dir(ANGBAND_DIR_FONTS);
5180 validate_dir(ANGBAND_DIR_TILES);
5181 validate_dir(ANGBAND_DIR_SOUNDS);
5182 validate_dir(ANGBAND_DIR_ICONS);
5183 validate_dir(ANGBAND_DIR_USER);
5184 validate_dir(ANGBAND_DIR_SAVE);
5185 validate_dir(ANGBAND_DIR_PANIC);
5186 validate_dir(ANGBAND_DIR_SCORES);
5187
5188 /* Build the filename */
5189 path_build(path, sizeof(path), ANGBAND_DIR_SCREENS, "news.txt");
5190
5191 /* Hack -- Validate the "news.txt" file */
5192 validate_file(path);
5193
5194 /* Build the filename */
5195 path_build(path, sizeof(path), ANGBAND_DIR_FONTS, DEFAULT_FONT);
5196
5197 /* Hack -- Validate the basic font */
5198 validate_file(path);
5199
5200 /* Validate the "graf" directory */
5201 validate_dir(ANGBAND_DIR_TILES);
5202
5203 /* Validate the "sound" directory */
5204 validate_dir(ANGBAND_DIR_SOUNDS);
5205 }
5206
5207
5208 /**
5209 * Perform (as ui-game.c's reinit_hook) platform-specific actions necessary
5210 * when restarting without exiting. Also called directly at startup.
5211 */
5212 static void win_reinit(void)
5213 {
5214 /* Initialise sound. */
5215 #ifdef SOUND
5216 #if defined(SOUND_SDL) || defined(SOUND_SOUND_SDL2)
5217 init_sound("sdl", 0, NULL);
5218 #else
5219 init_sound("win", 0, NULL);
5220 #endif /* else SOUND_SDL || SOUND_SDL2 */
5221 #endif /* SOUND */
5222
5223 /*
5224 * Watch for these events to set up and tear down protection against
5225 * against accessing the savefile from multiple application instances.
5226 */
5227 event_add_handler(EVENT_LEAVE_INIT, monitor_new_savefile, NULL);
5228 event_add_handler(EVENT_LEAVE_GAME, finish_monitoring_savefile, NULL);
5229 }
5230
5231
5232 int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
5233 LPSTR lpCmdLine, int nCmdShow)
5234 {
5235 int i;
5236
5237 WNDCLASS wc;
5238 HDC hdc;
5239 MSG msg;
5240
5241 /* Unused parameter */
5242 (void)nCmdShow;
5243
5244 #ifdef USE_SAVER
5245 if (lpCmdLine && ((*lpCmdLine == '-') || (*lpCmdLine == '/'))) {
5246 lpCmdLine++;
5247
5248 switch (*lpCmdLine)
5249 {
5250 case 's':
5251 case 'S':
5252 {
5253 screensaver = true;
5254
5255 /* Only run one screensaver at the time */
5256 screensaverSemaphore = CreateSemaphore(NULL, 0, 1,
5257 "AngbandSaverSemaphore");
5258
5259 if (!screensaverSemaphore) exit(0);
5260
5261 if (GetLastError() == ERROR_ALREADY_EXISTS) {
5262 CloseHandle(screensaverSemaphore);
5263 exit(0);
5264 }
5265
5266 break;
5267 }
5268
5269 case 'P':
5270 case 'p':
5271 case 'C':
5272 case 'c':
5273 case 'A':
5274 case 'a':
5275 {
5276 /*
5277 * ToDo: implement preview, configuration, and changing
5278 * the password (as well as checking it).
5279 */
5280 exit(0);
5281 }
5282 }
5283 }
5284
5285 #endif /* USE_SAVER */
5286
5287 /* Initialize */
5288 if (hPrevInst == NULL) {
5289 wc.style = CS_CLASSDC;
5290 wc.lpfnWndProc = AngbandWndProc;
5291 wc.cbClsExtra = 0;
5292 wc.cbWndExtra = 4; /* one long pointer to term_data */
5293 wc.hInstance = hInst;
5294 wc.hIcon = hIcon = LoadIcon(hInst, "ANGBAND");
5295 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
5296 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
5297 wc.lpszMenuName = "ANGBAND";
5298 wc.lpszClassName = AppName;
5299
5300 if (!RegisterClass(&wc)) exit(1);
5301
5302 wc.lpfnWndProc = AngbandListProc;
5303 wc.lpszMenuName = NULL;
5304 wc.lpszClassName = AngList;
5305
5306 if (!RegisterClass(&wc)) exit(2);
5307
5308 #ifdef USE_SAVER
5309
5310 wc.style = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
5311 wc.lpfnWndProc = AngbandSaverProc;
5312 wc.hCursor = NULL;
5313 wc.lpszMenuName = NULL;
5314 wc.lpszClassName = "WindowsScreenSaverClass";
5315
5316 if (!RegisterClass(&wc)) exit(3);
5317
5318 #endif /* USE_SAVER */
5319
5320 }
5321
5322 setlocale(LC_CTYPE, "");
5323
5324 /* Save globally */
5325 hInstance = hInst;
5326
5327 /* Temporary hooks */
5328 plog_aux = hack_plog;
5329 quit_aux = hack_quit;
5330
5331 /* Prepare the filepaths */
5332 init_stuff();
5333
5334 /* Determine if display is 16/256/true color */
5335 hdc = GetDC(NULL);
5336 colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
5337 paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? true : false);
5338 ReleaseDC(NULL, hdc);
5339
5340 /* Initialize the colors */
5341 for (i = 0; i < MAX_COLORS; i++) {
5342 uint8_t rv, gv, bv;
5343
5344 /* Extract desired values */
5345 rv = angband_color_table[i][1];
5346 gv = angband_color_table[i][2];
5347 bv = angband_color_table[i][3];
5348
5349 /* Extract the "complex" code */
5350 win_clr[i] = PALETTERGB(rv, gv, bv);
5351
5352 /* Save the "simple" code */
5353 angband_color_table[i][0] = win_pal[i];
5354 }
5355
5356 /* load the possible graphics modes */
5357 if (!init_graphics_modes()) {
5358 plog_fmt("Graphics list load failed");
5359 }
5360
5361 /* Prepare the windows */
5362 init_windows();
5363
5364 /* Activate hooks */
5365 plog_aux = hook_plog;
5366 quit_aux = hook_quit;
5367
5368 /* Set the system suffix */
5369 ANGBAND_SYS = "win";
5370
5371 /* Set command hook */
5372 cmd_get_hook = textui_get_cmd;
5373
5374 #ifdef USE_SAVER
5375 if (screensaver) {
5376 /* Start the screensaver */
5377 start_screensaver();
5378
5379 /* Paranoia */
5380 quit(NULL);
5381 }
5382 #endif /* USE_SAVER */
5383
5384 /*
5385 * Set action that needs to be done if restarting without exiting.
5386 * Also need to do it now.
5387 */
5388 reinit_hook = win_reinit;
5389 win_reinit();
5390
5391 /* Set up the display handlers and things. */
5392 init_display();
5393 init_angband();
5394
5395 textui_init();
5396
5397 initialized = true;
5398
5399 /* Did the user double click on a save file? */
5400 check_for_save_file(lpCmdLine);
5401
5402 /* Prompt the user */
5403 prt("[Choose 'New' or 'Open' from the 'File' menu]",
5404 (Term->hgt - 23) / 5 + 23, (Term->wid - 45) / 2);
5405 Term_fresh();
5406
5407 /* Process messages forever */
5408 while (GetMessage(&msg, NULL, 0, 0)) {
5409 TranslateMessage(&msg);
5410 DispatchMessage(&msg);
5411 }
5412
5413 /* Paranoia */
5414 quit(NULL);
5415
5416 /* Paranoia */
5417 return (0);
5418 }
5419
5420 /**
5421 * Use the existence or absence of a file in the user directory to guard against
5422 * accessing a savefile simultaneously from multiple instances of the
5423 * application. Return true if, according to that mechanism, no other instance
5424 * is accessing the savefile. Return false if another instance appears to be
5425 * accessing the savefile.
5426 */
5427 static bool create_savefile_tracking_file(bool message_on_failure)
5428 {
5429 const char *tracking_extension = ".lok";
5430 char *name = mem_alloc(MAX_PATH);
5431 size_t offset = path_filename_index(savefile);
5432 bool result = true;
5433
5434 path_build(name, MAX_PATH, ANGBAND_DIR_USER, savefile + offset);
5435 my_strcat(name, tracking_extension, MAX_PATH);
5436
5437 if (!suffix(name, tracking_extension)) {
5438 /* Ugh, no room for it all. Skip checking. */
5439 mem_free(name);
5440 return result;
5441 }
5442
5443 multapp_file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
5444 0, NULL, CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, NULL);
5445 if (multapp_file == INVALID_HANDLE_VALUE) {
5446 result = false;
5447 if (message_on_failure) {
5448 plog_fmt("Another instance of the game appears to using that savefile. If that's incorrect, delete %s and retry.", name);
5449 }
5450 }
5451
5452 mem_free(name);
5453 return result;
5454 }
5455
5456 /**
5457 * Set up to monitor an existing savefile so it isn't accessed by multiple
5458 * application instances.
5459 */
5460 static bool monitor_existing_savefile(void)
5461 {
5462 assert(multapp_file == INVALID_HANDLE_VALUE);
5463 return create_savefile_tracking_file(true);
5464 }
5465
5466 /**
5467 * Respond to EVENT_LEAVE_INIT events by monitoring the savefile, if not
5468 * already monitored from the request to open it, so it won't be accessed
5469 * simultaneously by multiple application instances.
5470 */
5471 static void monitor_new_savefile(game_event_type ev_type,
5472 game_event_data *ev_data, void *user)
5473 {
5474 assert(ev_type == EVENT_LEAVE_INIT && ev_data == NULL && user == NULL);
5475 if (multapp_file == INVALID_HANDLE_VALUE) {
5476 (void) create_savefile_tracking_file(false);
5477 }
5478 }
5479
5480 /**
5481 * Respond to EVENT_LEAVE_WORLD events by ceasing to monitor the savefile.
5482 */
5483 static void finish_monitoring_savefile(game_event_type ev_type,
5484 game_event_data *ev_data, void *user)
5485 {
5486 assert(ev_type == EVENT_LEAVE_GAME && ev_data == NULL && user == NULL);
5487 if (multapp_file != INVALID_HANDLE_VALUE) {
5488 (void) CloseHandle(multapp_file);
5489 multapp_file = INVALID_HANDLE_VALUE;
5490 }
5491 }
5492
5493 #endif /* WINDOWS */