OSDN Git Service

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