OSDN Git Service

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