OSDN Git Service

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