OSDN Git Service

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