OSDN Git Service

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