OSDN Git Service

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