OSDN Git Service

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