OSDN Git Service

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