OSDN Git Service

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