OSDN Git Service

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