OSDN Git Service

[Refactor] VSの警告対策
[hengbandforosx/hengbandosx.git] / src / main-win.cpp
1 /*!
2  * todo main関数を含むファイルの割に長過ぎる。main-win-utils.cなどといった形で分割したい
3  * @file main-win.c
4  * @brief Windows版固有実装(メインエントリポイント含む)
5  * @date 2018/03/16
6  * @author Hengband Team
7  * @details
8  *
9  * <h3>概要</h3>
10  * Windows98かその前後の頃を起点としたAPI実装。
11  * 各種のゲームエンジンは無論、
12  * DirectXといった昨今描画に標準的となったライブラリも用いていない。
13  * タイルの描画処理などについては、現在動作の詳細を検証中。
14  *
15  * <h3>フォーク元の概要</h3>
16  * <p>
17  * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, and others
18  *
19  * This software may be copied and distributed for educational, research,
20  * and not for profit purposes provided that this copyright and statement
21  * are included in all such copies.
22  * </p>
23  * <p>
24  * This file helps Angband work with Windows computers.
25  *
26  * To use this file, use an appropriate "Makefile" or "Project File",
27  * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
28  * make sure to obtain various extra files as described below.
29  *
30  * The official compilation uses the CodeWarrior Pro compiler, which
31  * includes a special project file and precompilable header file.
32  * </p>
33  *
34  * <p>
35  * The "lib/user/pref-win.prf" file contains keymaps, macro definitions,
36  * and/or color redefinitions.
37  * </p>
38  *
39  * <p>
40  * The "lib/user/font-win.prf" contains attr/char mappings for use with the
41  * normal "lib/xtra/font/*.fon" font files.
42  * </p>
43  *
44  * <p>
45  * The "lib/user/graf-win.prf" contains attr/char mappings for use with the
46  * special "lib/xtra/graf/*.bmp" bitmap files, which are activated by a menu
47  * item.
48  * </p>
49  *
50  * <p>
51  * Compiling this file, and using the resulting executable, requires
52  * several extra files not distributed with the standard Angband code.
53  * In any case, some "*.fon" files (including "8X13.FON" if nothing else)
54  * must be placed into "lib/xtra/font/".  All of these extra files can be found
55  * in the "ext-win" archive.
56  * </p>
57  *
58  * <p>
59  * The "term_xtra_win_clear()" function should probably do a low-level
60  * clear of the current window, and redraw the borders and other things,
61  * if only for efficiency.
62  * </p>
63  *
64  * <p>
65  * A simpler method is needed for selecting the "tile size" for windows.
66  * </p>
67  *
68  * <p>
69  * The various "warning" messages assume the existance of the "screen.w"
70  * window, I think, and only a few calls actually check for its existance,
71  * this may be okay since "NULL" means "on top of all windows". (?)  The
72  * user must never be allowed to "hide" the main window, or the "menubar"
73  * will disappear.
74  * </p>
75  *
76  * <p>
77  * Special "Windows Help Files" can be placed into "lib/xtra/help/" for
78  * use with the "winhelp.exe" program.  These files *may* be available
79  * at the ftp site somewhere, but I have not seen them.
80  * </p>
81  *
82  * <p>
83  * Initial framework (and most code) by Ben Harrison (benh@phial.com).
84  *
85  * Original code by Skirmantas Kligys (kligys@scf.usc.edu).
86  *
87  * Additional code by Ross E Becker (beckerr@cis.ohio-state.edu),
88  * and Chris R. Martin (crm7479@tam2000.tamu.edu).
89  * </p>
90  */
91
92 #include "autopick/autopick-pref-processor.h"
93 #include "cmd-io/cmd-process-screen.h"
94 #include "cmd-io/cmd-save.h"
95 #include "core/game-play.h"
96 #include "core/player-processor.h"
97 #include "core/scores.h"
98 #include "core/special-internal-keys.h"
99 #include "core/stuff-handler.h"
100 #include "core/visuals-reseter.h"
101 #include "dungeon/quest.h"
102 #include "floor/floor-base-definitions.h"
103 #include "floor/floor-events.h"
104 #include "game-option/game-play-options.h"
105 #include "game-option/runtime-arguments.h"
106 #include "game-option/special-options.h"
107 #include "io/files-util.h"
108 #include "io/inet.h"
109 #include "io/input-key-acceptor.h"
110 #include "io/record-play-movie.h"
111 #include "io/signal-handlers.h"
112 #include "io/write-diary.h"
113 #include "main/angband-initializer.h"
114 #include "main/music-definitions-table.h"
115 #include "main/sound-definitions-table.h"
116 #include "main/sound-of-music.h"
117 #include "monster-floor/monster-lite.h"
118 #include "system/angband-version.h"
119 #include "system/angband.h"
120 #include "system/floor-type-definition.h"
121 #include "system/system-variables.h"
122 #include "term/gameterm.h"
123 #include "term/screen-processor.h"
124 #include "term/term-color-types.h"
125 #include "util/angband-files.h"
126 #include "util/int-char-converter.h"
127 #include "util/string-processor.h"
128 #include "view/display-map.h"
129 #include "view/display-messages.h"
130 #include "wizard/spoiler-util.h"
131 #include "wizard/wizard-spoiler.h"
132 #include "world/world.h"
133
134 #ifdef WINDOWS
135 #include "dungeon/dungeon.h"
136 #include "save/save.h"
137 #include <direct.h>
138 #include <locale.h>
139 #include <windows.h>
140
141 /*
142  * Available graphic modes
143  */
144 #define GRAPHICS_NONE 0
145 #define GRAPHICS_ORIGINAL 1
146 #define GRAPHICS_ADAM_BOLT 2
147 #define GRAPHICS_HENGBAND 3
148
149 /*
150  * Menu constants -- see "ANGBAND.RC"
151  */
152 #define IDM_FILE_NEW 100
153 #define IDM_FILE_OPEN 101
154 #define IDM_FILE_SAVE 110
155 #define IDM_FILE_SCORE 120
156 #define IDM_FILE_MOVIE 121
157 #define IDM_FILE_EXIT 130
158
159 #define IDM_WINDOW_VIS_0 200
160 #define IDM_WINDOW_VIS_1 201
161 #define IDM_WINDOW_VIS_2 202
162 #define IDM_WINDOW_VIS_3 203
163 #define IDM_WINDOW_VIS_4 204
164 #define IDM_WINDOW_VIS_5 205
165 #define IDM_WINDOW_VIS_6 206
166 #define IDM_WINDOW_VIS_7 207
167
168 #define IDM_WINDOW_FONT_0 210
169 #define IDM_WINDOW_FONT_1 211
170 #define IDM_WINDOW_FONT_2 212
171 #define IDM_WINDOW_FONT_3 213
172 #define IDM_WINDOW_FONT_4 214
173 #define IDM_WINDOW_FONT_5 215
174 #define IDM_WINDOW_FONT_6 216
175 #define IDM_WINDOW_FONT_7 217
176
177 #define IDM_WINDOW_POS_0 220
178 #define IDM_WINDOW_POS_1 221
179 #define IDM_WINDOW_POS_2 222
180 #define IDM_WINDOW_POS_3 223
181 #define IDM_WINDOW_POS_4 224
182 #define IDM_WINDOW_POS_5 225
183 #define IDM_WINDOW_POS_6 226
184 #define IDM_WINDOW_POS_7 227
185
186 #define IDM_WINDOW_BIZ_0 230
187 #define IDM_WINDOW_BIZ_1 231
188 #define IDM_WINDOW_BIZ_2 232
189 #define IDM_WINDOW_BIZ_3 233
190 #define IDM_WINDOW_BIZ_4 234
191 #define IDM_WINDOW_BIZ_5 235
192 #define IDM_WINDOW_BIZ_6 236
193 #define IDM_WINDOW_BIZ_7 237
194
195 #define IDM_WINDOW_I_WID_0 240
196 #define IDM_WINDOW_I_WID_1 241
197 #define IDM_WINDOW_I_WID_2 242
198 #define IDM_WINDOW_I_WID_3 243
199 #define IDM_WINDOW_I_WID_4 244
200 #define IDM_WINDOW_I_WID_5 245
201 #define IDM_WINDOW_I_WID_6 246
202 #define IDM_WINDOW_I_WID_7 247
203
204 #define IDM_WINDOW_D_WID_0 250
205 #define IDM_WINDOW_D_WID_1 251
206 #define IDM_WINDOW_D_WID_2 252
207 #define IDM_WINDOW_D_WID_3 253
208 #define IDM_WINDOW_D_WID_4 254
209 #define IDM_WINDOW_D_WID_5 255
210 #define IDM_WINDOW_D_WID_6 256
211 #define IDM_WINDOW_D_WID_7 257
212
213 #define IDM_WINDOW_I_HGT_0 260
214 #define IDM_WINDOW_I_HGT_1 261
215 #define IDM_WINDOW_I_HGT_2 262
216 #define IDM_WINDOW_I_HGT_3 263
217 #define IDM_WINDOW_I_HGT_4 264
218 #define IDM_WINDOW_I_HGT_5 265
219 #define IDM_WINDOW_I_HGT_6 266
220 #define IDM_WINDOW_I_HGT_7 267
221
222 #define IDM_WINDOW_D_HGT_0 270
223 #define IDM_WINDOW_D_HGT_1 271
224 #define IDM_WINDOW_D_HGT_2 272
225 #define IDM_WINDOW_D_HGT_3 273
226 #define IDM_WINDOW_D_HGT_4 274
227 #define IDM_WINDOW_D_HGT_5 275
228 #define IDM_WINDOW_D_HGT_6 276
229 #define IDM_WINDOW_D_HGT_7 277
230
231 #define IDM_WINDOW_KEEP_SUBWINDOWS 280
232
233 #define IDM_OPTIONS_NO_GRAPHICS 400
234 #define IDM_OPTIONS_OLD_GRAPHICS 401
235 #define IDM_OPTIONS_NEW_GRAPHICS 402
236 #define IDM_OPTIONS_NEW2_GRAPHICS 403
237 #define IDM_OPTIONS_BIGTILE 409
238 #define IDM_OPTIONS_SOUND 410
239 #define IDM_OPTIONS_MUSIC 411
240 #define IDM_OPTIONS_SAVER 420
241 #define IDM_OPTIONS_MAP 430
242 #define IDM_OPTIONS_BG 440
243 #define IDM_OPTIONS_OPEN_BG 441
244
245 #define IDM_DUMP_SCREEN_HTML 450
246
247 #define IDM_HELP_CONTENTS 901
248
249 /*
250  * Exclude parts of WINDOWS.H that are not needed (Win32)
251  */
252 #define WIN32_LEAN_AND_MEAN
253 #define NONLS /* All NLS defines and routines */
254 #define NOSERVICE /* All Service Controller routines, SERVICE_ equates, etc. */
255 #define NOMCX /* Modem Configuration Extensions */
256
257 /*
258  * Include the "windows" support file
259  */
260 #include <windows.h>
261
262 /*
263  * Exclude parts of MMSYSTEM.H that are not needed
264  */
265 #define MMNODRV /* Installable driver support */
266 #define MMNOWAVE /* Waveform support */
267 #define MMNOMIDI /* MIDI support */
268 #define MMNOAUX /* Auxiliary audio support */
269 #define MMNOTIMER /* Timer support */
270 #define MMNOJOY /* Joystick support */
271 #define MMNOMCI /* MCI support */
272 #define MMNOMMIO /* Multimedia file I/O support */
273
274 #define INVALID_FILE_NAME (DWORD)0xFFFFFFFF
275 #define MOUSE_SENS 40
276
277 /*
278  * Include some more files. Note: the Cygnus Cygwin compiler
279  * doesn't use mmsystem.h instead it includes the winmm library
280  * which performs a similar function.
281  */
282 #include <commdlg.h>
283 #include <mmsystem.h>
284
285 /*
286  * Include the support for loading bitmaps
287  */
288 #include "term/readdib.h"
289
290 #define MoveTo(H, X, Y) MoveToEx(H, X, Y, NULL)
291
292 /*
293  * Foreground color bits
294  */
295 #define VID_BLACK 0x00
296 #define VID_BLUE 0x01
297 #define VID_GREEN 0x02
298 #define VID_CYAN 0x03
299 #define VID_RED 0x04
300 #define VID_MAGENTA 0x05
301 #define VID_YELLOW 0x06
302 #define VID_WHITE 0x07
303
304 /*
305  * Bright text
306  */
307 #define VID_BRIGHT 0x08
308
309 /*!
310  * @struct term_data
311  * @brief ターム情報構造体 / Extra "term" data
312  * @details
313  * <p>
314  * pos_x / pos_y は各タームの左上点座標を指す。
315  * </p>
316  * <p>
317  * tile_wid / tile_hgt は[ウィンドウ]メニューのタイルの幅/高さを~を
318  * 1ドットずつ調整するステータスを指す。
319  * また、フォントを変更すると都度自動調整される。
320  * </p>
321  * <p>
322  * Note the use of "font_want" for the names of the font file requested by
323  * the user, and the use of "font_file" for the currently active font file.
324  *
325  * The "font_file" is uppercased, and takes the form "8X13.FON", while
326  * "font_want" can be in almost any form as long as it could be construed
327  * as attempting to represent the name of a font.
328  * </p>
329  */
330 typedef struct {
331     term_type t;
332     concptr s;
333     HWND w;
334     DWORD dwStyle;
335     DWORD dwExStyle;
336
337     uint keys;
338     TERM_LEN rows; /* int -> uint */
339     TERM_LEN cols;
340
341     uint pos_x; //!< タームの左上X座標
342     uint pos_y; //!< タームの左上Y座標
343     uint size_wid;
344     uint size_hgt;
345     uint size_ow1;
346     uint size_oh1;
347     uint size_ow2;
348     uint size_oh2;
349
350     bool size_hack;
351     bool xtra_hack;
352     bool visible;
353     bool bizarre;
354     concptr font_want;
355     concptr font_file;
356     HFONT font_id;
357     int font_wid; //!< フォント横幅
358     int font_hgt; //!< フォント縦幅
359     int tile_wid; //!< タイル横幅
360     int tile_hgt; //!< タイル縦幅
361
362     uint map_tile_wid;
363     uint map_tile_hgt;
364
365     bool map_active;
366     LOGFONT lf;
367
368     bool posfix;
369 } term_data;
370
371 #define MAX_TERM_DATA 8 //!< Maximum number of windows
372
373 static term_data data[MAX_TERM_DATA]; //!< An array of term_data's
374 static term_data *my_td; //!< Hack -- global "window creation" pointer
375 POINT normsize; //!< Remember normal size of main window when maxmized
376
377 /*
378  * was main window maximized on previous playing
379  */
380 bool win_maximized = FALSE;
381
382 /*
383  * game in progress
384  */
385 bool game_in_progress = FALSE;
386
387 /*
388  * note when "open"/"new" become valid
389  */
390 bool initialized = FALSE;
391
392 /*
393  * screen paletted, i.e. 256 colors
394  */
395 bool paletted = FALSE;
396
397 /*
398  * 16 colors screen, don't use RGB()
399  */
400 bool colors16 = FALSE;
401
402 /*
403  * Saved instance handle
404  */
405 static HINSTANCE hInstance;
406
407 /*
408  * Yellow brush for the cursor
409  */
410 static HBRUSH hbrYellow;
411
412 /*
413  * An icon
414  */
415 static HICON hIcon;
416
417 /*
418  * A palette
419  */
420 static HPALETTE hPal;
421
422 /* bg */
423 static HBITMAP hBG = NULL;
424 static int use_bg = 0; //!< 背景使用フラグ、1なら私用。
425 static char bg_bitmap_file[1024] = "bg.bmp"; //!< 現在の背景ビットマップファイル名。
426
427 /*
428  * The screen saver window
429  */
430 static HWND hwndSaver;
431
432 /*!
433  * 現在使用中のタイルID(0ならば未使用)
434  * Flag set once "graphics" has been initialized
435  */
436 static byte current_graphics_mode = 0;
437
438 /*
439  * The global bitmap
440  */
441 static DIBINIT infGraph;
442
443 /*
444  * The global bitmap mask
445  */
446 static DIBINIT infMask;
447
448 /*
449  * Flag set once "sound" has been initialized
450  */
451 static bool can_use_sound = FALSE;
452
453 /*
454  * Show sub-windows even when Hengband is not in focus
455  */
456 static bool keep_subwindows = TRUE;
457
458 #define SAMPLE_SOUND_MAX 16
459 /*
460  * An array of sound file names
461  */
462 static concptr sound_file[SOUND_MAX][SAMPLE_SOUND_MAX];
463
464 #define SAMPLE_MUSIC_MAX 16
465 static concptr music_file[MUSIC_BASIC_MAX][SAMPLE_MUSIC_MAX];
466 static concptr dungeon_music_file[1000][SAMPLE_MUSIC_MAX];
467 static concptr town_music_file[1000][SAMPLE_MUSIC_MAX];
468 static concptr quest_music_file[1000][SAMPLE_MUSIC_MAX];
469 static bool can_use_music = FALSE;
470
471 static MCI_OPEN_PARMS mop;
472 static char mci_device_type[256];
473
474 static int current_music_type = TERM_XTRA_MUSIC_MUTE;
475 static int current_music_id = 0;
476 static char current_music_path[1024];
477
478 /*
479  * Full path to ANGBAND.INI
480  */
481 static concptr ini_file = NULL;
482
483 /*
484  * Name of application
485  */
486 static concptr AppName = "ANGBAND";
487
488 /*
489  * Name of sub-window type
490  */
491 static concptr AngList = "AngList";
492
493 /*
494  * Directory names
495  */
496 static concptr ANGBAND_DIR_XTRA_GRAF;
497 static concptr ANGBAND_DIR_XTRA_SOUND;
498 static concptr ANGBAND_DIR_XTRA_HELP;
499 static concptr ANGBAND_DIR_XTRA_MUSIC;
500
501 /*
502  * The "complex" color values
503  */
504 static COLORREF win_clr[256];
505
506 /*
507  * Flag for macro trigger with dump ASCII
508  */
509 static bool term_no_press = FALSE;
510
511 /*
512  * Copy and paste
513  */
514 static bool mouse_down = FALSE;
515 static bool paint_rect = FALSE;
516 static TERM_LEN mousex = 0, mousey = 0;
517 static TERM_LEN oldx, oldy;
518
519 /*!
520  * @brief The "simple" color values
521  * @details
522  * See "main-ibm.c" for original table information
523  * The entries below are taken from the "color bits" defined above.
524  * Note that many of the choices below suck, but so do crappy monitors.
525  */
526 static BYTE win_pal[256] = {
527     VID_BLACK, /* Dark */
528     VID_WHITE, /* White */
529     VID_CYAN, /* Slate XXX */
530     VID_RED | VID_BRIGHT, /* Orange XXX */
531     VID_RED, /* Red */
532     VID_GREEN, /* Green */
533     VID_BLUE, /* Blue */
534     VID_YELLOW, /* Umber XXX */
535     VID_BLACK | VID_BRIGHT, /* Light Dark */
536     VID_CYAN | VID_BRIGHT, /* Light Slate XXX */
537     VID_MAGENTA, /* Violet XXX */
538     VID_YELLOW | VID_BRIGHT, /* Yellow */
539     VID_MAGENTA | VID_BRIGHT, /* Light Red XXX */
540     VID_GREEN | VID_BRIGHT, /* Light Green */
541     VID_BLUE | VID_BRIGHT, /* Light Blue */
542     VID_YELLOW /* Light Umber XXX */
543 };
544
545 /*
546  * Hack -- define which keys are "special"
547  */
548 static bool special_key[256];
549 static bool ignore_key[256];
550
551 /*
552  * Hack -- initialization list for "special_key"
553  */
554 static byte special_key_list[] = {
555     VK_CLEAR, VK_PAUSE, VK_CAPITAL, VK_KANA, VK_JUNJA, VK_FINAL, VK_KANJI, VK_CONVERT, VK_NONCONVERT, VK_ACCEPT, VK_MODECHANGE, VK_PRIOR, VK_NEXT, VK_END,
556     VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_SELECT, VK_PRINT, VK_EXECUTE, VK_SNAPSHOT, VK_INSERT, VK_DELETE, VK_HELP, VK_APPS, VK_NUMPAD0, VK_NUMPAD1,
557     VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT, VK_DECIMAL,
558     VK_DIVIDE, VK_F1, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16, VK_F17, VK_F18, VK_F19,
559     VK_F20, VK_F21, VK_F22, VK_F23, VK_F24, VK_NUMLOCK, VK_SCROLL, VK_ATTN, VK_CRSEL, VK_EXSEL, VK_EREOF, VK_PLAY, VK_ZOOM, VK_NONAME, VK_PA1,
560     0 /* End of List */
561 };
562
563 static byte ignore_key_list[] = {
564     VK_ESCAPE, VK_TAB, VK_SPACE, 'F', 'W', 'O', /*'H',*/ /* these are menu characters.*/
565     VK_SHIFT, VK_CONTROL, VK_MENU, VK_LWIN, VK_RWIN, VK_LSHIFT, VK_RSHIFT, VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, 0 /* End of List */
566 };
567
568 /* Function prototype */
569
570 static bool is_already_running(void);
571
572 /* bg */
573 static void delete_bg(void)
574 {
575     if (hBG != NULL) {
576         DeleteObject(hBG);
577         hBG = NULL;
578     }
579 }
580
581 static int init_bg(void)
582 {
583     char *bmfile = bg_bitmap_file;
584     delete_bg();
585     if (use_bg == 0)
586         return 0;
587
588     hBG = static_cast<HBITMAP>(LoadImage(NULL, bmfile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
589     if (!hBG) {
590         plog_fmt(_("壁紙用ビットマップ '%s' を読み込めません。", "Can't load the bitmap file '%s'."), bmfile);
591         use_bg = 0;
592         return 0;
593     }
594
595     use_bg = 1;
596     return 1;
597 }
598
599 static void DrawBG(HDC hdc, RECT *r)
600 {
601     if (!use_bg || !hBG)
602         return;
603
604     int x = r->left, y = r->top;
605     int nx = x;
606     int ny = y;
607     BITMAP bm;
608     GetObject(hBG, sizeof(bm), &bm);
609     int swid = bm.bmWidth;
610     int shgt = bm.bmHeight;
611
612     HDC hdcSrc = CreateCompatibleDC(hdc);
613     HBITMAP hOld = static_cast<HBITMAP>(SelectObject(hdcSrc, hBG));
614
615     do {
616         int sx = nx % swid;
617         int cwid = MIN(swid - sx, r->right - nx);
618         do {
619             int sy = ny % shgt;
620             int chgt = MIN(shgt - sy, r->bottom - ny);
621             BitBlt(hdc, nx, ny, cwid, chgt, hdcSrc, sx, sy, SRCCOPY);
622             ny += chgt;
623         } while (ny < r->bottom);
624
625         ny = y;
626         nx += cwid;
627     } while (nx < r->right);
628
629     SelectObject(hdcSrc, hOld);
630     DeleteDC(hdcSrc);
631 }
632
633 /*
634  * Check for existance of a file
635  */
636 static bool check_file(concptr s)
637 {
638     char path[1024];
639     strcpy(path, s);
640     DWORD attrib = GetFileAttributes(path);
641     if (attrib == INVALID_FILE_NAME)
642         return FALSE;
643     if (attrib & FILE_ATTRIBUTE_DIRECTORY)
644         return FALSE;
645
646     return TRUE;
647 }
648
649 /*
650  * Check for existance of a directory
651  */
652 static bool check_dir(concptr s)
653 {
654     char path[1024];
655     strcpy(path, s);
656     int i = strlen(path);
657     if (i && (path[i - 1] == '\\'))
658         path[--i] = '\0';
659
660     DWORD attrib = GetFileAttributes(path);
661     if (attrib == INVALID_FILE_NAME)
662         return FALSE;
663     if (!(attrib & FILE_ATTRIBUTE_DIRECTORY))
664         return FALSE;
665
666     return TRUE;
667 }
668
669 /*
670  * Validate a file
671  */
672 static void validate_file(concptr s)
673 {
674     if (check_file(s))
675         return;
676
677     quit_fmt(_("必要なファイル[%s]が見あたりません。", "Cannot find required file:\n%s"), s);
678 }
679
680 /*
681  * Validate a directory
682  */
683 static void validate_dir(concptr s, bool vital)
684 {
685     if (check_dir(s))
686         return;
687
688     if (vital) {
689         quit_fmt(_("必要なディレクトリ[%s]が見あたりません。", "Cannot find required directory:\n%s"), s);
690     } else if (mkdir(s)) {
691         quit_fmt("Unable to create directory:\n%s", s);
692     }
693 }
694
695 /*!
696  * @brief (Windows版固有実装)Get the "size" for a window
697  */
698 static void term_getsize(term_data *td)
699 {
700     if (td->cols < 1)
701         td->cols = 1;
702     if (td->rows < 1)
703         td->rows = 1;
704
705     TERM_LEN wid = td->cols * td->tile_wid + td->size_ow1 + td->size_ow2;
706     TERM_LEN hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
707
708     RECT rw, rc;
709     if (td->w) {
710         GetWindowRect(td->w, &rw);
711         GetClientRect(td->w, &rc);
712
713         td->size_wid = (rw.right - rw.left) - (rc.right - rc.left) + wid;
714         td->size_hgt = (rw.bottom - rw.top) - (rc.bottom - rc.top) + hgt;
715
716         td->pos_x = rw.left;
717         td->pos_y = rw.top;
718     } else {
719         /* Tempolary calculation */
720         rc.left = 0;
721         rc.right = wid;
722         rc.top = 0;
723         rc.bottom = hgt;
724         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
725         td->size_wid = rc.right - rc.left;
726         td->size_hgt = rc.bottom - rc.top;
727     }
728 }
729
730 /*
731  * Write the "prefs" for a single term
732  */
733 static void save_prefs_aux(int i)
734 {
735     term_data *td = &data[i];
736     GAME_TEXT sec_name[128];
737     char buf[1024];
738
739     if (!td->w)
740         return;
741
742     sprintf(sec_name, "Term-%d", i);
743
744     if (i > 0) {
745         strcpy(buf, td->visible ? "1" : "0");
746         WritePrivateProfileString(sec_name, "Visible", buf, ini_file);
747     }
748
749 #ifdef JP
750     strcpy(buf, td->lf.lfFaceName[0] != '\0' ? td->lf.lfFaceName : "MS ゴシック");
751 #else
752     strcpy(buf, td->lf.lfFaceName[0] != '\0' ? td->lf.lfFaceName : "Courier");
753 #endif
754
755     WritePrivateProfileString(sec_name, "Font", buf, ini_file);
756
757     wsprintf(buf, "%d", td->lf.lfWidth);
758     WritePrivateProfileString(sec_name, "FontWid", buf, ini_file);
759     wsprintf(buf, "%d", td->lf.lfHeight);
760     WritePrivateProfileString(sec_name, "FontHgt", buf, ini_file);
761     wsprintf(buf, "%d", td->lf.lfWeight);
762     WritePrivateProfileString(sec_name, "FontWgt", buf, ini_file);
763
764     strcpy(buf, td->bizarre ? "1" : "0");
765     WritePrivateProfileString(sec_name, "Bizarre", buf, ini_file);
766
767     wsprintf(buf, "%d", td->tile_wid);
768     WritePrivateProfileString(sec_name, "TileWid", buf, ini_file);
769
770     wsprintf(buf, "%d", td->tile_hgt);
771     WritePrivateProfileString(sec_name, "TileHgt", buf, ini_file);
772
773     WINDOWPLACEMENT lpwndpl;
774     lpwndpl.length = sizeof(WINDOWPLACEMENT);
775     GetWindowPlacement(td->w, &lpwndpl);
776
777     RECT rc = lpwndpl.rcNormalPosition;
778     if (i == 0)
779         wsprintf(buf, "%d", normsize.x);
780     else
781         wsprintf(buf, "%d", td->cols);
782
783     WritePrivateProfileString(sec_name, "NumCols", buf, ini_file);
784
785     if (i == 0)
786         wsprintf(buf, "%d", normsize.y);
787     else
788         wsprintf(buf, "%d", td->rows);
789
790     WritePrivateProfileString(sec_name, "NumRows", buf, ini_file);
791     if (i == 0) {
792         strcpy(buf, IsZoomed(td->w) ? "1" : "0");
793         WritePrivateProfileString(sec_name, "Maximized", buf, ini_file);
794     }
795
796     GetWindowRect(td->w, &rc);
797     wsprintf(buf, "%d", rc.left);
798     WritePrivateProfileString(sec_name, "PositionX", buf, ini_file);
799
800     wsprintf(buf, "%d", rc.top);
801     WritePrivateProfileString(sec_name, "PositionY", buf, ini_file);
802     if (i > 0) {
803         strcpy(buf, td->posfix ? "1" : "0");
804         WritePrivateProfileString(sec_name, "PositionFix", buf, ini_file);
805     }
806 }
807
808 /*
809  * Write the "prefs"
810  * We assume that the windows have all been initialized
811  */
812 static void save_prefs(void)
813 {
814     char buf[128];
815     sprintf(buf, "%d", arg_graphics);
816     WritePrivateProfileString("Angband", "Graphics", buf, ini_file);
817
818     strcpy(buf, arg_bigtile ? "1" : "0");
819     WritePrivateProfileString("Angband", "Bigtile", buf, ini_file);
820
821     strcpy(buf, arg_sound ? "1" : "0");
822     WritePrivateProfileString("Angband", "Sound", buf, ini_file);
823
824     strcpy(buf, arg_music ? "1" : "0");
825     WritePrivateProfileString("Angband", "Music", buf, ini_file);
826
827     strcpy(buf, use_bg ? "1" : "0");
828     WritePrivateProfileString("Angband", "BackGround", buf, ini_file);
829     WritePrivateProfileString("Angband", "BackGroundBitmap", bg_bitmap_file[0] != '\0' ? bg_bitmap_file : "bg.bmp", ini_file);
830
831     int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
832     char tmp[1024] = "";
833     strncat(tmp, ANGBAND_DIR, path_length);
834
835     int n = strncmp(tmp, savefile, path_length);
836     if (n == 0) {
837         char relative_path[1024] = "";
838         snprintf(relative_path, sizeof(relative_path), ".\\%s", (savefile + path_length));
839         WritePrivateProfileString("Angband", "SaveFile", relative_path, ini_file);
840     } else {
841         WritePrivateProfileString("Angband", "SaveFile", savefile, ini_file);
842     }
843
844     strcpy(buf, keep_subwindows ? "1" : "0");
845     WritePrivateProfileString("Angband", "KeepSubwindows", buf, ini_file);
846
847     for (int i = 0; i < MAX_TERM_DATA; ++i) {
848         save_prefs_aux(i);
849     }
850 }
851
852 /*
853  * Load the "prefs" for a single term
854  */
855 static void load_prefs_aux(int i)
856 {
857     term_data *td = &data[i];
858     GAME_TEXT sec_name[128];
859     char tmp[1024];
860
861     int dispx = GetSystemMetrics(SM_CXVIRTUALSCREEN);
862     int dispy = GetSystemMetrics(SM_CYVIRTUALSCREEN);
863     int posx = 0;
864     int posy = 0;
865
866     sprintf(sec_name, "Term-%d", i);
867     sprintf(sec_name, "Term-%d", i);
868     if (i > 0) {
869         td->visible = (GetPrivateProfileInt(sec_name, "Visible", td->visible, ini_file) != 0);
870     }
871
872 #ifdef JP
873     GetPrivateProfileString(sec_name, "Font", "MS ゴシック", tmp, 127, ini_file);
874 #else
875     GetPrivateProfileString(sec_name, "Font", "Courier", tmp, 127, ini_file);
876 #endif
877
878     td->bizarre = (GetPrivateProfileInt(sec_name, "Bizarre", td->bizarre, ini_file) != 0);
879
880     td->font_want = string_make(tmp);
881     int hgt = 15;
882     int wid = 0;
883     td->lf.lfWidth = GetPrivateProfileInt(sec_name, "FontWid", wid, ini_file);
884     td->lf.lfHeight = GetPrivateProfileInt(sec_name, "FontHgt", hgt, ini_file);
885     td->lf.lfWeight = GetPrivateProfileInt(sec_name, "FontWgt", 0, ini_file);
886
887     td->tile_wid = GetPrivateProfileInt(sec_name, "TileWid", td->lf.lfWidth, ini_file);
888     td->tile_hgt = GetPrivateProfileInt(sec_name, "TileHgt", td->lf.lfHeight, ini_file);
889
890     td->cols = GetPrivateProfileInt(sec_name, "NumCols", td->cols, ini_file);
891     td->rows = GetPrivateProfileInt(sec_name, "NumRows", td->rows, ini_file);
892     normsize.x = td->cols;
893     normsize.y = td->rows;
894
895     if (i == 0) {
896         win_maximized = (GetPrivateProfileInt(sec_name, "Maximized", win_maximized, ini_file) != 0);
897     }
898
899     posx = GetPrivateProfileInt(sec_name, "PositionX", posx, ini_file);
900     posy = GetPrivateProfileInt(sec_name, "PositionY", posy, ini_file);
901     td->pos_x = MIN(MAX(0, posx), dispx - 128);
902     td->pos_y = MIN(MAX(0, posy), dispy - 128);
903
904     if (i > 0) {
905         td->posfix = (GetPrivateProfileInt(sec_name, "PositionFix", td->posfix, ini_file) != 0);
906     }
907 }
908
909 /*
910  * Load the "prefs"
911  */
912 static void load_prefs(void)
913 {
914     arg_graphics = (byte)GetPrivateProfileInt("Angband", "Graphics", GRAPHICS_NONE, ini_file);
915     arg_bigtile = (GetPrivateProfileInt("Angband", "Bigtile", FALSE, ini_file) != 0);
916     use_bigtile = arg_bigtile;
917     arg_sound = (GetPrivateProfileInt("Angband", "Sound", 0, ini_file) != 0);
918     arg_music = (GetPrivateProfileInt("Angband", "Music", 0, ini_file) != 0);
919     use_bg = GetPrivateProfileInt("Angband", "BackGround", 0, ini_file);
920     GetPrivateProfileString("Angband", "BackGroundBitmap", "bg.bmp", bg_bitmap_file, 1023, ini_file);
921     GetPrivateProfileString("Angband", "SaveFile", "", savefile, 1023, ini_file);
922
923     int n = strncmp(".\\", savefile, 2);
924     if (n == 0) {
925         int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
926         char tmp[1024] = "";
927         strncat(tmp, ANGBAND_DIR, path_length);
928         strncat(tmp, savefile + 2, strlen(savefile) - 2 + path_length);
929         strncpy(savefile, tmp, strlen(tmp));
930     }
931
932     keep_subwindows = (GetPrivateProfileInt("Angband", "KeepSubwindows", 0, ini_file) != 0);
933     for (int i = 0; i < MAX_TERM_DATA; ++i) {
934         load_prefs_aux(i);
935     }
936 }
937
938 /*
939  * - Taken from files.c.
940  *
941  * Extract "tokens" from a buffer
942  *
943  * This function uses "whitespace" as delimiters, and treats any amount of
944  * whitespace as a single delimiter.  We will never return any empty tokens.
945  * When given an empty buffer, or a buffer containing only "whitespace", we
946  * will return no tokens.  We will never extract more than "num" tokens.
947  *
948  * By running a token through the "text_to_ascii()" function, you can allow
949  * that token to include (encoded) whitespace, using "\s" to encode spaces.
950  *
951  * We save pointers to the tokens in "tokens", and return the number found.
952  */
953 static s16b tokenize_whitespace(char *buf, s16b num, char **tokens)
954 {
955     s16b k = 0;
956     char *s = buf;
957
958     while (k < num) {
959         char *t;
960         for (; *s && iswspace(*s); ++s) /* loop */
961             ;
962
963         if (!*s)
964             break;
965
966         for (t = s; *t && !iswspace(*t); ++t) /* loop */
967             ;
968
969         if (*t)
970             *t++ = '\0';
971
972         tokens[k++] = s;
973         s = t;
974     }
975
976     return k;
977 }
978
979 static void load_sound_prefs(void)
980 {
981     char tmp[1024];
982     char ini_path[1024];
983     char wav_path[1024];
984     char *zz[SAMPLE_SOUND_MAX];
985
986     path_build(ini_path, 1024, ANGBAND_DIR_XTRA_SOUND, "sound_debug.cfg");
987     if (GetPrivateProfileString("Device", "type", "", mci_device_type, 256, ini_path) == 0) {
988         path_build(ini_path, 1024, ANGBAND_DIR_XTRA_SOUND, "sound.cfg");
989         GetPrivateProfileString("Device", "type", "", mci_device_type, 256, ini_path);
990     }
991
992     for (int i = 0; i < SOUND_MAX; i++) {
993         GetPrivateProfileString("Sound", angband_sound_name[i], "", tmp, 1024, ini_path);
994         int num = tokenize_whitespace(tmp, SAMPLE_SOUND_MAX, zz);
995         for (int j = 0; j < num; j++) {
996             path_build(wav_path, 1024, ANGBAND_DIR_XTRA_SOUND, zz[j]);
997             if (check_file(wav_path))
998                 sound_file[i][j] = string_make(zz[j]);
999         }
1000     }
1001 }
1002
1003 static void load_music_prefs(void)
1004 {
1005     char tmp[1024];
1006     char ini_path[1024];
1007     char wav_path[1024];
1008     char *zz[SAMPLE_MUSIC_MAX];
1009     char key[80];
1010
1011     path_build(ini_path, 1024, ANGBAND_DIR_XTRA_MUSIC, "music_debug.cfg");
1012     if (GetPrivateProfileString("Device", "type", "", mci_device_type, 256, ini_path) == 0) {
1013         path_build(ini_path, 1024, ANGBAND_DIR_XTRA_MUSIC, "music.cfg");
1014         GetPrivateProfileString("Device", "type", "", mci_device_type, 256, ini_path);
1015     }
1016
1017     for (int i = 0; i < MUSIC_BASIC_MAX; i++) {
1018         GetPrivateProfileString("Basic", angband_music_basic_name[i], "", tmp, 1024, ini_path);
1019         int num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1020         for (int j = 0; j < num; j++) {
1021             path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1022             if (check_file(wav_path))
1023                 music_file[i][j] = string_make(zz[j]);
1024         }
1025     }
1026
1027     for (int i = 0; i < current_world_ptr->max_d_idx; i++) {
1028         sprintf(key, "dungeon%03d", i);
1029         GetPrivateProfileString("Dungeon", key, "", tmp, 1024, ini_path);
1030         int num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1031         for (int j = 0; j < num; j++) {
1032             path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1033             if (check_file(wav_path))
1034                 dungeon_music_file[i][j] = string_make(zz[j]);
1035         }
1036     }
1037
1038     for (int i = 0; i < max_q_idx; i++) {
1039         sprintf(key, "quest%03d", i);
1040         GetPrivateProfileString("Quest", key, "", tmp, 1024, ini_path);
1041         int num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1042         for (int j = 0; j < num; j++) {
1043             path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1044             if (check_file(wav_path))
1045                 quest_music_file[i][j] = string_make(zz[j]);
1046         }
1047     }
1048
1049     for (int i = 0; i < 1000; i++) /*!< @todo 町最大数指定 */
1050     {
1051         sprintf(key, "town%03d", i);
1052         GetPrivateProfileString("Town", key, "", tmp, 1024, ini_path);
1053         int num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1054         for (int j = 0; j < num; j++) {
1055             path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1056             if (check_file(wav_path))
1057                 town_music_file[i][j] = string_make(zz[j]);
1058         }
1059     }
1060 }
1061
1062 /*
1063  * Create the new global palette based on the bitmap palette
1064  * (if any), and the standard 16 entry palette derived from
1065  * "win_clr[]" which is used for the basic 16 Angband colors.
1066  *
1067  * This function is never called before all windows are ready.
1068  *
1069  * This function returns FALSE if the new palette could not be
1070  * prepared, which should normally be a fatal error.  XXX XXX
1071  *
1072  * Note that only some machines actually use a "palette".
1073  */
1074 static int new_palette(void)
1075 {
1076     int i, nEntries;
1077     int pLogPalSize;
1078     int lppeSize;
1079     LPLOGPALETTE pLogPal;
1080     LPPALETTEENTRY lppe;
1081     term_data *td;
1082     if (!paletted)
1083         return TRUE;
1084
1085     lppeSize = 0;
1086     lppe = NULL;
1087     nEntries = 0;
1088
1089     HPALETTE hBmPal = static_cast<HPALETTE>(infGraph.hPalette);
1090     if (hBmPal) {
1091         lppeSize = 256 * sizeof(PALETTEENTRY);
1092         lppe = (LPPALETTEENTRY)ralloc(lppeSize);
1093         nEntries = GetPaletteEntries(hBmPal, 0, 255, lppe);
1094         if ((nEntries == 0) || (nEntries > 220)) {
1095             plog(_("画面を16ビットか24ビットカラーモードにして下さい。", "Please switch to high- or true-color mode."));
1096             rnfree(lppe, lppeSize);
1097             return FALSE;
1098         }
1099     }
1100
1101     pLogPalSize = sizeof(LOGPALETTE) + (nEntries + 16) * sizeof(PALETTEENTRY);
1102     pLogPal = (LPLOGPALETTE)ralloc(pLogPalSize);
1103     pLogPal->palVersion = 0x300;
1104     pLogPal->palNumEntries = nEntries + 16;
1105     for (i = 0; i < nEntries; i++) {
1106         pLogPal->palPalEntry[i] = lppe[i];
1107     }
1108
1109     for (i = 0; i < 16; i++) {
1110         LPPALETTEENTRY p;
1111         p = &(pLogPal->palPalEntry[i + nEntries]);
1112         p->peRed = GetRValue(win_clr[i]);
1113         p->peGreen = GetGValue(win_clr[i]);
1114         p->peBlue = GetBValue(win_clr[i]);
1115         p->peFlags = PC_NOCOLLAPSE;
1116     }
1117
1118     if (lppe)
1119         rnfree(lppe, lppeSize);
1120
1121     HPALETTE hNewPal = CreatePalette(pLogPal);
1122     if (!hNewPal)
1123         quit(_("パレットを作成できません!", "Cannot create palette!"));
1124
1125     rnfree(pLogPal, pLogPalSize);
1126     td = &data[0];
1127     HDC hdc = GetDC(td->w);
1128     SelectPalette(hdc, hNewPal, 0);
1129     i = RealizePalette(hdc);
1130     ReleaseDC(td->w, hdc);
1131     if (i == 0)
1132         quit(_("パレットをシステムエントリにマップできません!", "Cannot realize palette!"));
1133
1134     for (i = 1; i < MAX_TERM_DATA; i++) {
1135         td = &data[i];
1136         hdc = GetDC(td->w);
1137         SelectPalette(hdc, hNewPal, 0);
1138         ReleaseDC(td->w, hdc);
1139     }
1140
1141     if (hPal)
1142         DeleteObject(hPal);
1143
1144     hPal = hNewPal;
1145     return TRUE;
1146 }
1147
1148 /*!
1149  * @brief グラフィクスを初期化する / Initialize graphics
1150  * @details
1151  * <ul>
1152  * <li>メニュー[オプション]>[グラフィクス]が「なし」以外の時に描画処理を初期化する。</li>
1153  * <li>呼び出されるタイミングはロード時、及び同メニューで「なし」以外に変更される毎になる。</li>
1154  * </ul>
1155  */
1156 static bool init_graphics(void)
1157 {
1158     char buf[1024];
1159     BYTE wid, hgt, twid, thgt, ox, oy;
1160     concptr name;
1161
1162     if (arg_graphics == GRAPHICS_ADAM_BOLT) {
1163         wid = 16;
1164         hgt = 16;
1165         twid = 16;
1166         thgt = 16;
1167         ox = 0;
1168         oy = 0;
1169         name = "16X16.BMP";
1170
1171         ANGBAND_GRAF = "new";
1172     } else if (arg_graphics == GRAPHICS_HENGBAND) {
1173         wid = 32;
1174         hgt = 32;
1175         twid = 32;
1176         thgt = 32;
1177         ox = 0;
1178         oy = 0;
1179         name = "32X32.BMP";
1180
1181         ANGBAND_GRAF = "ne2";
1182     } else {
1183         wid = 8;
1184         hgt = 8;
1185         twid = 8;
1186         thgt = 8;
1187         ox = 0;
1188         oy = 0;
1189         name = "8X8.BMP";
1190         ANGBAND_GRAF = "old";
1191     }
1192
1193     path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, name);
1194     if (!ReadDIB(data[0].w, buf, &infGraph)) {
1195         plog_fmt(_("ビットマップ '%s' を読み込めません。", "Cannot read bitmap file '%s'"), name);
1196         return FALSE;
1197     }
1198
1199     infGraph.CellWidth = wid;
1200     infGraph.CellHeight = hgt;
1201     infGraph.TileWidth = twid;
1202     infGraph.TileHeight = thgt;
1203     infGraph.OffsetX = ox;
1204     infGraph.OffsetY = oy;
1205
1206     if (arg_graphics == GRAPHICS_ADAM_BOLT) {
1207         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask.bmp");
1208         if (!ReadDIB(data[0].w, buf, &infMask)) {
1209             plog_fmt("Cannot read bitmap file '%s'", buf);
1210             return FALSE;
1211         }
1212     }
1213
1214     if (arg_graphics == GRAPHICS_HENGBAND) {
1215         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask32.bmp");
1216         if (!ReadDIB(data[0].w, buf, &infMask)) {
1217             plog_fmt("Cannot read bitmap file '%s'", buf);
1218             return FALSE;
1219         }
1220     }
1221
1222     if (!new_palette()) {
1223         plog(_("パレットを実現できません!", "Cannot activate palette!"));
1224         return FALSE;
1225     }
1226
1227     current_graphics_mode = arg_graphics;
1228     return current_graphics_mode != 0;
1229 }
1230
1231 /*
1232  * Initialize music
1233  */
1234 static void init_music(void)
1235 {
1236     if (!can_use_music) {
1237         load_music_prefs();
1238         can_use_music = TRUE;
1239     }
1240 }
1241
1242 /*
1243  * Hack -- Stop a music
1244  */
1245 static void stop_music(void)
1246 {
1247     mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
1248     mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
1249     current_music_type = TERM_XTRA_MUSIC_MUTE;
1250     current_music_id = 0;
1251     strcpy(current_music_path, "\0");
1252 }
1253
1254 /*
1255  * Initialize sound
1256  */
1257 static void init_sound(void)
1258 {
1259     if (!can_use_sound) {
1260         load_sound_prefs();
1261         can_use_sound = TRUE;
1262     }
1263 }
1264
1265 /*
1266  * Resize a window
1267  */
1268 static void term_window_resize(term_data *td)
1269 {
1270     if (!td->w)
1271         return;
1272
1273     SetWindowPos(td->w, 0, 0, 0, td->size_wid, td->size_hgt, SWP_NOMOVE | SWP_NOZORDER);
1274     InvalidateRect(td->w, NULL, TRUE);
1275 }
1276
1277 /*
1278  * todo 引数のpathを消す
1279  * Force the use of a new "font file" for a term_data.
1280  * This function may be called before the "window" is ready.
1281  * This function returns zero only if everything succeeds.
1282  * Note that the "font name" must be capitalized!!!
1283  */
1284 static errr term_force_font(term_data *td, concptr path)
1285 {
1286     if (td->font_id)
1287         DeleteObject(td->font_id);
1288
1289     (void)path;
1290     td->font_id = CreateFontIndirect(&(td->lf));
1291     int wid = td->lf.lfWidth;
1292     int hgt = td->lf.lfHeight;
1293     if (!td->font_id)
1294         return 1;
1295
1296     if (!wid || !hgt) {
1297         HDC hdcDesktop;
1298         HFONT hfOld;
1299         TEXTMETRIC tm;
1300
1301         hdcDesktop = GetDC(HWND_DESKTOP);
1302         hfOld = static_cast<HFONT>(SelectObject(hdcDesktop, td->font_id));
1303         GetTextMetrics(hdcDesktop, &tm);
1304         SelectObject(hdcDesktop, hfOld);
1305         ReleaseDC(HWND_DESKTOP, hdcDesktop);
1306
1307         wid = tm.tmAveCharWidth;
1308         hgt = tm.tmHeight;
1309     }
1310
1311     td->font_wid = wid;
1312     td->font_hgt = hgt;
1313
1314     return 0;
1315 }
1316
1317 /*
1318  * Allow the user to change the font for this window.
1319  */
1320 static void term_change_font(term_data *td)
1321 {
1322     CHOOSEFONT cf;
1323     memset(&cf, 0, sizeof(cf));
1324     cf.lStructSize = sizeof(cf);
1325     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
1326     cf.lpLogFont = &(td->lf);
1327
1328     if (!ChooseFont(&cf))
1329         return;
1330
1331     term_force_font(td, NULL);
1332     td->bizarre = TRUE;
1333     td->tile_wid = td->font_wid;
1334     td->tile_hgt = td->font_hgt;
1335     term_getsize(td);
1336     term_window_resize(td);
1337 }
1338
1339 /*
1340  * Allow the user to lock this window.
1341  */
1342 static void term_window_pos(term_data *td, HWND hWnd)
1343 {
1344     SetWindowPos(td->w, hWnd, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
1345 }
1346
1347 static void windows_map(player_type *player_ptr);
1348
1349 /*
1350  * Hack -- redraw a term_data
1351  */
1352 static void term_data_redraw(player_type *player_ptr, term_data *td)
1353 {
1354     if (td->map_active) {
1355         windows_map(player_ptr);
1356         return;
1357     }
1358
1359     term_activate(&td->t);
1360     term_redraw();
1361     term_activate(term_screen);
1362 }
1363
1364 void term_inversed_area(HWND hWnd, int x, int y, int w, int h)
1365 {
1366     term_data *td = (term_data *)GetWindowLong(hWnd, 0);
1367     int tx = td->size_ow1 + x * td->tile_wid;
1368     int ty = td->size_oh1 + y * td->tile_hgt;
1369     int tw = w * td->tile_wid - 1;
1370     int th = h * td->tile_hgt - 1;
1371
1372     HDC hdc = GetDC(hWnd);
1373     HBRUSH myBrush = CreateSolidBrush(RGB(255, 255, 255));
1374     HBRUSH oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1375     HPEN oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1376
1377     PatBlt(hdc, tx, ty, tw, th, PATINVERT);
1378
1379     SelectObject(hdc, oldBrush);
1380     SelectObject(hdc, oldPen);
1381 }
1382
1383 /*!
1384  * @brief //!< Windows版ユーザ設定項目実装部(実装必須) /Interact with the User
1385  */
1386 static errr term_user_win(int n)
1387 {
1388     (void)n;
1389     return 0;
1390 }
1391
1392 /*
1393  * React to global changes
1394  */
1395 static errr term_xtra_win_react(player_type *player_ptr)
1396 {
1397     if (colors16) {
1398         for (int i = 0; i < 256; i++) {
1399             win_pal[i] = angband_color_table[i][0];
1400         }
1401     } else {
1402         COLORREF code;
1403         byte rv, gv, bv;
1404         bool change = FALSE;
1405         for (int i = 0; i < 256; i++) {
1406             rv = angband_color_table[i][1];
1407             gv = angband_color_table[i][2];
1408             bv = angband_color_table[i][3];
1409             code = PALETTERGB(rv, gv, bv);
1410             if (win_clr[i] != code) {
1411                 change = TRUE;
1412                 win_clr[i] = code;
1413             }
1414         }
1415
1416         if (change)
1417             (void)new_palette();
1418     }
1419
1420     if (use_sound != arg_sound) {
1421         init_sound();
1422         use_sound = arg_sound;
1423     }
1424
1425     if (use_music != arg_music) {
1426         init_music();
1427         use_music = arg_music;
1428         if (!arg_music)
1429             stop_music();
1430         else
1431             select_floor_music(player_ptr);
1432     }
1433
1434     if (use_graphics != (arg_graphics > 0)) {
1435         if (arg_graphics && !init_graphics()) {
1436             plog(_("グラフィックスを初期化できません!", "Cannot initialize graphics!"));
1437             arg_graphics = GRAPHICS_NONE;
1438         }
1439
1440         use_graphics = (arg_graphics > 0);
1441         reset_visuals(player_ptr, process_autopick_file_command);
1442     }
1443
1444     for (int i = 0; i < MAX_TERM_DATA; i++) {
1445         term_type *old = Term;
1446         term_data *td = &data[i];
1447         if ((td->cols != td->t.wid) || (td->rows != td->t.hgt)) {
1448             term_activate(&td->t);
1449             term_resize(td->cols, td->rows);
1450             term_redraw();
1451             term_activate(old);
1452         }
1453     }
1454
1455     return 0;
1456 }
1457
1458 /*
1459  * Process at least one event
1460  */
1461 static errr term_xtra_win_event(int v)
1462 {
1463     MSG msg;
1464     if (v) {
1465         if (GetMessage(&msg, NULL, 0, 0)) {
1466             TranslateMessage(&msg);
1467             DispatchMessage(&msg);
1468         }
1469     } else {
1470         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1471             TranslateMessage(&msg);
1472             DispatchMessage(&msg);
1473         }
1474     }
1475
1476     return 0;
1477 }
1478
1479 /*
1480  * Process all pending events
1481  */
1482 static errr term_xtra_win_flush(void)
1483 {
1484     MSG msg;
1485     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1486         TranslateMessage(&msg);
1487         DispatchMessage(&msg);
1488     }
1489
1490     return 0;
1491 }
1492
1493 /*
1494  * Hack -- clear the screen
1495  *
1496  * Make this more efficient
1497  */
1498 static errr term_xtra_win_clear(void)
1499 {
1500     term_data *td = (term_data *)(Term->data);
1501
1502     RECT rc;
1503     rc.left = td->size_ow1;
1504     rc.right = rc.left + td->cols * td->tile_wid;
1505     rc.top = td->size_oh1;
1506     rc.bottom = rc.top + td->rows * td->tile_hgt;
1507
1508     HDC hdc = GetDC(td->w);
1509     SetBkColor(hdc, RGB(0, 0, 0));
1510     SelectObject(hdc, td->font_id);
1511     ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1512
1513     if (use_bg) {
1514         rc.left = 0;
1515         rc.top = 0;
1516         DrawBG(hdc, &rc);
1517     }
1518
1519     ReleaseDC(td->w, hdc);
1520     return 0;
1521 }
1522
1523 /*
1524  * Hack -- make a noise
1525  */
1526 static errr term_xtra_win_noise(void)
1527 {
1528     MessageBeep(MB_ICONASTERISK);
1529     return 0;
1530 }
1531
1532 /*
1533  * Hack -- make a sound
1534  */
1535 static errr term_xtra_win_sound(int v)
1536 {
1537     char buf[1024];
1538     if (!use_sound)
1539         return 1;
1540     if ((v < 0) || (v >= SOUND_MAX))
1541         return 1;
1542
1543     int i;
1544     for (i = 0; i < SAMPLE_SOUND_MAX; i++) {
1545         if (!sound_file[v][i])
1546             break;
1547     }
1548
1549     if (i == 0)
1550         return 1;
1551
1552     path_build(buf, 1024, ANGBAND_DIR_XTRA_SOUND, sound_file[v][Rand_external(i)]);
1553     return (PlaySound(buf, 0, SND_FILENAME | SND_ASYNC));
1554 }
1555
1556 /*
1557  * Hack -- play a music
1558  */
1559 static errr term_xtra_win_music(int n, int v)
1560 {
1561     int i = 0;
1562     char buf[1024];
1563
1564     if (n == TERM_XTRA_MUSIC_MUTE)
1565         stop_music();
1566
1567     if (!use_music) {
1568         return 1;
1569     }
1570
1571     if (n == TERM_XTRA_MUSIC_BASIC && ((v < 0) || (v >= MUSIC_BASIC_MAX)))
1572         return 1;
1573     else if (v < 0 || v >= 1000)
1574         return (1); /*!< TODO */
1575
1576     switch (n) {
1577     case TERM_XTRA_MUSIC_BASIC:
1578         for (i = 0; i < SAMPLE_MUSIC_MAX; i++)
1579             if (!music_file[v][i])
1580                 break;
1581         break;
1582     case TERM_XTRA_MUSIC_DUNGEON:
1583         for (i = 0; i < SAMPLE_MUSIC_MAX; i++)
1584             if (!dungeon_music_file[v][i])
1585                 break;
1586         break;
1587     case TERM_XTRA_MUSIC_QUEST:
1588         for (i = 0; i < SAMPLE_MUSIC_MAX; i++)
1589             if (!quest_music_file[v][i])
1590                 break;
1591         break;
1592     case TERM_XTRA_MUSIC_TOWN:
1593         for (i = 0; i < SAMPLE_MUSIC_MAX; i++)
1594             if (!town_music_file[v][i])
1595                 break;
1596         break;
1597     }
1598
1599     if (i == 0) {
1600         return 1;
1601     }
1602
1603     switch (n) {
1604     case TERM_XTRA_MUSIC_BASIC:
1605         path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, music_file[v][Rand_external(i)]);
1606         break;
1607     case TERM_XTRA_MUSIC_DUNGEON:
1608         path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, dungeon_music_file[v][Rand_external(i)]);
1609         break;
1610     case TERM_XTRA_MUSIC_QUEST:
1611         path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, quest_music_file[v][Rand_external(i)]);
1612         break;
1613     case TERM_XTRA_MUSIC_TOWN:
1614         path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, town_music_file[v][Rand_external(i)]);
1615         break;
1616     }
1617
1618     if (current_music_type == n && current_music_id == v)
1619         return 0;
1620
1621     if (current_music_type != TERM_XTRA_MUSIC_MUTE && n != TERM_XTRA_MUSIC_MUTE)
1622         if (0 == strcmp(current_music_path, buf))
1623             return 0;
1624
1625     current_music_type = n;
1626     current_music_id = v;
1627     strcpy(current_music_path, buf);
1628
1629     mop.lpstrDeviceType = mci_device_type;
1630     mop.lpstrElementName = buf;
1631     mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
1632     mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
1633     mciSendCommand(mop.wDeviceID, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mop);
1634     mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
1635     mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
1636     return 0;
1637 }
1638
1639 /*
1640  * Delay for "x" milliseconds
1641  */
1642 static int term_xtra_win_delay(int v)
1643 {
1644     Sleep(v);
1645     return 0;
1646 }
1647
1648 /*
1649  * todo z-termに影響があるのでplayer_typeの追加は保留
1650  * Do a "special thing"
1651  */
1652 static errr term_xtra_win(int n, int v)
1653 {
1654     switch (n) {
1655     case TERM_XTRA_NOISE: {
1656         return (term_xtra_win_noise());
1657     }
1658     case TERM_XTRA_MUSIC_BASIC:
1659     case TERM_XTRA_MUSIC_DUNGEON:
1660     case TERM_XTRA_MUSIC_QUEST:
1661     case TERM_XTRA_MUSIC_TOWN:
1662     case TERM_XTRA_MUSIC_MUTE: {
1663         return (term_xtra_win_music(n, v));
1664     }
1665     case TERM_XTRA_SOUND: {
1666         return (term_xtra_win_sound(v));
1667     }
1668     case TERM_XTRA_BORED: {
1669         return (term_xtra_win_event(0));
1670     }
1671     case TERM_XTRA_EVENT: {
1672         return (term_xtra_win_event(v));
1673     }
1674     case TERM_XTRA_FLUSH: {
1675         return (term_xtra_win_flush());
1676     }
1677     case TERM_XTRA_CLEAR: {
1678         return (term_xtra_win_clear());
1679     }
1680     case TERM_XTRA_REACT: {
1681         return (term_xtra_win_react(p_ptr));
1682     }
1683     case TERM_XTRA_DELAY: {
1684         return (term_xtra_win_delay(v));
1685     }
1686     }
1687
1688     return 1;
1689 }
1690
1691 /*
1692  * Low level graphics (Assumes valid input).
1693  *
1694  * Draw a "cursor" at (x,y), using a "yellow box".
1695  */
1696 static errr term_curs_win(int x, int y)
1697 {
1698     term_data *td = (term_data *)(Term->data);
1699     int tile_wid, tile_hgt;
1700     if (td->map_active) {
1701         tile_wid = td->map_tile_wid;
1702         tile_hgt = td->map_tile_hgt;
1703     } else {
1704         tile_wid = td->tile_wid;
1705         tile_hgt = td->tile_hgt;
1706     }
1707
1708     RECT rc;
1709     rc.left = x * tile_wid + td->size_ow1;
1710     rc.right = rc.left + tile_wid;
1711     rc.top = y * tile_hgt + td->size_oh1;
1712     rc.bottom = rc.top + tile_hgt;
1713
1714     HDC hdc = GetDC(td->w);
1715     FrameRect(hdc, &rc, hbrYellow);
1716     ReleaseDC(td->w, hdc);
1717     return 0;
1718 }
1719
1720 /*
1721  * Low level graphics (Assumes valid input).
1722  *
1723  * Draw a "big cursor" at (x,y), using a "yellow box".
1724  */
1725 static errr term_bigcurs_win(int x, int y)
1726 {
1727     term_data *td = (term_data *)(Term->data);
1728     int tile_wid, tile_hgt;
1729     if (td->map_active) {
1730         term_curs_win(x, y);
1731         return 0;
1732     } else {
1733         tile_wid = td->tile_wid;
1734         tile_hgt = td->tile_hgt;
1735     }
1736
1737     RECT rc;
1738     rc.left = x * tile_wid + td->size_ow1;
1739     rc.right = rc.left + 2 * tile_wid;
1740     rc.top = y * tile_hgt + td->size_oh1;
1741     rc.bottom = rc.top + tile_hgt;
1742
1743     HDC hdc = GetDC(td->w);
1744     FrameRect(hdc, &rc, hbrYellow);
1745     ReleaseDC(td->w, hdc);
1746     return 0;
1747 }
1748
1749 /*
1750  * Low level graphics (Assumes valid input).
1751  *
1752  * Erase a "block" of "n" characters starting at (x,y).
1753  */
1754 static errr term_wipe_win(int x, int y, int n)
1755 {
1756     term_data *td = (term_data *)(Term->data);
1757     RECT rc;
1758     rc.left = x * td->tile_wid + td->size_ow1;
1759     rc.right = rc.left + n * td->tile_wid;
1760     rc.top = y * td->tile_hgt + td->size_oh1;
1761     rc.bottom = rc.top + td->tile_hgt;
1762
1763     HDC hdc = GetDC(td->w);
1764     SetBkColor(hdc, RGB(0, 0, 0));
1765     SelectObject(hdc, td->font_id);
1766     if (use_bg)
1767         DrawBG(hdc, &rc);
1768     else
1769         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1770
1771     ReleaseDC(td->w, hdc);
1772     return 0;
1773 }
1774
1775 /*
1776  * Low level graphics.  Assumes valid input.
1777  *
1778  * Draw several ("n") chars, with an attr, at a given location.
1779  *
1780  * All "graphic" data is handled by "term_pict_win()", below.
1781  *
1782  * One would think there is a more efficient method for telling a window
1783  * what color it should be using to draw with, but perhaps simply changing
1784  * it every time is not too inefficient.
1785  */
1786 static errr term_text_win(int x, int y, int n, TERM_COLOR a, concptr s)
1787 {
1788     term_data *td = (term_data *)(Term->data);
1789     static HBITMAP WALL;
1790     static HBRUSH myBrush, oldBrush;
1791     static HPEN oldPen;
1792     static bool init_done = FALSE;
1793
1794     if (!init_done) {
1795         WALL = LoadBitmap(hInstance, AppName);
1796         myBrush = CreatePatternBrush(WALL);
1797         init_done = TRUE;
1798     }
1799
1800     RECT rc;
1801     rc.left = x * td->tile_wid + td->size_ow1;
1802     rc.right = rc.left + n * td->tile_wid;
1803     rc.top = y * td->tile_hgt + td->size_oh1;
1804     rc.bottom = rc.top + td->tile_hgt;
1805
1806     HDC hdc = GetDC(td->w);
1807     SetBkColor(hdc, RGB(0, 0, 0));
1808     if (colors16) {
1809         SetTextColor(hdc, PALETTEINDEX(win_pal[a]));
1810     } else if (paletted) {
1811         SetTextColor(hdc, win_clr[a & 0x0F]);
1812     } else {
1813         SetTextColor(hdc, win_clr[a]);
1814     }
1815
1816     SelectObject(hdc, td->font_id);
1817     if (use_bg)
1818         SetBkMode(hdc, TRANSPARENT);
1819
1820     if (td->bizarre || (td->tile_hgt != td->font_hgt) || (td->tile_wid != td->font_wid)) {
1821         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1822         if (use_bg)
1823             DrawBG(hdc, &rc);
1824
1825         rc.left += ((td->tile_wid - td->font_wid) / 2);
1826         rc.right = rc.left + td->font_wid;
1827         rc.top += ((td->tile_hgt - td->font_hgt) / 2);
1828         rc.bottom = rc.top + td->font_hgt;
1829
1830         for (int i = 0; i < n; i++) {
1831 #ifdef JP
1832             if (use_bigtile && *(s + i) == "■"[0] && *(s + i + 1) == "■"[1]) {
1833                 rc.right += td->font_wid;
1834                 oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1835                 oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1836                 Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1837                 SelectObject(hdc, oldBrush);
1838                 SelectObject(hdc, oldPen);
1839                 rc.right -= td->font_wid;
1840                 i++;
1841                 rc.left += 2 * td->tile_wid;
1842                 rc.right += 2 * td->tile_wid;
1843             } else if (iskanji(*(s + i))) /* 2バイト文字 */
1844             {
1845                 rc.right += td->font_wid;
1846                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 2, NULL);
1847                 rc.right -= td->font_wid;
1848                 i++;
1849                 rc.left += 2 * td->tile_wid;
1850                 rc.right += 2 * td->tile_wid;
1851             } else if (*(s + i) == 127) {
1852                 oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1853                 oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1854                 Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1855                 SelectObject(hdc, oldBrush);
1856                 SelectObject(hdc, oldPen);
1857                 rc.left += td->tile_wid;
1858                 rc.right += td->tile_wid;
1859             } else {
1860                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 1, NULL);
1861                 rc.left += td->tile_wid;
1862                 rc.right += td->tile_wid;
1863             }
1864 #else
1865             if (*(s + i) == 127) {
1866                 oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1867                 oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1868                 Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1869                 SelectObject(hdc, oldBrush);
1870                 SelectObject(hdc, oldPen);
1871                 rc.left += td->tile_wid;
1872                 rc.right += td->tile_wid;
1873             } else {
1874                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 1, NULL);
1875                 rc.left += td->tile_wid;
1876                 rc.right += td->tile_wid;
1877             }
1878 #endif
1879         }
1880     } else {
1881         ExtTextOut(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc, s, n, NULL);
1882     }
1883
1884     ReleaseDC(td->w, hdc);
1885     return 0;
1886 }
1887
1888 /*
1889  * Low level graphics.  Assumes valid input.
1890  *
1891  * Draw an array of "special" attr/char pairs at the given location.
1892  *
1893  * We use the "term_pict_win()" function for "graphic" data, which are
1894  * encoded by setting the "high-bits" of both the "attr" and the "char"
1895  * data.  We use the "attr" to represent the "row" of the main bitmap,
1896  * and the "char" to represent the "col" of the main bitmap.  The use
1897  * of this function is induced by the "higher_pict" flag.
1898  *
1899  * If "graphics" is not available, we simply "wipe" the given grids.
1900  */
1901 static errr term_pict_win(TERM_LEN x, TERM_LEN y, int n, const TERM_COLOR *ap, concptr cp, const TERM_COLOR *tap, concptr tcp)
1902 {
1903     term_data *td = (term_data *)(Term->data);
1904     int i;
1905     HDC hdcMask = NULL;
1906     if (!use_graphics) {
1907         return (term_wipe_win(x, y, n));
1908     }
1909
1910     TERM_LEN w1 = infGraph.CellWidth;
1911     TERM_LEN h1 = infGraph.CellHeight;
1912     TERM_LEN tw1 = infGraph.TileWidth;
1913     TERM_LEN th1 = infGraph.TileHeight;
1914     TERM_LEN w2, h2, tw2 = 0;
1915     if (td->map_active) {
1916         w2 = td->map_tile_wid;
1917         h2 = td->map_tile_hgt;
1918     } else {
1919         w2 = td->tile_wid;
1920         h2 = td->tile_hgt;
1921         tw2 = w2;
1922         if (use_bigtile)
1923             tw2 *= 2;
1924     }
1925
1926     TERM_LEN x2 = x * w2 + td->size_ow1 + infGraph.OffsetX;
1927     TERM_LEN y2 = y * h2 + td->size_oh1 + infGraph.OffsetY;
1928     HDC hdc = GetDC(td->w);
1929     HDC hdcSrc = CreateCompatibleDC(hdc);
1930     HBITMAP hbmSrcOld = static_cast<HBITMAP>(SelectObject(hdcSrc, infGraph.hBitmap));
1931
1932     if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND) {
1933         hdcMask = CreateCompatibleDC(hdc);
1934         SelectObject(hdcMask, infMask.hBitmap);
1935     }
1936
1937     for (i = 0; i < n; i++, x2 += w2) {
1938         TERM_COLOR a = ap[i];
1939         char c = cp[i];
1940         int row = (a & 0x7F);
1941         int col = (c & 0x7F);
1942         TERM_LEN x1 = col * w1;
1943         TERM_LEN y1 = row * h1;
1944
1945         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND) {
1946             TERM_LEN x3 = (tcp[i] & 0x7F) * w1;
1947             TERM_LEN y3 = (tap[i] & 0x7F) * h1;
1948             tw2 = tw2 * w1 / tw1;
1949             h2 = h2 * h1 / th1;
1950             if ((tw1 == tw2) && (th1 == h2)) {
1951                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
1952                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
1953                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
1954                 continue;
1955             }
1956
1957             SetStretchBltMode(hdc, COLORONCOLOR);
1958             StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x3, y3, w1, h1, SRCAND);
1959             StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCPAINT);
1960             if ((x1 != x3) || (y1 != y3)) {
1961                 StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
1962                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
1963             }
1964
1965             continue;
1966         }
1967
1968         if ((w1 == tw2) && (h1 == h2)) {
1969             BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
1970             continue;
1971         }
1972
1973         SetStretchBltMode(hdc, COLORONCOLOR);
1974         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
1975     }
1976
1977     SelectObject(hdcSrc, hbmSrcOld);
1978     DeleteDC(hdcSrc);
1979     if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND) {
1980         SelectObject(hdcMask, hbmSrcOld);
1981         DeleteDC(hdcMask);
1982     }
1983
1984     ReleaseDC(td->w, hdc);
1985     return 0;
1986 }
1987
1988 static void windows_map(player_type *player_ptr)
1989 {
1990     term_data *td = &data[0];
1991     TERM_COLOR ta;
1992     if (!use_graphics)
1993         return;
1994
1995     term_xtra_win_clear();
1996     td->map_tile_wid = (td->tile_wid * td->cols) / MAX_WID;
1997     td->map_tile_hgt = (td->tile_hgt * td->rows) / MAX_HGT;
1998     td->map_active = TRUE;
1999
2000     TERM_LEN min_x = 0;
2001     TERM_LEN min_y = 0;
2002     TERM_LEN max_x = player_ptr->current_floor_ptr->width;
2003     TERM_LEN max_y = player_ptr->current_floor_ptr->height;
2004
2005     char c;
2006     for (TERM_LEN x = min_x; x < max_x; x++) {
2007         for (TERM_LEN y = min_y; y < max_y; y++) {
2008             TERM_COLOR a;
2009             char tc;
2010             map_info(player_ptr, y, x, &a, (char *)&c, &ta, (char *)&tc);
2011             if ((a & 0x80) && (c & 0x80)) {
2012                 term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
2013             }
2014         }
2015     }
2016
2017     term_curs_win(player_ptr->x - min_x, player_ptr->y - min_y);
2018     term_inkey(&c, TRUE, TRUE);
2019     term_flush();
2020     td->map_active = FALSE;
2021     term_xtra_win_clear();
2022     term_redraw();
2023 }
2024
2025 /*
2026  * Create and initialize a "term_data" given a title
2027  */
2028 static void term_data_link(term_data *td)
2029 {
2030     term_type *t = &td->t;
2031     term_init(t, td->cols, td->rows, FILE_READ_BUFF_SIZE);
2032     t->soft_cursor = TRUE;
2033     t->higher_pict = TRUE;
2034     t->attr_blank = TERM_WHITE;
2035     t->char_blank = ' ';
2036     t->user_hook = term_user_win;
2037     t->xtra_hook = term_xtra_win;
2038     t->curs_hook = term_curs_win;
2039     t->bigcurs_hook = term_bigcurs_win;
2040     t->wipe_hook = term_wipe_win;
2041     t->text_hook = term_text_win;
2042     t->pict_hook = term_pict_win;
2043     t->data = (vptr)(td);
2044 }
2045
2046 /*
2047  * Create the windows
2048  *
2049  * First, instantiate the "default" values, then read the "ini_file"
2050  * to over-ride selected values, then create the windows, and fonts.
2051  *
2052  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
2053  * must make active to realize the palette.
2054  */
2055 static void init_windows(void)
2056 {
2057     term_data *td;
2058     td = &data[0];
2059     WIPE(td, term_data);
2060 #ifdef JP
2061     td->s = "変愚蛮怒";
2062 #else
2063     td->s = angband_term_name[0];
2064 #endif
2065
2066     td->keys = 1024;
2067     td->rows = 24;
2068     td->cols = 80;
2069     td->visible = TRUE;
2070     td->size_ow1 = 2;
2071     td->size_ow2 = 2;
2072     td->size_oh1 = 2;
2073     td->size_oh2 = 2;
2074     td->pos_x = 7 * 30;
2075     td->pos_y = 7 * 20;
2076     td->posfix = FALSE;
2077     td->bizarre = TRUE;
2078
2079     for (int i = 1; i < MAX_TERM_DATA; i++) {
2080         td = &data[i];
2081         WIPE(td, term_data);
2082         td->s = angband_term_name[i];
2083         td->keys = 16;
2084         td->rows = 24;
2085         td->cols = 80;
2086         td->visible = FALSE;
2087         td->size_ow1 = 1;
2088         td->size_ow2 = 1;
2089         td->size_oh1 = 1;
2090         td->size_oh2 = 1;
2091         td->pos_x = (7 - i) * 30;
2092         td->pos_y = (7 - i) * 20;
2093         td->posfix = FALSE;
2094         td->bizarre = TRUE;
2095     }
2096
2097     load_prefs();
2098
2099     /* Atrributes of main window */
2100     td = &data[0];
2101     td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_VISIBLE);
2102     td->dwExStyle = 0;
2103     td->visible = TRUE;
2104
2105     /* Attributes of sub windows */
2106     for (int i = 1; i < MAX_TERM_DATA; i++) {
2107         td = &data[i];
2108         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
2109         td->dwExStyle = (WS_EX_TOOLWINDOW);
2110     }
2111
2112     /* Font of each window */
2113     for (int i = 0; i < MAX_TERM_DATA; i++) {
2114         td = &data[i];
2115         strncpy(td->lf.lfFaceName, td->font_want, LF_FACESIZE);
2116         td->lf.lfCharSet = DEFAULT_CHARSET;
2117         td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
2118         term_force_font(td, NULL);
2119         if (!td->tile_wid)
2120             td->tile_wid = td->font_wid;
2121         if (!td->tile_hgt)
2122             td->tile_hgt = td->font_hgt;
2123         term_getsize(td);
2124         term_window_resize(td);
2125     }
2126
2127     /* Create sub windows */
2128     for (int i = MAX_TERM_DATA - 1; i >= 1; --i) {
2129         td = &data[i];
2130
2131         my_td = td;
2132         td->w
2133             = CreateWindowEx(td->dwExStyle, AngList, td->s, td->dwStyle, td->pos_x, td->pos_y, td->size_wid, td->size_hgt, HWND_DESKTOP, NULL, hInstance, NULL);
2134         my_td = NULL;
2135
2136         if (!td->w)
2137             quit(_("サブウィンドウに作成に失敗しました", "Failed to create sub-window"));
2138
2139         td->size_hack = TRUE;
2140         term_getsize(td);
2141         term_window_resize(td);
2142
2143         if (td->visible) {
2144             ShowWindow(td->w, SW_SHOW);
2145         }
2146         td->size_hack = FALSE;
2147
2148         term_data_link(td);
2149         angband_term[i] = &td->t;
2150
2151         if (td->visible) {
2152             /* Activate the window */
2153             SetActiveWindow(td->w);
2154         }
2155
2156         if (td->posfix) {
2157             term_window_pos(td, HWND_TOPMOST);
2158         } else {
2159             term_window_pos(td, td->w);
2160         }
2161     }
2162
2163     /* Create main window */
2164     td = &data[0];
2165     my_td = td;
2166     td->w = CreateWindowEx(td->dwExStyle, AppName, td->s, td->dwStyle, td->pos_x, td->pos_y, td->size_wid, td->size_hgt, HWND_DESKTOP, NULL, hInstance, NULL);
2167     my_td = NULL;
2168
2169     if (!td->w)
2170         quit(_("メインウィンドウの作成に失敗しました", "Failed to create Angband window"));
2171
2172     /* Resize */
2173     td->size_hack = TRUE;
2174     term_getsize(td);
2175     term_window_resize(td);
2176     td->size_hack = FALSE;
2177
2178     term_data_link(td);
2179     angband_term[0] = &td->t;
2180     normsize.x = td->cols;
2181     normsize.y = td->rows;
2182
2183     if (win_maximized)
2184         ShowWindow(td->w, SW_SHOWMAXIMIZED);
2185     else
2186         ShowWindow(td->w, SW_SHOW);
2187
2188     SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
2189     (void)new_palette();
2190     hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
2191     (void)term_xtra_win_flush();
2192 }
2193
2194 /*
2195  * Prepare the menus
2196  */
2197 static void setup_menus(void)
2198 {
2199     HMENU hm = GetMenu(data[0].w);
2200     EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2201     EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2202     EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2203     EnableMenuItem(hm, IDM_FILE_EXIT, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2204     EnableMenuItem(hm, IDM_FILE_SCORE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2205
2206     if (!current_world_ptr->character_generated) {
2207         EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
2208         EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
2209     }
2210
2211     if (current_world_ptr->character_generated) {
2212         EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_ENABLED);
2213     }
2214
2215     EnableMenuItem(hm, IDM_FILE_EXIT, MF_BYCOMMAND | MF_ENABLED);
2216     EnableMenuItem(hm, IDM_FILE_SCORE, MF_BYCOMMAND | MF_ENABLED);
2217
2218     for (int i = 0; i < MAX_TERM_DATA; i++) {
2219         EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2220         CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i, (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
2221         EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i, MF_BYCOMMAND | MF_ENABLED);
2222     }
2223
2224     for (int i = 0; i < MAX_TERM_DATA; i++) {
2225         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2226
2227         if (data[i].visible) {
2228             EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i, MF_BYCOMMAND | MF_ENABLED);
2229         }
2230     }
2231
2232     for (int i = 0; i < MAX_TERM_DATA; i++) {
2233         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2234         CheckMenuItem(hm, IDM_WINDOW_POS_0 + i, (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
2235         if (data[i].visible) {
2236             EnableMenuItem(hm, IDM_WINDOW_POS_0 + i, MF_BYCOMMAND | MF_ENABLED);
2237         }
2238     }
2239
2240     for (int i = 0; i < MAX_TERM_DATA; i++) {
2241         EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2242         CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i, (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
2243         if (data[i].visible) {
2244             EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i, MF_BYCOMMAND | MF_ENABLED);
2245         }
2246     }
2247
2248     for (int i = 0; i < MAX_TERM_DATA; i++) {
2249         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2250         if (data[i].visible) {
2251             EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i, MF_BYCOMMAND | MF_ENABLED);
2252         }
2253     }
2254
2255     for (int i = 0; i < MAX_TERM_DATA; i++) {
2256         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2257         if (data[i].visible) {
2258             EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i, MF_BYCOMMAND | MF_ENABLED);
2259         }
2260     }
2261
2262     for (int i = 0; i < MAX_TERM_DATA; i++) {
2263         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2264         if (data[i].visible) {
2265             EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i, MF_BYCOMMAND | MF_ENABLED);
2266         }
2267     }
2268
2269     for (int i = 0; i < MAX_TERM_DATA; i++) {
2270         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2271
2272         if (data[i].visible) {
2273             EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i, MF_BYCOMMAND | MF_ENABLED);
2274         }
2275     }
2276     EnableMenuItem(hm, IDM_WINDOW_KEEP_SUBWINDOWS, MF_BYCOMMAND | MF_ENABLED);
2277     CheckMenuItem(hm, IDM_WINDOW_KEEP_SUBWINDOWS, (keep_subwindows ? MF_CHECKED : MF_UNCHECKED));
2278
2279     EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2280     EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2281     EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2282     EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2283     EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2284 #ifdef JP
2285 #else
2286     EnableMenuItem(hm, IDM_OPTIONS_SAVER, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2287 #endif
2288
2289     if (use_graphics != GRAPHICS_NONE)
2290         EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_ENABLED);
2291     else
2292         EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
2293
2294     CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, (arg_graphics == GRAPHICS_NONE ? MF_CHECKED : MF_UNCHECKED));
2295     CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, (arg_graphics == GRAPHICS_ORIGINAL ? MF_CHECKED : MF_UNCHECKED));
2296     CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, (arg_graphics == GRAPHICS_ADAM_BOLT ? MF_CHECKED : MF_UNCHECKED));
2297     CheckMenuItem(hm, IDM_OPTIONS_NEW2_GRAPHICS, (arg_graphics == GRAPHICS_HENGBAND ? MF_CHECKED : MF_UNCHECKED));
2298     CheckMenuItem(hm, IDM_OPTIONS_BIGTILE, (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
2299     CheckMenuItem(hm, IDM_OPTIONS_MUSIC, (arg_music ? MF_CHECKED : MF_UNCHECKED));
2300     CheckMenuItem(hm, IDM_OPTIONS_SOUND, (arg_sound ? MF_CHECKED : MF_UNCHECKED));
2301     CheckMenuItem(hm, IDM_OPTIONS_BG, (use_bg ? MF_CHECKED : MF_UNCHECKED));
2302 #ifdef JP
2303 #else
2304     CheckMenuItem(hm, IDM_OPTIONS_SAVER, (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
2305 #endif
2306     EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_ENABLED);
2307     EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_ENABLED);
2308     EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_ENABLED);
2309     EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_ENABLED);
2310     EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_ENABLED);
2311     EnableMenuItem(hm, IDM_OPTIONS_SAVER, MF_BYCOMMAND | MF_ENABLED);
2312 }
2313
2314 /*
2315  * Check for double clicked (or dragged) savefile
2316  *
2317  * Apparently, Windows copies the entire filename into the first
2318  * piece of the "command line string".  Perhaps we should extract
2319  * the "basename" of that filename and append it to the "save" dir.
2320  */
2321 static void check_for_save_file(player_type *player_ptr, LPSTR cmd_line)
2322 {
2323     char *s;
2324     s = cmd_line;
2325     if (!*s)
2326         return;
2327
2328     strcpy(savefile, s);
2329     validate_file(savefile);
2330     game_in_progress = TRUE;
2331     play_game(player_ptr, FALSE, FALSE);
2332 }
2333
2334 /*
2335  * Process a menu command
2336  */
2337 static void process_menus(player_type *player_ptr, WORD wCmd)
2338 {
2339     term_data *td;
2340     OPENFILENAME ofn;
2341     switch (wCmd) {
2342     case IDM_FILE_NEW: {
2343         if (!initialized) {
2344             plog(_("まだ初期化中です...", "You cannot do that yet..."));
2345         } else if (game_in_progress) {
2346             plog(_("プレイ中は新しいゲームを始めることができません!", "You can't start a new game while you're still playing!"));
2347         } else {
2348             game_in_progress = TRUE;
2349             term_flush();
2350             strcpy(savefile, "");
2351             play_game(player_ptr, TRUE, FALSE);
2352             quit(NULL);
2353         }
2354
2355         break;
2356     }
2357     case IDM_FILE_OPEN: {
2358         if (!initialized) {
2359             plog(_("まだ初期化中です...", "You cannot do that yet..."));
2360         } else if (game_in_progress) {
2361             plog(_("プレイ中はゲームをロードすることができません!", "You can't open a new game while you're still playing!"));
2362         } else {
2363             memset(&ofn, 0, sizeof(ofn));
2364             ofn.lStructSize = sizeof(ofn);
2365             ofn.hwndOwner = data[0].w;
2366             ofn.lpstrFilter = "Save Files (*.)\0*\0";
2367             ofn.nFilterIndex = 1;
2368             ofn.lpstrFile = savefile;
2369             ofn.nMaxFile = 1024;
2370             ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
2371             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
2372
2373             if (GetOpenFileName(&ofn)) {
2374                 validate_file(savefile);
2375                 game_in_progress = TRUE;
2376                 term_flush();
2377                 play_game(player_ptr, FALSE, FALSE);
2378                 quit(NULL);
2379             }
2380         }
2381
2382         break;
2383     }
2384     case IDM_FILE_SAVE: {
2385         if (game_in_progress && current_world_ptr->character_generated) {
2386             if (!can_save) {
2387                 plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
2388                 break;
2389             }
2390
2391             msg_flag = FALSE;
2392             do_cmd_save_game(player_ptr, FALSE);
2393         } else {
2394             plog(_("今、セーブすることは出来ません。", "You may not do that right now."));
2395         }
2396
2397         break;
2398     }
2399     case IDM_FILE_EXIT: {
2400         if (game_in_progress && current_world_ptr->character_generated) {
2401             if (!can_save) {
2402                 plog(_("今は終了できません。", "You may not do that right now."));
2403                 break;
2404             }
2405
2406             msg_flag = FALSE;
2407             forget_lite(player_ptr->current_floor_ptr);
2408             forget_view(player_ptr->current_floor_ptr);
2409             clear_mon_lite(player_ptr->current_floor_ptr);
2410
2411             term_key_push(SPECIAL_KEY_QUIT);
2412             break;
2413         }
2414
2415         quit(NULL);
2416         break;
2417     }
2418     case IDM_FILE_SCORE: {
2419         char buf[1024];
2420         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
2421         highscore_fd = fd_open(buf, O_RDONLY);
2422         if (highscore_fd < 0) {
2423             msg_print("Score file unavailable.");
2424         } else {
2425             screen_save();
2426             term_clear();
2427             display_scores_aux(0, MAX_HISCORES, -1, NULL);
2428             (void)fd_close(highscore_fd);
2429             highscore_fd = -1;
2430             screen_load();
2431             term_fresh();
2432         }
2433
2434         break;
2435     }
2436     case IDM_FILE_MOVIE: {
2437         if (!initialized) {
2438             plog(_("まだ初期化中です...", "You cannot do that yet..."));
2439         } else if (game_in_progress) {
2440             plog(_("プレイ中はムービーをロードすることができません!", "You can't open a movie while you're playing!"));
2441         } else {
2442             memset(&ofn, 0, sizeof(ofn));
2443             ofn.lStructSize = sizeof(ofn);
2444             ofn.hwndOwner = data[0].w;
2445             ofn.lpstrFilter = "Angband Movie Files (*.amv)\0*.amv\0";
2446             ofn.nFilterIndex = 1;
2447             ofn.lpstrFile = savefile;
2448             ofn.nMaxFile = 1024;
2449             ofn.lpstrInitialDir = ANGBAND_DIR_USER;
2450             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
2451
2452             if (GetOpenFileName(&ofn)) {
2453                 prepare_browse_movie_without_path_build(savefile);
2454                 play_game(player_ptr, FALSE, TRUE);
2455                 quit(NULL);
2456                 return;
2457             }
2458         }
2459
2460         break;
2461     }
2462     case IDM_WINDOW_VIS_0: {
2463         plog(_("メインウィンドウは非表示にできません!", "You are not allowed to do that!"));
2464         break;
2465     }
2466     case IDM_WINDOW_VIS_1:
2467     case IDM_WINDOW_VIS_2:
2468     case IDM_WINDOW_VIS_3:
2469     case IDM_WINDOW_VIS_4:
2470     case IDM_WINDOW_VIS_5:
2471     case IDM_WINDOW_VIS_6:
2472     case IDM_WINDOW_VIS_7: {
2473         int i = wCmd - IDM_WINDOW_VIS_0;
2474         if ((i < 0) || (i >= MAX_TERM_DATA))
2475             break;
2476
2477         td = &data[i];
2478         if (!td->visible) {
2479             td->visible = TRUE;
2480             ShowWindow(td->w, SW_SHOW);
2481             term_data_redraw(player_ptr, td);
2482         } else {
2483             td->visible = FALSE;
2484             td->posfix = FALSE;
2485             ShowWindow(td->w, SW_HIDE);
2486         }
2487
2488         break;
2489     }
2490     case IDM_WINDOW_FONT_0:
2491     case IDM_WINDOW_FONT_1:
2492     case IDM_WINDOW_FONT_2:
2493     case IDM_WINDOW_FONT_3:
2494     case IDM_WINDOW_FONT_4:
2495     case IDM_WINDOW_FONT_5:
2496     case IDM_WINDOW_FONT_6:
2497     case IDM_WINDOW_FONT_7: {
2498         int i = wCmd - IDM_WINDOW_FONT_0;
2499         if ((i < 0) || (i >= MAX_TERM_DATA))
2500             break;
2501
2502         td = &data[i];
2503         term_change_font(td);
2504         break;
2505     }
2506     case IDM_WINDOW_POS_1:
2507     case IDM_WINDOW_POS_2:
2508     case IDM_WINDOW_POS_3:
2509     case IDM_WINDOW_POS_4:
2510     case IDM_WINDOW_POS_5:
2511     case IDM_WINDOW_POS_6:
2512     case IDM_WINDOW_POS_7: {
2513         int i = wCmd - IDM_WINDOW_POS_0;
2514         if ((i < 0) || (i >= MAX_TERM_DATA))
2515             break;
2516
2517         td = &data[i];
2518         if (!td->posfix && td->visible) {
2519             td->posfix = TRUE;
2520             term_window_pos(td, HWND_TOPMOST);
2521         } else {
2522             td->posfix = FALSE;
2523             term_window_pos(td, data[0].w);
2524         }
2525
2526         break;
2527     }
2528     case IDM_WINDOW_BIZ_0:
2529     case IDM_WINDOW_BIZ_1:
2530     case IDM_WINDOW_BIZ_2:
2531     case IDM_WINDOW_BIZ_3:
2532     case IDM_WINDOW_BIZ_4:
2533     case IDM_WINDOW_BIZ_5:
2534     case IDM_WINDOW_BIZ_6:
2535     case IDM_WINDOW_BIZ_7: {
2536         int i = wCmd - IDM_WINDOW_BIZ_0;
2537         if ((i < 0) || (i >= MAX_TERM_DATA))
2538             break;
2539
2540         td = &data[i];
2541         td->bizarre = !td->bizarre;
2542         term_getsize(td);
2543         term_window_resize(td);
2544         break;
2545     }
2546     case IDM_WINDOW_I_WID_0:
2547     case IDM_WINDOW_I_WID_1:
2548     case IDM_WINDOW_I_WID_2:
2549     case IDM_WINDOW_I_WID_3:
2550     case IDM_WINDOW_I_WID_4:
2551     case IDM_WINDOW_I_WID_5:
2552     case IDM_WINDOW_I_WID_6:
2553     case IDM_WINDOW_I_WID_7: {
2554         int i = wCmd - IDM_WINDOW_I_WID_0;
2555         if ((i < 0) || (i >= MAX_TERM_DATA))
2556             break;
2557
2558         td = &data[i];
2559         td->tile_wid += 1;
2560         term_getsize(td);
2561         term_window_resize(td);
2562         break;
2563     }
2564     case IDM_WINDOW_D_WID_0:
2565     case IDM_WINDOW_D_WID_1:
2566     case IDM_WINDOW_D_WID_2:
2567     case IDM_WINDOW_D_WID_3:
2568     case IDM_WINDOW_D_WID_4:
2569     case IDM_WINDOW_D_WID_5:
2570     case IDM_WINDOW_D_WID_6:
2571     case IDM_WINDOW_D_WID_7: {
2572         int i = wCmd - IDM_WINDOW_D_WID_0;
2573         if ((i < 0) || (i >= MAX_TERM_DATA))
2574             break;
2575
2576         td = &data[i];
2577         td->tile_wid -= 1;
2578         term_getsize(td);
2579         term_window_resize(td);
2580         break;
2581     }
2582     case IDM_WINDOW_I_HGT_0:
2583     case IDM_WINDOW_I_HGT_1:
2584     case IDM_WINDOW_I_HGT_2:
2585     case IDM_WINDOW_I_HGT_3:
2586     case IDM_WINDOW_I_HGT_4:
2587     case IDM_WINDOW_I_HGT_5:
2588     case IDM_WINDOW_I_HGT_6:
2589     case IDM_WINDOW_I_HGT_7: {
2590         int i = wCmd - IDM_WINDOW_I_HGT_0;
2591         if ((i < 0) || (i >= MAX_TERM_DATA))
2592             break;
2593
2594         td = &data[i];
2595         td->tile_hgt += 1;
2596         term_getsize(td);
2597         term_window_resize(td);
2598         break;
2599     }
2600     case IDM_WINDOW_D_HGT_0:
2601     case IDM_WINDOW_D_HGT_1:
2602     case IDM_WINDOW_D_HGT_2:
2603     case IDM_WINDOW_D_HGT_3:
2604     case IDM_WINDOW_D_HGT_4:
2605     case IDM_WINDOW_D_HGT_5:
2606     case IDM_WINDOW_D_HGT_6:
2607     case IDM_WINDOW_D_HGT_7: {
2608         int i = wCmd - IDM_WINDOW_D_HGT_0;
2609         if ((i < 0) || (i >= MAX_TERM_DATA))
2610             break;
2611
2612         td = &data[i];
2613         td->tile_hgt -= 1;
2614         term_getsize(td);
2615         term_window_resize(td);
2616         break;
2617     }
2618     case IDM_WINDOW_KEEP_SUBWINDOWS: {
2619         keep_subwindows = !keep_subwindows;
2620         break;
2621     }
2622     case IDM_OPTIONS_NO_GRAPHICS: {
2623         if (!inkey_flag) {
2624             plog("You may not do that right now.");
2625             break;
2626         }
2627
2628         if (arg_graphics != GRAPHICS_NONE) {
2629             arg_graphics = GRAPHICS_NONE;
2630             term_xtra_win_react(player_ptr);
2631             term_key_push(KTRL('R'));
2632         }
2633
2634         break;
2635     }
2636     case IDM_OPTIONS_OLD_GRAPHICS: {
2637         if (!inkey_flag) {
2638             plog("You may not do that right now.");
2639             break;
2640         }
2641
2642         if (arg_graphics != GRAPHICS_ORIGINAL) {
2643             arg_graphics = GRAPHICS_ORIGINAL;
2644             term_xtra_win_react(player_ptr);
2645             term_key_push(KTRL('R'));
2646         }
2647
2648         break;
2649     }
2650     case IDM_OPTIONS_NEW_GRAPHICS: {
2651         if (!inkey_flag) {
2652             plog("You may not do that right now.");
2653             break;
2654         }
2655
2656         if (arg_graphics != GRAPHICS_ADAM_BOLT) {
2657             arg_graphics = GRAPHICS_ADAM_BOLT;
2658             term_xtra_win_react(player_ptr);
2659             term_key_push(KTRL('R'));
2660         }
2661
2662         break;
2663     }
2664     case IDM_OPTIONS_NEW2_GRAPHICS: {
2665         if (!inkey_flag) {
2666             plog("You may not do that right now.");
2667             break;
2668         }
2669
2670         if (arg_graphics != GRAPHICS_HENGBAND) {
2671             arg_graphics = GRAPHICS_HENGBAND;
2672             term_xtra_win_react(player_ptr);
2673             term_key_push(KTRL('R'));
2674         }
2675
2676         break;
2677     }
2678     case IDM_OPTIONS_BIGTILE: {
2679         td = &data[0];
2680         if (!inkey_flag) {
2681             plog("You may not do that right now.");
2682             break;
2683         }
2684
2685         arg_bigtile = !arg_bigtile;
2686         term_activate(&td->t);
2687         term_resize(td->cols, td->rows);
2688         InvalidateRect(td->w, NULL, TRUE);
2689         break;
2690     }
2691     case IDM_OPTIONS_MUSIC: {
2692         if (!inkey_flag) {
2693             plog("You may not do that right now.");
2694             break;
2695         }
2696
2697         arg_music = !arg_music;
2698         term_xtra_win_react(player_ptr);
2699         term_key_push(KTRL('R'));
2700         break;
2701     }
2702     case IDM_OPTIONS_SOUND: {
2703         if (!inkey_flag) {
2704             plog("You may not do that right now.");
2705             break;
2706         }
2707
2708         arg_sound = !arg_sound;
2709         term_xtra_win_react(player_ptr);
2710         term_key_push(KTRL('R'));
2711         break;
2712     }
2713     case IDM_OPTIONS_BG: {
2714         if (!inkey_flag) {
2715             plog("You may not do that right now.");
2716             break;
2717         }
2718
2719         use_bg = !use_bg;
2720         init_bg();
2721         term_xtra_win_react(player_ptr);
2722         term_key_push(KTRL('R'));
2723         break;
2724     }
2725     case IDM_OPTIONS_OPEN_BG: {
2726         if (!inkey_flag) {
2727             plog("You may not do that right now.");
2728             break;
2729         }
2730
2731         memset(&ofn, 0, sizeof(ofn));
2732         ofn.lStructSize = sizeof(ofn);
2733         ofn.hwndOwner = data[0].w;
2734         ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0";
2735         ofn.nFilterIndex = 1;
2736         ofn.lpstrFile = bg_bitmap_file;
2737         ofn.nMaxFile = 1023;
2738         ofn.lpstrInitialDir = NULL;
2739         ofn.lpstrTitle = _("壁紙を選んでね。", "Choose wall paper.");
2740         ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
2741
2742         if (GetOpenFileName(&ofn)) {
2743             use_bg = 1;
2744             init_bg();
2745         }
2746
2747         term_xtra_win_react(player_ptr);
2748         term_key_push(KTRL('R'));
2749         break;
2750     }
2751     case IDM_DUMP_SCREEN_HTML: {
2752         static char buf[1024] = "";
2753         memset(&ofn, 0, sizeof(ofn));
2754         ofn.lStructSize = sizeof(ofn);
2755         ofn.hwndOwner = data[0].w;
2756         ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0";
2757         ofn.nFilterIndex = 1;
2758         ofn.lpstrFile = buf;
2759         ofn.nMaxFile = 1023;
2760         ofn.lpstrDefExt = "html";
2761         ofn.lpstrInitialDir = NULL;
2762         ofn.lpstrTitle = _("HTMLでスクリーンダンプを保存", "Save screen dump as HTML.");
2763         ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
2764
2765         if (GetSaveFileName(&ofn)) {
2766             do_cmd_save_screen_html_aux(buf, 0);
2767         }
2768
2769         break;
2770     }
2771     case IDM_OPTIONS_SAVER: {
2772         if (hwndSaver) {
2773             DestroyWindow(hwndSaver);
2774             hwndSaver = NULL;
2775             break;
2776         }
2777
2778         hwndSaver = CreateWindowEx(WS_EX_TOPMOST, "WindowsScreenSaverClass", "Angband Screensaver", WS_POPUP | WS_MAXIMIZE | WS_VISIBLE, 0, 0,
2779             GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL);
2780
2781         if (hwndSaver) {
2782             SetWindowPos(hwndSaver, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
2783         } else {
2784             plog(_("ウィンドウを作成出来ません", "Failed to create saver window"));
2785         }
2786
2787         break;
2788     }
2789     case IDM_OPTIONS_MAP: {
2790         windows_map(player_ptr);
2791         break;
2792     }
2793
2794     case IDM_HELP_CONTENTS: {
2795         char buf[1024];
2796         char tmp[1024];
2797         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.hlp");
2798         if (check_file(tmp)) {
2799             sprintf(buf, "winhelp.exe %s", tmp);
2800             WinExec(buf, SW_NORMAL);
2801             break;
2802         }
2803
2804         plog_fmt(_("ヘルプファイル[%s]が見付かりません。", "Cannot find help file: %s"), tmp);
2805         plog(_("代わりにオンラインヘルプを使用してください。", "Use the online help files instead."));
2806         break;
2807     }
2808     }
2809 }
2810
2811 /*
2812  * Add a keypress to the "queue"
2813  */
2814 static errr term_keypress(int k)
2815 {
2816     /* Refuse to enqueue non-keys */
2817     if (!k)
2818         return -1;
2819
2820     /* Store the char, advance the queue */
2821     Term->key_queue[Term->key_head++] = (char)k;
2822
2823     /* Circular queue, handle wrap */
2824     if (Term->key_head == Term->key_size)
2825         Term->key_head = 0;
2826
2827     if (Term->key_head != Term->key_tail)
2828         return 0;
2829
2830     return 1;
2831 }
2832
2833 static bool process_keydown(WPARAM wParam, LPARAM lParam)
2834 {
2835     bool mc = FALSE;
2836     bool ms = FALSE;
2837     bool ma = FALSE;
2838
2839     if (GetKeyState(VK_CONTROL) & 0x8000)
2840         mc = TRUE;
2841     if (GetKeyState(VK_SHIFT) & 0x8000)
2842         ms = TRUE;
2843     if (GetKeyState(VK_MENU) & 0x8000)
2844         ma = TRUE;
2845
2846     term_no_press = (ma) ? TRUE : FALSE;
2847     if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)])) {
2848         bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
2849         bool numpad = FALSE;
2850
2851         term_keypress(31);
2852         if (mc)
2853             term_keypress('C');
2854         if (ms)
2855             term_keypress('S');
2856         if (ma)
2857             term_keypress('A');
2858
2859         int i = LOBYTE(HIWORD(lParam));
2860         term_keypress('x');
2861         switch (wParam) {
2862         case VK_DIVIDE:
2863             term_no_press = TRUE;
2864             [[fallthrough]]; /* Fall through */
2865         case VK_RETURN:
2866             numpad = ext_key;
2867             break;
2868         case VK_NUMPAD0:
2869         case VK_NUMPAD1:
2870         case VK_NUMPAD2:
2871         case VK_NUMPAD3:
2872         case VK_NUMPAD4:
2873         case VK_NUMPAD5:
2874         case VK_NUMPAD6:
2875         case VK_NUMPAD7:
2876         case VK_NUMPAD8:
2877         case VK_NUMPAD9:
2878         case VK_ADD:
2879         case VK_MULTIPLY:
2880         case VK_SUBTRACT:
2881         case VK_SEPARATOR:
2882         case VK_DECIMAL:
2883             term_no_press = TRUE;
2884             [[fallthrough]]; /* Fall through */
2885         case VK_CLEAR:
2886         case VK_HOME:
2887         case VK_END:
2888         case VK_PRIOR:
2889         case VK_NEXT:
2890         case VK_INSERT:
2891         case VK_DELETE:
2892         case VK_UP:
2893         case VK_DOWN:
2894         case VK_LEFT:
2895         case VK_RIGHT:
2896             numpad = !ext_key;
2897         }
2898
2899         if (numpad)
2900             term_keypress('K');
2901
2902         term_keypress(hexsym[i / 16]);
2903         term_keypress(hexsym[i % 16]);
2904         term_keypress(13);
2905
2906         return 1;
2907     }
2908
2909     return 0;
2910 }
2911
2912 /*!
2913  * todo WNDCLASSに影響があるのでplayer_type*の追加は保留
2914  */
2915 LRESULT PASCAL AngbandWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2916 {
2917     PAINTSTRUCT ps;
2918     term_data *td;
2919     td = (term_data *)GetWindowLong(hWnd, 0);
2920
2921     switch (uMsg) {
2922     case WM_NCCREATE: {
2923         SetWindowLong(hWnd, 0, (LONG)(my_td));
2924         break;
2925     }
2926     case WM_CREATE: {
2927         mop.dwCallback = (DWORD)hWnd;
2928         return 0;
2929     }
2930     case WM_GETMINMAXINFO: {
2931         MINMAXINFO *lpmmi;
2932         RECT rc;
2933
2934         lpmmi = (MINMAXINFO *)lParam;
2935         if (!td)
2936             return 1;
2937
2938         rc.left = rc.top = 0;
2939         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
2940         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
2941
2942         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
2943
2944         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
2945         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
2946
2947         return 0;
2948     }
2949     case WM_PAINT: {
2950         BeginPaint(hWnd, &ps);
2951         if (td)
2952             term_data_redraw(p_ptr, td);
2953         EndPaint(hWnd, &ps);
2954         ValidateRect(hWnd, NULL);
2955         return 0;
2956     }
2957     case MM_MCINOTIFY: {
2958         if (wParam == MCI_NOTIFY_SUCCESSFUL) {
2959             mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
2960             mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
2961         }
2962
2963         return 0;
2964     }
2965     case WM_SYSKEYDOWN:
2966     case WM_KEYDOWN: {
2967         if (process_keydown(wParam, lParam))
2968             return 0;
2969         break;
2970     }
2971     case WM_CHAR: {
2972         if (term_no_press)
2973             term_no_press = FALSE;
2974         else
2975             term_keypress(wParam);
2976         return 0;
2977     }
2978     case WM_LBUTTONDOWN: {
2979         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
2980         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
2981         mouse_down = TRUE;
2982         oldx = mousex;
2983         oldy = mousey;
2984         return 0;
2985     }
2986     case WM_LBUTTONUP: {
2987         HGLOBAL hGlobal;
2988         LPSTR lpStr;
2989         TERM_LEN dx = abs(oldx - mousex) + 1;
2990         TERM_LEN dy = abs(oldy - mousey) + 1;
2991         TERM_LEN ox = (oldx > mousex) ? mousex : oldx;
2992         TERM_LEN oy = (oldy > mousey) ? mousey : oldy;
2993
2994         mouse_down = FALSE;
2995         paint_rect = FALSE;
2996
2997 #ifdef JP
2998         int sz = (dx + 3) * dy;
2999 #else
3000         int sz = (dx + 2) * dy;
3001 #endif
3002         hGlobal = GlobalAlloc(GHND, sz + 1);
3003         if (hGlobal == NULL)
3004             return 0;
3005         lpStr = (LPSTR)GlobalLock(hGlobal);
3006
3007         for (int i = 0; i < dy; i++) {
3008 #ifdef JP
3009             char *s;
3010             char **scr = data[0].t.scr->c;
3011
3012             C_MAKE(s, (dx + 1), char);
3013             strncpy(s, &scr[oy + i][ox], dx);
3014
3015             if (ox > 0) {
3016                 if (iskanji(scr[oy + i][ox - 1]))
3017                     s[0] = ' ';
3018             }
3019
3020             if (ox + dx < data[0].cols) {
3021                 if (iskanji(scr[oy + i][ox + dx - 1]))
3022                     s[dx - 1] = ' ';
3023             }
3024
3025             for (int j = 0; j < dx; j++) {
3026                 if (s[j] == 127)
3027                     s[j] = '#';
3028                 *lpStr++ = s[j];
3029             }
3030 #else
3031             for (int j = 0; j < dx; j++) {
3032                 *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
3033             }
3034 #endif
3035             if (dy > 1) {
3036                 *lpStr++ = '\r';
3037                 *lpStr++ = '\n';
3038             }
3039         }
3040
3041         GlobalUnlock(hGlobal);
3042         if (OpenClipboard(hWnd) == 0) {
3043             GlobalFree(hGlobal);
3044             return 0;
3045         }
3046
3047         EmptyClipboard();
3048         SetClipboardData(CF_TEXT, hGlobal);
3049         CloseClipboard();
3050         term_redraw();
3051         return 0;
3052     }
3053     case WM_MOUSEMOVE: {
3054         if (!mouse_down)
3055             return 0;
3056
3057         int dx, dy;
3058         int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
3059         int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
3060         int ox, oy;
3061
3062         if (paint_rect) {
3063             dx = abs(oldx - mousex) + 1;
3064             dy = abs(oldy - mousey) + 1;
3065             ox = (oldx > mousex) ? mousex : oldx;
3066             oy = (oldy > mousey) ? mousey : oldy;
3067             term_inversed_area(hWnd, ox, oy, dx, dy);
3068         } else {
3069             paint_rect = TRUE;
3070         }
3071
3072         dx = abs(cx - mousex) + 1;
3073         dy = abs(cy - mousey) + 1;
3074         ox = (cx > mousex) ? mousex : cx;
3075         oy = (cy > mousey) ? mousey : cy;
3076         term_inversed_area(hWnd, ox, oy, dx, dy);
3077
3078         oldx = cx;
3079         oldy = cy;
3080         return 0;
3081     }
3082     case WM_INITMENU: {
3083         setup_menus();
3084         return 0;
3085     }
3086     case WM_CLOSE: {
3087         if (!game_in_progress || !current_world_ptr->character_generated) {
3088             quit(NULL);
3089             return 0;
3090         }
3091
3092         if (!can_save) {
3093             plog(_("今は終了できません。", "You may not do that right now."));
3094             return 0;
3095         }
3096
3097         msg_flag = FALSE;
3098         forget_lite(p_ptr->current_floor_ptr);
3099         forget_view(p_ptr->current_floor_ptr);
3100         clear_mon_lite(p_ptr->current_floor_ptr);
3101         term_key_push(SPECIAL_KEY_QUIT);
3102         return 0;
3103     }
3104     case WM_QUERYENDSESSION: {
3105         if (!game_in_progress || !current_world_ptr->character_generated) {
3106             quit(NULL);
3107             return 0;
3108         }
3109
3110         msg_flag = FALSE;
3111         if (p_ptr->chp < 0)
3112             p_ptr->is_dead = FALSE;
3113         exe_write_diary(p_ptr, DIARY_GAMESTART, 0, _("----ゲーム中断----", "---- Save and Exit Game ----"));
3114
3115         p_ptr->panic_save = 1;
3116         signals_ignore_tstp();
3117         (void)strcpy(p_ptr->died_from, _("(緊急セーブ)", "(panic save)"));
3118         (void)save_player(p_ptr, SAVE_TYPE_CLOSE_GAME);
3119         quit(NULL);
3120         return 0;
3121     }
3122     case WM_QUIT: {
3123         quit(NULL);
3124         return 0;
3125     }
3126     case WM_COMMAND: {
3127         process_menus(p_ptr, LOWORD(wParam));
3128         return 0;
3129     }
3130     case WM_SIZE: {
3131         if (!td)
3132             return 1;
3133         if (!td->w)
3134             return 1;
3135         if (td->size_hack)
3136             return 1;
3137
3138         // todo 二重のswitch文。後で分割する.
3139         switch (wParam) {
3140         case SIZE_MINIMIZED: {
3141             for (int i = 1; i < MAX_TERM_DATA; i++) {
3142                 if (data[i].visible)
3143                     ShowWindow(data[i].w, SW_HIDE);
3144             }
3145
3146             return 0;
3147         }
3148         case SIZE_MAXIMIZED:
3149         case SIZE_RESTORED: {
3150             TERM_LEN cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
3151             TERM_LEN rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
3152             if ((td->cols != cols) || (td->rows != rows)) {
3153                 td->cols = cols;
3154                 td->rows = rows;
3155                 if (!IsZoomed(td->w) && !IsIconic(td->w)) {
3156                     normsize.x = td->cols;
3157                     normsize.y = td->rows;
3158                 }
3159
3160                 term_activate(&td->t);
3161                 term_resize(td->cols, td->rows);
3162                 InvalidateRect(td->w, NULL, TRUE);
3163             }
3164
3165             td->size_hack = TRUE;
3166             for (int i = 1; i < MAX_TERM_DATA; i++) {
3167                 if (data[i].visible)
3168                     ShowWindow(data[i].w, SW_SHOW);
3169             }
3170
3171             td->size_hack = FALSE;
3172
3173             return 0;
3174         }
3175         }
3176
3177         break;
3178     }
3179     case WM_PALETTECHANGED: {
3180         if ((HWND)wParam == hWnd)
3181             return 0;
3182     }
3183         [[fallthrough]]; /* Fall through */
3184     case WM_QUERYNEWPALETTE: {
3185         if (!paletted)
3186             return 0;
3187
3188         HDC hdc = GetDC(hWnd);
3189         SelectPalette(hdc, hPal, FALSE);
3190         int i = RealizePalette(hdc);
3191         if (i)
3192             InvalidateRect(hWnd, NULL, TRUE);
3193
3194         ReleaseDC(hWnd, hdc);
3195         return 0;
3196     }
3197     case WM_ACTIVATE: {
3198         if (!wParam || HIWORD(lParam))
3199             break;
3200
3201         for (int i = 1; i < MAX_TERM_DATA; i++) {
3202             if (!data[i].posfix)
3203                 term_window_pos(&data[i], hWnd);
3204         }
3205
3206         SetFocus(hWnd);
3207         return 0;
3208     }
3209     case WM_ACTIVATEAPP: {
3210         if (IsIconic(td->w))
3211             break;
3212
3213         for (int i = 1; i < MAX_TERM_DATA; i++) {
3214             if (data[i].visible) {
3215                 if (wParam == TRUE) {
3216                     ShowWindow(data[i].w, SW_SHOW);
3217                 } else {
3218                     ShowWindow(data[i].w, SW_HIDE);
3219                 }
3220             }
3221         }
3222     }
3223         [[fallthrough]]; /* Fall through */
3224     case WM_ENABLE: {
3225         if (wParam == FALSE && keep_subwindows) {
3226             for (int i = 0; i < MAX_TERM_DATA; i++) {
3227                 if (data[i].visible) {
3228                     ShowWindow(data[i].w, SW_SHOW);
3229                 }
3230             }
3231         }
3232     }
3233     }
3234
3235     return DefWindowProc(hWnd, uMsg, wParam, lParam);
3236 }
3237
3238 /*!
3239  * todo WNDCLASSに影響があるのでplayer_type*の追加は保留
3240  */
3241 LRESULT PASCAL AngbandListProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3242 {
3243     term_data *td;
3244     PAINTSTRUCT ps;
3245     td = (term_data *)GetWindowLong(hWnd, 0);
3246
3247     switch (uMsg) {
3248     case WM_NCCREATE: {
3249         SetWindowLong(hWnd, 0, (LONG)(my_td));
3250         break;
3251     }
3252     case WM_CREATE: {
3253         return 0;
3254     }
3255     case WM_GETMINMAXINFO: {
3256         MINMAXINFO *lpmmi;
3257         RECT rc;
3258
3259         lpmmi = (MINMAXINFO *)lParam;
3260         if (!td)
3261             return 1;
3262
3263         rc.left = rc.top = 0;
3264         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
3265         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
3266
3267         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
3268         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
3269         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
3270         return 0;
3271     }
3272     case WM_SIZE: {
3273         if (!td)
3274             return 1;
3275         if (!td->w)
3276             return 1;
3277         if (td->size_hack)
3278             return 1;
3279
3280         td->size_hack = TRUE;
3281
3282         TERM_LEN cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
3283         TERM_LEN rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
3284         if ((td->cols != cols) || (td->rows != rows)) {
3285             term_type *old_term = Term;
3286             td->cols = cols;
3287             td->rows = rows;
3288             term_activate(&td->t);
3289             term_resize(td->cols, td->rows);
3290             term_activate(old_term);
3291             InvalidateRect(td->w, NULL, TRUE);
3292             p_ptr->window_flags = 0xFFFFFFFF;
3293             handle_stuff(p_ptr);
3294         }
3295
3296         td->size_hack = FALSE;
3297         return 0;
3298     }
3299     case WM_PAINT: {
3300         BeginPaint(hWnd, &ps);
3301         if (td)
3302             term_data_redraw(p_ptr, td);
3303         EndPaint(hWnd, &ps);
3304         return 0;
3305     }
3306     case WM_SYSKEYDOWN:
3307     case WM_KEYDOWN: {
3308         if (process_keydown(wParam, lParam))
3309             return 0;
3310
3311         break;
3312     }
3313     case WM_CHAR: {
3314         if (term_no_press)
3315             term_no_press = FALSE;
3316         else
3317             term_keypress(wParam);
3318         return 0;
3319     }
3320     case WM_PALETTECHANGED: {
3321         if ((HWND)wParam == hWnd)
3322             return FALSE;
3323     }
3324         [[fallthrough]]; /* Fall through */
3325     case WM_QUERYNEWPALETTE: {
3326         if (!paletted)
3327             return 0;
3328
3329         HDC hdc = GetDC(hWnd);
3330         SelectPalette(hdc, hPal, FALSE);
3331         int i = RealizePalette(hdc);
3332         if (i)
3333             InvalidateRect(hWnd, NULL, TRUE);
3334
3335         ReleaseDC(hWnd, hdc);
3336         return 0;
3337     }
3338     case WM_NCLBUTTONDOWN: {
3339         if (wParam == HTCLOSE)
3340             wParam = HTSYSMENU;
3341
3342         if (wParam == HTSYSMENU) {
3343             if (td->visible) {
3344                 td->visible = FALSE;
3345                 ShowWindow(td->w, SW_HIDE);
3346             }
3347
3348             return 0;
3349         }
3350
3351         break;
3352     }
3353     }
3354
3355     return DefWindowProc(hWnd, uMsg, wParam, lParam);
3356 }
3357
3358 LRESULT PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3359 {
3360     static int iMouse = 0;
3361     static WORD xMouse = 0;
3362     static WORD yMouse = 0;
3363
3364     switch (uMsg) {
3365     case WM_NCCREATE: {
3366         break;
3367     }
3368
3369     case WM_SETCURSOR: {
3370         SetCursor(NULL);
3371         return 0;
3372     }
3373
3374     case WM_LBUTTONDOWN:
3375     case WM_MBUTTONDOWN:
3376     case WM_RBUTTONDOWN:
3377     case WM_KEYDOWN: {
3378         SendMessage(hWnd, WM_CLOSE, 0, 0);
3379         return 0;
3380     }
3381     case WM_MOUSEMOVE: {
3382         if (iMouse) {
3383             int dx = LOWORD(lParam) - xMouse;
3384             int dy = HIWORD(lParam) - yMouse;
3385
3386             if (dx < 0)
3387                 dx = -dx;
3388             if (dy < 0)
3389                 dy = -dy;
3390
3391             if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS)) {
3392                 SendMessage(hWnd, WM_CLOSE, 0, 0);
3393             }
3394         }
3395
3396         iMouse = 1;
3397         xMouse = LOWORD(lParam);
3398         yMouse = HIWORD(lParam);
3399
3400         return 0;
3401     }
3402     case WM_CLOSE: {
3403         DestroyWindow(hwndSaver);
3404         hwndSaver = NULL;
3405         return 0;
3406     }
3407     }
3408
3409     return DefWindowProc(hWnd, uMsg, wParam, lParam);
3410 }
3411
3412 /*
3413  * Display warning message (see "z-util.c")
3414  */
3415 static void hack_plog(concptr str)
3416 {
3417     if (str) {
3418 #ifdef JP
3419         MessageBox(NULL, str, "警告!", MB_ICONEXCLAMATION | MB_OK);
3420 #else
3421         MessageBox(NULL, str, "Warning", MB_ICONEXCLAMATION | MB_OK);
3422 #endif
3423     }
3424 }
3425
3426 /*
3427  * Display error message and quit (see "z-util.c")
3428  */
3429 static void hack_quit(concptr str)
3430 {
3431     if (str) {
3432 #ifdef JP
3433         MessageBox(NULL, str, "エラー!", MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
3434 #else
3435         MessageBox(NULL, str, "Error", MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
3436 #endif
3437     }
3438
3439     UnregisterClass(AppName, hInstance);
3440     if (hIcon)
3441         DestroyIcon(hIcon);
3442
3443     exit(0);
3444 }
3445
3446 /*
3447  * Display warning message (see "z-util.c")
3448  */
3449 static void hook_plog(concptr str)
3450 {
3451     if (str) {
3452 #ifdef JP
3453         MessageBox(data[0].w, str, "警告!", MB_ICONEXCLAMATION | MB_OK);
3454 #else
3455         MessageBox(data[0].w, str, "Warning", MB_ICONEXCLAMATION | MB_OK);
3456 #endif
3457     }
3458 }
3459
3460 /*
3461  * Display error message and quit (see "z-util.c")
3462  */
3463 static void hook_quit(concptr str)
3464 {
3465     if (str) {
3466 #ifdef JP
3467         MessageBox(data[0].w, str, "エラー!", MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
3468 #else
3469         MessageBox(data[0].w, str, "Error", MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
3470 #endif
3471     }
3472
3473     save_prefs();
3474     for (int i = MAX_TERM_DATA - 1; i >= 0; --i) {
3475         term_force_font(&data[i], NULL);
3476         if (data[i].font_want)
3477             string_free(data[i].font_want);
3478         if (data[i].w)
3479             DestroyWindow(data[i].w);
3480         data[i].w = 0;
3481     }
3482
3483     if (infGraph.hPalette)
3484         DeleteObject(infGraph.hPalette);
3485     if (infGraph.hBitmap)
3486         DeleteObject(infGraph.hBitmap);
3487     if (infMask.hPalette)
3488         DeleteObject(infMask.hPalette);
3489     if (infMask.hBitmap)
3490         DeleteObject(infMask.hBitmap);
3491
3492     DeleteObject(hbrYellow);
3493     delete_bg();
3494
3495     if (hPal)
3496         DeleteObject(hPal);
3497
3498     UnregisterClass(AppName, hInstance);
3499     if (hIcon)
3500         DestroyIcon(hIcon);
3501
3502     exit(0);
3503 }
3504
3505 /*
3506  * Init some stuff
3507  */
3508 static void init_stuff(void)
3509 {
3510     char path[1024];
3511     GetModuleFileName(hInstance, path, 512);
3512     argv0 = path;
3513     strcpy(path + strlen(path) - 4, ".INI");
3514     ini_file = string_make(path);
3515     int i = strlen(path);
3516
3517     for (; i > 0; i--) {
3518         if (path[i] == '\\') {
3519             break;
3520         }
3521     }
3522
3523     strcpy(path + i + 1, "lib\\");
3524     validate_dir(path, TRUE);
3525     init_file_paths(path, path);
3526     validate_dir(ANGBAND_DIR_APEX, FALSE);
3527     validate_dir(ANGBAND_DIR_BONE, FALSE);
3528     if (!check_dir(ANGBAND_DIR_EDIT)) {
3529         validate_dir(ANGBAND_DIR_DATA, TRUE);
3530     } else {
3531         validate_dir(ANGBAND_DIR_DATA, FALSE);
3532     }
3533
3534     validate_dir(ANGBAND_DIR_FILE, TRUE);
3535     validate_dir(ANGBAND_DIR_HELP, FALSE);
3536     validate_dir(ANGBAND_DIR_INFO, FALSE);
3537     validate_dir(ANGBAND_DIR_PREF, TRUE);
3538     validate_dir(ANGBAND_DIR_SAVE, FALSE);
3539     validate_dir(ANGBAND_DIR_DEBUG_SAVE, FALSE);
3540     validate_dir(ANGBAND_DIR_USER, TRUE);
3541     validate_dir(ANGBAND_DIR_XTRA, TRUE);
3542     path_build(path, sizeof(path), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
3543
3544     validate_file(path);
3545     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
3546     ANGBAND_DIR_XTRA_GRAF = string_make(path);
3547     validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
3548
3549     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
3550     ANGBAND_DIR_XTRA_SOUND = string_make(path);
3551     validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
3552
3553     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
3554     ANGBAND_DIR_XTRA_MUSIC = string_make(path);
3555     validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
3556
3557     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "help");
3558     ANGBAND_DIR_XTRA_HELP = string_make(path);
3559 }
3560
3561 /*!
3562  * @brief コマンドラインから全スポイラー出力を行う
3563  * Create Spoiler files from Command Line
3564  * @return spoiler_output_status
3565  */
3566 static spoiler_output_status create_debug_spoiler(LPSTR cmd_line)
3567 {
3568     char *s;
3569     concptr option;
3570     s = cmd_line;
3571     if (!*s)
3572         return SPOILER_OUTPUT_CANCEL;
3573     option = "--output-spoilers";
3574
3575     if (strncmp(s, option, strlen(option)) != 0)
3576         return SPOILER_OUTPUT_CANCEL;
3577
3578     init_stuff();
3579     init_angband(p_ptr, process_autopick_file_command, TRUE);
3580
3581     return output_all_spoilers();
3582 }
3583
3584 /*!
3585  * todo よく見るとhMutexはちゃんと使われていない……?
3586  * @brief (Windows固有)変愚蛮怒が起動済かどうかのチェック
3587  */
3588 static bool is_already_running(void)
3589 {
3590     HANDLE hMutex;
3591     hMutex = CreateMutex(NULL, TRUE, VERSION_NAME);
3592     if (GetLastError() == ERROR_ALREADY_EXISTS) {
3593         return TRUE;
3594     }
3595
3596     return FALSE;
3597 }
3598
3599 /*!
3600  * @brief (Windows固有)Windowsアプリケーションとしてのエントリポイント
3601  */
3602 int WINAPI WinMain(_In_ HINSTANCE hInst, _In_opt_ HINSTANCE hPrevInst, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
3603 {
3604     WNDCLASS wc;
3605     HDC hdc;
3606     MSG msg;
3607
3608     setlocale(LC_ALL, "ja_JP");
3609     (void)nCmdShow;
3610     hInstance = hInst;
3611     if (is_already_running()) {
3612         MessageBox(
3613             NULL, _("変愚蛮怒はすでに起動しています。", "Hengband is already running."), _("エラー!", "Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
3614         return FALSE;
3615     }
3616
3617     switch (create_debug_spoiler(lpCmdLine)) {
3618     case SPOILER_OUTPUT_SUCCESS:
3619         fprintf(stdout, "Successfully created a spoiler file.");
3620         quit(NULL);
3621         return 0;
3622     case SPOILER_OUTPUT_FAIL_FOPEN:
3623         fprintf(stderr, "Cannot create spoiler file.");
3624         quit(NULL);
3625         return 0;
3626     case SPOILER_OUTPUT_FAIL_FCLOSE:
3627         fprintf(stderr, "Cannot close spoiler file.");
3628         quit(NULL);
3629         return 0;
3630     default:
3631         break;
3632     }
3633
3634     if (hPrevInst == NULL) {
3635         wc.style = CS_CLASSDC;
3636         wc.lpfnWndProc = AngbandWndProc;
3637         wc.cbClsExtra = 0;
3638         wc.cbWndExtra = 4;
3639         wc.hInstance = hInst;
3640         wc.hIcon = hIcon = LoadIcon(hInst, AppName);
3641         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
3642         wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
3643         wc.lpszMenuName = AppName;
3644         wc.lpszClassName = AppName;
3645
3646         if (!RegisterClass(&wc))
3647             exit(1);
3648
3649         wc.lpfnWndProc = AngbandListProc;
3650         wc.lpszMenuName = NULL;
3651         wc.lpszClassName = AngList;
3652
3653         if (!RegisterClass(&wc))
3654             exit(2);
3655
3656         wc.style = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
3657         wc.lpfnWndProc = AngbandSaverProc;
3658         wc.hCursor = NULL;
3659         wc.lpszMenuName = NULL;
3660         wc.lpszClassName = "WindowsScreenSaverClass";
3661
3662         if (!RegisterClass(&wc))
3663             exit(3);
3664     }
3665
3666     plog_aux = hack_plog;
3667     quit_aux = hack_quit;
3668     core_aux = hack_quit;
3669
3670     init_stuff();
3671     for (int i = 0; special_key_list[i]; ++i) {
3672         special_key[special_key_list[i]] = TRUE;
3673     }
3674
3675     for (int i = 0; ignore_key_list[i]; ++i) {
3676         ignore_key[ignore_key_list[i]] = TRUE;
3677     }
3678
3679     hdc = GetDC(NULL);
3680     colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
3681     paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? TRUE : FALSE);
3682     ReleaseDC(NULL, hdc);
3683
3684     for (int i = 0; i < 256; i++) {
3685         byte rv = angband_color_table[i][1];
3686         byte gv = angband_color_table[i][2];
3687         byte bv = angband_color_table[i][3];
3688         win_clr[i] = PALETTERGB(rv, gv, bv);
3689         angband_color_table[i][0] = win_pal[i];
3690     }
3691
3692     init_windows();
3693     init_bg();
3694
3695     plog_aux = hook_plog;
3696     quit_aux = hook_quit;
3697     core_aux = hook_quit;
3698
3699     ANGBAND_SYS = "win";
3700     if (7 != GetKeyboardType(0))
3701         ANGBAND_KEYBOARD = "0";
3702     else {
3703         switch (GetKeyboardType(1)) {
3704         case 0x0D01:
3705         case 0x0D02:
3706         case 0x0D03:
3707         case 0x0D04:
3708         case 0x0D05:
3709         case 0x0D06:
3710             /* NEC PC-98x1 */
3711             ANGBAND_KEYBOARD = "NEC98";
3712             break;
3713         default:
3714             /* PC/AT */
3715             ANGBAND_KEYBOARD = "JAPAN";
3716         }
3717     }
3718
3719     signals_init();
3720     term_activate(term_screen);
3721     init_angband(p_ptr, process_autopick_file_command, FALSE);
3722     initialized = TRUE;
3723     check_for_save_file(p_ptr, lpCmdLine);
3724     prt(_("[ファイル] メニューの [新規] または [開く] を選択してください。", "[Choose 'New' or 'Open' from the 'File' menu]"), 23, _(8, 17));
3725     term_fresh();
3726     while (GetMessage(&msg, NULL, 0, 0)) {
3727         TranslateMessage(&msg);
3728         DispatchMessage(&msg);
3729     }
3730
3731     quit(NULL);
3732     return 0;
3733 }
3734 #endif /* WINDOWS */