OSDN Git Service

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