OSDN Git Service

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