OSDN Git Service

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