OSDN Git Service

6d19a1c2b057ea684469039e34c71488c0dd311c
[hengband/hengband.git] / src / main-win.c
1 /* File: main-win.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, and others
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.
9  */
10
11
12 /*
13  * This file helps Angband work with Windows computers.
14  *
15  * To use this file, use an appropriate "Makefile" or "Project File",
16  * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
17  * make sure to obtain various extra files as described below.
18  *
19  * The official compilation uses the CodeWarrior Pro compiler, which
20  * includes a special project file and precompilable header file.
21  *
22  *
23  * See also "main-dos.c" and "main-ibm.c".
24  *
25  *
26  * The "lib/user/pref-win.prf" file contains keymaps, macro definitions,
27  * and/or color redefinitions.
28  *
29  * The "lib/user/font-win.prf" contains attr/char mappings for use with the
30  * normal "lib/xtra/font/*.fon" font files.
31  *
32  * The "lib/user/graf-win.prf" contains attr/char mappings for use with the
33  * special "lib/xtra/graf/*.bmp" bitmap files, which are activated by a menu
34  * item.
35  *
36  *
37  * Compiling this file, and using the resulting executable, requires
38  * several extra files not distributed with the standard Angband code.
39  * If "USE_GRAPHICS" is defined, then "readdib.h" and "readdib.c" must
40  * be placed into "src/", and the "8X8.BMP" bitmap file must be placed
41  * into "lib/xtra/graf".  In any case, some "*.fon" files (including
42  * "8X13.FON" if nothing else) must be placed into "lib/xtra/font/".
43  * If "USE_SOUND" is defined, then some special library (for example,
44  * "winmm.lib") may need to be linked in, and desired "*.WAV" sound
45  * files must be placed into "lib/xtra/sound/".  All of these extra
46  * files can be found in the "ext-win" archive.
47  *
48  *
49  * The "Term_xtra_win_clear()" function should probably do a low-level
50  * clear of the current window, and redraw the borders and other things,
51  * if only for efficiency.  XXX XXX XXX
52  *
53  * A simpler method is needed for selecting the "tile size" for windows.
54  * XXX XXX XXX
55  *
56  * The various "warning" messages assume the existance of the "screen.w"
57  * window, I think, and only a few calls actually check for its existance,
58  * this may be okay since "NULL" means "on top of all windows". (?)  The
59  * user must never be allowed to "hide" the main window, or the "menubar"
60  * will disappear.  XXX XXX XXX
61  *
62  * Special "Windows Help Files" can be placed into "lib/xtra/help/" for
63  * use with the "winhelp.exe" program.  These files *may* be available
64  * at the ftp site somewhere, but I have not seen them.  XXX XXX XXX
65  *
66  *
67  * Initial framework (and most code) by Ben Harrison (benh@phial.com).
68  *
69  * Original code by Skirmantas Kligys (kligys@scf.usc.edu).
70  *
71  * Additional code by Ross E Becker (beckerr@cis.ohio-state.edu),
72  * and Chris R. Martin (crm7479@tam2000.tamu.edu).
73  */
74
75 #include "angband.h"
76
77 #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                         /*! @todo redraw 
1731                         wid = 64;
1732                         hgt = 64;
1733                         twid = 32;
1734                         thgt = 32;
1735                         ox = -16;
1736                         oy = -24;
1737                         name = "64X64.BMP";
1738                         */
1739
1740                         wid = 32;
1741                         hgt = 32;
1742                         twid = 32;
1743                         thgt = 32;
1744                         ox = 0;
1745                         oy = 0;
1746                         name = "32X32.BMP";
1747
1748                         ANGBAND_GRAF = "ne2";
1749                 }
1750                 else
1751                 {
1752                         wid = 8;
1753                         hgt = 8;
1754                         twid = 8;
1755                         thgt = 8;
1756                         ox = 0;
1757                         oy = 0;
1758                         name = "8X8.BMP";
1759                         ANGBAND_GRAF = "old";
1760                 }
1761
1762                 /* Access the bitmap file */
1763                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, name);
1764
1765                 /* Load the bitmap or quit */
1766                 if (!ReadDIB(data[0].w, buf, &infGraph))
1767                 {
1768 #ifdef JP
1769                         plog_fmt("¥Ó¥Ã¥È¥Þ¥Ã¥× '%s' ¤òÆɤ߹þ¤á¤Þ¤»¤ó¡£", name);
1770 #else
1771                         plog_fmt("Cannot read bitmap file '%s'", name);
1772 #endif
1773
1774                         return (FALSE);
1775                 }
1776
1777                 /* Save the new sizes */
1778                 infGraph.CellWidth = wid;
1779                 infGraph.CellHeight = hgt;
1780                 infGraph.TileWidth = twid;
1781                 infGraph.TileHeight = thgt;
1782                 infGraph.OffsetX = ox;
1783                 infGraph.OffsetY = oy;
1784
1785                 if (arg_graphics == GRAPHICS_ADAM_BOLT)
1786                 {
1787                         /* Access the mask file */
1788                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask.bmp");
1789
1790                         /* Load the bitmap or quit */
1791                         if (!ReadDIB(data[0].w, buf, &infMask))
1792                         {
1793                                 plog_fmt("Cannot read bitmap file '%s'", buf);
1794                                 return (FALSE);
1795                         }
1796                 }
1797                 if (arg_graphics == GRAPHICS_HENGBAND)
1798                 {
1799                         /* Access the mask file */
1800                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask32.bmp");
1801
1802                         /* Load the bitmap or quit */
1803                         if (!ReadDIB(data[0].w, buf, &infMask))
1804                         {
1805                                 plog_fmt("Cannot read bitmap file '%s'", buf);
1806                                 return (FALSE);
1807                         }
1808                 }
1809
1810                 /* Activate a palette */
1811                 if (!new_palette())
1812                 {
1813                         /* Free bitmap XXX XXX XXX */
1814
1815                         /* Oops */
1816 #ifdef JP
1817                         plog("¥Ñ¥ì¥Ã¥È¤ò¼Â¸½¤Ç¤­¤Þ¤»¤ó¡ª");
1818 #else
1819                         plog("Cannot activate palette!");
1820 #endif
1821
1822                         return (FALSE);
1823                 }
1824
1825                 /* Graphics available */
1826                 can_use_graphics = arg_graphics;
1827         }
1828
1829         /* Result */
1830         return (can_use_graphics);
1831 }
1832 #endif /* USE_GRAPHICS */
1833
1834
1835 #ifdef USE_MUSIC
1836 /*
1837  * Initialize music
1838  */
1839 static bool init_music(void)
1840 {
1841         /* Initialize once */
1842         if (!can_use_music)
1843         {
1844                 /* Load the prefs */
1845                 load_music_prefs();
1846
1847                 /* Sound available */
1848                 can_use_music = TRUE;
1849         }
1850
1851         /* Result */
1852         return (can_use_music);
1853 }
1854
1855 /*
1856  * Hack -- Stop a music
1857  */
1858 static void stop_music(void)
1859 {
1860         mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
1861         mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
1862 }
1863
1864 #endif /* USE_MUSIC */
1865
1866 #ifdef USE_SOUND
1867 /*
1868  * Initialize sound
1869  */
1870 static bool init_sound(void)
1871 {
1872         /* Initialize once */
1873         if (!can_use_sound)
1874         {
1875                 /* Load the prefs */
1876                 load_sound_prefs();
1877
1878                 /* Sound available */
1879                 can_use_sound = TRUE;
1880         }
1881
1882         /* Result */
1883         return (can_use_sound);
1884 }
1885 #endif /* USE_SOUND */
1886
1887
1888 /*
1889  * Resize a window
1890  */
1891 static void term_window_resize(term_data *td)
1892 {
1893         /* Require window */
1894         if (!td->w) return;
1895
1896         /* Resize the window */
1897         SetWindowPos(td->w, 0, 0, 0,
1898                      td->size_wid, td->size_hgt,
1899                      SWP_NOMOVE | SWP_NOZORDER);
1900
1901         /* Redraw later */
1902         InvalidateRect(td->w, NULL, TRUE);
1903 }
1904
1905
1906 /*
1907  * Force the use of a new "font file" for a term_data
1908  *
1909  * This function may be called before the "window" is ready
1910  *
1911  * This function returns zero only if everything succeeds.
1912  *
1913  * Note that the "font name" must be capitalized!!!
1914  */
1915 static errr term_force_font(term_data *td, cptr path)
1916 {
1917         int wid, hgt;
1918
1919 #if 0 /* #ifndef JP */
1920         int i;
1921         char *base;
1922         char buf[1024];
1923 #endif
1924
1925         /* Forget the old font (if needed) */
1926         if (td->font_id) DeleteObject(td->font_id);
1927
1928 #if 1 /* #ifdef JP */
1929         /* Unused */
1930         (void)path;
1931
1932         /* Create the font (using the 'base' of the font file name!) */
1933         td->font_id = CreateFontIndirect(&(td->lf));
1934         wid = td->lf.lfWidth;
1935         hgt = td->lf.lfHeight;
1936         if (!td->font_id) return (1);
1937 #else
1938         /* Forget old font */
1939         if (td->font_file)
1940         {
1941                 bool used = FALSE;
1942
1943                 /* Scan windows */
1944                 for (i = 0; i < MAX_TERM_DATA; i++)
1945                 {
1946                         /* Don't check when closing the application */
1947                         if (!path) break;
1948
1949                         /* Check "screen" */
1950                         if ((td != &data[i]) &&
1951                             (data[i].font_file) &&
1952                             (streq(data[i].font_file, td->font_file)))
1953                         {
1954                                 used = TRUE;
1955                         }
1956                 }
1957
1958                 /* Remove unused font resources */
1959                 if (!used) RemoveFontResource(td->font_file);
1960
1961                 /* Free the old name */
1962                 string_free(td->font_file);
1963
1964                 /* Forget it */
1965                 td->font_file = NULL;
1966         }
1967
1968         /* No path given */
1969         if (!path) return (1);
1970
1971         /* Local copy */
1972         strcpy(buf, path);
1973
1974         /* Analyze font path */
1975         base = analyze_font(buf, &wid, &hgt);
1976
1977         /* Verify suffix */
1978         if (!suffix(base, ".FON")) return (1);
1979
1980         /* Verify file */
1981         if (!check_file(buf)) return (1);
1982
1983         /* Load the new font */
1984         if (!AddFontResource(buf)) return (1);
1985
1986         /* Save new font name */
1987         td->font_file = string_make(base);
1988
1989         /* Remove the "suffix" */
1990         base[strlen(base)-4] = '\0';
1991
1992         /* Create the font (using the 'base' of the font file name!) */
1993         td->font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0,
1994                                  ANSI_CHARSET, OUT_DEFAULT_PRECIS,
1995                                  CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1996                                  FIXED_PITCH | FF_DONTCARE, base);
1997 #endif
1998
1999         /* Hack -- Unknown size */
2000         if (!wid || !hgt)
2001         {
2002                 HDC hdcDesktop;
2003                 HFONT hfOld;
2004                 TEXTMETRIC tm;
2005
2006                 /* all this trouble to get the cell size */
2007                 hdcDesktop = GetDC(HWND_DESKTOP);
2008                 hfOld = SelectObject(hdcDesktop, td->font_id);
2009                 GetTextMetrics(hdcDesktop, &tm);
2010                 SelectObject(hdcDesktop, hfOld);
2011                 ReleaseDC(HWND_DESKTOP, hdcDesktop);
2012
2013                 /* Font size info */
2014                 wid = tm.tmAveCharWidth;
2015                 hgt = tm.tmHeight;
2016         }
2017
2018         /* Save the size info */
2019         td->font_wid = wid;
2020         td->font_hgt = hgt;
2021
2022         /* Success */
2023         return (0);
2024 }
2025
2026
2027
2028 /*
2029  * Allow the user to change the font for this window.
2030  */
2031 static void term_change_font(term_data *td)
2032 {
2033 #if 1 /* #ifdef JP */
2034         CHOOSEFONT cf;
2035
2036         memset(&cf, 0, sizeof(cf));
2037         cf.lStructSize = sizeof(cf);
2038     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
2039     cf.lpLogFont = &(td->lf);
2040
2041         if (ChooseFont(&cf))
2042         {
2043                 /* Force the font */
2044                 term_force_font(td, NULL);
2045
2046                 /* Assume not bizarre */
2047                 td->bizarre = TRUE;
2048
2049                 /* Reset the tile info */
2050                 td->tile_wid = td->font_wid;
2051                 td->tile_hgt = td->font_hgt;
2052
2053                 /* Analyze the font */
2054                 term_getsize(td);
2055
2056                 /* Resize the window */
2057                 term_window_resize(td);
2058         }
2059
2060 #else
2061         OPENFILENAME ofn;
2062
2063         char tmp[1024] = "";
2064
2065         /* Extract a default if possible */
2066         if (td->font_file) strcpy(tmp, td->font_file);
2067
2068         /* Ask for a choice */
2069         memset(&ofn, 0, sizeof(ofn));
2070         ofn.lStructSize = sizeof(ofn);
2071         ofn.hwndOwner = data[0].w;
2072         ofn.lpstrFilter = "Angband Font Files (*.fon)\0*.fon\0";
2073         ofn.nFilterIndex = 1;
2074         ofn.lpstrFile = tmp;
2075         ofn.nMaxFile = 128;
2076         ofn.lpstrInitialDir = ANGBAND_DIR_XTRA_FONT;
2077         ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
2078         ofn.lpstrDefExt = "fon";
2079
2080         /* Force choice if legal */
2081         if (GetOpenFileName(&ofn))
2082         {
2083                 /* Force the font */
2084                 if (term_force_font(td, tmp))
2085                 {
2086                         /* Access the standard font file */
2087                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
2088
2089                         /* Force the use of that font */
2090                         (void)term_force_font(td, tmp);
2091                 }
2092
2093                 /* Assume not bizarre */
2094                 td->bizarre = FALSE;
2095
2096                 /* Reset the tile info */
2097                 td->tile_wid = td->font_wid;
2098                 td->tile_hgt = td->font_hgt;
2099
2100                 /* Analyze the font */
2101                 term_getsize(td);
2102
2103                 /* Resize the window */
2104                 term_window_resize(td);
2105         }
2106 #endif
2107
2108 }
2109
2110 /*
2111  * Allow the user to lock this window.
2112  */
2113 static void term_window_pos(term_data *td, HWND hWnd)
2114 {
2115         SetWindowPos(td->w, hWnd, 0, 0, 0, 0,
2116                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
2117 }
2118
2119 static void windows_map(void);
2120
2121 /*
2122  * Hack -- redraw a term_data
2123  */
2124 static void term_data_redraw(term_data *td)
2125 {
2126         if (td->map_active)
2127         {
2128                 /* Redraw the map */
2129                 windows_map();
2130         }
2131         else
2132         {
2133                 /* Activate the term */
2134                 Term_activate(&td->t);
2135
2136                 /* Redraw the contents */
2137                 Term_redraw();
2138
2139                 /* Restore the term */
2140                 Term_activate(term_screen);
2141         }
2142 }
2143
2144
2145 void Term_inversed_area(HWND hWnd, int x, int y, int w, int h)
2146 {
2147         HDC hdc;
2148         HPEN oldPen;
2149         HBRUSH myBrush, oldBrush;
2150
2151         term_data *td = (term_data *)GetWindowLong(hWnd, 0);
2152         int tx = td->size_ow1 + x * td->tile_wid;
2153         int ty = td->size_oh1 + y * td->tile_hgt;
2154         int tw = w * td->tile_wid - 1;
2155         int th = h * td->tile_hgt - 1;
2156
2157         hdc = GetDC(hWnd);
2158         myBrush = CreateSolidBrush(RGB(255, 255, 255));
2159         oldBrush = SelectObject(hdc, myBrush);
2160         oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2161
2162         PatBlt(hdc, tx, ty, tw, th, PATINVERT);
2163
2164         SelectObject(hdc, oldBrush);
2165         SelectObject(hdc, oldPen);
2166 }
2167
2168
2169
2170 /*** Function hooks needed by "Term" ***/
2171
2172
2173 #if 0
2174
2175 /*
2176  * Initialize a new Term
2177  */
2178 static void Term_init_win(term *t)
2179 {
2180         /* XXX Unused */
2181 }
2182
2183
2184 /*
2185  * Nuke an old Term
2186  */
2187 static void Term_nuke_win(term *t)
2188 {
2189         /* XXX Unused */
2190 }
2191
2192 #endif
2193
2194
2195 /*
2196  * Interact with the User
2197  */
2198 static errr Term_user_win(int n)
2199 {
2200         /* Unused */
2201         (void)n;
2202
2203         /* Success */
2204         return (0);
2205 }
2206
2207
2208 /*
2209  * React to global changes
2210  */
2211 static errr Term_xtra_win_react(void)
2212 {
2213         int i;
2214
2215
2216         /* Simple color */
2217         if (colors16)
2218         {
2219                 /* Save the default colors */
2220                 for (i = 0; i < 256; i++)
2221                 {
2222                         /* Simply accept the desired colors */
2223                         win_pal[i] = angband_color_table[i][0];
2224                 }
2225         }
2226
2227         /* Complex color */
2228         else
2229         {
2230                 COLORREF code;
2231
2232                 byte rv, gv, bv;
2233
2234                 bool change = FALSE;
2235
2236                 /* Save the default colors */
2237                 for (i = 0; i < 256; i++)
2238                 {
2239                         /* Extract desired values */
2240                         rv = angband_color_table[i][1];
2241                         gv = angband_color_table[i][2];
2242                         bv = angband_color_table[i][3];
2243
2244                         /* Extract a full color code */
2245                         code = PALETTERGB(rv, gv, bv);
2246
2247                         /* Activate changes */
2248                         if (win_clr[i] != code)
2249                         {
2250                                 /* Note the change */
2251                                 change = TRUE;
2252
2253                                 /* Apply the desired color */
2254                                 win_clr[i] = code;
2255                         }
2256                 }
2257
2258                 /* Activate the palette if needed */
2259                 if (change) (void)new_palette();
2260         }
2261
2262
2263 #ifdef USE_SOUND
2264
2265         /* Handle "arg_sound" */
2266         if (use_sound != arg_sound)
2267         {
2268                 /* Initialize (if needed) */
2269                 if (arg_sound && !init_sound())
2270                 {
2271                         /* Warning */
2272 #ifdef JP
2273                         plog("¥µ¥¦¥ó¥É¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó¡ª");
2274 #else
2275                         plog("Cannot initialize sound!");
2276 #endif
2277
2278
2279                         /* Cannot enable */
2280                         arg_sound = FALSE;
2281                 }
2282
2283                 /* Change setting */
2284                 use_sound = arg_sound;
2285         }
2286
2287 #endif
2288
2289 #ifdef USE_MUSIC
2290
2291         /* Handle "arg_sound" */
2292         if (use_music != arg_music)
2293         {
2294                 /* Initialize (if needed) */
2295                 if (arg_music && !init_music())
2296                 {
2297                         /* Warning */
2298 #ifdef JP
2299                         plog("BGM¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó¡ª");
2300 #else
2301                         plog("Cannot initialize BGM!");
2302 #endif
2303                         /* Cannot enable */
2304                         arg_music = FALSE;
2305                 }
2306
2307                 /* Change setting */
2308                 use_music = arg_music;
2309
2310                 if(!arg_music) stop_music();
2311                 else select_floor_music();
2312
2313         }
2314
2315 #endif
2316
2317
2318 #ifdef USE_GRAPHICS
2319
2320         /* Handle "arg_graphics" */
2321         if (use_graphics != arg_graphics)
2322         {
2323                 /* Initialize (if needed) */
2324                 if (arg_graphics && !init_graphics())
2325                 {
2326                         /* Warning */
2327 #ifdef JP
2328                         plog("¥°¥é¥Õ¥£¥Ã¥¯¥¹¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó!");
2329 #else
2330                         plog("Cannot initialize graphics!");
2331 #endif
2332
2333
2334                         /* Cannot enable */
2335                         arg_graphics = GRAPHICS_NONE;
2336                 }
2337
2338                 /* Change setting */
2339                 use_graphics = arg_graphics;
2340
2341                 /* Reset visuals */
2342 #ifdef ANGBAND_2_8_1
2343                 reset_visuals();
2344 #else /* ANGBAND_2_8_1 */
2345                 reset_visuals(TRUE);
2346 #endif /* ANGBAND_2_8_1 */
2347         }
2348
2349 #endif /* USE_GRAPHICS */
2350
2351
2352         /* Clean up windows */
2353         for (i = 0; i < MAX_TERM_DATA; i++)
2354         {
2355                 term *old = Term;
2356
2357                 term_data *td = &data[i];
2358
2359                 /* Update resized windows */
2360                 if ((td->cols != (uint)td->t.wid) || (td->rows != (uint)td->t.hgt))
2361                 {
2362                         /* Activate */
2363                         Term_activate(&td->t);
2364
2365                         /* Hack -- Resize the term */
2366                         Term_resize(td->cols, td->rows);
2367
2368                         /* Redraw the contents */
2369                         Term_redraw();
2370
2371                         /* Restore */
2372                         Term_activate(old);
2373                 }
2374         }
2375
2376
2377         /* Success */
2378         return (0);
2379 }
2380
2381
2382 /*
2383  * Process at least one event
2384  */
2385 static errr Term_xtra_win_event(int v)
2386 {
2387         MSG msg;
2388
2389         /* Wait for an event */
2390         if (v)
2391         {
2392                 /* Block */
2393                 if (GetMessage(&msg, NULL, 0, 0))
2394                 {
2395                         TranslateMessage(&msg);
2396                         DispatchMessage(&msg);
2397                 }
2398         }
2399
2400         /* Check for an event */
2401         else
2402         {
2403                 /* Check */
2404                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2405                 {
2406                         TranslateMessage(&msg);
2407                         DispatchMessage(&msg);
2408                 }
2409         }
2410
2411         /* Success */
2412         return 0;
2413 }
2414
2415
2416 /*
2417  * Process all pending events
2418  */
2419 static errr Term_xtra_win_flush(void)
2420 {
2421         MSG msg;
2422
2423         /* Process all pending events */
2424         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2425         {
2426                 TranslateMessage(&msg);
2427                 DispatchMessage(&msg);
2428         }
2429
2430         /* Success */
2431         return (0);
2432 }
2433
2434
2435 /*
2436  * Hack -- clear the screen
2437  *
2438  * Make this more efficient XXX XXX XXX
2439  */
2440 static errr Term_xtra_win_clear(void)
2441 {
2442         term_data *td = (term_data*)(Term->data);
2443
2444         HDC hdc;
2445         RECT rc;
2446
2447         /* Rectangle to erase */
2448         rc.left = td->size_ow1;
2449         rc.right = rc.left + td->cols * td->tile_wid;
2450         rc.top = td->size_oh1;
2451         rc.bottom = rc.top + td->rows * td->tile_hgt;
2452
2453         /* Erase it */
2454         hdc = GetDC(td->w);
2455         SetBkColor(hdc, RGB(0, 0, 0));
2456         SelectObject(hdc, td->font_id);
2457         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2458
2459         /* bg */
2460         if (use_bg)
2461         {
2462                 rc.left = 0; rc.top = 0;
2463                 DrawBG(hdc, &rc);
2464         }
2465         ReleaseDC(td->w, hdc);
2466
2467         /* Success */
2468         return 0;
2469 }
2470
2471
2472 /*
2473  * Hack -- make a noise
2474  */
2475 static errr Term_xtra_win_noise(void)
2476 {
2477         MessageBeep(MB_ICONASTERISK);
2478         return (0);
2479 }
2480
2481
2482 /*
2483  * Hack -- make a sound
2484  */
2485 static errr Term_xtra_win_sound(int v)
2486 {
2487 #ifdef USE_SOUND
2488         int i;
2489         char buf[1024];
2490 #endif /* USE_SOUND */
2491
2492         /* Sound disabled */
2493         if (!use_sound) return (1);
2494
2495         /* Illegal sound */
2496         if ((v < 0) || (v >= SOUND_MAX)) return (1);
2497
2498 #ifdef USE_SOUND
2499
2500         /* Count the samples */
2501         for (i = 0; i < SAMPLE_MAX; i++)
2502         {
2503                 if (!sound_file[v][i])
2504                         break;
2505         }
2506
2507         /* No sample */
2508         if (i == 0) return (1);
2509
2510         /* Build the path */
2511         path_build(buf, 1024, ANGBAND_DIR_XTRA_SOUND, sound_file[v][Rand_external(i)]);
2512
2513 #ifdef WIN32
2514
2515         /* Play the sound, catch errors */
2516         return (PlaySound(buf, 0, SND_FILENAME | SND_ASYNC));
2517
2518 #else /* WIN32 */
2519
2520         /* Play the sound, catch errors */
2521         return (sndPlaySound(buf, SND_ASYNC));
2522
2523 #endif /* WIN32 */
2524
2525 #else /* USE_SOUND */
2526
2527         /* Oops */
2528         return (1);
2529
2530 #endif /* USE_SOUND */
2531 }
2532
2533 /*
2534  * Hack -- play a music
2535  */
2536 static errr Term_xtra_win_music(int n, int v)
2537 {
2538 #ifdef USE_MUSIC
2539         int i;
2540         char buf[1024];
2541 #endif /* USE_MUSIC */
2542
2543         /* Sound disabled */
2544         if(!use_music) return (1);
2545
2546         /* Illegal sound */
2547         if(n == TERM_XTRA_MUSIC_BASIC && ((v < 0) || (v >= MUSIC_BASIC_MAX))) return (1);
2548         else if(v < 0 || v >= 1000) return(1); /*!< TODO */
2549
2550 #ifdef USE_MUSIC
2551
2552         switch(n)
2553         {
2554         case TERM_XTRA_MUSIC_BASIC:
2555                 for (i = 0; i < SAMPLE_MAX; i++) if(!music_file[v][i]) break;
2556                 break;
2557         case TERM_XTRA_MUSIC_DUNGEON:
2558                 for (i = 0; i < SAMPLE_MAX; i++) if(!dungeon_music_file[v][i]) break;
2559                 break;
2560         case TERM_XTRA_MUSIC_QUEST:
2561                 for (i = 0; i < SAMPLE_MAX; i++) if(!quest_music_file[v][i]) break;
2562                 break;
2563         case TERM_XTRA_MUSIC_TOWN:
2564                 for (i = 0; i < SAMPLE_MAX; i++) if(!town_music_file[v][i]) break;
2565                 break;
2566         }
2567
2568         /* No sample */
2569         if (i == 0)
2570         {
2571                 mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2572                 mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2573                 return (1);
2574         }
2575
2576         switch(n)
2577         {
2578         case TERM_XTRA_MUSIC_BASIC:
2579                 for (i = 0; i < SAMPLE_MAX; i++) if(!music_file[v][i]) break;
2580                 break;
2581         case TERM_XTRA_MUSIC_DUNGEON:
2582                 for (i = 0; i < SAMPLE_MAX; i++) if(!dungeon_music_file[v][i]) break;
2583                 break;
2584         case TERM_XTRA_MUSIC_QUEST:
2585                 for (i = 0; i < SAMPLE_MAX; i++) if(!quest_music_file[v][i]) break;
2586                 break;
2587         case TERM_XTRA_MUSIC_TOWN:
2588                 for (i = 0; i < SAMPLE_MAX; i++) if(!town_music_file[v][i]) break;
2589                 break;
2590         }
2591
2592         /* No sample */
2593         if (i == 0)
2594         {
2595                 mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2596                 mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2597                 return (1);
2598         }
2599
2600         switch(n)
2601         {
2602         case TERM_XTRA_MUSIC_BASIC:
2603                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, music_file[v][Rand_external(i)]);
2604                 break;
2605         case TERM_XTRA_MUSIC_DUNGEON:
2606                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, dungeon_music_file[v][Rand_external(i)]);
2607                 break;
2608         case TERM_XTRA_MUSIC_QUEST:
2609                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, quest_music_file[v][Rand_external(i)]);
2610                 break;
2611         case TERM_XTRA_MUSIC_TOWN:
2612                 path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, town_music_file[v][Rand_external(i)]);
2613                 break;
2614         }
2615
2616 #ifdef WIN32
2617
2618         mop.lpstrDeviceType = mci_device_type;  
2619         mop.lpstrElementName = buf;
2620         mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2621         mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2622         mciSendCommand(mop.wDeviceID, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mop);
2623         mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
2624         mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
2625         return (0);
2626
2627 #endif /* WIN32 */
2628
2629 #else /* USE_MUSIC */
2630
2631         return (1);
2632
2633 #endif /* USE_MUSIC */
2634
2635 }
2636
2637
2638 /*
2639  * Delay for "x" milliseconds
2640  */
2641 static int Term_xtra_win_delay(int v)
2642 {
2643
2644 #ifdef WIN32
2645
2646         /* Sleep */
2647         Sleep(v);
2648
2649 #else /* WIN32 */
2650
2651         DWORD t;
2652         MSG msg;
2653
2654         /* Final count */
2655         t = GetTickCount() + v;
2656
2657         /* Wait for it */
2658         while (GetTickCount() < t)
2659         {
2660                 /* Handle messages */
2661                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2662                 {
2663                         TranslateMessage(&msg);
2664                         DispatchMessage(&msg);
2665                 }
2666         }
2667
2668 #endif /* WIN32 */
2669
2670         /* Success */
2671         return (0);
2672 }
2673
2674
2675 /*
2676  * Do a "special thing"
2677  */
2678 static errr Term_xtra_win(int n, int v)
2679 {
2680         /* Handle a subset of the legal requests */
2681         switch (n)
2682         {
2683                 /* Make a bell sound */
2684                 case TERM_XTRA_NOISE:
2685                 {
2686                         return (Term_xtra_win_noise());
2687                 }
2688
2689                 /* Play a music */
2690                 case TERM_XTRA_MUSIC_BASIC:
2691                 case TERM_XTRA_MUSIC_DUNGEON:
2692                 case TERM_XTRA_MUSIC_QUEST:
2693                 case TERM_XTRA_MUSIC_TOWN:
2694                 {
2695                         return (Term_xtra_win_music(n, v));
2696                 }
2697
2698                 /* Make a special sound */
2699                 case TERM_XTRA_SOUND:
2700                 {
2701                         return (Term_xtra_win_sound(v));
2702                 }
2703
2704                 /* Process random events */
2705                 case TERM_XTRA_BORED:
2706                 {
2707                         return (Term_xtra_win_event(0));
2708                 }
2709
2710                 /* Process an event */
2711                 case TERM_XTRA_EVENT:
2712                 {
2713                         return (Term_xtra_win_event(v));
2714                 }
2715
2716                 /* Flush all events */
2717                 case TERM_XTRA_FLUSH:
2718                 {
2719                         return (Term_xtra_win_flush());
2720                 }
2721
2722                 /* Clear the screen */
2723                 case TERM_XTRA_CLEAR:
2724                 {
2725                         return (Term_xtra_win_clear());
2726                 }
2727
2728                 /* React to global changes */
2729                 case TERM_XTRA_REACT:
2730                 {
2731                         return (Term_xtra_win_react());
2732                 }
2733
2734                 /* Delay for some milliseconds */
2735                 case TERM_XTRA_DELAY:
2736                 {
2737                         return (Term_xtra_win_delay(v));
2738                 }
2739         }
2740
2741         /* Oops */
2742         return 1;
2743 }
2744
2745
2746
2747 /*
2748  * Low level graphics (Assumes valid input).
2749  *
2750  * Draw a "cursor" at (x,y), using a "yellow box".
2751  */
2752 static errr Term_curs_win(int x, int y)
2753 {
2754         term_data *td = (term_data*)(Term->data);
2755
2756         RECT rc;
2757         HDC hdc;
2758
2759         int tile_wid, tile_hgt;
2760
2761         if (td->map_active)
2762         {
2763                 tile_wid = td->map_tile_wid;
2764                 tile_hgt = td->map_tile_hgt;
2765         }
2766         else
2767         {
2768                 tile_wid = td->tile_wid;
2769                 tile_hgt = td->tile_hgt;
2770         }
2771
2772         /* Frame the grid */
2773         rc.left = x * tile_wid + td->size_ow1;
2774         rc.right = rc.left + tile_wid;
2775         rc.top = y * tile_hgt + td->size_oh1;
2776         rc.bottom = rc.top + tile_hgt;
2777
2778         /* Cursor is done as a yellow "box" */
2779         hdc = GetDC(td->w);
2780         FrameRect(hdc, &rc, hbrYellow);
2781         ReleaseDC(td->w, hdc);
2782
2783         /* Success */
2784         return 0;
2785 }
2786
2787
2788 /*
2789  * Low level graphics (Assumes valid input).
2790  *
2791  * Draw a "big cursor" at (x,y), using a "yellow box".
2792  */
2793 static errr Term_bigcurs_win(int x, int y)
2794 {
2795         term_data *td = (term_data*)(Term->data);
2796
2797         RECT rc;
2798         HDC hdc;
2799
2800         int tile_wid, tile_hgt;
2801
2802         if (td->map_active)
2803         {
2804                 /* Normal cursor in map window */
2805                 Term_curs_win(x, y);
2806                 return 0;
2807         }
2808         else
2809         {
2810                 tile_wid = td->tile_wid;
2811                 tile_hgt = td->tile_hgt;
2812         }
2813
2814         /* Frame the grid */
2815         rc.left = x * tile_wid + td->size_ow1;
2816         rc.right = rc.left + 2 * tile_wid;
2817         rc.top = y * tile_hgt + td->size_oh1;
2818         rc.bottom = rc.top + tile_hgt;
2819
2820         /* Cursor is done as a yellow "box" */
2821         hdc = GetDC(td->w);
2822         FrameRect(hdc, &rc, hbrYellow);
2823         ReleaseDC(td->w, hdc);
2824
2825         /* Success */
2826         return 0;
2827 }
2828
2829
2830 /*
2831  * Low level graphics (Assumes valid input).
2832  *
2833  * Erase a "block" of "n" characters starting at (x,y).
2834  */
2835 static errr Term_wipe_win(int x, int y, int n)
2836 {
2837         term_data *td = (term_data*)(Term->data);
2838
2839         HDC hdc;
2840         RECT rc;
2841
2842         /* Rectangle to erase in client coords */
2843         rc.left = x * td->tile_wid + td->size_ow1;
2844         rc.right = rc.left + n * td->tile_wid;
2845         rc.top = y * td->tile_hgt + td->size_oh1;
2846         rc.bottom = rc.top + td->tile_hgt;
2847
2848         hdc = GetDC(td->w);
2849         SetBkColor(hdc, RGB(0, 0, 0));
2850         SelectObject(hdc, td->font_id);
2851         /* bg */
2852         if (use_bg)
2853                 DrawBG(hdc, &rc);
2854         else
2855                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2856         ReleaseDC(td->w, hdc);
2857
2858         /* Success */
2859         return 0;
2860 }
2861
2862
2863 /*
2864  * Low level graphics.  Assumes valid input.
2865  *
2866  * Draw several ("n") chars, with an attr, at a given location.
2867  *
2868  * All "graphic" data is handled by "Term_pict_win()", below.
2869  *
2870  * One would think there is a more efficient method for telling a window
2871  * what color it should be using to draw with, but perhaps simply changing
2872  * it every time is not too inefficient.  XXX XXX XXX
2873  */
2874 static errr Term_text_win(int x, int y, int n, byte a, const char *s)
2875 {
2876         term_data *td = (term_data*)(Term->data);
2877         RECT rc;
2878         HDC hdc;
2879
2880 #if 1 /* #ifdef JP */
2881         static HBITMAP  WALL;
2882         static HBRUSH   myBrush, oldBrush;
2883         static HPEN     oldPen;
2884         static bool init_done = FALSE;
2885
2886         if (!init_done){
2887                 WALL = LoadBitmap(hInstance, AppName);
2888                 myBrush = CreatePatternBrush(WALL);
2889                 init_done = TRUE;
2890         }
2891 #endif
2892
2893         /* Total rectangle */
2894         rc.left = x * td->tile_wid + td->size_ow1;
2895         rc.right = rc.left + n * td->tile_wid;
2896         rc.top = y * td->tile_hgt + td->size_oh1;
2897         rc.bottom = rc.top + td->tile_hgt;
2898
2899         /* Acquire DC */
2900         hdc = GetDC(td->w);
2901
2902         /* Background color */
2903         SetBkColor(hdc, RGB(0, 0, 0));
2904
2905         /* Foreground color */
2906         if (colors16)
2907         {
2908                 SetTextColor(hdc, PALETTEINDEX(win_pal[a]));
2909         }
2910         else if (paletted)
2911         {
2912                 SetTextColor(hdc, win_clr[a&0x0F]);
2913         }
2914         else
2915         {
2916                 SetTextColor(hdc, win_clr[a]);
2917         }
2918
2919         /* Use the font */
2920         SelectObject(hdc, td->font_id);
2921         
2922         /* bg */
2923         if (use_bg) SetBkMode(hdc, TRANSPARENT);
2924
2925         /* Bizarre size */
2926         if (td->bizarre ||
2927             (td->tile_hgt != td->font_hgt) ||
2928             (td->tile_wid != td->font_wid))
2929         {
2930                 int i;
2931
2932                 /* Erase complete rectangle */
2933                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2934                 
2935                 /* bg */
2936                 if (use_bg) DrawBG(hdc, &rc);
2937
2938                 /* New rectangle */
2939                 rc.left += ((td->tile_wid - td->font_wid) / 2);
2940                 rc.right = rc.left + td->font_wid;
2941                 rc.top += ((td->tile_hgt - td->font_hgt) / 2);
2942                 rc.bottom = rc.top + td->font_hgt;
2943
2944                 /* Dump each character */
2945                 for (i = 0; i < n; i++)
2946                 {
2947 #ifdef JP
2948                         if (use_bigtile && *(s+i)=="¢£"[0] && *(s+i+1)=="¢£"[1])
2949                         {
2950                                 rc.right += td->font_wid;
2951
2952                                 oldBrush = SelectObject(hdc, myBrush);
2953                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2954
2955                                 /* Dump the wall */
2956                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2957
2958                                 SelectObject(hdc, oldBrush);
2959                                 SelectObject(hdc, oldPen);
2960                                 rc.right -= td->font_wid;
2961
2962                                 /* Advance */
2963                                 i++;
2964                                 rc.left += 2 * td->tile_wid;
2965                                 rc.right += 2 * td->tile_wid;
2966                         }
2967                         else if ( iskanji(*(s+i)) )  /*  £²¥Ð¥¤¥Èʸ»ú  */
2968                         {
2969                                 rc.right += td->font_wid;
2970                                 /* Dump the text */
2971                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2972                                        s+i, 2, NULL);
2973                                 rc.right -= td->font_wid;
2974
2975                                 /* Advance */
2976                                 i++;
2977                                 rc.left += 2 * td->tile_wid;
2978                                 rc.right += 2 * td->tile_wid;
2979                         } else if (*(s+i)==127){
2980                                 oldBrush = SelectObject(hdc, myBrush);
2981                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2982
2983                                 /* Dump the wall */
2984                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2985
2986                                 SelectObject(hdc, oldBrush);
2987                                 SelectObject(hdc, oldPen);
2988
2989                                 /* Advance */
2990                                 rc.left += td->tile_wid;
2991                                 rc.right += td->tile_wid;
2992                         } else {
2993                                 /* Dump the text */
2994                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s+i, 1, NULL);
2995
2996                                 /* Advance */
2997                                 rc.left += td->tile_wid;
2998                                 rc.right += td->tile_wid;
2999                         }
3000 #else
3001 #if 1
3002                         if (*(s+i)==127){
3003                                 oldBrush = SelectObject(hdc, myBrush);
3004                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
3005
3006                                 /* Dump the wall */
3007                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
3008
3009                                 SelectObject(hdc, oldBrush);
3010                                 SelectObject(hdc, oldPen);
3011
3012                                 /* Advance */
3013                                 rc.left += td->tile_wid;
3014                                 rc.right += td->tile_wid;
3015                         } else {
3016                                 /* Dump the text */
3017                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
3018                                        s+i, 1, NULL);
3019
3020                                 /* Advance */
3021                                 rc.left += td->tile_wid;
3022                                 rc.right += td->tile_wid;
3023                         }
3024 #else
3025                         /* Dump the text */
3026                         ExtTextOut(hdc, rc.left, rc.top, 0, &rc,
3027                                    s+i, 1, NULL);
3028
3029                         /* Advance */
3030                         rc.left += td->tile_wid;
3031                         rc.right += td->tile_wid;
3032 #endif
3033 #endif
3034
3035                 }
3036         }
3037
3038         /* Normal size */
3039         else
3040         {
3041                 /* Dump the text */
3042                 ExtTextOut(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc,
3043                            s, n, NULL);
3044         }
3045
3046         /* Release DC */
3047         ReleaseDC(td->w, hdc);
3048
3049         /* Success */
3050         return 0;
3051 }
3052
3053
3054 /*
3055  * Low level graphics.  Assumes valid input.
3056  *
3057  * Draw an array of "special" attr/char pairs at the given location.
3058  *
3059  * We use the "Term_pict_win()" function for "graphic" data, which are
3060  * encoded by setting the "high-bits" of both the "attr" and the "char"
3061  * data.  We use the "attr" to represent the "row" of the main bitmap,
3062  * and the "char" to represent the "col" of the main bitmap.  The use
3063  * of this function is induced by the "higher_pict" flag.
3064  *
3065  * If "graphics" is not available, we simply "wipe" the given grids.
3066  */
3067 static errr Term_pict_win(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
3068 {
3069         term_data *td = (term_data*)(Term->data);
3070
3071 #ifdef USE_GRAPHICS
3072
3073         int i;
3074         int x1, y1, w1, h1, tw1, th1;
3075         int x2, y2, w2, h2, tw2;
3076         int x3, y3;
3077
3078         HDC hdcMask;
3079
3080         HDC hdc;
3081         HDC hdcSrc;
3082         HBITMAP hbmSrcOld;
3083
3084         /* Paranoia */
3085         if (!use_graphics)
3086         {
3087                 /* Erase the grids */
3088                 return (Term_wipe_win(x, y, n));
3089         }
3090
3091         /* Size of bitmap cell */
3092         w1 = infGraph.CellWidth;
3093         h1 = infGraph.CellHeight;
3094         tw1 = infGraph.TileWidth;
3095         th1 = infGraph.TileHeight;
3096
3097         /* Size of window cell */
3098         if (td->map_active)
3099         {
3100                 w2 = td->map_tile_wid;
3101                 h2 = td->map_tile_hgt;
3102         }
3103         else
3104         {
3105                 w2 = td->tile_wid;
3106                 h2 = td->tile_hgt;
3107                 tw2 = w2;
3108
3109                 /* big tile mode */
3110                 if (use_bigtile) tw2 *= 2;
3111         }
3112
3113         /* Location of window cell */
3114         x2 = x * w2 + td->size_ow1 + infGraph.OffsetX;
3115         y2 = y * h2 + td->size_oh1 + infGraph.OffsetY;
3116
3117         /* Info */
3118         hdc = GetDC(td->w);
3119
3120         /* More info */
3121         hdcSrc = CreateCompatibleDC(hdc);
3122         hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
3123
3124         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3125         {
3126                 hdcMask = CreateCompatibleDC(hdc);
3127                 SelectObject(hdcMask, infMask.hBitmap);
3128         }
3129
3130         /* Draw attr/char pairs */
3131         for (i = 0; i < n; i++, x2 += w2)
3132         {
3133                 byte a = ap[i];
3134                 char c = cp[i];
3135
3136
3137                 /* Extract picture */
3138                 int row = (a & 0x7F);
3139                 int col = (c & 0x7F);
3140
3141                 /* Location of bitmap cell */
3142                 x1 = col * w1;
3143                 y1 = row * h1;
3144
3145                 if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3146                 {
3147                         x3 = (tcp[i] & 0x7F) * w1;
3148                         y3 = (tap[i] & 0x7F) * h1;
3149                         tw2 = tw2 * w1 / tw1;
3150                         h2 = h2 * h1 / th1;
3151
3152                         /* Perfect size */
3153                         if ((tw1 == tw2) && (th1 == h2))
3154                         {
3155                                 /* Copy the terrain picture from the bitmap to the window */
3156                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
3157
3158                                 /* Mask out the tile */
3159                                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
3160
3161                                 /* Draw the tile */
3162                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
3163                         }
3164
3165                         /* Need to stretch */
3166                         else
3167                         {
3168                                 /* Set the correct mode for stretching the tiles */
3169                                 SetStretchBltMode(hdc, COLORONCOLOR);
3170
3171                                 /* Copy the terrain picture from the bitmap to the window */
3172                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x3, y3, w1, h1, SRCAND);
3173
3174                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCPAINT);
3175
3176                                 /* Only draw if terrain and overlay are different */
3177                                 if ((x1 != x3) || (y1 != y3))
3178                                 {
3179                                         /* Mask out the tile */
3180                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
3181
3182                                         /* Draw the tile */
3183                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
3184                                 }
3185                         }
3186                 }
3187                 else
3188                 {
3189                         /* Perfect size */
3190                         if ((w1 == tw2) && (h1 == h2))
3191                         {
3192                                 /* Copy the picture from the bitmap to the window */
3193                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
3194                         }
3195
3196                         /* Need to stretch */
3197                         else
3198                         {
3199                                 /* Set the correct mode for stretching the tiles */
3200                                 SetStretchBltMode(hdc, COLORONCOLOR);
3201
3202                                 /* Copy the picture from the bitmap to the window */
3203                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
3204                         }
3205                 }
3206         }
3207
3208         /* Release */
3209         SelectObject(hdcSrc, hbmSrcOld);
3210         DeleteDC(hdcSrc);
3211
3212         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3213         {
3214                 /* Release */
3215                 SelectObject(hdcMask, hbmSrcOld);
3216                 DeleteDC(hdcMask);
3217         }
3218
3219         /* Release */
3220         ReleaseDC(td->w, hdc);
3221
3222 #else /* USE_GRAPHICS */
3223
3224         /* Just erase this grid */
3225         return (Term_wipe_win(x, y, n));
3226
3227 #endif /* USE_GRAPHICS */
3228
3229         /* Success */
3230         return 0;
3231 }
3232
3233
3234 static void windows_map(void)
3235 {
3236         term_data *td = &data[0];
3237         byte a, c;
3238         int x, min_x, max_x;
3239         int y, min_y, max_y;
3240
3241         byte ta, tc;
3242
3243         /* Only in graphics mode */
3244         if (!use_graphics) return;
3245
3246         /* Clear screen */
3247         Term_xtra_win_clear();
3248
3249         td->map_tile_wid = (td->tile_wid * td->cols) / MAX_WID;
3250         td->map_tile_hgt = (td->tile_hgt * td->rows) / MAX_HGT;
3251         td->map_active = TRUE;
3252
3253         {
3254                 min_x = 0;
3255                 min_y = 0;
3256                 max_x = cur_wid;
3257                 max_y = cur_hgt;
3258         }
3259
3260         /* Draw the map */
3261         for (x = min_x; x < max_x; x++)
3262         {
3263                 for (y = min_y; y < max_y; y++)
3264                 {
3265                         map_info(y, x, &a, (char*)&c, &ta, (char*)&tc);
3266
3267                         /* Ignore non-graphics */
3268                         if ((a & 0x80) && (c & 0x80))
3269                         {
3270                                 Term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
3271                         }
3272                 }
3273         }
3274
3275         /* Hilite the player */
3276         Term_curs_win(px - min_x, py - min_y);
3277
3278         /* Wait for a keypress, flush key buffer */
3279         Term_inkey(&c, TRUE, TRUE);
3280         Term_flush();
3281
3282         /* Switch off the map display */
3283         td->map_active = FALSE;
3284
3285         /* Restore screen */
3286         Term_xtra_win_clear();
3287         Term_redraw();
3288 }
3289
3290
3291 /*** Other routines ***/
3292
3293
3294 /*
3295  * Create and initialize a "term_data" given a title
3296  */
3297 static void term_data_link(term_data *td)
3298 {
3299         term *t = &td->t;
3300
3301         /* Initialize the term */
3302         term_init(t, td->cols, td->rows, td->keys);
3303
3304         /* Use a "software" cursor */
3305         t->soft_cursor = TRUE;
3306
3307         /* Use "Term_pict" for "graphic" data */
3308         t->higher_pict = TRUE;
3309
3310         /* Erase with "white space" */
3311         t->attr_blank = TERM_WHITE;
3312         t->char_blank = ' ';
3313
3314 #if 0
3315         /* Prepare the init/nuke hooks */
3316         t->init_hook = Term_init_win;
3317         t->nuke_hook = Term_nuke_win;
3318 #endif
3319
3320         /* Prepare the template hooks */
3321         t->user_hook = Term_user_win;
3322         t->xtra_hook = Term_xtra_win;
3323         t->curs_hook = Term_curs_win;
3324         t->bigcurs_hook = Term_bigcurs_win;
3325         t->wipe_hook = Term_wipe_win;
3326         t->text_hook = Term_text_win;
3327         t->pict_hook = Term_pict_win;
3328
3329         /* Remember where we came from */
3330         t->data = (vptr)(td);
3331 }
3332
3333
3334 /*
3335  * Create the windows
3336  *
3337  * First, instantiate the "default" values, then read the "ini_file"
3338  * to over-ride selected values, then create the windows, and fonts.
3339  *
3340  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
3341  * must make active to realize the palette.  XXX XXX XXX
3342  */
3343 static void init_windows(void)
3344 {
3345         int i;
3346
3347         term_data *td;
3348
3349 #if 0 /* #ifndef JP */
3350         char buf[1024];
3351 #endif
3352
3353         /* Main window */
3354         td = &data[0];
3355         WIPE(td, term_data);
3356 #ifdef JP
3357         td->s = "ÊѶòÈÚÅÜ";
3358 #else
3359         td->s = angband_term_name[0];
3360 #endif
3361
3362         td->keys = 1024;
3363         td->rows = 24;
3364         td->cols = 80;
3365         td->visible = TRUE;
3366         td->size_ow1 = 2;
3367         td->size_ow2 = 2;
3368         td->size_oh1 = 2;
3369         td->size_oh2 = 2;
3370         td->pos_x = 7 * 30;
3371         td->pos_y = 7 * 20;
3372         td->posfix = FALSE;
3373 #if 1 /* #ifdef JP */
3374         td->bizarre = TRUE;
3375 #endif
3376         /* Sub windows */
3377         for (i = 1; i < MAX_TERM_DATA; i++)
3378         {
3379                 td = &data[i];
3380                 WIPE(td, term_data);
3381                 td->s = angband_term_name[i];
3382                 td->keys = 16;
3383                 td->rows = 24;
3384                 td->cols = 80;
3385                 td->visible = FALSE;
3386                 td->size_ow1 = 1;
3387                 td->size_ow2 = 1;
3388                 td->size_oh1 = 1;
3389                 td->size_oh2 = 1;
3390                 td->pos_x = (7 - i) * 30;
3391                 td->pos_y = (7 - i) * 20;
3392                 td->posfix = FALSE;
3393 #if 1 /* #ifdef JP */
3394                         td->bizarre = TRUE;
3395 #endif
3396         }
3397
3398
3399         /* Load prefs */
3400         load_prefs();
3401
3402
3403         /* Main window (need these before term_getsize gets called) */
3404         td = &data[0];
3405         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU |
3406                        WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION |
3407                        WS_VISIBLE);
3408         td->dwExStyle = 0;
3409         td->visible = TRUE;
3410
3411         /* Sub windows (need these before term_getsize gets called) */
3412         for (i = 1; i < MAX_TERM_DATA; i++)
3413         {
3414                 td = &data[i];
3415                 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
3416                 td->dwExStyle = (WS_EX_TOOLWINDOW);
3417         }
3418
3419
3420         /* All windows */
3421         for (i = 0; i < MAX_TERM_DATA; i++)
3422         {
3423                 td = &data[i];
3424
3425 #if 1 /* #ifdef JP */
3426                 strncpy(td->lf.lfFaceName, td->font_want, LF_FACESIZE);
3427                 td->lf.lfCharSet = DEFAULT_CHARSET;
3428                 td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
3429                 /* Activate the chosen font */
3430                 term_force_font(td, NULL);
3431                 td->tile_wid = td->font_wid;
3432                 td->tile_hgt = td->font_hgt;
3433 #else
3434                 /* Access the standard font file */
3435                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, td->font_want);
3436
3437                 /* Activate the chosen font */
3438                 if (term_force_font(td, buf))
3439                 {
3440                         /* Access the standard font file */
3441                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
3442
3443                         /* Force the use of that font */
3444                         (void)term_force_font(td, buf);
3445
3446                         /* Oops */
3447                         td->tile_wid = 8;
3448                         td->tile_hgt = 13;
3449
3450                         /* Assume not bizarre */
3451                         td->bizarre = FALSE;
3452                 }
3453 #endif
3454
3455
3456                 /* Analyze the font */
3457                 term_getsize(td);
3458
3459                 /* Resize the window */
3460                 term_window_resize(td);
3461         }
3462
3463
3464         /* Sub windows (reverse order) */
3465         for (i = MAX_TERM_DATA - 1; i >= 1; --i)
3466         {
3467                 td = &data[i];
3468
3469                 my_td = td;
3470                 td->w = CreateWindowEx(td->dwExStyle, AngList,
3471                                        td->s, td->dwStyle,
3472                                        td->pos_x, td->pos_y,
3473                                        td->size_wid, td->size_hgt,
3474                                        HWND_DESKTOP, NULL, hInstance, NULL);
3475                 my_td = NULL;
3476 #ifdef JP
3477                 if (!td->w) quit("¥µ¥Ö¥¦¥£¥ó¥É¥¦¤ËºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
3478 #else
3479                 if (!td->w) quit("Failed to create sub-window");
3480 #endif
3481
3482
3483                 if (td->visible)
3484                 {
3485                         td->size_hack = TRUE;
3486                         ShowWindow(td->w, SW_SHOW);
3487                         td->size_hack = FALSE;
3488                 }
3489
3490                 term_data_link(td);
3491                 angband_term[i] = &td->t;
3492
3493                 if (td->visible)
3494                 {
3495                         /* Activate the window */
3496                         SetActiveWindow(td->w);
3497                 }
3498
3499                 if (data[i].posfix)
3500                 {
3501                         term_window_pos(&data[i], HWND_TOPMOST);
3502                 }
3503                 else
3504                 {
3505                         term_window_pos(&data[i], td->w);
3506                 }
3507         }
3508
3509
3510         /* Main window */
3511         td = &data[0];
3512
3513         /* Main window */
3514         my_td = td;
3515         td->w = CreateWindowEx(td->dwExStyle, AppName,
3516                                td->s, td->dwStyle,
3517                                td->pos_x, td->pos_y,
3518                                td->size_wid, td->size_hgt,
3519                                HWND_DESKTOP, NULL, hInstance, NULL);
3520         my_td = NULL;
3521 #ifdef JP
3522         if (!td->w) quit("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÎºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
3523 #else
3524         if (!td->w) quit("Failed to create Angband window");
3525 #endif
3526
3527
3528         term_data_link(td);
3529         angband_term[0] = &td->t;
3530         normsize.x = td->cols;
3531         normsize.y = td->rows;
3532
3533         /* Activate the main window */
3534         if (win_maximized) ShowWindow(td->w, SW_SHOWMAXIMIZED);
3535         else ShowWindow(td->w, SW_SHOW);
3536
3537         /* Bring main window back to top */
3538         SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
3539
3540
3541         /* New palette XXX XXX XXX */
3542         (void)new_palette();
3543
3544
3545         /* Create a "brush" for drawing the "cursor" */
3546         hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
3547
3548
3549         /* Process pending messages */
3550         (void)Term_xtra_win_flush();
3551 }
3552
3553
3554
3555 /*
3556  * Prepare the menus
3557  */
3558 static void setup_menus(void)
3559 {
3560         int i;
3561
3562         HMENU hm = GetMenu(data[0].w);
3563
3564
3565         /* Menu "File", Disable all */
3566         EnableMenuItem(hm, IDM_FILE_NEW,
3567                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3568         EnableMenuItem(hm, IDM_FILE_OPEN,
3569                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3570         EnableMenuItem(hm, IDM_FILE_SAVE,
3571                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3572         EnableMenuItem(hm, IDM_FILE_EXIT,
3573                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3574         EnableMenuItem(hm, IDM_FILE_SCORE,
3575                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3576
3577
3578         /* No character available */
3579         if (!character_generated)
3580         {
3581                 /* Menu "File", Item "New" */
3582                 EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
3583
3584                 /* Menu "File", Item "Open" */
3585                 EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
3586         }
3587
3588         /* A character available */
3589         if (character_generated)
3590         {
3591                 /* Menu "File", Item "Save" */
3592                 EnableMenuItem(hm, IDM_FILE_SAVE,
3593                            MF_BYCOMMAND | MF_ENABLED);
3594         }
3595
3596         /* Menu "File", Item "Exit" */
3597         EnableMenuItem(hm, IDM_FILE_EXIT,
3598                        MF_BYCOMMAND | MF_ENABLED);
3599
3600         EnableMenuItem(hm, IDM_FILE_SCORE,
3601                        MF_BYCOMMAND | MF_ENABLED);
3602
3603
3604         /* Menu "Window::Visibility" */
3605         for (i = 0; i < MAX_TERM_DATA; i++)
3606         {
3607                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3608                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3609
3610                 CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3611                               (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
3612
3613                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3614                                MF_BYCOMMAND | MF_ENABLED);
3615         }
3616
3617         /* Menu "Window::Font" */
3618         for (i = 0; i < MAX_TERM_DATA; i++)
3619         {
3620                 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3621                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3622
3623                 if (data[i].visible)
3624                 {
3625                         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3626                                        MF_BYCOMMAND | MF_ENABLED);
3627                 }
3628         }
3629
3630         /* Menu "Window::Window Position Fix" */
3631         for (i = 0; i < MAX_TERM_DATA; i++)
3632         {
3633                 EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3634                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3635
3636                 CheckMenuItem(hm, IDM_WINDOW_POS_0 + i,
3637                               (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
3638
3639                 if (data[i].visible)
3640                 {
3641                         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3642                                        MF_BYCOMMAND | MF_ENABLED);
3643                 }
3644         }
3645
3646         /* Menu "Window::Bizarre Display" */
3647         for (i = 0; i < MAX_TERM_DATA; i++)
3648         {
3649                 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3650                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3651
3652                 CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3653                               (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
3654
3655                 if (data[i].visible)
3656                 {
3657                         EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3658                                    MF_BYCOMMAND | MF_ENABLED);
3659
3660                 }
3661         }
3662
3663         /* Menu "Window::Increase Tile Width" */
3664         for (i = 0; i < MAX_TERM_DATA; i++)
3665         {
3666                 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3667                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3668
3669                 if (data[i].visible)
3670                 {
3671                         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3672                                    MF_BYCOMMAND | MF_ENABLED);
3673
3674                 }
3675         }
3676
3677         /* Menu "Window::Decrease Tile Width" */
3678         for (i = 0; i < MAX_TERM_DATA; i++)
3679         {
3680                 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3681                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3682
3683                 if (data[i].visible)
3684                 {
3685                         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3686                                    MF_BYCOMMAND | MF_ENABLED);
3687
3688                 }
3689         }
3690
3691         /* Menu "Window::Increase Tile Height" */
3692         for (i = 0; i < MAX_TERM_DATA; i++)
3693         {
3694                 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3695                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3696
3697                 if (data[i].visible)
3698                 {
3699                         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3700                                    MF_BYCOMMAND | MF_ENABLED);
3701
3702                 }
3703         }
3704
3705         /* Menu "Window::Decrease Tile Height" */
3706         for (i = 0; i < MAX_TERM_DATA; i++)
3707         {
3708                 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3709                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3710
3711                 if (data[i].visible)
3712                 {
3713                         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3714                                    MF_BYCOMMAND | MF_ENABLED);
3715
3716                 }
3717         }
3718
3719         /* Menu "Options", disable all */
3720         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3721                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3722         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3723                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3724         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3725                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3726         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE,
3727                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3728         EnableMenuItem(hm, IDM_OPTIONS_SOUND,
3729                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3730 #ifndef JP
3731         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3732                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3733 #endif
3734
3735         /* Menu "Options", Item "Map" */
3736         if (use_graphics != GRAPHICS_NONE)
3737                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_ENABLED);
3738         else
3739                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3740                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3741
3742         /* Menu "Options", update all */
3743         CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3744                       (arg_graphics == GRAPHICS_NONE ? MF_CHECKED : MF_UNCHECKED));
3745         CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3746                       (arg_graphics == GRAPHICS_ORIGINAL ? MF_CHECKED : MF_UNCHECKED));
3747         CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3748                       (arg_graphics == GRAPHICS_ADAM_BOLT ? MF_CHECKED : MF_UNCHECKED));
3749         CheckMenuItem(hm, IDM_OPTIONS_NEW2_GRAPHICS,
3750                       (arg_graphics == GRAPHICS_HENGBAND ? MF_CHECKED : MF_UNCHECKED));
3751         CheckMenuItem(hm, IDM_OPTIONS_BIGTILE,
3752                       (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
3753         CheckMenuItem(hm, IDM_OPTIONS_MUSIC,
3754                       (arg_music ? MF_CHECKED : MF_UNCHECKED));
3755         CheckMenuItem(hm, IDM_OPTIONS_SOUND,
3756                       (arg_sound ? MF_CHECKED : MF_UNCHECKED));
3757         CheckMenuItem(hm, IDM_OPTIONS_BG,
3758                       (use_bg ? MF_CHECKED : MF_UNCHECKED));
3759 #ifndef JP
3760         CheckMenuItem(hm, IDM_OPTIONS_SAVER,
3761                       (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
3762 #endif
3763
3764 #ifdef USE_GRAPHICS
3765         /* Menu "Options", Item "Graphics" */
3766         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_ENABLED);
3767         /* Menu "Options", Item "Graphics" */
3768         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_ENABLED);
3769         /* Menu "Options", Item "Graphics" */
3770         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_ENABLED);
3771         /* Menu "Options", Item "Graphics" */
3772         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_ENABLED);
3773 #endif /* USE_GRAPHICS */
3774
3775 #ifdef USE_SOUND
3776         /* Menu "Options", Item "Sound" */
3777         EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_ENABLED);
3778 #endif /* USE_SOUND */
3779
3780 #ifdef USE_SAVER
3781         /* Menu "Options", Item "ScreenSaver" */
3782         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3783                        MF_BYCOMMAND | MF_ENABLED);
3784 #endif /* USE_SAVER */
3785 }
3786
3787
3788 /*
3789  * Check for double clicked (or dragged) savefile
3790  *
3791  * Apparently, Windows copies the entire filename into the first
3792  * piece of the "command line string".  Perhaps we should extract
3793  * the "basename" of that filename and append it to the "save" dir.
3794  */
3795 static void check_for_save_file(LPSTR cmd_line)
3796 {
3797         char *s;
3798
3799         /* First arg */
3800         s = cmd_line;
3801
3802         /* No args */
3803         if (!*s) return;
3804
3805         /* Extract filename */
3806         strcat(savefile, s);
3807
3808         /* Validate the file */
3809         validate_file(savefile);
3810
3811         /* Game in progress */
3812         game_in_progress = TRUE;
3813
3814         /* Play game */
3815         play_game(FALSE);
3816 }
3817
3818
3819 /*
3820  * Process a menu command
3821  */
3822 static void process_menus(WORD wCmd)
3823 {
3824         int i;
3825
3826         term_data *td;
3827
3828         OPENFILENAME ofn;
3829
3830         /* Analyze */
3831         switch (wCmd)
3832         {
3833                 /* New game */
3834                 case IDM_FILE_NEW:
3835                 {
3836                         if (!initialized)
3837                         {
3838 #ifdef JP
3839                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3840 #else
3841                                 plog("You cannot do that yet...");
3842 #endif
3843
3844                         }
3845                         else if (game_in_progress)
3846                         {
3847 #ifdef JP
3848                                 plog("¥×¥ì¥¤Ãæ¤Ï¿·¤·¤¤¥²¡¼¥à¤ò»Ï¤á¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3849 #else
3850                                 plog("You can't start a new game while you're still playing!");
3851 #endif
3852
3853                         }
3854                         else
3855                         {
3856                                 game_in_progress = TRUE;
3857                                 Term_flush();
3858                                 play_game(TRUE);
3859                                 quit(NULL);
3860                         }
3861                         break;
3862                 }
3863
3864                 /* Open game */
3865                 case IDM_FILE_OPEN:
3866                 {
3867                         if (!initialized)
3868                         {
3869 #ifdef JP
3870                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3871 #else
3872                                 plog("You cannot do that yet...");
3873 #endif
3874
3875                         }
3876                         else if (game_in_progress)
3877                         {
3878 #ifdef JP
3879                                 plog("¥×¥ì¥¤Ãæ¤Ï¥²¡¼¥à¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3880 #else
3881                                 plog("You can't open a new game while you're still playing!");
3882 #endif
3883
3884                         }
3885                         else
3886                         {
3887                                 memset(&ofn, 0, sizeof(ofn));
3888                                 ofn.lStructSize = sizeof(ofn);
3889                                 ofn.hwndOwner = data[0].w;
3890                                 ofn.lpstrFilter = "Save Files (*.)\0*\0";
3891                                 ofn.nFilterIndex = 1;
3892                                 ofn.lpstrFile = savefile;
3893                                 ofn.nMaxFile = 1024;
3894                                 ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
3895                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
3896
3897                                 if (GetOpenFileName(&ofn))
3898                                 {
3899                                         /* Load 'savefile' */
3900                                         validate_file(savefile);
3901                                         game_in_progress = TRUE;
3902                                         Term_flush();
3903                                         play_game(FALSE);
3904                                         quit(NULL);
3905                                 }
3906                         }
3907                         break;
3908                 }
3909
3910                 /* Save game */
3911                 case IDM_FILE_SAVE:
3912                 {
3913                         if (game_in_progress && character_generated)
3914                         {
3915                                 /* Paranoia */
3916                                 if (!can_save)
3917                                 {
3918 #ifdef JP
3919                                         plog("º£¤Ï¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3920 #else
3921                                         plog("You may not do that right now.");
3922 #endif
3923
3924                                         break;
3925                                 }
3926
3927                                 /* Hack -- Forget messages */
3928                                 msg_flag = FALSE;
3929
3930                                 /* Save the game */
3931 #ifdef ZANGBAND
3932                                 do_cmd_save_game(FALSE);
3933 #else /* ZANGBAND */
3934                                 do_cmd_save_game();
3935 #endif /* ZANGBAND */
3936                         }
3937                         else
3938                         {
3939 #ifdef JP
3940                                 plog("º£¡¢¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3941 #else
3942                                 plog("You may not do that right now.");
3943 #endif
3944
3945                         }
3946                         break;
3947                 }
3948
3949                 /* Exit */
3950                 case IDM_FILE_EXIT:
3951                 {
3952                         if (game_in_progress && character_generated)
3953                         {
3954                                 /* Paranoia */
3955                                 if (!can_save)
3956                                 {
3957 #ifdef JP
3958                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
3959 #else
3960                                         plog("You may not do that right now.");
3961 #endif
3962
3963                                         break;
3964                                 }
3965
3966                                 /* Hack -- Forget messages */
3967                                 msg_flag = FALSE;
3968
3969                                 forget_lite();
3970                                 forget_view();
3971                                 clear_mon_lite();
3972
3973                                 /* Save the game */
3974 #ifdef ZANGBAND
3975                                 /* do_cmd_save_game(FALSE); */
3976 #else /* ZANGBAND */
3977                                 /* do_cmd_save_game(); */
3978 #endif /* ZANGBAND */
3979                                 Term_key_push(SPECIAL_KEY_QUIT);
3980                                 break;
3981                         }
3982                         quit(NULL);
3983                         break;
3984                 }
3985
3986                 /* Show scores */
3987                 case IDM_FILE_SCORE:
3988                 {
3989                         char buf[1024];
3990
3991                         /* Build the filename */
3992                         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
3993
3994                         /* Open the binary high score file, for reading */
3995                         highscore_fd = fd_open(buf, O_RDONLY);
3996
3997                         /* Paranoia -- No score file */
3998                         if (highscore_fd < 0)
3999                         {
4000                                 msg_print("Score file unavailable.");
4001                         }
4002                         else
4003                         {
4004                                 /* Save Screen */
4005                                 screen_save();
4006
4007                                 /* Clear screen */
4008                                 Term_clear();
4009
4010                                 /* Display the scores */
4011                                 display_scores_aux(0, MAX_HISCORES, -1, NULL);
4012
4013                                 /* Shut the high score file */
4014                                 (void)fd_close(highscore_fd);
4015
4016                                 /* Forget the high score fd */
4017                                 highscore_fd = -1;
4018
4019                                 /* Load screen */
4020                                 screen_load();
4021
4022                                 /* Hack - Flush it */
4023                                 Term_fresh();
4024                         }
4025
4026                         break;
4027                 }
4028
4029                 /* Open game */
4030                 case IDM_FILE_MOVIE:
4031                 {
4032                         if (!initialized)
4033                         {
4034 #ifdef JP
4035                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
4036 #else
4037                                 plog("You cannot do that yet...");
4038 #endif
4039                         }
4040                         else if (game_in_progress)
4041                         {
4042 #ifdef JP
4043                                 plog("¥×¥ì¥¤Ãæ¤Ï¥à¡¼¥Ó¡¼¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
4044 #else
4045                                 plog("You can't open a movie while you're playing!");
4046 #endif
4047                         }
4048                         else
4049                         {
4050                                 memset(&ofn, 0, sizeof(ofn));
4051                                 ofn.lStructSize = sizeof(ofn);
4052                                 ofn.hwndOwner = data[0].w;
4053                                 ofn.lpstrFilter = "Angband Movie Files (*.amv)\0*.amv\0";
4054                                 ofn.nFilterIndex = 1;
4055                                 ofn.lpstrFile = savefile;
4056                                 ofn.nMaxFile = 1024;
4057                                 ofn.lpstrInitialDir = ANGBAND_DIR_USER;
4058                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
4059
4060                                 if (GetOpenFileName(&ofn))
4061                                 {
4062                                         /* Load 'savefile' */
4063                                         prepare_browse_movie_aux(savefile);
4064                                         play_game(FALSE);
4065                                         quit(NULL);
4066                                         return;
4067                                 }
4068                         }
4069                         break;
4070                 }
4071
4072
4073                 case IDM_WINDOW_VIS_0:
4074                 {
4075 #ifdef JP
4076                         plog("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÏÈóɽ¼¨¤Ë¤Ç¤­¤Þ¤»¤ó¡ª");
4077 #else
4078                         plog("You are not allowed to do that!");
4079 #endif
4080
4081
4082                         break;
4083                 }
4084
4085                 /* Window visibility */
4086                 case IDM_WINDOW_VIS_1:
4087                 case IDM_WINDOW_VIS_2:
4088                 case IDM_WINDOW_VIS_3:
4089                 case IDM_WINDOW_VIS_4:
4090                 case IDM_WINDOW_VIS_5:
4091                 case IDM_WINDOW_VIS_6:
4092                 case IDM_WINDOW_VIS_7:
4093                 {
4094                         i = wCmd - IDM_WINDOW_VIS_0;
4095
4096                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4097
4098                         td = &data[i];
4099
4100                         if (!td->visible)
4101                         {
4102                                 td->visible = TRUE;
4103                                 ShowWindow(td->w, SW_SHOW);
4104                                 term_data_redraw(td);
4105                         }
4106                         else
4107                         {
4108                                 td->visible = FALSE;
4109                                 td->posfix = FALSE;
4110                                 ShowWindow(td->w, SW_HIDE);
4111                         }
4112
4113                         break;
4114                 }
4115
4116                 /* Window fonts */
4117                 case IDM_WINDOW_FONT_0:
4118                 case IDM_WINDOW_FONT_1:
4119                 case IDM_WINDOW_FONT_2:
4120                 case IDM_WINDOW_FONT_3:
4121                 case IDM_WINDOW_FONT_4:
4122                 case IDM_WINDOW_FONT_5:
4123                 case IDM_WINDOW_FONT_6:
4124                 case IDM_WINDOW_FONT_7:
4125                 {
4126                         i = wCmd - IDM_WINDOW_FONT_0;
4127
4128                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4129
4130                         td = &data[i];
4131
4132                         term_change_font(td);
4133
4134                         break;
4135                 }
4136
4137                 /* Window Z Position */
4138                 case IDM_WINDOW_POS_1:
4139                 case IDM_WINDOW_POS_2:
4140                 case IDM_WINDOW_POS_3:
4141                 case IDM_WINDOW_POS_4:
4142                 case IDM_WINDOW_POS_5:
4143                 case IDM_WINDOW_POS_6:
4144                 case IDM_WINDOW_POS_7:
4145                 {
4146                         i = wCmd - IDM_WINDOW_POS_0;
4147
4148                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4149
4150                         td = &data[i];
4151
4152                         if (!td->posfix && td->visible)
4153                         {
4154                                 td->posfix = TRUE;
4155                                 term_window_pos(td, HWND_TOPMOST);
4156                         }
4157                         else
4158                         {
4159                                 td->posfix = FALSE;
4160                                 term_window_pos(td, data[0].w);
4161                         }
4162
4163                         break;
4164                 }
4165
4166                 /* Bizarre Display */
4167                 case IDM_WINDOW_BIZ_0:
4168                 case IDM_WINDOW_BIZ_1:
4169                 case IDM_WINDOW_BIZ_2:
4170                 case IDM_WINDOW_BIZ_3:
4171                 case IDM_WINDOW_BIZ_4:
4172                 case IDM_WINDOW_BIZ_5:
4173                 case IDM_WINDOW_BIZ_6:
4174                 case IDM_WINDOW_BIZ_7:
4175                 {
4176                         i = wCmd - IDM_WINDOW_BIZ_0;
4177
4178                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4179
4180                         td = &data[i];
4181
4182                         td->bizarre = !td->bizarre;
4183
4184                         term_getsize(td);
4185
4186                         term_window_resize(td);
4187
4188                         break;
4189                 }
4190
4191                 /* Increase Tile Width */
4192                 case IDM_WINDOW_I_WID_0:
4193                 case IDM_WINDOW_I_WID_1:
4194                 case IDM_WINDOW_I_WID_2:
4195                 case IDM_WINDOW_I_WID_3:
4196                 case IDM_WINDOW_I_WID_4:
4197                 case IDM_WINDOW_I_WID_5:
4198                 case IDM_WINDOW_I_WID_6:
4199                 case IDM_WINDOW_I_WID_7:
4200                 {
4201                         i = wCmd - IDM_WINDOW_I_WID_0;
4202
4203                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4204
4205                         td = &data[i];
4206
4207                         td->tile_wid += 1;
4208
4209                         term_getsize(td);
4210
4211                         term_window_resize(td);
4212
4213                         break;
4214                 }
4215
4216                 /* Decrease Tile Height */
4217                 case IDM_WINDOW_D_WID_0:
4218                 case IDM_WINDOW_D_WID_1:
4219                 case IDM_WINDOW_D_WID_2:
4220                 case IDM_WINDOW_D_WID_3:
4221                 case IDM_WINDOW_D_WID_4:
4222                 case IDM_WINDOW_D_WID_5:
4223                 case IDM_WINDOW_D_WID_6:
4224                 case IDM_WINDOW_D_WID_7:
4225                 {
4226                         i = wCmd - IDM_WINDOW_D_WID_0;
4227
4228                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4229
4230                         td = &data[i];
4231
4232                         td->tile_wid -= 1;
4233
4234                         term_getsize(td);
4235
4236                         term_window_resize(td);
4237
4238                         break;
4239                 }
4240
4241                 /* Increase Tile Height */
4242                 case IDM_WINDOW_I_HGT_0:
4243                 case IDM_WINDOW_I_HGT_1:
4244                 case IDM_WINDOW_I_HGT_2:
4245                 case IDM_WINDOW_I_HGT_3:
4246                 case IDM_WINDOW_I_HGT_4:
4247                 case IDM_WINDOW_I_HGT_5:
4248                 case IDM_WINDOW_I_HGT_6:
4249                 case IDM_WINDOW_I_HGT_7:
4250                 {
4251                         i = wCmd - IDM_WINDOW_I_HGT_0;
4252
4253                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4254
4255                         td = &data[i];
4256
4257                         td->tile_hgt += 1;
4258
4259                         term_getsize(td);
4260
4261                         term_window_resize(td);
4262
4263                         break;
4264                 }
4265
4266                 /* Decrease Tile Height */
4267                 case IDM_WINDOW_D_HGT_0:
4268                 case IDM_WINDOW_D_HGT_1:
4269                 case IDM_WINDOW_D_HGT_2:
4270                 case IDM_WINDOW_D_HGT_3:
4271                 case IDM_WINDOW_D_HGT_4:
4272                 case IDM_WINDOW_D_HGT_5:
4273                 case IDM_WINDOW_D_HGT_6:
4274                 case IDM_WINDOW_D_HGT_7:
4275                 {
4276                         i = wCmd - IDM_WINDOW_D_HGT_0;
4277
4278                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4279
4280                         td = &data[i];
4281
4282                         td->tile_hgt -= 1;
4283
4284                         term_getsize(td);
4285
4286                         term_window_resize(td);
4287
4288                         break;
4289                 }
4290
4291                 case IDM_OPTIONS_NO_GRAPHICS:
4292                 {
4293                         /* Paranoia */
4294                         if (!inkey_flag)
4295                         {
4296                                 plog("You may not do that right now.");
4297                                 break;
4298                         }
4299
4300                         /* Toggle "arg_graphics" */
4301                         if (arg_graphics != GRAPHICS_NONE)
4302                         {
4303                                 arg_graphics = GRAPHICS_NONE;
4304
4305                                 /* React to changes */
4306                                 Term_xtra_win_react();
4307
4308                                 /* Hack -- Force redraw */
4309                                 Term_key_push(KTRL('R'));
4310                         }
4311
4312                         break;
4313                 }
4314
4315                 case IDM_OPTIONS_OLD_GRAPHICS:
4316                 {
4317                         /* Paranoia */
4318                         if (!inkey_flag)
4319                         {
4320                                 plog("You may not do that right now.");
4321                                 break;
4322                         }
4323
4324                         /* Toggle "arg_graphics" */
4325                         if (arg_graphics != GRAPHICS_ORIGINAL)
4326                         {
4327                                 arg_graphics = GRAPHICS_ORIGINAL;
4328
4329                                 /* React to changes */
4330                                 Term_xtra_win_react();
4331
4332                                 /* Hack -- Force redraw */
4333                                 Term_key_push(KTRL('R'));
4334                         }
4335
4336                         break;
4337                 }
4338
4339                 case IDM_OPTIONS_NEW_GRAPHICS:
4340                 {
4341                         /* Paranoia */
4342                         if (!inkey_flag)
4343                         {
4344                                 plog("You may not do that right now.");
4345                                 break;
4346                         }
4347
4348                         /* Toggle "arg_graphics" */
4349                         if (arg_graphics != GRAPHICS_ADAM_BOLT)
4350                         {
4351                                 arg_graphics = GRAPHICS_ADAM_BOLT;
4352
4353                                 /* React to changes */
4354                                 Term_xtra_win_react();
4355
4356                                 /* Hack -- Force redraw */
4357                                 Term_key_push(KTRL('R'));
4358                         }
4359
4360                         break;
4361                 }
4362
4363                 case IDM_OPTIONS_NEW2_GRAPHICS:
4364                 {
4365                         /* Paranoia */
4366                         if (!inkey_flag)
4367                         {
4368                                 plog("You may not do that right now.");
4369                                 break;
4370                         }
4371
4372                         /* Toggle "arg_graphics" */
4373                         if (arg_graphics != GRAPHICS_HENGBAND)
4374                         {
4375                                 arg_graphics = GRAPHICS_HENGBAND;
4376
4377                                 /* React to changes */
4378                                 Term_xtra_win_react();
4379
4380                                 /* Hack -- Force redraw */
4381                                 Term_key_push(KTRL('R'));
4382                         }
4383
4384                         break;
4385                 }
4386
4387                 case IDM_OPTIONS_BIGTILE:
4388                 {
4389                         term_data *td = &data[0];
4390
4391                         /* Paranoia */
4392                         if (!inkey_flag)
4393                         {
4394                                 plog("You may not do that right now.");
4395                                 break;
4396                         }
4397
4398                         /* Toggle "arg_sound" */
4399                         arg_bigtile = !arg_bigtile;
4400
4401                         /* Activate */
4402                         Term_activate(&td->t);
4403
4404                         /* Resize the term */
4405                         Term_resize(td->cols, td->rows);
4406
4407                         /* Redraw later */
4408                         InvalidateRect(td->w, NULL, TRUE);
4409
4410                         break;
4411                 }
4412
4413                 case IDM_OPTIONS_MUSIC:
4414                 {
4415                         /* Paranoia */
4416                         if (!inkey_flag)
4417                         {
4418                                 plog("You may not do that right now.");
4419                                 break;
4420                         }
4421
4422                         /* Toggle "arg_sound" */
4423                         arg_music = !arg_music;
4424
4425                         /* React to changes */
4426                         Term_xtra_win_react();
4427
4428                         /* Hack -- Force redraw */
4429                         Term_key_push(KTRL('R'));
4430
4431                         break;
4432                 }
4433
4434                 case IDM_OPTIONS_SOUND:
4435                 {
4436                         /* Paranoia */
4437                         if (!inkey_flag)
4438                         {
4439                                 plog("You may not do that right now.");
4440                                 break;
4441                         }
4442
4443                         /* Toggle "arg_sound" */
4444                         arg_sound = !arg_sound;
4445
4446                         /* React to changes */
4447                         Term_xtra_win_react();
4448
4449                         /* Hack -- Force redraw */
4450                         Term_key_push(KTRL('R'));
4451
4452                         break;
4453                 }
4454
4455                 /* bg */
4456                 case IDM_OPTIONS_BG:
4457                 {
4458                         /* Paranoia */
4459                         if (!inkey_flag)
4460                         {
4461                                 plog("You may not do that right now.");
4462                                 break;
4463                         }
4464
4465                         /* Toggle "use_bg" */
4466                         use_bg = !use_bg;
4467
4468                         init_bg();
4469
4470                         /* React to changes */
4471                         Term_xtra_win_react();
4472
4473                         /* Hack -- Force redraw */
4474                         Term_key_push(KTRL('R'));
4475
4476                         break;
4477                 }
4478
4479                 /* bg */
4480                 case IDM_OPTIONS_OPEN_BG:
4481                 {
4482                         /* Paranoia */
4483                         if (!inkey_flag)
4484                         {
4485                                 plog("You may not do that right now.");
4486                                 break;
4487                         }
4488                         else
4489                         {
4490                                 memset(&ofn, 0, sizeof(ofn));
4491                                 ofn.lStructSize = sizeof(ofn);
4492                                 ofn.hwndOwner = data[0].w;
4493                                 ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0";
4494                                 ofn.nFilterIndex = 1;
4495                                 ofn.lpstrFile = bg_bitmap_file;
4496                                 ofn.nMaxFile = 1023;
4497                                 ofn.lpstrInitialDir = NULL;
4498 #ifdef JP
4499                                 ofn.lpstrTitle = "ÊÉ»æ¤òÁª¤ó¤Ç¤Í¡£";
4500 #else
4501                                 ofn.lpstrTitle = "Choose wall paper.";
4502 #endif
4503                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
4504
4505                                 if (GetOpenFileName(&ofn))
4506                                 {
4507                                         /* Load 'savefile' */
4508                                         use_bg = 1;
4509                                         init_bg();
4510                                 }
4511
4512                                 /* React to changes */
4513                                 Term_xtra_win_react();
4514
4515                                 /* Hack -- Force redraw */
4516                                 Term_key_push(KTRL('R'));
4517                         }
4518                         break;
4519                 }
4520
4521                 case IDM_DUMP_SCREEN_HTML:
4522                 {
4523                         static char buf[1024] = "";
4524                         memset(&ofn, 0, sizeof(ofn));
4525                         ofn.lStructSize = sizeof(ofn);
4526                         ofn.hwndOwner = data[0].w;
4527                         ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0";
4528                         ofn.nFilterIndex = 1;
4529                         ofn.lpstrFile = buf;
4530                         ofn.nMaxFile = 1023;
4531                         ofn.lpstrDefExt = "html";
4532                         ofn.lpstrInitialDir = NULL;
4533 #ifdef JP
4534                         ofn.lpstrTitle = "HTML¤Ç¥¹¥¯¥ê¡¼¥ó¥À¥ó¥×¤òÊݸ";
4535 #else
4536                         ofn.lpstrTitle = "Save screen dump as HTML.";
4537 #endif
4538                         ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
4539
4540                         if (GetSaveFileName(&ofn))
4541                         {
4542                                 do_cmd_save_screen_html_aux(buf, 0);
4543                         }
4544                         break;
4545                 }
4546
4547 #ifdef USE_SAVER
4548
4549                 case IDM_OPTIONS_SAVER:
4550                 {
4551                         if (hwndSaver)
4552                         {
4553                                 DestroyWindow(hwndSaver);
4554                                 hwndSaver = NULL;
4555                         }
4556                         else
4557                         {
4558                                 /* Create a screen scaver window */
4559                                 hwndSaver = CreateWindowEx(WS_EX_TOPMOST, "WindowsScreenSaverClass",
4560                                                            "Angband Screensaver",
4561                                                            WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4562                                                            0, 0, GetSystemMetrics(SM_CXSCREEN),
4563                                                            GetSystemMetrics(SM_CYSCREEN),
4564                                                            NULL, NULL, hInstance, NULL);
4565
4566                                 if (hwndSaver)
4567                                 {
4568                                         /* Push the window to the bottom XXX XXX XXX */
4569                                         SetWindowPos(hwndSaver, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
4570                                 }
4571                                 else
4572                                 {
4573 #ifdef JP
4574                                         plog("¥¦¥£¥ó¥É¥¦¤òºîÀ®½ÐÍè¤Þ¤»¤ó");
4575 #else
4576                                         plog("Failed to create saver window");
4577 #endif
4578
4579                                 }
4580                         }
4581                         break;
4582                 }
4583
4584 #endif
4585
4586                 case IDM_OPTIONS_MAP:
4587                 {
4588                         windows_map();
4589                         break;
4590                 }
4591
4592                 case IDM_HELP_CONTENTS:
4593                 {
4594 #ifdef HTML_HELP
4595                         char tmp[1024];
4596                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.chm");
4597                         if (check_file(tmp))
4598                         {
4599                                 HtmlHelp(data[0].w, tmp, HH_DISPLAY_TOPIC, 0);
4600                         }
4601                         else
4602                         {
4603 #ifdef JP
4604                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
4605                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4606 #else
4607                                 plog_fmt("Cannot find help file: %s", tmp);
4608                                 plog("Use the online help files instead.");
4609 #endif
4610
4611                         }
4612                         break;
4613 #else /* HTML_HELP */
4614                         char buf[1024];
4615                         char tmp[1024];
4616                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.hlp");
4617                         if (check_file(tmp))
4618                         {
4619                                 sprintf(buf, "winhelp.exe %s", tmp);
4620                                 WinExec(buf, SW_NORMAL);
4621                         }
4622                         else
4623                         {
4624 #ifdef JP
4625                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
4626                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4627 #else
4628                                 plog_fmt("Cannot find help file: %s", tmp);
4629                                 plog("Use the online help files instead.");
4630 #endif
4631
4632                         }
4633                         break;
4634 #endif /* HTML_HELP */
4635                 }
4636         }
4637 }
4638
4639
4640 static bool process_keydown(WPARAM wParam, LPARAM lParam)
4641 {
4642         int i;
4643         bool mc = FALSE;
4644         bool ms = FALSE;
4645         bool ma = FALSE;
4646
4647         /* Extract the modifiers */
4648         if (GetKeyState(VK_CONTROL) & 0x8000) mc = TRUE;
4649         if (GetKeyState(VK_SHIFT)   & 0x8000) ms = TRUE;
4650         if (GetKeyState(VK_MENU)    & 0x8000) ma = TRUE;
4651
4652         Term_no_press = (ma) ? TRUE : FALSE;
4653
4654         /* Handle "special" keys */
4655         if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)]) )
4656         {
4657                 bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
4658                 bool numpad = FALSE;
4659
4660                 /* Begin the macro trigger */
4661                 Term_keypress(31);
4662
4663                 /* Send the modifiers */
4664                 if (mc) Term_keypress('C');
4665                 if (ms) Term_keypress('S');
4666                 if (ma) Term_keypress('A');
4667
4668                 /* Extract "scan code" */
4669                 i = LOBYTE(HIWORD(lParam));
4670
4671                 /* Introduce the scan code */
4672                 Term_keypress('x');
4673
4674                 /* Extended key bit */
4675                 switch (wParam)
4676                 {
4677                         /* Numpad Enter and '/' are extended key */
4678                 case VK_DIVIDE:
4679                         Term_no_press = TRUE;
4680                 case VK_RETURN: /* Enter */
4681                         numpad = ext_key;
4682                         break;
4683                         /* Other extended keys are on full keyboard */
4684                 case VK_NUMPAD0:
4685                 case VK_NUMPAD1:
4686                 case VK_NUMPAD2:
4687                 case VK_NUMPAD3:
4688                 case VK_NUMPAD4:
4689                 case VK_NUMPAD5:
4690                 case VK_NUMPAD6:
4691                 case VK_NUMPAD7:
4692                 case VK_NUMPAD8:
4693                 case VK_NUMPAD9:
4694                 case VK_ADD:
4695                 case VK_MULTIPLY:
4696                 case VK_SUBTRACT:
4697                 case VK_SEPARATOR:
4698                 case VK_DECIMAL:
4699                         Term_no_press = TRUE;
4700                 case VK_CLEAR:
4701                 case VK_HOME:
4702                 case VK_END:
4703                 case VK_PRIOR:  /* Page Up */
4704                 case VK_NEXT:   /* Page Down */
4705                 case VK_INSERT:
4706                 case VK_DELETE:
4707                 case VK_UP:
4708                 case VK_DOWN:
4709                 case VK_LEFT:
4710                 case VK_RIGHT:
4711                         numpad = !ext_key;
4712                 }
4713
4714                 /* Special modifiers for keypad keys */
4715                 if (numpad) Term_keypress('K');
4716
4717                 /* Encode the hexidecimal scan code */
4718                 Term_keypress(hexsym[i/16]);
4719                 Term_keypress(hexsym[i%16]);
4720
4721                 /* End the macro trigger */
4722                 Term_keypress(13);
4723
4724                 return 1;
4725         }
4726
4727         return 0;
4728 }
4729
4730
4731 #ifdef __MWERKS__
4732 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4733                                   WPARAM wParam, LPARAM lParam);
4734 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4735                                   WPARAM wParam, LPARAM lParam)
4736 #else /* __MWERKS__ */
4737 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4738                                           WPARAM wParam, LPARAM lParam)
4739 #endif /* __MWERKS__ */
4740 {
4741         PAINTSTRUCT ps;
4742         HDC hdc;
4743         term_data *td;
4744 #if 0
4745         MINMAXINFO FAR *lpmmi;
4746         RECT rc;
4747 #endif
4748         int i;
4749
4750
4751         /* Acquire proper "term_data" info */
4752         td = (term_data *)GetWindowLong(hWnd, 0);
4753
4754         /* Handle message */
4755         switch (uMsg)
4756         {
4757                 /* XXX XXX XXX */
4758                 case WM_NCCREATE:
4759                 {
4760                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4761                         break;
4762                 }
4763
4764                 /* XXX XXX XXX */
4765                 case WM_CREATE:
4766                 {
4767 #ifdef USE_MUSIC
4768                         mop.dwCallback=(DWORD)hWnd;
4769 #endif
4770                         return 0;
4771                 }
4772
4773                 case WM_GETMINMAXINFO:
4774                 {
4775                         MINMAXINFO FAR *lpmmi;
4776                         RECT rc;
4777
4778                         lpmmi = (MINMAXINFO FAR *)lParam;
4779
4780                         /* this message was sent before WM_NCCREATE */
4781                         if (!td) return 1;
4782
4783                         /* Minimum window size is 80x24 */
4784                         rc.left = rc.top = 0;
4785                         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
4786                         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4787
4788                         /* Adjust */
4789                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4790
4791                         /* Save minimum size */
4792                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4793                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4794
4795                         return 0;
4796                 }
4797
4798                 case WM_PAINT:
4799                 {
4800                         BeginPaint(hWnd, &ps);
4801                         if (td) term_data_redraw(td);
4802                         EndPaint(hWnd, &ps);
4803                         ValidateRect(hWnd, NULL);
4804                         return 0;
4805                 }
4806
4807 #ifdef USE_MUSIC
4808                 case MM_MCINOTIFY:
4809                 {
4810                         if(wParam == MCI_NOTIFY_SUCCESSFUL)
4811                         {
4812                                 mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
4813                                 mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
4814                         }
4815                         return 0;
4816                 }
4817 #endif
4818
4819                 case WM_SYSKEYDOWN:
4820                 case WM_KEYDOWN:
4821                 {
4822                         if (process_keydown(wParam, lParam))
4823                                 return 0;
4824                         break;
4825                 }
4826
4827                 case WM_CHAR:
4828                 {
4829                         if (Term_no_press) Term_no_press = FALSE;
4830                         else Term_keypress(wParam);
4831                         return 0;
4832                 }
4833
4834                 case WM_LBUTTONDOWN:
4835                 {
4836                         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4837                         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4838                         mouse_down = TRUE;
4839                         oldx = mousex;
4840                         oldy = mousey;
4841                         return 0;
4842                 }
4843
4844                 case WM_LBUTTONUP:
4845                 {
4846                         HGLOBAL hGlobal;
4847                         LPSTR lpStr;
4848                         int i, j, sz;
4849                         int dx = abs(oldx - mousex) + 1;
4850                         int dy = abs(oldy - mousey) + 1;
4851                         int ox = (oldx > mousex) ? mousex : oldx;
4852                         int oy = (oldy > mousey) ? mousey : oldy;
4853
4854                         mouse_down = FALSE;
4855                         paint_rect = FALSE;
4856
4857 #ifdef JP
4858                         sz = (dx + 3) * dy;
4859 #else
4860                         sz = (dx + 2) * dy;
4861 #endif
4862                         hGlobal = GlobalAlloc(GHND, sz + 1);
4863                         if (hGlobal == NULL) return 0;
4864                         lpStr = (LPSTR)GlobalLock(hGlobal);
4865
4866                         for (i = 0; i < dy; i++)
4867                         {
4868 #ifdef JP
4869                                 char *s;
4870                                 char **scr = data[0].t.scr->c;
4871
4872                                 C_MAKE(s, (dx + 1), char);
4873                                 strncpy(s, &scr[oy + i][ox], dx);
4874
4875                                 if (ox > 0)
4876                                 {
4877                                         if (iskanji(scr[oy + i][ox - 1])) s[0] = ' ';
4878                                 }
4879
4880                                 if (ox + dx < data[0].cols)
4881                                 {
4882                                         if (iskanji(scr[oy + i][ox + dx - 1])) s[dx - 1] = ' ';
4883                                 }
4884
4885                                 for (j = 0; j < dx; j++)
4886                                 {
4887                                         if (s[j] == 127) s[j] = '#';
4888                                         *lpStr++ = s[j];
4889                                 }
4890 #else
4891                                 for (j = 0; j < dx; j++)
4892                                 {
4893                                         *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
4894                                 }
4895 #endif
4896                                 if (dy > 1)
4897                                 {
4898                                         *lpStr++ = '\r';
4899                                         *lpStr++ = '\n';
4900                                 }
4901                         }
4902
4903                         GlobalUnlock(hGlobal);
4904                         if (OpenClipboard(hWnd) == 0)
4905                         {
4906                                 GlobalFree(hGlobal);
4907                                 return 0;
4908                         }
4909                         EmptyClipboard();
4910                         SetClipboardData(CF_TEXT, hGlobal);
4911                         CloseClipboard();
4912
4913                         Term_redraw();
4914
4915                         return 0;
4916                 }
4917
4918                 case WM_MOUSEMOVE:
4919                 {
4920                         if (mouse_down)
4921                         {
4922                                 int dx, dy;
4923                                 int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4924                                 int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4925                                 int ox, oy;
4926
4927                                 if (paint_rect)
4928                                 {
4929                                         dx = abs(oldx - mousex) + 1;
4930                                         dy = abs(oldy - mousey) + 1;
4931                                         ox = (oldx > mousex) ? mousex : oldx;
4932                                         oy = (oldy > mousey) ? mousey : oldy;
4933                                         Term_inversed_area(hWnd, ox, oy, dx, dy);
4934                                 }
4935                                 else
4936                                 {
4937                                         paint_rect = TRUE;
4938                                 }
4939
4940                                 dx = abs(cx - mousex) + 1;
4941                                 dy = abs(cy - mousey) + 1;
4942                                 ox = (cx > mousex) ? mousex : cx;
4943                                 oy = (cy > mousey) ? mousey : cy;
4944                                 Term_inversed_area(hWnd, ox, oy, dx, dy);
4945
4946                                 oldx = cx;
4947                                 oldy = cy;
4948                         }
4949                         return 0;
4950                 }
4951
4952                 case WM_INITMENU:
4953                 {
4954                         setup_menus();
4955                         return 0;
4956                 }
4957
4958                 case WM_CLOSE:
4959                 {
4960                         if (game_in_progress && character_generated)
4961                         {
4962                                 if (!can_save)
4963                                 {
4964 #ifdef JP
4965                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
4966 #else
4967                                         plog("You may not do that right now.");
4968 #endif
4969                                         return 0;
4970                                 }
4971
4972                                 /* Hack -- Forget messages */
4973                                 msg_flag = FALSE;
4974
4975                                 forget_lite();
4976                                 forget_view();
4977                                 clear_mon_lite();
4978
4979                                 /* Save the game */
4980 #ifdef ZANGBAND
4981                                 /* do_cmd_save_game(FALSE); */
4982 #else /* ZANGBAND */
4983                                 /* do_cmd_save_game(); */
4984 #endif /* ZANGBAND */
4985                                 Term_key_push(SPECIAL_KEY_QUIT);
4986                                 return 0;
4987                         }
4988                         quit(NULL);
4989                         return 0;
4990                 }
4991
4992                 case WM_QUERYENDSESSION:
4993                 {
4994                         if (game_in_progress && character_generated)
4995                         {
4996                                 /* Hack -- Forget messages */
4997                                 msg_flag = FALSE;
4998
4999                                 /* Mega-Hack -- Delay death */
5000                                 if (p_ptr->chp < 0) p_ptr->is_dead = FALSE;
5001
5002 #ifdef JP
5003                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, "----¥²¡¼¥àÃæÃÇ----");
5004 #else
5005                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, "---- Save and Exit Game ----");
5006 #endif
5007
5008                                 /* Hardcode panic save */
5009                                 p_ptr->panic_save = 1;
5010
5011                                 /* Forbid suspend */
5012                                 signals_ignore_tstp();
5013
5014                                 /* Indicate panic save */
5015 #ifdef JP
5016                                 (void)strcpy(p_ptr->died_from, "(¶ÛµÞ¥»¡¼¥Ö)");
5017 #else
5018                                 (void)strcpy(p_ptr->died_from, "(panic save)");
5019 #endif
5020
5021                                 /* Panic save */
5022                                 (void)save_player();
5023                         }
5024                         quit(NULL);
5025                         return 0;
5026                 }
5027
5028                 case WM_QUIT:
5029                 {
5030                         quit(NULL);
5031                         return 0;
5032                 }
5033
5034                 case WM_COMMAND:
5035                 {
5036                         process_menus(LOWORD(wParam));
5037                         return 0;
5038                 }
5039
5040                 case WM_SIZE:
5041                 {
5042                         /* this message was sent before WM_NCCREATE */
5043                         if (!td) return 1;
5044
5045                         /* it was sent from inside CreateWindowEx */
5046                         if (!td->w) return 1;
5047
5048                         /* was sent from WM_SIZE */
5049                         if (td->size_hack) return 1;
5050
5051                         switch (wParam)
5052                         {
5053                                 case SIZE_MINIMIZED:
5054                                 {
5055                                         /* Hide sub-windows */
5056                                         for (i = 1; i < MAX_TERM_DATA; i++)
5057                                         {
5058                                                 if (data[i].visible) ShowWindow(data[i].w, SW_HIDE);
5059                                         }
5060                                         return 0;
5061                                 }
5062
5063                                 case SIZE_MAXIMIZED:
5064                                 {
5065                                         /* fall through XXX XXX XXX */
5066                                 }
5067
5068                                 case SIZE_RESTORED:
5069                                 {
5070                                         uint cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
5071                                         uint rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
5072
5073                                         /* New size */
5074                                         if ((td->cols != cols) || (td->rows != rows))
5075                                         {
5076                                                 /* Save the new size */
5077                                                 td->cols = cols;
5078                                                 td->rows = rows;
5079
5080                                                 if (!IsZoomed(td->w) && !IsIconic(td->w))
5081                                                 {
5082                                                         normsize.x = td->cols;
5083                                                         normsize.y = td->rows;
5084                                                 }
5085
5086                                                 /* Activate */
5087                                                 Term_activate(&td->t);
5088
5089                                                 /* Resize the term */
5090                                                 Term_resize(td->cols, td->rows);
5091
5092                                                 /* Redraw later */
5093                                                 InvalidateRect(td->w, NULL, TRUE);
5094                                         }
5095
5096                                         td->size_hack = TRUE;
5097
5098                                         /* Show sub-windows */
5099                                         for (i = 1; i < MAX_TERM_DATA; i++)
5100                                         {
5101                                                 if (data[i].visible) ShowWindow(data[i].w, SW_SHOW);
5102                                         }
5103
5104                                         td->size_hack = FALSE;
5105
5106                                         return 0;
5107                                 }
5108                         }
5109                         break;
5110                 }
5111
5112                 case WM_PALETTECHANGED:
5113                 {
5114                         /* Ignore if palette change caused by itself */
5115                         if ((HWND)wParam == hWnd) return 0;
5116
5117                         /* Fall through... */
5118                 }
5119
5120                 case WM_QUERYNEWPALETTE:
5121                 {
5122                         if (!paletted) return 0;
5123
5124                         hdc = GetDC(hWnd);
5125
5126                         SelectPalette(hdc, hPal, FALSE);
5127
5128                         i = RealizePalette(hdc);
5129
5130                         /* if any palette entries changed, repaint the window. */
5131                         if (i) InvalidateRect(hWnd, NULL, TRUE);
5132
5133                         ReleaseDC(hWnd, hdc);
5134
5135                         return 0;
5136                 }
5137
5138                 case WM_ACTIVATE:
5139                 {
5140                         if (wParam && !HIWORD(lParam))
5141                         {
5142                                 /* Do something to sub-windows */
5143                                 for (i = 1; i < MAX_TERM_DATA; i++)
5144                                 {
5145                                         if (!data[i].posfix) term_window_pos(&data[i], hWnd);
5146                                 }
5147
5148                                 /* Focus on main window */
5149                                 SetFocus(hWnd);
5150
5151                                 return 0;
5152                         }
5153
5154                         break;
5155                 }
5156
5157                 case WM_ACTIVATEAPP:
5158                 {
5159                         if (IsIconic(td->w)) break;
5160
5161                         for (i = 1; i < MAX_TERM_DATA; i++)
5162                         {
5163                                 if(data[i].visible)
5164                                 {
5165                                         if (wParam == TRUE)
5166                                         {
5167                                                 ShowWindow(data[i].w, SW_SHOW);
5168                                         }
5169                                         else
5170                                         {
5171                                                 ShowWindow(data[i].w, SW_HIDE);
5172                                         }
5173                                 }
5174                         }
5175                 }
5176         }
5177
5178         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5179 }
5180
5181
5182 #ifdef __MWERKS__
5183 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5184                                            WPARAM wParam, LPARAM lParam);
5185 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5186                                            WPARAM wParam, LPARAM lParam)
5187 #else /* __MWERKS__ */
5188 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5189                                            WPARAM wParam, LPARAM lParam)
5190 #endif /* __MWERKS__ */
5191 {
5192         term_data *td;
5193 #if 0
5194         MINMAXINFO FAR *lpmmi;
5195         RECT rc;
5196 #endif
5197         PAINTSTRUCT ps;
5198         HDC hdc;
5199         int i;
5200
5201
5202         /* Acquire proper "term_data" info */
5203         td = (term_data *)GetWindowLong(hWnd, 0);
5204
5205         /* Process message */
5206         switch (uMsg)
5207         {
5208                 /* XXX XXX XXX */
5209                 case WM_NCCREATE:
5210                 {
5211                         SetWindowLong(hWnd, 0, (LONG)(my_td));
5212                         break;
5213                 }
5214
5215                 /* XXX XXX XXX */
5216                 case WM_CREATE:
5217                 {
5218                         return 0;
5219                 }
5220
5221                 case WM_GETMINMAXINFO:
5222                 {
5223                         MINMAXINFO FAR *lpmmi;
5224                         RECT rc;
5225
5226                         lpmmi = (MINMAXINFO FAR *)lParam;
5227
5228                         /* this message was sent before WM_NCCREATE */
5229                         if (!td) return 1;
5230
5231                         /* Minimum window size is 80x24 */
5232                         rc.left = rc.top = 0;
5233                         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
5234                         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
5235
5236                         /* Adjust */
5237                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
5238
5239                         /* Save minimum size */
5240                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
5241                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
5242
5243                         return 0;
5244                 }
5245
5246                 case WM_SIZE:
5247                 {
5248                         uint cols;
5249                         uint rows;
5250                         
5251                         /* this message was sent before WM_NCCREATE */
5252                         if (!td) return 1;
5253
5254                         /* it was sent from inside CreateWindowEx */
5255                         if (!td->w) return 1;
5256
5257                         /* was sent from inside WM_SIZE */
5258                         if (td->size_hack) return 1;
5259
5260                         td->size_hack = TRUE;
5261
5262                         cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
5263                         rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
5264
5265                         /* New size */
5266                         if ((td->cols != cols) || (td->rows != rows))
5267                         {
5268                                 /* Save old term */
5269                                 term *old_term = Term;
5270
5271                                 /* Save the new size */
5272                                 td->cols = cols;
5273                                 td->rows = rows;
5274
5275                                 /* Activate */
5276                                 Term_activate(&td->t);
5277
5278                                 /* Resize the term */
5279                                 Term_resize(td->cols, td->rows);
5280
5281                                 /* Activate */
5282                                 Term_activate(old_term);
5283
5284                                 /* Redraw later */
5285                                 InvalidateRect(td->w, NULL, TRUE);
5286
5287                                 /* HACK - Redraw all windows */
5288                                 p_ptr->window = 0xFFFFFFFF;
5289                                 window_stuff();
5290                         }
5291
5292                         td->size_hack = FALSE;
5293
5294                         return 0;
5295                 }
5296
5297                 case WM_PAINT:
5298                 {
5299                         BeginPaint(hWnd, &ps);
5300                         if (td) term_data_redraw(td);
5301                         EndPaint(hWnd, &ps);
5302                         return 0;
5303                 }
5304
5305                 case WM_SYSKEYDOWN:
5306                 case WM_KEYDOWN:
5307                 {
5308                         if (process_keydown(wParam, lParam))
5309                                 return 0;
5310                         break;
5311                 }
5312
5313                 case WM_CHAR:
5314                 {
5315                         if (Term_no_press) Term_no_press = FALSE;
5316                         else Term_keypress(wParam);
5317                         return 0;
5318                 }
5319
5320                 case WM_PALETTECHANGED:
5321                 {
5322                         /* ignore if palette change caused by itself */
5323                         if ((HWND)wParam == hWnd) return FALSE;
5324                         /* otherwise, fall through!!! */
5325                 }
5326
5327                 case WM_QUERYNEWPALETTE:
5328                 {
5329                         if (!paletted) return 0;
5330                         hdc = GetDC(hWnd);
5331                         SelectPalette(hdc, hPal, FALSE);
5332                         i = RealizePalette(hdc);
5333                         /* if any palette entries changed, repaint the window. */
5334                         if (i) InvalidateRect(hWnd, NULL, TRUE);
5335                         ReleaseDC(hWnd, hdc);
5336                         return 0;
5337                 }
5338
5339                 case WM_NCLBUTTONDOWN:
5340                 {
5341
5342 #ifdef HTCLOSE
5343                         if (wParam == HTCLOSE) wParam = HTSYSMENU;
5344 #endif /* HTCLOSE */
5345
5346                         if (wParam == HTSYSMENU)
5347                         {
5348                                 if (td->visible)
5349                                 {
5350                                         td->visible = FALSE;
5351                                         ShowWindow(td->w, SW_HIDE);
5352                                 }
5353
5354                                 return 0;
5355                         }
5356
5357                         break;
5358                 }
5359         }
5360
5361         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5362 }
5363
5364
5365 #ifdef USE_SAVER
5366
5367 #define MOUSE_SENS 40
5368
5369 #ifdef __MWERKS__
5370 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5371                                     WPARAM wParam, LPARAM lParam);
5372 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5373                                     WPARAM wParam, LPARAM lParam)
5374 #else /* __MWERKS__ */
5375 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5376                                             WPARAM wParam, LPARAM lParam)
5377 #endif /* __MWERKS__ */
5378 {
5379         static int iMouse = 0;
5380         static WORD xMouse = 0;
5381         static WORD yMouse = 0;
5382
5383         int dx, dy;
5384
5385
5386         /* Process */
5387         switch (uMsg)
5388         {
5389                 /* XXX XXX XXX */
5390                 case WM_NCCREATE:
5391                 {
5392                         break;
5393                 }
5394
5395                 case WM_SETCURSOR:
5396                 {
5397                         SetCursor(NULL);
5398                         return 0;
5399                 }
5400
5401 #if 0
5402                 case WM_ACTIVATE:
5403                 {
5404                         if (LOWORD(wParam) == WA_INACTIVE) break;
5405
5406                         /* else fall through */
5407                 }
5408 #endif
5409
5410                 case WM_LBUTTONDOWN:
5411                 case WM_MBUTTONDOWN:
5412                 case WM_RBUTTONDOWN:
5413                 case WM_KEYDOWN:
5414                 {
5415                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5416                         return 0;
5417                 }
5418
5419                 case WM_MOUSEMOVE:
5420                 {
5421                         if (iMouse)
5422                         {
5423                                 dx = LOWORD(lParam) - xMouse;
5424                                 dy = HIWORD(lParam) - yMouse;
5425
5426                                 if (dx < 0) dx = -dx;
5427                                 if (dy < 0) dy = -dy;
5428
5429                                 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
5430                                 {
5431                                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5432                                 }
5433                         }
5434
5435                         /* Save last location */
5436                         iMouse = 1;
5437                         xMouse = LOWORD(lParam);
5438                         yMouse = HIWORD(lParam);
5439
5440                         return 0;
5441                 }
5442
5443                 case WM_CLOSE:
5444                 {
5445                         DestroyWindow(hwndSaver);
5446                         hwndSaver = NULL;
5447                         return 0;
5448                 }
5449         }
5450
5451         /* Oops */
5452         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5453 }
5454
5455 #endif /* USE_SAVER */
5456
5457
5458
5459
5460
5461 /*** Temporary Hooks ***/
5462
5463
5464 /*
5465  * Display warning message (see "z-util.c")
5466  */
5467 static void hack_plog(cptr str)
5468 {
5469         /* Give a warning */
5470         if (str)
5471         {
5472 #ifdef JP
5473                 MessageBox(NULL, str, "·Ù¹ð¡ª",
5474                            MB_ICONEXCLAMATION | MB_OK);
5475 #else
5476                 MessageBox(NULL, str, "Warning",
5477                            MB_ICONEXCLAMATION | MB_OK);
5478 #endif
5479
5480         }
5481 }
5482
5483
5484 /*
5485  * Display error message and quit (see "z-util.c")
5486  */
5487 static void hack_quit(cptr str)
5488 {
5489         /* Give a warning */
5490         if (str)
5491         {
5492 #ifdef JP
5493                 MessageBox(NULL, str, "¥¨¥é¡¼¡ª",
5494                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5495 #else
5496                 MessageBox(NULL, str, "Error",
5497                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5498 #endif
5499
5500         }
5501
5502         /* Unregister the classes */
5503         UnregisterClass(AppName, hInstance);
5504
5505         /* Destroy the icon */
5506         if (hIcon) DestroyIcon(hIcon);
5507
5508         /* Exit */
5509         exit(0);
5510 }
5511
5512
5513
5514 /*** Various hooks ***/
5515
5516
5517 /*
5518  * Display warning message (see "z-util.c")
5519  */
5520 static void hook_plog(cptr str)
5521 {
5522         /* Warning */
5523         if (str)
5524         {
5525 #ifdef JP
5526                 MessageBox(data[0].w, str, "·Ù¹ð¡ª",
5527                            MB_ICONEXCLAMATION | MB_OK);
5528 #else
5529                 MessageBox(data[0].w, str, "Warning",
5530                            MB_ICONEXCLAMATION | MB_OK);
5531 #endif
5532
5533         }
5534 }
5535
5536
5537 /*
5538  * Display error message and quit (see "z-util.c")
5539  */
5540 static void hook_quit(cptr str)
5541 {
5542         int i;
5543
5544
5545         /* Give a warning */
5546         if (str)
5547         {
5548 #ifdef JP
5549                 MessageBox(data[0].w, str, "¥¨¥é¡¼¡ª",
5550                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5551 #else
5552                 MessageBox(data[0].w, str, "Error",
5553                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5554 #endif
5555
5556         }
5557
5558
5559         /* Save the preferences */
5560         save_prefs();
5561
5562
5563         /*** Could use 'Term_nuke_win()' XXX XXX XXX */
5564
5565         /* Destroy all windows */
5566         for (i = MAX_TERM_DATA - 1; i >= 0; --i)
5567         {
5568                 term_force_font(&data[i], NULL);
5569                 if (data[i].font_want) string_free(data[i].font_want);
5570                 if (data[i].w) DestroyWindow(data[i].w);
5571                 data[i].w = 0;
5572         }
5573
5574         /* Free the bitmap stuff */
5575 #ifdef USE_GRAPHICS
5576         if (infGraph.hPalette) DeleteObject(infGraph.hPalette);
5577         if (infGraph.hBitmap) DeleteObject(infGraph.hBitmap);
5578
5579         if (infMask.hPalette) DeleteObject(infMask.hPalette);
5580         if (infMask.hBitmap) DeleteObject(infMask.hBitmap);
5581
5582 #endif /* USE_GRAPHICS */
5583
5584         /*** Free some other stuff ***/
5585
5586         DeleteObject(hbrYellow);
5587
5588         /* bg */
5589         delete_bg();
5590
5591         if (hPal) DeleteObject(hPal);
5592
5593         UnregisterClass(AppName, hInstance);
5594
5595         if (hIcon) DestroyIcon(hIcon);
5596
5597         exit(0);
5598 }
5599
5600
5601
5602 /*** Initialize ***/
5603
5604
5605 /*
5606  * Init some stuff
5607  */
5608 static void init_stuff(void)
5609 {
5610         int i;
5611
5612         char path[1024];
5613
5614
5615         /* Get program name with full path */
5616         GetModuleFileName(hInstance, path, 512);
5617
5618         /* Save the "program name" XXX XXX XXX */
5619         argv0 = path;
5620
5621         /* Get the name of the "*.ini" file */
5622         strcpy(path + strlen(path) - 4, ".INI");
5623
5624         /* Save the the name of the ini-file */
5625         ini_file = string_make(path);
5626
5627         /* Analyze the path */
5628         i = strlen(path);
5629
5630         /* Get the path */
5631         for (; i > 0; i--)
5632         {
5633                 if (path[i] == '\\')
5634                 {
5635                         /* End of path */
5636                         break;
5637                 }
5638         }
5639
5640         /* Add "lib" to the path */
5641         strcpy(path + i + 1, "lib\\");
5642
5643         /* Validate the path */
5644         validate_dir(path, TRUE);
5645
5646         /* Init the file paths */
5647         init_file_paths(path);
5648
5649         /* Hack -- Validate the paths */
5650         validate_dir(ANGBAND_DIR_APEX, FALSE);
5651         validate_dir(ANGBAND_DIR_BONE, FALSE);
5652
5653         /* Allow missing 'edit' directory */
5654         if (!check_dir(ANGBAND_DIR_EDIT))
5655         {
5656                 /* Must have 'data'! */
5657                 validate_dir(ANGBAND_DIR_DATA, TRUE);
5658         }
5659         else
5660         {
5661                 /* Don't need 'data' */
5662                 validate_dir(ANGBAND_DIR_DATA, FALSE);
5663         }
5664
5665         validate_dir(ANGBAND_DIR_FILE, TRUE);
5666         validate_dir(ANGBAND_DIR_HELP, FALSE);
5667         validate_dir(ANGBAND_DIR_INFO, FALSE);
5668         validate_dir(ANGBAND_DIR_PREF, TRUE);
5669         validate_dir(ANGBAND_DIR_SAVE, FALSE);
5670         validate_dir(ANGBAND_DIR_USER, TRUE);
5671         validate_dir(ANGBAND_DIR_XTRA, TRUE);
5672
5673         /* Build the filename */
5674 #ifdef JP
5675         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news_j.txt");
5676 #else
5677         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news.txt");
5678 #endif
5679
5680
5681         /* Hack -- Validate the "news.txt" file */
5682         validate_file(path);
5683
5684
5685 #if 0 /* #ifndef JP */
5686         /* Build the "font" path */
5687         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "font");
5688
5689         /* Allocate the path */
5690         ANGBAND_DIR_XTRA_FONT = string_make(path);
5691
5692         /* Validate the "font" directory */
5693         validate_dir(ANGBAND_DIR_XTRA_FONT, TRUE);
5694
5695         /* Build the filename */
5696         path_build(path, sizeof(path), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
5697
5698         /* Hack -- Validate the basic font */
5699         validate_file(path);
5700 #endif
5701
5702
5703 #ifdef USE_GRAPHICS
5704
5705         /* Build the "graf" path */
5706         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
5707
5708         /* Allocate the path */
5709         ANGBAND_DIR_XTRA_GRAF = string_make(path);
5710
5711         /* Validate the "graf" directory */
5712         validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
5713
5714 #endif /* USE_GRAPHICS */
5715
5716
5717 #ifdef USE_SOUND
5718
5719         /* Build the "sound" path */
5720         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
5721
5722         /* Allocate the path */
5723         ANGBAND_DIR_XTRA_SOUND = string_make(path);
5724
5725         /* Validate the "sound" directory */
5726         validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
5727
5728 #endif /* USE_SOUND */
5729
5730 #ifdef USE_MUSIC
5731
5732         /* Build the "music" path */
5733         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
5734
5735         /* Allocate the path */
5736         ANGBAND_DIR_XTRA_MUSIC = string_make(path);
5737
5738         /* Validate the "music" directory */
5739         validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
5740
5741 #endif /* USE_MUSIC */
5742
5743         /* Build the "help" path */
5744         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "help");
5745
5746         /* Allocate the path */
5747         ANGBAND_DIR_XTRA_HELP = string_make(path);
5748
5749         /* Validate the "help" directory */
5750         /* validate_dir(ANGBAND_DIR_XTRA_HELP); */
5751 }
5752
5753 bool is_already_running()
5754 {
5755         bool result = FALSE;
5756         HANDLE hMutex;
5757
5758         hMutex = CreateMutex(NULL, TRUE, VERSION_NAME);
5759         if (GetLastError() == ERROR_ALREADY_EXISTS)
5760         {
5761                 result = TRUE;
5762         }
5763         return result;
5764 }
5765
5766
5767 int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
5768                        LPSTR lpCmdLine, int nCmdShow)
5769 {
5770         int i;
5771
5772         WNDCLASS wc;
5773         HDC hdc;
5774         MSG msg;
5775
5776         setlocale( LC_ALL, "ja_JP" );
5777
5778         /* Unused */
5779         (void)nCmdShow;
5780
5781         /* Save globally */
5782         hInstance = hInst;
5783         
5784         
5785         /* Prevent multiple run */
5786         if (is_already_running())
5787         {
5788                 MessageBox(NULL,
5789                                 _("ÊѶòÈÚÅܤϤ¹¤Ç¤Ëµ¯Æ°¤·¤Æ¤¤¤Þ¤¹¡£", "Hengband is already running."), 
5790                                 _("¥¨¥é¡¼¡ª", "Error") ,
5791                                 MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5792                 return FALSE;
5793         }
5794
5795         /* Initialize */
5796         if (hPrevInst == NULL)
5797         {
5798                 wc.style         = CS_CLASSDC;
5799                 wc.lpfnWndProc   = AngbandWndProc;
5800                 wc.cbClsExtra    = 0;
5801                 wc.cbWndExtra    = 4; /* one long pointer to term_data */
5802                 wc.hInstance     = hInst;
5803                 wc.hIcon         = hIcon = LoadIcon(hInst, AppName);
5804                 wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
5805                 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
5806                 wc.lpszMenuName  = AppName;
5807                 wc.lpszClassName = AppName;
5808
5809                 if (!RegisterClass(&wc)) exit(1);
5810
5811                 wc.lpfnWndProc   = AngbandListProc;
5812                 wc.lpszMenuName  = NULL;
5813                 wc.lpszClassName = AngList;
5814
5815                 if (!RegisterClass(&wc)) exit(2);
5816
5817 #ifdef USE_SAVER
5818
5819                 wc.style          = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
5820                 wc.lpfnWndProc    = AngbandSaverProc;
5821                 wc.hCursor        = NULL;
5822                 wc.lpszMenuName   = NULL;
5823                 wc.lpszClassName  = "WindowsScreenSaverClass";
5824
5825                 if (!RegisterClass(&wc)) exit(3);
5826
5827 #endif
5828
5829         }
5830
5831         /* Temporary hooks */
5832         plog_aux = hack_plog;
5833         quit_aux = hack_quit;
5834         core_aux = hack_quit;
5835
5836         /* Prepare the filepaths */
5837         init_stuff();
5838
5839         /* Initialize the keypress analyzer */
5840         for (i = 0; special_key_list[i]; ++i)
5841         {
5842                 special_key[special_key_list[i]] = TRUE;
5843         }
5844         /* Initialize the keypress analyzer */
5845         for (i = 0; ignore_key_list[i]; ++i)
5846         {
5847                 ignore_key[ignore_key_list[i]] = TRUE;
5848         }
5849
5850         /* Determine if display is 16/256/true color */
5851         hdc = GetDC(NULL);
5852         colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
5853         paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? TRUE : FALSE);
5854         ReleaseDC(NULL, hdc);
5855
5856         /* Initialize the colors */
5857         for (i = 0; i < 256; i++)
5858         {
5859                 byte rv, gv, bv;
5860
5861                 /* Extract desired values */
5862                 rv = angband_color_table[i][1];
5863                 gv = angband_color_table[i][2];
5864                 bv = angband_color_table[i][3];
5865
5866                 /* Extract the "complex" code */
5867                 win_clr[i] = PALETTERGB(rv, gv, bv);
5868
5869                 /* Save the "simple" code */
5870                 angband_color_table[i][0] = win_pal[i];
5871         }
5872
5873         /* Prepare the windows */
5874         init_windows();
5875
5876         /* bg */
5877         init_bg();
5878
5879         /* Activate hooks */
5880         plog_aux = hook_plog;
5881         quit_aux = hook_quit;
5882         core_aux = hook_quit;
5883
5884         /* Set the system suffix */
5885         ANGBAND_SYS = "win";
5886
5887         /* Set the keyboard suffix */
5888         if (7 != GetKeyboardType(0))
5889                 ANGBAND_KEYBOARD = "0";
5890         else
5891         {
5892                 /* Japanese keyboard */
5893                 switch (GetKeyboardType(1))
5894                 {
5895                 case 0x0D01: case 0x0D02:
5896                 case 0x0D03: case 0x0D04:
5897                 case 0x0D05: case 0x0D06:
5898                         /* NEC PC-98x1 */
5899                         ANGBAND_KEYBOARD = "NEC98";
5900                         break;
5901                 default:
5902                         /* PC/AT */
5903                         ANGBAND_KEYBOARD = "JAPAN";
5904                 }
5905         }
5906
5907         /* Catch nasty signals */
5908         signals_init();
5909
5910         /* Initialize */
5911         init_angband();
5912
5913         /* We are now initialized */
5914         initialized = TRUE;
5915 #ifdef CHUUKEI
5916         if(lpCmdLine[0] == '-'){
5917           switch(lpCmdLine[1])
5918           {
5919           case 'p':
5920           case 'P':
5921             {
5922               if (!lpCmdLine[2]) break;
5923               chuukei_server = TRUE;
5924               if(connect_chuukei_server(&lpCmdLine[2])<0){
5925                 msg_print("connect fail");
5926                 return 0;
5927               }
5928               msg_print("connect");
5929               msg_print(NULL);
5930               break;
5931             }
5932
5933           case 'c':
5934           case 'C':
5935             {
5936               if (!lpCmdLine[2]) break;
5937               chuukei_client = TRUE;
5938               connect_chuukei_server(&lpCmdLine[2]);
5939               play_game(FALSE);
5940               quit(NULL);
5941               return 0;
5942             }
5943           case 'X':
5944           case 'x':
5945             {
5946               if (!lpCmdLine[2]) break;
5947               prepare_browse_movie(&lpCmdLine[2]);
5948               play_game(FALSE);
5949               quit(NULL);
5950               return 0;
5951             }
5952           }
5953         }
5954 #endif
5955
5956 #ifdef CHUUKEI
5957         /* Did the user double click on a save file? */
5958         if(!chuukei_server) check_for_save_file(lpCmdLine);
5959 #else
5960         /* Did the user double click on a save file? */
5961         check_for_save_file(lpCmdLine);
5962 #endif
5963
5964         /* Prompt the user */
5965 #ifdef JP
5966         prt("[¥Õ¥¡¥¤¥ë] ¥á¥Ë¥å¡¼¤Î [¿·µ¬] ¤Þ¤¿¤Ï [³«¤¯] ¤òÁªÂò¤·¤Æ¤¯¤À¤µ¤¤¡£", 23, 8);
5967 #else
5968         prt("[Choose 'New' or 'Open' from the 'File' menu]", 23, 17);
5969 #endif
5970
5971         Term_fresh();
5972
5973         /* Process messages forever */
5974         while (GetMessage(&msg, NULL, 0, 0))
5975         {
5976                 TranslateMessage(&msg);
5977                 DispatchMessage(&msg);
5978         }
5979
5980         /* Paranoia */
5981         quit(NULL);
5982
5983         /* Paranoia */
5984         return (0);
5985 }
5986
5987
5988 #endif /* WINDOWS */
5989