OSDN Git Service

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