OSDN Git Service

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