OSDN Git Service

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