OSDN Git Service

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