OSDN Git Service

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