OSDN Git Service

2cf937ba6c80358da95011bc548ccc955e61c8ac
[hengband/hengband.git] / src / main-win.c
1 /*!
2 * @file main-win.c
3 * @brief Windows版固有実装(メインエントリポイント含む)
4 * @date 2018/03/16
5 * @author Hengband Team
6 * @detail
7 *
8 * <h3>概要</h3>
9 * Windows98かその前後の頃を起点としたAPI実装。
10 * 各種のゲームエンジンは無論、
11 * DirectXといった昨今描画に標準的となったライブラリも用いていない。
12 * タイルの描画処理などについては、現在動作の詳細を検証中。
13 *
14 * <h3>フォーク元の概要</h3>
15 * <p>
16 * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, and others
17 *
18 * This software may be copied and distributed for educational, research,
19 * and not for profit purposes provided that this copyright and statement
20 * are included in all such copies.
21 * </p>
22 * <p>
23 * This file helps Angband work with Windows computers.
24 *
25 * To use this file, use an appropriate "Makefile" or "Project File",
26 * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
27 * make sure to obtain various extra files as described below.
28 *
29 * The official compilation uses the CodeWarrior Pro compiler, which
30 * includes a special project file and precompilable header file.
31 * </p>
32 *
33 * <p>
34 * <del>See also "main-dos.c" and "main-ibm.c".</del>
35 * </p>
36 *
37 * <p>
38 * The "lib/user/pref-win.prf" file contains keymaps, macro definitions,
39 * and/or color redefinitions.
40 * </p>
41 *
42 * <p>
43 * The "lib/user/font-win.prf" contains attr/char mappings for use with the
44 * normal "lib/xtra/font/*.fon" font files.
45 * </p>
46 *
47 * <p>
48 * The "lib/user/graf-win.prf" contains attr/char mappings for use with the
49 * special "lib/xtra/graf/*.bmp" bitmap files, which are activated by a menu
50 * item.
51 * </p>
52 *
53 * <p>
54 * Compiling this file, and using the resulting executable, requires
55 * several extra files not distributed with the standard Angband code.
56 * If "USE_GRAPHICS" is defined, then "readdib.h" and "readdib.c" must
57 * be placed into "src/", and the "8X8.BMP" bitmap file must be placed
58 * into "lib/xtra/graf".  In any case, some "*.fon" files (including
59 * "8X13.FON" if nothing else) must be placed into "lib/xtra/font/".
60 * If "USE_SOUND" is defined, then some special library (for example,
61 * "winmm.lib") may need to be linked in, and desired "*.WAV" sound
62 * files must be placed into "lib/xtra/sound/".  All of these extra
63 * files can be found in the "ext-win" archive.
64 * </p>
65 *
66 * <p>
67 * The "Term_xtra_win_clear()" function should probably do a low-level
68 * clear of the current window, and redraw the borders and other things,
69 * if only for efficiency.  
70 * </p>
71 *
72 * <p>
73 * A simpler method is needed for selecting the "tile size" for windows.
74 * </p>
75 *
76 * <p>
77 * The various "warning" messages assume the existance of the "screen.w"
78 * window, I think, and only a few calls actually check for its existance,
79 * this may be okay since "NULL" means "on top of all windows". (?)  The
80 * user must never be allowed to "hide" the main window, or the "menubar"
81 * will disappear.  
82 * </p>
83 *
84 * <p>
85 * Special "Windows Help Files" can be placed into "lib/xtra/help/" for
86 * use with the "winhelp.exe" program.  These files *may* be available
87 * at the ftp site somewhere, but I have not seen them.  
88 * </p>
89 *
90 * <p>
91 * Initial framework (and most code) by Ben Harrison (benh@phial.com).
92 *
93 * Original code by Skirmantas Kligys (kligys@scf.usc.edu).
94 *
95 * Additional code by Ross E Becker (beckerr@cis.ohio-state.edu),
96 * and Chris R. Martin (crm7479@tam2000.tamu.edu).
97 * </p>
98 */
99
100 #include "angband.h"
101 #include "util.h"
102 #include "inet.h"
103
104 #include "cmd-dump.h"
105 #include "view-mainwindow.h"
106 #include "floor.h"
107 #include "floor-events.h"
108 #include "init.h"
109 #include "files.h"
110 #include "scores.h"
111 #include "quest.h"
112 #include "files.h"
113 #include "core.h"
114
115 #ifdef WINDOWS
116 #include <windows.h>
117 #include <direct.h>
118 #include <locale.h>
119 #include "z-term.h"
120 #include "save.h"
121 #include "dungeon.h"
122
123 /*
124  * Extract the "WIN32" flag from the compiler
125  */
126 #if defined(__WIN32__) || defined(__WINNT__) || defined(__NT__)
127 # ifndef WIN32
128 #  define WIN32
129 # endif
130 #endif
131
132
133 /*
134  * Hack -- allow use of "screen saver" mode
135  */
136 #define USE_SAVER
137
138
139 /*
140  * Menu constants -- see "ANGBAND.RC"
141  */
142
143 #define IDM_FILE_NEW                    100
144 #define IDM_FILE_OPEN                   101
145 #define IDM_FILE_SAVE                   110
146 #define IDM_FILE_SCORE                  120
147 #define IDM_FILE_MOVIE                  121
148 #define IDM_FILE_EXIT                   130
149
150 #define IDM_WINDOW_VIS_0                200
151 #define IDM_WINDOW_VIS_1                201
152 #define IDM_WINDOW_VIS_2                202
153 #define IDM_WINDOW_VIS_3                203
154 #define IDM_WINDOW_VIS_4                204
155 #define IDM_WINDOW_VIS_5                205
156 #define IDM_WINDOW_VIS_6                206
157 #define IDM_WINDOW_VIS_7                207
158
159 #define IDM_WINDOW_FONT_0               210
160 #define IDM_WINDOW_FONT_1               211
161 #define IDM_WINDOW_FONT_2               212
162 #define IDM_WINDOW_FONT_3               213
163 #define IDM_WINDOW_FONT_4               214
164 #define IDM_WINDOW_FONT_5               215
165 #define IDM_WINDOW_FONT_6               216
166 #define IDM_WINDOW_FONT_7               217
167
168 #define IDM_WINDOW_POS_0                220
169 #define IDM_WINDOW_POS_1                221
170 #define IDM_WINDOW_POS_2                222
171 #define IDM_WINDOW_POS_3                223
172 #define IDM_WINDOW_POS_4                224
173 #define IDM_WINDOW_POS_5                225
174 #define IDM_WINDOW_POS_6                226
175 #define IDM_WINDOW_POS_7                227
176
177 #define IDM_WINDOW_BIZ_0                230
178 #define IDM_WINDOW_BIZ_1                231
179 #define IDM_WINDOW_BIZ_2                232
180 #define IDM_WINDOW_BIZ_3                233
181 #define IDM_WINDOW_BIZ_4                234
182 #define IDM_WINDOW_BIZ_5                235
183 #define IDM_WINDOW_BIZ_6                236
184 #define IDM_WINDOW_BIZ_7                237
185
186 #define IDM_WINDOW_I_WID_0              240
187 #define IDM_WINDOW_I_WID_1              241
188 #define IDM_WINDOW_I_WID_2              242
189 #define IDM_WINDOW_I_WID_3              243
190 #define IDM_WINDOW_I_WID_4              244
191 #define IDM_WINDOW_I_WID_5              245
192 #define IDM_WINDOW_I_WID_6              246
193 #define IDM_WINDOW_I_WID_7              247
194
195 #define IDM_WINDOW_D_WID_0              250
196 #define IDM_WINDOW_D_WID_1              251
197 #define IDM_WINDOW_D_WID_2              252
198 #define IDM_WINDOW_D_WID_3              253
199 #define IDM_WINDOW_D_WID_4              254
200 #define IDM_WINDOW_D_WID_5              255
201 #define IDM_WINDOW_D_WID_6              256
202 #define IDM_WINDOW_D_WID_7              257
203
204 #define IDM_WINDOW_I_HGT_0              260
205 #define IDM_WINDOW_I_HGT_1              261
206 #define IDM_WINDOW_I_HGT_2              262
207 #define IDM_WINDOW_I_HGT_3              263
208 #define IDM_WINDOW_I_HGT_4              264
209 #define IDM_WINDOW_I_HGT_5              265
210 #define IDM_WINDOW_I_HGT_6              266
211 #define IDM_WINDOW_I_HGT_7              267
212
213 #define IDM_WINDOW_D_HGT_0              270
214 #define IDM_WINDOW_D_HGT_1              271
215 #define IDM_WINDOW_D_HGT_2              272
216 #define IDM_WINDOW_D_HGT_3              273
217 #define IDM_WINDOW_D_HGT_4              274
218 #define IDM_WINDOW_D_HGT_5              275
219 #define IDM_WINDOW_D_HGT_6              276
220 #define IDM_WINDOW_D_HGT_7              277
221
222 #define IDM_OPTIONS_NO_GRAPHICS   400
223 #define IDM_OPTIONS_OLD_GRAPHICS  401
224 #define IDM_OPTIONS_NEW_GRAPHICS  402
225 #define IDM_OPTIONS_NEW2_GRAPHICS 403
226 #define IDM_OPTIONS_BIGTILE               409
227 #define IDM_OPTIONS_SOUND                 410
228 #define IDM_OPTIONS_MUSIC                 411
229 #define IDM_OPTIONS_SAVER                 420
230 #define IDM_OPTIONS_MAP                   430
231 #define IDM_OPTIONS_BG                    440
232 #define IDM_OPTIONS_OPEN_BG               441
233
234 #define IDM_DUMP_SCREEN_HTML    450
235
236 #define IDM_HELP_CONTENTS       901
237
238 /*
239  * Exclude parts of WINDOWS.H that are not needed
240  */
241 #define NOCOMM            /* Comm driver APIs and definitions */
242 #define NOLOGERROR        /* LogError() and related definitions */
243 #define NOPROFILER        /* Profiler APIs */
244 #define NOLFILEIO         /* _l* file I/O routines */
245 #define NOOPENFILE        /* OpenFile and related definitions */
246 #define NORESOURCE        /* Resource management */
247 #define NOATOM            /* Atom management */
248 #define NOLANGUAGE        /* Character test routines */
249 #define NOLSTRING         /* lstr* string management routines */
250 #define NODBCS            /* Double-byte character set routines */
251 #define NOKEYBOARDINFO    /* Keyboard driver routines */
252 #define NOCOLOR           /* COLOR_* color values */
253 #define NODRAWTEXT        /* DrawText() and related definitions */
254 #define NOSCALABLEFONT    /* Truetype scalable font support */
255 #define NOMETAFILE        /* Metafile support */
256 #define NOSYSTEMPARAMSINFO /* SystemParametersInfo() and SPI_* definitions */
257 #define NODEFERWINDOWPOS  /* DeferWindowPos and related definitions */
258 #define NOKEYSTATES       /* MK_* message key state flags */
259 #define NOWH              /* SetWindowsHook and related WH_* definitions */
260 #define NOCLIPBOARD       /* Clipboard APIs and definitions */
261 #define NOICONS           /* IDI_* icon IDs */
262 #define NOMDI             /* MDI support */
263 #define NOHELP            /* Help support */
264
265 /* Not defined since it breaks Borland C++ 5.5 */
266 /* #define NOCTLMGR */    /* Control management and controls */
267
268 /*
269  * Exclude parts of WINDOWS.H that are not needed (Win32)
270  */
271 #define WIN32_LEAN_AND_MEAN
272 #define NONLS             /* All NLS defines and routines */
273 #define NOSERVICE         /* All Service Controller routines, SERVICE_ equates, etc. */
274 #define NOKANJI           /* Kanji support stuff. */
275 #define NOMCX             /* Modem Configuration Extensions */
276
277 /*
278  * Include the "windows" support file
279  */
280 #include <windows.h>
281
282 /*
283  * Exclude parts of MMSYSTEM.H that are not needed
284  */
285 #define MMNODRV          /* Installable driver support */
286 #define MMNOWAVE         /* Waveform support */
287 #define MMNOMIDI         /* MIDI support */
288 #define MMNOAUX          /* Auxiliary audio support */
289 #define MMNOTIMER        /* Timer support */
290 #define MMNOJOY          /* Joystick support */
291 #define MMNOMCI          /* MCI support */
292 #define MMNOMMIO         /* Multimedia file I/O support */
293 #define MMNOMMSYSTEM     /* General MMSYSTEM functions */
294
295 /*
296  * Include some more files. Note: the Cygnus Cygwin compiler
297  * doesn't use mmsystem.h instead it includes the winmm library
298  * which performs a similar function.
299  */
300 #include <mmsystem.h>
301 #include <commdlg.h>
302
303 /*
304  * HTML-Help requires htmlhelp.h and htmlhelp.lib from Microsoft's
305  * HTML Workshop < http://msdn.microsoft.com/workshop/author/htmlhelp/ >.
306  */
307 /* #define HTML_HELP */
308
309 #ifdef HTML_HELP
310 #include <htmlhelp.h>
311 #endif /* HTML_HELP */
312
313 /*
314  * Include the support for loading bitmaps
315  */
316 #ifdef USE_GRAPHICS
317 # include "readdib.h"
318 #endif
319
320 /*
321  * Hack -- Fake declarations from "dos.h" 
322  */
323 #ifdef WIN32
324 #define INVALID_FILE_NAME (DWORD)0xFFFFFFFF
325 #else /* WIN32 */
326 #define FA_LABEL    0x08        /* Volume label */
327 #define FA_DIREC    0x10        /* Directory */
328 unsigned _cdecl _dos_getfileattr(concptr , unsigned *);
329 #endif /* WIN32 */
330
331 /*
332  * Silliness in WIN32 drawing routine
333  */
334 #ifdef WIN32
335 # define MoveTo(H,X,Y) MoveToEx(H, X, Y, NULL)
336 #endif /* WIN32 */
337
338 /*
339  * Silliness for Windows 95
340  */
341 #ifndef WS_EX_TOOLWINDOW
342 # define WS_EX_TOOLWINDOW 0
343 #endif
344
345 /*
346  * Foreground color bits (hard-coded by DOS)
347  */
348 #define VID_BLACK       0x00
349 #define VID_BLUE        0x01
350 #define VID_GREEN       0x02
351 #define VID_CYAN        0x03
352 #define VID_RED         0x04
353 #define VID_MAGENTA     0x05
354 #define VID_YELLOW      0x06
355 #define VID_WHITE       0x07
356
357 /*
358  * Bright text (hard-coded by DOS)
359  */
360 #define VID_BRIGHT      0x08
361
362 /*
363  * Background color bits (hard-coded by DOS)
364  */
365 #define VUD_BLACK       0x00
366 #define VUD_BLUE        0x10
367 #define VUD_GREEN       0x20
368 #define VUD_CYAN        0x30
369 #define VUD_RED         0x40
370 #define VUD_MAGENTA     0x50
371 #define VUD_YELLOW      0x60
372 #define VUD_WHITE       0x70
373
374 /*
375  * Blinking text (hard-coded by DOS)
376  */
377 #define VUD_BRIGHT      0x80
378
379
380 /*
381  * Forward declare
382  */
383 typedef struct _term_data term_data;
384
385 /*!
386  * @struct _term_data
387  * @brief ターム情報構造体 / Extra "term" data
388  * @details
389  * <p>
390  * pos_x / pos_y は各タームの左上点座標を指す。
391  * </p>
392  * <p>
393  * tile_wid / tile_hgt は[ウィンドウ]メニューのタイルの幅/高さを~を
394  * 1ドットずつ調整するステータスを指す。
395  * また、フォントを変更すると都度自動調整される。
396  * </p>
397  * <p>
398  * Note the use of "font_want" for the names of the font file requested by
399  * the user, and the use of "font_file" for the currently active font file.
400  *
401  * The "font_file" is uppercased, and takes the form "8X13.FON", while
402  * "font_want" can be in almost any form as long as it could be construed
403  * as attempting to represent the name of a font.
404  * </p>
405  */
406 struct _term_data
407 {
408         term t;
409         concptr s;
410         HWND w;
411         DWORD dwStyle;
412         DWORD dwExStyle;
413
414         uint keys;
415         TERM_LEN rows;  /* int -> uint */
416         TERM_LEN cols;
417
418         uint pos_x; //!< タームの左上X座標
419         uint pos_y; //!< タームの左上Y座標
420         uint size_wid;
421         uint size_hgt;
422         uint size_ow1;
423         uint size_oh1;
424         uint size_ow2;
425         uint size_oh2;
426
427         bool size_hack;
428         bool xtra_hack;
429         bool visible;
430         bool bizarre;
431         concptr font_want;
432         concptr font_file;
433         HFONT font_id;
434         int font_wid;  //!< フォント横幅
435         int font_hgt;  //!< フォント縦幅
436         int tile_wid;  //!< タイル横幅
437         int tile_hgt;  //!< タイル縦幅
438
439         uint map_tile_wid;
440         uint map_tile_hgt;
441
442         bool map_active;
443 #if 1 /* #ifdef JP */
444         LOGFONT lf;
445 #endif
446
447         bool posfix;
448
449 };
450
451 #define MAX_TERM_DATA 8 //!< Maximum number of windows 
452
453 static term_data data[MAX_TERM_DATA]; //!< An array of term_data's
454 static term_data *my_td; //!< Hack -- global "window creation" pointer
455 POINT normsize; //!< Remember normal size of main window when maxmized
456
457 /*
458  * was main window maximized on previous playing
459  */
460 bool win_maximized = FALSE;
461
462 /*
463  * game in progress
464  */
465 bool game_in_progress = FALSE;
466
467 /*
468  * note when "open"/"new" become valid
469  */
470 bool initialized = FALSE;
471
472 /*
473  * screen paletted, i.e. 256 colors
474  */
475 bool paletted = FALSE;
476
477 /*
478  * 16 colors screen, don't use RGB()
479  */
480 bool colors16 = FALSE;
481
482 /*
483  * Saved instance handle
484  */
485 static HINSTANCE hInstance;
486
487 /*
488  * Yellow brush for the cursor
489  */
490 static HBRUSH hbrYellow;
491
492 /*
493  * An icon
494  */
495 static HICON hIcon;
496
497 /*
498  * A palette
499  */
500 static HPALETTE hPal;
501
502 /* bg */
503 static HBITMAP hBG = NULL;
504 static int use_bg = 0; //!< 背景使用フラグ、1なら私用。
505 static char bg_bitmap_file[1024] = "bg.bmp"; //!< 現在の背景ビットマップファイル名。
506
507 #ifdef USE_SAVER
508
509 /*
510  * The screen saver window
511  */
512 static HWND hwndSaver;
513
514 #endif /* USE_SAVER */
515
516
517 #ifdef USE_GRAPHICS
518
519 /*!
520  * 現在使用中のタイルID(0ならば未使用)
521  * Flag set once "graphics" has been initialized
522  */
523 static byte_hack current_graphics_mode = 0;
524
525 /*
526  * The global bitmap
527  */
528 static DIBINIT infGraph;
529
530 /*
531  * The global bitmap mask
532  */
533 static DIBINIT infMask;
534
535 #endif /* USE_GRAPHICS */
536
537
538 #ifdef USE_SOUND
539
540 /*
541  * Flag set once "sound" has been initialized
542  */
543 static bool can_use_sound = FALSE;
544
545 #define SAMPLE_MAX 8
546 /*
547  * An array of sound file names
548  */
549 static concptr sound_file[SOUND_MAX][SAMPLE_MAX];
550
551 #endif /* USE_SOUND */
552
553
554
555 #ifdef USE_MUSIC
556
557 #define SAMPLE_MUSIC_MAX 16
558 static concptr music_file[MUSIC_BASIC_MAX][SAMPLE_MUSIC_MAX];
559 static concptr dungeon_music_file[1000][SAMPLE_MUSIC_MAX];
560 static concptr town_music_file[1000][SAMPLE_MUSIC_MAX];
561 static concptr quest_music_file[1000][SAMPLE_MUSIC_MAX];
562 static bool can_use_music = FALSE;
563
564 static MCI_OPEN_PARMS mop;
565 static char mci_device_type[256];
566
567 int current_music_type = 0;
568 int current_music_id = 0;
569
570 #endif /* USE_MUSIC */
571
572
573 /*
574  * Full path to ANGBAND.INI
575  */
576 static concptr ini_file = NULL;
577
578 /*
579  * Name of application
580  */
581 static concptr AppName = "ANGBAND";
582
583 /*
584  * Name of sub-window type
585  */
586 static concptr AngList = "AngList";
587
588 /*
589  * Directory names
590  */
591 static concptr ANGBAND_DIR_XTRA_GRAF;
592 static concptr ANGBAND_DIR_XTRA_SOUND;
593 static concptr ANGBAND_DIR_XTRA_MUSIC;
594 static concptr ANGBAND_DIR_XTRA_HELP;
595 #if 0 /* #ifndef JP */
596 static concptr ANGBAND_DIR_XTRA_FONT;
597 #endif
598 #ifdef USE_MUSIC
599 static concptr ANGBAND_DIR_XTRA_MUSIC;
600 #endif
601
602
603 /*
604  * The "complex" color values
605  */
606 static COLORREF win_clr[256];
607
608
609 /*
610  * Flag for macro trigger with dump ASCII
611  */
612 static bool Term_no_press = FALSE;
613
614 /*
615  * Copy and paste
616  */
617 static bool mouse_down = FALSE;
618 static bool paint_rect = FALSE;
619 static TERM_LEN mousex = 0, mousey = 0;
620 static TERM_LEN oldx, oldy;
621
622
623 /*!
624  * @brief The "simple" color values
625  * @details
626  * See "main-ibm.c" for original table information
627  * The entries below are taken from the "color bits" defined above.
628  * Note that many of the choices below suck, but so do crappy monitors.
629  */
630 static BYTE win_pal[256] =
631 {
632         VID_BLACK,                                      /* Dark */
633         VID_WHITE,                                      /* White */
634         VID_CYAN,                                       /* Slate XXX */
635         VID_RED | VID_BRIGHT,           /* Orange XXX */
636         VID_RED,                                        /* Red */
637         VID_GREEN,                                      /* Green */
638         VID_BLUE,                                       /* Blue */
639         VID_YELLOW,                                     /* Umber XXX */
640         VID_BLACK | VID_BRIGHT,         /* Light Dark */
641         VID_CYAN | VID_BRIGHT,          /* Light Slate XXX */
642         VID_MAGENTA,                            /* Violet XXX */
643         VID_YELLOW | VID_BRIGHT,        /* Yellow */
644         VID_MAGENTA | VID_BRIGHT,       /* Light Red XXX */
645         VID_GREEN | VID_BRIGHT,         /* Light Green */
646         VID_BLUE | VID_BRIGHT,          /* Light Blue */
647         VID_YELLOW                                      /* Light Umber XXX */
648 };
649
650
651 /*
652  * Hack -- define which keys are "special"
653  */
654 static bool special_key[256];
655 static bool ignore_key[256];
656
657 #if 1
658 /*
659  * Hack -- initialization list for "special_key"
660  */
661 static byte special_key_list[] = {
662         VK_CLEAR, VK_PAUSE, VK_CAPITAL,
663         VK_KANA, VK_JUNJA, VK_FINAL, VK_KANJI,
664         VK_CONVERT, VK_NONCONVERT, VK_ACCEPT, VK_MODECHANGE,
665         VK_PRIOR, VK_NEXT, VK_END, VK_HOME,
666         VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN,
667         VK_SELECT, VK_PRINT, VK_EXECUTE, VK_SNAPSHOT,
668         VK_INSERT, VK_DELETE, VK_HELP, VK_APPS,
669         VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3,
670         VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7,
671         VK_NUMPAD8, VK_NUMPAD9, VK_MULTIPLY, VK_ADD,
672         VK_SEPARATOR, VK_SUBTRACT, VK_DECIMAL, VK_DIVIDE,
673         VK_F1, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6,
674         VK_F7, VK_F8, VK_F9, VK_F10, VK_F11, VK_F12,
675         VK_F13, VK_F14, VK_F15, VK_F16, VK_F17, VK_F18,
676         VK_F19,VK_F20, VK_F21, VK_F22, VK_F23, VK_F24,
677         VK_NUMLOCK, VK_SCROLL, VK_ATTN, VK_CRSEL,
678         VK_EXSEL, VK_EREOF, VK_PLAY, VK_ZOOM,
679         VK_NONAME, VK_PA1,
680         0       /* End of List */
681 };
682
683 static byte ignore_key_list[] = {
684         VK_ESCAPE, VK_TAB, VK_SPACE,
685         'F', 'W', 'O', /*'H',*/ /* these are menu characters.*/
686         VK_SHIFT, VK_CONTROL, VK_MENU, VK_LWIN, VK_RWIN,
687         VK_LSHIFT, VK_RSHIFT, VK_LCONTROL, VK_RCONTROL,
688         VK_LMENU, VK_RMENU,
689         0       /* End of List */
690 };
691 #else
692 /*
693  * Hack -- initialization list for "special_key"
694  *
695  * We ignore the modifier keys (shift, control, alt, num lock, scroll lock),
696  * and the normal keys (escape, tab, return, letters, numbers, etc), but we
697  * catch the keypad keys (with and without numlock set, including keypad 5),
698  * the function keys (including the "menu" key which maps to F10), and the
699  * "pause" key (between scroll lock and numlock).  We also catch a few odd
700  * keys which I do not recognize, but which are listed among keys which we
701  * do catch, so they should be harmless to catch.
702  */
703 static byte special_key_list[] =
704 {
705         VK_CLEAR,               /* 0x0C (KP<5>) */
706
707         VK_PAUSE,               /* 0x13 (pause) */
708
709         VK_PRIOR,               /* 0x21 (KP<9>) */
710         VK_NEXT,                /* 0x22 (KP<3>) */
711         VK_END,                 /* 0x23 (KP<1>) */
712         VK_HOME,                /* 0x24 (KP<7>) */
713         VK_LEFT,                /* 0x25 (KP<4>) */
714         VK_UP,                  /* 0x26 (KP<8>) */
715         VK_RIGHT,               /* 0x27 (KP<6>) */
716         VK_DOWN,                /* 0x28 (KP<2>) */
717         VK_SELECT,              /* 0x29 (?????) */
718         VK_PRINT,               /* 0x2A (?????) */
719         VK_EXECUTE,             /* 0x2B (?????) */
720         VK_SNAPSHOT,    /* 0x2C (?????) */
721         VK_INSERT,              /* 0x2D (KP<0>) */
722         VK_DELETE,              /* 0x2E (KP<.>) */
723         VK_HELP,                /* 0x2F (?????) */
724 #if 0
725         VK_NUMPAD0,             /* 0x60 (KP<0>) */
726         VK_NUMPAD1,             /* 0x61 (KP<1>) */
727         VK_NUMPAD2,             /* 0x62 (KP<2>) */
728         VK_NUMPAD3,             /* 0x63 (KP<3>) */
729         VK_NUMPAD4,             /* 0x64 (KP<4>) */
730         VK_NUMPAD5,             /* 0x65 (KP<5>) */
731         VK_NUMPAD6,             /* 0x66 (KP<6>) */
732         VK_NUMPAD7,             /* 0x67 (KP<7>) */
733         VK_NUMPAD8,             /* 0x68 (KP<8>) */
734         VK_NUMPAD9,             /* 0x69 (KP<9>) */
735         VK_MULTIPLY,    /* 0x6A (KP<*>) */
736         VK_ADD,                 /* 0x6B (KP<+>) */
737         VK_SEPARATOR,   /* 0x6C (?????) */
738         VK_SUBTRACT,    /* 0x6D (KP<->) */
739         VK_DECIMAL,             /* 0x6E (KP<.>) */
740         VK_DIVIDE,              /* 0x6F (KP</>) */
741 #endif
742         VK_F1,                  /* 0x70 */
743         VK_F2,                  /* 0x71 */
744         VK_F3,                  /* 0x72 */
745         VK_F4,                  /* 0x73 */
746         VK_F5,                  /* 0x74 */
747         VK_F6,                  /* 0x75 */
748         VK_F7,                  /* 0x76 */
749         VK_F8,                  /* 0x77 */
750         VK_F9,                  /* 0x78 */
751         VK_F10,                 /* 0x79 */
752         VK_F11,                 /* 0x7A */
753         VK_F12,                 /* 0x7B */
754         VK_F13,                 /* 0x7C */
755         VK_F14,                 /* 0x7D */
756         VK_F15,                 /* 0x7E */
757         VK_F16,                 /* 0x7F */
758         VK_F17,                 /* 0x80 */
759         VK_F18,                 /* 0x81 */
760         VK_F19,                 /* 0x82 */
761         VK_F20,                 /* 0x83 */
762         VK_F21,                 /* 0x84 */
763         VK_F22,                 /* 0x85 */
764         VK_F23,                 /* 0x86 */
765         VK_F24,                 /* 0x87 */
766         0
767 };
768 #endif
769
770
771 /* Function prototype */
772
773 static bool is_already_running(void);
774
775
776 /* bg */
777 static void delete_bg(void)
778 {
779         if (hBG != NULL)
780         {
781                 DeleteObject(hBG);
782                 hBG = NULL;
783         }
784 }
785
786 static int init_bg(void)
787 {
788         char * bmfile = bg_bitmap_file;
789
790         delete_bg();
791         if (use_bg == 0) return 0;
792
793         hBG = LoadImage(NULL, bmfile,  IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
794         if (!hBG) {
795                 plog_fmt(_("壁紙用ビットマップ '%s' を読み込めません。", "Can't load the bitmap file '%s'."), bmfile);
796                 use_bg = 0;
797                 return 0;
798         }
799         use_bg = 1;
800         return 1;
801 }
802
803 static void DrawBG(HDC hdc, RECT *r)
804 {
805         HDC hdcSrc;
806         HBITMAP hOld;
807         BITMAP bm;
808         int x = r->left, y = r->top;
809         int nx, ny, sx, sy, swid, shgt, cwid, chgt;
810         
811         if (!use_bg || !hBG)
812                 return;
813
814         nx = x; ny = y;
815         GetObject(hBG, sizeof(bm), &bm);
816         swid = bm.bmWidth; shgt = bm.bmHeight;
817
818         hdcSrc = CreateCompatibleDC(hdc);
819         hOld = SelectObject(hdcSrc, hBG);
820
821         do {
822                 sx = nx % swid;
823                 cwid = MIN(swid - sx, r->right - nx);
824                 do {
825                         sy = ny % shgt;
826                         chgt = MIN(shgt - sy, r->bottom - ny);
827                                 BitBlt(hdc, nx, ny, cwid, chgt, hdcSrc, sx, sy, SRCCOPY);
828                         ny += chgt;
829                 } while (ny < r->bottom);
830                 ny = y;
831                 nx += cwid;
832         } while (nx < r->right);
833         
834         SelectObject(hdcSrc, hOld);
835         DeleteDC(hdcSrc);
836 }
837
838 #if 0
839 /*
840  * Hack -- given a pathname, point at the filename
841  */
842 static concptr extract_file_name(concptr s)
843 {
844         concptr p;
845
846         /* Start at the end */
847         p = s + strlen(s) - 1;
848
849         /* Back up to divider */
850         while ((p >= s) && (*p != ':') && (*p != '\\')) p--;
851
852         /* Return file name */
853         return (p+1);
854 }
855 #endif
856
857
858 /*
859  * Hack -- given a simple filename, extract the "font size" info
860  *
861  * Return a pointer to a static buffer holding the capitalized base name.
862  */
863 #if 0 /* #ifndef JP */
864 static char *analyze_font(char *path, int *wp, int *hp)
865 {
866         int wid, hgt;
867
868         char *s, *p;
869
870         /* Start at the end */
871         p = path + strlen(path) - 1;
872
873         /* Back up to divider */
874         while ((p >= path) && (*p != ':') && (*p != '\\')) --p;
875
876         /* Advance to file name */
877         ++p;
878
879         /* Capitalize */
880         for (s = p; *s; ++s)
881         {
882                 /* Capitalize (be paranoid) */
883                 if (islower(*s)) *s = toupper(*s);
884         }
885
886         /* Find first 'X' */
887         s = my_strchr(p, 'X');
888
889         /* Extract font width */
890         wid = atoi(p);
891
892         /* Extract height */
893         hgt = s ? atoi(s+1) : 0;
894
895         /* Save results */
896         (*wp) = wid;
897         (*hp) = hgt;
898         return (p);
899 }
900 #endif
901
902
903 /*
904  * Check for existance of a file
905  */
906 static bool check_file(concptr s)
907 {
908         char path[1024];
909
910 #ifdef WIN32
911
912         DWORD attrib;
913
914 #else /* WIN32 */
915
916         unsigned int attrib;
917
918 #endif /* WIN32 */
919
920         /* Copy it */
921         strcpy(path, s);
922
923 #ifdef WIN32
924
925         /* Examine */
926         attrib = GetFileAttributes(path);
927
928         /* Require valid filename */
929         if (attrib == INVALID_FILE_NAME) return (FALSE);
930
931         /* Prohibit directory */
932         if (attrib & FILE_ATTRIBUTE_DIRECTORY) return (FALSE);
933
934 #else /* WIN32 */
935
936         /* Examine and verify */
937         if (_dos_getfileattr(path, &attrib)) return (FALSE);
938
939         /* Prohibit something */
940         if (attrib & FA_LABEL) return (FALSE);
941
942         /* Prohibit directory */
943         if (attrib & FA_DIREC) return (FALSE);
944
945 #endif /* WIN32 */
946
947         /* Success */
948         return (TRUE);
949 }
950
951
952 /*
953  * Check for existance of a directory
954  */
955 static bool check_dir(concptr s)
956 {
957         int i;
958
959         char path[1024];
960
961 #ifdef WIN32
962
963         DWORD attrib;
964
965 #else /* WIN32 */
966
967         unsigned int attrib;
968
969 #endif /* WIN32 */
970
971         /* Copy it */
972         strcpy(path, s);
973
974         /* Check length */
975         i = strlen(path);
976
977         /* Remove trailing backslash */
978         if (i && (path[i-1] == '\\')) path[--i] = '\0';
979
980 #ifdef WIN32
981
982         /* Examine */
983         attrib = GetFileAttributes(path);
984
985         /* Require valid filename */
986         if (attrib == INVALID_FILE_NAME) return (FALSE);
987
988         /* Require directory */
989         if (!(attrib & FILE_ATTRIBUTE_DIRECTORY)) return (FALSE);
990
991 #else /* WIN32 */
992
993         /* Examine and verify */
994         if (_dos_getfileattr(path, &attrib)) return (FALSE);
995
996         /* Prohibit something */
997         if (attrib & FA_LABEL) return (FALSE);
998
999         /* Require directory */
1000         if (!(attrib & FA_DIREC)) return (FALSE);
1001
1002 #endif /* WIN32 */
1003
1004         /* Success */
1005         return (TRUE);
1006 }
1007
1008
1009 /*
1010  * Validate a file
1011  */
1012 static void validate_file(concptr s)
1013 {
1014         /* Verify or fail */
1015         if (!check_file(s))
1016         {
1017                 quit_fmt(_("必要なファイル[%s]が見あたりません。", "Cannot find required file:\n%s"), s);
1018         }
1019 }
1020
1021
1022 /*
1023  * Validate a directory
1024  */
1025 static void validate_dir(concptr s, bool vital)
1026 {
1027         /* Verify or fail */
1028         if (!check_dir(s))
1029         {
1030                 /* This directory contains needed data */
1031                 if (vital)
1032                 {
1033                         quit_fmt(_("必要なディレクトリ[%s]が見あたりません。", "Cannot find required directory:\n%s"), s);
1034                 }
1035                 /* Attempt to create this directory */
1036                 else if (mkdir(s))
1037                 {
1038                         quit_fmt("Unable to create directory:\n%s", s);
1039                 }
1040         }
1041 }
1042
1043
1044 /*!
1045  * @brief (Windows版固有実装)Get the "size" for a window
1046  */
1047 static void term_getsize(term_data *td)
1048 {
1049         RECT rc;
1050         TERM_LEN wid, hgt;
1051         if (td->cols < 1) td->cols = 1;
1052         if (td->rows < 1) td->rows = 1;
1053
1054         /* Window sizes */
1055         wid = td->cols * td->tile_wid + td->size_ow1 + td->size_ow2;
1056         hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
1057
1058         /* Fake window size */
1059         rc.left = 0;
1060         rc.right = rc.left + wid;
1061         rc.top = 0;
1062         rc.bottom = rc.top + hgt;
1063
1064         /* rc.right += 1; */
1065         /* rc.bottom += 1; */
1066
1067         /* Adjust */
1068         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
1069
1070         /* Total size */
1071         td->size_wid = rc.right - rc.left;
1072         td->size_hgt = rc.bottom - rc.top;
1073
1074         /* See CreateWindowEx */
1075         if (!td->w) return;
1076
1077         /* Extract actual location */
1078         GetWindowRect(td->w, &rc);
1079
1080         /* Save the location */
1081         td->pos_x = rc.left;
1082         td->pos_y = rc.top;
1083 }
1084
1085
1086 /*
1087  * Write the "prefs" for a single term
1088  */
1089 static void save_prefs_aux(int i)
1090 {
1091         term_data *td = &data[i];
1092         GAME_TEXT sec_name[128];
1093         char buf[1024];
1094
1095         RECT rc;
1096         WINDOWPLACEMENT lpwndpl;
1097         if (!td->w) return;
1098
1099         /* Make section name */
1100         sprintf(sec_name, "Term-%d", i);
1101
1102         /* Visible */
1103         if (i > 0)
1104         {
1105                 strcpy(buf, td->visible ? "1" : "0");
1106                 WritePrivateProfileString(sec_name, "Visible", buf, ini_file);
1107         }
1108
1109         /* Font */
1110 #ifdef JP
1111         strcpy(buf, td->lf.lfFaceName[0]!='\0' ? td->lf.lfFaceName : "MS ゴシック");
1112 #else
1113 #if 0
1114         strcpy(buf, td->font_file ? td->font_file : "8X13.FON");
1115 #else
1116         strcpy(buf, td->lf.lfFaceName[0]!='\0' ? td->lf.lfFaceName : "Courier");
1117 #endif
1118 #endif
1119
1120         WritePrivateProfileString(sec_name, "Font", buf, ini_file);
1121
1122 #if 1 /* #ifdef JP */
1123         wsprintf(buf, "%d", td->lf.lfWidth);
1124         WritePrivateProfileString(sec_name, "FontWid", buf, ini_file);
1125         wsprintf(buf, "%d", td->lf.lfHeight);
1126         WritePrivateProfileString(sec_name, "FontHgt", buf, ini_file);
1127         wsprintf(buf, "%d", td->lf.lfWeight);
1128         WritePrivateProfileString(sec_name, "FontWgt", buf, ini_file);
1129 #endif
1130         /* Bizarre */
1131         strcpy(buf, td->bizarre ? "1" : "0");
1132         WritePrivateProfileString(sec_name, "Bizarre", buf, ini_file);
1133
1134         /* Tile size (x) */
1135         wsprintf(buf, "%d", td->tile_wid);
1136         WritePrivateProfileString(sec_name, "TileWid", buf, ini_file);
1137
1138         /* Tile size (y) */
1139         wsprintf(buf, "%d", td->tile_hgt);
1140         WritePrivateProfileString(sec_name, "TileHgt", buf, ini_file);
1141
1142         /* Get window placement and dimensions */
1143         lpwndpl.length = sizeof(WINDOWPLACEMENT);
1144         GetWindowPlacement(td->w, &lpwndpl);
1145
1146         /* Acquire position in *normal* mode (not minimized) */
1147         rc = lpwndpl.rcNormalPosition;
1148
1149         /* Window size (x) */
1150         if (i == 0) wsprintf(buf, "%d", normsize.x);
1151         else wsprintf(buf, "%d", td->cols);
1152         WritePrivateProfileString(sec_name, "NumCols", buf, ini_file);
1153
1154         /* Window size (y) */
1155         if (i == 0) wsprintf(buf, "%d", normsize.y);
1156         else wsprintf(buf, "%d", td->rows);
1157         WritePrivateProfileString(sec_name, "NumRows", buf, ini_file);
1158
1159         /* Maxmized (only main window) */
1160         if (i == 0)
1161         {
1162                 strcpy(buf, IsZoomed(td->w) ? "1" : "0");
1163                 WritePrivateProfileString(sec_name, "Maximized", buf, ini_file);
1164         }
1165
1166         /* Acquire position */
1167         GetWindowRect(td->w, &rc);
1168
1169         /* Window position (x) */
1170         wsprintf(buf, "%d", rc.left);
1171         WritePrivateProfileString(sec_name, "PositionX", buf, ini_file);
1172
1173         /* Window position (y) */
1174         wsprintf(buf, "%d", rc.top);
1175         WritePrivateProfileString(sec_name, "PositionY", buf, ini_file);
1176
1177         /* Window Z position */
1178         if (i > 0)
1179         {
1180                 strcpy(buf, td->posfix ? "1" : "0");
1181                 WritePrivateProfileString(sec_name, "PositionFix", buf, ini_file);
1182         }
1183 }
1184
1185
1186 /*
1187  * Write the "prefs"
1188  * We assume that the windows have all been initialized
1189  */
1190 static void save_prefs(void)
1191 {
1192         int i;
1193
1194         char buf[128];
1195
1196         /* Save the "arg_graphics" flag */
1197         sprintf(buf, "%d", arg_graphics);
1198         WritePrivateProfileString("Angband", "Graphics", buf, ini_file);
1199
1200         /* Save the "arg_bigtile" flag */
1201         strcpy(buf, arg_bigtile ? "1" : "0");
1202         WritePrivateProfileString("Angband", "Bigtile", buf, ini_file);
1203
1204         /* Save the "arg_sound" flag */
1205         strcpy(buf, arg_sound ? "1" : "0");
1206         WritePrivateProfileString("Angband", "Sound", buf, ini_file);
1207
1208         /* Save the "arg_sound" flag */
1209         strcpy(buf, arg_music ? "1" : "0");
1210         WritePrivateProfileString("Angband", "Music", buf, ini_file);
1211
1212         /* bg */
1213         strcpy(buf, use_bg ? "1" : "0");
1214         WritePrivateProfileString("Angband", "BackGround", buf, ini_file);
1215         WritePrivateProfileString("Angband", "BackGroundBitmap", 
1216                 bg_bitmap_file[0] != '\0' ? bg_bitmap_file : "bg.bmp", ini_file);
1217
1218         /* Save window prefs */
1219         for (i = 0; i < MAX_TERM_DATA; ++i)
1220         {
1221                 save_prefs_aux(i);
1222         }
1223 }
1224
1225
1226 /*
1227  * Load the "prefs" for a single term
1228  */
1229 static void load_prefs_aux(int i)
1230 {
1231         term_data *td = &data[i];
1232         GAME_TEXT sec_name[128];
1233         char tmp[1024];
1234
1235         int wid, hgt, posx, posy;
1236         int dispx = GetSystemMetrics( SM_CXVIRTUALSCREEN);
1237         int dispy = GetSystemMetrics( SM_CYVIRTUALSCREEN);
1238         posx=0;
1239         posy=0;
1240         
1241         /* Make section name */
1242         sprintf(sec_name, "Term-%d", i);
1243
1244         /* Make section name */
1245         sprintf(sec_name, "Term-%d", i);
1246
1247         /* Visible */
1248         if (i > 0)
1249         {
1250                 td->visible = (GetPrivateProfileInt(sec_name, "Visible", td->visible, ini_file) != 0);
1251         }
1252
1253         /* Desired font, with default */
1254 #ifdef JP
1255         GetPrivateProfileString(sec_name, "Font", "MS ゴシック", tmp, 127, ini_file);
1256 #else
1257 #if 0
1258         GetPrivateProfileString(sec_name, "Font", "8X13.FON", tmp, 127, ini_file);
1259 #else
1260         GetPrivateProfileString(sec_name, "Font", "Courier", tmp, 127, ini_file);
1261 #endif
1262 #endif
1263
1264
1265         /* Bizarre */
1266         td->bizarre = (GetPrivateProfileInt(sec_name, "Bizarre", td->bizarre, ini_file) != 0);
1267
1268         /* Analyze font, save desired font name */
1269 #if 1 /* #ifdef JP */
1270         td->font_want = string_make(tmp);
1271         hgt = 15; wid = 0;
1272         td->lf.lfWidth  = GetPrivateProfileInt(sec_name, "FontWid", wid, ini_file);
1273         td->lf.lfHeight = GetPrivateProfileInt(sec_name, "FontHgt", hgt, ini_file);
1274         td->lf.lfWeight = GetPrivateProfileInt(sec_name, "FontWgt", 0, ini_file);
1275 #else
1276         td->font_want = string_make(analyze_font(tmp, &wid, &hgt));
1277 #endif
1278
1279
1280         /* Tile size */
1281 #if 1 /* #ifdef JP */
1282         td->tile_wid = GetPrivateProfileInt(sec_name, "TileWid", td->lf.lfWidth, ini_file);
1283         td->tile_hgt = GetPrivateProfileInt(sec_name, "TileHgt", td->lf.lfHeight, ini_file);
1284 #else
1285         td->tile_wid = GetPrivateProfileInt(sec_name, "TileWid", wid, ini_file);
1286         td->tile_hgt = GetPrivateProfileInt(sec_name, "TileHgt", hgt, ini_file);
1287 #endif
1288
1289
1290         /* Window size */
1291         td->cols = GetPrivateProfileInt(sec_name, "NumCols", td->cols, ini_file);
1292         td->rows = GetPrivateProfileInt(sec_name, "NumRows", td->rows, ini_file);
1293         normsize.x = td->cols; normsize.y = td->rows;
1294
1295         /* Window size */
1296         if (i == 0)
1297         {
1298                 win_maximized = (GetPrivateProfileInt(sec_name, "Maximized", win_maximized, ini_file) != 0);
1299         }
1300
1301         /* Window position */
1302         posx = GetPrivateProfileInt(sec_name, "PositionX", posx, ini_file);
1303         posy = GetPrivateProfileInt(sec_name, "PositionY", posy, ini_file);
1304         td->pos_x = MIN(MAX(0, posx), dispx-128);
1305         td->pos_y = MIN(MAX(0, posy), dispy-128);
1306
1307         /* Window Z position */
1308         if (i > 0)
1309         {
1310                 td->posfix = (GetPrivateProfileInt(sec_name, "PositionFix", td->posfix, ini_file) != 0);
1311         }
1312 }
1313
1314
1315 /*
1316  * Load the "prefs"
1317  */
1318 static void load_prefs(void)
1319 {
1320         int i;
1321
1322         /* Extract the "arg_graphics" flag */
1323         arg_graphics = (byte_hack)GetPrivateProfileInt("Angband", "Graphics", GRAPHICS_NONE, ini_file);
1324
1325         /* Extract the "arg_bigtile" flag */
1326         arg_bigtile = (GetPrivateProfileInt("Angband", "Bigtile", FALSE, ini_file) != 0);
1327         use_bigtile = arg_bigtile;
1328
1329         /* Extract the "arg_sound" flag */
1330         arg_sound = (GetPrivateProfileInt("Angband", "Sound", 0, ini_file) != 0);
1331
1332         /* Extract the "arg_sound" flag */
1333         arg_music = (GetPrivateProfileInt("Angband", "Music", 0, ini_file) != 0);
1334
1335         /* bg */
1336         use_bg = GetPrivateProfileInt("Angband", "BackGround", 0, ini_file);
1337         GetPrivateProfileString("Angband", "BackGroundBitmap", "bg.bmp", bg_bitmap_file, 1023, ini_file);
1338
1339         /* Load window prefs */
1340         for (i = 0; i < MAX_TERM_DATA; ++i)
1341         {
1342                 load_prefs_aux(i);
1343         }
1344 }
1345
1346 #if defined(USE_SOUND) || defined(USE_MUSIC)
1347
1348 /*
1349  * - Taken from files.c.
1350  *
1351  * Extract "tokens" from a buffer
1352  *
1353  * This function uses "whitespace" as delimiters, and treats any amount of
1354  * whitespace as a single delimiter.  We will never return any empty tokens.
1355  * When given an empty buffer, or a buffer containing only "whitespace", we
1356  * will return no tokens.  We will never extract more than "num" tokens.
1357  *
1358  * By running a token through the "text_to_ascii()" function, you can allow
1359  * that token to include (encoded) whitespace, using "\s" to encode spaces.
1360  *
1361  * We save pointers to the tokens in "tokens", and return the number found.
1362  */
1363 static s16b tokenize_whitespace(char *buf, s16b num, char **tokens)
1364 {
1365         s16b k = 0;
1366         char *s = buf;
1367
1368         /* Process */
1369         while (k < num)
1370         {
1371                 char *t;
1372
1373                 /* Skip leading whitespace */
1374                 for ( ; *s && iswspace(*s); ++s) /* loop */;
1375
1376                 /* All done */
1377                 if (!*s) break;
1378
1379                 /* Find next whitespace, if any */
1380                 for (t = s; *t && !iswspace(*t); ++t) /* loop */;
1381
1382                 /* Nuke and advance (if necessary) */
1383                 if (*t) *t++ = '\0';
1384
1385                 /* Save the token */
1386                 tokens[k++] = s;
1387
1388                 /* Advance */
1389                 s = t;
1390         }
1391
1392         /* Count */
1393         return (k);
1394 }
1395
1396 #endif /* USE_SOUND || USE_MUSIC */
1397
1398 #ifdef USE_SOUND
1399
1400 static void load_sound_prefs(void)
1401 {
1402         int i, j, num;
1403         char tmp[1024];
1404         char ini_path[1024];
1405         char wav_path[1024];
1406         char *zz[SAMPLE_MAX];
1407
1408         /* Access the sound.cfg */
1409
1410         path_build(ini_path, 1024, ANGBAND_DIR_XTRA_SOUND, "sound.cfg");
1411
1412         for (i = 0; i < SOUND_MAX; i++)
1413         {
1414                 GetPrivateProfileString("Sound", angband_sound_name[i], "", tmp, 1024, ini_path);
1415
1416                 num = tokenize_whitespace(tmp, SAMPLE_MAX, zz);
1417
1418                 for (j = 0; j < num; j++)
1419                 {
1420                         /* Access the sound */
1421                         path_build(wav_path, 1024, ANGBAND_DIR_XTRA_SOUND, zz[j]);
1422
1423                         /* Save the sound filename, if it exists */
1424                         if (check_file(wav_path))
1425                                 sound_file[i][j] = string_make(zz[j]);
1426                 }
1427         }
1428 }
1429
1430 #endif /* USE_SOUND */
1431
1432 #ifdef USE_MUSIC
1433
1434 static void load_music_prefs(void)
1435 {
1436         int i, j, num;
1437         char tmp[1024];
1438         char ini_path[1024];
1439         char wav_path[1024];
1440         char *zz[SAMPLE_MAX];
1441         char key[80];
1442
1443         /* Access the music.cfg */
1444
1445         path_build(ini_path, 1024, ANGBAND_DIR_XTRA_MUSIC, "music.cfg");
1446
1447         GetPrivateProfileString("Device", "type", "", mci_device_type, 256, ini_path);
1448
1449         for (i = 0; i < MUSIC_BASIC_MAX; i++)
1450         {
1451                 GetPrivateProfileString("Basic", angband_music_basic_name[i], "", tmp, 1024, ini_path);
1452
1453                 num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1454
1455                 for (j = 0; j < num; j++)
1456                 {
1457                         /* Access the sound */
1458                         path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1459
1460                         /* Save the sound filename, if it exists */
1461                         if (check_file(wav_path))
1462                                 music_file[i][j] = string_make(zz[j]);
1463                 }
1464         }
1465
1466         for (i = 0; i < max_d_idx; i++)
1467         {
1468                 sprintf(key, "dungeon%03d", i);
1469                 GetPrivateProfileString("Dungeon", key, "", tmp, 1024, ini_path);
1470
1471                 num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1472
1473                 for (j = 0; j < num; j++)
1474                 {
1475                         /* Access the sound */
1476                         path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1477
1478                         /* Save the sound filename, if it exists */
1479                         if (check_file(wav_path))
1480                                 dungeon_music_file[i][j] = string_make(zz[j]);
1481                 }
1482         }
1483
1484         for (i = 0; i < max_q_idx; i++)
1485         {
1486                 sprintf(key, "quest%03d", i);
1487                 GetPrivateProfileString("Quest", key, "", tmp, 1024, ini_path);
1488
1489                 num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1490
1491                 for (j = 0; j < num; j++)
1492                 {
1493                         /* Access the sound */
1494                         path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1495
1496                         /* Save the sound filename, if it exists */
1497                         if (check_file(wav_path))
1498                                 quest_music_file[i][j] = string_make(zz[j]);
1499                 }
1500         }
1501
1502         for (i = 0; i < 1000; i++) /*!< @todo 町最大数指定 */
1503         {
1504                 sprintf(key, "town%03d", i);
1505                 GetPrivateProfileString("Town", key, "", tmp, 1024, ini_path);
1506
1507                 num = tokenize_whitespace(tmp, SAMPLE_MUSIC_MAX, zz);
1508
1509                 for (j = 0; j < num; j++)
1510                 {
1511                         /* Access the sound */
1512                         path_build(wav_path, 1024, ANGBAND_DIR_XTRA_MUSIC, zz[j]);
1513
1514                         /* Save the sound filename, if it exists */
1515                         if (check_file(wav_path))
1516                                 town_music_file[i][j] = string_make(zz[j]);
1517                 }
1518         }
1519
1520
1521 }
1522
1523 #endif /* USE_MUSIC */
1524
1525 /*
1526  * Create the new global palette based on the bitmap palette
1527  * (if any), and the standard 16 entry palette derived from
1528  * "win_clr[]" which is used for the basic 16 Angband colors.
1529  *
1530  * This function is never called before all windows are ready.
1531  *
1532  * This function returns FALSE if the new palette could not be
1533  * prepared, which should normally be a fatal error.  XXX XXX
1534  *
1535  * Note that only some machines actually use a "palette".
1536  */
1537 static int new_palette(void)
1538 {
1539         HPALETTE hBmPal;
1540         HPALETTE hNewPal;
1541         HDC hdc;
1542         int i, nEntries;
1543         int pLogPalSize;
1544         int lppeSize;
1545         LPLOGPALETTE pLogPal;
1546         LPPALETTEENTRY lppe;
1547
1548         term_data *td;
1549
1550
1551         /* This makes no sense */
1552         if (!paletted) return (TRUE);
1553
1554
1555         /* No bitmap */
1556         lppeSize = 0;
1557         lppe = NULL;
1558         nEntries = 0;
1559
1560 #ifdef USE_GRAPHICS
1561
1562         /* Check the bitmap palette */
1563         hBmPal = infGraph.hPalette;
1564
1565         /* Use the bitmap */
1566         if (hBmPal)
1567         {
1568                 lppeSize = 256 * sizeof(PALETTEENTRY);
1569                 lppe = (LPPALETTEENTRY)ralloc(lppeSize);
1570                 nEntries = GetPaletteEntries(hBmPal, 0, 255, lppe);
1571                 if ((nEntries == 0) || (nEntries > 220))
1572                 {
1573                         /* Warn the user */
1574                         plog(_("画面を16ビットか24ビットカラーモードにして下さい。", "Please switch to high- or true-color mode."));
1575
1576                         /* Cleanup */
1577                         rnfree(lppe, lppeSize);
1578
1579                         /* Fail */
1580                         return (FALSE);
1581                 }
1582         }
1583
1584 #endif /* USE_GRAPHICS */
1585
1586         /* Size of palette */
1587         pLogPalSize = sizeof(LOGPALETTE) + (nEntries + 16) * sizeof(PALETTEENTRY);
1588
1589         /* Allocate palette */
1590         pLogPal = (LPLOGPALETTE)ralloc(pLogPalSize);
1591
1592         /* Version */
1593         pLogPal->palVersion = 0x300;
1594
1595         /* Make room for bitmap and normal data */
1596         pLogPal->palNumEntries = nEntries + 16;
1597
1598         /* Save the bitmap data */
1599         for (i = 0; i < nEntries; i++)
1600         {
1601                 pLogPal->palPalEntry[i] = lppe[i];
1602         }
1603
1604         /* Save the normal data */
1605         for (i = 0; i < 16; i++)
1606         {
1607                 LPPALETTEENTRY p;
1608
1609                 /* Access the entry */
1610                 p = &(pLogPal->palPalEntry[i+nEntries]);
1611
1612                 /* Save the colors */
1613                 p->peRed = GetRValue(win_clr[i]);
1614                 p->peGreen = GetGValue(win_clr[i]);
1615                 p->peBlue = GetBValue(win_clr[i]);
1616
1617                 /* Save the flags */
1618                 p->peFlags = PC_NOCOLLAPSE;
1619         }
1620
1621         /* Free something */
1622         if (lppe) rnfree(lppe, lppeSize);
1623
1624         /* Create a new palette, or fail */
1625         hNewPal = CreatePalette(pLogPal);
1626         if (!hNewPal) quit(_("パレットを作成できません!", "Cannot create palette!"));
1627
1628         /* Free the palette */
1629         rnfree(pLogPal, pLogPalSize);
1630
1631         /* Main window */
1632         td = &data[0];
1633
1634         /* Realize the palette */
1635         hdc = GetDC(td->w);
1636         SelectPalette(hdc, hNewPal, 0);
1637         i = RealizePalette(hdc);
1638         ReleaseDC(td->w, hdc);
1639         if (i == 0) quit(_("パレットをシステムエントリにマップできません!", "Cannot realize palette!"));
1640
1641
1642         /* Sub-windows */
1643         for (i = 1; i < MAX_TERM_DATA; i++)
1644         {
1645                 td = &data[i];
1646
1647                 hdc = GetDC(td->w);
1648                 SelectPalette(hdc, hNewPal, 0);
1649                 ReleaseDC(td->w, hdc);
1650         }
1651
1652         /* Delete old palette */
1653         if (hPal) DeleteObject(hPal);
1654
1655         /* Save new palette */
1656         hPal = hNewPal;
1657
1658         /* Success */
1659         return (TRUE);
1660 }
1661
1662
1663 #ifdef USE_GRAPHICS
1664 /*!
1665  * @brief グラフィクスを初期化する / Initialize graphics
1666  * @details
1667  * <ul>
1668  * <li>メニュー[オプション]>[グラフィクス]が「なし」以外の時に描画処理を初期化する。</li>
1669  * <li>呼び出されるタイミングはロード時、及び同メニューで「なし」以外に変更される毎になる。</li>
1670  * </ul>
1671  */
1672 static bool init_graphics(void)
1673 {
1674         /* Initialize once */
1675         char buf[1024];
1676         BYTE wid, hgt, twid, thgt, ox, oy;
1677         concptr name;
1678
1679         if (arg_graphics == GRAPHICS_ADAM_BOLT)
1680         {
1681                 wid = 16;
1682                 hgt = 16;
1683                 twid = 16;
1684                 thgt = 16;
1685                 ox = 0;
1686                 oy = 0;
1687                 name = "16X16.BMP";
1688
1689                 ANGBAND_GRAF = "new";
1690         }
1691         else if (arg_graphics == GRAPHICS_HENGBAND)
1692         {
1693                 /*! @todo redraw
1694                 wid = 64;
1695                 hgt = 64;
1696                 twid = 32;
1697                 thgt = 32;
1698                 ox = -16;
1699                 oy = -24;
1700                 name = "64X64.BMP";
1701                 */
1702
1703                 wid = 32;
1704                 hgt = 32;
1705                 twid = 32;
1706                 thgt = 32;
1707                 ox = 0;
1708                 oy = 0;
1709                 name = "32X32.BMP";
1710
1711                 ANGBAND_GRAF = "ne2";
1712         }
1713         else
1714         {
1715                 wid = 8;
1716                 hgt = 8;
1717                 twid = 8;
1718                 thgt = 8;
1719                 ox = 0;
1720                 oy = 0;
1721                 name = "8X8.BMP";
1722                 ANGBAND_GRAF = "old";
1723         }
1724
1725         /* Access the bitmap file */
1726         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, name);
1727
1728         /* Load the bitmap or quit */
1729         if (!ReadDIB(data[0].w, buf, &infGraph))
1730         {
1731                 plog_fmt(_("ビットマップ '%s' を読み込めません。", "Cannot read bitmap file '%s'"), name);
1732                 return (FALSE);
1733         }
1734
1735         /* Save the new sizes */
1736         infGraph.CellWidth = wid;
1737         infGraph.CellHeight = hgt;
1738         infGraph.TileWidth = twid;
1739         infGraph.TileHeight = thgt;
1740         infGraph.OffsetX = ox;
1741         infGraph.OffsetY = oy;
1742
1743         if (arg_graphics == GRAPHICS_ADAM_BOLT)
1744         {
1745                 /* Access the mask file */
1746                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask.bmp");
1747
1748                 /* Load the bitmap or quit */
1749                 if (!ReadDIB(data[0].w, buf, &infMask))
1750                 {
1751                         plog_fmt("Cannot read bitmap file '%s'", buf);
1752                         return (FALSE);
1753                 }
1754         }
1755         if (arg_graphics == GRAPHICS_HENGBAND)
1756         {
1757                 /* Access the mask file */
1758                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask32.bmp");
1759
1760                 /* Load the bitmap or quit */
1761                 if (!ReadDIB(data[0].w, buf, &infMask))
1762                 {
1763                         plog_fmt("Cannot read bitmap file '%s'", buf);
1764                         return (FALSE);
1765                 }
1766         }
1767
1768         /* Activate a palette */
1769         if (!new_palette())
1770         {
1771                 /* Free bitmap */
1772
1773                 plog(_("パレットを実現できません!", "Cannot activate palette!"));
1774                 return (FALSE);
1775         }
1776
1777         /* Graphics available */
1778         current_graphics_mode = arg_graphics;
1779         return (current_graphics_mode);
1780 }
1781 #endif /* USE_GRAPHICS */
1782
1783
1784 #ifdef USE_MUSIC
1785 /*
1786  * Initialize music
1787  */
1788 static bool init_music(void)
1789 {
1790         /* Initialize once */
1791         if (!can_use_music)
1792         {
1793                 /* Load the prefs */
1794                 load_music_prefs();
1795
1796                 /* Sound available */
1797                 can_use_music = TRUE;
1798         }
1799         return (can_use_music);
1800 }
1801
1802 /*
1803  * Hack -- Stop a music
1804  */
1805 static void stop_music(void)
1806 {
1807         mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
1808         mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
1809 }
1810
1811 #endif /* USE_MUSIC */
1812
1813 #ifdef USE_SOUND
1814 /*
1815  * Initialize sound
1816  */
1817 static bool init_sound(void)
1818 {
1819         /* Initialize once */
1820         if (!can_use_sound)
1821         {
1822                 /* Load the prefs */
1823                 load_sound_prefs();
1824
1825                 /* Sound available */
1826                 can_use_sound = TRUE;
1827         }
1828         return (can_use_sound);
1829 }
1830 #endif /* USE_SOUND */
1831
1832
1833 /*
1834  * Resize a window
1835  */
1836 static void term_window_resize(term_data *td)
1837 {
1838         /* Require window */
1839         if (!td->w) return;
1840
1841         /* Resize the window */
1842         SetWindowPos(td->w, 0, 0, 0,
1843                      td->size_wid, td->size_hgt,
1844                      SWP_NOMOVE | SWP_NOZORDER);
1845
1846         /* Redraw later */
1847         InvalidateRect(td->w, NULL, TRUE);
1848 }
1849
1850
1851 /*
1852  * Force the use of a new "font file" for a term_data
1853  *
1854  * This function may be called before the "window" is ready
1855  *
1856  * This function returns zero only if everything succeeds.
1857  *
1858  * Note that the "font name" must be capitalized!!!
1859  */
1860 static errr term_force_font(term_data *td, concptr path)
1861 {
1862         int wid, hgt;
1863
1864 #if 0 /* #ifndef JP */
1865         int i;
1866         char *base;
1867         char buf[1024];
1868 #endif
1869
1870         /* Forget the old font (if needed) */
1871         if (td->font_id) DeleteObject(td->font_id);
1872
1873 #if 1 /* #ifdef JP */
1874         /* Unused */
1875         (void)path;
1876
1877         /* Create the font (using the 'base' of the font file name!) */
1878         td->font_id = CreateFontIndirect(&(td->lf));
1879         wid = td->lf.lfWidth;
1880         hgt = td->lf.lfHeight;
1881         if (!td->font_id) return (1);
1882 #else
1883         /* Forget old font */
1884         if (td->font_file)
1885         {
1886                 bool used = FALSE;
1887
1888                 /* Scan windows */
1889                 for (i = 0; i < MAX_TERM_DATA; i++)
1890                 {
1891                         /* Don't check when closing the application */
1892                         if (!path) break;
1893
1894                         /* Check "screen" */
1895                         if ((td != &data[i]) &&
1896                             (data[i].font_file) &&
1897                             (streq(data[i].font_file, td->font_file)))
1898                         {
1899                                 used = TRUE;
1900                         }
1901                 }
1902
1903                 /* Remove unused font resources */
1904                 if (!used) RemoveFontResource(td->font_file);
1905
1906                 /* Free the old name */
1907                 string_free(td->font_file);
1908
1909                 /* Forget it */
1910                 td->font_file = NULL;
1911         }
1912
1913         /* No path given */
1914         if (!path) return (1);
1915
1916         /* Local copy */
1917         strcpy(buf, path);
1918
1919         /* Analyze font path */
1920         base = analyze_font(buf, &wid, &hgt);
1921
1922         /* Verify suffix */
1923         if (!suffix(base, ".FON")) return (1);
1924
1925         /* Verify file */
1926         if (!check_file(buf)) return (1);
1927
1928         /* Load the new font */
1929         if (!AddFontResource(buf)) return (1);
1930
1931         /* Save new font name */
1932         td->font_file = string_make(base);
1933
1934         /* Remove the "suffix" */
1935         base[strlen(base)-4] = '\0';
1936
1937         /* Create the font (using the 'base' of the font file name!) */
1938         td->font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0,
1939                                  ANSI_CHARSET, OUT_DEFAULT_PRECIS,
1940                                  CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1941                                  FIXED_PITCH | FF_DONTCARE, base);
1942 #endif
1943
1944         /* Hack -- Unknown size */
1945         if (!wid || !hgt)
1946         {
1947                 HDC hdcDesktop;
1948                 HFONT hfOld;
1949                 TEXTMETRIC tm;
1950
1951                 /* all this trouble to get the cell size */
1952                 hdcDesktop = GetDC(HWND_DESKTOP);
1953                 hfOld = SelectObject(hdcDesktop, td->font_id);
1954                 GetTextMetrics(hdcDesktop, &tm);
1955                 SelectObject(hdcDesktop, hfOld);
1956                 ReleaseDC(HWND_DESKTOP, hdcDesktop);
1957
1958                 /* Font size info */
1959                 wid = tm.tmAveCharWidth;
1960                 hgt = tm.tmHeight;
1961         }
1962
1963         /* Save the size info */
1964         td->font_wid = wid;
1965         td->font_hgt = hgt;
1966
1967         /* Success */
1968         return (0);
1969 }
1970
1971
1972
1973 /*
1974  * Allow the user to change the font for this window.
1975  */
1976 static void term_change_font(term_data *td)
1977 {
1978 #if 1 /* #ifdef JP */
1979         CHOOSEFONT cf;
1980
1981         memset(&cf, 0, sizeof(cf));
1982         cf.lStructSize = sizeof(cf);
1983     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
1984     cf.lpLogFont = &(td->lf);
1985
1986         if (ChooseFont(&cf))
1987         {
1988                 /* Force the font */
1989                 term_force_font(td, NULL);
1990
1991                 /* Assume not bizarre */
1992                 td->bizarre = TRUE;
1993
1994                 /* Reset the tile info */
1995                 td->tile_wid = td->font_wid;
1996                 td->tile_hgt = td->font_hgt;
1997
1998                 /* Analyze the font */
1999                 term_getsize(td);
2000
2001                 /* Resize the window */
2002                 term_window_resize(td);
2003         }
2004
2005 #else
2006         OPENFILENAME ofn;
2007
2008         char tmp[1024] = "";
2009
2010         /* Extract a default if possible */
2011         if (td->font_file) strcpy(tmp, td->font_file);
2012
2013         /* Ask for a choice */
2014         memset(&ofn, 0, sizeof(ofn));
2015         ofn.lStructSize = sizeof(ofn);
2016         ofn.hwndOwner = data[0].w;
2017         ofn.lpstrFilter = "Angband Font Files (*.fon)\0*.fon\0";
2018         ofn.nFilterIndex = 1;
2019         ofn.lpstrFile = tmp;
2020         ofn.nMaxFile = 128;
2021         ofn.lpstrInitialDir = ANGBAND_DIR_XTRA_FONT;
2022         ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
2023         ofn.lpstrDefExt = "fon";
2024
2025         /* Force choice if legal */
2026         if (GetOpenFileName(&ofn))
2027         {
2028                 /* Force the font */
2029                 if (term_force_font(td, tmp))
2030                 {
2031                         /* Access the standard font file */
2032                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
2033
2034                         /* Force the use of that font */
2035                         (void)term_force_font(td, tmp);
2036                 }
2037
2038                 /* Assume not bizarre */
2039                 td->bizarre = FALSE;
2040
2041                 /* Reset the tile info */
2042                 td->tile_wid = td->font_wid;
2043                 td->tile_hgt = td->font_hgt;
2044
2045                 /* Analyze the font */
2046                 term_getsize(td);
2047
2048                 /* Resize the window */
2049                 term_window_resize(td);
2050         }
2051 #endif
2052
2053 }
2054
2055 /*
2056  * Allow the user to lock this window.
2057  */
2058 static void term_window_pos(term_data *td, HWND hWnd)
2059 {
2060         SetWindowPos(td->w, hWnd, 0, 0, 0, 0,
2061                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
2062 }
2063
2064 static void windows_map(void);
2065
2066 /*
2067  * Hack -- redraw a term_data
2068  */
2069 static void term_data_redraw(term_data *td)
2070 {
2071         if (td->map_active)
2072         {
2073                 /* Redraw the map */
2074                 windows_map();
2075         }
2076         else
2077         {
2078                 /* Activate the term */
2079                 Term_activate(&td->t);
2080
2081                 /* Redraw the contents */
2082                 Term_redraw();
2083
2084                 /* Restore the term */
2085                 Term_activate(term_screen);
2086         }
2087 }
2088
2089
2090 void Term_inversed_area(HWND hWnd, int x, int y, int w, int h)
2091 {
2092         HDC hdc;
2093         HPEN oldPen;
2094         HBRUSH myBrush, oldBrush;
2095
2096         term_data *td = (term_data *)GetWindowLong(hWnd, 0);
2097         int tx = td->size_ow1 + x * td->tile_wid;
2098         int ty = td->size_oh1 + y * td->tile_hgt;
2099         int tw = w * td->tile_wid - 1;
2100         int th = h * td->tile_hgt - 1;
2101
2102         hdc = GetDC(hWnd);
2103         myBrush = CreateSolidBrush(RGB(255, 255, 255));
2104         oldBrush = SelectObject(hdc, myBrush);
2105         oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2106
2107         PatBlt(hdc, tx, ty, tw, th, PATINVERT);
2108
2109         SelectObject(hdc, oldBrush);
2110         SelectObject(hdc, oldPen);
2111 }
2112
2113
2114
2115 /*** Function hooks needed by "Term" ***/
2116
2117
2118 #if 0
2119
2120 /*
2121  * Initialize a new Term
2122  */
2123 static void Term_init_win(term *t)
2124 {
2125         /* XXX Unused */
2126 }
2127
2128
2129 /*
2130  * Nuke an old Term
2131  */
2132 static void Term_nuke_win(term *t)
2133 {
2134         /* XXX Unused */
2135 }
2136
2137 #endif
2138
2139
2140 /*!
2141  * @brief //!< Windows版ユーザ設定項目実装部(実装必須) /Interact with the User
2142  */
2143 static errr Term_user_win(int n)
2144 {
2145         /* Unused */
2146         (void)n;
2147
2148         /* Success */
2149         return (0);
2150 }
2151
2152
2153 /*
2154  * React to global changes
2155  */
2156 static errr Term_xtra_win_react(void)
2157 {
2158         int i;
2159
2160         /* Simple color */
2161         if (colors16)
2162         {
2163                 /* Save the default colors */
2164                 for (i = 0; i < 256; i++)
2165                 {
2166                         /* Simply accept the desired colors */
2167                         win_pal[i] = angband_color_table[i][0];
2168                 }
2169         }
2170
2171         /* Complex color */
2172         else
2173         {
2174                 COLORREF code;
2175
2176                 byte rv, gv, bv;
2177
2178                 bool change = FALSE;
2179
2180                 /* Save the default colors */
2181                 for (i = 0; i < 256; i++)
2182                 {
2183                         /* Extract desired values */
2184                         rv = angband_color_table[i][1];
2185                         gv = angband_color_table[i][2];
2186                         bv = angband_color_table[i][3];
2187
2188                         /* Extract a full color code */
2189                         code = PALETTERGB(rv, gv, bv);
2190
2191                         /* Activate changes */
2192                         if (win_clr[i] != code)
2193                         {
2194                                 /* Note the change */
2195                                 change = TRUE;
2196
2197                                 /* Apply the desired color */
2198                                 win_clr[i] = code;
2199                         }
2200                 }
2201
2202                 /* Activate the palette if needed */
2203                 if (change) (void)new_palette();
2204         }
2205
2206
2207 #ifdef USE_SOUND
2208
2209         /* Handle "arg_sound" */
2210         if (use_sound != arg_sound)
2211         {
2212                 /* Initialize (if needed) */
2213                 if (arg_sound && !init_sound())
2214                 {
2215                         /* Warning */
2216                         plog(_("サウンドを初期化できません!", "Cannot initialize sound!"));
2217
2218                         /* Cannot enable */
2219                         arg_sound = FALSE;
2220                 }
2221
2222                 /* Change setting */
2223                 use_sound = arg_sound;
2224         }
2225
2226 #endif
2227
2228 #ifdef USE_MUSIC
2229
2230         /* Handle "arg_sound" */
2231         if (use_music != arg_music)
2232         {
2233                 /* Initialize (if needed) */
2234                 if (arg_music && !init_music())
2235                 {
2236                         /* Warning */
2237                         plog(_("BGMを初期化できません!", "Cannot initialize BGM!"));
2238                         /* Cannot enable */
2239                         arg_music = FALSE;
2240                 }
2241
2242                 /* Change setting */
2243                 use_music = arg_music;
2244
2245                 if(!arg_music) stop_music();
2246                 else select_floor_music();
2247
2248         }
2249
2250 #endif
2251
2252
2253 #ifdef USE_GRAPHICS
2254
2255         /* Handle "arg_graphics" */
2256         if (use_graphics != arg_graphics)
2257         {
2258                 /* Initialize (if needed) */
2259                 if (arg_graphics && !init_graphics())
2260                 {
2261                         /* Warning */
2262                         plog(_("グラフィックスを初期化できません!", "Cannot initialize graphics!"));
2263
2264                         /* Cannot enable */
2265                         arg_graphics = GRAPHICS_NONE;
2266                 }
2267
2268                 /* Change setting */
2269                 use_graphics = arg_graphics;
2270
2271                 /* Reset visuals */
2272 #ifdef ANGBAND_2_8_1
2273                 reset_visuals();
2274 #else /* ANGBAND_2_8_1 */
2275                 reset_visuals(TRUE);
2276 #endif /* ANGBAND_2_8_1 */
2277         }
2278
2279 #endif /* USE_GRAPHICS */
2280
2281
2282         /* Clean up windows */
2283         for (i = 0; i < MAX_TERM_DATA; i++)
2284         {
2285                 term *old = Term;
2286
2287                 term_data *td = &data[i];
2288
2289                 /* Update resized windows */
2290                 if ((td->cols != td->t.wid) || (td->rows != td->t.hgt))
2291                 {
2292                         /* Activate */
2293                         Term_activate(&td->t);
2294
2295                         /* Hack -- Resize the term */
2296                         Term_resize(td->cols, td->rows);
2297
2298                         /* Redraw the contents */
2299                         Term_redraw();
2300                         Term_activate(old);
2301                 }
2302         }
2303
2304
2305         /* Success */
2306         return (0);
2307 }
2308
2309
2310 /*
2311  * Process at least one event
2312  */
2313 static errr Term_xtra_win_event(int v)
2314 {
2315         MSG msg;
2316
2317         /* Wait for an event */
2318         if (v)
2319         {
2320                 /* Block */
2321                 if (GetMessage(&msg, NULL, 0, 0))
2322                 {
2323                         TranslateMessage(&msg);
2324                         DispatchMessage(&msg);
2325                 }
2326         }
2327
2328         /* Check for an event */
2329         else
2330         {
2331                 /* Check */
2332                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2333                 {
2334                         TranslateMessage(&msg);
2335                         DispatchMessage(&msg);
2336                 }
2337         }
2338
2339         /* Success */
2340         return 0;
2341 }
2342
2343
2344 /*
2345  * Process all pending events
2346  */
2347 static errr Term_xtra_win_flush(void)
2348 {
2349         MSG msg;
2350
2351         /* Process all pending events */
2352         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2353         {
2354                 TranslateMessage(&msg);
2355                 DispatchMessage(&msg);
2356         }
2357
2358         /* Success */
2359         return (0);
2360 }
2361
2362
2363 /*
2364  * Hack -- clear the screen
2365  *
2366  * Make this more efficient 
2367  */
2368 static errr Term_xtra_win_clear(void)
2369 {
2370         term_data *td = (term_data*)(Term->data);
2371
2372         HDC hdc;
2373         RECT rc;
2374
2375         /* Rectangle to erase */
2376         rc.left = td->size_ow1;
2377         rc.right = rc.left + td->cols * td->tile_wid;
2378         rc.top = td->size_oh1;
2379         rc.bottom = rc.top + td->rows * td->tile_hgt;
2380
2381         /* Erase it */
2382         hdc = GetDC(td->w);
2383         SetBkColor(hdc, RGB(0, 0, 0));
2384         SelectObject(hdc, td->font_id);
2385         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2386
2387         /* bg */
2388         if (use_bg)
2389         {
2390                 rc.left = 0; rc.top = 0;
2391                 DrawBG(hdc, &rc);
2392         }
2393         ReleaseDC(td->w, hdc);
2394
2395         /* Success */
2396         return 0;
2397 }
2398
2399
2400 /*
2401  * Hack -- make a noise
2402  */
2403 static errr Term_xtra_win_noise(void)
2404 {
2405         MessageBeep(MB_ICONASTERISK);
2406         return (0);
2407 }
2408
2409
2410 /*
2411  * Hack -- make a sound
2412  */
2413 static errr Term_xtra_win_sound(int v)
2414 {
2415 #ifdef USE_SOUND
2416         int i;
2417         char buf[1024];
2418 #endif /* USE_SOUND */
2419
2420         /* Sound disabled */
2421         if (!use_sound) return (1);
2422
2423         /* Illegal sound */
2424         if ((v < 0) || (v >= SOUND_MAX)) return (1);
2425
2426 #ifdef USE_SOUND
2427
2428         /* Count the samples */
2429         for (i = 0; i < SAMPLE_MAX; i++)
2430         {
2431                 if (!sound_file[v][i])
2432                         break;
2433         }
2434
2435         /* No sample */
2436         if (i == 0) return (1);
2437
2438         /* Build the path */
2439         path_build(buf, 1024, ANGBAND_DIR_XTRA_SOUND, sound_file[v][Rand_external(i)]);
2440
2441 #ifdef WIN32
2442
2443         /* Play the sound, catch errors */
2444         return (PlaySound(buf, 0, SND_FILENAME | SND_ASYNC));
2445
2446 #else /* WIN32 */
2447
2448         /* Play the sound, catch errors */
2449         return (sndPlaySound(buf, SND_ASYNC));
2450
2451 #endif /* WIN32 */
2452
2453 #else /* USE_SOUND */
2454
2455         return (1);
2456
2457 #endif /* USE_SOUND */
2458 }
2459
2460 /*
2461  * Hack -- play a music
2462  */
2463 static errr Term_xtra_win_music(int n, int v)
2464 {
2465 #ifdef USE_MUSIC
2466         int i = 0;
2467         char buf[1024];
2468 #endif /* USE_MUSIC */
2469
2470         /* Sound disabled */
2471
2472         if(!use_music) return (1);
2473
2474         /* Illegal sound */
2475         if(n == TERM_XTRA_MUSIC_BASIC && ((v < 0) || (v >= MUSIC_BASIC_MAX))) return (1);
2476         else if(v < 0 || v >= 1000) return(1); /*!< TODO */
2477
2478 #ifdef USE_MUSIC
2479
2480         switch(n)
2481         {
2482         case TERM_XTRA_MUSIC_BASIC:
2483                 for (i = 0; i < SAMPLE_MAX; i++) if(!music_file[v][i]) break;
2484                 break;
2485         case TERM_XTRA_MUSIC_DUNGEON:
2486                 for (i = 0; i < SAMPLE_MAX; i++) if(!dungeon_music_file[v][i]) break;
2487                 break;
2488         case TERM_XTRA_MUSIC_QUEST:
2489                 for (i = 0; i < SAMPLE_MAX; i++) if(!quest_music_file[v][i]) break;
2490                 break;
2491         case TERM_XTRA_MUSIC_TOWN:
2492                 for (i = 0; i < SAMPLE_MAX; i++) if(!town_music_file[v][i]) break;
2493                 break;
2494         }
2495
2496         /* No sample */
2497         if (i == 0)
2498         {
2499                 //mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2500                 //mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2501                 return (1);
2502         }
2503
2504         switch(n)
2505         {
2506         case TERM_XTRA_MUSIC_BASIC:
2507                 for (i = 0; i < SAMPLE_MAX; i++) if(!music_file[v][i]) break;
2508                 break;
2509         case TERM_XTRA_MUSIC_DUNGEON:
2510                 for (i = 0; i < SAMPLE_MAX; i++) if(!dungeon_music_file[v][i]) break;
2511                 break;
2512         case TERM_XTRA_MUSIC_QUEST:
2513                 for (i = 0; i < SAMPLE_MAX; i++) if(!quest_music_file[v][i]) break;
2514                 break;
2515         case TERM_XTRA_MUSIC_TOWN:
2516                 for (i = 0; i < SAMPLE_MAX; i++) if(!town_music_file[v][i]) break;
2517                 break;
2518         }
2519
2520         /* No sample */
2521         if (i == 0)
2522         {
2523                 mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2524                 mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2525                 return (1);
2526         }
2527
2528         switch(n)
2529         {
2530         case TERM_XTRA_MUSIC_BASIC:
2531                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, music_file[v][Rand_external(i)]);
2532                 break;
2533         case TERM_XTRA_MUSIC_DUNGEON:
2534                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, dungeon_music_file[v][Rand_external(i)]);
2535                 break;
2536         case TERM_XTRA_MUSIC_QUEST:
2537                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, quest_music_file[v][Rand_external(i)]);
2538                 break;
2539         case TERM_XTRA_MUSIC_TOWN:
2540                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, town_music_file[v][Rand_external(i)]);
2541                 break;
2542         }
2543
2544         if(current_music_type == n && current_music_id == v)
2545         {
2546                 return (0);
2547         }
2548         current_music_type = n;
2549         current_music_id = v;
2550
2551 #ifdef WIN32
2552
2553         mop.lpstrDeviceType = mci_device_type;  
2554         mop.lpstrElementName = buf;
2555         mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2556         mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2557         mciSendCommand(mop.wDeviceID, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mop);
2558         mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
2559         mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
2560         return (0);
2561
2562 #endif /* WIN32 */
2563
2564 #else /* USE_MUSIC */
2565
2566         return (1);
2567
2568 #endif /* USE_MUSIC */
2569
2570 }
2571
2572
2573 /*
2574  * Delay for "x" milliseconds
2575  */
2576 static int Term_xtra_win_delay(int v)
2577 {
2578
2579 #ifdef WIN32
2580
2581         /* Sleep */
2582         Sleep(v);
2583
2584 #else /* WIN32 */
2585
2586         DWORD t;
2587         MSG msg;
2588
2589         /* Final count */
2590         t = GetTickCount() + v;
2591
2592         /* Wait for it */
2593         while (GetTickCount() < t)
2594         {
2595                 /* Handle messages */
2596                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2597                 {
2598                         TranslateMessage(&msg);
2599                         DispatchMessage(&msg);
2600                 }
2601         }
2602
2603 #endif /* WIN32 */
2604
2605         /* Success */
2606         return (0);
2607 }
2608
2609
2610 /*
2611  * Do a "special thing"
2612  */
2613 static errr Term_xtra_win(int n, int v)
2614 {
2615         /* Handle a subset of the legal requests */
2616         switch (n)
2617         {
2618                 /* Make a bell sound */
2619                 case TERM_XTRA_NOISE:
2620                 {
2621                         return (Term_xtra_win_noise());
2622                 }
2623
2624                 /* Play a music */
2625                 case TERM_XTRA_MUSIC_BASIC:
2626                 case TERM_XTRA_MUSIC_DUNGEON:
2627                 case TERM_XTRA_MUSIC_QUEST:
2628                 case TERM_XTRA_MUSIC_TOWN:
2629                 {
2630                         return (Term_xtra_win_music(n, v));
2631                 }
2632
2633                 /* Make a special sound */
2634                 case TERM_XTRA_SOUND:
2635                 {
2636                         return (Term_xtra_win_sound(v));
2637                 }
2638
2639                 /* Process random events */
2640                 case TERM_XTRA_BORED:
2641                 {
2642                         return (Term_xtra_win_event(0));
2643                 }
2644
2645                 /* Process an event */
2646                 case TERM_XTRA_EVENT:
2647                 {
2648                         return (Term_xtra_win_event(v));
2649                 }
2650
2651                 /* Flush all events */
2652                 case TERM_XTRA_FLUSH:
2653                 {
2654                         return (Term_xtra_win_flush());
2655                 }
2656
2657                 /* Clear the screen */
2658                 case TERM_XTRA_CLEAR:
2659                 {
2660                         return (Term_xtra_win_clear());
2661                 }
2662
2663                 /* React to global changes */
2664                 case TERM_XTRA_REACT:
2665                 {
2666                         return (Term_xtra_win_react());
2667                 }
2668
2669                 /* Delay for some milliseconds */
2670                 case TERM_XTRA_DELAY:
2671                 {
2672                         return (Term_xtra_win_delay(v));
2673                 }
2674         }
2675
2676         return 1;
2677 }
2678
2679
2680
2681 /*
2682  * Low level graphics (Assumes valid input).
2683  *
2684  * Draw a "cursor" at (x,y), using a "yellow box".
2685  */
2686 static errr Term_curs_win(int x, int y)
2687 {
2688         term_data *td = (term_data*)(Term->data);
2689
2690         RECT rc;
2691         HDC hdc;
2692
2693         int tile_wid, tile_hgt;
2694
2695         if (td->map_active)
2696         {
2697                 tile_wid = td->map_tile_wid;
2698                 tile_hgt = td->map_tile_hgt;
2699         }
2700         else
2701         {
2702                 tile_wid = td->tile_wid;
2703                 tile_hgt = td->tile_hgt;
2704         }
2705
2706         /* Frame the grid */
2707         rc.left = x * tile_wid + td->size_ow1;
2708         rc.right = rc.left + tile_wid;
2709         rc.top = y * tile_hgt + td->size_oh1;
2710         rc.bottom = rc.top + tile_hgt;
2711
2712         /* Cursor is done as a yellow "box" */
2713         hdc = GetDC(td->w);
2714         FrameRect(hdc, &rc, hbrYellow);
2715         ReleaseDC(td->w, hdc);
2716
2717         /* Success */
2718         return 0;
2719 }
2720
2721
2722 /*
2723  * Low level graphics (Assumes valid input).
2724  *
2725  * Draw a "big cursor" at (x,y), using a "yellow box".
2726  */
2727 static errr Term_bigcurs_win(int x, int y)
2728 {
2729         term_data *td = (term_data*)(Term->data);
2730
2731         RECT rc;
2732         HDC hdc;
2733
2734         int tile_wid, tile_hgt;
2735
2736         if (td->map_active)
2737         {
2738                 /* Normal cursor in map window */
2739                 Term_curs_win(x, y);
2740                 return 0;
2741         }
2742         else
2743         {
2744                 tile_wid = td->tile_wid;
2745                 tile_hgt = td->tile_hgt;
2746         }
2747
2748         /* Frame the grid */
2749         rc.left = x * tile_wid + td->size_ow1;
2750         rc.right = rc.left + 2 * tile_wid;
2751         rc.top = y * tile_hgt + td->size_oh1;
2752         rc.bottom = rc.top + tile_hgt;
2753
2754         /* Cursor is done as a yellow "box" */
2755         hdc = GetDC(td->w);
2756         FrameRect(hdc, &rc, hbrYellow);
2757         ReleaseDC(td->w, hdc);
2758
2759         /* Success */
2760         return 0;
2761 }
2762
2763
2764 /*
2765  * Low level graphics (Assumes valid input).
2766  *
2767  * Erase a "block" of "n" characters starting at (x,y).
2768  */
2769 static errr Term_wipe_win(int x, int y, int n)
2770 {
2771         term_data *td = (term_data*)(Term->data);
2772
2773         HDC hdc;
2774         RECT rc;
2775
2776         /* Rectangle to erase in client coords */
2777         rc.left = x * td->tile_wid + td->size_ow1;
2778         rc.right = rc.left + n * td->tile_wid;
2779         rc.top = y * td->tile_hgt + td->size_oh1;
2780         rc.bottom = rc.top + td->tile_hgt;
2781
2782         hdc = GetDC(td->w);
2783         SetBkColor(hdc, RGB(0, 0, 0));
2784         SelectObject(hdc, td->font_id);
2785         /* bg */
2786         if (use_bg)
2787                 DrawBG(hdc, &rc);
2788         else
2789                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2790         ReleaseDC(td->w, hdc);
2791
2792         /* Success */
2793         return 0;
2794 }
2795
2796
2797 /*
2798  * Low level graphics.  Assumes valid input.
2799  *
2800  * Draw several ("n") chars, with an attr, at a given location.
2801  *
2802  * All "graphic" data is handled by "Term_pict_win()", below.
2803  *
2804  * One would think there is a more efficient method for telling a window
2805  * what color it should be using to draw with, but perhaps simply changing
2806  * it every time is not too inefficient.  
2807  */
2808 static errr Term_text_win(int x, int y, int n, TERM_COLOR a, concptr s)
2809 {
2810         term_data *td = (term_data*)(Term->data);
2811         RECT rc;
2812         HDC hdc;
2813
2814 #if 1 /* #ifdef JP */
2815         static HBITMAP  WALL;
2816         static HBRUSH   myBrush, oldBrush;
2817         static HPEN     oldPen;
2818         static bool init_done = FALSE;
2819
2820         if (!init_done){
2821                 WALL = LoadBitmap(hInstance, AppName);
2822                 myBrush = CreatePatternBrush(WALL);
2823                 init_done = TRUE;
2824         }
2825 #endif
2826
2827         /* Total rectangle */
2828         rc.left = x * td->tile_wid + td->size_ow1;
2829         rc.right = rc.left + n * td->tile_wid;
2830         rc.top = y * td->tile_hgt + td->size_oh1;
2831         rc.bottom = rc.top + td->tile_hgt;
2832
2833         /* Acquire DC */
2834         hdc = GetDC(td->w);
2835
2836         /* Background color */
2837         SetBkColor(hdc, RGB(0, 0, 0));
2838
2839         /* Foreground color */
2840         if (colors16)
2841         {
2842                 SetTextColor(hdc, PALETTEINDEX(win_pal[a]));
2843         }
2844         else if (paletted)
2845         {
2846                 SetTextColor(hdc, win_clr[a&0x0F]);
2847         }
2848         else
2849         {
2850                 SetTextColor(hdc, win_clr[a]);
2851         }
2852
2853         /* Use the font */
2854         SelectObject(hdc, td->font_id);
2855         
2856         /* bg */
2857         if (use_bg) SetBkMode(hdc, TRANSPARENT);
2858
2859         /* Bizarre size */
2860         if (td->bizarre ||
2861             (td->tile_hgt != td->font_hgt) ||
2862             (td->tile_wid != td->font_wid))
2863         {
2864                 int i;
2865
2866                 /* Erase complete rectangle */
2867                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2868                 
2869                 /* bg */
2870                 if (use_bg) DrawBG(hdc, &rc);
2871
2872                 /* New rectangle */
2873                 rc.left += ((td->tile_wid - td->font_wid) / 2);
2874                 rc.right = rc.left + td->font_wid;
2875                 rc.top += ((td->tile_hgt - td->font_hgt) / 2);
2876                 rc.bottom = rc.top + td->font_hgt;
2877
2878                 /* Dump each character */
2879                 for (i = 0; i < n; i++)
2880                 {
2881 #ifdef JP
2882                         if (use_bigtile && *(s+i)=="■"[0] && *(s+i+1)=="■"[1])
2883                         {
2884                                 rc.right += td->font_wid;
2885
2886                                 oldBrush = SelectObject(hdc, myBrush);
2887                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2888
2889                                 /* Dump the wall */
2890                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2891
2892                                 SelectObject(hdc, oldBrush);
2893                                 SelectObject(hdc, oldPen);
2894                                 rc.right -= td->font_wid;
2895
2896                                 /* Advance */
2897                                 i++;
2898                                 rc.left += 2 * td->tile_wid;
2899                                 rc.right += 2 * td->tile_wid;
2900                         }
2901                         else if ( iskanji(*(s+i)) )  /*  2バイト文字  */
2902                         {
2903                                 rc.right += td->font_wid;
2904                                 /* Dump the text */
2905                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2906                                        s+i, 2, NULL);
2907                                 rc.right -= td->font_wid;
2908
2909                                 /* Advance */
2910                                 i++;
2911                                 rc.left += 2 * td->tile_wid;
2912                                 rc.right += 2 * td->tile_wid;
2913                         } else if (*(s+i)==127){
2914                                 oldBrush = SelectObject(hdc, myBrush);
2915                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2916
2917                                 /* Dump the wall */
2918                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2919
2920                                 SelectObject(hdc, oldBrush);
2921                                 SelectObject(hdc, oldPen);
2922
2923                                 /* Advance */
2924                                 rc.left += td->tile_wid;
2925                                 rc.right += td->tile_wid;
2926                         } else {
2927                                 /* Dump the text */
2928                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s+i, 1, NULL);
2929
2930                                 /* Advance */
2931                                 rc.left += td->tile_wid;
2932                                 rc.right += td->tile_wid;
2933                         }
2934 #else
2935 #if 1
2936                         if (*(s+i)==127){
2937                                 oldBrush = SelectObject(hdc, myBrush);
2938                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2939
2940                                 /* Dump the wall */
2941                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2942
2943                                 SelectObject(hdc, oldBrush);
2944                                 SelectObject(hdc, oldPen);
2945
2946                                 /* Advance */
2947                                 rc.left += td->tile_wid;
2948                                 rc.right += td->tile_wid;
2949                         } else {
2950                                 /* Dump the text */
2951                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2952                                        s+i, 1, NULL);
2953
2954                                 /* Advance */
2955                                 rc.left += td->tile_wid;
2956                                 rc.right += td->tile_wid;
2957                         }
2958 #else
2959                         /* Dump the text */
2960                         ExtTextOut(hdc, rc.left, rc.top, 0, &rc,
2961                                    s+i, 1, NULL);
2962
2963                         /* Advance */
2964                         rc.left += td->tile_wid;
2965                         rc.right += td->tile_wid;
2966 #endif
2967 #endif
2968
2969                 }
2970         }
2971
2972         /* Normal size */
2973         else
2974         {
2975                 /* Dump the text */
2976                 ExtTextOut(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc,
2977                            s, n, NULL);
2978         }
2979
2980         /* Release DC */
2981         ReleaseDC(td->w, hdc);
2982
2983         /* Success */
2984         return 0;
2985 }
2986
2987
2988 /*
2989  * Low level graphics.  Assumes valid input.
2990  *
2991  * Draw an array of "special" attr/char pairs at the given location.
2992  *
2993  * We use the "Term_pict_win()" function for "graphic" data, which are
2994  * encoded by setting the "high-bits" of both the "attr" and the "char"
2995  * data.  We use the "attr" to represent the "row" of the main bitmap,
2996  * and the "char" to represent the "col" of the main bitmap.  The use
2997  * of this function is induced by the "higher_pict" flag.
2998  *
2999  * If "graphics" is not available, we simply "wipe" the given grids.
3000  */
3001 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)
3002 {
3003         term_data *td = (term_data*)(Term->data);
3004
3005 #ifdef USE_GRAPHICS
3006
3007         int i;
3008         TERM_LEN x1, y1, w1, h1, tw1, th1;
3009         TERM_LEN x2, y2, w2, h2, tw2 = 0;
3010         TERM_LEN x3, y3;
3011
3012         HDC hdcMask = NULL;
3013
3014         HDC hdc;
3015         HDC hdcSrc;
3016         HBITMAP hbmSrcOld;
3017         if (!use_graphics)
3018         {
3019                 /* Erase the grids */
3020                 return (Term_wipe_win(x, y, n));
3021         }
3022
3023         /* Size of bitmap cell */
3024         w1 = infGraph.CellWidth;
3025         h1 = infGraph.CellHeight;
3026         tw1 = infGraph.TileWidth;
3027         th1 = infGraph.TileHeight;
3028
3029         /* Size of window cell */
3030         if (td->map_active)
3031         {
3032                 w2 = td->map_tile_wid;
3033                 h2 = td->map_tile_hgt;
3034         }
3035         else
3036         {
3037                 w2 = td->tile_wid;
3038                 h2 = td->tile_hgt;
3039                 tw2 = w2;
3040
3041                 /* big tile mode */
3042                 if (use_bigtile) tw2 *= 2;
3043         }
3044
3045         /* Location of window cell */
3046         x2 = x * w2 + td->size_ow1 + infGraph.OffsetX;
3047         y2 = y * h2 + td->size_oh1 + infGraph.OffsetY;
3048
3049         /* Info */
3050         hdc = GetDC(td->w);
3051
3052         /* More info */
3053         hdcSrc = CreateCompatibleDC(hdc);
3054         hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
3055
3056         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3057         {
3058                 hdcMask = CreateCompatibleDC(hdc);
3059                 SelectObject(hdcMask, infMask.hBitmap);
3060         }
3061
3062         /* Draw attr/char pairs */
3063         for (i = 0; i < n; i++, x2 += w2)
3064         {
3065                 TERM_COLOR a = ap[i];
3066                 char c = cp[i];
3067
3068
3069                 /* Extract picture */
3070                 int row = (a & 0x7F);
3071                 int col = (c & 0x7F);
3072
3073                 /* Location of bitmap cell */
3074                 x1 = col * w1;
3075                 y1 = row * h1;
3076
3077                 if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3078                 {
3079                         x3 = (tcp[i] & 0x7F) * w1;
3080                         y3 = (tap[i] & 0x7F) * h1;
3081                         tw2 = tw2 * w1 / tw1;
3082                         h2 = h2 * h1 / th1;
3083
3084                         /* Perfect size */
3085                         if ((tw1 == tw2) && (th1 == h2))
3086                         {
3087                                 /* Copy the terrain picture from the bitmap to the window */
3088                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
3089
3090                                 /* Mask out the tile */
3091                                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
3092
3093                                 /* Draw the tile */
3094                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
3095                         }
3096
3097                         /* Need to stretch */
3098                         else
3099                         {
3100                                 /* Set the correct mode for stretching the tiles */
3101                                 SetStretchBltMode(hdc, COLORONCOLOR);
3102
3103                                 /* Copy the terrain picture from the bitmap to the window */
3104                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x3, y3, w1, h1, SRCAND);
3105
3106                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCPAINT);
3107
3108                                 /* Only draw if terrain and overlay are different */
3109                                 if ((x1 != x3) || (y1 != y3))
3110                                 {
3111                                         /* Mask out the tile */
3112                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
3113
3114                                         /* Draw the tile */
3115                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
3116                                 }
3117                         }
3118                 }
3119                 else
3120                 {
3121                         /* Perfect size */
3122                         if ((w1 == tw2) && (h1 == h2))
3123                         {
3124                                 /* Copy the picture from the bitmap to the window */
3125                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
3126                         }
3127
3128                         /* Need to stretch */
3129                         else
3130                         {
3131                                 /* Set the correct mode for stretching the tiles */
3132                                 SetStretchBltMode(hdc, COLORONCOLOR);
3133
3134                                 /* Copy the picture from the bitmap to the window */
3135                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
3136                         }
3137                 }
3138         }
3139
3140         /* Release */
3141         SelectObject(hdcSrc, hbmSrcOld);
3142         DeleteDC(hdcSrc);
3143
3144         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3145         {
3146                 /* Release */
3147                 SelectObject(hdcMask, hbmSrcOld);
3148                 DeleteDC(hdcMask);
3149         }
3150
3151         /* Release */
3152         ReleaseDC(td->w, hdc);
3153
3154 #else /* USE_GRAPHICS */
3155
3156         /* Just erase this grid */
3157         return (Term_wipe_win(x, y, n));
3158
3159 #endif /* USE_GRAPHICS */
3160
3161         /* Success */
3162         return 0;
3163 }
3164
3165
3166 static void windows_map(void)
3167 {
3168         term_data *td = &data[0];
3169         TERM_COLOR a;
3170         char c;
3171         TERM_LEN x, min_x, max_x;
3172         TERM_LEN y, min_y, max_y;
3173
3174         TERM_COLOR ta;
3175         char tc;
3176
3177         /* Only in graphics mode */
3178         if (!use_graphics) return;
3179         Term_xtra_win_clear();
3180
3181         td->map_tile_wid = (td->tile_wid * td->cols) / MAX_WID;
3182         td->map_tile_hgt = (td->tile_hgt * td->rows) / MAX_HGT;
3183         td->map_active = TRUE;
3184
3185         {
3186                 min_x = 0;
3187                 min_y = 0;
3188                 max_x = current_floor_ptr->width;
3189                 max_y = current_floor_ptr->height;
3190         }
3191
3192         /* Draw the map */
3193         for (x = min_x; x < max_x; x++)
3194         {
3195                 for (y = min_y; y < max_y; y++)
3196                 {
3197                         map_info(y, x, &a, (char*)&c, &ta, (char*)&tc);
3198
3199                         /* Ignore non-graphics */
3200                         if ((a & 0x80) && (c & 0x80))
3201                         {
3202                                 Term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
3203                         }
3204                 }
3205         }
3206
3207         /* Hilite the player */
3208         Term_curs_win(p_ptr->x - min_x, p_ptr->y - min_y);
3209
3210         /* Wait for a keypress, flush key buffer */
3211         Term_inkey(&c, TRUE, TRUE);
3212         Term_flush();
3213
3214         /* Switch off the map display */
3215         td->map_active = FALSE;
3216
3217         /* Restore screen */
3218         Term_xtra_win_clear();
3219         Term_redraw();
3220 }
3221
3222
3223 /*** Other routines ***/
3224
3225
3226 /*
3227  * Create and initialize a "term_data" given a title
3228  */
3229 static void term_data_link(term_data *td)
3230 {
3231         term *t = &td->t;
3232
3233         /* Initialize the term */
3234         term_init(t, td->cols, td->rows, td->keys);
3235
3236         /* Use a "software" cursor */
3237         t->soft_cursor = TRUE;
3238
3239         /* Use "Term_pict" for "graphic" data */
3240         t->higher_pict = TRUE;
3241
3242         /* Erase with "white space" */
3243         t->attr_blank = TERM_WHITE;
3244         t->char_blank = ' ';
3245
3246 #if 0
3247         /* Prepare the init/nuke hooks */
3248         t->init_hook = Term_init_win;
3249         t->nuke_hook = Term_nuke_win;
3250 #endif
3251
3252         /* Prepare the template hooks */
3253         t->user_hook = Term_user_win;
3254         t->xtra_hook = Term_xtra_win;
3255         t->curs_hook = Term_curs_win;
3256         t->bigcurs_hook = Term_bigcurs_win;
3257         t->wipe_hook = Term_wipe_win;
3258         t->text_hook = Term_text_win;
3259         t->pict_hook = Term_pict_win;
3260
3261         /* Remember where we came from */
3262         t->data = (vptr)(td);
3263 }
3264
3265
3266 /*
3267  * Create the windows
3268  *
3269  * First, instantiate the "default" values, then read the "ini_file"
3270  * to over-ride selected values, then create the windows, and fonts.
3271  *
3272  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
3273  * must make active to realize the palette.  
3274  */
3275 static void init_windows(void)
3276 {
3277         int i;
3278
3279         term_data *td;
3280
3281 #if 0 /* #ifndef JP */
3282         char buf[1024];
3283 #endif
3284
3285         /* Main window */
3286         td = &data[0];
3287         WIPE(td, term_data);
3288 #ifdef JP
3289         td->s = "変愚蛮怒";
3290 #else
3291         td->s = angband_term_name[0];
3292 #endif
3293
3294         td->keys = 1024;
3295         td->rows = 24;
3296         td->cols = 80;
3297         td->visible = TRUE;
3298         td->size_ow1 = 2;
3299         td->size_ow2 = 2;
3300         td->size_oh1 = 2;
3301         td->size_oh2 = 2;
3302         td->pos_x = 7 * 30;
3303         td->pos_y = 7 * 20;
3304         td->posfix = FALSE;
3305 #if 1 /* #ifdef JP */
3306         td->bizarre = TRUE;
3307 #endif
3308         /* Sub windows */
3309         for (i = 1; i < MAX_TERM_DATA; i++)
3310         {
3311                 td = &data[i];
3312                 WIPE(td, term_data);
3313                 td->s = angband_term_name[i];
3314                 td->keys = 16;
3315                 td->rows = 24;
3316                 td->cols = 80;
3317                 td->visible = FALSE;
3318                 td->size_ow1 = 1;
3319                 td->size_ow2 = 1;
3320                 td->size_oh1 = 1;
3321                 td->size_oh2 = 1;
3322                 td->pos_x = (7 - i) * 30;
3323                 td->pos_y = (7 - i) * 20;
3324                 td->posfix = FALSE;
3325 #if 1 /* #ifdef JP */
3326                         td->bizarre = TRUE;
3327 #endif
3328         }
3329
3330
3331         /* Load prefs */
3332         load_prefs();
3333
3334
3335         /* Main window (need these before term_getsize gets called) */
3336         td = &data[0];
3337         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU |
3338                        WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION |
3339                        WS_VISIBLE);
3340         td->dwExStyle = 0;
3341         td->visible = TRUE;
3342
3343         /* Sub windows (need these before term_getsize gets called) */
3344         for (i = 1; i < MAX_TERM_DATA; i++)
3345         {
3346                 td = &data[i];
3347                 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
3348                 td->dwExStyle = (WS_EX_TOOLWINDOW);
3349         }
3350
3351
3352         /* All windows */
3353         for (i = 0; i < MAX_TERM_DATA; i++)
3354         {
3355                 td = &data[i];
3356
3357 #if 1 /* #ifdef JP */
3358                 strncpy(td->lf.lfFaceName, td->font_want, LF_FACESIZE);
3359                 td->lf.lfCharSet = DEFAULT_CHARSET;
3360                 td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
3361                 /* Activate the chosen font */
3362                 term_force_font(td, NULL);
3363                 if(!td->tile_wid) td->tile_wid = td->font_wid;
3364                 if(!td->tile_hgt) td->tile_hgt = td->font_hgt;
3365 #else
3366                 /* Access the standard font file */
3367                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, td->font_want);
3368
3369                 /* Activate the chosen font */
3370                 if (term_force_font(td, buf))
3371                 {
3372                         /* Access the standard font file */
3373                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
3374
3375                         /* Force the use of that font */
3376                         (void)term_force_font(td, buf);
3377
3378                         td->tile_wid = 8;
3379                         td->tile_hgt = 13;
3380
3381                         /* Assume not bizarre */
3382                         td->bizarre = FALSE;
3383                 }
3384 #endif
3385
3386
3387                 /* Analyze the font */
3388                 term_getsize(td);
3389
3390                 /* Resize the window */
3391                 term_window_resize(td);
3392         }
3393
3394
3395         /* Sub windows (reverse order) */
3396         for (i = MAX_TERM_DATA - 1; i >= 1; --i)
3397         {
3398                 td = &data[i];
3399
3400                 my_td = td;
3401                 td->w = CreateWindowEx(td->dwExStyle, AngList,
3402                                        td->s, td->dwStyle,
3403                                        td->pos_x, td->pos_y,
3404                                        td->size_wid, td->size_hgt,
3405                                        HWND_DESKTOP, NULL, hInstance, NULL);
3406                 my_td = NULL;
3407                 if (!td->w) quit(_("サブウィンドウに作成に失敗しました", "Failed to create sub-window"));
3408
3409                 if (td->visible)
3410                 {
3411                         td->size_hack = TRUE;
3412                         ShowWindow(td->w, SW_SHOW);
3413                         td->size_hack = FALSE;
3414                 }
3415
3416                 term_data_link(td);
3417                 angband_term[i] = &td->t;
3418
3419                 if (td->visible)
3420                 {
3421                         /* Activate the window */
3422                         SetActiveWindow(td->w);
3423                 }
3424
3425                 if (data[i].posfix)
3426                 {
3427                         term_window_pos(&data[i], HWND_TOPMOST);
3428                 }
3429                 else
3430                 {
3431                         term_window_pos(&data[i], td->w);
3432                 }
3433         }
3434
3435
3436         /* Main window */
3437         td = &data[0];
3438
3439         /* Main window */
3440         my_td = td;
3441         td->w = CreateWindowEx(td->dwExStyle, AppName,
3442                                td->s, td->dwStyle,
3443                                td->pos_x, td->pos_y,
3444                                td->size_wid, td->size_hgt,
3445                                HWND_DESKTOP, NULL, hInstance, NULL);
3446         my_td = NULL;
3447         if (!td->w) quit(_("メインウィンドウの作成に失敗しました", "Failed to create Angband window"));
3448
3449         term_data_link(td);
3450         angband_term[0] = &td->t;
3451         normsize.x = td->cols;
3452         normsize.y = td->rows;
3453
3454         /* Activate the main window */
3455         if (win_maximized) ShowWindow(td->w, SW_SHOWMAXIMIZED);
3456         else ShowWindow(td->w, SW_SHOW);
3457
3458         /* Bring main window back to top */
3459         SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
3460
3461
3462         /* New palette */
3463         (void)new_palette();
3464
3465
3466         /* Create a "brush" for drawing the "cursor" */
3467         hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
3468
3469
3470         /* Process pending messages */
3471         (void)Term_xtra_win_flush();
3472 }
3473
3474
3475
3476 /*
3477  * Prepare the menus
3478  */
3479 static void setup_menus(void)
3480 {
3481         int i;
3482
3483         HMENU hm = GetMenu(data[0].w);
3484
3485
3486         /* Menu "File", Disable all */
3487         EnableMenuItem(hm, IDM_FILE_NEW,
3488                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3489         EnableMenuItem(hm, IDM_FILE_OPEN,
3490                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3491         EnableMenuItem(hm, IDM_FILE_SAVE,
3492                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3493         EnableMenuItem(hm, IDM_FILE_EXIT,
3494                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3495         EnableMenuItem(hm, IDM_FILE_SCORE,
3496                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3497
3498
3499         /* No character available */
3500         if (!character_generated)
3501         {
3502                 /* Menu "File", Item "New" */
3503                 EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
3504
3505                 /* Menu "File", Item "Open" */
3506                 EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
3507         }
3508
3509         /* A character available */
3510         if (character_generated)
3511         {
3512                 /* Menu "File", Item "Save" */
3513                 EnableMenuItem(hm, IDM_FILE_SAVE,
3514                            MF_BYCOMMAND | MF_ENABLED);
3515         }
3516
3517         /* Menu "File", Item "Exit" */
3518         EnableMenuItem(hm, IDM_FILE_EXIT,
3519                        MF_BYCOMMAND | MF_ENABLED);
3520
3521         EnableMenuItem(hm, IDM_FILE_SCORE,
3522                        MF_BYCOMMAND | MF_ENABLED);
3523
3524
3525         /* Menu "Window::Visibility" */
3526         for (i = 0; i < MAX_TERM_DATA; i++)
3527         {
3528                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3529                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3530
3531                 CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3532                               (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
3533
3534                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3535                                MF_BYCOMMAND | MF_ENABLED);
3536         }
3537
3538         /* Menu "Window::Font" */
3539         for (i = 0; i < MAX_TERM_DATA; i++)
3540         {
3541                 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3542                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3543
3544                 if (data[i].visible)
3545                 {
3546                         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3547                                        MF_BYCOMMAND | MF_ENABLED);
3548                 }
3549         }
3550
3551         /* Menu "Window::Window Position Fix" */
3552         for (i = 0; i < MAX_TERM_DATA; i++)
3553         {
3554                 EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3555                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3556
3557                 CheckMenuItem(hm, IDM_WINDOW_POS_0 + i,
3558                               (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
3559
3560                 if (data[i].visible)
3561                 {
3562                         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3563                                        MF_BYCOMMAND | MF_ENABLED);
3564                 }
3565         }
3566
3567         /* Menu "Window::Bizarre Display" */
3568         for (i = 0; i < MAX_TERM_DATA; i++)
3569         {
3570                 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3571                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3572
3573                 CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3574                               (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
3575
3576                 if (data[i].visible)
3577                 {
3578                         EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3579                                    MF_BYCOMMAND | MF_ENABLED);
3580
3581                 }
3582         }
3583
3584         /* Menu "Window::Increase Tile Width" */
3585         for (i = 0; i < MAX_TERM_DATA; i++)
3586         {
3587                 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3588                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3589
3590                 if (data[i].visible)
3591                 {
3592                         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3593                                    MF_BYCOMMAND | MF_ENABLED);
3594
3595                 }
3596         }
3597
3598         /* Menu "Window::Decrease Tile Width" */
3599         for (i = 0; i < MAX_TERM_DATA; i++)
3600         {
3601                 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3602                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3603
3604                 if (data[i].visible)
3605                 {
3606                         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3607                                    MF_BYCOMMAND | MF_ENABLED);
3608
3609                 }
3610         }
3611
3612         /* Menu "Window::Increase Tile Height" */
3613         for (i = 0; i < MAX_TERM_DATA; i++)
3614         {
3615                 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3616                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3617
3618                 if (data[i].visible)
3619                 {
3620                         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3621                                    MF_BYCOMMAND | MF_ENABLED);
3622
3623                 }
3624         }
3625
3626         /* Menu "Window::Decrease Tile Height" */
3627         for (i = 0; i < MAX_TERM_DATA; i++)
3628         {
3629                 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3630                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3631
3632                 if (data[i].visible)
3633                 {
3634                         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3635                                    MF_BYCOMMAND | MF_ENABLED);
3636
3637                 }
3638         }
3639
3640         /* Menu "Options", disable all */
3641         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3642                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3643         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3644                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3645         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3646                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3647         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE,
3648                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3649         EnableMenuItem(hm, IDM_OPTIONS_SOUND,
3650                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3651 #ifndef JP
3652         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3653                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3654 #endif
3655
3656         /* Menu "Options", Item "Map" */
3657         if (use_graphics != GRAPHICS_NONE)
3658                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_ENABLED);
3659         else
3660                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3661                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3662
3663         /* Menu "Options", update all */
3664         CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3665                       (arg_graphics == GRAPHICS_NONE ? MF_CHECKED : MF_UNCHECKED));
3666         CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3667                       (arg_graphics == GRAPHICS_ORIGINAL ? MF_CHECKED : MF_UNCHECKED));
3668         CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3669                       (arg_graphics == GRAPHICS_ADAM_BOLT ? MF_CHECKED : MF_UNCHECKED));
3670         CheckMenuItem(hm, IDM_OPTIONS_NEW2_GRAPHICS,
3671                       (arg_graphics == GRAPHICS_HENGBAND ? MF_CHECKED : MF_UNCHECKED));
3672         CheckMenuItem(hm, IDM_OPTIONS_BIGTILE,
3673                       (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
3674         CheckMenuItem(hm, IDM_OPTIONS_MUSIC,
3675                       (arg_music ? MF_CHECKED : MF_UNCHECKED));
3676         CheckMenuItem(hm, IDM_OPTIONS_SOUND,
3677                       (arg_sound ? MF_CHECKED : MF_UNCHECKED));
3678         CheckMenuItem(hm, IDM_OPTIONS_BG,
3679                       (use_bg ? MF_CHECKED : MF_UNCHECKED));
3680 #ifndef JP
3681         CheckMenuItem(hm, IDM_OPTIONS_SAVER,
3682                       (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
3683 #endif
3684
3685 #ifdef USE_GRAPHICS
3686         /* Menu "Options", Item "Graphics" */
3687         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_ENABLED);
3688         /* Menu "Options", Item "Graphics" */
3689         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_ENABLED);
3690         /* Menu "Options", Item "Graphics" */
3691         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_ENABLED);
3692         /* Menu "Options", Item "Graphics" */
3693         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_ENABLED);
3694 #endif /* USE_GRAPHICS */
3695
3696 #ifdef USE_SOUND
3697         /* Menu "Options", Item "Sound" */
3698         EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_ENABLED);
3699 #endif /* USE_SOUND */
3700
3701 #ifdef USE_SAVER
3702         /* Menu "Options", Item "ScreenSaver" */
3703         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3704                        MF_BYCOMMAND | MF_ENABLED);
3705 #endif /* USE_SAVER */
3706 }
3707
3708
3709 /*
3710  * Check for double clicked (or dragged) savefile
3711  *
3712  * Apparently, Windows copies the entire filename into the first
3713  * piece of the "command line string".  Perhaps we should extract
3714  * the "basename" of that filename and append it to the "save" dir.
3715  */
3716 static void check_for_save_file(LPSTR cmd_line)
3717 {
3718         char *s;
3719
3720         /* First arg */
3721         s = cmd_line;
3722
3723         /* No args */
3724         if (!*s) return;
3725
3726         /* Extract filename */
3727         strcat(savefile, s);
3728
3729         /* Validate the file */
3730         validate_file(savefile);
3731
3732         /* Game in progress */
3733         game_in_progress = TRUE;
3734
3735         /* Play game */
3736         play_game(FALSE);
3737 }
3738
3739
3740 /*
3741  * Process a menu command
3742  */
3743 static void process_menus(WORD wCmd)
3744 {
3745         int i;
3746
3747         term_data *td;
3748
3749         OPENFILENAME ofn;
3750
3751         /* Analyze */
3752         switch (wCmd)
3753         {
3754                 /* New game */
3755                 case IDM_FILE_NEW:
3756                 {
3757                         if (!initialized)
3758                         {
3759                                 plog(_("まだ初期化中です...", "You cannot do that yet..."));
3760                         }
3761                         else if (game_in_progress)
3762                         {
3763                                 plog(_("プレイ中は新しいゲームを始めることができません!", "You can't start a new game while you're still playing!"));
3764                         }
3765                         else
3766                         {
3767                                 game_in_progress = TRUE;
3768                                 Term_flush();
3769                                 play_game(TRUE);
3770                                 quit(NULL);
3771                         }
3772                         break;
3773                 }
3774
3775                 /* Open game */
3776                 case IDM_FILE_OPEN:
3777                 {
3778                         if (!initialized)
3779                         {
3780                                 plog(_("まだ初期化中です...", "You cannot do that yet..."));
3781                         }
3782                         else if (game_in_progress)
3783                         {
3784                                 plog(_("プレイ中はゲームをロードすることができません!", "You can't open a new game while you're still playing!"));
3785                         }
3786                         else
3787                         {
3788                                 memset(&ofn, 0, sizeof(ofn));
3789                                 ofn.lStructSize = sizeof(ofn);
3790                                 ofn.hwndOwner = data[0].w;
3791                                 ofn.lpstrFilter = "Save Files (*.)\0*\0";
3792                                 ofn.nFilterIndex = 1;
3793                                 ofn.lpstrFile = savefile;
3794                                 ofn.nMaxFile = 1024;
3795                                 ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
3796                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
3797
3798                                 if (GetOpenFileName(&ofn))
3799                                 {
3800                                         /* Load 'savefile' */
3801                                         validate_file(savefile);
3802                                         game_in_progress = TRUE;
3803                                         Term_flush();
3804                                         play_game(FALSE);
3805                                         quit(NULL);
3806                                 }
3807                         }
3808                         break;
3809                 }
3810
3811                 /* Save game */
3812                 case IDM_FILE_SAVE:
3813                 {
3814                         if (game_in_progress && character_generated)
3815                         {
3816                                 if (!can_save)
3817                                 {
3818                                         plog(_("今はセーブすることは出来ません。", "You may not do that right now."));
3819                                         break;
3820                                 }
3821
3822                                 /* Hack -- Forget messages */
3823                                 msg_flag = FALSE;
3824
3825                                 /* Save the game */
3826 #ifdef ZANGBAND
3827                                 do_cmd_save_game(FALSE);
3828 #else /* ZANGBAND */
3829                                 do_cmd_save_game();
3830 #endif /* ZANGBAND */
3831                         }
3832                         else
3833                         {
3834                                 plog(_("今、セーブすることは出来ません。", "You may not do that right now."));
3835                         }
3836                         break;
3837                 }
3838
3839                 /* Exit */
3840                 case IDM_FILE_EXIT:
3841                 {
3842                         if (game_in_progress && character_generated)
3843                         {
3844                                 if (!can_save)
3845                                 {
3846                                         plog(_("今は終了できません。", "You may not do that right now."));
3847                                         break;
3848                                 }
3849
3850                                 /* Hack -- Forget messages */
3851                                 msg_flag = FALSE;
3852
3853                                 forget_lite();
3854                                 forget_view();
3855                                 clear_mon_lite();
3856
3857                                 /* Save the game */
3858 #ifdef ZANGBAND
3859                                 /* do_cmd_save_game(FALSE); */
3860 #else /* ZANGBAND */
3861                                 /* do_cmd_save_game(); */
3862 #endif /* ZANGBAND */
3863                                 Term_key_push(SPECIAL_KEY_QUIT);
3864                                 break;
3865                         }
3866                         quit(NULL);
3867                         break;
3868                 }
3869
3870                 /* Show scores */
3871                 case IDM_FILE_SCORE:
3872                 {
3873                         char buf[1024];
3874
3875                         /* Build the filename */
3876                         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
3877
3878                         /* Open the binary high score file, for reading */
3879                         highscore_fd = fd_open(buf, O_RDONLY);
3880
3881                         /* Paranoia -- No score file */
3882                         if (highscore_fd < 0)
3883                         {
3884                                 msg_print("Score file unavailable.");
3885                         }
3886                         else
3887                         {
3888                                 screen_save();
3889                                 Term_clear();
3890
3891                                 /* Display the scores */
3892                                 display_scores_aux(0, MAX_HISCORES, -1, NULL);
3893
3894                                 /* Shut the high score file */
3895                                 (void)fd_close(highscore_fd);
3896
3897                                 /* Forget the high score fd */
3898                                 highscore_fd = -1;
3899                                 screen_load();
3900
3901                                 /* Hack - Flush it */
3902                                 Term_fresh();
3903                         }
3904
3905                         break;
3906                 }
3907
3908                 /* Open game */
3909                 case IDM_FILE_MOVIE:
3910                 {
3911                         if (!initialized)
3912                         {
3913                                 plog(_("まだ初期化中です...", "You cannot do that yet..."));
3914                         }
3915                         else if (game_in_progress)
3916                         {
3917                                 plog(_("プレイ中はムービーをロードすることができません!", "You can't open a movie while you're playing!"));
3918                         }
3919                         else
3920                         {
3921                                 memset(&ofn, 0, sizeof(ofn));
3922                                 ofn.lStructSize = sizeof(ofn);
3923                                 ofn.hwndOwner = data[0].w;
3924                                 ofn.lpstrFilter = "Angband Movie Files (*.amv)\0*.amv\0";
3925                                 ofn.nFilterIndex = 1;
3926                                 ofn.lpstrFile = savefile;
3927                                 ofn.nMaxFile = 1024;
3928                                 ofn.lpstrInitialDir = ANGBAND_DIR_USER;
3929                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
3930
3931                                 if (GetOpenFileName(&ofn))
3932                                 {
3933                                         /* Load 'savefile' */
3934                                         prepare_browse_movie_aux(savefile);
3935                                         play_game(FALSE);
3936                                         quit(NULL);
3937                                         return;
3938                                 }
3939                         }
3940                         break;
3941                 }
3942
3943
3944                 case IDM_WINDOW_VIS_0:
3945                 {
3946                         plog(_("メインウィンドウは非表示にできません!", "You are not allowed to do that!"));
3947                         break;
3948                 }
3949
3950                 /* Window visibility */
3951                 case IDM_WINDOW_VIS_1:
3952                 case IDM_WINDOW_VIS_2:
3953                 case IDM_WINDOW_VIS_3:
3954                 case IDM_WINDOW_VIS_4:
3955                 case IDM_WINDOW_VIS_5:
3956                 case IDM_WINDOW_VIS_6:
3957                 case IDM_WINDOW_VIS_7:
3958                 {
3959                         i = wCmd - IDM_WINDOW_VIS_0;
3960
3961                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3962
3963                         td = &data[i];
3964
3965                         if (!td->visible)
3966                         {
3967                                 td->visible = TRUE;
3968                                 ShowWindow(td->w, SW_SHOW);
3969                                 term_data_redraw(td);
3970                         }
3971                         else
3972                         {
3973                                 td->visible = FALSE;
3974                                 td->posfix = FALSE;
3975                                 ShowWindow(td->w, SW_HIDE);
3976                         }
3977
3978                         break;
3979                 }
3980
3981                 /* Window fonts */
3982                 case IDM_WINDOW_FONT_0:
3983                 case IDM_WINDOW_FONT_1:
3984                 case IDM_WINDOW_FONT_2:
3985                 case IDM_WINDOW_FONT_3:
3986                 case IDM_WINDOW_FONT_4:
3987                 case IDM_WINDOW_FONT_5:
3988                 case IDM_WINDOW_FONT_6:
3989                 case IDM_WINDOW_FONT_7:
3990                 {
3991                         i = wCmd - IDM_WINDOW_FONT_0;
3992
3993                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3994
3995                         td = &data[i];
3996
3997                         term_change_font(td);
3998
3999                         break;
4000                 }
4001
4002                 /* Window Z Position */
4003                 case IDM_WINDOW_POS_1:
4004                 case IDM_WINDOW_POS_2:
4005                 case IDM_WINDOW_POS_3:
4006                 case IDM_WINDOW_POS_4:
4007                 case IDM_WINDOW_POS_5:
4008                 case IDM_WINDOW_POS_6:
4009                 case IDM_WINDOW_POS_7:
4010                 {
4011                         i = wCmd - IDM_WINDOW_POS_0;
4012
4013                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4014
4015                         td = &data[i];
4016
4017                         if (!td->posfix && td->visible)
4018                         {
4019                                 td->posfix = TRUE;
4020                                 term_window_pos(td, HWND_TOPMOST);
4021                         }
4022                         else
4023                         {
4024                                 td->posfix = FALSE;
4025                                 term_window_pos(td, data[0].w);
4026                         }
4027
4028                         break;
4029                 }
4030
4031                 /* Bizarre Display */
4032                 case IDM_WINDOW_BIZ_0:
4033                 case IDM_WINDOW_BIZ_1:
4034                 case IDM_WINDOW_BIZ_2:
4035                 case IDM_WINDOW_BIZ_3:
4036                 case IDM_WINDOW_BIZ_4:
4037                 case IDM_WINDOW_BIZ_5:
4038                 case IDM_WINDOW_BIZ_6:
4039                 case IDM_WINDOW_BIZ_7:
4040                 {
4041                         i = wCmd - IDM_WINDOW_BIZ_0;
4042
4043                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4044
4045                         td = &data[i];
4046
4047                         td->bizarre = !td->bizarre;
4048
4049                         term_getsize(td);
4050
4051                         term_window_resize(td);
4052
4053                         break;
4054                 }
4055
4056                 /* Increase Tile Width */
4057                 case IDM_WINDOW_I_WID_0:
4058                 case IDM_WINDOW_I_WID_1:
4059                 case IDM_WINDOW_I_WID_2:
4060                 case IDM_WINDOW_I_WID_3:
4061                 case IDM_WINDOW_I_WID_4:
4062                 case IDM_WINDOW_I_WID_5:
4063                 case IDM_WINDOW_I_WID_6:
4064                 case IDM_WINDOW_I_WID_7:
4065                 {
4066                         i = wCmd - IDM_WINDOW_I_WID_0;
4067
4068                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4069
4070                         td = &data[i];
4071
4072                         td->tile_wid += 1;
4073
4074                         term_getsize(td);
4075
4076                         term_window_resize(td);
4077
4078                         break;
4079                 }
4080
4081                 /* Decrease Tile Height */
4082                 case IDM_WINDOW_D_WID_0:
4083                 case IDM_WINDOW_D_WID_1:
4084                 case IDM_WINDOW_D_WID_2:
4085                 case IDM_WINDOW_D_WID_3:
4086                 case IDM_WINDOW_D_WID_4:
4087                 case IDM_WINDOW_D_WID_5:
4088                 case IDM_WINDOW_D_WID_6:
4089                 case IDM_WINDOW_D_WID_7:
4090                 {
4091                         i = wCmd - IDM_WINDOW_D_WID_0;
4092
4093                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4094
4095                         td = &data[i];
4096
4097                         td->tile_wid -= 1;
4098
4099                         term_getsize(td);
4100
4101                         term_window_resize(td);
4102
4103                         break;
4104                 }
4105
4106                 /* Increase Tile Height */
4107                 case IDM_WINDOW_I_HGT_0:
4108                 case IDM_WINDOW_I_HGT_1:
4109                 case IDM_WINDOW_I_HGT_2:
4110                 case IDM_WINDOW_I_HGT_3:
4111                 case IDM_WINDOW_I_HGT_4:
4112                 case IDM_WINDOW_I_HGT_5:
4113                 case IDM_WINDOW_I_HGT_6:
4114                 case IDM_WINDOW_I_HGT_7:
4115                 {
4116                         i = wCmd - IDM_WINDOW_I_HGT_0;
4117
4118                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4119
4120                         td = &data[i];
4121
4122                         td->tile_hgt += 1;
4123
4124                         term_getsize(td);
4125
4126                         term_window_resize(td);
4127
4128                         break;
4129                 }
4130
4131                 /* Decrease Tile Height */
4132                 case IDM_WINDOW_D_HGT_0:
4133                 case IDM_WINDOW_D_HGT_1:
4134                 case IDM_WINDOW_D_HGT_2:
4135                 case IDM_WINDOW_D_HGT_3:
4136                 case IDM_WINDOW_D_HGT_4:
4137                 case IDM_WINDOW_D_HGT_5:
4138                 case IDM_WINDOW_D_HGT_6:
4139                 case IDM_WINDOW_D_HGT_7:
4140                 {
4141                         i = wCmd - IDM_WINDOW_D_HGT_0;
4142
4143                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4144
4145                         td = &data[i];
4146
4147                         td->tile_hgt -= 1;
4148
4149                         term_getsize(td);
4150
4151                         term_window_resize(td);
4152
4153                         break;
4154                 }
4155
4156                 case IDM_OPTIONS_NO_GRAPHICS:
4157                 {
4158                         if (!inkey_flag)
4159                         {
4160                                 plog("You may not do that right now.");
4161                                 break;
4162                         }
4163
4164                         /* Toggle "arg_graphics" */
4165                         if (arg_graphics != GRAPHICS_NONE)
4166                         {
4167                                 arg_graphics = GRAPHICS_NONE;
4168
4169                                 /* React to changes */
4170                                 Term_xtra_win_react();
4171
4172                                 /* Hack -- Force redraw */
4173                                 Term_key_push(KTRL('R'));
4174                         }
4175
4176                         break;
4177                 }
4178
4179                 case IDM_OPTIONS_OLD_GRAPHICS:
4180                 {
4181                         if (!inkey_flag)
4182                         {
4183                                 plog("You may not do that right now.");
4184                                 break;
4185                         }
4186
4187                         /* Toggle "arg_graphics" */
4188                         if (arg_graphics != GRAPHICS_ORIGINAL)
4189                         {
4190                                 arg_graphics = GRAPHICS_ORIGINAL;
4191
4192                                 /* React to changes */
4193                                 Term_xtra_win_react();
4194
4195                                 /* Hack -- Force redraw */
4196                                 Term_key_push(KTRL('R'));
4197                         }
4198
4199                         break;
4200                 }
4201
4202                 case IDM_OPTIONS_NEW_GRAPHICS:
4203                 {
4204                         if (!inkey_flag)
4205                         {
4206                                 plog("You may not do that right now.");
4207                                 break;
4208                         }
4209
4210                         /* Toggle "arg_graphics" */
4211                         if (arg_graphics != GRAPHICS_ADAM_BOLT)
4212                         {
4213                                 arg_graphics = GRAPHICS_ADAM_BOLT;
4214
4215                                 /* React to changes */
4216                                 Term_xtra_win_react();
4217
4218                                 /* Hack -- Force redraw */
4219                                 Term_key_push(KTRL('R'));
4220                         }
4221
4222                         break;
4223                 }
4224
4225                 case IDM_OPTIONS_NEW2_GRAPHICS:
4226                 {
4227                         if (!inkey_flag)
4228                         {
4229                                 plog("You may not do that right now.");
4230                                 break;
4231                         }
4232
4233                         /* Toggle "arg_graphics" */
4234                         if (arg_graphics != GRAPHICS_HENGBAND)
4235                         {
4236                                 arg_graphics = GRAPHICS_HENGBAND;
4237
4238                                 /* React to changes */
4239                                 Term_xtra_win_react();
4240
4241                                 /* Hack -- Force redraw */
4242                                 Term_key_push(KTRL('R'));
4243                         }
4244
4245                         break;
4246                 }
4247
4248                 case IDM_OPTIONS_BIGTILE:
4249                 {
4250                         td = &data[0];
4251                         if (!inkey_flag)
4252                         {
4253                                 plog("You may not do that right now.");
4254                                 break;
4255                         }
4256
4257                         /* Toggle "arg_sound" */
4258                         arg_bigtile = !arg_bigtile;
4259
4260                         /* Activate */
4261                         Term_activate(&td->t);
4262
4263                         /* Resize the term */
4264                         Term_resize(td->cols, td->rows);
4265
4266                         /* Redraw later */
4267                         InvalidateRect(td->w, NULL, TRUE);
4268
4269                         break;
4270                 }
4271
4272                 case IDM_OPTIONS_MUSIC:
4273                 {
4274                         if (!inkey_flag)
4275                         {
4276                                 plog("You may not do that right now.");
4277                                 break;
4278                         }
4279
4280                         /* Toggle "arg_sound" */
4281                         arg_music = !arg_music;
4282
4283                         /* React to changes */
4284                         Term_xtra_win_react();
4285
4286                         /* Hack -- Force redraw */
4287                         Term_key_push(KTRL('R'));
4288
4289                         break;
4290                 }
4291
4292                 case IDM_OPTIONS_SOUND:
4293                 {
4294                         if (!inkey_flag)
4295                         {
4296                                 plog("You may not do that right now.");
4297                                 break;
4298                         }
4299
4300                         /* Toggle "arg_sound" */
4301                         arg_sound = !arg_sound;
4302
4303                         /* React to changes */
4304                         Term_xtra_win_react();
4305
4306                         /* Hack -- Force redraw */
4307                         Term_key_push(KTRL('R'));
4308
4309                         break;
4310                 }
4311
4312                 /* bg */
4313                 case IDM_OPTIONS_BG:
4314                 {
4315                         if (!inkey_flag)
4316                         {
4317                                 plog("You may not do that right now.");
4318                                 break;
4319                         }
4320
4321                         /* Toggle "use_bg" */
4322                         use_bg = !use_bg;
4323
4324                         init_bg();
4325
4326                         /* React to changes */
4327                         Term_xtra_win_react();
4328
4329                         /* Hack -- Force redraw */
4330                         Term_key_push(KTRL('R'));
4331
4332                         break;
4333                 }
4334
4335                 /* bg */
4336                 case IDM_OPTIONS_OPEN_BG:
4337                 {
4338                         if (!inkey_flag)
4339                         {
4340                                 plog("You may not do that right now.");
4341                                 break;
4342                         }
4343                         else
4344                         {
4345                                 memset(&ofn, 0, sizeof(ofn));
4346                                 ofn.lStructSize = sizeof(ofn);
4347                                 ofn.hwndOwner = data[0].w;
4348                                 ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0";
4349                                 ofn.nFilterIndex = 1;
4350                                 ofn.lpstrFile = bg_bitmap_file;
4351                                 ofn.nMaxFile = 1023;
4352                                 ofn.lpstrInitialDir = NULL;
4353                                 ofn.lpstrTitle = _("壁紙を選んでね。", "Choose wall paper.");
4354                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
4355
4356                                 if (GetOpenFileName(&ofn))
4357                                 {
4358                                         /* Load 'savefile' */
4359                                         use_bg = 1;
4360                                         init_bg();
4361                                 }
4362
4363                                 /* React to changes */
4364                                 Term_xtra_win_react();
4365
4366                                 /* Hack -- Force redraw */
4367                                 Term_key_push(KTRL('R'));
4368                         }
4369                         break;
4370                 }
4371
4372                 case IDM_DUMP_SCREEN_HTML:
4373                 {
4374                         static char buf[1024] = "";
4375                         memset(&ofn, 0, sizeof(ofn));
4376                         ofn.lStructSize = sizeof(ofn);
4377                         ofn.hwndOwner = data[0].w;
4378                         ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0";
4379                         ofn.nFilterIndex = 1;
4380                         ofn.lpstrFile = buf;
4381                         ofn.nMaxFile = 1023;
4382                         ofn.lpstrDefExt = "html";
4383                         ofn.lpstrInitialDir = NULL;
4384                         ofn.lpstrTitle = _("HTMLでスクリーンダンプを保存", "Save screen dump as HTML.");
4385                         ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
4386
4387                         if (GetSaveFileName(&ofn))
4388                         {
4389                                 do_cmd_save_screen_html_aux(buf, 0);
4390                         }
4391                         break;
4392                 }
4393
4394 #ifdef USE_SAVER
4395
4396                 case IDM_OPTIONS_SAVER:
4397                 {
4398                         if (hwndSaver)
4399                         {
4400                                 DestroyWindow(hwndSaver);
4401                                 hwndSaver = NULL;
4402                         }
4403                         else
4404                         {
4405                                 /* Create a screen scaver window */
4406                                 hwndSaver = CreateWindowEx(WS_EX_TOPMOST, "WindowsScreenSaverClass",
4407                                                            "Angband Screensaver",
4408                                                            WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4409                                                            0, 0, GetSystemMetrics(SM_CXSCREEN),
4410                                                            GetSystemMetrics(SM_CYSCREEN),
4411                                                            NULL, NULL, hInstance, NULL);
4412
4413                                 if (hwndSaver)
4414                                 {
4415                                         /* Push the window to the bottom */
4416                                         SetWindowPos(hwndSaver, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
4417                                 }
4418                                 else
4419                                 {
4420                                         plog(_("ウィンドウを作成出来ません", "Failed to create saver window"));
4421                                 }
4422                         }
4423                         break;
4424                 }
4425
4426 #endif
4427
4428                 case IDM_OPTIONS_MAP:
4429                 {
4430                         windows_map();
4431                         break;
4432                 }
4433
4434                 case IDM_HELP_CONTENTS:
4435                 {
4436 #ifdef HTML_HELP
4437                         char tmp[1024];
4438                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.chm");
4439                         if (check_file(tmp))
4440                         {
4441                                 HtmlHelp(data[0].w, tmp, HH_DISPLAY_TOPIC, 0);
4442                         }
4443                         else
4444                         {
4445                                 plog_fmt(_("ヘルプファイル[%s]が見付かりません。", "Cannot find help file: %s"), tmp);
4446                                 plog(_("代わりにオンラインヘルプを使用してください。", "Use the online help files instead."));
4447                         }
4448                         break;
4449 #else /* HTML_HELP */
4450                         char buf[1024];
4451                         char tmp[1024];
4452                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.hlp");
4453                         if (check_file(tmp))
4454                         {
4455                                 sprintf(buf, "winhelp.exe %s", tmp);
4456                                 WinExec(buf, SW_NORMAL);
4457                         }
4458                         else
4459                         {
4460                                 plog_fmt(_("ヘルプファイル[%s]が見付かりません。", "Cannot find help file: %s"), tmp);
4461                                 plog(_("代わりにオンラインヘルプを使用してください。", "Use the online help files instead."));
4462
4463                         }
4464                         break;
4465 #endif /* HTML_HELP */
4466                 }
4467         }
4468 }
4469
4470
4471 static bool process_keydown(WPARAM wParam, LPARAM lParam)
4472 {
4473         int i;
4474         bool mc = FALSE;
4475         bool ms = FALSE;
4476         bool ma = FALSE;
4477
4478         /* Extract the modifiers */
4479         if (GetKeyState(VK_CONTROL) & 0x8000) mc = TRUE;
4480         if (GetKeyState(VK_SHIFT)   & 0x8000) ms = TRUE;
4481         if (GetKeyState(VK_MENU)    & 0x8000) ma = TRUE;
4482
4483         Term_no_press = (ma) ? TRUE : FALSE;
4484
4485         /* Handle "special" keys */
4486         if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)]) )
4487         {
4488                 bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
4489                 bool numpad = FALSE;
4490
4491                 /* Begin the macro trigger */
4492                 Term_keypress(31);
4493
4494                 /* Send the modifiers */
4495                 if (mc) Term_keypress('C');
4496                 if (ms) Term_keypress('S');
4497                 if (ma) Term_keypress('A');
4498
4499                 /* Extract "scan code" */
4500                 i = LOBYTE(HIWORD(lParam));
4501
4502                 /* Introduce the scan code */
4503                 Term_keypress('x');
4504
4505                 /* Extended key bit */
4506                 switch (wParam)
4507                 {
4508                         /* Numpad Enter and '/' are extended key */
4509                 case VK_DIVIDE:
4510                         Term_no_press = TRUE;
4511                 case VK_RETURN: /* Enter */
4512                         numpad = ext_key;
4513                         break;
4514                         /* Other extended keys are on full keyboard */
4515                 case VK_NUMPAD0:
4516                 case VK_NUMPAD1:
4517                 case VK_NUMPAD2:
4518                 case VK_NUMPAD3:
4519                 case VK_NUMPAD4:
4520                 case VK_NUMPAD5:
4521                 case VK_NUMPAD6:
4522                 case VK_NUMPAD7:
4523                 case VK_NUMPAD8:
4524                 case VK_NUMPAD9:
4525                 case VK_ADD:
4526                 case VK_MULTIPLY:
4527                 case VK_SUBTRACT:
4528                 case VK_SEPARATOR:
4529                 case VK_DECIMAL:
4530                         Term_no_press = TRUE;
4531                 case VK_CLEAR:
4532                 case VK_HOME:
4533                 case VK_END:
4534                 case VK_PRIOR:  /* Page Up */
4535                 case VK_NEXT:   /* Page Down */
4536                 case VK_INSERT:
4537                 case VK_DELETE:
4538                 case VK_UP:
4539                 case VK_DOWN:
4540                 case VK_LEFT:
4541                 case VK_RIGHT:
4542                         numpad = !ext_key;
4543                 }
4544
4545                 /* Special modifiers for keypad keys */
4546                 if (numpad) Term_keypress('K');
4547
4548                 /* Encode the hexidecimal scan code */
4549                 Term_keypress(hexsym[i/16]);
4550                 Term_keypress(hexsym[i%16]);
4551
4552                 /* End the macro trigger */
4553                 Term_keypress(13);
4554
4555                 return 1;
4556         }
4557
4558         return 0;
4559 }
4560
4561
4562 #ifdef __MWERKS__
4563 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4564                                   WPARAM wParam, LPARAM lParam);
4565 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4566                                   WPARAM wParam, LPARAM lParam)
4567 #else /* __MWERKS__ */
4568 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
4569 #endif /* __MWERKS__ */
4570 {
4571         PAINTSTRUCT ps;
4572         HDC hdc;
4573         term_data *td;
4574 #if 0
4575         MINMAXINFO FAR *lpmmi;
4576         RECT rc;
4577 #endif
4578         int i;
4579
4580
4581         /* Acquire proper "term_data" info */
4582         td = (term_data *)GetWindowLong(hWnd, 0);
4583
4584         /* Handle message */
4585         switch (uMsg)
4586         {
4587                 case WM_NCCREATE:
4588                 {
4589                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4590                         break;
4591                 }
4592
4593                 case WM_CREATE:
4594                 {
4595 #ifdef USE_MUSIC
4596                         mop.dwCallback=(DWORD)hWnd;
4597 #endif
4598                         return 0;
4599                 }
4600
4601                 case WM_GETMINMAXINFO:
4602                 {
4603                         MINMAXINFO FAR *lpmmi;
4604                         RECT rc;
4605
4606                         lpmmi = (MINMAXINFO FAR *)lParam;
4607
4608                         /* this message was sent before WM_NCCREATE */
4609                         if (!td) return 1;
4610
4611                         /* Minimum window size is 80x24 */
4612                         rc.left = rc.top = 0;
4613                         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
4614                         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4615
4616                         /* Adjust */
4617                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4618
4619                         /* Save minimum size */
4620                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4621                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4622
4623                         return 0;
4624                 }
4625
4626                 case WM_PAINT:
4627                 {
4628                         BeginPaint(hWnd, &ps);
4629                         if (td) term_data_redraw(td);
4630                         EndPaint(hWnd, &ps);
4631                         ValidateRect(hWnd, NULL);
4632                         return 0;
4633                 }
4634
4635 #ifdef USE_MUSIC
4636                 case MM_MCINOTIFY:
4637                 {
4638                         if(wParam == MCI_NOTIFY_SUCCESSFUL)
4639                         {
4640                                 mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
4641                                 mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
4642                         }
4643                         return 0;
4644                 }
4645 #endif
4646
4647                 case WM_SYSKEYDOWN:
4648                 case WM_KEYDOWN:
4649                 {
4650                         if (process_keydown(wParam, lParam))
4651                                 return 0;
4652                         break;
4653                 }
4654
4655                 case WM_CHAR:
4656                 {
4657                         if (Term_no_press) Term_no_press = FALSE;
4658                         else Term_keypress(wParam);
4659                         return 0;
4660                 }
4661
4662                 case WM_LBUTTONDOWN:
4663                 {
4664                         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4665                         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4666                         mouse_down = TRUE;
4667                         oldx = mousex;
4668                         oldy = mousey;
4669                         return 0;
4670                 }
4671
4672                 case WM_LBUTTONUP:
4673                 {
4674                         HGLOBAL hGlobal;
4675                         LPSTR lpStr;
4676                         int j, sz;
4677                         TERM_LEN dx = abs(oldx - mousex) + 1;
4678                         TERM_LEN dy = abs(oldy - mousey) + 1;
4679                         TERM_LEN ox = (oldx > mousex) ? mousex : oldx;
4680                         TERM_LEN oy = (oldy > mousey) ? mousey : oldy;
4681
4682                         mouse_down = FALSE;
4683                         paint_rect = FALSE;
4684
4685 #ifdef JP
4686                         sz = (dx + 3) * dy;
4687 #else
4688                         sz = (dx + 2) * dy;
4689 #endif
4690                         hGlobal = GlobalAlloc(GHND, sz + 1);
4691                         if (hGlobal == NULL) return 0;
4692                         lpStr = (LPSTR)GlobalLock(hGlobal);
4693
4694                         for (i = 0; i < dy; i++)
4695                         {
4696 #ifdef JP
4697                                 char *s;
4698                                 char **scr = data[0].t.scr->c;
4699
4700                                 C_MAKE(s, (dx + 1), char);
4701                                 strncpy(s, &scr[oy + i][ox], dx);
4702
4703                                 if (ox > 0)
4704                                 {
4705                                         if (iskanji(scr[oy + i][ox - 1])) s[0] = ' ';
4706                                 }
4707
4708                                 if (ox + dx < data[0].cols)
4709                                 {
4710                                         if (iskanji(scr[oy + i][ox + dx - 1])) s[dx - 1] = ' ';
4711                                 }
4712
4713                                 for (j = 0; j < dx; j++)
4714                                 {
4715                                         if (s[j] == 127) s[j] = '#';
4716                                         *lpStr++ = s[j];
4717                                 }
4718 #else
4719                                 for (j = 0; j < dx; j++)
4720                                 {
4721                                         *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
4722                                 }
4723 #endif
4724                                 if (dy > 1)
4725                                 {
4726                                         *lpStr++ = '\r';
4727                                         *lpStr++ = '\n';
4728                                 }
4729                         }
4730
4731                         GlobalUnlock(hGlobal);
4732                         if (OpenClipboard(hWnd) == 0)
4733                         {
4734                                 GlobalFree(hGlobal);
4735                                 return 0;
4736                         }
4737                         EmptyClipboard();
4738                         SetClipboardData(CF_TEXT, hGlobal);
4739                         CloseClipboard();
4740
4741                         Term_redraw();
4742
4743                         return 0;
4744                 }
4745
4746                 case WM_MOUSEMOVE:
4747                 {
4748                         if (mouse_down)
4749                         {
4750                                 int dx, dy;
4751                                 int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4752                                 int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4753                                 int ox, oy;
4754
4755                                 if (paint_rect)
4756                                 {
4757                                         dx = abs(oldx - mousex) + 1;
4758                                         dy = abs(oldy - mousey) + 1;
4759                                         ox = (oldx > mousex) ? mousex : oldx;
4760                                         oy = (oldy > mousey) ? mousey : oldy;
4761                                         Term_inversed_area(hWnd, ox, oy, dx, dy);
4762                                 }
4763                                 else
4764                                 {
4765                                         paint_rect = TRUE;
4766                                 }
4767
4768                                 dx = abs(cx - mousex) + 1;
4769                                 dy = abs(cy - mousey) + 1;
4770                                 ox = (cx > mousex) ? mousex : cx;
4771                                 oy = (cy > mousey) ? mousey : cy;
4772                                 Term_inversed_area(hWnd, ox, oy, dx, dy);
4773
4774                                 oldx = cx;
4775                                 oldy = cy;
4776                         }
4777                         return 0;
4778                 }
4779
4780                 case WM_INITMENU:
4781                 {
4782                         setup_menus();
4783                         return 0;
4784                 }
4785
4786                 case WM_CLOSE:
4787                 {
4788                         if (game_in_progress && character_generated)
4789                         {
4790                                 if (!can_save)
4791                                 {
4792                                         plog(_("今は終了できません。", "You may not do that right now."));
4793                                         return 0;
4794                                 }
4795
4796                                 /* Hack -- Forget messages */
4797                                 msg_flag = FALSE;
4798
4799                                 forget_lite();
4800                                 forget_view();
4801                                 clear_mon_lite();
4802
4803                                 /* Save the game */
4804 #ifdef ZANGBAND
4805                                 /* do_cmd_save_game(FALSE); */
4806 #else /* ZANGBAND */
4807                                 /* do_cmd_save_game(); */
4808 #endif /* ZANGBAND */
4809                                 Term_key_push(SPECIAL_KEY_QUIT);
4810                                 return 0;
4811                         }
4812                         quit(NULL);
4813                         return 0;
4814                 }
4815
4816                 case WM_QUERYENDSESSION:
4817                 {
4818                         if (game_in_progress && character_generated)
4819                         {
4820                                 /* Hack -- Forget messages */
4821                                 msg_flag = FALSE;
4822
4823                                 /* Mega-Hack -- Delay death */
4824                                 if (p_ptr->chp < 0) p_ptr->is_dead = FALSE;
4825                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, _("----ゲーム中断----", "---- Save and Exit Game ----"));
4826
4827                                 /* Hardcode panic save */
4828                                 p_ptr->panic_save = 1;
4829
4830                                 /* Forbid suspend */
4831                                 signals_ignore_tstp();
4832
4833                                 /* Indicate panic save */
4834                                 (void)strcpy(p_ptr->died_from, _("(緊急セーブ)", "(panic save)"));
4835
4836                                 /* Panic save */
4837                                 (void)save_player();
4838                         }
4839                         quit(NULL);
4840                         return 0;
4841                 }
4842
4843                 case WM_QUIT:
4844                 {
4845                         quit(NULL);
4846                         return 0;
4847                 }
4848
4849                 case WM_COMMAND:
4850                 {
4851                         process_menus(LOWORD(wParam));
4852                         return 0;
4853                 }
4854
4855                 case WM_SIZE:
4856                 {
4857                         /* this message was sent before WM_NCCREATE */
4858                         if (!td) return 1;
4859
4860                         /* it was sent from inside CreateWindowEx */
4861                         if (!td->w) return 1;
4862
4863                         /* was sent from WM_SIZE */
4864                         if (td->size_hack) return 1;
4865
4866                         switch (wParam)
4867                         {
4868                                 case SIZE_MINIMIZED:
4869                                 {
4870                                         /* Hide sub-windows */
4871                                         for (i = 1; i < MAX_TERM_DATA; i++)
4872                                         {
4873                                                 if (data[i].visible) ShowWindow(data[i].w, SW_HIDE);
4874                                         }
4875                                         return 0;
4876                                 }
4877
4878                                 case SIZE_MAXIMIZED:
4879                                 {
4880                                         /* fall through */
4881                                 }
4882
4883                                 case SIZE_RESTORED:
4884                                 {
4885                                         TERM_LEN cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4886                                         TERM_LEN rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4887
4888                                         /* New size */
4889                                         if ((td->cols != cols) || (td->rows != rows))
4890                                         {
4891                                                 /* Save the new size */
4892                                                 td->cols = cols;
4893                                                 td->rows = rows;
4894
4895                                                 if (!IsZoomed(td->w) && !IsIconic(td->w))
4896                                                 {
4897                                                         normsize.x = td->cols;
4898                                                         normsize.y = td->rows;
4899                                                 }
4900
4901                                                 /* Activate */
4902                                                 Term_activate(&td->t);
4903
4904                                                 /* Resize the term */
4905                                                 Term_resize(td->cols, td->rows);
4906
4907                                                 /* Redraw later */
4908                                                 InvalidateRect(td->w, NULL, TRUE);
4909                                         }
4910
4911                                         td->size_hack = TRUE;
4912
4913                                         /* Show sub-windows */
4914                                         for (i = 1; i < MAX_TERM_DATA; i++)
4915                                         {
4916                                                 if (data[i].visible) ShowWindow(data[i].w, SW_SHOW);
4917                                         }
4918
4919                                         td->size_hack = FALSE;
4920
4921                                         return 0;
4922                                 }
4923                         }
4924                         break;
4925                 }
4926
4927                 case WM_PALETTECHANGED:
4928                 {
4929                         /* Ignore if palette change caused by itself */
4930                         if ((HWND)wParam == hWnd) return 0;
4931
4932                         /* Fall through... */
4933                 }
4934
4935                 case WM_QUERYNEWPALETTE:
4936                 {
4937                         if (!paletted) return 0;
4938
4939                         hdc = GetDC(hWnd);
4940
4941                         SelectPalette(hdc, hPal, FALSE);
4942
4943                         i = RealizePalette(hdc);
4944
4945                         /* if any palette entries changed, repaint the window. */
4946                         if (i) InvalidateRect(hWnd, NULL, TRUE);
4947
4948                         ReleaseDC(hWnd, hdc);
4949
4950                         return 0;
4951                 }
4952
4953                 case WM_ACTIVATE:
4954                 {
4955                         if (wParam && !HIWORD(lParam))
4956                         {
4957                                 /* Do something to sub-windows */
4958                                 for (i = 1; i < MAX_TERM_DATA; i++)
4959                                 {
4960                                         if (!data[i].posfix) term_window_pos(&data[i], hWnd);
4961                                 }
4962
4963                                 /* Focus on main window */
4964                                 SetFocus(hWnd);
4965
4966                                 return 0;
4967                         }
4968
4969                         break;
4970                 }
4971
4972                 case WM_ACTIVATEAPP:
4973                 {
4974                         if (IsIconic(td->w)) break;
4975
4976                         for (i = 1; i < MAX_TERM_DATA; i++)
4977                         {
4978                                 if(data[i].visible)
4979                                 {
4980                                         if (wParam == TRUE)
4981                                         {
4982                                                 ShowWindow(data[i].w, SW_SHOW);
4983                                         }
4984                                         else
4985                                         {
4986                                                 ShowWindow(data[i].w, SW_HIDE);
4987                                         }
4988                                 }
4989                         }
4990                 }
4991         }
4992
4993         return DefWindowProc(hWnd, uMsg, wParam, lParam);
4994 }
4995
4996
4997 #ifdef __MWERKS__
4998 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4999                                            WPARAM wParam, LPARAM lParam);
5000 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5001                                            WPARAM wParam, LPARAM lParam)
5002 #else /* __MWERKS__ */
5003 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5004                                            WPARAM wParam, LPARAM lParam)
5005 #endif /* __MWERKS__ */
5006 {
5007         term_data *td;
5008 #if 0
5009         MINMAXINFO FAR *lpmmi;
5010         RECT rc;
5011 #endif
5012         PAINTSTRUCT ps;
5013         HDC hdc;
5014         int i;
5015
5016
5017         /* Acquire proper "term_data" info */
5018         td = (term_data *)GetWindowLong(hWnd, 0);
5019
5020         /* Process message */
5021         switch (uMsg)
5022         {
5023                 case WM_NCCREATE:
5024                 {
5025                         SetWindowLong(hWnd, 0, (LONG)(my_td));
5026                         break;
5027                 }
5028
5029                 case WM_CREATE:
5030                 {
5031                         return 0;
5032                 }
5033
5034                 case WM_GETMINMAXINFO:
5035                 {
5036                         MINMAXINFO FAR *lpmmi;
5037                         RECT rc;
5038
5039                         lpmmi = (MINMAXINFO FAR *)lParam;
5040
5041                         /* this message was sent before WM_NCCREATE */
5042                         if (!td) return 1;
5043
5044                         /* Minimum window size is 80x24 */
5045                         rc.left = rc.top = 0;
5046                         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
5047                         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
5048
5049                         /* Adjust */
5050                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
5051
5052                         /* Save minimum size */
5053                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
5054                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
5055
5056                         return 0;
5057                 }
5058
5059                 case WM_SIZE:
5060                 {
5061                         TERM_LEN cols;
5062                         TERM_LEN rows;
5063                         
5064                         /* this message was sent before WM_NCCREATE */
5065                         if (!td) return 1;
5066
5067                         /* it was sent from inside CreateWindowEx */
5068                         if (!td->w) return 1;
5069
5070                         /* was sent from inside WM_SIZE */
5071                         if (td->size_hack) return 1;
5072
5073                         td->size_hack = TRUE;
5074
5075                         cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
5076                         rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
5077
5078                         /* New size */
5079                         if ((td->cols != cols) || (td->rows != rows))
5080                         {
5081                                 /* Save old term */
5082                                 term *old_term = Term;
5083
5084                                 /* Save the new size */
5085                                 td->cols = cols;
5086                                 td->rows = rows;
5087
5088                                 /* Activate */
5089                                 Term_activate(&td->t);
5090
5091                                 /* Resize the term */
5092                                 Term_resize(td->cols, td->rows);
5093
5094                                 /* Activate */
5095                                 Term_activate(old_term);
5096
5097                                 /* Redraw later */
5098                                 InvalidateRect(td->w, NULL, TRUE);
5099
5100                                 /* HACK - Redraw all windows */
5101                                 p_ptr->window = 0xFFFFFFFF;
5102                                 handle_stuff();
5103                         }
5104
5105                         td->size_hack = FALSE;
5106
5107                         return 0;
5108                 }
5109
5110                 case WM_PAINT:
5111                 {
5112                         BeginPaint(hWnd, &ps);
5113                         if (td) term_data_redraw(td);
5114                         EndPaint(hWnd, &ps);
5115                         return 0;
5116                 }
5117
5118                 case WM_SYSKEYDOWN:
5119                 case WM_KEYDOWN:
5120                 {
5121                         if (process_keydown(wParam, lParam))
5122                                 return 0;
5123                         break;
5124                 }
5125
5126                 case WM_CHAR:
5127                 {
5128                         if (Term_no_press) Term_no_press = FALSE;
5129                         else Term_keypress(wParam);
5130                         return 0;
5131                 }
5132
5133                 case WM_PALETTECHANGED:
5134                 {
5135                         /* ignore if palette change caused by itself */
5136                         if ((HWND)wParam == hWnd) return FALSE;
5137                         /* otherwise, fall through!!! */
5138                 }
5139
5140                 case WM_QUERYNEWPALETTE:
5141                 {
5142                         if (!paletted) return 0;
5143                         hdc = GetDC(hWnd);
5144                         SelectPalette(hdc, hPal, FALSE);
5145                         i = RealizePalette(hdc);
5146                         /* if any palette entries changed, repaint the window. */
5147                         if (i) InvalidateRect(hWnd, NULL, TRUE);
5148                         ReleaseDC(hWnd, hdc);
5149                         return 0;
5150                 }
5151
5152                 case WM_NCLBUTTONDOWN:
5153                 {
5154
5155 #ifdef HTCLOSE
5156                         if (wParam == HTCLOSE) wParam = HTSYSMENU;
5157 #endif /* HTCLOSE */
5158
5159                         if (wParam == HTSYSMENU)
5160                         {
5161                                 if (td->visible)
5162                                 {
5163                                         td->visible = FALSE;
5164                                         ShowWindow(td->w, SW_HIDE);
5165                                 }
5166
5167                                 return 0;
5168                         }
5169
5170                         break;
5171                 }
5172         }
5173
5174         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5175 }
5176
5177
5178 #ifdef USE_SAVER
5179
5180 #define MOUSE_SENS 40
5181
5182 #ifdef __MWERKS__
5183 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5184                                     WPARAM wParam, LPARAM lParam);
5185 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5186                                     WPARAM wParam, LPARAM lParam)
5187 #else /* __MWERKS__ */
5188 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5189                                             WPARAM wParam, LPARAM lParam)
5190 #endif /* __MWERKS__ */
5191 {
5192         static int iMouse = 0;
5193         static WORD xMouse = 0;
5194         static WORD yMouse = 0;
5195
5196         int dx, dy;
5197
5198
5199         /* Process */
5200         switch (uMsg)
5201         {
5202                 case WM_NCCREATE:
5203                 {
5204                         break;
5205                 }
5206
5207                 case WM_SETCURSOR:
5208                 {
5209                         SetCursor(NULL);
5210                         return 0;
5211                 }
5212
5213 #if 0
5214                 case WM_ACTIVATE:
5215                 {
5216                         if (LOWORD(wParam) == WA_INACTIVE) break;
5217
5218                         /* else fall through */
5219                 }
5220 #endif
5221
5222                 case WM_LBUTTONDOWN:
5223                 case WM_MBUTTONDOWN:
5224                 case WM_RBUTTONDOWN:
5225                 case WM_KEYDOWN:
5226                 {
5227                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5228                         return 0;
5229                 }
5230
5231                 case WM_MOUSEMOVE:
5232                 {
5233                         if (iMouse)
5234                         {
5235                                 dx = LOWORD(lParam) - xMouse;
5236                                 dy = HIWORD(lParam) - yMouse;
5237
5238                                 if (dx < 0) dx = -dx;
5239                                 if (dy < 0) dy = -dy;
5240
5241                                 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
5242                                 {
5243                                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5244                                 }
5245                         }
5246
5247                         /* Save last location */
5248                         iMouse = 1;
5249                         xMouse = LOWORD(lParam);
5250                         yMouse = HIWORD(lParam);
5251
5252                         return 0;
5253                 }
5254
5255                 case WM_CLOSE:
5256                 {
5257                         DestroyWindow(hwndSaver);
5258                         hwndSaver = NULL;
5259                         return 0;
5260                 }
5261         }
5262
5263         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5264 }
5265
5266 #endif /* USE_SAVER */
5267
5268
5269
5270
5271
5272 /*** Temporary Hooks ***/
5273
5274
5275 /*
5276  * Display warning message (see "z-util.c")
5277  */
5278 static void hack_plog(concptr str)
5279 {
5280         /* Give a warning */
5281         if (str)
5282         {
5283 #ifdef JP
5284                 MessageBox(NULL, str, "警告!",
5285                            MB_ICONEXCLAMATION | MB_OK);
5286 #else
5287                 MessageBox(NULL, str, "Warning",
5288                            MB_ICONEXCLAMATION | MB_OK);
5289 #endif
5290
5291         }
5292 }
5293
5294
5295 /*
5296  * Display error message and quit (see "z-util.c")
5297  */
5298 static void hack_quit(concptr str)
5299 {
5300         /* Give a warning */
5301         if (str)
5302         {
5303 #ifdef JP
5304                 MessageBox(NULL, str, "エラー!",
5305                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5306 #else
5307                 MessageBox(NULL, str, "Error",
5308                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5309 #endif
5310
5311         }
5312
5313         /* Unregister the classes */
5314         UnregisterClass(AppName, hInstance);
5315
5316         /* Destroy the icon */
5317         if (hIcon) DestroyIcon(hIcon);
5318
5319         /* Exit */
5320         exit(0);
5321 }
5322
5323
5324
5325 /*** Various hooks ***/
5326
5327
5328 /*
5329  * Display warning message (see "z-util.c")
5330  */
5331 static void hook_plog(concptr str)
5332 {
5333         /* Warning */
5334         if (str)
5335         {
5336 #ifdef JP
5337                 MessageBox(data[0].w, str, "警告!",
5338                            MB_ICONEXCLAMATION | MB_OK);
5339 #else
5340                 MessageBox(data[0].w, str, "Warning",
5341                            MB_ICONEXCLAMATION | MB_OK);
5342 #endif
5343
5344         }
5345 }
5346
5347
5348 /*
5349  * Display error message and quit (see "z-util.c")
5350  */
5351 static void hook_quit(concptr str)
5352 {
5353         int i;
5354
5355
5356         /* Give a warning */
5357         if (str)
5358         {
5359 #ifdef JP
5360                 MessageBox(data[0].w, str, "エラー!",
5361                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5362 #else
5363                 MessageBox(data[0].w, str, "Error",
5364                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5365 #endif
5366
5367         }
5368
5369
5370         /* Save the preferences */
5371         save_prefs();
5372
5373
5374         /*** Could use 'Term_nuke_win()' */
5375
5376         /* Destroy all windows */
5377         for (i = MAX_TERM_DATA - 1; i >= 0; --i)
5378         {
5379                 term_force_font(&data[i], NULL);
5380                 if (data[i].font_want) string_free(data[i].font_want);
5381                 if (data[i].w) DestroyWindow(data[i].w);
5382                 data[i].w = 0;
5383         }
5384
5385         /* Free the bitmap stuff */
5386 #ifdef USE_GRAPHICS
5387         if (infGraph.hPalette) DeleteObject(infGraph.hPalette);
5388         if (infGraph.hBitmap) DeleteObject(infGraph.hBitmap);
5389
5390         if (infMask.hPalette) DeleteObject(infMask.hPalette);
5391         if (infMask.hBitmap) DeleteObject(infMask.hBitmap);
5392
5393 #endif /* USE_GRAPHICS */
5394
5395         /*** Free some other stuff ***/
5396
5397         DeleteObject(hbrYellow);
5398
5399         /* bg */
5400         delete_bg();
5401
5402         if (hPal) DeleteObject(hPal);
5403
5404         UnregisterClass(AppName, hInstance);
5405
5406         if (hIcon) DestroyIcon(hIcon);
5407
5408         exit(0);
5409 }
5410
5411
5412
5413 /*** Initialize ***/
5414
5415
5416 /*
5417  * Init some stuff
5418  */
5419 static void init_stuff(void)
5420 {
5421         int i;
5422
5423         char path[1024];
5424
5425
5426         /* Get program name with full path */
5427         GetModuleFileName(hInstance, path, 512);
5428
5429         /* Save the "program name" */
5430         argv0 = path;
5431
5432         /* Get the name of the "*.ini" file */
5433         strcpy(path + strlen(path) - 4, ".INI");
5434
5435         /* Save the the name of the ini-file */
5436         ini_file = string_make(path);
5437
5438         /* Analyze the path */
5439         i = strlen(path);
5440
5441         /* Get the path */
5442         for (; i > 0; i--)
5443         {
5444                 if (path[i] == '\\')
5445                 {
5446                         /* End of path */
5447                         break;
5448                 }
5449         }
5450
5451         /* Add "lib" to the path */
5452         strcpy(path + i + 1, "lib\\");
5453
5454         /* Validate the path */
5455         validate_dir(path, TRUE);
5456
5457         /* Init the file paths */
5458         init_file_paths(path);
5459
5460         /* Hack -- Validate the paths */
5461         validate_dir(ANGBAND_DIR_APEX, FALSE);
5462         validate_dir(ANGBAND_DIR_BONE, FALSE);
5463
5464         /* Allow missing 'edit' directory */
5465         if (!check_dir(ANGBAND_DIR_EDIT))
5466         {
5467                 /* Must have 'data'! */
5468                 validate_dir(ANGBAND_DIR_DATA, TRUE);
5469         }
5470         else
5471         {
5472                 /* Don't need 'data' */
5473                 validate_dir(ANGBAND_DIR_DATA, FALSE);
5474         }
5475
5476         validate_dir(ANGBAND_DIR_FILE, TRUE);
5477         validate_dir(ANGBAND_DIR_HELP, FALSE);
5478         validate_dir(ANGBAND_DIR_INFO, FALSE);
5479         validate_dir(ANGBAND_DIR_PREF, TRUE);
5480         validate_dir(ANGBAND_DIR_SAVE, FALSE);
5481         validate_dir(ANGBAND_DIR_USER, TRUE);
5482         validate_dir(ANGBAND_DIR_XTRA, TRUE);
5483
5484         /* Build the filename */
5485         path_build(path, sizeof(path), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
5486
5487         /* Hack -- Validate the "news.txt" file */
5488         validate_file(path);
5489
5490
5491 #if 0 /* #ifndef JP */
5492         /* Build the "font" path */
5493         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "font");
5494
5495         /* Allocate the path */
5496         ANGBAND_DIR_XTRA_FONT = string_make(path);
5497
5498         /* Validate the "font" directory */
5499         validate_dir(ANGBAND_DIR_XTRA_FONT, TRUE);
5500
5501         /* Build the filename */
5502         path_build(path, sizeof(path), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
5503
5504         /* Hack -- Validate the basic font */
5505         validate_file(path);
5506 #endif
5507
5508
5509 #ifdef USE_GRAPHICS
5510
5511         /* Build the "graf" path */
5512         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
5513
5514         /* Allocate the path */
5515         ANGBAND_DIR_XTRA_GRAF = string_make(path);
5516
5517         /* Validate the "graf" directory */
5518         validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
5519
5520 #endif /* USE_GRAPHICS */
5521
5522
5523 #ifdef USE_SOUND
5524
5525         /* Build the "sound" path */
5526         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
5527
5528         /* Allocate the path */
5529         ANGBAND_DIR_XTRA_SOUND = string_make(path);
5530
5531         /* Validate the "sound" directory */
5532         validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
5533
5534 #endif /* USE_SOUND */
5535
5536 #ifdef USE_MUSIC
5537
5538         /* Build the "music" path */
5539         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
5540
5541         /* Allocate the path */
5542         ANGBAND_DIR_XTRA_MUSIC = string_make(path);
5543
5544         /* Validate the "music" directory */
5545         validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
5546
5547 #endif /* USE_MUSIC */
5548
5549         /* Build the "help" path */
5550         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "help");
5551
5552         /* Allocate the path */
5553         ANGBAND_DIR_XTRA_HELP = string_make(path);
5554
5555         /* Validate the "help" directory */
5556         /* validate_dir(ANGBAND_DIR_XTRA_HELP); */
5557 }
5558
5559 /*!
5560  * @brief (Windows固有)変愚蛮怒が起動済かどうかのチェック
5561  */
5562 static bool is_already_running(void)
5563 {
5564         bool result = FALSE;
5565         HANDLE hMutex;
5566
5567         hMutex = CreateMutex(NULL, TRUE, VERSION_NAME);
5568         if (GetLastError() == ERROR_ALREADY_EXISTS)
5569         {
5570                 result = TRUE;
5571         }
5572         return result;
5573 }
5574
5575
5576 /*!
5577  * @brief (Windows固有)Windowsアプリケーションとしてのエントリポイント
5578  */
5579 int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
5580                        LPSTR lpCmdLine, int nCmdShow)
5581 {
5582         int i;
5583
5584         WNDCLASS wc;
5585         HDC hdc;
5586         MSG msg;
5587
5588         setlocale(LC_ALL, "ja_JP.utf8");
5589
5590         /* Unused */
5591         (void)nCmdShow;
5592
5593         /* Save globally */
5594         hInstance = hInst;
5595         
5596         
5597         /* Prevent multiple run */
5598         if (is_already_running())
5599         {
5600                 MessageBox(NULL,
5601                                 _("変愚蛮怒はすでに起動しています。", "Hengband is already running."), 
5602                                 _("エラー!", "Error") ,
5603                                 MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5604                 return FALSE;
5605         }
5606
5607         if (hPrevInst == NULL)
5608         {
5609                 wc.style         = CS_CLASSDC;
5610                 wc.lpfnWndProc   = AngbandWndProc;
5611                 wc.cbClsExtra    = 0;
5612                 wc.cbWndExtra    = 4; /* one long pointer to term_data */
5613                 wc.hInstance     = hInst;
5614                 wc.hIcon         = hIcon = LoadIcon(hInst, AppName);
5615                 wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
5616                 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
5617                 wc.lpszMenuName  = AppName;
5618                 wc.lpszClassName = AppName;
5619
5620                 if (!RegisterClass(&wc)) exit(1);
5621
5622                 wc.lpfnWndProc   = AngbandListProc;
5623                 wc.lpszMenuName  = NULL;
5624                 wc.lpszClassName = AngList;
5625
5626                 if (!RegisterClass(&wc)) exit(2);
5627
5628 #ifdef USE_SAVER
5629
5630                 wc.style          = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
5631                 wc.lpfnWndProc    = AngbandSaverProc;
5632                 wc.hCursor        = NULL;
5633                 wc.lpszMenuName   = NULL;
5634                 wc.lpszClassName  = "WindowsScreenSaverClass";
5635
5636                 if (!RegisterClass(&wc)) exit(3);
5637
5638 #endif
5639
5640         }
5641
5642         /* Temporary hooks */
5643         plog_aux = hack_plog;
5644         quit_aux = hack_quit;
5645         core_aux = hack_quit;
5646
5647         /* Prepare the filepaths */
5648         init_stuff();
5649
5650         /* Initialize the keypress analyzer */
5651         for (i = 0; special_key_list[i]; ++i)
5652         {
5653                 special_key[special_key_list[i]] = TRUE;
5654         }
5655         /* Initialize the keypress analyzer */
5656         for (i = 0; ignore_key_list[i]; ++i)
5657         {
5658                 ignore_key[ignore_key_list[i]] = TRUE;
5659         }
5660
5661         /* Determine if display is 16/256/true color */
5662         hdc = GetDC(NULL);
5663         colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
5664         paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? TRUE : FALSE);
5665         ReleaseDC(NULL, hdc);
5666
5667         /* Initialize the colors */
5668         for (i = 0; i < 256; i++)
5669         {
5670                 byte rv, gv, bv;
5671
5672                 /* Extract desired values */
5673                 rv = angband_color_table[i][1];
5674                 gv = angband_color_table[i][2];
5675                 bv = angband_color_table[i][3];
5676
5677                 /* Extract the "complex" code */
5678                 win_clr[i] = PALETTERGB(rv, gv, bv);
5679
5680                 /* Save the "simple" code */
5681                 angband_color_table[i][0] = win_pal[i];
5682         }
5683
5684         /* Prepare the windows */
5685         init_windows();
5686
5687         /* bg */
5688         init_bg();
5689
5690         /* Activate hooks */
5691         plog_aux = hook_plog;
5692         quit_aux = hook_quit;
5693         core_aux = hook_quit;
5694
5695         /* Set the system suffix */
5696         ANGBAND_SYS = "win";
5697
5698         /* Set the keyboard suffix */
5699         if (7 != GetKeyboardType(0))
5700                 ANGBAND_KEYBOARD = "0";
5701         else
5702         {
5703                 /* Japanese keyboard */
5704                 switch (GetKeyboardType(1))
5705                 {
5706                 case 0x0D01: case 0x0D02:
5707                 case 0x0D03: case 0x0D04:
5708                 case 0x0D05: case 0x0D06:
5709                         /* NEC PC-98x1 */
5710                         ANGBAND_KEYBOARD = "NEC98";
5711                         break;
5712                 default:
5713                         /* PC/AT */
5714                         ANGBAND_KEYBOARD = "JAPAN";
5715                 }
5716         }
5717
5718         /* Catch nasty signals */
5719         signals_init();
5720
5721         Term_activate(term_screen);
5722         init_angband();
5723
5724         /* We are now initialized */
5725         initialized = TRUE;
5726 #ifdef CHUUKEI
5727         if(lpCmdLine[0] == '-'){
5728           switch(lpCmdLine[1])
5729           {
5730           case 'p':
5731           case 'P':
5732             {
5733               if (!lpCmdLine[2]) break;
5734               chuukei_server = TRUE;
5735               if(connect_chuukei_server(&lpCmdLine[2])<0){
5736                 msg_print("connect fail");
5737                 return 0;
5738               }
5739               msg_print("connect");
5740               msg_print(NULL);
5741               break;
5742             }
5743
5744           case 'c':
5745           case 'C':
5746             {
5747               if (!lpCmdLine[2]) break;
5748               chuukei_client = TRUE;
5749               connect_chuukei_server(&lpCmdLine[2]);
5750               play_game(FALSE);
5751               quit(NULL);
5752               return 0;
5753             }
5754           case 'X':
5755           case 'x':
5756             {
5757               if (!lpCmdLine[2]) break;
5758               prepare_browse_movie(&lpCmdLine[2]);
5759               play_game(FALSE);
5760               quit(NULL);
5761               return 0;
5762             }
5763           }
5764         }
5765 #endif
5766
5767 #ifdef CHUUKEI
5768         /* Did the user double click on a save file? */
5769         if(!chuukei_server) check_for_save_file(lpCmdLine);
5770 #else
5771         /* Did the user double click on a save file? */
5772         check_for_save_file(lpCmdLine);
5773 #endif
5774
5775         /* Prompt the user */
5776         prt(_("[ファイル] メニューの [新規] または [開く] を選択してください。", "[Choose 'New' or 'Open' from the 'File' menu]"), 23, _(8, 17));
5777
5778         Term_fresh();
5779
5780         /* Process messages forever */
5781         while (GetMessage(&msg, NULL, 0, 0))
5782         {
5783                 TranslateMessage(&msg);
5784                 DispatchMessage(&msg);
5785         }
5786         quit(NULL);
5787         return (0);
5788 }
5789
5790
5791 #endif /* WINDOWS */
5792