OSDN Git Service

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