OSDN Git Service

Merge pull request #1080 from sikabane-works/feature/enum-trap-type
[hengbandforosx/hengbandosx.git] / src / main-win.cpp
1 /*!
2  * @file main-win.cpp
3  * @brief Windows版固有実装(メインエントリポイント含む)
4  * @date 2018/03/16
5  * @author Hengband Team
6  * @details
7  *
8  * <h3>概要</h3>
9  * Windows98かその前後の頃を起点としたAPI実装。
10  * 各種のゲームエンジンは無論、
11  * DirectXといった昨今描画に標準的となったライブラリも用いていない。
12  * タイルの描画処理などについては、現在動作の詳細を検証中。
13  *
14  * <h3>フォーク元の概要</h3>
15  * <p>
16  * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, and others
17  *
18  * This software may be copied and distributed for educational, research,
19  * and not for profit purposes provided that this copyright and statement
20  * are included in all such copies.
21  * </p>
22  * <p>
23  * This file helps Angband work with Windows computers.
24  *
25  * To use this file, use an appropriate "Makefile" or "Project File",
26  * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
27  * make sure to obtain various extra files as described below.
28  *
29  * The official compilation uses the CodeWarrior Pro compiler, which
30  * includes a special project file and precompilable header file.
31  * </p>
32  *
33  * <p>
34  * The "lib/user/pref-win.prf" file contains keymaps, macro definitions,
35  * and/or color redefinitions.
36  * </p>
37  *
38  * <p>
39  * The "lib/user/font-win.prf" contains attr/char mappings for wall.bmp.
40  * </p>
41  *
42  * <p>
43  * The "lib/user/graf-win.prf" contains attr/char mappings for use with the
44  * special bitmap files in "lib/xtra/graf", which are activated by a menu
45  * item.
46  * </p>
47  *
48  * <p>
49  * Compiling this file, and using the resulting executable, requires
50  * several extra files not distributed with the standard Angband code.
51  * All of these extra files can be found in the "ext-win" archive.
52  * </p>
53  *
54  * <p>
55  * The "term_xtra_win_clear()" function should probably do a low-level
56  * clear of the current window, and redraw the borders and other things,
57  * if only for efficiency.
58  * </p>
59  *
60  * <p>
61  * A simpler method is needed for selecting the "tile size" for windows.
62  * </p>
63  *
64  * <p>
65  * The various "warning" messages assume the existance of the "screen.w"
66  * window, I think, and only a few calls actually check for its existance,
67  * this may be okay since "NULL" means "on top of all windows". (?)  The
68  * user must never be allowed to "hide" the main window, or the "menubar"
69  * will disappear.
70  * </p>
71  *
72  * <p>
73  * Initial framework (and most code) by Ben Harrison (benh@phial.com).
74  *
75  * Original code by Skirmantas Kligys (kligys@scf.usc.edu).
76  *
77  * Additional code by Ross E Becker (beckerr@cis.ohio-state.edu),
78  * and Chris R. Martin (crm7479@tam2000.tamu.edu).
79  * </p>
80  */
81
82 #ifdef WINDOWS
83
84 #include "cmd-io/cmd-save.h"
85 #include "cmd-visual/cmd-draw.h"
86 #include "core/game-play.h"
87 #include "core/player-processor.h"
88 #include "core/score-util.h"
89 #include "core/scores.h"
90 #include "core/special-internal-keys.h"
91 #include "core/stuff-handler.h"
92 #include "core/visuals-reseter.h"
93 #include "floor/floor-events.h"
94 #include "game-option/runtime-arguments.h"
95 #include "game-option/special-options.h"
96 #include "io/files-util.h"
97 #include "io/input-key-acceptor.h"
98 #include "io/record-play-movie.h"
99 #include "io/signal-handlers.h"
100 #include "io/write-diary.h"
101 #include "main-win/commandline-win.h"
102 #include "main-win/graphics-win.h"
103 #include "main-win/main-win-bg.h"
104 #include "main-win/main-win-file-utils.h"
105 #include "main-win/main-win-mci.h"
106 #include "main-win/main-win-menuitem.h"
107 #include "main-win/main-win-music.h"
108 #include "main-win/main-win-sound.h"
109 #include "main-win/main-win-utils.h"
110 #include "main/angband-initializer.h"
111 #include "main/sound-of-music.h"
112 #include "monster-floor/monster-lite.h"
113 #include "save/save.h"
114 #include "system/angband.h"
115 #include "system/player-type-definition.h"
116 #include "system/system-variables.h"
117 #include "term/gameterm.h"
118 #include "term/screen-processor.h"
119 #include "term/term-color-types.h"
120 #include "util/angband-files.h"
121 #include "util/int-char-converter.h"
122 #include "util/string-processor.h"
123 #include "view/display-messages.h"
124 #include "view/display-scores.h"
125 #include "wizard/spoiler-util.h"
126 #include "wizard/wizard-spoiler.h"
127 #include "world/world.h"
128
129 #include <cstdlib>
130 #include <locale>
131 #include <string>
132
133 #include <commdlg.h>
134 #include <direct.h>
135
136 /*
137  * Window names
138  */
139 LPCWSTR win_term_name[] = { L"Hengband", L"Term-1", L"Term-2", L"Term-3", L"Term-4", L"Term-5", L"Term-6", L"Term-7" };
140
141 /*!
142  * @struct term_data
143  * @brief ターム情報構造体 / Extra "term" data
144  * @details
145  * <p>
146  * pos_x / pos_y は各タームの左上点座標を指す。
147  * </p>
148  * <p>
149  * tile_wid / tile_hgt は[ウィンドウ]メニューのタイルの幅/高さを~を
150  * 1ドットずつ調整するステータスを指す。
151  * また、フォントを変更すると都度自動調整される。
152  * </p>
153  * <p>
154  * Note the use of "font_want" for the names of the font file requested by
155  * the user.
156  * </p>
157  */
158 typedef struct {
159     term_type t;
160     LPCWSTR name;
161     HWND w;
162     DWORD dwStyle;
163     DWORD dwExStyle;
164
165     TERM_LEN rows; /* int -> uint */
166     TERM_LEN cols;
167
168     uint pos_x; //!< タームの左上X座標
169     uint pos_y; //!< タームの左上Y座標
170     uint size_wid;
171     uint size_hgt;
172     uint size_ow1;
173     uint size_oh1;
174     uint size_ow2;
175     uint size_oh2;
176
177     bool size_hack;
178     bool visible;
179     concptr font_want;
180     HFONT font_id;
181     int font_wid; //!< フォント横幅
182     int font_hgt; //!< フォント縦幅
183     int tile_wid; //!< タイル横幅
184     int tile_hgt; //!< タイル縦幅
185
186     LOGFONTW lf;
187
188     bool posfix;
189 } term_data;
190
191 #define MAX_TERM_DATA 8 //!< Maximum number of windows
192
193 static term_data data[MAX_TERM_DATA]; //!< An array of term_data's
194 static term_data *my_td; //!< Hack -- global "window creation" pointer
195 POINT normsize; //!< Remember normal size of main window when maxmized
196
197 /*
198  * was main window maximized on previous playing
199  */
200 bool win_maximized = FALSE;
201
202 /*
203  * game in progress
204  */
205 bool game_in_progress = false;
206
207 /*
208  * movie in progress
209  */
210 bool movie_in_progress = false;
211
212 /*
213  * note when "open"/"new" become valid
214  */
215 bool initialized = FALSE;
216
217 /*
218  * Saved instance handle
219  */
220 static HINSTANCE hInstance;
221
222 /*
223  * Yellow brush for the cursor
224  */
225 static HBRUSH hbrYellow;
226
227 /*
228  * An icon
229  */
230 static HICON hIcon;
231
232 /* bg */
233 bg_mode current_bg_mode = bg_mode::BG_NONE;
234 #define DEFAULT_BG_FILENAME "bg.bmp"
235 char wallpaper_file[MAIN_WIN_MAX_PATH] = ""; //!< 壁紙ファイル名。
236
237 /*
238  * Show sub-windows even when Hengband is not in focus
239  */
240 static bool keep_subwindows = TRUE;
241
242 /*
243  * Full path to ANGBAND.INI
244  */
245 static concptr ini_file = NULL;
246
247 /*
248  * Name of application
249  */
250 static LPCWSTR AppName = L"ANGBAND";
251
252 /*
253  * Name of sub-window type
254  */
255 static LPCWSTR AngList = L"AngList";
256
257 /*
258  * The "complex" color values
259  */
260 static COLORREF win_clr[256];
261
262 /*
263  * Flag for macro trigger with dump ASCII
264  */
265 static bool term_no_press = FALSE;
266
267 /*
268  * Copy and paste
269  */
270 static bool mouse_down = FALSE;
271 static bool paint_rect = FALSE;
272 static TERM_LEN mousex = 0, mousey = 0;
273 static TERM_LEN oldx, oldy;
274
275 /*
276  * Hack -- define which keys are "special"
277  */
278 static bool special_key[256];
279 static bool ignore_key[256];
280
281 /*
282  * Hack -- initialization list for "special_key"
283  */
284 static byte special_key_list[] = {
285     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,
286     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,
287     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,
288     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,
289     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,
290     0 /* End of List */
291 };
292
293 static byte ignore_key_list[] = {
294     VK_ESCAPE, VK_TAB, VK_SPACE, 'F', 'W', 'O', /*'H',*/ /* these are menu characters.*/
295     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 */
296 };
297
298 /*!
299  * @brief Validate a file
300  */
301 static void validate_file(concptr s)
302 {
303     if (check_file(s))
304         return;
305
306     quit_fmt(_("必要なファイル[%s]が見あたりません。", "Cannot find required file:\n%s"), s);
307 }
308
309 /*!
310  * @brief Validate a directory
311  */
312 static void validate_dir(concptr s, bool vital)
313 {
314     if (check_dir(s))
315         return;
316
317     if (vital) {
318         quit_fmt(_("必要なディレクトリ[%s]が見あたりません。", "Cannot find required directory:\n%s"), s);
319     } else if (mkdir(s)) {
320         quit_fmt("Unable to create directory:\n%s", s);
321     }
322 }
323
324 /*!
325  * @brief (Windows版固有実装)Get the "size" for a window
326  */
327 static void term_getsize(term_data *td)
328 {
329     if (td->cols < 1)
330         td->cols = 1;
331     if (td->rows < 1)
332         td->rows = 1;
333
334     TERM_LEN wid = td->cols * td->tile_wid + td->size_ow1 + td->size_ow2;
335     TERM_LEN hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
336
337     RECT rw, rc;
338     if (td->w) {
339         GetWindowRect(td->w, &rw);
340         GetClientRect(td->w, &rc);
341
342         td->size_wid = (rw.right - rw.left) - (rc.right - rc.left) + wid;
343         td->size_hgt = (rw.bottom - rw.top) - (rc.bottom - rc.top) + hgt;
344
345         td->pos_x = rw.left;
346         td->pos_y = rw.top;
347     } else {
348         /* Tempolary calculation */
349         rc.left = 0;
350         rc.right = wid;
351         rc.top = 0;
352         rc.bottom = hgt;
353         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
354         td->size_wid = rc.right - rc.left;
355         td->size_hgt = rc.bottom - rc.top;
356     }
357 }
358
359 /*!
360  * @brief Write the "prefs" for a single term
361  */
362 static void save_prefs_aux(int i)
363 {
364     term_data *td = &data[i];
365     GAME_TEXT sec_name[128];
366     char buf[1024];
367
368     if (!td->w)
369         return;
370
371     wsprintfA(sec_name, "Term-%d", i);
372
373     if (i > 0) {
374         strcpy(buf, td->visible ? "1" : "0");
375         WritePrivateProfileStringA(sec_name, "Visible", buf, ini_file);
376     }
377
378     auto pwchar = td->lf.lfFaceName[0] != '\0' ? td->lf.lfFaceName : _(L"MS ゴシック", L"Courier");
379     WritePrivateProfileStringA(sec_name, "Font", to_multibyte(pwchar).c_str(), ini_file);
380
381     wsprintfA(buf, "%d", td->lf.lfWidth);
382     WritePrivateProfileStringA(sec_name, "FontWid", buf, ini_file);
383     wsprintfA(buf, "%d", td->lf.lfHeight);
384     WritePrivateProfileStringA(sec_name, "FontHgt", buf, ini_file);
385     wsprintfA(buf, "%d", td->lf.lfWeight);
386     WritePrivateProfileStringA(sec_name, "FontWgt", buf, ini_file);
387
388     wsprintfA(buf, "%d", td->tile_wid);
389     WritePrivateProfileStringA(sec_name, "TileWid", buf, ini_file);
390
391     wsprintfA(buf, "%d", td->tile_hgt);
392     WritePrivateProfileStringA(sec_name, "TileHgt", buf, ini_file);
393
394     WINDOWPLACEMENT lpwndpl;
395     lpwndpl.length = sizeof(WINDOWPLACEMENT);
396     GetWindowPlacement(td->w, &lpwndpl);
397
398     RECT rc = lpwndpl.rcNormalPosition;
399     if (i == 0)
400         wsprintfA(buf, "%d", normsize.x);
401     else
402         wsprintfA(buf, "%d", td->cols);
403
404     WritePrivateProfileStringA(sec_name, "NumCols", buf, ini_file);
405
406     if (i == 0)
407         wsprintfA(buf, "%d", normsize.y);
408     else
409         wsprintfA(buf, "%d", td->rows);
410
411     WritePrivateProfileStringA(sec_name, "NumRows", buf, ini_file);
412     if (i == 0) {
413         strcpy(buf, IsZoomed(td->w) ? "1" : "0");
414         WritePrivateProfileStringA(sec_name, "Maximized", buf, ini_file);
415     }
416
417     GetWindowRect(td->w, &rc);
418     wsprintfA(buf, "%d", rc.left);
419     WritePrivateProfileStringA(sec_name, "PositionX", buf, ini_file);
420
421     wsprintfA(buf, "%d", rc.top);
422     WritePrivateProfileStringA(sec_name, "PositionY", buf, ini_file);
423     if (i > 0) {
424         strcpy(buf, td->posfix ? "1" : "0");
425         WritePrivateProfileStringA(sec_name, "PositionFix", buf, ini_file);
426     }
427 }
428
429 /*!
430  * @brief Write the "prefs"
431  * We assume that the windows have all been initialized
432  */
433 static void save_prefs(void)
434 {
435     char buf[128];
436     wsprintfA(buf, "%d", arg_graphics);
437     WritePrivateProfileStringA("Angband", "Graphics", buf, ini_file);
438
439     strcpy(buf, arg_bigtile ? "1" : "0");
440     WritePrivateProfileStringA("Angband", "Bigtile", buf, ini_file);
441
442     strcpy(buf, arg_sound ? "1" : "0");
443     WritePrivateProfileStringA("Angband", "Sound", buf, ini_file);
444
445     strcpy(buf, arg_music ? "1" : "0");
446     WritePrivateProfileStringA("Angband", "Music", buf, ini_file);
447     strcpy(buf, use_pause_music_inactive ? "1" : "0");
448     WritePrivateProfileStringA("Angband", "MusicPauseInactive", buf, ini_file);
449
450     wsprintfA(buf, "%d", current_bg_mode);
451     WritePrivateProfileStringA("Angband", "BackGround", buf, ini_file);
452     WritePrivateProfileStringA("Angband", "BackGroundBitmap", wallpaper_file[0] != '\0' ? wallpaper_file : DEFAULT_BG_FILENAME, ini_file);
453
454     int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
455     char tmp[1024] = "";
456     strncat(tmp, ANGBAND_DIR, path_length);
457
458     int n = strncmp(tmp, savefile, path_length);
459     if (n == 0) {
460         char relative_path[1024] = "";
461         snprintf(relative_path, sizeof(relative_path), ".\\%s", (savefile + path_length));
462         WritePrivateProfileStringA("Angband", "SaveFile", relative_path, ini_file);
463     } else {
464         WritePrivateProfileStringA("Angband", "SaveFile", savefile, ini_file);
465     }
466
467     strcpy(buf, keep_subwindows ? "1" : "0");
468     WritePrivateProfileStringA("Angband", "KeepSubwindows", buf, ini_file);
469
470     for (int i = 0; i < MAX_TERM_DATA; ++i) {
471         save_prefs_aux(i);
472     }
473 }
474
475 /*!
476  * @brief callback for EnumDisplayMonitors API
477  */
478 BOOL CALLBACK monitorenumproc([[maybe_unused]] HMONITOR hMon, [[maybe_unused]] HDC hdcMon, [[maybe_unused]] LPRECT lpMon, LPARAM dwDate)
479 {
480     bool *result = (bool *)dwDate;
481     *result = true;
482     return FALSE;
483 }
484
485 /*!
486  * @brief Load the "prefs" for a single term
487  */
488 static void load_prefs_aux(int i)
489 {
490     term_data *td = &data[i];
491     GAME_TEXT sec_name[128];
492     char tmp[1024];
493
494     wsprintfA(sec_name, "Term-%d", i);
495     if (i > 0) {
496         td->visible = (GetPrivateProfileIntA(sec_name, "Visible", td->visible, ini_file) != 0);
497     }
498
499     GetPrivateProfileStringA(sec_name, "Font", _("MS ゴシック", "Courier"), tmp, 127, ini_file);
500
501     td->font_want = string_make(tmp);
502     int hgt = 15;
503     int wid = 0;
504     td->lf.lfWidth = GetPrivateProfileIntA(sec_name, "FontWid", wid, ini_file);
505     td->lf.lfHeight = GetPrivateProfileIntA(sec_name, "FontHgt", hgt, ini_file);
506     td->lf.lfWeight = GetPrivateProfileIntA(sec_name, "FontWgt", 0, ini_file);
507
508     td->tile_wid = GetPrivateProfileIntA(sec_name, "TileWid", td->lf.lfWidth, ini_file);
509     td->tile_hgt = GetPrivateProfileIntA(sec_name, "TileHgt", td->lf.lfHeight, ini_file);
510
511     td->cols = GetPrivateProfileIntA(sec_name, "NumCols", td->cols, ini_file);
512     td->rows = GetPrivateProfileIntA(sec_name, "NumRows", td->rows, ini_file);
513     normsize.x = td->cols;
514     normsize.y = td->rows;
515
516     if (i == 0) {
517         win_maximized = (GetPrivateProfileIntA(sec_name, "Maximized", win_maximized, ini_file) != 0);
518     }
519
520     int posx = GetPrivateProfileIntA(sec_name, "PositionX", 0, ini_file);
521     int posy = GetPrivateProfileIntA(sec_name, "PositionY", 0, ini_file);
522     // 保存座標がモニタ内の領域にあるかチェック
523     RECT rect = { posx, posy, posx + 128, posy + 128 };
524     bool in_any_monitor = false;
525     ::EnumDisplayMonitors(NULL, &rect, monitorenumproc, (LPARAM)&in_any_monitor);
526     if (in_any_monitor) {
527         // いずれかのモニタに表示可能、ウインドウ位置を復元
528         td->pos_x = posx;
529         td->pos_y = posy;
530     }
531
532     if (i > 0) {
533         td->posfix = (GetPrivateProfileIntA(sec_name, "PositionFix", td->posfix, ini_file) != 0);
534     }
535 }
536
537 /*!
538  * @brief Load the "prefs"
539  */
540 static void load_prefs(void)
541 {
542     arg_graphics = (byte)GetPrivateProfileIntA("Angband", "Graphics", static_cast<byte>(graphics_mode::GRAPHICS_NONE), ini_file);
543     arg_bigtile = (GetPrivateProfileIntA("Angband", "Bigtile", FALSE, ini_file) != 0);
544     use_bigtile = arg_bigtile;
545     arg_sound = (GetPrivateProfileIntA("Angband", "Sound", 0, ini_file) != 0);
546     arg_music = (GetPrivateProfileIntA("Angband", "Music", 0, ini_file) != 0);
547     use_pause_music_inactive = (GetPrivateProfileIntA("Angband", "MusicPauseInactive", 0, ini_file) != 0);
548     current_bg_mode = static_cast<bg_mode>(GetPrivateProfileIntA("Angband", "BackGround", 0, ini_file));
549     GetPrivateProfileStringA("Angband", "BackGroundBitmap", DEFAULT_BG_FILENAME, wallpaper_file, 1023, ini_file);
550     GetPrivateProfileStringA("Angband", "SaveFile", "", savefile, 1023, ini_file);
551
552     int n = strncmp(".\\", savefile, 2);
553     if (n == 0) {
554         int path_length = strlen(ANGBAND_DIR) - 4; /* \libの4文字分を削除 */
555         char tmp[1024] = "";
556         strncat(tmp, ANGBAND_DIR, path_length);
557         strncat(tmp, savefile + 2, strlen(savefile) - 2 + path_length);
558         strncpy(savefile, tmp, strlen(tmp));
559     }
560
561     keep_subwindows = (GetPrivateProfileIntA("Angband", "KeepSubwindows", 0, ini_file) != 0);
562     for (int i = 0; i < MAX_TERM_DATA; ++i) {
563         load_prefs_aux(i);
564     }
565 }
566
567 /*!
568  * @brief Initialize music
569  */
570 static void init_music(void)
571 {
572     // Flag set once "music" has been initialized
573     static bool can_use_music = FALSE;
574
575     if (!can_use_music) {
576         main_win_music::load_music_prefs();
577         can_use_music = TRUE;
578     }
579 }
580
581 /*!
582  * @brief Initialize sound
583  */
584 static void init_sound(void)
585 {
586     // Flag set once "sound" has been initialized
587     static bool can_use_sound = FALSE;
588
589     if (!can_use_sound) {
590         load_sound_prefs();
591         can_use_sound = TRUE;
592     }
593 }
594
595 /*!
596  * @brief Change sound mode
597  * @param new_mode bool
598  */
599 static void change_sound_mode(bool new_mode)
600 {
601     use_sound = new_mode;
602     if (use_sound) {
603         init_sound();
604     }
605 }
606
607 /*!
608  * @brief Initialize background
609  */
610 static void init_background(void)
611 {
612     // Flag set once "background" has been initialized
613     static bool can_use_background = FALSE;
614
615     if (!can_use_background) {
616         load_bg_prefs();
617         can_use_background = TRUE;
618     }
619 }
620
621 /*!
622  * @brief Change background mode
623  * @param new_mode bg_mode
624  * @param show_error trueに設定した場合のみ、エラーダイアログを表示する
625  * @param force_redraw trueの場合、モード変更に関わらずウインドウを再描画する
626  * @retval true success
627  * @retval false failed
628  */
629 static bool change_bg_mode(bg_mode new_mode, bool show_error = false, bool force_redraw = false)
630 {
631     bg_mode old_bg_mode = current_bg_mode;
632     current_bg_mode = new_mode;
633     if (current_bg_mode != bg_mode::BG_NONE) {
634         init_background();
635         if (!load_bg(wallpaper_file)) {
636             current_bg_mode = bg_mode::BG_NONE;
637             if (show_error)
638                 plog_fmt(_("壁紙用ファイル '%s' を読み込めません。", "Can't load the image file '%s'."), wallpaper_file);
639         }
640     } else {
641         delete_bg();
642     }
643
644     const bool mode_changed = (current_bg_mode != old_bg_mode);
645     if (mode_changed || force_redraw) {
646         // 全ウインドウ再描画
647         for (int i = 0; i < MAX_TERM_DATA; i++) {
648             term_data *td = &data[i];
649             if (td->visible)
650                 InvalidateRect(td->w, NULL, FALSE);
651         }
652     }
653
654     return (current_bg_mode == new_mode);
655 }
656
657 /*!
658  * @brief Resize a window
659  */
660 static void term_window_resize(term_data *td)
661 {
662     if (!td->w)
663         return;
664
665     SetWindowPos(td->w, 0, 0, 0, td->size_wid, td->size_hgt, SWP_NOMOVE | SWP_NOZORDER);
666     InvalidateRect(td->w, NULL, TRUE);
667 }
668
669 /*!
670  * @brief Force the use of a new font for a term_data.
671  * This function may be called before the "window" is ready.
672  * This function returns zero only if everything succeeds.
673  * @note that the "font name" must be capitalized!!!
674  */
675 static errr term_force_font(term_data *td)
676 {
677     if (td->font_id)
678         DeleteObject(td->font_id);
679
680     td->font_id = CreateFontIndirectW(&(td->lf));
681     int wid = td->lf.lfWidth;
682     int hgt = td->lf.lfHeight;
683     if (!td->font_id)
684         return 1;
685
686     if (!wid || !hgt) {
687         HDC hdcDesktop;
688         HFONT hfOld;
689         TEXTMETRIC tm;
690
691         hdcDesktop = GetDC(HWND_DESKTOP);
692         hfOld = static_cast<HFONT>(SelectObject(hdcDesktop, td->font_id));
693         GetTextMetrics(hdcDesktop, &tm);
694         SelectObject(hdcDesktop, hfOld);
695         ReleaseDC(HWND_DESKTOP, hdcDesktop);
696
697         wid = tm.tmAveCharWidth;
698         hgt = tm.tmHeight;
699     }
700
701     td->font_wid = wid;
702     td->font_hgt = hgt;
703
704     return 0;
705 }
706
707 /*!
708  * @brief Allow the user to change the font for this window.
709  */
710 static void term_change_font(term_data *td)
711 {
712     CHOOSEFONTW cf;
713     memset(&cf, 0, sizeof(cf));
714     cf.lStructSize = sizeof(cf);
715     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
716     cf.lpLogFont = &(td->lf);
717
718     if (!ChooseFontW(&cf))
719         return;
720
721     term_force_font(td);
722     td->tile_wid = td->font_wid;
723     td->tile_hgt = td->font_hgt;
724     term_getsize(td);
725     term_window_resize(td);
726 }
727
728 /*!
729  * @brief Allow the user to lock this window.
730  */
731 static void term_window_pos(term_data *td, HWND hWnd)
732 {
733     SetWindowPos(td->w, hWnd, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
734 }
735
736 /*!
737  * @brief Hack -- redraw a term_data
738  */
739 static void term_data_redraw(term_data *td)
740 {
741     term_activate(&td->t);
742     term_redraw();
743     term_activate(term_screen);
744 }
745
746 /*!
747  * @brief termの反転色表示
748  */
749 void term_inversed_area(HWND hWnd, int x, int y, int w, int h)
750 {
751     term_data *td = (term_data *)GetWindowLong(hWnd, 0);
752     int tx = td->size_ow1 + x * td->tile_wid;
753     int ty = td->size_oh1 + y * td->tile_hgt;
754     int tw = w * td->tile_wid - 1;
755     int th = h * td->tile_hgt - 1;
756
757     HDC hdc = GetDC(hWnd);
758     HBRUSH myBrush = CreateSolidBrush(RGB(255, 255, 255));
759     HBRUSH oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
760     HPEN oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
761
762     PatBlt(hdc, tx, ty, tw, th, PATINVERT);
763
764     SelectObject(hdc, oldBrush);
765     SelectObject(hdc, oldPen);
766 }
767
768 /*!
769  * @brief Windows版ユーザ設定項目実装部(実装必須) /Interact with the User
770  */
771 static errr term_user_win(int n)
772 {
773     (void)n;
774     return 0;
775 }
776
777 /*!
778  * @brief カラーパレットの変更?
779  */
780 static void refresh_color_table()
781 {
782     for (int i = 0; i < 256; i++) {
783         byte rv = angband_color_table[i][1];
784         byte gv = angband_color_table[i][2];
785         byte bv = angband_color_table[i][3];
786         win_clr[i] = PALETTERGB(rv, gv, bv);
787     }
788 }
789
790 /*!
791  * @brief グラフィクスのモード変更
792  */
793 static void change_graphics_mode(graphics_mode mode)
794 {
795     graphics_mode ret = graphic.change_graphics(mode);
796     if (ret != mode) {
797         plog(_("グラフィクスを初期化できません!", "Cannot initialize graphics!"));
798     }
799     arg_graphics = static_cast<byte>(ret);
800     use_graphics = (arg_graphics > 0);
801 }
802
803 /*!
804  * @brief React to global changes
805  */
806 static errr term_xtra_win_react(player_type *player_ptr)
807 {
808     refresh_color_table();
809
810     const byte current_mode = static_cast<byte>(graphic.get_mode());
811     if (current_mode != arg_graphics) {
812         change_graphics_mode(static_cast<graphics_mode>(arg_graphics));
813         reset_visuals(player_ptr);
814     }
815
816     for (int i = 0; i < MAX_TERM_DATA; i++) {
817         term_type *old = Term;
818         term_data *td = &data[i];
819         if ((td->cols != td->t.wid) || (td->rows != td->t.hgt)) {
820             term_activate(&td->t);
821             term_resize(td->cols, td->rows);
822             term_redraw();
823             term_activate(old);
824         }
825     }
826
827     return 0;
828 }
829
830 /*!
831  * @brief Process at least one event
832  */
833 static errr term_xtra_win_event(int v)
834 {
835     MSG msg;
836     if (v) {
837         if (GetMessage(&msg, NULL, 0, 0)) {
838             TranslateMessage(&msg);
839             DispatchMessage(&msg);
840         }
841     } else {
842         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
843             TranslateMessage(&msg);
844             DispatchMessage(&msg);
845         }
846     }
847
848     return 0;
849 }
850
851 /*!
852  * @brief Process all pending events
853  */
854 static errr term_xtra_win_flush(void)
855 {
856     MSG msg;
857     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
858         TranslateMessage(&msg);
859         DispatchMessage(&msg);
860     }
861
862     return 0;
863 }
864
865 /*!
866  * @brief Hack -- clear the screen
867  * @details
868  * Make this more efficient
869  */
870 static errr term_xtra_win_clear(void)
871 {
872     term_data *td = (term_data *)(Term->data);
873
874     RECT rc;
875     GetClientRect(td->w, &rc);
876
877     HDC hdc = GetDC(td->w);
878     SetBkColor(hdc, RGB(0, 0, 0));
879     SelectObject(hdc, td->font_id);
880     ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
881
882     if (current_bg_mode != bg_mode::BG_NONE) {
883         rc.left = 0;
884         rc.top = 0;
885         draw_bg(hdc, &rc);
886     }
887
888     ReleaseDC(td->w, hdc);
889     return 0;
890 }
891
892 /*!
893  * @brief Hack -- make a noise
894  */
895 static errr term_xtra_win_noise(void)
896 {
897     MessageBeep(MB_ICONASTERISK);
898     return 0;
899 }
900
901 /*!
902  * @brief Hack -- make a sound
903  */
904 static errr term_xtra_win_sound(int v)
905 {
906     if (!use_sound)
907         return 1;
908     return play_sound(v);
909 }
910
911 /*!
912  * @brief Hack -- play a music
913  */
914 static errr term_xtra_win_music(int n, int v)
915 {
916     if (!use_music) {
917         return 1;
918     }
919
920     return main_win_music::play_music(n, v);
921 }
922
923 /*!
924  * @brief Hack -- play a music matches a situation
925  */
926 static errr term_xtra_win_scene(int v)
927 {
928     // TODO 場面に合った壁紙変更対応
929     if (!use_music) {
930         return 1;
931     }
932
933     return main_win_music::play_music_scene(v);
934 }
935
936 /*!
937  * @brief Delay for "x" milliseconds
938  */
939 static int term_xtra_win_delay(int v)
940 {
941     Sleep(v);
942     return 0;
943 }
944
945 /*!
946  * @brief Do a "special thing"
947  * @todo z-termに影響があるのでplayer_typeの追加は保留
948  */
949 static errr term_xtra_win(int n, int v)
950 {
951     switch (n) {
952     case TERM_XTRA_NOISE: {
953         return (term_xtra_win_noise());
954     }
955     case TERM_XTRA_MUSIC_BASIC:
956     case TERM_XTRA_MUSIC_DUNGEON:
957     case TERM_XTRA_MUSIC_QUEST:
958     case TERM_XTRA_MUSIC_TOWN:
959     case TERM_XTRA_MUSIC_MONSTER: {
960         return term_xtra_win_music(n, v);
961     }
962     case TERM_XTRA_MUSIC_MUTE: {
963         return main_win_music::stop_music();
964     }
965     case TERM_XTRA_SCENE: {
966         return term_xtra_win_scene(v);
967     }
968     case TERM_XTRA_SOUND: {
969         return (term_xtra_win_sound(v));
970     }
971     case TERM_XTRA_BORED: {
972         return (term_xtra_win_event(0));
973     }
974     case TERM_XTRA_EVENT: {
975         return (term_xtra_win_event(v));
976     }
977     case TERM_XTRA_FLUSH: {
978         return (term_xtra_win_flush());
979     }
980     case TERM_XTRA_CLEAR: {
981         return (term_xtra_win_clear());
982     }
983     case TERM_XTRA_REACT: {
984         return (term_xtra_win_react(p_ptr));
985     }
986     case TERM_XTRA_DELAY: {
987         return (term_xtra_win_delay(v));
988     }
989     }
990
991     return 1;
992 }
993
994 /*!
995  * @brief Low level graphics (Assumes valid input).
996  * @details
997  * Draw a "cursor" at (x,y), using a "yellow box".
998  */
999 static errr term_curs_win(int x, int y)
1000 {
1001     term_data *td = (term_data *)(Term->data);
1002     int tile_wid, tile_hgt;
1003     tile_wid = td->tile_wid;
1004     tile_hgt = td->tile_hgt;
1005
1006     RECT rc;
1007     rc.left = x * tile_wid + td->size_ow1;
1008     rc.right = rc.left + tile_wid;
1009     rc.top = y * tile_hgt + td->size_oh1;
1010     rc.bottom = rc.top + tile_hgt;
1011
1012     HDC hdc = GetDC(td->w);
1013     FrameRect(hdc, &rc, hbrYellow);
1014     ReleaseDC(td->w, hdc);
1015     return 0;
1016 }
1017
1018 /*!
1019  * @brief Low level graphics (Assumes valid input).
1020  * @details
1021  * Draw a "big cursor" at (x,y), using a "yellow box".
1022  */
1023 static errr term_bigcurs_win(int x, int y)
1024 {
1025     term_data *td = (term_data *)(Term->data);
1026     int tile_wid, tile_hgt;
1027     tile_wid = td->tile_wid;
1028     tile_hgt = td->tile_hgt;
1029
1030     RECT rc;
1031     rc.left = x * tile_wid + td->size_ow1;
1032     rc.right = rc.left + 2 * tile_wid;
1033     rc.top = y * tile_hgt + td->size_oh1;
1034     rc.bottom = rc.top + tile_hgt;
1035
1036     HDC hdc = GetDC(td->w);
1037     FrameRect(hdc, &rc, hbrYellow);
1038     ReleaseDC(td->w, hdc);
1039     return 0;
1040 }
1041
1042 /*!
1043  * @brief Low level graphics (Assumes valid input).
1044  * @details
1045  * Erase a "block" of "n" characters starting at (x,y).
1046  */
1047 static errr term_wipe_win(int x, int y, int n)
1048 {
1049     term_data *td = (term_data *)(Term->data);
1050     RECT rc;
1051     rc.left = x * td->tile_wid + td->size_ow1;
1052     rc.right = rc.left + n * td->tile_wid;
1053     rc.top = y * td->tile_hgt + td->size_oh1;
1054     rc.bottom = rc.top + td->tile_hgt;
1055
1056     HDC hdc = GetDC(td->w);
1057     SetBkColor(hdc, RGB(0, 0, 0));
1058     SelectObject(hdc, td->font_id);
1059     if (current_bg_mode != bg_mode::BG_NONE)
1060         draw_bg(hdc, &rc);
1061     else
1062         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1063
1064     ReleaseDC(td->w, hdc);
1065     return 0;
1066 }
1067
1068 /*!
1069  * @brief Low level graphics.  Assumes valid input.
1070  * @details
1071  * Draw several ("n") chars, with an attr, at a given location.
1072  *
1073  * All "graphic" data is handled by "term_pict_win()", below.
1074  *
1075  * One would think there is a more efficient method for telling a window
1076  * what color it should be using to draw with, but perhaps simply changing
1077  * it every time is not too inefficient.
1078  */
1079 static errr term_text_win(int x, int y, int n, TERM_COLOR a, concptr s)
1080 {
1081     term_data *td = (term_data *)(Term->data);
1082     static HBITMAP WALL;
1083     static HBRUSH myBrush, oldBrush;
1084     static HPEN oldPen;
1085     static bool init_done = FALSE;
1086
1087     if (!init_done) {
1088         WALL = LoadBitmapW(hInstance, AppName);
1089         myBrush = CreatePatternBrush(WALL);
1090         init_done = TRUE;
1091     }
1092
1093     RECT rc{ static_cast<LONG>(x * td->tile_wid + td->size_ow1), static_cast<LONG>(y * td->tile_hgt + td->size_oh1),
1094         static_cast<LONG>(rc.left + n * td->tile_wid), static_cast<LONG>(rc.top + td->tile_hgt) };
1095
1096     HDC hdc = GetDC(td->w);
1097     SetBkColor(hdc, RGB(0, 0, 0));
1098     SetTextColor(hdc, win_clr[a]);
1099
1100     SelectObject(hdc, td->font_id);
1101     if (current_bg_mode != bg_mode::BG_NONE)
1102         SetBkMode(hdc, TRANSPARENT);
1103
1104     ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
1105     if (current_bg_mode != bg_mode::BG_NONE)
1106         draw_bg(hdc, &rc);
1107
1108     rc.left += ((td->tile_wid - td->font_wid) / 2);
1109     rc.right = rc.left + td->font_wid;
1110     rc.top += ((td->tile_hgt - td->font_hgt) / 2);
1111     rc.bottom = rc.top + td->font_hgt;
1112
1113     for (int i = 0; i < n; i++) {
1114 #ifdef JP
1115         if (use_bigtile && *(s + i) == "■"[0] && *(s + i + 1) == "■"[1]) {
1116             rc.right += td->font_wid;
1117             oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1118             oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1119             Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1120             SelectObject(hdc, oldBrush);
1121             SelectObject(hdc, oldPen);
1122             rc.right -= td->font_wid;
1123             i++;
1124             rc.left += 2 * td->tile_wid;
1125             rc.right += 2 * td->tile_wid;
1126         } else if (iskanji(*(s + i))) /* 2バイト文字 */
1127         {
1128             const auto *buf = to_wchar(s + i).wc_str();
1129             rc.right += td->font_wid;
1130             if (buf == NULL)
1131                 ExtTextOutA(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 2, NULL);
1132             else
1133                 ExtTextOutW(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, buf, wcslen(buf), NULL);
1134             rc.right -= td->font_wid;
1135             i++;
1136             rc.left += 2 * td->tile_wid;
1137             rc.right += 2 * td->tile_wid;
1138         } else if (*(s + i) == 127) {
1139             oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1140             oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1141             Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1142             SelectObject(hdc, oldBrush);
1143             SelectObject(hdc, oldPen);
1144             rc.left += td->tile_wid;
1145             rc.right += td->tile_wid;
1146         } else {
1147             ExtTextOutA(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 1, NULL);
1148             rc.left += td->tile_wid;
1149             rc.right += td->tile_wid;
1150         }
1151 #else
1152         if (*(s + i) == 127) {
1153             oldBrush = static_cast<HBRUSH>(SelectObject(hdc, myBrush));
1154             oldPen = static_cast<HPEN>(SelectObject(hdc, GetStockObject(NULL_PEN)));
1155             Rectangle(hdc, rc.left, rc.top, rc.right + 1, rc.bottom + 1);
1156             SelectObject(hdc, oldBrush);
1157             SelectObject(hdc, oldPen);
1158             rc.left += td->tile_wid;
1159             rc.right += td->tile_wid;
1160         } else {
1161             ExtTextOutA(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s + i, 1, NULL);
1162             rc.left += td->tile_wid;
1163             rc.right += td->tile_wid;
1164         }
1165 #endif
1166     }
1167
1168     ReleaseDC(td->w, hdc);
1169     return 0;
1170 }
1171
1172 /*!
1173  * @brief Low level graphics.  Assumes valid input.
1174  * @details
1175  * Draw an array of "special" attr/char pairs at the given location.
1176  *
1177  * We use the "term_pict_win()" function for "graphic" data, which are
1178  * encoded by setting the "high-bits" of both the "attr" and the "char"
1179  * data.  We use the "attr" to represent the "row" of the main bitmap,
1180  * and the "char" to represent the "col" of the main bitmap.  The use
1181  * of this function is induced by the "higher_pict" flag.
1182  *
1183  * If "graphics" is not available, we simply "wipe" the given grids.
1184  */
1185 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)
1186 {
1187     term_data *td = (term_data *)(Term->data);
1188     int i;
1189     HDC hdcMask = NULL;
1190     if (!use_graphics) {
1191         return (term_wipe_win(x, y, n));
1192     }
1193
1194     const tile_info &infGraph = graphic.get_tile_info();
1195     const bool has_mask = (infGraph.hBitmapMask != NULL);
1196     TERM_LEN w1 = infGraph.CellWidth;
1197     TERM_LEN h1 = infGraph.CellHeight;
1198     TERM_LEN tw1 = infGraph.TileWidth;
1199     TERM_LEN th1 = infGraph.TileHeight;
1200     TERM_LEN w2, h2, tw2 = 0;
1201     w2 = td->tile_wid;
1202     h2 = td->tile_hgt;
1203     tw2 = w2;
1204     if (use_bigtile)
1205         tw2 *= 2;
1206
1207     TERM_LEN x2 = x * w2 + td->size_ow1 + infGraph.OffsetX;
1208     TERM_LEN y2 = y * h2 + td->size_oh1 + infGraph.OffsetY;
1209     HDC hdc = GetDC(td->w);
1210     HDC hdcSrc = CreateCompatibleDC(hdc);
1211     HBITMAP hbmSrcOld = static_cast<HBITMAP>(SelectObject(hdcSrc, infGraph.hBitmap));
1212
1213     if (has_mask) {
1214         hdcMask = CreateCompatibleDC(hdc);
1215         SelectObject(hdcMask, infGraph.hBitmapMask);
1216     }
1217
1218     for (i = 0; i < n; i++, x2 += w2) {
1219         TERM_COLOR a = ap[i];
1220         char c = cp[i];
1221         int row = (a & 0x7F);
1222         int col = (c & 0x7F);
1223         TERM_LEN x1 = col * w1;
1224         TERM_LEN y1 = row * h1;
1225
1226         if (has_mask) {
1227             TERM_LEN x3 = (tcp[i] & 0x7F) * w1;
1228             TERM_LEN y3 = (tap[i] & 0x7F) * h1;
1229             tw2 = tw2 * w1 / tw1;
1230             h2 = h2 * h1 / th1;
1231             if ((tw1 == tw2) && (th1 == h2)) {
1232                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
1233                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
1234                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
1235                 continue;
1236             }
1237
1238             SetStretchBltMode(hdc, COLORONCOLOR);
1239             StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x3, y3, w1, h1, SRCAND);
1240             StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCPAINT);
1241             if ((x1 != x3) || (y1 != y3)) {
1242                 StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
1243                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
1244             }
1245
1246             continue;
1247         }
1248
1249         if ((w1 == tw2) && (h1 == h2)) {
1250             BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
1251             continue;
1252         }
1253
1254         SetStretchBltMode(hdc, COLORONCOLOR);
1255         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
1256     }
1257
1258     SelectObject(hdcSrc, hbmSrcOld);
1259     DeleteDC(hdcSrc);
1260     if (has_mask) {
1261         SelectObject(hdcMask, hbmSrcOld);
1262         DeleteDC(hdcMask);
1263     }
1264
1265     ReleaseDC(td->w, hdc);
1266     return 0;
1267 }
1268
1269 /*!
1270  * @brief Create and initialize a "term_data" given a title
1271  */
1272 static void term_data_link(term_data *td)
1273 {
1274     term_type *t = &td->t;
1275     term_init(t, td->cols, td->rows, FILE_READ_BUFF_SIZE);
1276     t->soft_cursor = TRUE;
1277     t->higher_pict = TRUE;
1278     t->attr_blank = TERM_WHITE;
1279     t->char_blank = ' ';
1280     t->user_hook = term_user_win;
1281     t->xtra_hook = term_xtra_win;
1282     t->curs_hook = term_curs_win;
1283     t->bigcurs_hook = term_bigcurs_win;
1284     t->wipe_hook = term_wipe_win;
1285     t->text_hook = term_text_win;
1286     t->pict_hook = term_pict_win;
1287     t->data = (vptr)(td);
1288 }
1289
1290 /*!
1291  * @brief Create the windows
1292  * @details
1293  * First, instantiate the "default" values, then read the "ini_file"
1294  * to over-ride selected values, then create the windows, and fonts.
1295  *
1296  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
1297  * must make active to realize the palette.
1298  */
1299 static void init_windows(void)
1300 {
1301     term_data *td;
1302     td = &data[0];
1303     WIPE(td, term_data);
1304     td->name = win_term_name[0];
1305
1306     td->rows = 24;
1307     td->cols = 80;
1308     td->visible = TRUE;
1309     td->size_ow1 = 2;
1310     td->size_ow2 = 2;
1311     td->size_oh1 = 2;
1312     td->size_oh2 = 2;
1313     td->pos_x = 7 * 30;
1314     td->pos_y = 7 * 20;
1315     td->posfix = FALSE;
1316
1317     for (int i = 1; i < MAX_TERM_DATA; i++) {
1318         td = &data[i];
1319         WIPE(td, term_data);
1320         td->name = win_term_name[i];
1321         td->rows = 24;
1322         td->cols = 80;
1323         td->visible = FALSE;
1324         td->size_ow1 = 1;
1325         td->size_ow2 = 1;
1326         td->size_oh1 = 1;
1327         td->size_oh2 = 1;
1328         td->pos_x = (7 - i) * 30;
1329         td->pos_y = (7 - i) * 20;
1330         td->posfix = FALSE;
1331     }
1332
1333     load_prefs();
1334
1335     /* Atrributes of main window */
1336     td = &data[0];
1337     td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_VISIBLE);
1338     td->dwExStyle = 0;
1339     td->visible = TRUE;
1340
1341     /* Attributes of sub windows */
1342     for (int i = 1; i < MAX_TERM_DATA; i++) {
1343         td = &data[i];
1344         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
1345         td->dwExStyle = (WS_EX_TOOLWINDOW);
1346     }
1347
1348     /* Font of each window */
1349     for (int i = 0; i < MAX_TERM_DATA; i++) {
1350         td = &data[i];
1351         wcsncpy(td->lf.lfFaceName, to_wchar(td->font_want).wc_str(), LF_FACESIZE);
1352         td->lf.lfCharSet = _(SHIFTJIS_CHARSET, DEFAULT_CHARSET);
1353         td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1354         term_force_font(td);
1355         if (!td->tile_wid)
1356             td->tile_wid = td->font_wid;
1357         if (!td->tile_hgt)
1358             td->tile_hgt = td->font_hgt;
1359         term_getsize(td);
1360         term_window_resize(td);
1361     }
1362
1363     /* Create sub windows */
1364     for (int i = MAX_TERM_DATA - 1; i >= 1; --i) {
1365         td = &data[i];
1366
1367         my_td = td;
1368         td->w = CreateWindowExW(
1369             td->dwExStyle, AngList, td->name, td->dwStyle, td->pos_x, td->pos_y, td->size_wid, td->size_hgt, HWND_DESKTOP, NULL, hInstance, NULL);
1370         my_td = NULL;
1371
1372         if (!td->w)
1373             quit(_("サブウィンドウに作成に失敗しました", "Failed to create sub-window"));
1374
1375         td->size_hack = TRUE;
1376         term_getsize(td);
1377         term_window_resize(td);
1378
1379         if (td->visible) {
1380             ShowWindow(td->w, SW_SHOW);
1381         }
1382         td->size_hack = FALSE;
1383
1384         term_data_link(td);
1385         angband_term[i] = &td->t;
1386
1387         if (td->visible) {
1388             /* Activate the window */
1389             SetActiveWindow(td->w);
1390         }
1391
1392         if (td->posfix) {
1393             term_window_pos(td, HWND_TOPMOST);
1394         } else {
1395             term_window_pos(td, td->w);
1396         }
1397     }
1398
1399     /* Create main window */
1400     td = &data[0];
1401     my_td = td;
1402     td->w = CreateWindowExW(
1403         td->dwExStyle, AppName, _(L"変愚蛮怒", td->name), td->dwStyle, td->pos_x, td->pos_y, td->size_wid, td->size_hgt, HWND_DESKTOP, NULL, hInstance, NULL);
1404     my_td = NULL;
1405
1406     if (!td->w)
1407         quit(_("メインウィンドウの作成に失敗しました", "Failed to create Angband window"));
1408
1409     /* Resize */
1410     td->size_hack = TRUE;
1411     term_getsize(td);
1412     term_window_resize(td);
1413     td->size_hack = FALSE;
1414
1415     term_data_link(td);
1416     angband_term[0] = &td->t;
1417     normsize.x = td->cols;
1418     normsize.y = td->rows;
1419
1420     if (win_maximized)
1421         ShowWindow(td->w, SW_SHOWMAXIMIZED);
1422     else
1423         ShowWindow(td->w, SW_SHOW);
1424
1425     SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1426     hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
1427     (void)term_xtra_win_flush();
1428 }
1429
1430 /*!
1431  * @brief Prepare the menus
1432  */
1433 static void setup_menus(void)
1434 {
1435     HMENU hm = GetMenu(data[0].w);
1436
1437     if (current_world_ptr->character_generated) {
1438         EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1439         EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1440         EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_ENABLED);
1441     } else {
1442         EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
1443         EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
1444         EnableMenuItem(hm, IDM_FILE_SAVE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1445     }
1446
1447     for (int i = 0; i < MAX_TERM_DATA; i++) {
1448         EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1449         CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i, (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
1450         EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i, MF_BYCOMMAND | MF_ENABLED);
1451     }
1452
1453     for (int i = 0; i < MAX_TERM_DATA; i++) {
1454         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1455
1456         if (data[i].visible) {
1457             EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i, MF_BYCOMMAND | MF_ENABLED);
1458         }
1459     }
1460
1461     for (int i = 0; i < MAX_TERM_DATA; i++) {
1462         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1463         CheckMenuItem(hm, IDM_WINDOW_POS_0 + i, (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
1464         if (data[i].visible) {
1465             EnableMenuItem(hm, IDM_WINDOW_POS_0 + i, MF_BYCOMMAND | MF_ENABLED);
1466         }
1467     }
1468
1469     for (int i = 0; i < MAX_TERM_DATA; i++) {
1470         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1471         if (data[i].visible) {
1472             EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i, MF_BYCOMMAND | MF_ENABLED);
1473         }
1474     }
1475
1476     for (int i = 0; i < MAX_TERM_DATA; i++) {
1477         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1478         if (data[i].visible) {
1479             EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i, MF_BYCOMMAND | MF_ENABLED);
1480         }
1481     }
1482
1483     for (int i = 0; i < MAX_TERM_DATA; i++) {
1484         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1485         if (data[i].visible) {
1486             EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i, MF_BYCOMMAND | MF_ENABLED);
1487         }
1488     }
1489
1490     for (int i = 0; i < MAX_TERM_DATA; i++) {
1491         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1492
1493         if (data[i].visible) {
1494             EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i, MF_BYCOMMAND | MF_ENABLED);
1495         }
1496     }
1497     CheckMenuItem(hm, IDM_WINDOW_KEEP_SUBWINDOWS, (keep_subwindows ? MF_CHECKED : MF_UNCHECKED));
1498
1499     CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_NONE) ? MF_CHECKED : MF_UNCHECKED));
1500     CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_ORIGINAL) ? MF_CHECKED : MF_UNCHECKED));
1501     CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_ADAM_BOLT) ? MF_CHECKED : MF_UNCHECKED));
1502     CheckMenuItem(hm, IDM_OPTIONS_NEW2_GRAPHICS, (arg_graphics == static_cast<byte>(graphics_mode::GRAPHICS_HENGBAND) ? MF_CHECKED : MF_UNCHECKED));
1503     CheckMenuItem(hm, IDM_OPTIONS_BIGTILE, (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
1504     CheckMenuItem(hm, IDM_OPTIONS_MUSIC, (arg_music ? MF_CHECKED : MF_UNCHECKED));
1505     CheckMenuItem(hm, IDM_OPTIONS_MUSIC_PAUSE_INACTIVE, (use_pause_music_inactive ? MF_CHECKED : MF_UNCHECKED));
1506     CheckMenuItem(hm, IDM_OPTIONS_SOUND, (arg_sound ? MF_CHECKED : MF_UNCHECKED));
1507     CheckMenuItem(hm, IDM_OPTIONS_NO_BG, ((current_bg_mode == bg_mode::BG_NONE) ? MF_CHECKED : MF_UNCHECKED));
1508     CheckMenuItem(hm, IDM_OPTIONS_BG, ((current_bg_mode == bg_mode::BG_ONE) ? MF_CHECKED : MF_UNCHECKED));
1509     CheckMenuItem(hm, IDM_OPTIONS_PRESET_BG, ((current_bg_mode == bg_mode::BG_PRESET) ? MF_CHECKED : MF_UNCHECKED));
1510     // TODO IDM_OPTIONS_PRESET_BG を有効にする
1511     EnableMenuItem(hm, IDM_OPTIONS_PRESET_BG, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
1512 }
1513
1514 /*!
1515  * @brief Check for double clicked (or dragged) savefile
1516  * @details
1517  * Apparently, Windows copies the entire filename into the first
1518  * piece of the "command line string".  Perhaps we should extract
1519  * the "basename" of that filename and append it to the "save" dir.
1520  * @param savefile_option savefile path
1521  */
1522 static void check_for_save_file(const std::string &savefile_option)
1523 {
1524     if (savefile_option.empty())
1525         return;
1526
1527     strcpy(savefile, savefile_option.c_str());
1528     validate_file(savefile);
1529     game_in_progress = true;
1530 }
1531
1532 /*!
1533  * @brief Process a menu command
1534  */
1535 static void process_menus(player_type *player_ptr, WORD wCmd)
1536 {
1537     if (!initialized) {
1538         plog(_("まだ初期化中です...", "You cannot do that yet..."));
1539         return;
1540     }
1541
1542     term_data *td;
1543     OPENFILENAMEW ofn;
1544     switch (wCmd) {
1545     case IDM_FILE_NEW: {
1546         if (game_in_progress || movie_in_progress) {
1547             plog(_("プレイ中は新しいゲームを始めることができません!", "You can't start a new game while you're still playing!"));
1548         } else {
1549             game_in_progress = true;
1550             savefile[0] = '\0';
1551         }
1552
1553         break;
1554     }
1555     case IDM_FILE_OPEN: {
1556         if (game_in_progress || movie_in_progress) {
1557             plog(_("プレイ中はゲームをロードすることができません!", "You can't open a new game while you're still playing!"));
1558         } else {
1559             memset(&ofn, 0, sizeof(ofn));
1560             ofn.lStructSize = sizeof(ofn);
1561             ofn.hwndOwner = data[0].w;
1562             ofn.lpstrFilter = L"Save Files (*.)\0*\0";
1563             ofn.nFilterIndex = 1;
1564             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
1565
1566             if (get_open_filename(&ofn, ANGBAND_DIR_SAVE, savefile, MAIN_WIN_MAX_PATH)) {
1567                 validate_file(savefile);
1568                 game_in_progress = true;
1569             }
1570         }
1571
1572         break;
1573     }
1574     case IDM_FILE_SAVE: {
1575         if (game_in_progress && current_world_ptr->character_generated) {
1576             if (!can_save) {
1577                 plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
1578                 break;
1579             }
1580
1581             msg_flag = FALSE;
1582             do_cmd_save_game(player_ptr, FALSE);
1583         } else {
1584             plog(_("今、セーブすることは出来ません。", "You may not do that right now."));
1585         }
1586
1587         break;
1588     }
1589     case IDM_FILE_EXIT: {
1590         if (game_in_progress && current_world_ptr->character_generated) {
1591             if (!can_save) {
1592                 plog(_("今は終了できません。", "You may not do that right now."));
1593                 break;
1594             }
1595
1596             msg_flag = FALSE;
1597             forget_lite(player_ptr->current_floor_ptr);
1598             forget_view(player_ptr->current_floor_ptr);
1599             clear_mon_lite(player_ptr->current_floor_ptr);
1600
1601             term_key_push(SPECIAL_KEY_QUIT);
1602             break;
1603         }
1604
1605         quit(NULL);
1606         break;
1607     }
1608     case IDM_FILE_SCORE: {
1609         char buf[1024];
1610         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
1611         highscore_fd = fd_open(buf, O_RDONLY);
1612         if (highscore_fd < 0) {
1613             msg_print("Score file unavailable.");
1614         } else {
1615             screen_save();
1616             term_clear();
1617             display_scores(0, MAX_HISCORES, -1, NULL);
1618             (void)fd_close(highscore_fd);
1619             highscore_fd = -1;
1620             screen_load();
1621             term_fresh();
1622         }
1623
1624         break;
1625     }
1626     case IDM_FILE_MOVIE: {
1627         if (game_in_progress || movie_in_progress) {
1628             plog(_("プレイ中はムービーをロードすることができません!", "You can't open a movie while you're playing!"));
1629         } else {
1630             memset(&ofn, 0, sizeof(ofn));
1631             ofn.lStructSize = sizeof(ofn);
1632             ofn.hwndOwner = data[0].w;
1633             ofn.lpstrFilter = L"Angband Movie Files (*.amv)\0*.amv\0";
1634             ofn.nFilterIndex = 1;
1635             ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
1636
1637             if (get_open_filename(&ofn, ANGBAND_DIR_USER, savefile, MAIN_WIN_MAX_PATH)) {
1638                 prepare_browse_movie_without_path_build(savefile);
1639                 movie_in_progress = true;
1640             }
1641         }
1642
1643         break;
1644     }
1645     case IDM_WINDOW_VIS_0: {
1646         plog(_("メインウィンドウは非表示にできません!", "You are not allowed to do that!"));
1647         break;
1648     }
1649     case IDM_WINDOW_VIS_1:
1650     case IDM_WINDOW_VIS_2:
1651     case IDM_WINDOW_VIS_3:
1652     case IDM_WINDOW_VIS_4:
1653     case IDM_WINDOW_VIS_5:
1654     case IDM_WINDOW_VIS_6:
1655     case IDM_WINDOW_VIS_7: {
1656         int i = wCmd - IDM_WINDOW_VIS_0;
1657         if ((i < 0) || (i >= MAX_TERM_DATA))
1658             break;
1659
1660         td = &data[i];
1661         if (!td->visible) {
1662             td->visible = TRUE;
1663             ShowWindow(td->w, SW_SHOW);
1664             term_data_redraw(td);
1665         } else {
1666             td->visible = FALSE;
1667             td->posfix = FALSE;
1668             ShowWindow(td->w, SW_HIDE);
1669         }
1670
1671         break;
1672     }
1673     case IDM_WINDOW_FONT_0:
1674     case IDM_WINDOW_FONT_1:
1675     case IDM_WINDOW_FONT_2:
1676     case IDM_WINDOW_FONT_3:
1677     case IDM_WINDOW_FONT_4:
1678     case IDM_WINDOW_FONT_5:
1679     case IDM_WINDOW_FONT_6:
1680     case IDM_WINDOW_FONT_7: {
1681         int i = wCmd - IDM_WINDOW_FONT_0;
1682         if ((i < 0) || (i >= MAX_TERM_DATA))
1683             break;
1684
1685         td = &data[i];
1686         term_change_font(td);
1687         break;
1688     }
1689     case IDM_WINDOW_POS_1:
1690     case IDM_WINDOW_POS_2:
1691     case IDM_WINDOW_POS_3:
1692     case IDM_WINDOW_POS_4:
1693     case IDM_WINDOW_POS_5:
1694     case IDM_WINDOW_POS_6:
1695     case IDM_WINDOW_POS_7: {
1696         int i = wCmd - IDM_WINDOW_POS_0;
1697         if ((i < 0) || (i >= MAX_TERM_DATA))
1698             break;
1699
1700         td = &data[i];
1701         if (!td->posfix && td->visible) {
1702             td->posfix = TRUE;
1703             term_window_pos(td, HWND_TOPMOST);
1704         } else {
1705             td->posfix = FALSE;
1706             term_window_pos(td, data[0].w);
1707         }
1708
1709         break;
1710     }
1711     case IDM_WINDOW_I_WID_0:
1712     case IDM_WINDOW_I_WID_1:
1713     case IDM_WINDOW_I_WID_2:
1714     case IDM_WINDOW_I_WID_3:
1715     case IDM_WINDOW_I_WID_4:
1716     case IDM_WINDOW_I_WID_5:
1717     case IDM_WINDOW_I_WID_6:
1718     case IDM_WINDOW_I_WID_7: {
1719         int i = wCmd - IDM_WINDOW_I_WID_0;
1720         if ((i < 0) || (i >= MAX_TERM_DATA))
1721             break;
1722
1723         td = &data[i];
1724         td->tile_wid += 1;
1725         term_getsize(td);
1726         term_window_resize(td);
1727         break;
1728     }
1729     case IDM_WINDOW_D_WID_0:
1730     case IDM_WINDOW_D_WID_1:
1731     case IDM_WINDOW_D_WID_2:
1732     case IDM_WINDOW_D_WID_3:
1733     case IDM_WINDOW_D_WID_4:
1734     case IDM_WINDOW_D_WID_5:
1735     case IDM_WINDOW_D_WID_6:
1736     case IDM_WINDOW_D_WID_7: {
1737         int i = wCmd - IDM_WINDOW_D_WID_0;
1738         if ((i < 0) || (i >= MAX_TERM_DATA))
1739             break;
1740
1741         td = &data[i];
1742         td->tile_wid -= 1;
1743         term_getsize(td);
1744         term_window_resize(td);
1745         break;
1746     }
1747     case IDM_WINDOW_I_HGT_0:
1748     case IDM_WINDOW_I_HGT_1:
1749     case IDM_WINDOW_I_HGT_2:
1750     case IDM_WINDOW_I_HGT_3:
1751     case IDM_WINDOW_I_HGT_4:
1752     case IDM_WINDOW_I_HGT_5:
1753     case IDM_WINDOW_I_HGT_6:
1754     case IDM_WINDOW_I_HGT_7: {
1755         int i = wCmd - IDM_WINDOW_I_HGT_0;
1756         if ((i < 0) || (i >= MAX_TERM_DATA))
1757             break;
1758
1759         td = &data[i];
1760         td->tile_hgt += 1;
1761         term_getsize(td);
1762         term_window_resize(td);
1763         break;
1764     }
1765     case IDM_WINDOW_D_HGT_0:
1766     case IDM_WINDOW_D_HGT_1:
1767     case IDM_WINDOW_D_HGT_2:
1768     case IDM_WINDOW_D_HGT_3:
1769     case IDM_WINDOW_D_HGT_4:
1770     case IDM_WINDOW_D_HGT_5:
1771     case IDM_WINDOW_D_HGT_6:
1772     case IDM_WINDOW_D_HGT_7: {
1773         int i = wCmd - IDM_WINDOW_D_HGT_0;
1774         if ((i < 0) || (i >= MAX_TERM_DATA))
1775             break;
1776
1777         td = &data[i];
1778         td->tile_hgt -= 1;
1779         term_getsize(td);
1780         term_window_resize(td);
1781         break;
1782     }
1783     case IDM_WINDOW_KEEP_SUBWINDOWS: {
1784         keep_subwindows = !keep_subwindows;
1785         break;
1786     }
1787     case IDM_OPTIONS_NO_GRAPHICS: {
1788         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_NONE)) {
1789             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_NONE);
1790             if (game_in_progress)
1791                 do_cmd_redraw(player_ptr);
1792         }
1793         break;
1794     }
1795     case IDM_OPTIONS_OLD_GRAPHICS: {
1796         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_ORIGINAL)) {
1797             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_ORIGINAL);
1798             if (game_in_progress)
1799                 do_cmd_redraw(player_ptr);
1800         }
1801
1802         break;
1803     }
1804     case IDM_OPTIONS_NEW_GRAPHICS: {
1805         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_ADAM_BOLT)) {
1806             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_ADAM_BOLT);
1807             if (game_in_progress)
1808                 do_cmd_redraw(player_ptr);
1809         }
1810
1811         break;
1812     }
1813     case IDM_OPTIONS_NEW2_GRAPHICS: {
1814         if (arg_graphics != static_cast<byte>(graphics_mode::GRAPHICS_HENGBAND)) {
1815             arg_graphics = static_cast<byte>(graphics_mode::GRAPHICS_HENGBAND);
1816             if (game_in_progress)
1817                 do_cmd_redraw(player_ptr);
1818         }
1819
1820         break;
1821     }
1822     case IDM_OPTIONS_BIGTILE: {
1823         td = &data[0];
1824         arg_bigtile = !arg_bigtile;
1825         term_activate(&td->t);
1826         term_resize(td->cols, td->rows);
1827         InvalidateRect(td->w, NULL, TRUE);
1828         break;
1829     }
1830     case IDM_OPTIONS_MUSIC: {
1831         arg_music = !arg_music;
1832         use_music = arg_music;
1833         if (use_music) {
1834             init_music();
1835             if (game_in_progress)
1836                 select_floor_music(player_ptr);
1837         } else {
1838             main_win_music::stop_music();
1839         }
1840         break;
1841     }
1842     case IDM_OPTIONS_MUSIC_PAUSE_INACTIVE: {
1843         use_pause_music_inactive = !use_pause_music_inactive;
1844         break;
1845     }
1846     case IDM_OPTIONS_OPEN_MUSIC_DIR: {
1847         std::vector<char> buf(MAIN_WIN_MAX_PATH);
1848         path_build(&buf[0], MAIN_WIN_MAX_PATH, ANGBAND_DIR_XTRA_MUSIC, "music.cfg");
1849         open_dir_in_explorer(&buf[0]);
1850         break;
1851     }
1852     case IDM_OPTIONS_SOUND: {
1853         arg_sound = !arg_sound;
1854         change_sound_mode(arg_sound);
1855         break;
1856     }
1857     case IDM_OPTIONS_OPEN_SOUND_DIR: {
1858         std::vector<char> buf(MAIN_WIN_MAX_PATH);
1859         path_build(&buf[0], MAIN_WIN_MAX_PATH, ANGBAND_DIR_XTRA_SOUND, "sound.cfg");
1860         open_dir_in_explorer(&buf[0]);
1861         break;
1862     }
1863     case IDM_OPTIONS_NO_BG: {
1864         change_bg_mode(bg_mode::BG_NONE);
1865         break;
1866     }
1867     case IDM_OPTIONS_PRESET_BG: {
1868         change_bg_mode(bg_mode::BG_PRESET);
1869         break;
1870     }
1871     case IDM_OPTIONS_BG: {
1872         if (change_bg_mode(bg_mode::BG_ONE))
1873             break;
1874         // 壁紙の設定に失敗した(ファイルが存在しない等)場合、壁紙に使うファイルを選択させる
1875     }
1876         [[fallthrough]]; /* Fall through */
1877     case IDM_OPTIONS_OPEN_BG: {
1878         memset(&ofn, 0, sizeof(ofn));
1879         ofn.lStructSize = sizeof(ofn);
1880         ofn.hwndOwner = data[0].w;
1881         ofn.lpstrFilter = L"Image Files (*.bmp;*.png;*.jpg;*.jpeg;)\0*.bmp;*.png;*.jpg;*.jpeg;\0";
1882         ofn.nFilterIndex = 1;
1883         ofn.lpstrTitle = _(L"壁紙を選んでね。", L"Choose wall paper.");
1884         ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
1885
1886         if (get_open_filename(&ofn, NULL, wallpaper_file, MAIN_WIN_MAX_PATH)) {
1887             change_bg_mode(bg_mode::BG_ONE, true, true);
1888         }
1889         break;
1890     }
1891     case IDM_DUMP_SCREEN_HTML: {
1892         save_screen_as_html(data[0].w);
1893         break;
1894     }
1895     }
1896 }
1897
1898 /*!
1899  * @brief Add a keypress to the "queue"
1900  */
1901 static errr term_keypress(int k)
1902 {
1903     /* Refuse to enqueue non-keys */
1904     if (!k)
1905         return -1;
1906
1907     /* Store the char, advance the queue */
1908     Term->key_queue[Term->key_head++] = (char)k;
1909
1910     /* Circular queue, handle wrap */
1911     if (Term->key_head == Term->key_size)
1912         Term->key_head = 0;
1913
1914     if (Term->key_head != Term->key_tail)
1915         return 0;
1916
1917     return 1;
1918 }
1919
1920 /*!
1921  * @brief Add a keypress to the "queue"
1922  * @details マルチバイト文字をkey_queueに追加する。
1923  * @param str マルチバイト文字列
1924  */
1925 static void term_keypress(char *str)
1926 {
1927     if (str) {
1928         char *psrc = str;
1929         while (*psrc) {
1930             term_keypress(*psrc);
1931             ++psrc;
1932         }
1933     }
1934 }
1935
1936 /*!
1937  * @brief キーダウンのハンドラ
1938  */
1939 static bool process_keydown(WPARAM wParam, LPARAM lParam)
1940 {
1941     bool mc = FALSE;
1942     bool ms = FALSE;
1943     bool ma = FALSE;
1944
1945     if (GetKeyState(VK_CONTROL) & 0x8000)
1946         mc = TRUE;
1947     if (GetKeyState(VK_SHIFT) & 0x8000)
1948         ms = TRUE;
1949     if (GetKeyState(VK_MENU) & 0x8000)
1950         ma = TRUE;
1951
1952     term_no_press = (ma) ? TRUE : FALSE;
1953     if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)])) {
1954         bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
1955         bool numpad = FALSE;
1956
1957         term_keypress(31);
1958         if (mc)
1959             term_keypress('C');
1960         if (ms)
1961             term_keypress('S');
1962         if (ma)
1963             term_keypress('A');
1964
1965         int i = LOBYTE(HIWORD(lParam));
1966         term_keypress('x');
1967         switch (wParam) {
1968         case VK_DIVIDE:
1969             term_no_press = TRUE;
1970             [[fallthrough]]; /* Fall through */
1971         case VK_RETURN:
1972             numpad = ext_key;
1973             break;
1974         case VK_NUMPAD0:
1975         case VK_NUMPAD1:
1976         case VK_NUMPAD2:
1977         case VK_NUMPAD3:
1978         case VK_NUMPAD4:
1979         case VK_NUMPAD5:
1980         case VK_NUMPAD6:
1981         case VK_NUMPAD7:
1982         case VK_NUMPAD8:
1983         case VK_NUMPAD9:
1984         case VK_ADD:
1985         case VK_MULTIPLY:
1986         case VK_SUBTRACT:
1987         case VK_SEPARATOR:
1988         case VK_DECIMAL:
1989             term_no_press = TRUE;
1990             [[fallthrough]]; /* Fall through */
1991         case VK_CLEAR:
1992         case VK_HOME:
1993         case VK_END:
1994         case VK_PRIOR:
1995         case VK_NEXT:
1996         case VK_INSERT:
1997         case VK_DELETE:
1998         case VK_UP:
1999         case VK_DOWN:
2000         case VK_LEFT:
2001         case VK_RIGHT:
2002             numpad = !ext_key;
2003         }
2004
2005         if (numpad)
2006             term_keypress('K');
2007
2008         term_keypress(hexsym[i / 16]);
2009         term_keypress(hexsym[i % 16]);
2010         term_keypress(13);
2011
2012         return 1;
2013     }
2014
2015     return 0;
2016 }
2017
2018 /*!
2019  * @brief ウィンドウのアクティブ/非アクティブのハンドラ
2020  */
2021 static void handle_app_active(HWND hWnd, UINT uMsg, WPARAM wParam, [[maybe_unused]] LPARAM lParam)
2022 {
2023     switch (uMsg) {
2024     case WM_ACTIVATEAPP: {
2025         if (wParam) {
2026             if (use_pause_music_inactive)
2027                 main_win_music::resume_music();
2028         } else {
2029             if (use_pause_music_inactive)
2030                 main_win_music::pause_music();
2031         }
2032         break;
2033     }
2034     case WM_WINDOWPOSCHANGING: {
2035         if (!IsIconic(hWnd))
2036             if (use_pause_music_inactive)
2037                 main_win_music::resume_music();
2038         break;
2039     }
2040     }
2041 }
2042
2043 /*!
2044  * @brief メインウインドウ用ウインドウプロシージャ
2045  */
2046 LRESULT PASCAL AngbandWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2047 {
2048     PAINTSTRUCT ps;
2049     term_data *td;
2050     td = (term_data *)GetWindowLong(hWnd, 0);
2051
2052     handle_app_active(hWnd, uMsg, wParam, lParam);
2053
2054     switch (uMsg) {
2055     case WM_NCCREATE: {
2056         SetWindowLong(hWnd, 0, (LONG)(my_td));
2057         break;
2058     }
2059     case WM_CREATE: {
2060         setup_mci(hWnd);
2061         return 0;
2062     }
2063     case WM_GETMINMAXINFO: {
2064         MINMAXINFO *lpmmi;
2065         RECT rc;
2066
2067         lpmmi = (MINMAXINFO *)lParam;
2068         if (!td)
2069             return 1;
2070
2071         rc.left = rc.top = 0;
2072         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
2073         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
2074
2075         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
2076
2077         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
2078         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
2079
2080         return 0;
2081     }
2082     case WM_ERASEBKGND: {
2083         return 1;
2084     }
2085     case WM_PAINT: {
2086         BeginPaint(hWnd, &ps);
2087         if (td)
2088             term_data_redraw(td);
2089         EndPaint(hWnd, &ps);
2090         ValidateRect(hWnd, NULL);
2091         return 0;
2092     }
2093     case MM_MCINOTIFY: {
2094         main_win_music::on_mci_notify(wParam, lParam);
2095
2096         return 0;
2097     }
2098     case WM_SYSKEYDOWN:
2099     case WM_KEYDOWN: {
2100         if (process_keydown(wParam, lParam))
2101             return 0;
2102         break;
2103     }
2104     case WM_CHAR: {
2105         // wParam is WCHAR because using RegisterClassW
2106         if (term_no_press)
2107             term_no_press = FALSE;
2108         else {
2109             WCHAR wc[2] = { (WCHAR)wParam, '\0' };
2110             term_keypress(to_multibyte(wc).c_str());
2111         }
2112         return 0;
2113     }
2114     case WM_LBUTTONDOWN: {
2115         if (macro_running())
2116             return 0;
2117         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
2118         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
2119         mouse_down = TRUE;
2120         oldx = mousex;
2121         oldy = mousey;
2122         return 0;
2123     }
2124     case WM_LBUTTONUP: {
2125         if (!mouse_down)
2126             return 0;
2127         HGLOBAL hGlobal;
2128         LPSTR lpStr;
2129         TERM_LEN dx = abs(oldx - mousex) + 1;
2130         TERM_LEN dy = abs(oldy - mousey) + 1;
2131         TERM_LEN ox = (oldx > mousex) ? mousex : oldx;
2132         TERM_LEN oy = (oldy > mousey) ? mousey : oldy;
2133
2134         mouse_down = FALSE;
2135         paint_rect = FALSE;
2136
2137 #ifdef JP
2138         int sz = (dx + 3) * dy;
2139 #else
2140         int sz = (dx + 2) * dy;
2141 #endif
2142         hGlobal = GlobalAlloc(GHND, sz + 1);
2143         if (hGlobal == NULL)
2144             return 0;
2145         lpStr = (LPSTR)GlobalLock(hGlobal);
2146
2147         for (int i = 0; i < dy; i++) {
2148 #ifdef JP
2149             char *s;
2150             const auto &scr = data[0].t.scr->c;
2151
2152             C_MAKE(s, (dx + 1), char);
2153             strncpy(s, &scr[oy + i][ox], dx);
2154
2155             if (ox > 0) {
2156                 if (iskanji(scr[oy + i][ox - 1]))
2157                     s[0] = ' ';
2158             }
2159
2160             if (ox + dx < data[0].cols) {
2161                 if (iskanji(scr[oy + i][ox + dx - 1]))
2162                     s[dx - 1] = ' ';
2163             }
2164
2165             for (int j = 0; j < dx; j++) {
2166                 if (s[j] == 127)
2167                     s[j] = '#';
2168                 *lpStr++ = s[j];
2169             }
2170 #else
2171             for (int j = 0; j < dx; j++) {
2172                 *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
2173             }
2174 #endif
2175             if (dy > 1) {
2176                 *lpStr++ = '\r';
2177                 *lpStr++ = '\n';
2178             }
2179         }
2180
2181         GlobalUnlock(hGlobal);
2182         if (OpenClipboard(hWnd) == 0) {
2183             GlobalFree(hGlobal);
2184             return 0;
2185         }
2186
2187         EmptyClipboard();
2188         SetClipboardData(CF_TEXT, hGlobal);
2189         CloseClipboard();
2190         term_redraw();
2191         return 0;
2192     }
2193     case WM_MOUSEMOVE: {
2194         if (!mouse_down)
2195             return 0;
2196
2197         int dx, dy;
2198         int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
2199         int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
2200         int ox, oy;
2201
2202         if (paint_rect) {
2203             dx = abs(oldx - mousex) + 1;
2204             dy = abs(oldy - mousey) + 1;
2205             ox = (oldx > mousex) ? mousex : oldx;
2206             oy = (oldy > mousey) ? mousey : oldy;
2207             term_inversed_area(hWnd, ox, oy, dx, dy);
2208         } else {
2209             paint_rect = TRUE;
2210         }
2211
2212         dx = abs(cx - mousex) + 1;
2213         dy = abs(cy - mousey) + 1;
2214         ox = (cx > mousex) ? mousex : cx;
2215         oy = (cy > mousey) ? mousey : cy;
2216         term_inversed_area(hWnd, ox, oy, dx, dy);
2217
2218         oldx = cx;
2219         oldy = cy;
2220         return 0;
2221     }
2222     case WM_INITMENU: {
2223         setup_menus();
2224         return 0;
2225     }
2226     case WM_CLOSE: {
2227         if (!game_in_progress || !current_world_ptr->character_generated) {
2228             quit(NULL);
2229             return 0;
2230         }
2231
2232         if (!can_save) {
2233             plog(_("今は終了できません。", "You may not do that right now."));
2234             return 0;
2235         }
2236
2237         msg_flag = FALSE;
2238         forget_lite(p_ptr->current_floor_ptr);
2239         forget_view(p_ptr->current_floor_ptr);
2240         clear_mon_lite(p_ptr->current_floor_ptr);
2241         term_key_push(SPECIAL_KEY_QUIT);
2242         return 0;
2243     }
2244     case WM_QUERYENDSESSION: {
2245         if (!game_in_progress || !current_world_ptr->character_generated) {
2246             quit(NULL);
2247             return 0;
2248         }
2249
2250         msg_flag = FALSE;
2251         if (p_ptr->chp < 0)
2252             p_ptr->is_dead = FALSE;
2253         exe_write_diary(p_ptr, DIARY_GAMESTART, 0, _("----ゲーム中断----", "---- Save and Exit Game ----"));
2254
2255         p_ptr->panic_save = 1;
2256         signals_ignore_tstp();
2257         (void)strcpy(p_ptr->died_from, _("(緊急セーブ)", "(panic save)"));
2258         (void)save_player(p_ptr, SAVE_TYPE_CLOSE_GAME);
2259         quit(NULL);
2260         return 0;
2261     }
2262     case WM_QUIT: {
2263         quit(NULL);
2264         return 0;
2265     }
2266     case WM_COMMAND: {
2267         process_menus(p_ptr, LOWORD(wParam));
2268         return 0;
2269     }
2270     case WM_SIZE: {
2271         if (!td)
2272             return 1;
2273         if (!td->w)
2274             return 1;
2275         if (td->size_hack)
2276             return 1;
2277
2278         //!< @todo 二重のswitch文。後で分割する.
2279         switch (wParam) {
2280         case SIZE_MINIMIZED: {
2281             for (int i = 1; i < MAX_TERM_DATA; i++) {
2282                 if (data[i].visible)
2283                     ShowWindow(data[i].w, SW_HIDE);
2284             }
2285
2286             return 0;
2287         }
2288         case SIZE_MAXIMIZED:
2289         case SIZE_RESTORED: {
2290             TERM_LEN cols = (LOWORD(lParam) - td->size_ow1 - td->size_ow2) / td->tile_wid;
2291             TERM_LEN rows = (HIWORD(lParam) - td->size_oh1 - td->size_oh2) / td->tile_hgt;
2292             if ((td->cols != cols) || (td->rows != rows)) {
2293                 td->cols = cols;
2294                 td->rows = rows;
2295                 if (!IsZoomed(td->w) && !IsIconic(td->w)) {
2296                     normsize.x = td->cols;
2297                     normsize.y = td->rows;
2298                 }
2299
2300                 term_activate(&td->t);
2301                 term_resize(td->cols, td->rows);
2302                 InvalidateRect(td->w, NULL, TRUE);
2303             }
2304
2305             td->size_hack = TRUE;
2306             for (int i = 1; i < MAX_TERM_DATA; i++) {
2307                 if (data[i].visible)
2308                     ShowWindow(data[i].w, SW_SHOWNA);
2309             }
2310
2311             td->size_hack = FALSE;
2312
2313             return 0;
2314         }
2315         }
2316
2317         break;
2318     }
2319     case WM_ACTIVATE: {
2320         if (!wParam || HIWORD(lParam))
2321             break;
2322
2323         for (int i = 1; i < MAX_TERM_DATA; i++) {
2324             if (!data[i].posfix)
2325                 term_window_pos(&data[i], hWnd);
2326         }
2327
2328         SetFocus(hWnd);
2329         return 0;
2330     }
2331     case WM_ACTIVATEAPP: {
2332         if (IsIconic(td->w))
2333             break;
2334
2335         for (int i = 1; i < MAX_TERM_DATA; i++) {
2336             if (data[i].visible) {
2337                 if (wParam == TRUE) {
2338                     ShowWindow(data[i].w, SW_SHOWNA);
2339                 } else {
2340                     ShowWindow(data[i].w, SW_HIDE);
2341                 }
2342             }
2343         }
2344     }
2345         [[fallthrough]]; /* Fall through */
2346     case WM_ENABLE: {
2347         if (wParam == FALSE && keep_subwindows) {
2348             for (int i = 1; i < MAX_TERM_DATA; i++) {
2349                 if (data[i].visible) {
2350                     ShowWindow(data[i].w, SW_SHOWNA);
2351                 }
2352             }
2353         }
2354     }
2355     }
2356
2357     return DefWindowProc(hWnd, uMsg, wParam, lParam);
2358 }
2359
2360 /*!
2361  * @brief サブウインドウ用ウインドウプロシージャ
2362  */
2363 LRESULT PASCAL AngbandListProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2364 {
2365     term_data *td;
2366     PAINTSTRUCT ps;
2367     td = (term_data *)GetWindowLong(hWnd, 0);
2368
2369     switch (uMsg) {
2370     case WM_NCCREATE: {
2371         SetWindowLong(hWnd, 0, (LONG)(my_td));
2372         break;
2373     }
2374     case WM_CREATE: {
2375         return 0;
2376     }
2377     case WM_GETMINMAXINFO: {
2378         MINMAXINFO *lpmmi;
2379         RECT rc;
2380
2381         lpmmi = (MINMAXINFO *)lParam;
2382         if (!td)
2383             return 1;
2384
2385         rc.left = rc.top = 0;
2386         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
2387         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
2388
2389         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
2390         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
2391         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
2392         return 0;
2393     }
2394     case WM_SIZE: {
2395         if (!td)
2396             return 1;
2397         if (!td->w)
2398             return 1;
2399         if (td->size_hack)
2400             return 1;
2401
2402         td->size_hack = TRUE;
2403
2404         TERM_LEN cols = (LOWORD(lParam) - td->size_ow1 - td->size_ow2) / td->tile_wid;
2405         TERM_LEN rows = (HIWORD(lParam) - td->size_oh1 - td->size_oh2) / td->tile_hgt;
2406         if ((td->cols != cols) || (td->rows != rows)) {
2407             term_type *old_term = Term;
2408             td->cols = cols;
2409             td->rows = rows;
2410             term_activate(&td->t);
2411             term_resize(td->cols, td->rows);
2412             term_activate(old_term);
2413             InvalidateRect(td->w, NULL, TRUE);
2414             p_ptr->window_flags = 0xFFFFFFFF;
2415             handle_stuff(p_ptr);
2416         }
2417
2418         td->size_hack = FALSE;
2419         return 0;
2420     }
2421     case WM_ERASEBKGND: {
2422         return 1;
2423     }
2424     case WM_PAINT: {
2425         BeginPaint(hWnd, &ps);
2426         if (td)
2427             term_data_redraw(td);
2428         EndPaint(hWnd, &ps);
2429         return 0;
2430     }
2431     case WM_SYSKEYDOWN:
2432     case WM_KEYDOWN: {
2433         if (process_keydown(wParam, lParam))
2434             return 0;
2435
2436         break;
2437     }
2438     case WM_CHAR: {
2439         // wParam is WCHAR because using RegisterClassW
2440         if (term_no_press)
2441             term_no_press = FALSE;
2442         else {
2443             WCHAR wc[2] = { (WCHAR)wParam, '\0' };
2444             term_keypress(to_multibyte(wc).c_str());
2445         }
2446         return 0;
2447     }
2448     case WM_NCLBUTTONDOWN: {
2449         if (wParam == HTCLOSE)
2450             wParam = HTSYSMENU;
2451
2452         if (wParam == HTSYSMENU) {
2453             if (td->visible) {
2454                 td->visible = FALSE;
2455                 ShowWindow(td->w, SW_HIDE);
2456             }
2457
2458             return 0;
2459         }
2460
2461         break;
2462     }
2463     }
2464
2465     return DefWindowProc(hWnd, uMsg, wParam, lParam);
2466 }
2467
2468 /*!
2469  * @brief Display warning message (see "z-util.c")
2470  */
2471 static void hook_plog(concptr str)
2472 {
2473     if (str) {
2474         MessageBoxW(data[0].w, to_wchar(str).wc_str(), _(L"警告!", L"Warning"), MB_ICONEXCLAMATION | MB_OK);
2475     }
2476 }
2477
2478 /*!
2479  * @brief Display error message and quit (see "z-util.c")
2480  */
2481 static void hook_quit(concptr str)
2482 {
2483     if (str) {
2484         MessageBoxW(data[0].w, to_wchar(str).wc_str(), _(L"エラー!", L"Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
2485     }
2486
2487     save_prefs();
2488     for (int i = MAX_TERM_DATA - 1; i >= 0; --i) {
2489         term_force_font(&data[i]);
2490         if (data[i].font_want)
2491             string_free(data[i].font_want);
2492         if (data[i].w)
2493             DestroyWindow(data[i].w);
2494         data[i].w = 0;
2495     }
2496
2497     DeleteObject(hbrYellow);
2498     finalize_bg();
2499     graphic.finalize();
2500
2501     UnregisterClassW(AppName, hInstance);
2502     if (hIcon)
2503         DestroyIcon(hIcon);
2504
2505     exit(0);
2506 }
2507
2508 /*!
2509  * @brief Init some stuff
2510  */
2511 static void init_stuff(void)
2512 {
2513     char path[MAIN_WIN_MAX_PATH];
2514     DWORD path_len = GetModuleFileNameA(hInstance, path, MAIN_WIN_MAX_PATH);
2515     strcpy(path + path_len - 4, ".INI");
2516     ini_file = string_make(path);
2517
2518     int i = path_len;
2519     for (; i > 0; i--) {
2520         if (path[i] == '\\') {
2521             break;
2522         }
2523     }
2524
2525     strcpy(path + i + 1, "lib\\");
2526     validate_dir(path, TRUE);
2527     init_file_paths(path, path);
2528     validate_dir(ANGBAND_DIR_APEX, FALSE);
2529     validate_dir(ANGBAND_DIR_BONE, FALSE);
2530     if (!check_dir(ANGBAND_DIR_EDIT)) {
2531         validate_dir(ANGBAND_DIR_DATA, TRUE);
2532     } else {
2533         validate_dir(ANGBAND_DIR_DATA, FALSE);
2534     }
2535
2536     validate_dir(ANGBAND_DIR_FILE, TRUE);
2537     validate_dir(ANGBAND_DIR_HELP, FALSE);
2538     validate_dir(ANGBAND_DIR_INFO, FALSE);
2539     validate_dir(ANGBAND_DIR_PREF, TRUE);
2540     validate_dir(ANGBAND_DIR_SAVE, FALSE);
2541     validate_dir(ANGBAND_DIR_DEBUG_SAVE, FALSE);
2542     validate_dir(ANGBAND_DIR_USER, TRUE);
2543     validate_dir(ANGBAND_DIR_XTRA, TRUE);
2544     path_build(path, sizeof(path), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
2545
2546     validate_file(path);
2547     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
2548     ANGBAND_DIR_XTRA_GRAF = string_make(path);
2549     validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
2550
2551     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
2552     ANGBAND_DIR_XTRA_SOUND = string_make(path);
2553     validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
2554
2555     path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
2556     ANGBAND_DIR_XTRA_MUSIC = string_make(path);
2557     validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
2558
2559     for (i = 0; special_key_list[i]; ++i) {
2560         special_key[special_key_list[i]] = TRUE;
2561     }
2562
2563     for (i = 0; ignore_key_list[i]; ++i) {
2564         ignore_key[ignore_key_list[i]] = TRUE;
2565     }
2566
2567     ANGBAND_SYS = "win";
2568     if (7 != GetKeyboardType(0))
2569         ANGBAND_KEYBOARD = "0";
2570     else {
2571         switch (GetKeyboardType(1)) {
2572         case 0x0D01:
2573         case 0x0D02:
2574         case 0x0D03:
2575         case 0x0D04:
2576         case 0x0D05:
2577         case 0x0D06:
2578             /* NEC PC-98x1 */
2579             ANGBAND_KEYBOARD = "NEC98";
2580             break;
2581         default:
2582             /* PC/AT */
2583             ANGBAND_KEYBOARD = "JAPAN";
2584         }
2585     }
2586 }
2587
2588 /*!
2589  * @brief 全スポイラー出力を行う
2590  * Create Spoiler files
2591  * @details スポイラー出力処理の成功、失敗に関わらずプロセスを終了する。
2592  */
2593 void create_debug_spoiler(void)
2594 {
2595     init_stuff();
2596     init_angband(p_ptr, TRUE);
2597
2598     switch (output_all_spoilers()) {
2599     case spoiler_output_status::SPOILER_OUTPUT_SUCCESS:
2600         fprintf(stdout, "Successfully created a spoiler file.");
2601     case spoiler_output_status::SPOILER_OUTPUT_FAIL_FOPEN:
2602         fprintf(stderr, "Cannot create spoiler file.");
2603     case spoiler_output_status::SPOILER_OUTPUT_FAIL_FCLOSE:
2604         fprintf(stderr, "Cannot close spoiler file.");
2605     default:
2606         break;
2607     }
2608
2609     quit(NULL);
2610 }
2611
2612 /*!
2613  * @brief メインウインドウ、サブウインドウのウインドウクラス登録
2614  */
2615 static void register_wndclass(void)
2616 {
2617     WNDCLASSW wc{};
2618     wc.style = CS_CLASSDC;
2619     wc.lpfnWndProc = AngbandWndProc;
2620     wc.cbClsExtra = 0;
2621     wc.cbWndExtra = 4;
2622     wc.hInstance = hInstance;
2623     wc.hIcon = hIcon = LoadIconW(hInstance, AppName);
2624     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2625     wc.hbrBackground = NULL;
2626     wc.lpszMenuName = AppName;
2627     wc.lpszClassName = AppName;
2628
2629     if (!RegisterClassW(&wc))
2630         exit(1);
2631
2632     wc.lpfnWndProc = AngbandListProc;
2633     wc.lpszMenuName = NULL;
2634     wc.lpszClassName = AngList;
2635
2636     if (!RegisterClassW(&wc))
2637         exit(2);
2638 }
2639
2640 /*!
2641  * @brief (Windows固有)Windowsアプリケーションとしてのエントリポイント
2642  */
2643 int WINAPI WinMain(
2644     _In_ HINSTANCE hInst, [[maybe_unused]] _In_opt_ HINSTANCE hPrevInst, [[maybe_unused]] _In_ LPSTR lpCmdLine, [[maybe_unused]] _In_ int nCmdShow)
2645 {
2646     setlocale(LC_ALL, "ja_JP");
2647     hInstance = hInst;
2648     if (is_already_running()) {
2649         MessageBoxW(
2650             NULL, _(L"変愚蛮怒はすでに起動しています。", L"Hengband is already running."), _(L"エラー!", L"Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
2651         return FALSE;
2652     }
2653
2654     command_line.handle();
2655     register_wndclass();
2656
2657     // before term_data initialize
2658     plog_aux = [](concptr str) {
2659         if (str)
2660             MessageBoxW(NULL, to_wchar(str).wc_str(), _(L"警告!", L"Warning"), MB_ICONEXCLAMATION | MB_OK);
2661     };
2662     quit_aux = [](concptr str) {
2663         if (str) {
2664             MessageBoxW(NULL, to_wchar(str).wc_str(), _(L"エラー!", L"Error"), MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
2665         }
2666
2667         UnregisterClassW(AppName, hInstance);
2668         if (hIcon)
2669             DestroyIcon(hIcon);
2670
2671         exit(0);
2672     };
2673     core_aux = quit_aux;
2674
2675     init_stuff();
2676
2677     HDC hdc = GetDC(NULL);
2678     if (GetDeviceCaps(hdc, BITSPIXEL) <= 8) {
2679         quit(_("画面を16ビット以上のカラーモードにして下さい。", "Please switch to High Color (16-bit) or higher color mode."));
2680     }
2681     ReleaseDC(NULL, hdc);
2682
2683     refresh_color_table();
2684     init_windows();
2685     change_graphics_mode(static_cast<graphics_mode>(arg_graphics));
2686     change_bg_mode(current_bg_mode, true);
2687
2688     // after term_data initialize
2689     plog_aux = hook_plog;
2690     quit_aux = hook_quit;
2691     core_aux = hook_quit;
2692
2693     signals_init();
2694     term_activate(term_screen);
2695     init_angband(p_ptr, FALSE);
2696     initialized = TRUE;
2697
2698     check_for_save_file(command_line.get_savefile_option());
2699     prt(_("[ファイル] メニューの [新規] または [開く] を選択してください。", "[Choose 'New' or 'Open' from the 'File' menu]"), 23, _(8, 17));
2700     term_fresh();
2701
2702     change_sound_mode(arg_sound);
2703     use_music = arg_music;
2704     if (use_music) {
2705         init_music();
2706     }
2707
2708     // ユーザーがゲーム開始を選択するまで待つループ
2709     MSG msg;
2710     while (GetMessage(&msg, NULL, 0, 0)) {
2711         TranslateMessage(&msg);
2712         DispatchMessage(&msg);
2713         if (game_in_progress || movie_in_progress)
2714             break;
2715     }
2716
2717     term_flush();
2718     if (movie_in_progress) {
2719         // selected movie
2720         play_game(p_ptr, FALSE, TRUE);
2721     } else if (savefile[0] == '\0') {
2722         // new game
2723         play_game(p_ptr, TRUE, FALSE);
2724     }
2725     else {
2726         // selected savefile
2727         play_game(p_ptr, FALSE, FALSE);
2728     }
2729
2730     quit(NULL);
2731     return 0;
2732 }
2733 #endif /* WINDOWS */