OSDN Git Service

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