OSDN Git Service

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