OSDN Git Service

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