OSDN Git Service

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