OSDN Git Service

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