OSDN Git Service

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