OSDN Git Service

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